[m-dev.] Typeclasses and constructor classes.

Ralph Becket rbeck at microsoft.com
Wed Sep 20 20:28:55 AEDT 2000


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.

For example, consider  the class of sequence types S ranging over 
element types T - the necessary operations will include

:- typeclass sequence(S, T) where [
	func cons(T, S) = S,
	func head(S) = T,     % Assume an error is raised on S empty
	func tail(S) = T,     % Assume an error is raised on S empty
	func empty = S,
	...
].

The empty/0 func causes us some grief, since it doesn't mention T; instead
we need to jump through a hoop like

	func empty(T) = S,    % T is a dummy arg to make the types work
	mode empty(unused) = out is det,

which isn't very satisfying.  But things get worse: in an
instance declaration the parameters must all be of the form
`typename(typevar, ...)', and not just `typevar', which
rules out  

:- instance sequence(list(T), T).

Oh deary me!  What is needed, as was discussed at the end of last year,
is the ability to handle constructor classes, so we could write

:- 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),
	...
].

and

:- instance sequence(list(T)).

Moreover, we can now describe things like map/2 in the typeclass
definition:

	func map(func(T) = U, S(T)) = S(U) <= sequence(S(U))

which is impossible in the current scheme.

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.  

Ralph

--
Ralph Becket      |      MSR Cambridge      |      rbeck at microsoft.com 
--------------------------------------------------------------------------
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