From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p14BnZ8L021604 for ; Fri, 4 Feb 2011 12:49:35 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArECAPJ2S01KfVIuimdsb2JhbACWZY5ACBUBAQEKCQwHDwYgoAOKB4IXhC0viFkBAQMFhVUEhzaEMYgtOg X-IronPort-AV: E=Sophos;i="4.60,425,1291590000"; d="scan'208";a="75339839" Received: from mail-ww0-f46.google.com ([74.125.82.46]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-MD5; 04 Feb 2011 12:49:30 +0100 Received: by wwj40 with SMTP id 40so2148960wwj.3 for ; Fri, 04 Feb 2011 03:49:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:reply-to:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=pJAd0nVzD6p3cgVUundPl3hljA8tO3savj6doX1x/VE=; b=I1Va0UMQhnge/9Hp7IYou7wdoalffpoiJUcjaM6/DMf04khXnc4FrFvj+OU3XyKAPC 6Gka2luJjhRqQfXcg1IN11dbGAg9KGKMHvwWa2JaVFvqE6jlSe3rdpEdYSDjqF0vZp9K yAiCfelm46E4lyet5qgpfs9K21Sg3E3mOec3I= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:reply-to:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; b=qFnxf15dy7QPHKiuNI+5YQM9K+tZTHjWU4JJY29BMiSfy8GZvd7O2Vgk8bjRwJTofR kuGlUHR1HMz7eDH7ngLwbJQKHFaf8WkxB8H+LPqxXTW8qG7XdP53eKD4useayCpS0yLZ qWOpkLuThqP34gm3Oc0VMBqyv6q3mbaAZIcfc= MIME-Version: 1.0 Received: by 10.227.147.14 with SMTP id j14mr11994675wbv.98.1296820168653; Fri, 04 Feb 2011 03:49:28 -0800 (PST) Received: by 10.227.136.202 with HTTP; Fri, 4 Feb 2011 03:49:28 -0800 (PST) Reply-To: david.baelde@ens-lyon.org In-Reply-To: <20110131164630.GN18069@localhost> References: <476C4EB3-59D2-4756-927C-C1697E7AE4D8@math.harvard.edu> <20110131164630.GN18069@localhost> Date: Fri, 4 Feb 2011 12:49:28 +0100 Message-ID: From: David Baelde To: Guillaume Yziquel Cc: Nicolas Ojeda Bar , caml-list@yquem.inria.fr Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [Caml-list] reference cells On Mon, Jan 31, 2011 at 5:46 PM, Guillaume Yziquel wrote: >> # let x = ref 10;; >> val x : int ref = {contents = 10} >> # Obj.tag (Obj.repr x);; >> - : int = 0 A funny side note about that test: the fact that you pass your reference to a function (Obj.repr) prevents the compiler from optimizing it. So even if you try (let x = ref 10 in Obj.tag (Obj.repr x)) you'll see the same tag, while (as Alain said) Ocaml can actually avoid heap allocation in similar expressions where your reference is clearly local. The lambda code remains the best option: $ cat test.ml let () = let x = ref 10 in Printf.printf "%d\n" (Obj.tag (Obj.repr !x)) let () = let x = ref 10 in Printf.printf "%d\n" (Obj.tag (Obj.repr x)) $ ocamlc -dlambda test.ml (setglobal Test! (let (match/61 (let (x/58 10) (apply (field 1 (global Printf!)) "%d\n" (caml_obj_tag (id x/58)))) match/60 (let (x/59 (makemutable 0 10)) (apply (field 1 (global Printf!)) "%d\n" (caml_obj_tag (id x/59))))) (makeblock 0))) Cheers, -- David