โ† back to index

F-Algebras

Bartosz Milewski ยท 2017 ยท Category Theory for Programmers, Ch. 19

Prereqs: ๐Ÿž Ch7 (Functors), ๐Ÿž Ch6 (Simple Algebraic Data Types). 10 min.

An F-algebra is a functor F plus a morphism F(a) → a. The initial algebra is the fixed point of the functor: it is the recursive data type. A catamorphism (fold) is the unique morphism from the initial algebra to any other algebra. Every recursive data type you define is an initial algebra. Every fold you write is a catamorphism.

What is an F-algebra

An F-algebra has three parts: an endofunctor F (the shape of one layer of recursion), a carrier object a (the result type), and an evaluator F(a) → a (how to collapse one layer). The functor generates expressions; the evaluator interprets them.

F(a) F(a) a a F(f) f alg alg the algebra alg : F(a) → a is the evaluator
Scheme

Fixed points and initial algebras

A recursive data type is the fixed point of its functor: Fix f = f (Fix f). Wrapping with Fix lets you nest layers infinitely. The fixed point is the initial algebra: there is exactly one algebra homomorphism from it to any other algebra. Lambek's theorem says the initial algebra's evaluator is an isomorphism: F(Fix f) ≅ Fix f.

Scheme

Catamorphisms โ€” the universal fold

A catamorphism takes an algebra (evaluator) and recursively collapses a fixed-point structure into the carrier. It peels off one layer with unfix, recursively processes all sub-structures with fmap, then applies the evaluator. This is the unique homomorphism guaranteed by initiality.

F(Fix F) F(a) Fix F a F(cata) cata alg in alg cata is the unique arrow from the initial algebra
Scheme

Lists as an initial algebra

Lists are the initial algebra of ListF e a = Nil | Cons e a. The catamorphism over ListF is foldr. Every fold you've ever written is a catamorphism in disguise.

Scheme

Coalgebras and anamorphisms โ€” the dual

An F-coalgebra reverses the arrow: a → F(a). Instead of collapsing structure, it generates it. The terminal coalgebra is the greatest fixed point. The anamorphism (unfold) is the unique morphism into the terminal coalgebra. In Haskell, the fixed point for both is Fix f, but conceptually: algebras consume, coalgebras produce.

Scheme

Ring expressions โ€” algebras as evaluators

F-algebras cleanly separate syntax from semantics. The functor defines the shape of expressions (syntax). Different algebras over the same functor give different interpretations (semantics). Here: ring expressions can be evaluated to integers, pretty-printed to strings, or simplified, all via different algebras over the same RingF functor.

Scheme

Notation reference

Blog / Haskell Scheme Meaning
type Algebra f a = f a -> a(eval-int x)F-algebra: evaluator from one layer to carrier
Fix f = Fix (f (Fix f))(fix x) / (unfix x)Fixed point: recursive nesting of functor layers
cata alg = alg . fmap (cata alg) . unFix(cata fmap alg x)Catamorphism: universal fold from initial algebra
ana coalg = Fix . fmap (ana coalg) . coalg(ana fmap coalg seed)Anamorphism: universal unfold into terminal coalgebra
data NatF a = ZeroF | SuccF a'(zero) / (list 'succ a)Functor for natural numbers
data ListF e a = NilF | ConsF e a'(nil) / (list 'cons e a)Functor for lists
Lambek: F(Fix f) ≅ Fix ffix/unfix are inversesInitial algebra evaluator is an isomorphism
Neighbors

Milewski chapters

Foundations (Wikipedia)

Translation notes

The blog post uses Haskell's type system to enforce the functor constraint on cata and to distinguish Fix from unFix at the type level. In Scheme, fix and unfix are just list wrappers; the functor map is passed explicitly. The blog post covers infinite structures via coalgebras (streams, the Sieve of Eratosthenes). This page limits coalgebras to finite unfolding because BiwaScheme does not support lazy evaluation. The blog post also proves that Lambek's theorem follows from initiality: the inverse of the evaluator is itself an algebra homomorphism, so it must be unique, making the evaluator an isomorphism. That proof is structural, not computational, so it does not translate to runnable code.

Ready for the real thing? Read the blog post. Start with the definition of Algebra, then trace how Fix and cata fall out of initiality.