Programming Language Concepts: Lecture 18 Madhavan Mukund Chennai - - PowerPoint PPT Presentation

programming language concepts lecture 18
SMART_READER_LITE
LIVE PREVIEW

Programming Language Concepts: Lecture 18 Madhavan Mukund Chennai - - PowerPoint PPT Presentation

Programming Language Concepts: Lecture 18 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 18, 30 March 2009 One step reduction Can have other reduction rules


slide-1
SLIDE 1

Programming Language Concepts: Lecture 18

Madhavan Mukund

Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009

PLC 2009, Lecture 18, 30 March 2009

slide-2
SLIDE 2

One step reduction

◮ Can have other reduction rules like β

slide-3
SLIDE 3

One step reduction

◮ Can have other reduction rules like β ◮ Observe that λx.(Mx) and M are equivalent with respect to

β-reduction

slide-4
SLIDE 4

One step reduction

◮ Can have other reduction rules like β ◮ Observe that λx.(Mx) and M are equivalent with respect to

β-reduction

◮ New reduction rule η

λx.(Mx) →η M

slide-5
SLIDE 5

One step reduction

◮ Can have other reduction rules like β ◮ Observe that λx.(Mx) and M are equivalent with respect to

β-reduction

◮ New reduction rule η

λx.(Mx) →η M

◮ Given basic rules β, η, . . . , we are allowed to use them “in

any context”

slide-6
SLIDE 6

One step reduction

◮ Can have other reduction rules like β ◮ Observe that λx.(Mx) and M are equivalent with respect to

β-reduction

◮ New reduction rule η

λx.(Mx) →η M

◮ Given basic rules β, η, . . . , we are allowed to use them “in

any context”

◮ Define a one step reduction relation → inductively

M →x M′ M → M

x ∈ {β, η, . . .} M → M′ λx.M → λx.M′ M → M′ MN → M′N N → N′ MN → MN′

slide-7
SLIDE 7

Normal forms

◮ Computation — a maximal sequence of reduction steps

slide-8
SLIDE 8

Normal forms

◮ Computation — a maximal sequence of reduction steps ◮ “Values” are expressions that cannot be further reduced:

normal forms

slide-9
SLIDE 9

Normal forms

◮ Computation — a maximal sequence of reduction steps ◮ “Values” are expressions that cannot be further reduced:

normal forms

◮ Allow reduction in any context ⇒ multiple expressions may

qualify for reduction in one step

slide-10
SLIDE 10

Normal forms

◮ Computation — a maximal sequence of reduction steps ◮ “Values” are expressions that cannot be further reduced:

normal forms

◮ Allow reduction in any context ⇒ multiple expressions may

qualify for reduction in one step Natural questions

slide-11
SLIDE 11

Normal forms

◮ Computation — a maximal sequence of reduction steps ◮ “Values” are expressions that cannot be further reduced:

normal forms

◮ Allow reduction in any context ⇒ multiple expressions may

qualify for reduction in one step Natural questions

◮ Does every term reduce to a normal form?

slide-12
SLIDE 12

Normal forms

◮ Computation — a maximal sequence of reduction steps ◮ “Values” are expressions that cannot be further reduced:

normal forms

◮ Allow reduction in any context ⇒ multiple expressions may

qualify for reduction in one step Natural questions

◮ Does every term reduce to a normal form? ◮ Can a term reduce to more than one normal form, depending

  • n order reduction strategy?
slide-13
SLIDE 13

Normal forms

◮ Computation — a maximal sequence of reduction steps ◮ “Values” are expressions that cannot be further reduced:

normal forms

◮ Allow reduction in any context ⇒ multiple expressions may

qualify for reduction in one step Natural questions

◮ Does every term reduce to a normal form? ◮ Can a term reduce to more than one normal form, depending

  • n order reduction strategy?

◮ If a term has a normal form, can we always find it?

slide-14
SLIDE 14

Normal forms . . .

Does every term reduce to a normal form?

◮ Consider (λx.xx)(λx.xx)

slide-15
SLIDE 15

Normal forms . . .

Does every term reduce to a normal form?

◮ Consider (λx.xx)(λx.xx) ◮ (λx.xx)(λx.xx) →β (λx.xx)(λx.xx)

◮ Reduction never terminates

slide-16
SLIDE 16

Normal forms . . .

Does every term reduce to a normal form?

◮ Consider (λx.xx)(λx.xx) ◮ (λx.xx)(λx.xx) →β (λx.xx)(λx.xx)

◮ Reduction never terminates

◮ Call this term Ω

slide-17
SLIDE 17

Normal forms . . .

Does every term reduce to a normal form?

◮ Consider (λx.xx)(λx.xx) ◮ (λx.xx)(λx.xx) →β (λx.xx)(λx.xx)

◮ Reduction never terminates

◮ Call this term Ω

slide-18
SLIDE 18

Normal forms . . .

Can a term reduce to more than one normal form, depending on

  • rder reduction strategy?

◮ Consider FalseΩ = (λyz.z)((λx.xx)(λx.xx))

slide-19
SLIDE 19

Normal forms . . .

Can a term reduce to more than one normal form, depending on

  • rder reduction strategy?

◮ Consider FalseΩ = (λyz.z)((λx.xx)(λx.xx)) ◮ Outermost reduction:

(λyz.z)((λx.xx)(λx.xx)) → λz.z

slide-20
SLIDE 20

Normal forms . . .

Can a term reduce to more than one normal form, depending on

  • rder reduction strategy?

◮ Consider FalseΩ = (λyz.z)((λx.xx)(λx.xx)) ◮ Outermost reduction:

(λyz.z)((λx.xx)(λx.xx)) → λz.z

◮ Innermost reduction:

(λyz.z)((λx.xx)(λx.xx)) → (λyz.z)((λx.xx)(λx.xx)) → · · ·

slide-21
SLIDE 21

Normal forms . . .

Can a term reduce to more than one normal form, depending on

  • rder reduction strategy?

◮ Consider FalseΩ = (λyz.z)((λx.xx)(λx.xx)) ◮ Outermost reduction:

(λyz.z)((λx.xx)(λx.xx)) → λz.z

◮ Innermost reduction:

(λyz.z)((λx.xx)(λx.xx)) → (λyz.z)((λx.xx)(λx.xx)) → · · ·

◮ Choice of reduction strategies may determine whether a

normal form is reached . . .

slide-22
SLIDE 22

Normal forms . . .

Can a term reduce to more than one normal form, depending on

  • rder reduction strategy?

◮ Consider FalseΩ = (λyz.z)((λx.xx)(λx.xx)) ◮ Outermost reduction:

(λyz.z)((λx.xx)(λx.xx)) → λz.z

◮ Innermost reduction:

(λyz.z)((λx.xx)(λx.xx)) → (λyz.z)((λx.xx)(λx.xx)) → · · ·

◮ Choice of reduction strategies may determine whether a

normal form is reached . . .

◮ . . . but the question is, can more than one normal form be

reached?

slide-23
SLIDE 23

Normal forms . . .

If a term has a normal form, can we always find it?

slide-24
SLIDE 24

Normal forms . . .

If a term has a normal form, can we always find it?

◮ We have seen how to encode recursive functions in λ-calculus

slide-25
SLIDE 25

Normal forms . . .

If a term has a normal form, can we always find it?

◮ We have seen how to encode recursive functions in λ-calculus ◮ Given a recursive function f and an argument n, we cannot

determine, in general, if computation of f (n) terminates

slide-26
SLIDE 26

Normal forms . . .

If a term has a normal form, can we always find it?

◮ We have seen how to encode recursive functions in λ-calculus ◮ Given a recursive function f and an argument n, we cannot

determine, in general, if computation of f (n) terminates

◮ Computing f (n) is equivalent to asking if f n achieves a

normal form

slide-27
SLIDE 27

Normal forms . . .

Can a term reduce to more than one normal form, depending on

  • rder reduction strategy?

◮ Define an equivalence relation ↔ on λ-terms

M ↔ N iff ∃P. P →∗ M, P →∗ N M ↔ N if both M and N can be obtained by reduction from a common “ancestor” P

slide-28
SLIDE 28

Normal forms . . .

Can a term reduce to more than one normal form, depending on

  • rder reduction strategy?

◮ Define an equivalence relation ↔ on λ-terms

M ↔ N iff ∃P. P →∗ M, P →∗ N M ↔ N if both M and N can be obtained by reduction from a common “ancestor” P

◮ ↔ is the symmetric transitive closure of →∗

M →∗ N M ↔ N M ↔ N N ↔ M M ↔ N, N ↔ P M ↔ P

slide-29
SLIDE 29

Normal forms . . .

Can a term reduce to more than one normal form, depending on

  • rder reduction strategy?

◮ Define an equivalence relation ↔ on λ-terms

M ↔ N iff ∃P. P →∗ M, P →∗ N M ↔ N if both M and N can be obtained by reduction from a common “ancestor” P

◮ ↔ is the symmetric transitive closure of →∗

M →∗ N M ↔ N M ↔ N N ↔ M M ↔ N, N ↔ P M ↔ P

◮ In general, for any reflexive, transitive relation R, can define

the symmetric, transitive closure R ↔

slide-30
SLIDE 30

Church-Rosser Theorem

Diamond property or Church-Rosser property

◮ Let R be any reflexive, transitive relation (such as →∗) ◮ R has the diamond property if, whenever X R Y and X R Z

there is W such that Y R W and Z R W

slide-31
SLIDE 31

Church-Rosser Theorem

Diamond property or Church-Rosser property

◮ Let R be any reflexive, transitive relation (such as →∗) ◮ R has the diamond property if, whenever X R Y and X R Z

there is W such that Y R W and Z R W Theorem [Church-Rosser] Let R be Church-Rosser. Then M R ↔ N implies there exists Z, M R Z and N R Z

slide-32
SLIDE 32

Church-Rosser Theorem

Diamond property or Church-Rosser property

◮ Let R be any reflexive, transitive relation (such as →∗) ◮ R has the diamond property if, whenever X R Y and X R Z

there is W such that Y R W and Z R W Theorem [Church-Rosser] Let R be Church-Rosser. Then M R ↔ N implies there exists Z, M R Z and N R Z Proof By induction on the definition of R ↔

slide-33
SLIDE 33

Church-Rosser Theorem

Corollary [Church-Rosser] Let R be a reduction relation that is Church-Rosser. Then a term can have at most one normal form with respect to R

slide-34
SLIDE 34

Church-Rosser Theorem

Corollary [Church-Rosser] Let R be a reduction relation that is Church-Rosser. Then a term can have at most one normal form with respect to R Proof By picture

slide-35
SLIDE 35

Church-Rosser Theorem

Is →∗ Church-Rosser?

slide-36
SLIDE 36

Church-Rosser Theorem

Is →∗ Church-Rosser? Consider (λx.xx)((λx.x)(λx.x))

slide-37
SLIDE 37

Church-Rosser Theorem

Is →∗ Church-Rosser? Consider (λx.xx)((λx.x)(λx.x)) Two possible reductions

◮ (λx.xx)((λx.x)(λx.x)) →

((λx.x)(λx.x))((λx.x)(λx.x)) (Outermost)

◮ (λx.xx)((λx.x)(λx.x)) → ((λx.xx)(λx.x)

(Innermost)

slide-38
SLIDE 38

Church-Rosser Theorem

Is →∗ Church-Rosser? Consider (λx.xx)((λx.x)(λx.x)) Two possible reductions

◮ (λx.xx)((λx.x)(λx.x)) →

((λx.x)(λx.x))((λx.x)(λx.x)) (Outermost)

◮ (λx.xx)((λx.x)(λx.x)) → ((λx.xx)(λx.x)

(Innermost) From second option, in one step we get (λx.xx)(λx.x) → ((λx.x)(λx.x))

slide-39
SLIDE 39

Church-Rosser Theorem

Is →∗ Church-Rosser? Consider (λx.xx)((λx.x)(λx.x)) Two possible reductions

◮ (λx.xx)((λx.x)(λx.x)) →

((λx.x)(λx.x))((λx.x)(λx.x)) (Outermost)

◮ (λx.xx)((λx.x)(λx.x)) → ((λx.xx)(λx.x)

(Innermost) From second option, in one step we get (λx.xx)(λx.x) → ((λx.x)(λx.x)) Can reach this term from the first option as well, but it requires two steps!

slide-40
SLIDE 40

Church-Rosser Theorem

Solution: Define a new notion of one step reduction ։ such that

◮ This new reduction is Church-Rosser. ◮ Its reflexive, transitive closure is equal to →∗.

slide-41
SLIDE 41

Church-Rosser Theorem

Solution: Define a new notion of one step reduction ։ such that

◮ This new reduction is Church-Rosser. ◮ Its reflexive, transitive closure is equal to →∗.

Define ։ as follows. M ։ M M ։ M′ λx.M ։ λx.M′ M ։ M′, N ։ N′ MN ։ M′N′ M ։ M′, N ։ N′ (λx.M)N ։ M′{x ← N′}

◮ ։ combines nonoverlapping → reductions into one parallel

step

slide-42
SLIDE 42

Recursive definitions

Suppose F = λx1x2 . . . xnE, where where E contains an occurrence

  • f F

◮ Choose a new variable f ◮ Convert E to E ∗ replacing every F in E by ff

◮ If E is of the form · · · F · · · F · · · then E ∗ is · · · (ff ) · · · (ff ) · · ·.

Now write G = λfx1x2 . . . xn.E ∗ = λfx1x2 . . . xn. · · · (ff ) · · · (ff ) · · · Then GG = λx1x2 . . . xn. · · · (GG) · · · (GG) · · ·

◮ GG satisfies the equation defining F ◮ Write F = GG, where G = λfx1x2 . . . xn.E ∗.

slide-43
SLIDE 43

Fixed point combinator

◮ Consider recursive definition F = λx.x(Fx)

slide-44
SLIDE 44

Fixed point combinator

◮ Consider recursive definition F = λx.x(Fx) ◮ Can use the GG trick to get a λ-expression of F

F = GG, where G = λfx.x(ffx) = λfx.x(λfx.x(ffx)λfx.x(ffx)x)

slide-45
SLIDE 45

Fixed point combinator

◮ Consider recursive definition F = λx.x(Fx) ◮ Can use the GG trick to get a λ-expression of F

F = GG, where G = λfx.x(ffx) = λfx.x(λfx.x(ffx)λfx.x(ffx)x)

◮ Note that FX = X(FX) for any term X

slide-46
SLIDE 46

Fixed point combinator

◮ Consider recursive definition F = λx.x(Fx) ◮ Can use the GG trick to get a λ-expression of F

F = GG, where G = λfx.x(ffx) = λfx.x(λfx.x(ffx)λfx.x(ffx)x)

◮ Note that FX = X(FX) for any term X ◮ Fixed point : Given Z, M such that ZM = M.

slide-47
SLIDE 47

Fixed point combinator

◮ Consider recursive definition F = λx.x(Fx) ◮ Can use the GG trick to get a λ-expression of F

F = GG, where G = λfx.x(ffx) = λfx.x(λfx.x(ffx)λfx.x(ffx)x)

◮ Note that FX = X(FX) for any term X ◮ Fixed point : Given Z, M such that ZM = M. ◮ F Z is a fixed point for Z

slide-48
SLIDE 48

Fixed point combinator

◮ Consider recursive definition F = λx.x(Fx) ◮ Can use the GG trick to get a λ-expression of F

F = GG, where G = λfx.x(ffx) = λfx.x(λfx.x(ffx)λfx.x(ffx)x)

◮ Note that FX = X(FX) for any term X ◮ Fixed point : Given Z, M such that ZM = M. ◮ F Z is a fixed point for Z ◮ Due to Turing — Θ

slide-49
SLIDE 49

Terms without normal forms

Are all terms without normal forms equally “meaningless”? Can we define an equivalence ≈ on λ-terms such that:

◮ (λxM)N ≈ M{x ← N}—that is, ≈ the equivalence induced

by the β reduction.

slide-50
SLIDE 50

Terms without normal forms

Are all terms without normal forms equally “meaningless”? Can we define an equivalence ≈ on λ-terms such that:

◮ (λxM)N ≈ M{x ← N}—that is, ≈ the equivalence induced

by the β reduction.

◮ If M and N do not have normal forms, then M ≡ N.

slide-51
SLIDE 51

Terms without normal forms

Are all terms without normal forms equally “meaningless”? Can we define an equivalence ≈ on λ-terms such that:

◮ (λxM)N ≈ M{x ← N}—that is, ≈ the equivalence induced

by the β reduction.

◮ If M and N do not have normal forms, then M ≡ N. ◮ Functions that are equated by ≈ yield equivalent results for

the same arguments. That is, if M ≈ N then for all R, MR ≈ NR.

slide-52
SLIDE 52

Terms without normal forms

Consider the function F defined by Fxb = if b then x else (Fxb)

slide-53
SLIDE 53

Terms without normal forms

Consider the function F defined by Fxb = if b then x else (Fxb) If we unravel FF, we get F = GG, where G = λfxb.( if b then x else (ffxb))

slide-54
SLIDE 54

Terms without normal forms

Consider the function F defined by Fxb = if b then x else (Fxb) If we unravel FF, we get F = GG, where G = λfxb.( if b then x else (ffxb)) Consider FXtrue and FXfalse

slide-55
SLIDE 55

Terms without normal forms

Consider the function F defined by Fxb = if b then x else (Fxb) If we unravel FF, we get F = GG, where G = λfxb.( if b then x else (ffxb)) Consider FXtrue and FXfalse

◮ FXtrue → if T then X else (FXtrue) → X.

slide-56
SLIDE 56

Terms without normal forms

Consider the function F defined by Fxb = if b then x else (Fxb) If we unravel FF, we get F = GG, where G = λfxb.( if b then x else (ffxb)) Consider FXtrue and FXfalse

◮ FXtrue → if T then X else (FXtrue) → X. ◮ FXfalse → if F then X else (FXfalse) → FXfalse.

slide-57
SLIDE 57

Terms without normal forms

FZ → (λxb.(if b then x else (Fxb)))Z → λb.(if b then Z else (FZb)) → λb.(if b then Z else G), where G = if b then Z else (fZB) → λb.(if b then Z else (if b then Z else G)) → . . .

slide-58
SLIDE 58

Terms without normal forms

FZ → (λxb.(if b then x else (Fxb)))Z → λb.(if b then Z else (FZb)) → λb.(if b then Z else G), where G = if b then Z else (fZB) → λb.(if b then Z else (if b then Z else G)) → . . .

◮ FZ does not terminate for any Z ⇒ FX ≈ FY for all X, Y

slide-59
SLIDE 59

Terms without normal forms

FZ → (λxb.(if b then x else (Fxb)))Z → λb.(if b then Z else (FZb)) → λb.(if b then Z else G), where G = if b then Z else (fZB) → λb.(if b then Z else (if b then Z else G)) → . . .

◮ FZ does not terminate for any Z ⇒ FX ≈ FY for all X, Y ◮ FX ≈ FY implies FXM ≈ FYM for all M

slide-60
SLIDE 60

Terms without normal forms

FZ → (λxb.(if b then x else (Fxb)))Z → λb.(if b then Z else (FZb)) → λb.(if b then Z else G), where G = if b then Z else (fZB) → λb.(if b then Z else (if b then Z else G)) → . . .

◮ FZ does not terminate for any Z ⇒ FX ≈ FY for all X, Y ◮ FX ≈ FY implies FXM ≈ FYM for all M ◮ FXtrue ≈ FY true

slide-61
SLIDE 61

Terms without normal forms

FZ → (λxb.(if b then x else (Fxb)))Z → λb.(if b then Z else (FZb)) → λb.(if b then Z else G), where G = if b then Z else (fZB) → λb.(if b then Z else (if b then Z else G)) → . . .

◮ FZ does not terminate for any Z ⇒ FX ≈ FY for all X, Y ◮ FX ≈ FY implies FXM ≈ FYM for all M ◮ FXtrue ≈ FY true ◮ FZtrue → Z for all Z, so X ≈ Y for all X and Y !