G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21....

36
G54FOP: Lecture 11 Untyped λ-calculus: Operational Semantics and Reduction Orders Henrik Nilsson University of Nottingham, UK G54FOP: Lecture 11 – p.1/21

Transcript of G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21....

Page 1: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

G54FOP: Lecture 11Untyped λ-calculus: Operational Semantics

and Reduction Orders

Henrik Nilsson

University of Nottingham, UK

G54FOP: Lecture 11 – p.1/21

Page 2: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Name Capture

Recall that[x 7→ t]F

means “substitute t for free occurrences of x in F .

What is:[x 7→ y](λx.x) = ???

G54FOP: Lecture 11 – p.2/21

Page 3: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Name Capture

Recall that[x 7→ t]F

means “substitute t for free occurrences of x in F .

Because x not free in λx.x:[x 7→ y](λx.x) = (λx.x)

G54FOP: Lecture 11 – p.2/21

Page 4: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Name Capture

Recall that[x 7→ t]F

means “substitute t for free occurrences of x in F .

Because x not free in λx.x:[x 7→ y](λx.x) = (λx.x)

What is:[x 7→ y](λy.x) = ???

G54FOP: Lecture 11 – p.2/21

Page 5: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Name Capture

Recall that[x 7→ t]F

means “substitute t for free occurrences of x in F .

Because x not free in λx.x:[x 7→ y](λx.x) = (λx.x)

x is free in λy.x, but must avoid name capture :[x 7→ y](λy.x) 6= (λy.y)

G54FOP: Lecture 11 – p.2/21

Page 6: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Substitution Caveats

We have seen that there are some caveats withsubstitution:

G54FOP: Lecture 11 – p.3/21

Page 7: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Substitution Caveats

We have seen that there are some caveats withsubstitution:

• Must only substitute for free variables:

[x 7→ t](λx.x) 6= λx.t

G54FOP: Lecture 11 – p.3/21

Page 8: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Substitution Caveats

We have seen that there are some caveats withsubstitution:

• Must only substitute for free variables:

[x 7→ t](λx.x) 6= λx.t

• Must avoid name capture :

[x 7→ y](λy.x) 6= λy.y

G54FOP: Lecture 11 – p.3/21

Page 9: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Substitution Caveats

We have seen that there are some caveats withsubstitution:

• Must only substitute for free variables:

[x 7→ t](λx.x) 6= λx.t

• Must avoid name capture :

[x 7→ y](λy.x) 6= λy.y

“Substitution” almost always meanscapture-avoiding substitution .

G54FOP: Lecture 11 – p.3/21

Page 10: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Capture-Avoiding Substitution (1)

[x 7→ s]y =

s, if x ≡ y

y, if x 6≡ y

[x 7→ s](t1 t2) = ([x 7→ s]t1) ([x 7→ s]t2)

[x 7→ s](λy.t) =

λy.t, if x ≡ y

λy.[x 7→ s]t, if x 6≡ y ∧ y /∈ FV(s)

λz.[x 7→ s]([y 7→ z]t), if x 6≡ y ∧ y ∈ FV(s),

where z is fresh

where s, t and indexed variants denote lambda-terms; x,y, and z denote variables; FV(t) denotes the free variablesof term t; and ≡ denotes syntactic equality.

G54FOP: Lecture 11 – p.4/21

Page 11: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Capture-Avoiding Substitution (2)

The condition “z is fresh” can be relaxed:

z 6≡ x ∧ z /∈ FV (s) ∧ z /∈ FV (t)

is enough.

G54FOP: Lecture 11 – p.5/21

Page 12: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Capture-Avoiding Substitution (3)A slight variation:

[x 7→ s]y =

s, if x ≡ y

y, if x 6≡ y

[x 7→ s](t1 t2) = ([x 7→ s]t1) ([x 7→ s]t2)

[x 7→ s](λy.t) =

λy.t, if x ≡ y

λy.[x 7→ s]t, if x 6≡ y ∧ y /∈ FV(s)

[x 7→ s](λz.[y 7→ z]t), if x 6≡ y ∧ y ∈ FV(s),

where z /∈ FV(s)

∧ z /∈ FV(t)

Homework: Why isn’t z 6≡ x needed in this case?G54FOP: Lecture 11 – p.6/21

Page 13: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

α- and η-conversion

• Renaming bound variables is known asα-conversion . E.g.

(λx.x) ↔α

(λy.y)

• Note that (λx.F x) G →β

F G if x not free in F .

This justifies η-conversion :

λx.F x ↔η

F if x /∈ FV(F )

G54FOP: Lecture 11 – p.7/21

Page 14: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Capture-Avoiding Substitution (4)

If we adopt the convention that terms that differ only in thenames of bound variables are interchangeable in allcontexts, then the following partial definition can be usedas long as it is understood that an α-conversion has to becarried out if no case applies:

[x 7→ s]y =

s, if x ≡ y

y, if x 6≡ y

[x 7→ s](t1 t2) = ([x 7→ s]t1) ([x 7→ s]t1)

[x 7→ s](λy.t) = λy.[x 7→ s]t, if x 6≡ y ∧ y /∈ FV(s)

G54FOP: Lecture 11 – p.8/21

Page 15: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Call-By-Value (1)

Abstract syntax:

t → terms:

x variable

| λx.t abstraction

| t t application

Values:v → values:

λx.t abstraction value

G54FOP: Lecture 11 – p.9/21

Page 16: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Call-By-Value (2)

Call-by-value operational semantics:

G54FOP: Lecture 11 – p.10/21

Page 17: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Call-By-Value (2)

Call-by-value operational semantics:

t1 −→ t′1

t1 t2 −→ t′1t2

(E-APP1)

G54FOP: Lecture 11 – p.10/21

Page 18: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Call-By-Value (2)

Call-by-value operational semantics:

t1 −→ t′1

t1 t2 −→ t′1t2

(E-APP1)

t2 −→ t′2

v1 t2 −→ v1 t′2

(E-APP2)

G54FOP: Lecture 11 – p.10/21

Page 19: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Call-By-Value (2)

Call-by-value operational semantics:

t1 −→ t′1

t1 t2 −→ t′1t2

(E-APP1)

t2 −→ t′2

v1 t2 −→ v1 t′2

(E-APP2)

(λx.t) v −→ [x 7→ v]t (E-APPABS)

G54FOP: Lecture 11 – p.10/21

Page 20: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Full β-reduction

Operational semantics for full β-reduction(non-deterministic). Syntax as before, but thesyntactic category of values not used:

G54FOP: Lecture 11 – p.11/21

Page 21: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Full β-reduction

Operational semantics for full β-reduction(non-deterministic). Syntax as before, but thesyntactic category of values not used:

t1 −→ t′1

t1 t2 −→ t′1t2

(E-APP1)

G54FOP: Lecture 11 – p.11/21

Page 22: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Full β-reduction

Operational semantics for full β-reduction(non-deterministic). Syntax as before, but thesyntactic category of values not used:

t1 −→ t′1

t1 t2 −→ t′1t2

(E-APP1)

t2 −→ t′2

t1 t2 −→ t1 t′2

(E-APP2)

G54FOP: Lecture 11 – p.11/21

Page 23: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Full β-reduction

Operational semantics for full β-reduction(non-deterministic). Syntax as before, but thesyntactic category of values not used:

t1 −→ t′1

t1 t2 −→ t′1t2

(E-APP1)

t2 −→ t′2

t1 t2 −→ t1 t′2

(E-APP2)

(λx.t1) t2 −→ [x 7→ t2]t1 (E-APPABS)

G54FOP: Lecture 11 – p.11/21

Page 24: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Normal-Order

Normal-order operational semantics is somewhatawkward to specify. Like full β-reduction, exceptleft-most, outermost redex first.

G54FOP: Lecture 11 – p.12/21

Page 25: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Call-By-Name

Call-by-name like normal order, but no evaluationunder λ:

