360

# The Curry-Howard-Lambek Correspondence January 23, 2019

## Transcript

1. the curry-howard-lambek correspondence
Denisa Diaconescu
Bucharest FP #38 · 23 January 2019

2. change perspectives
Roger Antonsen
University of Oslo
TED Talk: Math is the hidden secret to understanding the world
... understanding has to do with the ability to change your perspective
1

3. overview
We will look at some elements from three different perspectives:
∙ Type Theory
∙ Logic
∙ Category Theory
Disclamer: We will not go into depth in any of the above ﬁelds.
2

4. some type theory

5. a simple program in haskell
data Point = Point Int Int
makePoint :: Int → Int → Point
makePoint x y = Point x y
getX :: Point → Int
getX (Point x y) = x
getY :: Point → Int
getY (Point x y) = y
origin :: Point
origin = makePoint 0 0
4

6. a simple program in haskell
Let’s change perspective!
data Point = Point Int Int
makePoint :: Int → Int → Point
makePoint x y = Point x y
x : Int y : Int
makePoint x y : Point (PointI)
getX :: Point → Int
getX (Point x y) = x
p : Point
getX p : Int (PointE1
)
getY :: Point → Int
getY (Point x y) = y
p : Point
getY p : Int (PointE2
)
5

7. generalising
x : Int y : Int
makePoint x y : Point (PointI)
a : A b : B
⟨a, b⟩ : A × B (×I)
p : Point
getX p : Int (PointE1
)
p : A × B
fst p : A (×E1
)
p : Point
getY p : Int (PointE2
)
p : A × B
snd p : B (×E2
)
6

8. another simple example
> let f = (\x → x * 3) :: Int → Int
[x : Int]
.
.
.
x ∗ 3 : Int
λx.x ∗ 3 : Int → Int (funI)
> f 5
15
f : Int → Int 5 : Int
f 5 : Int (funE)
7

9. generalising
[x : Int]
.
.
.
x ∗ 3 : Int
λx.x ∗ 3 : Int → Int (funI)
[x : A]
.
.
.
b : B
λx.b : A → B (→I)
(abstraction)
f : Int → Int 5 : Int
f 5 : Int (funE)
f : A → B x : A
f x : B (→E)
(application)
8

10. summing up
A Typed λ-Calculus
a : A b : B
⟨a, b⟩ : A × B (×I)
p : A × B
fst p : A (×E1
)
p : A × B
snd p : B (×E2
)
[x : A]
.
.
.
b : B
λx.b : A → B (→I)
f : A → B x : A
f x : B (→E)
9

11. some logic

12. what’s true and what’s false?
If it is dark outside then if pigs ﬂy then it is dark outside.
A = it is dark outside
A ⊃ (B ⊃ A)
B = pigs ﬂy
Is this proposition true?
A B B ⊃ A A ⊃ (B ⊃ A)
false false true true
false true false true
true false true true
true true true true
This proposition is always true!
11

13. what’s true and what’s false?
If it is dark outside then if pigs ﬂy then it is dark outside.
A = it is dark outside
A ⊃ (B ⊃ A)
B = pigs ﬂy
Is this proposition true?
A B B ⊃ A A ⊃ (B ⊃ A)
0 0 1 1
0 1 0 1
1 0 1 1
1 1 1 1
This proposition is always true!
12

14. truth-value semantics
We give values to atoms in the set {0, 1}, i.e.
deﬁne an evaluation e : Atoms → {0, 1}.
Given an evaluation, we extend it to propositions using the truth tables:
& : {0, 1} × {0, 1} → {0, 1} ⊃: {0, 1} × {0, 1} → {0, 1}
A B A&B
0 0 0
0 1 0
1 0 0
1 1 1
A B A ⊃ B
0 0 1
0 1 1
1 0 0
1 1 1
If for all evaluations, a proposition takes the value 1 then we say it is
always true (a tautology).
13

15. the faces of a logic
Syntax of a logic
∙ Deals with the notion of provability
∙ Gives a method to manipulate the symbols from the logic (i.e, atoms,
⊃, &) in order to establish when a proposition is provable (a theorem)
Completeness = syntax and semantics coincide
Soundness = syntax implies semantics
14

16. a natural deduction system
∙ Rules for handling each logical connectives
∙ Rules for introducing and eliminating connectives
∙ Rules are of the form
Assumptions
Conclusion
A B
A&B (&I)
A&B
A (&E1
)
A&B
B (&E2
)
[A]
.
.
.
B
A ⊃ B (⊃I)
A ⊃ B A
B (⊃E)
Does it look familiar?
15

17. what we have seen so far
A Typed λ-Calculus A Natural Deduction System
a : A b : B
⟨a, b⟩ : A × B (×I)
A B
A&B (&I)
p : A × B
fst p : A (×E1
)
A&B
A (&E1
)
p : A × B
snd p : B (×E2
)
A&B
B (&E2
)
[x : A]
.
.
.
b : B
λx.n : A → B (→I)
[A]
.
.
.
B
A ⊃ B (⊃I)
f : A → B x : A
f x : B (→E)
A ⊃ B A
B (⊃E)
Propositions are types! ♡ 16

18. let’s dig further
A Typed λ-Calculus A Natural Deduction System
a : A b : B
⟨a, b⟩ : A × B (×I)
A B
A&B (&I)
Inhabitation of type A means that A is provable! ♡
17

19. let’s dig further
A Typed λ-Calculus A Natural Deduction System
λx.x : A → A (id)
[A]
A
A ⊃ A
λx.(λy.x) : A → (B → A) (const)
[A]
[B]
A
B ⊃ A
A ⊃ (B ⊃ A)
Proofs are Terms! ♡
18

20. the curry-howard correspondence
Type Theory Logic
types propositions
terms proofs
inhabitation of type A proof of proposition A
product type conjunction
function type implication
sum type disjunction
void type (empty type) falsity
unit type (singleton type) truth
19

21. natural deduction for classical (propositional) logic
[A]
.
.
.
B
A ⊃ B (⊃I)
A ⊃ B A
B (⊃E)
A B
A&B (&I)
A&B
A (&E1
)
A&B
B (&E2
)
A
A ∨ B (∨I1
)
B
A ∨ B (∨I2
)
A ∨ B A ⊃ C B ⊃ C
C (∨E)

A (ex falso quodlibet) ¬¬A
(¬A is an abbreviation for A ⊃⊥)
20

22. natural deduction for intuitionistic (propositional) logic
[A]
.
.
.
B
A ⊃ B (⊃I)
A ⊃ B A
B (⊃E)
A B
A&B (&I)
A&B
A (&E1
)
A&B
B (&E2
)
A
A ∨ B (∨I1
)
B
A ∨ B (∨I2
)
A ∨ B A ⊃ C B ⊃ C
C (∨E)

A (ex falso quodlibet)
(¬A is an abbreviation for A ⊃⊥)
21

23. intuitionistic logic
∙ Constructive logic
∙ Based on the notion of proof
∙ Useful because proofs are executable and produce examples
∙ The following equivalent propositions are unprovable
in intuitionistic logic:
∙ double negation law: ¬¬A ⊃ A
∙ excluded middle law: A ∨ ¬A
∙ Pierce’s law: ((A ⊃ B) ⊃ A) ⊃ A
∙ There is no truth-values semantics for intuitionistic logic!
It has alternative semantics (e.g., Kripke semantics)
The original Curry-Howard correspondence is between
Church’s simply typed
and
Gentzen’s natural deduction
λ-calculus for intuitionistic logic.
22

24. go beyond original case
Type Theory Logic
types propositions
terms proofs
inhabitation of type A proof of proposition A
product type conjunction
function type implication
sum type disjunction
void type (empty type) falsity
unit type (singleton type) truth
dependent types quantiﬁers
call/cc operator Peirce’s law
23

25. why care?
∙ This is just fascinating
∙ Don’t think of logic and computing as distinct ﬁelds
∙ Thinking from a different perspective can help you know what is
possible/impossible
∙ Type systems should not be ad hoc piles of rules!
24

26. some category theory

27. a category
∙ A category is an embarrassingly simple concept.
– Bartosz Milewski, Category Theory for Programmers
∙ Category = objects + arrows
∙ Key ingredient: composition of arrows
26

28. a category
credits: Bartosz Milewski
27

29. a category
A category C consists of
∙ Objects: denoted A, B, C, . . .
∙ Arrows: for any objects A and B, there is a set of arrows C(A, B)
∙ we denote f ∈ C(A, B) with f : A → B or A f
−→ B
∙ Composition: for any arrows f : A → B and g : B → C there is an arrow
g ◦ f : A → C
A B
C
f
g◦f
g
∙ Identity: for any object A there is an arrow idA : A → A
∙ Axioms: for any arrows f : A → B, g : B → C, and h : C → D
h ◦ (g ◦ f) = (h ◦ g) ◦ f f ◦ idA = f = idB ◦ f
28

30. example - the category of sets
The category Set has
∙ Objects: sets
∙ Arrows: functions
∙ Composition: composition of functions
∙ Identity: for each set A, the identity function idA : A → A, idA(a) = a
∙ Axioms: ✓
29

31. monoids
A monoid M is a structure ⟨M, +, e⟩ such that
∙ M is a set
∙ + : M × M → M is associative
(aka (a + b) + c = a + (b + c) for any a, b, c ∈ M)
∙ e ∈ M is an identity for +
(aka e + a = a + e = a for any a ∈ M)
Monoids are an amazingly powerful concept:
∙ They are behind basic arithmetics
∙ both addition and multiplication form a monoid
∙ They are ubiquitous in programming
∙ strings, lists, foldable data structures, . . .
30

32. example - the category of monoids
The category Mon has
∙ Objects: monoids
∙ Arrows: monoid morphisms
(aka functions not messing with the monoid operation)
∙ Composition: composition of monoid morphisms
∙ Identity: for each object M, idM : M → M, idM(m) = m
∙ Axioms: ✓
31

33. example - a monoid is a category
Any monoid M = ⟨M, +, e⟩ is a category with
∙ Objects: just one object □
∙ Arrows: the elements of the set M (i.e, M(□, □) = M)
∙ Composition: the monoid operation +
∙ Identity: the monoid identity e
∙ Axioms:
h ◦ (g ◦ f) = (h ◦ g) ◦ f f ◦ idA = f = idB ◦ f
a + (b + c) = (a + b) + c a + e = a = e + a
32

34. initial and terminal object
In a category C
∙ an object T is called terminal if for every object A there exists a unique
arrow
τA : A → T
∙ an object I is called initial if for every object A there exists a unique
arrow
ιA : I → A
33

35. why the terminal object?
We can generalise our rules. For example:
A B
A&B (&I) Γ ⊢ A Γ ⊢ B
Γ ⊢ A&B (&I)
Deduction from no assumptions Deduction from assumptions Γ
Γ = ∅ (Deduction from truth)
Let C be a category with a terminal object T. We have the following
interpretations:
∙ Propositions as objects of C
∙ Truth as the terminal object T
∙ A proof of A as an arrow f : T → A
∙ A proof of A from assumptions B as an arrow f : B → A
34

36. products
Let A and B be objects in a category C.
We say that
A π1
←− A × B π2
−→ B
is a product of A and B if for every
A f
←− C g
−→ B
there exists a unique arrow
⟨f, g⟩ : C −→ A × B
such that
π1 ◦ ⟨f, g⟩ = f π2 ◦ ⟨f, g⟩ = g
35

37. products
A A × B B
C
π1 π2
f g
⟨f,g⟩
36

38. let’s change the perspective
Let C be a category with a terminal object T and products.
Let A, B be two objects in C.
A A × B B
T
π1 π2
f g
⟨f,g⟩
f : T → A g : T → B
⟨f, g⟩ : T → A × B
⟨f, g⟩ : T → A × B
π1 ◦ ⟨f, g⟩ : T → A
⟨f, g⟩ : T → A × B
π2 ◦ ⟨f, g⟩ : T → B
Does it look familiar?
37

39. what we have seen so far
A Typed λ-Calculus A Natural Ded. Syst. A Category∗
a : A b : B
⟨a, b⟩ : A × B (×I)
A B
A&B (&I)
f : T → A g : T → B
⟨f, g⟩ : T → A × B
p : A × B
fst p : A (×E1
)
A&B
A (&E1
)
⟨f, g⟩ : T → A × B
π1 ◦ ⟨f, g⟩ : T → A
p : A × B
snd p : B (×E2
)
A&B
B (&E2
)
⟨f, g⟩ : T → A × B
π2 ◦ ⟨f, g⟩ : T → B
* A category with a terminal object T and products
38

