caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Type abstraction and (polymorphic) equality
@ 2005-06-29  0:31 Christophe TROESTLER
  2005-06-29  9:12 ` [Caml-list] " Jon Harrop
                   ` (4 more replies)
  0 siblings, 5 replies; 22+ messages in thread
From: Christophe TROESTLER @ 2005-06-29  0:31 UTC (permalink / raw)
  To: O'Caml Mailing List

[-- Attachment #1: Type: Text/Plain, Size: 2898 bytes --]

Hi,

I'd like here to present a problem that I think is fairly common (and
likely probably discussed) to have suggestions about what is the best
way to deal with it.

Let me start with a little story that happened to me recently.
Suppose one has a module A declaring an abstract type t and some
functions.  You are happy with the interface of A and have a working
implementation so you go on and write programs using A.  Later on, you
realize that you could improve A implementation by attaching
additional information to each variable of type t (this does not
change the interface of A).  Then all of a sudden, your programs
start to die with Out_of_memory exceptions...

The problem was that the attached information (an additional field in
a record) was a cyclic data structure.  From there on, all equality
tests became deadly! (I would have preferred to have the exception
Invalid_argument "equal: abstract value".)  What made matters worse is
that the compiler could not help me to find the locations of such
problems -- which can be hidden e.g. in List.mem.  Not a nice job to
do...

One may argue that its my fault: I should have declared from the start
a function

  val eq : t -> t -> bool

While that would have solved some of the problems I have, it would
have introduced new ones.  In particular, some functions relying on
equality couldn't be used anymore (but, again, without the compiler
helping to fund them).  An example is List.mem.  Of course, there is
List.exists to achieve the same goal but maybe one uses a library
declaring internally

  type 'a s = X | Y of 'a

and using equality on values of type ['a s] (here the default
structural equality is arguably what we want).  You may not even be
aware that the library does so if you did not write it.  However, for
values of type [t s] on needs a special equality...  which in practice
precludes the use of this library!

Here one sees that the problems with equality stand in the way of
abstraction and code reusability.

Now the questions are:

* Is there a solution in the current state of OCaml? (I'll be glad if
  there is.)

* If the first answer is "no", is there a medium term perspective to
  bring a solution to this problem?  I can see two:

  - one allows to redefine equality for new types, say with a syntax
    like

    type t = ... with compare x y = ...

    as this is already done for blocks with custom tags.  (BTW, why
    aren't Big_int such blocks with an appropriate compare?)

  - One introduces the same capability of providing a special equality
    (comparison) for certain types but, during compilation, "expand"
    functions till the type for "=" is given by known functions
    (something like a "generic" equality).  I guess however that that
    may cause problems with separate compilation...

I'll be glad to hear similar experiences and comments about the above
ideas.

Regards,
ChriS



[-- Attachment #2: a.mli --]
[-- Type: Text/Plain, Size: 27 bytes --]

type t
val x : t
val y : t

[-- Attachment #3: b.ml --]
[-- Type: Text/Plain, Size: 44 bytes --]



let () = Printf.printf "%b\n" (A.x = A.y)

[-- Attachment #4: a.ml --]
[-- Type: Text/Plain, Size: 133 bytes --]

type t = {
  a : int;
  b : t * t; (* extra info for optimization *)
}


let rec x = { a=1; b = (x, y) }
and y = { a=1; b = (y,x) }


^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2005-07-01 12:49 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-29  0:31 Type abstraction and (polymorphic) equality Christophe TROESTLER
2005-06-29  9:12 ` [Caml-list] " Jon Harrop
2005-06-29 10:06   ` Andreas Rossberg
2005-06-29 13:32   ` Christophe TROESTLER
2005-06-29 23:39     ` brogoff
2005-06-30  7:46       ` Christophe Raffalli
2005-06-29 20:27   ` Christophe TROESTLER
2005-06-29 20:37   ` John Skaller
2005-06-30  9:53     ` Andreas Rossberg
2005-06-30 17:08       ` brogoff
2005-06-30 17:22         ` Andreas Rossberg
2005-06-30 19:56       ` John Skaller
2005-07-01 12:49         ` Andreas Rossberg
2005-06-29  9:45 ` Jean-Christophe Filliatre
2005-06-29 17:33 ` William Lovas
2005-06-29 18:08 ` sejourne_kevin
2005-06-30  9:51   ` Andreas Rossberg
2005-06-30 19:54     ` John Skaller
2005-06-30 22:24       ` Alain Frisch
2005-06-30 12:19 ` Alain Frisch
2005-06-30 12:32   ` padiolea
2005-06-30 12:57     ` Alain Frisch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).