caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien Doligez <damien.doligez@inria.fr>
To: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Safe Obj.magic container ?
Date: Thu, 28 Feb 2008 15:23:35 +0100	[thread overview]
Message-ID: <DE69B31E-7754-4054-8482-B1DEBB0EFB8D@inria.fr> (raw)
In-Reply-To: <37B36607-9F22-4537-B4DB-1E04348E2B90@inria.fr>

On 2008-02-27, at 10:43, Luc Maranget wrote:

> The semantics of Obj.magic is as follows:
>
> 1. Obj.magic does not exist. Don't use it.
>
> 2. Obj.magic is black magic, you can see it as the identity
>  (fun x -> x)  with type 'a -> 'b.
>
> As I see it, 2. steems from the uniform representation of values by  
> OCaml.
>
> A value is either a scalar, or a pointer, and this can be tested
> dynamically (by looking at the low order bit) by
> Obj.is_int/Obj.is_block.


If that was the whole story, Obj.magic would be pretty safe.  But
there are exceptions to the uniform representation of values, and
they make Obj.magic really hard to use.

The most important exception is floating-point numbers.  They are
not always represented as one word each, and that means even
Obj.repr is unsafe.

Example (on a 32-bit machine):

        Objective Caml version 3.10.2+rc1

# let x = Obj.repr 1;;
val x : Obj.t = <abstr>
# let y = Obj.repr 1.0;;
val y : Obj.t = <abstr>
# [| x; y |];;
- : Obj.t array = [|<abstr>; <abstr>|]
# [| y; x |];;
Bus error

My advice: don't use the Obj module if you were surprised by this
example.

-- Damien


  parent reply	other threads:[~2008-02-28 14:23 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-27  9:09 David Teller
2008-02-27  9:43 ` [Caml-list] " Luc Maranget
     [not found]   ` <37B36607-9F22-4537-B4DB-1E04348E2B90@inria.fr>
2008-02-28 14:23     ` Damien Doligez [this message]
2008-02-28 15:29       ` David Teller
2008-02-28 16:24         ` Damien Doligez
2008-02-29  1:52         ` Jacques Garrigue
2008-02-29  8:18           ` David Teller
2008-02-28 19:20       ` Jake Donham
2008-02-28 23:19         ` Richard Jones
2008-02-27 11:23 ` Tom Primožič
2008-02-27 21:22   ` David Teller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DE69B31E-7754-4054-8482-B1DEBB0EFB8D@inria.fr \
    --to=damien.doligez@inria.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).