lecture 15 the curry-howard isomorphismlecture 15 | the curry-howard isomorphism zach tatlock winter...

75
CSE 505: Programming Languages Lecture 15 — The Curry-Howard Isomorphism Zach Tatlock Winter 2015

Upload: others

Post on 15-Mar-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

CSE 505: Programming Languages

Lecture 15 — The Curry-Howard Isomorphism

Zach TatlockWinter 2015

Page 2: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

We are Language Designers!

What have we done?I Define a programming language

I we were fairly formalI still pretty close to OCaml if you squint real hard

I Define a type systemI outlaw bad programs that “get stuck”I sound: no typable programs get stuckI incomplete: knocked out some OK programs too, ohwell

Zach Tatlock CSE 505 Winter 2015, Lecture 15 2

Page 3: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

We are Language Designers!

What have we done?I Define a programming language

I we were fairly formalI still pretty close to OCaml if you squint real hard

I Define a type systemI outlaw bad programs that “get stuck”I sound: no typable programs get stuckI incomplete: knocked out some OK programs too, ohwell

Zach Tatlock CSE 505 Winter 2015, Lecture 15 2

Page 4: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Elsewhere in the Universe (or the other side of campus)

What do logicians do?I Define formal logics

I tools to precisely state propositions

I Define proof systemsI tools to figure out which propositions are true

Turns out, we did that too!

Zach Tatlock CSE 505 Winter 2015, Lecture 15 3

Page 5: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Elsewhere in the Universe (or the other side of campus)

What do logicians do?I Define formal logics

I tools to precisely state propositions

I Define proof systemsI tools to figure out which propositions are true

Turns out, we did that too!

Zach Tatlock CSE 505 Winter 2015, Lecture 15 3

Page 6: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Elsewhere in the Universe (or the other side of campus)

What do logicians do?I Define formal logics

I tools to precisely state propositions

I Define proof systemsI tools to figure out which propositions are true

Turns out, we did that too!

Zach Tatlock CSE 505 Winter 2015, Lecture 15 3

Page 7: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Punchline

We are accidental logicians!

The Curry-Howard Isomorphism

I Proofs : Propositions :: Programs : Types

I proofs are to propositions as programs are to types

Zach Tatlock CSE 505 Winter 2015, Lecture 15 4

Page 8: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Punchline

We are accidental logicians!

The Curry-Howard Isomorphism

I Proofs : Propositions :: Programs : Types

I proofs are to propositions as programs are to types

Zach Tatlock CSE 505 Winter 2015, Lecture 15 4

Page 9: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Woah. Back up a second. Logic?!

Let’s trim down our (explicitly typed) simply-typed λ-calculus to:

e ::= x | λx. e | e e| (e, e) | e.1 | e.2| A(e) | B(e) | match e with Ax. e | Bx. e

τ ::= b | τ → τ | τ ∗ τ | τ + τ

I Lambdas, Pairs, and Sums

I Any number of base types b1, b2, . . .

I No constants (can add one or more if you want)

I No fix

What good is this?!

Well, even sans constants, plenty of terms type-check with Γ = ·

Zach Tatlock CSE 505 Winter 2015, Lecture 15 5

Page 10: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Woah. Back up a second. Logic?!

Let’s trim down our (explicitly typed) simply-typed λ-calculus to:

e ::= x | λx. e | e e| (e, e) | e.1 | e.2| A(e) | B(e) | match e with Ax. e | Bx. e

τ ::= b | τ → τ | τ ∗ τ | τ + τ

I Lambdas, Pairs, and Sums

I Any number of base types b1, b2, . . .

I No constants (can add one or more if you want)

I No fix

What good is this?!

Well, even sans constants, plenty of terms type-check with Γ = ·

Zach Tatlock CSE 505 Winter 2015, Lecture 15 5

Page 11: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Woah. Back up a second. Logic?!

Let’s trim down our (explicitly typed) simply-typed λ-calculus to:

e ::= x | λx. e | e e| (e, e) | e.1 | e.2| A(e) | B(e) | match e with Ax. e | Bx. e

τ ::= b | τ → τ | τ ∗ τ | τ + τ

I Lambdas, Pairs, and Sums

I Any number of base types b1, b2, . . .

I No constants (can add one or more if you want)

I No fix

What good is this?!

Well, even sans constants, plenty of terms type-check with Γ = ·

Zach Tatlock CSE 505 Winter 2015, Lecture 15 5

Page 12: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b. x

has type

b→ b

Zach Tatlock CSE 505 Winter 2015, Lecture 15 6

Page 13: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b. x

has type

b→ b

Zach Tatlock CSE 505 Winter 2015, Lecture 15 6

Page 14: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b1. λf :b1 → b2. f x

has type

b1 → (b1 → b2)→ b2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 7

Page 15: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b1. λf :b1 → b2. f x

has type

b1 → (b1 → b2)→ b2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 7

Page 16: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b1 → b2 → b3. λy:b2. λz:b1. x z y

has type

(b1 → b2 → b3)→ b2 → b1 → b3

Zach Tatlock CSE 505 Winter 2015, Lecture 15 8

Page 17: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b1 → b2 → b3. λy:b2. λz:b1. x z y

has type

(b1 → b2 → b3)→ b2 → b1 → b3

Zach Tatlock CSE 505 Winter 2015, Lecture 15 8

Page 18: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b1. (A(x),A(x))

has type

b1 → ((b1 + b7) ∗ (b1 + b4))

Zach Tatlock CSE 505 Winter 2015, Lecture 15 9

Page 19: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b1. (A(x),A(x))

has type

b1 → ((b1 + b7) ∗ (b1 + b4))

Zach Tatlock CSE 505 Winter 2015, Lecture 15 9

Page 20: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λf :b1 → b3. λg:b2 → b3. λz:b1 + b2.(match z with Ax. f x | Bx. g x)

has type

(b1 → b3)→ (b2 → b3)→ (b1 + b2)→ b3

Zach Tatlock CSE 505 Winter 2015, Lecture 15 10

Page 21: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λf :b1 → b3. λg:b2 → b3. λz:b1 + b2.(match z with Ax. f x | Bx. g x)

has type

(b1 → b3)→ (b2 → b3)→ (b1 + b2)→ b3

Zach Tatlock CSE 505 Winter 2015, Lecture 15 10

Page 22: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b1 ∗ b2. λy:b3. ((y, x.1), x.2)

has type

(b1 ∗ b2)→ b3 → ((b3 ∗ b1) ∗ b2)

Zach Tatlock CSE 505 Winter 2015, Lecture 15 11

Page 23: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b1 ∗ b2. λy:b3. ((y, x.1), x.2)

has type

(b1 ∗ b2)→ b3 → ((b3 ∗ b1) ∗ b2)

Zach Tatlock CSE 505 Winter 2015, Lecture 15 11

Page 24: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Empty and Nonempty Types

Just saw a few “nonempty” types

I τ nonempy if closed term e has type τ

I τ empty otherwise

Are there any empty types?

Sure! b1 b1 → b2 b1 → (b2 → b1)→ b2

What does this one mean?

b1 + (b1 → b2)

I wonder if there’s any way to distinguish empty vs. nonempty...

Ohwell, now for a totally irrelevant tangent!

Zach Tatlock CSE 505 Winter 2015, Lecture 15 12

Page 25: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Empty and Nonempty Types

Just saw a few “nonempty” types

I τ nonempy if closed term e has type τ

I τ empty otherwise

Are there any empty types?

Sure! b1 b1 → b2 b1 → (b2 → b1)→ b2

What does this one mean?

b1 + (b1 → b2)

I wonder if there’s any way to distinguish empty vs. nonempty...

Ohwell, now for a totally irrelevant tangent!

Zach Tatlock CSE 505 Winter 2015, Lecture 15 12

Page 26: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Empty and Nonempty Types

Just saw a few “nonempty” types

I τ nonempy if closed term e has type τ

I τ empty otherwise

Are there any empty types?

Sure! b1 b1 → b2 b1 → (b2 → b1)→ b2

What does this one mean?

b1 + (b1 → b2)

I wonder if there’s any way to distinguish empty vs. nonempty...

Ohwell, now for a totally irrelevant tangent!

Zach Tatlock CSE 505 Winter 2015, Lecture 15 12

Page 27: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Empty and Nonempty Types

Just saw a few “nonempty” types

I τ nonempy if closed term e has type τ

I τ empty otherwise

Are there any empty types?

Sure! b1 b1 → b2 b1 → (b2 → b1)→ b2

What does this one mean?

b1 + (b1 → b2)

