<<

Type Lecture 3: Martin Löf Type Theory

Andreas Abel

Department of and Engineering Chalmers and Gothenburg University

ESSLLI 2016 28th European Summer School in , Language, and unibz, Bozen/Bolzano, Italy 15-19 August 2016

Andreas Abel (GU) Type Theory ESSLLI 2016 1 / 24 Contents

1 Martin-Löf Type Theory

2 Dependent type revisited

3 Dependent pairs

4 Booleans

5 Natural numbers

6 type

Andreas Abel (GU) Type Theory ESSLLI 2016 2 / 24 Martin-Löf Type Theory

Full Dependent Types

LF’s dependent types are refinements of simple types. Martin-Löf Type Theory has full-fledged dependent types. In particular, a type can be defined by case distinction and on a value. (_) R : N → type 0 R = > n+1 n R = R × R n There is no erasure of R to a simple type. Dependent types are sometimes used in linear :

n n inner :( n : N) → R × R → R n,m m,l n,l mmult :( n m l : N) → R × R → R

Andreas Abel (GU) Type Theory ESSLLI 2016 3 / 24 Martin-Löf Type Theory

Martin-Löf Type Theory

Martin-Löf Type Theory (MLTT) is understood as an open system. One can add new types given by formation introduction and elimination computation (β) (η) We will formulate it with two judgements:

1 Γ ` M : A “in Γ, expression M has type A” Established by formation (A = s), introduction, and elimination.

2 Γ ` M = M0 : A “inhabitants M and M0 of A are definitionally equal” Established by computation (β) and extensionality (η).

Andreas Abel (GU) Type Theory ESSLLI 2016 4 / 24 Martin-Löf Type Theory

Basic Rules

Hypotheses. (x:A) ∈ Γ hyp Γ ` x : A

Conversion. Γ ` M : A Γ ` A = B : s conv Γ ` M : B

Equivalence rules for judgemental equality. Γ ` M : A Γ ` M = M0 : A refl sym Γ ` M = M : A Γ ` M0 = M : A

Γ ` M1 = M2 : A Γ ` M2 = M3 : A trans Γ ` M1 = M3 : A

Andreas Abel (GU) Type Theory ESSLLI 2016 5 / 24 Dependent revisited

Dependent function type revisited

Formation. Γ ` A : type Γ, x:A ` B : type ΠF Γ ` (x : A) → B : type

Introduction. Γ, x:A ` M : B ΠI Γ ` λx.M :(x : A) → B

Elimination. Γ ` M :(x : A) → B Γ ` N : A ΠE Γ ` MN : B[N/x]

Computation. Γ, x:A ` M : B Γ ` N : A Πβ Γ ` (λx.M) N = M[N/x]: B[N/x]

Andreas Abel (GU) Type Theory ESSLLI 2016 6 / 24 Dependent function type revisited

Dependent function type revisited

Extensionality (note x 6∈ FV(M)). Γ ` M :(x : A) → B Πη Γ ` M = λx. M x :(x : A) → B

Compatibility rules Γ ` A = A0 : type Γ, x:A ` B = B0 : type ΠF= Γ ` (x : A) → B = (x : A0) → B0 : type

Γ, x:A ` M = M0 : B ΠI= Γ ` λx.M = λx.M0 :(x : A) → B

Γ ` M = M0 :(x : A) → B Γ ` N = N0 : A ΠE= Γ ` MN = M0 N0 : B[N/x]

Andreas Abel (GU) Type Theory ESSLLI 2016 7 / 24 Dependent pairs

Dependent pairs (strong Σ-type)

Formation. Γ ` A : type Γ, x:A ` B : type ΣF Γ ` (x : A) × B : type Introduction. Γ ` M : A Γ ` N : B[M/x] ΣI Γ ` hM, Ni :(x : A) × B Elimination. Γ ` M :(x : A) × B Γ ` M :(x : A) × B ΣE ΣE Γ ` fst M : A 1 Γ ` snd M : B[fst M/x] 2 Computation. Γ ` M : A Γ ` N : B Γ ` M : A Γ ` N : B Σβ Σβ Γ ` fst hM, Ni = M : A 1 Γ ` snd hM, Ni = N : B 2

Andreas Abel (GU) Type Theory ESSLLI 2016 8 / 24 Dependent pairs

Interpretations of the Σ type

Non-dependent: cartesion A × B.

