Lambda Calculus · v1.5.2
2026-10-19 02:45:49
Everything built in this module comes from those two mechanisms alone.
Abstraction: replace a concrete part with a name, supplied later.
A cost for one price becomes a function of price.
The λ calculus keeps the operation and throws away the wrapping.
\[\langle \mathrm{expression} \rangle ::= \langle \mathrm{name} \rangle \mid \langle \mathrm{function} \rangle \mid \langle \mathrm{application} \rangle\]
\[\langle \mathrm{function} \rangle ::= \lambda\langle \mathrm{name} \rangle.\langle \mathrm{body} \rangle\]
\[\lambda x.x\]
\[\lambda \mathrm{first}.\lambda \mathrm{second}.\mathrm{first}\]
\[\lambda f.\lambda a.(f\ a)\]
\[ \begin{align} \langle \mathrm{application} \rangle &::= (\langle \mathrm{function\ expr} \rangle\ \langle \mathrm{argument\ expr} \rangle) \end{align} \]
\[ \begin{align} &(\lambda x.x\ \lambda a.\lambda b.b) \end{align} \]
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.
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\).
β computes; α and η rearrange an expression without changing what it means.
\[\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.
\[\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.
An expression is in normal form when no β reduction applies to it: no function is left applied to an argument.
An expression has a normal form when some sequence of reductions reaches one.
The unsolvability of halting, appearing here in a few characters.
\[\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.
Any sugared form compiles back to λ calculus before evaluation.
\[\mathrm{def}\ \langle \mathrm{name} \rangle = \langle \mathrm{function} \rangle\]
def adds no construct to the language: it is textual abbreviation.
A name is replaced by its definition before evaluation:
rec, so it never finishesRecursion needs a self-reproducing λ expression, not a self-naming definition.
\[(\mathrm{identity2}\ \mathrm{identity}) \Rightarrow \ldots \Rightarrow \mathrm{identity}\]
Any argument reduces back to itself — the effect of identity.
Write every step; when a derivation stalls, check the bracketing 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.
\[ \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.
select_first and the first argument comes backselect_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}\]
Next unit’s true is exactly “select the first argument”.
In \(\lambda x.x\), \(x\) is bound. In \((f\ \lambda x.x)\), \(f\) is free.
In the body of \(\lambda f.(f\ \lambda f.f)\), which is \((f\ \lambda f.f)\):
The distinction is per occurrence, not per name.
\[ \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.
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.
\[\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.
Everything is eventually a β reduction.
Nothing more is added to the language. Everything is built inside it.
Conditions, Booleans and Integers — the next unit in this module.
true and falseNOT, AND, OR, then the natural numbers, built inductively out of pairs