Experience Implementing a Performant Category-Theory Library in Coq

Jason Gross, Adam Chlipala, David I. Spivak · DOI

Abstract

We describe our experience implementing a broad category-theory library in Coq. Category theory and computational performance are not usually mentioned in the same breath, but we have needed substantial engineering effort to teach Coq to cope with large categorical constructions without slowing proof script processing unacceptably. In this paper, we share the lessons we have learned about how to represent very abstract mathematical objects and arguments in Coq and how future proof assistants might be designed to better support such reasoning. One particular encoding trick to which we draw attention allows category-theoretic arguments involving duality to be internalized in Coq's logic with definitional equality. Ours may be the largest Coq development to date that uses the relatively new Coq version developed by homotopy type theorists, and we reflect on which new features were especially helpful.

Introduction

Category theory is a highly abstract branch of mathematics that unifies concepts across disparate domains. While its structural nature makes it a prime candidate for formalization in proof assistants like Coq, naive implementations frequently encounter severe performance bottlenecks. These issues typically manifest as "universe inconsistencies" and massive term bloat.

Because Coq's type checker manipulates the underlying syntax trees of proofs, complex categorical constructions can generate goals containing hundreds of thousands of words. This causes interactive theorem proving to grind to a halt, with single tactical steps taking seconds or even minutes. This paper details the design of a library that bypasses these performance traps while remaining mathematically rigorous.

Methodology and Design Choices

Adopting Homotopy Type Theory (HoTT)

The library was built using a specialized fork of Coq developed for Homotopy Type Theory. This choice provided two critical features: advanced universe polymorphism and Higher Inductive Types (HITs). Universe polymorphism allows for a clean, reusable hierarchy of categories without the need for code duplication, while HITs provide a more efficient way to handle quotients than traditional methods.

"Inside" vs. "Outside" Definitions

A pivotal architectural decision involved how to represent categories. Instead of passing the types of objects and morphisms as explicit parameters to a Category (the "outside" approach), the authors bundled them as fields within the Category record (the "inside" approach). This encapsulation prevents the exponential blowup of parameters in complex constructions like functors and natural transformations.

Propositional Equality over Setoids

Many existing libraries rely on "setoids"—types bundled with custom equivalence relations—to handle equality. However, setoids are computationally expensive, requiring the user to carry around proofs that every function respects the custom relation. By utilizing standard propositional equality supplemented by HITs, the authors significantly reduced the computational overhead of the type checker.

Internalizing Duality via Definitional Equality

In category theory, many theorems have a "dual" (e.g., a theorem about products has a mirror image about disjoint unions). The authors introduced a novel encoding trick: by requiring redundant proofs for both left-to-right and right-to-left associativity in the definition of a category, a category and its dual become judgmentally equal. This allows Coq to solve dual theorems automatically via computation, sparing the user from writing mirror-image proofs manually.

Strict Abstraction Barriers

To prevent the type checker from choking on massive, flattened definitions, the authors factored complex structures into smaller, modular components. For example, the definition of limits, which initially contained 14 nested binders, was broken down to hide complexity from the type checker until absolutely necessary.

Key Results and Performance

The engineering choices described above resulted in substantial performance gains:

  • Record Bundling: Switching to the "inside" approach for category definitions yielded a 3x speedup in total compilation time.
  • Goal Size Reduction: By implementing stricter abstraction barriers in the definition of limits and universal morphisms, the word count of a single proof goal was reduced from 163,811 words to just 1,390 words. This led to a 10x speedup in the compilation of specific files (from 39 seconds to 3 seconds).
  • Library Scale: The final library consists of 578 definitions across 147 files, compiling in approximately 490 seconds. Benchmarking shows it matches or outperforms existing Coq and Agda libraries while supporting more advanced constructions such as Kan extensions, Yoneda, and adjunctions.

Discussion and Future Work

While the library is highly performant, its development highlighted limitations in Coq 8.4. The use of nested $\Sigma$ types was particularly slow due to the lack of primitive record projections. Furthermore, the absence of judgmental $\eta$ rules for records prevented certain advanced dualities (like functor categories) from being definitionally equal.

Future proof assistants could better support abstract reasoning by implementing:

  1. Primitive projections to handle large records efficiently.
  2. Better computation rules for pattern matching.
  3. Hash-consing or irrelevant fields to prevent large, computationally irrelevant proof terms from slowing down the type checker.

By treating performance as a first-class citizen in the design of mathematical libraries, it is possible to formalize even the most abstract concepts without sacrificing the interactivity of the proof assistant.

Summary updated

BibTeX
@inproceedings{gross2014experience,
  author = {Jason Gross and Adam Chlipala and David I. Spivak},
  title = {{Experience Implementing a Performant Category-Theory Library in Coq}},
  booktitle = {ITP},
  year = {2014},
  doi = {10.1007/978-3-319-08970-6\_18},
}
Figure