[m-dev.] Typeclasses and constructor classes.

Ralph Becket rbeck at microsoft.com
Thu Sep 21 20:19:19 AEDT 2000


>From Michael Day on 21/09/2000 06:55:10
> 
> > :- 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)).
> 
> Hmm, can I say
> 
> :- instance sequence(string, char).
> 
> or some equivalent?

If we adopt the rather excellent scheme in Mark Jones' "Type Classes 
and Functional Dependencies" (http://www.cse.ogi.edu/~mpj/fds.html),
then we could write (I'm making up some syntax here):

:- typeclass sequence(S, T) given S >> T where [
	func empty = S,
	func cons(T, S) = S,
	...
	func map(func(T) = U, S) = SU <= sequence(SU, U)
].

:- instance sequence(string, char) where [...].

I've replaced Jones' `|' with `given' and `->' with `>>'.  The
`given S >> T' in the typeclass declaration reads `given that
S uniquely determines T'.  Now our typing problems disappear:
empty/0 doesn't cause a problem by not mentioning T since T is
deducible from whatever S happens to be; map/2 also works out
nicely, since we can declare

:- instance sequence(list(T), T) where [...].

[I believe this is correct, anyway].

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