[clean-list] About semantic restrictions on type classes.
Fabien Todescato
f.todescato@larisys.fr
Thu, 23 Jan 2003 13:30:21 +0100
Sjaak Smetsers writes :
So what?
Huh, sorry, I meant the 2nd case to be :
class k x t :: x ( t x ) -> t x
so that the instance :
instance k x t
has type x ( t x ) -> t x, a type that the 1st case class :
class k x y :: x y -> y
cannot be instanciated to because of the type flat semantic restriction.
That is :
class k x y :: x y -> y
instance k x ( t x )
is illegal, as you emphasized.
I don't see your point.
Well, I was designing for some concrete programming task a type class and
encountered the flat type restriction that would prevent me for
instanciating my type class as I wanted. After some head-scratching I
realized that I could have the right class instance provided I changed the
class definition and used a type constructor variable instead of a type
variable. This is what I wanted to illustrate in a more abstract settings.
I am feeling this may be a general trick-of-the-trade that Clean programmers
may want to be aware of :)
Best regards, and thank you for your patient explanations. (I feel kind of
stupid at times.)
Fabien Todescato
> -----Message d'origine-----
> De: Sjaak Smetsers [SMTP:sjakie@cs.kun.nl]
> Date: jeudi 23 janvier 2003 12:41
> À: Fabien Todescato
> Cc: clean-list@cs.kun.nl
> Objet: Re: [clean-list] About semantic restrictions on type
> classes.
>
> Dear Fabian,
>
> >In §6.11 of the Clean Language Report 2.1, it is said that :
> >
> > A type instance of an overloaded type must be a flat type ,
> i.e. a
> >type of the form T a1...an where ai are type variables which are all
> >different.
>
> In Clean we have the same restriction on instance types as in Haskell. I
> don't know whether this restriction is fundamental, what i do know is that
>
> the current implementation of Clean cannot be extended (easily) in such a
> way that it can deal with 'overlapping' instance types.
>
> >supposing I have the following :
> >
> >class k x y :: x y -> y
> >
> >The above-mentionned restriction prevents the definition of :
> >
> >instance k x ( t x )
> >
> >where t is some type constructor.
> >
> >Now, if the class k is instead defined as follows :
> >
> >class k x t y :: x ( t y ) -> t y
> >
> >I can write :
> >
> >instance k x t y.
>
> So what?
>
> In the first case your (illegal) instance would have type
>
> x ( t x ) -> ( t x )
>
> whereas in the second case it has type
>
> x ( t y ) -> t y
>
> Two different instances, so what are you trying to illustrate?
>
> >That is, using type-constructor classes may provide in some cases a
> >workaround to the limitations imposed by the 'flat type' semantic
> >restriction above.
>
> I don't see your point.
>
> Regards,
>
> Sjaak Smetsers