Precedence parsing error [GHC-88747]
Haskell widely uses operators such as ($)
, (.)
and (-)
and allows users to define their own. If multiple operators are used in the same expression without brackets, it might be unclear to GHC in which order to apply them, even in the presence of fixity declarations such as infix
/ infixl
/ infixr
.
The most robust way to disambiguate parsing is to add brackets.
Examples
Unary minus mixed with multiplication
Main.hs:1:8: error: [GHC-88747]
Precedence parsing error
cannot mix โ*โ [infixl 7] and prefix `-' [infixl 6] in the same infix expression
|
1 | main = print $ 5 * -3
| ^^^^^^^^^^^^^^
The most general solution is to add brackets: 5 * (-3)
.
If one of the clashing operators is unary minus, consider replacing it with negate
: 5 * negate 3
. Another possibility is to enable {-# LANGUAGE LexicalNegation #-}
or {-# LANGUAGE NegativeLiterals #-}
, which makes -
bind to 3
as early as possible, eliminating the ambiguity.
Main.hs
Before
main = print $ 5 * -3
After
main = print $ 5 * (-3)