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 | (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 ` a la Church Formulation with contexts to declare the free variables: x 1 : A 1 , x 2 : A 2 , . . . , x n : A n is a context, usually denoted by Γ. Derivation rules of λ → (` a la Church): x : A ∈ Γ Γ ⊢ M : A → B Γ ⊢ N : A Γ , x : A ⊢ P : B Γ ⊢ x : A Γ ⊢ M N : 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 ` a la 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 ` a la 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 • x 1 : B 1 , x 2 : B 2 , . . . , x n : B n ⊢ M : A can be read as M is a proof of A from the assumptions B 1 , B 2 , . . . , B n . 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 λ → : := λ x : A . x : A → A I K := λ x : A .λ y : B . x : A → B → A := λ x : A → B → C .λ y : A → B .λ z : A . x z ( y z ) S : ( 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 = S K K . 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 2 D 1 A D 2 B − → 1 D 1 A → B A B B [ x : A ] 1 D 2 D 1 D 2 P : A M : B − → β 1 D 1 λ x : A . M : A → B P : A 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 [ 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 ∧ .) Γ ⊢ P : A Γ ⊢ Q : B Γ ⊢ M : A × B Γ ⊢ M : A × B Γ ⊢ � P , Q � : A × B Γ ⊢ π 1 M : A Γ ⊢ π 2 M : B With reduction rules π 1 � P , Q � → P π 2 � P , Q � → 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 ∀ x : D .φ and ∃ x : 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 ∀ Term rules for the ∀ -quantifier in predicate logic. Γ ⊢ M : ∀ x : D . A Γ ⊢ M : A if t : D x not free in Γ Γ ⊢ M t : A [ x := t ] Γ ⊢ λ x : D . M : ∀ x : 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 := ∀ x , y : D . ( Rxy ) → ( Ryx ) → ⊥ Irrefl R := ∀ x : D . ( Rxx ) → ⊥ Derivation in predicate logic: ∀ x , y : D . R x y → R y x → ⊥ ∀ y : D . R x y → R y x → ⊥ [ R x x ] 1 R x x → R x x → ⊥ [ R x x ] 1 R x x → ⊥ ⊥ 1 R x x → ⊥ ∀ x : D . R x x → ⊥ H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ -calculus 19 / 65
Example derivation in type theory, with terms H : ∀ x , y : D . R x y → R y x → ⊥ H x : ∀ y : D . R x y → R y x → ⊥ [ H ′ : R x x ] 1 H x x : R x x → R x x → ⊥ H x x H ′ : R x x → ⊥ [ H ′ : R x x ] 1 H x x H ′ H ′ : ⊥ 1 λ H ′ :( R x x ) . H x x H ′ H ′ : R x x → ⊥ λ x : A .λ H ′ :( R x x ) . H x x H ′ H ′ : ∀ x : D . R x x → ⊥ H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ -calculus 20 / 65
Recommend
More recommend