This page explains how booleans and pairs can be represented using only functions. To make the ideas intuitive, we first compare them with how Python handles the same concepts.
The λ‑calculus is a tiny mathematical model of computation. It has only:
x, yλx. MThe main evaluation rule is:
(λx. M) N → M with x replaced by N
Unlike Python, the λ‑calculus has no built‑in booleans, numbers, lists, or pairs. So we must build everything using functions.
True False
if condition:
X
else:
Y
pair = (10, 20) first = pair[0] second = pair[1]
Python has built‑in data types. The λ‑calculus does not — so we must encode these ideas using functions.
To understand the encoding, think about what booleans do in Python.
if True:
print("A")
else:
print("B")
This prints A — the first option.
So in the λ‑calculus, we represent booleans as functions that choose between two values.
true ≡ λa b. a ; choose the first false ≡ λa b. b ; choose the second
This means:
true X Y → X false X Y → Y
Now the definition of if becomes natural.
An if‑statement simply applies the boolean to the two branches:
if ≡ λp x y. p x y
Here:
p is a boolean function (true or false)x is the “then” valuey is the “else” valueif true 10 20 → true 10 20 → 10
A pair in Python is a container holding two values:
(A, B)
But the λ‑calculus has no containers. So we ask: what does a pair actually do?
A pair gives two values to something that wants them.
So we encode a pair as a function that takes a “selector” function.
pair ≡ λx y f. f x y
This means:
x and yf, it calls f x yfst ≡ λp. p (λa b. a) snd ≡ λp. p (λa b. b)
These work because:
λa b. a returns the first argumentλa b. b returns the second argumentLet’s store two numbers:
myPair = pair 5 8
Extract the first value:
fst myPair → 5
Extract the second value:
snd myPair → 8