Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers - - PowerPoint PPT Presentation

lecture 3 typed lambda calculus and curry howard
SMART_READER_LITE
LIVE PREVIEW

Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers - - PowerPoint PPT Presentation

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 -


slide-1
SLIDE 1

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

slide-2
SLIDE 2

Outline

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 2 / 65

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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
  • mitted:

(α → β) → (β → γ) → α → γ

  • Types are denoted by A, B, . . ..
  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 4 / 65

slide-5
SLIDE 5

Simple type theory ` a la Church

Formulation with contexts to declare the free variables: x1 : A1, x2 : A2, . . . , xn : An is a context, usually denoted by Γ. Derivation rules of λ→ (` a la Church): x:A ∈ Γ Γ ⊢ x : A Γ ⊢ M : A → B Γ ⊢ N : A Γ ⊢ M N : B Γ, x:A ⊢ P : 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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Example

[A → B → C]3 [A]1 B → C [A → B]2 [A]1 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

slide-11
SLIDE 11

Example

[x : A → B → C]3 [z : A]1 x z : B → C [y : A → B]2 [z : A]1 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

slide-12
SLIDE 12

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 = S K K.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 12 / 65

slide-13
SLIDE 13

Computation = Cut-elimination

  • β-reduction: (λx:A.M)P →β M[x := P]

Cut-elimination in minimal logic = β-reduction in λ→. [A]1 D1 B 1 A → B D2 A B − → D2 A D1 B [x : A]1 D1 M : B 1 λx:A.M : A → B D2 P : A (λx:A.M)P : B − →β D2 P : A D1 M[x := P] : B

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 13 / 65

slide-14
SLIDE 14

Example

Proof of A → A → B, (A → B) → A ⊢ B with a cut. [A]1 [A]1 A → A → B A → B B A → B (A → B) → A [A]1 [A]1 A → A → B A → B 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

slide-15
SLIDE 15

Example proof with term information

[y : A]1 [y : A]1 p : A → A → B p y : A → B p y y : B λy:A.p y y : A → B q : (A → B) → A [x : A]1 [x : A]1 p : A → A p x : A → B p x x : B λx:A.p x x : 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

slide-16
SLIDE 16

Extension with other connectives

Adding product types × to λ→. (Proposition logic with conjunction ∧.) Γ ⊢ M : A × B Γ ⊢ π1M : A Γ ⊢ M : A × B Γ ⊢ π2M : B Γ ⊢ P : A Γ ⊢ Q : B Γ ⊢ P, Q : A × B With reduction rules π1P, Q → P π2P, 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

slide-17
SLIDE 17

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

slide-18
SLIDE 18

Idea of extending to ∀

Term rules for the ∀-quantifier in predicate logic. Γ ⊢ M : ∀x:D.A if t : D Γ ⊢ M t : A[x := t] Γ ⊢ M : A x not free in Γ Γ ⊢ λ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

slide-19
SLIDE 19

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 → R x x → ⊥ [R x x]1 R x x → ⊥ [R x x]1 ⊥ 1 R x x → ⊥ ∀x:D.R x x → ⊥

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 19 / 65

slide-20
SLIDE 20

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 x x : R x x → R x x → ⊥ [H′ : R x x]1 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

slide-21
SLIDE 21

Dependent Type Theory

  • We have seen informally “dependent types at work” in the

predicate logic example.

  • Now: the rules

With dependent types:

  • everything depends on everything
  • we can’t first define the types, then the terms
  • two universes: ∗ and
  • ∗ is the universe of types
  • We can’t have ∗ : ∗, so we have another universe: ∗ : .

NB The Coq system uses “Set” and “Prop” for what I call ∗ and “Type” for what I call .

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 21 / 65

slide-22
SLIDE 22

First order Dependent Type theory, λP

Derive judgements of the form Γ ⊢ M : B

  • Γ is a context

x1 : B1, x2 : B2, . . . , xn : Bn

  • M and B are terms

taken from the set of pseudoterms T ::= Var | ∗ | | (T T) | (λx:T.T) | Πx:T.T Auxiliary judgement Γ ⊢ denoting that Γ is a correct context.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 22 / 65

slide-23
SLIDE 23

Derivation rules of λP

s ranges over {∗, }. (base) ∅ ⊢ (ctxt) Γ ⊢ A : s Γ, x:A ⊢ if x not in Γ (ax) Γ ⊢ Γ ⊢ ∗ : (proj) Γ ⊢ Γ ⊢ x : A if x:A ∈ Γ (Π) Γ ⊢ A : ∗ Γ, x:A ⊢ B : s Γ ⊢ Πx:A.B : s (λ) Γ, x:A ⊢ M : B Γ ⊢ Πx:A.B : s Γ ⊢ λx:A.M : Πx:A.B (app) Γ ⊢ M : Πx:A.B Γ ⊢ N : A Γ ⊢ MN : B[x := N] (conv) Γ ⊢ M : B Γ ⊢ A : s Γ ⊢ M : A A =βη B Notation: write A → B for Πx:A.B if x / ∈ FV(B).

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 23 / 65

