advanced34 min studyLesson 538 in Mathematics

Monads and algebras for a monad

Flatten nested finite subsets and verify the unit and algebra laws by explicit calculation.

On this page

What you’ll understand

  • State both monad unit laws and the associativity law with matching types.
  • Verify the finite powerset monad using singleton insertion and union.
  • Calculate an algebra structure and distinguish it from the monad multiplication.

A monad records an operation that can introduce structured data and flatten repeated layers of that structure coherently. The flattening is constrained by two unit laws and an associativity law; an arbitrary endofunctor is not enough. Finite subsets provide an example in which every layer is a finite set that can be inspected. An algebra for this monad then interprets a finite collection inside a particular object, such as taking its maximum in an ordered chain.

An endofunctor needs coherent unit and multiplication

A monad on C\mathcal C consists of an endofunctor TT, a natural transformation η:Id⇒T\eta:\mathrm{Id}\Rightarrow T, and a natural transformation μ:T2⇒T\mu:T^2\Rightarrow T. Both ways of inserting a trivial layer into an already structured object must flatten back to that object. Flattening three layers in either order must agree. The subscripts specify which layer is being manipulated, so the two unit expressions are different maps even when their composites with multiplication coincide.

An adjunction F⊣GF\dashv G produces a monad T=GFT=GF with the adjunction's unit and with multiplication GεFG\varepsilon F. The triangle identities imply the unit laws, while naturality of the counit supplies associativity. This explains one source of monads without claiming that every useful calculation needs the adjunction written down. In the concrete example below, singleton insertion and union can be verified directly, including their compatibility with maps.

μX∘T(ηX)=idTX=μX∘ηTX,μX∘T(μX)=μX∘μTX.\mu_X\circ T(\eta_X)=\mathrm{id}_{TX}=\mu_X\circ\eta_{TX},\qquad \mu_X\circ T(\mu_X)=\mu_X\circ\mu_{TX}.
The unit equations have domain T(X), and the associativity equation has domain T³(X).↔ Scroll wide equations horizontally.

The finite powerset monad uses direct images

On FinSet, let T(X)T(X) be the set of all subsets of XX. For a function ff, define T(f)T(f) by direct image of subsets. This is covariant: direct images preserve identities and compose in the same order as functions. Set ηX(x)={x}\eta_X(x)=\{x\}, and let μX\mu_X take the union of a family of subsets. The empty subset and the empty family are included. Direct image commutes with union and singleton formation, proving naturality of both transformations.

The two unit laws say that unioning a singleton family containing S or unioning the family of singleton subsets of S both recover S. Associativity says membership in the final union is equivalent to membership in some subset belonging to some family, regardless of which union is performed first. Since X is finite, all iterated powersets remain finite; for a three-point X their first two sizes are eight and 256. Inverse image would have the wrong variance for this endofunctor.

ηX(x)={x},μX(A)=⋃S∈AS,∣X∣=3⟹∣TX∣=8, ∣T2X∣=256.\eta_X(x)=\{x\},\qquad \mu_X(\mathscr A)=\bigcup_{S\in\mathscr A}S,\qquad |X|=3\Longrightarrow |TX|=8,\ |T^2X|=256.
The multiplication removes one family layer while preserving the underlying elements.↔ Scroll wide equations horizontally.

An algebra interprets structured data in one object

An algebra for a monad is an object X with a map α:TX→X\alpha:TX\to X satisfying αηX=idX\alpha\eta_X=\mathrm{id}_X and αμX=αT(α)\alpha\mu_X=\alpha T(\alpha). The first says interpreting a trivial structure returns its original element. The second says interpreting after flattening agrees with interpreting each inner structure and then interpreting the resulting outer structure. These equations involve the algebra map, which has a different target from the monad multiplication.

For the finite powerset monad, a finite chain with a least element becomes an algebra by taking the maximum of a nonempty subset and interpreting the empty subset as the least element. Singleton interpretation is immediate. For a family, the largest element appearing anywhere is also the largest of the inner maxima, with the empty case handled by the least element. More generally these algebras express finite joins with a bottom element; the empty-set convention is part of the laws, not a disposable special case.

α∘ηX=idX,α∘μX=α∘T(α).\alpha\circ\eta_X=\mathrm{id}_X,\qquad \alpha\circ\mu_X=\alpha\circ T(\alpha).
An algebra evaluates one structured layer in X, rather than flattening it into another structured layer.↔ Scroll wide equations horizontally.

Interpret a family of subsets in a three-point chain

For X={0,1,2}X=\{0,1,2\} ordered normally, let α(S)=max⁡S\alpha(S)=\max S for nonempty S and α(∅)=0\alpha(\varnothing)=0. Compare the two sides of the algebra law on A={{0,2},{1},∅}\mathscr A=\{\{0,2\},\{1\},\varnothing\}, and check associativity on a concrete three-layer family.

  1. Union the members of A\mathscr A to obtain {0,1,2}\{0,1,2\}, whose interpretation is two. Alternatively interpret the three members first: they give two, one, and zero. The outer interpretation of {2,1,0}\{2,1,0\} is again two.

  2. For each element x, the singleton {x}\{x\} has maximum x, proving the algebra unit law at all three points. The empty family interprets through union as zero, consistently with the chosen bottom element.

  3. Take the three-layer object B={{{0},{1}},{{2},∅}}\mathscr B=\{\{\{0\},\{1\}\},\{\{2\},\varnothing\}\}. Flattening the outer layer first gives the family {{0},{1},{2},∅}\{\{0\},\{1\},\{2\},\varnothing\}. Flattening each inner family first gives {{0,1},{2}}\{\{0,1\},\{2\}\}. One more union gives {0,1,2}\{0,1,2\} by either route.

α(μX(A))=2=α(T(α)(A)).\alpha\bigl(\mu_X(\mathscr A)\bigr)=2=\alpha\bigl(T(\alpha)(\mathscr A)\bigr).
The equality compares interpreting a union with taking the maximum of the inner interpretations.↔ Scroll wide equations horizontally.
The result

Both algebra-law routes return two, and both monad-associativity routes return the full three-point subset.

Explore the model

Interactive lab

Interactive lab · probability

Count subsets while tracking lost structure

Pause and Reset, then set success probability to 0.5 and trials to 3. Predict eight equally likely subsets of a three-point set, grouped by size with counts 1, 3, 3, and 1. The three singleton subsets lie in one bar, although their maxima in the worked chain are 0, 1, and 2. The histogram counts subsets but neither computes their unions nor their algebra interpretations. For fresh comparisons use Pause, Reset, then both controls; parameter changes retain elapsed time.

Count subsets while tracking lost structureThese are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Current readings: Mean · np: 5; Variance · np(1−p): 2.5; Total probability: 1. All parameters are adjustable with the labeled controls below.03581000.070.150.220.3Successes kP(X = k)k = 0 · P = 9.77e-4
Exact probabilityHighlighted outcomeMean np
Mean · np
5
Variance · np(1−p)
2.5
Total probability
1
t = 0 s

These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forAn algebra uses labeled elements and their operation, information that a subset-size histogram does not preserve.

Check your understanding

For the finite powerset monad and S={0,2}, which statement about the two unit insertions is correct?

Try it yourself

For X={0,1,2}X=\{0,1,2\} and S={0,2}S=\{0,2\}, list ηTX(S)\eta_{TX}(S) and T(ηX)(S)T(\eta_X)(S) explicitly. Apply the monad multiplication to each and explain why their difference does not violate a unit law.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

A singleton containing a subset differs from a family containing the singleton subsets of its elements. Count the members of each family before taking unions.

Compare with the solution

The first family is {{0,2}}\{\{0,2\}\} and has one member. The second is {{0},{2}}\{\{0\},\{2\}\} and has two members. Union removes the outer layer in each case and gives {0,2}\{0,2\}. The unit laws require equality after multiplication, not equality of the two insertion maps themselves. Both composites are the identity at S.

Take it with you

  • A monad includes natural unit and multiplication maps satisfying three coherence equations.
  • The powerset monad is covariant through direct images, with singleton insertion and union.
  • An algebra interprets structured data and must respect both singleton insertion and flattening.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.