[mercury-users] Typeclassed binary type arguments?

doug.auclair at logicaltypes.com doug.auclair at logicaltypes.com
Thu Apr 6 12:18:21 AEST 2006


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.

To fix this, I pass along a value of the type
into the (revised) nil/1 term:

:- type foo(T) ---> nil(T); bar(T).
:- instance showable(foo(T)) <= showable(T) where [
  show(nil(_)) = "nil",
  show(bar(T)) = show(T) ++ " bar"
].

   { Nada = nil(some_kind_of_T_value) },
   print(show(Nada))

Now the compiler's happy, the program runs, but
I'm dissatisfied with allocating and then passing
an unused value just to convey type information.

Is there a way to please both the compiler and me
in this situation?  

Sincerely,
Doug Auclair

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