Documentation

Complexity.Algorithms.WordRAM.PrefixSums.Dynamic

Stateful prefix sums implemented by one fixed Word-RAM program #

The abstract state is a list, independently of the tree representation. An operation either increments one entry or returns the sum of an initial segment. A complete balanced tree stores n = 2 ^ h entries in 2 n - 1 words. One fixed fourteen-register program dispatches both operations, with every executed instruction charged, including its final halt.

The representation is supplied at entry. Installing the three operation-argument registers is the external calling convention and is uncharged. Tree construction is a separate obligation; these theorems do not charge or claim a construction algorithm. Natural-number sums and increments must fit the chosen word width. This is a segment tree, not a Fenwick tree.

Main definitions #

Main results #

References #

Array operations. Updates use a zero-based position; queries use a prefix length.

  • add (position increment : ℕ) : Operation

    Add a nonnegative increment to one array entry.

  • sum (length : ℕ) : Operation

    Return the sum of the given number of initial entries.

Instances For

    Abstract list specification. The admissibility of an update includes the word bound on its new total; a query does not modify the array.

    Instances For

      Initialize constants and traversal registers, charging all seven instructions.

      Instances For

        One dispatcher for both operations, independent of size and word width.

        Instances For

          Fixed code and external argument registers. Decoding reads only the answer register.

          Instances For

            Mathematical description of charged register setup.

            Instances For

              A list is represented by a bounded tree, with persistent root and capacity registers.

              Instances For
                theorem Cslib.Algorithms.WordRAM.PrefixSums.Rep.length {h w b : ℕ} {x : WordRAM.Data 14} {s : List ℕ} (hx : Rep h b w s x) :
                s.length = 2 ^ h

                The abstract array length is determined by the persistent tree representation.

                theorem Cslib.Algorithms.WordRAM.PrefixSums.arguments_fit {h w b : ℕ} (ha : WordRAM.Fits w (b + 2 * 2 ^ h)) {s : List ℕ} (o : Operation) (ho : (spec h w).valid s o) (j : Fin 14) :
                theorem Cslib.Algorithms.WordRAM.PrefixSums.dispatch_spec {h w b : ℕ} {x : WordRAM.Data 14} (ha : WordRAM.Fits w (b + 2 * 2 ^ h)) {s : List ℕ} (o : Operation) (hx : Rep h b w s x) (ho : (spec h w).valid s o) :
                ∃ (y : WordRAM.Data 14), WordRAM.ExecWithin w dispatch ((program h w).prepare o x) y (13 * (h + 1) + 11) ∧ y.regs Reg.ans = ((spec h w).step s o).2 ∧ Rep h b w ((spec h w).step s o).1 y ∧ ∀ (a : ℕ), a < b ∨ b + Tree.words h ≤ a → y.mem a = x.mem a

                Every operation uses the same dispatcher, restores the list representation and returns the specified result within 13 (h + 1) + 11 structured instructions. All memory outside the 2 * 2 ^ h - 1 words of the tree is preserved.

                Semantic correctness and totality of the fixed bounded-word implementation.

                Instances For

                  Separate worst-case instruction certificate, counting the final halt.

                  The same instruction certificate stated in terms of the abstract array length.

                  theorem Cslib.Algorithms.WordRAM.PrefixSums.execOp_frame {h w b : ℕ} {x : WordRAM.Data 14} (ha : WordRAM.Fits w (b + 2 * 2 ^ h)) {s : List ℕ} {o : Operation} {y : WordRAM.Data 14} {a : ℕ} {trace : WordRAM.DynamicProgram.OperationTrace} (hx : Rep h b w s x) (ho : (spec h w).valid s o) (he : (program h w).ExecOp w x o a y trace) {j : ℕ} (hj : j < b ∨ b + Tree.words h ≤ j) :
                  y.mem j = x.mem j

                  Every actual call preserves the complement of the fixed tree region. In particular, updates do not allocate storage depending on the number of previous operations.

                  theorem Cslib.Algorithms.WordRAM.PrefixSums.client_spec {h w b : ℕ} {x : WordRAM.Data 14} (ha : WordRAM.Fits w (b + 2 * 2 ^ h)) {α : Type} (p : (spec h w).Client α) {s : List ℕ} (hx : Rep h b w s x) (hv : (spec h w).ValidHistory s ((spec h w).runClient s p).2.2) :
                  ∃ (y : WordRAM.Data 14) (traces : List WordRAM.DynamicProgram.OperationTrace), Stateful.Executes ((program h w).ExecOp w) p x ((spec h w).runClient s p).1 y traces ∧ Rep h b w ((spec h w).runClient s p).2.1 y ∧ Stateful.total WordRAM.DynamicProgram.OperationTrace.instructions traces ≤ ((spec h w).runClient s p).2.2.length * (13 * (h + 1) + 12)

                  Every admissible adaptive client has a concrete execution with the specified result and final array, and at most 13 (h + 1) + 12 instructions for each selected operation. The client controller itself is mathematical; this counts its calls into the fixed machine program.

                  Concrete zero memory and persistent metadata. Supplying this state is not a charged initialization algorithm.

                  Instances For
                    theorem Cslib.Algorithms.WordRAM.PrefixSums.rep_zeroData {h w b : ℕ} (ha : WordRAM.Fits w (b + 2 * 2 ^ h)) :
                    Rep h b w (Tree.zero h).leaves (zeroData h b)

                    The representation assumptions are inhabited at every fitting size and root pointer.