Exercise 5.13 (Scott 1981, PRG-19, Lecture V) — a one-one pairing `num : N × N → #
N`
Prove the existence of a one-one function
num : ℕ × ℕ → ℕsuch that
num(0, 0) = 0,num(n, m+1) = num(n+1, m) + 1,num(n+1, 0) = num(0, n) + 1.Draw a picture (an infinite matrix) for the function and find a closed form for its values. Use the function to prove the isomorphism of the domains
P N,P(N × N),P N × P N.
The picture #
Reading n down the rows and m across the columns, the three recurrences walk
the anti-diagonals
n + m = const, climbing one step (+1) each move and jumping to the start of
the next diagonal at
the left edge:
m=0 m=1 m=2 m=3
n=0 0 2 5 9
n=1 1 4 8
n=2 3 7
n=3 6
The closed form #
On the anti-diagonal s = n + m the values run T(s), T(s)+1, …, T(s)+s where
T(s) = s(s+1)/2
is the s-th triangular number; the offset within the diagonal is exactly m.
Hence
num n m = (n + m) * (n + m + 1) / 2 + m
— the Cantor pairing function. We take this as the definition (num), verify
Scott's three
recurrences (num_zero_zero, num_succ_right, num_succ_left), and prove it is
one-one
(num_injective). In fact it is a bijection: we build a choice-free inverse
unnum (iterate the
diagonal walk nextCell from (0,0)) and package the bijection as numEquiv : ℕ × ℕ ≃ ℕ.
The domain isomorphisms #
Following Exercise 4.17, the power-set domain P A is modelled by the complete
lattice (Set A, ⊆).
Order-isomorphisms of these domains are then induced by bijections of the index
types
(setCongr). Since ℕ × ℕ ≃ ℕ (via numEquiv) and ℕ ⊕ ℕ ≃ ℕ (Mathlib's
Equiv.natSumNatEquivNat), all three domains are isomorphic:
P N ≅ P(N × N) (pnOrderIsoPNN),
P N ≅ P N × P N (pnOrderIsoProd),
P(N × N) ≅ P N × P N (pnnOrderIsoProd).
Everything (including numEquiv and the order-isomorphisms) is choice-free
(#print axioms ⊆ {propext, Quot.sound}).
Triangular numbers #
The k-th triangular number T(k) = k(k+1)/2.
Equations
- Domain.Neighborhood.Exercise513.tri k = k * (k + 1) / 2
Instances For
The pairing function num n m = (n+m)(n+m+1)/2 + m (Cantor's diagonal
enumeration).
Equations
Instances For
Its uncurried form, the actual N × N → N of the exercise.
Equations
Instances For
The function is one-one. The diagonal s = n+m is recovered as the unique
s with
T(s) ≤ num n m < T(s+1); then m is the offset and n = s - m.
The inverse: walking the diagonals #
One step of the diagonal walk: the cell holding num c + 1. Moving up-right
within a diagonal,
or to the start of the next diagonal at the top edge.
Equations
Instances For
The domain isomorphisms #
The power-set domain P A is the complete lattice (Set A, ⊆) (Exercise 4.17). A
bijection of
index types lifts to an order-isomorphism of power-set domains.
P N ≅ P N × P N — N ≅ N ⊕ N together with P(A ⊕ B) ≅ P A × P B
(Set.sumEquiv).
Equations
Instances For
P(N × N) ≅ P N × P N — by composing the two isomorphisms above.