W-types

# W-types

## Idea

In type theory, by a $\mathcal{W}$-type [Martin-Löf (1984), pp. 43] one means a type which is defined inductively in a $\mathcal{W}$ell-founded way based on a type $C$ of “constructors” and a type of $A$ of “arities”. As such, $\mathcal{W}$-types are special kinds of inductive types (see below).

The same terminology “$\mathcal{W}$-type” is used [Moerdijk & Palmgren (2000)] for objects in (suitable pre-)toposes which provide categorical semantics for the type-theoretic notion (see further below).

Concretely, the terms/elements of a $\mathcal{W}$-type may be thought of as “rooted well-founded trees” with a certain branching type; different $\mathcal{W}$-types are distinguished by their branching signatures: In categorical semantics in Sets, a branching signature is represented by a family of sets $\{A_c\}_{c \in C}$ such that

• each node of the tree is labeled with an element $c \colon C$ – referring to a constructor;

• if a node is labeled by $c$ then it has exactly ${|A_c|}$ outgoing edges, each labeled by some $a \colon A_c$ – the arity of the constructor $c$.

If one discards the requirement that the trees be well-founded, then the notion of $\mathcal{W}$-type becomes that of a coinductive type called an M-type (presumably since “M” is like a “W” upside down).

In practice, $\mathcal{W}$-types are used to:

1. provide a constructive counterpart of the classical notion of a well-ordering

2. uniformly define a variety of well-behaved inductive types.

More complex inductive types, with multiple constructors that are assumed only to be strictly positive, can be reduced to $\mathcal{W}$-types, at least in the presence of other structure such as sum types and function extensionality; see for instance Abbott, Altenkirch & Ghani (2004). This can even be extended to inductive families.

In most set theoretic mathematical foundations, $\mathcal{W}$-types can be proven to exist, but in predicative mathematics and in type theory, where this is not the case, they are often introduced axiomatically (as usual for inductive types more generally).

## Definition

There are two slightly different formulations of W-types:

### $\mathcal{W}$-types in type theory

###### Definition

(inference rules for $\mathcal{W}$-types)

$\frac { C \,\colon\, Type \;; \;\;\; c \,\colon\, C \;\;\vdash\;\; A(c) \,\colon\,Type \mathclap{\phantom{\vert_{\vert}}} } { \mathclap{\phantom{\vert^{\vert}}} \underset{c \colon C}{\mathcal{W}}\, A(c) \,\colon\, Type }$
$\frac{ \vdash\; root \,\colon\, C \;; \;\; subtr \,\colon\, A(root) \to \underset{c \colon C}{\mathcal{W}}\, A(c) }{ \mathclap{\phantom{\vert^{\vert}}} tree\big(root ,\, subtr\big) \,\colon\, \underset{c \colon C}{\mathcal{W}}\, A(c) }$
$\frac{ \begin{array}{l} t \,\colon\, \underset{c \colon C}{\mathcal{W}}\, A(c) \;\vdash\; D(t) \,\colon\, Type \;; \\ root \,\colon\, C \,, \; subt \,\colon\, A(root) \to \underset{c \colon C}{\mathcal{W}}\, A(c) \,, \; subt_D \,\colon\, \underset{a \colon A(root)}{\prod} D\big(subt(a)\big) \\ \;\;\;\;\;\;\;\;\;\;\;\;\;\;\; \;\;\vdash\;\; tree_D\big(root,\,subt ,\, subt_D\big) \,\colon\, D\big(tree(c,\, subt)\big) \mathclap{\phantom{\vert_{\vert}}} \end{array} }{ \mathclap{\phantom{\vert^{\vert}}} t \,\colon\, \underset{c \colon C}{\mathcal{W}}\, A(c) \;\vdash\; wrec_{(D,tree_D)}(t) \,\colon\, D(t) }$

(4) computation rule:

$\frac{ \begin{array}{l} t \,\colon\, \underset{c \colon C}{\mathcal{W}}\, A(c) \;\vdash\; D(t) \,\colon\, Type \;; \\ root \,\colon\, C \,,\; subt \,\colon\, A(root) \to \underset{c \colon C}{\mathcal{W}}\, A(c) \,, \; subt_D \,\colon\, \underset{a\colon A(c)}{\Pi} D\big(subt(a)\big) \\ \;\;\;\;\;\;\;\;\;\;\;\;\;\;\; \;\;\vdash\;\; tree_D\big(root,\,subt,\,subt_D\big) \,\colon\, D\big(tree(root,\, subt)\big) \mathclap{\phantom{\vert_{\vert}}} \end{array} }{ \begin{array}{l} \mathclap{\phantom{\vert^{\vert}}} root \,\colon\, C \,, \;\; subtr \,\colon\, A(root) \to \underset{c \colon C}{\mathcal{W}}\, A(c) \\ \;\;\;\;\;\;\;\;\;\;\; \;\;\vdash\;\; wrec_{(D,tree_D)}\big( tree(root, \, subtr) \big) \;=\; tree_D \Big( root ,\, subt ,\, \lambda a . wrec_{(D,tree_D)}\big(subtr(a)\big) \Big) \end{array} }$

(due to Martin-Löf (1984), pp. 43; streamlined review e.g. in Awodey, Gambino & Sojakova (2012, §2))

###### Remark

If the type theory is extensional, i.e. identities have unique proofs and (dependent) function types are extensional ($f=g$ if and only if $f(x)=g(x)$ for all $x$), then this is more or less equivalent to the 1-category-theoretic definition below. The type theories that are the internal logic of familiar kinds of categories are all extensional in this sense.

The main distinction from the naive categorical theory below is that the map $f$ (1) must be assumed to be a display map, i.e. to exhibit $A$ as a dependent type over $B$, in order that the dependent product $\Pi_f$ be defined.

In the case of dependent polynomial functors, it seems that $q$ must also be a display map, in order to define $\Sigma_q$. However, using adjointness, one can still define the W-type even if $q$ is not a display map. This more general version is what in type theory is called an indexed W-type; if $q$ is a display map then one sometimes refers to non-uniform parameters instead of indices. (By contrast, uniform parameters are the kind discussed above where $g f = h$, so that the entire construction takes place in a single slice category This is an instance of the red herring principle, since non-uniform parameters are not really parameters at all, but a special kind of indices.) For example, identity types are indexed W-types but not parametrized ones (even non-uniformly); see this blog post.)

###### Remark

In intensional type theory, a $\mathcal{W}$-type is only an initial algebra with respect to propositional equality, not definitional equality. In particular, the constructors are injective only propositionally, not definitionally. This applies already for the natural numbers type (Exp. ).

### $\mathcal{W}$-types in categories

#### Plain version

We discuss the categorical semantics of $\mathcal{W}$-types, due to Moerdijk & Palmgren (2000).

Here one describes a $\mathcal{W}$-type as an initial algebra for an endofunctor on an ambient category $\mathcal{C}$. The family $\{A_c\}_{c\in C}$ can be thought of as a morphism

(1)$\array{ A \\ \big\downarrow\mathrlap{^{f}} \\ C }$

in some category $\mathcal{C}$ (such that the fiber over $c\in C$ is $A_c$), and the endofunctor in question is the composite

$\mathcal{C} \overset{A^*}{\longrightarrow} \mathcal{C}_{/A} \overset{\Pi_f}{\longrightarrow} \mathcal{C}_{/C} \overset{\Sigma_B}{\longrightarrow} \mathcal{C} \,,$

where

• $A^*$ denotes context extension, hence the pullback functor (a.k.a. $A\times -$);

• $\Pi_f$ denotes the interpretation of the dependent product, i.e. the right base change along $f$;

• $\Sigma_C$ the denotes the interoretation of the dependent sum, i.e. the left base change given by the forgetful functor from $\mathcal{C}_{/B}$ to $\mathcal{C}$.

Such a composite is called a polynomial endofunctor.

This definition makes sense in any locally cartesian closed category, although the W-type (the initial algebra) may or may not exist in any given such category. (A non-elementary construction of them is given by the transfinite construction of free algebras.)

The above definition is most useful when the category $\mathcal{C}$ is not just locally cartesian closed but is a Π-pretopos, since often we want to use at least coproducts in constructing $A$ and $C$. For example, a natural numbers object (Exp. ) is a $\mathcal{W}$-type specified by one of the coproduct inclusions $1\to 1+1$, and the list object $L X$ is a $\mathcal{W}$-type specified by $X\to X+1$. More generally, endofunctors that look like polynomials in the traditional sense:

$F(Y) = A_n \times Y^{\times n} + \dots + A_1 \times Y + A_0$

can be constructed as polynomial endofunctors in the above sense in any $\Pi$-pretopos. A $\Pi$-pretopos in which all W-types exist is called a ΠW-pretopos.

In a topos with a natural numbers object (NNO), W-types for any polynomial endofunctor can be constructed as certain sets of well-founded trees; thus every topos with a NNO is a ΠW-pretopos. This applies in particular in the topos Set (unless one is a predicativist, in which case $Set$ is not a topos and W-types in it must be postulated explicitly).

#### Dependent W-types

The above has a natural generalization to dependent or indexed W-types (Gambino & Hyland (2004)) with a type $C$ of indices:

given a diagram of the form

$\array{ A &\stackrel{f}{\longrightarrow}& B \\ \downarrow^{\mathrlap{h}} && \downarrow^{\mathrlap{g}} \\ C && C }$

there is the dependent polynomial functor

$\mathcal{C}_{/C} \overset{h^\ast}{\longrightarrow} \mathcal{C}_{/A} \overset{\Pi_f}{\longrightarrow} \mathcal{C}_{/B} \overset{\Sigma_g}{\longrightarrow} \mathcal{C}_{/C} \,,$

This reduces to the above for $C = \ast$ the terminal object.

Notice that we do not necessarily have $g f = h$, so this is not just a polynomial endofunctor of $\mathcal{C}/_{C}$ considered as a lccc in its own right. If we do have $g f = h$, then $C$ is called a type of parameters instead of indices.

## Examples

###### Example

(natural numbers type as a $\mathcal{W}$-type)
The natural numbers type $(\mathbb{N},\, 0,\, succ)$ is equivalently the $\mathcal{W}$-type (Def. ) with

• $C \,\coloneqq\, \{0, succ\} \,\simeq\, \ast \sqcup \ast$;

• $A_0 \,\coloneqq\, \varnothing$ (empty type);

$A_{succ} \,\coloneqq\, \ast$ (unit type)

[Martin-Löf (1984), pp. 45, Dybjer (1997, p. 330, 333)]

## Properties

###### Proposition

(Danielsson (2012))
In homotopy type theory, if $C$ has h-level $n\geq -1$, then any $\mathcal{W}$-type of the form $\underset{C}{\mathcal{W}} B$ has h-level $n$ (as it should be for $\infty$-colimits).

## References

### General

The original definition in type theory is due to

On the h-level of W-types:

• Nils Anders Danielsson, Positive h-levels are closed under W (2012) [web]

which also computes the identity types of W-types (and more generally indexed W-types).

W-types in Coq: wiki

### Categorical semantics of $\mathcal{W}$-types

The observation that the categorical semantics of well-founded inductive types ($\mathcal{W}$-types) is given by initial algebras over polynomial endofunctors on the type system:

Further discussion:

Generalization to inductive families (dependent $\mathcal{W}$-types):

• Nicola Gambino, Martin Hyland, Wellfounded Trees and Dependent Polynomial Functors, in: Types for Proofs and Programs TYPES 200, Lecture Notes in Computer Science 3085, Springer (2004) $[$doi:10.1007/978-3-540-24849-1_14$]$

• Michael Abbott, Thorsten Altenkirch, Neil Ghani: Representing Nested Inductive Types using W-types, in: Automata, Languages and Programming, ICALP 2004, Lecture Notes in Computer Science, 3142, Springer (2004) $[$doi:10.1007/978-3-540-27836-8_8, pdf$]$

exposition: Inductive Types for Free – Representing nested inductive types using W-types, talk at ICALP (2004) $[$pdf$]$

Towards combining generalization to dependent and homotopical W-types:

Last revised on January 3, 2023 at 15:05:51. See the history of this page for a list of all contributions to it.