Chapter 3
Typing Rules
The kernel's job is to decide the judgment "term has type ". This chapter defines that judgment by a small set of inference rules, one for each kind of term and one extra rule that lets computation happen inside types. Reading each rule bottom-up turns it into a recipe for checking a term, and that recipe becomes the algorithm of Chapter 5.
Contexts and judgments
A term such as cannot be checked in isolation: its type depends on what and are. A context records that information. It is an ordered list of assumptions
giving the type of each variable in scope, and denotes the set of names . The empty context is written .
Order matters. A later type may mention an earlier variable, as in : the assumption only makes sense once is known to be a proposition. Names in a context will be required to be distinct, so a context can be used like a dictionary from names to types; but it is a dictionary whose entries depend on each other in sequence.
Two judgments are defined together.
- means that the context is well formed: each assumed type is a type in the context before it.
- means that under the assumptions in , the term has type .
Through the Curry-Howard lens, says that is a proof of from the hypotheses .
The rules
Figure 3.1 lists all the rules. Each rule has premises above the line and a conclusion below it; the conclusion holds whenever every premise does. The rule names on the right are used throughout the book.
Figure 3.1. Well-formed contexts (top row) and typing rules.
Two of the rules use operations on terms that the next chapters treat as black boxes.
Substitution. T-App writes for the result of replacing every free occurrence of in by . For example, . Replacing names inside binders needs care, which Chapter 11 explains; until then the intuitive meaning is enough.
Definitional equality. T-Conv writes when and are "the same type" in a sense the kernel can decide mechanically. Two kinds of difference are ignored.
- The names of bound variables. and state the same proposition. Terms that differ only in this way are alpha-equivalent, written .
- Computation. If , then computes to by plugging in for , so a proof of should count as a proof of . The computation step is called beta-reduction.
The kernel decides by computing both sides as far as possible and then checking whether the results are alpha-equivalent. Chapter 12 defines this precisely.
Reading the rules
The rules are best read from the bottom up, as a recipe: to establish the conclusion, establish the premises.
C-Empty and C-Ext. A context is built one assumption at a time, and each new assumption must have a type as its type. The side condition forbids two assumptions with the same name. Since the names of bound variables do not matter, this restricts nothing: a binder that would clash can always be renamed first. Chapter 11 shows what goes wrong in a kernel that skips this condition.
T-Sort. Every universe lives in the next one: , , and so on. Chapter 13 explains why a universe cannot be its own type.
T-Var. A variable has the type the context assigns to it. Logically: use a hypothesis.
T-Pi. is a type if is a type and is a type once is assumed. The resulting universe is , which is if and otherwise. In words: a -type is a proposition whenever its body is a proposition, no matter what it quantifies over; otherwise it lives in the larger of the two universes. Logically, this rule says when and are well-formed statements.
T-Lam. To build a function from to , assume and build a term of type . The body's type may mention , and it is kept as is in the resulting -type, which is where dependency enters. Logically, this is the introduction rule for implication and for the universal quantifier: to prove , assume and prove .
T-App. If maps each to an element of , and , then has type with replaced by . When does not mention this is modus ponens: from and , conclude . When it does, it is instantiation of a universal statement. For example, if , then (natural numbers arrive in Chapter 7).
T-Conv. Types that are equal up to computation are interchangeable: from we may conclude . The premise ensures that the new type is itself a type. This is the only rule whose use is not determined by the shape of , a fact that matters for the algorithm in Chapter 5.
Why application matters
The rules that build things (T-Lam and T-Pi) may seem more important than T-App, but without application a proof can return a hypothesis without ever using it. Roughly, only propositions whose conclusion is literally one of the hypotheses, such as , would be provable. Composition,
has the proof , which applies and then . Every proof that does more than restate an assumption goes through T-App, and in a dependent type theory T-App is also where substitution, and therefore most of the subtlety, lives.
A first derivation
A derivation is a tree of rule instances whose root is the judgment being established. Here is the derivation that proves . Write and , and omit the well-formedness premises , which are routine.
The kernel builds this tree. Below, each node is one application of a rule, with the judgment it establishes; switch to the call view to read it top-down as the recursion that produces it. Replace the term with your own to see which rules fire.
Two observations. First, the shape of the tree follows the shape of the term: a at the root, a below it, variables at the leaves. Second, the type at the root is , whereas the stated proposition was . These differ only in the name of a bound variable ( versus the anonymous _ of the arrow), so they are alpha-equivalent, and T-Conv bridges the gap. Chapter 6 revisits this example with the algorithm.
Exercises
Which rule, applied bottom-up, handles each of the following judgments, and what premises does it produce? (a) . (b) . (c) where .
Solution
(a) T-Lam, with premises for some and ; the latter is T-Var. The resulting type is , which is up to the binder name.
(b) T-Sort, with the single premise .
(c) T-App, with premises and . The conclusion is , since _ does not occur in .
Using T-Pi, compute the universe of and of .
Solution
For the first, so , and (by T-Pi again with ) so . Hence the universe is : a statement quantifying over all propositions is itself a proposition. For the second, and so , giving . Chapter 13 explains the asymmetry.
What to remember
- A context is an ordered list of distinct assumptions ; the judgment says that has type under those assumptions.
- There is one rule per kind of term, plus T-Conv, which identifies types up to computation and bound-variable renaming.
- T-App is where hypotheses are used and where substitution happens; T-Conv is the only rule not triggered by the shape of the term.
- Substitution and definitional equality are treated as black boxes until Part III.