Simply Typed λ-calculus

Similar documents
Simply Typed λ-calculus

The Lambda-Calculus Reduction System

Lecture 3. Lambda calculus. Iztok Savnik, FAMNIT. October, 2015.

Functional Programming with Coq. Yuxin Deng East China Normal University

Constructive approach to relevant and affine term calculi

Alonzo Church ( ) Lambda Calculus. λ-calculus : syntax. Grammar for terms : Inductive denition for λ-terms

COMP6463: λ-calculus

Lambda calculus L9 103

Lecture 2. Lambda calculus. Iztok Savnik, FAMNIT. March, 2018.

Advanced Lambda Calculus Lecture 5

THE A-CALCULUS IS c-incomplete

Lazy Strong Normalization

Computation Theory, L 9 116/171

Henk Barendregt and Freek Wiedijk assisted by Andrew Polonsky. Radboud University Nijmegen. March 5, 2012

Reducibility proofs in λ-calculi with intersection types

Tutorial on Semantics Part I

Advanced Lambda Calculus. Henk Barendregt & Giulio Manzonetto ICIS Faculty of Science Radboud University Nijmegen, The Netherlands

Lambda Calculus with Types. Henk Barendregt ICIS Radboud University Nijmegen The Netherlands

Mathematical Logic IV

Models of Computation,

Type Systems Winter Semester 2006

Lambda Calculus. Andrés Sicard-Ramírez. Semester Universidad EAFIT

Optimizing Optimal Reduction: A Type Inference Algorithm for Elementary Affine Logic

Local Representations of Binding

Resource control and strong normalisation

TYPED LAMBDA CALCULI. Andrzej S. Murawski University of λeicester.

Introduction to λ-calculus

An Implicit Characterization of PSPACE

Lambda Calculus. Week 12 The canonical term models for λ. Henk Barendregt, Freek Wiedijk assisted by Andrew Polonsky

NICTA Advanced Course. Theorem Proving Principles, Techniques, Applications

CMSC 336: Type Systems for Programming Languages Lecture 10: Polymorphism Acar & Ahmed 19 February 2008

3.2 Equivalence, Evaluation and Reduction Strategies

Combinators & Lambda Calculus

Predicate Logic. Xinyu Feng 09/26/2011. University of Science and Technology of China (USTC)

Introduction to Type Theory

ADVANCED CALCULUS - MTH433 LECTURE 4 - FINITE AND INFINITE SETS

Logic and Probability Lecture 3: Beyond Boolean Logic

Lecture Notes on Subject Reduction and Normal Forms

On the Standardization Theorem for λβη-calculus

A call-by-name lambda-calculus machine

Reasoning with Higher-Order Abstract Syntax and Contexts: A Comparison

Reminder of Notation. For a variable-free term t, we let t N N stand for the interpretation of t in N. (For example, (SSS0 SS0) N equals 6.

Origin in Mathematical Logic

Spring 2016 Program Analysis and Verification. Lecture 3: Axiomatic Semantics I. Roman Manevich Ben-Gurion University

2.2 Separable Equations

Beyond First-Order Logic

Intersection and Singleton Type Assignment Characterizing Finite Böhm-Trees

Lecture 17: Floyd-Hoare Logic for Partial Correctness

The Safe Lambda Calculus

TERMS FOR NATURAL DEDUCTION, SEQUENT CALCULUS AND CUT ELIMINATION IN CLASSICAL LOGIC

3.2 Reduction 29. Truth. The constructor just forms the unit element,. Since there is no destructor, there is no reduction rule.

Encoding Graph Transformation in Linear Logic

On Upper Bounds on the Church-Rosser Theorem

Komponenten- und Service-orientierte Softwarekonstruktion

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

Kleene realizability and negative translations

Comparing Cubes of Typed and Type Assignment Systems

Type Systems as a Foundation for Reliable Computing

MATH 324 Summer 2011 Elementary Number Theory. Notes on Mathematical Induction. Recall the following axiom for the set of integers.

Computational Soundness of a Call by Name Calculus of Recursively-scoped Records. UMM Working Papers Series, Volume 2, Num. 3.

Equivalence of Algebraic λ -calculi extended abstract

Variants of the basic calculus of constructions

Graph models for the untyped λ-calculus

Typing λ-terms. Types. Typed λ-terms. Base Types. The Typing Relation. Advanced Formal Methods. Lecture 3: Simply Typed Lambda calculus

Sequent Combinators: A Hilbert System for the Lambda Calculus

Non-Idempotent Typing Operators, beyond the λ-calculus

Spring 2015 Program Analysis and Verification. Lecture 4: Axiomatic Semantics I. Roman Manevich Ben-Gurion University

1 Strict local optimality in unconstrained optimization

Categories, Proofs and Programs

Justifying Algorithms for βη-conversion

CSE 505, Fall 2005, Midterm Examination 8 November Please do not turn the page until everyone is ready.

Mechanizing a Decision Procedure for Coproduct Equality in Twelf

Introduction to lambda calculus Part 2

Strong Normalization with Singleton Types

Semantics with Intersection Types

Characterizing Polynomial and Exponential Complexity Classes in Elementary Lambda-Calculus

Example. Lemma. Proof Sketch. 1 let A be a formula that expresses that node t is reachable from s

Lambda-Calculus (cont): Fixpoints, Naming. Lecture 10 CS 565 2/10/08

1. each λ-variable is a λρ-term, called atom or atomic term, 2. if M and N are λρ-term then (MN) is a λρ-term called application,

Call-by-value non-determinism in a linear logic type discipline

Rewriting, Explicit Substitutions and Normalisation

Lecture Notes on Combinatory Modal Logic

Topic 13 Notes Jeremy Orloff

An Introduction to the Lambda Calculus

Silvia Ghilezan and Jelena Ivetić

Lecture 14 Rosser s Theorem, the length of proofs, Robinson s Arithmetic, and Church s theorem. Michael Beeson

Legendre s Equation. PHYS Southern Illinois University. October 18, 2016

Decidable structures between Church-style and Curry-style

FIXED POINTS AND EXTENSIONALITY IN TYPED FUNCTIONAL PROGRAMMING LANGUAGES

Solving Differential Equations Using Power Series

Solving Differential Equations Using Power Series

hal , version 1-21 Oct 2009

The Safe Lambda Calculus

A Behavioural Model for Klop s Calculus

Intersection Synchronous Logic

Church and Curry: Combining Intrinsic and Extrinsic Typing

HORSes: format, termination and confluence

Coherence and Transitivity of Subtyping as Entailment

FORMAL SYSTEMS: COMBINATORY LOGIC

Modèles des langages de programmation Domaines, catégories, jeux. Programme de cette seconde séance:

Transcription:

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