Continuation Passing Style
Total Page:16
File Type:pdf, Size:1020Kb
Type Systems Lecture 10: Classical Logic and Continuation-Passing Style Neel Krishnaswami University of Cambridge Proof (and Refutation) Terms Propositions A ::= > j A ^ B j ? j A _ B j :A True contexts Γ ::= · j Γ; x : A False contexts ∆ ::= · j ∆; u : A Values e ::= hi j he; e0i j L e j R e j not(k) j µu : A: c Continuations k ::= [] j [k; k0] j fst k j snd k j not(e) j µx : A: c Contradictions c ::= he jA ki 1 Expressions — Proof Terms x : A 2 Γ Hyp Γ; ∆ ` x : A true >P (No rule for ?) Γ; ∆ ` hi : > true 0 Γ; ∆ ` e : A true Γ; ∆ ` e : B true ⟨ ⟩ ^ 0 P Γ; ∆ ` e; e : A ^ B true Γ; ∆ ` e : A true Γ; ∆ ` e : B true _P1 _P2 Γ; ∆ ` L e : A _ B true Γ; ∆ ` R e : A _ B true Γ; ∆ ` k : A false :P Γ; ∆ ` not(k): :A true 2 Continuations — Refutation Terms x : A 2 ∆ Hyp Γ; ∆ ` x : A false ?R (No rule for >) Γ; ∆ ` [] : ? false 0 Γ; ∆ ` k : A false Γ; ∆ ` k : B false [ ] _ 0 R Γ; ∆ ` k; k : A _ B false Γ; ∆ ` k : A false Γ; ∆ ` k : B false ^R1 ^R2 Γ; ∆ ` fst k : A ^ B false Γ; ∆ ` snd k : A ^ B false Γ; ∆ ` e : A true :R Γ; ∆ ` not(e): :A false 3 Contradictions Γ; ∆ ` e : A true Γ; ∆ ` k : A false Contr Γ; ∆ ` he jA ki contr Γ; ∆; u : A ` c contr Γ; x : A; ∆ ` c contr Γ; ∆ ` µu : A: c : A true Γ; ∆ ` µx : A: c : A false 4 Operational Semantics hhe1; e2i jA^B fst ki 7! he1 jA ki hhe1; e2i jA^B snd ki 7! he2 jB ki hL e jA_B [k1; k2]i 7! he jA k1i hR e jA_B [k1; k2]i 7! he jB k2i hnot(k) j:A not(e)i 7! he jA ki hµu : A: c jA ki 7! [k=u]c he jA µx : A: ci 7! [e=x]c 5 Type Safety? Preservation If ·; · ` c contr and c ; c0 then ·; · ` c0 contr. Proof By case analysis on evaluation derivations! (We don’t even need induction!) 6 Type Preservation hhe1; e2i jA^B fst ki ; he1 jA ki Assumption z }|(1) { z (}|2) { Γ; ∆ ` he1; e2i : A ^ B true Γ; ∆ ` fst k : A ^ B false Γ; ∆ ` hhe1; e2i jA^B fst ki contr Assumption z (}|3) { Γ; ∆ ` e1 : A true Γ; ∆ ` e2 : B true ^P Γ; ∆ ` he1; e2i : A ^ B true Analysis of (1) z (}|4) { Γ; ∆ ` k : A false ^R1 Γ; ∆ ` fst k : A ^ B false Analysis of (2) ·; · ` he1 jA ki contr By rule on (3), (4) 7 Progress? Progress? If ·; · ` c contr then c ; c0 (or c final). Proof: 1. A closed term c is a contradiction 2. Hopefully, there aren’t any contradictions! 3. So this theorem is vacuous (assuming classical logic is consistent) 8 Making Progress Less Vacuous Propositions A ::= ::: j ans Values e ::= ::: j halt Continuations k ::= ::: j done Γ; ∆ ` halt : ans true Γ; ∆ ` done : ans false 9 Progress 0 Progress If ·; · ` c contr then c ; c or c = hhalt jans donei. Proof By induction on typing derivations 10 The Price of Progress Γ; A; ∆ ` ans true Γ; A; ∆ ` ans false Γ; ∆; A ` ans true Γ; ∆; A ` ans false Γ; A; ∆ ` contr Γ; ∆; A ` contr Γ; ∆ ` A false Γ; ∆ ` A true Γ; ∆ ` :A true Γ; ∆ ` A ^ :A true • As a term: hµu : A: hhalt j donei; not(µx : A: hhalt j donei)i • Adding a halt configuration makes classical logic inconsistent – A ^ :A is derivable 11 Embedding Classical Logic into Intuitionistic Logic • Intuitionistic logic has a clean computational reading • Classical logic almost has a clean computational reading • Q: Is there any way to equip classical logic with computational meaning? • A: Embed classical logic into intuitionistic logic 12 The Double Negation Translation • Fix an intuitionistic proposition p • Define “quasi-negation” ∼X as X ! p • Now, we can define a translation on types as follows: ◦ (:A) = ∼A◦ >◦ = 1 ◦ (A ^ B) = A◦ × B◦ ?◦ = p ◦ (A _ B) = ∼∼(A◦ + B◦) 13 Triple-Negation Elimination In general, ::X ! X is not derivable constructively. However, the following is derivable: Lemma For all X, there is a function tne :(∼∼∼X) ! ∼X ::: ` q : X ! p ::: ` x : X k : ∼∼∼X; x : X; q : ∼X ` q x : p ::: k : ∼∼∼X; x : X ` λq: q x : ∼∼X k : ∼∼∼X; x : X ` k (λq: q x): p k : ∼∼∼X ` λx: k (λq: q a): ∼X · ` ∼∼∼ ! ∼ |λk: λa: k{z(λq: q a}) :( X) X tne 14 Intuitionistic Double Negation Elimination Lemma For all A, there is a term dneA such that ◦ ◦ · ` dneA : ∼∼A ! A Proof By induction on A. dne> = λx: hi* + dneA (λk: q (λp: k (fst p))); dneA^B = λp: dneB (λk: q (λp: k (snd p))) dne? = λq: q (λx: x) ∼∼ ∼∼ ◦ _ ◦ dneA_B = λq : | (A{z B }): tne q ◦ (A_B) ∼∼ ∼ ◦ dne:A = λq : |( {zA }): tne q ◦ (:A) 15 Double Negation Elimination for ? q :(p ! p) ! p; x : p ` x : p q :(p ! p) ! p ` q :(p ! p) ! p q :(p ! p) ! p ` λx : p: x : p q :(p ! p) ! p ` q (λx : p: x): p · ` λq :(p ! p) ! p: q (λx : p: x) : ((p ! p) ! p) ! p · ` λq : ∼∼p: q (λx : p: x): ∼∼p ! p ◦ ◦ ◦ · ` λq : ∼∼⊥ : q (λx : p: x): ∼∼⊥ !? 16 Translating Derivations Theorem Classical terms embed into intutionistic terms: 1. If Γ; ∆ ` e : A true then Γ◦; ∼∆ ` e◦ : A◦. 2. If Γ; ∆ ` k : A false then Γ◦; ∼∆ ` k◦ : ∼A◦. 3. If Γ; ∆ ` c contr then Γ◦; ∼∆ ` c◦ : p. Proof By induction on derivations – but first, we have to define the translation! 17 Translating Contexts Translating Value Contexts: ◦ (·) = · ◦ (Γ; x : A) = Γ◦; x : A◦ Translating Continuation Contexts: ∼(·) = · ∼(Γ; x : A) = ∼Γ; x : ∼A◦ 18 Translating Contradictions Γ; ∆ ` e : A true Γ; ∆ ` k : A false Contr Γ; ∆ ` he jA ki contr Define: ◦ ◦ he jA ki = k e 19 Translating (Most) Expressions x◦ = x ◦ hi = hi ◦ ◦ ◦ he1; e2i = he1 ; e2 i ◦ (L e) = λk : ∼(A◦ + B◦): k (L e◦) ◦ (R e) = λk : ∼(A◦ + B◦): k (R e◦) ◦ (not(k)) = k◦ 20 Translating (Most) Continuations x◦ = x ◦ [] = λx : ans: x ◦ ◦ ◦ [k1; k2] = λk : ∼∼(A + B ): k (λi : A◦ + B◦: ! ◦ ! ◦ case(i; L x k1 x; R y k2 y)) ◦ (fst k) = λp :(A◦ × B◦): k◦ (fst p) ◦ (snd k) = λp :(A◦ × B◦): k◦ (snd p) ◦ ◦ ◦ (not(e)) = λk : |{z}∼A : k e ◦ (:A) 21 Translating Proof by Contradiction Γ; ∆; u : A ` c contr Γ; ∆ ` µu : A: c : A true 1 Γ◦; ∼(∆; u : A) ` c◦ : p Assumption 2 Γ◦; ∼∆; u : ∼A◦ ` c◦ : p Def. of ∼ on contexts 3 Γ◦; ∼∆ ` λu : ∼A◦: c◦ : ∼A◦ ! p !I 4 Γ◦; ∼∆ ` λu : ∼A◦: c◦ : ∼∼A◦ Def. of ∼ on types ◦ ◦ ◦ ◦ 5 Γ ; ∼∆ ` dneA(λu : u : ∼A : c ): A !E So we define ◦ ◦ ◦ (µu : A: c) = dneA(λu : ∼A : c ) 22 Translating Refutation by Contradiction Γ; x : A; ∆ ` c contr Γ; ∆ ` µx : A: c : A false ◦ 1. We assume Γ; x : A ; ∼∆ ` c◦ : p 2. So Γ◦; x : A◦; ∼∆ ` c◦ : p 3. So Γ◦; ∼∆ ` λx : A◦: c◦ : A◦ ! p 4. So Γ◦; ∼∆ ` λx : A◦: c◦ : ∼A◦ So we define ◦ ◦ ◦ (µx : A: c) = λx : A : c 23 Consequences • We now have a proof that every classical proof has a corresponding intuitionistic proof • So classical logic is a subsystem of intuitionistic logic • Because intuitionistic logic is consistent, so is classical logic • Classical logic can inherit operational semantics from intuitionistic logic! 24 Many Different Embeddings • Many different translations of classical logic were discovered many times • Gerhard Gentzen and Kurt Gödel • Andrey Kolmogorov • Valery Glivenko • Sigekatu Kuroda • The key property is to show that ∼∼A◦ ! A◦ holds. 25 The Gödel-Gentzen Translation Now, we can define a translation on types as follows: ◦ :A = ∼A◦ >◦ = 1 ◦ (A ^ B) = A◦ × B◦ ?◦ = p ◦ (A _ B) = ∼(∼A◦ × ∼B◦) • This uses a different de Morgan duality for disjunction 26 The Kolmogorov Translation Now, we can define another translation on types as follows: • :A = ∼∼∼A• • A ⊃ B = ∼∼(A• ! B•) >• = ∼∼1 • (A ^ B) = ∼∼(A• × B•) ?• = ∼∼⊥ • (A _ B) = ∼∼(A• + B•) • Uniformly stick a double-negation in front of each connective. • Deriving ∼∼A• ! A• is particularly easy: • The tne term will always work! 27 Implementing Classical Logic Axiomatically • The proof theory of classical logic is elegant • It is also very awkward to use: • Binding only arises from proof by contradiction • Difficult to write nested computations • Continuations/stacks are always explicit • Functional languages make the stack implicit • Can we make the continuations implicit? 28 The Typed Lambda Calculus with Continuations Types X ::= 1 j X × Y j 0 j X + Y j X ! Y j :X Terms e ::= x j hi j he; ei j fst e j snd e j abort j L e j R e j case(e; L x ! e0; R y ! e00) j λx : X: e j e e0 j throw(e; e0) j letcont x: e Contexts Γ ::= · j Γ; x : X 29 Units and Pairs 1I Γ ` hi : 1 0 Γ ` e : X Γ ` e : Y ⟨ ⟩ × 0 I Γ ` e; e : X × Y Γ ` e : X × Y Γ ` e : X × Y ×E1 ×E1 Γ ` fst e : X Γ ` snd e : Y 30 Functions and Variables X : X 2 Γ Γ; x : X ` e : Y Hyp !I Γ ` x : X Γ ` λx : X: e : X ! Y 0 Γ ` e : X ! Y Γ ` e : X ! 0 E Γ ` e e : Y 31 Sums and the Empty Type Γ ` e : X Γ ` e : Y +I1 +I2 Γ ` L e : X + Y Γ ` R e : X + Y 0 00 Γ ` e : X + Y Γ; x : X ` e : Z Γ; y : Y ` e : Z 0 00 +E Γ ` case(e; L x ! e ; R y ! e ): Z Γ ` e : 0 0E (no intro for 0) Γ ` abort e : Z 32 Continuation Typing 0 Γ; u : :X ` e : X Γ ` e : :X Γ ` e : X Cont 0 Throw Γ ` letcont u : X: e : X Γ ` throwY(e; e ): Y 33 Examples Double-negation elimination: dneX : ::X ! X dneX , λk : ::X: letcont u : :X: throw(k; u) The Excluded Middle: t : X _:X t , letcont u : :(X _:X): throw(u; R (letcont q : ::X: throw(u; L (dneX q))) 34 Continuation-Passing Style (CPS) Translation Type translation: • :X = ∼∼∼X• • X ! Y = ∼∼(X• ! Y•) 1• = ∼∼1 • (X × Y) = ∼∼(X• × Y•) 0• = ∼∼0 • (X + Y) = ∼∼(X• + Y•) Translating contexts: • (·) = · • (Γ; x : A) = Γ•; x : A• 35 The CPS Translation Theorem Theorem If Γ ` e : X then Γ• ` e• : X•.