From signatures to monads in UniMath

The term UniMath refers both to a formal system for mathematics, as well as a computer-checked library of mathematics formalized in that system. The UniMath system is a core dependent type theory, augmented by the univalence axiom. The system is kept as small as possible in order to ease verification of it - in particular, general inductive types are not part of the system. In this work, we partially remedy the lack of inductive types by constructing some datatypes and their associated induction principles from other type constructors. This involves a formalization of a category-theoretic result on the construction of initial algebras, as well as a mechanism to conveniently use the datatypes obtained. We also connect this construction to a previous formalization of substitution for languages with variable binding. Altogether, we construct a framework that allows us to concisely specify, via a simple notion of binding signature, a language with variable binding. From such a specification we obtain the datatype of terms of that language, equipped with a certified monadic substitution operation and a suitable recursion scheme. Using this we formalize the untyped lambda calculus and the raw syntax of Martin-L\"of type theory.


Introduction
The UniMath 1 language is meant to be a core dependent type theory, making use of as few type constructors as possible. The goal of this restriction to a minimal "practical" type theory is to make a formal proof of (equi-)consistency of the theory feasible. In practice, the UniMath language is (currently) a subset of the language implemented by the proof assistant Coq. Importantly, the UniMath language does not include a primitive for postulating arbitrary inductive types. Concretely, this means that the use of the Coq Inductive vernacular is not part of the subset that constitutes the UniMath language. The purpose of avoiding the Inductive vernacular is to ease the semantic analysis of UniMath, that is, the construction of models of the UniMath language. Another benefit of keeping the language as small as possible is that it will be easier to one day port the library to a potential proof assistant specifically designed for univalent mathematics.
In the present work, we partially remedy the lack of general inductive types in UniMath by constructing datatypes as initial algebras. We provide a suitable induction principle for the types we construct, analogous to the induction principle the Inductive scheme would generate for us. This way we can construct standard datatypes, for instance the type of lists over a fixed type, with reasonable computational behavior as explained in Section 5. 1. In what follows we refer to types defined using Coq's Inductive scheme as "inductive types" and the types we construct as "initial algebras" or "datatypes".
Intuitively, datatypes are types of tree-shaped data, and inductive datatypes limit them to wellfounded trees; here we exemplify two use cases: Here option X is X together with one extra element. This is an example of a "nested datatype" (see Section 3.5).
There are two ways to characterize (or specify) inductive datatypes: either externally, via inference rules, or internally, via a universal property. The relationship between the two ways was studied in [6]. There, the authors do not ask whether (some) inductive types are derivable in univalent mathematics. Instead, they start with a basic type theory with the axiom of function extensionality, and present two extensions of that type theory by axioms postulating inductive types, in two different ways: first by axioms mimicking the inference rules, that is, by an internal variant of an external postulate, and second by axioms postulating existence of initial algebras for polynomial functors. The authors then show that those extensions are (logically) equivalent. In the present work, we are interested in an internal characterization of datatypes, as initial objects, and we construct suitable initial algebras.
An inductive datatype has to come with a recursion principle (a calculational form of the universal property) which ought to be mechanically derived together with the datatype itself. Doing this by hand on a case-by-case basis means doing similar tasks many times. For the research program that tries to avoid this "boiler plate" of multiple instances of the same higher-level principles, the name "datatype-generic programming" has been coined by Roland Backhouse and Jeremy Gibbons-nicely indicating in what sense genericity is aimed at.
In this work we focus on a particular class of datatypes that represent languages with variable binding. Those datatypes are families of types that are indexed over the type of free variables allowed to occur in the expressions of the language. Variable binding modifies the indexing type by adding extra free variables in the scope of the binder, as seen in the motivating code example LC of representations of lambda terms above.
Still within the target area of datatype-generic programming (and reasoning), but more specifically, the datatypes we focus on in the present work are canonically equipped with a substitution operation-itself defined via a variant of the recursion principle associated to the datatypes (recursion in Mendler-style [23]). This substitution satisfies the laws of the well-known mathematical structure of a monad -an observation originating in [7,9,5]. In this work, we not only construct the datatypes themselves, but also provide a monadic structure-both the operations and the laws-on those datatypes.
The datatypes representing languages with binders are specified via a notion of signature. A signature abstractly describes the shape of the trees by specifying • the type of nodes and • the "number" of subtrees of a node.
In the present work, we consider two notions of signatures, and relate them by constructing a function from one type of signatures to the other. One notion is that of a binding signature (cf. Definition 1), a simple notion of signature for which we know how to construct their associated datatypes. The other notion is that of a signature with strength (cf. Definition 4), introduced in [22]. The latter is a more general notion of signature which comes with information on how to perform substitution on the associated language (or, more generally, on any "model" of the signature-even including coinductive interpretations in form of languages with non-wellfounded legal parse trees, that, however, are not studied in the present work).
Outline of the paper. The present work is built on top of existing work. Here, we list previous work as well as work done for the present article, in order to give a coherent picture: (i). In Section 2, we construct a signature with strength from a binding signature. This involves constructing an endofunctor on the category of endofunctors on a base category C from a family of lists of natural numbers, as well as a strength (a natural transformation with extra properties) between suitable functors.
(ii). Instantiating the base category C of the previous section to Set, we construct the data type, as an initial algebra of the endofunctor on endofunctors on the category of sets that is specified by a binding signature, using just the type constructors available in the UniMath language. In particular, we do not use the Coq vernacular Inductive. This work is reported on in Section 3.
(iii). In previous work [22,3], a model ("substitution system") of a signature with strength was constructed on a hypothetical initial algebra. This construction was carried out over an arbitrary base category, which, by hypothesis, is sufficiently well-behaved. In particular, right Kan extensions were required to exist. In the present work, we base the needed scheme of generalized iteration in Mendler-style on another theorem in [10] that is based on cocontinuity assumptions instead of the existence of right Kan extensions. We apply this modified construction to the data type constructed in item (ii), where the base category is the category of sets. We hence have to provide the prerequisites for that general construction, in particular, we show that precomposition with a functor preserves colimits of any kind (while only preservation of initial objects and colimits of chains is required for the iteration scheme). This work is reported on in Section 4.1.
(iv). In previous work [22,3], a monad was constructed from any substitution system over an arbitrary base category-thus showing that the substitution constructed in (iii) satisfies widely recognized minimum requirements on substitution. The modified construction of the present work can be applied to our more specific situation without any further conceptual work, see Section 4.2.
The construction of (indexed) datatypes described in item (ii) certainly constitutes the bulk of the present work, but connecting this construction with the previous work mentioned above also required some effort. The construction is done by combining two results: • a classical category-theoretic result saying that an initial algebra of an ω-cocontinuous functor can be constructed from a colimit of a certain chain (i. e., a countably infinite linear diagram) [1]; • the constructibility of colimits in the category of sets (a.k.a. discrete types) in UniMath as a consequence of the constructibility of set quotients.
The construction of set-level quotients was done by Voevodsky [28]. It is a prime example of the new possibilities that the univalence axiom and its consequences provide for the formalization of (set level) mathematics compared to the type theories implemented by Coq or Agda without the univalence axiom.
On the way to our results, we also deepened the degree of categorical analysis, e. g., we organized the signatures with strength into a category, constructed certain limits and colimits in that category, and identified pointed distributive laws as a means to construct signatures with strength.
The results presented in this article are not surprising-it is our hope, however, that their formalization will be useful and that its underlying ideas extend to richer notions of datatypes and type families. One envisioned use of the library formalized in the present work is outlined in Section 5.2.3.

About UniMath
The UniMath language is a variation of intensional Martin-Löf type theory [21]. It has dependent function types (also called Π-types), dependent pair types (also called Σ-types), identity types and coproduct types.
There are also a few base types: the type of natural numbers, the empty type, the unit type, the type of booleans. Furthermore, we assume that all the types are elements of a universe U-for sake of simplicity, and while waiting for a satisfying universe mechanism (that supports resizing rules besides universe polymorphism), we even assume the inconsistent typing rule U : U. This means that the Coq system does not provide us with a validation of our usage of universes, although we informally claim that we do not exploit that rule in inconsistent ways (to be confirmed in future implementations). We denote by A B the type of equivalences between types A and B. For details we refer to [25,Chapter 2.4].
An important part of UniMath is the univalence axiom. This axiom characterizes the identity type on the universe U. It asserts that the type of identities between types is equivalent to the type of equivalences between those types. More precisely, it asserts that the map from identities between types to equivalences between types that is specified by sending the reflexivity to the identity equivalence is an equivalence. In the present work, we crucially use some consequences of the univalence axiom that are not provable in pure Martin-Löf type theory. Details are described in Section 5.1.
Note that the general scheme to define strictly positive inductive types and families in Coq, the vernacular Inductive, is not part of UniMath. Indeed, while the types above are, for technical reasons, implemented in UniMath using the Inductive vernacular, its use is not permitted outside a "preamble" that introduces those types. In this way we simulate a theory in which the above types are primitive rather than an instance of a general type definition mechanism. It is the purpose of the present work to construct some of the inductive types that could otherwise be defined using the Inductive scheme. 2 Consequently, the experimental Higher Inductive Types (HITs) [25] are not part of the UniMath language either.
In UniMath, types are stratified according to their homotopy level : we say that a type is contractible if it has exactly one element/inhabitant. A type is a proposition if any two of its inhabitants are identical (there need not be any inhabitant, corresponding to an unprovable proposition). A type is a set if all of its identity types are propositions. The hierarchy of homotopy levels continues with groupoids, 2-groupoids and so on, but in the present work these higher levels are not used.
We call propositional truncation a type transformation that associates to any type A the proposition A . Intuitively, A is empty when A is, and contractible otherwise. Note that propositional truncation, often implemented as a HIT, is implemented in UniMath via a universal quantification, in the style of a generalized double negation: The propositional truncation is used to turn the strong, constructive, existential quantification into a weak, classical, one: we write ∃a : A, B(a) for Σ a:A B(a) . As in [25] we use the term merely exists to denote the weaker notion of existence.
This distinction between structure and property given by the two different existential quantifiers-Σ and ∃, respectively-is also reflected in our use of the vocabulary 'Problem & Construction' vs. 'Theorem & Proof'. Indeed, whenever we describe the construction of a structure, that is, when we construct a term of a type that is not a proposition in the above sense, we use the terminology 'Problem & Construction'. The pair 'Theorem & Proof' is reserved for the construction of inhabitants of a proposition. A corner case is strong unique existence which is exactly the same as being contractible, and contractibility of a given type is a proposition, but still it comes with a construction.
The UniMath library contains a significant amount of category theory, for details see [2]. A category C in UniMath is given by: • a type C 0 of objects; • for any two objects A, B : C 0 , a set C(A, B) of morphisms; • for any three objects A, B, C : C 0 , a composition operation • for any object A : C 0 , an identity arrow 1 = 1 A : C(A, A), subject to the usual axioms of category theory. Functors, natural transformations, etc. are defined in the usual way.
The category Set has as objects sets and as morphisms from X to Y the set of (type-theoretic) functions from X to Y . Given categories C and D, we denote by [C, D] the category of functors from C to D, and natural transformations between them.
The article [2] calls "precategory" the notion here introduced as category, and reserves the word "category" for precategories with an additional property, called "univalence (for categories)". This property is not relevant for the work reported here. We will occasionally remark on what would be guaranteed in addition for a univalent base category. The category Set is univalent, and univalence is inherited from the target category D of a functor category [C, D].
In the present work we reuse the existing library of category theory and extend it as described below.

Notational conventions regarding category theory
We assume the reader to be familiar with the concepts of category theory. Here, we only point to the specific but rather standard notations and conventions we will use throughout.
Instead of writing that F is an object of the functor category [C, D], we often abbreviate this to F : [C, D], but also to F : C → D. Given d : D, we call d : C → D the functor that is constantly d and 1 d on objects and morphisms, respectively. This notation hides the category C, which will usually be deducible from the context. We write Id for the identity endofunctor on C. We also let (co)product denote general indexed (co)products and explicitly write if they are binary.
The category Ptd(C) has, as objects, pointed endofunctors on C, that is, pairs of an endofunctor F : C → C and a natural transformation η : Id → F . We write id for the identity functor with its trivial point. Let U be the forgetful functor from Ptd(C) to [C, C] (that forgets the point).
Categories, functors and natural transformations constitute the prime example of a 2-category. We write • for vertical composition of natural transformations and · for their horizontal composition. If one of the arguments to horizontal composition is the identity on some functor, we just write the functor as the respective argument. The corner case where both arguments are the identity on some functors X and Y is just functor composition that is hence written X · Y (on objects and morphisms, this is X applied after Y , hence (X · Y )(A) = X(Y A) and likewise for morphisms). Horizontal composition of µ : F → G and ν : F → G has µ · ν : F · F → G · G provided F, G : D → E and F , G : C → D. The order of vertical composition • is the same as of functor composition: if F, G, H : C → D and µ : G → H and ν : F → G, then µ • ν : F → H is defined by object-wise composition in D.
Given a functor F : [A, B] and a category C we define the functor · F on functor categories: This functor takes a functor X : [B, C] and precomposes it with F , that is, X → X · F , and likewise with the morphisms, i. e., the natural transformations. Once again the category C is hidden, but it can often be deduced from the context. We follow [3] in making explicit the monoidal structure on functor category [C, C] that carries over to Ptd(C): let α X,Y,Z : X · (Y · Z) (X · Y ) · Z, ρ X : 1 C · X X and λ X : X · 1 C X denote the monoidal isomorphisms. Notice that all those morphisms are pointwise the identity, but making them explicit is needed for typechecking in the implementation [3].

Two notions of signature
As outlined in the introduction, a signature abstractly specifies a datatype by describing the shape of elements of that type. We give two notions of signatures suitable for the description of languages with variable binding, such as the untyped lambda calculus. We first describe a rather syntactic notion of signature: binding signatures. We then proceed with a description of a semantic notion of signature: signatures with strength. We give constructions to obtain signatures with strength and finally associate a signature with strength to each binding signature.

Binding signatures
A binding signature is given by simple syntactic data that allows one to concisely specify a language with variable binding. Binding signatures are less expressive than the signatures with strength that will be presented in the next section. On the other hand, they are easier to specify.
Definition 1 (Arity, Binding signature). An arity is a (finite) list of natural numbers. A binding signature is a family of arities, more precisely, • a type I with decidable equality and • a function ar : I → List(N).
Intuitively, the type I of a binding signature indexes the language constructors, and the function ar associates an arity to each constructor. We need decidable equality on the indexing type (which, by Hedberg's theorem [16], is a set) in the proof of Lemma 44. Hypothesizing a decidable equality also makes our notion of binding signature equal to the notion used in [14].
In UniMath we define this as a nested Σ-type (with UU for the universe U): Definition BindingSig : UU := Σ (I : UU) (h : isdeceq I ), I → list nat.
We also define functions for accessing the components of a BindingSig and a constructor function for constructing one: This way we can mimic the behavior of Coq's Record types which are not part of UniMath as they are defined using Inductive.
We can take the coproduct of two binding signatures by taking the coproduct of the underlying indexing sets, and, for the function specifying the arities, the induced function on the coproduct type.
Example 2 (Binding signature of untyped lambda calculus). The binding signature of the untyped lambda calculus is given by I := {abs, app} and the arity function is This is to be read as follows: there are-besides variables that are treated generically in Section 4.1two constructors. The first constructor abs, corresponding to lambda abstraction, has just one argument (as ar(abs) is a one-element list), and this argument can make use of 1 extra variable being bound by the constructor. The second constructor app, corresponding to application, has two arguments, and there is no binding involved.
Example 3 (Binding signature of presyntax of Martin-Löf type theory). The binding signature of Martin-Löf type theory is given in Section 4.2, as part of an extended example that uses an infinite index set. Using the coproduct of binding signatures, it can easily be decomposed, in particular, using the binding signature of the untyped lambda calculus as one ingredient.

Signatures with strength
The next, more semantic, notion of signature was defined in [22,Definition 5]; there, it was merely called "signature". In order to explicitly distinguish them from binding signatures, we call them "signatures with strength" here. As a new contribution, we organize the signatures with strength as a category.
Definition 4 (Signatures with strength). Given a category C, a signature with strength is a pair (H, θ) of an endofunctor H on [C, C], called the signature functor, and a natural transformation In more detail, the bifunctors applied to a pair of objects (X, (Z, e)) with X : [C, C] and (Z, e) : Ptd(C) (X for the argument symbolized by − and (Z, e) for the argument symbolized by ∼) yield HX · Z and H(X · Z), thus θ X,(Z,e) : HX · Z → H(X · Z). By 'linearity' of θ in the second argument we mean the equations , using the monoidal isomorphism λ introduced in Section 1.2) and as illustrated by the diagram Composition and identity morphisms of signatures with strength are given by composition and identity of natural transformations. This defines the category of signatures with strength.
Examples of signatures with strength are given in [22]. Another way of producing examples is the map defined in Construction 13.
The signatures with strength do not distinguish between arities and signatures. As developed in [3], there is a way to build a new signature by taking the coproduct of two signatures. Intuitively, and just as for binding signatures, this corresponds to constructing a new language by taking the disjoint union of the language constructors of two given languages. What is new here compared to [3] is the explicitly categorical treatment (i. e., taking into account morphisms of signatures with strength). The construction generalizes easily to the coproduct of an arbitrary family of such signatures: Definition 6 (Coproduct of signatures with strength). If C has coproducts, then the coproduct of a family of signatures with strength is defined as follows: • the signature functor is given by the coproduct in the endofunctor category on [C, C] induced by that on C; • the strength is induced by coproduct of arrows.
The strength laws are simple consequences of the strength laws of each member of the family of signatures, and the universal property is readily established.
Definition 7 (Binary product of signatures with strength). If C has binary products, then the binary product of two signatures with strength has, as signature functor, the binary product of the functors of the given signatures. The strength is then induced analogously to coproducts.
By way of iteration, binary products will be used to model multiple arguments of a datatype constructor.
Definitions 6 and 7 entail that the forgetful functor from signatures with strength to endofunctors on [C, C] lifts and preserves coproducts and binary products.

Signatures with strength from binding signatures
Constructing suitable signatures with strength for a language seems like a daunting task. Fortunately, it is often sufficient to specify the binding signature. The generic solution to the following problem then yields the corresponding signature with strength.
Problem 8. Let C be a category with coproducts, binary products and a terminal object. Given a binding signature, to construct a signature with strength on C. This task is naturally divided into  Here, the functor option : C → C is defined on objects by option(A) := 1 + A. The product is implemented as an iterated binary product. Put differently, we define a functor The functor associated to the signature (I, ar) is then obtained as the coproduct of the functors associated to each arity, For the construction of this functor over the category of sets (i. e., when C = Set), it is essential for I to be a set. This is the case, as a consequence of our hypothesis of I having decidable equality as the indexing set of a binding signature.
As we have just seen, the signature functors H that arise from binding signatures are of a special shape, where the argument X only enters in the form of X · option n . This can be exploited in the construction of the strength θ for H. The right level of generality of this pattern is signature functors H that are given by precomposition with a fixed endofunctor G on C, i. e., with HX = X · G. Pointed distributive laws for G to be introduced next will lift to strengths for H, hence providing signatures with strength from a simpler input.
Definition 10 (Pointed distributive law). Let C be a category and G : where the second equation is commutation of the following diagram: Note that, in analogy with the definition of signature with strength, we symbolize the sole argument of the functors as ∼. Note that setting δ id to ρ −1 G • λ G instead of the identity would be to emphasize the monoidal structure on [C, C], but our implementation did not run into problems with our simplified definition (that, anyway, is pointwise identical).
The prime example is with G = option, where with the injections inl 1,A and inr 1,A into option(A).
The following lemma is obtained by easy calculations.
Lemma 11. Let C be a category, G : [C, C] and δ a pointed distributive law for G. Let H be precomposition with G, then as illustrated by the diagram yields a natural transformation, and (H, θ) is a signature with strength.
Also the next lemma is obtained by easy calculations.
Lemma 12. Let C be a category, G 1 , G 2 : [C, C] with pointed distributive laws δ 1 and δ 2 , respectively. Then, the following is a pointed distributive law for G 1 · G 2 : visualized as follows: Construction 13 (Part (ii) of Problem 8). Let (I, ar) be a binding signature. It suffices to define the signature with strength associated to any ar(i) for i : I. The signature with strength associated to (I, ar) is then obtained by taking the coproduct of all the signatures with strength associated to ar(i) as in Definition 6. Let i : I. Thanks to Definition 7 for binary products, used repeatedly in order to account for multiple arguments (i. e., multiple elements in the list ar(i)), it suffices to define the strength associated to the endofunctor on [C, C], expressed by the term X · option n k in the above construction. However, this is an instance of Lemma 11, with G = option n k , and the latter is an iterated composition of option for which the pointed distributive law has been given above. So, Lemma 12 provides a pointed distributive law for option n k .
Example 14 (The signature with strength for the untyped lambda calculus). Consider the binding signature of Example 2. The signature functor obtained from that binding signature via the map defined in Construction 9 is given by We also obtain a strength law for this functor by Construction 13. For more details about this see [22].
The next section is dedicated to the construction of initial algebras for the signature functor associated to a binding signature by Construction 9, culminating in Theorem 44 and Construction 46. In Section 4 we then equip those initial algebras with a monad structure.

Construction of datatypes as initial algebras
Given a category D, we define the datatype specified by a functor F : D → D to be any initial algebra of F . Note that by this definition, such datatypes are only defined up to unique isomorphism. For a given endofunctor F on D, an initial algebra might or might not exist. In this section, we construct initial algebras for signature functors as in Section 2, with D instantiated to the category of endofunctors on the category of sets, hence with category C of the previous section fixed to Set. However, the results of this section are stated and proved for arbitrary categories C equipped with suitable structure, and only instantiated to Set in the end.
Our main tool for the construction of initial algebras is Construction 27. That construction yields an initial F -algebra for an ω-cocontinuous endofunctor F from a certain colimit. It hence reduces our task of constructing datatypes (i. e., initial algebras) to the construction of certain colimits (see Section 3.3) and to showing that various functors preserve these colimits (see Sections 3.4 and 3.5).

Colimits
In our formalization, colimits are parametrized by diagrams over graphs, as suggested by [19, p. 71].

Definition 15 (Graph). A graph is a pair consisting of
• a type vertex : U representing the vertices and • a family edge : vertex → vertex → U representing the edges as a dependent family of types.
A diagram, accordingly, is a map from a graph into the graph underlying a category.
Definition 16 (Diagram). Given a graph G made of vertex G and edge G and a category C, a diagram of shape G in C is a pair consisting of Henceforth, we will abbreviate u : vertex G by u : G. These definitions are also conveniently represented in UniMath using Σ-types with suitable accessor and constructor functions: Remark 17. For conceptual economy, it is customary in category theory to index limits and colimits by categories instead of graphs, and by functors instead of diagrams. The extra structure that categories and functors have compared to graphs and diagrams is not used in what we are presenting here. However, our formalization can also be used with categories and functors, thanks to coercions from categories and functors to graphs and diagrams, respectively.
Definition 18 (Cocone). Given a diagram d of shape G in C made of dob d and dmor d , and an object C : C 0 , a cocone under d with tip C is given by • a family of equalities (u:G,v:G,e:edge G (u,v)) a(v) • dmor d (e) = a(u).
Let Cocone(d, C) be the type of cocones under d with tip C.
The equalities in the definition can be depicted as: We often omit the equalities, denoting a cocone just by its family of morphisms. This definition can be illustrated by the following diagram: Here iscontr is a predicate saying that the type is contractible, in other words that it has only one inhabitant which exactly captures the unique existence of f .
Remark 20 (Uniqueness of colimits). If C is a univalent category [2], and d is a diagram of shape G in C, then the type of colimits of d, Definition 21 (Preservation of colimits). Fix a graph G. We say that F preserves colimits of shape G if, for any diagram d of shape G in C, and any cocone a under d with tip C, the cocone F a is colimiting for F d whenever a is colimiting for d. A functor is called ω-cocontinuous if it preserves colimits of diagrams of the shape that is, diagrams on the graph where objects are natural numbers and where there is a unique arrow from m to n if and only if 1 + m = n. We refer to diagrams of this shape as chains.
Actually, in the formalization, the type of arrows from m to n is defined to be the type of proofs that 1 + m = n, exploiting the fact that the type of natural numbers is a set: Definition nat graph : graph := mk graph nat (λ m n, 1 + m = n). Note that, as preservation of colimits is a proposition, it suffices for the natural isomorphism α to merely exist for the lemma to hold.
Next, we construct colimits in the functor category from colimits in the target category: Problem 23 (Colimits in functor categories). Let C be a category, and let D be a category with all (specified) colimits of a given shape. To construct colimits of the same shape in the functor category [C, D].
Construction 24 (Solution to Problem 23). The construction of colimits in a functor category is pointwise: the colimit C of a diagram is given, at point c : C 0 , as the colimit in D of the diagram obtained by evaluating the diagram in c : C 0 .
Limits have been formalized in the same way as colimits, that is, parametrized by graphs and diagrams. We have implemented a similar construction for lifting limits to functor categories. We omit the details of the dualization.
In the formalization some (co)limits (e. g., pullbacks and pushouts) are also implemented directly, in addition to them being formalized as a colimit over a specific graph. For instance, binary coproducts are formalized as a type parametrized by two objects in a category, instead of by a diagram on the graph 2 with two objects and no non-trivial morphisms. We provide suitable maps going back and forth between the different implementations of (co)limits.
The advantage of formalizing the 'special' (co)limits as instances of general (co)limits is that results such as the lifting of (co)limits to functor categories restricts immediately to these (co)limits of special shapes.
On the other hand, the direct formulation is more convenient to work with in practice. In particular, we experienced some performance issues in the compilation of our library when we attempted to replace the direct lifting of binary (co)products to functor categories by a specialization of the general lifting of (co)limits. Those performance issues are related to a 'structure vs. property' question: the lifting should happen in such a way that the binary product of two functors F, G : C → D, evaluated in an object C : C 0 , computes (that is, is judgmentally equal) to the binary product of F C and GC, the latter of which was given by hypothesis as a structure.

Initial algebras from colimits of chains
The construction of initial algebras as colimits of chains was first described by Adámek in [1]. It is a purely categorical construction and the formalization presented no surprises.
Problem 25 (Initial algebras of ω-cocontinuous functors). Let C be a category with initial object 0, and let F : C → C be ω-cocontinuous. Let c be a colimiting cocone with tip C of the chain chn F given as follows: Equip C with an F -algebra structure α : C(F C, C) and show that (C, α) is an initial F algebra.
To motivate the solution to the problem, we recall Lambek's well-known lemma that we also formalized.
Lemma 26 (Lambek). Given F : C → C and an initial algebra (A, a) of F , then a : C(F A, A) is an isomorphism.
Proof. The inverse arrow to a is obtained as the unique algebra morphism to the algebra (F A, F a).
Thanks to this result, we are bound to find an α : C(F C, C) above that is even an isomorphism.
Construction 27 (Solving Problem 25). In order to construct an isomorphism α : F C ∼ = C, we use that we obtain an isomorphism between any two objects that are colimits for the same diagram. It hence suffices to show that F C is the tip of a colimit of the above diagram. But F C is a colimit of the diagram F chn F obtained by applying F to each object and arrow of chn F , by ω-cocontinuity of F . At the same time, the colimit of F chn F is the same as of chn F , since the colimit of a chain remains the same under the "shift" of a chain, or, more generally, under the removal of a finite prefix of a chain (this is due to the fact that the cocones can always be "completed leftwards" by pure calculation).
Given an algebra (A, a), we have to construct a cocone under chn F with tip A in order to obtain a morphism from C to A. The cocone is defined by induction on natural numbers: the morphism of index 0 is the one from the initial object. The morphism at index n + 1 is constructed by composing a with the image of that at index n under F . This forms a cocone, which induces a morphism f : C(C, A). This morphism is also a morphism of algebras from (C, α) to (A, a). Its uniqueness is a consequence of it being unique as a morphism out of the tip C of the colimit.

Colimits in Set
The construction of colimits in the category of sets we present in this section requires two consequences of the univalence axiom: function extensionality and univalence for propositions.
It is well-known that the construction of colimits can be split into the construction of coproducts and the construction of coequalizers (see [19, p. 113] for the dual situation with limits). Using this point of view, it is the construction of coequalizers that is not possible in pure Martin-Löf type theory (see, e.g., [12]) and requires the aforementioned consequences of the univalence axiom.

Set quotients in UniMath
Set-level quotients were constructed by Voevodsky in his Foundations library (which is now a part of UniMath); a brief overview can be found in [28]. None of the work described in this section is our own.
Given a type X, we call eqrel(X) the type of equivalence relations R : X → X → Prop, that is, reflexive, symmetric, and transitive relations. For such an equivalence relation R, the set quotient X/R, together with the canonical surjection pr : X → X/R, has the following universal property: for any set S and map f : X → S such that R(x, y) implies f (x) = f (y), there is a unique map f : X/R → S such that the following diagram commutes. The construction Voevodsky gives of the set quotient X/R in terms of equivalence classes of R, uses function extensionality and univalence for propositions.

Construction of colimits in Set
The goal of this section is a solution to the following problem: Set). Given a graph G and a diagram d of shape G in Set, to construct the colimit of d.

Problem 28 (Colimits in
Construction 29 (Solution to Problem 28). The tip of the colimit of d is given by with ∼ being the smallest equivalence relation containing the relation ∼ 0 , defined by The colimiting cocone under C is given by composing the projection pr with the injection maps C d(u), (v:G) dob d (v) . The fact that the family of maps thus obtained constitutes a cocone makes use of the equivalence of (3.1). The (unique) map to any cocone is obtained by the universal property of the set quotient. Showing uniqueness of that map makes use of the fact that the projection is surjective, and hence an epimorphism in the category of sets.
Note that in the above formula, we use the truncated existential ∃ instead of the proof-relevant Σ. This is necessary in order to give ∼ 0 the target type Prop, and hence to apply the construction of quotients described in Section 3.3.1.
Note also that for the above construction to be correct, we need the type of vertices of G to be small. In the present work, we are ultimately interested in colimits of chains, that is, of diagrams where the set of vertices is given by the set of natural numbers-a small set.
In order to construct the smallest equivalence relation containing a relation R 0 , we need to close R 0 under reflexivity, symmetry and transitivity: Definition 30. Let R 0 : X → X → Prop be a relation on a type X. Its closure is defined to be the relation x ∼ y given by x ∼ y := R:eqrel(X) Here, we denote by R 0 ⊆ R that R 0 (x, y) implies R(x, y) for any x, y : X. Note that this definition requires impredicativity for h-levels: the fact that x ∼ y : Prop is a consequence of R(x, y) being a proposition for any equivalence relation R. We do not worry about the universe level of the relation ∼.
Lemma 31. The relation defined in Definition 30 is the smallest equivalence relation containing R 0 .
Proof. Minimality is direct by the impredicative definition; ∼ is itself an equivalence relation because equivalence relations are closed under arbitrary intersections.

Functors preserving colimits
In this section, we prove results on functors preserving colimits, in particular colimits of chains. The first is a classical result about preservation of colimits by left adjoints [19, p. 119].
Lemma 32. If F : C → D is a left adjoint with right adjoint G : D → C, then it preserves colimits.
Proof. Call ϕ the (natural) family of isomorphisms ϕ C,D : D(F C, D) C(C, GD) of the adjunction. We omit the subscripts in what follows. Given a colimiting cocone (a i ) i:I with tip L for some diagram d, we need to show that the right-hand cocone is colimiting for the diagram F d. The last equivalence is given by hypothesis for the cocone ϕ(e i ) i with tip GM : In what follows we write C I for the I-indexed product category of a category C.
(ii). Any constant functor d : C → D preserves colimits of chains.
(iii). If C has specified products, the diagonal functor ∆ : C → C I mapping an object X to the constant I-indexed family X i:I preserves colimits.
(iv). If C has specified coproducts, the functor : C I → C, mapping I-indexed families of X i to their coproduct, preserves colimits.
Proof. The points (i) and (ii) are direct. The other two points follow by Lemma 32. Indeed, under the assumptions specified in each case we have adjunctions:

∆ Π
where Π : C I → C is the functor that maps I-indexed families of X i to their product.
Note that point (ii) is only stated for chains, that is because it is in general not true that constant functors preserve colimits.
The next results state that various functors preserve cocontinuity of all kinds. By this, we mean that if the input functors preserve colimits of shape G for a graph G, then so does the output functor, in particular, this yields preservation of ω-cocontinuity (which does not follow from preservation of cocontinuity).

Lemma 34 (Examples of preservation of cocontinuity).
(i). The composition of two functors preserves colimits of a certain kind, if the input functors do.
(ii). Given a family of functors F i : C → D indexed by i : I, where I has decidable equality. If all the F i preserve colimits of a certain kind, then the functor F i i:I : C I → D I preserves colimits of that kind.
Proof. The first point is direct. For (ii) we sketch the binary case. We first prove that the projection functors preserves colimits. For the first projection, π 1 : C 2 → C, we are by assumption given a colimiting cocone and need to show that the cocone (a i ) with tip L is colimiting. Given a cocone (a i ) with tip X this can be illustrated constructing the map f in: and by assumption obtain a unique map from (L, M ) to (X, M ). This then gives us the desired map f : L → X.
The proof that the second projection functor preserves colimits is analogous. For this we need to construct a cocone over (L, X) from a cocone (b i ) with b i : B i → X instead. Decidable equality on I is needed for the general case for proving that π i : C I → C preserves colimits. Indeed, we need to be able to decide equality on the indices to construct the cocone whose tip contains X at index i.
Using that the projection functors preserve colimits it is direct to show that F 1 , F 2 : C 2 → D 2 preserves colimits of a certain kind if F 1 and F 2 do so. Given a colimiting cocone (A i , B i ) with tip (L, M ) we obtain the colimiting cocones (A i ) with tip L and (B i ) with tip M by the above proofs. As F 1 and F 2 preserve colimits we get that ( It was quite cumbersome to formalize the proof of point (ii) above as we needed to define cocones where the type of the tips depends on the decidable equality of I. The interested reader may consult the formalization for details.
Using what we have defined so far we can define the coproduct of an I-indexed family of functors F i : C → D by: On an object X this functor acts by: Being the composition of (ω-)cocontinuous functors this is also (ω-)cocontinuous. We now turn our attention to the binary version of the product functor, which we denote by × : C 2 → C. In order to show that this functor is ω-cocontinuous we need more structure on the category C.
Definition 35 (Exponentials). Let C have specified binary products. An exponential structure for C is, for any A : C 0 , a right adjoint for the functor A × − given on objects by X → A × X. Given an exponential structure on C, we denote the right adjoint of A × − by (−) A . That is, on objects it acts as B → B A .
Example 36. The exponential structure on the category Set is given, for the functor A × −, by the functor given on object B by B A = A → B.
The functor − × A is defined analogously for each A : C 0 . The functors A × − and − × A are naturally isomorphic, so if one of them has a right adjoint the other does as well. Hence the choice of which argument is fixed in Definition 35 is not crucial. The following lemma is another instance of Lemma 32: Lemma 37. Let C have (specified) binary products and exponentials, and let A : C 0 . The functors A × − and − × A preserves colimits.
Only the next result is specifically about ω-cocontinuity. A search for existing proofs of this theorem in the literature only revealed a sketch in an online resource [24]; however, we have not found a precise proof of it. Here, we give a direct proof of this theorem. While the proof idea is simple, writing out all the details in the formalization is quite complicated. Our outline here is not more detailed than the one in [24], but we have the advantage of being able to refer to the formalization for details.
Theorem 38. Let C be a category with specified binary products such that A × − and − × B are ω-cocontinuous for all A, B : C 0 . Then the functor × : C 2 → C is ω-cocontinuous.

Proof. Given a diagram
with colimit (L, R) (we omit the cocone maps), we need to show that L × R is the colimit of To this end, we consider the grid The idea is to first take the colimit in each column, and then to take the colimit of the chain of colimits thus obtained. In slightly more detail, by hypothesis, the colimit of the ith column is given by A i × R. This gives rise to a chain A i × R → A i+1 × R, the limit of which is given by L × R. The difficult part of the proof is actually the handling of the arrows involved, something we completely omitted in this sketch.
Note that if C has exponentials then the conditions of the lemma are fulfilled. Hence it applies in particular to Set, or any other cartesian closed category.
Using what we have defined so far, it is possible to construct many datatypes, for example lists or binary trees over sets.
Example 39 (Lists of sets). Lists over a set A can be defined as the initial algebra of the following endofunctor on Set (using our notation for constant functors): which, when evaluated at a set X, is L A (X) = 1 + A × X. In UniMath this is written as: Definition L A : omega cocont functor HSET HSET := '1 + 'A × Id.
Here HSET is the category Set. This definition directly produces an ω-cocontinuous functor by exploiting the Coq notation mechanism and the packaging of functors with a proof that they are ω-cocontinuous.
By Construction 27 this has an initial algebra consisting of µL A : Set (representing lists of A) and a morphism α : L A (µL A ) → µL A . If we expand the type of the morphism we get α : 1 + A × µL A → µL A and by precomposing with the injection maps into the coproduct we obtain: We write nil for nil map tt of type µL A (here tt denotes the canonical element of the terminal set 1) and cons for the curried version of cons map whose type is A → µL A → µL A . As their names indicate, they correspond to the standard constructors for lists where nil is the empty list and cons adds an element to the front of a list.
Given a set X, an element x : X and a function f : A × X → X we can construct another Lalgebra by (X, [λ .x, f ]) where [λ .x, f ] is the coproduct of the constant map to x with f and hence of type 1 + A × X → X. By initiality of (µL A , α) we get an L-algebra morphism foldr : µL A → X satisfying: By precomposing with the injection maps this commutative diagram gives us the equations: foldr nil = x foldr (cons y ys) = f (y, foldr ys) These are the usual computation rules (modulo currying and implicit arguments) of the foldr function as defined, for example, in Haskell. Hence this defines a recursion principle. We can also obtain an induction principle: Lemma listIndhProp (P : List → hProp) : P nil → (Π a l , P l → P (cons a l )) → Π l , P l .
Using all of this we can define standard functions on these lists, for example map and length, and prove some of their properties: Definition length : List → nat := foldr natHSET 0 (λ (n : nat), 1 + n).
Note that the foldr function in the formalization takes a curried function as opposed to the one above.
The computation rules for these lists do not hold definitionally, this make them a little cumbersome to work with as one has to rewrite with the equations above explicitly instead of letting Coq do the simplifications automatically. This is discussed further in Section 5.1.
We have also defined binary trees analogously to lists as the initial algebra of the functor that maps X to 1 + A × X × X. It is hence possible to introduce various homogeneous datatypes using what has been developed so far.
For nested datatypes, such as the introductory example of lambda terms, we can just try to use [C, C] instead of the base category C. While this is the right solution in principle, there are some technical details to be addressed to make this work. This is done in the next section which allows us to define heterogeneous nested datatypes representing syntax of languages with binders.

The datatype specified by a binding signature
In the introduction, we showed the motivating code example of a representation of lambda terms by the family LC of types that we qualified as nested datatype, a name due to [8]. In general, nested datatypes are datatypes that consist of a family of types that are indexed over all types and where the constructors of the datatype relate different family members. The homogeneous lists are indexed over all types, but are no nested datatype since each list X can be understood individually, while LC has the constructor Abs that relates representations of lambda terms with different sets of free variables. Being indexed "over all types" needs to be specified properly. For us, it means that the indexing parameter of the family runs through the objects of the same category C that serves to represent the family members. In particular, there is no inductive definition of a suitable maximal indexing set, such as the natural numbers to represent a countably infinite supply of "fresh" variable names.
From the point of view of category theory, nested datatypes are endofunctors on a category C that arise as fixed points (up to isomorphism) of endofunctors on [C, C]. In the present work, we exclusively study fixed points given by initial algebras. We do not insist on the datatype to be truly a nested datatype in the above sense of relating different family members through the constructors. Nonetheless, we want to capture the general situation where indices of family members in the arguments of datatype constructors are calculated by an arbitrary functor F . As illustrated in Example 14, this calculation is done by using precomposition with that functor, in the example with F = option that represents "context extension". Indeed, looking at the example, we see that variable binding is indicated by a summand in the signature functor that maps an endofunctor X to X · option.
So, in order to construct nested datatypes in our setting, we would like to show that functors on functor categories of the form · F : : [A, B]) are ω-cocontinuous, i.e., preserve colimits of chains. Ultimately, we are interested in the case where A = B = C, but we prove a more general theorem below.
First, we need some auxiliary results.
Lemma 40. Let G be a graph and D be a diagram of shape G in C. Given two cocones with tips C and C , respectively, such that the cocone with tip C is colimiting, then the cocone with tip C is colimiting if and only if the induced morphism from C to C is an isomorphism.
Theorem 41. Fix a graph G and assume C has colimits of shape G. Given a diagram D of shape G in the functor category [A, C] and a cocone with tip F , then this cocone is colimiting if and only if for any object A : A 0 the "pointwise" cocone with tip F A is colimiting for the pointwise diagram DA in C.
Proof. In the proof we only mention the tip F of the cocone, but formally we have to handle the whole cocone. First, suppose that F is a colimit. For any A : A 0 , we have the colimit, say F A, of DA in C. Via Construction 24, the pointwise colimits F A yield a functor F that is a colimit of D. Since both F and F are colimits of D, we obtain an isomorphism F ∼ = F by Lemma 40, and hence an isomorphism F A ∼ = F A for any A : A 0 . Since F A is a colimit for DA, so is F A.
On the other hand, suppose that F A is a colimit of DA for any A : A 0 . Lifting those colimits to the functor category, we obtain a functor F , that is definitionally equal to F on objects, and that is a colimit of D. The induced natural transformation from F to F is an isomorphism F ∼ = F that is pointwise the identity. By Lemma 40, since F is a colimit of D, so is F .
Using this we can now prove the main technical contribution of this section.
Theorem 42 (Precomposition functor preserves colimits). Fix a graph G and suppose C has specified colimits of shape G. Let F : A → B be a functor, then the functor · F : [B, C] → [A, C] preserves colimits of shape G.
Proof. Let D be a diagram of shape G in [B, C], and let C be its colimit. We need to show that C · F is the colimit of the diagram G · F in [A, C]. By Theorem 41, it suffices to show that for any A : A 0 , the object (C · F )A ≡ C(F A) is a colimit of (G · F )A ≡ G(F A) in C. By the other implication of Theorem 41, instantiated to F A, this is indeed the case.
Example 43. Putting together results 42, 33(iv) in the binary case, 34(ii), and 38, we obtain that the functor for the untyped lambda calculus of Example 14 defined on objects as is ω-cocontinuous, being the composition of ω-cocontinuous functors. Hence initial algebras can be constructed for it by Construction 27. Note that we have not taken into account the variables yet. This will be done below.
More generally, any signature functor over a category C obtained from a binding signature via Construction 9 preserves colimits of chains: Theorem 44. Let C be a category with coproducts, products, and colimits of chains such that F × − is ω-cocontinuous for every F : C → C. Then, the signature functor over C associated to a binding signature via Construction 9 is ω-cocontinuous.
By Lemma 32, the last requirement on C is satisfied if C has exponentials, thus the theorem applies to C = Set. We also remark that the theorem uses the lifting of colimits to functor categories (Construction 24).
The binding signatures studied in Section 2 are incapable of expressing that the free variables in the language are considered as legal expressions, as we will argue now. Had we also var : I in Example 2, any element of ar(var) would mean a lambda-term as argument to the constructor, and if ar(var) were the empty list, this would generate one constant only. On the level of signature functors, however, we just have to replace the H found by Construction 9 by Id + H. Indeed, for any (Id + H)algebra (T, α), the natural transformation α : Id + HT → T decomposes into two [C, C]-morphisms η : Id → T , τ : HT → T defined by η = α • inl Id,HT and τ = α • inr Id,HT .
In case (T, α) is an initial algebra, the first component η can then be considered as the injection of variables into the well-formed expressions, i. e., for every object C : C, η C : C → T C injects C as "variable names" into T C, the "terms over C". The second component τ represents all the other constructors of T together, hence those specified by the binding signature we started with.
Definition 45. The datatype specified by a signature functor H over C (and hence by a binding signature) is given by an initial algebra of Id + H.
Combining Theorem 44 with Adámek's Theorem (Construction 27), we obtain Construction 46 (Datatypes specified by binding signatures). Let C be a category with coproducts, products, and colimits of chains such that F × − is ω-cocontinuous for every F : C → C. For any binding signature (I, ar), construct the ω-cocontinuous signature functor H. Then, Id + H is ωcocontinuous. Construct the datatype over C as initial algebra of the latter functor, where we get the required colimiting cocone of Construction 27 from C having specified colimits of chains. In particular, denoting the carrier of the algebra by T : C → C, this yields η : Id → T , τ : HT → T such that [η, τ] is an isomorphism.
Once again, for C = Set, the prerequisites of the construction are met, in particular thanks to the construction of colimits in the category of sets (Construction 29).

From binding signatures to monads
In this section we combine the results of the previous sections with the construction of a substitution operation on an initial algebra in order to obtain a "substitution" monad from a binding signature. We end the section with two examples: the untyped lambda calculus and a variation of Martin-Löf type theory.

A substitution operation on the datatype of a binding signature
The results of the previous section permit the construction of initial algebras of signature functors. The purpose of this section is to construct a substitution operation on such initial algebras. To this end, we apply Theorem 48 (a variant of a theorem from previous work, stated below) to our specific situation. The goal of this section is hence to recall the previous results and discuss some necessary modifications.
Even if not only initial algebras are considered (e. g., one might aim at inverses of final coalgebras to model coinductive syntax, as was one of the motivations for [22]), the following abstract definition of the existence of a substitution operation makes sense.
The following is a variant of a theorem from [22], formalized in [3]. The original theorem required the existence of a right adjoint for the functor ·Z : [C, C] → [C, C] for every Ptd(C)-object (Z, e). The present variant replaces that hypothesis on right adjoints by suitable assumptions on ω-cocontinuity.
Theorem 48 (Construction of a substitution operation on an initial algebra). Let C be a category with initial object, binary coproducts and products, and colimits of chains. Let (H, θ) be a signature over base category C. If H is ω-cocontinuous, then an initial (Id + H)-algebra can be constructed via Construction 27, and this initial algebra is a heterogeneous substitution system for (H, θ).
The proof is done by generalized iteration in Mendler-style (in the category-theoretic form introduced by [10, Theorem 1]), both for the existence and the uniqueness of f . Here, unlike in the previous work [22,3], the initial algebra has to come from ω-cocontinuity of the signature functor. The previous condition on existence of the right adjoint in the theorem would not allow to apply it to the category Set.
Theorem 49 (Matthes and Uustalu [22], formalized in [3]). (Construction of a monad from a substitution system) Let C be a category with binary coproducts and (H, θ) a signature with strength over base category C. If (T, α) is an hss for (H, θ), then T , together with the canonically associated η : Id → T as unit and 1 (T,η) : T · T → T as multiplication, form a monad.
Functional programmers normally do not consider monad multiplication when studying monads but rather the operation called bind. It is well-known that the formulations of monads with unit and multiplication and those with unit and bind are equivalent. Given A, B : C and a substitution rule f : A → T B, the effect of a parallel substitution with f , is then 1 (T,η) B • T f : T A → T B, which is the bind operation for argument f . For C = Set, this just means that, for an argument t : T A, each free variable occurrence of a variable a : A in t is replaced by the term f a : T B. The monad laws then become conditions for substitution, and they are guaranteed by the theorem.

Binding signatures to monads
We now recall the results presented in the paper and explain how to combine them in order to obtain a monad from a binding signature.
Let C be a category with binary products and coproducts. Let (I, ar) be a binding signature, by constructions 9 and 13 we obtain a signature with strength (H, θ). In UniMath: Definition BindingSigToSignature (TC : Terminal C ) (sig : BindingSig) (CC : Coproducts (BindingSigIndex sig) C ) : Signature C hsC .
Note that we here require that C has both binary and I-indexed coproducts, we could instead assume that C has all indexed coproducts (as in the statement of Problem 8).
Here constprod functor1 F denotes the functor that sends G to F × G. Construction 27 allows us to construct an initial algebra for Id + H under suitable hypotheses on C: Definition SignatureInitialAlgebra (IC : Initial C ) (CLC : Colims of shape nat graph C ) (H : Signature C hsC ) (Hs : is omega cocont H ) : Initial (FunctorAlg (Id H H )).
By Theorem 48 we then obtain an initial heterogeneous substitution system: We see that the category C needs to have binary coproducts and products, initial and terminal objects, colimits of chains, I-indexed coproducts and products, and the functor G → F × G has to be ω-cocontinuous. All of the assumptions on C are satisfied by Set. In the formalization we have implemented special functions instantiated with Set taking fewer arguments, in particular: Definition BindingSigToMonadHSET : BindingSig → Monad HSET.
We end by showing how the framework developed in this paper can be used to conveniently obtain monads from binding signatures for two well-known languages.
Example 50 (Untyped lambda calculus). As explained in the beginning of the paper the binding signature for the untyped lambda calculus is given by I := {app, abs} and the arity function In UniMath we implement this as a bool-indexed family: From this we obtain a signature with strength: Definition LamSignature : Signature HSET has homsets HSET := BindingSigToSignatureHSET LamSig.
Using this we can add variables in order to get a representation of the complete syntax of the untyped lambda calculus. We also get an initial algebra from this functor by Construction 27: Definition LamFunctor : functor HSET2 HSET2 := Id H LamSignature.
Here HSET2 is notation for [Set, Set]. Using this we can define constructors and propositional computation rules as for lists. We omit these due to space constraints but the interested reader can consult the formalization. Finally we also get a substitution monad: Definition LamMonad : Monad HSET := BindingSigToMonadHSET LamSig.
Example 51 (Raw syntax of Martin-Löf type theory). We have also implemented a more substantial example: the raw syntax of Martin-Löf type theory as presented in [20]. This syntax has Π-types, Σ-types, coproduct types, identity types, finite types, natural numbers, W-types and an infinite hierarchy of universes. See Table 1 for a summary of this language.
Because there are both infinitely many finite types and universes the syntax has infinitely many constructors. This is the reason why we above consider families of lists of natural numbers and indexed coproducts. Note that all of the operations take finitely many arguments which is why we don't need to also consider infinite arities and indexed products. Here the function three rec a b c performs case analysis and returns one of a, b or c. We then combine all of these binding signatures by taking their sum: Definition MLTT79Sig := PiSig ++ SigmaSig ++ SumSig ++ IdSig ++ FinSig ++ NatSig ++ WSig ++ USig.
Finally we also obtain a substitution monad on Set for this language: Definition MLTT79Monad : Monad HSET := BindingSigToMonadHSET MLTT79Sig.

Conclusions
We have formalized some classical category-theoretic results on the construction of initial algebras, as well as on cocontinuity of functors. Maybe surprisingly, the formalization of results yielding ωcocontinuous functors as input to the construction of initial algebras proved to be much more difficult than the construction of colimits in Set as output of that theorem. Our formalization has been integrated into the UniMath library. Statistics related to the contributions of this paper have been summarized in Table 2. 3 The first three columns show lines of code and the last two show the number of vernacular commands. Our datatypes come with a recursion principle, defined via the universal property of the datatype as an initial algebra. This recursion principle allows us to define maps such as foldr for lists. Those maps satisfy the usual computation rules judgmentally, provided that (i). the output type is one of the predefined types of UniMath; and (ii). the computation is done lazily.
An instance of this is the length function for lists, the output type of which is the type nat of natural numbers, defined as an inductive Coq type. Maps whose output type is a datatype constructed via our framework do not compute to a normal form. An example of such a map is the function concatenating two lists into one list. Trying to compute the normal form of such a concatenated list leads to memory exhaustion. However, we can still reason about such maps by rewriting, that is, by replacing computational steps by a suitable lemma stating this step as a propositional equality. This is precisely how many recursive maps are handled in SSReflect [15]. There, computation of recursive maps is deliberately blocked for efficiency reasons in order to avoid too much unfolding. Instead, computation steps are simulated by applying suitable rewriting lemmas. This indicates that the lack of a computable normal form for the inhabitants of our datatypes is not an obstacle for mathematical reasoning about the maps that we define on those datatypes.
In the proofs and constructions presented here, the univalence principle is only used in a restricted form: • function extensionality, a consequence of univalence, is used in many places; • the construction of set-level quotients by Voevodsky makes use of the univalence principle for propositions: two propositions are equal when they are logically equivalent. Consequently, our construction of colimits in the category of sets also depends on the univalence axiom for propositions.
An alternative to the use of these axioms (by admitting the univalence axiom) would have been to work with setoids. There, the idea is to abandon the identity type; instead, each type comes equipped with its own equivalence relation, reflecting the intended "equality". This would have been extremely cumbersome, since in that case, one needs to postulate respectively prove that any operation respects the equivalence in the source and target. For the identity type, on the other hand, this respectfulness is automatic. Another alternative would be to work in a system where these are provable, and hence not axioms anymore, like Cubical Type Theory [13]. The additional judgmental equalities in such a system could potentially simplify some proofs, but that needs to be studied further.

Future work
In this section, we lay out some plans for future work and connections to other work.

Initiality for the constructed monad
As illustrated in Section 4.2, we have formalized a mechanism that, when provided with a binding signature, yields the associated "term monad" and a suitable recursion principle for defining maps from the term monad to other (families of) sets. This recursion principle stems from the universal property of initiality that the functor underlying the monad satisfies. However, the constructed monad itself has not, in the present work, been equipped with a universal property.
Hirschowitz and Maggesi [17,18] equip the term monad of a signature S with a universal property by considering a category of representations of a given signature. A representation of S is given by any monad T and a family of module morphisms of suitable type over T . We should be able to formalize Hirschowitz and Maggesi's initiality theorem using the monad we have constructed in the present work.

Generalization to multi-sorted binding signatures
The notion of binding signature considered in this paper does not incorporate a notion of typing. Suitable generalizations to typed (or multi-sorted) signatures have been considered, for instance, in [4]. In general, a multi-sorted signature contains not only information about the number of bound variables, but also of their types. Furthermore, it specifies an output type for each constructor. Multi-sorted binding signatures allow to specify languages such as the simply-typed lambda calculus and PCF (Dana Scott's language for "Programming Computable Functions"). We are currently working on extending our notions of signature, as well as the construction of initial algebras, to the multi-sorted setting.

Connection to Voevodsky's C-systems
Voevodsky is currently considering Cartmell's contextual categories [11], under the name of "Csystem", for a mathematical description of type theories (see, e.g., [27,26]). In particular, one of Voevodsky's goals is to give a precise construction of the C-system formed by the syntax of a given type theory. One step of this construction is given in [26], where he constructs a C-system from a pair of a monad on Set and a module over that monad with values in Set. Such a pair can be constructed from a monad on Set 2 and a choice of a set.
It is our goal to formalize this construction in UniMath, and to apply it to the term monads of 2-sorted signatures obtained via the generalization envisioned in Section 5.2.2. We will thus obtain, for any suitable 2-sorted signature, a C-system of raw syntax of that signature.