Strange evaluation order

Nicolas Barbulesco

Michel Quercia

Nicolas Barbulesco
 Michel Quercia

Nicolas Barbulesco

Michel Quercia
[
Home
]
[ Index:
by date

by threads
]
[ Message by date: previous  next ] [ Message in thread: previous  next ] [ Thread: previous  next ]
[ Message by date: previous  next ] [ Message in thread: previous  next ] [ Thread: previous  next ]
Date:  20020519 (11:59) 
From:  Michel Quercia <michel.quercia@p...> 
Subject:  Re: "ocaml_beginners"::[] Strange evaluation order 
Le Sun, 19 May 2002 21:50:04 +0200 Nicolas Barbulesco <nbarbulesco@y...> écrivit : > > You can force a lefttoright evaluation order by stating it > > explicitely : > > > > # let f = firstOfTheTwo (double 5) in f(double 6) ;; > > double 5 > > double 6 > >  : int = 10 > > How Caml can do something different ? I don't understand why specifying > this changes. let x = something in somethingelse is granted to evaluate "something" before "somethingelse" because "something else" might depend on the identifier bound (x above). And even if "somethingelse" does not depend on this identifier, this granted evaluation order ensures that side effects are done in a known order. I thought that (because the function is curryfied) when I > call firstOfTheTwo a b Caml did exactly that : applying firstOfTheTwo to > a, and applying the result to b, which corresponds to what you wrote > with the let f = firstOfTheTwo a in f b syntax. And which corresponds > too with #trace : an intermediary function (firstOfTheTwo*) appears, and > is applied to b. I write f for firstOfTheTwo below: "f exp1 exp2" means "(f exp1) exp2" that is to say : My dear Ocaml machine, please evaluate (f exp1) and (exp2) in any order that you want, then apply the first result (f exp1) with the second one (exp2) and send me back the final result. Thanks. If you trace the function f, then you'll see that (f exp1) is evaluated before (res1 res2), but you don't see in which order res1 and res2 are evaluated. On the other hand, "let res1 = f exp1 in let res2 = exp2 in res1 res2" means : evaluate ... exactly in the order specified ... ... lazyness ... > OK, thanx. This is even a little more lazy that what I told, because b > is sometimes needed (not never) and evaluated only when needed. But, > with this ondemand evaluation, is there the side effect that if b is > needed twice it will be evaluated twice ? No. The first Lazy.force will trigger the computation of b and this result will be cached for subsequent calls to Lazy.force. > If there is not this > sideeffect, we can call this the ideal lazy evaluation, or ? We can. > By the > way, is there any advantages of eager evaluation compared to ideal lazy > evaluation ? Compactness and speed. Recording the expression to be evaluated on demand and checking if it is in the cache or not on each access costs some memory (begign) and some time (worse). Lazy values are mainly interresting (in my opinion) when dealing with infinite data streams that you don't want to expand completely before processing. Otherwise ordinary values are more efficient. Regards,  Michel Quercia 23 rue de Montchapet, 21000 Dijon http://michel.quercia.free.fr (maths) http://pauillac.inria.fr/~quercia (informatique) mailto:michel.quercia@p...