โ† back to index

Free Monoids

Bartosz Milewski ยท 2015 ยท Category Theory for Programmers, Ch. 12

Prereqs: ๐Ÿž Ch3 (Categories Great and Small โ€” monoids). 7 min.

The free monoid on a set of generators is the list type. Lists are the cheapest way to make concatenation associative with an identity element (the empty list). Any function from generators into a monoid extends uniquely to a monoid homomorphism from the free monoid: that extension is fold.

Monoids: a set with a binary operation

A monoid is a set with an associative binary operation and an identity element. Strings under concatenation, numbers under addition, lists under append. The question this chapter answers: given a raw set of generators, what is the most general monoid you can build from them?

Scheme

Free construction: add structure, nothing extra

A free construction takes a plain set and builds the most general algebraic structure from it. "Most general" means: impose only the laws the structure requires (associativity, identity), and no additional equations. Starting from generators {a, b}, the free monoid contains e, a, b, aa, ab, ba, bb, aab, ...: all finite sequences, with concatenation as the operation. No element equals any other unless the monoid laws force it.

Scheme
a b e a b ab ba aab ... all finite sequences
x free(x) U(n) η q h (unique = fold)

The universal property

The free monoid on a set x is defined by a universal property. Given any function q : x -> U(n) from the generators into the underlying set of some monoid n, there exists a unique monoid homomorphism h : free(x) -> n that extends q. The forgetful functor U strips a monoid down to its underlying set. The free construction is its left adjoint: the cheapest way to promote a set to a monoid.

Scheme

Monoid homomorphisms: structure-preserving maps

A monoid homomorphism is a function between monoids that preserves the operation and the identity. If h(a * b) = h(a) * h(b) and h(e) = e', then h is a homomorphism. The universal property says the homomorphism from a free monoid is determined entirely by where the generators go. No choice remains.

Scheme

Fold: the universal homomorphism

Here is the punchline in code. fold (or reduce) is the unique homomorphism guaranteed by the universal property. You give it a function from generators to a target monoid and the target's identity. It does the only thing it can: apply the function to each generator and combine the results with the target's operation. There is no other homomorphism that agrees on the generators.

Scheme

Free means no extra equations

Any monoid with elements from a set of generators is a quotient of the free monoid: take the free monoid and identify some elements. The integers under addition are the free monoid on one generator (the natural numbers and their negatives, a.k.a. lists of +1 and -1). But the integers mod 3 are a quotient: 0 = 3 = 6 = .... The free monoid makes the fewest identifications possible. Every other monoid makes more.

Scheme

Notation reference

Blog / Haskell Scheme Meaning
[a](list 'a 'b ...)Free monoid (list of generators)
[] (mempty)'()Identity element (empty list)
++ (mappend)(append xs ys)Monoid operation (concatenation)
U : Mon -> Set(forgetful functor)Strips monoid structure, keeps the set
h : free(x) -> n(fold-right op e (map q xs))Unique homomorphism (fold)
foldMap f(fold-right op e (map f xs))Map generators then fold with target op
Neighbors

Milewski chapters

  • ๐Ÿž Chapter 3 โ€” Categories Great and Small: where monoids are introduced
  • (next) Chapter 13 โ€” Representable Functors

Foundations (Wikipedia)

Translation notes

The blog post defines free monoids via a universal construction in the category Mon of monoids, using the forgetful functor U : Mon -> Set and ranking candidates by the existence of unique homomorphisms. This page skips the categorical machinery and works directly with lists and fold. The key correspondence: Haskell's foldMap :: Monoid m => (a -> m) -> [a] -> m is the concrete implementation of the universal homomorphism. Give it a function from generators to any monoid, and it produces the unique structure-preserving map. The blog post also discusses how every monoid is a quotient of a free monoid. This page illustrates that with mod-3 arithmetic, but the full picture involves equivalence relations on the free monoid that are compatible with concatenation (i.e., congruences).

Ready for the real thing? Read the blog post. Start with the universal construction, then see how lists emerge as the answer.