Scott’s Representation Theorem and the Univalent Karoubi Envelope
Abstract
Lambek and Scott constructed a correspondence between simply-typed lambda calculi and Cartesian closed categories. Scott’s Representation Theorem is a cousin to this result for untyped lambda calculi. It states that every untyped lambda calculus arises from a reflexive object in some category.
We present a formalization of Scott’s Representation Theorem in univalent foundations, in the (Rocq-)UniMath library. Specifically, we implement two proofs of that theorem, one by Scott and one by Hyland. We also explain the role of the Karoubi envelope – a categorical construction – in the proofs and the impact the chosen foundation has on this construction. Finally, we report on some automation we have implemented for the reduction of -terms.
Keywords and phrases:
Lambda calculi, algebraic theories, categorical semantics, Karoubi envelope, formalization, Rocq-UniMath, univalent foundationsCopyright and License:
2012 ACM Subject Classification:
Theory of computation Denotational semantics ; Theory of computation Logic and verificationSupplementary Material:
Software (Source Code): https://github.com/UniMath/UniMath/tree/5941f44e3c13d2ac385f5a8b9b486c0088dd3f46archived at
swh:1:dir:93afb3f22a54064c0baa8493bd326f385fc96ebe
Acknowledgements:
We thank Mohamed Barakat, Tom de Jong, and Niels van der Weide, as well as the anonymous referees for valuable feedback on versions of this paper.We gratefully acknowledge the work by the Rocq development team in providing the Rocq proof assistant and surrounding infrastructure, as well as their support in keeping UniMath compatible with Rocq.Editors:
Yannick Forster and Chantal KellerSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
We present a formalization of two proofs of Dana Scott’s Representation Theorem (SRT) and a detailed comparison between them, all incorporated into the UniMath library [42] of univalent mathematics, based on the proof assistant Rocq (formerly named Coq) [35].
SRT provides denotational semantics for the untyped lambda calculus. It is a cousin to the arguably better-known result by Lambek and P.J. Scott [25], which provides denotational semantics for the simply-typed lambda calculus (Section 1.1). Specifically, SRT states that lambda calculi can be modelled by certain objects in certain categories, and that every lambda calculus arises as such an object (Section 1.2).
In this work, based on [38], we formalize the original proof of SRT by Dana Scott [32] and a more modern proof by Martin Hyland [23]. We formalize them in univalent foundations, which are more granular than traditional set-theoretic foundations. This shows strongly in our analysis of these proofs: univalent foundations provide exactly the right language to explain the fundamental difference between the two constructions.
In the remainder of the introduction, we review Lambek and Scott’s correspondence between Cartesian closed categories and simply-typed lambda calculi in Section 1.1. Afterwards, in Section 1.2, we give an introduction to SRT. In Section 1.3, we provide a brief overview of univalent foundations and category theory therein, to the extent used in this paper. In Section 1.4, we give an overview of the paper and pointers to the formalization.
1.1 The Correspondence Between Categories and Typed -calculi
We sketch the correspondence between simply-typed lambda calculi (STLCs) and Cartesian closed categories. A STLC is a simply-typed language with at least abstraction and application. The type system of the language should be closed under function types . Lambek and Scott [25] describe the construction of denotational models for such calculi.
Definition 1.
A Cartesian closed category, abbreviated CCC, is a category , equipped with a terminal object , binary products , and exponentials .
Now, a Cartesian closed category gives rise to a STLC, its “internal language”: roughly, take the objects of the category to be types, and morphisms of the category to be terms of type in context . Conversely, any simply-typed lambda calculus gives rise to a Cartesian closed category, as follows. A type of the calculus gives an object in the category. A term of type in context gives a morphism .
Internal logic and interpretation, together, provide a correspondence between simply-typed lambda calculi and Cartesian closed categories:
| (1) |
1.2 Scott’s Representation Theorem
In 1969, Scott gave a famous series of lectures [33] where he initially claimed that untyped -calculi did not have (non-trivial) categorical models. Intuitively, for an object of some category to be a model of the untyped lambda calculus, one would need functions modelling abstraction and application, such that the composition is the identity – to model -equality. (For -equality, the other composite must be the identity on .) In particular, this would mean that the function space can be embedded into itself. In the category of sets, this is only possible for . However, as Scott’s lectures progressed, he realized that his initial claim was incorrect. In the end, Scott constructed a model in the category of directed complete partial orders (dcpos). Later, models such as Scott’s graph model and Böhm’s tree model were constructed; see, e.g., [11, Part V].
To arrive at a result similar to the correspondence of Diagram (1), SRT abstracts away from particular calculi and models. We use Hyland’s [23] axiomatization of lambda calculi as -theories, which are particular algebraic theories from universal algebra. A -theory is an algebraic theory equipped with operations modelling abstraction and application, satisfying - (and potentially -) equality. On the semantic side, a model of a lambda calculus is defined to be a reflexive object in a Cartesian closed category; that is, a diagram that exhibits as a retract of (see Definition 16).
For every reflexive object, one can construct its so-called endomorphism theory. Then SRT can be stated as follows: every untyped -calculus arises as the endomorphism theory of a reflexive object. To summarize, SRT provides the following diagram, where consists of -theories and of pairs of a Cartesian closed category and a reflexive object therein:
| (2) |
Unlike in the case of simply-typed lambda calculi, this diagram does not represent an equivalence, but only a section-retraction pair. That is, if we start with a lambda calculus, interpret it and then take the endomorphism theory of the interpretation, we end up with “the same” lambda calculus. However, starting with a reflexive object, taking its endomorphism theory and then the interpretation does not necessarily yield the original reflexive object; see also Remark 19.
Scott and Hyland gave different constructions of the interpretation of a lambda calculus and the proof that this forms a section. In this paper, we formalize both, and compare them, in the light of univalent foundations.
1.3 A Quick Tour of Univalent Foundations and Univalent Categories
In this section, we give a brief introduction to univalent foundations and univalent category theory. Other short introductions can be found in [18, 9]. A comprehensive introduction is given in the HoTT book [37]; in particular, [37, Chap. 9] discusses univalent categories.
Univalent foundations (UF) are foundations of mathematics satisfying the univalence principle. This principle says, roughly, that two equivalent mathematical objects satisfy all the same properties. Technically, UF are an extension of Martin-Löf type theory (MLTT) [27] by the univalence axiom and, optionally, (certain) higher inductive types. We assume the reader is familiar with the basics of MLTT as discussed, for instance, in [37, Chap. 1]. To fix notation: we write for the composition of functions and .
Given a type and elements , we denote by the identity type between and . The type can contain multiple distinct elements, that is, different identity terms from to . For any , we have the identity term . In particular, given two types and (elements of some universe type), we have the type . There is also a seemingly weaker notion of sameness, called equivalence, written ; its elements are functions with a two-sided inverse (see [37, Chap. 4] for details). We have a function , sending to the identity equivalence on . The univalence axiom says that this map is an equivalence, for any and . In particular, every structure on types transports along an equivalence of types.
In univalent foundations, types are stratified according to how “complex” their identity types are: Let be a type. It is contractible if there is an equivalence to the unit type . It is a proposition if any two of its elements are identical: . It is a set if all of its identity types , for , are propositions (identity proofs in are unique). It is a groupoid if all of its identity types , for , are sets. This stratification of types can be continued recursively, but the aforementioned definitions suffice for the purpose of this paper.
Given a type , we form a new type , the propositional truncation of ; is a proposition, and inhabited if and only if is inhabited. When postulating that something “merely exists in with property ” (such as in Definition 44), we formalize it as .
There are two notions of category (
) in univalent foundations.
Both kinds consist, in particular, of a type of objects and a dependent type of morphisms.
Given a category , we write or for the type of morphisms between objects and .
A category also has suitably typed composition and identity operations, and proofs of the associativity and unitality of composition.
To ensure that unitality and associativity are propositions, we require that all the are sets.
Just like with functions, we write for the composition of morphisms and .
For details, we refer to [5, Def. 3.1] – there, categories are called precategories.
A (pre)category as described above does not correspond to a category in Voevodsky’s model of univalent foundations in simplicial sets [24]; it has data that is not usually present in a category, namely a potentially non-trivial identity type on the type of objects. There are two ways to “kill” that data. Firstly, we could assert that the type of objects should also form a set in the above sense; this leads to the notion of setcategory. Secondly, we could assert that the identities of correspond exactly to isomorphisms of the category; this leads to the notion of univalent category. More precisely, in a univalent category we demand that for every , the map , mapping to the identity isomorphism on , is an equivalence, in analogy to the univalence axiom. One can show that the type of objects of a univalent category forms a groupoid.
There are thus two theories of categories in univalent foundations. Setcategory theory behaves much like traditional category theory; however, many naturally occurring categories are not setcategories. On the other hand, there are many univalent categories. Importantly, the category of sets (of a fixed universe) in the sense described above, is univalent; its objects are pairs of a type (of a fixed universe) together with a proof that is a set, and morphisms are type-theoretic functions. Roughly, univalence of this category is a consequence of the univalence axiom (for that universe). Furthermore, for two categories and , the functor category is univalent if is univalent; hence, in particular, presheaf categories are univalent. Still, not every category is univalent, e.g., the category with two distinct but isomorphic objects, or the set-Karoubi envelope of Section 3.1, see Example 42. However, every category has a univalent replacement, which we call its Rezk completion:
Theorem 2 ([5, Thm. 8.5]).
For every category , there exists a univalent category with a fully faithful and essentially surjective functor .
In summary, univalent foundations give us two flavors of category theory: category theory up to isomorphism (setcategories) and category theory up to adjoint equivalence (univalent categories). We demonstrate the difference between Scott’s and Hyland’s proof of SRT by showing that Scott’s construction happens in the realm of setcategories, whereas Hyland’s construction happens in the realm of univalent categories. Furthermore, Hyland’s construction yields the Rezk completion of Scott’s construction.
1.4 Synopsis and Guide to the Formalization
In Section 2 we present our formalization of SRT as shown in Equation 2. More precisely, we formalize two different constructions of the function labelled “interpretation”, one by Scott, and one by Hyland, and we prove that both are sections to the function associating to a reflexive object its endomorphism theory. In Section 3 we zoom in on the different interpretation functions. Both use a variant of the Karoubi envelope of a category. We provide a novel analysis and comparison of the two variants, through the lens of univalent foundations, and show how the two variants relate Scott’s and Hylands constructions. In Section 4 we describe a tactic for the manipulation of lambda terms. In Section 5 we discuss related work.
Most of the definitions and results presented in this paper are formalized and computer-checked in UniMath [42], a library of univalent mathematics based on Rocq. Our code has been integrated into the UniMath library, and comprises more than 11000 lines of code, split up more or less evenly between definitions and proofs. We can identify the following main components: the definitions of the basic categories with their object and morphism types (3000 lines); the proof of Scott’s version of SRT, which uses a lot of reasoning about -terms (3000 lines); the definition of the Karoubi envelope (2000 lines); the constructions of various algebraic theories, like the endomorphism theory, and the proof of the relation between the representation theorems (both 1000 lines).
Throughout the paper, definitions and results are decorated with links, for example,
, to an HTML version of UniMath.
That HTML version is derived from commit 5941f44 of UniMath.
Proof-checking and creation of the HTML documentation can be reproduced locally by following
the UniMath compilation instructions.
Note that for technical reasons, the formalization does not always closely follow the material in this paper, with the main difference described in Remark 21.
Another point of complication is the necessary switching between two different ways to encode n-tuples , either as nested 2-tuples or as functions .
2 What we Formalized: Scott’s Representation Theorem
In this section, we present our formalization of SRT. In Section 2.1, we define the type of -theories, the type of reflexive objects, and the endomorphism theory generated by a reflexive object, in the form of a function ( for “endomorphism theory”). In Sections 2.2 and 2.3, we present Scott’s and Hyland’s constructions of a section to , that is, of functions (where and stand for “Scott” and “Hyland”, respectively) such that .
| (3) |
Remark 3.
It might seem surprising that we can show instead of just for and a suitable notion of isomorphism of -theories. We can show the seemingly stronger identity thanks to the univalence axiom. Specifically, the univalence axiom entails that isomorphisms of -theories coincide with identities – expressed below by the statement that the category of -theories is univalent (Proposition 13). That is, our statement of SRT does not make reference to (iso)morphisms of -theories; the construction, however, does: we construct an isomorphism first, and turn it into an identity using univalence afterwards.
2.1 Untyped -Calculi and Their Denotational Semantics
We give a definition of untyped -calculi (Definition 8), an extension of the concept of an algebraic theory from universal algebra (Definition 4). We also define reflexive objects in Cartesian closed categories, the intended denotational semantics for such calculi.
Definition 4 (
).
We define an algebraic theory to be a sequence of sets indexed over with for all elements (“variables” or “projections”) (we often leave implicit and write ), together with a substitution operation for all and , such that for all , , and ,
Algebraic theories are also known as (abstract) clones; they are similar to operads – see, e.g., [22, 36] for details.
Definition 5 (
).
A morphism between algebraic theories and is a sequence of functions (we usually leave the implicit and just write if the context is clear) such that for all , and , , and .
Algebraic theories and their morphisms form a category (
).
Furthermore, an isomorphism of algebraic theories consists of pointwise bijections that respect the variables and substitution.
By univalence, each corresponds to an identity also respecting the algebraic structure.
Again by univalence, the ’s combine into an identity of algebraic theories .
Hence:
Example 7 (![[Uncaptioned image]](rocq.png)
).
We have a forgetful functor from to sending an algebraic theory to the set . Conversely, for every set one can construct an algebraic theory with . The variables are given by , for . The substitution is defined as follows: if , then ; if , then . Then, is part of a functor . Furthermore, is a left adjoint to the forgetful functor.
Let be the “weakening” function . Note that and .
Definition 8 (
).
A -theory is an algebraic theory , together with sequences of functions and , such that for all , and ,
Furthermore, we assume that satisfies -equality, that is for all .
We also formalized the notion of -theory without -equality, and of -theory with both - and -equality (meaning ), but we only formalized the main results for the -theories of Definition 8.
Example 9 (
).
An important example of a -theory is provided by the -calculus without constants . We present it here as a higher inductive type, to have it satisfy -equality. For , , and , it has constructors
together with identities about the interaction between with every constructor, as well as the identity for -equality. The functions are given by .
Remark 10 (
).
Because the UniMath community does not permit the usage of inductive types in the core library, we defined synthetically, instead of constructing it: our formalization takes a hypothesis of a type describing the -calculus without constants as a sequence of sets with functions , , and that satisfy the correct identities. The type of also contains an induction principle, which allows one to construct terms of type for a family of sets . The non-dependent version of this induction principle takes a set and functions , , and , satisfying the same identities as the constructors of , and gives functions such that , etc.
Example 12 (
).
For any -theory , there is a unique morphism , defined using structural induction on the terms of : it sends variables to variables, substitution to substitution, etc. This makes into the initial object of .
We can prove the following analogously to Proposition 6:
Proposition 13 expresses that identities of -theories are equivalent to isomorphisms ; this enables us to express SRT purely on the level of types, without needing to mention (iso)morphisms of -theories, as discussed in Remark 3.
Let be a -theory. Hyland shows that corresponds to , which is applied to [23, Section 3.1]. Now consider the element .
Remark 15.
This means that has the structure of a -calculus with -equality, with variables , application and abstraction .
Now we are ready to talk about denotational semantics for the untyped -calculus, for which we will need the following definition.
Definition 16 (
).
If is a category with binary products, a reflexive object in is an object such that the exponential exists, and with a retraction from onto : morphisms and such that .
Definition 17 (
).
We define a function as follows. Let be a reflexive object in a category , with the retraction given by and .
The endomorphism theory of is the algebraic theory given by with projections as variables and a substitution that sends and to .
If is the bijection given by the exponential , we can give a -theory structure by setting, for and ,
-equality for follows immediately from the fact that is a retraction.
The proofs that is an algebraic theory follow mainly from properties of the product and naturality of . Note that would satisfy -equality if were a section.
Example 18.
As a trivial example, take in the category of sets. The set of functions is isomorphic to , and so we get an endomorphism theory with .
Because of cardinality reasons, Example 18 is the only reflexive object in the category of sets. Hence, for nontrivial models of the untyped -calculi, we have to look in categories different from . Scott’s model lives in the category of dcpos whose morphisms are (continuous) section-retraction pairs [31].
SRT states that we can represent every untyped -calculus as an endomorphism theory of some reflexive object:
Theorem.
The function of Definition 17 has a right inverse:
| (4) |
In the following two subsections, we will discuss two proofs of this theorem: the original proof by Dana Scott (Theorem 24) and a more abstract proof by Martin Hyland (Theorem 36).
Remark 19 (
).
The constructions by Scott and Hyland yield different reflexive objects. In particular, Hyland’s category has an empty object with no morphisms into it, whereas Scott’s category always has at least one morphism between any two objects. By the representation theorems, the function sends both of these to the same -theory, so is not injective. Therefore, contrary to the situation for STLCs in Section 1.1, is not an equivalence.
Remark 20.
By definition, the -theories we consider satisfy -equality. A variant of SRT also holds for -theories satisfying both and -equality.
Remark 21.
We use the technology of displayed categories [6] for implementing many of the concepts in this section. A displayed category over a category gives the objects and morphisms of “additional structure”. Consider how a -theory is an algebraic theory, together with operations and , and a -theory morphism is an algebraic theory morphism, that preserves and . This is formalized by defining to be displayed over . In turn, is displayed over . The goal of displayed categories is to build categories of complicated objects and morphisms in “layers”, incrementally adding more structure. These layers can be built and reasoned about modularly. The proof of univalence of those categories, Propositions 6 and 13, is then especially simple: it relies on univalence of and univalence of the layers leading up to . In the formalization, after the categories have been defined, we define the object types in terms of their categories, to minimize the amount of duplicate code. For example, we define a -theory to be an object of . A drawback to this approach is that it somewhat obfuscates the precise definition of the objects and morphisms of such a category: the structure of a -theory is hidden away in a stack of four displayed categories, although the constructor and accessors give some hints about what a -theory consists of.
2.2 Scott’s Construction
In this subsection, we will give Scott’s original proof [32] of SRT.
It is a fairly syntactical proof, making heavy use of the operations of the -calculus for the various constructions. We first define the category (Definition 22) and show that it has products and exponential objects. Then we construct an object in this category and show that it is a reflexive object. Lastly, we construct the endomorphism theory and show that it is isomorphic to the -theory that we started with (Theorem 24).
Let be a -theory. First of all, for , we define
Although, actually, since every one of these starts with a -abstraction, we need to lift the constants to to make the definitions above typecheck. Note that and .
We define (analogous for ) and correspondingly .
Scott introduces the following category, referred to as the category of retracts:
Definition 22 (
).
Let be the category with as objects the such that and as morphisms the such that . The composition is given by the composition of -terms and the identity on is given by itself.
Now we can define our reflexive object:
Definition 23 (![[Uncaptioned image]](rocq.png)
).
We define , given by the identity . For all , we can also consider as both a morphism and a morphism with , exhibiting as a retract of . In particular, is reflexive.
This allows us to give Scott’s proof of the representation theorem:
Theorem 24 (
).
The function of Definition 17 has a right inverse , given by .
Proof.
Let be a -theory. As mentioned in Definition 23, is a reflexive object, so the endomorphism theory has a -theory structure.
We have bijections , given by and . These are bijections because , with . Explicitly, has variables , substitution , abstraction and application for and . Using this, it is pretty straightforward to check that the respect the -theory structure, so is an isomorphism of -theories. Then, univalence of gives an identity .
2.3 Hyland’s Construction
While Scott proves the representation theorem in a very syntactical way, constructing the category for his representation theorem using the -calculus operations, Hyland uses more machinery from category theory, and works with a different category: the category of (-)presheaves of a -theory .
Remark 25.
The -presheaves can be described as the presheaves over the category , referred to as the Lawvere theory associated to (Definition 33). Instead of working with the general construction, we spell out what this means, and refer to presheaves as the unfolded version hereof. In Lemma 34, we show that these notions coincide.
In this section, we define the category of presheaves (Definition 26), and construct a reflexive object herein Corollary 32. Furthermore, we show that the endomorphism theory of the reflexive object is isomorphic to the -theory that we started with (Theorem 36).
Definition 26 (
).
A presheaf for an algebraic theory is a sequence of sets indexed over , together with an action
for all , such that for all , and ,
A prime example of a -presheaf is itself:
Definition 27 (
).
For an algebraic theory , its theory presheaf (also denoted ) is the -presheaf given by the sets of . Its -action is the substitution operation of .
Definition 28 (
).
For an algebraic theory , a morphism between -presheaves and is a sequence of functions such that for all and , .
-presheaves and their morphisms form the category of -presheaves (
).
Because presheaf (iso)morphisms preserve , we have, analogous to Proposition 6:
Definition 30 (
).
Given a -presheaf , we can construct a presheaf with and, for and , whose action is given by
This is reminiscent of the -theory axioms.
Lemma 31 (![[Uncaptioned image]](rocq.png)
).
The category of -presheaves has binary products: for presheaves and , we have and . Furthermore, for all -presheaves , is the exponential object .
Proof.
Given a -theory , we have sequences of functions and . These commute with the -actions, so they constitute presheaf morphisms. Furthermore, by -equality, we have .
For ease of understanding, and to make Hyland’s version of SRT particularly smooth, we defined presheaves in a very algebraic way. However, in category theory, a presheaf is commonly defined to be a contravariant, set-valued functor on some category . Lemma 34 justifies our definition by showing that -presheaves are equivalent to presheaves on some category:
Definition 33 (
).
Let be an algebraic theory. We construct the category with objects, morphisms, identity and composition for , given by
We call the Lawvere theory associated to .
Note that in , the object behaves as the -fold product with product projections .
Lemma 34 (
).
Let be an algebraic theory, and be its associated Lawvere theory as defined in Definition 33. The category of -presheaves is equivalent to the category of presheaves (contravariant functors) on .
Proof.
A -presheaf corresponds to a -presheaf , where the sets correspond to the action of on objects , and the -action corresponds to the action of on morphisms .
In the proof of the following lemma, we will make use of the so-called Yoneda embedding of a category into its presheaf category. It sends an object to the functor . Note that it is fully faithful: it gives bijections on the morphisms . A well-known lemma in category theory is the Yoneda lemma, which shows that for and , there is an equivalence , which is natural in and .
Proof.
Lemma 34 shows that -presheaves are equivalent to presheaves on the Lawvere theory associated to . Under this equivalence, corresponds to the power of the theory presheaf, for all . Then the Yoneda lemma gives a bijection . Explicitly, it sends to and to . Now we can give Hyland’s proof of the representation theorem:
Theorem 36 (
).
The function of Definition 17 has a right inverse , given by , where the last is the theory presheaf of Definition 27.
Proof.
Let be a -theory. Recall from Corollary 32 that the theory presheaf is a reflexive object with , so has a -theory structure.
Lemma 35 gives a sequence of bijections for all , sending to , and conversely sending to . It considers -terms in variables as -ary functions on the -calculus. Therefore, preserves the , , and , which makes it into an isomorphism of -theories. Then, univalence of gives an identity .
So Hyland shows that the representation theorem follows largely from the fact that the functions from to itself can be represented by , together with the Yoneda lemma for the Lawvere theory associated to (Lemma 35).
Observe that the proof of Theorem 36 does not require - or -equality, but if has - or -equality, then we can immediately see (even without the isomorphism from the theorem) that the endomorphism theory also has this property.
3 The Karoubi Envelope
In this section, we relate the construction of Hyland to that of Scott. The relation is made precise by realizing that their categories are both strongly related to the Karoubi envelope.
Below, we characterize the Karoubi envelope via its defining universal property (Definition 37). In Sections 3.1 and 3.2, we provide two implementations hereof. Classically, these two constructions are equivalent. In univalent foundations however, we see that the former provides a setcategory, whereas the latter provides a univalent category, and that one is the Rezk completion of the other (Theorem 47).
Before stating the defining property of the Karoubi envelope, we first need the following definitions. Let be a category and objects. We will denote the type of section-retraction pairs of onto with
Now, note that for , is an idempotent morphism, since . We say that some idempotent morphism splits if we can find some and some such that . If does not split, a natural question to ask is whether we can find an embedding into some category such that the idempotent does split. This is one way to arrive at the Karoubi envelope. Its classical universal property is as follows:
Definition 37 (
).
The Karoubi envelope of is a category such that
-
1.
Every idempotent in splits;
-
2.
There is a fully faithful functor ;
-
3.
For every object , there merely exists an object and a retraction .
Classically, this determines the Karoubi envelope up to equivalence of categories. However, as explained in Section 1.3, in univalent foundations, there are two notions of category: setcategories and univalent categories. We hence add another condition:
-
4.
The setcategory Karoubi envelope (i.e. the Karoubi envelope in setcategory theory) should be a setcategory; the univalent Karoubi envelope should be a univalent category.
In Section 3.1 (resp. 3.2), we construct the setcategory (resp. univalent) Karoubi envelope.
3.1 The Setcategory Construction
In this section, we construct a category from a category , and show that it is the setcategory Karoubi envelope if is a setcategory. The construction is the same as the one formalized in the 1lab [26] (see also Section 5).
Definition 38 (
).
We define the category . The objects of are tuples with and such that . The morphisms between and are morphisms such that . This can be summarized in the following diagram:
| (5) |
The identity morphism on is given by and inherits composition from .
Proof.
-
1.
Let and idempotent. Then splits via since .
-
2.
Let be the functor sending to and to . It is fully faithful:
-
3.
Let with and idempotent. Then, , since .
-
4.
If is a setcategory, the objects in form a set, since both the type of objects of and are sets, for any .
It is a general fact that completions induce monads:
Proof.
The fully faithful embedding induces equivalences . We also have an equivalence , because any identity also preserves the identity morphism on . Therefore, if is univalent, we have a chain of equivalences , which shows that is univalent as well.
To show that the converse of Proposition 41 does not hold, the next example considers the -object category associated to a monoid . That is, has one object called, say, , morphisms , and the composition is given by the monoid multiplication.
Example 42.
Consider the commutative monoid consisting of the three matrices , and under matrix multiplication. Then, is univalent since is the only isomorphism. However, is not univalent; indeed, we have .
Example 43 (
).
For a -theory, the category (Definition 22) is equal to , where is the monoid , consisting of terms with -equality.
3.2 The Univalent Construction
In this section, we construct a category from a category , and show that it is the univalent Karoubi envelope of , regardless of whether is univalent or not. We use the fully faithful Yoneda embedding .
Definition 44 (
).
We define the category as the full subcategory of consisting of objects such that there merely exist an object and a retraction-section pair , summarized in the following diagram:
| (6) |
Proof.
-
1.
Let be idempotent. Consider the equalizer of and in . Note that , so by the universal property of the equalizer, there exists such that .
(7) Note that , so again by the universal property of the equalizer, , exhibiting as a retract of in . Since we can compose retracts, and since is in , we get some such that is in . Lastly, since the morphisms of are borrowed directly from , we have and splits.
-
2.
The Yoneda embedding provides a functor , given by
Since the Yoneda embedding is fully faithful, is fully faithful as well.
-
3.
For every , witnesses that there merely exist an object and a retraction in , and this gives a retraction .
-
4.
is univalent as a full subcategory of a univalent presheaf category.
Remark 46 (
).
Instead of Item 1 above, one can also show that idempotents split in by showing that idempotents split in , and that, under certain conditions (including univalence of the target category), taking a functor category and a full subcategory preserves the splitting of idempotents.
Proof.
is univalent, and there is a fully faithful and essentially surjective functor , sending to the equalizer of and .
3.3 The Relation Between Scott’s and Hyland’s Constructions
In this section, we will use the setcategory and univalent Karoubi envelopes to “embed” Scott’s reflexive object into Hyland’s reflexive object, and show that the correctness of Hyland’s construction can be viewed as a consequence of the correctness of Scott’s construction.
Recall that Scott and Hyland construct different right inverses to the function . Scott constructed the right inverse as , whereas Hyland constructed . In this section, we show that can be embedded into , and that and coincide under this embedding.
Fix a -theory . Recall that is equal to , where is the monoid ; see Example 43. We construct the embedding as a composition of the following functors:
| (8) |
where is the monoid . The following propositions construct two of the functors:
Proof.
The functions and give an isomorphism of monoids , because elements of (resp. ) satisfy -equality (resp. -equality). The functoriality of and turns this into an equivalence of categories .
Proposition 49 (
).
Let be a category with all colimits. Precomposition with the embedding gives an equivalence between functor categories and .
Taking gives an equivalence , because taking opposite categories preserves equivalences. Together with Proposition 48, Theorem 47, the fact that is a full subcategory of and Lemma 34, this gives an embedding of into :
Theorem 50 (![[Uncaptioned image]](rocq.png)
).
The composite of the functors is fully faithful. Furthermore, they send the reflexive object to the reflexive object .
The embedding of Theorem 50 witnesses the passage from setcategories (the top row) to univalent categories (the lower row), and the vertical arrow in the diagram is precisely given by the Rezk completion. The theorem allows us to view as a full subcategory of , where becomes , which relates Hyland’s proof to that of Scott:
Scott’s and Hyland’s constructions send a -theory to the reflexive objects and respectively. The endomorphism theory of a reflexive object is the -theory with . The result of this construction does not change when we pass to a larger category, as long as the powers of , the exponential and the morphisms between them do not change. Therefore, one expects an isomorphism , which can be composed with the isomorphism from Scott’s version of SRT to get for Hyland’s version of SRT: if Scott’s construction gives a section to the retraction , then Hyland’s construction gives a section as well, even though the constructions themselves are quite different.
4 A Tactic for Propagating Substitutions
Now, as mentioned in Remark 15, any -theory allows the operations , , and with the same interaction as for the pure -calculus. Using these, we can define combinators like or for , used in the original proof of SRT. However the equalities about the interaction between and the other operations are usually not definitional. Consider the following term (using concatenation for application): . It is not hard to see that this results in . However, it takes several steps to rewrite this: moving the substitution past the -abstraction, then into four applications, and lastly using five variable substitutions, resulting in a total of ten rewrites for a seemingly trivial term. In other proofs, 40 or more of these rewrites need to be done, and since there are many such proofs, this quickly becomes tedious. Therefore, we developed a tactic to perform these rewrites. A first version of this tactic was a variation of
attempting to rewrite with some of the identities. The statement repeat reduce_lambda saved a lot of manual work, but it sometimes took a couple of seconds.
Therefore, we developed a new version of the tactic, called propagate_subst (
), written in Ltac2 [30].
It recursively traverses the -term in the left-hand side of the goal, checking whether the term matches a form that can be rewritten into something else. It performs the possible rewrites, and also prints these rewrite statements which can replace it. For a small example, if the goal is about the interaction between the object and substitution,
U_term f = U_term,
a call to unfold U_term; repeat reduce_lambda takes ca. 100 ms, but propagate_subst () solves the goal in about 35 ms and prints
Replacing the call to propagate_subst by these tactics results in the same rewrites, but these only take about five milliseconds. Many equations in AlgebraicTheories.Combinators were proved using the tactic. For example, the first 9 lines of subst_compose take about 40 ms, whereas propagate_subst () and reduce_lambda take about 330 ms and a second respectively. Apart from the performance gain at the expense of having longer proofs, another reason for replacing calls to propagate_subst by the generated statements is the fact that the UniMath community aims to avoid extensive automation in the final proofs, to increase maintainability.
On top of the speedup, this tactic is modular: some of its parts are also tactics themselves, and can be reused for other tactics as well. For example, the traverse tactic, which traverses a -term in the goal and executes something for every subterm, is also used in another tactic we programmed that is called generate_refine, which takes a pattern, and for every subterm that matches it, prints a statement such as
which can be used to quickly generate statements that very precisely rewrite one subterm.
The propagate_subst tactic is also extensible: the patterns for both the subterm traversal and the rewrites are kept in a list, which can be extended when new combinators are defined. For example, at the point where the tactics are defined, the traversal only works for the constructors , , and , and the rewrites only work for the interactions between those. Using this, composition is defined, and so a pattern to branch into and is added to the traversals, and a rewrite with is added. Progressing through the file, the same is done for combinators like the pair , the projection (including a rewrite ), and (consisting of nested pairs).
We generalized propagate_subst to a tactic for simplification with arbitrary sets of identities (
), and applied this to formulas in a hyperdoctrine (
), a formalization related to [41].
5 Related Work
We first discuss related work in the area of formalization of denotational semantics of lambda calculi, with particular focus on fixpoints.
Perhaps most relevant to our work, Benton, Kennedy, and Varming [13] formalize, in Rocq, a constructive notion of -cpos and the construction, via limits, of solutions to recursive domain equations in the category of -cpos. This involves constructing fixpoints on the level of types. Similarly, Dockins [17] formalizes effective domain theory in Rocq. Those works are thus complementary to ours: the authors work exclusively in the category of certain domains (e.g., -cpos), whereas we study general reflexive objects in suitable categories: their specific -cpos could be fed into our endomorphism theory function to obtain specific lambda calculi.
De Jong [15] models PCF in domains, formalizing the Scott model in particular. In [16], De Jong and Escardó construct Scott’s in univalent foundations, in Agda.
Møgelberg [28] develops a language with fixed point combinators as a metalanguage to reason about domains. In another line of work, guarded type theory (see, e.g., [14, 29]) is being developed as a meta-language for reasoning about recursive domain equations.
Less closely related to our work on the technical level are other efforts formalizing the syntax and semantics of programming languages specifically in UniMath. In particular, Ahrens, Lumsdaine, and Voevodsky [7] compare, in UniMath, different notions of model for dependent type theories. Van der Weide [39] constructs an equivalence of bicategories between univalent locally cartesian closed categories and univalent categories with suitable type formers, and various extensions of that equivalence to additional structure.
Finally, Altenkirch, Kaposi, Šinkarovs, and Végh [10] construct, in Cubical Agda, an equivalence between the simply-typed lambda calculus and combinatory logic. Both theories are given as Generalized Algebraic Theories.
We mention some work on formalization of initial algebra semantics for languages including the untyped lambda calculus. Hirschowitz and Maggesi [20, Thm. 3] formalize the untyped lambda calculus with - and -equality. Ahrens [1] proves an initial semantics result including, as a guiding instance, the lambda calculus with - and -reduction (as opposed to - and -equality), formalized in Rocq. Ahrens, Hirschowitz, Lafont, and Maggesi [4, 3] prove initial semantics for abstract syntax with equations, and formalize their results in UniMath.
Next, we discuss related work in the area of formalization of (univalent) category theory. The formalization of univalent category theory started with [5], and was continued in many different works, e.g., [6, 8, 2, 40], by various authors. We are basing our formalization on the library of univalent category theory built to accompany these formalizations.
There are several other libraries on univalent category theory. The HoTT library [12] focuses on “wild” (higher) categories, that is, categories that omit truncation levels for types of cells. For our purpose, it is important to work with “true” categories, that is, things that correspond to categories in Voevodsky’s simplicial set model of univalent foundations [24]. There does not seem to be a formalization of the Karoubi envelope in the HoTT library. The 1lab [26] is another library of univalent category theory. It features a formalization of the Karoubi envelope of a precategory at https://1lab.dev/Cat.Instances.Karoubi.html. Here, a “precategory” is essentially a category without the assumption that it is either a setcategory or a univalent category. The construction implemented there is the one described in Section 3.1; as discussed in Example 42, it does not necessarily yield a univalent category.
There are many other libraries of formalized category theory; for space reasons, we only point to papers listing and comparing some of them. Gross, Chlipala, and Spivak [19] provide a comparison of different libraries that, at publication time, was quite comprehensive. Hu and Carette [21] provide another comparison of different libraries.
Finally, Shulman [34] studies the question whether idempotent functions between types in MLTT split, with a formalization in Rocq. Semantically, that work is concerned with morphisms between -groupoids, not with morphisms in a 1-category as in our setting.
6 Conclusion
We have presented a formalization of SRT in univalent foundations. We see two benefits to working in univalent foundations:
-
1.
We can express on the level of types a meaningful relation between -theories and reflexive objects, by stating that the functions of Diagram (2) form a section-retraction pair. Proving this is possible because, starting with a given -theory, going to reflexive objects and back to -theories yields an isomorphic – and hence identical – -theory. This uses crucially that the category of -theories is univalent, that is, that isomorphism of -theories is the same as their identities – a consequence of the univalence axiom.
-
2.
The differences between Scott’s and Hyland’s constructions are clarified in univalent foundations: Scott’s is adequate for setcategories, Hyland’s for univalent categories.
We have furthermore programmed a tactic to manipulate lambda terms, combining the convenience of automation with the good performance of precise rewriting.
There are some questions we have left open. In particular, one could ask how Diagram (2) lifts to the level of categories. In that case, the left-hand side clearly forms a univalent 1-category (Proposition 13). The status of the right-hand side is less clear: it could form a (univalent) 1-category whose objects are certain setcategories with an object therein, or a bicategory whose objects are certain univalent categories.
References
- [1] Benedikt Ahrens. Modules over relative monads for syntax and semantics. Math. Struct. Comput. Sci., 26(1):3–37, 2016. doi:10.1017/S0960129514000103.
- [2] Benedikt Ahrens, Dan Frumin, Marco Maggesi, Niccolò Veltri, and Niels van der Weide. Bicategories in univalent foundations. Math. Struct. Comput. Sci., 31(10):1232–1269, 2021. doi:10.1017/S0960129522000032.
- [3] Benedikt Ahrens, André Hirschowitz, Ambroise Lafont, and Marco Maggesi. High-level signatures and initial semantics. In Dan R. Ghica and Achim Jung, editors, 27th EACSL Annual Conference on Computer Science Logic, CSL 2018, September 4-7, 2018, Birmingham, UK, volume 119 of LIPIcs, pages 4:1–4:22. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2018. doi:10.4230/LIPIcs.CSL.2018.4.
- [4] Benedikt Ahrens, André Hirschowitz, Ambroise Lafont, and Marco Maggesi. Modular specification of monads through higher-order presentations. In Herman Geuvers, editor, 4th International Conference on Formal Structures for Computation and Deduction, FSCD 2019, June 24-30, 2019, Dortmund, Germany, volume 131 of LIPIcs, pages 6:1–6:19. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2019. doi:10.4230/LIPIcs.FSCD.2019.6.
- [5] Benedikt Ahrens, Krzysztof Kapulkin, and Michael Shulman. Univalent categories and the Rezk completion. Mathematical Structures in Computer Science, 25(5):1010–1039, January 2015. doi:10.1017/s0960129514000486.
- [6] Benedikt Ahrens and Peter LeFanu Lumsdaine. Displayed categories. Log. Methods Comput. Sci., 15(1), 2019. doi:10.23638/LMCS-15(1:20)2019.
- [7] Benedikt Ahrens, Peter LeFanu Lumsdaine, and Vladimir Voevodsky. Categorical structures for type theory in univalent foundations. Log. Methods Comput. Sci., 14(3), 2018. doi:10.23638/LMCS-14(3:18)2018.
- [8] Benedikt Ahrens, Ralph Matthes, Niels van der Weide, and Kobe Wullaert. Displayed monoidal categories for the semantics of linear logic. In Amin Timany, Dmitriy Traytel, Brigitte Pientka, and Sandrine Blazy, editors, Proceedings of the 13th ACM SIGPLAN International Conference on Certified Programs and Proofs, CPP 2024, London, UK, January 15-16, 2024, pages 260–273. ACM, 2024. doi:10.1145/3636501.3636956.
- [9] Benedikt Ahrens and Paige Randall North. Univalent foundations and the equivalence principle. In Stefania Centrone, Deborah Kant, and Deniz Sarikaya, editors, Reflections on the Foundations of Mathematics: Univalent Foundations, Set Theory and General Thoughts, pages 137–150. Springer International Publishing, Cham, 2019. doi:10.1007/978-3-030-15655-8_6.
- [10] Thorsten Altenkirch, Ambrus Kaposi, Artjoms Šinkarovs, and Tamás Végh. Combinatory Logic and Lambda Calculus Are Equal, Algebraically. In Marco Gaboardi and Femke van Raamsdonk, editors, 8th International Conference on Formal Structures for Computation and Deduction (FSCD 2023), volume 260 of Leibniz International Proceedings in Informatics (LIPIcs), pages 24:1–24:19, Dagstuhl, Germany, 2023. Schloss Dagstuhl – Leibniz-Zentrum für Informatik. doi:10.4230/LIPIcs.FSCD.2023.24.
- [11] Hendrik Pieter Barendregt. The lambda calculus - its syntax and semantics, volume 103 of Studies in logic and the foundations of mathematics. North-Holland, 1985.
- [12] 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 Yves Bertot and Viktor Vafeiadis, editors, Proceedings of the 6th ACM SIGPLAN Conference on Certified Programs and Proofs, CPP 2017, Paris, France, January 16-17, 2017, pages 164–172. ACM, 2017. doi:10.1145/3018610.3018615.
- [13] Nick Benton, Andrew Kennedy, and Carsten Varming. Some Domain Theory and Denotational Semantics in Coq. In Stefan Berghofer, Tobias Nipkow, Christian Urban, and Makarius Wenzel, editors, Theorem Proving in Higher Order Logics, pages 115–130, Berlin, Heidelberg, 2009. Springer Berlin Heidelberg. doi:10.1007/978-3-642-03359-9_10.
- [14] Lars Birkedal, Rasmus Ejlers Møgelberg, Jan Schwinghammer, and Kristian Støvring. First steps in synthetic guarded domain theory: step-indexing in the topos of trees. Log. Methods Comput. Sci., 8(4), 2012. doi:10.2168/LMCS-8(4:1)2012.
- [15] Tom de Jong. The Scott model of PCF in univalent type theory. Math. Struct. Comput. Sci., 31(10):1270–1300, 2021. doi:10.1017/S0960129521000153.
- [16] Tom de Jong and Martín Hötzel Escardó. Domain theory in constructive and predicative univalent foundations. In Christel Baier and Jean Goubault-Larrecq, editors, 29th EACSL Annual Conference on Computer Science Logic, CSL 2021, January 25-28, 2021, Ljubljana, Slovenia (Virtual Conference), volume 183 of LIPIcs, pages 28:1–28:18. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2021. doi:10.4230/LIPICS.CSL.2021.28.
- [17] Robert Dockins. Formalized, effective domain theory in coq. In Gerwin Klein and Ruben Gamboa, editors, Interactive Theorem Proving - 5th International Conference, ITP 2014, Held as Part of the Vienna Summer of Logic, VSL 2014, Vienna, Austria, July 14-17, 2014. Proceedings, volume 8558 of Lecture Notes in Computer Science, pages 209–225. Springer, 2014. doi:10.1007/978-3-319-08970-6_14.
- [18] Daniel R. Grayson. An introduction to univalent foundations for mathematicians. Bulletin of the American Mathematical Society, 55(4):427–450, 2018. doi:10.1090/bull/1616.
- [19] 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 - 5th International Conference, ITP 2014, Held as Part of the Vienna Summer of Logic, VSL 2014, Vienna, Austria, July 14-17, 2014. Proceedings, volume 8558 of Lecture Notes in Computer Science, pages 275–291. Springer, 2014. doi:10.1007/978-3-319-08970-6_18.
- [20] André Hirschowitz and Marco Maggesi. Modules over monads and initial semantics. Information and Computation, 208(5):545–564, 2010. Special Issue: 14th Workshop on Logic, Language, Information and Computation (WoLLIC 2007). doi:10.1016/j.ic.2009.07.003.
- [21] Jason Z. S. Hu and Jacques Carette. Formalizing category theory in Agda. In Catalin Hritcu and Andrei Popescu, editors, CPP ’21: 10th ACM SIGPLAN International Conference on Certified Programs and Proofs, Virtual Event, Denmark, January 17-19, 2021, pages 327–342. ACM, 2021. doi:10.1145/3437992.3439922.
- [22] J. Martin E. Hyland. Towards a notion of lambda monoid. In John Power and Cai Wingfield, editors, Proceedings of the Workshop on Algebra, Coalgebra and Topology, WACT 2013, Bath, UK, March 1, 2013, volume 303 of Electronic Notes in Theoretical Computer Science, pages 59–77. Elsevier, 2013. doi:10.1016/J.ENTCS.2014.02.004.
- [23] J. Martin E. Hyland. Classical lambda calculus in modern dress. Mathematical Structures in Computer Science, 27(5):762–781, 2017. doi:10.1017/S0960129515000377.
- [24] Krzysztof Kapulkin and Peter LeFanu Lumsdaine. The simplicial model of Univalent Foundations (after Voevodsky). Journal of the European Mathematical Society, 23(6):2071–2126, 2021. doi:10.4171/jems/1050.
- [25] J. Lambek and P. J. Scott. Introduction to higher order categorical logic, volume 7 of Cambridge Studies in Advanced Mathematics. Cambridge University Press, Cambridge, 1986.
- [26] Amélia Liao, Jonathan Coates, Reed Mullanix, et al. 1lab. URL: https://1lab.dev/.
- [27] Per Martin-Löf. An intuitionistic theory of types. Bibliopolis, 1984.
- [28] Rasmus Ejlers Møgelberg. Interpreting polymorphic FPC into domain theoretic models of parametric polymorphism. In Michele Bugliesi, Bart Preneel, Vladimiro Sassone, and Ingo Wegener, editors, Automata, Languages and Programming, 33rd International Colloquium, ICALP 2006, Venice, Italy, July 10-14, 2006, Proceedings, Part II, volume 4052 of Lecture Notes in Computer Science, pages 372–383. Springer, 2006. doi:10.1007/11787006_32.
- [29] Rasmus Ejlers Møgelberg and Marco Paviotti. Denotational semantics of recursive types in synthetic guarded domain theory. Math. Struct. Comput. Sci., 29(3):465–510, 2019. doi:10.1017/S0960129518000087.
- [30] Pierre-Marie Pédrot. Ltac2: Tactical warfare, 2019. Talk at CoqPL 2019. URL: https://popl19.sigplan.org/details/CoqPL-2019/8/Ltac2-Tactical-Warfare.
- [31] Dana S. Scott. Continuous lattices. In F. W. Lawvere, editor, Toposes, Algebraic Geometry and Logic, pages 97–136, Berlin, Heidelberg, 1972. Springer Berlin Heidelberg. doi:10.1007/BFb0073967.
- [32] Dana S. Scott. Relating theories of the -calculus. In J. P. Seldin and J. R. Hindley, editors, To H. B. Curry: Essays on Combinatory Logic and Formalism, pages 403–450. 1980.
- [33] Dana S. Scott. A type-theoretical alternative to ISWIM, CUCH, OWHY. Theoretical Computer Science, 121(1):411–440, 1993. doi:10.1016/0304-3975(93)90095-B.
- [34] Michael Shulman. Idempotents in intensional type theory. Log. Methods Comput. Sci., 12(3), 2016. doi:10.2168/LMCS-12(3:9)2016.
- [35] The Coq Development Team. The Coq reference manual – release 8.19.0. https://coq.inria.fr/doc/V8.19.0/refman, 2024.
- [36] S. N. Tronin. Abstract clones and operads. Siberian Mathematical Journal, 43:746–755, 2002. doi:10.1023/A:1016340806503.
- [37] The Univalent Foundations Program. Homotopy Type Theory: Univalent Foundations of Mathematics. https://homotopytypetheory.org/book, Institute for Advanced Study, 2013.
- [38] Arnoud van der Leer. Universal algebra, univalent foundations and the untyped -calculus. Master’s thesis, Delft University of Technology, December 2024. URL: https://repository.tudelft.nl/record/uuid:e6582866-9c0d-4a13-8eda-42c25e0deba4.
- [39] Niels van der Weide. The internal languages of univalent categories. CoRR, abs/2411.06636, 2024. doi:10.48550/arXiv.2411.06636.
- [40] Niels van der Weide, Nima Rasekh, Benedikt Ahrens, and Paige Randall North. Univalent double categories. In Amin Timany, Dmitriy Traytel, Brigitte Pientka, and Sandrine Blazy, editors, Proceedings of the 13th ACM SIGPLAN International Conference on Certified Programs and Proofs, CPP 2024, London, UK, January 15-16, 2024, pages 246–259. ACM, 2024. doi:10.1145/3636501.3636955.
- [41] Niels van der Weide and Kobe Wullaert. Rezk completions for (elementary) topoi. https://msp.cis.strath.ac.uk/types2025/abstracts/TYPES2025_paper71.pdf, 2025. Abstract presented at TYPES 2025.
- [42] Vladimir Voevodsky, Benedikt Ahrens, Daniel R. Grayson, et al. Unimath — a computer-checked library of univalent mathematics. available at http://unimath.org. doi:10.5281/zenodo.13828995.
