Documentation

CslibFrontier.Computability.Catalytic.Comparison

Catalytic equality tests #

Two equality tests that borrow memory with arbitrary contents, use it, and give it back exactly. addSqDist and keyTest each visit their two operands alternately, four times in all: they see only one operand at a time, so the operands never have to be stored together. Between visits they keep only their phase and the value they are accumulating. Arbitrary initial contents of the borrowed memory cancel, because everything added to it is subtracted again and its contributions to the result cancel in pairs. The equality test eqTest runs addSqDist twice, so it makes eight visits.

This file proves what the tests compute and that they restore the borrowed memory. It has no machine model, and no theorem bounds the number of visits or the storage.

The registers are those of the Cook–Mertz procedure (Cslib.TreeEval.proc): a register file S : ι → Fin ℓ → R, whose registers hold ℓ ring elements and are updated with Function.update. A test borrows one register r of the file, for instance a register of Cslib.TreeEval.State (the case ι = Fin 3 over a field). As there, a call carries a coefficient c, and the call with -c undoes it.

The sum-of-squares test. addSqDist c r x y adds c · ⟨x - y, x - y⟩ = c · ∑ᵢ (xᵢ - yᵢ)² to an accumulator z, whatever the contents of S and z, and leaves S unchanged (addSqDist_eq). Visiting x masks register r with x and adds c ⟨x, x⟩; visiting y adds c (⟨y, y⟩ - 2 ⟨S r, y⟩); visiting x again unmasks r; visiting y again adds 2c ⟨S r, y⟩. The two inner products with y see S r + x and S r, so the unknown contents of S r cancel and -2c ⟨x, y⟩ remains. eqTest runs this with c = 1, observes whether the accumulator changed, and runs it with c = -1, which restores the registers and the accumulator (eqTest_snd).

This four-visit test follows the construction in OpenAI's Lean formalization accompanying [OAI26e], the file CatalyticComparison.lean at revision 7ef5c0cc: its fair (composed of first, second, pointR and pointZ) is addSqDist with the same split of terms across the visits, and its lemmas pupils, back, test_eye, paw and small correspond to addSqDist_eq, addSqDist_neg_addSqDist (with eqTest_snd), eqTest_fst_iff_sqDist (with eqTest_eq) and natCast_sqDist_eq_zero_iff. The construction is re-proved independently here: registers are vectors over a commutative ring with the dot product, where that file uses digit-encoded natural numbers, and the names, statements and proofs are our own.

The flag reports whether ⟨x - y, x - y⟩ = 0 (eqTest_fst_iff_sqDist), so it is decide (x = y) when the squared distance detects equality (eqTest_eq). Over ℤ it always does (eqTest_eq_int). Over ZMod m it can fail by wraparound: in ZMod 5, 1² + 2² = 0. It holds for vectors of naturals below b when ℓ · (b - 1)² < m (natCast_sqDist_eq_zero_iff, eqTest_eq_natCast).

Cost. eqTest compares the accumulator with its initial value, so a realization keeps that value or starts the accumulator at zero. With k = size ℓ + 2 · size b bits, that is over ZMod (2 ^ k), the test is exact for ℓ digits below b (mul_sq_lt_two_pow_size, eqTest_eq_two_pow): the clean storage is this k-bit accumulator, a copy of its initial value (unnecessary if it starts at zero), a phase among eight and the flag, and the borrowed register of ℓ elements of ZMod (2 ^ k) is restored.

The key test. keyTest c a b compares two indices a b : κ with a borrowed table V : κ → M: it adds c to V a, adds V b to a result, subtracts c from V a and subtracts V b from the result. The table is restored and the result gains c if a = b and 0 otherwise (keyTest_eq). Over ZMod 2 with c = 1 this is the exclusive-or test of [OAI26e]. Here the clean storage is the phase and the result.

Main definitions #

Main results #

References #

def Cslib.Catalytic.addSqDist {R : Type u_1} {ι : Type u_2} [CommRing R] [DecidableEq ι] {ℓ : ℕ} (c : R) (r : ι) (x y : Fin ℓ → R) (T : (ι → Fin ℓ → R) × R) :
(ι → Fin ℓ → R) × R

Add c · ⟨x - y, x - y⟩ to the accumulator, borrowing register r of the register file. The four visits see x, y, x, y in turn.

theorem Cslib.Catalytic.addSqDist_eq {R : Type u_1} {ι : Type u_2} [CommRing R] [DecidableEq ι] {ℓ : ℕ} (c : R) (r : ι) (x y : Fin ℓ → R) (T : (ι → Fin ℓ → R) × R) :
addSqDist c r x y T = (T.1, T.2 + c * (x - y) ⬝ᵥ (x - y))

The sum-of-squares test adds the squared distance. Whatever the contents of the registers and the accumulator, the accumulator gains c · ⟨x - y, x - y⟩ and the registers are unchanged.

theorem Cslib.Catalytic.addSqDist_neg_addSqDist {R : Type u_1} {ι : Type u_2} [CommRing R] [DecidableEq ι] {ℓ : ℕ} (c : R) (r : ι) (x y : Fin ℓ → R) (T : (ι → Fin ℓ → R) × R) :
addSqDist (-c) r x y (addSqDist c r x y T) = T

The call with -c undoes the call with c.

