[Haskell-cafe] Why are && and || right-associative?
Ivan Perez
ivanperezdominguez at gmail.com
Fri Apr 12 09:44:33 UTC 2019
Correct me if I'm wrong here.
On Fri, 12 Apr 2019 at 05:21, Richard O'Keefe <raoknz at gmail.com> wrote:
> How does the right associativity of the short-circuiting
> Boolean operators in any way contradict the way that such operators work
> in other languages? These operators are associative, so a && (b && c)
> necessarily has the same value and effects as (a && b) && c.
>
In pure Haskell, perhaps, but in other languages, I would say no.
In a language like C, I would expect that:
- a && b && c be represented in the AST as (a && b) && c
- The compiler optimizes the implementation of && to short circuit, which
is, in some way, using laziness.
This is not to say that they are right-associative; it's just a compiler
optimization.
> It has never been the case that all operators in all programming
> languages were left associative. For addition and subtraction it matters;
> you don't want a-b+c interpreted as a-(b+c), but not for || and not for
> &&. My expectation is that these operators should be right associative.
>
I can't find any reference for logic itself and, because /\ is introduced
as associative from the start in propositional logic, it does not really
matter. However, my training as a kid in math and the exposure to how I
studied to solve (+) left to right (implicitly associating to the left)
would have led me to intuitively parse / parenthesize conjunctions with
multiple (&&) the same way unless instructed otherwise.
I think this portion of the Haskell Report is also relevant to this
intuition in the case of haskell programmers: "If no fixity declaration is
given for `op` then it defaults to highest precedence and left
associativity" (Section 4.4.2).
Cheers
Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190412/79ab65ff/attachment.html>
More information about the Haskell-Cafe
mailing list