Simply Typed λ-calculus Lecture 2 Jeremy Dawson The Australian National University Semester 2, 2017 Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 1 / 19
Outline Properties of Curry type system: Type preservation (also known as subject reduction): is typeability preserved by β-reduction? Context lemmas: how does typing depend on contexts, what changes to contexts that preserve typing. Substitution lemma: is a typeable term (in a given context) also typeable after substitution? Subterm lemma: is a subterm of a typeable term also typeable? Church-Rosser property for typed λ-calculus. Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 2 / 19
Type preservation Type preservation, in general (given any type system), relates a program execution to its type. Why is it important? If f is, say, a factorial function, taking a natural number to to a natural number, then we would like f to satisfy: f(n) evaluates to a natural number, for any natural number n. If the type system has the type preservation property, then one needs to check the type of f once, and be convinced that f(n) always produces integer. This is the essence of static analysis in programming language. Check programs at compile time and don t need to worry about its runtime behaviours. Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 3 / 19
Curry type system Γ λcu x : τ axiom,if (x : τ) Γ Γ,x : τ 1 λcu M : τ 2 Γ λcu (λx.m) : τ 1 τ 2 intro Γ λcu M : τ τ Γ λcu N : τ Γ λcu (M N) : τ elim Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 4 / 19
Substitution (review) x[x := N] N y[x := N] y; provided x y (P Q)[x := N] (P[x := N]) (Q[x := N]) (λy.p)[x := N] λy.(p[x := N]) provided y x (λx.p)[x := N] (λx.p) Note: assume Barendregt s variable convention: bound names are always chosen to be distinct from free names. Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 5 / 19
β-reduction (review) Axiom: (λx.m) N β M[x := N] M β N M P β N P M β N P M β P N M β N λx.m β λx.n Note that β-reduction can be applied in any subterm. M β N means M can be reduced to N using zero or more β-reduction steps. Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 6 / 19
Type preservation for Curry type system Type preservation (subject reduction) Suppose M β M. Then Γ λcu M : τ implies Γ λcu M : τ. Problems: Need to consider reduction in subterms. Need also to prove that substitution preserves typing, because β-reduction uses substitutions. To prove the substitution lemma, we need a series of lemmas about properties of typing context in typing derivation. Dealing with bound variables, freshness constraints etc in inductive proofs is messy (we ll see one example). Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 7 / 19
Context lemmas: weakening Context weakening Let Γ be a context and let Γ be another context such that Γ Γ. Then Γ λcu M : τ implies Γ λcu M : τ. Proof: By induction on the structure of the derivation of Γ λcu M : τ. A problem: suppose M = λx.n, τ τ 1 τ 2,and x appears in Γ, say (x : σ) Γ and σ τ 1. Γ,x : τ 1 N : τ 2 Γ (λx.n) : τ 1 τ 2 intro Γ,x : τ 1 N : τ 2 Γ (λx.n) : τ 1 τ 2 intro This is not valid because a variable x has two distinct declarations in Γ,x : τ 1. Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 8 / 19
Context lemmas: weakening (continued) Need to rename x in (λx.n) to a fresh variable, say z. Γ,z : τ 1 N[x := z] : τ 2 Γ (λz.n[x := z]) : τ 1 τ 2 intro But how do we know Γ,z : τ 1 N[x := z] : τ 2 is derivable? Need to prove this separately as a lemma Lemma (Renaming) If Γ,x : σ M : τ is derivable in λ Cu and y is a fresh variable not appearing anywhere in Γ and M, then Γ,y : σ M[x := y] : τ is derivable in λ Cu with exactly the same deduction steps. Proof: left as an exercise. Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 9 / 19
Context lemmas: free variables Recall the definition of free variables in a term: FV(x) = {x} FV(M N) = FV(M) FV(N) FV(λx.M) = FV(M)\{x} Given a context Γ = {x 1 : τ 1,...,x n : τ n }, we denote by dom(γ) the domain of Γ, i.e., the set dom(γ) = {x 1,...,x n }. Let V be a set of variables. The restriction of Γ to V is defined as: Γ V = {x : σ x V and (x : σ) Γ} Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 10 / 19
Context lemmas: free variables If Γ M : τ is derivable, then the only relevant declarations in Γ are those that occur free in M. Free-variables lemma 1 If Γ λcu M : σ then FV(M) dom(γ). 2 If Γ λcu M : σ then Γ FV(M) λcu M : σ. Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 11 / 19
Substitution lemma We are now ready to prove the following substitution lemma: Lemma (Substitution) Suppose Γ,x : σ λcu M : τ and Γ λcu N : σ. Then Γ λcu M[x := N] : τ. Notice that M[x := N] is derivable without x : σ. This says that replacing a free variable x with a term of the same type preserves typeability. Proof by induction on the structure of the derivation (OR, on the size or structure of the term M) Base cases: M = x and τ = σ, then M[x := N] is N M = y, y x, and y : τ Γ, then M[x := N] is y Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 12 / 19
Substitution lemma : one inductive case Inductive hypothesis: assume it is true for all smaller terms M (OR, assume it is true for all earlier steps in showing that Γ,x : σ M : τ) Example case: M is an application M M, so the last rule in its typing deduction must be Γ,x : σ λcu M : τ τ Γ,x : σ λcu M : τ Γ,x : σ λcu (M M ) : τ elim By induction, Γ λcu M[x := N] : τ τ and Γ λcu M [x := N] : τ. Then Γ λcu M[x := N] : τ τ Γ λcu M [x := N] : τ Γ λcu (M[x := N]) (M [x := N]) : τ elim And as (M M )[x := N] (M[x := N]) (M [x := N]) (by definition of...[x := N]) we have (M M )[x := N] : τ, as required Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 13 / 19
Type preservation (subject reduction) Finally, the main property we want to prove (let s give it a theorem status to say it s important): Theorem (Subject reduction) Suppose M β M. Then Γ λcu M : τ implies Γ λcu M : τ. Proof by induction on the size of M, OR on the structure of the proof that M β M base case in definition of β : (λx.m) N β M[x := N], the typing deduction of (λx.m) N : τ must look like this; Γ,x : σ M : τ intro Γ λx.m : σ τ Γ (λx.m) N : τ Γ N : σ elim then Γ M[x := N] : τ, by the Substitution Lemma. Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 14 / 19
Proof of Subject reduction : one inductive case Inductive hypothesis: assume it is true for all smaller terms M (OR, assume it is true for all earlier steps in showing that M β M ) One example case (one rule in definition of β ): M β N M P β N P Given M P : τ, we want to show N P : τ The derivation of M P : τ must conclude with Γ λcu M : σ τ Γ λcu P : σ Γ λcu (M P) : τ elim so, by induction, have N : σ τ, and Γ λcu N : σ τ Γ λcu P : σ Γ λcu (N P) : τ elim Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 15 / 19
Church-Rosser property Recall the Church-Rosser property for untyped λ-calculus: If M β N 1 and M β N 2 then for some N 3, one has N 1 β N 3 and N 2 β N 3. In picture: β N 1 M β N 2 β N 3 β Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 16 / 19
Church-Rosser for typeable terms Suppose M is a closed term of type τ (typeable in λ Cu ) and M β N 1 and M β N 2. Does there exist a term N 3 such that N 3 has type τ and N 1 β N 3 and N 2 β N 3? Thanks to subject reduction, the answer is yes. Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 17 / 19
Subterm typeability If a term M is typeable, does it follow that each subterm of M is typeable? Subterm typeability Let M be a subterm of M. If Γ λcu M : σ then Γ λcu M : σ for some Γ and σ. Note that σ may not be equal to σ. Also Γ need not be related to Γ. Useful to show untypeability of a term. Example: to show (λx.x x) (λy.y y) untypeable it is enough to show (λx.x x) untypeable. Is the converse true? That is, if every strict subterm of M is typeable, does it follow that M is typeable? (Exercise) Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 18 / 19
Subject expansion Consider the reverse of subject reduction. Suppose N has type τ and M β N. Does it follow that M also has type τ? Unfortunately, this is not always true. Consider this: N = λxλy.y M = (λzλxλy.y) (λp.p p) We have M β N but M is untypeable (why?) A consequence of this: typeability is not preserved by β-equality = β. Jeremy Dawson (ANU) COMP4630,Lecture 2 Semester 2, 2017 19 / 19