def Cslib.Catalytic.eqTest {R : Type u_1} {ι : Type u_2} [CommRing R] [DecidableEq ι] {ℓ : ℕ} [DecidableEq R] (r : ι) (x y : Fin ℓ → R) (T : (ι → Fin ℓ → R) × R) :
Bool × (ι → Fin ℓ → R) × R

The restoring equality test. Add the squared distance of x and y to the accumulator, observe whether the accumulator changed, and subtract the squared distance again. The result is the flag together with the registers and the accumulator.

@[simp]
theorem Cslib.Catalytic.eqTest_snd {R : Type u_1} {ι : Type u_2} [CommRing R] [DecidableEq ι] {ℓ : ℕ} [DecidableEq R] (r : ι) (x y : Fin ℓ → R) (T : (ι → Fin ℓ → R) × R) :
(eqTest r x y T).2 = T

The equality test restores the registers and the accumulator.

theorem Cslib.Catalytic.eqTest_fst_iff_sqDist {R : Type u_1} {ι : Type u_2} [CommRing R] [DecidableEq ι] {ℓ : ℕ} [DecidableEq R] (r : ι) (x y : Fin ℓ → R) (T : (ι → Fin ℓ → R) × R) :
(eqTest r x y T).1 = true ↔ (x - y) ⬝ᵥ (x - y) = 0

The flag of the equality test reports whether the squared distance vanishes.

theorem Cslib.Catalytic.eqTest_eq {R : Type u_1} {ι : Type u_2} [CommRing R] [DecidableEq ι] {ℓ : ℕ} [DecidableEq R] {x y : Fin ℓ → R} (h : (x - y) ⬝ᵥ (x - y) = 0 ↔ x = y) (r : ι) (T : (ι → Fin ℓ → R) × R) :
eqTest r x y T = (decide (x = y), T)

The restoring equality test is exact whenever the squared distance detects equality: for any contents of the registers and the accumulator, the flag is decide (x = y) and the registers and the accumulator are restored.

theorem Cslib.Catalytic.eqTest_eq_int {ι : Type u_1} [DecidableEq ι] {ℓ : ℕ} (r : ι) (x y : Fin ℓ → ℤ) (T : (ι → Fin ℓ → ℤ) × ℤ) :
eqTest r x y T = (decide (x = y), T)

Over ℤ the restoring equality test is exact.

theorem Cslib.Catalytic.natCast_sqDist_eq_zero_iff {ℓ m b : ℕ} {x y : Fin ℓ → ℕ} (hx : ∀ (i : Fin ℓ), x i < b) (hy : ∀ (i : Fin ℓ), y i < b) (hm : ℓ * (b - 1) ^ 2 < m) :

No wraparound for small digits. For vectors of naturals below b, the squared distance of their images in ZMod m vanishes only if they are equal, provided ℓ · (b - 1)² < m.

theorem Cslib.Catalytic.eqTest_eq_natCast {ℓ m b : ℕ} {ι : Type u_1} [DecidableEq ι] {x y : Fin ℓ → ℕ} (hx : ∀ (i : Fin ℓ), x i < b) (hy : ∀ (i : Fin ℓ), y i < b) (hm : ℓ * (b - 1) ^ 2 < m) (r : ι) (T : (ι → Fin ℓ → ZMod m) × ZMod m) :
eqTest r (Nat.cast ∘ x) (Nat.cast ∘ y) T = (decide (x = y), T)

Over ZMod m, the restoring equality test of digit vectors below b is exact when ℓ · (b - 1)² < m.

theorem Cslib.Catalytic.mul_sq_lt_two_pow_size (ℓ b : ℕ) :
ℓ * (b - 1) ^ 2 < 2 ^ (ℓ.size + 2 * b.size)

Accumulator size. The squared distance of ℓ digits below b fits in size ℓ + 2 · size b bits.

theorem Cslib.Catalytic.eqTest_eq_two_pow {ℓ b : ℕ} {ι : Type u_1} [DecidableEq ι] {x y : Fin ℓ → ℕ} (hx : ∀ (i : Fin ℓ), x i < b) (hy : ∀ (i : Fin ℓ), y i < b) (r : ι) (T : (ι → Fin ℓ → ZMod (2 ^ (ℓ.size + 2 * b.size))) × ZMod (2 ^ (ℓ.size + 2 * b.size))) :
eqTest r (Nat.cast ∘ x) (Nat.cast ∘ y) T = (decide (x = y), T)

The equality test with a size ℓ + 2 · size b-bit accumulator. Over ZMod (2 ^ (size ℓ + 2 · size b)), the restoring equality test of ℓ digits below b is exact for any contents of the registers and the accumulator.

def Cslib.Catalytic.keyTest {κ : Type u_1} {M : Type u_2} [DecidableEq κ] [AddCommGroup M] (c : M) (a b : κ) (V : κ → M) (s : M) :
(κ → M) × M

The key test. Add c to the entry of the borrowed table at a, add the entry at b to the result, subtract c from the entry at a, and subtract the entry at b from the result. The four visits see a, b, a, b in turn.

theorem Cslib.Catalytic.keyTest_eq {κ : Type u_1} {M : Type u_2} [DecidableEq κ] [AddCommGroup M] (c : M) (a b : κ) (V : κ → M) (s : M) :
keyTest c a b V s = (V, s + if a = b then c else 0)

The key test restores the table and adds c to the result exactly when the keys agree, whatever the contents of the table.