[mercury-users] variable parameters in typeclass instances

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Nov 5 16:55:25 AEDT 1999


On 04-Nov-1999, Juergen Stuber <juergen at mpi-sb.mpg.de> wrote:
>
> I think something like mode declarations for typeclasses would help here.
> With these we could state dependencies between the types of a typeclass,
> and checking for overlapping instances would be similar
> to determinism checking.
> Even the name mangling should work by ignoring "out" arguments.
> 
> Maybe this is related to the "functional dependencies" some
> Haskell people recently started talking about?

Yes.  I've been thinking about how that kind of thing might fit into
Mercury.

For predicates and functions, we use existential/universal type
quantifiers to indicate the modes of the type variables.
E.g.

	% here the type variable `T' has mode `out'
	:- some [T] pred p(T).

	% here the type variable `T' has mode `in'
	:- all [T] pred p(T).

So it might make sense to do the same kind of thing for type classes:

	% this indicates a function
	:- all [T1] some [T2]
	   typeclass foo(T1, T2) where [
		...
	].

However, it's not just modes that are needed, for functional
dependencies the determinism is important too.
Also using `all' doesn't seem right, because not every type
need be a member of a given typeclass.
So perhaps that is not such a good idea.

Another alternative would be to use function syntax.
You can consider a typeclass as a bit like a predicate on types;
a type class constraint `foo(T1, T2)' is true iff the specified
types are instances of that typeclass.  Analagously, we could
consider function-like type classes, which would be analagous
to (semidet) functions on types.

	:- typeclass foo(T1) = T2 where [
		...
	].

However, both of these suggestions would allow only one functional
dependency per typeclass.  I think Mark Jones' proposal for Haskell
allows multiple functional dependencies per typeclass.

-- 
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