Skip to content

instance Semigroup/Monoid (f (g a)) => Semigroup/Monoid (Compose f g a)

Motivation

I am defining a higher-kinded data structure representing configuration:

data Config f = Config
  { _port :: f Int
  , _db :: f FilePath
  }

instance (forall a . Semigroup (f a)) => Semigroup (Config f) where
  Config p1 db1 <> Config p2 db2 = Config (p1 <> p2) (db1 <> db2)

instance (forall a . Monoid (f a)) => Monoid (Config f) where
  mempty = Config mempty mempty

I want to represent partial configs using the Maybe (Last a)) Monoid. To make that fit, I have to use Data.Functor.Compose.Compose:

type FullConfig = Config Identity
type PartialConfig = Config (Compose Maybe Last)

However, Compose f g a has no Monoid instance that uses the underlying f (g a). At this point I am sad, and have to either write an orphan, or my own newtype Last a = Last (Maybe a) which is the same as Data.Maybe.Last (which was meant to be deprecated in 8.8, according to base-4.13.0.0 haddocks?).

Proposal

Add the following instances (possibly GeneralizedNewtypeDeriving will give them to us):

instance Semigroup (f (g a)) => Semigroup (Compose f g a)
instance Monoid (f (g a)) => Monoid (Compose f g a)

Related Issues

#15028 (closed) - Deprecate and remove Data.Semigroup.Option; Data.Monoid.{First,Last}
#16636 - Eq1/Show1 etc for Data.Monoid.{First,Last}

Edited by jackkelly
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information