# Monoid

A monoid $$M$$ is a pair $$(X, \diamond)$$ where $$X$$ is a set and $$\diamond$$ is an associative binary operator with an identity.$$\diamond$$ is often interpreted as concatenation; data structures that support concatenation and have an “empty element” (such as lists, strings, and the natural numbers under addition) are examples of monoids.

Monoids are algebraic structures. We write $$x \diamond y$$ for the application of $$\diamond$$ to $$x, y \in X$$, which must be defined.$$x \diamond y$$ is commonly abbreviated $$xy$$ when $$\diamond$$ can be inferred from context. The monoid axioms (which govern the behavior of $$\diamond$$) are as follows.

1. (Closure) For all $$x, y$$ in $$X$$, $$xy$$ is also in $$X$$.

2. (Associativity) For all $$x, y, z$$ in $$X$$, $$x(yz) = (xy)z$$.

3. (Identity) There is an $$e$$ in $$X$$ such that, for all $$x$$ in $$X$$, $$xe = ex = x.$$

The axiom of closure says that $$x \diamond y \in X$$, i.e. that combining two elements of $$X$$ using $$\diamond$$ yields another element of $$X$$. In other words, $$X$$ is closed under $$\diamond$$.

The axiom of associativity says that $$\diamond$$ is an associative operation, which justifies omitting parenthesis when describing the application of $$\diamond$$ to many elements in sequence..

The axiom of identity says that there is some element $$e$$ in $$X$$ that $$\diamond$$ treats as “empty”: If you apply $$\diamond$$ to $$e$$ and $$x$$, then $$\diamond$$ simply returns $$x$$. The identity is unique: Given two elements $$e$$ and $$z$$ that satisfy the axiom of identity, we have $$ze = e = ez = z.$$ Thus, we can speak of “the identity” $$e$$ of $$M$$. $$e$$ is often written $$1$$ or $$1_M$$.

knows-requisite(Category theory): Equivalently, a monoid is a category with exactly one object.

Monoids are semigroups equipped with an identity. Groups are monoids with inverses. For more on how monoids relate to other algebraic structures, refer to the tree of algebraic structures.

# Notation

Given a monoid $$M = (X, \diamond)$$, we say “$X$ forms a monoid under $$\diamond$$.” For example, the set of finite bitstrings forms a monoid under concatenation: The set of finite bitstrings is closed under concatenation; concatenation is an associative operation; and the empty bitstring is a finite bitstring that acts like an identity under concatenation.

$$X$$ is called the underlying set of $$M$$, and $$\diamond$$ is called the monoid operation.$$x \diamond y$$ is usually abbreviated $$xy$$. $$M$$ is generally allowed to substitute for $$X$$ when discussing the monoid. For example, we say that the elements $$x, y \in X$$ are “in $$M$$,” and sometimes write “$x, y \in M$” or talk about the “elements of $$M$$.”

# Examples

Bitstrings form a monoid under concatenation, with the empty string as the identity.

The set of finite lists of elements drawn from $$Y$$ (for any set $$Y$$) form a monoid under concatenation, with the empty list as the identity.

The natural numbers $$\mathbb N$$ form a monid under addition, with $$0$$ as the identity.

Monoids have found some use in functional programming languages such as Haskell and Scala, where they are used to generalize over data types in which values can be “combined” (by some operation $$\diamond$$) and which include an “empty” value (the identity).