Monads and Algebras/Monads from Adjunctions

Lesson 7.11,343 words

Monads from Adjunctions

A monad on a category is an endofunctor equipped with a unit and a multiplication satisfying associativity and unit laws — the data of a monoid, written internally to the category of endofunctors. Every adjunction induces one, and the list, exception, and state constructions that model computational effects are all monads on Set.

╌╌╌╌

An adjunction between categories and induces structure on the domain side. The round trip is an endofunctor ; the unit is a natural transformation into it; and the counit , inserted in the middle, produces a natural transformation collapsing two applications of into one. That triple of data (an endofunctor, a unit into it, and a flattening map) is a monad, and it satisfies laws that make it a monoid.

Barr and Wells call the structure a triple, and the two words name the same thing.1 The monad records what an adjunction does to its domain without reference to the codomain or the functor that reached it. Two different adjunctions can induce the same monad, and every monad can be resolved back into an adjunction, generally into many.

The definition

Here and are the two whiskerings of by : has component , while . The associativity law states that the two ways of contracting a triple stack down to agree. The unit law states that inserting a fresh -layer by (either underneath, as , or on top, as ) and then multiplying returns the identity.

The associativity square: contracting the outer pair of -layers (, writing for ) or the inner pair () and then multiplying again give the same map .
The two unit triangles: inserting a layer with the unit (writing for ) underneath () or on top (), then multiplying, is the identity on .

The diagonal maps in the second figure are identity arrows ; the triangles assert that the two composites down through equal them.

The monoid analogy

The laws reproduce the axioms of a monoid, transcribed one level up. A monoid in has a multiplication and a unit satisfying associativity and the unit laws. Replace the set by an endofunctor , the cartesian product by composition of endofunctors, the one-point set by the identity functor, and the axioms become the monad laws.2

Monoid in Monad on
carrier set endofunctor
unit unit
multiplication multiplication

The category of endofunctors carries a composition that plays the role of a product (a monoidal structure, with the identity functor as its unit). A monad is a monoid object in that monoidal category. Reading the table line by line gives the standard formulation: a monad is a monoid in the category of endofunctors.

Adjunctions induce monads

Write . The unit of the monad is the unit of the adjunction. The multiplication has component : it applies the counit in the middle, at the object , and transports the result back through . The associativity and unit laws of the monad follow from the two triangle identities of the adjunction, applied under and over .

An adjunction between (drawn ) and (drawn ) yields the monad on ; the counit at supplies the multiplication and the adjunction unit supplies the monad unit.

Every adjunction gives a monad by this proposition, and conversely every monad arises from some adjunction, generally from many. The Kleisli and Eilenberg–Moore constructions produce two canonical adjunctions resolving a given monad; they sit at the two extremes of a poset of such resolutions.

The free monoid, as a monad

The free–forgetful adjunction between and produces the first computational example. The free monoid on a set is , the set of finite strings (lists) over ; forgetting the monoid structure returns the underlying set. Composing free after forgetful gives the endofunctor

with mapping over each list. This is the list monad.4 Its unit and multiplication read off directly:

  • Unit sends an element to the singleton list .
  • Multiplication takes a list of lists and concatenates it, erasing the inner brackets. For example flattens to .
The list monad's multiplication flattens a list of lists by concatenation; the unit wraps a single element as a one-element list.

Associativity of the monad is associativity of concatenation: flattening a triply-nested list of lists gives the same result whether the inner brackets or the outer brackets go first. The unit laws say that wrapping every element as a singleton and then flattening, or wrapping the whole list once and then flattening, both leave the list unchanged.

Monoids and the writer monad

Barr and Wells give a second construction from a fixed monoid , the representation triple.5 Define

where is the identity of and is its product. The unit and multiplication of the monad are the unit and multiplication of , threaded past the carried element . In programming this is the writer monad: a value paired with an accumulated -labelled output (a log, a running cost, a count), and combines two accumulations with the monoid operation. The monad laws reduce directly to the monoid laws of .

This monad arises from the underlying-set / free adjunction on the category of -sets, exactly as the list monad arises from and .

Monads as computational effects

A monad on packages a notion of computation producing an into the type . The unit injects a pure value as a trivial computation, and the multiplication collapses a computation-of-a-computation into a single one. The recurring examples of effectful programming are all monads.

MonadEffect modelled
Listfinite lists over nondeterminism, multiple results
Maybe (the left injection)partiality, failure
Writeraccumulated output over a monoid
Readerconstant function a fixed environment
Statea threaded mutable state

The maybe monad adds a distinguished no result point; merges the two failure points into one, so a failure anywhere in a chain propagates. The state monad makes a computation a function that consumes an initial state and returns a value together with a new state; threads the state through two such steps in order. Each entry is a monad on arising from an adjunction.

The algebras of these monads recover the corresponding categories of structured sets, and the multiplication becomes the sequencing operation of effectful programs in the Kleisli category.

Comonads

Dualizing the definition — reversing every arrow in the two coherence diagrams — gives a comonad, which Barr and Wells call a cotriple.6

An adjunction induces a comonad on , the codomain side, at the same time as it induces the monad on . Where a monad models building up a structure and collapsing nested layers, a comonad models a context that can be observed () and duplicated (): infinite streams, labelled trees, and stores of values are comonads. The Kleisli construction of a comonad — the co-Kleisli category — reappears in the theory of cartesian closed categories built from a comonad, a route into models of linear logic.7

Algebras, sequencing, and the bare endofunctor

Three constructions build on a monad:

  • Algebras. Its algebras are objects carrying a -structure map compatible with and ; they form the Eilenberg–Moore category.
  • Sequencing. Its Kleisli category has as arrows the effectful programs, composed by .
  • The bare endofunctor. Dropping and leaves an endofunctor and its algebras, whose initial object is the least fixed point that models inductive datatypes.

Footnotes

  1. Barr & Wells, Category Theory for Computing Science, §14.3.1 — Definition of a triple and its unitary and associative identities; §14.3 notes that monad is the widely used synonym.
  2. Barr & Wells, §14.3.1 — the names unit and multiplication come from the analogy between triples and monoids, made precise in §14.3.4.
  3. Barr & Wells, §14.3.2–14.3.3 (Proposition) — an adjoint pair gives the triple on the domain of the left adjoint.
  4. Barr & Wells, §14.3.6 Exercise 3 — the Kleene-closure functor with singleton unit and concatenation multiplication is a triple.
  5. Barr & Wells, §14.3.4 — the representation triple of a monoid , arising from the free / underlying adjunction on -sets.
  6. Barr & Wells, §14.3.5 — a cotriple is a triple in .
  7. Barr & Wells, §16.2.5 — the (co-)Kleisli category of a cotriple on a symmetric monoidal closed category, used to construct models of classical logic inside linear logic.

╌╌ END ╌╌