Lecture 3: Typed Lambda Calculus and Curry-Howard
Total Page:16
File Type:pdf, Size:1020Kb
Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers Radboud University Nijmegen, NL 21st Estonian Winter School in Computer Science Winter 2016 H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 1 / 65 Outline H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 2 / 65 Typed λ calculus as a basis for logic λ-term : type M:A program : data type proof : formula program : (full) specification Aim: • Type Theory as an integrated system for proving and programming. • Type Theory as a basis for proof assistants and interactive theorem proving. H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 3 / 65 Simple type theory Simplest system: λ! or simple type theory, STT. Just arrow types Typ := TVar j (Typ ! Typ) • Examples: (α ! β) ! α,(α ! β) ! ((β ! γ) ! (α ! γ)) • Brackets associate to the right and outside brackets are omitted: (α ! β) ! (β ! γ) ! α ! γ • Types are denoted by A; B;:::. H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 4 / 65 Simple type theory `ala Church Formulation with contexts to declare the free variables: x1 : A1; x2 : A2;:::; xn : An is a context, usually denoted byΓ. Derivation rules of λ! (`ala Church): x:A 2 Γ Γ ` M : A ! B Γ ` N : A Γ; x:A ` P : B Γ ` x : A Γ ` MN : B Γ ` λx:A:P : A ! B Γ `λ! M : A if there is a derivation using these rules with conclusion Γ ` M : A H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 5 / 65 Examples ` λx : A.λy : B:x : A ! B ! A ` λx : A ! B.λy : B ! C.λz : A:y (x z):(A!B)!(B!C)!A!C ` λx : A.λy :(B ! A) ! A:y(λz : B:x): A ! ((B ! A) ! A) ! A Not for every type there is a closed term of that type: (A ! A) ! A is not inhabited That is: there is no term M such that ` M :(A ! A) ! A: H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 6 / 65 Typed Terms versus Type Assignment • With typed terms also called typing `ala Church, we have terms with type information in the λ-abstraction λx : A:x : A ! A • Terms have unique types, • The type is directly computed from the type info in the variables. • With typed assignment also called typing `ala Curry, we assign types to untyped λ-terms λx:x : A ! A • Terms do not have unique types, • A principal type can be computed using unification. H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 7 / 65 Church vs. Curry typing • The Curry formulation is especially interesting for programming: you want to write as little type information as possible; let the compiler infer the types for you. • The Church formulation is especially interesting for proof checking: terms are created interactively; type structure is so intricate that type inference is undecidable (if you start from an untyped term). [ This lecture] H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 8 / 65 Formulas-as-Types (Curry, Howard) Recall: there are two readings of a judgement M : A 1 term as algorithm/program, type as specification: M is a function of type A 2 type as a proposition, term as its proof: M is a proof of the proposition A • There is a one-to-one correspondence: typable terms in λ!' derivations in minimal proposition logic • x1 : B1; x2 : B2;:::; xn : Bn ` M : A can be read as M is a proof of A from the assumptions B1; B2;:::; Bn. H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 9 / 65 Example [A ! B ! C]3 [A]1 [A ! B]2 [A]1 B ! C B C 1 ' A ! C 2 (A ! B) ! A ! C 3 (A ! B ! C) ! (A ! B) ! A ! C λx:A ! B ! C.λy:A ! B.λz:A:x z (y z) :(A ! B ! C) ! (A ! B) ! A ! C H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 10 / 65 Example [x : A ! B ! C]3 [z : A]1 [y : A ! B]2 [z : A]1 x z : B ! C y z : B x z (y z): C 1 λz:A:x z (y z): A ! C 2 λy:A ! B.λz:A:x z (y z):( A ! B) ! A ! C 3 λx:A ! B ! C.λy:A ! B.λz:A:x z (y z):( A!B!C)!(A!B)!A!C Exercise: Give the derivation that corresponds to λx:C ! E.λy:(C ! E) ! E:y(λz:y x): (C ! E) ! ((C ! E) ! E) ! E H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 11 / 65 Typed Combinatory Logic We have seen Combinatory Logic with the axioms for I, K and S. We now know their typed definition in λ!: I := λx : A:x : A ! A K := λx : A.λy : B:x : A ! B ! A S := λx:A ! B ! C.λy:A ! B.λz:A:x z (y z) :(A ! B ! C) ! (A ! B) ! A ! C • The three axiom schemes A ! A, A ! B ! A and (A ! B ! C) ! (A ! B) ! A ! C together with the derivation rule Modus Ponens is exactly Hilbert style minimal proposition logic. • The typed CL terms are exactly the derivations in this logic. • Modus Ponens corresponds with Application in CL Exercise: Show that the scheme A ! A is derivable. Cast in CL terminology: I can be defined in terms of S and K. To be precise: I = SKK. H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 12 / 65 Computation = Cut-elimination • β-reduction:(λx:A:M)P !β M[x := P] Cut-elimination in minimal logic= β-reduction in λ!. [A]1 D D1 2 A B D2 −! 1 D A ! B A 1 B B [x : A]1 D1 D2 D P : A M : B 2 −! 1 β D λx:A:M : A ! B P : A 1 M[x := P]: B (λx:A:M)P : B H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 13 / 65 Example Proof of A ! A ! B; (A ! B) ! A ` B with a cut. [A]1 A ! A ! B [A]1 A ! A ! B [A]1 A ! B [A]1 A ! B B B (A ! B) ! A A ! B A ! B A B It contains a cut: a !-i directly followed by an !-e. H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 14 / 65 Example proof with term information [x : A]1 p : A ! A ! B [y : A]1 p : A ! A ! B [x : A]1 p x : A ! B [y : A]1 p y : A ! B p x x : B p y y : B q :(A ! B) ! A λx:A:p x x : A ! B λy:A:p y y : A ! B q(λx:A:p x x): A (λy:A:p y y)(q(λx:A:p x x)): B Term contains a β-redex: (λx:A:p x x)(q(λx:A:p x x)) H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 15 / 65 Extension with other connectives Adding product types × to λ!. (Proposition logic with conjunction ^.) Γ ` M : A × B Γ ` M : A × B Γ ` P : A Γ ` Q : B Γ ` π1M : A Γ ` π2M : B Γ ` hP; Qi : A × B With reduction rules π1hP; Qi ! P π2hP; Qi ! Q Similar rules can be given for sum-types A + B, corresponding to disjunction A _ B. H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 16 / 65 Extension to predicate logic • First order language: domain D, with variables x; y; z : D and possibly functions over D, e.g. f : D ! D, g : D ! D ! D. • Rules for 8x:D.φ and 9x:D.φ. • NB There are two \kinds" of variables: the first order variables (ranging over the domain D) and the \proof variables" (used as [local] assumptions of formulas). • Formulas and domain are both types. What is the type of a predicate or relation? • A predicate P is a map from D to the collection of types, ∗ • P : D ! ∗ for P a predicate and R : D ! D ! ∗ for R a binary relation on D. • We will have to make this more precise . H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 17 / 65 Idea of extending to 8 Term rules for the 8-quantifier in predicate logic. Γ ` M : 8x:D:A Γ ` M : A if t : D x not free in Γ Γ ` M t : A[x := t] Γ ` λx:D:M : 8x:D:A With the usual β-reduction rule (λx:D:M)t ! M[x := t] . This conforms with cut-elimination (or \detour elimination") on logical derivations. H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 18 / 65 Example Deriving irreflexivity from anti-symmetry AntiSym R := 8x; y:D:(Rxy) ! (Ryx) !? Irrefl R := 8x:D:(Rxx) !? Derivation in predicate logic: 8x; y:D:R x y ! R y x !? 8y:D:R x y ! R y x !? R x x ! R x x !? [R x x]1 R x x !? [R x x]1 ? 1 R x x !? 8x:D:R x x !? H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 19 / 65 Example derivation in type theory, with terms H : 8x; y:D:R x y ! R y x !? H x : 8y:D:R x y ! R y x !? H x x : R x x ! R x x !? [H0 : R x x]1 H x x H0 : R x x !? [H0 : R x x]1 H x x H0 H0 : ? 1 λH0:(R x x):H x x H0 H0 : R x x !? λx:A.λH0:(R x x):H x x H0 H0 : 8x:D:R x x !? H.