caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Identifying the code part of a closure
@ 2002-11-01  1:56 Alain Frisch
  2002-11-04 12:32 ` Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Alain Frisch @ 2002-11-01  1:56 UTC (permalink / raw)
  To: Caml list

Hello,

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);;

Do you see a better way to achieve this ?


-- Alain


(*) for those who are interested, what I'm trying to do is to implement a
functional language by compiling it to OCaml; the closures of the language
map directly to OCaml closures. It turns out that the language has
explicit typing for functions (for instance:  fun (t1 -> t2) x -> ...) and
the semantics of the language has to access the type information (t1 ->
t2) at runtime; all the closures for a function share this piece of
information, so I'd like to store it during program initialization in a
structure indexed by some integer computed from a sample closure of the
function.

-------------------
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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Identifying the code part of a closure
  2002-11-01  1:56 [Caml-list] Identifying the code part of a closure Alain Frisch
@ 2002-11-04 12:32 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 2002-11-04 12:32 UTC (permalink / raw)
  To: Alain Frisch; +Cc: Caml list

> 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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-11-04 12:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-01  1:56 [Caml-list] Identifying the code part of a closure Alain Frisch
2002-11-04 12:32 ` Xavier Leroy

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).