Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 G. Klein, NICTA

2 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

3 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 )

4 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.

5 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

6 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

7 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’

8 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?

9 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.

10 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.

11 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)

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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 !

21 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.

22 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

23 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.

24 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.

25 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.

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


Download ppt "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."

Similar presentations


Ads by Google