I found the following quote on the Wikipedia page on Short-circuit evaluation1.
The quote is from Edger W. Dijkstra2
The conditional connectives — "cand" and "cor" for short — are ... less innocent than they might seem at first sight. For instance, cor does not distribute over cand: compare
(A cand B) cor C with (A cor C) cand (B cor C);
in the case ¬A ∧ C , the second expression requires B to be defined, the first one does not. Because the conditional connectives thus complicate the formal reasoning about programs, they are better avoided.
— Edsger W. Dijkstra[2]
“There be a lot of long words in there; and I'm naught but a humble pirate3.”
Let's break it down.
(A && B) || C
(A || C) && (B || C)
Possibilities are:
A | B | C | (A && B) || C | (A || C) && (B || C) |
---|---|---|---|---|
false | false | false | false | false |
true | false | false | false | false |
false | true | false | false | false |
true | true | false | true | true |
false | false | true | true | true |
true | false | true | true | true |
false | true | true | true | true |
true | true | true | true | true |
So, for A = false and C = true,
(false && B) || true -> does not need to evaluate B
(false || true) && (B || true) -> does need to evaluate B.
And this while (A && B) || C == (A || C) && (B || C) holds true.
So that's interesting.
But I fear Edger W. Dijkstra may have been needlessly dogmatic here.
References
- [1] Wikipedia - Short-circuit evaluation
- https://en.wikipedia.org/wiki/Short-circuit_evaluation
- [2] Texas Univerity - Edger W. Dijkstra
- http://www.cs.utexas.edu/users/EWD/ewd10xx/EWD1009.PDF
- [3] Wikiquote - Pirates of the Caribbean: The Curse of the Black Pearl
- https://en.wikiquote.org/wiki/Pirates_of_the_Caribbean:_The_Curse_of_the_Black_Pearl
No comments:
Post a Comment