From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA05234; Mon, 4 Nov 2002 13:32:08 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id NAA05025 for ; Mon, 4 Nov 2002 13:32:07 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id gA4CW6529071; Mon, 4 Nov 2002 13:32:06 +0100 (MET) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA05415; Mon, 4 Nov 2002 13:32:06 +0100 (MET) Date: Mon, 4 Nov 2002 13:32:06 +0100 From: Xavier Leroy To: Alain Frisch Cc: Caml list Subject: Re: [Caml-list] Identifying the code part of a closure Message-ID: <20021104133206.A4916@pauillac.inria.fr> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: ; from frisch@clipper.ens.fr on Fri, Nov 01, 2002 at 02:56:44AM +0100 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > for some reasons(*), I need to associate to a runtime closure an integer > that identifies its code pointer. Is the following a "safe" solution > for ocamlc and ocamlopt ("safe" means here that it works in all cases, > with the current implementation, under all the architectures where > ocamlopt exists) ? > > let addr (h : 'a -> 'b) : int = fst (Obj.magic h);; It will not work as you expect in the case of curried functions compiled with ocamlopt: let f x y = x + y let g x y = x - y let _ = if addr f = addr g then print_string "equal" else print_string "different"; print_newline() This will print "different" when compiled with ocamlc, and "equal" when compiled with ocamlopt. The reason is that ocamlopt represents closures for curried functions as follows: field 0 contains a pointer to a generic, shared "un-currying" combinator that handles partial applications; the real code pointer is stored elsewhere (in field 2). - Xavier Leroy ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners