W-types in setoids

We present a construction of W-types in the setoid model of extensional Martin-L\"of type theory using dependent W-types in the underlying intensional theory. More precisely, we prove that the internal category of setoids has initial algebras for polynomial endofunctors. In particular, we characterise the setoid of algebra morphisms from the initial algebra to a given algebra as a setoid on a dependent W-type. We conclude by discussing the case of free setoids. We work in a fully intensional theory and, in fact, we assume identity types only when discussing free setoids. By using dependent W-types we can also avoid elimination into a type universe. The results have been verified in Coq and a formalisation is available on the author's GitHub page.


Introduction
The present paper is a contribution to the study of models of extensional properties in intensional type theories and is in particular concerned with W-types. The W-type constructor in Martin-Löf type theory [ML84,ML82] produces an inductive type whose terms can be understood as well-founded trees. We provide a construction, verified in Coq, of W-types in the setoid model of extensional Martin-Löf type theory using dependent W-types in the underlying intensional theory. Although we work internally in intensional Martin-Löf type theory, we present our results using the category-theoretic language. More precisely, we consider a logical-framework presentation of a dependent type theory with unit type, -types, -types and dependent W-types and we show in Theorem 4.20 that the internal category of setoids has initial algebras for polynomial endofunctors. These were identified as a category-theoretic counterpart of W-types by Moerdijk and Palmgren [MP00]. Initiality of an algebra and the induction principle of the underlying type are related via the notion of contractibility in [AGS17].
Dependent W-type were introduced by Petersson and Synek [PS89], see also [NPS90,Chapter 16], to provide a constructor for general inductive data types, and are also known as indexed W-types or tree types. The dependent W-type constructor produces a family of mutually inductive types, as opposed to the single inductive type produced by the ordinary W-type constructor. Indeed, ordinary W-types are type-theoretically equivalent and in intensional type theories, but it is important for inductive arguments. We can avoid dealing with transitive closures when proving Theorem 4.18 since the definition of telescopic functions only involves the setoid of immediate subtrees of a tree, which has a natural and straightforward definition as an image factorisation, see Definition 3.7 and Remark 3.8.
We also use dependent W-types to compare the setoid of extensional trees with respect to discrete setoids to the discrete setoid on the W-type of the underlying types. In particular, we see in Corollary 5.9 that every discrete tree with nodes from a 0-type, i.e. a type with decidable equality, is extensional. It seems that, without further assumptions, not every discrete tree is extensional. However, if function extensionality holds, then it is possible to identify the setoid of extensional trees on discrete setoids with a subsetoid of discrete trees, see Theorem 5.8.
The setoid construction that we consider is an instance of a quotient completion, see [MR13,MR16]. The author is aware of two other constructions of W-types for quotient completions. The first one, that we already mentioned, was formulated by Palmgren for the setoid model in intensional Martin-Löf type theory and then adapted by Bressan [Bre15] to minimalist type theory [Mai09]. The argument requires a 'large' elimination principle for W-types, in the sense that it must be possible to eliminate into a type universe or a universe of propositions. The second construction is due to van den Berg [Ber05] and it applies to exact completions of categories with finite limits. In intensional Martin-Löf type theory, the assumption of finite limits is met by the category of types assuming function extensionality and Uniqueness of Identity Proofs, by its full subcategory on the 0-types assuming only function extensionality [UFP13,RS15], and by the e-category of types assuming only UIP [EP20]. However, there is little hope that an internal category of types in a fully intensional type theory would have ordinary finite limits. Setoids are also closely related to groupoids. Vidmar has given a construction of initial algebras for polynomial endofunctors on groupoids from initial algebras for polynomial endofunctors on sets [Vid18].
In the next section we discuss the preliminaries needed for the construction. Section 3 contains the construction of the algebra of extensional trees and the definition of the setoid family of immediate subtrees. The proof of its initiality is in Section 4. We conclude the paper with a discussion of the case of extensional trees over discrete setoids in Section 5. Each section begins with a brief overview of the content.
The Coq formalisation of the present paper is available on the author's GitHub page [Emm18] and it includes Definitions 2.2 and 2.3 and all numbered definitions and results in Sections 2.5 and 3 to 5, except for Proposition 2.9, a Coq proof of which can be found at [Pal12a].

Preliminaries
We present the type theory we will be working with in Section 2.1; briefly review W-types in Section 2.2 and their dependent verion in Section 2.3; and recall some basic facts and definitions about setoid and setoid families in Section 2.4, and about polynomial functors in Section 2.5.
2.1. The type-theoretic setting. We work in a logical framework formulation of Martin-Löf type theory, see [NPS90,Part III], similar to the fragment of Coq that we used for the formalisation. The logical framework consists of record types, -types and a universeà la Russell U, which is the type Set in the Coq formalisation. Logic is interpreted according to the propositions-as-types interpretation. When we wish to emphasise that a type is in U we say that it is a small type. Judgemental equality is denoted ≡ and application of function terms will usually be denoted by concatenation as in f b. However, in the presence of multiple arguments we may adopt the notation with parenthesis as in ar(i, n, b).
The theory itself is then specified by declaring constants and equations. Specifically, we require the universe U to be closed under -types, and to contain the unit type 1, -types, and dependent W-types. Ordinary W-types are recovered as dependent W-types over the unit type 1, but in the Coq formalisation these are assumed to be primitive for convenience. In Figures 1 and 2 we spell out the constants and equations for (dependent) W-types in the form of rules to increase readability. In these rules, the premises of W-FORM and DW-FORM are left understood in the other rules. We might as well drop (some) subscripts if they are clear from the context. The system has η-conversion only for -types.
Note that we do not require identity types. These will be assumed only in Section 5 for discussing the case of discrete setoids. It is well-known that record types can be replaced with -types at the expense of readability. In this case we would not need to assume that these coincide with the -types in the theory.
2.2. W-types. W-types can be used to construct several inductive types, including natural numbers and lists [Dyb97], and to give a constructive justifications of certain theories of inductive definitions [Pal92]. More generally, W-types provide a predicative counterpart to the notion of well-ordering. Furthermore, they are instrumental in Aczel's model of Constructive Zermelo-Fraenkel set theory [Acz78], in the form of the type of iterative sets, where they also allow to interpret the Regular Extension Axiom which adds general inductive definitions to CZF [Acz86]. The idea of a type of iterative sets is also central in Palmgren's interpretation of type universes in the setoid model [Pal19].
By elimination of W-types, there are function terms node : W B → A and ist : Figure 1: Rules for W-types.  such that node (sup a f ) ≡ a and ist (sup a f ) ≡ f . Henceforth, we write ist w for ist w. We refer to terms of a W-type W B as trees, to terms a : A as nodes, to terms b : B a as branches and, for every branch b : B a, to the term ist w b as the immediate subtree of w on branch b.
In the set-theoretic interpretation of type theory, W-types are sets of well-founded trees with labelled nodes. The set A is the set of labels for the nodes and, for each a ∈ A, the set B a consists of the branches out of the node with name a. Trees are formed by providing a node a ∈ A and attaching other trees to the branches in B a . This procedure is formally specified by functions f : B a → W B which stipulate that the tree f (b) is attached to the branch b ∈ B a . Figure 3 shows a tree in W B when A contains three elements a 0 , a 1 and a 2 such that B a 0 is empty and B a 1 = {0, 1} = B a 2 . The corresponding canonical element of W B is displayed on the right-hand side together with the five functions mapping a branch to the tree attached to it, among which occur the empty function f ∅ into W B .
As it is well-known, these sets of trees can be characterised more abstractly as free term algebras for infinitary single-sorted signatures. In this view, the set A contains the function symbols of the signature, while (the cardinality of) B a is the arity of symbol a. Terms are built out of function symbols according to composition instructions specified 28:6
The tree in Figure 3 is the term also written a 1 (a 0 , a 2 (a 0 , a 0 )), in a signature with function symbols a 0 with arity 0 and a 1 , a 2 with arity 2.
2.3. Dependent W-types. In the same vein, dependent W-types can be understood as free term algebras for infinitary multi-sorted signatures: sorts are elements of I, elements of N i are function symbols with codomain sort i and the arity of a function symbol a ∈ N i is given by the function ar i,a : B i,a → I that maps each b-th argument of a to its sort. Figure 4 depicts an element of a dependent W-type. Compared to the graphical representation of trees in Figure 3, the sort i of a function symbol a ∈ N i appears as an additional leg pointing downwards out of the node corresponding to a, and each branch b ∈ Br i,a is labelled with the sort ar(i, a, b) ∈ I. The dependent tree in Figure 4 is a term in the signature containing two sorts i 0 and i 1 , and three function symbols, a 0 of sort i 0 and a 1 , a 2 of sort i 1 , such that a 0 has empty arity, a 1 has arity i 0 and i 1 , and a 2 has arity i 0 and i 0 . This is formally specified by requiring that i 0 , i 1 ∈ I, a 0 ∈ N i 0 , a 1 , a 2 ∈ N i 1 , Br i 0 ,a 0 = ∅, Br i 1 ,a 1 = {0, 1} = Br i 1 ,a 2 , ar(i 0 , a 0 ) is the empty function into I, ar(i 1 , a 1 ) maps 0 to i 0 and 1 to i 1 and ar(i 1 , a 2 ) maps 0 and 1 to i 0 . A common presentation of the term in Figure 4 is a 1 (a 0 , a 2 (a 0 , a 0 )) : i 1 .
Note that there must be at least one node with no branches, i.e. at least one constant, in order for these trees to be well-founded. Indeed, in terms of signatures, these sets of trees consist of closed terms. This is reflected in type theory: whenever B is a family of non-empty types, W B is type-theoretically equivalent to the empty type 0, when the latter Figure 4: Canonical term of a dependent W-type. is available. In order to make this precise, the meaning of 'non-empty' can be understood either as a:A (B a → 0) → 0 or as a:A ||B a|| if propositional truncation is available, in the sense that assuming either of them proves that W B is equivalent to 0.
Similarly to the non-dependent case, using the elimination rule of dependent W-types, we obtain for every i : I two function terms such that dnode i dsup(i, n, f ) ≡ n and dist i dsup(i, n, f ) ≡ f . We refer to I as the type of indices or sorts, to N and Br as the node family and the branching family, respectively, and to ar : i n Br(i, n) → I as the arity function.
The following examples are just for the sake of illustrating the use of dependent W-types and we do not use the definitions therein elsewhere in the paper.
(1) Let X : U and Y : X → U. We can define a type of (root-to-leaf) paths in a tree w : W Y as a dependent W-type over W Y as follows. Say that a path in w ≡ sup x f is a branch y : Y x together with a path in the immediate subtree f y.
The family Path Y of paths in trees in W Y is the dependent W-type on W Y with node and branching families Br := λw, y.1 : and with arity function ar := λw, y, .ist w y : w y 1 W Y . A canonical element of Path Y w consists of a branch y of w together with a function 1 → Path Y (ist w y), i.e. a path in the immediate subtree ist w y.
(2) If the theory has finite sums (in fact, the type 2 suffices) and the empty type 0, it is possible to define a type of finite paths in a tree w : W Y similarly to the previous example. Say that a finite path in w ≡ sup x f is either the empty path, or it is a branch y : Y x together with a finite path in the immediate subtree f y : W Y . The family FinPath Y : W Y → U of finite paths in trees in W Y is the dependent W-type on W Y with node and branching families and with arity function ar := λw, z, .
0 → W Y is the eliminator of the empty type. A canonical element of FinPath Y w consists either of a term t : 1 and a function 0 → W Y , which we regard as 'the' empty path, or of a branch b of w and a function 1 → FinPaths Y (ist w y), i.e. a finite path in the immediate subtree ist w y. Either this path consists of a term of 1 and an empty function, in which case the path is over with length one. Or it consists of a branch y and a finite path in ist ist y y .
The function term FinPath Y w → N that maps each finite path to its length can be easily defined using the elimination rule of dependent W-types in Figure 2. 2.4. Setoids and setoid families. A setoid is informally defined as a type together with a type-theoretic equivalence relation on it. This can be made precise in various different ways, e.g. considering partial relations, or Prop-valued relations if a type of proposition is available, or instead 0-types and mere relations. See [BCP03] for a survey of possible definitions. In our context we define a setoid as follows.
where |A| : U is a small type, the type family ≈ A : |A| → |A| → U is an equivalence relation with proofs of reflexivity, symmetry and transitivity given by, respectively: The type of setoids Std is defined as a record on the types of |A|, ≈ A , r A , s A and t A . For a setoid A, we refer to the relation ≈ A as the equality of A.
Since our theory has a unit type, every small type X gives rise to a setoid on X with equality λa, a .1, called the the codiscrete setoid on X.
In Section 5 we will have identity types available. In this case every small type X can be equipped with the equality λx, x .x = X x given by the identity type. This setoid is called the discrete setoid on X. More generally, a setoid A is discrete or (free) if a ≈ A a if and only if a = |A| a for every a, a : A.
In the category of setoids that we will define below, the codiscrete setoid over any inhabited type is a terminal object, and it is isomorphic to the discrete setoid on 1 if identity types are available.
Definition 2.3. Let A and B be setoids. A function term f : |A| → |B| is extensional (with respect to ≈ A and ≈ B ) if there is a term of type (2.3) We will refer to extensional function terms as extensional functions, or simply as functions, and we will write |f | for the underlying function term of an extensional function f . The setoid A ⇒ B of extensional functions from A to B has the type of extensional functions In fact, in the Coq implementation we find it more convenient to define the type of extensional functions as a record rather than as a -type. However this makes no essential difference.
It is easy to see that this is a partial equivalence relation on |A| → |B| whose domain, i.e. the type of function terms f such that extgen(f, f ), is the type of extensional function |A ⇒ B|. Furthermore, two extensional functions f and g are equal if and only if the type extgen(|f |, |g|) is inhabited.
In the rest of the paper we write a : A for a setoid A, to mean a : |A|, and we often not distinguish between an extensional function f : A ⇒ B and its underlying function term |f | : |A| → |B|. We also write f α : f a ≈ B f a for the proof of extensionality of f applied to α : a ≈ A a . Occasionally, we also find it convenient to drop the subscript from the equality of a setoid. We do not expect these abuses of notation to lead to confusion.
Setoids provide us with a formulation of the notion of category without equality on objects that was introduced by Aczel, who formalised it in Lego [Acz93]. See also [HS00] for a formalisation in Coq that uses setoids with Prop-valued equalities.
Definition 2.5. A (locally small) e-category A consists of a type of objects Obj A and a family of setoids Hom A : Obj A → Obj A → Std, of arrows, together with function terms for identity and composition, where the latter is extensional, in the sense that it has type a,b,c:Obj A and with identity and associativity axioms formulated using equalities of the family of setoids Hom A .
An e-functor between two e-categories A and B consists of a function term F : Obj A → Obj B together with a term of type a,a :Obj A Hom A (a, a ) ⇒ Hom B (F a, F a ) and proof terms witnessing that it is functorial with respect to the equalities of the family of setoids Hom B .
An e-natural transformation between two e-functors F, G : A → B consists of a term n : whose action on a : Obj A we denote as n a , together with a term of type a,a :Obj A f :a⇒a The names 'e-category', 'e-functor', etc. were introduced to distinguish these concepts from the standard ones. However, since these are the only formulations that appear in the present paper, we will just say 'category' to mean 'e-category' and similarly for the others.
For a setoid A, the discrete category A # on A is defined as follows. Its type of objects is |A| and, for a, a : A, its setoid of arrows from a to a is the codiscrete setoid on the type 28:10

