Elements of Lambda Calculus

Lambda Calculus · v1.5.2

2026-10-19 02:45:49

Where we are

Scope and prerequisites

  • The previous unit named two mechanisms: abstraction and application
  • That unit motivated; this one defines
  • No numbers, booleans, data structures, conditionals or recursion
  • Only functions, and only application

Everything built in this module comes from those two mechanisms alone.

Outline of key topics

  • Abstraction, as an idea you already use
  • The grammar: three productions, and every expression is one of them
  • The first functions: identity, self-application, function application
  • Notation for naming things
  • Building new functions from old ones; selectors and pairs
  • Free and bound variables, α and η conversion

From motivation to mechanism

The order of work

  1. Abstraction as an idea, and where it already appears
  2. The grammar — three productions
  3. The first functions: identity, self-application, function application
  4. Notation for naming things
  5. Building new functions from old ones, then selectors and pairs
  6. Free and bound variables, α and η conversion

How to read this unit

  • Each example assumes the previous ones
  • These functions are the building blocks of every later unit
  • Work through the reductions by hand, in order

What abstraction is

Definition — abstraction

Abstraction: replace a concrete part with a name, supplied later.

  • notice a part that could vary
  • replace it with a name
  • the result is a function of that name

A cost for one price becomes a function of price.

The same move, in languages you know

  • procedures and functions — abstract over a computation
  • parameters — abstract over the values it works on
  • naming, generally — lets one thing stand for another

The λ calculus keeps the operation and throws away the wrapping.

The grammar of λ expressions

Three productions

\[\langle \mathrm{expression} \rangle ::= \langle \mathrm{name} \rangle \mid \langle \mathrm{function} \rangle \mid \langle \mathrm{application} \rangle\]

  • a name — any sequence of non-blank characters
  • a function — an abstraction over a λ expression
  • an application — specialising an abstraction by supplying a value

Definition — function

\[\langle \mathrm{function} \rangle ::= \lambda\langle \mathrm{name} \rangle.\langle \mathrm{body} \rangle\]

  • the name after λ is the bound variable — like a formal parameter
  • the body may be any λ expression, including another function
  • functions have no names — a definition can sit where it is used

Three functions, three shapes of body

\[\lambda x.x\]

\[\lambda \mathrm{first}.\lambda \mathrm{second}.\mathrm{first}\]

\[\lambda f.\lambda a.(f\ a)\]

  • the body is the bound variable itself
  • the body is another function, so two names are bound in turn
  • the body is an application of one bound name to the other

Definition — application

\[ \begin{align} \langle \mathrm{application} \rangle &::= (\langle \mathrm{function\ expr} \rangle\ \langle \mathrm{argument\ expr} \rangle) \end{align} \]

  • also called a bound pair
  • the function expression is applied to the argument expression

\[ \begin{align} &(\lambda x.x\ \lambda a.\lambda b.b) \end{align} \]

Evaluating an application

Occurrences of the bound variable in the body are replaced by:

Applicative order

the value of the argument

the argument is evaluated before the call, as in C, Java and Python

Normal order

the unevaluated argument

the argument is evaluated only where the body uses it

All applications in this unit are evaluated in normal order.

The three rewrite rules

Let \(x\) and \(y\) be names and \(E\), \(A\) be λ expressions. Write \(E[x \to e]\) for \(E\) with every free occurrence of \(x\) replaced by \(e\).

  • β — \((\lambda x.E\ \ A) \Rightarrow E[x \to A]\)
  • α — \(\lambda x.E \equiv \lambda y.E[x \to y]\)
  • η — \(\lambda x.(E\ x) \equiv E\), when \(x\) does not occur free in \(E\)

β computes; α and η rearrange an expression without changing what it means.

The first three functions

Example 1: identity

\[\lambda x.x\]

Returns whatever argument it is applied to.

\[(\lambda x.{\color{#c0392b}x}\ {\color{#0d6efd}\lambda x.x}) \Rightarrow {\color{#0d6efd}\lambda x.x}\]

Red is the bound variable; blue is the argument that replaces it.

Example 2: self-application

\[\lambda s.(s\ s)\]

Applies its argument to its argument.

\[(\lambda s.(s\ s)\ \lambda s.(s\ s)) \Rightarrow (\lambda s.(s\ s)\ \lambda s.(s\ s))\]

It reduces to itself, forever.

Definition — normal form

An expression is in normal form when no β reduction applies to it: no function is left applied to an argument.

  • \(\lambda x.x\) — in normal form; there is nothing to reduce
  • \((\lambda x.x\ \lambda a.\lambda b.b)\) — not: the application is a redex
  • it reduces to \(\lambda a.\lambda b.b\), which is in normal form

An expression has a normal form when some sequence of reductions reaches one.

No normal form

  • the function is \(\lambda s.(s\ s)\) — its body is \(s\) applied to \(s\)
  • two symbols: no recursion, no loop, no repetition construct
  • applied to itself it reduces to itself, and so computes forever
  • the λ calculus can express computations with no normal form

The unsolvability of halting, appearing here in a few characters.

Example 3: function application

\[\lambda \mathrm{func}.\lambda \mathrm{arg}.(\mathrm{func}\ \mathrm{arg})\]

Returns a function that applies its first argument to its second.

\[ \begin{align} &((\lambda {\color{#c0392b}\mathrm{func}}.\lambda \mathrm{arg}.({\color{#c0392b}\mathrm{func}}\ \mathrm{arg})\ {\color{#0d6efd}\lambda x.x})\ \lambda s.(s\ s)) \\ \Rightarrow\ &({\color{#0d6efd}\lambda x.x}\ \lambda s.(s\ s)) \\ \Rightarrow\ &\lambda s.(s\ s) \end{align} \]

As a value, application can be passed and abstracted over.

Naming and notation

Syntactic sugar

  • New syntax is introduced through substitution rules
  • Applying a rule involves no choices
  • Rules expand to pure λ expressions in finitely many steps

Any sugared form compiles back to λ calculus before evaluation.

Naming functions

\[\mathrm{def}\ \langle \mathrm{name} \rangle = \langle \mathrm{function} \rangle\]

def identity   = λx.x
def self_apply = λs.(s s)
def apply      = λfunc.λarg.(func arg)
  • A name is an abbreviation, expanded where it appears
  • Not a reference resolved at call time

def adds no construct to the language: it is textual abbreviation.

The golden rule — no recursive definitions

A name is replaced by its definition before evaluation:

rec = λx.(rec x)
    = λx.((λx.(rec x)) x)
    = ...
  • every expansion reproduces rec, so it never finishes
  • the name therefore denotes no λ expression

Recursion needs a self-reproducing λ expression, not a self-naming definition.

Functions built from functions

A second identity

def identity2 = λx.((apply identity) x)

\[(\mathrm{identity2}\ \mathrm{identity}) \Rightarrow \ldots \Rightarrow \mathrm{identity}\]

Any argument reduces back to itself — the effect of identity.

What “building” means here

  • There is no way to see the answer in advance
  • Reduce until stuck, then match the result against a known definition
  • An error shows up as an expression that matches nothing

Write every step; when a derivation stalls, check the bracketing first.

Selectors and pairs

Example 4: selecting the first argument

def select_first = λfirst.λsecond.first

\[ \begin{align} &((\mathrm{select\_first}\ {\color{#0d6efd}\mathrm{identity}})\ {\color{#c0392b}\mathrm{apply}}) \\ \Rightarrow\ &(\lambda \mathrm{second}.{\color{#0d6efd}\mathrm{identity}}\ {\color{#c0392b}\mathrm{apply}}) \\ \Rightarrow\ &{\color{#0d6efd}\mathrm{identity}} \end{align} \]

Blue is kept, red is discarded: the body never mentions second.

Example 5: selecting the second argument

def select_second = λfirst.λsecond.second

\[ \begin{align} &((\mathrm{select\_second}\ {\color{#c0392b}\mathrm{identity}})\ {\color{#0d6efd}\mathrm{apply}}) \\ \Rightarrow\ &(\lambda \mathrm{second}.\mathrm{second}\ {\color{#0d6efd}\mathrm{apply}}) \\ \Rightarrow\ &{\color{#0d6efd}\mathrm{apply}} \end{align} \]

The colours swap: first never appears in the body, so red is lost.

Example 6: making a pair

def make_pair = λfirst.λsecond.λfunc.
    ((func first) second)
  • Apply it to select_first and the first argument comes back
  • Apply it to select_second and the second does

\[(\mathrm{make\_pair}\ \mathrm{identity}\ \mathrm{apply}\ \mathrm{select\_first}) \Rightarrow \mathrm{identity}\] \[(\mathrm{make\_pair}\ \mathrm{identity}\ \mathrm{apply}\ \mathrm{select\_second}) \Rightarrow \mathrm{apply}\]

What a pair is

  • A pair is a function, not a container — it takes a selector
  • Storage is replaced by a function that hands back what you ask for
  • Structures nest: a pair of pairs gives triples, lists, trees

Next unit’s true is exactly “select the first argument”.

Free and bound variables

Definition — bound and free

  • bound — an occurrence in the body of the λ that introduces that name
  • unless a nested λ rebinds the name; then it belongs to the inner λ
  • free — every other occurrence

In \(\lambda x.x\), \(x\) is bound. In \((f\ \lambda x.x)\), \(f\) is free.

Example: nested rebinding

In the body of \(\lambda f.(f\ \lambda f.f)\), which is \((f\ \lambda f.f)\):

  • the first \(f\) is free — it is the outer bound variable
  • subsequent \(f\)s are bound, and distinct from it

The distinction is per occurrence, not per name.

Name clashes and α conversion

The clash

def apply = λfunc.λarg.(func arg)

\[ \begin{align} &((\mathrm{apply}\ {\color{#0d6efd}\mathrm{arg}})\ \mathrm{boing}) \\ \Rightarrow\ &(\lambda {\color{#c0392b}\mathrm{arg}}.({\color{#0d6efd}\mathrm{arg}}\ {\color{#c0392b}\mathrm{arg}})\ \mathrm{boing}) \\ \Rightarrow\ &(\mathrm{boing}\ \mathrm{boing}) \quad {\color{#c0392b}\boldsymbol{\times}} \end{align} \]

The free arg in blue was captured by the bound arg in red, so the result is wrong.

The fix — α conversion

Rename the bound variable consistently:

\[ \begin{align} &((\lambda \mathrm{func}.\lambda {\color{#c0392b}\mathrm{arg1}}.(\mathrm{func}\ {\color{#c0392b}\mathrm{arg1}})\ {\color{#0d6efd}\mathrm{arg}})\ \mathrm{boing}) \\ \Rightarrow\ &(\lambda {\color{#c0392b}\mathrm{arg1}}.({\color{#0d6efd}\mathrm{arg}}\ {\color{#c0392b}\mathrm{arg1}})\ \mathrm{boing}) \\ \Rightarrow\ &({\color{#0d6efd}\mathrm{arg}}\ \mathrm{boing}) \quad {\color{#2e7d32}\checkmark} \end{align} \]

A name clash: β reduction puts a free variable in the scope of a bound variable of the same name. α conversion renames to remove it.

A captured variable still reduces — to the wrong thing, silently.

Simplification through η reduction

η reduction

\[\lambda\langle {\color{#c0392b}\mathrm{name}} \rangle.(\langle \mathrm{expression} \rangle\ \langle {\color{#c0392b}\mathrm{name}} \rangle) \quad \equiv \quad \langle \mathrm{expression} \rangle\]

\[ \begin{align} &(\lambda\langle {\color{#c0392b}\mathrm{name}} \rangle.(\langle \mathrm{expression} \rangle\ \langle {\color{#c0392b}\mathrm{name}} \rangle)\ \langle {\color{#0d6efd}\mathrm{argument}} \rangle) \\ \Rightarrow\ &(\langle \mathrm{expression} \rangle\ \langle {\color{#0d6efd}\mathrm{argument}} \rangle) \end{align} \]

The wrapper adds nothing: both give the same result.

Three conversions, three jobs

  • β — substitutes an argument for a bound variable; the only one that computes
  • α — renames a bound variable to prevent capture; changes no meaning
  • η — removes a redundant wrapper; changes no meaning

Everything is eventually a β reduction.

Summary

The complete library

def identity      = λx.x
def self_apply    = λs.(s s)
def apply         = λfunc.λarg.(func arg)
def select_first  = λfirst.λsecond.first
def select_second = λfirst.λsecond.second
def make_pair     = λfirst.λsecond.λfunc.
                    ((func first) second)

Nothing more is added to the language. Everything is built inside it.

Summary

  • Three grammar productions: name, function, application
  • Computation is governed by three conversions: β, α, η
  • Normal and applicative order can disagree on termination
  • Data and control encode as functions — a pair takes a selector
  • Some expressions have no normal form

Where next

Conditions, Booleans and Integers — the next unit in this module.

  • The two selectors built here become true and false
  • NOT, AND, OR, then the natural numbers, built inductively out of pairs