concrete functor and monad transformers

#86Semigroup / Monoid instances for monad transformers

I would like to contribute Semigroup / Monoid instances of the form:

instance Semigroup a => Semigroup (F a) where
    (<>) = liftA2 (<>)

instance Monoid a => Monoid (F a) where
    mempty = pure mempty

… where F is each monad transformer. Or equivalently:

deriving (Semigroup, Monoid) via (Ap F a)

I motivate why these instances are useful and why the existence of Ap alone does not suffice in this post:

https://www.haskellforall.com/2022/03/applicatives-should-usually-implement.html

An alternative variation on this proposal that I'd also be fine with would be to omit the Semigroup / Monoid instance for monad transformers that have a non-lifted Alternative instance (such as MaybeT)

  • I just want to add my support for this proposal. I find it very convenient being able to use foldMap with transformers, but I currently can't do that without defining orphan instances.

  • I'd like to propose that this issue be extended to also cover the Applicative transformers (Backwards and Lift).