Jacopo Emmenegger
Vol. 17:3 a ≈ A a . Since ≈ A is symmetric, the category A # is a groupoid, i.e. a category where every arrow is invertible. Functors between two categories A and B and natural transformations between them form a category Fun(A, B). We denote the action of a functor F on an arrow α : Hom A (a, a ) as F α : Hom B (F a, F a ).
Setoids and extensional functions form a category [PW14].
Definition 2.6. The category of setoids is defined as follows. The type of objects is the type of setoids Std and the family of arrows is the family λA, B.A ⇒ B of setoids of extensional functions. Identity and composition are defined in the obvious way, the latter will be denoted as g • f . We abuse notation and denoted this category also as Std.
For a setoid A, we define and call a functor B : Fam A a setoid family over A. We refer to the action of B on an arrow For every extensional function f : A ⇒ A and every setoid family Remark 2.7. Besides the standard functoriality conditions on identities and composites, setoid families enjoy an additional coherence. Indeed, for every α, α : a ≈ A a , it is α ≈ α as arrows in A # . It follows by extensionality of B on arrows that for every α and α as above. Indeed, setoid families in this sense are the proof-irrelevant setoid families studied in [Pal12b]. In particular, for every α, α : and, for every α : a ≈ A a and b, b In fact, setoid families are formalised in [Emm18] directly as a record on the types of function terms and their functoriality and proof-irrelevance properties.
It is an important feature of the category of sets in set theory that the slice over a set A is equivalent to families of sets indexed over A. The same holds for the category of setoids in the fragment of intensional Martin-Löf type theory that we are considering, see Section 2.1.
Theorem 2.8. For a setoid A, the slice category Std/A is equivalent to the category of setoid families Fam A.
Proof. We describe the two functors, leaving the straightforward verification of the equivalence to the reader.
Every setoid family B : Fam A gives rise to a setoid ( a:A B a, ≈) where the equality is The projection into A is extensional, with extensionality proof also given by the first projection. This defines an extensional function into A.
A natural transformation n from B to C is mapped to the extensional function with underlying term λ(a, b).(a, n a b). Its extensionality follows, for every (α, β) : which holds by naturality of n and extensionality of n a .
Conversely, let f : B ⇒ A be a function. Consider, for every a : This assignment extends to a setoid family whose transport along α : a ≈ A a maps a pair (b, β) to the pair (b, β ), where β : f b ≈ A a is the concatenation of β and α.
An arrow in Std/A from f to some g : C ⇒ A is a function k : B ⇒ C such that g •k ≈ f . This gives rise to a natural transformation as follows. For a : A, the underlying function This function term is clearly extensional as k is. As transport along α : a ≈ A a does not modify the first component, naturality in a : A is trivial.
2.5. Polynomial functors and W-types. Recall that a category is locally cartesian closed if it has a terminal object and all its slices are cartesian closed, that is, have all binary products and exponentials. Theorem 2.8 may be used to prove the following result, another proof can be found in [EP20].
Proposition 2.9. The category of setoids Std is locally cartesian closed.
In a locally cartesian closed category C, for every arrow f : B → A, the pullback functor f * has a left adjoint f ! , defined by post-composition with f , and a right adjoint f * , as below.
When f is the unique arrow B → 1 to the terminal object, the above functors become It follows that every arrow f : B → A in C gives rise to a functor P f : C → C, the polynomial endofunctor associated to f , defined as the composite A polynomial endofunctor is then defined to be an endofunctor on C which is naturally isomorphic to P f for some f in C.

