caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andrej Bauer <Andrej.Bauer@andrej.com>
To: Attila Kondacs <attila@zurich.ai.mit.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] native-code and byte-code compiler differences
Date: 02 Apr 2002 19:14:55 +0200	[thread overview]
Message-ID: <vkay9g6vx80.fsf@laurie.fmf.uni-lj.si> (raw)
In-Reply-To: Attila Kondacs's message of "Fri, 29 Mar 2002 16:00:41 -0500 (EST)"


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


      parent reply	other threads:[~2002-04-02 20:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-03-29 21:00 Attila Kondacs
2002-03-31 18:59 ` Michel Quercia
2002-03-31 19:09 ` Sylvain LE GALL
2002-03-31 19:32 ` Tim Freeman
2002-04-02 17:14 ` Andrej Bauer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=vkay9g6vx80.fsf@laurie.fmf.uni-lj.si \
    --to=andrej.bauer@andrej.com \
    --cc=attila@zurich.ai.mit.edu \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).