40. the curry-howard-lambek correspondence
Type Theory Logic Category Theory
types propositions objects
terms proofs arrows
inhabitation of type A proof of proposition A arrow f : T → A
function type implication ?
product type conjunction product
sum type disjunction coproduct
void type (empty type) falsity initial object
unit type (singleton type) truth terminal object T
39

41. towards an interpretation for implication
In Set, given two sets A and B, we can form the set of functions
A ⇒ B = Set(A, B) which is again a set!
How can we axiomatise this situation?
What can we do with it operationally?
Apply functions to their arguments! That is, there is an arrow
ApA,B : (A ⇒ B) × A → B ApA,B(f, a) = f(a)
40

42. towards an interpretation for implication
For any g : C × A → B, there is an unique arrow Λ(g) : C → (A ⇒ B) such
that
A ⇒ B (A ⇒ B) × A B
C C × A
ApA,B
Λ(g) Λ(g)×idA g
In Set, this is deﬁned by
Λ(g)(c) = f where f : A → B with f(a) = g(c, a)
This process of transforming a function of two arguments into a function
with one argument is known as Currying.
41

43. exponentials in a category
Let C be a category with a terminal object T and products.
We say that C has exponentials if for any objects A and B,
there are an object A ⇒ B and an arrow
ApA,B : (A ⇒ B) × A → B
such that for any arrow g : C × A → B, there is a unique arrow
Λ(g) : C → (A ⇒ B) such that
ApA,B ◦ (Λ(g) × idA) = g
A ⇒ B (A ⇒ B) × A B
C C × A
ApA,B
Λ(g) Λ(g)×idA g
42

44. cartesian closed categories
A category with
∙ a terminal object
∙ products
∙ exponentials
is called a Cartesian Closed Category (CCC)
43

45. implication
Let C be a CCC with a terminal object T.
A Typed λ-Calculus A Natural Ded. Syst. A CCC
[x : A]
.
.
.
b : B
λx.n : A → B (→I)
[A]
.
.
.
B
A ⊃ B (⊃I)
g : T×A → B
Λ(g) : T → (A ⇒ B)
f : A → B x : A
f x : B (→E)
A ⊃ B A
B (⊃E)
f : T → (A ⇒ B) g : T → A
ApA,B ◦ ⟨f, g⟩ : T → B
A ⇒ B (A ⇒ B) × A B
T T × A
ApA,B
Λ(g) Λ(g)×idA g
B
A ⇒ B (A ⇒ B) × A A
T
π1 π2
ApA,B
f g
⟨f,g⟩
44

46. what we have seen so far
A Typed λ-Calculus A Natural Ded. Syst. A CCC
a : A b : B
⟨a, b⟩ : A × B (×I)
A B
A&B (&I)
f : T → A g : T → B
⟨f, g⟩ : T → A × B
p : A × B
fst p : A (×E1
)
A&B
A (&E1
)
⟨f, g⟩ : T → A × B
π1 ◦ ⟨f, g⟩ : T → A
p : A × B
snd p : B (×E2
)
A&B
B (&E2
)
⟨f, g⟩ : T → A × B
π2 ◦ ⟨f, g⟩ : T → B
[x : A]
.
.
.
b : B
λx.n : A → B (→I)
[A]
.
.
.
B
A ⊃ B (⊃I)
g : T×A → B
Λ(g) : T → (A ⇒ B)
f : A → B x : A
f x : B (→E)
A ⊃ B A
B (⊃E)
f : T → (A ⇒ B) g : T → A
ApA,B ◦ ⟨f, g⟩ : T → B
45

47. the curry-howard-lambek correspondence
Type Theory Logic Category Theory
types propositions objects
terms proofs arrows
inhabitation of type A proof of proposition A arrow f : T → A
function type implication exponential
product type conjunction product
sum type disjunction coproduct
void type (empty type) falsity initial object
unit type (singleton type) truth terminal object T
46

49. references
∙ Roger Antonsen, TED Talk: Math is the hidden secret to understanding
the world
https://www.ted.com/talks/roger_antonsen_math_is_the_hidden_secret_to_
understanding_the_world
∙ Samson Abramsky, Categories, Proofs and Processed Lecture III - The
Curry-Howard-Lambek Correspondence
http://www.math.helsinki.fi/logic/sellc-2010/course/LectureIII.pdf
∙ Philip Wadler, Propositions as Types