Some applications require mempty
. For example, what should be the result of foldMap
ping over an empty list? There is no value of a
that you could pass to the displayed function to get m
. Thus, there must be a default. It is nice if it is an identity element of an associative operation - for example, it allows you to arbitrarily reorder / break the fold without changing the result. When folding over a tree, in fact a lot of mempty
can be displayed in the middle, but you always know that they will eventually be “squeezed out”, so you will not depend on the exact tree layout for reproducible results.
However, you are right about your concern: Semigroup
would be quite common, and it would probably be better if this class was used where mempty
not needed (since there are actually some pretty different types of Semigroup
, but not Monoid
) However, the early design of the standard library did not seem to consider this important enough to guarantee an additional class, so a lot of code began to rely on Monoid
without the need for a monoid.
This means the same problem as with Monad
: a lot of code really needed only Applicative
or even just Functor
, but for historical reasons Monad
still lingered before AMP. Ultimately, the problem is that the Haskell class hierarchies cannot really be refined after the fact, only extended down and not up.
source share