Chapter 5: Fixed Points and Recursion
Fixed Points
In regular mathematics, a fixed point x0 of a function f is a point that maps back to itself, i.e.
f(x0)=x0
In regular mathematics, not all functions have a fixed point. But in lambda calculus, everything is a function and has a fixed point.
Fixed-point Combinators
How to solve fixed points of a function? It may surprise you that there is a kind of functions called fixed-point combinators capable to generate fixed points from arbitrary function by applying it with the function. I will introduce two of them.
Curry’s Paradoxical Combinator:Turing Combinator:YΘ==λf.(λx.f(xx))(λx.f(xx))(λxy.y(xxy))(λxy.y(xxy))
For arbitrary function F, it has two fixed points (YF) and (ΘF), i. e. F(YF)=YF and F(ΘF)=ΘF.
Proof:
YF====(λf.(λx.f(xx))(λx.f(xx)))F(λx.F(xx))(λx.F(xx))F((λx.F(xx))(λx.F(xx)))F(YF)ΘF===(λxy.y(xxy))(λxy.y(xxy))FF((λxy.y(xxy))(λxy.y(xxy))F)F(ΘF)
Infinity
What if I apply fixed-point combinator with succussor function S?
YS=S(YS)=S(S(YS))=⋯=S(S(S(⋯)))=∞
Let's call it infinity. And it still meet requirement of a fixed point, since
S(∞)=∞
is reasonable and acceptable.
Recursion
To implement certain algorithms, recursion is often needed. Let's take factorial for example:
factorial=λn.(is-zero n)1(mul n(factorial(pred n)))
Since functions are anonymous, the name of the function is not assigned until the full definition of the function, So factorial is actually used before its full definition, which is a hideous logical defect! Don't be panic, let's take advantages of fixed points!
Take an inverse β reduction on the right:
factorial=(λf.λn.(is-zero n)1(mul n(f(pred n))))factorial
Observe this carefully. It states that factorial is a fixed point of
(λf.λn.(is-zero n)1(mul n(f(pred n))))
Which means, the definition of factorial could be rewritten as:
factorial=Θ(λf.λn.(is-zero n)1(mul n(f(pred n))))
Perhaps you did never associate fixed points and recursion together before this fantastic journey. Mathematical consequence may be far beyond our imagination.
Division, Remainder and Logarithm
With the help of recursion, the decrement of church numerals speeds up.
div=Θ(λf.λmn.(lt mn)0(add 1(f(sub mn)n)))rem=Θ(λf.λmn.(lt mn)m(f(sub mn)n))log=Θ(λf.λmn.(lt mn)0(add 1(f(div mn)n)))
Recursive Algorithms and Data Structures
Recursive algorithms work well with recursive data structures such as linked list. A typical reduction function could be defined as:
reduce=Θ(λf.λlr.(is-nil (pop-front l))(get-front l)(r(get-front l)(f(pop-front l)r)))
With l=(1,3,5), you could get:
reduce ladd=9reduce lmul=15
With the powerful weapon of recursion, now it is not difficult to implement arbitrary computational task via lambda calculus.