Monads

A monad is less mysterious than its name: a box around a value, plus one rule for chaining steps on boxes. Built here from scratch to tidy a chain of lookups that can fail.

Intermediate7 steps~7 min
Value — a plain value, here a name
Full box — the wrapper with a value inside
Empty box — the wrapper with nothing inside
Step — a function: plain value in, box out
// THE SHORT VERSION

In 30 seconds

Many steps in a program come with a complication: the value may be missing, may arrive later, or may be an error. Dealing with that after every single step buries the real work.

A monad packs the complication into a wrapper around the value, and adds one operation that chains steps: it feeds the wrapped value to the next step and returns a new wrapper.

Maybe (a value or nothing) and Promise (a value later) are everyday examples. You write the plain steps; the wrapper handles what happens in between.

Monads, step by step

STEPS01 / 07

An informal picture. Formally, wrap and chain must also obey three laws (two identity laws and associativity). Promise is only monad-like: then also accepts plain return values and flattens nested promises.

STEP 01 / 07 A chain that can break
THE MATH
ana → ben → cy → dee three lookups, a check after each one
  1. Step 1: A chain that can break. Ana’s boss is Ben, Ben’s is Cy, Cy’s is Dee, and Dee has none. To find who sits three levels above someone, look up the boss three times. Any lookup may come back with nothing, so the careful version tests the result after every step, and the tests start to bury the real work.
  2. Step 2: Put the answer in a box. Instead of returning either a name or nothing, make the lookup always return a box. The box is either full, written Some('ben'), or empty, written None. This wrapper is usually called Maybe or Option. The empty case is no longer a trap: it is an ordinary value like any other.
  3. Step 3: One rule for chaining. Give the box one method, chain, which takes the next step as a function. A full box hands its value to the function and returns the box that the function produces, so boxes never pile up inside boxes. An empty box ignores the function and stays empty. The test for “is there a value?” now lives in one place.
  4. Step 4: Three steps, all found. Now the three lookups read as one straight line. Each chain opens the box, runs boss on the name inside, and passes along the new box that boss returns. Starting from ana, the boxes hold ben, then cy, then dee: the answer, still in its box.
  5. Step 5: One step fails, rest skip. Start from cy instead. The first lookup finds dee; the second finds nobody and returns an empty box. The third chain sees the empty box and never calls boss. The code is the same line as before and contains no if: the emptiness simply travels to the end.
  6. Step 6: Promise: the same shape. A Promise is the same idea for a different concern: a box for a value that arrives later, such as a reply from a server. Its chain method is called then. Each then waits for the value and passes it to the next step, and a failure (a rejection) skips the remaining steps, just as None did.
  7. Step 7: The pattern has a name. That is all a monad is: a kind of wrapper, a way to put a plain value into it, and a chain operation that feeds the wrapped value to a function returning another wrapper. Each kind looks after one concern between the steps: a missing value, a wait, a failure, or many values at once.
VALUES SO FAR
lookups3, any may failproblem
full····box
empty····box
chain····rule
from ana····result
from cy····result
Promise····chain
monad····pattern

Spotted a mistake on this page?