[mercury-users] restriction on typeclass instances

Mark Anthony BROWN dougl at cs.mu.OZ.AU
Mon Nov 15 13:02:43 AEDT 1999


Ralph Becket writes:
> 
> I tried the following just
> for kicks (it's illegal under the current restrictions)...
> 
> :- type thing(T) == T.
> 
> :- instance sequence(my_list(T), thing(T)) where [...].
> 
> And the compiler threw an exception.
> 
> Hmm, this restriction (no free type variables as instance arguments) could
> be a bit of a pain.
> 

If you change the definition of `thing' to

	:- type thing(T) ---> thing(T).

then your example should compile OK (and not throw unexpected exceptions).
This has worked for me the one or two times I have needed it,
though I'm not claiming it will always work.

A disadvantage of doing this is that data needs to be wrapped/unwrapped
all the time, which adds a bit of clutter to code.  There should be
no run-time cost, though, because the compiler should optimize these
away.  Another disadvantage is that for each instance of this form
you need a new type.

On the upside, using a wrapper type is sufficient (although probably not
necessary) to disambiguate which instance of a typeclass is being
used.  I would consider a well chosen name to be useful documentation,
too.

So, I agree that the restriction *could* be a pain, but I'm not
convinced.  Can anyone think of an example where this work-around
would not be adequate to overcome the restriction?

Cheers,
Mark.
-- 
Mark Brown, PhD student            )O+  |  "Another of Fortran's breakthroughs
(m.brown at cs.mu.oz.au)                   |  was the GOTO statement, which was...
Dept. of Computer Science and Software  |  uniquely simple and understandable"
Engineering, University of Melbourne    |              -- IEEE, 1994
--------------------------------------------------------------------------
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