Skip to content

Commit 62fb35e

Browse files
BodigrimBinderDavid
authored andcommitted
Document GHC-88747 "Precedence parsing error"
1 parent 17fdeaa commit 62fb35e

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Precedence parsing error
3+
summary: Cannot determine the order of operators in an expression
4+
severity: error
5+
introduced: 9.6.1
6+
---
7+
8+
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`.
9+
10+
The most robust way to disambiguate parsing is to add brackets.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main = print $ 5 * (-3)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main = print $ 5 * -3
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Unary minus mixed with multiplication
3+
---
4+
5+
```
6+
Main.hs:1:8: error: [GHC-88747]
7+
Precedence parsing error
8+
cannot mix ‘*’ [infixl 7] and prefix `-' [infixl 6] in the same infix expression
9+
|
10+
1 | main = print $ 5 * -3
11+
| ^^^^^^^^^^^^^^
12+
```
13+
14+
The most general solution is to add brackets: `5 * (-3)`.
15+
16+
If one of the clashing operators is unary minus, consider replacing it with `negate`: `5 * negate 3`. Another possibility is to enable [`{-# LANGUAGE LexicalNegation #-}`](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/lexical_negation.html) or [`{-# LANGUAGE NegativeLiterals #-}`](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/negative_literals.html), which makes `-` bind to `3` as early as possible, eliminating the ambiguity.

0 commit comments

Comments
 (0)