1 UNTYPED LAMBDA CALCULUS (II)
RECALL: CALL-BY-VALUE O.S. Basic rul Sarch ruls: (\x.) v [v/x] 1 1 1 1 v v
CALL-BY-VALUE EVALUATION EXAMPLE (\x. x x) (\y. y) x x [\y. y / x] = (\y. y) (\y. y) y [\y. y / y] = \y. y 3
ANOTHER EXAMPLE (\x. x x) (\x. x x) x x [\x. x x/x] = (\x. x x) (\x. x x) In othr words, it is simpl to writ nontrminating computations in th lambda calculus what ls can w do? 4
WE CAN DO EVERYTHING Th lambda calculus can b usd as an assmbly languag W can show how to compil usful, high-lvl oprations and languag faturs into th lambda calculus Rsult = adding high-lvl oprations is convnint for programmrs, but not a computational ncssity Concrt syntax vs. abstract syntax Syntactic sugar Rsult = mak your compilr intrmdiat languag simplr 5
BOOLEANS w can ncod boolans w will rprsnt tru and fals as functions namd tru and fls how do w dfin ths functions? think about how tru and fals can b usd thy can b usd by a tsting function: tst b thn ls rturns thn if b is tru and rturns ls if b is fals th only thing th implmntation of tst is going to b abl to do with b is to apply it th functions tru and fls must distinguish thmslvs whn thy ar applid 6
BOOLEANS tru = \t.\f. t fls = \t.\f. f tst = \x.\thn.\ls. x thn ls E.g. (undrlind ar rdxs): tst tru a b = (\x.\thn.\ls. x thn ls) tru a b (\thn.\ls. tru thn ls) a b (\ls. tru a ls) b tru a b = (\t.\f. t) a b (\f. a) b a Try apply tst to fls?? 7
BOOLEANS tru = \t.\f. t and = \b.\c. b c fls fls = \t.\f. f and tru tru * tru tru fls * tru (* stands for multi-stp valuation) 8
BOOLEANS tru = \t.\f. t and = \b.\c. b c fls fls = \t.\f. f and fls tru * fls tru fls * fls What will b th dfinition of or and not? 9
BOOLEANS tru = \t.\f. t or = \b.\c. b tru c fls = \t.\f. f or fls tru * fls tru tru * tru or fls fls * fls tru fls * fls 10
PAIRS pair = \f.\s.\b. b f s (*pair is a constructor: pair x y*) fst = \p. p tru snd = \p. p fls fst (pair v w) = fst ((\f.\s.\b. b f s) v w) fst ((\s.\b. b v s) w) fst (\b. b v w) = (\p. p tru) (\b. b v w) (\b. b v w) tru tru v w * v 11
AND WE CAN GO ON... numbrs arithmtic xprssions (+, -, *,...) lists, trs and datatyps xcptions, loops,...... th gnral trick: valus will b functions construct ths functions so that thy rturn th appropriat information whn calld by an opration 1
SIMPLY-TYPED LAMBDA CALCULUS 13
SIMPLY TYPED LAMBDA-CALCULUS Goal: construct a similar systm of languag that combins th pur lambda-calculus with th basic typs such as bool and num. A nw typ: (arrow typ) St of simpl typs ovr th typ bool is t ::= bool t 1 t Not: typ constructor is right associativ: t1 t t3 == t1 (t t3) 14
SYNTAX (I) ::= xprssions: x (variabl) tru (tru valu) fals (fals valu) if 1 thn ls 3 (conditional) \x : t. (abstraction) 1 (application) v ::= valus: tru (tru valu) fals (fals valu) \x : t. (abstraction valu) 15
SYNTAX (II) t ::= typs: bool (bas boolan typ) t 1 t (typ of functions) Γ ::= contxts:. (mpty contxt) Γ, x: t (variabl binding) 16
TYPING RULES Th typ systm of a languag consists of a st of inductiv dfinitions with judgmnt form: Γ : t If th currnt typing contxt is Γ, thn xprssion has typ t. This judgmnt is known as hypothtical judgmnt (Γ is th hypothsis). Γ (somtims writtn as G) is a typing contxt (typ map) which is mapping btwn x and t of th form x: t x is th variabl nam apparing in t is a typ that s bound to x 17
EVALUATION (O.S.) [ ] if 1 thn ls 1 3 1 ' if 1 ' thn ls 3 (E -if0) if tru thn ls 3 if fals thn ls 3 3 (E -if1) (E -if) 1 1 1 ' ' 1 (E - App1) v 1 v 1 ' ' (E - App) ( x : t. ) v [ v / x] (E - AppAbs) 18
TYPING 1 1 11 1 11 1 1 1 1 3 1 3 1 : : : :. : : :, : ls thn if : : : : : : : t t t t t t t x t t x t t t bool bool fals bool tru t x t x 19 (T-Var) (T-Abs) (T-App) (T-Tru) (T-If) (T-Fals) [Γ : t]
PROPERTIES OF SIMPLY-TYPED LAMBDA CALCULUS Lmma 1 (Uniqunss of Typing). For vry typing contxt Γ and xprssion, thr xists at most on t such that Γ -- : t. (not: w don t considr sub-typing hr) Proof: By induction on th drivation of Γ - : t. Cas t-var: sinc thr s at most on binding for x in Γ, x has ithr no typ or on typ t. Cas provd Cas t-tru and t-fals: obviously tru. Cas t-if: (1) t is uniqu (By I.H.) Cas provd. 1 : bool : t if thn ls : t 1 3 3 : t 0
PROPERTIES OF SIMPLY-TYPED LAMBDA CALCULUS, x : t : t 1 Cas t-abs: x : t1. : t1 t (1) t is uniqu (By I.H.) () Γ contains just on (x, t) pair so t 1 is uniqu (By (1) and assumption of t-abs) (3) t1 t is uniqu (By () and t-abs) 1 : t11 t1 : t11 Cas t-app: 1 : t1 (1) 1 and satisfis Lmma 1 (By I.H.) () Thr s at most on instanc of t 11 (By (1)) (3) t 1 is uniqu, too (By () & I.H.) 1
PROPERTIES OF SIMPLY-TYPED LAMBDA CALCULUS Lmma (Invrsion for Typing). If Γ x : t thn x : t Γ If Γ (λx : t 1.) : t thn thr is a t such that t = t 1 t and Γ, x : t 1 : t If Γ 1 : t thn thr is a t such that Proof: Γ 1 : t t and Γ : t From th dfinition of th typing ruls, thr is only on rul for ach typ of xprssion, hnc th rsult. Wll-typdnss: An xprssion in th languag L is said to b wll-typd, if thr xists som typ t, such that : t.
PROPERTIES OF SIMPLY-TYPED LAMBDA CALCULUS Canonical Forms Lmma (Ida: Givn a typ, want to know somthing about th shap of th valu) If. - v: t thn If t = bool thn v = tru or v = fals; If t = t 1 t thn v = \x: t 1. Proof: By inspction of th typing ruls. 3
PROPERTIES OF SIMPLY-TYPED LAMBDA CALCULUS Exchang Lmma If G, x:t1, y:t, G' - :t, thn G, y:t, x:t1, G' - :t. Proof by induction on drivation of G, y:t, x:t, G' - :t (Practic!) Wakning Lmma If G - :t thn G, x:t' - :t (providd x not in Dom(G)) (Practic!) 4
TYPE SAFETY Safty = Progrss + Prsrvation Progrss: A wll-typ trm is not stuck (ithr it is a valu or it can tak a stp according to th valuation ruls) Prsrvation: If a wll-typd trm (with typ t) taks a stp of valuation, thn th rsulting trm is also wll typd with typ t. Typ-chcking: th procss of vrifying typ safty of a program (or a trm). 5
PROGRESS THEOREM Suppos is a closd and wll-typd trm (that is : t for som t). Thn ithr is a valu or ls thr is som for which. Proof: By induction on th drivation of typing: [Γ : t] Cas T-Var: dosn t occur bcaus is closd. Cas T-Tru, T-Fals, T-Abs: immdiat sinc ths ar valus. Cas T-App: (1) 1 is a valu or can tak on stp valuation. Likwis for. (By I.H.) () If 1 can tak a stp, thn E-App1 can apply to ( 1 ). (By (1)) (3) If can tak a stp, thn E-App can apply to ( 1 ) (By (1)) (4) If both 1 and ar valus, thn 1 must b an abstraction, thrfor E-AppAbs can apply to ( 1 ) (By (1) and canonical forms v) (5) Hnc (1 ) can always tak a stp forward. (By (,3,4)) 6
PROGRESS THEOREM (CONT D) Cas T-if: 1. 1 can ithr tak a stp or is a valu (By I.H.). Subcas 1: 1 can tak a stp (By I.H.) 1. if 1 thn ls 3 can tak a stp (By E-if0) 3. Subcas : 1 is a valu (By I.H.) 1. If 1 = tru, if 1 thn ls 3 (By E-if1). If 1 = fals, if 1 thn ls 3 3 (By E-if) 4. In both subcass, can tak a stp. Cas provd. 7
PRESERVATION THEOREM If G - : t and, thn G - : t. Proof: By induction on th drivation of G - : t. Cas T-Var, T-Abs, T-Tru, T-Fals: Cas dosn t apply bcaus variabl or valus can t tak on stp valuation. Cas T-If: = if 1 thn ls 3. If thr ar two subcass cass: Subcas 1: 1 is not a valu. (1) 1 : bool (By assumption and invsion of T-if) () 1 1 and 1 : bool (By IH) (3) G - : t (By T-If and ()) Subcas : 1 is a valu, i.. ithr tru or fals. (4) or 3 and : t ( = or 3) (By E-If1, E-If and IH) Cas provd. 8
PRESERVATION THEOREM (CONT D) Cas T-App: = 1. Nd to prov, G - : t 1 If 1 is not a valu thn: (5) 1 1, and 1 : t 11 t 1. (By IH) (6) 1 : t 1 (By T-App) If 1 is a valu thn: (7) 1 is an abstraction. (By assumption and T-Abs) Thr ar two subcass for. Subcas 1: is a valu. Lt s call it v. (8) = \x. v, and G - x : t 11, G, x: t 11 - : t 1, G - v : t 11 (By (7) and invrsion of T-Abs) (9) \x. v [v / x] (By E-AppAbs) (10) G - [v / x] : t 1. (By (8), (9) and substitution lmma) (11) G - : t 1 (By (10) & assumption) 9
Subcas : is not a valu. (1) Suppos. Thn 1, i.., = 1. (By E-App) (13) G - : t 11, thrfor G - 1 : t 1. (By I.H., T-App) (14) G - : t 1. (By (13)) Cas provd. QED. 30
SUBSTITUTION LEMMA If G, x : t - : t, and G - v : t, thn G - [ v / x] : t. Proof lft as an xrcis. 31
CURRY-HOWARD CORRESPONDENCE A.k.a Curry-Howard Isomorphism Connction btwn typ thory and logic Logic Propositions Proposition P Q Proposition P Q Proof of proposition P Proposition P is provabl Programming Languags Typs Typ P Q Typ P Q (product/pair typ) Exprssion of typ P Typ P is inhabitd (by som xprssion) 3