[mercury-users] constructor class syntax

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Nov 3 00:40:57 AEDT 1999


On 02-Nov-1999, Michael Day <mikeday at corplink.com.au> wrote:
> 
> making the assumption that Mercury will have support for constructor
> classes in the not impossibly distant future, would it be reasonable to
> expect syntax roughly like this:
> 
> :- class mappable(F(T)) where
> 	[
> 		pred map(pred(X, Y), F(A), F(B)),
> 		mode map(in, in, out) is det
> 	].
> 
> :- instance mappable(list(T)) where
> 	[
> 		pred(map/3) is list__map
> 	].

With the exception of the `F(T)' part and the use of `:- class' rather
than `:- typeclass', that looks reasonable to me at first glance.
But you have probably spent more time thinking about this than any of
the rest of us.

> I'm assuming here that type classes are merely a special case of
> constructor classes and that both can be declared in a single unified
> syntax. With that in mind it seems easier to combine :- typeclass and a
> hypothetical :- constructorclass into :- class, and save typing all round.

The classes in question are type classes and type constructor classes
(note that constructor here refers to a _type_ constructor).
In fact it would make perfect sense to have one parameter being a type
and another being a type class, so it would be a bad idea to separate
these two notions.

However, I still think the declaration should be `:- typeclass'
rather than `:- class'.  The Haskell `class' declaration often
confuses beginners, who think it declares a class of objects,
i.e. a type, rather than declaring a class of types.
This is particularly common amongst people who have learnt C++ or Java
first ;-).  The `:- typeclass' notation emphases that the concept is
quite different from C++ or Java classes.

> It seems constraints on constructors as with normal type classes would be
> useful, but what about constraints on constructor arguments?
> 
> :- class mappable(F(T)) <= container(F) where
> 	[
> 		pred map(pred(X, Y), F(A), F(B)),
> 		mode map(in, in, out) is det
> 	].
> 
> I can't see any use of expressing constraints on T, at this point. Would
> it in fact be desirable to rephrase the declaration as:
> 
> :- class mappable(F/1) ...

Yes, that makes more sense.  Or we could perhaps take the Haskell
approach of inferring the kind from the context, and just use the
syntax `:- typeclass mappable(F)', with the arity being inferred
from the occurrences of `F(A)' and `F(B)' in the body.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list