Documentation

CslibFrontier.Computability.Complexity.Classes.CountingSimulation.Accumulate

Streamed sums modulo a computed modulus #

Cslib.Complexity.CountingSimulation.modSum r modulus count c adds the terms r (c, 0), …, r (c, count c - 1) to an accumulator one at a time, reducing modulo modulus c after each addition. The terms are visited by the binary-index fold Cslib.GenericRAM.IndexFold, which keeps only the index, the context c and the accumulator, so the workspace does not grow with the number of terms.

This is the outer loop of the deterministic counting simulation of #1833: the context is the input and the clock, the terms are the residues of the contractions of the networks of the movement schedules, and the modulus is a power of two above the count being recovered (Nat.foldl_range_add_mod_eq_of_lt).

The resource bound (Cslib.Complexity.CountingSimulation.exists_computableInO_modSum) is a native multi-tape certificate: if the number of terms and the modulus are computable in polynomial time, the terms lie below the modulus, and a certificate computes the term at every summed index within time R c and space Q c, then the streamed sum is computable within time (count c + 1) * (poly + R c) and space poly + Q c, for a polynomial in the encoded length of the context. Neither R nor Q needs to be polynomial.

Main definitions #

Main results #

def Cslib.Complexity.CountingSimulation.modSumStep {C : Type} (r : C × ℕ → ℕ) (modulus count : C → ℕ) (p : ℕ × C × ℕ) :

One round of the streamed sum at index p.1, with context p.2.1 and accumulator p.2.2: below the number of terms, add the term and reduce modulo the modulus; otherwise leave the state unchanged.

def Cslib.Complexity.CountingSimulation.modSum {C : Type} (r : C × ℕ → ℕ) (modulus count : C → ℕ) (c : C) :

The terms r (c, 0), …, r (c, count c - 1) summed one at a time from zero, reducing modulo modulus c after each addition, by a streaming fold over a binary index.

theorem Cslib.Complexity.CountingSimulation.run_modSumStep {C : Type} {r : C × ℕ → ℕ} {modulus count : C → ℕ} {c : C} {j : ℕ} (hj : j ≤ count c) :
GenericRAM.IndexFold.run (modSumStep r modulus count) j (c, 0) = (j, c, List.foldl (fun (acc i : ℕ) => (acc + r (c, i)) % modulus c) 0 (List.range j))

The state of the streamed sum after j ≤ count c rounds: the index j, the unchanged context and the residue of the first j terms.

theorem Cslib.Complexity.CountingSimulation.modSum_eq_sum_mod {C : Type} (r : C × ℕ → ℕ) (modulus count : C → ℕ) (c : C) :
modSum r modulus count c = (∑ i ∈ Finset.range (count c), r (c, i)) % modulus c

The streamed sum is the sum modulo the modulus.

theorem Cslib.Complexity.CountingSimulation.exists_computableInO_modSum {C : Type} {r : C × ℕ → ℕ} {modulus count : C → ℕ} {encC : C ↪ List Bool} (hmodulus : Turing.MultiTapeTM.ComputableInPolyTime modulus encC Turing.MultiTapeTM.encNat) (hcount : Turing.MultiTapeTM.ComputableInPolyTime count encC Turing.MultiTapeTM.encNat) (cr : GenericRAM.NativeCertificate r (Turing.MultiTapeTM.encPair encC Turing.MultiTapeTM.encNat) Turing.MultiTapeTM.encNat) (hpos : ∀ (c : C), 0 < modulus c) (hr : ∀ (c : C), ∀ i < count c, r (c, i) < modulus c) (R Q : C → ℕ) (hR : ∀ (c : C), ∀ i < count c, cr.time (c, i) ≤ R c) (hQ : ∀ (c : C), ∀ i < count c, cr.space (c, i) ≤ Q c) :
∃ (a : ℕ) (d : ℕ), Turing.MultiTapeTM.ComputableInO (modSum r modulus count) encC Turing.MultiTapeTM.encNat (fun (c : C) => (count c + 1) * (a * ((encC c).length + 1) ^ d + R c)) fun (c : C) => a * ((encC c).length + 1) ^ d + Q c

Resource bound of the streamed sum. Suppose the number of terms and the modulus are computable in polynomial time, the modulus is positive, every summed term lies below it, and a native certificate computes the terms within time R c and space Q c at the summed indices. Then the streamed sum is computable within time (count c + 1) * (a * (n + 1) ^ d + R c) and space a * (n + 1) ^ d + Q c, where n is the encoded length of the context and a, d are constants: the workspace holds one binary index, the context and an accumulator below the modulus.