English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] How can I check for the use of polymorphic equality?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-04-03 (19:44)
From: Jason Hickey <jyh@c...>
Subject: Re: [Caml-list] How can I check for the use of polymorphic equality?
Neel Krishnaswami wrote:
> I spent the last few hours tracking down a bug that was the result of
> accidentally using the polymorphic equality rather than a custom
> comparison function. Is there any way I can tell the compiler not to
> allow the use of polymorphic equality at certain types, or even just
> to raise an exception when it tries to compare values of those types?

This has been bothering me for quite some time.  There are times when I 
would like to modify an implementation to use a coarser comparison 
function than Pervasives.compare.  This requires manually finding all 
uses of =, <, etc. and modifying them to use the new comparison 
function.  It would be great if the typechecker would help, since it is 
easy to forget and reintroduce the use of = by accident.

I suppose the real answer would be to extend the type system with 
equality types, similar to those in SML.

In the meantime, I use a hack to help catch errors at runtime.  The idea 
is this.
    1. The type you care about is probably abstract.
    2. Add an abstract value to the data in your type, so that equality
       will fail (at runtime).

Suppose that your type is declared like this:

    type t = t1

    let create ... : t = e

Modify this as follows:

    type t = t1 * Obj.t

    let create ... : t =
        let bogus = Obj.new_block Obj.abstract_tag 1 in
           e, bogus

You'll see the following behavior:

    let o1 = create ... in
    let o2 = create ... in
        o1 = o2

Exception: Invalid_argument "equal: abstract value"

To find these errors, you'll probably want to use exception tracing. 
Use byte-code, and set the environment variable OCAMLRUNPARAM=b.

The "bogus" values are wasted space, so you may want to remove this 
wrapper code if you are confident that you found all the errors.


Jason Hickey                  http://www.cs.caltech.edu/~jyh
Caltech Computer Science      Tel: 626-395-6568 FAX: 626-792-4257

To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners