Basics of Type Theory and Coq Logic Types ∧, ∨, ⇒, ¬, ∀, ∃ ×, +, →, Q, P Michael Shulman
Total Page:16
File Type:pdf, Size:1020Kb
Type-theoretic foundations Set theory Type theory Basics of type theory and Coq Logic Types ^; _; ); :; 8; 9 ×; +; !; Q; P Michael Shulman Sets Logic January 31, 2012 ×; +; !; Q; P ^; _; ); :; 8; 9 x 2 A is a proposition x : A is a typing judgment 1 / 77 2 / 77 Type theory is programming Typing judgments Type theory consists of rules for manipulating judgments. The most important judgment is a typing judgment: x1 : A1; x2 : A2;::: xn : An ` b : B For now, think of type theory as a programming language. The turnstile ` binds most loosely, followed by commas. • Closely related to functional programming languages like This should be read as: ML, Haskell, Lisp, Scheme. In the context of variables x of type A , x of type A ,..., • More expressive and powerful. 1 1 2 2 and xn of type An, the expression b has type B. • Can manipulate “mathematical objects”. Examples ` 0: N x : N; y : N ` x + y : N f : R ! R; x : R ` f (x): R 1 (n) 1 f : C (R; R); n: N ` f : C (R; R) 4 / 77 5 / 77 Type constructors Derivations The basic rules tell us how to construct valid typing judgments, We write these rules as follows. i.e. how to write programs with given input and output types. This includes: ` A: Type ` B : Type 1 How to construct new types (judgments Γ ` A: Type). ` A ! B : Type 2 How to construct terms of these types. 3 How to use such terms to construct terms of other types. x : A ` b : B A Example (Function types) ` λx :b : A ! B 1 If A: Type and B : Type, then A ! B : Type. ` f : A ! B ` a: A 2 If x : A ` b : B, then λxA:b : A ! B. ` f (a): B 3 If a: A and f : A ! B, then f (a): B. 6 / 77 7 / 77 Derivations in Context Type theory as programming This is just a mathematical syntax for programming. More generally, we allow an arbitrary context Γ = (x1 : A1;:::; xn : An) of typed variables. int square(int x) { return (x * x); } Γ ` A: Type Γ ` B : Type def square(x): Γ ` A ! B : Type return (x * x) square :: Int -> Int Γ; x : A ` b : B introduction square x = x * x Γ ` λxA:b : A ! B fun square (n:int):int = n * n Γ ` f : A ! B Γ ` a: A elimination (define (square n) ( n n)) Γ ` f (a): B * square := λxZ:(x ∗ x) 8 / 77 9 / 77 Evaluation Functions of many variables The rules also tell us how to evaluate or compute terms. The general rule is: A function of two variables can be represented as a function of • introduction plus elimination computes to substitution. one variable which returns a function of another variable. foo := λxZ: λy Z:(2 ∗ x + y ∗ y) Γ; x : A ` b : B Γ ` a: A Γ ` (λxA:b)(a) ! b[a=x] β foo(3)(1) !β λy Z:(2 ∗ x + y ∗ y) [3=x](1) Here b[a=x] means b with a substituted for x. ≡ λy Z:(2 ∗ 3 + y ∗ y) (1) !β (2 ∗ 3 + y ∗ y)[1=y] For historical reasons, this is called β-reduction. ≡ (2 ∗ 3 + 1 ∗ 1) Z This is called currying (after Haskell Curry). square(2) ≡ (λx :(x ∗ x))(2) !β (x ∗ x)[2=x] ≡ 2 ∗ 2 10 / 77 12 / 77 Functions of many variables Another example: disjoint unions A simplified notation for abstractions: foo := λxZ: λy Z:(2 ∗ x + y ∗ y) Γ ` A: Type Γ ` B : Type ≡ λxZy Z:(2 ∗ x + y ∗ y) Γ ` A + B : Type And for types, ! associates to the right: A ! B ! C means A ! (B ! C) Γ ` a: A Γ ` b : B Γ ` inl(a): A + B Γ ` inr(b): A + B And for application: Γ ` C : Type foo(3)(1) foo 3 1 Γ ` p : A + B Γ; x : A ` cA : C Γ; y : B ` cB : C That is, juxtaposition means application, which associates to A B Γ ` case(p; x :cA; y :cB): C the left: foo 3 1 means (foo 3) 1 13 / 77 15 / 77 Case switching Evaluating case switches Γ ` C : Type Γ ` p : A + B Γ; x : A ` cA : C Γ; y : B ` cB : C A B Γ ` C : Type Γ ` p : A + B Γ ` case(p; x :cA; y :cB): C Γ; x : A ` cA : C Γ; y : B ` cB : C Γ ` a: A switch(p) { A B Γ ` case(inl(a); x :c ; y :c ) !β c [a=x] if p is inl(x): A B A do cA with x if p is inr(y): Γ ` C : Type Γ ` p : A + B do cB with y Γ; x : A ` cA : C Γ; y : B ` cB : C Γ ` b : B A B } Γ ` case(inr(b); x :cA; y :cB) !β cB[b=y] Don’t worry about the exact syntax of “case”. Everyone does it differently, and we’ll mostly use Coq’s syntax (later). 16 / 77 17 / 77 The unit type Polarity Negative types Positive types Γ ` unit: Type Γ ` tt: unit A ! BA + B Q x : A B(x) A × B Γ ` p : unit Γ ` C : Type Γ ` c : C unit Γ ` triv(p; c): C empty P If we know how to produce a C using all the possible inputs that x : A B(x) can go into a unit, then we can produce a C from any unit. • A negative type is characterized by its eliminations. Γ ` C : Type Γ ` c : C 1 We use a term by applying it. 2 We construct a term by saying what it does when applied. Γ ` triv(tt; c) !β c • A positive type is characterized by its introductions. When we evaluate the eliminator on a term of canonical form, 1 We construct a term with a constructor. we obtain the data that went into the eliminator associated to 2 We use a term by saying what to do with each constructor. that form. 19 / 77 21 / 77 Polarity Types in Coq Negative types Positive types Coq uses a type theory called the predicative Calculus of (co)Inductive Constructions. There are only four ways to A ! BA + B construct types in Coq. Q x : A B(x) A × B unit 1 Dependent product (negative). • Includes A ! B as a special case; more later empty • Constructed with fun x => ... P x : A B(x) • Applied with juxtaposition f x 2 Inductive type families (positive). • Built with constructors like inl, inr, tt. NB: This is an oversimplification; some or all of these “positive • Eliminated with match. types” could also be presented negatively. But for us, they will • More details later. be positive. 3 Universes (sorts) like Type (unpolarized). 4 Coinductive type families (negative). 21 / 77 22 / 77 Exercise #1 Projections Exercise Define the cartesian product A × B as a positive type. Γ ` A: Type Γ ` B : Type For p : A × B: Γ ` A × B : Type fst(p) := unpack(p; xA y B:x): A Γ ` a: A Γ ` b : B Γ ` (a; b): A × B snd(p) := unpack(p; xA y B:y): B Γ ` C : Type Γ ` p : A × B Γ; x : A; y : B ` c : C Γ ` unpack(p; xA y B:c): C Γ ` C : Type Γ ` a: A Γ ` b : B Γ; x : A; y : B ` c : C Γ ` unpack((a; b); xA y B:c) ! c[a=x; b=y] β 23 / 77 24 / 77 Exercise #2 Structural rules Exercise Define the empty type ; as a positive type. We also need a few rules for “how to get going” with typing judgments. Γ ` A: Type Γ ` ;: Type start (x 2= Γ) Γ; x : A ` x : A (no introduction rule) Γ ` A: Type Γ ` b : B weakening (x 2= Γ) Γ; x : A ` b : B Γ ` p : ; Γ ` C : Type Γ ` a: A Γ ` A $β B Γ ` abort(p): C conversion Γ ` a: B (no computation rule) ($β is the equivalence relation generated by !β) 26 / 77 28 / 77 Now you know something! Logic in the style of type theory We can also read a typing judgment Definition The structural rules plus the type constructor ! (and nothing x1 : P1;:::; xn : Pn ` q : Q else) form the simply typed lambda calculus“ λ!”. as a truth judgment We can of course add other constructors. Sometimes people Under hypotheses P1, P2,..., Pn, write λ×→ for λ! with cartesian products and unit, etc. the conclusion Q is provable. 29 / 77 31 / 77 Logical connectives Implication The basic rules tell us how to construct valid truth judgments. To emphasize this viewpoint, we write Prop rather than Type. This includes: Γ ` P : Prop Γ ` Q : Prop 1 How to construct new propositions. Γ ` (P ) Q): Prop 2 How to prove such propositions. 3 How to use such propositions to prove other propositions. Γ; x : P ` q : Q Example (Implication) Γ ` λxP :q : P ) Q 1 If P and Q are propositions, then so is P ) Q. 2 If assuming P, we can prove Q, then we can prove P ) Q. Γ ` f : P ) Q Γ ` p : P 3 If we can prove P and P ) Q, then we can prove Q. Γ ` f (p): Q 32 / 77 33 / 77 Conjunction Disjunction _ (P ^ Q means “P and Q”) (P Q means “P or Q”) Γ ` : Γ ` : Γ ` P : Prop Γ ` Q : Prop P Prop Q Prop Γ ` ( _ ): Γ ` (P ^ Q): Prop P Q Prop Γ ` : Γ ` : Γ ` p : P Γ ` q : Q p P q Q Γ ` ( ): _ Γ ` ( ): _ Γ ` (p; q): P ^ Q inl p P Q inr q P Q Γ ` : Γ ` R : Prop Γ ` s : P ^ Q Γ; x : P; y : Q ` r : R R Prop Γ ` s : P _ Q Γ; x : P ` r : R Γ; y : Q ` r : R P Q P Q Γ ` unpack(s; x y :r): R P Q Γ ` case(s; x :rP ; y :rQ): R 34 / 77 35 / 77 Propositions as types Proof terms a.k.a.