Proposal: Add Applicative (and Monoid) instances in GHC.Generics (Libraries Digest, Vol 174, Issue 27)

Li-yao Xia lysxia at gmail.com
Thu Feb 22 21:52:41 UTC 2018


Thanks everyone. So there is agreement on having Applicative and Monoid 
instances, and on not having additional helpers for generic deriving. 
That's fine to me.

Replying to Ryan:

>> 3. Add Monoid instances
> 
> I think I support this idea. But just to be sure we're on the same
> page: can you say which instances in particular you're adding, and how
> you'd implement them?

The main Monoid instances would be:

Monoid (f p) => Monoid (M1 i c f p)  -- newtype-derived
Monoid a => Monoid (K1 i a p)  -- newtype-derived
(Monoid (f p), Monoid (g p)) => Monoid ((f :*: g) p)  -- product monoid
Monoid (U1 p)  -- unit-like

No instance for (:+:).

Now that you mention it, I'm not sure about adding Monoid instances for 
the type constructors involved in Generic1, they would be (all 
newtype-derived):

Monoid p => Monoid (Par1 p)
Monoid (f p) => Monoid (Rec1 f p)
Monoid (f (g p)) => Monoid ((f :.: g) p)

Is ther an opinion against that?

I forgot to mention Semigroup instances. They would follow the same 
structure, and for completeness we can also have

Semigroup (V1 p)

This enables a more general "generic semigroup" than via Applicative.

Li-yao


More information about the Libraries mailing list