Constructor classes and type classes.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Constructor classes and type classes.

Patrick Browne
{-
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


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Constructor classes and type classes.

Patrick Browne
My intended use of the term 'subclass' is as the Haskell report:
"class Num of numeric types is a subclass of Eq, since all numbers may be compared for equality"
Of course what I have written may not be consistent with the report.
Regards,
Pat

On 31 October 2016 at 09:26, Imants Cekusins <[hidden email]> wrote:
HelloPat,

I am not sure if terms superclass and subclass are applicable to Haskell classes.

Haskell class is very different from Java class. It is more like an interface.

;)


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


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Constructor classes and type classes.

Dimitri DeFigueiredo
In reply to this post by Patrick Browne
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 <[hidden email]>
> To: The Haskell-Beginners Mailing List - Discussion of primarily
> beginner-level topics related to Haskell <[hidden email]>
> Subject: [Haskell-beginners] Constructor classes and type classes.
> Message-ID:
> <CAGFLrKfQVaQQJybMTn-pJksV1VKUXrmBks14Jkm==Zqt=[hidden email]>
> 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
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Constructor classes and type classes.

Patrick Browne
Dimitri,
Thanks for taking the time and effort to respond. Your comments have clarified some of my confusion. My motivation is that I am trying to implement the code from a paper with the following data and classes:

{-# LANGUAGE MultiParamTypeClasses #-}
data TimeFacts t = Bef (t, t, TimeFacts t) | New

class SetClass t  where
class SetClass t => Time l t  where
class Time l t => TimeE l t where
class Time l t => TimeTO l t where

However I cannot get the instances to compile:
instance SetClass (TimeFacts t) where -- OK
instance (SetClass t) => Time TimeFacts t where -- Expecting one more argument to 'TimeFacts'
instance TimeE TimeFacts t where -- Expecting one more argument to 'TimeFacts'
instance TimeTO TimeFacts t where -- Expecting one more argument to 'TimeFacts'


Note, I am only using the headings for the classes and instances, which should be OK.
Any pointers to a solution would be much appreciated.

Regards,
Pat


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


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Constructor classes and type classes.

Imants Cekusins
this builds. not sure if the instances are as intended


{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
module SetClass where

data TimeFacts t = Bef (t, t, TimeFacts t) | New

class SetClass t  where
class SetClass t => Time l t  where
class Time l t => TimeE l t where
class Time l t => TimeTO l t where


instance SetClass (TimeFacts t) where -- OK
instance (SetClass t) => Time (TimeFacts t) t where
instance (SetClass t) => TimeE (TimeFacts t) t where
instance (SetClass t) => TimeTO (TimeFacts t) t where


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Constructor classes and type classes.

Patrick Browne
In reply to this post by Patrick Browne
Imants
Thanks for the sanity check on the instances.

I am concerned about the use of the function immsucc' from the same paper, which is defined in instances the Time class (but omitted from the class). The function  immsucc' is first used without arguments, which to me implies that it 'might' a function passed to the higher-order closure function. It is then used as just an ordinary function with arguments. I have found a version of closure based on Craft of Function Programming. which does not require a function as an argument.

So basically my question is: does the usage of immsucc' make sense in the code below?, do the types match up?
Regards,
Pat



data TimeFacts t = Bef (t, t, TimeFacts t) | New

class SetClass t => Time l t where
 new :: (t, t) -> l t
 bef :: (t, t, l t) -> l t
 succ , pred :: (t, l t) -> Set t
 immsucc , immpred :: (t, l t) -> Set t
 before, after :: (t, t, l t) -> Bool
 immedbefore, immedafter :: (t, t, l t) -> Bool
 immedafter (a, b, t) = immedbefore (b, a, t)
 after (a, b, t) = before (b, a, t)
 before (a, b, t) = memberInSet (b, succ (a, t))
 succ (a, tl) = closure ([a], immsucc' , tl)
 pred (a, tl) = closure ([a], immpred' , tl)
 immsucc (a, tl) = immsucc' ([a], [], tl)
 immpred (a, tl) = immpred' ([a], [], tl)

instance (SetClass t) => Time TimeFacts t where
 immsucc’ (a, s, New ) = s
 immsucc’ (a, s, Bef (e, f, tt)) = if memberInSet (e,a) then immsucc’ (a, insertInSet (f,s), tt) else immsucc’ (a, s,tt)


On 3 November 2016 at 18:05, Imants Cekusins <[hidden email]> wrote:
this builds. not sure if the instances are as intended


{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
module SetClass where

data TimeFacts t = Bef (t, t, TimeFacts t) | New

class SetClass t  where
class SetClass t => Time l t  where
class Time l t => TimeE l t where
class Time l t => TimeTO l t where


instance SetClass (TimeFacts t) where -- OK
instance (SetClass t) => Time (TimeFacts t) t where
instance (SetClass t) => TimeE (TimeFacts t) t where
instance (SetClass t) => TimeTO (TimeFacts t) t where


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



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


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners