caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <xavier.leroy@inria.fr>
To: Alain Frisch <frisch@clipper.ens.fr>
Cc: Caml list <caml-list@inria.fr>
Subject: Re: [Caml-list] Identifying the code part of a closure
Date: Mon, 4 Nov 2002 13:32:06 +0100	[thread overview]
Message-ID: <20021104133206.A4916@pauillac.inria.fr> (raw)
In-Reply-To: <Pine.SOL.4.44.0211010243300.19413-100000@clipper.ens.fr>; from frisch@clipper.ens.fr on Fri, Nov 01, 2002 at 02:56:44AM +0100

> 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


      reply	other threads:[~2002-11-04 12:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-01  1:56 Alain Frisch
2002-11-04 12:32 ` Xavier Leroy [this message]

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=20021104133206.A4916@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=frisch@clipper.ens.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).