In category theory, a Monoid is a triple (, , ) in a monoidal category (, , ) together with two morphisms:
- is a natural transformation (the unit)
- is another natural transformation (the multiplication)
These must satisfy the following coherence conditions:
- (associativity)
- (left and right identiy)
We can rephrase these conditions using the subsequent commutative diagrams:
Monoids are a powerful abstraction that can be used to solve a wide variety of problems. Monoids are becoming increasingly important in computer science because they provide a versatile framework for combining elements, especially in the context of parallel and distributed computing. For example, if you need to combine values in a way that’s associative and has an identity, you can model your problem as a monoid.
Consider using monoids for aggregations on large data sets. Because of the associative property, the operation can occur in any order and still yield the same result, enabling parallel processing. The identity element provides a starting value for this computation. The classic examples of this are sum and multiplication on numbers, but also concatenation on strings or lists and more. Monoids are also used in the design of compilers and interpreters. For example, the abstract syntax tree of a program can be represented as a monoid.
Example
The Monoid, by definition, requires us to implement two functions: the unit, which is called mempty in Haskell, where we have to provide a neutral element, and the multiplication
<>
(mappend).
Haskell Definition of Monoid (Interface)
These have to obey the Monoid laws (<> infix notation for mappend) in pseudo notation:
An Instance of Monoid, the List Monoid
Another Instance, the Maybe Monoid
All of the above is already implemented in the standard Haskell library, so you can also simply open an interactive Haskell interpreter (ghci) and test the following examples.
Some more examples are:
-
The naturals numbers under addition. This forms a monoid because addition is associative, and 0 serves as the identity, as any number added by 0 remains the same.
-
The natural numbers under multiplication. This forms a monoid because multiplication is associative, and 1 serves as the identity, as any number multiplied by 1 remains the same.
-
Strings under concatenation. This forms a monoid because string concatenation is associative, and the empty string serves as the identity, as any string concatenated with remains the same.
References
- 0.The diagram displayed at the top of this post is a modified version of Brent Yorgey's Typeclassopedia diagram ↩
- 1.Monoids in ncatlab ↩
- 2.Monoids in Haskell ↩