Definition
Monad
A monad on a category is an endofunctor equipped with two natural transformations:
- Unit (return):
- Multiplication (join):
satisfying associativity and unitality:
Programming
In functional programming, a monad structures computations with effects. The operations are usually written:
| Category Theory | Programming |
|---|---|
type constructor M a | |
| (unit) | return :: a -> M a |
| (join) | join :: M (M a) -> M a |
fmap then return | |
| Kleisli composition | >=> or bind (>>=) |
Monads as programmable semicolons
A monad captures a pattern of sequencing. The
bindoperation (>>=) threads a value through a chain of effectful computations, where the exact meaning of “effect” (state, I/O, exceptions, non-determinism) is determined by the particular monad instance.