Abstract 1 Introduction 2 The formalization 3 Challenges 4 Related work 5 Future work References

Formalizing Colimits in 𝒞at

Mario Carneiro ORCID Chalmers University of Technology, Gothenburg, Sweden Emily Riehl111Corresponding author ORCID Department of Mathematics, Johns Hopkins University, Baltimore, MD, USA
Abstract

Certain results involving “higher structures” are not currently accessible to computer formalization because the prerequisite -category theory has not been formalized. To support future work on formalizing -category theory in Lean’s mathematics library, we formalize some fundamental constructions involving the 1-category of categories. Specifically, we construct the left adjoint to the nerve embedding of categories into simplicial sets, defining the homotopy category functor. We prove further that this adjunction is reflective, which allows us to conclude that 𝒞at has colimits. To our knowledge this is the first formalized proof that the nerve functor is a fully faithful right adjoint and that the category of categories is cocomplete.

Keywords and phrases:
category theory, infinity-category theory, nerve, simplicial set, colimit
Funding:
Emily Riehl: NSF DMS-2204304, AFOSR FA9550-21-1-0009, ARO W911NF-20-1-0082.
Copyright and License:
[Uncaptioned image] © Mario Carneiro and Emily Riehl; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Logic and verification
Acknowledgements:
This project was first instantiated at the 2024 Prospects of Formal Mathematics Trimester Program at the Hausdorff Institute for Mathematics in Bonn. The authors are grateful to the organizers of that program for facilitating many new collaborations and to the HIM for its hospitality. They also wish to thank Joël Riou for the excellent suggestions he made during the Mathlib PR review process. The final journal version contains myriad expository improvements suggested by the reviewers.
Editors:
Yannick Forster and Chantal Keller

1 Introduction

One of the myriad ways in which “category theory eats itself” is that the categorically-defined notions of limits and colimits can be interpreted in the category 𝒞at itself, whose objects are categories333An important point that is often elided on paper but is necessary to treat precisely in formalization is that the category of categories Cat.{v,u} is parametrized by two implicit universe levels, one u for the type of objects and one v for the hom-types. and functors between them. Any suitably-small diagram of categories and functors has a limit in 𝒞at which is constructed by forming the limit of the corresponding diagrams of objects and hom-sets.444Specifically, the category Cat.{u,u} of small categories at any universe level u, has all limits indexed by categories J in Cat.{u,u}. The definition of limit is reviewed in §3.5; colimits are dual, formed by reversing the arrows. For example, the product of two categories C and D is the category whose objects are defined as the product obC×obD of the types of objects of C and D. Given objects c,c:obC and d,d:obD, the hom-type from (c,d) to (c,d) in the product category is the product HomC(c,c)×HomD(d,d) of the hom-types in C and D. The generalization of this construction to arbitrary diagram shapes is formalized in Lean’s mathlib as Cat.instHasLimits by Kim Morrison [27].

It is also the case that any suitably-small diagram in 𝒞at has a colimit, but the construction is considerably more complicated. To illustrate, consider the terminal category Fin(1) (with one object and only its identity arrow) and the walking arrow Fin(2) (with two objects 0 and 1 and a single non-identity morphism :01). The coequalizer of the parallel pair of functors 0,1:Fin(1)Fin(2) that pick out these two objects is the category B with a single object but with a morphism for each natural number, with composition defined by addition and the morphism 0 serving as the identity. The quotient functor q:Fin(2)B sends the morphism to the generating non-identity morphism 1.

(1)

Given the much greater complexity involved in the construction of colimits in 𝒞at, it is perhaps unsurprising that none of the libraries of formal mathematics that currently exist in Agda [7, 15, 19, 34, 35], HOL Light [14], Isabelle [24, 25, 26, 29], Lean [23], or Rocq [3, 12, 36] contain a proof that 𝒞at is cocomplete. In particular, we discovered that this result was missing from Lean’s mathlib while attempting to formalize a theorem that is relevant to -category theory, the extension of ordinary category theory to settings where there are “higher homotopies” between maps.555As noted recently by the authors of [18], “myriad recent results from algebraic K-theory [5], derived and spectral algebraic geometry [21, 20], the Langlands program [10], and symplectic geometry [28] are inaccessible to formalization” because mathlib contains relatively few results from -category theory. When this project commenced in Summer 2024, mathlib contained an important 1-categorical pre-requisite to -category theory, namely the construction of the nerve of a 1-category, formalized by Joël Riou.

The nerve of a category C, is a simplicial set, a family of types (nerveCn)n: equipped with various maps between them. More precisely, a simplicial set is a contravariant functor indexed by the SimplexCategory Δ; objects are natural numbers n: commonly denoted by [n] and morphisms [m][n] are defined in SimplexCategory.Hom to be order-preserving functions Fin(m+1)Fin(n+1). The category SSet of simplicial sets is the functor category 𝒮etΔop. Explicitly, nerveCn is the type of functors Fin(n+1)C, where Fin(n+1) is the ordinal category with n+1 objects depicted below:

/-- ComposableArrows C n is the type of functors Fin (n + 1) C‘. -/
abbrev ComposableArrows (n : ℕ) := Fin (n + 1) C
/-- The nerve of a category -/
def nerve (C : Type) [Category C] : SSet where
obj Δ := ComposableArrows C (Δ.unop.len)
map f x := x.whiskerLeft (SimplexCategory.toCat.map f.unop)

Up to equivalence, nerveC0 is the type of objects of C, nerveC1 is the type of arrows of C, nerveC2 is the type of composable pairs of arrows, and so on. In the presence of the “higher homotopies” alluded to above, this data is used to index “homotopy coherent diagrams” of shape C, incarnating the 1-category C as an -category.

The nerve construction defines a functor nerve:𝒞at𝒮etΔop valued in the category of simplicial sets. Our objective was to define its left adjoint ho:𝒮etΔop𝒞at, a functor which takes a simplicial set to its homotopy category. This functor is used to collapse an -category to its quotient 1-category and will also be used to define the 2-category of -categories described in §5. As noted for instance in [32, 6.5.iv], this adjunction exists for formal reasons as a special case of a result which has already been formalized in mathlib. Bhavik Mehta and Joël Riou formalized the construction of a general family of “Yoneda adjunctions”:666We’ve simplified the statement here for readability. The only hypothesis that is actually required is that the left Kan extension of the functor A along the Yoneda embedding H:𝒜𝒮et𝒜op exists. In practice, one proves that this left Kan extension exists by showing that 𝒜 is small and is cocomplete.