slide-24
SLIDE 24

The use of the Π-type

  • The Π rule allows to form two forms of function types.

(Π) Γ, x:A ⊢ B : s Γ ⊢ A : ∗ Γ ⊢ Πx:A.B : s Πx:A.B ≃ {f | ∀a : A(f a : B[x := a])} Write A → B if x / ∈ FV(B)

  • With s = ∗, we can form D → D and Πx:D.x = x, etc.
  • With s = , we can form D → D → ∗ and D → ∗.
  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 24 / 65

slide-25
SLIDE 25

Representation of PRED (minimal predicate logic) into λP

Represent both the domains of the logic and the formulas as types. A : ∗, P : A → ∗, R : A → A → ∗, Now implication is represented as → and ∀ is represented as Π: ∀x:A.P x → Πx:A.P x Intro and elim rules are just λ-abstraction and application

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 25 / 65

slide-26
SLIDE 26

Example

A : ∗, R : A → A → ∗ ⊢ λz:A.λh:(Πx, y:A.R x y).h z z : Πz:A.(Πx, y:A.R x y) → R z z This term is a proof of ∀z:A.(∀x, y:A.R(x, y)) → R(z, z) Exercise: Find terms of the following types (NB → binds strongest) (Πx:A.P x → Q x) → (Πx:A.P x) → Πx:A.Q x and (Πx:A.P x → Πz.R z z) → (Πx:A.P x) → Πz:A.R z z). Also write down the contexts in which these terms are typed.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 26 / 65

slide-27
SLIDE 27

Direct embedding of logic in type theory

For λ→ and λP we have seen Direct representations of logic in type theory.

  • Connectives each have a counterpart in the type theory:

implication ∼ →-type universal quantification ∼ ∀-type

  • Logical rules have their direct counterpart in type theory

λ-abstraction ∼ →-introduction application ∼ →- elimination λ-abstraction ∼ ∀-introduction application ∼ ∀-elimination

  • Context declares signature, local varibales and assumptions.
  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 27 / 65

slide-28
SLIDE 28

LF embedding of logic in type theory

Second way of interpreting logic in type theory De Bruijn: Logical framework encoding of logic in type theory.

  • Type theory used as a meta system for encoding ones own

logic.

  • Choose an appropriate context ΓL, in which the logic L

(including its proof rules) is declared.

  • Context used as a signature for the logic.
  • Use the type system as the ‘meta’ calculus for dealing with

substitution and binding.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 28 / 65

slide-29
SLIDE 29

Direct and LF embedding

proof formula direct embedding λx:A.x A → A LF embedding imp intr A A λx:T A.x T(A ⇒ A)

  • Direct representation: One type system : One logic, Logical

rules ∼ type theoretic rules

  • LF encoding One type system : Many logics, Logical rules ∼

context declarations

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 29 / 65

slide-30
SLIDE 30

Examples of the Deep embedding

The encoding of logics in a logical framework is shown by three examples:

1 Minimal proposition logic 2 Minimal predicate logic (just {⇒, ∀}) 3 Untyped λ-calculus

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 30 / 65

slide-31
SLIDE 31

Minimal propositional logic

Fix the signature (context) of minimal propositional logic. prop : ∗ imp : prop → prop → prop Notation: A ⇒ B for imp A B The type prop is the type of ‘names’ of propositions. NB : A term of type propcan not be inhabited (proved), as it is not a type. We ‘lift’ a name p : prop to the type of its proofs by introducing the following map: T : prop → ∗. Intended meaning of Tp is ‘the type of proofs of p’. We interpret ‘p is valid’ by ‘Tp is inhabited’.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 31 / 65

slide-32
SLIDE 32

Encoding of derivations

To derive Tp we also encode the logical derivation rules imp intr : Πp, q : prop.(Tp → Tq) → T(p ⇒ q), imp el : Πp, q : prop.T(p ⇒ q) → Tp → Tq. New phenomenon: Π-type: Πx:A.B(x) ≃ the type of functions f such that f a : B(a) for all a:A imp intr takes two (names of) propositions p and q and a term f : T p → T q and returns a term of type T(p ⇒ q) Indeed A ⇒ A, becomes valid: imp intrA A(λx:T A.x) : T(A ⇒ A) Exercise: Construct a term of type T(A ⇒ (B ⇒ A))

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 32 / 65

slide-33
SLIDE 33

Signature of PROP in LF

To encode proposition logic in LF we need a context (signature) ΣPROP: prop : ∗ ⇒ : prop → prop → prop T : prop → ∗ imp intr : (A, B : prop)(T A → T B) → T(A ⇒ B) imp el : (A, B : prop)T(A ⇒ B) → T A → T B. Desired properties of the encoding:

  • Adequacy (soundness) of the encoding:

⊢PROP A = ⇒ ΣPROP, a1:prop, . . . , an:prop ⊢ p : T A for some {a, . . . , an} is the set of proposition variables in A.

  • Faithfulness (or completeness) is the converse. It also holds,

