programming language concepts: lecture 17madhavan/courses/pl2009/slides/... · programming language...
TRANSCRIPT
Programming Language Concepts: Lecture 17
Madhavan Mukund
Chennai Mathematical Institute
http://www.cmi.ac.in/~madhavan/courses/pl2009
PLC 2009, Lecture 17, 25 March 2009
Recursive functions
Recursive functions [Godel]
Initial functions
◮ Zero: Z (n) = 0.
◮ Successor: S(n) = n+1.
◮ Projection: Πk
i(n1, n2, . . . , nk) = ni
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))
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)
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
Encoding recursive functions . . .
◮ 〈n〉 ≡ λfx .(f nx).
Encoding recursive functions . . .
◮ 〈n〉 ≡ λfx .(f nx).
◮ Successor 〈succ〉 ≡ λnfx .(f (nfx)) such thatsucc〈n〉 →∗ 〈n + 1〉.
Encoding recursive functions . . .
◮ 〈n〉 ≡ λfx .(f nx).
◮ Successor 〈succ〉 ≡ λnfx .(f (nfx)) such thatsucc〈n〉 →∗ 〈n + 1〉.
◮ Zero 〈Z 〉 ≡ λx .(λgy .y).
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 .
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
Encoding recursive functions . . .
Primitive recursion
◮ Assume f (n+1) is defined in terms of g and h(n, f (n))
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
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
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))))
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))
Recursive functions . . .
Primitive Recursion
◮ We will evaluate t(n) bottom up
◮ Much like dynamic programming for recursive functions
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)
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) . . .
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))
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
Recursive functions . . .
An encoding for pairs
◮ 〈pair〉 ≡ λxyz .(zxy).
◮ 〈fst〉 ≡ λp.(p(λxy .x)).
◮ 〈snd〉 ≡ λp.(p(λxy .y)).
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)
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)
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)
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
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
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)
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)
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)
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
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
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 →β
Back to primitive recursion . . .
f (n+1) is defined in terms of g and h(n, f (n))
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))))
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)
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)).
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)〉)
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)〉)))
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)〉)))
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)〉)
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))〉
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))〉
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)〉.
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〉
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〉
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〉))
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.)
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)
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)
Minimalization
◮ A direct encoding of primitive recursion would require us todefine f (n+1) in terms of f (n)
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
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
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;
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);}
}
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.
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.
Recursive definitions
Suppose F = λx1x2 . . . xnE , where where E contains an occurrenceof F
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 ) · · ·.
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 ) · · ·
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 ) · · ·
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∗.
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);}
}
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
Minimalization
◮ 〈True〉 ≡ λxy .x
◮ 〈False〉 ≡ λxy .y
◮ 〈if − b − then − x − else − y〉 ≡ λbxy . bxy
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
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
Minimalization
◮ Want to define f : Nk → N by minimalization from a
g : Nk+1 → N
◮ Already have an encoding 〈g〉 for g
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
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〉.
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〉
Minimalization
〈iszero〉 = λn.n(λz .〈false〉)〈true〉
Minimalization
〈iszero〉 = λn.n(λz .〈false〉)〈true〉
〈iszero〉 〈0〉
Minimalization
〈iszero〉 = λn.n(λz .〈false〉)〈true〉
〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)
Minimalization
〈iszero〉 = λn.n(λz .〈false〉)〈true〉
〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)→β (λfx .x)(λz .〈false〉)〈true〉
Minimalization
〈iszero〉 = λn.n(λz .〈false〉)〈true〉
〈iszero〉 〈0〉 = (λn.n(λz .〈false〉)〈true〉) (λfx .x)→β (λfx .x)(λz .〈false〉)〈true〉→β (λx .x)〈true〉
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〉
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〉
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)
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〉
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〉
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〉
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〉