[m-dev.] Typeclass constraints on types

Mark Anthony BROWN dougl at cs.mu.OZ.AU
Thu Oct 26 17:48:14 AEDT 2000


Hi,

Ralph Becket writes:
> I want to describe a type thus:
> 
> :- type show_pred == ( pred(X, io__state, io__state) <= showable(X) ).
> 
> But I can't do it: I get
> 
> Error: free type parameter in RHS of type definition

Equivalence type definitions must be transparent, meaning that all
type variables appearing on the RHS also appear on the LHS.  The
definition above is not transparent.

> 
> But X isn't really free, it's constrained.  So what's the trick?  
> 

There are two tricks.  First, you can make X a parameter to the type:

	:- type show_pred(X) == ...

which makes the type definition transparent.  However, with the current
implementation you won't be able to put a typeclass constraint on X.
This shouldn't cause serious problems, but arguably it is nicer to be
able to constrain type parameters.  (IIRC, this issue has come up on this
list previously.)

Second, if you make the type a discriminated union, then X can be
existentially quantified and can have a typeclass constraint.  This means
that X does not need to appear on the LHS.

	:- type show_pred ---> some [X] (show_pred(...) => showable(X)).

Which of these tricks you should choose depends on what you want to do
with the type.

Cheers,
Mark.

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