[m-dev.] Typeclasses and constructor classes.

David Glen JEFFERY dgj at students.cs.mu.oz.au
Wed Sep 20 21:52:44 AEDT 2000


On 20-Sep-2000, Ralph Becket <rbeck at microsoft.com> wrote:
> I've worked out what it is about typeclasses that has been causing me
> so much grief and it's the lack of constructor classes.
>
> [good examples of why we need constructor classes deleted]
> 
> :- typeclass sequence(S(T)) where [
> 	func empty = S(T),
> 	func cons(T, S(T)) = S(T),
> 	func head(S(T)) = T,
> 	func tail(S(T)) = S(T),
> 	...
> ].

This is more likely to be what is allowed:

:- typeclass sequence(S, T) where [
	... % same method types
].

> and
> 
> :- instance sequence(list(T)).

...becomes

:- instance sequence(list, T).

I'm sure that sent off alarm bells in a few people's heads. The instance
I've written above wouldn't be accepted according to Mercury's current rules
for ensuring that instances do not overlap. (ie. top level type constructors
must be bound).

This is a very common situation when trying to model things with multi
parameter constructor classes. Our simple rule for ensuring non-overlapping
instances (which is overly restrictive for a lot of things you'd like to
be able to do with plain old vanilla flavoured type classes) is really *way* 
off the mark when it comes to (multi parameter) constructor classes.

This turned out to be such a problem for the Haskell people that `functional
dependencies' were born. Check out
	http://www.cse.ogi.edu/~mpj/fds.html
for an overview. There was also a paper published on this earlier in the
year... I don't have a reference handy though, sorry.

> Without constructor classes typeclasses are severely
> restricted.  Is anyone working on adding constructor
> classes and, if so, have we got an ETA?  It would seem like
> a fairly high priority to me.  

I have a partly complete implementation sitting in one of my workspaces. A
fair bit of the analysis is done, but it is going to take quite a bit of work 
to get it to the point of working properly. (Amongst other things, it will
require a fair bit of work on the RTTI, and a lot of the kind inference still
needs to be done).

I've just started a new job working on the HAL project, so I'm not sure how
much time I'm going to be able to spend on it. It is something that I'd like
to complete for my th**** , though.

(We are making quite a bit of use of type classes in HAL, particularly as
a way of modelling constraint solvers (a `plug and play' interface). I've
got this sneaking suspicion that very, very soon we are going to realise that
we need constructor classes to do what we're trying to do... in which case
I will probably wind up doing the implementation anyway, both in HAL
and in our target language Mercury).

I'll keep you posted...


dgj
-- 
David Jeffery (dgj at cs.mu.oz.au) | If your thesis is utterly vacuous
PhD student,                    | Use first-order predicate calculus.
Dept. of Comp. Sci. & Soft. Eng.|     With sufficient formality
The University of Melbourne     |     The sheerist banality
Australia                       | Will be hailed by the critics: "Miraculous!"
                                |     -- Anon.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list