Documentation

CslibFrontier.DataStructure.FenwickTree.WordRAM

Word-RAM Fenwick Trees (Binary Indexed Trees) #

This module implements Fenwick trees ([Fenwick1994], pp. 328–332) as fixed bounded-word machine programs over Cslib.WordRAM.Basis.exact (lifted to Basis.standard via StateTransitionImpl.toStandard), realizing dynamic prefix sums (PrefixSums.Op ℕ, PrefixSums.step) in exact n words of memory [base, base + n) for any length n:

Main definitions #

Main results #

References #

Unsigned bitwise lowbit identities #

Clearing the lowest set bit via m &&& (m - 1) equals m - lowbit m for every m : ℕ.

Isolating the lowest set bit via m - (m &&& (m - 1)) equals lowbit m for every m : ℕ.

Word-bounded dynamic prefix-sum specification #

Word-fit admissibility for PrefixSums.Op ℕ at word width w: operation arguments fit in a w-bit word, and an in-bounds increment add pos δ keeps the total sum below 2 ^ w.

@[reducible, inline]

Dynamic prefix sums on List ℕ restricted to w-bit fitting operations (PrefixSums.step).

Memory view as a FenwickTree ℕ #

Extract the n-word region [base, base + n) of Word-RAM memory as a FenwickTree ℕ.

@[simp]
theorem Cslib.DataStructure.FenwickTree.WordRAM.size_treeOfMem (base n : ℕ) (mem : ℕ → ℕ) :
(treeOfMem base n mem).size = n
@[simp]
theorem Cslib.DataStructure.FenwickTree.WordRAM.getElem_treeOfMem (base n : ℕ) (mem : ℕ → ℕ) (i : ℕ) (hi : i < (treeOfMem base n mem).tree.size) :
(treeOfMem base n mem).tree[i] = mem (base + i)
theorem Cslib.DataStructure.FenwickTree.WordRAM.treeOfMem_congr {base n : ℕ} {mem mem' : ℕ → ℕ} (h : ∀ a ∈ { base := base, length := n }.footprint, mem' a = mem a) :
treeOfMem base n mem' = treeOfMem base n mem
theorem Cslib.DataStructure.FenwickTree.WordRAM.treeOfMem_zeros {base n : ℕ} {mem : ℕ → ℕ} (hzero : ∀ a ∈ { base := base, length := n }.footprint, mem a = 0) :
treeOfMem base n mem = zeros n
theorem Cslib.DataStructure.FenwickTree.WordRAM.tree_treeOfMem_update_add {base n idx v : ℕ} {mem : ℕ → ℕ} :
(treeOfMem base n (Function.update mem (base + idx) (mem (base + idx) + v))).tree = (treeOfMem base n mem).tree.modify idx fun (x : ℕ) => x + v

Register assignment and machine programs #

@[reducible, inline]

Persistent bump-pointer frontier register (base + n after initialization).

@[reducible, inline]

Persistent base address of [base, base + n).

@[reducible, inline]

Persistent array length n.

@[reducible, inline]

Operation tag (0 for add, nonzero for sum).

@[reducible, inline]

Operation pos (for add) or len (for sum).

@[reducible, inline]

Operation increment δ (for add).

@[reducible, inline]

Query accumulator / output register (curReg during initClear).

@[reducible, inline]

Traversal index register (1-based node m or 0-based counter in buildCode).

@[reducible, inline]

Constant 0 scratch (zeroReg during initClear).

@[reducible, inline]

Constant 1 scratch (oneReg during initClear).

@[reducible, inline]

Bitwise / address scratch 1.

@[reducible, inline]

Address / value scratch 2.

Concrete Fenwick-tree representation invariant at [base, base + n) and word width w.

  • bounded : WordRAM.Data.Bounded w x

    All registers and memory words fit in w bits.

  • base_eq : x.regs Reg.rBase = b

    Persistent base register holds b.

  • size_eq : x.regs Reg.rLen = n

    Persistent size register holds n.

  • sum_fits : WordRAM.Fits w s.sum

    Total sum of s fits in w bits (so no prefix or Fenwick interval sum overflows).

  • valid_tree : (treeOfMem b n x.mem).Valid s

    [b, b + n) forms the unique valid Fenwick tree for s.

theorem Cslib.DataStructure.FenwickTree.WordRAM.Rep.length_eq {b n w : ℕ} {s : List ℕ} {x : WordRAM.Data 12} (hr : Rep b n w s x) :
s.length = n

Downward prefix-query loop (queryLoop) #

Single downward Fenwick query step: adds mem[rBase + (rIdx - 1)] to rAns and clears the lowest set bit of rIdx (rIdx ← rIdx &&& (rIdx - 1)).

theorem Cslib.DataStructure.FenwickTree.WordRAM.queryLoop_spec {w b n m a : ℕ} {s : List ℕ} {x : WordRAM.Data 12} (hvt : (treeOfMem b n x.mem).Valid s) (hz : x.regs Reg.rZero = 0) (ho : x.regs Reg.rOne = 1) (hb : x.regs Reg.rBase = b) (hm_reg : x.regs Reg.rIdx = m) (ha_reg : x.regs Reg.rAns = a) (hm_le : m ≤ n) (hfit_addr : WordRAM.Fits w (b + n)) (hfit_sum : WordRAM.Fits w (a + (List.take m s).sum)) :

queryLoop accumulates (s.take m).sum into rAns in 7 * (downChain m).length + 2 instructions (≤ 7 * Nat.clog 2 (n + 1) + 2), preserving memory and persistent registers.

Upward point-update loop (updateLoop) #

