Lecture 3: Typed Lambda Calculus and...

42
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

Transcript of Lecture 3: Typed Lambda Calculus and...

Page 1: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Lecture 3: Typed Lambda Calculus andCurry-Howard

H. Geuvers

Radboud UniversityNijmegen, NL

21st Estonian Winter School in Computer ScienceWinter 2016

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 1 / 65

Page 2: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Outline

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 2 / 65

Page 3: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Typed λ calculus as a basis for logic

λ-term : type

M : A

program : data typeproof : formula

program : (full) specification

Aim:

• Type Theory as an integrated system for proving andprogramming.

• Type Theory as a basis for proof assistants and interactivetheorem proving.

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 3 / 65

Page 4: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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 areomitted:(α→ β)→ (β → γ)→ α→ γ

• Types are denoted by A,B, . . ..

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 4 / 65

Page 5: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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 withconclusion Γ ` M : A

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 5 / 65

Page 6: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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

Page 7: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Typed Terms versus Type Assignment

• With typed terms also called typing a la Church, we haveterms 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, weassign 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

Page 8: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Church vs. Curry typing

• The Curry formulation is especially interesting forprogramming: you want to write as little type information aspossible; let the compiler infer the types for you.

• The Church formulation is especially interesting for proofchecking: terms are created interactively; type structure is sointricate that type inference is undecidable (if you start froman untyped term).[ This lecture]

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 8 / 65

Page 9: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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 propositionlogic

• x1 : B1, x2 : B2, . . . , xn : Bn ` M : A can be read asM is a proof of A from the assumptions B1,B2, . . . ,Bn.

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 9 / 65

Page 10: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Example

[A→ B → C ]3 [A]1

B → C

[A→ B]2 [A]1

B

C1

A→ C2

(A→ B)→ A→ C3

(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

Page 11: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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) : C1

λz :A.x z (y z) : A→ C2

λy :A→ B.λz :A.x z (y z) : (A→ B)→ A→ C3

λ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

Page 12: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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→ AK := λx : A.λy : B.x : A→ B → AS := λ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 thederivation rule Modus Ponens is exactly Hilbert style minimalproposition 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. Tobe precise: I = SKK.

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 12 / 65

Page 13: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Computation = Cut-elimination

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

Cut-elimination in minimal logic = β-reduction in λ→.

[A]1

D1

B1

A→ B

D2

A

B

−→

D2

AD1

B

[x : A]1

D1

M : B1

λx :A.M : A→ B

D2

P : A

(λx :A.M)P : B

−→β

D2

P : AD1

M[x := P] : B

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 13 / 65

Page 14: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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

Page 15: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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→ B

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

Page 16: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Extension with other connectives

Adding product types × to λ→. (Proposition logic withconjunction ∧.)

Γ ` M : A× B

Γ ` π1M : A

Γ ` M : A× B

Γ ` π2M : B

Γ ` P : A Γ ` Q : B

Γ ` 〈P,Q〉 : A× B

With reduction rules

π1〈P,Q〉 → P

π2〈P,Q〉 → Q

Similar rules can be given for sum-types A + B, corresponding todisjunction A ∨ B.

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 16 / 65

Page 17: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Extension to predicate logic

• First order language: domain D, with variables x , y , z : D andpossibly 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 ordervariables (ranging over the domain D) and the “proofvariables” (used as [local] assumptions of formulas).

• Formulas and domain are both types. What is the type of apredicate 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

Page 18: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Idea of extending to ∀

Term rules for the ∀-quantifier in predicate logic.

Γ ` M : ∀x :D.Aif t : D

Γ ` M t : A[x := t]

Γ ` M : Ax 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”) onlogical derivations.

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 18 / 65

Page 19: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Example

Deriving irreflexivity from anti-symmetry

AntiSymR := ∀x , y :D.(Rxy)→ (Ryx)→ ⊥IrreflR := ∀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

