[m-dev.] for review: tuples [1]

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Aug 2 12:00:38 AEST 2000


On 01-Aug-2000, Ralph Becket <rbeck at microsoft.com> wrote:
> > From: Fergus Henderson [mailto:fjh at cs.mu.OZ.AU]
> > 
> > From a language design stand-point, one disadvantage of tuples
> > is that it is difficult to write type class instance declarations
> > for them, because they are unbounded in arity, and there is no
> > way to write a single instance declaration which works for all
> > tuple types.
> 
> Hang on, surely {T1}, {T2, T2}, {T1, T2, T3} are all distinct
> types, each with a very definite arity?

Yes.  It's easy enough to write an instance definition for any one
of them.  But when you're writing an instance definition for one
of them, most often you will want to write an instance definition for
all of them.  And there's no way to write a finite set of instance
declarations that covers them all.

One possibility would be to add list-like features for tuples.
The type `{ T1 | T2 }' could denote a tuple type whose first
element has type T1 and whose remainder has type T2 (which must
be a tuple type).  The expression `{ X | Xs }' could match
a tuple whose first element is X and whose remainder is Xs
(which must have a tuple type).

Then you could write two instance declarations that would
cover all tuple types:

	:- typeclass fooable(T).
	:- instance fooable({}).
	:- instance fooable({T1 | T2}) <= fooable(T2).

For example, you might have

	:- typeclass size(T) where [
		func sizeof(T) = int
	].
		
	:- instance size({}) where [
		func(sizeof/1) is sizeof_empty_tuple
	].
	:- instance size({T1|T2}) <= size(T1), size(T2) where [
		func(sizeof/1) is sizeof_nonempty_tuple
	].

	sizeof_empty_tuple({}) = 1.
	sizeof_nonempty_tuple({X1 | X2}) = sizeof(X1) + sizeof(X2).

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