[Haskell-beginners] Constructor classes and type classes.

PATRICK BROWNE patrick.browne at dit.ie
Sun Oct 30 20:56:12 UTC 2016


{-
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.html>


More information about the Beginners mailing list