Monads & String Diagrams

Monads from adjunctions and the string diagram calculus

Monads & String Diagrams

Every adjunction gives rise to a monad, and string diagrams provide a beautiful visual calculus for working with them. Monads unify concepts from algebra (free algebras), topology (compactifications), and computer science (side effects in functional programming).

Monad from Adjunction

Every adjunction F ⊢ G gives rise to a monad. The endofunctor is T = G∘F, the unit comes from the adjunction unit η : Id → GF, and the multiplication μ : GFGF → GF is built using the counit ε : FG → Id applied "in the middle."

CDAFG

Endofunctor T = G∘F: Starting in category C, F takes us to D, then G brings us back to C. The round-trip T = GF is an endofunctor on C.

Key insight: Every adjunction gives a monad, and every monad arises from an adjunction (in fact, from at least two canonical ones: the Kleisli category and the Eilenberg-Moore category). The monad "remembers" the round-trip structure while forgetting which specific adjunction produced it.

String Diagram Builder

In string diagrams, vertical strings represent functors and nodes (dots/boxes) represent natural transformations. Time flows downward. Drag nodes to rearrange the diagram and explore different presets.

Two natural transformations composed vertically: beta after alpha.

FGHαβtime →
String = Functor
Node = Natural Transformation
Drag nodes to rearrange

Key insight: String diagrams turn abstract algebra into topology. Vertical composition is stacking nodes along a string, horizontal composition is placing strings side by side. The naturality condition becomes the ability to slide nodes past each other freely — making equalities visible as continuous deformations.

Monad Laws as String Diagrams

The monad laws become topological deformations in string diagram notation. The unit η appears as a "cap" node and the multiplication μ as a "merge" node. Watch how one side continuously deforms into the other.

μ ∘ (ηT) = id
LHS
ημTT
RHS
T= id_T
=Applying η then μ on the left is the same as doing nothing

Left unit law: μ ∘ (ηT) = id. If we first apply the unit η on the left strand (creating a "bump"), then merge with μ, we get back the identity. In string diagrams, the bump "straightens out."

Key insight: In string diagrams, the monad laws become topological: unit laws say a "cap-then-merge" bump can be smoothed flat, and associativity says two merges can slide past each other. These are exactly the axioms of a monoid — a monad is a "monoid in the category of endofunctors."

Monad Examples Gallery

Monads appear everywhere in mathematics and programming. Each monad packages an endofunctor T with unit η and multiplication μ satisfying the monad laws. Toggle between the mathematical and code perspectives.

List Monad

The free monoid monad
Endofunctor T
T(X) = List(X) = ∐ₙ Xⁿ (finite sequences of elements from X)
Unit η
η_X : X → List(X), η(x) = [x] (singleton list)
Multiplication μ
μ_X : List(List(X)) → List(X), μ = concat (flatten nested lists)

The List monad captures nondeterminism. T(X) is the free monoid on X. The unit wraps a value in a singleton list, and multiplication flattens a list of lists into a single list.

TListηunitμmultiplyString diagram

Key insight: The same monad structure connects mathematics and programming: the mathematical (T, η, μ) triple corresponds exactly to a type constructor with return and join (or equivalently, bind). The monad laws ensure that sequencing computations is well-behaved.

Key Takeaways

  • Monads from Adjunctions — Every adjunction F ⊢ G gives a monad T = GF with unit η and multiplication μ.
  • String Diagrams — A topological calculus where strings = functors and nodes = natural transformations.
  • Monad Laws — Unit and associativity laws are simple topological deformations in string diagrams.
  • Ubiquitous Monads — List, Maybe/Option, PowerSet: monads appear in both math and programming.