Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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 fields. 2

Slide 4

Slide 4 text

some type theory

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

some logic

Slide 12

Slide 12 text

what’s true and what’s false? If it is dark outside then if pigs fly then it is dark outside. A = it is dark outside A ⊃ (B ⊃ A) B = pigs fly 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

Slide 13

Slide 13 text

what’s true and what’s false? If it is dark outside then if pigs fly then it is dark outside. A = it is dark outside A ⊃ (B ⊃ A) B = pigs fly 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

Slide 14

Slide 14 text

truth-value semantics We give values to atoms in the set {0, 1}, i.e. define 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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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 (reductio ad absurdum) (¬A is an abbreviation for A ⊃⊥) 20

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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 quantifiers call/cc operator Peirce’s law monads a modal logic 23

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

some category theory

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

a category credits: Bartosz Milewski 27

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

Thanks for your attention!

Slide 49

Slide 49 text

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 https://homepages.inf.ed.ac.uk/wadler/papers/propositions-as-types/ propositions-as-types.pdf ∙ Dan Grossman, Lecture notes on The Curry-Howard Isomorphism https://courses.cs.washington.edu/courses/cse505/12au/lec12_6up.pdf 47