Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] native-code and byte-code compiler differences
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Andrej Bauer <Andrej.Bauer@a...>
Subject: Re: [Caml-list] native-code and byte-code compiler differences

Attila Kondacs <attila@zurich.ai.mit.edu> writes:
> here is the code:
> ...
>	
> let map_keyboard = fun  ?(mouse_action = (fun () -> ())) charactionlist ->
>   let st = ref true in
>     while !st = true do
>       if mouse_action != (fun () -> ())
>       then (if (try button_down() with Graphic_failure _
> 	      -> (open_graph ""; button_down ()))
> 	    then mouse_action ());
>       if key_pressed()
>       then (match read_key() with
> 	      | 'q' -> 
> 		  begin
> 		    ((find_char_action_pair charactionlist 'q' )());
> 		    st := false 
> 		  end
> 	      | a -> ((find_char_action_pair charactionlist a )()));
>     done;;

Ignoring the fact that this is a very bad way to wait for an event, I
am very much puzzled by the line that says

  if mouse_action != (fun () -> ())

You are performing a physical inequality test on functions. I am sure
you do not want to do this. (Beware: in ocaml there are = and <>, and
there are == and !=. Make sure you know which is which.)

Consider the following example:


        Objective Caml version 3.04

# (fun () -> ()) != (fun () -> ());;
- : bool = true


So, fun () -> () and fun () -> () are not equal, since they are two
closures located in different parts of memory. Presumably, your
program wil never execute the 'else' part of the outermost
'if then else' statement, because

  mouse_action != (fun () -> ())

will always evaluate to true. It's not clear to me how your program
could work at all. You should never compare functions. Do you think
this should evaluate to true:

  (fun x -> x + x) = (fun x -> 2 * x)

How about this?

  (fun x -> x + 0) = (fun x -> x + 1 - 1)

In general, comparison of functions is not decidable. Physical
equality test (are two functions the same piece of code?) can be done,
but is _usually_ not what you want.

However, I am unable to come up with an example that uses != on
functions and behaves differently when compiled with ocamlc and
ocamlopt. Can anyone help?

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