B prop B type A prop conjunction A ∧ B mix A type mix pair

Dependent pair type (x : A) × B.

B prop B type A prop proof-relevant conjunction proof-rel. dep. pair A type existential quant. ∃x:A. B dependent pairΣx:A. B

Andreas Abel (GU) Type Theory ESSLLI 2016 9 / 24 Dependent pairs

Σ type: examples

n Lists from vectors List R = (n : N) × R Positive numbers Pos = (n : N) × n ≥ 1 Exercises: 1 Assuming divisibility Divides : N → N → type, define the type of prime numbers. 2 Define the type of lists of length < n. 3 Define the type of monotone functions on N.

Andreas Abel (GU) Type Theory ESSLLI 2016 10 / 24 Booleans

Booleans

Formation BoolF Γ ` Bool : type Introduction BoolI BoolI Γ ` true : Bool 1 Γ ` false : Bool 2 Elimination Γ, x:Bool ` C : s Γ ` M : Bool Γ ` N : C[true/x]Γ ` O : C[false/x] BoolE Γ ` ifx.C M then N else O : C[M/x] Computation Γ, x:Bool ` C : s Γ ` N : C[true/x]Γ ` O : C[false/x] Boolβ Γ ` ifx.C true then N else O = N : C[true/x] Γ ` ifx.C false then N else O = O : C[false/x]

Andreas Abel (GU) Type Theory ESSLLI 2016 11 / 24 Booleans

Booleans (ctd.)

Extensionality: Every boolean is either true or false [1, 2]. Open problem: adding Bool extensionality to MLTT. Type-checking will become complicated but powerful:

f : Bool → Bool, x : Bool ` f (f (f x)) = f x : Bool

would hold definitionally. Exercise: add the compatibility rules for if_then_else_! Programming with the booleans:

not : Bool → Bool not = λx. if_.Bool x then false else true

Exercise: Define other boolean functions, like exclusive-or xor!

Andreas Abel (GU) Type Theory ESSLLI 2016 12 / 24 Booleans

Defining the disjoint union

Disjoint union A + B is definable using if-then-else with types! _ + _ : type → type → type A + B =( x : Bool) × if_.type x then A else B Here we eliminate a value x : Bool to produce a type. This is called a large elimination (aka strong elimination). inl :( AB : type) → A + B → A inl = λA.λB.λa. htrue, ai inr :( AB : type) → A + B → B inr = λA.λB.λb. hfalse, bi

Exercise: Define case :(ABC : type) → A + B → (A → C) → (B → C) → C and check its computation laws!

Andreas Abel (GU) Type Theory ESSLLI 2016 13 / 24 Natural numbers

Natural numbers and induction

Formation. NF Γ ` N : type Introduction. Γ ` M : N NI1 NI2 Γ ` zero : N Γ ` suc M : N Elimination. Γ, x:N ` C : s Γ ` M0 : C[zero/x] Γ, y:N, ih : C[y/x] ` M1 : C[suc y/x] Γ ` N : N NE Γ ` recNx.C (M0, y.ih.M1, N): C[N/x]

Andreas Abel (GU) Type Theory ESSLLI 2016 14 / 24 Natural numbers

Natural numbers: computation

Γ, x:N ` C : s Γ ` M0 : C[zero/x] Γ, y:N, ih : C[y/x] ` M1 : C[suc y/x] Nβ1 Γ ` recNx.C (M0, y.ih.M1, zero) = M0 : C[zero/x]

Γ, x:N ` C : s Γ ` M0 : C[zero/x] Γ, y:N, ih : C[y/x] ` M1 : C[suc y/x] Γ ` N : N Nβ2 Γ ` recNx.C (M0, y.ih.M1, suc N) = M1[N/y, recNx.C (M0, y.ih.M1, N)/ih]: C[suc N/x]

Andreas Abel (GU) Type Theory ESSLLI 2016 15 / 24 Natural numbers

Programming with natural numbers

Elimination for N is higher-order primitive recursion. Predecessor and addition:

pred : N → N pred = λn. recN_.N(zero, y._.y, n) plus : N → N → N plus = λn.λm. recN_.N(m, _.z.suc z, n)

Exercise: define multiplication and subtraction!

Andreas Abel (GU) Type Theory ESSLLI 2016 16 / 24 Identity type

Identity type

Formation Γ ` A : type Γ ` M : A Γ ` N : A IdF Γ ` IdA(M, N): type

Introduction Γ ` M = N : A IdI Γ ` refl : IdA(M, N) Elimination (substitution) and computation

Γ ` A : type Γ, x:A ` C : type Γ ` M : A Γ ` N : A Γ ` P : IdA(M, N) Γ ` O : C[M/x] IdE− Γ ` substA,x.C (M, N, P, O): C[N/x] Γ ` substA,x.C (M, M, refl, O) = O : C[M/x]

Andreas Abel (GU) Type Theory ESSLLI 2016 17 / 24 Identity type

Identity type: more elimination rules

Full elimination (J)

Γ ` A : type Γ, x:A, y:A, p : IdA(x, y) ` C : type Γ ` M : A Γ ` N : A Γ ` P : IdA(M, N) Γ, z:A ` O : C[z/x, z/y, refl/p] IdE Γ ` JA,x.y.p.C (M, N, P, z.O): C[M/x, N/y, P/p] Γ ` JA,x.y.p.C (M, M, refl, z.O) = O[M/x]: C[M/x, M/y, refl/p]

Uniquess of identity proofs (Streicher’s K ) [7]

Γ ` A : type Γ, x:A, p : IdA(x, x) ` C : type Γ ` M : A Γ ` P : IdA(M, M) Γ, z:A ` O : C[z/x, refl/p] IdE Γ ` KA,x.p.C (M, P, z.O): C[M/x, P/p] Γ ` KA,x.p.C (M, refl, z.O) = O[M/x]: C[M/x, refl/p]

Andreas Abel (GU) Type Theory ESSLLI 2016 18 / 24 Identity type

Digression: groupoid

Each type A can be interpreted as a category.

IdA(M, N) is the of morphims between objects M, N : A. refl is the identity morphims. Transitivity is morphism composition. Symmetry makes the category into a groupoid [3].

AddingK makes groupoid trivial: Id A(M, N) has at most one inhabitant.

Andreas Abel (GU) Type Theory ESSLLI 2016 19 / 24 Identity type

Digression: Type Theory

Started by Field’s medallist Vladimir Voevodsky. Drop axiomK.

Interpret IdA(M, N) as path from M to N. Pathes form a groupoid. Groupoid laws form again a groupoid... ω-groupoid.

Andreas Abel (GU) Type Theory ESSLLI 2016 20 / 24 Identity type

Equality proofs

Exercise: for the identity type, prove: subst is a definable fromJ symmetry is definable with subst transitivity is definable with subst if you are courageous: K implies uniqueness of identity proofs (UIP):

IdIdA(M,N)(P, Q) is inhabited. Exercise: show that IdI is equivalent to:

Γ ` M : A IdI0 Γ ` refl : IdA(M, M)

Andreas Abel (GU) Type Theory ESSLLI 2016 21 / 24 Identity type

Agda

For the rest, we use Agda! More user-friendly interface to Type Theory: User-definable data types Function definitions by pattern matching Termination checking

Andreas Abel (GU) Type Theory ESSLLI 2016 22 / 24 Identity type

ReferencesI

Thorsten Altenkirch, Peter Dybjer, Martin Hofmann, and Philip J. Scott. Normalization by evaluation for typed lambda with coproducts. In LICS’01, pages 303–310. IEEE CS Press, 2001. Thorsten Altenkirch and Tarmo Uustalu. Normalization by evaluation for λ→2. In FLOPS’04, volume 2998 of LNCS, pages 260–275. Springer, 2004. Martin Hofmann and Thomas Streicher. The groupoid model refutes uniqueness of identity proofs. In LICS’94, pages 208–212. IEEE CS Press, 1994.

Andreas Abel (GU) Type Theory ESSLLI 2016 23 / 24 Identity type

ReferencesII

Per Martin-Löf. An intuitionistic theory of types: Predicative part. In Logic Colloquium ‘73, pages 73–118. North-Holland, 1975. Bengt Nordström, Kent Petersson, and Jan M. Smith. Programming in Martin Löf’s Type Theory: An Introduction. Clarendon Press, Oxford, 1990. Ulf Norell. Towards a Practical Based on Theory. PhD thesis, Chalmers, Göteborg, Sweden, 2007. Thomas Streicher. Investigations into Intensional Type Theory, 1993. Habilitation thesis, Ludwig-Maximilians-University Munich. Andreas Abel (GU) Type Theory ESSLLI 2016 24 / 24