caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] native-code and byte-code compiler differences
@ 2002-03-29 21:00 Attila Kondacs
  2002-03-31 18:59 ` Michel Quercia
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Attila Kondacs @ 2002-03-29 21:00 UTC (permalink / raw)
  To: caml-list

the following is a code segment that opens a window and if 'q' is
pressed  on the keyboard it closes it. It works with the byte code
compiler fine but does not react to pressed keys when compiled with the
native code compiler (using ocaml 3.04 for debian - from the unstable
distribution).

According to the ocaml manual this may happen if there is devision by 0
caught as an exception, or stack overflow, or if signal handling is
involved. The first two cases are out of question here, and as far as I
know reading mouse and keyboard events do not involve signals. So what's
wrong here? and how can it be put right?

here is the code:


open Graphics;;
let rec find_char_action_pair = fun pairlist (a:char) ->
  match pairlist with
    | hd::tl -> if (fst hd) = a then (snd hd) else find_char_action_pair
tl a
    | [] -> (fun () -> ());;
	
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;;

open_graph "";;

map_keyboard
  [ ' ' , (fun () -> print_string "\n yes, it works!!")];;

close_graph();;	

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] native-code and byte-code compiler differences
  2002-03-29 21:00 [Caml-list] native-code and byte-code compiler differences Attila Kondacs
@ 2002-03-31 18:59 ` Michel Quercia
  2002-03-31 19:09 ` Sylvain LE GALL
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Michel Quercia @ 2002-03-31 18:59 UTC (permalink / raw)
  To: Attila Kondacs; +Cc: caml-list

Le Fri, 29 Mar 2002 16:00:41 -0500 (EST)
Attila Kondacs <attila@zurich.ai.mit.edu> écrivit :

> the following is a code segment that opens a window and if 'q' is
> pressed  on the keyboard it closes it. It works with the byte code
> compiler fine but does not react to pressed keys when compiled with the
> native code compiler (using ocaml 3.04 for debian - from the unstable
> distribution).

>       if mouse_action != (fun () -> ())
>       then (if (try button_down()...
>       if key_pressed() ...
>     done;;

I don't know the reason for those different behaviors, but you shoudn't
wait for events this way because you actually burn CPU cycles for nothing
until something happens. The following code works the same way with ocamlc
and ocamlopt :

open Graphics;;
	
let map_keyboard = fun  ?(mouse_action = (fun () -> ())) charactionlist ->
  let st = ref true in
    while !st do
      match wait_next_event [Button_down; Key_pressed] with
	| {button     = true} -> mouse_action()
	| {keypressed = true; key=a} ->
	    (try List.assoc a charactionlist () with Not_found -> ());
	    if a = 'q' then st := false
	| _ -> ()
    done;;

open_graph "";;

map_keyboard
  [ ' ' , (fun () -> print_string "\n yes, it works!!"; flush stdout)];;

close_graph();;	

Regards,
-- 
Michel Quercia
57 rue abbé Grégoire, 38000 Grenoble
http://michel.quercia.free.fr (maths)
http://pauillac.inria.fr/~quercia (informatique)
mailto:michel.quercia@prepas.org
-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] native-code and byte-code compiler differences
  2002-03-29 21:00 [Caml-list] native-code and byte-code compiler differences 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
  3 siblings, 0 replies; 5+ messages in thread
From: Sylvain LE GALL @ 2002-03-31 19:09 UTC (permalink / raw)
  To: Attila Kondacs; +Cc: caml-list

On Fri, Mar 29, 2002 at 04:00:41PM -0500, Attila Kondacs wrote:
> the following is a code segment that opens a window and if 'q' is
> pressed  on the keyboard it closes it. It works with the byte code
> compiler fine but does not react to pressed keys when compiled with the
> native code compiler (using ocaml 3.04 for debian - from the unstable
> distribution).
> 
> According to the ocaml manual this may happen if there is devision by 0
> caught as an exception, or stack overflow, or if signal handling is
> involved. The first two cases are out of question here, and as far as I
> know reading mouse and keyboard events do not involve signals. So what's
> wrong here? and how can it be put right?
> 

Your code is good and work perfectly but as you say : 
 According to the ocaml manual ... 
   Unix:
   This library is implemented under the X11 windows system. Programs
   that use the graphics library must be linked as follows:

   ocamlc other options graphics.cma other files

   For interactive use of the graphics library, do:

   ocamlmktop -o mytop graphics.cma
           ./mytop

	   or (if dynamic linking of C libraries is supported on your
	   platform), start ocaml and type #load "graphics.cma";;.


In other word no command specified for ocamlopt. So it seems to not
works properly with native compiler.

As far as my exprience, graphics is a pretty good library but as many
restriction. If you want other fault ( and it is with the bytecode ).
Try open_graphic... Unix.socket... Unix.select. For me the code crash
and give me an exception explaining an unknow error raised ( but it
works with no graphics opened ).

If i were you i try labltk or lablgtk... ( longer to code but more close
to system abstraction )

Sylvain LE GALL
-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] native-code and byte-code compiler differences
  2002-03-29 21:00 [Caml-list] native-code and byte-code compiler differences 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
  3 siblings, 0 replies; 5+ messages in thread
From: Tim Freeman @ 2002-03-31 19:32 UTC (permalink / raw)
  To: attila; +Cc: caml-list


I didn't completely anaylze the code, but I see this:

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

which has undefined behavior.  Equality among function objects is
allowed to return false at any time, although it shouldn't return true
unless they really are equal.

I also see

    while !st = true do...

which could be improved to

    while !st do...

-- 
Tim Freeman       
tim@fungible.com
-------------------
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] native-code and byte-code compiler differences
  2002-03-29 21:00 [Caml-list] native-code and byte-code compiler differences Attila Kondacs
                   ` (2 preceding siblings ...)
  2002-03-31 19:32 ` Tim Freeman
@ 2002-04-02 17:14 ` Andrej Bauer
  3 siblings, 0 replies; 5+ messages in thread
From: Andrej Bauer @ 2002-04-02 17:14 UTC (permalink / raw)
  To: Attila Kondacs; +Cc: caml-list


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-04-02 20:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-29 21:00 [Caml-list] native-code and byte-code compiler differences 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 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).