Documentation

CslibFrontier.Foundations.Analysis.SignedDigitStream.Elementary

Elementary stream operations, finite-state prefix comparisons, ranks, and buffered tests #

This module formalizes the deterministic scaled integer approximations and low-residue window identities for the elementary signed-digit stream operations of [OpenAI2026LogSpace, Sections 10.2 and 10.5, Lemmas 10.3, 10.6, 10.7, and 10.8]:

  1. Public rational streams (ratApprox, ratStream): exact integer floor approximations ⌊2 ^ t * r⌋ for rational constants r : ℚ.
  2. Low-residue locality (windowPrefix, intPrefix_emod_eq_windowPrefix_emod): modulo 2 ^ W, intPrefix x K depends only on the W digits in Ico (K + 1 - W) (K + 1).
  3. 27-state prefix comparison automaton (cmpStep, cmpPrefix): scanning from place 0 to K with state clamped to [-13, 13] decides the exact order of streamPrefix x K and streamPrefix y K.
  4. Elementary operations (pairAvgApprox, maxApprox, minApprox, shiftClipApprox): pair averages (x + y) / 2, maxima, minima, and arbitrary 2 ^ v shifts clipped to [-1, 1].
  5. Zero-padded detector ranks (zeroPaddedRankApprox): Finset.kthLargest over stream prefixes at t + 4 (which inserts 0 so negative order statistics clamp up to 0, matching the detector rank of Sections 4.2 and 8.2 rather than the general signed order statistic of Lemma 10.6), with error < 2 by Finset.abs_kthLargest_sub_kthLargest_le.
  6. Buffered margin tests (cmpBuffered, bufferedTest): 63-state finite-precision test accepting all s > 2 u and rejecting all s ≤ u when u ≥ ρ ≥ 2 ^ (-R).

Main definitions #

Main results #

References #

theorem Cslib.SignedDigitStream.abs_ediv_sub_div_lt_one (A : ℤ) {M : ℤ} (hM : 0 < M) :
|↑(A / M) - ↑A / ↑M| < 1

Euclidean floor quotient A / M approximates the real quotient (A : ℝ) / M within < 1.

Exact integer floor approximation (2 ^ t * r.num) / r.den for a rational constant r.

Computable signed-digit stream representing a rational constant r ∈ [-1, 1].

Windowed integer prefix summing only digits j ∈ Ico lo (K + 1) at scale 2 ^ K.

Low-residue locality [OpenAI2026LogSpace, Lemma 10.3]: modulo 2 ^ W, intPrefix x K depends only on the at most W digits in Ico (K + 1 - W) (K + 1).

Transition step of the 27-state absorbing prefix comparison automaton on [-13, 13].

Streaming comparison of x^{[K]} and y^{[K]} using states in {-13, ..., 13} [OpenAI2026LogSpace, Lemma 10.3].

theorem Cslib.SignedDigitStream.cmpPrefix_eq_clamp_sub {x y : ℕ → ℤ} (hx : IsSignedStream x) (hy : IsSignedStream y) (K : ℕ) :
cmpPrefix x y K = max (-13) (min 13 (intPrefix x K - intPrefix y K))

Exactness of the 27-state prefix comparator [OpenAI2026LogSpace, Lemma 10.3]: for valid signed-digit streams, cmpPrefix x y K equals intPrefix x K - intPrefix y K clamped to [-13, 13].

Scaled integer approximation to (streamVal x + streamVal y) / 2 using K = t + 4 [OpenAI2026LogSpace, Lemma 10.3].

Scaled integer approximation to max (streamVal x) (streamVal y) using cmpPrefix at K = t + 4 [OpenAI2026LogSpace, Lemma 10.3].

Scaled integer approximation to min (streamVal x) (streamVal y) using cmpPrefix at K = t + 4 [OpenAI2026LogSpace, Lemma 10.3].

noncomputable def Cslib.SignedDigitStream.shiftClipVal (v : ℤ) (z : ℝ) :

Real 2 ^ v scaling followed by clamping to [-1, 1].

Scaled integer approximation to shiftClipVal v (streamVal x) [OpenAI2026LogSpace, Lemma 10.7]: returns 0 without querying x when t + v + 4 < 0, and otherwise clamps intPrefix x K at K = (t + v + 4).toNat to [-2 ^ (t + 4), 2 ^ (t + 4)] before floor-shifting by 16.

