[mercury-users] Typeclassed binary type arguments?

Mark Brown mark at cs.mu.OZ.AU
Thu Apr 6 12:40:41 AEST 2006


Hi Doug,

On 05-Apr-2006, doug.auclair at logicaltypes.com <doug.auclair at logicaltypes.com> wrote:
> Dear all,
> 
> Hi!  I wish to constrain the type of a type argument
> using a typeclass ...
> 
> :- typeclass showable(T) where [ func show(T) = string ].
> :- type foo(T) ---> nil; bar(T).
> 
> ... so I've typeclassed the type argument in
> the instance declaration on foo (because, in
> this case, foo is also a showable instance):
> 
> :- instance showable(foo(T)) <= showable(T) where [
>   show(nil) = "nil",
>   show(bar(T)) = show(T) ++ " bar"
> ].
> 
> That's all fine until I select the nil term
> and call the instance method:
> 
>     { Nada = nil },
>     print(show(Nada))
> 
> this causes the compiler to complain about the
> unsatisfied typeclass constraint.  Yes, it is
> unsatisfied, but it's a 'don't care' situation.

The problem here is that there is an unbound type variable, which will
implicitly be bound to void -- the type of Nada is foo(void).  Since void
is not an instance of showable, the constraint on the foo(void) instance
isn't satisfied.

Probably the easiest way to fix this is to use a type qualifier:

	{ Nada = nil : foo(int) }

where the int should be replaced by any other instance of showable.

Cheers,
Mark.

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