Jacopo Emmenegger
Vol. 17:3 An algebra for a polynomial endofunctor P is given by an object X and an arrow s : P X → X, called algebra map. Such an algebra is initial if for any algebra t : It is a well-known result by Lambek that the algebra map of an initial algebra is invertible.
In extensional type theory with one universe, the category of small types and function terms is locally cartesian closed if the universe has 1, identity types, and types. Hence we may consider polynomial endofuctors for each function term f : B → A. An initial algebra is then given by the W-type of the type family In Std, we take advantage of Theorem 2.8 and formulate the notion of polynomial functor for setoid families.
Definition 2.10. Let B : Fam A be a setoid family over a setoid A. The polynomial endofunctor associated to B is the functor Std → Std defined on X : Std by We say that an endofunctor on Std is polynomial if it is naturally isomorphic to a polynomial functor associated to a setoid family B.
In fact, the formalisation [Emm18] only contains the definition of the action of P B on setoids and functions.
We need to make sure that this definition does coincide with the standard one in (2.8) for an arbitrary locally cartesian closed category.
Let us begin by unfolding the definition of P f in (2.8) for an extensional function f : B ⇒ A. Recall that the three adjoint functors in (2.7) are constructed using -types and -types. More precisely, the first functor in (2.8) maps a setoid X to the projection pr 2 : X × B ⇒ B, and the last one maps a function x : X ⇒ A to its domain X. To describe the action of the functor f * : Std/B → Std/A on a function y : Y ⇒ B, let us say that a pair (a, g) in This defines a setoid LS y of local sections of y, and the function f * y : LS y ⇒ A is the obvious projection.
Let us now denote by S(f ) the setoid family constructed from the extensional function f in the proof of Theorem 2.8. Unfolding the definitions, one sees that, for every X, the setoid P f X is in bijection with P S(f ) X, and that the bijection is natural in X. If we denote by E the inverse construction to S from the same proof, it also follows that for every setoid family B : Fam A the functor P E(B) is naturally isomorphic to P B . Definition 2.11. Let B : Fam A be a setoid family.
(1) An algebra for P B in Std is a setoid X together with an extensional function P B X ⇒ X.
(2) Let x : P B X ⇒ X and y : P B Y ⇒ Y be algebras for P B . The setoid Alg(x, y) of algebra morphisms from x to y consists of functions h : In formulas: Definition 2.12. Let B : Fam A be a setoid family and C a setoid. A family of extensional functions F : be the type of proofs that F is coherent. (1) The family F is coherent with respect to f * B : Fam A .
Thus it clearly follows from coherence of F taking α := f α . The converse is a direct consequence of proof irrelevance of B, see (2.5) in Remark 2.7.
Remark 2.14. The value of the polynomial functor P B on a setoid C can be described as the total setoid of the setoid family λx.B x ⇒ C over A. Accordingly, Definition 2.12 and Lemma 2.13 can be phrased (and proved) in greater generality. However we do not need that generality here.

The algebra of extensional trees
Let B : Fam A be a setoid family over a setoid A. In this section we construct an algebra from the W-type W |B| on the underlying type family |B| : |A| → U. We keep A and B fixed throughout the section and we drop the subscript B in the polynomial functor P B . The underlying setoid of the algebra is constructed in Section 3.1. Before showing that sup lifts to an algebra map in Section 3.3, we introduce the setoid family of immediate subtrees in Section 3.2. Section 3.3 contains also a proof that the algebra map is invertible.

Jacopo Emmenegger
Vol. 17:3 3.1. The setoid of extensional trees. To construct an algebra, we first need a setoid of trees, that is, an equivalence relation on W |B| . However, we should not expect W |B| to be the underlying type of our setoid of trees though or, in other words, we should not expect our relation to be total on W |B| . Consider in analogy the situation described in Remark 2.4 with function terms: the domain of the equivalence relation extgen on the function type consists precisely of the extensional functions.
In order to find sufficient conditions for defining a suitable relation, let us suppose that such a partial equivalence relation ≈ W : W |B| → W |B| → U does exist. This means that, if we denote by W : Std the setoid induced by ≈ W on its domain |W | := w:W |B| w ≈ W w, there is an initial algebra s : PW ⇒ W whose underlying function term |s| : ( a:A B a ⇒ W ) → |W | is a lift of sup : a:A |B| a → W |B| → W |B| . Since s must be a bijection by Lambek's Theorem, for every tree w ≡ sup a f such that w ≈ W w there is g : B a ⇒ W such that s(a, g) ≈ W w. This can be equivalently stated as follows.
If sup a f ≈ W sup a f , then for every Equivalently, for every tree w in the domain of ≈ W the immediate-subtree function term ist w is extensional. Second, again because s is a bijection, and by definition of P in 2.10, it must be Let us now try to use (W1-2) to define ≈ W . In condition (W2) the relation ≈ W occurs on the right-hand side only between immediate subtrees. This feature makes (W2) a possible candidate for the inductive step in an inductive definition of the relation ≈ W . However, it does not seem possible to derive (W1) from (W2). Instead, we combine them in the following definition. Note that in 3.1.2 below the equality between b and b is over the term α as in (2.4). A tree that is matching itself will be called self-matching or extensional. Accordingly, we define the type family W per B : W |B| → W |B| → U of proofs that two trees are matching as the (curried version of) the dependent W-type on W |B| × W |B| with family of nodes N := λ(w, w ).node w ≈ A node w : W |B| × W |B| → U branching family Br := λ(w, w ), α. The matching relation satisfies (W1) and (W2): for the former take α in 3.1.1 to be reflexivity on a, and for the latter take b := B α b and reflexivity on b in 3.1.2.
Remark 3.2. For two trees w, w : W |B| , a proof term of 3.1.1 is α : node w ≈ A node w and a proof term of 3.1.2 is If we have two such terms, then a proof that w and w are matching is Conversely, if w and w are matching with proof γ, then α is obtained by applying the dependent node function dnode (w,w ) from (2.1) to γ, and φ by evaluating the dependent branching function dist (w,w ) from (2.2) on γ.
Proposition 3.3. The type family W per B : W |B| → W |B| → U is a partial equivalence relation on W |B| , that is, the following types are inhabited: Proof. The proof terms are obtained from straightforward applications of the elimination rule for dependent W-types. Alternatively, one may use Remark 3.2 and recursion on W |B| .
We can now form the setoid of extensional trees.
We often leave the proof term in W per B w w implicit, drop the subscript B when it is clear from context, and write w : W to mean w : W |B| and w extensional.
Similarly to the situation described in Remark 2.4, to prove that a tree w is extensional it is sufficient to consider an instance of the type of φ in Remark 3.2, namely the one where α is reflexivity at the node of w. In particular, every immediate subtree of an extensional tree is extensional and f lifts to an extensional function B a ⇒ W .
Proof. If w is extensional the conclusion follows by (W1). The converse follows from Remark 3.2 choosing reflexivity as α.