theorem Cslib.SignedDigitStream.pairAvgApprox_emod {x y : ℕ → ℤ} (t q : ℕ) :
pairAvgApprox x y t % 2 ^ q = (windowPrefix x (t + 5 - (q + 5)) (t + 4) + windowPrefix y (t + 5 - (q + 5)) (t + 4)) % 2 ^ (q + 5) / 32
theorem Cslib.SignedDigitStream.maxApprox_eq_ediv {x y : ℕ → ℤ} (hx : IsSignedStream x) (hy : IsSignedStream y) (t : ℕ) :
maxApprox x y t = max (intPrefix x (t + 4)) (intPrefix y (t + 4)) / 16
theorem Cslib.SignedDigitStream.minApprox_eq_ediv {x y : ℕ → ℤ} (hx : IsSignedStream x) (hy : IsSignedStream y) (t : ℕ) :
minApprox x y t = min (intPrefix x (t + 4)) (intPrefix y (t + 4)) / 16

Shift-and-clip approximation [OpenAI2026LogSpace, Lemma 10.7]: shiftClipApprox v x approximates max (-1) (min 1 (2 ^ v * streamVal x)) within scaled error < 2.

def Cslib.SignedDigitStream.zeroPaddedRankApprox {ι : Type u_1} (k : ℕ) (s : Finset ι) (x : ι → ℕ → ℤ) (t : ℕ) :

Scaled integer approximation to the zero-padded detector rank Finset.kthLargest k s (fun i => streamVal (x i)) using prefixes at K = t + 4. Because Finset.kthLargest inserts 0 [OpenAI2026LogSpace, Sections 4.2 and 8.2], negative order statistics clamp up to 0 (unlike the general signed order statistic of [OpenAI2026LogSpace, Lemma 10.6]).

theorem Cslib.SignedDigitStream.kthLargest_streamPrefix_eq_div {ι : Type u_1} (k : ℕ) (s : Finset ι) (x : ι → ℕ → ℤ) (K : ℕ) :
(Finset.kthLargest k s fun (i : ι) => streamPrefix (x i) K) = ↑(Finset.kthLargest k s fun (i : ι) => intPrefix (x i) K) / 2 ^ K
theorem Cslib.SignedDigitStream.zeroPaddedRankApprox_isScaledApprox {ι : Type u_1} (k : ℕ) (s : Finset ι) {x : ι → ℕ → ℤ} (hx : ∀ i ∈ s, IsSignedStream (x i)) :

Zero-padded detector-rank approximation (adapting [OpenAI2026LogSpace, Lemma 10.6] to the zero-padded Finset.kthLargest of Sections 4.2 and 8.2): zeroPaddedRankApprox k s x approximates kthLargest k s (fun i => streamVal (x i)) within < 2.

Transition step of the 63-state absorbing 2 s - 3 u comparator on [-31, 31].

Finite-state streaming comparator computing 2 * intPrefix s K - 3 * intPrefix u K clamped to [-31, 31].

Finite buffered test [OpenAI2026LogSpace, Lemma 10.8]: accepts iff 2 * s̃ > 3 * ũ at fixed truncation place K = M + R + 7, independent of any outer numerical clock.

theorem Cslib.SignedDigitStream.cmpBuffered_eq_clamp {s u : ℕ → ℤ} (hs : IsSignedStream s) (hu : IsSignedStream u) (K : ℕ) :
cmpBuffered s u K = max (-31) (min 31 (2 * intPrefix s K - 3 * intPrefix u K))
theorem Cslib.SignedDigitStream.bufferedTest_eq_true_of_two_mul_lt {M R : ℕ} {s u : ℕ → ℤ} (hs : IsSignedStream s) (hu : IsSignedStream u) {sVal uVal ρ : ℝ} (hsVal : sVal = 2 ^ M * streamVal s) (huVal : uVal = 2 ^ M * streamVal u) (hρ : 1 / 2 ^ R ≤ ρ) (huρ : ρ ≤ uVal) (hlt : 2 * uVal < sVal) :

Lemma 10.8 (completeness of buffered test) [OpenAI2026LogSpace, Lemma 10.8]: if sVal and uVal have normalized streams s, u at scale 2 ^ M, uVal ≥ ρ ≥ 1 / 2 ^ R, and 2 * uVal < sVal, then bufferedTest M R s u = true.

theorem Cslib.SignedDigitStream.bufferedTest_eq_false_of_le {M R : ℕ} {s u : ℕ → ℤ} (hs : IsSignedStream s) (hu : IsSignedStream u) {sVal uVal ρ : ℝ} (hsVal : sVal = 2 ^ M * streamVal s) (huVal : uVal = 2 ^ M * streamVal u) (hρ : 1 / 2 ^ R ≤ ρ) (huρ : ρ ≤ uVal) (hle : sVal ≤ uVal) :

Lemma 10.8 (soundness of buffered test) [OpenAI2026LogSpace, Lemma 10.8]: under the same scaling hypotheses, if sVal ≤ uVal, then bufferedTest M R s u = false.