Skip to content

Commit fff62b8

Browse files
Merge pull request #429 from TauOmicronMu/78892
Add docs for GHC-78892
2 parents c2679ab + c125654 commit fff62b8

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Example where
2+
3+
-- Is the second argument the square of the first?
4+
sq :: Int -> Int
5+
sq x = x^2
6+
7+
f :: Int -> Int -> Bool
8+
f n m
9+
| m == sq n = True
10+
| otherwise = False
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Example where
2+
3+
-- Is the second argument the square of the first?
4+
f :: Int -> Int -> Bool
5+
f n (let sq = x^2 in sq n) = True
6+
f n _ = False
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: Using let ... in ... syntax in pattern.
3+
---
4+
5+
When pattern matching, let expressions are not allowed as patterns to be matched against. The logic encapsulated here should either be lifted to outside of the function, or moved inside of the function itself.
6+
7+
# Error Message
8+
9+
```
10+
Example.hs:5:6: error: [GHC-78892]
11+
(let ... in ...)-syntax in pattern
12+
|
13+
5 | f n (let exp = x^2 in exp n) = True
14+
| ^^^^^^^^^^^^^^^^^^^^^^
15+
```
16+
17+
#  Intermediate fix step
18+
In the below example, it is worth noting that you may first have considered refactoring to something along the lines of
19+
```
20+
sq :: Int -> Int
21+
sq x = x^2
22+
23+
f :: Int -> Int -> Bool
24+
f n (sq n) = True
25+
f n _ = False
26+
27+
```
28+
29+
The above code would throw a parse error in the pattern [GHC-07626](https://errors.haskell.org/messages/GHC-07626/).
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: let-syntax in pattern
3+
summary: An attempt has been made to use a let expression whilst pattern matching.
4+
severity: error
5+
introduced: 9.6.1
6+
---
7+
8+
Pattern matching allows constructors and literals to be used to take apart values, exposing their contents. Even though patterns resemble expressions, they are not evaluated. The `let ... in ...` syntax is used to introduce declarations that are scoped to an expression, which doesn't make sense as a pattern without evaluation. See [GHC-04584](https://errors.haskell.org/messages/GHC-78892/) for more information on why expressions aren't permitted in pattern matching.
9+
10+
## Example Text
11+
```
12+
(let ... in ...)-syntax in pattern
13+
```

0 commit comments

Comments
 (0)