โ† back to index

Lawvere Theories

Bartosz Milewski ยท 2017 ยท Category Theory for Programmers, Ch. 24

Prereqs: ๐Ÿž Ch17 (Monads), ๐Ÿž Ch16 (Adjunctions). 12 min.

A wpLawvere theory is a category whose objects are natural numbers (powers of a generic object) and whose morphisms are n-ary operations. A model is a product-preserving functor L → Set. Every algebraic structure (monoids, groups, rings) is a model of some Lawvere theory. Every finitary monad arises from a Lawvere theory, and every Lawvere theory induces a monad. Lawvere theories compose better than monads: they have coproducts and tensor products where monads have no generic composition recipe.

0 1 2 3 η μ projections (from Fᵒᵖ) ternary ops unit generic pair triple morphisms n → 1 are n-ary operations

From universal algebra to categories

wpUniversal algebra describes algebraic structures by their operations and arities. A monoid has a binary operation (arity 2) and a unit (arity 0). A group adds a unary inverse (arity 1). Lawvere's insight: encode these arities categorically. Objects are natural numbers 0, 1, 2, 3, ... representing powers of a generic object. Morphisms n → 1 are n-ary operations. The whole theory lives in a single category.

Scheme

Models — interpreting the theory in Set

A model of a Lawvere theory L is a product-preserving functor M : L → Set. It maps the generic object 1 to some set a, and each n-ary operation n → 1 to a function an → a. Product-preserving means M(n) = an. Different models of the monoid theory give different monoids: integers under addition, strings under concatenation, booleans under AND.

Scheme

Building LMon — the Lawvere theory for monoids

The hom-set LMon(n, 1) contains all n-ary operations you can build from the monoid axioms. For a free monoid on n generators, these are all distinct products of the generators. LMon(2, 1) includes: the unit (ignore both inputs), the two projections (return one input), the product (combine both), and all longer expressions like aba, aab, bba, ... In a free monoid, each of these is a distinct morphism.

Scheme

Natural transformations between models

A morphism between models is a natural transformation: a function between their carrier sets that commutes with every operation. For monoids, that is exactly a monoid homomorphism. The naturality square says: applying the operation then the function equals applying the function to each input then the operation.

Scheme

From Lawvere theories to monads

Every Lawvere theory L induces a monad via the free/forgetful adjunction on its models. The forgetful functor U sends each model to its carrier set. The free functor F builds the free model. The monad T = U∘F is given by the coend formula: T(a) = ∫n an × L(n, 1). For LMon, this gives the list monad: T(a) = List(a). Each element of T(a) is a word over the alphabet a, which is exactly a list.

Scheme

Side effects as Lawvere theories: exceptions

Adding a single nullary operation (0 → 1) to Fop creates the theory of exceptions. The coend collapses to T(a) = a0 + a1 = 1 + a. In Haskell: Maybe a. A nullary operation means "produce a value from nothing" which is exactly throwing an exception (returning a default failure value). More nullary operations give more exception types.

Scheme

Lawvere theories vs monads — when they diverge

Every Lawvere theory gives a finitary monad. Every finitary monad comes from a Lawvere theory. But not every monad is finitary. The continuation monad (rra) is the classic counterexample: it cannot be expressed as a Lawvere theory because it is not determined by its action on finite sets. The advantage of Lawvere theories: they compose via coproducts (combine operations from two theories) and tensor products (interleave operations). Monads have no generic composition recipe. This composability is what makes Lawvere theories a natural fit for jktyped wiring diagrams, where operations from different theories must plug together along typed interfaces.

Scheme

Notation reference

Blog / Haskell Scheme Meaning
Fopn → mOpposite of the skeleton of FinSet
IL : Fop → L(make-theory name ops)Embedding of Fop into the Lawvere theory
L(n, 1)(enumerate-ops k)n-ary operations (hom-set)
M : L → Set(make-model carrier mu eta)Model (product-preserving functor)
Nat(M, N)(homomorphism? h ...)Natural transformation = homomorphism
TL a = ∫n an × L(n,1)(T-return x) / (T-bind xs f)Monad from Lawvere theory (coend)
Cont r a = (a → r) → r(cont-return x) / (cont-bind m f)Continuation monad (not finitary)
Neighbors

Milewski chapters

Paper pages that use these concepts

Foundations (Wikipedia)

Translation notes

The blog post builds Lawvere theories in five steps: FinSet → its skeleton F → Fop → the Lawvere theory L → models Mod(L, Set). This page collapses the first three steps and focuses on what the resulting category looks like (objects = arities, morphisms = operations). The coend formula TL(a) = ∫n an × L(n, 1) is presented through the list monad example rather than in full generality. The blog post also discusses the category Law of Lawvere theories, where a morphism between theories is a product-preserving functor that commutes with the embedding from Fop. We skip this level of abstraction. The key point preserved: Lawvere theories and finitary monads are equivalent descriptions of algebraic structure, but Lawvere theories compose (via coproducts and tensor products) where monads do not.

Ready for the real thing? Read the blog post. Start with the five-step construction path, then trace how the coend formula recovers familiar monads.
← Topoi fin ยท 24 of 24