[Haskell-beginners] Constructor classes and type classes.

Dimitri DeFigueiredo defigueiredo at ucdavis.edu
Thu Nov 3 15:43:33 UTC 2016


Hi Patrick,

I am not sure I understand what you are trying to do, but here are some
thoughts.

- your use of the names `typeVar` and `typeCons` make total sense to me.
But your name `dataCons` in the definition of SetClass2 does not. You
are only talking about types here. All you are doing is talking about
*type* constructors. You are not constructing a specific value of that
type. Maybe one way to see the difference between the type constructors
and the data constructors is to change your first line to:

data SetType typeVar = SetTypeDataConstructor [typeVar] deriving Show

- I also don't understand why you named your type variable
`dataVariable` instead of sticking to `typeVar` in SetClass3. It is a
*type* variable in the type signature of the functions in the type class.

- I don't understand what you mean by a "constructor class". My simple
understanding of what a type constructors and data constructors are is here
https://wiki.haskell.org/Constructor#Type_constructor.

- I don't see the terms super class and sub class ever used in haskell
land. Although I do understand they make sense, I think they are really
OO terms. I've tried to map OO concepts into haskell and there are many
different ways to do it. The best description of all those ways that I
found was in Oleg Kiselyov's work
https://arxiv.org/pdf/cs/0509027.pdf

- I think parametric polymorphism as used in haskell forces us to
compare types in terms of equality constraints (this type is "the same"
as that), whereas subtype polymorphism as used in OO languages forces us
to think in terms of inequalities (this type is "more specific" than
that). I think those force fundamentally different ways to think about
and model your problems. I would encourage you to use equality as much
as possible, but if you want to model subtyping in haskell, I think
Oleg's work above has a few approaches that I think are better than type
classes. In particular, in OO there was always an ambiguity between
modelling subtyping with inheritance or with a "has a" relationship when
the subclass explicitly contains an object of the superclass. You can
use the latter idea in haskell, Oleg's work has a good way to do it.

Hope this helps.

Cheers,

Dimitri


> From: PATRICK BROWNE <patrick.browne at dit.ie>
> To: The Haskell-Beginners Mailing List - Discussion of primarily
> 	beginner-level topics related to Haskell <beginners at haskell.org>
> Subject: [Haskell-beginners] Constructor classes and type classes.
> Message-ID:
> 	<CAGFLrKfQVaQQJybMTn-pJksV1VKUXrmBks14Jkm==Zqt=bdy5A at 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]))
>

-- 
2E45 D376 A744 C671 5100 A261 210B 8461 0FB0 CA1F


-- 
2E45 D376 A744 C671 5100 A261 210B 8461 0FB0 CA1F


More information about the Beginners mailing list