In abstract algebra, a branch of mathematics, a monoid is an algebraic structure with a single associative binary operation and an identity element. Monoids are studied in semigroup theory as they are semigroups with identity.
Wikipedia has the above definition of a Monoid. This blog post will desconstruct that definition to simply describe what a monoid is, and why you would want to use one.
We will, however, ignore a proper defintion of ‘abstract algebra’ as that would mean writing several textbooks. Just imagine an ‘algebraic structure’ as a thing that contains functions. A ‘thing that contains functions’ is sometimes named a class in programming languages.
So, with that out of the way, this is the list of things we will cover:
You should have learnt some associative things during Mathematics lessons at primary school. Addition and Multiplication are associative. Subtraction and Division are not. Associativity means that you can reorder operations on a list of things, and always receive the same result.
Semigroup is the name for a thing that provides an associative function. For Integers,
* are associative functions. A Semigroup is one of those “algebraic structures”, and it has one function; an associative one.
It would looks like this, for adding up Integers:
It is worth noting that a Semigroup will apply to a whole type. This Semigroup will apply to all
Int, as denoted by the square brackets after the class name. The example is written in Scala, where
Int is an alias for
Also known as the zero value, or the ‘value for which nothing happens’. This will depend on what your monoid does. For instance, for an adding monoid, the identity value will be zero. Adding zero to
x gives you
x. For multiplication, the identity value will be 1. Multiplying 1 by
x gives you
x. If we had kept the identity value as zero, multiplying
x by zero would return zero, thereby being useless. Likewise, any other value would mess up our calculation.
So what is a monoid?
A monoid, has an associative function, and it has an identity function. As you may be able to tell from the wikipedia description, it really is a Semigroup, but with an identity function.
As shown above, the
+ operator on Integers is associative, and we know that the zero value for this is
Why is this useful?
Consider the following Scala example.
|+| is an alias for our previously-seen associative method. This particular associative method (taken from scalaz) merges two Maps together by summing the values if the keys are equal.
We can also take advantage of the
reduce method to do this:
If you can define your problem as a Monoid, it becomes trivial to compute, and trivial to parallelise too. Remember, associative functions can be batched and executed in any order.
Hopefully it is becoming clear why this is useful. If you want to add Integers together, most languages already include basic addition. However, Monoids can potentially be written for any type of data. As long as you define
associative, they can do anything you want!