Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language...

82
Programming Language Concepts: Lecture 17 Madhavan Mukund Chennai Mathematical Institute [email protected] http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 17, 25 March 2009

Transcript of Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language...

Page 1: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Programming Language Concepts: Lecture 17

Madhavan Mukund

Chennai Mathematical Institute

[email protected]

http://www.cmi.ac.in/~madhavan/courses/pl2009

PLC 2009, Lecture 17, 25 March 2009

Page 2: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions

Recursive functions [Godel]

Initial functions

◮ Zero: Z (n) = 0.

◮ Successor: S(n) = n+1.

◮ Projection: Πk

i(n1, n2, . . . , nk) = ni

Page 3: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions

Recursive functions [Godel]

Initial functions

◮ Zero: Z (n) = 0.

◮ Successor: S(n) = n+1.

◮ Projection: Πk

i(n1, n2, . . . , nk) = ni

Composition Given f : Nk → N and

g1, g2, . . . , gk : Nh → N,

f ◦ (g1, g2, . . . , gk)(n1, n2, . . . , nh) =

f (g1(n1, n2, . . . , nh), g2(n1, n2, . . . , nh), . . . , gk(n1, n2, . . . , nh))

Page 4: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

Primitive recursion Given g : Nk → N and

h : Nk+2 → N

define f : Nk+1 → N by primitive recursion as follows:

f (0, n1, n2, . . . , nk) = g(n1, n2, . . . , nk)f (n+1, n1, . . . , nk) = h(n, f (n, n1, n2, . . . , nk), n1, . . . , nk)

Page 5: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

Primitive recursion Given g : Nk → N and

h : Nk+2 → N

define f : Nk+1 → N by primitive recursion as follows:

f (0, n1, n2, . . . , nk) = g(n1, n2, . . . , nk)f (n+1, n1, . . . , nk) = h(n, f (n, n1, n2, . . . , nk), n1, . . . , nk)

MinimalizationGiven g : N

k+1 → N, define f : Nk → N by minimalization from g

f (n1, n2, . . . , nk) = µn.(g(n, n1, n2 . . . , nk) = 0)

where µn.P(n) returns the least natural number n such that P(n)holds

Page 6: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Encoding recursive functions . . .

◮ 〈n〉 ≡ λfx .(f nx).

Page 7: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Encoding recursive functions . . .

◮ 〈n〉 ≡ λfx .(f nx).

◮ Successor 〈succ〉 ≡ λnfx .(f (nfx)) such thatsucc〈n〉 →∗ 〈n + 1〉.

Page 8: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Encoding recursive functions . . .

◮ 〈n〉 ≡ λfx .(f nx).

◮ Successor 〈succ〉 ≡ λnfx .(f (nfx)) such thatsucc〈n〉 →∗ 〈n + 1〉.

◮ Zero 〈Z 〉 ≡ λx .(λgy .y).

Page 9: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Encoding recursive functions . . .

◮ 〈n〉 ≡ λfx .(f nx).

◮ Successor 〈succ〉 ≡ λnfx .(f (nfx)) such thatsucc〈n〉 →∗ 〈n + 1〉.

◮ Zero 〈Z 〉 ≡ λx .(λgy .y).

◮ Projection 〈Πk

i〉 ≡ λx1x2 . . . xk .xi .

Page 10: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Encoding recursive functions . . .

◮ 〈n〉 ≡ λfx .(f nx).

◮ Successor 〈succ〉 ≡ λnfx .(f (nfx)) such thatsucc〈n〉 →∗ 〈n + 1〉.

◮ Zero 〈Z 〉 ≡ λx .(λgy .y).

◮ Projection 〈Πk

i〉 ≡ λx1x2 . . . xk .xi .

Composition is easy

Page 11: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Encoding recursive functions . . .

Primitive recursion

◮ Assume f (n+1) is defined in terms of g and h(n, f (n))

Page 12: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Encoding recursive functions . . .

Primitive recursion

◮ Assume f (n+1) is defined in terms of g and h(n, f (n))

◮ Main difficulty is to eliminate recursion◮ λ-calculus functions are anonymous◮ Cannot directly use name of f inside definition of f

Page 13: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Encoding recursive functions . . .

Primitive recursion

◮ Assume f (n+1) is defined in terms of g and h(n, f (n))

