[GHC] #10063: State a law for foldMap

GHC ghc-devs at haskell.org
Tue Feb 3 20:59:56 UTC 2015


#10063: State a law for foldMap
-------------------------------------+-------------------------------------
              Reporter:  ekmett      |             Owner:  ekmett
                  Type:  feature     |            Status:  new
  request                            |         Milestone:  7.12.1
              Priority:  low         |           Version:  7.10.1-rc1
             Component:  Core        |  Operating System:  Unknown/Multiple
  Libraries                          |   Type of failure:  Documentation
              Keywords:              |  bug
          Architecture:              |        Blocked By:
  Unknown/Multiple                   |   Related Tickets:
             Test Case:              |
              Blocking:              |
Differential Revisions:              |
-------------------------------------+-------------------------------------
 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

 {{{#!hs
 g mempty = mempty
 g (mappend a b) = mappend (g a) (g b)
 }}}

 holds.

 For any monoid homomorphism `g`:

 {{{#!hs
 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`.

 {{{#!hs
 foldMap f &&& foldMap g = foldMap (f &&& g)
 }}}

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10063>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list