Documentation

CslibFrontier.Algorithms.WordRAM.ThreeSum.ChanHe.Place

Native heavy extraction and light placement #

The input, unsigned residue array and histogram are supplied by the actual Count.program. One scan copies heavy integers, in input order, to a compact output. Each light integer is tagged and placed directly in the packed node array. The third pass negates its integer and its residue, and writes both copies required by arrZ.

The mathematical comparator is Anthropic's Buckets.lean and NodeArray.lean at formal-math@16a5ce41304fcdaea4e0be7c132e1c361dc26da5. The native code, encoding arithmetic and execution proofs here are original. No Light execution theorem or source-machine simulation is assumed.

Main definitions #

References #

@[reducible, inline]

Input array base.

@[reducible, inline]

Unsigned residue array base.

@[reducible, inline]

Input length.

@[reducible, inline]

Histogram base.

@[reducible, inline]

Compact heavy output base.

@[reducible, inline]

Packed node output base.

@[reducible, inline]

Positive modulus, decoded unsigned.

@[reducible, inline]

Twice the positive tag added to a light integer.

@[reducible, inline]

Column in a four-cell group.

@[reducible, inline]

Distance to the second write: zero for X/Y, 4M for Z.

@[reducible, inline]

Zero for X/Y, one for Z.

@[reducible, inline]

Input cursor.

@[reducible, inline]

Number of heavy integers already written.

@[reducible, inline]

Temporary address.

@[reducible, inline]

Current remainder.

@[reducible, inline]

Current value and its tagged encoding.

@[reducible, inline]

Twice the current magnitude.

@[reducible, inline]

Current input sign.

@[reducible, inline]

Constant one.

@[reducible, inline]

Constant two.

@[reducible, inline]

Constant four.

@[reducible, inline]

Constant zero.

@[reducible, inline]

Current histogram count.

Negating a Euclidean residue needs only a subtraction and a remainder.

Integer contribution before the positive tag.

The target index is exactly the residue of the signed contribution.

A prefix of the compact output; membership is tested against the full histogram.

A compact prefix has at most as many cells as the corresponding input prefix.

theorem Cslib.Algorithms.WordRAM.ThreeSum.ChanHe.Place.heavyList_eq_filter (values : ℕ → ℤ) (n M k : ℕ) :
heavyList values n M k = List.map values (List.filter (fun (i : ℕ) => decide (1 < Histogram.count (fun (j : ℕ) => residueKey M (values j)) n (residueKey M (values i)))) (List.range k))

The compact array is the input subsequence selected by the histogram test.

theorem Cslib.Algorithms.WordRAM.ThreeSum.ChanHe.Place.heavyList_toFinset (values : ℕ → ℤ) (n : ℕ) {M : ℕ} (hM : 0 < M) (hinj : Set.InjOn values ↑(Finset.range n)) :

The native compact output has exactly the existing heavy subset as its underlying set.

def Cslib.Algorithms.WordRAM.ThreeSum.ChanHe.Place.write (mem : ℕ → ℕ) (values : ℕ → ℤ) (n M out dst col dist t : ℕ) (neg : Bool) (k : ℕ) :
ℕ → ℕ

The exact per-input write, either to the next compact heavy cell or to packed light cells.

def Cslib.Algorithms.WordRAM.ThreeSum.ChanHe.Place.memory (mem : ℕ → ℕ) (values : ℕ → ℤ) (n M out dst col dist t : ℕ) (neg : Bool) :
ℕ → ℕ → ℕ

All writes of the first k iterations, framed by arbitrary initial memory.

Copy a heavy integer without changing its signed encoding.

Tag a light integer using its magnitude and sign, then write its required cells.

Every iteration reads the original integer, its unsigned key and its exact bucket size.

The same finite code handles every input length, width, modulus and output length.

All runtime parameters survive the scan.

theorem Cslib.Algorithms.WordRAM.ThreeSum.ChanHe.Place.memory_outside (mem : ℕ → ℕ) (values : ℕ → ℤ) (n M out dst col dist t N : ℕ) (neg : Bool) (hM : 0 < M) (hN : 2 * M ≤ N) (hc : col < 4) (hd : dist ≤ 4 * M) {k : ℕ} (hk : k ≤ n) {a : ℕ} (ha : ¬(out ≤ a ∧ a < out + n)) (hb : ¬(dst ≤ a ∧ a < dst + 4 * N)) :
memory mem values n M out dst col dist t neg k a = mem a

A scan can only change its heavy capacity block and its packed output block.