◮ Main difficulty is to eliminate recursion◮ λ-calculus functions are anonymous◮ Cannot directly use name of f inside definition of f

◮ Convert recursion into iteration

Page 14: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Encoding recursive functions . . .

Primitive recursion

◮ Assume f (n+1) is defined in terms of g and h(n, f (n))

◮ Main difficulty is to eliminate recursion◮ λ-calculus functions are anonymous◮ Cannot directly use name of f inside definition of f

◮ Convert recursion into iteration

Define t(n) = (n, f (n))

◮ Functions fst and snd extract first and second component of apair

t(0) = (0, f (0)) = (0, g)t(n+1) = (n+1, f (n+1)) = (n+1, h(n, f (n)))

= (succ(fst(t(n))),h(fst(t(n)), snd(t(n))))

Page 15: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Encoding recursive functions . . .

Primitive recursion

◮ Assume f (n+1) is defined in terms of g and h(n, f (n))

◮ Main difficulty is to eliminate recursion◮ λ-calculus functions are anonymous◮ Cannot directly use name of f inside definition of f

◮ Convert recursion into iteration

Define t(n) = (n, f (n))

◮ Functions fst and snd extract first and second component of apair

t(0) = (0, f (0)) = (0, g)t(n+1) = (n+1, f (n+1)) = (n+1, h(n, f (n)))

= (succ(fst(t(n))),h(fst(t(n)), snd(t(n))))

◮ Clearly, f (n) = snd(t(n))

Page 16: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

Primitive Recursion

◮ We will evaluate t(n) bottom up

◮ Much like dynamic programming for recursive functions

Page 17: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

Primitive Recursion

◮ We will evaluate t(n) bottom up

◮ Much like dynamic programming for recursive functions

◮ Define a function step that does the following

step(n, f (n)) = (n+1, f (n+1))

i.e.

step(t(n)) = t(n+1)

Page 18: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

Primitive Recursion

◮ We will evaluate t(n) bottom up

◮ Much like dynamic programming for recursive functions

◮ Define a function step that does the following

step(n, f (n)) = (n+1, f (n+1))

i.e.

step(t(n)) = t(n+1)

◮ So, t(n) = stepn(0, f (0)) = stepn(0, g) . . .

Page 19: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

Primitive Recursion

◮ We will evaluate t(n) bottom up

◮ Much like dynamic programming for recursive functions

◮ Define a function step that does the following

step(n, f (n)) = (n+1, f (n+1))

i.e.

step(t(n)) = t(n+1)

◮ So, t(n) = stepn(0, f (0)) = stepn(0, g) . . .

◮ . . . and f (n) = snd(t(n)) = snd(stepn(0, g))

Page 20: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

Primitive Recursion

◮ We will evaluate t(n) bottom up

◮ Much like dynamic programming for recursive functions

◮ Define a function step that does the following

step(n, f (n)) = (n+1, f (n+1))

i.e.

step(t(n)) = t(n+1)

◮ So, t(n) = stepn(0, f (0)) = stepn(0, g) . . .

◮ . . . and f (n) = snd(t(n)) = snd(stepn(0, g))

◮ Will require constructions for building pairs and decomposingthem using fst and snd

Page 21: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

An encoding for pairs

◮ 〈pair〉 ≡ λxyz .(zxy).

◮ 〈fst〉 ≡ λp.(p(λxy .x)).

◮ 〈snd〉 ≡ λp.(p(λxy .y)).

Page 22: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

An encoding for pairs

◮ 〈pair〉 ≡ λxyz .(zxy).

◮ 〈fst〉 ≡ λp.(p(λxy .x)).

◮ 〈snd〉 ≡ λp.(p(λxy .y)).

Thus

◮ 〈pair〉 a b →∗ λz .zab

◮ 〈fst〉(〈pair 〉ab)

Page 23: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

An encoding for pairs

◮ 〈pair〉 ≡ λxyz .(zxy).

◮ 〈fst〉 ≡ λp.(p(λxy .x)).

◮ 〈snd〉 ≡ λp.(p(λxy .y)).

Thus

◮ 〈pair〉 a b →∗ λz .zab

◮ 〈fst〉(〈pair 〉ab) →β λp.(p(λxy .x))(λz .zab)

Page 24: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

An encoding for pairs

◮ 〈pair〉 ≡ λxyz .(zxy).

◮ 〈fst〉 ≡ λp.(p(λxy .x)).

◮ 〈snd〉 ≡ λp.(p(λxy .y)).

Thus

◮ 〈pair〉 a b →∗ λz .zab

◮ 〈fst〉(〈pair 〉ab) →β λp.(p(λxy .x))(λz .zab)→β (λz .zab)(λxy .x)

Page 25: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

An encoding for pairs

◮ 〈pair〉 ≡ λxyz .(zxy).

◮ 〈fst〉 ≡ λp.(p(λxy .x)).

◮ 〈snd〉 ≡ λp.(p(λxy .y)).

Thus

◮ 〈pair〉 a b →∗ λz .zab

◮ 〈fst〉(〈pair 〉ab) →β λp.(p(λxy .x))(λz .zab)→β (λz .zab)(λxy .x) →β (λxy .x)ab

Page 26: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

An encoding for pairs

◮ 〈pair〉 ≡ λxyz .(zxy).

◮ 〈fst〉 ≡ λp.(p(λxy .x)).

◮ 〈snd〉 ≡ λp.(p(λxy .y)).

Thus

◮ 〈pair〉 a b →∗ λz .zab

◮ 〈fst〉(〈pair 〉ab) →β λp.(p(λxy .x))(λz .zab)→β (λz .zab)(λxy .x) →β (λxy .x)ab →β (λy .a)b

Page 27: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

An encoding for pairs

◮ 〈pair〉 ≡ λxyz .(zxy).

◮ 〈fst〉 ≡ λp.(p(λxy .x)).

◮ 〈snd〉 ≡ λp.(p(λxy .y)).

Thus

◮ 〈pair〉 a b →∗ λz .zab

◮ 〈fst〉(〈pair 〉ab) →β λp.(p(λxy .x))(λz .zab)→β (λz .zab)(λxy .x) →β (λxy .x)ab →β (λy .a)b →β a

◮ 〈snd〉(〈pair〉ab)

Page 28: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

An encoding for pairs

◮ 〈pair〉 ≡ λxyz .(zxy).

◮ 〈fst〉 ≡ λp.(p(λxy .x)).

◮ 〈snd〉 ≡ λp.(p(λxy .y)).

Thus

◮ 〈pair〉 a b →∗ λz .zab

◮ 〈fst〉(〈pair 〉ab) →β λp.(p(λxy .x))(λz .zab)→β (λz .zab)(λxy .x) →β (λxy .x)ab →β (λy .a)b →β a

◮ 〈snd〉(〈pair〉ab) →β λp.(p(λxy .y))(λz .zab)

Page 29: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

An encoding for pairs

◮ 〈pair〉 ≡ λxyz .(zxy).

◮ 〈fst〉 ≡ λp.(p(λxy .x)).

◮ 〈snd〉 ≡ λp.(p(λxy .y)).

Thus

◮ 〈pair〉 a b →∗ λz .zab

◮ 〈fst〉(〈pair 〉ab) →β λp.(p(λxy .x))(λz .zab)→β (λz .zab)(λxy .x) →β (λxy .x)ab →β (λy .a)b →β a

◮ 〈snd〉(〈pair〉ab) →β λp.(p(λxy .y))(λz .zab)→β (λz .zab)(λxy .y)

Page 30: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

An encoding for pairs

◮ 〈pair〉 ≡ λxyz .(zxy).

◮ 〈fst〉 ≡ λp.(p(λxy .x)).

◮ 〈snd〉 ≡ λp.(p(λxy .y)).

Thus

◮ 〈pair〉 a b →∗ λz .zab

◮ 〈fst〉(〈pair 〉ab) →β λp.(p(λxy .x))(λz .zab)→β (λz .zab)(λxy .x) →β (λxy .x)ab →β (λy .a)b →β a

◮ 〈snd〉(〈pair〉ab) →β λp.(p(λxy .y))(λz .zab)→β (λz .zab)(λxy .y) →β (λxy .y)ab

Page 31: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

An encoding for pairs

◮ 〈pair〉 ≡ λxyz .(zxy).

◮ 〈fst〉 ≡ λp.(p(λxy .x)).

◮ 〈snd〉 ≡ λp.(p(λxy .y)).

Thus

◮ 〈pair〉 a b →∗ λz .zab

◮ 〈fst〉(〈pair 〉ab) →β λp.(p(λxy .x))(λz .zab)→β (λz .zab)(λxy .x) →β (λxy .x)ab →β (λy .a)b →β a

◮ 〈snd〉(〈pair〉ab) →β λp.(p(λxy .y))(λz .zab)→β (λz .zab)(λxy .y) →β (λxy .y)ab →β (λy .y)b

Page 32: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

An encoding for pairs

◮ 〈pair〉 ≡ λxyz .(zxy).

◮ 〈fst〉 ≡ λp.(p(λxy .x)).

◮ 〈snd〉 ≡ λp.(p(λxy .y)).

Thus

◮ 〈pair〉 a b →∗ λz .zab

◮ 〈fst〉(〈pair 〉ab) →β λp.(p(λxy .x))(λz .zab)→β (λz .zab)(λxy .x) →β (λxy .x)ab →β (λy .a)b →β a

◮ 〈snd〉(〈pair〉ab) →β λp.(p(λxy .y))(λz .zab)→β (λz .zab)(λxy .y) →β (λxy .y)ab →β (λy .y)b →β

Page 33: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

f (n+1) is defined in terms of g and h(n, f (n))

Page 34: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

f (n+1) is defined in terms of g and h(n, f (n))

t(0) = (0, f (0)) = (0, g)t(n+1) = (n+1, f (n+1)) = (n+1, h(n, f (n)))

= (succ(fst(t(n))),h(fst(t(n)), snd(t(n))))

Page 35: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

f (n+1) is defined in terms of g and h(n, f (n))

t(0) = (0, f (0)) = (0, g)t(n+1) = (n+1, f (n+1)) = (n+1, h(n, f (n)))

= (succ(fst(t(n))),h(fst(t(n)), snd(t(n))))

step(t(n)) = step(n, f (n)) = (n+1, f (n+1)) = t(n+1)

Page 36: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

f (n+1) is defined in terms of g and h(n, f (n))

t(0) = (0, f (0)) = (0, g)t(n+1) = (n+1, f (n+1)) = (n+1, h(n, f (n)))

= (succ(fst(t(n))),h(fst(t(n)), snd(t(n))))

step(t(n)) = step(n, f (n)) = (n+1, f (n+1)) = t(n+1)

Use primitive recursive defn of t(n) to encode step

〈step〉 ≡ λx .〈pair〉 (〈succ〉(〈fst〉x)) (〈h〉 (〈fst〉x) (〈snd〉x)).

Page 37: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

〈step〉 ≡ λx .〈pair〉 (〈succ〉(〈fst〉x)) (〈h〉 (〈fst〉x) (〈snd〉x)).

Check that 〈step〉 (〈pair〉〈n〉〈f (n)〉) →∗ 〈pair〉 〈n+1〉〈f (n+1)〉

〈step〉 (〈pair〉 〈n〉 〈f (n)〉)

Page 38: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

〈step〉 ≡ λx .〈pair〉 (〈succ〉(〈fst〉x)) (〈h〉 (〈fst〉x) (〈snd〉x)).

Check that 〈step〉 (〈pair〉〈n〉〈f (n)〉) →∗ 〈pair〉 〈n+1〉〈f (n+1)〉

〈step〉 (〈pair〉 〈n〉 〈f (n)〉)→ 〈pair〉 (〈succ〉(〈fst〉(〈pair 〉 〈n〉 〈f (n)〉)))

Page 39: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

〈step〉 ≡ λx .〈pair〉 (〈succ〉(〈fst〉x)) (〈h〉 (〈fst〉x) (〈snd〉x)).

Check that 〈step〉 (〈pair〉〈n〉〈f (n)〉) →∗ 〈pair〉 〈n+1〉〈f (n+1)〉

〈step〉 (〈pair〉 〈n〉 〈f (n)〉)→ 〈pair〉 (〈succ〉(〈fst〉(〈pair 〉 〈n〉 〈f (n)〉)))

(〈h〉(〈fst〉(〈pair〉 〈n〉 〈f (n)〉))(〈sec〉(〈pair 〉〈n〉 〈f (n)〉)))

Page 40: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

〈step〉 ≡ λx .〈pair〉 (〈succ〉(〈fst〉x)) (〈h〉 (〈fst〉x) (〈snd〉x)).

Check that 〈step〉 (〈pair〉〈n〉〈f (n)〉) →∗ 〈pair〉 〈n+1〉〈f (n+1)〉

〈step〉 (〈pair〉 〈n〉 〈f (n)〉)→ 〈pair〉 (〈succ〉(〈fst〉(〈pair 〉 〈n〉 〈f (n)〉)))

(〈h〉(〈fst〉(〈pair〉 〈n〉 〈f (n)〉))(〈sec〉(〈pair 〉〈n〉 〈f (n)〉)))→ 〈pair〉 (〈succ〉 〈n〉) (〈h〉〈n〉〈f (n)〉)

Page 41: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

〈step〉 ≡ λx .〈pair〉 (〈succ〉(〈fst〉x)) (〈h〉 (〈fst〉x) (〈snd〉x)).

Check that 〈step〉 (〈pair〉〈n〉〈f (n)〉) →∗ 〈pair〉 〈n+1〉〈f (n+1)〉

〈step〉 (〈pair〉 〈n〉 〈f (n)〉)→ 〈pair〉 (〈succ〉(〈fst〉(〈pair 〉 〈n〉 〈f (n)〉)))

(〈h〉(〈fst〉(〈pair〉 〈n〉 〈f (n)〉))(〈sec〉(〈pair 〉〈n〉 〈f (n)〉)))→ 〈pair〉 (〈succ〉 〈n〉) (〈h〉〈n〉〈f (n)〉)→ 〈pair〉 (〈succ〉 〈n〉) 〈h(n, f (n))〉

Page 42: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

〈step〉 ≡ λx .〈pair〉 (〈succ〉(〈fst〉x)) (〈h〉 (〈fst〉x) (〈snd〉x)).

Check that 〈step〉 (〈pair〉〈n〉〈f (n)〉) →∗ 〈pair〉 〈n+1〉〈f (n+1)〉

〈step〉 (〈pair〉 〈n〉 〈f (n)〉)→ 〈pair〉 (〈succ〉(〈fst〉(〈pair 〉 〈n〉 〈f (n)〉)))

(〈h〉(〈fst〉(〈pair〉 〈n〉 〈f (n)〉))(〈sec〉(〈pair 〉〈n〉 〈f (n)〉)))→ 〈pair〉 (〈succ〉 〈n〉) (〈h〉〈n〉〈f (n)〉)→ 〈pair〉 (〈succ〉 〈n〉) 〈h(n, f (n))〉→ 〈pair〉 〈n+1〉 〈h(n, f (n))〉

Page 43: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

〈step〉 ≡ λx .〈pair〉 (〈succ〉(〈fst〉x)) (〈h〉 (〈fst〉x) (〈snd〉x)).

Check that 〈step〉 (〈pair〉〈n〉〈f (n)〉) →∗ 〈pair〉 〈n+1〉〈f (n+1)〉

〈step〉 (〈pair〉 〈n〉 〈f (n)〉)→ 〈pair〉 (〈succ〉(〈fst〉(〈pair 〉 〈n〉 〈f (n)〉)))

(〈h〉(〈fst〉(〈pair〉 〈n〉 〈f (n)〉))(〈sec〉(〈pair 〉〈n〉 〈f (n)〉)))→ 〈pair〉 (〈succ〉 〈n〉) (〈h〉〈n〉〈f (n)〉)→ 〈pair〉 (〈succ〉 〈n〉) 〈h(n, f (n))〉→ 〈pair〉 〈n+1〉 〈h(n, f (n))〉→ 〈pair〉 〈n+1〉 〈f (n+1)〉.

Page 44: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

Recall that t(n) = stepn(0, g)Hence

〈t〉 ≡ λy .y 〈step〉(〈pair 〉〈0〉〈g〉)

Check that for all n, 〈t〉〈n〉 →∗ pair 〈n〉 〈f (n)〉.

〈t〉〈0〉 → 〈0〉 step (pair 〈0〉 〈g〉) → pair 〈0〉 〈g〉

Page 45: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

Recall that t(n) = stepn(0, g)Hence

〈t〉 ≡ λy .y 〈step〉(〈pair 〉〈0〉〈g〉)

Check that for all n, 〈t〉〈n〉 →∗ pair 〈n〉 〈f (n)〉.

