Lably
Skip to content

Semantics of type theory

  • 16 min read
  • 6 chapters

Related on Lably

Through Type theory and Category (mathematics)

There are several ways to package the structure of a model of type theory, including categories with families, comprehension categories, categories with attributes and contextual categories. In all cases, a model is a base substrate, together with extra structure and requirements for each rule of the type theory under consideration.

Categories with families

Categories with families, introduced by Dybjer, are a commonly used notion of model which stays relatively close to the syntax of type theory.

Introduction

The CwF structure is designed to closely parallel the syntax of type theory.

  • To each derivable syntactic judgment of the form “Γ is a context”, the semantics in a CwF associates an element Γ^ of a set of contexts denoted Con.
  • To each derivable judgment “A is a type in context Γ” is associated an element A^ of a set of types in context Γ^ denoted Ty⁡(Γ^). (Here A^ depends on Γ and not merely on A although this dependency is dropped in the notation.)
  • To each derivable judgment “t is a term of type A in context Γ” (usually denoted Γ⊢t:A) is associated an element t^ of a set of terms of type A^ in context Γ^, denoted TmΓ^⁡(A^).

(The terminology may be confusing because the "terms" of the model need not be finite syntactic objects as in the traditional sense of "term" in mathematical logic, but merely constitute the semantics of the syntactic terms of the type theory. Some authors instead call them elements, or refer to semantic contexts, semantic types and semantic terms.)

The semantic interpretation of definitional equality is always actual equality in the model, so whenever a judgment “A and B are definitionally equal types in context Γ” is derivable, the interpretation in the model must ensure A^=B^, and whenever “t and u are definitionally equal terms of type A in context Γ” is derivable, the interpretation ensures t^=u^.

A CwF contains operations to interpret contexts: a special element ⋄∈Con to model the empty context, and a context extension operation ▹ to model the addition of a new variable to a context.

The remaining data of CwFs serves to interpret the substitution operation necessitated by dependent types: if f has the dependent function type (x:A)→B where B is a term involving the variable x, the application of f to a term t has the type B[x:=t]. CwFs use parallel substitutions, namely substitutions between entire contexts. If there were a judgment in the syntax which was directly interpreted by a substitution from Γ^ to Δ^ in the CwF, it would be “σ is a list of terms in context Γ, one for each type in Δ, having this type with the previous variables from Δ substituted by the previous terms in the list” (e.g., a parallel substitution from Γ to x:A,y:B in the syntax would be a term t of type A in context Γ and a term u of type B[x:=t] in context Γ).

Definition

Let Fam denote the category of families of sets. An object of Fam is a family (Ai)i∈I where I is a set and each Ai is a set. A morphism f:(Ai)i∈I→(Bj)j∈J in Fam is a function r:I→J together with, for each i∈I, a function ti:Ai→Br(i). (This category is equivalent to the arrow category of Set, through the correspondence between a function f:A→I and the family of sets (f−1({i}))i∈I.)

A category with families (CwF) consists of the following data:

  • A category.

    The class of objects is denoted Con and the objects are called contexts. The class of morphisms from a context Δ to a context Γ is denoted Sub⁡(Δ,Γ) and these morphisms are called substitutions from Δ to Γ.

  • A contravariant functor from this category to Fam.

    The image of a context Γ is a family of sets denoted (TmΓ⁡(A))A∈Ty⁡(Γ) (we usually drop the subscript in TmΓ⁡(A)). The elements of Ty⁡(Γ) are called types in context Γ, and the elements of Tm⁡(A) are called terms of type A (in context Γ).

    The image of a substitution σ∈Sub⁡(Δ,Γ) is a morphism (Tm⁡(A))A∈Ty⁡(Γ)→(Tm⁡(B))B∈Ty⁡(Δ) in Fam. Its component Ty⁡(Γ)→Ty⁡(Δ) is denoted by A↦A[σ], and for each A∈Ty⁡(Γ), its component Tm⁡(A)→Tm⁡(A[σ]) is also denoted t↦t[σ]. These operations are respectively called substitution in types and substitution in terms.

    Alternatively, this data can be packaged as a presheaf of types Ty on the category of contexts, and a dependent presheaf of terms Tm on the category of contexts over the presheaf of types.

  • A terminal object in the category of contexts.

    It is denoted ⋄ and called the empty context.

  • For each context Γ and for each type A∈Ty⁡(Γ), consider the presheaf on the category of contexts which sends a context Δ to the set of pairs of a substitution σ∈Sub⁡(Δ,Γ) and a term t∈TmΔ⁡(A[σ]). The functorial action of the presheaf on a substitution δ∈Sub⁡(Σ,Δ) is given by (σ,t)↦(σ∘δ,t[δ]). The last part of the CwF data is a representing object for this presheaf, together with a natural isomorphism witnessing the representation.

    The representing context is denoted Γ▹A and called the context extension of Γ by A.

    In one direction, the natural isomorphism sends a substitution σ∈Sub⁡(Δ,Γ) and a term t∈Tm⁡(A[σ]) to a new substitution (σ▹t)∈Sub⁡(Δ,Γ▹A) called the substitution extension of σ by t. In the other direction, it sends a substitution σ′∈Sub⁡(Δ,Γ▹A) to a pair of its tail tl⁡(σ′)∈Sub⁡(Δ,Γ) and its head term hd⁡(σ′)∈Tm⁡(A[tl⁡(σ′)]).

    Explicitly, the fact that this is a natural isomorphism means that the following laws hold:tl⁡(σ▹t)=σhd⁡(σ▹t)=tσ′=tl⁡(σ′)▹hd⁡(σ′)(σ▹t)∘δ=(σ∘δ)▹t[δ]tl⁡(σ′)∘δ=tl⁡(σ′∘δ)hd⁡(σ′)[δ]=hd⁡(σ′∘δ)

