[m-dev.] Foreign type compare and unification

Peter Schachte schachte at cs.mu.OZ.AU
Fri May 24 13:51:49 AEST 2002


On Thu, May 23, 2002 at 02:45:01PM +1000, Mark Brown wrote:
> > But when someone else using that type in another way in another
> > part of the code declares that type to be in a class (and define its
> > methods), it does not make sense that this should have any effect on
> > my code.
> 
> Do you mean an effect on the validity of your code, or on the behaviour
> of your code?

Both.

> If you mean the latter, then aren't we in violent
> agreement?

I don't think so.  As I understand it, you still want some random
distant piece of code to be able to declare that string is an instance
of comparable, and my code should benefit from it.  I would argue that
unless string is made an instance of comparable in the interface of
the module that defines string, or my code has such a declaration,
then my code should not consider string to be an instance of
comparable.  I'd rather get a compile-time error (not a link-time
error) if my code tries to compare two strings, or passes strings to a
predicate that does so.

> > My conjecture is that if I could define an equavalence type, but the
> > old type's class memberships did not flow automatically to the new
> > one,
> 
> For the record, I consider this to be an abuse of the concept of
> "equivalent".

Yeah, it is.

Come to think of it, I'm really looking for something more like a
restricted kind of inheritance.  The *data* could not be changed in
the subclass, only the methods could be overridden.  Thus it's always
possible to convert (cast) a superclass to a subclass, and always
possible to pass a subclass where a superclass is required.

So I could declare case_insensitive_string to be the same type as
string, but to have a different comparison method.  I could pass terms
of this type to any predicate that expected a string, or a comparable
thing, as argument.  I could also convert a string to a
case_insensitive_string with 0 runtime cost and no possibility of
runtime error.  But when two case_insensitive_strings are compared,
the comparison is case insensitive.

I guess I can get most of that by defining

	:- type case_insensitive_string ---> cis(string).

	:- instance comparable(case_insensitive_string) where [
		'<' is case_insensitive_less
	]

and then converting from string to case_insensitive_string using
cis/1.  What this doesn't give me, though is the ability to pass a
case_insensitive_string anywhere I can pass a string, but still have
the method vector of case_insensitive_string tag along, and that's
essential to the idea.  I don't see how to do that.



-- 
Peter Schachte              The fantastic advances in the field of
schachte at cs.mu.OZ.AU        communication constitute a grave danger to the
www.cs.mu.oz.au/~schachte/  privacy of the individual.
Phone: +61 3 8344 9166          -- Earl Warren 
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list