〈t〉〈0〉 → 〈0〉 step (pair 〈0〉 〈g〉) → pair 〈0〉 〈g〉〈t〉〈n+1〉

Page 46: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

Recall that t(n) = stepn(0, g)Hence

〈t〉 ≡ λy .y 〈step〉(〈pair 〉〈0〉〈g〉)

Check that for all n, 〈t〉〈n〉 →∗ pair 〈n〉 〈f (n)〉.

〈t〉〈0〉 → 〈0〉 step (pair 〈0〉 〈g〉) → pair 〈0〉 〈g〉〈t〉〈n+1〉 → 〈n+1〉 step (pair 〈0〉 〈g〉)

→ step (〈n〉 step (pair 〈0〉 〈g〉))

Page 47: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

Recall that t(n) = stepn(0, g)Hence

〈t〉 ≡ λy .y 〈step〉(〈pair 〉〈0〉〈g〉)

Check that for all n, 〈t〉〈n〉 →∗ pair 〈n〉 〈f (n)〉.

〈t〉〈0〉 → 〈0〉 step (pair 〈0〉 〈g〉) → pair 〈0〉 〈g〉〈t〉〈n+1〉 → 〈n+1〉 step (pair 〈0〉 〈g〉)

→ step (〈n〉 step (pair 〈0〉 〈g〉))→ step (pair 〈n〉 〈f (n)〉) (by ind. hyp.)

Page 48: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

Recall that t(n) = stepn(0, g)Hence

〈t〉 ≡ λy .y 〈step〉(〈pair 〉〈0〉〈g〉)

Check that for all n, 〈t〉〈n〉 →∗ pair 〈n〉 〈f (n)〉.

〈t〉〈0〉 → 〈0〉 step (pair 〈0〉 〈g〉) → pair 〈0〉 〈g〉〈t〉〈n+1〉 → 〈n+1〉 step (pair 〈0〉 〈g〉)

→ step (〈n〉 step (pair 〈0〉 〈g〉))→ step (pair 〈n〉 〈f (n)〉) (by ind. hyp.)→ pair 〈n+1〉 〈f (n+1)〉 (by what has been proved above)

Page 49: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Back to primitive recursion . . .

Clearly f (n) = snd(t(n))

〈f 〉 ≡ λy .〈snd〉(〈t〉y)

Collapsing all the steps we have seen above, we have an expression〈PR〉 that constructs a primitive recursive definition from thefunctions g and h:

〈PR〉 ≡ λhgy .〈snd〉(y (λx .〈pair 〉 (〈succ〉 (〈fst〉 x))(h(〈fst〉 x)) (〈snd〉 x)))(〈pair 〉 〈0〉 g)

Page 50: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

◮ A direct encoding of primitive recursion would require us todefine f (n+1) in terms of f (n)

Page 51: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

◮ A direct encoding of primitive recursion would require us todefine f (n+1) in terms of f (n)

◮ Going via t(n) and step avoided such a recursive definition

Page 52: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

◮ A direct encoding of primitive recursion would require us todefine f (n+1) in terms of f (n)

◮ Going via t(n) and step avoided such a recursive definition

◮ Recursive definitions arise again in our translation ofminimilation

Page 53: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

Minimalization

◮ To evaluate

f (n1, n2, . . . , nk) = µn.(g(n, n1, n2 . . . , nk) = 0)

we go back to the idea of computing a while loop

n := 0;

while (g(n,n1,n2,...,nk) != 0) {n := n+1};

return n;

Page 54: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

Minimalization

◮ To evaluate

f (n1, n2, . . . , nk) = µn.(g(n, n1, n2 . . . , nk) = 0)

we go back to the idea of computing a while loop

n := 0;

while (g(n,n1,n2,...,nk) != 0) {n := n+1};

return n;

◮ Implement the while loop using recursion

f(n1,n2,...,nk) = check(0,n1,n2...nk)

where