Jacopo Emmenegger
Vol. 17:3 The function term node : W |B| → |A| lifts to an extensional function node : W ⇒ A, with proof of extensionality given by α in Remark 3.2. By Lemma 3.5, for every extensional tree w the function term ist w : |B| (node w) → W |B| lifts to an extensional function ist w : B (node w) ⇒ W . So we have a family ist : of extensional functions. We continue writing ist w for ist w and we will do the same also for others dependent function terms on W . As the subscript will always be a tree in W , no confusion should arise with the action of setoid families on equalities. Also to prove that two extensional trees are matching it is enough to consider just an instance of the type of φ in Remark 3.2, as for extensional functions in Remark 2.4.
Equivalently, for every w, w : W , it is w ≈ W w if and only if there are Proof. If w and w are matching, the conclusion follows by (W2). Conversely, by Remark 3.2 it is enough to construct a term By Lemma 3.5 we may assume that the function f is extensional. It follows that for every 3.2. Setoid families on extensional trees. Extensionality of node : W ⇒ A yields, for every γ : w ≈ W w between extensional tree, a function where node γ denotes the proof of extensionality (2.3) of node applied to γ. Since B is a setoid family, the family of setoids λw.B (node w) : W → Std is a setoid family with the functions in (3.2) as transports. We refer to this setoid family as the family of branches of trees.
There is also another natural setoid family on extensional trees which will turn out to be instrumental in the characterisation of algebra morphisms in Section 4. Accordingly, the setoid of immediate subtrees of w, denoted ISTrees w, has |B| (node w) as underlying type, and s ≈ ISTrees w s := ist w s ≈ W ist w s as equality.
For s : ISTrees w and γ : w ≈ W w , the assignment ISTrees γ s := B node γ s : ISTrees w defines transport maps for ISTrees. Hence we obtain a setoid family ISTrees : Fam W , the family of immediate subtrees. Remark 3.8. The category of setoids Std is exact and, assuming the identity type, it is the exact completion of its subcategory of discrete setoids. We refer to [EP20] for details but see also [BM18]. Being exact, every function has a factorisation as a regular epi followed by a mono, called image factorisation. Being an exact completion, every function f : X ⇒ Y has a canonical such factorisation. Briefly: the setoid Z has |X| as underlying type and λx, x . (f x ≈ Y f x ) as equality; the regular epi X ⇒ Z has λx.x as underlying function term, which is extensional because f is; and the mono Z ⇒ Y has |f | as underlying function term, which is extensional by definition of Z. In fact, this canonical factorisation exists also when identity types are not assumed. By applying this factorisation to the function ist w : B (node w) ⇒ W we recover the setoid of immediate subtrees of w: The family e is coherent in the sense that holds since ISTrees γ and B node γ have the same function term. The family m is coherent in the sense of Definition 2.12 by Remark 3.9.
Remark 3.9. By (W2), the family of functions ist from (3.1) is coherent with respect to the setoid family ISTrees in the sense of Definition 2.12, i.e. for every γ : w ≈ W w there is ist-coh γ : ist w ≈ ist w • ISTrees γ .
The proof term ist-coh γ is obtained instantiating φ in Remark 3.2 with b := ISTrees γ b and reflexivity on b . Similarly, the family of functions m from (3.3) is coherent with respect to the setoid family ISTrees in the sense of Definition 2.12, i.e. for every γ : w ≈ W w there is m-coh γ : m w ≈ m w • ISTrees γ . (3.5) The proof term m-coh γ is obtained from ist-coh using (3.4) and the fact that e w is a (canonical regular) epi. where ρ is reflexivity of A, ε f is obtained uncurrying w is a proof that w is extensional. Lemma 3.10. The function term s : PW → W is extensional.
Proof. By Definition 2.10, to have two equal elements in the domain is to have a, a : A, f : B a ⇒ W , f : B a ⇒ W , and a term α : a ≈ a such that f ≈ f • B α . Lemma 3.6 and Definition 3.1 yield the claim.
We conclude this section with a proof that the algebra map s : PW ⇒ W is invertible. It is extensional because node is extensional, and because ist is coherent by Remark 3.9. The equation us • s ≈ id PW is straightforward from the definitions. For s • us ≈ id W use Lemma 3.6.

Initiality of the algebra of extensional trees
This section is mainly devoted to the characterisation of algebra morphisms as telescopic functions in Theorem 4.18. The existence of W-types in Std follows in Theorem 4.20.
4.1. Characterisation of algebra morphisms. Throughout this section we fix setoids A and C, a setoid family B : Fam A and a P B -algebra a C : P B C ⇒ C. As in the previous section, we drop the index B from the polynomial functor P B and the algebra of extensional trees W B .
We begin with a simple observation. Recall from Definition 2.11.2 that Alg(s, a C ) denotes the setoid of algebra morphisms from s : PW ⇒ W to a C : PC ⇒ C, i.e. those functions h : W ⇒ C such that h • s ≈ a C • Ph. In particular, the value of the algebra morphism h at the tree w is obtained by "glueing" the values of h on the immediate subtrees of w, collected in F w , according to the algebra a C . Note that the right-hand side of (4.3) makes sense for any family F as in (4.2). Let us say that a function k : ISTrees w ⇒ C is a glueing datum in C for w, and that a family F as in (4.2) is a family of glueing data in C. Two families F and F of glueing data are equal if, for every w : W , the glueing data for w are equal, i.e. F w ≈ F w . Given a family F of glueing data in C as in (4.2), we can use (4.3) to define a function term h : W → C. Proposition 4.3 isolates conditions on F for this term to be extensional and, further, an algebra morphism. The problem of constructing an element of Alg(s, a C ) could thus be reduced to the problem of finding a suitable family of glueing data in C and to prove that it is the unique such. We do so in Theorem 4.18, where we prove that Alg(s, a C ) is in bijection with a subsetoid of families of glueing data in C that we name, in Definition 4.7, telescopic families. In Proposition 4.19 we construct a telescopic family by direct induction on the underlying W-type W |B| of W . Its uniqueness is in Corollary 4.12. It follows that Alg(s, a C ) is a singleton. This is in accordance with the result in [AGS17], that an algebra a is equivalent to a W-type if and only if the type of morphisms of algebras out of a is contractible, once we observe that, in the setoid interpretation, a type is contractible precisely when the corresponding setoid is a singleton.
There is no reason, at this stage, to prefer families of functions on immediate subtrees as in (4.2) over families of functions B (node w) ⇒ C on branches of trees, but our strategy does not seem to work with the latter. One motivation is discussed in Remark 4.6.
First, we give names to the two constructions outlined above. (4.5) We need one more definition to state the next result. Coherence for a family of extensional functions was defined in 2.12.
Definition 4.2. Let us denote by GlueD the setoid of families of glueing data in C. The underlying type is w:W ISTrees w ⇒ C and the equality between two families F and F is The subsetoid of GlueD on the coherent families of glueing data is denoted CohGlueD and has underlying type (6) For every coherent family F : w:W ISTrees w ⇒ C of glueing data
(1) For every γ : w ≈ W w , it is h • m w ≈ h • m w • ISTrees γ since m w is coherent by (3.5) in Remark 3.9, and h is extensional.
(2) This is Lemma 2.13 together with extensionality of a C .
(3) If h ≈ h , then for every w : (4) Let ϕ : F ≈ F . If F w ≈ F w , then by extensionality of a C and proof irrelevance of B from (2.6) it is a C (a, F w ) ≈ C a C (a, F w ). (5) The claim follows from the fact that, for every w ≡ s(a, f ), it is (6) Unfolding the right hand side for w ≡ s(a, f ) yields By extensionality of a C and proof irrelevance (2.6), this holds if for every b : B a it is

