caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Francois Maurel" <Francois.Maurel@normalesup.org>
To: "Alain Frisch" <Alain.Frisch@inria.fr>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Polymorphic recursion
Date: Thu, 5 Apr 2007 11:46:48 +0200	[thread overview]
Message-ID: <5e321f880704050246q1538edf2p89ae2d9c48c271fa@mail.gmail.com> (raw)
In-Reply-To: <4614037E.6060709@inria.fr>

About unintuitive - and, of course, strongly implementation dependent
- Obj.magic stuff, one can also patch your example into:

===
let magic x =
  let y =
    Obj.magic x
  in
  y

let () =
  let x = if ("a" = "b") then magic 0 else String.copy "abc" in
 for i = 0 to 100000 do ignore (ref [1]) done;
  Gc.major ();
 print_endline x
===

which is somehow "correct".

The version
===
let magic x =
  Obj.magic x

 ...
===
is not.

François

On 04/04/07, Alain Frisch <Alain.Frisch@inria.fr> 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 () =
>   let x = if ("a" = "b") then Obj.magic 0 else String.copy "abc" in
>   for i = 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
>
>


  parent reply	other threads:[~2007-04-05  9:46 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-03 16:59 Loup Vaillant
2007-04-03 17:20 ` [Caml-list] " Jeremy Yallop
2007-04-04  5:27   ` Alain Frisch
2007-04-04 12:54     ` Loup Vaillant
2007-04-03 17:35 ` Till Varoquaux
2007-04-03 20:00   ` brogoff
2007-04-04  1:27     ` skaller
2007-04-04  1:40       ` skaller
2007-04-04 13:49 ` Roland Zumkeller
2007-04-04 15:13   ` Alain Frisch
2007-04-04 15:20     ` Alain Frisch
2007-04-04 16:45       ` Roland Zumkeller
2007-04-04 19:58         ` Alain Frisch
2007-04-04 20:13           ` brogoff
2007-04-05  9:33           ` Roland Zumkeller
2007-04-05  9:54             ` Alain Frisch
2007-04-05 10:07               ` Daniel de Rauglaudre
2007-04-05  9:46           ` Francois Maurel [this message]
2007-04-04 15:50     ` Stefan Monnier
2007-04-04 23:36 ` [Caml-list] " Brian Hurt
2007-04-05  8:17   ` Loup Vaillant
  -- strict thread matches above, loose matches on Subject: below --
2008-05-12 21:55 polymorphic recursion Jacques Le Normand
2008-05-12 22:16 ` [Caml-list] " Christophe TROESTLER
2003-08-24 18:01 [Caml-list] Polymorphic recursion Lukasz Stafiniak
2003-08-25  0:30 ` Jacques Garrigue
2003-08-25  0:43   ` Jacques Garrigue

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=5e321f880704050246q1538edf2p89ae2d9c48c271fa@mail.gmail.com \
    --to=francois.maurel@normalesup.org \
    --cc=Alain.Frisch@inria.fr \
    --cc=caml-list@yquem.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).