Semigroup and Monoid instances for ReaderT

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Semigroup and Monoid instances for ReaderT

Olaf Klinke
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
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.