Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 11 additions & 0 deletions message-index/messages/GHC-59692/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Duplicate Instances
summary: Multiple instances defined for the same type class and type.
severity: error
introduced: 9.6.1
---

For type class coherence, at most one instance may be defined for each type for the same type class.

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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module MultipleInstances where

newtype Sum = Sum { getSum :: Int }

instance Semigroup Sum where
(<>) x y = Sum (getSum x + getSum y)

newtype Product = Product { getProduct :: Int }

instance Semigroup Product where
(<>) x y = Product (getProduct x * getProduct y)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module MultipleInstances where

instance Semigroup Int where
(<>) = (+)

instance Semigroup Int where
(<>) = (*)
6 changes: 6 additions & 0 deletions message-index/messages/GHC-59692/multipleInstances/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Multiple Instances for Semigroup Int
---

It is not possible to give two definitions for `Semigroup Int`, one for the semigroup given by addition and one for the semigroup given by multiplication.
If we require both instances, then we should define two `newtype` wrappers for `Int`.