# nLab free monoid

### Context

#### Algebra

higher algebra

universal algebra

# Finite lists and free monoids

## Idea

Given a set $S$, the free monoid on $S$ is the set ${S}^{*}$ of all lists (finite sequences) of elements of $S$, made into a monoid using concatenation. The free functor from Set to Mon? takes $S$ to ${S}^{*}$.

## Definitions

We will give three definitions, which can all be shown equivalent.

### As functions

An element of ${S}^{*}$ consists of a natural number $n$ (possibly $n=0$) and function from $\left[n\right]$ to $S$, where $\left[n\right]$ is the subset $\left\{i:N\phantom{\rule{thickmathspace}{0ex}}\mid \phantom{\rule{thickmathspace}{0ex}}i of $N=\left\{0,1,2,\dots \right\}$. Such an element is called a list or (to specify $n$) $n$-tuple of elements of $S$. The number $n$ is called the length of the list.

The empty list is the unique list of length $0$. It may be written $\left(\right)$, $*$, or $ϵ$, perhaps with a subscript $S$ if desired.

If $n>0$, then the list which assigns $0,\dots ,n-1$ to ${a}_{0},{a}_{1},\dots ,{a}_{n-1}$ may be written $\left({a}_{0},{a}_{1},\dots ,{a}_{n-1}\right)$. For example, if $a,b,c$ are elements of $S$, then $\left(a,b,c\right)$ is an element of ${S}^{*}$.

Given two lists $x$ and $y$, the former of length $m$ and the latter of length $n$, their concatenation $x*y$ is a list of length $m+n$, given as follows:

$i↦\left\{\begin{array}{cc}{x}_{i}& \mathrm{if}\phantom{\rule{thickmathspace}{0ex}}ii \mapsto \left\{ \array { x_i & if\; i \lt m \\ x_{i-m} & if\; i \geq m } \right.

One can now show that concatenation is associative with the empty list as identity; hence ${S}^{*}$ is a monoid.

### Recursively

The (underlying) set ${S}^{*}$ may be defined as an inductive type as follows. There are two basic constructors, one with no arguments, and one with two arguments, of which one is an element of $S$ and the other is an element of ${S}^{*}$. By the yoga of inductive types, that is a complete definition, but we spell it out in more detail while also giving terminology and notation.

So, a list is either the empty list or the cons (short for ‘constructor’ and deriving from Lisp) of an element $a$ of $S$ and a (previously constructed) list $x$. The empty list may may be written $\left(\right)$, $*$, or $\mathrm{nil}$, perhaps with a subscript $S$ if desired; the cons of $a$ and $x$ may be written $a:x$, $\left(a\right)*x$, or $\mathrm{cons}\left(a,x\right)$. We interpret the definition recursively, so we can list the elements of ${S}^{*}$ in the order in which they appear:

• $\left(\right)$,
• $a:\left(\right)$,
• $a:b:\left(\right)$,
• $a:b:c:\left(\right)$,
• etc.

Here, $a,b,c,\dots$ are elements of $S$. We may continue the ‘etc’ as far as we like, but no farther; while there are lists of arbitrarily long finite length, there are no lists of infinite length. (We would get such lists, however, if we interpreted the definition corecursively, known in computer science as a stream.) We normally abbreviate the lists above as follows:

• $\left(\right)$,
• $\left(a\right)$,
• $\left(a,b\right)$,
• $\left(a,b,c\right)$,
• etc.

We still must define the monoidal structure on ${S}^{*}$; we define the concatenation $x*y$ of $x$ and $y$ recursively in $x$. To be explicit:

• $\left(\right)*y=y$;
• $\left(a:x\right)*y=a:\left(x*y\right)$ (with parentheses for grouping, but the parentheses can be dropped now that have this definition).

One can now show that concatenation is associative with the empty list as identity; hence ${S}^{*}$ is a monoid.

### By general abstract nonsense

To prove that the category Mon? of monoids is a complete category, one normally shows that the forgetful functor $U$ (from $\mathrm{Mon}$ to the category Set of sets) preserves all limits. Then, the adjoint functor theorem defines a left adjoint to $U$ if a size condition is met; this adjoint is the functor $*$ that takes a set to its free monoid ${S}^{*}$.

To be sure, meeting the solution set condition basically requires starting the constructions in one of the other definitions above; but the proofs may all be thrown onto the adjoint functor theorem.

Another abstract approach is given in the following general theorem, which applies to more general monoids in a monoidal category:

###### Theorem

Suppose $C$ is a monoidal category with countable coproducts for which the tensor product distributes over countable coproducts (for example, a cocomplete monoidal biclosed category?). Then a left adjoint to the forgetful functor $\mathrm{Mon}\left(C\right)\to C$ exists, taking an object $c$ to

$\sum _{n\ge 0}{c}^{\otimes n},$\sum_{n \geq 0} c^{\otimes n},

which thereby becomes the free monoid on $c$.

This applies immediately to $C=\mathrm{Set}$, as this is a cocomplete cartesian closed category.

## Examples

• If $S$ is the empty set, then ${S}^{*}$ consists only of the empty list; it is the trivial monoid, one manifestation of the point.
• If $S$ is the point, then ${S}^{*}$ is $N$; the only information in a list of indistinguishable points is the length of the list. The monoid operation on $N$ is addition.
• If $S$ is $N$, then ${N}^{*}$ is still a denumerable set. But note that $N\cong {N}^{*}$ only as sets (that is, $\mid {N}^{*}\mid =\mid N\mid ={\aleph }_{0}$ as cardinal numbers); they are quite different as monoids.
• Generalising the above, $\mid {S}^{*}\mid =\mid S\mid$ is $S$ is an infinite set, or more generally $\mid {S}^{*}\mid =\mathrm{max}\left({\aleph }_{0},S\right)$ if $S$ is an inhabited set. (These theorems probably require the axiom of choice, but I haven't checked thoroughly.)

If the free monoid functor $F:\mathrm{Set}\to \mathrm{Mon}$ is followed by the forgetful functor $U:\mathrm{Mon}\to \mathrm{Set}$, then we get a monad on $\mathrm{Set}$. This monad is very important in computer science, where it is known as the list monad.

The list monad bears the same relation to multicategories as the identity monad on $\mathrm{Set}$ bears to ordinary categories. This relation should be explained at generalized multicategory.

## Foundational relevance

Every definition of free monoid makes use of some form of axiom of infinity, either $N$ directly or the ability to form general inductive types. Indeed, as $N={\mathrm{pt}}^{*}$, the axiom of infinity follows from the existence of free monoids.

## Stacks and queues

In computer science, lists often appear as stacks (not to be confused with the stacks from higher sheaf theory) and queues.

Fix a monoidal category that has coproducts with the unit object $I$. Given an object $A$, an object of stacks on $A$ is an object ${S}_{A}$ equipped with morphisms ${\mathrm{push}}_{A}:{S}_{A}\otimes A\to {S}_{A}$ and ${\mathrm{pop}}_{A}:{S}_{A}\to {S}_{A}\otimes A+I$ such that these diagrams commute:

$\begin{array}{ccccc}{S}_{A}\otimes A& & \stackrel{{\iota }_{{S}_{A}\otimes A,I}}{\to }& & {S}_{A}\otimes A+I\\ & {}_{{\mathrm{push}}_{A}}↘& & {↗}_{{\mathrm{pop}}_{A}}& & {↘}^{{\mathrm{push}}_{A}+{\mathrm{id}}_{I}}\\ & & {S}_{A}& & \underset{{\iota }_{{S}_{A}\otimes A,I}}{\to }& & {S}_{A}+I\end{array}$\array { S_A \otimes A & & \overset{\iota_{S_A \otimes A,I}}\to & & S_A \otimes A + I \\ & {}_{push_A}\searrow & & \nearrow_{pop_A} & & \searrow^{push_A + id_I} \\ & & S_A & & \underset{\iota_{S_A \otimes A,I}}\to & & S_A + I }

The idea is that ${\mathrm{push}}_{A}$ and ${\mathrm{pop}}_{A}$ are as close to inverses as reasonably possible, but ${\mathrm{pop}}_{A}$ takes us to ${S}_{A}\otimes A+I$ rather than to ${S}_{A}\otimes A$, because of the empty stack.

Queues are a little more complicated. An object of queues on $A$ is an object ${Q}_{A}$ equipped with morphisms ${\mathrm{ins}}_{A}:A\otimes {Q}_{A}\to {Q}_{A}$ (‘insert’) and ${\mathrm{rem}}_{A}:{Q}_{A}\to {Q}_{A}\otimes A+I$ (‘remove’). These operations are far from inverses; whereas popping a stack returns the last item to be pushed onto it, removing an item from a queue returns the first item to have been inserted into it.

What are the diagrams for this? I seem to recall that we need a distributive category; in particular, we need a cartesian monoidal category, so that $\otimes$ is $×$. But perhaps a 2-rig will be sufficient?

Revised on September 22, 2012 18:40:17 by Urs Schreiber (89.204.130.57)