Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Constructor classes and type classes. (PATRICK BROWNE)


----------------------------------------------------------------------

Message: 1
Date: Sun, 30 Oct 2016 20:56:12 +0000
From: PATRICK BROWNE <patrick.bro...@dit.ie>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] Constructor classes and type classes.
Message-ID:
        <CAGFLrKfQVaQQJybMTn-pJksV1VKUXrmBks14Jkm==Zqt=bd...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

{-
I am trying to understand constructor classes and their relationship with
ordinary type classes.
I wrote the code below to help me understand the distinction. The code is
only for explanatory purposes, set operations use a tuple syntax.

I use the naming convention of 'typeVar', 'typeCons', and 'dataCons' for
type variables, type constructors, and data constructors respectively.
Question1:
Is my naming convention correct?
I am particularly concerned about SetClass3 where the super class seems to
use a type constructor but the subclass seems to use the same term as a
data constructor.

Q2:
In this case constructor classes and type classes seem to provide similar
functionality.
In general what situation are each best suited?
-}

import Data.List
data SetType typeVar = SetType [typeVar] deriving Show


class SetClass1 typeCons where
  member1 ::   Eq typeVar  =>  (typeVar, typeCons typeVar)  -> Bool
  intersect1 ::  (Eq typeVar,Show typeVar) =>  (typeCons typeVar, typeCons
typeVar) -> typeCons typeVar


class  SetClass2 dataCons typeVar where
  member2 ::  (typeVar, (dataCons typeVar))  -> Bool
  intersect2 :: (dataCons typeVar, dataCons typeVar) -> dataCons typeVar


class SetClass1 typeCons => SetClass3  typeCons dataVariable where
 union3 ::  (Eq dataVariable,Show dataVariable) =>  (typeCons dataVariable,
typeCons dataVariable) -> typeCons dataVariable

instance SetClass1 SetType where
 member1 (x ,(SetType y)) = elem  x y
 intersect1 ((SetType x),(SetType y)) = SetType (intersect x y)

instance SetClass2 SetType Int where
 member2 (x ,SetType y) =   elem  x y
 intersect2 (SetType x,SetType y) = SetType (intersect x y)

instance SetClass3 SetType Int where
 union3 (SetType x,SetType y) = SetType (union x y)


test1a = member1 (1, (SetType [1,2]))
test1b = intersect1 ((SetType [1,3,4]),(SetType [1,2]))
test2a = member2 (1, (SetType [1::Int]))
test2b = intersect2 ((SetType [1::Int]), (SetType [(1::Int)]))
test3a = union3 ((SetType [1::Int,2::Int,3::Int]),(SetType [4::Int,5::Int]))

-- 


This email originated from DIT. If you received this email in error, please 
delete it from your system. Please note that if you are not the named 
addressee, disclosing, copying, distributing or taking any action based on 
the contents of this email or attachments is prohibited. www.dit.ie

Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí 
earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an 
seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon 
dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa 
ríomhphost nó sna hiatáin seo. www.dit.ie

Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to 
Grangegorman <http://www.dit.ie/grangegorman>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20161030/119ccdde/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 100, Issue 23
******************************************

Reply via email to