| name | category-theory |
| description | Mathematical framework for abstract structures and their relationships via categories, functors, natural transformations, limits, adjunctions, and monads. Provides dual mathematical and programming perspectives with Haskell typeclasses (Functor/Monad/Applicative) and Python libraries (pycategories, category-theory-python). Use when reasoning about compositional program structure, understanding functional abstractions, connecting algebraic concepts to code, working with monadic effects or F-algebras for recursive data, or following the Category Theory for Programmers curriculum. |
Category Theory for Programmers
Overview
Category theory studies mathematical structures through their relationships rather than internal composition. Every concept has a dual perspective: a precise mathematical definition and a direct programming interpretation.
Core thesis (Milewski): Composition is the essence of programming, and composition is at the root of category theory — it is part of the definition of a category. The historical progression from subroutines to structured programming to OOP to functional programming reflects increasing composability. Side effects break composition; category theory provides the framework for making effects explicit (monads) and composition principled (adjunctions, universal properties).
A category has objects and morphisms (arrows) that compose associatively with identity morphisms. In programming, the prototypical category is Hask: Haskell/Python types as objects, functions as morphisms. A functor maps between categories preserving structure — in code, fmap lifts a function into a container context. A natural transformation relates two functors uniformly across all objects — in code, a function that converts one container type to another regardless of contents.
When to Use
- Understanding or designing Haskell typeclasses (Functor, Applicative, Monad)
- Using Python category theory libraries (pycategories, category-theory-python)
- Reasoning about compositional program structure and algebraic data types
- Connecting mathematical structures (monoids, adjunctions, F-algebras) to code
- Designing APIs using universal properties as interface contracts
- Working with monadic effects, recursion schemes, or free constructions
- Studying type theory semantics via cartesian closed categories
- Following the Category Theory for Programmers (CTFP) curriculum
Core Concepts
Categories
A category C has objects and morphisms with associative composition and identity morphisms.
| Perspective | Description |
|---|
| Math | Objects + Hom-classes + composition satisfying associativity and unit axioms |
| Code | Hask: types as objects, functions as morphisms. f : A -> B is a morphism |
Key examples: Set (sets/functions), Grp (groups/homomorphisms), a monoid as a single-object category, a preorder as a category with at most one morphism per pair.
Semigroups and Monoids
Foundational algebraic structures that precede functors and monads in the abstraction hierarchy.
| Perspective | Description |
|---|
| Math | Semigroup: set + associative binary operation. Monoid: semigroup + identity element |
| Code | mappend(a, b) is associative; mempty is the identity. Strings under concatenation, integers under addition |
A monoid is the simplest categorical structure — a single-object category where morphisms are the monoid elements and composition is the binary operation.
Functors
| Perspective | Description |
|---|
| Math | Maps objects to objects, morphisms to morphisms, preserving identities and composition |
| Code | Type constructor F with fmap(f, F(a)) -> F(b). Laws: fmap(id) == id, fmap(g∘f) == fmap(g)∘fmap(f) |
Covariant functors preserve arrow direction; contravariant functors reverse it (equivalently, covariant from the opposite category). Forgetful functors strip structure (Grp → Set); free functors build it (Set → Grp via free groups).
Natural Transformations
| Perspective | Description |
|---|
| Math | Family of morphisms η_X : F(X) → G(X) satisfying the naturality square: η_Y ∘ F(f) = G(f) ∘ η_X |
| Code | Uniform conversion between container types. E.g., list :: Maybe a -> [a] where list(Just x) = [x], list(Nothing) = [] |
When each component is an isomorphism, the functors are naturally isomorphic — "the same" up to canonical identification.
Monads (Preview)
A monad is a monoid in the category of endofunctors: endofunctor T with unit η : Id → T and multiplication μ : T² → T satisfying associativity and unit laws. In programming, monads model computational effects (Maybe for failure, List for nondeterminism, State for mutable state). Full treatment in reference files.
Usage Examples
Haskell: Functor and Monad Typeclasses
class Functor f where
fmap :: (a -> b) -> f a -> f b
-- Laws: fmap id == id; fmap (g . f) == fmap g . fmap f
class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
-- Laws: return a >>= f == f a
-- m >>= return == m
-- (m >>= f) >>= g == m >>= (\x -> f x >>= g)
Python: pycategories — Maybe with fmap
from categories import fmap
from categories.maybe import Just, Nothing
result = fmap(lambda x: x.upper(), Just("hello"))
print(result)
result = fmap(lambda x: x.upper(), Nothing())
print(result)
Python: pycategories — Custom Monoid Instance with Law Checking
from categories import monoid, mappend, mempty
monoid.instance(dict, lambda: {}, lambda a, b: dict(**a, **b))
result = mappend({'x': 1}, {'y': 2})
print(result)
print(monoid.identity_law({'a': 1}))
print(monoid.associativity_law({'a': 1}, {'b': 2}, {'c': 3}))
Python: category-theory-python — Monoids with Property-Based Testing
from category_theory.monoid import IntPlus, String
a, b = IntPlus(3), IntPlus(5)
print(a + b)
print(a + IntPlus.e())
from category_theory.operations import fold
values = [IntPlus(v) for v in [1, 2, 3, 4]]
print(fold(values, IntPlus))
Advanced Topics
Categories and Morphisms: Categories, morphism types, functors, natural transformations, semigroups, monoids → Categories and Morphisms
Universal Properties: Products, coproducts, limits, colimits, adjunctions, Yoneda lemma → Universal Properties
Monads and Algebras: Monads, comonads, monoidal categories, F-algebras, Lawvere theories → Monads and Algebras
Higher Category Theory: 2-categories, bicategories, quasi-categories, infinity-groupoids, topoi → Higher Category Theory
CTFP Curriculum: Milewski's "Category Theory for Programmers" — thesis, structure, and chapter guide → CTFP Curriculum
Python Category Theory: pycategories and category-theory-python libraries with deep API coverage → Python Category Theory
Design Patterns: Practical CT-inspired patterns for API design and code architecture → Design Patterns