Formalizing Category Theory in Agda
Total Page:16
File Type:pdf, Size:1020Kb
Formalizing Category Theory in Agda Jason Z. S. Hu Jacques Carette [email protected] [email protected] McGill University McMaster University Montréal, Québec, Canada Hamilton, Ontario, Canada Abstract embody choices; some were forced by the ambient logic of The generality and pervasiveness of category theory in mod- the host system, others were pragmatic decisions, some were ern mathematics makes it a frequent and useful target of philosophical stances, while finally others were simply de- formalization. It is however quite challenging to formalize, sign decisions. for a variety of reasons. Agda currently (i.e. in 2020) does Category theory is often picked as a challenge, as it is both not have a standard, working formalization of category the- be quite amenable to formalization and to involve many non- ory. We document our work on solving this dilemma. The trivial decisions that can have drastic effects on the usability formalization revealed a number of potential design choices, and effectiveness of the results [16]. With the rapid rise in and we present, motivate and explain the ones we picked. the use of category theory as a tool in computer science, and In particular, we find that alternative definitions or alter- with the advent of applied category theory, having a stable native proofs from those found in standard textbooks can formalization in the standard library of one’s favourite proof be advantageous, as well as “fit” Agda’s type theory more assistant becomes necessary. smoothly. Some definitions regarded as equivalent in stan- Our journey started as the authors were trying to keep dard textbooks turn out to make different “universe level” the “old” category theory library for Agda [26] alive. Unfor- assumptions, with some being more polymorphic than oth- tunately, as Agda [3] evolved, some of the features used in ers. We also pay close attention to engineering issues so that that library were no longer well-supported, and eventually the library integrates well with Agda’s own standard library, the library simply stopped working. As it became clear that as well as being compatible with as many of supported type simply continuing to patch that library was no longer viable, theories in Agda as possible. a new version was in order. This gave us the opportunity to revisit various design CCS Concepts: • Theory of computation ! Type the- decisions of the earlier implementation — which we will doc- ory; Logic and verification. ument. We also wanted to preserve as much formalization Keywords: Agda, category theory, formal mathematics effort as possible, while also use language features intro- duced in Agda 2.6+ like generalized variables and revise the ACM Reference Format: theoretical foundation which the library relies on. This new Jason Z. S. Hu and Jacques Carette. 2021. Formalizing Category version is then partly a “port” of the previous one to current Theory in Agda. In Proceedings of the 10th ACM SIGPLAN Interna- versions of Agda, but also heavily refactored, including some tional Conference on Certified Programs and Proofs (CPP ’21), January large changes in design. 18–19, 2021, Virtual, Denmark. ACM, New York, NY, USA, 16 pages. https://doi.org/10.1145/3437992.3439922 Our principal theoretical contribution is to show that setoid-based proof-relevant category theory works just as well 1 Introduction as various other “flavours” of category theory by supporting a large number of definitions and theorems. Our main engi- There have been many formalizations of category theory [7, neering contribution is a coherent set of design decisions for 21] in many different proof assistants, over more than 25 a widely reusable and working library of category theory in years [4, 16, 18, 24, 26, 28–30, 32, 35, 37, etc.]. All of them Agda, freely available1. Permission to make digital or hard copies of all or part of this work for This paper is structured as follows. In Section2, we discuss personal or classroom use is granted without fee provided that copies our global design choices. We discuss the rationale behind are not made or distributed for profit or commercial advantage and that non-strictness, proof-relevance, hom-setoids, universe poly- copies bear this notice and the full citation on the first page. Copyrights morphism, (not) requiring extra laws and concepts as record for components of this work owned by others than the author(s) must types. In Section3, we give examples on how proof-relevance be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific drives us to find concepts in an alternative way. In Section4, permission and/or a fee. Request permissions from [email protected]. we discuss other design decisions and some efficiency issues. CPP ’21, January 18–19, 2021, Virtual, Denmark In Section5, we compare category theory libraries in other © 2021 Copyright held by the owner/author(s). Publication rights licensed systems. Finally, we conclude in Section6. to ACM. ACM ISBN 978-1-4503-8299-1/21/01...$15.00 https://doi.org/10.1145/3437992.3439922 1at https://github.com/agda/agda-categories CPP ’21, January 18–19, 2021, Virtual, Denmark Jason Z. S. Hu and Jacques Carette For reasons of space, we have to make some assumptions 2.2 Which Category Theory? of our readership, namely that they are familiar with: Category theory is often presented as a single theory, but 1. category theory, there are in fact a wealth of flavours: set-theoretic, where 2. dependent type theory, a category has a single hom-set equipped with source and 3. formalization, and target maps; ETCS-style [20], where there are no objects at 4. proof assistants (e.g. familiarity with Agda and a pass- all; dependently-typed, where hom-“sets” are parametrized ing knowledge of other systems). by two objects; proof-irrelevant, where the associativity and identity laws are considered to be unique [4, 16, 26, 35]; 2 Design Choices setoid-based, where each category relies on a local notion of equivalence of hom-sets rather than relying on a global Choices arise from both the system and its logic, as well as equality relation [26, 37]. There are also questions of being from the domain itself. strict or weak, whether to do 1-categories, =-categories or even 1-categories. What to choose? 2.1 Fitting with Agda Standard textbooks often define a category as follows: The previous formalization [26] was done in a much older Agda, with a seriously under-developed standard library. To Definition 2.1. A category C consists of the following data: better fit with modern Agda, we choose to: 1. a collection of objects, C0, 2. a collection of morphisms, C , between two objects. 1. use dependent types, 1 We use 5 : 퐴 ) 퐵 to denote the morphism 5 2 C is 2. be constructive, 1 between objects 퐴 and 퐵, 3. re-use as much of the standard library [12] as possible, 3. for each object 퐴, we have an identity morphism 1 : 4. use the naming convention of its standard library when- 퐴 퐴 ) 퐴, and ever meaningful, 4. morphism composition ◦ composing two morphisms 5. use the variable generalization feature for levels and 5 : 퐵 ) 퐶 and 6 : 퐴 ) 퐵 into another morphism categories, 5 ◦ 6 : 퐴 ) 퐶. 6. try to fit with as many modes of Agda as possible. These must satisfy the following laws: The first two requirements are natural, as choosing other- 1. identity: for any morphism 5 : 퐴 ) 퐵, we have 5 ◦ wise would create a clash of philosophy between the system 1 = 5 = 1 ◦ 5 , and and one of its libraries. The next two are just good soft- 퐴 퐵 2. associativity: for any three morphisms 5 , 6 and ℎ, we ware engineering, while the fifth is mere convenience. Note have ¹5 ◦ 6º ◦ ℎ = 5 ◦ ¹6 ◦ ℎº. that re-using the standard library pushes us towards setoids (more on that later) as its formalization of algebra uses them Embedded in the above definition are a variety of decisions, extensively. and we will use these as a running example to explain ours. The last requirement is more subtle: we want to allow 2.2.1 Collections. The first item to notice is the useof others to use alternative systems or make postulates if they collection rather than set or type. Textbooks tend to do this wish, and still be able to use our library. This means that we to side-step “size” issues, and then define various kinds of need to avoid using features that are incompatible with sup- categories depending on whether each of the collections ported systems in Agda. For example, when added to Martin- (objects, all morphisms, all morphisms given a pair of ob- Löf Type Theory (MLTT) [23], axiom K [31], equivalent to jects) is “small”, i.e. a set. This matters because a number of Uniqueness of Identity Proofs (UIP), creates a propositionally constructions in category theory produce large results. extensional type theory incompatible with univalence [33]. We define collections of objects to be types, with no fur- Thus Agda has options such as --without-K [10] to access ther assumptions or requirements. We do know that in MLTT the intensional type theory MLTT, and conversely --with-K types are well modeled by 1-groupoids [17, 36] — so wouldn’t to turn on axiom K. Separately, there is cubical type the- this higher structure be a problem? No! This is because we ory (--cubical)[34] which implements a computational never look at it, i.e. we never look at the identity type (or interpretation of homotopy type theory (HoTT) [33] and their identity types) of objects.