GHC has a few language flags, such as DeriveFunctor
, DeriveDataTypeable
etc., which enable compiler generation of derived instances for type classes other than those allowed in Haskell 98. This especially makes sense for something like Functor
, where the laws of that class dictate an obvious, "natural" derived instance.
So why not for Monoid
? It seems like for any data type with a single data constructor:
data T = MkT a b c ...
one could mechanically produce a Monoid
instance (excuse the pseudocode):
instance (Monoid a, Monoid b, Monoid c, ...) => Monoid T where
mempty =
MkT mempty mempty mempty ...
mappend (MkT a1 b1 c1 ...) (MkT a2 b2 c2 ...) =
MkT (mappend a1 a2) (mappend b1 b2) (mappend c1 c2) ...
I'm aware that the derive package provides this, but my question specifically is whether there's a reason why GHC does not.
Functor
instance. The same is not true aboutMonoid
. – augustss