Recursion and Arithmetic

Lambda Calculus · v1.1.1

2026-11-02 12:55:42

Where we are

Scope and prerequisites

  • Unit 3 built truth values, the conditional, the boolean operators, and the natural numbers with succ, pred and iszero
  • Adding \(m\) and \(n\) requires applying succ \(n\) times
  • The calculus has no construct that repeats anything
  • Nothing is added to the language here: three productions, and β, α, η

Outline of key topics

  • Iteration and recursion; primitive and general recursion
  • Why a definition that mentions its own name has no finite expansion
  • Self-application: passing a function to itself
  • Applicative order, and the constraint it imposes
  • The recursion function, and rec notation
  • Arithmetic: power, subtraction, comparison, division

Iteration and recursion

Definition — recursion

  • Iteration repeats by going round again, updating variables — it requires mutable state
  • Recursion repeats by nesting: a call whose answer depends on another call of the same shape, on a smaller problem

Every recursive definition has two parts:

  • a base case that terminates without a further call
  • a recursive case that calls again on something strictly smaller

Both are required for termination.

Primitive and general recursion

  • primitive recursion — the number of repetitions is known
    • finite nesting depth; equivalent to bounded iteration with finite memory
  • general recursion — the number of repetitions is unknown
    • unknown nesting depth; equivalent to unbounded iteration with infinite memory

Primitive recursion is weaker than general recursion.

Why a recursive definition fails

Attempt 1 — add names itself

def add x y =
    if iszero y
    then x
    else add (succ x) (pred y)
add one two
=> add (succ one) (pred two)
=> add (succ (succ one)) (pred (pred two))
=> (succ (succ one))
== three

Incrementing the first and decrementing the second until the second is zero. Reduction appears to work.

Attempt 1 fails — no finite expansion

λx.λy. if iszero y then x else
  ((λx.λy. if iszero y then x else
      add (succ x) (pred y))
   (succ x) (pred y))
== ...
  • unit 2 requires every name to be replaced by its definition before evaluation
  • a name here is an abbreviation, not a reference resolved at call time
  • add reappears at every level, so replacement never terminates