Now, by definitions (4.4) and (4.5) it is
and the claim follows.
Corollary 4.4. The functions restr and glue establish a bijection between the setoid Alg(s, a C ) and those coherent families F of glueing data in C such that restr (glue F ) ≈ F .
Proof. Proposition 4.3.5 entails that restr (glue (restr h)) ≈ restr h for every algebra morphism h. Conversely, If F is such that restr (glue F ) ≈ F , then glue F is in Alg(s, a C ) by Proposition 4.3.6.
The bijection in Corollary 4.4 suggests a recursive characterisation of algebra morphisms into C as those morphisms obtained by glueing a coherent family of glueing data in C, such that each component F w is itself obtained by glueing a suitable family, and so on. In order to make this intuition precise, we need to bring coherent families of glueing data and glueing one "subtree level" up. We do not need to go any higher thanks to dependent W-types, which will cover the other cases for us in Definition 4.7, see also Remark 4.9. The characterisation is accomplished in Theorem 4.18.
For w an extensional tree, we say that a family G : is a family of glueing subdata for w in C. We write G s for G s as we do for families of glueing data on subtrees, and we refer to terms of type ISTrees (m w s) as 2-subtrees of w. By extensionality of m w , the family of setoids λb.ISTrees (m w b) over ISTrees w is a setoid family in the same way as for the family of subtrees ISTrees in Definition 3.7. We can thus form the setoid CohGlueSubD w of coherent families of glueing subdata for w, as we did in Definition 4.2 for coherent families of glueing data of subtrees.
In fact, CohGlueSubD is another setoid family over W , whose transport function for γ : w ≈ W w is defined on G and s : ISTrees w as where m-coh γ −1 s : m s ≈ m (ISTrees γ −1 s ). It follows that for all G : CohGlueSubD w and G : CohGlueSubD w . As in (4.5) for every family G of glueing subdata for w there is a function term subglue w G : ISTrees w → C defined on s : ISTrees w by subglue w G s := a C node (m w s), G s • e (mw s) . (4.9) Lemma 4.5.
(1) For every coherent family of glueing subdata G : CohGlueSubD w, the function subglue w G defined in (4.9) is extensional.
(3) For every γ : w ≈ W w , G : CohGlueSubD w and G : CohGlueSubD w there is Proof. (1) It follows from Lemma 2.13 and extensionality of a C .
(3) Let γ, G and G be as above, and let ϕ : G ≈ γ G and s : ISTrees w. Using extensionality of a C , it is enough to show (node (m w s), G s ) ≈ node (m w (ISTrees γ s)), G (ISTreesγ s) .
For the first component we may take α := node (m-coh γ) from Remark 3.9. It remains to show that, for every s : ISTrees w, By (4.8) it is enough to use ϕ.
Remark 4.6. Lemma 4.5 is the reason for choosing to work with the setoid family of immediate subtrees instead of the setoid family of branches of trees from Section 3.2.
Indeed, Proposition 4.3 works equally well with immediate subtrees replaced by branches of trees, in the sense that there are function terms between extensional functions W ⇒ C and families of glueing data on branches satisfying the same properties, mutatis mutandis, of restr and glue. Problems arise at the next subtree level.
If we say that, for an extensional tree w, a term of type b:B (node w) B (node (ist w b)) ⇒ C

Jacopo Emmenegger
Vol. 17:3 is a family of glueing subdata on branches for w in C, we can then form, for every w : W , the setoid CB w of coherent families of glueing subdata on branches for w. Nevertheless, it seems that transport of families of glueing subdata on branches over γ : w ≈ W w does not preserve coherence. It follows that, contrary to CohGlueSubD, the family λw.CB w is not a setoid family on W . In particular, there is no function term like CohGlueSubD γ from (4.7), which is needed, together with Lemma 4.5.3, in a crucial step in the proof of Lemma 4.13. The issue seems to arise because branches, contrary to subtrees, do not form a subsetoid of W .
Note that there is no version of restr for 2-subtrees: a function k : ISTrees w ⇒ C cannot be restricted to ISTrees (m w s) for some s : ISTrees w simply because an immediate subtree of m w s is not an immediate subtree of w, that is, being an immediate subtree is not a transitive property. This entails that, in order to say that k is a glueing, we need to assume that a coherent family of glueing subdata for w exists.
Definition 4.7. Let w ≡ s(a, f ) be an extensional tree. A function k : ISTrees w ⇒ C is telescopic over w if (1) the function k is the glueing of a coherent family of glueing subdata G : CohGlueSubD w, i.e. k ≈ subglue w G, and (2) for every s : ISTrees w, the function G s : ISTrees (f s) ⇒ C is telescopic over f s. Accordingly, we define the type family Henceforth we write isTelescopic w k for isTelescopic (w, k) and we may drop the subscript w if it is clear from context. Definition 4.8. A family F of glueing data in C is telescopic if each of its components is a telescopic function over w, that is, if w:W isTelescopic F w is inhabited. We say 'telescopic family' to mean 'telescopic family of glueing data in C'.
Telescopic families of glueing data form a subsetoid TelGlueD of GlueD. Remark 4.9. If we say, similarly, that a family G of glueing subdata for w is telescopic over w if each component G s is telescopic over the immediate subtree m w s, then Definition 4.7 can be phrased as: a function is telescopic over w if it is the glueing of a telescopic family of glueing subdata for w.
The canonical components of a proof that a function k : ISTrees w ⇒ C is telescopic are as follows, see also Figure 2. From the dependent node function (2.1) we obtain, for every T : isTelescopic w k, a coherent family of glueing subdata csfam k T : CohGlueSubD w (4.10) and a proof isglue k T : k ≈ subglue w (csfam k T ), (4.11) that k is a glueing of the family csfam k T . The dependent subtree function (2.2) yields a proof telsfam k T : (s:ISTrees w) isTelescopic (mw s) (csfam k T s).
(4.12) that every function in the family csfam k T is telescopic, that is, the family csfam of glueing subdata is itself telescopic, see Remark 4.9. Clearly, terms in (4.10) and (4.11) are proof terms for 4.7.1, and the term in (4.12) is a proof term for 4.7.2.
As we will extensively use the elimination principle in Figure 2 of the dependent W-type of telescopic functions from Definition 4.7, let us unfold its inductive hypothesis too. Let V : w,k isTelescopic k → U be a (small) type family. The inductive hypothesis tells us that, for T ≡ dsup (w, k) G E : isTelescopic k and for every s : ISTrees w, there is a term where G ≡ csfam T is the coherent family of glueing subdata obtained from T as in (4.10), and the term E ≡ telsfam T is the proof (4.12) that every G s is telescopic over m w s. The fact that functions ISTrees w ⇒ C cannot be restricted to families of glueing subdata, discussed right before Definition 4.7, does not prevent us from showing that restrictions of algebra morphisms are telescopic, since a function W ⇒ C can be restricted to any "subtree level". In particular, to immediate subtrees and 2-subtrees. Proof. The proof is by W-elimination on w : W |B| into the type . Note that, whenever we wish to apply the inductive hypothesis to a subtree index of w, we have to provide a proof that the subtree is extensional. Such a proof will always be available since w is extensional by assumption and immediate subtrees of extensional trees are extensional by Lemma 3.5. So we may assume without loss of generality that all the trees that we will be dealing with are extensional.
Let then w ≡ sup a f . By Definition 4.7, in order to apply dsup, we first need to provide a coherent family of glueing subdata G : CohGlueSubD w and a proof that h • m w ≈ subglue w G. It follows from this lemma that the function restr : (W ⇒ C) ⇒ GlueD from Proposition 4.3.3 restricts to a function restr : Alg(s, a C ) ⇒ TelGlueD (4.14) that maps an algebra morphism to its family of restrictions as defined in (4.4). Because of the bijection in Corollary 4.4, to see this we could have equally well proved that every coherent family F of glueing data such that restr (glue F ) ≈ F is a telescopic family. Our choice is justified by the fact that functions and their restrictions are simpler objects than families of glueing data and their restrictions. The fact that every such F is telescopic is an immediate consequence of the Lemma above and Proposition 4.3.6. Our aim is now to prove that glue lifts to an inverse of restr. To do so we need to prove that a telescopic family is coherent. Then by Proposition 4.3.6 it will be enough to show that a telescopic family is the restriction of its own glueing.
First we need some technical lemmas about telescopic functions. We begin by showing that any two functions which are telescopic over the same w are necessarily equal. A more general version is in Lemma 4.14.
As family of glueing subdata for w we can take the family G defined by G s := F (mw s) . Its coherence follows as for Corollary 4.15 from Lemma 4.14 and the fact that F is telescopic.
by definition of glue in (4.5) and definition of subglue in (4.9). Since for every w, the function F w is telescopic, this is the case in particular for G s ≡ F mw s for every s : ISTrees w.
Corollary 4.17. For every telescopic family F , it is Proof. The first equality follows from Lemma 4.16 and Lemma 4.11. So the function glue F is an algebra morphism by Proposition 4.3.6.
It follows that the function glue : CohGlueD ⇒ (W ⇒ C) defined in (4.5) restricts to a function glue : TelGlueD ⇒ Alg(s, a C ) (4.15) from telescopic families to algebra morphisms. Proof. Proposition 4.3.5 yields that glue (restr h) ≈ h for every h : Alg(s, a C ). The other equality is in Corollary 4.17.
As an immediate consequence, we see from Corollary 4.12 that the setoid of algebra morphisms is a subsingleton. It is then clear that Alg(s, s) is a singleton, and that the only telescopic family in this case is m. It only remains to construct an inhabitant in the general case.
Proposition 4.19. There is a telescopic family of glueing data in C, i.e. a term telfam : TelGlueD.
Proof. Given an extensional tree w : W , we need to show that there is a telescopic function The proof is by W-elimination on w : W |B| into the type W per B w w −→ k:ISTrees w⇒C isTelescopic k.
As in the proof of Lemma 4.10, we may assume without loss of generality that all the trees in the proof are extensional. We have a tree of the form w ≡ sup a f and the inductive hypothesis consists, for every s : ISTrees w, of a telescopic function This is the same as a telescopic family G of glueing subdata for w, see Remark 4.9.
It follows from Lemma 4.14 that G is coherent, so we may define telfam w := subglue w G : ISTrees w ⇒ C as the glueing of a telescopic family G of glueing subdata for w. It is a telescopic function by definition.
4.2. Initiality. We have finally reached our main result.
Theorem 4.20. For every setoid family B over a setoid A, the associated polynomial endofunctor P has an initial algebra. It follows that the category of setoids Std has initial algebras for polynomial endofunctors.
Proof. Given a setoid family B over A, the initial algebra for the polynomial functor P is the algebra s B : PW ⇒ W of extensional trees constructed in Section 3.1. It follows by Theorem 4.18 that, for every algebra a C , the only morphism of algebras from s to a C is the function glue(telfam) : W ⇒ C, where glue : TelGlueD ⇒ Alg(s B , a C ) is the glueing function from Theorem 4.18 and telfam is the only telescopic family of glueing data in C, that we constructed in Proposition 4.19 and proved unique in Corollary 4.12.
It is straightforward to verify that every polynomial functor P on Std has initial algebra s B • i W : P W ⇒ W , where i is a natural isomorphism from P to P as in Definition 2.10.
Gambino and Hyland have proved that, in a locally cartesian closed category, dependent W-types can be constructed from non-dependent ones, thus providing justification for an analogous result in extensional type theory claimed in [PS89]. As a consequence of Theorem 4.20, we expect that setoids have dependent W-types too, i.e. that the category of Std has initial algebras for dependent polynomial endofunctors.

Trees on discrete setoids
In this section we work with the additional assumption that our theory has identity types [NPS90,Chapter 20.4], see also [UFP13, Chapter 1.12]. We unfold the definition of the setoid of extensional trees in the case of a discrete setoid family, i.e. a setoid family coming from an extensional function between discrete setoids. We then compare it to the discrete setoid on the W-type of the underlying type family. The main result is Theorem 5.8.
In comparing the two, we will also make use of function extensionality. This is because the functions of immediate subtrees of two matching trees are only required to be pointwise equal, as in the right-hand side of (W2). Therefore, rather than to compare the two directly, we prefer to identify another (total) equivalence relation on W |B| , which is logically equivalent to the identity type on W |B| in the presence of function extensionality, and to compare it to W per B in the general case.
Recall that x = X x denotes the identity type of two terms x, x : X. For a type family Y : X → U, we denote as Y ξ : Y x → Y x the transport function term given by identity elimination on ξ : x = X x , and we write y = ξ y for the identity type Y ξ y = (Y x ) y .
Definition 5.1. A setoid family B : Fam X over the discrete setoid on X is discrete over X if, for every x : X and b, b where |B| ξ : B x → B x is the transport function of the underlying type family |B| of B given by elimination of the identity type.
The equivalence in Theorem 2.8 restricts to an equivalence between the full subcategory of Std/X on the functions into X from a discrete setoid, and the full subcategory of Fam X on the discrete setoid families over X.
It follows in particular that every type family Y : X → U gives rise to a discrete setoid family over X, by equipping each fibre Y x with the equality x is the transport function given by elimination of the identity type. 5.2. Pointwise equality of trees. Next, we look for an equivalent characterisation of the discrete setoid on a W-type in the presence of function extensionality.
Definition 5.2. Let Y : X → U be a family of small types. Two trees w ≡ sup x f and w ≡ sup x f in W Y are pointwise equal if ξ : x = X x and, for every y ∈ Y x and y : Y x , if y = ξ y then the immediate subtrees f y and f y are pointwise equal.
The type family ptEq Y : W Y → W Y → U of proofs that w and w are pointwise is defined as the (curried version of the) dependent W-type on I := W Y × W Y with family of nodes N := λ(w, w ).node w = X node w : W Y × W Y → U branching family Br := λ(w, w ), p. y,y y = ξ y : I N (w, w ) → U and arity function ar := λ(w, w ), p, (y, y , q).(ist w y, ist w y ) : It is possible to show that ptEq Y is a symmetric and transitive relation on W Y as for W per B in Proposition 3.3. Reflexivity follows from the following lemma. Lemma 5.3. For every w, w : Thus we have a setoid W Y of pointwise-equal trees, that is the type W Y with ptEq as equality, together with an extensional function Proof of Lemma 5.3. The proof is by induction on w ≡ sup x f and the inductive hypothesis tells us that if a tree is equal to an immediate subtree of w, then it is pointwise equal to it. By induction on r : w = w , it is enough to show that ptEq Y w w. A canonical term is given taking the canonical proof of x = X x and proving that, for every y, y : Y x, if y = (Y x) y then the trees f y and f y are pointwise equal. This follows from the inductive hypothesis using the fact that every function term-f in particular, is extensional with respect to the identity type.
Unsurprisingly, it is for the converse that we need function extensionality.
Lemma 5.4. Assume that the theory has function extensionality, i.e. that there is a constant funext as given in the rule below.
Then for every w, w : Proof. Let w ≡ sup x f and w ≡ sup x f '. The proof is by induction on the term E : ptEq w w . To conclude w = w it is enough to show that there is ξ : The existence of p follows by definition of ptEq w w . By function extensionality and the inductive hypothesis it is then enough to show that, for every y : Y x, the immediate subtrees f y and f (Y ξ y) are pointwise equal. This also follows by definition of ptEq w w .
It follows that, in the presence of function extensionality, the discrete setoid on W Y is isomorphic to the setoid W Y of pointwise-equal trees via the function q Y in (5.1). 5.3. Equivariant trees. Now we look for a characterisation of the setoid W B of extensional trees of a discrete setoid family B as subsetoid of W |B| , i.e. as a setoid of pointwise equal trees.
Definition 5.5. Let Y : X → U be a type family. A tree w ≡ sup x f : W Y is equivariant if, for every l : x = X x and every y : Y x, the immediate subtrees f (Y l y) and f y are pointwise equal, and every immediate subtree of w is equivariant.
The type family isEquivariant : W Y → U of proofs that a tree is equivariant is defined as the dependent W-type in I := W Y with families of nodes and branches N := λw.
Let EqvTrees Y be the subsetoid of W Y on the equivariant trees, that is to say, two equivariant trees are equal in EqvTrees Y if they are pointwise equal.
Let also EqvTrees = Y be the subsetoid of W Y on the equivariant trees, that is, two equivariant trees w and w ar equal in EqvTrees = Y if w = W Y w .

Jacopo Emmenegger
Vol. 17:3 Let B : Fam X be a discrete setoid family over the discrete setoid on X : U. Note first that, by its very definition, for every ξ : x = X x and every b : B x, there is l : 3) Recall that we write B ξ for the transport of the setoid family B, and |B| ξ for the transport of the underlying type family |B|-the former being given by definition and the latter by elimination of the identity type. The matching relation W per B was defined in 3.1. Lemma 5.6. Let B : Fam X be a discrete setoid family over the discrete setoid on X : U.
(2) Every extensional tree is equivariant, i.e. for every w : W |B| , (1) The proof is by induction on w ≡ sup x f : W |B| and the inductive hypothesis tells us that if a tree is matching with an immediate subtree f b of w, then it is pointwise equal to f b.
Let then w ≡ sup x f be a tree an suppose that w and w are matching. To prove that w and w are pointwise equal we need first to provide a proof ξ : x = X x , which is given by α in Remark 3.2. We then need to show that for every b : B x and b : B x , if B ξ b ≈ B x b , then the immediate subtrees f b and f b are pointwise equal. By the inductive hypothesis, it is enough to show that the trees f b and f b are matching, which holds by definition.
(2) The proof is again by induction on w : W |B| , into the type W per B w w −→ isEquivariant w. By the inductive hypothesis and the fact that every subtree of an extensional subtree is extensional 3.5, it follows that every immediate subtree of w is equivariant. To prove that w is equivariant, it only remains to show that for every l : x = X x and every b : B x, the subtrees f (|B| l b) and f b are pointwise equal. By (1) just proved and the fact that f is extensional 3.5, it is enough to prove that |B| l b ≈ B x b. This is immediate by Definition 5.1 and standard properties of transport along identity proofs.
It follows from Lemma 5.6 that there is an extensional function which is the identity on the underlying trees. The next lemma proves that j B is injective.
Lemma 5.7. Let B : Fam X be a discrete setoid family over the discrete setoid on X : U, and let w, w : W |B| be equivariant trees. Then ptEq |B| w w −→ W per B w w . Proof. This is proven by induction on the proof E that w ≡ sup x f is equivariant, into the type w :W Y isEquivariant w → ptEq |B| w w → W per B w w .
Let then w ≡ sup x f be equivariant and suppose that w and w are pointwise equal. In particular, there is ξ : x = X x . So to prove that w and w are matching it is enough to show that for every b : B Theorem 5.8. Let B : Fam X be a discrete setoid family over the discrete setoid on X.
(1) The function j B in (5.4) is a bijection between the setoid of extensional trees W B and the setoid of equivariant trees EqvTrees |B| on the underlying type family |B|.
(2) Assuming function extensionality (5.2), the setoid of extensional trees W B on B is in bijection with the subsetoid EqvTrees = Y of the (discrete setoid on the) W-type W |B| . Proof.
(2) By Lemmas 5.3 and 5.4, the discrete setoid of trees W |B| is isomorphic to the setoid W |B| of pointwise-equal trees. Now the claim follows by (1) since the setoid of equivariant trees EqvTrees |B| is a subsetoid of W |B| by definition.
Even if assuming function extensionality does not seem to ensure, in general, that the setoid W B of extensional trees with respect to a discrete setoid family B is discrete, this happens whenever the base type is a 0-type, i.e. a type with decidable equality.
Corollary 5.9. Let B : Fam X be a discrete setoid family over the discrete setoid on X, and suppose that the type X is a 0-type.
(1) Every tree in W |B| is extensional with respect to B. In particular, the setoid W B of extensional trees is in bijection with the setoid W |B| of pointwise-equal trees.
(2) Assuming function extensionality (5.2), the setoid W B of extensional trees is in bijection with the discrete setoid on the W-type W |B| .
Proof. We only need to prove that every tree is extensional, as all the other claims follow from Theorem 5.8. A tree is extensional if and only if it is equivariant by Lemmas 5.6 and 5.7, and it is straightforward to prove that every tree in W |B| is equivariant, by induction on the tree and using the assumption that X is a 0-type.