LAMBDA CALCULUS
Computation with nothing but functions
TRUE = λx. λy. x returns its first argument; FALSE = λx. λy. y returns its second. You reduce λy. TRUE y by putting y in place of x inside TRUE, with a substitution that never renames the variables a λ binds. What do you get?
The lambda calculus has three kinds of term: a variable x, a function λx. e and an application e₁ e₂. Computing is one rule, beta reduction: (λx. body) arg becomes body with arg substituted for x. Numbers and booleans are encoded as functions (Church encodings).
The hard part is substitution. If the argument has a free y and you substitute it under a λy, that y now points at the wrong binder. That is capture. The fix is to rename the inner binder first: λy becomes λy1, which changes no meaning.
Terms:
e ::= x | λx. e | e₁ e₂
Beta: (λx. body) arg → body[x := arg]
Capture-avoiding substitution:
x[x := v] = v
y[x := v] = y (y ≠ x)
(e₁ e₂)[x := v] = e₁[x := v] e₂[x := v]
(λx. e)[x := v] = λx. e (x is shadowed)
(λy. e)[x := v] = λy. e[x := v] if y ∉ FV(v) or x ∉ FV(e)
= λz. e[y := z][x := v] otherwise, z fresh
Church encodings:
TRUE = λx. λy. x FALSE = λx. λy. y
ZERO = λf. λx. x ONE = λf. λx. f x
SUCC = λn. λf. λx. f (n f x)
Ω = (λx. x x) (λx. x x) → Ω → Ω … (no normal form) The strip checks two things: no β-step captures a free variable, and the final term matches the reference normal form up to alpha-equivalence. NAIVE SUBSTITUTION fails the capture check on its first β-step and the normal-form check at the end; FIX: ALPHA-RENAME reduces the same term with one rename and lands on TRUE. Ω never reaches a normal form, so the reducer stops at its step limit.
This run reduces λy. TRUE y to TRUE. Switch the reducer to naive substitution at a point where it turns TRUE into FALSE.
GOAL: the final term is not alpha-equivalent to TRUE
Make subst(term, x, value) capture-avoiding: when a binder λy would capture a free variable of value, rename y first, to a name that is free in neither value nor the body. The grader checks each result up to alpha-equivalence, then reduces λy. TRUE y with your function.
Interpreters, macro expanders, compiler inliners and beta-reducers in proof assistants. Hygienic macros, de Bruijn indices and the Barendregt convention (rename everything apart first) are all answers to capture.
“Implement substitution for the lambda calculus” or “what goes wrong when you inline a function?” Show the capture example, give the alpha-renaming rule, and mention de Bruijn indices as the representation that makes capture impossible by construction.