Documentation

CslibFrontier.Algorithms.WordRAM.Histogram

Charged word-RAM histograms #

A fixed program clears a histogram and inserts the keys of a disjoint input array. An insertion into a bucket of size c creates 2c ordered collisions. Every address calculation, load, increment, store and loop test is charged.

Main definitions #

Main results #

References #

Number of occurrences of a key among the first k input positions.

@[simp]
theorem Cslib.Algorithms.WordRAM.Histogram.count_zero (keys : ℕ → ℕ) (b : ℕ) :
count keys 0 b = 0
theorem Cslib.Algorithms.WordRAM.Histogram.count_succ (keys : ℕ → ℕ) (k b : ℕ) :
count keys (k + 1) b = count keys k b + if keys k = b then 1 else 0

Inserting one key changes just its bucket.

theorem Cslib.Algorithms.WordRAM.Histogram.count_le (keys : ℕ → ℕ) (k b : ℕ) :
count keys k b ≤ k

A bucket cannot contain more positions than the prefix.

Ordered collisions accumulated while inserting successive keys.

theorem Cslib.Algorithms.WordRAM.Histogram.pairs_le_sq (keys : ℕ → ℕ) (k : ℕ) :
pairs keys k ≤ k * k

The prefix collision counter is at most the square of the prefix length.

theorem Cslib.Algorithms.WordRAM.Histogram.pairs_eq_card (keys : ℕ → ℕ) (k : ℕ) :
pairs keys k = {p ∈ (Finset.range k).offDiag | keys p.1 = keys p.2}.card

The insertion counter counts exactly the ordered pairs of different positions with the same key, including when equal keys occur at different input positions.

def Cslib.Algorithms.WordRAM.Histogram.memory (mem keys : ℕ → ℕ) (dst bins k : ℕ) :
ℕ → ℕ

The original memory, with the histogram block replaced by prefix bucket counts.

theorem Cslib.Algorithms.WordRAM.Histogram.memory_read (mem keys : ℕ → ℕ) (dst bins k b : ℕ) (hb : b < bins) :
memory mem keys dst bins k (dst + b) = count keys k b

Reading a histogram bucket.

theorem Cslib.Algorithms.WordRAM.Histogram.memory_outside (mem keys : ℕ → ℕ) (dst bins k a : ℕ) (ha : ¬(dst ≤ a ∧ a < dst + bins)) :
memory mem keys dst bins k a = mem a

Histogram writes preserve all other cells.

theorem Cslib.Algorithms.WordRAM.Histogram.memory_succ (mem keys : ℕ → ℕ) (dst bins k : ℕ) (hk : keys k < bins) :
memory mem keys dst bins (k + 1) = Function.update (memory mem keys dst bins k) (dst + keys k) (count keys k (keys k) + 1)

One insertion is a single histogram-cell update.

@[reducible, inline]

Input base.

@[reducible, inline]

Histogram base.

@[reducible, inline]

Number of input keys.

@[reducible, inline]

Number of buckets.

@[reducible, inline]

Prefix length.

@[reducible, inline]

Temporary address.

@[reducible, inline]

Current key.

@[reducible, inline]

Current bucket count.

@[reducible, inline]

Number of ordered collisions.

@[reducible, inline]

Constant one.

@[reducible, inline]

Constant zero.

Clear [base..base+count) via zeroFill, loading zero = 0 and one = 1 and using cur and stop as scratch cursor and end-address registers.

theorem Cslib.Algorithms.WordRAM.Histogram.exec_clearBlock {r w : ℕ} (base count cur stop zero one : Fin r) (hnodup : [base, count, cur, stop, zero, one].Nodup) (x : WordRAM.Data r) (h1 : WordRAM.Fits w 1) (hd : WordRAM.Fits w (x.regs base + x.regs count)) :
∃ (y : WordRAM.Data r), WordRAM.ExecWithin w (clearBlock base count cur stop zero one) x y (4 * x.regs count + 6) ∧ y.mem = WordRAM.clear x.mem (x.regs base) (x.regs count) ∧ (∀ (i : Fin r), i ≠ cur → i ≠ stop → i ≠ zero → i ≠ one → y.regs i = x.regs i) ∧ y.regs zero = 0 ∧ y.regs one = 1

Clearing count cells with clearBlock executes in 4 * count + 6 instructions, leaves zero = 0 and one = 1, and preserves every register distinct from cur, stop, zero, and one.

Insert one key and add twice its old bucket count.

Clear the histogram, retaining the input and all four parameters.

Fixed code, with all bases and lengths supplied in registers.

theorem Cslib.Algorithms.WordRAM.Histogram.exec {w : ℕ} (x : WordRAM.Data 11) (keys : ℕ → ℕ) (h1 : WordRAM.Fits w 1) (hs : WordRAM.Fits w (x.regs src + x.regs len)) (hd : WordRAM.Fits w (x.regs dst + x.regs bins)) (ht : WordRAM.Fits w (x.regs len * x.regs len + x.regs len + 1)) (hdisj : x.regs src + x.regs len ≤ x.regs dst) (hkeys : ∀ i < x.regs len, keys i < x.regs bins) (hinput : ∀ i < x.regs len, x.mem (x.regs src + i) = keys i) :
∃ (y : WordRAM.Data 11), WordRAM.ExecWithin w program x y (4 * x.regs bins + 11 * x.regs len + 10) ∧ y.mem = memory x.mem keys (x.regs dst) (x.regs bins) (x.regs len) ∧ y.regs total = pairs keys (x.regs len) ∧ ∀ (i : Fin 11), ↑i < 4 → y.regs i = x.regs i

Build a histogram and count ordered collisions within 4M + 11n + 10 native instructions. The input keys lie in [0,M). The input precedes the output block. Both one-past-end addresses and the quadratic counter bound must fit. These are explicit local contracts; constructing the keys and deriving a logarithmic width are separate client obligations.