Skip to content

Commit 03fdd21

Browse files
Make suggested example changes
1 parent d88dedd commit 03fdd21

File tree

6 files changed

+78
-42
lines changed

6 files changed

+78
-42
lines changed

message-index/messages/GHC-25897/example1/after/Example1.hs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,7 @@ data F x where
1515
-- result type of the pattern match
1616
-- is known, from the type signature,
1717
-- to be 'Bool'.
18-
foo2 :: F x -> Bool
19-
foo2 = \case
20-
FA -> True
21-
FB -> False
22-
23-
type family G a where
24-
G A = Bool
25-
G B = Bool
26-
27-
-- Compiles successfully, because the
28-
-- result type of the pattern match
29-
-- is known, from the type signature,
30-
-- to be 'G x'
31-
foo3 :: F x -> G x
32-
foo3 = \case
18+
foo :: F x -> Bool
19+
foo = \case
3320
FA -> True
3421
FB -> False
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{-# LANGUAGE LambdaCase #-}
2+
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE GADTs #-}
4+
{-# LANGUAGE TypeFamilies #-}
5+
6+
module Example2 where
7+
8+
data X = A | B
9+
10+
data F x where
11+
FA :: F A
12+
FB :: F B
13+
14+
type family G a where
15+
G A = Bool
16+
G B = Bool
17+
18+
-- Compiles successfully, because the
19+
-- result type of the pattern match
20+
-- is known, from the type signature,
21+
-- to be 'G x'
22+
foo3 :: F x -> G x
23+
foo3 = \case
24+
FA -> True
25+
FB -> False

message-index/messages/GHC-25897/example1/before/Example1.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ data F x where
1212
FB :: F B
1313

1414
-- Results in error GHC-25897
15-
foo1 = \case
15+
foo = \case
1616
FA -> True
1717
FB -> False
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{-# LANGUAGE LambdaCase #-}
2+
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE GADTs #-}
4+
{-# LANGUAGE TypeFamilies #-}
5+
6+
module Example2 where
7+
8+
data X = A | B
9+
10+
data F x where
11+
FA :: F A
12+
FB :: F B
13+
14+
-- Results in error GHC-25897
15+
foo = \case
16+
FA -> True
17+
FB -> False
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
11
---
22
title: A pattern match, without known return type, on a GADT
33
---
4+
5+
In this example, the definition `foo` uses pattern matching on a GADT. GHC is not able to infer types for GADT pattern matches, but no type signature is provided. Following the suggestion in the error message and adding a type signature solves the problem.
6+
7+
Both starting files in this example are identical, but they are matched with different solutions. `Example2.hs` demonstrates that GHC's expected type need not be a concrete base type - even a type family satisfies this check.
8+
9+
## Message
10+
11+
The following message is provided for both lines 16 and 17, because GHC type checks each branch of a pattern match separately:
12+
13+
```
14+
Example1.hs:16:9: error:
15+
• Could not deduce (p ~ Bool)
16+
from the context: x ~ 'A
17+
bound by a pattern with constructor: FA :: F 'A,
18+
in a \case alternative
19+
at Example1.hs:16:3-4
20+
‘p’ is a rigid type variable bound by
21+
the inferred type of foo :: F x -> p
22+
at Example1.hs:(15,1)-(17,13)
23+
• In the expression: True
24+
In a \case alternative: FA -> True
25+
In the expression:
26+
\case
27+
FA -> True
28+
FB -> False
29+
• Relevant bindings include
30+
foo :: F x -> p (bound at Example1.hs:15:1)
31+
Suggested fix: Consider giving ‘foo’ a type signature
32+
|
33+
16 | FA -> True
34+
|
35+
```

message-index/messages/GHC-25897/index.md

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,4 @@ type of the pattern match. This information might, for example, be derived from
1010
signature, or by type inference due to the context in which the pattern match occurs.
1111

1212
To solve the problem you must somehow tell GHC the type of the pattern
13-
match. For example, in the below error message GHC suggests giving
14-
the pattern match a type signature.
15-
16-
```
17-
example1.hs:14:9: error: [GHC-25897]
18-
• Could not deduce ‘p ~ Bool’
19-
from the context: x ~ A
20-
bound by a pattern with constructor: FA :: F A,
21-
in a \case alternative
22-
at test17.hs:14:3-4
23-
‘p’ is a rigid type variable bound by
24-
the inferred type of foo1 :: F x -> p
25-
at test17.hs:(13,1)-(15,13)
26-
• In the expression: True
27-
In a \case alternative: FA -> True
28-
In the expression:
29-
\case
30-
FA -> True
31-
FB -> False
32-
• Relevant bindings include
33-
foo1 :: F x -> p (bound at test17.hs:13:1)
34-
Suggested fix: Consider giving ‘foo1’ a type signature
35-
|
36-
14 | FA -> True
37-
| ^^^^
38-
```
13+
match.

0 commit comments

Comments
 (0)