check(n,n1,n2...nk){

if (iszero(g(n,n1,n2,...,nk)) {return n;}

else {check(n+1,n1,n2,...,nk);}

}

Page 55: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

Minimalization

◮ To evaluate

f (n1, n2, . . . , nk) = µn.(g(n, n1, n2 . . . , nk) = 0)

we go back to the idea of computing a while loop

n := 0;

while (g(n,n1,n2,...,nk) != 0) {n := n+1};

return n;

◮ Implement the while loop using recursion

f(n1,n2,...,nk) = check(0,n1,n2...nk)

where

check(n,n1,n2...nk){

if (iszero(g(n,n1,n2,...,nk)) {return n;}

else {check(n+1,n1,n2,...,nk);}

}

◮ Need a mechanism to encode booleans, if-then-else inλ-calculus.

Page 56: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive functions . . .

Minimalization

◮ To evaluate

f (n1, n2, . . . , nk) = µn.(g(n, n1, n2 . . . , nk) = 0)

we go back to the idea of computing a while loop

n := 0;

while (g(n,n1,n2,...,nk) != 0) {n := n+1};

return n;

◮ Implement the while loop using recursion

f(n1,n2,...,nk) = check(0,n1,n2...nk)

where

check(n,n1,n2...nk){

if (iszero(g(n,n1,n2,...,nk)) {return n;}

else {check(n+1,n1,n2,...,nk);}

}

◮ Need a mechanism to encode booleans, if-then-else inλ-calculus. Also need a mechanism for recursion.

Page 57: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive definitions

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

Page 58: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive definitions

Suppose F = λx1x2 . . . xnE , where where E contains an occurrenceof 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 ) · · ·.

Page 59: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive definitions

Suppose F = λx1x2 . . . xnE , where where E contains an occurrenceof 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 ) · · ·

Page 60: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive definitions

Suppose F = λx1x2 . . . xnE , where where E contains an occurrenceof 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 ) · · ·

ThenGG = λx1x2 . . . xn. · · · (GG ) · · · (GG ) · · ·

Page 61: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Recursive definitions

Suppose F = λx1x2 . . . xnE , where where E contains an occurrenceof 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 ) · · ·

ThenGG = λx1x2 . . . xn. · · · (GG ) · · · (GG ) · · ·

◮ GG satisfies the equation defining F

◮ Write F = GG , where G = λfx1x2 . . . xn.E∗.

Page 62: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

f(n1,n2,...,nk) = check(0,n1,n2...nk)

where

check(n,n1,n2...nk){

if (iszero(g(n,n1,n2,...,nk)) {return n;}

else {check(n+1,n1,n2,...,nk);}

}

Page 63: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

f(n1,n2,...,nk) = check(0,n1,n2...nk)

where

check(n,n1,n2...nk){

if (iszero(g(n,n1,n2,...,nk)) {return n;}

else {check(n+1,n1,n2,...,nk);}

}

Encoding Booleans

◮ 〈True〉 ≡ λxy .x

◮ 〈False〉 ≡ λxy .y

◮ 〈if − b − then − x − else − y〉 ≡ λbxy . bxy

Page 64: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

◮ 〈True〉 ≡ λxy .x

◮ 〈False〉 ≡ λxy .y

◮ 〈if − b − then − x − else − y〉 ≡ λbxy . bxy

Page 65: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

◮ 〈True〉 ≡ λxy .x

◮ 〈False〉 ≡ λxy .y

◮ 〈if − b − then − x − else − y〉 ≡ λbxy . bxy

(λbxy .bxy)〈True〉fg → λxy .((λxy .x)xy)fg→ λy .((λxy .x)fy)g→ (λxy .x)fg→ (λy .f )g→ f

Page 66: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

◮ 〈True〉 ≡ λxy .x

◮ 〈False〉 ≡ λxy .y

◮ 〈if − b − then − x − else − y〉 ≡ λbxy . bxy

(λbxy .bxy)〈True〉fg → λxy .((λxy .x)xy)fg→ λy .((λxy .x)fy)g→ (λxy .x)fg→ (λy .f )g→ f

(λbxy .bxy)〈False〉fg → λxy .((λxy .y)xy)fg→ λy .((λxy .y)fy)g→ (λxy .y)fg→ (λy .y)g→ g

Page 67: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

◮ Want to define f : Nk → N by minimalization from a

g : Nk+1 → N

◮ Already have an encoding 〈g〉 for g

Page 68: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

◮ Want to define f : Nk → N by minimalization from a

g : Nk+1 → N

◮ Already have an encoding 〈g〉 for g

Define F as follows:

F = λnx1x2 . . . xk . if 〈iszero〉(〈g〉 n x1 x2 . . . xk)then n

else F (〈succ〉n) x1 x2 . . . xk

Page 69: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

◮ Want to define f : Nk → N by minimalization from a

g : Nk+1 → N

◮ Already have an encoding 〈g〉 for g

Define F as follows:

F = λnx1x2 . . . xk . if 〈iszero〉(〈g〉 n x1 x2 . . . xk)then n

else F (〈succ〉n) x1 x2 . . . xk

◮ F : the lambda term for F after unravelling the recursivedefinition

◮ 〈f 〉 is then F 〈0〉.

Page 70: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

◮ Want to define f : Nk → N by minimalization from a

g : Nk+1 → N

◮ Already have an encoding 〈g〉 for g

Define F as follows:

F = λnx1x2 . . . xk . if 〈iszero〉(〈g〉 n x1 x2 . . . xk)then n

else F (〈succ〉n) x1 x2 . . . xk

◮ F : the lambda term for F after unravelling the recursivedefinition

◮ 〈f 〉 is then F 〈0〉.

Still need to define 〈iszero〉

Page 71: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

〈iszero〉 = λn.n(λz .〈false〉)〈true〉

Page 72: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

〈iszero〉 = λn.n(λz .〈false〉)〈true〉

〈iszero〉 〈0〉

Page 73: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

〈iszero〉 = λn.n(λz .〈false〉)〈true〉

〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)

Page 74: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

〈iszero〉 = λn.n(λz .〈false〉)〈true〉

〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)→β (λfx .x)(λz .〈false〉)〈true〉

