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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Edgar Friendly <thelema314@g...>
Subject: Re: [Caml-list] Physical counterpart to Pervasives.compare?
Elnatan Reisner wrote:
> Is there something that can complete this analogy:
> (=) is to (==) as Pervasives.compare 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.

> I'm afraid of getting into trouble with Obj.magic, but what would this do:
> let f (x:'a) (y:'a) = compare (Obj.magic x) (Obj.magic y)
> ? Or would annotations make any difference:
> let f (x:'a) (y:'a) = compare (Obj.magic x : int) (Obj.magic y : int)
> 
> -Elnatan

Nope, Obj.magic and casting only have compile-time effects, the code
given compiles exactly the same as [let f x y = compare x y].

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.

E