caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* evaluation order
@ 2009-06-14 16:36 Christophe Raffalli
  2009-06-14 17:45 ` Rémi Vanicat
  2009-06-14 19:40 ` [Caml-list] " Jon Harrop
  0 siblings, 2 replies; 4+ messages in thread
From: Christophe Raffalli @ 2009-06-14 16:36 UTC (permalink / raw)
  To: OCaml


Hello,

In OCaml-3.11.1 (I did not try other version),
the following code print 0 when compiled in bytecode and 1 in nativecode
for obvious reason of different evaluation order in the pair ...


let ptr = ref 0
let fn b =
  if b then incr ptr else decr ptr
let c = fn true, !ptr
let _ = print_int (snd c); print_newline ()

Is any difference between ocamlc and ocamlopt a bug ?

Cheers,
Christophe



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

* Re: evaluation order
  2009-06-14 16:36 evaluation order Christophe Raffalli
@ 2009-06-14 17:45 ` Rémi Vanicat
  2009-06-14 19:40 ` [Caml-list] " Jon Harrop
  1 sibling, 0 replies; 4+ messages in thread
From: Rémi Vanicat @ 2009-06-14 17:45 UTC (permalink / raw)
  To: caml-list

Christophe Raffalli <raffalli@univ-savoie.fr> writes:

> Hello,
>
> In OCaml-3.11.1 (I did not try other version),
> the following code print 0 when compiled in bytecode and 1 in nativecode
> for obvious reason of different evaluation order in the pair ...
>
>
> let ptr = ref 0
> let fn b =
>  if b then incr ptr else decr ptr
> let c = fn true, !ptr
> let _ = print_int (snd c); print_newline ()
>
> Is any difference between ocamlc and ocamlopt a bug ?

Both must follow the specification, but order of evaluation is left
unspecified in ocaml, so this difference is not a bug. The bug is in
your code that depend of an unspecified feature of one of the compiler.
-- 
Rémi Vanicat


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

* Re: [Caml-list] evaluation order
  2009-06-14 16:36 evaluation order Christophe Raffalli
  2009-06-14 17:45 ` Rémi Vanicat
@ 2009-06-14 19:40 ` Jon Harrop
  2009-06-14 21:12   ` Christophe Raffalli
  1 sibling, 1 reply; 4+ messages in thread
From: Jon Harrop @ 2009-06-14 19:40 UTC (permalink / raw)
  To: caml-list

On Sunday 14 June 2009 17:36:39 Christophe Raffalli wrote:
> Hello,
>
> In OCaml-3.11.1 (I did not try other version),
> the following code print 0 when compiled in bytecode and 1 in nativecode
> for obvious reason of different evaluation order in the pair ...
>
>
> let ptr = ref 0
> let fn b =
>   if b then incr ptr else decr ptr
> let c = fn true, !ptr
> let _ = print_int (snd c); print_newline ()
>
> Is any difference between ocamlc and ocamlopt a bug ?

Not at all, no. They are expected to have different behaviour with respect to 
floating point arithmetic. In this case, they have chosen to evaluate 
subexpressions in a different order, which is fine because the order of 
evaluation is officially undefined.

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


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

* Re: [Caml-list] evaluation order
  2009-06-14 19:40 ` [Caml-list] " Jon Harrop
@ 2009-06-14 21:12   ` Christophe Raffalli
  0 siblings, 0 replies; 4+ messages in thread
From: Christophe Raffalli @ 2009-06-14 21:12 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

Jon Harrop a écrit :
> On Sunday 14 June 2009 17:36:39 Christophe Raffalli wrote:
>   
>> Hello,
>>
>> In OCaml-3.11.1 (I did not try other version),
>> the following code print 0 when compiled in bytecode and 1 in nativecode
>> for obvious reason of different evaluation order in the pair ...
>>
>>
>> let ptr = ref 0
>> let fn b =
>>   if b then incr ptr else decr ptr
>> let c = fn true, !ptr
>> let _ = print_int (snd c); print_newline ()
>>
>> Is any difference between ocamlc and ocamlopt a bug ?
>>     
>
> Not at all, no. They are expected to have different behaviour with respect to 
> floating point arithmetic. In this case, they have chosen to evaluate 
> subexpressions in a different order, which is fine because the order of 
> evaluation is officially undefined.
>
>   
Despite the fact that it should not be written in OCaml because the 
result rely on the unspecified evaluation order, my example still seems 
to contradict chapter 11 of OCaml's documentation:

"Compatibility with the bytecode compiler is extremely high: the same 
source code should run identically when compiled with ocamlc and ocamlopt."

This kind of bug could also be a nightmare to debug ... because the 
debugger is only available for bytecode ...

Cheers,
Christophe


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

end of thread, other threads:[~2009-06-14 21:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-06-14 16:36 evaluation order Christophe Raffalli
2009-06-14 17:45 ` Rémi Vanicat
2009-06-14 19:40 ` [Caml-list] " Jon Harrop
2009-06-14 21:12   ` Christophe Raffalli

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