[Haskell-cafe] Precedence of "if then else"

Sean Leather leather at cs.uu.nl
Thu Nov 25 19:02:29 CET 2010

This may be a silly, but I occasionally run into a situation where I'm
wondering what the effective precedence of "if then else" is. I was finally
motivated to do a few experiments, and it seems like the precedence level is
reset (below 0) after "if", "then", and "else".

λ> if not $ False then "here" else "x"
λ> if False then (++ "y") $ "x" else "here"
λ> if True then "here" else (++ "y") $ "x"

I suppose I knew this was the case with "if", but I had not given it much
thought for "then" or "else". Of course, it makes sense. It's the same for
"case" and many other syntactic constructs.

I was wondering where this was defined in the Language Report. I had looked
for prose, but found nothing. Now, I look at it again (with more motivation)
and see it (clearly) in the following excerpt of the grammar (Ch. 3

exp → infixexp :: [context =>] type     (expression type signature)
    | infixexp

infixexp → lexp qop infixexp     (infix operator application)
         | - infixexp     (prefix negation)
         | lexp

lexp → \ apat1 … apatn -> exp     (lambda abstraction, n ≥ 1)
     | let decls in exp     (let expression)
     | if exp [;] then exp [;] else exp     (conditional)
     | case exp of { alts }     (case expression)

Plainly and simply, this says that all infix expressions (infixexp) are
expressions (exp), and these can be found in "if then else" and "case" among
others. So, while I was looking for something regarding precedence, it is
actually defined straightforwardly in the grammar. In case others happen to
search for the same thing I did, maybe this will provide them with an answer
of sorts.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20101125/9109e78d/attachment.html

More information about the Haskell-Cafe mailing list