Defined operations

Let Γ be a context and A a type in Γ. The head and tail of the identity substitution from Γ▹A to itself are respectively a substitution wk∈Sub⁡(Γ▹A,Γ) called weakening, and a term vz∈TmΓ▹A⁡(A[wk]) called the head variable. Informally, the weakening adds an unused variable to the context, and the head variable is the term given by the variable last declared in the context.

Observe that weakening and the head variable allow to recover the tail and head of any substitution σ∈Sub⁡(Δ,Γ▹A) by tl⁡(σ)=tl⁡(id∘σ)=tl⁡(id)∘σ=wk∘σ and hd⁡(σ)=hd⁡(id∘σ)=hd⁡(id)[σ]=vz⁡[σ].

Furthermore, given a substitution σ∈Sub⁡(Δ,Γ), and a type A∈Ty⁡(Γ), we define a lifted substitution σ+∈Sub⁡(Δ▹A[σ],Γ▹A) by σ+:=(σ∘wk)▹vz.

Extra structure for type formers

For each rule of a particular type theory, there is added structure and/or requirements on a CwF for it to model the type theory.

  • A rule which introduces a type or term former is interpreted by an operation returning a type or term of the CwF, which is required to commute with substitution. The latter requirement is necessary since substitution is defined recursively in the syntax, e.g., (A×B)[x:=t] is A[x:=t]×B[x:=t].
  • A rule adding definitional equalities is interpreted by a requirement that the operations on the CwF satisfy a corresponding equation.

To illustrate this, consider the case of dependent pair types (Σ-types). The type formation rule is

ΓcontextΓ⊢AtypeΓ,x:A⊢BtypeΓ⊢(x:A)×Btype

The corresponding operation on a CwF is: for each Γ∈Con, for each A∈Ty⁡(Γ), and for each B∈Ty⁡(Γ▹A), a type Σ(A,B)∈Ty⁡(Γ). This operation is required to satisfy the substitution rule: for all substitution σ∈Sub⁡(Δ,Γ) and types A∈Ty⁡(Γ),B∈Ty⁡(Γ▹A), we must have Σ(A,B)[σ]=Σ(A[σ],B[σ+]).

The term formation rules for the pairing operation is:

ΓcontextΓ⊢AtypeΓ,x:A⊢BtypeΓ⊢t:AΓ⊢u:B[x:=t]Γ⊢⟨t,u⟩:(x:A)×B

The corresponding operation on a CwF is: for each Γ∈Con, A∈Ty⁡(Γ), B∈Ty⁡(Γ▹A), t∈Tm⁡(A) and u∈Tm⁡(B[id▹t])), a term ⟨t,u⟩∈Tm⁡(Σ(A,B)). This must satisfy the substitution rule:

⟨t,u⟩[σ]=⟨t[σ],u[σ]⟩.

This equation is meaningful because of the CwF laws and the substitution rule for Σ. The left hand side is an element of Σ(A,B)[σ], which by the substitution rule for Σ is equal to Σ(A[σ],B[σ+]). To see that the right hand side is in this set, one has to check that u[σ] is in B[σ+][id▹t[σ]]. This holds because u[σ] is in B[id▹t][σ] and

B[id▹t][σ]=B[(id▹t)∘σ]=B[(id∘σ)▹t[σ]]=B[σ▹t[σ]]

while

B[σ+][id▹t[σ]]=B[(σ∘wk)▹vz][id▹t[σ]]=B[((σ∘wk)▹vz)∘(id▹t[σ])]=B[((σ∘wk)∘(id▹t[σ]))▹vz⁡[id▹t[σ]]]=B[σ∘(wk∘(id▹t[σ]))▹vz⁡[id▹t[σ]]]=B[σ∘tl⁡(id▹t[σ])▹hd⁡(id▹t[σ])]=B[(σ∘id)▹t[σ]]=B[σ▹t[σ]].

Finally, the remaining rules for dependent pair types are the computation rules (or β-rules)

ΓcontextΓ⊢AtypeΓ,x:A⊢BtypeΓ⊢t:AΓ⊢u:B[x:=t]Γ⊢fst⁡(⟨t,u⟩)≡t:A

ΓcontextΓ⊢AtypeΓ,x:A⊢BtypeΓ⊢t:AΓ⊢u:B[x:=t]Γ⊢snd⁡(⟨t,u⟩)≡u:B[x:=t]

and the uniqueness rule (or η-rule)

ΓcontextΓ⊢AtypeΓ,x:A⊢BtypeΓ⊢p:(x:A)×BΓ⊢p≡⟨fst⁡(p),snd⁡(p)⟩:(x:A)×B

These are interpreted by requiring the following equations on the previous operations:

  • fst⁡(⟨t,u⟩)=t and snd⁡(⟨t,u⟩)=u (for Γ∈Con, A∈Ty⁡(Γ), B∈Ty⁡(Γ▹A), t∈Tm⁡(A), u∈Tm⁡(B[id▹t])),
  • p=⟨fst⁡(p),snd⁡(p)⟩ (for Γ∈Con, A∈Ty⁡(Γ), B∈Ty⁡(Γ▹A), p∈Tm⁡(Σ(A,B))).

Universes

The case of universes is somewhat special because it is common to define the syntax of type theory without a separate judgment for “A is a type in context Γ”, instead using “A is a term of type U in context Γ”, i.e., Γ⊢A:Un, where Un is the n-th universe (also denoted Typen, especially in proof assistants). This is called Russell universes. To give semantics to this kind of type theory, one needs to requirement in the CwF structure that the interpretation of both kinds of judgments in the semantics coincide.

First consider the simpler case of a single Russell universe U containing itself (the syntax has the type in type rule that U has type U, which is inconsistent). The corresponding requirements on a CwF are:

  • For each context Γ, a type U∈Ty⁡(Γ) (we leave the dependency on Γ implicit to lighten the notation), with the substitution rule U[σ]=U. (This is equivalent to the data of a single type in the empty context U∈Ty⁡(⋄), the version in an arbitrary context Γ being recovered as U[ϵ] where ϵ is the unique substitution from Γ to ⋄.)
  • An equality of sets Ty⁡(Γ)=TmΓ⁡(U) for every context Γ.

For a hierarchy of universes U0, U1 where Un has type Un+1, …, there is a complication since types are stratified in the syntax and therefore must also be stratified in the semantics. This is treated using a slight variant of CwFs which consists of a category of contexts and substitutions, a terminal object for the empty context, and for each universe level n, a functor from this category to families of sets, mapping a context Γ to a family Γ↦(TmΓ⁡(A))A∈Tyn⁡(Γ) of types of level n and terms of these types, equipped with an operation to extend the context with a type of level n (giving for A∈Tyn⁡(Γ) a bijection Sub⁡(Δ,Γ▹nA)≅(σ∈Sub⁡(Δ,Γ))×TmΔ⁡(A[σ]) natural in Δ). One can then model universes with, for each level n, types Un∈Tyn+1⁡(Γ) with the substitution rule Un[σ]=Un, and an equality of sets Tyn⁡(Γ)=TmΓ⁡(Un).

A slightly weaker variant is Coquand universes, which require a bijection between the sets Tyn⁡(Γ) and TmΓ⁡(Un) natural in Γ, instead of an equality. This corresponds to a syntax with a primitive operation El that takes an element of the universe U (a “code” for a type) and gives an actual type, and another primitive operation code that converts back a type into a code, with definitional equalities code⁡(El⁡(t))≡t and El⁡(code⁡(A))≡A. Coquand's original motivation for introducing this style of universes is that they are the most natural to construct in presheaf models.

Finally, an even weaker variant is Tarski universes, which only have the El operation but not a code operation, and which require, for each type former, a counterpart on the level of codes, e.g., an operation −×c− on TmΓ⁡(U) with the definitional equality El⁡(t×cu)≡El⁡(t)×El⁡(u), etc.

Some type theories have universe cumulativity: a type in a universe is also automatically a type in all higher universes. This can be treated in various ways, including subset inclusions between the Tyn⁡(Γ), and several variants of lifting operations between universes.

Democracy

The basic intuition stemming from the syntax is that a context is a list of types x0:A0,…,xk−1:Ak−1, which is essentially equivalent to an iterated Σ-type (x0:A0)×⋯×(xk−2:Ak−2)×Ak−1. This is not true in all models, but it can be a useful property to consider.

A CwF is democratic when every context Γ is isomorphic (in the category of contexts) to ⋄▹A for some closed type A (i.e., a type in the empty context ⋄).

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication