Version française
Home     About     Download     Resources     Contact us    
Browse thread
polymorphic equality and overloading
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: polymorphic equality and overloading
> > Equality really is overloaded, but since overloading isn't easily
> > integrated into ML style type systems, some other solution is chosen. 
> 
> So, I'm wondering why equality is overloaded in Caml, unlike addition
> (+ for integers, +. for floats, ^ for strings, etc.) for example.  Or,
> why is equality automatically defined for tuples and datatypes, while
> addition isn't?  Is that just because it is often useful?

The answer is in your sentence: how do you define addition on tuples ?
Or even on strings: ^ is not addition but concatenation;
fun s1 s2 -> string_of_float (float_of_string s1 +. float_of_string s2)
will give you a very different result.

The only form of overloading currently accepted in Caml is universal
overloading, that is operations available at all types.
Comparison is just "naturally" defined on almost anything, the only
exceptions being function closures which have no identity, and C data
when no custom operation is defined.
There is some arbitrary part in this definition, but even so
being able to compare values is useful anyway (Set and Map modules).
I do not really see what would be the use of an underspecified
addition on algebraic datatypes, for instance.

If you want other candidates for universal overloading, print would be a
better choice. Theoretically all values can be printed in a way or
another, and there may be uses for that. However this requires much
more runtime type information.

Regards,

---------------------------------------------------------------------------
Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>