• No results found

Simply-typed lambda calculus

N/A
N/A
Protected

Academic year: 2021

Share "Simply-typed lambda calculus"

Copied!
51
0
0

Loading.... (view fulltext now)

Full text

(1)

Simply-typed lambda calculus

CS 152 (Spring 2021)

Harvard University

Tuesday, March 2, 2021

(2)

Today, we will learn about

I Simply-typed lambda calculus

I Type soundness

I Normalization

2 / 51

(3)

Types

I A type is a collection of computational entities that share some common property.

I For example, the type int represents all

expressions that evaluate to an integer, and the type int → int represents all functions from integers to integers.

I The Pascal subrange type [1..100] represents

(4)

Types

Type systems are a lightweight formal method for reasoning about behavior of a program.

4 / 51

(5)

Uses of type systems

I Naming and organizing useful concepts

I Providing information (to the compiler or programmer) about data manipulated by a program

I Ensuring that the run-time behavior of programs meet certain criteria.

(6)

Simply-typed lambda calculus

We will consider a type system for the lambda calculus that ensures that values are used correctly.

For example, that a program never tries to add an integer to a function.

The resulting language (lambda calculus plus the type system) is called the simply-typed lambda calculus.

6 / 51

(7)

Simply-typed lambda calculus

In the simply-typed lambda calculus, we explicitly state what the type of the argument is.

That is, in an abstraction λx : τ. e, the τ is the expected type of the argument.

(8)

Simply-typed lambda calculus: Syntax

We will include integer literals n, addition e1 + e2, and the unit value (). The unit value is the only value of type unit.

8 / 51

(9)

Simply-typed lambda calculus: Syntax

expressions e ::= x | λx : τ. e | e1 e2 | n | e1 + e2 | () values v ::= λx : τ. e | n | ()

types τ ::= int | unit | τ1 → τ2

(10)

Simply-typed lambda calculus: CBV small

step operational semantics

The operational semantics of the simply-typed lambda calculus are the same as the untyped lambda calculus.

10 / 51

(11)

Simply-typed lambda calculus: CBV small

step operational semantics

E ::= [·] | E e | v E | E + e | v + E

Context e −→ e0 E [e] −→ E [e0]

β-reduction

(λx . e) v −→ e{v /x }

(12)

The typing relation

The presence of types does not alter the evaluation of an expression at all. So what use are types?

12 / 51

(13)

The typing relation

We will use types to restrict what expressions we will evaluate. Specifically, the type system for the simply-typed lambda calculus will ensure that any well-typed program will not get stuck.

(14)

The typing relation

A term e is stuck if e is not a value and there is no term e0 such that e −→ e0.

14 / 51

(15)

The typing relation

42 + λx . x

(16)

The typing relation

() 47

16 / 51

(17)

Typing judgment

I We introduce a relation (or judgment) over typing contexts (or type environments) Γ, expressions e, and types τ .

I The judgment

Γ ` e : τ

is read as “e has type τ in context Γ”.

(18)

I A typing context is a sequence of variables and their types.

I In the typing judgment Γ ` e : τ , we will ensure that if x is a free variable of e, then Γ

associates x with a type.

18 / 51

(19)

Typing judgment

I We can view a typing context as a partial function from variables to types.

I We will write Γ, x : τ or Γ[x 7→ τ ] to indicate the typing context that extends Γ by

associating variable x with with type τ .

I We write ` e : τ to mean that the closed term

(20)

Well-typed expression

I Given a typing environment Γ and expression e, if there is some τ such that Γ ` e : τ , we say that e is well-typed under context Γ

I If Γ is the empty context, we say e is well-typed.

20 / 51

(21)

Inductive definition of Γ ` e : τ

T-Int

Γ ` n : int T-Add Γ ` e1: int Γ ` e2: int Γ ` e1+ e2: int

T-Unit

Γ ` () : unit

T-Var

Γ ` x : τ Γ(x ) = τ T-Abs Γ, x : τ ` e : τ0 Γ ` λx : τ. e : τ → τ0 Γ ` e1: τ → τ0 Γ ` e2: τ

(22)

Inductive definition of Γ ` e : τ

An integer n always has type int. Expression e1+ e2

has type int if both e1 and e2 have type int. The unit value () always has type unit.

22 / 51

(23)

Inductive definition of Γ ` e : τ

I Variable x has whatever type the context associates with x .

I The abstraction λx : τ. e has the function type τ → τ0 if the function body e has type τ0 under the assumption that x has type τ .

I An application e1 e2 has type τ0 provided that e1 is a function of type τ → τ0, and e2 is an

(24)

Type-checking an expression

Consider the program (λx : int. x + 40) 2.

24 / 51

(25)

Type-checking an expression

The following is a proof that (λx : int. x + 40) 2 is well-typed.

(26)

Type-checking an expression

T-App T-Abs

T-Add T-Var

x : int ` x : int T-Int

x : int ` 40 : int x : int ` x + 40 : int

` λx : int. x + 40 : int → int T-Int

` 2 : int

` (λx : int. x + 40) 2 : int

26 / 51

(27)

Theorem (Type soundness)

If ` e : τ and e −→ e0 then either e0 is a value, or there exists e00 such that e0 −→ e00.

(28)

Theorem (Type soundness)

To prove this, we use two lemmas: preservation and progress.

28 / 51

(29)

Theorem (Type soundness)

Intuitively, preservation says that if an expression e is well-typed, and e can take a step to e0, then e0 is well-typed. That is, evaluation preserves

well-typedness.

(30)

Theorem (Type soundness)

Progress says that if an expression e is well-typed, then either e is a value, or there is an e0 such that e can take a step to e0. That is, well-typedness means that the expression cannot get stuck.

30 / 51

(31)

Together, these two lemmas suffice to prove type soundness.

(32)

Lemma (Preservation)

If ` e : τ and e −→ e0 then ` e0: τ .

32 / 51

(33)

P(e −→ e

0

) = ∀τ. if ` e : τ then ` e

0

: τ

To prove this, we proceed by induction on e −→ e0. That is, we will prove for all e and e0 such that e −→ e0, that P(e −→ e0) holds, where

P(e −→ e0) = ∀τ. if ` e : τ then ` e0: τ.

(34)

P(e −→ e

0

) = ∀τ. if ` e : τ then ` e

0

: τ

Consider each of the inference rules for the small step relation.

34 / 51

(35)

P(e −→ e

0

) = ∀τ. if ` e : τ then ` e

0

: τ

Add

Assume ` e : τ .

Here e ≡ n1 + n2, and e0 = n where n = n1 + n2, and τ = int. By the typing rule T-Int, we have

` e0: int as required.

(36)

P(e −→ e

0

) = ∀τ. if ` e : τ then ` e

0

: τ

β-reduction Assume ` e : τ .

Here, e ≡ (λx : τ0. e1) v and e0 ≡ e1{v /x}. Since e is well-typed, we have derivations showing

` λx : τ0. e1: τ0 → τ and ` v : τ0. There is only one typing rule for abstractions, T-Abs, from which we know x : τ ` e1: τ . By the substitution lemma (see below), we have ` e1{v /x} : τ as required.

36 / 51

(37)

P(e −→ e

0

) = ∀τ. if ` e : τ then ` e

0

: τ

Context Assume ` e : τ .

Here, we have some context E such that e = E [e1] and e0 = E [e2] for some e1 and e2 such that

e1 −→ e2. The inductive hypothesis is that P(e1 −→ e2).

Since e is well-typed, we can show by induction on the structure of E that ` e1: τ1 for some τ1. By the inductive hypothesis, we thus have ` e2: τ1. By the context lemma (see below) we have ` E [e0] : τ as

(38)

If ` e : τ and e −→ e

0

then ` e

0

: τ

This proves the lemma.

38 / 51

(39)

Additional lemmas we used in the proof above.

Lemma (Substitution)

If x : τ0 ` e : τ and ` v : τ0 then ` e{v /x } : τ .

Lemma (Context)

If ` E [e0] : τ and ` e0: τ0 and ` e1: τ0 then

` E [e1] : τ .

(40)

Lemma (Progress)

If ` e : τ then either e is a value or there exists an e0 such that e −→ e0.

40 / 51

(41)

If ` e : τ then either e is a value or there

exists an e

0

such that e −→ e

0

.

We proceed by induction on the derivation of

