# Category Theory

> 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.

- Skill: `tangledgroup/category-theory` (Agent Skill, multi-file: 9 files)
- Install (CLI): `npx skillmds@latest add tangledgroup/category-theory`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tangledgroup/category-theory/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: tangledgroup (https://skillmd.com/u/tangledgroup)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tangledgroup/category-theory

---


# 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

```haskell
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

```python
from categories import fmap
from categories.maybe import Just, Nothing

# Lifting a function into the Maybe context
result = fmap(lambda x: x.upper(), Just("hello"))
print(result)  # Just("HELLO")

# Short-circuiting on Nothing
result = fmap(lambda x: x.upper(), Nothing())
print(result)  # Nothing
```

### Python: pycategories — Custom Monoid Instance with Law Checking

```python
from categories import monoid, mappend, mempty

# Define a monoid instance for dict (merge by keys)
monoid.instance(dict, lambda: {}, lambda a, b: dict(**a, **b))

# Use it
result = mappend({'x': 1}, {'y': 2})
print(result)  # {'x': 1, 'y': 2}

# Verify laws
print(monoid.identity_law({'a': 1}))           # True
print(monoid.associativity_law({'a': 1}, {'b': 2}, {'c': 3}))  # True
```

### Python: category-theory-python — Monoids with Property-Based Testing

```python
from category_theory.monoid import IntPlus, String

# IntPlus monoid (integers under addition, identity 0)
a, b = IntPlus(3), IntPlus(5)
print(a + b)           # IntPlus(8)
print(a + IntPlus.e()) # IntPlus(3) — identity law

# Fold a list of monoids
from category_theory.operations import fold
values = [IntPlus(v) for v in [1, 2, 3, 4]]
print(fold(values, IntPlus))  # IntPlus(10)
```

## Advanced Topics

**Categories and Morphisms**: Categories, morphism types, functors, natural transformations, semigroups, monoids → [Categories and Morphisms](reference/01-categories-and-morphisms.md)

**Universal Properties**: Products, coproducts, limits, colimits, adjunctions, Yoneda lemma → [Universal Properties](reference/02-universal-properties.md)

**Monads and Algebras**: Monads, comonads, monoidal categories, F-algebras, Lawvere theories → [Monads and Algebras](reference/03-monads-and-algebras.md)

**Higher Category Theory**: 2-categories, bicategories, quasi-categories, infinity-groupoids, topoi → [Higher Category Theory](reference/04-higher-category-theory.md)

**CTFP Curriculum**: Milewski's "Category Theory for Programmers" — thesis, structure, and chapter guide → [CTFP Curriculum](reference/05-ctfp-curriculum.md)

**Python Category Theory**: pycategories and category-theory-python libraries with deep API coverage → [Python Category Theory](reference/06-python-category-theory.md)

**Design Patterns**: Practical CT-inspired patterns for API design and code architecture → [Design Patterns](reference/07-design-patterns.md)