Page 75: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

〈iszero〉 = λn.n(λz .〈false〉)〈true〉

〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)→β (λfx .x)(λz .〈false〉)〈true〉→β (λx .x)〈true〉

Page 76: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

〈iszero〉 = λn.n(λz .〈false〉)〈true〉

〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)→β (λfx .x)(λz .〈false〉)〈true〉→β (λx .x)〈true〉→β 〈true〉

Page 77: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

〈iszero〉 = λn.n(λz .〈false〉)〈true〉

〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)→β (λfx .x)(λz .〈false〉)〈true〉→β (λx .x)〈true〉→β 〈true〉

〈iszero〉 〈1〉

Page 78: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

〈iszero〉 = λn.n(λz .〈false〉)〈true〉

〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)→β (λfx .x)(λz .〈false〉)〈true〉→β (λx .x)〈true〉→β 〈true〉

〈iszero〉 〈1〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .fx)

Page 79: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

〈iszero〉 = λn.n(λz .〈false〉)〈true〉

〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)→β (λfx .x)(λz .〈false〉)〈true〉→β (λx .x)〈true〉→β 〈true〉

〈iszero〉 〈1〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .fx)→β (λfx .fx)(λz .〈false〉)〈true〉

Page 80: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

〈iszero〉 = λn.n(λz .〈false〉)〈true〉

〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)→β (λfx .x)(λz .〈false〉)〈true〉→β (λx .x)〈true〉→β 〈true〉

〈iszero〉 〈1〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .fx)→β (λfx .fx)(λz .〈false〉)〈true〉→β (λx .(λz .〈false〉) x)〈true〉

Page 81: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

〈iszero〉 = λn.n(λz .〈false〉)〈true〉

〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)→β (λfx .x)(λz .〈false〉)〈true〉→β (λx .x)〈true〉→β 〈true〉

〈iszero〉 〈1〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .fx)→β (λfx .fx)(λz .〈false〉)〈true〉→β (λx .(λz .〈false〉) x)〈true〉→β (λz .〈false〉) 〈true〉

Page 82: Programming Language Concepts: Lecture 17madhavan/courses/pl2009/slides/... · Programming Language Concepts: Lecture 17 ... λ-calculus functions are anonymous ... Primitive recursion

Minimalization

〈iszero〉 = λn.n(λz .〈false〉)〈true〉

〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)→β (λfx .x)(λz .〈false〉)〈true〉→β (λx .x)〈true〉→β 〈true〉

〈iszero〉 〈1〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .fx)→β (λfx .fx)(λz .〈false〉)〈true〉→β (λx .(λz .〈false〉) x)〈true〉→β (λz .〈false〉) 〈true〉→β 〈false〉

By induction, for n > 0 . . .

〈iszero〉 〈n〉 →∗

β (λz .〈false〉)n 〈true〉 →∗

β 〈false〉