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
Physical compare
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-11-23 (19:43)
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

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

Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists