Introduction to Type Theory February 2008 Alpha Lernet Summer School Piriapolis, Uruguay Herman Geuvers Nijmegen & Eindhoven, NL Lecture 1: Introduction, Overview, Simple Type Theory 1
Types and sets Types are not sets. Types are a bit like sets, but:... types give syntactic information 3 + (7 8) 5 : nat sets give semantic information 3 {n IN x, y, z IN + (x n + y n z n )} 2
Sets are about semantics 3 {n IN x, y, z IN + (x n + y n z n )} because there are no positive x, y, z such that x n + y n = z n [Wiles; Fermat s last Theorem ] set theory talks about what things exist (semantics, ontology). A set X such that for all sets Y with Y < X, 2 Y < X? sets are extensional: {n IN x, y, z IN + (x n + y n = z n )} = {0, 1, 2} sets are collections of things. membership is undecidable 3
Types are about syntax 3 + (7 8) 5 : nat because 3, 7, 8 are of type nat and the operations take objects of type nat to nat. 1 2 Σ n=02 n : IN is not a typing judgment. type theory talks about how things can be constructed (syntax, formal language, expressions) types are intensional {n x, y, z nat + (x n +y n z n )} {n n = 0 n = 1 n = 2} typing (and type checking) is decidable 4
Overview What I will be doing in these lectures. Problem: there are so many type systems and so many ways of defining them... Central theme: two readings of typing judments M : A M is a term (program, expression) of the data type A M is a proof (derivation) of the formula A Curry-Howard isomorphism of formulas-as-types (and proofs-as-terms) 5
Overview of these lectures 1. Simply typed λ-calculus (Simple Type Theory) and Curry Howard isomorphism 2. Simple Type Theory: Curry type assignment, principle type algorithm and normalization 3. Polymorphic type theory: full polymorphism and ML style polymorphism 4. Dependent type theory: logical framework and type checking algorithm 5. Higher order logic, inductive types and proof assistants based on type theory. 6
Simplest system: λ or STT Just arrow types Typ := TVar (Typ Typ) Examples: (α β) α, (α β) ((β γ) (α γ)) Brackets associate to the right and outside brackets are omitted: (α β) (β γ) α γ denotes (α β) ((β γ) (α γ)) Types are denoted by σ, τ,.... Terms: typed variables x σ 1, x σ 2,..., countably many for every σ. application: if M : σ τ and N : σ, then (MN) : τ abstraction: if P : τ, then (λx σ.p) : σ τ 7
Examples I σ := λx σ.x : σ σ K στ := λx σ.λy τ.x : σ τ σ λx α β.λy β γ.λz α.y(xz) : (α β) (β γ) α γ λx α.λy (β α) α.y(λz β.x) : α ((β α) α) α For λx α β.λy β γ.λz α.y(xz): If x : α β, y : β γ and z : α, then xz : β, so y(xz) : γ, so λz α.y(xz) : α γ, so λy β γ.λz α.y(xz) : (β γ) α γ, so λx α β.λy β γ.λz α.y(xz) : (α β) (β γ) α γ 8
Notation In λ-calculus (and type theory) we have some special notational conventions: λ-abstraction: - we write λx σ.λy τ.m or even λx σ y τ.m for λx σ.(λy σ.m) Function application: - we write (F M) (and not F(M))! - in multiple applications, F M N denotes (F M) N (and not F (M N)). Brackets associate to the left in applications. Types: - we write σ τ ρ for σ (τ ρ). Brackets associate to the right in types. 9
Notation Conventions about types and applications fit together nicely: If F : σ τ ρ, M : σ and P : τ, then F M : τ ρ and F M P : ρ NB Every type of STT can be written as with α a type variable σ 1 σ 2... α NB Functions of multiple arguments are dealt with by Currying: We don t have F : σ τ ρ but instead we use F : σ τ ρ 10
Computation A λ-term of the form (λx σ.m)p is a β-redex (reducible expression). A redex can be contracted: (λx σ.m)p β M[x := P] M[x := P] denotes M with P substituted for x Example (λx σ.λy τ.x)p β λy τ.p But what if P = y? then This is clearly not what we want. (λx σ.λy τ.x)y β λy τ.y!! The λ is a binder and we have to make sure that free variables don t get bound by a substitution. Solution: Remaning of bound variables before substitution 11
Free and bound variables and α-conversion FV(x) = {x} BV(x) = FV(MN) = FV(M) FV(N) BV(MN) = BV(M) BV(N) FV(λx σ.m) = FV(M) \ {x} BV(λx σ.m) = BV(M) {x} Definition M N or M = α N if M is equal to N modulo renaming of bound variables Examples: λx σ.λy τ.x λx σ.λz τ.x λx σ.λy τ.x λy σ.λx τ.y NB also: λx σ.λy τ.y λx σ.λx τ.x 12
We work modulo α-conversion We consider terms modulo α-equality, so we don t distinguish between λx σ.λy τ.x and λx σ.λz τ.x. In examples we always rename bound variables such that no clashes can arise. This is known as the Barendregt convention Before reduction or substitution, we rename (if necessary): (λx σ.λy τ.x)y (λx σ.λz τ.x)y β λz τ.y Notation: β is the transitive reflexive closure of β, = β is the transitive reflexive symmetric closure of β. 13
Examples of computation with simple typed terms The type (σ σ) σ σ is called the type of numerals over σ: n := λf σ σ.λx σ.f n (x) where So 2 := λf σ σ.λx σ.f(f x) f n (x) denotes f(...f(f }{{} n times f x)) λz σ. 2I σ z λz σ.(λf σ σ.λx σ.f(f x))i σ z β λz σ.(λx σ.i σ (I σ x))z β λz σ.i σ (I σ z) β β λz σ.i σ z λz σ.z I σ 14
Examples of computation with simple typed terms S := λx σ σ σ.λy σ σ.λz σ.xz(y z) : (σ σ σ) (σ σ) σ σ Then SK σσ I σ : σ σ and SK σσ I σ β (λy σ σ.λz σ.k σσ z(y z))i σ There are several ways of reducing this term further: (λy σ σ.λz σ.k σσ z(y z))i σ K σσ z is a redex is a redex 15
Example ctd (λy σ σ.λz σ.k σσ z(y z))i σ β λz σ.k σσ z(i σ z) λz σ.(λp σ, q σ.p) z(i σ z) β λz σ.(λq σ.z) (I σ z) Call by Value β λz σ.(λq σ.z)z β λz σ.z But also (λy σ σ.λz σ.k σσ z(y z))i σ (λy σ σ.λz σ.(λp σ, q σ.p) z(y z))i σ β (λy σ σ.λz σ.(λq σ.z)(y z)i σ Call by Name β (λy σ σ.λz σ.z)i σ β λz σ.z 16
Properties of reduction in STT Subject Reduction If M : σ and M β P, then P : σ. Church-Rosser If M is well-typed and M β P and M β N, then there is a Q such that P β Q and N β Q. Strong Normalisation If M is well-typed, then there is no infinite β-reduction path starting from M. 17
Different presentations of STT Inductive definition of the terms: typed variables x σ 1, x σ 2,..., countably many for every σ. application: if M : σ τ and N : σ, then (MN) : τ abstraction: if P : τ, then (λx σ.p) : σ τ Alternative: Inductive definition of the terms in rule form: x σ : σ M : σ τ N : σ MN : τ P : τ λx σ.p : σ τ Advantage: We also have a derivation tree, a proof of the fact that the term has that type. We can reason over derivations. 18
Simple type theory a la Church Formulation with contexts to declare the free variables: x 1 : σ 1, x 2 : σ 2,...,x n : σ n is a context, usually denoted by Γ. Derivation rules of λ (à la Church): x:σ Γ Γ x : σ Γ M : σ τ Γ N : σ Γ MN : τ Γ, x:σ P : τ Γ λx:σ.p : σ τ Γ λ M : σ if there is a derivation using these rules with conclusion Γ M : σ 19
Formulas-as-Types (Curry, Howard) There are two readings of a judgement M : σ 1. term as algorithm/program, type as specification: M is a function of type σ 2. type as a proposition, term as its proof: M is a proof of the proposition σ There is a one-to-one correspondence: typable terms in λ derivations in minimal proposition logic The judgement x 1 : τ 1, x 2 : τ 2,...,x n : τ n M : σ can be read as M is a proof of σ from the assumptions τ 1, τ 2,...,τ n. 20
Example [α β γ] 3 [α] 1 β γ γ 1 α γ (α β) α γ [α β] 2 [α] 1 β (α β γ) (α β) α γ 2 3 λx:α β γ.λy:α β.λz:α.xz(yz) : (α β γ) (α β) α γ 21
Example [x : α β γ] 3 [z : α] 1 xz : β γ xz(yz) : γ λz:α.xz(yz) : α γ [y : α β] 2 [z : α] 1 1 yz : β λy:α β.λz:α.xz(yz) : (α β) α γ λx:α β γ.λy:α β.λz:α.xz(yz) : (α β γ) (α β) α γ Exercise: Add types to the λ-abstractions) and give the derivation that corresponds to λx.λy.y(λz.y x) : (γ ε) ((γ ε) ε) ε 2 3 22
Computation Cut-elimination in minimal logic = β-reduction in λ. [σ] 1 D 1 τ 1 σ τ τ [x : σ] 1 D 1 D 2 σ M : τ D 2 1 λx:σ.m : σ τ P : σ (λx:σ.m)p : τ β D 2 σ D 1 τ D 2 P : σ D 1 M[x := P] : τ 23
Example: Proof of A A B, (A B) A B [A] 1 A A B [A] 1 A B B A B (A B) A [A] 1 A A B [A] 1 A B A B A B It contains a cut: a -i directly followed by an -e. B 24
Example: Proof of A A B, (A B) A B after reduction [A] 1 A A B [A] 1 A B [A] 1 A A B [A] 1 A B B B (A B) A A B (A B) A A B A A A B A A B B 25
Example: Proof of A A B, (A B) A B with term information [x : A] 1 p : A A B [x : A] 1 p x : A B p xx : B λx:a.p xx : A B q : (A B) A (λx:a.p xx)(q(λx:a.p xx)) : B Term contains a β-redex: (λx:a.p x x) (q(λx:a.p x x)) [x : A] 1 p : A A B [x : A] 1 p x : A B p xx : B λx:a.p xx : A B q(λx:a.p xx) : A 26
Example: Reduced proof of A A B, (A B) A B with term info [x : A] 1 p : A A B [x : A] 1 p : A A B [x : A] 1 p x : A B [x : A] 1 p x : A B p x x : B p x x : B q : (A B) A λx:a.p x x : A B q : (A B) A λx:a.p x x : A B q(λx:a.p x x) : A p : A A B q(λx:a.p x x) : A p(q(λx:a.p x x)) : A B p(q(λx:a.p x x))(q(λx:a.p x x)) : B 27