From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.4 required=5.0 tests=SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 3A19DBC6B for ; Thu, 5 Apr 2007 11:46:50 +0200 (CEST) Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.247]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l359knLO004163 for ; Thu, 5 Apr 2007 11:46:49 +0200 Received: by an-out-0708.google.com with SMTP id c24so601604ana for ; Thu, 05 Apr 2007 02:46:48 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=rHRwVNx2TOV7WQIxRs7wTEPYpvUL8njV2mJTgds2mKODspijy1YzhfCxllRpPpKyXOi6WUMhY8/IibOaEhzIgpDym5KGYZwY6QLO2chGFuTeYYPeGivIpSDxEBaXd4RpyM6tJa75CHZGw5zn9OiGw4OThdUs8haqTRR4iC7M204= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=mvqCMQQ4VFG7us08F+WsXAoghiG8gwy460rLU5AoPIpVWx3+5ovrOFp1dLq0aw1usYDUa6xF4ALcWbYinL9/G/yWLq3cbci6mx7v4YrBePFYLue3sfn326cEKqdeNjlNqA0H8NJAPYZK/YNibZ5j+OK0S94KrJPQjGgFdFfh8to= Received: by 10.100.132.16 with SMTP id f16mr1166886and.1175766408415; Thu, 05 Apr 2007 02:46:48 -0700 (PDT) Received: by 10.100.196.19 with HTTP; Thu, 5 Apr 2007 02:46:48 -0700 (PDT) Message-ID: <5e321f880704050246q1538edf2p89ae2d9c48c271fa@mail.gmail.com> Date: Thu, 5 Apr 2007 11:46:48 +0200 From: "Francois Maurel" Sender: francois.maurel@gmail.com To: "Alain Frisch" Subject: Re: [Caml-list] Polymorphic recursion Cc: caml-list@yquem.inria.fr In-Reply-To: <4614037E.6060709@inria.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <6f9f8f4a0704030959l8ebb155g8532e3ee6d31c66d@mail.gmail.com> <4613C0AA.8010405@inria.fr> <4613C22E.5060206@inria.fr> <4614037E.6060709@inria.fr> X-Google-Sender-Auth: 727277a99388800e X-Miltered: at discorde with ID 4614C589.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; recursion:01 endline:01 frisch:01 frisch:01 annotations:01 well-typed:01 coq:01 ocaml:01 type-checker:01 ocaml:01 runtime:01 endline:01 coq:01 ocamlopt:01 segfault:01 About unintuitive - and, of course, strongly implementation dependent - Obj.magic stuff, one can also patch your example into: =3D=3D=3D let magic x =3D let y =3D Obj.magic x in y let () =3D let x =3D if ("a" =3D "b") then magic 0 else String.copy "abc" in for i =3D 0 to 100000 do ignore (ref [1]) done; Gc.major (); print_endline x =3D=3D=3D which is somehow "correct". The version =3D=3D=3D let magic x =3D Obj.magic x ... =3D=3D=3D is not. Fran=E7ois On 04/04/07, Alain Frisch wrote: > Roland Zumkeller wrote: > > The function can be checked in richer type systems with annotations > > (e.g. Coq's), so we know that Obj.magic is not dangerous here. > > The fact that something is well-typed in Coq does not mean that you can > just translate it to OCaml by adding a few Obj.magic to make the > type-checker happy. OCaml programmers tend to have a rough mental > picture of what the semantic of the Obj module is and what are the > important properties of the runtime representation of values, but they > often only see part of the picture. Do you know what the following piece > of code does? > > let () =3D > let x =3D if ("a" =3D "b") then Obj.magic 0 else String.copy "abc" in > for i =3D 0 to 100000 do ignore (ref [1]) done; > Gc.major (); > print_endline x > > Well, if I knew Coq, I could prove that "a" is not equal to "b" and thus > that x is always bound to a valid string. So the Caml code should print > "abc". Right? > > No. This code compiled with ocamlopt produces a segfault on my machine. > I remember spending hours (and wasting my boss' precious time) on a bug > I introduced in some code because I thought that Obj.magic 0 and > Obj.magic () are equivalent. The code above show that this is not the > case (if you replace 0 with (), it works fine). > > If you don't understand what's going on, you'd better not use the Obj > module. If you know why, there is probably some other dark corner which > you don't understand and that will bite you some day. > > > In the present case, we have good solutions that don't require Obj. > Unless a strong case is made that performance is not adequate, there is > really no reason to use Obj. > > -- Alain > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > >