|
< Free
|
As in Chapter 8, we need to develop a few basic lemmas before we can
First off, an
inversion lemma
records a collection of observations about how typing
If ⊢ x : R , then x:R .
If ⊢ λx:T 1 . t 2 : R , then R = T 1 R 2 for some R 2 with , x:T 1 ⊢ t 2 : R 2 .
If ⊢ t 1 t 2 : R , then there is some type T 11 such that ⊢ t 1 : T 11 R and ⊢ t 2 : T 11 .
If ⊢ true : R , then R = Bool .
If ⊢ false : R , then R = Bool .
If ⊢ if t 1 then t 2 else t 3 : R , then ⊢ t 1 : Bool and ⊢ t 2 , t 3 : R .
Proof: Immediate from the definition of the typing relation.
Is there any context and type T such that ⊢ x x : T ? If so, give and T and show a typing derivation for ⊢ x x : T ; if not, prove it.
In §9.2, we chose an explicitly typed presentation of the calculus to simplify the job of
In a given typing context , a term t (with free variables all in the domain of ) has at most one type. That is, if a term is typable, then its type is unique. Moreover, there is just one derivation of this typing built from the inference rules that generate the typing relation.
Proof: Exercise. The proof is actually so direct that there is almost nothing to say; but writing out some of the details is good practice in "setting up" proofs about the typing relation.
For many of the type systems that we will see later in the book, this simple correspondence between terms and derivations will not hold: a single term will be assigned many types, and each of these will be justified by many typing derivations. In these systems, there will often be significant work involved in showing that typing derivations can be recovered effectively from terms.
If v is a value of type Bool , then v is either true or false .
If v is a value of type T 1 T 2 , then v = λx:T 1 .t 2 .
Proof: Straightforward. (Similar to the proof of the canonical forms lemma for arithmetic expressions, 8.3.1.)
Using the canonical forms lemma, we can prove a progress theorem analogous to Theorem 8.3.2. The statement of the theorem needs one small change: we are interested only in closed terms, with no free variables. For open terms, the progress theorem actually fails: a term like f true is a normal form, but not a value. However, this failure does not represent a defect in the language, since complete programs - which are the terms we actually care about evaluating - are always closed.
Suppose t is a closed, well-typed term (that is, ⊢ t : T for some T ). Then either t is a value or else there is some t with t t .
Proof: Straightforward induction on typing derivations. The cases for boolean constants and conditions are exactly the same as in the proof of progress for typed arithmetic expressions (8.3.2). The variable case cannot occur (because t is closed). The abstraction case is immediate, since abstractions are values.
The only interesting case is the one for application, where t = t 1 t 2 with ⊢ t 1 : T 11 T 12 and ⊢ t 2 : T 11 . By the induction hypothesis, either t 1 is a value or else it can make a step of evaluation, and likewise t 2 . If t 1 can take a step, then rule E-APP1 applies to t . If t 1 is a value and t 2 can take a step, then rule E-APP2 applies. Finally, if both t 1 and t 2 are values, then the canonical forms lemma tells us that t 1 has the form λx:T 11 .t 12 , and so rule E-APPABS applies to t .
Our next job is to prove that evaluation
The first structural lemma tells us that we may permute the elements of a context, as
If ⊢ t : T and Δ is a permutation of , then Δ ⊢ t : T . Moreover, the latter derivation has the same depth as the former.
Proof: Straightforward induction on typing derivations.
If ⊢ t : T and x ∉ dom ( ), then , x:S ⊢ t : T . Moreover, the latter derivation has the same depth as the former.
Proof: Straightforward induction on typing derivations.
Using these technical lemmas, we can prove a crucial property of the typing relation: that well-typedness is preserved when variables are substituted with terms of appropriate types. Similar lemmas play such a ubiquitous role in the safety proofs of programming languages that it is often called just "the substitution lemma."
If , x:S ⊢ t : T and ⊢ s : S , then ⊢ [x ↦ s]t : T .
Proof: By induction on a derivation of the statement , x:S ⊢ t : T . For a given derivation, we proceed by cases on the final typing rule used in the proof. [3] The most interesting cases are the ones for variables and abstractions.
|
Case T-VAR : |
t = z with z:T ( , x:S ) |
There are two sub-cases to consider, depending on whether z is x or another variable. If z = x , then [x ↦ s]z = s . The required result is then ⊢ s : S , which is among the assumptions of the lemma. Otherwise, [x ↦ s]z = z , and the desired result is immediate.
|
Case T-ABS : |
t = λy:T 2 .t 1 T = T 2 T 1 , x:S, y:T 2 ⊢ t 1 : T 1 |
By convention 5.3.4, we may assume x ≠ y and y FV ( s ). Using permutation on the given subderivation, we obtain ∉ , y:T 2 , x:S ⊢ t 1 : T 1 . Using weakening on the other given derivation ( ⊢ s : S ), we obtain , y:T 2 ⊢ s : S . Now, by the induction hypothesis, , y:T 2 ⊢ [x ↦ s]t 1 : T 1 . By T-ABS, ⊢ λy:T 2 . [x ↦ s]t 1 : T 2 T 1 . But this is precisely the needed result, since, by the definition of substitution, [x ↦ s]t = λ y:T 1 . [x ↦ s]t 1 .
|
Case T-APP : |
t = t 1 t 2 , x:S ⊢ t 1 : T 2 T 1 , x:S ⊢ t 2 : T 2 T = T 1 |
By the induction hypothesis, ⊢ [x ↦ s]t 1 : T 2 T 1 and ⊢ [x ↦ s]t 2 : T 2 . By T-APP, ⊢ [x ↦ s]t 1 [x ↦ s]t 2 : T , i.e., ⊢ [x ↦ s] ( t 1 t 2 ) : T .
|
Case T-TRUE : |
t = true T = Bool |
Then [x ↦ s]t = true , and the desired result, ⊢ [x ↦ s]t : T , is immediate.
|
Case T-FALSE : |
t = false T = Bool |
Similar.
|
Case T-IF : |
t = if t 1 then t 2 else t 3 , x:S ⊢ t 1 : Bool , x:S ⊢ t 2 : T , x:S ⊢ t 3 : T |
Three uses of the induction hypothesis yield
⊢ [x ↦ s]t 1 : Bool ⊢ [x ↦ s]t 2 : T ⊢ [x ↦ s]t 3 : T,
from which the result
Using the substitution lemma, we can prove the other half of the type safety property - that evaluation preserves well-typedness.
If ⊢ t : T and t t , then ⊢ t : T .
Proof: Exercise [Recommended, ⋆⋆⋆ ]. The structure is very similar to the proof of the type preservation theorem for arithmetic expressions (8.3.3), except for the use of the substitution lemma.
In Exercise 8.3.6 we investigated the subject expansion property for our simple calculus of typed arithmetic expressions. Does it hold for the "functional part" of the simply typed lambda-calculus? That is, suppose t does not contain any conditional expressions. Do t t and ⊢ t : T imply ⊢ t : T ?
[3] Or, equivalently, by cases on the possible shapes of t , since for each syntactic constructor there is exactly one typing rule.
| < Free Open Study > |