LAMBDA CALCULUS

Computation with nothing but functions

L215 MINMATH & LOGIC
PREDICT

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.

▶ SIMULATION
term—
✓ no β-step captures a free variable of its argument✓ final term ≡α the reference normal form
SCHEDULE
01[EVAL]capture-avoiding substitution
02[EVAL]load SUCC ONE
03[EVAL]β-step
04[EVAL]β-step
05[EVAL]β-step (or report normal form)
06[CHECK]compare with the reference normal form
Start of beta: succ one scenario. 6 steps.
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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

the lambda calculus is substitution and nothing else, and substitution is only correct if it respects scope. A bound variable’s name is an implementation detail; a free variable’s name is its identity. Rename binders when they would steal a free variable, and compare terms up to alpha-equivalence, never by spelling.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: TYPE INFERENCE →