[mercury-users] overlapping instance declarations

Michael Day mcda at students.cs.mu.oz.au
Sun Aug 6 17:11:13 AEST 2000


Would it be possible, meaningful or useful to allow overlapping instance
declarations, as long as they are in separate modules?

Currently, instance declarations are global and overlapping instances are
detected at link time. If all instance declarations were module qualified
in some way, this link time check could be skipped, if there was a check
to ensure that the instance declarations in a particular module did not
overlap.

This could lead to ambiguity when attempting to satisfy a type class
constraint if there are multiple instance declarations for that type in
the current scope (imported from other modules). This could be treated
similar to any other ambiguous situation; the user would have to module
qualify the variable in question (somehow) to indicate which module's
instance declaration to use. I can't quite envisage an appropriate syntax
for that, it would seem to be related to type qualification...

	% Bar is an int using fooable instance from quux?
	foo_method(Bar : int <= quux:fooable(int)),

	% Bar using the instance fooable/1 from module quux?
	foo_method(Bar <= instance(quux:fooable/1)),

Anyway, assuming it could be done and assuming there is suitable syntax
for resolving ambiguities (which would be necessary in instance
declarations that depend on other instance declarations as well as
constrained predicate calls whenever there is more than one instance in
the current scope)... why do we want to do it?

With seductive type classes such as:

:- typeclass hashable(T) where [ func hash(T) = int ].
:- typeclass ring(N) where [ ... insert ring operations here ... ].

It seems that it would be very easy to get into a situation where you are
stuck with an instance declaration you don't like, but can't replace. What
happens when you want your own :- instance hashable(string). when someone
has thoughtlessly already taken it? What happens when you want a number of
instances of ring(float) using different operations? Linking in one extra
module to a working program can introduce link errors if that module has
an instance declaration that already exists in the program, even if the
instance declaration is not exported and no other modules are using it.
That doesn't feel right.

What are the reasons for making instance declarations global unlike
everything else in the language?

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