h5i

Chapter 4

The Typing Rules as a Logic

Chapter 3 presented eight typing rules as rules of logic. To make that claim precise, this chapter chooses a particular logic, translates its formulas into types, and shows that every proof in the logic becomes a term accepted by the kernel. It then explains why the kernel also needs consistency to be trustworthy.

A logic to compare against

The logic has formulas built from proposition variables P,Q,…P, Q, \ldots by implication and by quantification over propositions:

φ,ψ  ::=  P  ∣  φ→ψ  ∣  ∀P. φ\varphi, \psi \;::=\; P \;\mid\; \varphi \to \psi \;\mid\; \forall P.\,\varphi

This is second-order propositional logic: "second-order" because ∀\forall ranges over propositions rather than over individuals. It is small, but far from trivial. Conjunction, disjunction, negation, and falsity can all be defined inside it, for example ⊥=∀P. P\bot = \forall P.\,P and φ∧ψ=∀R. (φ→ψ→R)→R\varphi \wedge \psi = \forall R.\,(\varphi \to \psi \to R) \to R.

Proofs in this logic are natural-deduction derivations of judgments Δ⊢φ\Delta \vdash \varphi, where Δ\Delta is a list of hypotheses. The rules are the ones in the left column of Figure 4.1 below: use a hypothesis, introduce and eliminate implication, introduce and eliminate the quantifier.

Translating formulas into types

Each formula translates to a type, written φ∗\varphi^\ast:

P∗=P,(φ→ψ)∗=φ∗→ψ∗,(∀P. φ)∗=Π(P:Sort 0). φ∗.P^\ast = P, \qquad (\varphi \to \psi)^\ast = \varphi^\ast \to \psi^\ast, \qquad (\forall P.\,\varphi)^\ast = \tPi{P}{\Sort{0}}{\varphi^\ast}.

A proposition variable becomes a type variable, implication becomes the function type, and quantification over propositions becomes a Π\Pi-type over Sort 0\Sort{0}.

A list of hypotheses translates to a context. Besides one assumption hi:φi∗h_i : \varphi_i^\ast per hypothesis, the context must declare the proposition variables that occur anywhere in the judgment:

(φ1,…,φn)∗  =  P1:Sort 0,…,Pk:Sort 0,  h1:φ1∗,…,hn:φn∗,(\varphi_1, \ldots, \varphi_n)^\ast \;=\; P_1 : \Sort{0}, \ldots, P_k : \Sort{0},\; h_1 : \varphi_1^\ast, \ldots, h_n : \varphi_n^\ast,

where P1,…,PkP_1, \ldots, P_k are the proposition variables free in Δ\Delta or in the conclusion φ\varphi of the judgment Δ⊢φ\Delta \vdash \varphi being translated, and h1,…,hnh_1, \ldots, h_n are fresh names. The conclusion must be included because a variable may occur only there, as PP does in ⋅⊢P→P\cdot \vdash P \to P.

Figure 4.1 places each logical rule next to the typing rule that implements it. The premises that merely check that a type is well formed are omitted.

Figure 4.1. Natural deduction (middle column) and the typing rules that implement it (right column). In each row, Δ∗\Delta^\ast is the context for the conclusion.

Hypothesisφi∈ΔΔ⊢φi(hi:φi∗)∈Δ∗Δ∗⊢hi:φi∗;(T-Var)→-introΔ,φ⊢ψΔ⊢φ→ψΔ∗,h:φ∗⊢t:ψ∗Δ∗⊢λ(h:φ∗). t:φ∗→ψ∗;(T-Lam)→-elimΔ⊢φ→ψΔ⊢φΔ⊢ψΔ∗⊢f:φ∗→ψ∗Δ∗⊢a:φ∗Δ∗⊢f  a:ψ∗;(T-App)∀-introΔ⊢φP∉FV(Δ)Δ⊢∀P. φΔ∗,P:Sort 0⊢t:φ∗Δ∗⊢λ(P:Sort 0). t:Π(P:Sort 0). φ∗;(T-Lam)∀-elimΔ⊢∀P. φΔ⊢φ[P↦ψ]Δ∗⊢f:Π(P:Sort 0). φ∗Δ∗⊢ψ∗:Sort 0Δ∗⊢f  ψ∗:φ∗[P↦ψ∗];(T-App)\begin{array}{lcc} \text{Hypothesis} & \dfrac{\varphi_i \in \Delta}{\Delta \vdash \varphi_i} & \dfrac{(h_i : \varphi_i^\ast) \in \Delta^\ast}{\Delta^\ast \vdash h_i : \varphi_i^\ast}\rulename{T-Var} \\[18pt] \to\text{-intro} & \dfrac{\Delta, \varphi \vdash \psi}{\Delta \vdash \varphi \to \psi} & \dfrac{\Delta^\ast, h : \varphi^\ast \vdash t : \psi^\ast}{\Delta^\ast \vdash \tLam{h}{\varphi^\ast}{t} : \varphi^\ast \to \psi^\ast}\rulename{T-Lam} \\[18pt] \to\text{-elim} & \dfrac{\Delta \vdash \varphi \to \psi \qquad \Delta \vdash \varphi}{\Delta \vdash \psi} & \dfrac{\Delta^\ast \vdash f : \varphi^\ast \to \psi^\ast \qquad \Delta^\ast \vdash a : \varphi^\ast}{\Delta^\ast \vdash f\;a : \psi^\ast}\rulename{T-App} \\[18pt] \forall\text{-intro} & \dfrac{\Delta \vdash \varphi \qquad P \notin \FV(\Delta)}{\Delta \vdash \forall P.\,\varphi} & \dfrac{\Delta^\ast, P : \Sort{0} \vdash t : \varphi^\ast}{\Delta^\ast \vdash \tLam{P}{\Sort{0}}{t} : \tPi{P}{\Sort{0}}{\varphi^\ast}}\rulename{T-Lam} \\[18pt] \forall\text{-elim} & \dfrac{\Delta \vdash \forall P.\,\varphi}{\Delta \vdash \subst{\varphi}{P}{\psi}} & \dfrac{\Delta^\ast \vdash f : \tPi{P}{\Sort{0}}{\varphi^\ast} \qquad \Delta^\ast \vdash \psi^\ast : \Sort{0}}{\Delta^\ast \vdash f\;\psi^\ast : \subst{\varphi^\ast}{P}{\psi^\ast}}\rulename{T-App} \end{array}

Erasing the terms from the right column gives the middle column exactly. Two details deserve comment.

In the ∀\forall-intro row, the logic's side condition P∉FV(Δ)P \notin \FV(\Delta) guarantees that no hypothesis mentions PP, so the typing rule may add P:Sort 0P : \Sort{0} after the hypotheses without breaking the dependency order of the context.

In the ∀\forall-elim row, the premise ψ∗:Sort 0\psi^\ast : \Sort{0} holds for every formula ψ\psi, even one that contains ∀\forall, because T-Pi places every translated formula in Sort 0\Sort{0}. (The imax\imax in T-Pi is responsible: Π(P:Sort 0). φ∗\tPi{P}{\Sort{0}}{\varphi^\ast} lands in Sort imax(1,0)=Sort 0\Sort{\imax(1, 0)} = \Sort{0}.) So a universal statement can be instantiated with any formula, as it can in the logic. Chapter 13 returns to this point, which is called impredicativity.

Proofs are terms

Theorem 4.1 (Proofs are terms). If Δ⊢φ\Delta \vdash \varphi is provable in the logic, then Δ∗⊢t:φ∗\Delta^\ast \vdash t : \varphi^\ast for some term tt.

Figure 4.1 turns each step of a logical proof into a step of a typing derivation, so every proof becomes a term by induction on the proof. Appendix A states the theorem with the scoping of proposition variables made explicit and gives the full proof.

The theorem says that the type checker can check every proof of the logic. For the kernel to be trustworthy, we also need the opposite guarantee: it must not accept a term for a proposition that the logic refutes.

Consistency

Define ⊥=∀P. P\bot = \forall P.\,P, "everything holds". No proof of ⊥\bot exists in the logic: if each proposition variable is read as true or false, every rule preserves truth under every reading, but ∀P. P\forall P.\,P is false under the reading that makes some PP false.

The type theory should agree. That is, no closed term should have the type

⊥∗  =  Π(P:Sort 0). P.\bot^\ast \;=\; \tPi{P}{\Sort{0}}{P}.

A closed term tt of this type would be a disaster: for every proposition C:Sort 0C : \Sort{0}, the application t  Ct\;C would have type CC, so every proposition would be provable and the kernel would be worthless.

Definition 4.2 (Consistency). A type system is consistent if no closed term has type Π(P:Sort 0). P\tPi{P}{\Sort{0}}{P}.

Consistency is the property that every design decision in this book must preserve. In particular, a design for universes is acceptable only if the system remains consistent; Chapter 13 shows a natural-looking design that fails this test, and Chapter 14 states consistency for our system as Theorem 14.5.

Exercises

Translate the formula ∀P. ∀Q. (P→Q)→P→Q\forall P.\,\forall Q.\,(P \to Q) \to P \to Q to a type, and write the proof term that Figure 4.1 produces for the natural-deduction proof "assume P→QP \to Q and PP; apply the first to the second".

Solution

The type is Π(P:Sort 0). Π(Q:Sort 0). (P→Q)→P→Q\tPi{P}{\Sort{0}}{\tPi{Q}{\Sort{0}}{(P \to Q) \to P \to Q}}. Two ∀\forall-intros give two outer λ\lambdas over Sort 0\Sort{0}, two →\to-intros give λ(f:P→Q)\lambda(f : P \to Q) and λ(p:P)\lambda(p : P), and →\to-elim gives the body f  pf\;p: λ(P:Sort 0). λ(Q:Sort 0). λ(f:P→Q). λ(p:P). f  p\tLam{P}{\Sort{0}}{\tLam{Q}{\Sort{0}}{\tLam{f}{P \to Q}{\tLam{p}{P}{f\;p}}}}.

Show that from t:Π(P:Sort 0). Pt : \tPi{P}{\Sort{0}}{P} one can build a term of type Π(P:Sort 0). Π(Q:Sort 0). P→Q\tPi{P}{\Sort{0}}{\tPi{Q}{\Sort{0}}{P \to Q}}, "every proposition implies every other".

Solution

λ(P:Sort 0). λ(Q:Sort 0). λ(p:P). t  Q\tLam{P}{\Sort{0}}{\tLam{Q}{\Sort{0}}{\tLam{p}{P}{t\;Q}}}. The hypothesis pp is ignored and t  Q:Qt\;Q : Q by T-App. This is the sense in which a proof of ⊥∗\bot^\ast proves everything.

What to remember

  • Second-order propositional logic translates into the type theory: →\to becomes the function type, ∀P\forall P becomes Π(P:Sort 0)\Pi(P : \Sort{0}), hypotheses become context entries.
  • Theorem 4.1: every proof in the logic becomes a well-typed term. The translation is rule by rule (Figure 4.1).
  • The kernel is trustworthy only if it is consistent: no closed term of type Π(P:Sort 0). P\tPi{P}{\Sort{0}}{P}.
  • Impredicativity, via imax\imax, is what lets a universal statement be instantiated by any formula, including itself.