Algebras and Coalgebras
Algebras and coalgebras provide complementary ways to model construction and behavior.
An algebra consumes structure into a value:
A coalgebra unfolds a value into observable structure or next behavior:
Algebras
Algebras are useful for folds, accumulation, interpretation, and summarization.
Examples:
- Folding committed events into state samples.
- Accumulating observations into a projection.
- Interpreting a command value into a transition decision.
- Reducing validation results into accept, reject, or nil.
Coalgebras
Coalgebras are useful for behavior, observation, transition systems, and processes that expose next steps.
Examples:
- A state machine exposing possible next transitions.
- A process exposing its next required input, timeout, or command.
- A behavior exposing current value and future evolution.
- An observer exposing what it can observe, emit, or request next.
Algebras and coalgebras are connected to event-state duality. Folding events into state is algebraic. Observing state for possible events or future behavior is coalgebraic. Neither side fully replaces the other.
State machines give the standard example. The initial algebra of event histories maps into a machine's states by reachability, and the states map into the final coalgebra of behaviors by observability. The composite is a table between past and future histories, an instance of element-predicate duality, and the minimal machine is the factorization of that table through the fewest states.