Michael Shulman
January 31, 2012
1 / 77
Type-theoretic foundations
Set theory Type theory
Logic Types ∧, ∨, ⇒, ¬, ∀, ∃ ×, +, →, Q, P
Sets Logic ×, +, →, Q, P ∧, ∨, ⇒, ¬, ∀, ∃
x ∈ A is a proposition x : A is a typing judgment
2 / 77 Type theory is programming
For now, think of type theory as a programming language. • Closely related to functional programming languages like ML, Haskell, Lisp, Scheme. • More expressive and powerful. • Can manipulate “mathematical objects”.
4 / 77
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
The turnstile ` binds most loosely, followed by commas. This should be read as:
In the context of variables x1 of type A1, x2 of type A2,..., and xn of type An, the expression b has type B.
Examples ` 0: N x : N, y : N ` x + y : N f : R → R, x : R ` f (x): R ∞ (n) ∞ f : C (R, R), n: N ` f : C (R, R)
5 / 77 Type constructors
The basic rules tell us how to construct valid typing judgments, i.e. how to write programs with given input and output types. This includes: 1 How to construct new types (judgments Γ ` A: Type). 2 How to construct terms of these types. 3 How to use such terms to construct terms of other types. Example (Function types)
1 If A: Type and B : Type, then A → B : Type. 2 If x : A ` b : B, then λxA.b : A → B. 3 If a: A and f : A → B, then f (a): B.
6 / 77
Derivations
We write these rules as follows. ` A: Type ` B : Type ` A → B : Type
x : A ` b : B ` λxA.b : A → B
` f : A → B ` a: A ` f (a): B
7 / 77 Derivations in Context
More generally, we allow an arbitrary context Γ = (x1 : A1,..., xn : An) of typed variables.
Γ ` A: Type Γ ` B : Type Γ ` A → B : Type
Γ, x : A ` b : B introduction Γ ` λxA.b : A → B
Γ ` f : A → B Γ ` a: A elimination Γ ` f (a): B
8 / 77
Type theory as programming
This is just a mathematical syntax for programming. int square(int x) { return (x * x); } def square(x): return (x * x) square :: Int -> Int square x = x * x fun square (n:int):int = n * n
(define (square n) (* n n))
square := λxZ.(x ∗ x)
9 / 77 Evaluation
The rules also tell us how to evaluate or compute terms. The general rule is: • introduction plus elimination computes to substitution.
Γ, x : A ` b : B Γ ` a: A A Γ ` (λx .b)(a) →β b[a/x] Here b[a/x] means b with a substituted for x.
For historical reasons, this is called β-reduction.
Z square(2) ≡ (λx .(x ∗ x))(2) →β (x ∗ x)[2/x] ≡ 2 ∗ 2
10 / 77
Functions of many variables
A function of two variables can be represented as a function of one variable which returns a function of another variable. foo := λxZ. λy Z.(2 ∗ x + y ∗ y)