Version française
Home     About     Download     Resources     Contact us    
Browse thread
Physical compare
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] Physical compare
On Wednesday 23 November 2005 19:20, Tom Hawkins wrote:
> Is their a version of "compare" that is based on physical equality?  If
> not, how can I define one?

Due to the use of a copying collector you cannot define one because the 
pointers that make up references can change value, i.e. a < b might not 
remain true for very long.

> I need to make a Map where the keys are distinguished by the physical
> instance.

Tag every key with a unique integer using something like this:

module Tag : sig
  type 'a t
  val make : 'a -> 'a t
  val compare : 'a t -> 'a t -> int
end = struct
  type 'a t = int * 'a
  let i = ref 0
  let make x =
    incr i;
    !i, x
  let compare (a, _) (b, _) = compare a b
end;;

Then use the Tag.compare function to perform physical comparison with a total 
ordering.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists