[mercury-users] Newbie questions about typeclasses

Fergus Henderson fjh at cs.mu.OZ.AU
Tue Apr 24 16:43:17 AEST 2001


On 24-Apr-2001, Critterrathman at aol.com <Critterrathman at aol.com> wrote:
> 1).  For some reason, MMC complains about not being able to find shape.int3.  

If your program consists of more than one module, use `mmake' rather than
`mmc' to compile it.  It's possible to do it manually using `mmc', but
using `mmake' is really much easier.

>       Here's the calls to MMC for the interface generate:
> 
>             mmc --make-int shape.m
>             mmc --make-int circle.m

The problem here is that you forgot to do

              mmc --make-short-int shape.m
              mmc --make-short-int circle.m

first.

> 2).  I'm not sure how to get the methods for the typeclass to be used.  What 
> I wanted is to just expose the record and the typeclass definitions in the 
> interface and then map the specific function to the typeclass within the 
> implementation section of the module.  This would mean that the interface 
> might expose a method called 'getRadius' while the local method can be 
> implemented locally as 'getRadius_Circle'.  If I try to access the method by 
> the name of the typeclass method, I get an error message about an unsatisfied 
> constraint.
> 
>       Here's the call in the main routine that causes the problem:
> 
>             draw_Circle(setRadius(ACircle, 30))
> 
>       Here's the error message:
> 
>             In clause for predicate `polymorph:main/2':
>               unsatisfiable typeclass constraint(s):
>               `circle:circleClass((circle:circleRecord))'.

The problem here is that there is no instance declaration

	:- instance circleClass(circleRecord) where [
		...
	].

visible at that point in your source code.
Either
(a) there's just no such declaration in your program,
    so you need to add it;
(b) your program does contain such a declaration,
    but it hasn't been imported into the current module (`polymorph'),
    so you need to add an appropriate `:- import_module'
    or `:- use_module' declaration in polymorph.m; or
(c) your program does contain such a declaration, but
    it occurs only in the implementation section of the
    module containing it, not in the interface.
    To use it from another module, you also need to export the
    instance declaration.
    If you want to keep the details private, you can just export it
    abstractly, using an abstract instance declaration

	:- instance circleClass(circleRecord).

    in the module's interface section.

Oh, I see you included source code in your email,
and looking at that I can see that (c) is the cause.
You just need to add the appropriate abstract instance
declarations in the interface of circle.m.

> Along these lines, I really didn't want to put the local implementation mode 
> into the interface section.  If I could get the method names to map off of 
> the class, I'd probably be able to push the local names into the 
> implementation section.

Yes, if you just export the abstract instance declarations,
then you don't need to export the procedures used to implement
the methods.

> 3).  Also along similar lines, I was having difficulty getting the compiler 
> to accept pred(draw/3) in the instance declaration.  The pred() compiles fine 
> in the typeclass statement

The problem here was that your type class declaration declared a predicate,
but didn't declare any modes for that predicate.
You need to add a mode declaration for the draw/3 predicate 
to the type class declaration.

Unfortunately the compiler didn't report any error or warning at the
type class declaration, and instead only reported a rather confusing
error at the instance declaration.  I think this is a bug -- I think
we should fix the compiler so that it complains at the type class
declaration.  Thanks for reporting that.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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