[mercury-users] overlapping instance declarations

Michael Day mcda at students.cs.mu.oz.au
Tue Aug 8 17:39:52 AEST 2000


> But which module?  Suppose I have a predicate p that uses an instance method m
> for a type class c.  When p is called by q, it is passed an argument of type
> t.  Each of p, q, c, and t can be in different modules; which should have its
> overlapping instance declaration respected?

There is always one point at which an instance declaration is associated
with a particular variable. In the situation you describe (a universal
type class constraint) the association takes place at the call site
(caller decides). If there is only one instance declaration for the type
in question in the scope of the call site, no problem. If there is more
than one, then it must be module qualified in some way. Similarly for
returning type class constrained existentally quantified types (did I say
that right?), except this time the callee decides.  

> I agree, this is a problem.  I suppose you can work around it by defining
> wrapper types with their own instance declarations, but if it happens much,
> it'll be pretty ugly.

In some cases you can, in some you can't, or don't want to. Either way, it
seems like you shouldn't be forced to, in this case.

> Here's a thought experiment.  Suppose predicate q calls p passing a term of
> type t in a position where an instance of type class c is expected, and
> suppose t is not declared to belong to class c.  Should I be able to fix this
> by linking in a completely different module m that contains the needed
> instance declaration?  Somehow, that doesn't seem right to me.  Why should I
> feel the effect of an instance declaration in a module I don't import?

I would prefer that only the instance declarations in the current module
and imported modules were visible.

> It would be nice to have some kind of precedence of instance declarations, so
> a module could supply a "default" instance declaration for a type it defines,
> but a module that actually uses that type could make its own instance
> declaration, which would take precedence for calls made from that module.

Such a precedence scheme would come after that which I've described above
- worth noting that there is no precedence scheme for types or other
symbols, a clash is a clash regardless. But anyway.

Anyone else have comments on overlapping instance declarations in separate
modules?

Michael

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