Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Identifying the code part of a closure
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] Identifying the code part of a closure
> 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