[mercury-users] Polymorphism / Type classes

David Jeffery dgj at cs.mu.OZ.AU
Thu Jun 14 21:20:37 AEST 2001


On 14-Jun-2001, Ralph Becket <rbeck at microsoft.com> wrote:
> 
> I confess to being very confused too.  Would it be possible for
> someone in the know to post the type inference rules regarding
> typeclasses?  This sort of problem seems to crop up every time.

Sounds like someone is offering to proof read this chapter I'm working on. ;-)

But seriously, type classes aren't the cause of the problem here. For example,
if we had:

:- pred with_useful(pred(U, U), F, F).
:- mode with_useful(in(pred(di, uo) is det), di, uo) is det.

with_useful(...) :- ...

Then the following code is accepted:

:- pred do_useful(F, F).
:- mode do_useful(di, uo) is det.

 do_useful -->
       with_useful(do_stuff).  % type error

:- pred do_stuff(U, U).
:- mode do_stuff(di, uo) is det.

do_stuff --> does lots of stuff.

*But* what has happened here is that the type variable `U' has been bound
to `void'. You haven't passed the polymorphic predicate as a polymorphic
predicate: you have passed a monomorphic instance (and right or not, Mercury
has chosen `void' as that instance).

Also now the definition of with_useful is unlikely to work as
you'd expect: you can't actually apply the higher order argument to
anything inside the definition of with_useful: it acts on entities of
type `U', but the other arguments to with_useful are of type `F'.

HTH.


dgj
-- 
David Jeffery (dgj at cs.mu.oz.au) | If you want to build a ship, don't drum up 
PhD student,                    | people together to collect wood or assign 
Dept. of Comp. Sci. & Soft. Eng.| them tasks and work, but rather teach them 
The University of Melbourne     | to long for the endless immensity of the sea.
Australia                       | -- Antoine de Saint Exupery
--------------------------------------------------------------------------
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