Skip to content

Basic explanation of 59692 #534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions message-index/messages/GHC-59692/example1/after/Example1.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Example1 where

data T a

instance Eq a => Eq (T a) where
(==) = (==)
9 changes: 9 additions & 0 deletions message-index/messages/GHC-59692/example1/before/Example1.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Example1 where

data T a

instance Eq a => Eq (T a) where
(==) = (==)

instance Show a => Eq (T a) where
(==) = (==)
19 changes: 19 additions & 0 deletions message-index/messages/GHC-59692/example1/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Identical Instance Heads
---

Even though the instances have different constraints, they are still considered the same.

## Error message

```
messages/GHC-59692/example1/before/Example1.hs:3:10: error: [GHC-59692]
Duplicate instance declarations:
instance Eq a => Eq (T a)
-- Defined at messages/GHC-59692/example1/before/Example1.hs:3:10
instance Show a => Eq (T a)
-- Defined at messages/GHC-59692/example1/before/Example1.hs:6:10
|
3 | instance Eq a => Eq (T a) where
| ^^^^^^^^^^^^^^^^
```
12 changes: 8 additions & 4 deletions message-index/messages/GHC-59692/index.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
---
title: Duplicate Instances
summary: Multiple instances defined for the same type class and type.
severity: error
introduced: 9.6.1
severity: error
---

For type class coherence, at most one instance may be defined for each type for the same type class.
You can only define a single instance for every type, because it must be possible to automatically resolve class constraints without any ambiguity.

If the instances are identical, you should simply remove one.
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.

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.

Identical instances should simply be removed.
If multiple instances for the same type are required, circumventing this restriction is possible by introducing a `newtype` wrapper.
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.