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
Re: [Caml-list] Physical counterpart to
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-07-29 (07:07)
From: Pascal Cuoq <Pascal.Cuoq@c...>
Subject: Re: [Caml-list] Physical counterpart to
Edgar Friendly wrote:
> Elnatan Reisner wrote:
>> Is there something that can complete this analogy:
>> (=) is to (==) as is to ___?
>> That is, is there a polymorphic total ordering with respect to  
>> *physical*
>> entities, rather than to their structure?
> No, but it'd be pretty trivial to implement through the C interface.

No, it would not be trivial to implement, because you would not want the
order of two values to change each time one is moved from the minor heap
to the major heap or when the major heap is compacted.
The article linked below is about this kind of topic (among other
things), in the context of Haskell.

The simple solution is to number at creation the objects that you want  
physically compare, using an additional field.

> If you had to stay in the OCaml realm, you might be able to do [let
> phys_comp (x:'a) (y:'a) = (Obj.magic x) - (Obj.magic y)], but it  
> depends
> on the exact implementation of (-) on your architecture, as it may
> produce a value that's not an OCaml int when given non-ints as input.

As an additional general remark, it is a bad idea to use "x - y"
as an implementation of "compare x y" because of overflows.