Category Theory As The Language Of Composable AI

When the shape of what an AI learns determines whether it can keep learning at all.

Category Theory · Machine Learning · AI Safety
The Forgotten Mathematics That Could Make AI Trustworthy
2026 · Daice Labs

There is a branch of mathematics so abstract that its practitioners once called it "general abstract nonsense" as a term of affection. Category theory spent decades as the private language of pure mathematicians. Now a growing number of researchers believe it could reshape how we build, understand, and guarantee the safety of artificial intelligence.

The central idea is deceptively simple. Category theory is the mathematics of composition: of how parts fit together to form wholes while preserving structure. And that, it turns out, is precisely what AI research keeps running into as its hardest problems. How do you compose neural modules so the system behaves predictably? How do you reason about what a learning algorithm is actually computing? How do you build AI agents whose behaviors do not explode in surprising ways when combined?

I. The Problem Nobody Talks About

When a language model gives you a confident, fluent, completely wrong answer, the failure feels almost personal. But the engineers who built the model have a different word for it. They call it a failure of compositionality.

The model learned things in pieces. It absorbed facts about history, science, language, and logic from billions of text examples. But when you ask it to combine those pieces in a new way, in a context it has never seen, something breaks down. The whole does not behave the way the parts predicted. The system fails to generalize.

This is not a quirk of any particular model or any particular training run. It is, many researchers now believe, a structural problem. Current AI systems lack something that human cognition has, something that allows us to understand a sentence we have never heard, to follow a recipe we have never tried, to navigate a city we have just arrived in. We build up meaning compositionally, combining smaller understood pieces according to rules we have internalized. Our models, despite their power, often fake this process rather than embody it.

For the past decade, a small but growing community of mathematicians, computer scientists, and AI researchers has been arguing that the right tool for understanding composition, not just informally but with full mathematical precision, is a branch of pure mathematics that most AI researchers have never studied. It is called category theory. And the story of how it arrived at the frontier of AI is one of the stranger intellectual journeys in recent science.

II. Abstract Nonsense With a Purpose

Category theory was invented in 1945 by Samuel Eilenberg and Saunders Mac Lane, two mathematicians who were trying to make precise a vague but persistent feeling that different areas of mathematics were secretly saying the same thing. They were working on algebraic topology, and they kept noticing that certain constructions in very different mathematical settings had identical shapes. The theorems looked the same even when the objects being described were utterly different.

Their solution was radical. Instead of studying the objects of mathematics, they would study the relationships between them. A category consists of objects and arrows connecting those objects, with two rules: arrows can be composed (if there is an arrow from A to B and an arrow from B to C, there is one from A to C), and every object has an identity arrow that leaves it unchanged. That is it. No sets, no elements, no membership. Just objects, arrows, and composition.

The name that mathematicians gave to this new field reflected their affection and their skepticism in equal measure. They called it "general abstract nonsense." It was so stripped of content, so relentlessly focused on pure structure, that it seemed to many mathematicians to be philosophy dressed up as math. What could you possibly do with arrows and composition laws?

It turned out you could do quite a lot. Category theory eventually became the language in which mathematicians describe the deep unity between areas of mathematics that look completely unrelated on the surface. But for decades, this remained an insight available only to pure mathematicians. The applications to AI were essentially nonexistent. Then, around 2015, something began to change.

01 What a Category Is
Objects, arrows, and one rule: arrows compose. This minimal structure turns out to be the skeleton of logic, computation, and learning all at once.
A CATEGORY A B C f g g∘f composition: if f: A→B and g: B→C, then g∘f: A→C THE SAME STRUCTURE IN THREE WORLDS Types & Programs Objects = types · Arrows = functions · Compose = call Logic & Proofs Objects = propositions · Arrows = proofs · Compose = deduce Neural Networks Objects = tensor types · Arrows = layers · Compose = stack One definition. Three fields. The structure is the insight.
Objects (types, propositions, systems)
Morphisms (functions, proofs, transformations)
Composition (the organizing principle)
III. The Functor Hiding Inside Every Neural Network

In 2017, mathematician Brendan Fong sat down with his collaborators David Spivak and Rémy Tuyéras to write a paper with a title that sounded almost like a joke. They called it "Backprop as Functor."

Backpropagation is the algorithm that trains neural networks. It works by running inputs through the network, computing how wrong the output was, and then propagating that error backward through the network's layers to figure out how to adjust each parameter. The chain rule of calculus coordinates the whole thing. Researchers had been using this algorithm since the 1980s, and it had become the engine driving the entire deep learning revolution.

Fong and his colleagues looked at backpropagation and saw something nobody had explicitly named before. The process of composing neural network layers, where you stack one layer on top of another to build a deeper network, and the process of composing the corresponding learning rules are governed by the same mathematical structure. Specifically, they proved that there is a functor, a structure-preserving map between categories, that takes parameterized functions and turns them into learning systems in a way that perfectly respects how those functions compose.