but more involved to prove.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 33 / 65

slide-34
SLIDE 34

Minimal predicate logic over one domain A

Signature: prop : ∗, A : ∗, T : prop → ∗ f : A → A, R : A → A → prop, ⇒ : prop → prop → prop, imp intr : Πp, q : prop.(Tp → Tq) → T(p ⇒ q), imp el : Πp, q : prop.T(p ⇒ q) → Tp → Tq. Now encode ∀: ∀ takes a P : A → prop and returns a proposition, so we add: ∀ : (A → prop) → prop

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 34 / 65

slide-35
SLIDE 35

Minimal predicate logic over one domain A

Signature: ΣPRED prop : ∗, A : ∗, . . . imp intr : Πp, q : prop.(Tp → Tq) → T(p ⇒ q), imp el : Πp, q : prop.T(p ⇒ q) → Tp → Tq. Now encode ∀: ∀ takes a P : A → prop and returns a proposition, so: ∀ : (A → prop) → prop Universal quantification is translated as follows. ∀x:A.(Px) → ∀(λx:A.(Px))

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 35 / 65

slide-36
SLIDE 36

Intro and Elim rules for ∀

∀ : (A → prop) → prop, ∀ intr : ΠP:A → prop.(Πx:A.T(Px)) → T(∀P), ∀ elim : ΠP:A → prop.T(∀P) → Πx:A.T(Px). The proof of ∀z:A(∀x, y:A.Rxy) ⇒ Rzz is now mirrored by the proof-term ∀ intr[ ]( λz:A.imp intr[ ][ ](λh:T(∀x, y:A.Rxy). ∀ elim[ ](∀ elim[ ]hz)z) ) We have replaced the instantiations of the Π-type by [ ]. This term is of type T(∀(λz:A.imp(∀(λx:A.(∀(λy:A.Rxy))))(Rzz)))

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 36 / 65

slide-37
SLIDE 37

Intro and Elim rules for ∀

∀ : (A → prop) → prop, ∀ intr : ΠP:A → prop.(Πx:A.T(Px)) → T(∀P), ∀ elim : ΠP:A → prop.T(∀P) → Πx:A.T(Px). The proof of ∀z:A(∀x, y:A.Rxy) ⇒ Rzz is now mirrored by the proof-term ∀ intr[ ]( λz:A.imp intr[ ][ ](λh:T(∀x, y:A.Rxy). ∀ elim[ ](∀ elim[ ]hz)z) ) Exercise: Construct a proof-term that mirrors the (obvious) proof

  • f

∀x(P x ⇒ Q x) ⇒ ∀x.P x ⇒ ∀x.Q x

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 37 / 65

slide-38
SLIDE 38

Untyped λ-calculus

Signature Σlambda : D : ∗; app : D → (D → D); abs : (D → D) → D.

  • A variable x in λ-calculus becomes x : D in the type system.
  • The translation [−] : Λ → Term(D) is defined as follows.

[x] = x; [PQ] = app [P] [Q]; [λx.P] = abs (λx:D.[P]). Examples: [λx.xx] := abs(λx:D.app x x) [(λx.xx)(λy.y)] := app(abs(λx:D.app x x))(abs(λy:D.y)).

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 38 / 65

slide-39
SLIDE 39

Introducing β-equality

eq:D → D → ∗. Notation P = Q for eq P Q. Rules for proving equalities. refl : Πx:D.x = x, sym : Πx, y:D.x = y → y = x, trans : Πx, y, z:D.x = y → y = z → x = z, mon : Πx, x′, z, z′:D.x = x′ → z = z′ → (app z x) = (app z′ x′), xi : Πf , g:D → D.(Πx:D.(fx) = (gx)) → (abs f ) = (abs g), beta : Πf :D → D.Πx:D.(app(abs f )x) = (fx).

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 39 / 65

slide-40
SLIDE 40

Properties of λP

  • Uniqueness of types

If Γ ⊢ M : σ and Γ ⊢ M : τ, then σ=βητ.

  • Subject Reduction

If Γ ⊢ M : σ and M →βη N, then Γ ⊢ N : σ.

  • Strong Normalization

If Γ ⊢ M : σ, then all βη-reductions from M terminate. Proof of SN is by defining a reduction preserving map from λP to λ→.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 40 / 65

slide-41
SLIDE 41

Decidability Questions

Γ ⊢ M : σ? TCP Γ ⊢ M : ? TSP Γ ⊢? : σ TIP For λP:

  • TIP is undecidable
  • TCP/TSP: simultaneously with Context checking
  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 41 / 65

slide-42
SLIDE 42

Curry-Howard-de Bruijn

logic ∼ type theory formula ∼ type proof ∼ term detour elimination ∼ β-reduction proposition logic ∼ simply typed λ-calculus predicate logic ∼ dependently typed λ-calculus λP intuitionistic logic ∼ . . . + inductive types higher order logic ∼ . . . + higher types and polymorphism classical logic ∼ . . . + exceptions

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Typed λ-calculus 42 / 65