I wonder if there’s any way to distinguish empty vs. nonempty...

Ohwell, now for a totally irrelevant tangent!

Zach Tatlock CSE 505 Winter 2015, Lecture 15 12

Page 28: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Empty and Nonempty Types

Just saw a few “nonempty” types

I τ nonempy if closed term e has type τ

I τ empty otherwise

Are there any empty types?

Sure! b1 b1 → b2 b1 → (b2 → b1)→ b2

What does this one mean?

b1 + (b1 → b2)

I wonder if there’s any way to distinguish empty vs. nonempty...

Ohwell, now for a totally irrelevant tangent!

Zach Tatlock CSE 505 Winter 2015, Lecture 15 12

Page 29: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Empty and Nonempty Types

Just saw a few “nonempty” types

I τ nonempy if closed term e has type τ

I τ empty otherwise

Are there any empty types?

Sure! b1 b1 → b2 b1 → (b2 → b1)→ b2

What does this one mean?

b1 + (b1 → b2)

I wonder if there’s any way to distinguish empty vs. nonempty...

Ohwell, now for a totally irrelevant tangent!

Zach Tatlock CSE 505 Winter 2015, Lecture 15 12

Page 30: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Totally irrelevant tangent.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 13

Page 31: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Propositional Logic

Suppose we have some set b of basic propositions b1, b2, . . .

I e.g. “ML is better than Haskell”

Then, using standard operators ⊃,∧,∨, we can define formulas:

p ::= b | p ⊃ p | p ∧ p | p ∨ pI e.g. “ML is better than Haskell” ∧ “Haskell is not pure”

Some formulas are tautologies: by virtue of their structure, theyare always true regardless of the truth of their constituentpropositions.

I e.g. p1 ⊃ p1

Not too hard to build a proof system to establish tautologyhood.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 14

Page 32: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Propositional Logic

Suppose we have some set b of basic propositions b1, b2, . . .

I e.g. “ML is better than Haskell”

Then, using standard operators ⊃,∧,∨, we can define formulas:

p ::= b | p ⊃ p | p ∧ p | p ∨ pI e.g. “ML is better than Haskell” ∧ “Haskell is not pure”

Some formulas are tautologies: by virtue of their structure, theyare always true regardless of the truth of their constituentpropositions.

I e.g. p1 ⊃ p1

Not too hard to build a proof system to establish tautologyhood.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 14

Page 33: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Propositional Logic

Suppose we have some set b of basic propositions b1, b2, . . .

I e.g. “ML is better than Haskell”

Then, using standard operators ⊃,∧,∨, we can define formulas:

p ::= b | p ⊃ p | p ∧ p | p ∨ pI e.g. “ML is better than Haskell” ∧ “Haskell is not pure”

Some formulas are tautologies: by virtue of their structure, theyare always true regardless of the truth of their constituentpropositions.

I e.g. p1 ⊃ p1

Not too hard to build a proof system to establish tautologyhood.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 14

Page 34: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Propositional Logic

Suppose we have some set b of basic propositions b1, b2, . . .

I e.g. “ML is better than Haskell”

Then, using standard operators ⊃,∧,∨, we can define formulas:

p ::= b | p ⊃ p | p ∧ p | p ∨ pI e.g. “ML is better than Haskell” ∧ “Haskell is not pure”

Some formulas are tautologies: by virtue of their structure, theyare always true regardless of the truth of their constituentpropositions.

I e.g. p1 ⊃ p1

Not too hard to build a proof system to establish tautologyhood.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 14

Page 35: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Propositional Logic

Suppose we have some set b of basic propositions b1, b2, . . .

I e.g. “ML is better than Haskell”

Then, using standard operators ⊃,∧,∨, we can define formulas:

p ::= b | p ⊃ p | p ∧ p | p ∨ pI e.g. “ML is better than Haskell” ∧ “Haskell is not pure”

Some formulas are tautologies: by virtue of their structure, theyare always true regardless of the truth of their constituentpropositions.

I e.g. p1 ⊃ p1

Not too hard to build a proof system to establish tautologyhood.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 14

Page 36: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Proof System

Γ ::= · | Γ, p

Γ ` p

Γ ` p1 Γ ` p2Γ ` p1 ∧ p2

Γ ` p1 ∧ p2Γ ` p1

Γ ` p1 ∧ p2Γ ` p2

Γ ` p1Γ ` p1 ∨ p2

Γ ` p2Γ ` p1 ∨ p2

Γ ` p1 ∨ p2 Γ, p1 ` p3 Γ, p2 ` p3Γ ` p3

p ∈ Γ

Γ ` pΓ, p1 ` p2

Γ ` p1 ⊃ p2Γ ` p1 ⊃ p2 Γ ` p1

Γ ` p2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 15

Page 37: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Proof System

Γ ::= · | Γ, p

Γ ` p

Γ ` p1 Γ ` p2Γ ` p1 ∧ p2

Γ ` p1 ∧ p2Γ ` p1

Γ ` p1 ∧ p2Γ ` p2

Γ ` p1Γ ` p1 ∨ p2

Γ ` p2Γ ` p1 ∨ p2

Γ ` p1 ∨ p2 Γ, p1 ` p3 Γ, p2 ` p3Γ ` p3

p ∈ Γ

Γ ` pΓ, p1 ` p2

Γ ` p1 ⊃ p2Γ ` p1 ⊃ p2 Γ ` p1

Γ ` p2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 15

Page 38: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Proof System

Γ ::= · | Γ, p

Γ ` p

Γ ` p1 Γ ` p2Γ ` p1 ∧ p2

Γ ` p1 ∧ p2Γ ` p1

Γ ` p1 ∧ p2Γ ` p2

Γ ` p1Γ ` p1 ∨ p2

Γ ` p2Γ ` p1 ∨ p2

Γ ` p1 ∨ p2 Γ, p1 ` p3 Γ, p2 ` p3Γ ` p3

p ∈ Γ

Γ ` pΓ, p1 ` p2

Γ ` p1 ⊃ p2Γ ` p1 ⊃ p2 Γ ` p1

Γ ` p2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 15

Page 39: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Proof System

Γ ::= · | Γ, p

Γ ` p

Γ ` p1 Γ ` p2Γ ` p1 ∧ p2

Γ ` p1 ∧ p2Γ ` p1

Γ ` p1 ∧ p2Γ ` p2

Γ ` p1Γ ` p1 ∨ p2

Γ ` p2Γ ` p1 ∨ p2

Γ ` p1 ∨ p2 Γ, p1 ` p3 Γ, p2 ` p3Γ ` p3

p ∈ Γ

Γ ` pΓ, p1 ` p2

Γ ` p1 ⊃ p2Γ ` p1 ⊃ p2 Γ ` p1

Γ ` p2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 15

Page 40: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Proof System

Γ ::= · | Γ, p

Γ ` p

Γ ` p1 Γ ` p2Γ ` p1 ∧ p2

Γ ` p1 ∧ p2Γ ` p1

Γ ` p1 ∧ p2Γ ` p2

Γ ` p1Γ ` p1 ∨ p2

Γ ` p2Γ ` p1 ∨ p2

Γ ` p1 ∨ p2 Γ, p1 ` p3 Γ, p2 ` p3Γ ` p3

p ∈ Γ

Γ ` pΓ, p1 ` p2

Γ ` p1 ⊃ p2Γ ` p1 ⊃ p2 Γ ` p1

Γ ` p2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 15

Page 41: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Proof System

Γ ::= · | Γ, p

Γ ` p

Γ ` p1 Γ ` p2Γ ` p1 ∧ p2

Γ ` p1 ∧ p2Γ ` p1

Γ ` p1 ∧ p2Γ ` p2

Γ ` p1Γ ` p1 ∨ p2

Γ ` p2Γ ` p1 ∨ p2

Γ ` p1 ∨ p2 Γ, p1 ` p3 Γ, p2 ` p3Γ ` p3

p ∈ Γ

Γ ` pΓ, p1 ` p2

Γ ` p1 ⊃ p2Γ ` p1 ⊃ p2 Γ ` p1

Γ ` p2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 15

Page 42: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Proof System

Γ ::= · | Γ, p

Γ ` p

Γ ` p1 Γ ` p2Γ ` p1 ∧ p2

Γ ` p1 ∧ p2Γ ` p1

Γ ` p1 ∧ p2Γ ` p2

Γ ` p1Γ ` p1 ∨ p2

Γ ` p2Γ ` p1 ∨ p2

Γ ` p1 ∨ p2 Γ, p1 ` p3 Γ, p2 ` p3Γ ` p3

p ∈ Γ

Γ ` pΓ, p1 ` p2

Γ ` p1 ⊃ p2Γ ` p1 ⊃ p2 Γ ` p1

Γ ` p2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 15

Page 43: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Proof System

Γ ::= · | Γ, p

Γ ` p

Γ ` p1 Γ ` p2Γ ` p1 ∧ p2

Γ ` p1 ∧ p2Γ ` p1

Γ ` p1 ∧ p2Γ ` p2

Γ ` p1Γ ` p1 ∨ p2

Γ ` p2Γ ` p1 ∨ p2

Γ ` p1 ∨ p2 Γ, p1 ` p3 Γ, p2 ` p3Γ ` p3

p ∈ Γ

Γ ` p

Γ, p1 ` p2Γ ` p1 ⊃ p2

Γ ` p1 ⊃ p2 Γ ` p1Γ ` p2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 15

Page 44: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Proof System

Γ ::= · | Γ, p

Γ ` p

Γ ` p1 Γ ` p2Γ ` p1 ∧ p2

Γ ` p1 ∧ p2Γ ` p1

Γ ` p1 ∧ p2Γ ` p2

Γ ` p1Γ ` p1 ∨ p2

Γ ` p2Γ ` p1 ∨ p2

Γ ` p1 ∨ p2 Γ, p1 ` p3 Γ, p2 ` p3Γ ` p3

p ∈ Γ

Γ ` pΓ, p1 ` p2

Γ ` p1 ⊃ p2

Γ ` p1 ⊃ p2 Γ ` p1Γ ` p2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 15

Page 45: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Proof System

Γ ::= · | Γ, p

Γ ` p

Γ ` p1 Γ ` p2Γ ` p1 ∧ p2

Γ ` p1 ∧ p2Γ ` p1

Γ ` p1 ∧ p2Γ ` p2

Γ ` p1Γ ` p1 ∨ p2

Γ ` p2Γ ` p1 ∨ p2

Γ ` p1 ∨ p2 Γ, p1 ` p3 Γ, p2 ` p3Γ ` p3

p ∈ Γ

Γ ` pΓ, p1 ` p2

Γ ` p1 ⊃ p2Γ ` p1 ⊃ p2 Γ ` p1

Γ ` p2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 15

Page 46: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Wait a second...

Zach Tatlock CSE 505 Winter 2015, Lecture 15 16

Page 47: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Wait a second...

Zach Tatlock CSE 505 Winter 2015, Lecture 15 16

Page 48: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Wait a second... ZOMG!

That’s exactly our type system! Just erase terms, change each τ to a p,and translate→ to ⊃, ∗ to ∧, + to ∨.

Γ ` e : τ

Γ ` e1 : τ1 Γ ` e2 : τ2

Γ ` (e1, e2) : τ1 ∗ τ2Γ ` e : τ1 ∗ τ2Γ ` e.1 : τ1

Γ ` e : τ1 ∗ τ2Γ ` e.2 : τ2

Γ ` e : τ1

Γ ` A(e) : τ1 + τ2

Γ ` e : τ2

Γ ` B(e) : τ1 + τ2

Γ ` e : τ1 + τ2 Γ, x:τ1 ` e1 : τ Γ, y:τ2 ` e2 : τ

Γ ` match e with Ax. e1 | By. e2 : τ

Γ(x) = τ

Γ ` x : τ

Γ, x : τ1 ` e : τ2

Γ ` λx. e : τ1 → τ2

Γ ` e1 : τ2 → τ1 Γ ` e2 : τ2

Γ ` e1 e2 : τ1

Zach Tatlock CSE 505 Winter 2015, Lecture 15 17

Page 49: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

What does it all mean? The Curry-Howard Isomorphism.

I Given a well-typed closed term, take the typing derivation,erase the terms, and have a propositional-logic proof

I Given a propositional-logic proof, there exists a closed termwith that type

I A term that type-checks is a proof — it tells you exactly howto derive the logic formula corresponding to its type

I Constructive (hold that thought) propositional logic andsimply-typed lambda-calculus with pairs and sums are thesame thing.

I Computation and logic are deeply connectedI λ is no more or less made up than implication

I Revisit our examples under the logical interpretation...

Zach Tatlock CSE 505 Winter 2015, Lecture 15 18

Page 50: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

What does it all mean? The Curry-Howard Isomorphism.

I Given a well-typed closed term, take the typing derivation,erase the terms, and have a propositional-logic proof

I Given a propositional-logic proof, there exists a closed termwith that type

I A term that type-checks is a proof — it tells you exactly howto derive the logic formula corresponding to its type

I Constructive (hold that thought) propositional logic andsimply-typed lambda-calculus with pairs and sums are thesame thing.

I Computation and logic are deeply connectedI λ is no more or less made up than implication

I Revisit our examples under the logical interpretation...

Zach Tatlock CSE 505 Winter 2015, Lecture 15 18

Page 51: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b. x

is a proof that

b→ b

Zach Tatlock CSE 505 Winter 2015, Lecture 15 19

Page 52: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b1. λf :b1 → b2. f x

is a proof that

b1 → (b1 → b2)→ b2

Zach Tatlock CSE 505 Winter 2015, Lecture 15 20

Page 53: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b1 → b2 → b3. λy:b2. λz:b1. x z y

is a proof that

(b1 → b2 → b3)→ b2 → b1 → b3

Zach Tatlock CSE 505 Winter 2015, Lecture 15 21

Page 54: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b1. (A(x),A(x))

is a proof that

b1 → ((b1 + b7) ∗ (b1 + b4))

Zach Tatlock CSE 505 Winter 2015, Lecture 15 22

Page 55: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λf :b1 → b3. λg:b2 → b3. λz:b1 + b2.(match z with Ax. f x | Bx. g x)

is a proof that

(b1 → b3)→ (b2 → b3)→ (b1 + b2)→ b3

Zach Tatlock CSE 505 Winter 2015, Lecture 15 23

Page 56: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

λx:b1 ∗ b2. λy:b3. ((y, x.1), x.2)

is a proof that

(b1 ∗ b2)→ b3 → ((b3 ∗ b1) ∗ b2)

Zach Tatlock CSE 505 Winter 2015, Lecture 15 24

Page 57: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

So what?

Because:

I This is just fascinating (glad I’m not a dog)

I Don’t think of logic and computing as distinct fields

I Thinking “the other way” can help you know what’spossible/impossible

I Can form the basis for theorem provers

I Type systems should not be ad hoc piles of rules!

So, every typed λ-calculus is a proof system for some logic...

Is STLC with pairs and sums a complete proof system forpropositional logic? Almost...

Zach Tatlock CSE 505 Winter 2015, Lecture 15 25

Page 58: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

So what?

Because:

I This is just fascinating (glad I’m not a dog)

I Don’t think of logic and computing as distinct fields

I Thinking “the other way” can help you know what’spossible/impossible

I Can form the basis for theorem provers

I Type systems should not be ad hoc piles of rules!

So, every typed λ-calculus is a proof system for some logic...

Is STLC with pairs and sums a complete proof system forpropositional logic? Almost...

Zach Tatlock CSE 505 Winter 2015, Lecture 15 25

Page 59: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive

Classical propositional logic has the “law of the excluded middle”:

Γ ` p1 + (p1 → p2)

(Think “p+¬p” – also equivalent to double-negation ¬¬p→ p)

STLC does not support this law; for example, no closed expressionhas type b1 + (b1 → b2)

Logics without this rule are called constructive. They’re usefulbecause proofs “know how the world is” and “are executable” and“produce examples”

Can still “branch on possibilities” by making the excluded middlean explicit assumption:

((p1 + (p1 → p2)) ∗ (p1 → p3) ∗ ((p1 → p2)→ p3))→ p3

Zach Tatlock CSE 505 Winter 2015, Lecture 15 26

Page 60: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive

Classical propositional logic has the “law of the excluded middle”:

Γ ` p1 + (p1 → p2)

(Think “p+¬p” – also equivalent to double-negation ¬¬p→ p)

STLC does not support this law; for example, no closed expressionhas type b1 + (b1 → b2)

Logics without this rule are called constructive. They’re usefulbecause proofs “know how the world is” and “are executable” and“produce examples”

Can still “branch on possibilities” by making the excluded middlean explicit assumption:

((p1 + (p1 → p2)) ∗ (p1 → p3) ∗ ((p1 → p2)→ p3))→ p3

Zach Tatlock CSE 505 Winter 2015, Lecture 15 26

Page 61: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive

