Version française
Home     About     Download     Resources     Contact us    
Browse thread
Converting variants with only constant constructors to integers
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Converting variants with only constant constructors to integers
From: Kaustuv Chaudhuri <kaustuv.chaudhuri@inria.fr>
> On Tue, Jun 8, 2010 at 4:37 PM, Jacques Garrigue
> <garrigue@math.nagoya-u.ac.jp> wrote:
>> Of course intify can cause a segmentation fault!
>>
>> # let arr = Array.of_list [intify 1.0; 0];;
>> Segmentation fault
> 
> This may be splitting hairs, but the reason that fails is that
> Array.of_list's ad hoc polymorphism heuristic assumes that if the
> first element of the list is allocated, then all elements are
> allocated. Merely changing the order works.
> 
>   # Array.of_list [Obj.repr 0 ; Obj.repr 1.0] ;;
>   - : Obj.t array = [|<abstr>; <abstr>|]
> 
> Which one of Obj.repr of Array.of_list to blame, if that's the right
> word, is not that relevant to any code a sane person might write.

No, this is not an heuristic, but a compilation method based on the
assumption that values of the same type have the same representation.
This is enforced by the compiler, but by using the Obj module, which
is out of the safe realm, you can break this invariant.
So, from the point of view of the ocaml specification, Obj.repr is
clearly to blame (and defined as unsafe to start with).

Jacques Garrigue