Version française
Home     About     Download     Resources     Contact us    
Browse thread
weird behavior with built-in ignore function (a bug?)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Julien Moutinho <julien.moutinho@g...>
Subject: Re: [Caml-list] weird behavior with built-in ignore function (a bug?)
On Sat, Nov 24, 2007 at 01:48:18PM -0500, Peng Zang wrote:
> Ahhh... it's good to know this is a bug and that it has been fixed.  I think 
> for the interim I will simply define my own ignore function.  It also bothers 
> me that even in the fixed version ignore does not equal itself.
Have a glance at this little thread:
  http://caml.inria.fr/pub/ml-archives/caml-list/2005/04/838a1399a42491c4db9aeaa684458a6b.en.html

> I also don't see why ignore is even defined externally.
> Doesn't the compiler optimize ignore calls away?
Well, in bytecomp/translcore.ml in the primitives_table
"%ignore" is associated with Pignore

and in asmcomp/cmmgen.ml:
  | Pignore ->
      return_unit(remove_unit (transl arg))

So I guess Pervasives.ignore is defined like that
to be able to segregate it at compile time to avoid
a function call.

% cat t.ml
ignore (Random.bits ())

% ocamlopt -dcmm t.ml
[...]
(function camlT__entry ()
 (app{random.ml:157,14-32} "camlRandom__bits_89" (load (+a "camlRandom" 52))
   unit)
 1a)
[...]


% cat tt.ml
let ignore _ = ()
in ignore (Random.bits ())

% ocamlopt -dcmm tt.ml
[...]
(data int 2295 "camlTt__1": addr "camlTt__ignore_58" int 3)
(function camlTt__ignore_58 (x/59: addr) 1a)

(function camlTt__entry ()
 (let ignore/58 "camlTt__1"
   (app{random.ml:157,14-32} "camlRandom__bits_89"
     (load (+a "camlRandom" 52)) unit))
 1a)
[...]


BTW that's the use of return_unit which disables tail-rec optimization:
% ocaml
        Objective Caml version 3.10.1+dev2 (2007-11-20)

# let rec f () = ignore (f ());;
val f : unit -> unit = <fun>
# f ();;
Stack overflow during evaluation (looping recursion?).

# let rec f () = f ();;
val f : unit -> 'a = <fun>
# f ();;
Interrupted.

# let rec f () = let _ = f () in ();;
val f : unit -> unit = <fun>
# f ();;
Stack overflow during evaluation (looping recursion?).

# let rec f () : unit = Obj.magic (f ());;
val f : unit -> unit = <fun>
# f ();;
Interrupted.

Regards,
  Julien.