G54FOP: Lecture 11 – p.13/21

Page 26: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Call-By-Name

Call-by-name like normal order, but no evaluationunder λ:

t1 −→ t′1

t1 t2 −→ t′1t2

(E-APP1)

G54FOP: Lecture 11 – p.13/21

Page 27: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Call-By-Name

Call-by-name like normal order, but no evaluationunder λ:

t1 −→ t′1

t1 t2 −→ t′1t2

(E-APP1)

(λx.t1) t2 −→ [x 7→ t2]t1 (E-APPABS)

G54FOP: Lecture 11 – p.13/21

Page 28: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Op. Semantics: Call-By-Name

Call-by-name like normal order, but no evaluationunder λ:

t1 −→ t′1

t1 t2 −→ t′1t2

(E-APP1)

(λx.t1) t2 −→ [x 7→ t2]t1 (E-APPABS)

Note: Argument not evaluated “prior to call”!

G54FOP: Lecture 11 – p.13/21

Page 29: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Call-By-Value vs. Call-By-Name (1)

Exercises:

1. Evaluate the following term both bycall-by-name and call-by-value:

(λx.λy.y) ((λz.z z) (λz.z z))

2. For some term t and some value v, supposet

∗→β

v in, say 100 steps. Consider (λx.x x) t

under both call-by-value and call-by-name.How many steps of evaluation in the twocases? (Roughly)

G54FOP: Lecture 11 – p.14/21

Page 30: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Call-By-Value vs. Call-By-Name (2)

Questions:• Do we get the same result (modulo termination

issues) regardless of evaluation order?• Which order is “better”?

G54FOP: Lecture 11 – p.15/21

Page 31: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

The Church-Rosser Theorems (1)

Church-Rosser Theorem I:

For all λ-calculus terms t, t1, and t2 suchthat t

∗→β

t1 and t∗→β

t2, there exists a term

t3 such that t1∗→β

t3 and t2∗→β

t3.

That is, β-reduction is confluent .

This is also known as the “diamond property”.

G54FOP: Lecture 11 – p.16/21

Page 32: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

The Church-Rosser Theorems (2)

Church-Rosser Theorem II:

If t1∗→β

t2 and t2 is a normal form (no

redexes), then t1 will reduce to t2 undernormal-order reduction.

G54FOP: Lecture 11 – p.17/21

Page 33: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Which Reduction Order? (1)

So, which reduction order is “best”?• Depends on the application. Sometimes

reduction under λ needed, sometimes not.• Normal-order reduction has the best possible

termination properties: if a term has a normalform, normal-order reduction will find it.

G54FOP: Lecture 11 – p.18/21

Page 34: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Which Reduction Order? (2)

• In terms of reduction steps (fewer is moreefficient), none is strictly better than the other.E.g.:- Call-by-value may run forever on a term

where normal-order would terminate.- Normal-order often duplicates redexes (by

substitution of reducible expressions forvariables), thereby possibly duplicatingwork, something that call-by-value avoids.

G54FOP: Lecture 11 – p.19/21

Page 35: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Lazy Evaluation (1)

Lazy evaluation is an implementationtechnique that seeks to combine theadvantages of the various orders by:

• evaluate on demand only, but• evaluate any one redex at most once

(avoiding duplication of work)

Idea: Graph Reduction to avoid duplication byexplicit sharing of redexes.

G54FOP: Lecture 11 – p.20/21

Page 36: G54FOP: Lecture 11psznhn/G54FOP/LectureNotes/lecture11.pdf · G54FOP: Lecture 11 – p.3/21. Capture-Avoiding Substitution (1) [x 7→s]y = s, if x ≡ y y, if x 6≡y [x 7→s](t1

Lazy Evaluation (2)

Result: normal-order/call-by-need semantics, butefficiency closer to call-by-value (whencall-by-value doesn’t do unnecessary work).However, there are inherent implementationoverheads of lazy evaluation.

Lazy evaluation is used in languages like Haskell.

G54FOP: Lecture 11 – p.21/21