caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Physical counterpart to Pervasives.compare?
@ 2009-07-29  1:25 Elnatan Reisner
  2009-07-29  3:06 ` [Caml-list] " Edgar Friendly
  2009-07-29  6:13 ` Alain Frisch
  0 siblings, 2 replies; 5+ messages in thread
From: Elnatan Reisner @ 2009-07-29  1:25 UTC (permalink / raw)
  To: caml-list

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?

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Physical counterpart to Pervasives.compare?
  2009-07-29  1:25 Physical counterpart to Pervasives.compare? Elnatan Reisner
@ 2009-07-29  3:06 ` Edgar Friendly
  2009-07-29  3:58   ` Edgar Friendly
  2009-07-29  6:13 ` Alain Frisch
  1 sibling, 1 reply; 5+ messages in thread
From: Edgar Friendly @ 2009-07-29  3:06 UTC (permalink / raw)
  To: Elnatan Reisner, caml-list

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Physical counterpart to Pervasives.compare?
  2009-07-29  3:06 ` [Caml-list] " Edgar Friendly
@ 2009-07-29  3:58   ` Edgar Friendly
  0 siblings, 0 replies; 5+ messages in thread
From: Edgar Friendly @ 2009-07-29  3:58 UTC (permalink / raw)
  To: Elnatan Reisner, caml-list

Edgar Friendly wrote:
> Elnatan Reisner wrote:
>> 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].
>
It looks like I'm wrong on this - there's versions of compare
specialized for floats, strings and ints.  Maybe you *could* trigger the
use of the caml_int_compare by this kind of type system manipulation.
It looks quite safe to use on non-ints.

Testing on your platform is possible, by trying to find store pointers
to ints, so that caml_compare compares the ints, but the tricked
caml_int_compare compares the pointers, and gets a different result.

E


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Physical counterpart to Pervasives.compare?
  2009-07-29  1:25 Physical counterpart to Pervasives.compare? Elnatan Reisner
  2009-07-29  3:06 ` [Caml-list] " Edgar Friendly
@ 2009-07-29  6:13 ` Alain Frisch
  1 sibling, 0 replies; 5+ messages in thread
From: Alain Frisch @ 2009-07-29  6:13 UTC (permalink / raw)
  To: Elnatan Reisner; +Cc: caml-list

On 7/29/2009 3:25 AM, 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?

Not really. The physical location of heap values is not stable (because 
of the GC), so you cannot use it as a total ordering.

It may be useful to know that the generic structural comparison has a 
physical behavior for OCaml objects (it compares their unique ids). So 
wrapping your values inside objects can be a good trick to get physical 
comparison (and hashing), as in:

class ['a] physical_reference init = object
   val mutable current : 'a = init
   method get = current
   method set x = current <- x
end


-- Alain


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Physical counterpart to Pervasives.compare?
       [not found] <20090826100004.0169CBC58@yquem.inria.fr>
@ 2009-08-26 10:14 ` Pascal Cuoq
  0 siblings, 0 replies; 5+ messages in thread
From: Pascal Cuoq @ 2009-08-26 10:14 UTC (permalink / raw)
  To: caml-list

Christophe Raffalli <christophe.raffalli@univ-savoie.fr> wrote:
> Or, if all major GCs are compacting and the minor heap is at
> a higher adress than the major heap, then OCaml's could also
> preserve the adress order between GC ...

I do not think that the current minor GC algorithm does
any effort to copy the live blocks from the minor heap in order
(i.e. to copy first the blocks with the lower addresses).
It copies them as it finds them, recursively. Two blocks from
the minor heap may still see the order in which they are
in memory change when they are both copied
to the major heap.
For this reason, I do not think that the above trick would work.

Pascal


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-08-26 10:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-29  1:25 Physical counterpart to Pervasives.compare? Elnatan Reisner
2009-07-29  3:06 ` [Caml-list] " Edgar Friendly
2009-07-29  3:58   ` Edgar Friendly
2009-07-29  6:13 ` Alain Frisch
     [not found] <20090826100004.0169CBC58@yquem.inria.fr>
2009-08-26 10:14 ` Pascal Cuoq

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).