* Efficiency of let/and @ 2005-09-25 13:31 Brian Hurt 2005-09-25 14:47 ` [Caml-list] " Jon Harrop 2005-09-26 4:32 ` Ant: " Martin Chabr 0 siblings, 2 replies; 21+ messages in thread From: Brian Hurt @ 2005-09-25 13:31 UTC (permalink / raw) To: caml-list Say I have two variables I want to set- variable a to the value expr1 and variable b to the value expr2. The two expressions are pure (no side effects), and neither one depends upon the other (neither expr1 nor expr2 contain either a or b as a value), so they can be evaluated in either order or in parallel with no harm. With expressions like these, I've gotten into the habit of using let/and to express the parallelism, that is I go: let a = expr1 and b = expr2 in ... rather than: let a = expr1 in let b = expr2 in So my question is: is there any value (other than the documentation value) in doing this? Just wondering. Brian ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [Caml-list] Efficiency of let/and 2005-09-25 13:31 Efficiency of let/and Brian Hurt @ 2005-09-25 14:47 ` Jon Harrop 2005-09-26 4:32 ` Ant: " Martin Chabr 1 sibling, 0 replies; 21+ messages in thread From: Jon Harrop @ 2005-09-25 14:47 UTC (permalink / raw) To: caml-list On Sunday 25 September 2005 14:31, Brian Hurt wrote: > So my question is: is there any value (other than the documentation value) > in doing this? Good question. I've no idea. However, I did notice that this change has been undone (i.e. to use "let" twice and not "and") in the stdlib. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. Objective CAML for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists ^ permalink raw reply [flat|nested] 21+ messages in thread
* Ant: [Caml-list] Efficiency of let/and 2005-09-25 13:31 Efficiency of let/and Brian Hurt 2005-09-25 14:47 ` [Caml-list] " Jon Harrop @ 2005-09-26 4:32 ` Martin Chabr 2005-09-26 5:24 ` Fernando Alegre ` (3 more replies) 1 sibling, 4 replies; 21+ messages in thread From: Martin Chabr @ 2005-09-26 4:32 UTC (permalink / raw) To: Brian Hurt; +Cc: caml-list As it appears to me, there is no semantic difference between both alternatives. It can be shown with two dependent expressions y = 1 and z = y + 2: # let y = 1 in let z = y + 2 in z;; - : int = 3 # let y = 1 and z = y + 2 in z;; - : int = 3 The order is important in both cases: # let z = y + 2 in let y = x + 1 in z;; Characters 8-9: let z = y + 2 in ^ Unbound value y # let z = y + 2 and y = 1 in z;; Characters 8-9: let z = y + 2 ^ Unbound value y So the "and"-form depends on the order as well and I think the syntactic difference can be just used for documentation. A good idea, by the way. I hope this helps Martin --- Brian Hurt <bhurt@spnz.org> schrieb: > > Say I have two variables I want to set- variable a > to the value expr1 and > variable b to the value expr2. The two expressions > are pure (no side > effects), and neither one depends upon the other > (neither expr1 nor expr2 > contain either a or b as a value), so they can be > evaluated in either > order or in parallel with no harm. With expressions > like these, I've > gotten into the habit of using let/and to express > the parallelism, that is > I go: > > let a = expr1 > and b = expr2 > in > ... > > rather than: > let a = expr1 in > let b = expr2 in > > So my question is: is there any value (other than > the documentation value) > in doing this? > > Just wondering. > > Brian > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: > http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > ___________________________________________________________ Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: Ant: [Caml-list] Efficiency of let/and 2005-09-26 4:32 ` Ant: " Martin Chabr @ 2005-09-26 5:24 ` Fernando Alegre 2005-09-26 5:56 ` William Lovas ` (2 subsequent siblings) 3 siblings, 0 replies; 21+ messages in thread From: Fernando Alegre @ 2005-09-26 5:24 UTC (permalink / raw) To: Martin Chabr; +Cc: Brian Hurt, caml-list There is a small advantage in using "and" instead of "in let": # let () = let x = 2 and x = 5 in ();; This variable is bound several times in this matching # let () = let x = 2 in let x = 5 in ();; Probably the second "x" should have been a "y"... so the compiler catches that mistake. Fernando > > let a = expr1 > > and b = expr2 > > in > > ... > > > > rather than: > > let a = expr1 in > > let b = expr2 in > > > > So my question is: is there any value (other than > > the documentation value) > > in doing this? ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: Ant: [Caml-list] Efficiency of let/and 2005-09-26 4:32 ` Ant: " Martin Chabr 2005-09-26 5:24 ` Fernando Alegre @ 2005-09-26 5:56 ` William Lovas 2005-09-26 7:17 ` Bill Wood 2005-09-26 20:59 ` Ant: " Martin Chabr 2005-09-26 13:22 ` Brian Hurt 2005-09-26 17:05 ` Marius Nita 3 siblings, 2 replies; 21+ messages in thread From: William Lovas @ 2005-09-26 5:56 UTC (permalink / raw) To: caml-list On Mon, Sep 26, 2005 at 06:32:40AM +0200, Martin Chabr wrote: > As it appears to me, there is no semantic difference > between both alternatives. It can be shown with two > dependent expressions y = 1 and z = y + 2: This is not universally true: > # let y = 1 > and z = y + 2 in > z;; > - : int = 3 Objective Caml version 3.08.1 # let y = 1 and z = y + 2 in z;; Unbound value y So either you are using a version older than 3.08.1 or this is a fairly recent change. In the latter case, people who wish to remain backward- compatible might eschew this style for sequential bindings, regardless of any potential performance problems. William ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: Ant: [Caml-list] Efficiency of let/and 2005-09-26 5:56 ` William Lovas @ 2005-09-26 7:17 ` Bill Wood 2005-09-26 20:59 ` Ant: " Martin Chabr 1 sibling, 0 replies; 21+ messages in thread From: Bill Wood @ 2005-09-26 7:17 UTC (permalink / raw) To: caml-list . . . > This is not universally true: > > > # let y = 1 > > and z = y + 2 in > > z;; > > - : int = 3 > > Objective Caml version 3.08.1 > > # let y = 1 > and z = y + 2 in > z;; > Unbound value y > > So either you are using a version older than 3.08.1 or this is a fairly > recent change. In the latter case, people who wish to remain backward- > compatible might eschew this style for sequential bindings, regardless > of any potential performance problems. I'm using Objective Caml version 3.08.3 and had this interaction: # let y = 1 and z = y + 2 in z;; Unbound value y # -- Bill Wood bill.wood@acm.org ^ permalink raw reply [flat|nested] 21+ messages in thread
* Ant: Re: Ant: [Caml-list] Efficiency of let/and 2005-09-26 5:56 ` William Lovas 2005-09-26 7:17 ` Bill Wood @ 2005-09-26 20:59 ` Martin Chabr 1 sibling, 0 replies; 21+ messages in thread From: Martin Chabr @ 2005-09-26 20:59 UTC (permalink / raw) To: William Lovas; +Cc: caml-list Yes, you are right. Shame on me. I must have had a bounding for y from an earlier experiment. When I restart the interpreter, so that it is in a virgin state, I get the same error message about unbound y as you. Martin --- William Lovas <wlovas@stwing.upenn.edu> schrieb: > On Mon, Sep 26, 2005 at 06:32:40AM +0200, Martin > Chabr wrote: > > As it appears to me, there is no semantic > difference > > between both alternatives. It can be shown with > two > > dependent expressions y = 1 and z = y + 2: > > This is not universally true: > > > # let y = 1 > > and z = y + 2 in > > z;; > > - : int = 3 > > Objective Caml version 3.08.1 > > # let y = 1 > and z = y + 2 in > z;; > Unbound value y > > So either you are using a version older than 3.08.1 > or this is a fairly > recent change. In the latter case, people who wish > to remain backward- > compatible might eschew this style for sequential > bindings, regardless > of any potential performance problems. > > William > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: > http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > Martin Chabr Hochstrasse 28 8044 Zürich Schweiz / Switzerland Tel.P.: 01-261 17 24 ___________________________________________________________ Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: Ant: [Caml-list] Efficiency of let/and 2005-09-26 4:32 ` Ant: " Martin Chabr 2005-09-26 5:24 ` Fernando Alegre 2005-09-26 5:56 ` William Lovas @ 2005-09-26 13:22 ` Brian Hurt 2005-09-26 16:05 ` Ant: " Stefan Monnier 2005-09-26 17:04 ` Ant: [Caml-list] " Mackenzie Straight 2005-09-26 17:05 ` Marius Nita 3 siblings, 2 replies; 21+ messages in thread From: Brian Hurt @ 2005-09-26 13:22 UTC (permalink / raw) To: Martin Chabr; +Cc: caml-list On Mon, 26 Sep 2005, Martin Chabr wrote: > As it appears to me, there is no semantic difference > between both alternatives. It can be shown with two > dependent expressions y = 1 and z = y + 2: > > # let y = 1 in > let z = y + 2 in > z;; > - : int = 3 Actually, my example would be more like: let y = 1 in let z = 2 in ... vr.s let y = 1 and z = 2 in ... Or, more commonly, something like: let foo arr1 arr2 = (* I need the lengths of both arrays *) let len1 = Array.length arr1 and len2 = Array.length arr2 in ... Syntactically and semantically there is no difference. I was wondering if the ocamlopt compiler took advatange of the implicit paralellism at all. Brian ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: Ant: Efficiency of let/and 2005-09-26 13:22 ` Brian Hurt @ 2005-09-26 16:05 ` Stefan Monnier 2005-09-26 16:30 ` [Caml-list] " Brian Hurt 2005-09-27 5:32 ` skaller 2005-09-26 17:04 ` Ant: [Caml-list] " Mackenzie Straight 1 sibling, 2 replies; 21+ messages in thread From: Stefan Monnier @ 2005-09-26 16:05 UTC (permalink / raw) To: caml-list > Syntactically and semantically there is no difference. I was wondering if > the ocamlopt compiler took advatange of the implicit paralellism at all. If someone tries to use such things as `and' or unspecified-argument-evaluation-order in the hopes that the compiler will extract some imagined parallelism is simply deluding himself. In some cases, the freedom to execute in any order does lead to better code, but that code rarely if ever uses any kind of parallelism. Stefan ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [Caml-list] Re: Ant: Efficiency of let/and 2005-09-26 16:05 ` Ant: " Stefan Monnier @ 2005-09-26 16:30 ` Brian Hurt 2005-09-27 5:52 ` skaller 2005-09-27 5:32 ` skaller 1 sibling, 1 reply; 21+ messages in thread From: Brian Hurt @ 2005-09-26 16:30 UTC (permalink / raw) To: Stefan Monnier; +Cc: caml-list On Mon, 26 Sep 2005, Stefan Monnier wrote: >> Syntactically and semantically there is no difference. I was wondering if >> the ocamlopt compiler took advatange of the implicit paralellism at all. > > If someone tries to use such things as `and' or > unspecified-argument-evaluation-order in the hopes that the compiler will > extract some imagined parallelism is simply deluding himself. > In some cases, the freedom to execute in any order does lead to better > code, but that code rarely if ever uses any kind of parallelism. I was thinking of instruction-level parallelism- the ability of the compiler to reorder instructions to better use the available functional units, not thread-level parallelism. Sorry for not being clear there. I'm not even sure how much extra efficiency is there to be had. Obviously it'd be hard "thread" calls to complex functions, so code like: let foo lst1 lst2 = let len1 = List.length lst1 and len2 = List.length lst2 in ... wouldn't be helped- it'd be computationally infeasible for the compiler to interleave the two different calls to List.length. So you'd pretty obviously be limited to "simple" expressions, at which point the CPU's own prefetching and reordering is likely to do the work for you wether the compiler does it or not. In fact, the CPU's reordering can start executing the code to List.length lst2 speculatively before the call to List.length lst1 is complete, and in that sense the CPU's reordering is more capable then what the compiler can do (this, BTW, is the fundamental problem with the Itanium). Brian ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [Caml-list] Re: Ant: Efficiency of let/and 2005-09-26 16:30 ` [Caml-list] " Brian Hurt @ 2005-09-27 5:52 ` skaller 2005-09-27 13:06 ` Brian Hurt 0 siblings, 1 reply; 21+ messages in thread From: skaller @ 2005-09-27 5:52 UTC (permalink / raw) To: Brian Hurt; +Cc: Stefan Monnier, caml-list On Mon, 2005-09-26 at 11:30 -0500, Brian Hurt wrote: > I'm not even sure how much extra efficiency is there to be had. Obviously > it'd be hard "thread" calls to complex functions, Why? Hyperthreading allows two completely independent processes to execute on a hyperthread enabled P4 .. the hardware can already do it .. even better with dual core. There is no lack of small scale low level parallelism in modern computing systems, just a lack of software that knows how to take advantage of it. There are plenty of places in an average program where one can determine parallel execution would be ok, so it is really a lack of capability in the software. I personally don't think of this as real parallelism, that's something you get on a machine with K's or M's of processing units .. eg the human eye. -- John Skaller <skaller at users dot sf dot net> Felix, successor to C++: http://felix.sf.net ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [Caml-list] Re: Ant: Efficiency of let/and 2005-09-27 5:52 ` skaller @ 2005-09-27 13:06 ` Brian Hurt 2005-09-27 13:24 ` Alan Falloon 2005-09-27 15:24 ` Stefan Monnier 0 siblings, 2 replies; 21+ messages in thread From: Brian Hurt @ 2005-09-27 13:06 UTC (permalink / raw) To: skaller; +Cc: Stefan Monnier, caml-list On Tue, 27 Sep 2005, skaller wrote: > On Mon, 2005-09-26 at 11:30 -0500, Brian Hurt wrote: > >> I'm not even sure how much extra efficiency is there to be had. Obviously >> it'd be hard "thread" calls to complex functions, > > Why? Hyperthreading allows two completely independent processes > to execute on a hyperthread enabled P4 .. the hardware can already > do it .. even better with dual core. Creating a new kernel-level process/thread (required to get code executing on a different processor or pseudo-processor) is generally expensive. You don't want to do it except for very large functions. And then, once you do have the second thread of execution, you now have all the fun of multithreaded code- race conditions and deadlocks and livelocks, oh my. I have contemplated writting a purely-functional (no imperitive) language that does micro-threads ala cilk- but it's more work than I really want to put in to that project. > > There is no lack of small scale low level parallelism in > modern computing systems, just a lack of software that knows > how to take advantage of it. The benefit may be there, theoretically, but practical considerations may make it not worth the effort to go after the benefit. > > There are plenty of places in an average program where one > can determine parallel execution would be ok, so it is really > a lack of capability in the software. > > I personally don't think of this as real parallelism, > that's something you get on a machine with K's or M's > of processing units .. eg the human eye. Heh. We've hit the point where we have so many transistors on a chip we literally don't know what to do with them all- we have no idea how to spend the transistors to provide more than very small incremental performance improvements to single-threaded execution. Which is why the sudden interest in parallelism (Symmetric Mult-Threading aka Hyperthreading, multi-core chips, etc.). The problem is that the theory on how to write race condition/deadlock/livelock -free code isn't there, to my knowledge (someone please prove me wrong). Brian ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [Caml-list] Re: Ant: Efficiency of let/and 2005-09-27 13:06 ` Brian Hurt @ 2005-09-27 13:24 ` Alan Falloon 2005-09-27 15:24 ` Stefan Monnier 1 sibling, 0 replies; 21+ messages in thread From: Alan Falloon @ 2005-09-27 13:24 UTC (permalink / raw) To: Brian Hurt; +Cc: skaller, Stefan Monnier, caml-list Brian Hurt wrote: > On Tue, 27 Sep 2005, skaller wrote: > >> I personally don't think of this as real parallelism, >> that's something you get on a machine with K's or M's >> of processing units .. eg the human eye. > > > Heh. We've hit the point where we have so many transistors on a chip > we literally don't know what to do with them all- we have no idea how > to spend the transistors to provide more than very small incremental > performance improvements to single-threaded execution. Which is why > the sudden interest in parallelism (Symmetric Mult-Threading aka > Hyperthreading, multi-core chips, etc.). The problem is that the > theory on how to write race condition/deadlock/livelock -free code > isn't there, to my knowledge (someone please prove me wrong). I did see a paper called "Composable Memory Transactions" by some of the more well know Haskell researchers its avaliable at http://got.net/~landauer/cs/fp/ea8-composablememory_stm.pdf The idea is to introduce new concurrency abstractions (a replacement for mutex and friends) that are composable to make it easy to reason about thread-safe sections in isolation. ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [Caml-list] Re: Ant: Efficiency of let/and 2005-09-27 13:06 ` Brian Hurt 2005-09-27 13:24 ` Alan Falloon @ 2005-09-27 15:24 ` Stefan Monnier 2005-09-27 16:11 ` Brian Hurt 1 sibling, 1 reply; 21+ messages in thread From: Stefan Monnier @ 2005-09-27 15:24 UTC (permalink / raw) To: Brian Hurt; +Cc: skaller, caml-list > chips, etc.). The problem is that the theory on how to write race > condition/deadlock/livelock -free code isn't there, to my knowledge I think the theory is pretty much there. The problem is to put it into practice. Stefan ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [Caml-list] Re: Ant: Efficiency of let/and 2005-09-27 15:24 ` Stefan Monnier @ 2005-09-27 16:11 ` Brian Hurt 0 siblings, 0 replies; 21+ messages in thread From: Brian Hurt @ 2005-09-27 16:11 UTC (permalink / raw) To: Stefan Monnier; +Cc: skaller, caml-list On Tue, 27 Sep 2005, Stefan Monnier wrote: >> chips, etc.). The problem is that the theory on how to write race >> condition/deadlock/livelock -free code isn't there, to my knowledge > > I think the theory is pretty much there. The problem is to put it > into practice. I'm not 100% sure it is. Oh, it is for the "trivial" questions- like will this program run to completion and will it return the right answer? But it isn't there yet (to my knowledge) on the more difficult questions, like can I automatically tune this program to efficiently use N threads (where both N=1 and N ~ millions are interesting values)? And what is the minimum amount of synchronization this program requires at N threads? Etc. IMHO, we need to take synchronization out of the hands of the programmer just like we took garbage collection out of the hands of the programmer. But, then again, I'm widely regarded as a radical :-). Brian ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [Caml-list] Re: Ant: Efficiency of let/and 2005-09-26 16:05 ` Ant: " Stefan Monnier 2005-09-26 16:30 ` [Caml-list] " Brian Hurt @ 2005-09-27 5:32 ` skaller 2005-09-27 15:21 ` Stefan Monnier 1 sibling, 1 reply; 21+ messages in thread From: skaller @ 2005-09-27 5:32 UTC (permalink / raw) To: Stefan Monnier; +Cc: caml-list On Mon, 2005-09-26 at 12:05 -0400, Stefan Monnier wrote: > > Syntactically and semantically there is no difference. I was wondering if > > the ocamlopt compiler took advatange of the implicit paralellism at all. > > If someone tries to use such things as `and' or > unspecified-argument-evaluation-order in the hopes that the compiler will > extract some imagined parallelism is simply deluding himself. > In some cases, the freedom to execute in any order does lead to better > code, but that code rarely if ever uses any kind of parallelism. This is not so at all. Limited Parallelism is indeed found in all modern processors, which can, for example, distribute multiple instructions on several pipelines, decode in parallel with computing values, or perform several integer and/or floating operations simultaneously. In fact, as far back as 197x, the Cyber 70 could do this, in fact it relied on it. In particular, register calculations and memory fetches and stores we always done in parallel, automatically, until a join point. When you did a load, for example, the next instruction would be executed immediately, without waiting for the load to complete, provided it didn't use the register being loaded (and I think .. it would then proceed to the next instruction, and execute THAT whilst suspending the previous one, provided it used distinct registers .. but I'm not sure). It may well be that this has no impact on Ocaml code. However it certainly DOES have an impact on low level C code. In addition, parallel assignment is a major benefit, not because the assignments are done concurrently, but because it can save memory. For example: x,y = 1,2 cf x,y = y,x The first pair of assignments is faster and uses no temporaries, the second requires a temporary. So an arbitrary parallel assignment can, in fact, be optimised to reduce the number of temporaries used, and thus the number of operations. Parallel assignments as written aren't that useful, however there is a particular optimisation of great benefit which is requires parallel assignment: self-tail-recursion. In this case, the assignment arises through reusing the same closure, and the optimisation is of the form: x,y,x = f(x,y,z), g(x,y,z), h(x,y,x) goto start In particular, the argument of the new invocation can depend on the argument of the old invocation, and, in tuple form the assignment would, without analysis, require a temporary the size of the argument: tmp = f(x,y,z), g(x,y,z), h(x,y,z) x,y,z = tmp Which, in the worst case, *doubles* the number of assignments, and thus the cost of looping -- in a tight inner loop this can be critical. In a language like Ocaml, a parallel operation can have a major performance advantage, because it explicitly indicates to the compiler that the order of evaluation is irrelevant: due to side-effects and separate compilation, the order of evaluation of sequential assignments is likely to be fixed, and therefore cannot be optimised. I do not know whether Ocaml does parallel assignment optimisations on self-tail recursions .. but I can tell you that Felix DOES. Perhaps this is why it calculates Ackermann's function faster than Ocaml and C? [I have no idea of the real reason .. but it does] -- John Skaller <skaller at users dot sf dot net> Felix, successor to C++: http://felix.sf.net ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [Caml-list] Re: Ant: Efficiency of let/and 2005-09-27 5:32 ` skaller @ 2005-09-27 15:21 ` Stefan Monnier 0 siblings, 0 replies; 21+ messages in thread From: Stefan Monnier @ 2005-09-27 15:21 UTC (permalink / raw) To: skaller; +Cc: caml-list >> If someone tries to use such things as `and' or >> unspecified-argument-evaluation-order in the hopes that the compiler will >> extract some imagined parallelism is simply deluding himself. >> In some cases, the freedom to execute in any order does lead to better >> code, but that code rarely if ever uses any kind of parallelism. > This is not so at all. Limited Parallelism is indeed found in all > modern processors, which can, for example, distribute multiple > instructions on several pipelines, decode in parallel with > computing values, or perform several integer and/or floating > operations simultaneously. This has nothing to do with what I said. Compilers can equally take advantage of ILP with a `let' or with a specified evaluation order. Stefan ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: Ant: [Caml-list] Efficiency of let/and 2005-09-26 13:22 ` Brian Hurt 2005-09-26 16:05 ` Ant: " Stefan Monnier @ 2005-09-26 17:04 ` Mackenzie Straight 1 sibling, 0 replies; 21+ messages in thread From: Mackenzie Straight @ 2005-09-26 17:04 UTC (permalink / raw) To: Brian Hurt; +Cc: Martin Chabr, caml-list On 9/26/05, Brian Hurt <bhurt@spnz.org> wrote: > Syntactically and semantically there is no difference. I was wondering if > the ocamlopt compiler took advatange of the implicit paralellism at all. In any case, it makes (as far as I know) no difference which syntax you use: % ocaml -drawlambda Objective Caml version 3.08.3 # let foo a1 a2 = let l1 = Array.length a1 and l2 = Array.length a2 in l1+l2;; (let (foo/65 (function a1/66 a2/67 (let (l1/68 (array.length a1/66) l2/69 (array.length a2/67)) (+ l1/68 l2/69)))) (apply (field 1 (global Toploop!)) "foo" foo/65)) val foo : 'a array -> 'b array -> int = <fun> # let bar a1 a2 = let l1 = Array.length a1 in let l2 = Array.length a2 in l1+l2;; (let (bar/70 (function a1/71 a2/72 (let (l1/73 (array.length a1/71) l2/74 (array.length a2/72)) (+ l1/73 l2/74)))) (apply (field 1 (global Toploop!)) "bar" bar/70)) val bar : 'a array -> 'b array -> int = <fun> ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: Ant: [Caml-list] Efficiency of let/and 2005-09-26 4:32 ` Ant: " Martin Chabr ` (2 preceding siblings ...) 2005-09-26 13:22 ` Brian Hurt @ 2005-09-26 17:05 ` Marius Nita 2005-09-26 17:36 ` David McClain 3 siblings, 1 reply; 21+ messages in thread From: Marius Nita @ 2005-09-26 17:05 UTC (permalink / raw) To: Martin Chabr; +Cc: caml-list Martin Chabr wrote: > As it appears to me, there is no semantic difference > between both alternatives. I've always thought that there was. In the `and' form, the bindings cannot depend on each other. The program you pasted shouldn't run in any (recent) OCaml toplevel. Are you using a really old one or something? -marius It can be shown with two > dependent expressions y = 1 and z = y + 2: > > # let y = 1 in > let z = y + 2 in > z;; > - : int = 3 > > # let y = 1 > and z = y + 2 in > z;; > - : int = 3 > > The order is important in both cases: > > # let z = y + 2 in > let y = x + 1 in > z;; > Characters 8-9: > let z = y + 2 in > ^ > Unbound value y > > # let z = y + 2 > and y = 1 in > z;; > Characters 8-9: > let z = y + 2 > ^ > Unbound value y > > So the "and"-form depends on the order as well and I > think the syntactic difference can be just used for > documentation. A good idea, by the way. > > I hope this helps > > Martin > > > > --- Brian Hurt <bhurt@spnz.org> schrieb: > >> Say I have two variables I want to set- variable a >> to the value expr1 and >> variable b to the value expr2. The two expressions >> are pure (no side >> effects), and neither one depends upon the other >> (neither expr1 nor expr2 >> contain either a or b as a value), so they can be >> evaluated in either >> order or in parallel with no harm. With expressions >> like these, I've >> gotten into the habit of using let/and to express >> the parallelism, that is >> I go: >> >> let a = expr1 >> and b = expr2 >> in >> ... >> >> rather than: >> let a = expr1 in >> let b = expr2 in >> >> So my question is: is there any value (other than >> the documentation value) >> in doing this? >> >> Just wondering. >> >> Brian >> >> _______________________________________________ >> Caml-list mailing list. Subscription management: >> > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list >> Archives: http://caml.inria.fr >> Beginner's list: >> http://groups.yahoo.com/group/ocaml_beginners >> Bug reports: http://caml.inria.fr/bin/caml-bugs >> > > > > > > ___________________________________________________________ > Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: Ant: [Caml-list] Efficiency of let/and 2005-09-26 17:05 ` Marius Nita @ 2005-09-26 17:36 ` David McClain 0 siblings, 0 replies; 21+ messages in thread From: David McClain @ 2005-09-26 17:36 UTC (permalink / raw) To: caml I have understood that the "and" syntax was needed to support the creation of mutually recursive bindings. While it can also be used as a form of "parallel let" within a function definition, it is vitally important for the mutual recursion case. - David McClain, Tucson, AZ, USA ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [Caml-list] Efficiency of let/and
@ 2005-09-26 23:25 Oliver Bandel
0 siblings, 0 replies; 21+ messages in thread
From: Oliver Bandel @ 2005-09-26 23:25 UTC (permalink / raw)
To: caml-list
On Sun, Sep 25, 2005 at 08:31:55AM -0500, Brian Hurt wrote:
>
> Say I have two variables I want to set- variable a to the value expr1 and
> variable b to the value expr2. The two expressions are pure (no side
> effects), and neither one depends upon the other (neither expr1 nor expr2
> contain either a or b as a value), so they can be evaluated in either
> order or in parallel with no harm. With expressions like these, I've
> gotten into the habit of using let/and to express the parallelism, that is
> I go:
>
> let a = expr1
> and b = expr2
> in
> ...
>
> rather than:
> let a = expr1 in
> let b = expr2 in
>
> So my question is: is there any value (other than the documentation value)
> in doing this?
When you write it in the second form, then the "let a ="
must be evaluated before the "let b =" expression, because "a"
is used in the "b" expression (or might be used, but "a"
is part of "b"'s environment, so that it must be evaluated
first).
You can use this form also for doing imperative operations in
a certain order, because the more-outer expression is evaluated first.
If you use the form with "and" you can use definitions for functions,
that call each other, so called mutually recursive functions.
(For types this is also possible (e.g. tree-stuff).)
An example (that makes no sense to be used and will raise an
Stack-overflow exception, but I want to show, what is possible to define):
=============================================================
# let rec f1 x = f2 x + 10
and f2 x = f1 x + 100;;
val f1 : 'a -> int = <fun>
val f2 : 'a -> int = <fun>
# f2 3;;
Stack overflow during evaluation (looping recursion?).
#
=============================================================
One could try to use "rec" for binding of simple values
(not functions with parameters), but this would make no sense,
and if evaluated would also create an overflow exception.
When defining mutually recursive functions, the recursion would
start, when calling the function with it's arguments, so that
the resulting value would be calculated. E.g. in the toplevel,
you can do this by calling the functions with a parameter
(as done above).
But when you have a simple value, and not a function,
then it's value would be calculated right after the
definition, and so the definition of that value
would rise the exception, because the expression
would be immediately evaluated (eager evaluation).
This would probably yield to difficulties, and that
seems to me to be the reason, why it is forbidden to use such
an expression:
============================================================
first:~ oliver$ ocaml
Objective Caml version 3.08.0
# let rec a = 4
and b = 6;;
val a : int = 4
val b : int = 6
# let rec a = b + 4
and b = a * 8;;
This kind of expression is not allowed as right-hand side of `let rec'
#
============================================================
So, you can use "and" for defining some variables at the same
scope/environment in parallel, that do not depend on each other...
... but if you define functions, you can also define functions,
that depend on each other (mutually recursive calls).
Hope this helps.
Ciao,
Oliver
^ permalink raw reply [flat|nested] 21+ messages in thread
end of thread, other threads:[~2005-09-27 16:10 UTC | newest] Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2005-09-25 13:31 Efficiency of let/and Brian Hurt 2005-09-25 14:47 ` [Caml-list] " Jon Harrop 2005-09-26 4:32 ` Ant: " Martin Chabr 2005-09-26 5:24 ` Fernando Alegre 2005-09-26 5:56 ` William Lovas 2005-09-26 7:17 ` Bill Wood 2005-09-26 20:59 ` Ant: " Martin Chabr 2005-09-26 13:22 ` Brian Hurt 2005-09-26 16:05 ` Ant: " Stefan Monnier 2005-09-26 16:30 ` [Caml-list] " Brian Hurt 2005-09-27 5:52 ` skaller 2005-09-27 13:06 ` Brian Hurt 2005-09-27 13:24 ` Alan Falloon 2005-09-27 15:24 ` Stefan Monnier 2005-09-27 16:11 ` Brian Hurt 2005-09-27 5:32 ` skaller 2005-09-27 15:21 ` Stefan Monnier 2005-09-26 17:04 ` Ant: [Caml-list] " Mackenzie Straight 2005-09-26 17:05 ` Marius Nita 2005-09-26 17:36 ` David McClain 2005-09-26 23:25 Oliver Bandel
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox