* Physical compare
@ 2005-11-23 19:20 Tom Hawkins
2005-11-23 19:38 ` [Caml-list] " Jon Harrop
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Tom Hawkins @ 2005-11-23 19:20 UTC (permalink / raw)
To: caml-list
Is their a version of "compare" that is based on physical equality? If
not, how can I define one? I tried:
let compareq a b = if a == b then 0 else if a > b then 1 else (-1)
But unfortunately, (>) is a structural comparison.
I need to make a Map where the keys are distinguished by the physical
instance.
Thanks!
-Tom
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Physical compare
2005-11-23 19:20 Physical compare Tom Hawkins
@ 2005-11-23 19:38 ` Jon Harrop
2005-11-23 19:49 ` Nicolas Cannasse
2005-11-24 15:33 ` Jean-Christophe Filliatre
2 siblings, 0 replies; 4+ messages in thread
From: Jon Harrop @ 2005-11-23 19:38 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Physical compare
2005-11-23 19:20 Physical compare Tom Hawkins
2005-11-23 19:38 ` [Caml-list] " Jon Harrop
@ 2005-11-23 19:49 ` Nicolas Cannasse
2005-11-24 15:33 ` Jean-Christophe Filliatre
2 siblings, 0 replies; 4+ messages in thread
From: Nicolas Cannasse @ 2005-11-23 19:49 UTC (permalink / raw)
To: Tom Hawkins; +Cc: caml-list
Tom Hawkins wrote:
> Is their a version of "compare" that is based on physical equality? If
> not, how can I define one? I tried:
>
> let compareq a b = if a == b then 0 else if a > b then 1 else (-1)
>
> But unfortunately, (>) is a structural comparison.
>
> I need to make a Map where the keys are distinguished by the physical
> instance.
>
> Thanks!
>
> -Tom
It's not possible.
One of the reason is that the GC might move your memory addresses around
and then break your Map constitency. You need to attribute an unique id
to each of your items and use it for comparison.
Nicolas
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Physical compare
2005-11-23 19:20 Physical compare Tom Hawkins
2005-11-23 19:38 ` [Caml-list] " Jon Harrop
2005-11-23 19:49 ` Nicolas Cannasse
@ 2005-11-24 15:33 ` Jean-Christophe Filliatre
2 siblings, 0 replies; 4+ messages in thread
From: Jean-Christophe Filliatre @ 2005-11-24 15:33 UTC (permalink / raw)
To: Tom Hawkins; +Cc: caml-list
Hello,
Tom Hawkins writes:
> Is their a version of "compare" that is based on physical equality? If
> not, how can I define one? I tried:
>
> let compareq a b = if a == b then 0 else if a > b then 1 else (-1)
>
> But unfortunately, (>) is a structural comparison.
>
> I need to make a Map where the keys are distinguished by the physical
> instance.
Others already gave the right answer i.e. that you need to tag your
values with unique integers to do that.
Just to mention it, I wrote a little hash-consing library that does
precisely this, together with specialized versions of Set and Map for
these tagged values (based on Patricia trees). There is even a short
article describing the technique. All is available on this page:
http://www.lri.fr/~filliatr/software.en.html
Hope this helps,
--
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2005-11-24 15:33 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-23 19:20 Physical compare Tom Hawkins
2005-11-23 19:38 ` [Caml-list] " Jon Harrop
2005-11-23 19:49 ` Nicolas Cannasse
2005-11-24 15:33 ` Jean-Christophe Filliatre
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox