Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, 2006-07 Some material from B. Pierce: TAPL + some from.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Substitution & Evaluation Order cos 441 David Walker.
Introduction to Proofs
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Chapter Three: Closure Properties for Regular Languages
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Primitive Recursive Functions (Chapter 3)
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
1 How to transform an analyzer into a verifier. 2 OUTLINE OF THE LECTURE a verification technique which combines abstract interpretation and Park’s fixpoint.
Advanced Formal Methods Lecture 2: Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from G. Klein, NICTA.
Introduction to Computability Theory
Foundations of Programming Languages: Introduction to Lambda Calculus
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Advanced Formal Methods Lecture 7: Isabelle – Sets Mads Dam KTH/CSC Course 2D1453, Material from L. Paulson.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Sparkle A theorem prover for the functional language Clean Maarten de Mol University of Nijmegen February 2002.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Semantics for MinML COS 441 Princeton University Fall 2004.
CS5205: Foundation in Programming Languages Lecture 1 : Overview
Propositional Calculus Math Foundations of Computer Science.
Systems Architecture I1 Propositional Calculus Objective: To provide students with the concepts and techniques from propositional calculus so that they.
Reading and Writing Mathematical Proofs
MATH 224 – Discrete Mathematics
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
Sound Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen Chalmers University of Technology.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Universal Types Report by Matthias Horbach. Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity.
Types and Programming Languages Lecture 12 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Advanced Formal Methods Lecture 4: Isabelle – Types and Terms Mads Dam KTH/CSC Course 2D1453, Some material from Paulson.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Types and Programming Languages
1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Lesson 10 Type Reconstruction
CS5205: Foundation in Programming Languages Type Reconstruction
CS5205: Foundations in Programming Languages
Programming Languages and Compilers (CS 421)
CS 550 Programming Languages Jeremy Johnson
CSE-321 Programming Languages Simply Typed -Calculus
A lightening tour in 45 minutes
More on Lambda Calculus
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Revisiting Predicate Logic LN chapters 3,4
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Typed Arithmetic Expressions
Organization of Programming Languages
Presentation transcript:

Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from G. Klein, NICTA

Typing -terms The uptyped -calculus allows ”strange” terms to be formed: D D =  not(D D) succ (pair ff ff) Solution: Rule out ill-formed terms using types (Church 1940) Ill-formed term: Computation can ”go wrong” succ (pair ff ff): Cannot complete computation to produce a sensible value = normal form Type unsafety – runtime error

Types Simply typed -calculus, ! : Only two types, base types and function types Syntax: T ::= A | T ! T A: Base type, e.g. bool, int, float, array[int],... T 1 ! T 2 : Type of functions from T 1 to T 2 Type constructor ! right-associative: T 1 ! T 2 ! T 3 == T 1 ! (T 2 ! T 3 )

Typed -terms x.t: Must be of function type T 1 ! T 2 But where to find T 1 ? Alt. 1: Give domain type explicitly as typed -term x:T 1.t Example: x:int.x + x : int ! int Used here initially Alt. 2: Keep untyped syntax Use types as well-formedness predicate x.x + x : int ! int x.x: int ! int, but also x.x: bool ! bool, etc.

The Typing Relation Typing relation  ` t : T  : Type environment Also: Type context, type assumptions Finite function x  T x Must have FV(t) µ dom(  ) Typing rules:  x : T 1 ` t : T 2  ` x : T 1. t : T 1 ! T 2 x : T 2   ` x : T  ` t : T 1 ! T 2  ` s : T 1  ` t s : T 2 Function update: x  dom(  )  omitted if empty

Base Types Easy to extend to base types Example: Booleans Base type Bool Terms t ::= x | x : T. t | t t | true | false | if t then t else t |... New typing rules (+ one for false too):  ` t : bool  ` s 1 : T  ` s 2 : T  ` if t then s 1 else s 2 : T   ` true : bool

Terms, Notation, Reduction Same syntactic conventions for typed terms: x : T 1 y : T 2. T == x: T 1. y : T 2. T Sometimes use, as separator for clarity Similar for associativity Alpha-conversion, substitution, free and bound variables Reduction: - ( x : T. t) s !  t[s/x] s !  s’ s t !  s’ t t !  t’ s t !  s t’ t !  t’ x : T. t !  x : T. t’

Typing, Examples Exercise 1: Give type derivations to show: 1. ` x : A, y : B. x : A ! B ! A 2. ` x : A ! B, y : B ! C, z : A. y (x z) : (A ! B)! (B ! C) ! A ! C Exercise 2: Find a context under which f x y has type A. Can you give a simple description of all such contexts?

Properties of the Typing Relation Lemma 1: 1.If  ` x : T then x : T 2  2.If  ` x : T 1. t : S then S = T 1 ! T 2 for some S such that , x : T 1 ` t : T 2 3.If  ` t s : T 2 then there is some T 1 such that  ` t : T 1 ! T 2 and  ` s : T 1 Exercise 3: Prove this statement Exercise 4: Is there any context  and type T such that  ` x x : T? If so, give a type derivation. If not, prove it.

Unique Typing and Normal Forms Lemma 2: If  ` t : T 1 and  ` t : T 2 then T 1 = T 2 Exercise 5: Prove this statement. Unique typing fails for many richer languages Values: v 2 Val ::= x | x v... v | x : T. v Lemma 3: t 9  iff t 2 Val Exercise 6: Prove (or disprove) this statement.

Substitution Lemma  ·  : For all x,  (x) is defined implied  (x) is defined and then  (x) =  (x) Proposition 1: If  ` t : T and  ·  then  ` t : T Lemma 4 [Substitution]: If , x : S ` t: T and  ` s : S then  ` t[s/x] : T We’ll prove this statement in class. Theorem 1 [Subject Reduction]: If  ` t : T and t !  t’ then  ` t’ : T Exercise 7: Prove this statement (hint: Use induction on the derivation of  ` t : T)

Extensions - Products Many extensions possible, see TAPL for more First: Product types Types: T ::=... | T £ T Terms: t ::=... | (t, t) | fst | snd Reduction: Use generic ! instead of !  Can support different evaluation orders

Products – Reduction and Typing Reduction rules: + rules for context closure: Typing rules: - fst(t,s) ! t - snd(t,s) ! s t ! t’ (t,s) ! (t’,s) s ! s’ (t,s) ! (t,s’) t ! t’ fst t ! fst t’ t ! t’ snd t ! snd t’  ` t : T  ` s : S  ` (t, s) : T £ S   ` fst : T £ S ! T   ` snd : T £ S ! S

Sums Types: T ::=... | T + T Terms: t ::=... | in 1 | in 2 | cases in 1 => t || in 2 => t Syntax slightly uncommon. Often use sugared version, something like: case t of in 1 (x : T 1 ) => s 1 || in 2 (y : T 2 ) => s 2 == (cases in 1 => x : T 1. s 1 || y:T 2. s 2 ) t

Sums – Reduction and Typing Reduction rules: Exercise: Give suitable context closure rules for sums Typing: Exercise 8: Unique typing fails for the type system with sums. Why? - (cases in 1 => s 1 || in 2 => s 2 ) (in 1 t) ! s 1 t -  ` in 1 : T ! T + S  ` s 1 : T 1 ! S  ` s 2 : T 2 ! S  ` cases in1 => s1 || in2 => s2 : T 1 + T 2 ! S - (cases in 1 => s 1 || in 2 => s 2 ) (in 2 t) ! s 2 t -  ` in 2 : S ! T + S

General Recursion fix is not definable in ! (see later), but can be introduced as new constant Terms: t ::=... | fix Reduction: fix f ! f (fix f) Typing: Exercise 9: Add a natural number base type, and define equal, plus, times, and factorial using fix -  ` fix : (T ! T) ! T

More Exercises Exercise 10: Add the following constructs to simply typed lambda calculus, with reduction and typing rules: t ::=... | let x : T = t 1 in t 2 | letrec x : T = t 1 in t 2 The intention (of course) is that ”let” is used for non- recursive definitions, and ”letrec” for recursive ones. Give reduction and typing rules for ”let” and ”letrec”. Show how ”let” and ”letrec” can be coded in !. Do the same for mutually recursive definitions: t ::= … | letrec x 1 : T 1 = t 1, …, x n : T n = t n in t Note: In more realistic languages one will generally want type annotations T, T 1,… to be inferred automatically by the type checker

The ML Language With the extensions above ! is a ”grandmother” of many typed functional languages ML: –Highly influential programming language –Originally developed as a MetaLanguage for the LCF theorem prover [Gordon-Milner-Wadsworth-79] –ML used for programming proof search in LCF Introduce base type ”theorem” The metalanguage must ensure type safety: The only values of type ”theorem” are those that really are theorems in the logic being represented –ML main features: cbv semantics, automatic type inference, polymorphic types

ML, Haskell, PCF ML and other languages: –ML was influenced by Landin’s ISWIM –SML – Standard ML of 1997 Comprehensive formal transition semantics and type system by [Milner-Tofte-Harper, 1990] –Check out: SML of New Jersey, OCAML –SML used in descendants of LCF: HOL, Isabelle –Haskell is a descendant with cbn (lazy) semantics (and other twists) –PCF [Plotkin-77] ! + naturals + more types + recursion Popular in theoretical studies

Strong Normalization We are now addressing the base calculus ! with a single base type A Strong normalization: t 2 SN n iff any !  -derivation t = t 0 !  t 1 !   !  t n !   has length at most n SN = {t | 9n. t 2 SN n } Theorem 2 [Strong Normalization]: If ` t : T then t 2 SN This immediately shows that all terms of functional type must express total functions on closed terms Thus, general recursion cannot be encoded in !

Logical Relations Exercise 11: Why is normalization tricky to prove? As always, the trick is to find the right inductive argument Proof here follows Tait [JSL-67] and Girard-Lafont-Taylor, Proofs and Types, CUP’89 Define predicate R T on closed terms by: –R A = {t | t 2 SN} –R S ! T = {t | whenever s 2 R S then t s 2 R T } Note: Do not require t 2 R T implies ` t : T.

Proof of Normalization Lemma 6: If t !  t’ and t 2 R T then t’ 2 R T Proof: By structural induction on the structure of T Exercise 12: Prove lemma 6. Neutral term: Either a variable or an application Lemma 7: 1.If t 2 R T then t 2 SN 2.If t is neutral and for all t’, t !  t’ implies t’ 2 R T, then t 2 R T

Proof of Lemma 7 Proof by simultaneous induction on T T = A. Both 1 and 2 are immediate T = T 1 ! T 2. 1: Let t 2 R T. By the induction hypothesis (2), x 2 R T 1, so t x 2 R T 2. Then t x 2 SN, so t 2 SN as well. 2: Suppose t is neutral and whenever t !  t’ then t’ 2 R T. Let t 1 2 R T 1. We show t t 1 2 R T 2. By the induction hypothesis (1), t 1 2 SN n for some n. We proceed by nested induction on n. It is sufficient to show t 2 2 R T 2 whenever t t 1 !  t 2, by the induction hypothesis (2), and since t t 1 is neutral. Since t is neutral, either t !  t’ and t 2 = t’ t 1, or else t 1 !  t 1 ’, and t 2 = t t 1 ’. In the first case, t 2 2 R T 2 by the assumptions, and in the second, t 1 ’ 2 R T 1. Then t 1 ’ 2 SN n’, n’ < n. So by the inner i.h. T t 1 ’ 2 R T 2.

Abstraction Lemma Lemma 8: If t 1 [t/x] 2 R T 2 whenever t 2 R T 1 then x : T 1. t 1 2 R T 1 ! T 2 Proof: Assume t 2 R T 1. We must show t’ = ( x : T 1. t 1 ) t 2 R T-1. By 7.1, t 2 SN n 2 and t 1 2 SN n 1 for some n 1, n 2. Then n 1 +n 2 is an upper bound on the number of reduction steps that can be performed before the outermost redex in t’ must be reduced, so we proceed by induction on n 1 +n 2. By 7.2 it is sufficient to show t’’ 2 R T 2 whenever t’ !  t’’. Check out the possible cases: - t’’ = t 1 [t/x]. We are done by the assumptions. - t’’ = ( x : T 1. t 1 ) s and t !  s. Then s 2 R T 1 by Lemma 6 and s 2 SN n 2 ’, n 2 ’<n 2 so we’re done by the induction hypothesis. - t’’ = ( x : T 1. t 1 ’) t and t 1 !  t 1 ’. By lemma 6, t 1 ’[t/x] 2 R T 2, and t 1 ’ 2 SN n 1 ’ for some n 1 ’ < n 1. So t’’ 2 R T-2.

Fundamental Lemma Lemma 9: Suppose x 1 : T 1,...,x n : T n ` t : T. If t i 2 R T i for all i: 1· i· n, then t[t 1 /x 1,...,t n /x n ] 2 R T. Note: This proves theorem 2, for n = 0. Proof: By induction on size of the type derivation. Let  = x 1 : T 1,...,x n : T n and t/x abbreviate t 1 /x 1,...,t n /x n. t = x i : Then t[t/x] = t i, T = T i, and t i 2 R T i by the assumptions. t = t’ t’’: By the induction hypothesis, t’[t/x] 2 R T’ ! T and t’’[t/x] 2 R T ’. Then t[t/x] = (t’ t’’)[t/x] = (t’[t/x]) ( t’’[t/x]) 2 R T. t = x’’ : T’’. t’. Then T = T’’ ! T’. Let t’’ 2 R T’’ be arbitrary. By the induction hypothesis, t’[t/x,t’’/x’’] 2 R T’. But then x : T’’. t’[t/x] = t[t/x] 2 R T as desired.

Exercise Exercise 13: We did not require that t 2 R T only if ` t : T. Why was that?