Skip to content

Recursion

Advanced Logic Computation

One thing is conspicuously missing: loops. Functions have no names, so nothing can refer to itself — there is no f(...) where f can call f. And yet every real algorithm needs repetition. The fix is the most famous trick in the field.

The divergent term

Start with a paradox. What happens if a function eats itself?

Ω = (λx.x x) (λx.x x) → (λx.x x) (λx.x x) it reproduces itself exactly — forever

Ω β-reduces to itself: an infinite loop in a single line. (The stepper will give up at its safety cap — it never reaches a normal form because there isn't one.)

Fixed points

A fixed point of a function g is a value X with g X = X. Now suppose a magic combinator Y exists with the property:

Y g = g (Y g) Y hands g a copy of "Y g" — so g can invoke itself by calling its argument

Feed Y a function that says "if n is zero, return 1, else multiply n by myself of n−1" — and the argument is the recursive call. Self-reference, built out of nothing but substitution.

The Y combinator

Y = λf.(λx.f (x x)) (λx.f (x x)) Y g → (λx.g (x x)) (λx.g (x x)) → g ((λx.g (x x)) (λx.g (x x))) = g (Y g) there it is — g applied to Y g

Notice the Ω trick inside: x x is self-application again, but this time wrapped in f (…) so each "loop" does one step of real work before recursing. Ω diverges; Y does something each round.

Factorial, in principle

FACT = Y (λf.λn. IF (ISZERO n) ONE (MULT n (f (PRED n)))) the anonymous function receives itself as f — recursion with no name

Every piece on that line is something you built: IF, ISZERO, MULT from Church encodings, PRED (the hard one — it rides on pairs), and Y for the loop. No built-ins. No names. One rule.

What you just saw From three kinds of expression and one rewrite rule: numbers, arithmetic, booleans, logic, pairs, and unbounded recursion — which is all a computer is. That's why the Church–Turing thesis says "computable" means λ-definable, and why a 1936 logic formalism is the skeleton inside every functional language today.

Where next

Practice

(λx.x x) (λx.x x) — the term Ω — does what?

Y g unfolds to ___ (one β-step's worth)

Why does λ-calculus need a combinator for recursion?

Write the Y combinator: Y = ___