Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2002-11-04 (12:32)
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";

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 Archives:
Bug reports: FAQ:
Beginner's list: