# Monoid

In abstract algebra, a branch of mathematics, a monoid is a set equipped with an associative binary operation and an identity element. For example, the nonnegative integers with addition form a monoid, the identity element being 0.
Monoids are semigroups with identity. Such algebraic structures occur in several branches of mathematics.
The functions from a set into itself form a monoid with respect to function composition. More generally, in category theory, the morphisms of an object to itself form a monoid, and, conversely, a monoid may be viewed as a category with a single object.
In computer science and computer programming, the set of strings built from a given set of characters is a free monoid. Transition monoids and syntactic monoids are used in describing finite-state machines. Trace monoids and history monoids provide a foundation for process calculi and concurrent computing.
In theoretical computer science, the study of monoids is fundamental for automata theory (Krohn–Rhodes theory), and formal language theory (star height problem).
See semigroup for the history of the subject, and some other general properties of monoids.

## Definition

A set S equipped with a binary operation S × S → S, which we will denote •, is a monoid if it satisfies the following two axioms:

### Associativity

    For all a, b and c in S, the equation (a • b) • c = a • (b • c) holds.

### Identity element

    There exists an element e in S such that for every element a in S, the equalities e • a = a and a • e = a hold.
In other words, a monoid is a semigroup with an identity element. It can also be thought of as a magma with associativity and identity. The identity element of a monoid is unique. For this reason the identity is regarded as a constant, i. e. 0-ary (or nullary) operation. The monoid therefore is characterized by specification of the triple (S, • , e).
Depending on the context, the symbol for the binary operation may be omitted, so that the operation is denoted by juxtaposition; for example, the monoid axioms may be written (ab)c = a(bc) and ea = ae = a. This notation does not imply that it is numbers being multiplied.
A monoid in which each element has an inverse is a group.

## Monoid structures
### Submonoids

A submonoid of a monoid (M, •) is a subset N of M that is closed under the monoid operation and contains the identity element e of M. Symbolically, N is a submonoid of M if e ∈ N ⊆ M, and x • y ∈ N whenever x, y ∈ N. In this case, N is a monoid under the binary operation inherited from M.
On the other hand, if N is a subset of a monoid that is closed under the monoid operation, and is a monoid for this inherited operation, then N is not always a submonoid, since the identity elements may differ. For example, the singleton set {0} is closed under multiplication, and is not a submonoid of the (multiplicative) monoid of the nonnegative integers.

### Generators

A subset S of M is said to generate M if the smallest submonoid of M containing S is M. If there is a finite set that generates M, then M is said to be a finitely generated monoid.

### Commutative monoid

A monoid whose operation is commutative is called a commutative monoid (or, less commonly, an abelian monoid). Commutative monoids are often written additively. Any commutative monoid is endowed with its algebraic preordering ≤, defined by x ≤ y if there exists z such that x + z = y. An order-unit of a commutative monoid M is an element u of M such that for any element x of M, there exists v in the set generated by u such that x ≤ v. This is often used in case M is the positive cone of a partially ordered abelian group G, in which case we say that u is an order-unit of G.

### Partially commutative monoid

A monoid for which the operation is commutative for some, but not all elements is a trace monoid; trace monoids commonly occur in the theory of concurrent computation.

## Examples

* Out of the 16 possible binary Boolean operators, four have a two-sided identity that is also commutative and associative. These four each make the set {False, True} a commutative monoid. Under the standard definitions, AND and XNOR have the identity True while XOR and OR have the identity False. The monoids from AND and OR are also idempotent while those from XOR and XNOR are not.
* The set of natural numbers N = {0, 1, 2, ...} is a commutative monoid under addition (identity element 0) or multiplication (identity element 1). A submonoid of N under addition is called a numerical monoid.
* The set of positive integers N ∖ {0} is a commutative monoid under multiplication (identity element 1).
* Given a set A, the set of subsets of A is a commutative monoid under intersection (identity element is A itself).
* Given a set A, the set of subsets of A is a commutative monoid under union (identity element is the empty set).
* Generalizing the previous example, every bounded semilattice is an idempotent commutative monoid.
** In particular, any bounded lattice can be endowed with both a meet- and a join- monoid structure. The identity elements are the lattice's top and its bottom, respectively. Being lattices, Heyting algebras and Boolean algebras are endowed with these monoid structures.
* Every singleton set {x} closed under a binary operation • forms the trivial (one-element) monoid, which is also the trivial group.
* Every group is a monoid and every abelian group a commutative monoid.
* Any semigroup S may be turned into a monoid simply by adjoining an element e not in S and defining e • s = s = s • e for all s ∈ S. This conversion of any semigroup to the monoid is done by the free functor between the category of semigroups and the category of monoids.
** Thus, an idempotent monoid (sometimes known as find-first) may be formed by adjoining an identity element e to the left zero semigroup over a set S. The opposite monoid (sometimes called find-last) is formed from the right zero semigroup over S.
*** Adjoin an identity e to the left-zero semigroup with two elements {lt, gt}. Then the resulting idempotent monoid {lt, e, gt} models the lexicographical order of a sequence given the orders of its elements, with e representing equality.
* The underlying set of any ring, with addition or multiplication as the operation. (By definition, a ring has a multiplicative identity 1.)
** The integers, rational numbers, real numbers or complex numbers, with addition or multiplication as operation.
** The set of all n by n matrices over a given ring, with matrix addition or matrix multiplication as the operation.
* The set of all finite strings over some fixed alphabet Σ forms a monoid with string concatenation as the operation. The empty string serves as the identity element. This monoid is denoted Σ$^∗$ and is called the free monoid over Σ. It is not commutative if Σ has at least two elements.
* Given any monoid M, the opposite monoid M$^{op}$ has the same carrier set and identity element as M, and its operation is defined by x •$^{op}$ y = y • x. Any commutative monoid is the opposite monoid of itself.
* Given two sets M and N endowed with monoid structure (or, in general, any finite number of monoids, M$_1$, ..., M$_k$), their Cartesian product M × N, with the binary operation and identity element defined on corresponding coordinates, called the direct product, is also a monoid (respectively, M$_1$ × ⋅⋅⋅ × M$_k$).
* Fix a monoid M. The set of all functions from a given set to M is also a monoid. The identity element is a constant function mapping any value to the identity of M; the associative operation is defined pointwise.
* Fix a monoid M with the operation • and identity element e, and consider its power set P(M) consisting of all subsets of M. A binary operation for such subsets can be defined by S • T = { s • t : s ∈ S, t ∈ T }. This turns P(M) into a monoid with identity element {e}. In the same way the power set of a group G is a monoid under the product of group subsets.
* Let S be a set. The set of all functions S → S forms a monoid under function composition. The identity is just the identity function. It is also called the full transformation monoid of S. If S is finite with n elements, the monoid of functions on S is finite with n$^n$ elements.
* Generalizing the previous example, let C be a category and X an object of C. The set of all endomorphisms of X, denoted End$_C$(X), forms a monoid under composition of morphisms. For more on the relationship between category theory and monoids see below.
* The set of homeomorphism classes of compact surfaces with the connected sum. Its unit element is the class of the ordinary 2-sphere. Furthermore, if a denotes the class of the torus, and b denotes the class of the projective plane, then every element c of the monoid has a unique expression in the form c = na + mb where n is a positive integer and m = 0, 1, or 2. We have 3b = a + b.
* Let ⟨f⟩ be a cyclic monoid of order n, that is, ⟨f⟩ = {f$^0$, f$^1$, ..., f$^{n−1}$}. Then f$^n$ = f$^k$ for some 0 ≤ k < n. Each such k gives a distinct monoid of order n, and every cyclic monoid is isomorphic to one of these. Moreover, f can be considered as a function on the points {0, 1, 2, ..., n−1} given by
${\begin{bmatrix}0&1&2&\cdots &n-2&n-1\\1&2&3&\cdots &n-1&k\end{bmatrix}}$ or, equivalently $f(i):={\begin{cases}i+1,&{\text{if }}0\leq i<n-1\\k,&{\text{if }}i=n-1.\end{cases}}$
Multiplication of elements in ⟨f⟩ is then given by function composition.
When k = 0 then the function f is a permutation of {0, 1, 2, ..., n−1}, and gives the unique cyclic group of order n.

## Properties

The monoid axioms imply that the identity element e is unique: If e and f are identity elements of a monoid, then e = ef = f.

### Products and powers

For each nonnegative integer n, one can define the product $p_{n}=\textstyle \prod _{i=1}^{n}a_{i}$ of any sequence (a$_1$, ..., a$_n$) of n elements of a monoid recursively: let p$_0$ = e and let p$_m$ = p$_{m−1}$ • a$_m$ for 1 ≤ m ≤ n.
As a special case, one can define nonnegative integer powers of an element x of a monoid: x$^0$ = 1 and x$^n$ = x$^{n−1}$ • x for n ≥ 1. Then x$^{m+n}$ = x$^m$ • x$^n$ for all m, n ≥ 0.

### Invertible elements

An element x is called invertible if there exists an element y such that x • y = e and y • x = e. The element y is called the inverse of x. Inverses, if they exist, are unique: if y and z are inverses of x, then by associativity y = ey = (zx)y = z(xy) = ze = z.
If x is invertible, say with inverse y, then one can define negative powers of x by setting x$^{−n}$ = y$^n$ for each n ≥ 1; this makes the equation x$^{m+n}$ = x$^m$ • x$^n$ hold for all m, n ∈ Z.
The set of all invertible elements in a monoid, together with the operation •, forms a group.

### Grothendieck group

Not every monoid sits inside a group. For instance, it is perfectly possible to have a monoid in which two elements a and b exist such that a • b = a holds even though b is not the identity element. Such a monoid cannot be embedded in a group, because in the group multiplying both sides with the inverse of a would get that b = e, which is not true.
A monoid (M, •) has the cancellation property (or is cancellative) if for all a, b and c in M, the equality a • b = a • c implies b = c, and the equality b • a = c • a implies b = c.
A commutative monoid with the cancellation property can always be embedded in a group via the Grothendieck group construction. That is how the additive group of the integers (a group with operation +) is constructed from the additive monoid of natural numbers (a commutative monoid with operation + and cancellation property). However, a non-commutative cancellative monoid need not be embeddable in a group.
If a monoid has the cancellation property and is finite, then it is in fact a group.
The right- and left-cancellative elements of a monoid each in turn form a submonoid (i.e. are closed under the operation and obviously include the identity). This means that the cancellative elements of any commutative monoid can be extended to a group.
The cancellative property in a monoid is not necessary to perform the Grothendieck construction – commutativity is sufficient. However, if a commutative monoid does not have the cancellation property, the homomorphism of the monoid into its Grothendieck group is not injective. More precisely, if a • b = a • c, then b and c have the same image in the Grothendieck group, even if b ≠ c. In particular, if the monoid has an absorbing element, then its Grothendieck group is the trivial group.

### Types of monoids

An inverse monoid is a monoid where for every a in M, there exists a unique a$^{−1}$ in M such that a = a • a$^{−1}$ • a and a$^{−1}$ = a$^{−1}$ • a • a$^{−1}$. If an inverse monoid is cancellative, then it is a group.
In the opposite direction, a zerosumfree monoid is an additively written monoid in which a + b = 0 implies that a = 0 and b = 0: equivalently, that no element other than zero has an additive inverse.

## Acts and operator monoids

Let M be a monoid, with the binary operation denoted by • and the identity element denoted by e. Then a (left) M-act (or left act over M) is a set X together with an operation ⋅ : M × X → X which is compatible with the monoid structure as follows:
* for all x in X: e ⋅ x = x;
* for all a, b in M and x in X: a ⋅ (b ⋅ x) = (a • b) ⋅ x.
This is the analogue in monoid theory of a (left) group action. Right M-acts are defined in a similar way. A monoid with an act is also known as an operator monoid. Important examples include transition systems of semiautomata. A transformation semigroup can be made into an operator monoid by adjoining the identity transformation.

## Monoid homomorphisms

A homomorphism between two monoids (M, ∗) and (N, •) is a function f : M → N such that
* f(x ∗ y) = f(x) • f(y) for all x, y in M
* f(e$_M$) = e$_N$,
where e$_M$ and e$_N$ are the identities on M and N respectively. Monoid homomorphisms are sometimes simply called monoid morphisms.
Not every semigroup homomorphism between monoids is a monoid homomorphism, since it may not map the identity to the identity of the target monoid, even though the identity is the identity of the image of the homomorphism. For example, consider [Z]$_n$, the set of residue classes modulo n equipped with multiplication. In particular, [1]$_n$ is the identity element. Function f : [Z]$_3$ → [Z]$_6$ given by [k]$_3$ ↦ [3k]$_6$ is a semigroup homomorphism, since [3k ⋅ 3l]$_6$ = [9kl]$_6$ = [3kl]$_6$. However, f([1]$_3$) = [3]$_6$ ≠ [1]$_6$, so a monoid homomorphism is a semigroup homomorphism between monoids that maps the identity of the first monoid to the identity of the second monoid and the latter condition cannot be omitted.
In contrast, a semigroup homomorphism between groups is always a group homomorphism, as it necessarily preserves the identity (because, in the target group of the homomorphism, the identity element is the only element x such that x ⋅ x = x).
A bijective monoid homomorphism is called a monoid isomorphism. Two monoids are said to be isomorphic if there is a monoid isomorphism between them.

## Equational presentation

Monoids may be given a presentation, much in the same way that groups can be specified by means of a group presentation. One does this by specifying a set of generators Σ, and a set of relations on the free monoid Σ$^∗$. One does this by extending (finite) binary relations on Σ$^∗$ to monoid congruences, and then constructing the quotient monoid, as above.
Given a binary relation R ⊂ Σ$^∗$ × Σ$^∗$, one defines its symmetric closure as R ∪ R$^{−1}$. This can be extended to a symmetric relation E ⊂ Σ$^∗$ × Σ$^∗$ by defining x ~$_E$ y if and only if x = sut and y = svt for some strings u, v, s, t ∈ Σ$^∗$ with (u,v) ∈ R ∪ R$^{−1}$. Finally, one takes the reflexive and transitive closure of E, which is then a monoid congruence.
In the typical situation, the relation R is simply given as a set of equations, so that R = {u$_1$ = v$_1$, ..., u$_n$ = v$_n$}. Thus, for example,
    $\langle p,q\,\vert \;pq=1\rangle$
is the equational presentation for the bicyclic monoid, and
    $\langle a,b\,\vert \;aba=baa,bba=bab\rangle$
is the plactic monoid of degree 2 (it has infinite order). Elements of this plactic monoid may be written as $a^{i}b^{j}(ba)^{k}$ for integers i, j, k, as the relations show that ba commutes with both a and b.

## Relation to category theory

Monoids can be viewed as a special class of categories. Indeed, the axioms required of a monoid operation are exactly those required of morphism composition when restricted to the set of all morphisms whose source and target is a given object. That is,
    A monoid is, essentially, the same thing as a category with a single object.
More precisely, given a monoid (M, •), one can construct a small category with only one object and whose morphisms are the elements of M. The composition of morphisms is given by the monoid operation •.
Likewise, monoid homomorphisms are just functors between single object categories. So this construction gives an equivalence between the category of (small) monoids Mon and a full subcategory of the category of (small) categories Cat. Similarly, the category of groups is equivalent to another full subcategory of Cat.
In this sense, category theory can be thought of as an extension of the concept of a monoid. Many definitions and theorems about monoids can be generalised to small categories with more than one object. For example, a quotient of a category with one object is just a quotient monoid.
Monoids, just like other algebraic structures, also form their own category, Mon, whose objects are monoids and whose morphisms are monoid homomorphisms.
There is also a notion of monoid object which is an abstract definition of what is a monoid in a category. A monoid object in Set is just a monoid.

## Monoids in computer science

In computer science, many abstract data types can be endowed with a monoid structure. In a common pattern, a sequence of elements of a monoid is "folded" or "accumulated" to produce a final value. For instance, many iterative algorithms need to update some kind of "running total" at each iteration; this pattern may be elegantly expressed by a monoid operation. Alternatively, the associativity of monoid operations ensures that the operation can be parallelized by employing a prefix sum or similar algorithm, in order to utilize multiple cores or processors efficiently.
Given a sequence of values of type M with identity element ε and associative operation •, the fold operation is defined as follows:
    $\mathrm {fold} :M^{*}\rightarrow M=\ell \mapsto {\begin{cases}\varepsilon &{\mbox{if }}\ell =\mathrm {nil} \\m\bullet \mathrm {fold} \,\ell '&{\mbox{if }}\ell =\mathrm {cons} \,m\,\ell '\end{cases}}$
In addition, any data structure can be 'folded' in a similar way, given a serialization of its elements. For instance, the result of "folding" a binary tree might differ depending on pre-order vs. post-order tree traversal.

## MapReduce

An application of monoids in computer science is the so-called MapReduce programming model (see Encoding Map-Reduce As A Monoid With Left Folding). MapReduce, in computing, consists of two or three operations. Given a dataset, "Map" consists of mapping arbitrary data to elements of a specific monoid. "Reduce" consists of folding those elements, so that in the end we produce just one element.
For example, if we have a multiset, in a program it is represented as a map from elements to their numbers. Elements are called keys in this case. The number of distinct keys may be too big, and in this case, the multiset is being sharded. To finalize reduction properly, the "Shuffling" stage regroups the data among the nodes. If we do not need this step, the whole Map/Reduce consists of mapping and reducing; both operations are parallelizable, the former due to its element-wise nature, the latter due to associativity of the monoid.

## Complete monoids

A complete monoid is a commutative monoid equipped with an infinitary sum operation $\Sigma _{I}$ for any index set I such that
    $\sum _{i\in \emptyset }{m_{i}}=0;\quad \sum _{i\in \{j\}}{m_{i}}=m_{j};\quad \sum _{i\in \{j,k\}}{m_{i}}=m_{j}+m_{k}\quad {\text{ for }}j\neq k$
and
    $\sum _{j\in J}{\sum _{i\in I_{j}}{m_{i}}}=\sum _{i\in I}m_{i}\quad {\text{ if }}\bigcup _{j\in J}I_{j}=I{\text{ and }}I_{j}\cap I_{j'}=\emptyset \quad {\text{ for }}j\neq j'$.
An ordered commutative monoid is a commutative monoid M together with a partial ordering ≤ such that a ≥ 0 for every a ∈ M, and a ≤ b implies a + c ≤ b + c for all a, b, c ∈ M.
A continuous monoid is an ordered commutative monoid (M, ≤) in which every directed subset has a least upper bound, and these least upper bounds are compatible with the monoid operation:
    $a+\sup S=\sup(a+S)$
for every a ∈ M and directed subset S of M.
If (M, ≤) is a continuous monoid, then for any index set I and collection of elements (a$_i$)$_{i∈I}$, one can define
    $\sum _{I}a_{i}=\sup _{{\text{finite }}E\subset I}\;\sum _{E}a_{i},$
and M together with this infinitary sum operation is a complete monoid.