In plain language: when you combine two independently designed network components into a larger network, the learning behavior of the larger network is automatically, provably determined by the learning behaviors of the components. This is not something you would guess from staring at the training algorithm. It is a theorem about the underlying structure of what learning is.

Around the same time, researchers noticed something else. Every layer in a neural network has the shape of a mathematical structure called a lens: a forward pass that produces output from input, and a backward pass that propagates error signals and parameter updates. Compose two lenses and you get a lens. Stack two layers and you get a layer. Bruno Gavranović's 2024 thesis showed that the same lens framework describes reinforcement learning as well. Supervised learning and reinforcement learning, which look completely different algorithmically, are different algebras on the same mathematical skeleton.

02 Layers as Lenses
Every neural network layer packages a forward pass and a backward pass into a single composable unit. Composing two lenses gives a lens. Composing two layers gives a layer. The chain rule is the composition law.
Layer A forward: x → f(x) backward: ∂L/∂x a lens Layer B forward: y → g(y) backward: ∂L/∂y a lens forward backward Composed Network forward: x → g(f(x)) backward: chain rule learning behavior is provably determined also a lens The chain rule is not a coincidence. It is the composition law of lenses.
Layer A (a lens)
Layer B (a lens)
Composed network (also a lens)
Backward pass (gradient)
String diagrams: the visual language of category theory

String diagrams: the visual language of category theory. Two diagrams that can be continuously deformed into each other without breaking any wires represent computations that are provably identical. Researchers now use this notation to reason about attention mechanisms, neural network architectures, and multi-agent systems.

IV. The Triple Correspondence

In 1969, a logician named William Howard wrote a manuscript that remained unpublished for a decade. It described something he had noticed while studying the relationship between mathematical logic and computation. The structure of proofs in intuitionistic logic corresponded exactly to the structure of programs in a certain kind of type system. A proof of a logical proposition was, in a precise technical sense, the same thing as a program that inhabited a type.

This became known as the Curry-Howard correspondence, named for Howard and for Haskell Curry, who had noticed a related connection a decade earlier. When Joachim Lambek added the observation that the same structure also described morphisms in certain categories, the correspondence became a triangle: logic, programming, and category theory are three different windows onto the same mathematical reality. A proposition is a type is an object. A proof is a program is a morphism. Logical deduction is program execution is categorical composition.

For AI, this triple correspondence has become practically important in a way its discoverers could not have anticipated. When a large language model generates a step-by-step mathematical argument, it is producing something that looks like a proof. But it is not a proof: it has not been checked. The Curry-Howard correspondence provides the bridge to make it one. A valid logical argument corresponds to a well-typed program, and whether a program is well-typed is something a computer can verify automatically and definitively.

This is the mathematical foundation of a new approach to AI that combines neural networks with formal verification systems. The language model generates candidate proof steps, drawing on its training to produce plausible mathematical reasoning. A separate verification system either accepts or rejects each step. DeepMind's AlphaProof, which in 2024 solved four of the six problems from the International Mathematical Olympiad by generating proofs in the Lean 4 formal proof system, is the most dramatic demonstration of this approach so far. The mathematics behind it is fifty years old.

V. Why Architectures Are Not Arbitrary

The past few years have brought a proliferation of neural network architectures: transformers, convolutional networks, graph neural networks, recurrent networks, state space models. From the outside, the differences can look like engineering choices: different ways of arranging the same basic operations. From a categorical perspective, they look like something quite different.

In 2024, de Felice, Toumi, and Coecke published a paper at the International Conference on Machine Learning titled "Categorical Deep Learning: An Algebraic Theory of Architectures." Their argument: each major neural network architecture corresponds to a functor between categories that encode specific geometric or relational symmetries.

A convolutional neural network enforces translation symmetry. A feature detector trained to recognize an edge works the same way wherever in the image that edge appears. In the categorical framework, this is a constraint: the network's computation must be equivariant with respect to the group of translations acting on the image grid. The convolutional structure is not an engineering choice. It is the unique architecture compatible with this categorical constraint.

A graph neural network enforces permutation symmetry. The order in which you list the nodes of a graph should not affect the output. A transformer has a sequential symmetry that is deliberately broken by positional encodings, which is why positional encodings are necessary for transformers to understand order at all. The implications: architecture design could become a systematic discipline. Identify the symmetries your problem has, find the functor that respects those symmetries, build the architecture that implements it.

VI. The Agent Problem

As AI systems become more capable, researchers have begun deploying them not just to answer questions but to take actions: browsing the web, writing code, executing programs, coordinating with other AI systems. These systems are called agents, and their failure modes are qualitatively different. An agent that makes a mistake does not just give a wrong answer. It does something wrong in the world, often in ways that are difficult to reverse.

The mathematical framework of polynomial functors, developed by David Spivak and his collaborators over the past decade, offers a way of reasoning about agents and their interactions that is precise enough to derive formal guarantees. A polynomial functor models a system with two kinds of information: what outputs it can produce, and what inputs it can receive in each possible output state. This captures the interaction structure of an agent exactly.

