Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Accuracy of Gc.stat ()
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Kim Nguyen <nguyen@b...>
Subject: Re: [Caml-list] Accuracy of Gc.stat ()
Hello,

On Sat, 22 Nov 2003 11:49:08 +0000
Richard Jones <rich@annexia.org> wrote:


> > Neat trick. This _ought_ to work, but it doesn't for some reason:
> > 
> > 	let arg = (true, false) 
> > 	external is_bytecode : bool * bool -> bool = "%field0" "%field1" 
> >  
> > 	let () = 
> > 	  if is_bytecode arg then 
> > 	    print_endline "Bytecode!" 
> > 	  else 
> > 	    print_endline "Native!" 
> > 
> > Perhaps someone can explain why ...
> 
> OK, closer inspection of the manual[*] reveals why this doesn't work.
> Surely OCaml should flag the above code as a bug, because there are
> fewer than the magical 6 arguments?
> 
> Rich.

Well, my guess is that '%' primitives pre-defined in Ocaml are not compiled as
function. Some are just a way to bypass the typechecker (like %identity) and others
are directly translated to a sequence of bytecode/native instructions and inlined 
in the code. Thus when you use a "%primitive" in an external only the first declaration
 would by used, and so, the %field1 above would get ignored by the compiler. 
You can see it if you try to mix both kinds of primitive :

external is_bytecode : bool * bool -> bool = "field0_bytecode" "%field1" 

the compilation process fine but at link time, the linker complain about an undefiened
 symbol '$25field1'.
if you use :
external is_bytecode : bool * bool -> bool = "%field0" "field1_native"
then the "field1_native" is ignored %field0 is used in both cases.

Anyhow, I just meant to toy with the compiler and don't think using this 'feature' of
the compiler should be considered safe, as it is not specified in the documentation
(unless we have clearer explanation by an Ocaml guru :-). It could be a nice feature
though.

Regards,

Kim.