Theorem 2 (Presheaf.yonedaAdjunction).

Suppose A:𝒜 is any functor, where 𝒜 is small and is cocomplete. Then the left Kan extension of A along the Yoneda embedding H:𝒜𝒮et𝒜op is left adjoint to the restricted Yoneda functor:

The nerve adjunction is an example of a Yoneda adjunction defined relative to the functor SimplexCategory.toCat:Δ𝒞at which sends n: to the ordinal category Fin(n+1). However, we could not apply this result because mathlib did not contain a proof that 𝒞at has all colimits, only a proof that 𝒞at has limits, which are far easier to construct. Consulting the literature again [32, 4.5.16], we were reminded that colimits in 𝒞at can be constructed as a corollary of the adjunction we were hoping to define: since the nerve functor is fully faithful, the adjunction exhibits 𝒞at as a reflective subcategory of a cocomplete category. Thus our new formalization objective was to give a direct proof of the following pair of results:

Theorem 3 (nerveAdjunction).

The nerve functor admits a left adjoint defined by the functor that sends a simplicial set to its homotopy category:

Theorem 4 (nerveFunctor.fullyfaithful).

The nerve functor is full and faithful.

A functor is reflective if it is a fully faithful right adjoint.

/-- A functor is *reflective*, or *a reflective inclusion*, if it is fully faithful and right adjoint. -/
class Reflective (R : D C) extends R.Full, R.Faithful where
/-- a choice of a left adjoint to R -/
L : C D
/-- R is a right adjoint -/
adj : L R

As formalized by Kim Morrison, Jack McKoen, and Bhavik Mehta, any reflective subcategory of a cocomplete category has colimits.

/-- If C has colimits then any reflective subcategory has colimits. -/
theorem hasColimits_of_reflective (R : D C) [Reflective R]
[HasColimits C] : HasColimits D := ...

Thus, as an immediate corollary of Theorems 3 and 4:777Most of the Lean statements here have been simplified to avoid asides about universes, but in this theorem statement it is important that we only assert the existence of u-small colimits in the category of u-small categories, which is indicated by the use of universe u in both objects and homs.

/-- The category of small categories has all small colimits as a reflective
subcategory of the category of simplicial sets, which has all small colimits. -/
Corollary 5 (Cat.instHasColimits).

𝒞at is cocomplete.

Proof.

The presheaf category 𝒮etΔop is cocomplete, with colimits defined objectwise in 𝒮et. On account of the adjunction with a fully faithful right adjoint the reflective subcategory 𝒞at inherits these colimits, defined by taking the nerves of the categories in the original diagram, forming the colimit in 𝒮etΔop, and then applying the homotopy category functor to the colimit cone.

Importantly for the aim of contributing to a general-purpose library, the construction given in the proof of Corollary 5 results in a good description of colimits in 𝒞at. Applied to the example (1), Corollary 5 tells us to first form the coequalizer in simplicial sets

and then take the homotopy category. As described in Definition 12 below, the homotopy category of S1 is the free category defined by the underlying reflexive quiver – which has a single object and a single non-identity endo-arrow – modulo relations witnessed by non-degenerate 2-simplices. As the simplicial set S1 has no non-degenerate 2-simplices, ho(S1)B is the free category on a single non-identity endo-arrow, which is the category B described above. The construction for other diagram shapes is similar.

Other methods for proving that 𝒞at is cocomplete are discussed in §4, on related work. These methods typically invoke the theorem that a colimit of a diagram of any shape can be expressed as a coequalizer between two coproducts [22, V.2.2], so that it suffices to give explicit constructions of coproducts and coequalizers in 𝒞at. This works, but it results in more convoluted expressions for concrete colimits; for our running example, it would calculate the colimit B as the coequalizer of a pair of functors between the coproducts

which is more complicated than it needs to be.

In §2, we describe our formalizations of Theorems 3 and 4. After a PR process that took five and a half months, this entire formalization is now a part of mathlib. In §3, we describe some challenges we encountered along the way. In §4, we discuss alternate theoretical approaches to the construction of colimits in 𝒞at and related formalization projects. In §5, we discuss plans for future work that will expand and apply these results.

2 The formalization

The formalized proof of our first main result, Theorem 3, which defines the left adjoint to the nerve functor, takes one line:

Unpacking, this tells us that up to a natural isomorphism Nerve.cosk2Iso between right adjoints abbreviated as “υ” in the diagram below, the nerve adjunction is defined as a composite of two other adjunctions:

In particular, the homotopy category functor is defined as the composite of the two left adjoints:

/-- The functor that takes a simplicial set to its homotopy category by passing through the 2-truncation. -/
def hoFunctor : SSet Cat := SSet.truncation 2 \ldocTwo[SSet.]{Truncated.hoFunctor}{}

One of these adjunctions, SSet.coskAdj 2, had already been formalized, so our task was to define the other adjunction nerve2Adj and construct the isomorphism Nerve.cosk2Iso. We first describe the construction of the natural isomorphism Nerve.cosk2Iso, which exhibits an important property of the nerve functor called “2-coskeletality.” Then, we describe our formalization of the adjunction nerve2Adj and our proof that its right adjoint is fully faithful, which implies that the nerve functor is fully faithful as well.

2.1 The 2-coskeletality of the nerve

The fact that the “homotopy category nerve” adjunction can be constructed as a composite of two other adjunctions follows from the fact that the nerve functor factors (up to isomorphism) as a composite of two right adjoints, through a category that we now introduce. Recall that the category of simplicial sets is defined as a presheaf category 𝒮etΔop. Given n:, we define the n-truncated simplex category Δn as the full subcategory ΔnΔ spanned by the objects [0],,[n]. The category of n-truncated simplicial sets, 𝒮etΔnop, is then the category of presheaves over Δn. The restriction functor, known as n-truncation, has fully faithful left and right adjoints defined by left and right Kan extension:888The right and left Kan extensions exist because Δ is small and 𝒮et is complete and cocomplete.

/-- The adjunction between the n-skeleton and n-truncation. -/
def skAdj : Truncated.sk (C := C) n truncation n := lanAdjunction _ _
/-- The adjunction between n-truncation and the n-coskeleton. -/
def coskAdj : truncation (C := C) n Truncated.cosk n := ranAdjunction _ _

For any functor valued in simplicial sets, we can obtain its n-truncated version by postcomposing with the functor trn:𝒮etΔop𝒮etΔnop. In particular, the 2-truncated nerve nerve2 is defined to be the composite functor:

/-- The essential data of the nerve functor is contained in the 2-truncation, which is recorded by the composite functor nerveFunctor₂’. -/

The canonical natural transformation υ:nervecosk2nerve2 is defined by left whiskering the unit η:idcosk2tr2 of the adjunction tr2cosk2 with the nerve functor:

The natural transformation υ is the underlying map of the isomorphism Nerve.cosk2Iso.

/-- The natural isomorphism between nerveFunctor and nerveFunctor Truncated.cosk 2’ whose components nerve C (Truncated.cosk 2).obj (nerveFunctor₂.obj C)’ show that nerves of categories are 2-coskeletal. -/
(fun _ (coskAdj 2).unit.naturality _)
/-- The canonical isomorphism X (cosk n).obj X defined when X is coskeletal and the n-coskeleton functor exists. -/
def isoCoskOfIsCoskeletal [X.IsCoskeletal n] : X (cosk n).obj X :=
asIso ((coskAdj n).unit.app X)

A simplicial set X is 2-coskeletal just when the unit component ηX:Xcosk2tr2X is an isomorphism. Thus, our first main subproblem, to prove that the natural transformation υ is a natural isomorphism, is achieved by the following proposition:

Proposition 6 (isCoskeletal).

Nerves of categories are 2-coskeletal.

instance (C : Type u) [Category C] : (nerve C).IsCoskeletal 2 := ...

We unpack the 2-coskeletality condition and describe the proof of Proposition 6 in §3.5.

2.2 The 2-truncated nerve adjunction

On account of the natural isomorphism υ:nervecosk2nerve2 provided by Proposition 6, it now suffices to construct a left adjoint ho2:𝒮etΔ2op𝒞at to the functor nerve2. To do so, we unpack the data contained in a (2-truncated) simplicial set.

On paper there is a well-known presentation of the morphisms in the SimplexCategory Δ by generators and relations [11, §I.1] that is in the process of being formalized by Robin Carlier. At present, mathlib contains the generating face maps δi:[n][n+1], corresponding to monomorphisms whose image omits iFin(n+2), as well as the generating degeneracy maps σi:[n+1][n], corresponding to epimorphisms for which iFin(n+1) has two elements in its preimage; mathlib also knows the composition relations these maps satisfy, but does not contain a proof that Δ is equivalent to the category SimplexCategoryGenRel with this presentation.

A 2-truncated simplicial set X is given by the data of three sets X0,X1,X2 and maps between them indexed by the morphisms between the three objects [0],[1],[2]:Δ2Δ. The further 1-truncation of X is given by the data below-left defined by the contravariant actions of the morphisms in Δ1 displayed below-right:

(7)

The composition relations σ0δ0=id=σ0δ1 in Δ induce dual relations δ0σ0=id=δ1σ0. As explained in Example 11, this is closely related to categorical data known as a reflexive quiver.

A quiver is defined in mathlib as a type equipped with a dependent type of arrows:

class Quiver (V : Type) where
/-- The type of edges/arrows/morphisms between a given source and target. -/
Hom : V V Type

Kim Morrison formalized the “free forgetful” adjunction between the category 𝒬uiv, of quivers and structure-preserving morphisms, called prefunctors, between them, and 𝒞at.

Proposition 8 (Quiv.adj).

The free category on a quiver is left adjoint to the forgetful functor:

A similar result also holds for reflexive quivers, which extend quivers with specified “identity” endo-arrows for each term in the base type:

/-- A reflexive quiver extends a quiver with a specified arrow id X : X X for each X in its type of objects. We denote these arrows by id since categories can be understood as an extension of reflexive quivers. -/
class ReflQuiver (obj : Type) extends Quiver obj where
/-- The identity morphism on an object. -/
id : X : obj, Hom X X

We introduced reflexive quivers and reflexive prefunctors and defined the evident forgetful functors Ur:𝒞atr𝒬uiv from the category of categories to the category of reflexive quivers and Uq:r𝒬uiv𝒬uiv from the category of reflexive quivers to the category of quivers. Definitionally, we have U=UqUr:𝒞at𝒬uiv.

Definition 9 (Cat.FreeRefl).

The free category on a reflexive quiver Q is the quotient of the free category FUqQ on the underlying quiver of Q by the hom relation that identifies the specified reflexivity arrows in Q with the identity arrows in the category FUqQ.

/-- A reflexive quiver generates a free category, defined as as quotient of the free category on its underlying quiver (called the "path category") by the hom relation that uses the specified reflexivity arrows as the identity arrows. -/
def FreeRefl (V) [ReflQuiver V] :=
Quotient (C := Cat.free.obj (Quiv.of V)) (FreeReflRel (V := V))
instance (V) [ReflQuiver V] : Category (FreeRefl V) :=
inferInstanceAs (Category (Quotient _))

This defines a functor Fr:r𝒬uiv𝒞at and a natural transformation q:FUqFr whose components qQ:FUqQFrQ are universal among functors with domain FUqQ that respect the hom relation, sending the specified reflexivity arrows of Q to identities.

We then formalized the following proof:

Proposition 10 (ReflQuiv.adj).

The free category on a reflexive quiver is left adjoint to the forgetful functor:

Proof.

We construct the adjunction by defining the unit and counit and verifying the triangle identities. The component of the counit ϵCr at a category C is defined by appealing to the universal property of the quotient functor qUrC in the following diagram:

Modulo the definitional equality UqUr=U, the counit component ϵC of the adjunction FU sends the specified reflexivity arrows of UrC, the identities of C, to identities. Thus this functor factors through the quotient functor qUrC to define the counit component ϵCr. Naturality of ϵ follows from the uniqueness of the universal property of the quotient functor.

The construction of the unit components at a reflexive quiver Q is more subtle. In the category 𝒬uiv of quivers and prefunctors we have the following composite:

defining a prefunctor between the underlying quivers of the reflexive quivers Q and UrFrQ. In fact, this map preserves the specified reflexivity arrows and hence lifts to a reflexive prefunctor ηQr:QUrFrQ with defining equality:

Naturality in this case actually holds by reflexivity.

The proofs of the triangle equalities require diagram chases. To verify commutativity of

we use the fact that the functor Uq:r𝒬uiv𝒬uiv is faithful and the fact that Uq of this composite is equal to the corresponding triangle identity composite for the adjunction FU. To verify commutativity of

we use the universal property of the quotient functor to instead verify commutativity of

which follows from naturality of q, the defining equalities for ϵr and ηr, naturality of ϵ, and the corresponding triangle equality for FU. These steps occupy 7 lines of a 23 line proof due to the challenges described in §3.1.

Example 11 (instReflQuiverOneTruncation2).

Returning to (7), a 2-truncated simplicial set X has an underlying reflexive quiver U12X whose type of objects is X0 and whose type of arrows from x:X0 to y:X0 is the type of 1-simplices f:X1 so that δ1f=x and δ0f=y.999To explain the orientation, note that δ1:[0][1] picks out the source object of Fin(2) while δ0:[0][1] picks out the target object.

We can now define the 2-truncated homotopy category functor.

Definition 12 (Truncated.HomotopyCategory).

The homotopy category ho2X is the quotient of the free category FrU12X on the underlying reflexive quiver of a 2-truncated simplicial set X by a hom relation generated by the 2-simplices: for every 2-simplex σ:X2, we identify the face δ1σ with the composite δ0σδ2σ in the category FrU12X.

/-- The 2-simplices in a 2-truncated simplicial set V generate a hom relation on the free category on the underlying reflexive quiver of V’. -/
inductive HoRel2 {V : SSet.Truncated 2} :
(X Y : Cat.FreeRefl (OneTruncation2 V)) (f g : X Y) Prop
| mk : V _⦋2⦌₂) : HoRel2 _ _
(Quot.mk _ (Quiver.Hom.toPath (ev022 φ)))
(Quot.mk _ ((Quiver.Hom.toPath (ev012 φ)).comp
/-- The type underlying the homotopy category of a 2-truncated simplicial set V. -/
Quotient (HoRel2 (V := V))
instance (V : SSet.Truncated 2) : Category.{u} (V.HomotopyCategory) :=
inferInstanceAs (Category (CategoryTheory.Quotient ..))

This defines a functor ho2:𝒮etΔ2op𝒞at and a natural transformation q:FrU12ho2 whose components qX:FrU12Xho2X are universal among functors with domain FrU12X that respect the hom relation, sending the image of the composite δ0σδ2σ of the arrows along the “spine” of a 2-simplex σ:X2 to the image of the “diagonal” arrow δ1σ.

The proof that ho2 is left adjoint to nerve2 proceeds similarly to the proof of Proposition 10, with one step made considerably more difficult. As before, the counit of ho2nerve2 is defined using the universal property of the quotient functor q while the unit is defined by lifting a map from the category of reflexive quivers to the category of 2-truncated simplicial sets. This lifting is enabled by the following proposition:

Proposition 13 (toNerve2.mk, toNerve2.ext).

Let X:𝒮etΔ2op and C:𝒞at.

  1. (i)

    Consider a reflexive prefunctor F:U12XU12nerve2CUrC from the underlying reflexive quiver of X to the underlying reflexive quiver of C. If for each 2-simplex σ:X2, F(δ1σ) equals the composite F(δ0σ)F(δ2σ) in C, then F lifts to a map of 2-truncated simplicial sets.

  2. (ii)

    Any parallel pair of maps of 2-truncated simplicial sets F,G:Xnerve2C are equal if their underlying reflexive prefunctors agree.

We discuss the proof of these results in §3.4.

Proposition 14 (nerve2Adj).

The 2-truncated homotopy category functor is left adjoint to the 2-truncated nerve:

Proof.

We construct the adjunction by defining the unit and counit and verifying the triangle identities. The component of the counit ϵC at a category C is defined by appealing to the universal property of the quotient functor qnerve2C in the following diagram:

Modulo a natural isomorphism ϕ:U12nerve2Ur between the two constructions of the reflexive quiver underlying a category, the counit component ϵCr of the adjunction FrUr respects the defining hom relation. Naturality is again established using the uniqueness of the universal property of the quotient functor.

The unit components are constructed by applying Proposition 13(i) to the composition of reflexive prefunctors defined below, which thus satisfies the defining equation:

By Proposition 13(ii), naturality can be checked at the level of reflexive prefunctors, at which point it follows from naturality of ηr, q, and ϕ.

The proofs of the triangle equalities require diagram chases. To verify commutativity of

we use Proposition 13(ii) to instead verify commutativity of the underlying reflexive prefunctors. This diagram chase follows from the defining equalities for η and ϵ, naturality of ϕ and ηr, and the corresponding triangle equality for FrUr, modulo cancelling ϕ with its inverse. These steps occupy 8 lines of a 25 line proof due to the challenges described in §3.1.

To verify commutativity of

we use the universal property of the quotient functor to instead verify commutativity of

which follows from naturality of q, the defining equalities for η and ϵ, naturality of ϵr, and the corresponding triangle equality for FrUr, modulo cancelling ϕ with its inverse. These steps occupy 8 lines of a 24 line proof due to the challenges described in §3.1.

As discussed, Propositions 14 and 6 imply Theorem 3, proving our first main theorem. To show that the adjunction honerve is reflective and conclude that 𝒞at has colimits, we must show that the nerve functor is fully faithful. As the nerve is isomorphic to the composite of the functors cosk2 and nerve2 and cosk2 is fully faithful (since Δ2Δ is the inclusion of a full subcategory), it suffices to show that the 2-truncated nerve is fully faithful.

Lemma 15 (nerveFunctor2.faithful).

The 2-truncated nerve functor is faithful.

Proof.

We have a natural isomorphism ϕ:U12nerve2Ur and the functor Ur is faithful. Thus, as the first functor of a faithful composite, nerve2 is faithful.

With considerably more effort, discussed in §3.3, we can also show:

Lemma 16 (nerveFunctor2.full).

The 2-truncated nerve functor is full.

These lemmas combine to prove Theorem 4, our second main theorem, and these results together give Corollary 5 that 𝒞at has colimits.

3 Challenges

Although the mathematics of Theorems 3 and 4 is well established in the literature, and we came to the project suitably armed with category theoretic and Lean formalization expertise, the project ended up being quite challenging, for both expected and unexpected reasons. Below, we will discuss some of the aspects of the formalization that posed the most difficulties, and what we did to resolve or work around the issues.

3.1 Bundling categories

In mathlib, many notions come in two flavors of presentation, referred to as the “bundled” and “(partially) unbundled” presentations.101010There is also a “fully unbundled” presentation, but mathlib does not use it and we will not discuss it. In the unbundled presentation, a category is given as a type variable C together with a typeclass instance argument CategoryC which provides the rest of the data for a category whose type of objects is C. This allows for a form of formal synecdoche, where one need only explicitly mention the type of objects and the rest of the structure comes along for the ride. In bundled style a category is an element C:Cat, with suitable projections obC:𝖳𝗒𝗉𝖾 and Hom:obCobC𝖳𝗒𝗉𝖾.

Generally, mathlib prefers the unbundled form, but the bundled form is necessary if we need to consider the category of categories, that is, CategoryCat, and our main theorem is about Cat so we cannot avoid it completely. A similar situation occurs for functors, which appear unbundled as CD and also bundled as HomCat(C,D).111111Note the former notion of functor is strictly speaking more general than the latter, in which the universe levels of the domain category must match those of the codomain. There is functor composition FG and morphism composition FCatG, and so on. Not only are many notions duplicated, but by applying lemmas coming from each context one can end up with a mixture of both notions in goals, and simplification would frequently get stuck in this situation. We eventually mitigated this issue by unbundling as early as possible, stating lemmas only in the unbundled form, and packaging them up only for the higher order statements about operations on Cat.

3.2 Equality of functors

Our main theorem fundamentally deals with the 1-categorical (rather than 2-categorical) structure of 𝒞at. The very statement that 𝒞at has small colimits involves equality of functors, as does the definition of the nerve – a simplicial set rather than a simplicially-indexed category-valued pseudofunctor.121212The nerve of C could be regarded as a pseudofunctor nerveC:Δop𝒞at valued in 𝒞at, sending the object [n]:Δ to the category of functors Fin(n+1)C, but this extra coherence data is unnecessary. The type-valued mapping nerveC:Δop𝒮et is strictly functorial, and indeed (due to the care taken in formalizing the definition of ComposableArrows) the map_id and map_comp fields of nerve are filled by rfl. There are advantages to working strictly (1-categorically) rather than weakly (2-categorically or bicategorically) when possible. In future work, described in §5, we plan to use our homotopy category functor to convert from simplicial to categorical enrichments. As we have formalized this as a strict functor between 1-categories we can tap into existing mathlib developments of change of base in enriched category theory.

Lean’s mathlib provides an extensionality theorem for functors, contributed by Reid Barton, but note the warning:

/-- Proving equality between functors. This isnt an extensionality lemma, because usually you dont really want to do this. -/
theorem ext {F G : C D} (h_obj : X, F.obj X = G.obj X) (h_map : X Y f,
F.map f = eqToHom (h_obj X) G.map f eqToHom (h_obj Y).symm) : F = G

So we knew in advance there would be trouble coming and needed to structure our proofs in specific ways to take advantage of eqToHom.

At the core of this warning is the following issue: When one has an equality of objects, or an equality of functors (which involves an equality of objects – the h_obj assumption above), this induces an equality of types, namely Hom(A,B)=Hom(A,B) given B=B. In dependent type theory, if one has f:Hom(A,B) and an equality h:B=B, the plain expression f:Hom(A,B) does not typecheck (unless B and B are definitionally equal). However, it is possible to use h to cast f to the correct type, producing a term casthf:Hom(A,B). This is also known as “rewriting” and it is a common technique in proofs (using the rw tactic), but it is hazardous when applied to “data” such as f, because casthf is not the same term as f and so mismatches can arise between expressions such as casth(fg) and (casthf)g. It is possible to prove these are equal, but it generally requires a rather delicate “induction on equality” to prove, with a manually written induction motive. Complications with transport of this nature are often referred to as “DTT hell.”

The mathlib statement of Functor.ext is employing a technique to avoid this issue, which is to make use of the category structure to help alleviate the pains associated with using the cast function directly, by first converting h:B=B into eqToHomh:Hom(B,B), so that (eqToHomh)f:Hom(A,B). This presents the casting of f as a composition, which means that many more lemmas from category theory apply (such as associativity of composition), and eqToHom itself has many lemmas for how it factors over various operations.

For the most part, this technique did its job. Although we obtained many goals that involved these eqToHom morphisms, after formalizing appropriate lemmas, the simplifier was able to push them out of the way. For instance, we added a Quiver.homOfEq to perform an analogous casting for (reflexive) quivers (which lack composition) and proved various lemmas by induction on equality. This is the kind of complication that essentially never appears in informal category theory, due to its more extensional handling of morphism typing.

3.3 Proving functoriality

Another example of DTT hell arises in the proof of nerveFunctor2.full, that the 2-truncated nerve functor nerve2:𝒞at𝒮etΔ2op is full. Given a pair of categories C and D and a map F:nerve2Cnerve2D between their 2-truncated nerves, our task is to lift this to a functor between C and D. The underlying reflexive prefunctor is defined by the 1-truncation of F, but it remains to show that this reflexive prefunctor is functorial, preserving composition of morphisms in C in addition to identities.

The idea of the proof is to note that the composable morphisms kh define a 2-simplex σk,h:(nerve2C)2. The 2-simplex F(σk,h):(nerve2D)2 defines a functor Fin(3)D, which gives rise to a commutative triangle involving three arrows in D. Using the naturality of F with respect to the three morphisms δi:[1][2], this may be converted into the desired equality F(kh)=FkFh. But these naturality equalities live in the type (nerve2D)1 of functors Fin(2)D, which give rise to heterogeneous equalities between the corresponding arrows in D. We were forced to first prove and then string together a sequence of heterogeneous equalities before finally concluding the desired equality between arrows belonging to the same hom-type. It then remained to show that the 2-truncated nerve carries this lifted functor to the original map F, which is proven using Proposition 13(ii).

3.4 Proving naturality

In Proposition 13(i), our aim is to define a map of 2-truncated simplicial sets F:Xnerve2C from a map between the underlying reflexive quivers. Such a map is a natural transformation with three components F0, F1, and F2 corresponding to the three objects [0],[1],[2]:Δ2, which can be constructed straightforwardly from the hypotheses. But complications arise in the proof of naturality, which amounts to establishing commutative squares of the form

(17)

for each morphism α:[m][n] in Δ2 (there are 31 such morphisms). One annoyance, discussed at the start of §2.2, is the fact that mathlib does not yet know that Δ2 is generated by the three degeneracy maps σi and the five face maps δi. Our initial formalization instead explicitly treated the nine cases presented by the choice of domain and codomain objects, with the cases involving a morphism with domain [2] reducing to the cases involving domain [0] or [1] by the 2-coskeletality of the nerve. Once we fixed domain and codomain objects there were often further case splits involving the specific nature of the morphism α:[m][n].

When we submitted this as a pull request to mathlib, the reviewer Joël Riou suggested we think of this result as the task of showing that the MorphismProperty of satisfying the naturality condition (17) holds for every morphism α in Δ2. An easier task than showing that the simplex category can be presented by generators and relations is to show that the naturalityProperty of its arrows follows from the cases corresponding to faces and degeneracy maps, as Riou formalized for us in Truncated.morphismProperty_eq_top. This reduces the problem to the eight cases corresponding to generating morphisms α:[m][n] in Δ2, each of which still involves establishing an equality between functors Fin(m+1)C.

The extensionality result of Proposition 13(ii) is simpler but still less innocuous than it appears. Two natural transformations F and G are equal just when their three components are equal, and as these are functions between types it suffices to show they define the same mapping on terms. However, this involves proving equalities in the types (nerve2C)0, (nerve2C)1, and (nerve2C)2, which are types of functors, and thus these equalities involved a fair amount of pain. In the final case F2=G2, naturality again provided us with equalities in the type (nerve2C)1 of functors Fin(2)C which gave rise to heterogeneous equalities between the mappings on arrows of the functors Fin(3)C we were tasked with identifying.

3.5 Proving nerves are 2-coskeletal

By a folklore result involving Reedy category theory [31], a simplicial set X is 2-coskeletal, meaning that the adjunction unit component ηX:Xcosk2tr2X is an isomorphism, just when every k-simplex boundary in X can be filled to a simplex, for all k>2. This property would be easy to check in the case where X is the nerve of a category, but unfortunately this characterization of 2-coskeletal simplicial sets has not been formalized. Thus, we directly unpack the invertible unit component definition into a condition that we can actually check.

Recall the functor cosk2 is defined by right Kan extension along the inclusion Δ2opΔop. Thus, the condition of a simplicial set X being 2-coskeletal asserts that the identity natural transformation defines a right Kan extension:

(18)
/-- A simplicial object X is n’-coskeletal when it is the right Kan extension of its restriction along ‘(Truncated.inclusion n).op via the identity natural transformation. -/
class IsCoskeletal (X : SimplicialObject C) (n : ℕ) : Prop where
isRightKanExt : IsRightKanExtension X (𝟙 ((Truncated.inclusion n).op X))
X.IsCoskeletal n IsIso ((coskAdj n).unit.app X) := by
exact isRightKanExtension_iff_isIso ((coskAdj n).unit.app X)
((coskAdj n).counit.app _) (𝟙 _) ((coskAdj n).left_triangle_components X)

As the category 𝒮et is complete, any such right Kan extension (18) is pointwise, meaning that X is 2-coskeletal if and only if for each n:, a canonical cone – with summit Xn over a diagram with objects Xj indexed by arrows [j][n] in Δ with j2 – is a limit cone, the definition of which we now recall:

/-- A cone t on F is a limit cone if each cone on F admits a unique cone morphism to t’. -/
structure IsLimit (t : Cone F) where
/-- There is a morphism from any cone point to t.pt -/
lift : s : Cone F, s.pt t.pt
/-- The map makes the triangle with the two natural transformations commute -/
fac : (s : Cone F) (j : J), lift s t.π.app j = s.π.app j
/-- It is the unique such map to do this -/
uniq : (s : Cone F) (m : s.pt t.pt)
(_ : j : J, m t.π.app j = s.π.app j), m = lift s

Thus, for the simplicial set nerveC, we must:

  1. (i)

    Construct lift, a function valued in functors Fin(n+1)C.

  2. (ii)

    Prove fac, an equality between functions valued in functors Fin(j+1)C for j=0,1,2.

  3. (iii)

    Prove uniq, an equality between functions valued in functors Fin(n+1)C.

We submitted a proof along these lines in our initial mathlib pull request, involving repeated painful appeals to Functor.ext. During the PR review process, Joël Riou suggested a useful abstraction boundary, using the following characterization of simplicial sets defined by nerves of categories.

Proposition 19 (Nerve.strictSegal).

For a simplicial set X the following are equivalent:

  1. (i)

    X is isomorphic to the nerve of a category.131313In fact, when this condition holds, X is isomorphic to the nerve of the homotopy category ho(X).

  2. (ii)

    X satisfies the StrictSegal condition: for all n:, the canonical map that carries an n-simplex of X to the path of edges defined by its spine is an equivalence.

As Proposition 19 explains, the only examples of strict Segal simplicial sets are nerves of categories. But it was cleaner to formalize the proof that a strict Segal simplicial set X is 2-coskeletal because the functions involved in lift, fac, and uniq are now valued in an abstract type Xn which is equivalent to the type of paths of edges of length n in X. Thus, we formalized:

/-- A strict Segal simplicial set is 2-coskeletal. -/
lift s x := ...
fac s j := ...
uniq s m hm := ...

together with a proof Nerve.strictSegal of the implication (i)(ii) in Proposition 19.

4 Related work

There are myriad libraries of formalized mathematics that contain a substantial amount of category theory [3, 7, 15, 19, 23, 24, 25, 26, 34, 35, 36]. Several papers on the subject [12, 30] recall Harrison’s assessment, based on the early efforts of [16], that “category theory [is] notoriously hard to formalize in any kind of system” [13]. Others describe additional complexities involved in developing a category theory library that is compatible with intensional identity types or homotopy type theory [2, 15].

A common feature of category theory libraries is a broad array of sophisticated categorical definitions, constructions, and theorems. To give a simplified illustration related to the theory of colimits, one will commonly find the general definition of a colimit, specific examples of colimits such as coproducts and coequalizers, and a proof that a colimit of any diagram shape can be built out of coequalizers and suitably-sized coproducts. What is less common is to find formalizations of applications of this general theory to specific categories, e.g., a proof that a specific category has coproducts and coequalizers and thus has all suitably-sized colimits – with a common exception being the category of types and functions.141414The 1Lab also contains a formalization of limits and colimits in type-valued presheaf categories: 1lab.dev/Cat.Instances.Presheaf.Colimits.html

More extensive examples of applications of category theory to specific categories can be found in the UniMath library in Rocq [36], with the categorical content originating from [2], as well as Lean’s mathlib [23], whose category theory formalizations grew out of an experimental library developed by Kim Morrison [27]. Both libraries contain the construction of limits in 𝒞at. More explicitly, UniMath contains a construction of equalizers between so-called “strict categories” (those whose objects and morphisms form sets, rather than higher types).151515See: UniMath/CategoryTheory/Limits/Examples/CategoryOfSetcategoriesLimits.v By contrast, mathlib directly formalizes the construction of the limit of a diagram of any shape as the category whose objects and hom-sets are defined by limits of the induced diagrams.161616See: Mathlib/CategoryTheory/Category/Cat/Limit.html We do not know of any other formalizations of the nerve functor, the nerve adjunction, or colimits in 𝒞at.

Alternate pen-and-paper approaches to the construction of colimits in 𝒞at can be given. Since coproducts in 𝒞at are elementary to define – created by the forgetful functor to reflexive quivers – this reduces the problem to the construction of coequalizers. In [6, 5.1.7], coequalizers are constructed in reference to the adjunction of Proposition 8 lifted to the category of quivers with commutativity conditions. In [4], which Robert Maxton brought to our attention after our formalization was complete, coequalizers in 𝒞at are constructed as quotient functors defined with respect to what the authors call a generalized congruence, generalizing the composition-stable hom-wise equivalence relations called congruences that we use here. This approach provides an explicit construction of coequalizers in 𝒞at that would be worth formalizing, though the construction of general colimits as coequalizers of maps between coproducts would still suffer from the unnecessary complexity described in the introduction. A final approach that we considered would be to observe that 𝒞at is the category of models of a finite limit theory, and thus is an essentially algebraic theory, and all such categories are locally presentable, and in particular cocomplete [1, §3.D]. The construction of colimits given in the proof of this result is rather unwieldy [1, 3.36]. As noted above, our approach follows [32, 4.5.16].

5 Future work

There are several avenues for future work pursuing the various ways in which the formalizations undertaken here could be extended, generalized, or applied. We plan to make use of the homotopy category functor to formalize a proof of the converse implication (ii)(i) in Proposition 19 by showing that a strict Segal simplicial set is canonically isomorphic, via the unit of our nerve adjunction, to the nerve of its homotopy category. At our suggestion, Nick Ward has formalized the n-truncated analog of strict Segal simplicial sets. Using this, we also plan to refactor our construction of the unit of the nerve adjunction through the abstraction boundary of strict Segal 2-truncated simplicial sets, which will allow us to consolidate, though not totally remove, the uses of Functor.ext.

The proofs of Propositions 10 and 14 are strikingly similar, and it is tempting to search for a common generalization. The adjoint triangle theorem [9, 17] constructs a left adjoint to the first functor in a composable pair whose second functor and composite both admit left adjoints, but this result makes use of a hypothesis that the domain category – 𝒞at in both of our examples – has reflexive coequalizers, so is inapplicable here. Nevertheless, there may be some useful common abstraction that captures the essential features of the particular construction of the 𝒞at-valued left adjoint and associated quotient functor used in both proofs here.

Finally, the homotopy category functor will play a role in the task of formalizing -category theory in Lean. Lean’s mathlib contains a popular “analytic” definition of -categories as quasi-categories: a Quasicategory is a simplicial set satisfying a particular horn filling condition. In a series of PRs that are currently under review, with help from Robin Carlier, Bhavik Mehta, Thomas Murrills, Adam Topaz, Andrew Yang, and Zeyi Zhao, we have formalized a proof that the homotopy category functor preserves finite products. Applying this functor hom-wise converts the simplicially enriched category of quasi-categories to a categorically enriched category of quasi-categories, aka a strict bicategory of quasi-categories. Recent work of Riehl and Verity has shown that the core basic theory of -categories can be developed within this strict bicategory, whose objects are -categories, whose morphisms are -functors, and whose 2-cells are -natural transformations [33]. In particular, the existing mathlib formalization of adjunctions in a bicategory will specialize to provide the theory of adjunctions between -categories. In a nearly completed masters thesis project, Jack McKoen is formalizing a proof that the full subcategory 𝒬𝒞at of 𝒮etΔop defined by the quasi-categories is cartesian closed, meaning it is enriched over itself. Once this is complete, we will be able to conclude that the strict bicategory of quasi-categories is cartesian closed, allowing us to define limits and colimits in an -category and prove that right adjoints preserve limits [8, 33].

References

  • [1] J. Adamek and J. Rosicky. Locally Presentable and Accessible Categories. London Mathematical Society Lecture Note Series. Cambridge University Press, 1994.
  • [2] Benedikt Ahrens, Krzysztof Kapulkin, and Michael Shulman. Univalent categories and the rezk completion. Mathematical Structures in Computer Science, 25(5):1010–1039, 2015. doi:10.1017/S0960129514000486.
  • [3] Andrej Bauer, Jason Gross, Peter LeFanu Lumsdaine, Michael Shulman, Matthieu Sozeau, and Bas Spitters. The HoTT library: A formalization of homotopy type theory in Coq. In Proceedings of the 6th ACM SIGPLAN Conference on Certified Programs and Proofs, CPP 2017, pages 164–172, New York, NY, USA, 2017. Association for Computing Machinery. doi:10.1145/3018610.3018615.
  • [4] Marek A. Bednarczyk, Andrzej M. Borzyszkowski, and Wieslaw Pawlowski. Generalized congruences – Epimorphisms in Cat. Theory and Applications of Categories, 5(11):266–280, 1999.
  • [5] A.J. Blumberg, D. Gepner, and G. Tabuada. A universal characterization of higher algebraic k-theory. Geometry & Topology, 2(17):733–838, 2013. doi:10.2140/gt.2013.17.733.
  • [6] Francis Borceux. Handbook of Categorical Algebra 1: Basic Category Theory. Encyclopedia of Mathematics and its Applications. Cambridge University Press, 1994.
  • [7] Guillaume Brunerie, Kuen-Bang Hou (Favonia), Evan Cavallo, Tim Baumann, Eric Finster, Jesper Cockx, Christian Sattler, Chris Jeris, Michael Shulman, et al. Homotopy type theory in Agda. https://github.com/HoTT/HoTT-Agda, 2019.
  • [8] Mario Carneiro, Emily Riehl, and Dominic Verity. The -cosmos project. https://emilyriehl.github.io/infinity-cosmos/, 2025.
  • [9] Eduardo Dubuc. Adjoint triangles. In S. MacLane, editor, Reports of the Midwest Category Seminar II, pages 69–91, Berlin, Heidelberg, 1968. Springer Berlin Heidelberg.
  • [10] L. Fargues and P. Scholze. Geometrization of the local Langlands correspondence. arXiv:2102.13459, 2024.
  • [11] P.G. Goerss and J.F. Jardine. Simplicial homotopy theory. Birkhäuser Basel, 2009.
  • [12] Jason Gross, Adam Chlipala, and David I. Spivak. Experience implementing a performant category-theory library in coq. In Gerwin Klein and Ruben Gamboa, editors, Interactive Theorem Proving, pages 275–291, Cham, 2014. Springer International Publishing. doi:10.1007/978-3-319-08970-6_18.
  • [13] John Harrison. Formalized mathematics. Technical Report 36, Turku Centre for Computer Science (TUCS), 1996. URL: http://www.cl.cam.ac.uk/˜jrh13/papers/form-math3.html.
  • [14] John Harrison. Hol light: An overview. In International Conference on Theorem Proving in Higher Order Logics, pages 60–66. Springer, 2009. doi:10.1007/978-3-642-03359-9_4.
  • [15] Jason Z. S. Hu and Jacques Carette. Formalizing category theory in Agda. In Proceedings of the 10th ACM SIGPLAN International Conference on Certified Programs and Proofs, CPP 2021, pages 327–342, New York, NY, USA, 2021. Association for Computing Machinery. doi:10.1145/3437992.3439922.
  • [16] Gérard Huet and Amokrane Saïbi. Constructive category theory. In Proof, Language, and Interaction: Essays in Honour of Robin Milner. The MIT Press, May 2000. doi:10.7551/mitpress/5641.003.0015.
  • [17] S. A. Huq. An interpolation theorem for adjoint functors. Proc. Amer. Math. Soc., 25:880–883, 1970. doi:10.1090/S0002-9939-1970-0260824-1.
  • [18] Nikolai Kudasov, Emily Riehl, and Jonathan Weinberger. Formalizing the -categorical Yoneda lemma. In Proceedings of the 13th ACM SIGPLAN International Conference on Certified Programs and Proofs, CPP 2024, pages 274–290, New York, NY, USA, 2024. Association for Computing Machinery. doi:10.1145/3636501.3636945.
  • [19] Amélia Liao, Daniel Sölch, Jonathan Coates, Matthew McQuaid, Nathan van Doorn, Naïm Favier, Patrick Nicodemus, Reed Mullanix, favonia, uni, et al. 1Lab. https://1lab.dev/, 2025.
  • [20] J. Lurie. Spectral Algebraic Geometry. www.math.ias.edu/lurie/papers/SAG-rootfile.pdf, February 2018.
  • [21] Jacob Lurie. Derived algebraic geometry. PhD thesis, Massachusetts Institute of Technology, 2004. URL: http://hdl.handle.net/1721.1/30144.
  • [22] S. Mac Lane. Categories for the Working Mathematician. Graduate Texts in Mathematics. Springer-Verlag, second edition, 1998.
  • [23] The mathlib Community. The lean mathematical library. In Proceedings of the 9th ACM SIGPLAN International Conference on Certified Programs and Proofs, CPP 2020, pages 367–381, New York, NY, USA, 2020. Association for Computing Machinery. doi:10.1145/3372885.3373824.
  • [24] Mihails Milehins. Category theory for ZFC in HOL I: Foundations: Design patterns, set theory, digraphs, semicategories. Archive of Formal Proofs, September 2021. , Formal proof development. URL: https://isa-afp.org/entries/CZH_Foundations.html.
  • [25] Mihails Milehins. Category theory for ZFC in HOL II: Elementary theory of 1-categories. Archive of Formal Proofs, September 2021. , Formal proof development. URL: https://isa-afp.org/entries/CZH_Elementary_Categories.html.
  • [26] Mihails Milehins. Category theory for ZFC in HOL III: Universal constructions. Archive of Formal Proofs, September 2021. , Formal proof development. URL: https://isa-afp.org/entries/CZH_Universal_Constructions.html.
  • [27] Kim Morrison. Lean category theory. https://github.com/kim-em/lean-category-theory, 2023.
  • [28] David Nadler and Hiro Lee Tanaka. A stable -category of Lagrangian cobordisms. Advances in Mathematics, 366:107026, 2020. doi:10.1016/j.aim.2020.107026.
  • [29] Tobias Nipkow, Markus Wenzel, and Lawrence C Paulson. Isabelle/HOL: a proof assistant for higher-order logic. Springer, 2002. doi:10.1007/3-540-45949-9.
  • [30] Greg O’Keefe. Towards a readable formalisation of category theory. Electronic Notes in Theoretical Computer Science, 91:212–228, 2004. Proceedings of Computing: The Australasian Theory Symposium (CATS) 2004. doi:10.1016/j.entcs.2003.12.014.
  • [31] C.L. Reedy. Homotopy theory of model categories. unpublished manuscript. URL: https://math.mit.edu/˜psh/reedy.pdf.
  • [32] Emily Riehl. Category Theory in Context. Aurora Modern Math Originals. Dover Publications, 2016.
  • [33] Emily Riehl and Dominic Verity. Elements of -Category Theory. Cambridge Studies in Advanced Mathematics. Cambridge University Press, 2022.
  • [34] Egbert Rijke, Elisabeth Bonnevier, Jonathan Prieto-Cubides, Fredrik Bakke, et al. The agda-unimath library, 2025. URL: https://github.com/UniMath/agda-unimath/.
  • [35] Andrea Vezzosi, Anders Mörtberg, and Andreas Abel. Cubical agda: A dependently typed programming language with univalence and higher inductive types. Journal of Functional Programming, 31:e8, 2021. doi:10.1017/S0956796821000034.
  • [36] Vladimir Voevodsky, Benedikt Ahrens, Daniel Grayson, et al. Unimath — a computer-checked library of univalent mathematics. doi:10.5281/zenodo.7848572.