[Haskell-cafe] Could someone teach me why we use Data.Monoid?

Eugene Kirpichov ekirpichov at gmail.com
Fri Nov 13 11:56:37 EST 2009

There is an astonishing number of things in programming that are monoids:
 - Numbers, addition, 0
 - Numbers, multiplication, 1
 - Lists, concatenation, [] (including strings)
 - Sorted lists, merge with respect to a linear order, []
 - Sets, union, {}
 - Maps, left-biased or right-biased union, {}
 - Maps K->V, union where Vs for same K get merged in some other monoid, {}
 - For any M: Subsets of M, intersection, M
 - Any lattice with an upper bound, minimum, upper bound;
symmetrically for a lower-bounded set
 - If (S, *, u)  is a monoid, then (A -> S, \f g x -> f x * g x, \x ->
u) is a monoid
 - Product (a,b) and co-product (Either) of monoids
 - Parsers, alternation, a parser that always fails
 - etc.

The benefits of calling something a monoid arise from using
general-purpose structures operating on monoids:
 - Finger trees http://apfelmus.nfshost.com/monoid-fingertree.html
 - Aforementioned maps which merge values for a key in a given monoid
 - Aforementioned monoids lifted to functions
 - Monoidal folds (Data.Foldable)
 - ...

2009/11/13 Magicloud Magiclouds <magicloud.magiclouds at gmail.com>:
> Hi,
>  I have looked the concept of monoid and something related, but
> still, I do not know why we use it?
> --
> 竹密岂妨流水过
> 山高哪阻野云飞
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

Eugene Kirpichov
Web IR developer, market.yandex.ru

More information about the Haskell-Cafe mailing list