Page 20: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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

Page 21: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Dependent Type Theory

• We have seen informally “dependent types at work” in thepredicate 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

Page 22: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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 termstaken 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

Page 23: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Derivation rules of λP

s ranges over {∗,�}.

(base) ∅ ` (ctxt)Γ ` A : s

Γ, x :A `if x not in Γ (ax)

Γ `

Γ ` ∗ : �

(proj)Γ `

Γ ` x : Aif 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 : AA =βη B

Notation: write A→ B for Πx :A.B if x /∈ FV(B).

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 23 / 65

Page 24: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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

Page 25: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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 7→ Πx :A.P x

Intro and elim rules are just λ-abstraction and application

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 25 / 65

Page 26: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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 → bindsstrongest)

(Π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

Page 27: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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 ∼ →-typeuniversal quantification ∼ ∀-type

• Logical rules have their direct counterpart in type theoryλ-abstraction ∼ →-introductionapplication ∼ →- elimination λ-abstraction ∼ ∀-introductionapplication ∼ ∀-elimination

• Context declares signature, local varibales and assumptions.

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 27 / 65

Page 28: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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 ownlogic.

• 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 withsubstitution and binding.

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 28 / 65

Page 29: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Direct and LF embedding

proof formula

direct embedding λx :A.x A→ ALF embedding imp intrAAλx :T A.x T (A⇒ A)

• Direct representation: One type system : One logic, Logicalrules ∼ type theoretic rules

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

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 29 / 65

Page 30: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Examples of the Deep embedding

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

1 Minimal proposition logic

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

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 30 / 65

Page 31: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Minimal propositional logic

Fix the signature (context) of minimal propositional logic.

prop : ∗imp : prop→ prop→ prop

Notation:A⇒ B for impAB

The type prop is the type of ‘names’ of propositions.NB : A term of type propcan not be inhabited (proved), as it isnot a type.We ‘lift’ a name p : prop to the type of its proofs by introducingthe 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

Page 32: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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 termf : T p → T q and returns a term of type T(p ⇒ q)Indeed A⇒ A, becomes valid:

imp intrAA(λx :TA.x) : T(A⇒ A)

Exercise: Construct a term of type T(A⇒ (B ⇒ A))

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 32 / 65

Page 33: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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)(TA→ TB)→ T(A⇒ B)

imp el : (A,B : prop)T(A⇒ B)→ TA→ TB.

Desired properties of the encoding:

• Adequacy (soundness) of the encoding:

`PROP A =⇒ ΣPROP, a1:prop, . . . , an:prop ` p : TA for some p.

{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

Page 34: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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

Page 35: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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) 7→ ∀(λx :A.(Px))

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 35 / 65

Page 36: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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

Page 37: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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) proofof

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

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 37 / 65

Page 38: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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

Page 39: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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

Page 40: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Properties of λP

• Uniqueness of typesIf Γ ` M : σ and Γ ` M : τ , then σ=βητ .

• Subject ReductionIf Γ ` M : σ and M →βη N, then Γ ` N : σ.

• Strong NormalizationIf Γ ` 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

Page 41: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

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

Page 42: Lecture 3: Typed Lambda Calculus and Curry-Howardcs.ioc.ee/ewscs/2016/geuvers/geuvers-slides-lecture3.pdf · 2016. 3. 1. · Lecture 3: Typed Lambda Calculus and Curry-Howard H. Geuvers

Curry-Howard-de Bruijn

logic ∼ type theory

formula ∼ typeproof ∼ term

detour elimination ∼ β-reduction

proposition logic ∼ simply typed λ-calculuspredicate logic ∼ dependently typed λ-calculus λP

intuitionistic logic ∼ . . . + inductive typeshigher order logic ∼ . . . + higher types and polymorphism

classical logic ∼ . . . + exceptions

H. Geuvers - Radboud Univ. EWSCS 2016 Typed λ-calculus 42 / 65