diff --git a/message-index/messages/GHC-59692/index.md b/message-index/messages/GHC-59692/index.md new file mode 100644 index 00000000..7a4ceaa8 --- /dev/null +++ b/message-index/messages/GHC-59692/index.md @@ -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. diff --git a/message-index/messages/GHC-59692/multipleInstances/after/MultipleInstances.hs b/message-index/messages/GHC-59692/multipleInstances/after/MultipleInstances.hs new file mode 100644 index 00000000..a5bc7113 --- /dev/null +++ b/message-index/messages/GHC-59692/multipleInstances/after/MultipleInstances.hs @@ -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) diff --git a/message-index/messages/GHC-59692/multipleInstances/before/MultipleInstances.hs b/message-index/messages/GHC-59692/multipleInstances/before/MultipleInstances.hs new file mode 100644 index 00000000..1a6a3c68 --- /dev/null +++ b/message-index/messages/GHC-59692/multipleInstances/before/MultipleInstances.hs @@ -0,0 +1,7 @@ +module MultipleInstances where + +instance Semigroup Int where + (<>) = (+) + +instance Semigroup Int where + (<>) = (*) diff --git a/message-index/messages/GHC-59692/multipleInstances/index.md b/message-index/messages/GHC-59692/multipleInstances/index.md new file mode 100644 index 00000000..caf5a01f --- /dev/null +++ b/message-index/messages/GHC-59692/multipleInstances/index.md @@ -0,0 +1,6 @@ +--- +title: Multiple Instances for Semigroup Int +--- + +Haskell does not allow to give more than one definition of `Semigroup Int`, even though we might want to provide both an instance for the semigroup given by multiplication and the semigroup given by addition. +If we require both instances, then we should define two `newtype` wrappers for `Int`.