Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think...

23
Simply Typed λ-calculus Lecture 1 Jeremy Dawson The Australian National University Semester 2, 2019 Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 1 / 23

Transcript of Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think...

Page 1: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Simply Typed λ-calculusLecture 1

Jeremy Dawson

The Australian National University

Semester 2, 2019

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 1 / 23

Page 2: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

A Brief History of Type Theory

First developed by Bertrand Russell (around 1903), to avoid certainparadoxes in the foundations of math, e.g., Russell’s paradox.

A hierarchy of types introduced to solve this. Typically, a class of allsets is of a different type than a set, etc.

Formalised by Alonzo Church in the 1930’s in his simple type theory,which introduces types to (untyped) λ-calculus.

The full simple type theory deals with logical concepts as well (alsoknown as higher-order logic).

We shall deal only with the functional part, i.e., we only consider theβ-equality part of Church’s simple theory of types.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 2 / 23

Page 3: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Types in computation

Types are used to restrict the behaviours of a program.

Maxim: Well-typed programs don’t go wrong.

In the case of λ-calculus, types can be used to enforce termination.

Think of it as a light-weight theorem proving: types correspond to theproperty we want the program to satisfy.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 3 / 23

Page 4: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Outline of the course

Lecture 1: Introduction, Church & Curry type systems.

Lecture 2: The type preservation theorem.

Lecture 3: Type inference.

Lecture 4: The normalisation theorem (typed terms always havenormal forms); intuitionistic logic and Kripke semantics.

Lecture 5: Curry-Howard correspondence, relating intuitionistic logicand simply typed lambda calculus.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 4 / 23

Page 5: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

References

Henk Barendregt. Lambda calculi with types. Handbook of Logic inComputer Science, Vol. II, Editors: S. Abramsky, D.M. Gabbay,T.S.E. Maibaum, Oxford University Press, 1993.

J. Roger Hindley. Basic Simple Type Theory. Cambridge UniversityPress, 1997.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 5 / 23

Page 6: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Course website

Course materials will be made available on Wattle and/or the COMP4630website:http://cs.anu.edu.au/courses/COMP4630/schedule.html

Email me at [email protected], phone (612)57778I am in the Hanna Neumann building, bldg 145, rm 2.24 Tuesdays,Thursdays and alternate Mondays.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 6 / 23

Page 7: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Overview of untyped λ-calculus

Syntax:M,N ::= x | M N | λx .M

α-equivalence: syntactic equality modulo renaming of boundvariables, e.g., λxλy .x ≡ λuλv .u.Substitution.

I Notation: M[x := N]. (Other notations exist also).I Capture avoiding: no free variables in N got captured in M after

substitution: use α-equivalence first to avoid the problem

(λv . u v)[u := v ] 6≡ λv . v v

but (λv . u v)[u := v ] ≡ λw . v w .I Substitute for free variables only

(λv . u v)[v := w ] 6≡ λv . u w

but (λv . u v)[v := w ] ≡ λv . u v

Computation via β-reduction:

(λx .M) N −→β M[x := N]

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 7 / 23

Page 8: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Two type systems

There are two ways to define typed λ-calculus:

Curry’s style: type assignment (also, implicit typing)I Terms are as untyped λ-calculus.I Given a term M, what types can be assigned to M? There can be zero

or many.

Church’s style: type annotations (also, explicit typing).I Terms are explicitly annotated with type expressions.I Given a term M, if it is typeable, it has a unique type.

We shall see that the two systems are essentially equivalent.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 8 / 23

Page 9: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Some questions

Some typical questions one asks about a type system:

What are the properties of well-typed terms?I Termination: A well-typed term has a normal form.I Type preservation: If M has type τ and M −→β N, then N has type τ.

Type checking: Given a term M and a type τ , is it decidable to checkwhether M has type τ?

Type inference: Given a term M, is there a type τ such that M hastype τ?

Type inhabitant: Given a type τ , is there a term M such that M hastype τ?

We’ll see answers to some of these questions for simple type systems.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 9 / 23

Page 10: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Simple types

Assume an infinite set of type variables. We use α, β, γ to denotetype variables.

The set T of simple types is the smallest set such that:I Type variables: α ∈ T, for every type variable α.I Function types: If τ1, τ2 ∈ T, then τ1 → τ2 ∈ T.

We write τ ≡ τ ′ if τ and τ ′ are syntactically equal.

Notational convention: we assume → associate to the right, so

α1 → α2 → α3

meansα1 → (α2 → α3).

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 10 / 23

Page 11: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Typing statements, type declarations and typing contexts

When assigning a type to a term (in both Church & Curry typesystems), we assign types to free variables in the term.

A type declaration is a pair x : τ of a (term) variable x and a type τ .

A typing statement is a pair M : τ of a term and a type (read M hastype τ).

A typing context Γ is a finite set of typing declarations

{x1 : τ1, . . . , xn : τn}

where x1, . . . , xn are pairwise distinct.

Typing judgments:Γ ` M : τ

Read: the statement M : τ is derivable (in a type system) from thetyping context Γ

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 11 / 23

Page 12: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

λCu: Curry’s type system

The typing judgmentΓ `λCu

M : τ

is derivable in λCu if Γ `λCuM : τ can be produced by the following rules:

Γ `λCux : τ

axiom, if (x : τ) ∈ ΓΓ, x : τ1 `λCu

M : τ2

Γ `λCu(λx .M) : τ1 → τ2

→ intro

Γ `λCuM : τ ′ → τ Γ `λCu

N : τ ′

Γ `λCu(M N) : τ

→ elim

Here Γ, x : τ1 means Γ ∪ {x : τ1}.

Note that in → intro, x has to be “fresh” w.r.t. Γ (ie, x not in Γ)

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 12 / 23

Page 13: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Example of a typing deduction (or typing derivation)

For simplicity, we omit the subscript λCu in `λCu:

x : α→ β, y : α ` x : α→ β x : α→ β, y : α ` y : α

x : α→ β, y : α ` (x y) : β

x : α→ β ` (λy .x y) : α→ β

` (λxλy .x y) : (α→ β)→ α→ β

Exercises: prove the following typing judgments.

` (λx .x) : α→ α.

` (λx .x) : (β → β)→ (β → β).

` λxλyλz . x z (y z) : (α→ β → γ)→ (α→ β)→ α→ γ.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 13 / 23

Page 14: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Example: an untypeable term

The term λx .x x is untypeable in λCu, i.e., for every type τ , there doesnot exist a derivation of ` (λx .x x) : τ.

Exercise: Prove this.

(We’ll see a more systematic way to prove this in Lecture 3).

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 14 / 23

Page 15: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Church’s type system λCh: annotated terms

In λCh, types are incorporated explicitly into terms.

The set of T-annotated terms ΛT is defined inductively as follows:I x ∈ ΛT for every term variable x .I If M,N ∈ ΛT then (M N) ∈ ΛT.I If M ∈ ΛT and τ ∈ T then (λx : τ.M) ∈ ΛT.

The notions of type declarations, typing statement, typing contextsand typing judgments are as in Curry’s system.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 15 / 23

Page 16: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Reductions in λCh-terms

Strictly speaking, terms in ΛT are not λ-terms, so one needs to defineits equality theory, reductions, etc.

The notions of β-reduction −→β, multi-step reduction −→∗β aredefined as in untyped λ-calculus.

(λx : τ.M) N −→β M[x := N].

Church-Rosser property also holds for T-annotated terms.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 16 / 23

Page 17: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Inference rules of λCh

Γ `λChx : τ

axiom, if (x : τ) ∈ Γ

Γ, x : τ1 `λChM : τ2

Γ `λCh(λx : τ1.M) : (τ1 → τ2)

→ intro

Note that x has to be fresh w.r.t. Γ.

Γ `λChM : τ ′ → τ Γ `λCh

N : τ ′

Γ `λCh(M N) : τ

→ elim

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 17 / 23

Page 18: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Example

The following are derivable in λCh:

` (λx : τ.x) : (τ → τ).

` (λx : σλy : τ.x) : (σ → τ → σ).

x : σ ` (λy : τ.x) : (τ → σ).

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 18 / 23

Page 19: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Church’s system: uniqueness of types

Theorem

Suppose Γ `λChM : τ and Γ `λCh

M : τ ′. Then τ ≡ τ ′.

Proof.

By induction on the structure of M.Exercise: complete the proof.

Type uniqueness does not hold for Curry’s system, but we’ll see that everytypeable term in λCu has a unique principal type.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 19 / 23

Page 20: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Relating the Curry and Church systems

Church’s system can be simulated by Curry’s system by forgetting thetype annotations.

Let Λ denote the set of unannotated λ-terms.

Define a ‘forgetful map’ | · | from ΛT to Λ as follows:

|x | ≡ x|M N| ≡ |M| |N|

|λx : τ.M| ≡ λx .|M|

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 20 / 23

Page 21: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Relating the Curry and Church systems

Theorem (Church to Curry)

Let M ∈ ΛT. Then Γ `λChM : τ implies Γ `λCu

|M| : τ.

Proof: (exercise)

Theorem (Curry to Church)

Let M ∈ Λ. Then Γ `λCuM : τ implies that there exists M ′ ∈ ΛT such that

Γ `λChM ′ : τ and |M ′| ≡ M.

Proof: (exercise)

Corollary

A type τ ∈ T is inhabited in λCh if and only if it is inhabited in λCu.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 21 / 23

Page 22: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

On-line type checking and inferencing - Haskell

Haskell: a functional language with types based on simple type theory

ghci on the departmental systems

To download it yourself, can use Hugs (adequate and much smaller)https://www.haskell.org/hugs/

λxλyλz . x z (y z) is entered as \x -> \y -> \z -> x z (y z)

to get the type of this, > :t \x -> \y -> \z -> x z (y z)

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 22 / 23

Page 23: Simply Typed -calculus · In the case of -calculus, types can be used to enforce termination. Think of it as a light-weight theorem proving: types correspond to the property we want

Summary

We have covered two styles of type systems for λ-calculus.

Church’s system annotates the λ-terms with types explicitly.

Curry’s system leaves types implicit.

Both are essentially the same.

For next lecture, we shall be studying mainly properties of Curry’ssystem.

Jeremy Dawson (ANU) COMP4630,Lecture 1 Semester 2, 2019 23 / 23