Skip to content

Commit 71f4cfd

Browse files
authored
Basic explanation of 59692 (#534)
1 parent fb281e9 commit 71f4cfd

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Example1 where
2+
3+
data T a
4+
5+
instance Eq a => Eq (T a) where
6+
(==) = (==)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Example1 where
2+
3+
data T a
4+
5+
instance Eq a => Eq (T a) where
6+
(==) = (==)
7+
8+
instance Show a => Eq (T a) where
9+
(==) = (==)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Identical Instance Heads
3+
---
4+
5+
Even though the instances have different constraints, they are still considered the same.
6+
7+
## Error message
8+
9+
```
10+
messages/GHC-59692/example1/before/Example1.hs:3:10: error: [GHC-59692]
11+
Duplicate instance declarations:
12+
instance Eq a => Eq (T a)
13+
-- Defined at messages/GHC-59692/example1/before/Example1.hs:3:10
14+
instance Show a => Eq (T a)
15+
-- Defined at messages/GHC-59692/example1/before/Example1.hs:6:10
16+
|
17+
3 | instance Eq a => Eq (T a) where
18+
| ^^^^^^^^^^^^^^^^
19+
```
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
---
22
title: Duplicate Instances
33
summary: Multiple instances defined for the same type class and type.
4-
severity: error
54
introduced: 9.6.1
5+
severity: error
66
---
77

8-
For type class coherence, at most one instance may be defined for each type for the same type class.
8+
You can only define a single instance for every type, because it must be possible to automatically resolve class constraints without any ambiguity.
9+
10+
If the instances are identical, you should simply remove one.
11+
If you want multiple instances for the same type, you can circumvent this restriction by introducing a `newtype` wrapper. An example of this is shown in the solution for the "Multiple Instances for Semigroup Int" example.
12+
13+
Note that instances are considered the same even if they have different constraints. Only the arguments of the class are considered when matching instances. This issue is shown in the "Identical Instance Heads" example.
914

10-
Identical instances should simply be removed.
11-
If multiple instances for the same type are required, circumventing this restriction is possible by introducing a `newtype` wrapper.
15+
See the [GHC User Guide](https://downloads.haskell.org/~ghc/9.12.1/docs/users_guide/exts/instances.html) for more information on instance declarations and resolution.

0 commit comments

Comments
 (0)