The surprising structural theorem at the heart of this theory: comonoids in the category of polynomial functors are exactly categories. Every composable AI agent, every multi-step planning system, every environment that an agent can interact with, lives naturally inside this framework. The practical upshot is a type system for agents. Just as a typed programming language prevents you from passing a string to a function that expects a number, a polynomial-functor type for an agent specifies precisely what observations it accepts and what actions it can produce. Containment, the property that an agent cannot affect parts of the environment outside its designated scope, becomes a type constraint rather than a runtime monitoring problem.

03 Agents as Typed Interfaces
A polynomial functor gives each agent a precise interface: what it observes, what it can do. Two agents with compatible interfaces compose safely. Incompatible interfaces are caught before the system runs — not by monitoring, but by mathematics.
Agent A observes: sensor data acts: motor commands type: p = Σ y^p(i) Agent B observes: motor commands acts: joint positions type: q = Σ y^q(j) type check ✓ Composed System Interface verified before runtime Containment by construction No unexpected interactions formally safe Incompatible types are rejected before the system runs — not by monitoring, but by mathematics.
Agent A (typed interface)
Agent B (typed interface)
Composed system (formally verified)
VII. The Safety Bet

In 2024, ARIA, the UK's Advanced Research and Invention Agency, committed £59 million to a program called Safeguarded AI. The program's stated aim is foundational research on formally verifiable AI systems: systems whose behavior can be guaranteed by mathematical proof rather than empirical testing alone.

The bet embedded in this investment is explicit. Compositional structure is what makes formal safety guarantees tractable: if a complex system is built from mathematically well-behaved parts, you can derive properties of the whole from properties of the parts without analyzing every possible interaction. And the mathematics of compositional structure is category theory.

The Topos Institute in Berkeley, co-founded by Spivak and Brendan Fong, has become the institutional center of this research. Their AlgebraicJulia project has applied categorical methods to epidemiological modeling, robotics task planning, and biochemical reaction networks. Categorical distributional semantics, developed by Clark, Coecke, and Sadrzadeh, gives a compositional semantics to natural language sentences, where the grammatical structure provides a wiring diagram and word vectors are plugged into the wires.

The mechanistic interpretability program at Anthropic, which aims to decompose neural network computations into understandable circuits, implicitly depends on a notion of circuit composition: what does it mean for two understood components to combine into an understood whole? The categorical framework provides a precise answer. The reward modeling problem in AI alignment, the challenge of specifying what we want an AI to do in a way it cannot misinterpret or game, is in categorical terms a problem of semantics: finding a morphism between the space of behavioral specifications and the space of agent objectives that preserves the structure that matters.

04 From Pure Math to AI Safety
How categorical ideas travel from abstract definitions to engineering constraints. Step through each stage of the journey.
The definition
Type systems
Architecture design
Formal verification
Safety guarantees
VIII. What Comes Next

The researchers working at this intersection face a familiar problem: the mathematics is mature enough to be useful, but the communication gap between pure mathematicians and AI engineers is wide. Most machine learning researchers have not studied category theory. Most category theorists do not work on machine learning. The papers that bridge the two fields require fluency in both, and there are currently very few people who have that fluency.

This is changing, slowly. Graduate courses in categorical deep learning are beginning to appear. Libraries like DisCoPy and Catlab.jl are making the mathematics computational and accessible to researchers who think in code rather than proofs. Introductory texts written specifically for non-specialists, including Fong and Spivak's Seven Sketches in Compositionality and the new string diagrams book by Piedeleu and Zanasi (Cambridge, 2025), are filling a gap that previously required years of graduate study to bridge.

The question is whether the field will move fast enough. The development of AI capabilities is not waiting for the mathematics to catch up. Systems are being deployed at scale, in consequential domains, with safety properties that are tested empirically rather than proven. The history of other engineering fields suggests this is not sustainable: eventually you need the mathematics, or the empirical testing is not enough.

Category theory does not promise to make AI safe. What it promises is to make the question of safety mathematically tractable: to give researchers a language precise enough to state what they want, a framework powerful enough to derive what follows from what, and a notion of composition rigorous enough to build complex systems from simple guarantees. That is a modest promise, by the standards of AI research. But it is also a real one. And right now, in a field increasingly aware of how much it does not understand about its own most powerful tools, a real promise is worth quite a lot.

Research synthesis drawing on: Fong, Spivak, Tuyéras, Backprop as Functor (LICS 2019); Cruttwell et al., Categorical Foundations of Gradient-Based Learning (ESOP 2022); Gavranović, Compositional Deep Learning (thesis, 2024); de Felice, Toumi, Coecke, Categorical Deep Learning (ICML 2024); Spivak and Niu, Polynomial Functors (Cambridge, 2025); Piedeleu and Zanasi, Introduction to String Diagrams for Computer Scientists (Cambridge, 2025); DeepMind AlphaProof (2024); Fong and Spivak, Seven Sketches in Compositionality (Cambridge, 2019); Abramsky and Coecke, Categorical Quantum Mechanics (2004); Lambek and Scott, Introduction to Higher-Order Categorical Logic (1986).