Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Parameter evaluation order
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-08-26 (12:09)
From: Christophe Raffalli <christophe.raffalli@u...>
Subject: Re: [Caml-list] Re: Parameter evaluation order
Jon Harrop a écrit :
> On Friday 26 August 2005 10:53, Christophe Raffalli wrote:
>>This looks strange, because the semicolumn is used both to specify order
>>evaluation left-to-right in sequence and right-to-left in record.
> I haven't checked but it is probably undefined, rather than right-to-left in 
> records.
> Semicolons are used in many places in OCaml's grammar. Would you expect the 
> members of a list literal to be evaluated left-to-right, for example?
> # [print_endline "1"; print_endline "2"];;
> 2
> 1
> - : unit list = [(); ()]

yes I would ...

In fact, after though, the only place where evalauation order should be 
unspecified or better, specified right-to-left is function application 
and all other case (tuple, variants (and therefore list), should be 

Let me explain:

- why function should be right-to-left:

because the syntax is somehow wrong, the argument should come first !
A good way to think about it is when you write the operationnal 
semantics of call-by-value

if v is a value then (fun x -> t) v evaluates t[x:=v]

in this sentence we are looking at v first.

Another point: if you use a stack (either to define the semantics or to 
implement), the left most argument should be on the top of the stack and 
therefore pushed last.

Why are most language left-to-right: because nobody wanted to reverse 
the notation of function aplpication :-) (and if you think, the standard 
notation for numbers should also be reversed ... and whe should read 
from right to left, which is what we do when we do an addition with 
paper and pencil)

- why other contructor (tuple, records, variants) should be 
left-to-right: because this is the most natural thing to do, and in fact 
there is no semantical reason to choose one evaluation order or another 
(except for variant constructor if you consider that they are function 
like others). (except that they are people around the world that writes 
right-to-left or top-to-bottom ... what are the camlp4 guys doing for 
this poor people ;-)

- why should the evaluation order be specified: this is needed if you 
want to formally reason about programs ... as far as I know. I had like 
to find a system where I can prove programs in call-by-value whatever 
evaluation order they use ... but I do not know how to do that, without 
proving programs for all the possible evaluation order as soon as the 
argument have side effect which is too much.

- Yes, this is a pity since you may want the compiler to interleave the 
code of the various arguments to fill the pileline. But I think doing 
this when the code has no side effect is enough (like with arithmetic 
expression in a + b + c, d + e + f).