Classical propositional logic has the “law of the excluded middle”:

Γ ` p1 + (p1 → p2)

(Think “p+¬p” – also equivalent to double-negation ¬¬p→ p)

STLC does not support this law; for example, no closed expressionhas type b1 + (b1 → b2)

Logics without this rule are called constructive. They’re usefulbecause proofs “know how the world is” and “are executable” and“produce examples”

Can still “branch on possibilities” by making the excluded middlean explicit assumption:

((p1 + (p1 → p2)) ∗ (p1 → p3) ∗ ((p1 → p2)→ p3))→ p3

Zach Tatlock CSE 505 Winter 2015, Lecture 15 26

Page 62: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive

Classical propositional logic has the “law of the excluded middle”:

Γ ` p1 + (p1 → p2)

(Think “p+¬p” – also equivalent to double-negation ¬¬p→ p)

STLC does not support this law; for example, no closed expressionhas type b1 + (b1 → b2)

Logics without this rule are called constructive. They’re usefulbecause proofs “know how the world is” and “are executable” and“produce examples”

Can still “branch on possibilities” by making the excluded middlean explicit assumption:

((p1 + (p1 → p2)) ∗ (p1 → p3) ∗ ((p1 → p2)→ p3))→ p3

Zach Tatlock CSE 505 Winter 2015, Lecture 15 26

Page 63: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive, an Example

Theorem: There exist irrational numbers a and b such that ab is rational.

Classical Proof:

Let x =√

2. Either xx is rational or it is irrational.

If xx is rational, let a = b =√

2, done.

If xx is irrational, let a = xx and b = x. Since(√2

√2)√2

=√

2(√

2·√

2)=√

22

= 2, done.

Well, I guess we know there are some a and b satisfying the theorem...but which ones? LAME.

Constructive Proof:

Let a =√

2, b = log2 9.

Since√

2log2 9

= 9log2

√2 = 9log2(2

0.5) = 90.5 = 3, done.

To prove that something exists, we actually had to produce it. SWEET.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 27

Page 64: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive, an Example

Theorem: There exist irrational numbers a and b such that ab is rational.

Classical Proof:

Let x =√

2. Either xx is rational or it is irrational.

If xx is rational, let a = b =√

2, done.

If xx is irrational, let a = xx and b = x. Since(√2

√2)√2

=√

2(√

2·√

2)=√

22

= 2, done.

Well, I guess we know there are some a and b satisfying the theorem...but which ones? LAME.

Constructive Proof:

Let a =√

2, b = log2 9.

Since√

2log2 9

= 9log2

√2 = 9log2(2

0.5) = 90.5 = 3, done.

To prove that something exists, we actually had to produce it. SWEET.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 27

Page 65: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive, an Example

Theorem: There exist irrational numbers a and b such that ab is rational.

Classical Proof:

Let x =√

2. Either xx is rational or it is irrational.

If xx is rational, let a = b =√

2, done.

If xx is irrational, let a = xx and b = x. Since(√2

√2)√2

=√

2(√

2·√

2)=√

22

= 2, done.

Well, I guess we know there are some a and b satisfying the theorem...but which ones?

LAME.

Constructive Proof:

Let a =√

2, b = log2 9.

Since√

2log2 9

= 9log2

√2 = 9log2(2

0.5) = 90.5 = 3, done.

To prove that something exists, we actually had to produce it. SWEET.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 27

Page 66: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive, an Example

Theorem: There exist irrational numbers a and b such that ab is rational.

Classical Proof:

Let x =√

2. Either xx is rational or it is irrational.

If xx is rational, let a = b =√

2, done.

If xx is irrational, let a = xx and b = x. Since(√2

√2)√2

=√

2(√

2·√

2)=√

22

= 2, done.

Well, I guess we know there are some a and b satisfying the theorem...but which ones? LAME.

Constructive Proof:

Let a =√

2, b = log2 9.

Since√

2log2 9

= 9log2

√2 = 9log2(2

0.5) = 90.5 = 3, done.

To prove that something exists, we actually had to produce it. SWEET.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 27

Page 67: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive, an Example

Theorem: There exist irrational numbers a and b such that ab is rational.

Classical Proof:

Let x =√

2. Either xx is rational or it is irrational.

If xx is rational, let a = b =√

2, done.

If xx is irrational, let a = xx and b = x. Since(√2

√2)√2

=√

2(√

2·√

2)=√

22

= 2, done.

Well, I guess we know there are some a and b satisfying the theorem...but which ones? LAME.

Constructive Proof:

Let a =√

2, b = log2 9.

Since√

2log2 9

= 9log2

√2 = 9log2(2

0.5) = 90.5 = 3, done.

To prove that something exists, we actually had to produce it. SWEET.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 27

Page 68: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive, an Example

Theorem: There exist irrational numbers a and b such that ab is rational.

Classical Proof:

Let x =√

2. Either xx is rational or it is irrational.

If xx is rational, let a = b =√

2, done.

If xx is irrational, let a = xx and b = x. Since(√2

√2)√2

=√

2(√

2·√

2)=√

22

= 2, done.

Well, I guess we know there are some a and b satisfying the theorem...but which ones? LAME.

Constructive Proof:

Let a =√

2, b = log2 9.

Since√

2log2 9

= 9log2

√2 = 9log2(2

0.5) = 90.5 = 3, done.

To prove that something exists, we actually had to produce it. SWEET.Zach Tatlock CSE 505 Winter 2015, Lecture 15 27

Page 69: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive, a Perspective

Constructive logic allows us to distinguish between things thatclassical logic lumps together.

Consider “P is true.” vs. “It would be absurd if P were false.”I P vs. ¬¬P

Those are different things, but classical logic can’t tell.

Our friends Godel and Gentzen gave us thisnice result:

P is provable in classical logic iff ¬¬P isprovable in constructive logic.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 28

Page 70: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive, a Perspective

Constructive logic allows us to distinguish between things thatclassical logic lumps together.

Consider “P is true.” vs. “It would be absurd if P were false.”I P vs. ¬¬P

Those are different things, but classical logic can’t tell.

Our friends Godel and Gentzen gave us thisnice result:

P is provable in classical logic iff ¬¬P isprovable in constructive logic.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 28

Page 71: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive, a Perspective

Constructive logic allows us to distinguish between things thatclassical logic lumps together.

Consider “P is true.” vs. “It would be absurd if P were false.”I P vs. ¬¬P

Those are different things, but classical logic can’t tell.

Our friends Godel and Gentzen gave us thisnice result:

P is provable in classical logic iff ¬¬P isprovable in constructive logic.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 28

Page 72: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Classical vs. Constructive, a Perspective

Constructive logic allows us to distinguish between things thatclassical logic lumps together.

Consider “P is true.” vs. “It would be absurd if P were false.”I P vs. ¬¬P

Those are different things, but classical logic can’t tell.

Our friends Godel and Gentzen gave us thisnice result:

P is provable in classical logic iff ¬¬P isprovable in constructive logic.

Zach Tatlock CSE 505 Winter 2015, Lecture 15 28

Page 73: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Fix

A “non-terminating proof” is no proof at all.

Remember the typing rule for fix:

Γ ` e : τ → τ

Γ ` fix e : τ

That let’s us prove anything! Example: fix λx:b. x has type b

So the “logic” is inconsistent (and therefore worthless)

Related: In ML, a value of type ’a never terminates normally(raises an exception, infinite loop, etc.)

let rec f x = f x

let z = f 0

Zach Tatlock CSE 505 Winter 2015, Lecture 15 29

Page 74: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Last word on Curry-Howard

It’s not just STLC and constructive propositional logic

Every logic has a correspondng typed λ calculus (and no consistentlogic has something as “powerful” as fix).

I Example: When we add universal types (“generics”) in a laterlecture, that corresponds to adding universal quantification

If you remember one thing: the typing rule for function applicationis modus ponens

Zach Tatlock CSE 505 Winter 2015, Lecture 15 30

Page 75: Lecture 15 The Curry-Howard IsomorphismLecture 15 | The Curry-Howard Isomorphism Zach Tatlock Winter 2015. We are Language Designers! What have we done? I De ne a programming language

Last word on Curry-Howard

It’s not just STLC and constructive propositional logic

Every logic has a correspondng typed λ calculus (and no consistentlogic has something as “powerful” as fix).

I Example: When we add universal types (“generics”) in a laterlecture, that corresponds to adding universal quantification

If you remember one thing: the typing rule for function applicationis modus ponens

Zach Tatlock CSE 505 Winter 2015, Lecture 15 30