Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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-04 (19:05)
From: Jason Hickey <jyh@c...>
Subject: Re: [Caml-list] How can I check for the use of polymorphic equality?
Pierre Weis wrote:
>>and more importantly b) means you can't use = anywhere, doesn't it?
> No: it means you have to do something more than just writing = when
> you need to call the predefined generic equality. You first have to
> think if there is not another predicate that is not more appropriate
> to the situation, and if there is none, you just have to insert a mere
>   let ( = ) = Pervasives.( = ) in
> before each relevant occurrence of ( = ). Those single lines can be
> easily removed after proper debugging (use an emacs keyboard macro + a
> M-x grep -n -e ... *.ml).
 > ...
 > Pierre Weis

One really nice thing about your solution is that we can get type 
inference to catch uses of =.  But it can be awkward because we still 
need relations on base types.  Imagine that we have a Set module that 
should use Set.equal, not =.  Consider the fixpoint code below.

type empty

let (=) (x : empty) (y : empty) = assert false
let (>) (x : empty) (y : empty) = assert false

let rec fixpoint s1 s2 =
    if Set.equal s1 s2 then
    (* Can't use >, so use Pervasives.(>) directly *)
    else if Pervasives.(>) (Set.cardinal s1) (Set.cardinal s2) then
       fixpoint (f s1) s2
       fixpoint s1 (f s2)

It can get ugly if we have to use the Pervasives.(=) a lot.  Of course, 
we could be a little smarter, and define something like the following 
for each of the base types.

let (=@) : int -> int -> bool = Pervasives.(=)

Perhaps even

let (=$) : 'a set -> 'a set -> bool = Set.equal

This might be a compromise, though remembering all the equality suffixes 
would be a hassle.


Jason Hickey        
Caltech Computer Science      Tel: 626-395-6568 FAX: 626-792-4257

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: