A Note on Generalized Algebraic Theories and Categories with Families

We give a new syntax independent definition of the notion of a generalized algebraic theory as an initial object in a category of categories with families (cwfs) with extra structure. To this end we define inductively how to build a valid signature $\Sigma$ for a generalized algebraic theory and the associated category of cwfs with a $\Sigma$-structure and cwf-morphisms that preserve this structure on the nose. Our definition refers to uniform families of contexts, types, and terms, a purely semantic notion. Furthermore, we show how to syntactically construct initial cwfs with $\Sigma$-structures. This result can be viewed as a generalization of Birkhoff's completeness theorem for equational logic. It is obtained by extending Castellan, Clairambault, and Dybjer's construction of an initial cwf. We provide examples of generalized algebraic theories for monoids, categories, categories with families, and categories with families with extra structure for some type formers of dependent type theory. The models of these are internal monoids, internal categories, and internal categories with families (with extra structure) in a category with families.


Introduction
Generalized algebraic theories (gats) were introduced by Cartmell in his PhD thesis [6] as a dependently typed generalization of many sorted algebraic theories. Each gat is specified by a signature with (possibly infinite) sets of sort symbols, operator symbols, and equations. Cartmell's definition of gats [6,7] is based on a notion of derived rule expressed in terms of a traditional syntactic system for dependent type theory. He also defines a notion of model whereby sort symbols are interpreted as families of sets.
Categories with families (cwfs) [13] were introduced as a new notion of model of dependent type theory. Cwfs arise by reformulating the notion of category with attributes (cwa) in Martin Hofmann's sense [15]. The key point is to make it clear that cwfs arise as models of a certain generalized algebraic theory closely related to Martin-Löf's substitution calculus [18]. As such the notion of cwf becomes a useful intermediary between traditional syntactic systems for dependent type theory and a variety of categorical notions of model.
The gat of cwfs is thus a kind of idealized formal system of dependent type theory. In contrast to Martin-Löf's substitution calculus, and other syntactic systems for dependent type theory, it is not formulated in terms of grammars and inference rules for the forms of judgment of type theory. Instead it is formulated in terms of sort symbols (corresponding to the judgment forms), operator symbols (corresponding to the formation, introduction, and elimination rules), and equations (corresponding to the equality rules for the type formers). Some of the general reasoning (about equality, substitution, and assumptions) is taken care of by the underlying machinery of dependent type theory. This makes it possible to abstract away from some of the details in the formulation of grammars and inference rules. In contrast to the various syntactic systems, we would like to argue that the gat of cwfs appears to be "canonical". We may define dependent type theory in a syntax independent way as the initial object in a category of cwfs with extra structure for interpreting the type formers.
In this note we explore the interdependence between gats and cwfs. The main novelty is a syntax independent definition of gats in terms of cwfs with extra structure. We define a new finitely presented notion of generalized algebraic theory and simultaneosly a general categorical notion of model. We define what it means to be a valid signature Σ for a gat and the associated category CwF Σ with extra structure for Σ. This definition refers to uniform families of contexts, types, and terms in CwF Σ , a purely semantic notion. Afterwards, we construct initial objects T Σ ∈ CwF Σ by extending Castellan, Clairambault, and Dybjer's construction of an initial object in the category CwF of cwfs [8,9].
Plan of the paper. In Section 2 we define the category CwF of categories with families and morphisms preserving cwf-structure on the nose. Section 3 contains our main definition of a syntax independent notion of valid signature Σ for a gat and the category CwF Σ of cwfs with a Σ-structure. In Section 4 we construct an initial object T Σ in CwF Σ . In Section 5 we show several examples of gats: for monoids, categories, cwfs, and cwfs with extra structure for one universe. We point out that cwfs with extra structure for gats of monoids, categories, cwfs are cwfs with an internal monoid, category, and cwf, respectively. We also sketch how to extend our approach to some countably presented gats, and show the example of contextual cwfs, a variant of Cartmell's contextual categories [6,7]. Finally, in Section 6 we discuss related work, for example relating to Voevodsky's initiality conjecture [21] and Altenkirch and Kaprosi's quotient inductive-inductive types [3].
All our development can be formulated in a constructive set theory, as described for instance by Aczel [2], though the set theory we use for formulating the notion of cwf with a Σstructure is probably much weaker. As emphasized by Voevodsky [21], we study structures invariant under isomorphisms and not under equivalences, and it is actually misleading to call them "category" (and this is why Voevodsky used the term "C-system" for what Cartmell called "contextual category"). As he also noticed, this important distinction between categories and notions invariant under isomorphisms becomes precise in the setting of univalent foundations where not all collections of objects are constructed from sets.

Categories with families
2.1. The category of cwfs and strict cwf-morphisms. Definition 1 . Fam is a category whose objects are set-indexed families of sets, denoted as (U x ) x∈X . A morphism of Fam with source (U x ) x∈X and target (V y ) y∈Y consists of a reindexing function f : X → Y together with a family (g x ) x∈X of functions g The next step is to define the category CwF. We split this definition in two: first the objects, which are called categories with families, in Definition 2, and then the morphisms in Definition 3. Since CwF has been developed as a categorical framework for the semantics of type theory, much of the terminology (contexts, substitutions, types, terms) refers to the syntax of type theory, suggesting the intended interpretation of this syntax in the so-called CwF-semantics.
The main novelty of this note is to use CwF as a framework to define a new notion of a generalized algebraic theory. Contexts, substitutions, types, and terms also make sense in relation to gats.

Definition 2 .
A category with families (cwf ) consists of the following data: • A terminal object 1 ∈ C, and unique maps Γ ∈ C(Γ, 1) for all objects Γ of C; • Operations . , , , p and q explained in the following paragraphs. These four operations and their associated equations are referred to as context comprehension.
We let Γ, ∆, . . . range over objects of C, and refer to them as contexts. We let δ, γ, . . . range over morphisms, and refer to them as substitutions. We refer to 1 as the empty context; the terminal maps Γ represent the empty substitutions.
If T (Γ) = (U x ) x∈X , we write Ty(Γ) for the set X. We call the elements of Ty(Γ) types in context Γ, and let A, B, C range over such types. Furthermore, for A ∈ Ty(Γ), we write Tm(Γ, A) for the set U A and call the elements of Tm(Γ, A) terms of type A in context Γ.
For γ : ∆ → Γ, the functorial action of T yields a morphism consisting of a reindexing function [γ] : Ty(Γ) → Ty(∆) referred to as substitution in types, and for each A ∈ Ty(Γ) a function [γ] : Tm(Γ, A) → Tm(∆, A[γ]), referred to as substitution in terms. Now we turn to the explanation of the operations . , , , p, q. Given Γ ∈ C, A ∈ Ty(Γ), γ : ∆ → Γ, and a ∈ Tm(∆, A[γ]), we have We call Γ.A the extended context and γ, a A the extended substitution. The operations . , , , p, q satisfy the following universal property: γ, a A is the unique substitution satisfying We refer (colloquially) to p as the first projection, and to q as the second projection. A cwf is thus a structure (C, 1, , T, . , , , p, q), subject to equations, for the category and the presheaf, and universal properties, formulated purely equationally, for the terminal object and for context comprehension. The morphisms to be defined next preserve this structure, even in a strict way, 'on the nose'. We often shorten the notation of a cwf to (C, T ), or even just C, leaving the remaining structure implicit. Since F nat is a natural transformation between Fam-valued presheaves, F nat has a component for any object Γ of C, and these components are morphisms in Fam(T C (Γ), T D (F fun (Γ))). Recall that morphisms in Fam consist of a reindexing function and a family of functions. It is convenient to denote F fun , all reindexing functions, as well as all members of the families of functions, simply by F . Thus we have F (A) ∈ Ty D (F (Γ)) and F (a) ∈ Tm D (F (Γ), F (A)), for all Γ and A ∈ Ty C (Γ) and a ∈ Tm C (Γ, A).
Naturality of F nat amounts to preservation of substitution, i.e., for all γ : Last but not least, we turn to the preservation of context comprehension on the nose, and require Note that the universal property implies that F ( γ, a ) = F (γ), F (a) . The same is true for the terminal maps: Small cwfs with strict cwfs-morphisms form a category, written CwF.

Signatures and models of generalized algebraic theories
We now come to the main point of this note. We define how to build a valid gat signature Σ and the associated category CwF Σ of cwfs with a Σ-structure. Each object of CwF Σ is a cwf with extra structure and each morphism is a cwf morphism preserving this extra structure. For this definition, we will need the following auxiliary notions.
A uniform family of contexts is a family of contexts Γ = (Γ C ) for each C ∈ CwF Σ such that F (Γ C ) = Γ D for all morphisms F ∈ CwF Σ (C, D). If Γ is such a family, a uniform family of types over Γ is a family of types A = (A C ) with A C type over Γ C and F (A C ) = A D for all morphisms F ∈ CwF Σ (C, D). Finally, given Γ and A, a uniform family of terms is a family of terms a = (a C ) in Tm C (Γ C , A C ) such that F (a C ) = a D for all morphisms F ∈ CwF Σ (C, D). Definition 4 . We define inductively how to build a valid signature Σ and the category CwF Σ of cwfs with a Σ-structure and cwf-morphisms that preserve this structure. First, the base case: The empty signature: The empty signature ∅ is valid and CwF ∅ = CwF. Assume now that we have defined Σ as a valid signature and the associated category CwF Σ . Then we can add a new sort symbol, or a new operator symbol, or a new equation, to get a new valid signature, as follows: Adding a sort symbol: Let Γ = (Γ C ) be a uniform family of contexts. Then we can extend Σ with a new sort symbol S relative to Γ, to obtain the gat Σ ′ = (Σ, (Γ, S)). The Adding an operator symbol: If Γ is a uniform family of contexts and A a uniform family of types over Γ, then we can extend Σ with a new operator symbol f relative to Γ and A, to obtain the gat

Adding an equation:
If Γ is a uniform family of contexts, A is a uniform family of types over Γ and a, a ′ are uniform families of terms in A, then we can extend Σ with a new equation a = a ′ , to obtain the gat Σ ′ = (Σ, (Γ, A, a, a ′ )). In this case, This definition is syntax independent. However, it corresponds to a purely syntactic definition of an initial object T Σ in CwF Σ (for all valid signatures Σ) in terms of grammars and inference rules. A context in T Σ will be an equivalence class [Γ] of raw contexts, and similarly for substitutions, types, and terms. To give a uniform family of contexts Γ C is then equivalent to giving a context Uniform families of types and terms arise from types and terms in T Σ in a similar way.

The construction of an initial object in CwF Σ
We shall now show our main theorem. It can be viewed as a generalization of Birkhoff's completeness theorem for equational logic [4]: Theorem 1 . The category CwF Σ has an initial object T Σ , for every valid signature Σ.
The construction of T Σ will be by induction on the construction of Σ. It is based on construction of initial cwfs in [8,9] and we refer the reader to those papers for more details.
Here we only provide a sketch and focus on how to extend the construction to T Σ .
For each Σ we will define the following. • A grammar for raw contexts in Ctx Σ , raw substitutions in Sub Σ , raw types in Ty Σ , and raw terms in Tm Σ . • A system of inference rules that generate four families of partial equivalence relations (pers) by a mutual inductive definition: , and a, a ′ ∈ Tm Σ . Instances of these pers correspond to valid equality judgments of a variable free version of dependent type theory with explicit substitutions based on the cwf-combinators. The ordinary judgments will be defined as the reflexive instances of these equality judgments. For example Γ ⊢ Σ (meaning "Γ is a valid context") is defined as the reflexive instance Γ = Γ ⊢ Σ . • A cwf T Σ is then constructed from the equivalence classes of derivable judgments. For example, the contexts in T Σ are equivalence classes [Γ], such that Γ ⊢ Σ . We will show that T Σ is a cwf with a Σ-structure, that is, an object of CwF Σ . • A CwF Σ -morphism − : T Σ → C for every C ∈ CwF Σ . This is the interpretation morphism. This morphism is a partial function defined by induction on the raw syntax, that (whenever it is defined) maps raw contexts to contexts in C, raw substitutions to substitutions in C, raw types to types in C, and raw terms to terms in C. We show that these partial functions preserve the partial equivalence relations so that we can define the interpretation morphism on the equivalence classes. Finally we show that it indeed is a CwF Σ -morphism and the unique such into C.
We begin with the construction for the base case: the empty signature ∅.
• We start with the following grammar for raw contexts, raw substitutions, raw types, and raw terms.
These grammars generate a language of cwf-combinators. • The system of inference rules is displayed in [8,9]. It is a system of general rules, rules for dependent type theory which come before we introduce any sort symbols and operator symbols and equations (or any rules for the type formers of intuitionistic type theory). We do not have room here to display them, but note that they can be divided into four groups: the per rules, amounting to symmetry and transitivity for the four forms of equality judgments; -preservation rules for judgments, amounting to substitution of equals for equals (an example of such a rule is the type equality rule); -congruence rules for operators expressing that the cwf-combinators preserve equality; -conversion rules for the cwf-combinators.
• Note that the initial cwf T ∅ is rather uninteresting: its category of contexts contains only a terminal object (the empty context), and there are no types and terms. Nevertheless, the grammar and inference rules used in its definition form a starting point. The grammar for raw types and raw terms will be extended each time we add a new sort symbol or operator symbol, respectively. For each such new symbol and each new equation we will add a new inference rule. As a consequence we will generate a non-trivial T Σ . Assume now for the induction step that we have defined the grammar, the inference rules, T Σ and the interpretation morphism − : T Σ → C in CwF Σ . Let Σ ′ be Σ extended by a new sort symbol, a new operator symbol, or a new equation. We shall now explain how to define T Σ ′ . Adding a sort symbol: If Γ ⊢ Σ , then we can introduce a new sort symbol S in the context Γ representing the sequence of types of the arguments of S.
• We add a new production for raw types A ::= S to the productions for T Σ . • We add the inference rule • We extend the definition of the interpretation morphism − to an interpretation morphism − ′ : T Σ ′ → C by [S] ′ = S C It follows that this is a morphism in CwF Σ ′ and that it is unique.
Adding an operator symbol: If Γ ⊢ Σ A, then we can introduce a new operator symbol f , where the context Γ represents the sequence of types of the arguments and A is the type of the result.
• We add a new production for raw terms a ::= f to the productions for T Σ . • We add the inference rule • We extend the definition of the interpretation morphism − to an interpretation morphism − ′ : T Σ ′ → C by [f ] ′ = f C It follows that this is a morphism in CwF Σ ′ and that it is unique. Adding an equation: If Γ ⊢ Σ a : A and Γ ⊢ Σ a ′ : A we can introduce a new equation a = a ′ .
• T ′ Σ has the same productions as T Σ . • We add the inference rule Γ ⊢ Σ ′ a = a ′ : A to the inference rules for T Σ . • T Σ ′ is based on the same raw syntax as T Σ but the equivalence relation has changed.
To show that T Σ ′ ∈ CwF ′ Σ we just need to show that [a] = [a ′ ] but this follows from the inference rule Γ ⊢ Σ ′ a = a ′ : A.
• In order to define − ′ we first define the partial function on the raw syntax to be identical to the partial function on the raw syntax for − . We then prove that this partial function preserves the extended partial equivalence relation and define − ′ on the new equivalence classes. It follows that − ′ is unique. This concludes the proof of the theorem.

Examples of generalized algebraic theories
We will now display the sort symbols, operator symbols, and equations for the generalized algebraic theories of internal monoids, internal categories, and internal cwfs. We will show how to add operator symbols and equations when adding internal notions of Π, N and a universe closed under Π and N. The reason for prefacing these notions by the word "internal" is that the models of the theories are internal monoids, categories, and cwfs in CwF Σ for the respective signatures for these theories. Moreover, internal monoids, categories, and cwfs in the cwf Set are small monoids, categories, and cwfs, respectively. Note that our underlying notion of cwf in Section 2 is not small, and hence not an internal cwf in the cwf Set.
Our final example is the gat of internal contextual cwfs, a variant of Cartmell's contextual categories. The contexts in such contextual cwfs come with a length n. We sketch how this can be axiomatized as a gat with countably many sort symbols ctx n , sub n , ty n , tm n for an external natural number n (and similarly for the operator symbols and equations). We also indicate how our framework can be extended to cover such gats. 5.1. Internal monoids. The one-sorted algebraic theory of monoids has two operator symbols, e for identity and * for composition, and associativity and identity laws as equations. As any other ordinary algebraic theory, the algebraic theory of monoids yields a generalized algebraic theory.
We now show in some detail how to build a valid signature for internal monoids using the cwf-combinators, following the recipe for introducing sort symbols, operators symbols, and equations in the previous section. (We omit the index Σ in ⊢ Σ .) • First, we have 1 ⊢ for the empty signature, so we can add a production for the constant sort symbol M and the inference rule: Note that we project M on the right twice, reflecting that * is binary. • We omit the rendering of the right identity and associativity laws. We also display the sort symbols, operator symbols, and equations in ordinary notation with named variables for comparison: Examples later in this section will use named variables, which are easier to read, but more difficult to deal with semantically.
A cwf C with extra structure for this gat has an internal monoid in C. This is a cwfversion of the notion of internal monoid which can be defined in any category with finite products. Ordinary (small) monoids come out as internal monoids in Set, the cwf of small sets.

Internal categories. The gat of categories was one of Cartmell's motivating examples.
It has the following sort symbols, operator symbols, and equations. Again, note that in our case the models are internal categories in a cwf. To emphasize the difference between the internal notions of category and cwf and the external notions (introduced in Section 2), our notation for sort symbols in the gat of internal cwfs use lower case letters (obj, hom, ty, tm). This is in contrast to the upper case letters for the external versions (Ty, Tm). We will however overload notation for operator symbols, and for example use • both for the cwfcombinator and for the operator symbol in the gat of internal categories.
Sort symbols: Note that composition is officially an operator symbol with five arguments. In the official notation we should write γ • Ξ,∆,Γ δ, but we suppress the context arguments Ξ, ∆, Γ. We will do so for some other operations too.
The rendering of the gat of categories in cwf-combinator language and the proof that it indeed yields a valid signature are similar to what they were for the gat of monoids. The inference rules for the two sort symbols in cwf-combinator language are We omit the verbose cwf-renderings of the operator symbol for composition and the equations.
A cwf with extra structure for the generalized algebraic theory of categories is a cwf with an internal category. This is a cwf-based analogue of the usual notion of internal category in a category with finite limits. As shown by Martin Hofmann [15,16], every category with finite limits yields a category with attributes, and hence a cwf. However, not every cwf has finite limits. To achieve this we need more structure. As shown by Clairambault and Dybjer [11,12] the 2-category of categories with finite limits is biequivalent to the 2-category of democratic cwfs that support Σ-types and extensional identity types.
An internal category in the cwf Set of small sets is a small category.

5.3.
Internal cwfs. The gat of internal cwfs is obtained by extending the gat of internal categories with new sort symbols, operator symbols, and equations for a family valued functor, and then new operator symbols and equations for a terminal object, and context comprehension. We here rename the sort obj of objects of the category of contexts to ctx. A cwf with extra structure supporting the generalized algebraic theory of cwfs is a cwf with an internal cwf. An internal cwf in the cwf Set of small sets is a small cwf, that is, it is a cwf in the ordinary sense (see Definition 2) except that it has small sets of objects, morphisms, types, and terms.
An example of a cwf with an internal cwf is provided by the cwf Set of small sets with an internal category of very small sets. We can make this precise if we work in set theory with two Grothendieck universes V 0 ∈ V 1 . We call the members of V 1 "small sets" and the members of V 0 "very small sets". The category of contexts of the cwf Set is the usual category of small sets, by which we here mean that its objects are in V 1 . Moreover, the types are also the small sets in V 1 . To get an internal cwf, we interpret its sort of objects ctx as the small set V 0 of very small sets, and the sorts of types ty(Γ) also as V 0 .

5.4.
Internal cwfs with Π-types. We add three operator symbols in addition to the operator symbols for cwfs in Section 5. where γ + = γ • p, q .

5.5.
Internal cwfs with N. We add four operator symbols N, 0, s, R in addition to the operator symbols for cwfs in Section 5.2 and 5.3, and equations for the induction principle R and commutativity with substitution, but omit the details. 5.6. Internal cwfs with U 0 closed under Π and N. We add four operator symbols in addition to the operator symbols for cwfs with Π-types and N in Section 5.2 -5.5: (U 0 ) Γ is the universe (a type) relative to the context Γ; T 0 is the decoding operation mapping a term in the universe to the corresponding type; N 0 is the code for N in the universe, and Π 0 forms codes for Π-types in the universe. (Note that we have dropped the context argument of T 0 and Π 0 .) We add the decoding equations: and the equations for preservation of substitution: We remark that the gat for the universe is inevitablyà la Tarski in the sense that we distinguish between types and terms in a cwf and we must have an operation decoding a term into a type. However, Martin-Löf's distinction betweenà la Russell andà la Tarski [17] is a distinction between two different formulation of the raw syntax and inference rules of type theory.

5.7.
A possible refinement to internal contextual cwfs. Our treatment can be adapted to some non finitely presented gats. If we have an increasing sequence of signatures Σ n we can consider the union of the theory T Σn . For instance, we can describe a gat of contextual cwfs [10] (similar to Cartmell's contextual categories and Voevodsky's C-systems) by the following stratification of the theory of cwfs. We replace the sort ctx by a sequence of sorts ctx 0 , ctx 1 , . . . where ctx n represents the sort of contexts of length n and a corresponding sequence of sorts ty n (Γ) for Γ in ctx n and tm n (Γ, A) for A in ty n (Γ). Context extension Γ.A is now in ctx n+1 if A is in ty n (Γ) and so on. We also add destructors: we have ft(Γ) in ctx n and st(Γ) in ty n (ft(Γ)) with Γ = ft(Γ).st(Γ). Similarly we have a stratification of the sort of substitutions hom n,m (∆, Γ) for ∆ in ctx n and Γ in ctx m . The resulting models are internal contextual cwfs in a cwf.

Related work
The first proof of initiality of a formal system with dependent types is due to Streicher [20]. He proved that the formal system for the Calculus of Construction forms an initial object in a category of contextual categories with suitable extra structure. Recently, Brunerie et al [5] presented a formalized proof in the Agda system that a formal system for Martin-Löf type theory forms an initial object in a category of contextual categories with extra structure for the type formers.
More generally, Voevodsky [21] has outlined a new vision of the theory of syntax and semantics of dependent type theories. In this vision formal systems for dependent type theory are proved to be initial in suitable categories of models (the initiality conjecture). The above mentioned contributions by Streicher and Brunerie et al are two examples of such characterizations. However, Voevodsky's aim was to go further and characterize a whole class of type theories and prove uniform initiality results for them with the aim to form the basis for a general metatheory of dependent type theory. Our work can be viewed as a contribution to Voevodsky's programme, since we prove an initiality theorem for the whole class of finitely presented generalized algebraic theories.
Another related contribution is Palmgren and Vickers' [19] detailed construction of an initial model of an essentially algebraic theory.
Altenkirch and Kaprosi [3] give several examples of quotient inductive-inductive types (qiits). Their main example is a definition of dependent type theory with Π-types and a universe, as a simultaneous definition in the Agda system [1] of the data types Ctx of contexts, Sub(∆, Γ) of substitutions, Ty(Γ), and Tm(Γ, A) of terms. Their definition is inductiveinductive [14], since the index sets of Sub, Ty, and Tm are generated simultaneously, and as a consequence are not indexed inductive definitions in the usual sense where the index sets are fixed in advance. Furthermore, it is a quotient inductive-inductive type since they also have constructors for identity types, as in higher inductive types.
There is a close relationship with our initial internal cwf with Π-types and a universe. Like our definition, their qiit-definition uses cwf-combinators. Moreover, our sort symbols correspond to their formation rules (data type constructors), our operator symbols correspond to their introduction rules (constructors), and our equations correspond to their propositional identities. However, the fact that our equations are judgmental equalities while theirs are propositional identities is an important difference. As a consequence they need to use transport maps when moving between identical types. Using the terminology of qiits, we here provide a definition of a class of valid qiits (in our modified sense) together with a class of models for each of them.