\(\color{#c0392b}\boldsymbol{\times}\) The definition never becomes a λ expression at all.

Passing a function to itself

Delaying through abstraction

A function is used in an application, and the use may be delayed by abstracting at that point:

<function> <argument>
λf.(f <argument>) <function>
  • the two are equivalent
  • the original function has become the argument of a new application

Attempt 2 — add1 takes a function argument

def add1 f x y =
    if iszero y
    then x
    else f (succ x) (pred y)

def add = add1 add1

Reducing add1 add1 gives a body containing:

add1 (succ x) (pred y)
  • add1 appears with two arguments where three are needed
  • nothing corresponds to the bound variable f, so the function is not passed down

\(\color{#c0392b}\boldsymbol{\times}\) The effect required is add1 add1 (succ x) (pred y).

Attempt 3 — add2 passes the function to itself

def add2 f x y =
    if iszero y
    then x
    else f f (succ x) (pred y)

def add = add2 add2
  • the argument for f is passed to the argument itself
  • f f (succ x) (pred y) carries the function down to the next level
  • self-reference has become an ordinary argument

\(\color{#2e7d32}\checkmark\) Correct, but every recursive call must pass the function to itself explicitly.

Applicative order

Definition — applicative order

Applicative order reduces an argument to a value before substituting it into the function body.

  • unit 2’s derivations used normal order: the argument is substituted unevaluated
  • the derivations that follow use applicative order
  • \((\lambda s.(s\ s)\ \lambda s.(s\ s))\) reduces to itself forever

A self-application supplied as an argument is evaluated first, and never finishes.

The recursion function

Definition — mult, recursively

def mult x y =
    if iszero y
    then zero
    else add x (mult x (pred y))

Add the first number to the product of the first and the decremented second; the product is zero when the second is zero.

Abstracting the recursion point

def mult1 f x y =
    if iszero y
    then zero
    else add x (f x (pred y))

def mult = recursive mult1
  • mult1 is mult with the self-reference replaced by a bound variable
  • recursive is a constructor: it builds a recursive function from a non-recursive one

What recursive must satisfy

def recursive f = f <'f' and copy>

Reducing recursive mult1 leaves <'f' and copy> x (pred y) in the body, where mult1 <'f' and copy> x (pred y) is required. So:

<'f' and copy> => ... => f <'f' and copy>
  • the copy mechanism must be an application
  • that application must be self-replicating

Definition — recursive

def recursive f =
    λs.(f (s s)) λs.(f (s s))
  • \(\lambda s.(s\ s)\) self-replicates when applied to itself, without end
  • \(\lambda s.(f\ (s\ s))\) wraps the self-application inside a call to \(f\)
  • each round of replication passes through \(f\), which is the conditional

The base case can stop the reduction before the next self-application is demanded.

rec notation

Definition — rec

rec <name> = <expression>
  • the occurrence of the name is replaced by abstraction
  • the paradoxical combinator is then applied to the whole defining expression
  • known as a fixed point finder, and called \(Y\) in the literature

rec adds nothing: the grammar is still three productions and the conversions β, α, η.

Example — add and mult with rec

rec add x y =
    if iszero y
    then x
    else add
      (succ x)
      (pred y)
rec mult x y =
    if iszero y
    then zero
    else add x
      (mult x
        (pred y))

A recursive reference is left in place when a definition is expanded or evaluated.

Arithmetic from recursion

Definition — power

rec power x y =
    if iszero y
    then one
    else mult x (power x (pred y))
power two three
=> mult two (power two (pred three))
=> mult two (mult two (power two one))
=> ...

Definition — sub

rec sub x y =
    if iszero y
    then x
    else sub (pred x) (pred y)
sub four two
=> sub (pred four) (pred two)
=> pred (pred four)
=> two

Natural subtraction

sub one two
=> sub (pred one) (pred two)
=> pred (pred one)
=> pred zero
=> zero
  • pred returns zero when decrementing zero
  • sub returns zero whenever the second number is larger than the first

This is natural subtraction.

Definition — equal

def abs_diff x y = add (sub x y) (sub y x)
def equal x y = iszero (abs_diff x y)
equal two three
=> iszero (add (sub two three) (sub three two))
=> iszero (add zero one)
=> false
  • equal numbers give zero from both subtractions
  • otherwise one subtraction gives zero and the other the real difference

Definition — equal, recursively

rec equal x y =
    if and (iszero x) (iszero y)
    then true
    else
    if or (iszero x) (iszero y)
    then false
    else equal (pred x) (pred y)
  • both zero: equal; exactly one zero: unequal
  • otherwise decrement both and compare again

Definition — comparison

def greater x y = not (iszero (sub x y))
def greater_or_equal x y = iszero (sub y x)
  • sub x y is non-zero exactly when \(x > y\)
  • sub y x is zero exactly when \(x \geq y\)

Comparison is built from natural subtraction.

Definition — div

rec div1 x y =
    if greater y x
    then zero
    else succ (div1 (sub x y) y)

def div x y =
    if iszero y
    then zero
    else div1 x y
  • div1 counts how often the divisor subtracts from the dividend
  • division by zero is defined to be zero, so a zero divisor must be checked for

Summary

The shape of every operation

  • a conditional testing a base case, and a recursive call on a smaller number
  • the decisions are all at the zero boundary
  • sub, div and pred each return zero rather than an undefined value
  • comparison is built from subtraction, so it follows it

Summary

  • Repetition is recursion: a base case, and a recursive case on a smaller problem
  • A name is an abbreviation, so a self-mentioning definition has no finite expansion
  • Self-application supplies the function to itself as an argument
  • recursive f = λs.(f (s s)) λs.(f (s s)) routes each replication through f
  • rec is sugar over the combinator
  • Power, subtraction, comparison and division follow from recursion