Skip to content

State a law for foldMap

After being prodded by /u/random_crank on reddit, I realized there is a law we can state for foldMap:

A monoid homomorphism g is a function such that

g mempty = mempty
g (mappend a b) = mappend (g a) (g b)

holds.

For any monoid homomorphism g:

foldMap (g . f) = g . foldMap f

This isn't strictly a free theorem as it relies on the Monoid laws, but it is true and has the same general flavor as the equally complicated free theorem for foldr.

This law is a bit technical, but is a nice tool for equational reasoning, and gives rise to a much nicer foldMap-based version of the "banana split" theorem for foldr.

foldMap f &&& foldMap g = foldMap (f &&& g)
Trac metadata
Trac field Value
Version 7.10.1-rc1
Type FeatureRequest
TypeOfFailure OtherFailure
Priority low
Resolution Unresolved
Component Core Libraries
Test case
Differential revisions
BlockedBy
Related
Blocking
CC core-libraries-committee@haskell.org
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information