Single upward Fenwick update step: adds rDelta to mem[rBase + (rIdx - 1)], isolates lb = lowbit rIdx via rIdx - (rIdx &&& (rIdx - 1)), and either exits (rIdx ← 0) when rLen - rIdx < lb or advances rIdx ← rIdx + lb.

theorem Cslib.DataStructure.FenwickTree.WordRAM.updateLoop_spec {w b n m δ S : ℕ} {x : WordRAM.Data 12} (hz : x.regs Reg.rZero = 0) (ho : x.regs Reg.rOne = 1) (hb : x.regs Reg.rBase = b) (hsz : x.regs Reg.rLen = n) (hm_reg : x.regs Reg.rIdx = m) (hd_reg : x.regs Reg.rDelta = δ) (hm_pos : 0 < m) (hm_le : m ≤ n) (hfit_addr : WordRAM.Fits w (b + n)) (hfit_sum : WordRAM.Fits w (S + δ)) (hcells : ∀ (k : ℕ), m ≤ k → k ≤ n → x.mem (b + (k - 1)) ≤ S) :

updateLoop performs addLoop n m δ in-place on [b, b + n) in at most 13 * (upChain n m).length + 2 instructions (≤ 13 * Nat.clog 2 (n + 1) + 2), preserving outside memory and persistent registers.

Operation dispatcher and StateTransitionImpl #

Unified operation dispatcher for add (rTag = 0) and sum (rTag ≠ 0) over Basis.exact.

Relocatable SubroutineCode over Basis.exact for Fenwick-tree dynamic prefix sums.

Encode PrefixSums.Op ℕ into caller-supplied input registers (rTag, rPos, rDelta).

Register calling convention matching encodeOp on rTag, rPos, and rDelta.

Decode the typed result of PrefixSums.Op ℕ from rAns.

Representation-independent per-operation instruction budget 13 * ⌈log₂ (|s| + 1)⌉ + 10.

theorem Cslib.DataStructure.FenwickTree.WordRAM.opCode_spec {w b n : ℕ} (hw : 0 < w) (hfit_addr : WordRAM.Fits w (b + n)) {s : List ℕ} {x : WordRAM.Data 12} (o : PrefixSums.Op ℕ) (hr : Rep b n w s x) (hv : (spec w).valid s o) (harg : argMatch o x.regs) :
∃ (y : WordRAM.Data 12) (t : ℕ), WordRAM.Exec w opCode x y t ∧ decodeOp o y.regs = some ((spec w).stepFn s o).2 ∧ Rep b n w ((spec w).stepFn s o).1 y ∧ WordRAM.Modifies { base := b, length := n }.footprint x.mem y.mem ∧ y.regs Reg.rFrontier = x.regs Reg.rFrontier ∧ t ≤ charge s o

Per-operation execution theorem for opCode over Basis.exact.

Unified WordRAM.StateTransitionImpl certificate over Basis.exact for Fenwick-tree dynamic prefix sums of length n starting from List.replicate n 0.

@[reducible, inline]

Standard-basis Fenwick-tree WordRAM.StateTransitionImpl, lifted from Basis.exact.

Canonical native result format for PrefixSums.Op.Result ℕ.

Paid 1-instruction canonical result recoding for standalone DynamicProgram projection.

def Cslib.DataStructure.FenwickTree.WordRAM.certificate {w b n : ℕ} (hw : 0 < w) (hfit_addr : WordRAM.Fits w (b + n)) :

Standalone DynamicCertificate over Basis.standard for Fenwick-tree dynamic prefix sums.

theorem Cslib.DataStructure.FenwickTree.WordRAM.instruction_bound {w b n : ℕ} (hw : 0 < w) (hfit_addr : WordRAM.Fits w (b + n)) :

Worst-case instruction bound 13 * Nat.clog 2 (s.length + 1) + 12 per DynamicProgram call (including 1-instruction canonical recoding and final halt).

theorem Cslib.DataStructure.FenwickTree.WordRAM.client_spec {w b n : ℕ} (hw : 0 < w) (hfit_addr : WordRAM.Fits w (b + n)) {β : Type u_1} (p : (spec w).Client β) {s : List ℕ} {x : WordRAM.Data 12} (hr : Rep b n w s x) (hv : (spec w).ValidHistory s ((spec w).clientHistory s p)) :

Every admissible adaptive client executes on the Word-RAM Fenwick tree from any valid representation Rep b n w s x (whether zero-initialized or constructed in O(n) instructions by buildCode_spec), returning (spec w).clientResult s p in at most history.length * (13 * Nat.clog 2 (s.length + 1) + 12) total instructions.

O(n) in-place Word-RAM immediate-parent construction (buildCode) #

Single immediate-parent propagation iteration (m = rIdx + 1): adds mem[rBase + rIdx] into mem[rBase + (m + lowbit m - 1)] when m + lowbit m ≤ rLen, then increments rIdx.

In-place O(n) Fenwick-tree construction over Basis.exact: transforms an array of n raw values in [rBase, rBase + n) into its Fenwick tree (ofList s).

theorem Cslib.DataStructure.FenwickTree.WordRAM.buildCode_spec {w b : ℕ} {s : List ℕ} {x : WordRAM.Data 12} (hw : 0 < w) (hfit_addr : WordRAM.Fits w (b + s.length)) (hfit_sum : WordRAM.Fits w s.sum) (hbdd : WordRAM.Data.Bounded w x) (hb : x.regs Reg.rBase = b) (hsz : x.regs Reg.rLen = s.length) (hmem : ∀ (i : ℕ) (hi : i < s.length), x.mem (b + i) = s[i]) :

In-place immediate-parent propagation buildCode transforms raw entries s in [b, b + s.length) into a valid Rep b s.length w s y (whose memory equals ofList s) within 17 * s.length + 5 exact-basis instructions, preserving outside memory and persistent registers.