` e : τ . That is, we will show for all e and τ such that ` e : τ , we have P(` e : τ ), where

P(` e : τ ) = either e is a value or ∃e0 such that e −→ e0.

(42)

If ` e : τ then either e is a value or there

exists an e

0

such that e −→ e

0

.

T-Var This case is impossible, since a variable is not well-typed in the empty environment.

42 / 51

(43)

If ` e : τ then either e is a value or there

exists an e

0

such that e −→ e

0

.

T-Unit, T-Int, T-Abs

Trivial, since e must be a value.

(44)

If ` e : τ then either e is a value or there

exists an e

0

such that e −→ e

0

.

T-Add

Here e ≡ e1+ e2 and ` ei: int for i ∈ {1, 2}. By the inductive hypothesis, for i ∈ {1, 2}, either ei is a value or there is an ei0 such that ei −→ ei0.

If e1 is not a value, then by Context,

e1 + e2 −→ e10 + e2. If e1 is a value and e2 is not a value, then by Context, e1 + e2 −→ e1 + e20. If e1 and e2 are values, then, it must be the case that they are both integer literals, and so, by Add, we have e1 + e2 −→ n where n equals e1 plus e2.

44 / 51

(45)

If ` e : τ then either e is a value or there

exists an e

0

such that e −→ e

0

.

T-App

Here e ≡ e1 e2 and ` e1: τ0 → τ and ` e2: τ0. By the inductive hypothesis, for i ∈ {1, 2}, either ei is a value or there is an ei0 such that ei −→ ei0.

If e1 is not a value, then by Context,

e1 e2 −→ e10 e2. If e1 is a value and e2 is not a

value, then by Context, e1 e2 −→ e1 e20. If e1 and e2 are values, then, it must be the case that e1 is an abstraction λx : τ0. e0, and so, by β-reduction, we

(46)

If ` e : τ then either e is a value or there

exists an e

0

such that e −→ e

0

.

This proves the Progress lemma.

46 / 51

(47)

Expressive power of the simply-typed

lambda calculus

Are there programs that do not get stuck that are not well-typed?

(48)

Expressive power of the simply-typed

lambda calculus

Unfortunately, the answer is yes.

Consider the identity function λx . x .

We must provide a type for the argument. If we specify λx : int. x , then the program (λx : int. x ) () is not well-typed, even though it does not get stuck.

48 / 51

(49)

Expressive power of the simply-typed

lambda calculus: Recursion

We can no longer write recursive functions.

Consider Ω = (λx . x x ) (λx . x x ). Let’s suppose that the type of λx . x x is τ → τ0. Then τ must be equal to τ → τ0. There is no such type for which this equality holds.

(50)

Theorem (Normalization)

If ` e : τ then there exists a value v such that e −→ v .

50 / 51

(51)

This is known as normalization since it means that given any well-typed expression, we can reduce it to a normal form, which, in our case, is a value.

References

Related documents

Lawrences amerikanischer Literaturgeschichte über Richard Slotkin bis zu Mark Seltzer (Serial Killers). Sehr schön auf den Punkt gebracht wird dieses amerikanische

Indeed, a change in the dynamics of the El Nino/Southern Oscillation (ENSO) system occurred around 3000 BC, which culminated in a series of strong and frequent El Nino events

psychosis onset in the ultra high risk for psychosis “prodromal” pop- ulation. Subjective symptoms of schizophrenia in research and the clinic: the basic symptom concept. The

McAfee VirusScan Enterprise 7.0.x McAfee VirusScan Enterprise 7.1.x McAfee VirusScan Enterprise 7.5.x McAfee VirusScan Enterprise 8.0.x McAfee VirusScan Enterprise

Keywords: Chondrogenic differentiation, Mesenchymal stromal cells, Obstetric factors, Proliferation, Umbilical cord, Wharton ’ s jelly.. *

In this work we investigate the decay fluorescence due to Frenkel excitons in linear systems with interstitial traps ar- ranged according to the Fibonacci and Thue-Morse se-

The Cao Bang Basin is the northernmost of the basins related to the Cao Bang-Tien Yen Fault Zone in northern Vietnam.. The basin is filled with a thick series of

The maximum total pressure in the combustion chamber is 1.17e+05, and the average total pressure in the combustion chamber is 1.10e+05, the average total pressure