P is for Practical | |
PerlMonks |
Re^2: Why is the execution order of subexpressions undefined? (basics)by BrowserUk (Patriarch) |
on Apr 17, 2005 at 03:21 UTC ( [id://448565]=note: print w/replies, xml ) | Need Help?? |
Maybe, just maybe, I've hit upon the explanation that will calrify the defined EO thing. Your assertion is that : If I write $x = f() + g(); and the order of evaluation is defined, let's say from left to right, then f() must be called before g(). First, let's assume that for reasons of generalisation, that the order defined is right to left. That's okay in your example because addition is communicative, so your expression will still do the right thing. But, now let's switch to a non communicative operator: $x = f() - g();. What happens? Nothing. Because the defined EO only pertains to the order in which the functions are called, not to the order in which the results they produce are used. The defined EO doesn't force the results to be used in reverse. Now come back to your assertion that defined EO means that f() and g() cannot be parallelised because f() must be called before g(). Why? Once f() and g() have returned their values, you have a simple expression: $rv1 + $rv2; and there is nothing about defined EO that prevents that from being completed. And prior to that, f() and g() can be called in parallel, because I'm explicitly stating that with defined EO, parallelisable operations either side of a non-serialising operator can be called in parallel. That is, I'm not just saying I think they can, I'm saying that defined EO can be defined such that it is so. Where the defined EO comes into play is when you have a compound expression. This is, as you picked up on when you said: Now, BrowserUk had an example closer to $x = f($i) + g(++$i); and so perhaps BrowserUk is thinking that a defined EO is important to allow the compiler to add parallelization because otherwise writing this expression is basically an error -- because what gets passed to f() and/or g() depends on the implementation. If so, then he has a point there. the purpose of the parameters I showed in my examples. They are the simplest sub-expressions that have a side effect, which is their entire purpose for being there. It is at the level of the compound expression, where the defined EO becomes important. I'm going to contrive an example for the purposes of discussion. It will probably be a weak and somewhat non-sensical in it's contrivance, but I'm admitting that up front to avoid the ensuing argument about it. I'm contriving the circumstance using the simplest expressions possible to demonstrate the point, but the expressions and logic behind them act as placeholders for any similar set of circumstances that could exist. Here I want the first value returned by $o->next given to f() and the second to g():
Here, I want the reverse:
Yes, I can achieve the same thing in the presence of undefined EO by doing:
and reversed
but why should I have to? Did you see how much clearer the first pair are than the second? And did you notice that the second pair contain an error? An error that would have stood out like a sore thumb in the first pair, but that gets lost in the noise of the second. Of course, nothing is forcing anyone to use the first form if EO is defined, it just enables it for those that wish to. There is no shotgun either way with defined EO, but without it one option is excluded--not very Perlish. And yes, in the presence of defined EO, f() and g() can be executed in parallel--if we chose to allow that. No compiler analysis is required, the programmer is explicitly condoning it. If that does not convince you that defined EO is a gain-something, lose-nothing proposal, then probably nothing will, but your being unconvinced will not change that it is so. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco.
Rule 1 has a caveat! -- Who broke the cabal?
In Section
Seekers of Perl Wisdom
|
|