[Haskell-cafe] Semigroup and Monoid instances for ReaderT

Olaf Klinke olf at aatal-apotheke.de
Tue Mar 21 20:09:58 UTC 2017


Maybe I missed something, but aren't the transformers a red herring here? 

instance (Semigroup a, Applicative f) => Semigroup (f a) where
  (<>) = liftA2 (<>)

My first guess was that you could add this instance to your code without the need for changes to the transformers package. The problem with the semigroup instance above is that it conflicts with semigroup instances for f. For example, there is a semigroup instance for [a] when a is not a semigroup. For the declaration above, ghci 7.8.3 gave me:

    Overlapping instance declarations:
      instance (Semigroup a, Applicative f) => Semigroup (f a)
        -- Defined at <interactive>:4:10
      instance (Semigroup a, Semigroup b) => Semigroup (a, b)
        -- Defined in ‘Data.Semigroup’

Hence I guess you must use a newtype wrapper to declare when you want the semigroup instance of a and when you want the instance of f. 

Olaf


More information about the Haskell-Cafe mailing list