Documentation

CslibFrontier.DataStructure.PriorityQueue.BinaryHeap.Native.Initialize

Compact heap initialization primitives #

Existing realization rules reserve the original three-array span and compute three layout words. Reservation preserves the complete physical memory; a separately supplied local-zero block gives the existing compact empty invariant. Metadata staging is actual charged scalar glue and copies, leaving both input words and every memory cell intact.

The separate canonical allocator certificate preserves Memory.allocLayout's two dirty queries and larger 3*n+2 span, wrapping its final layout in the existing faithful sigma encoding. These primitives do not replace Memory.allocLayout, instantiate a local-zero generic allocator, choose the final caller ABI or claim the legacy eleven-instruction initializer. Code, scratch and exchange constants are uniform before all runtime sizes, bases and widths.

Main results #

Reserve the exact original span. The scalar certificate avoids materializing a constant three, which need not fit in an admitted one-bit zero-handle initialization.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Native.Initialize.reserve_exec {r : ℕ} (frontier : Fin r) (src : Fin 1 → Fin r) (tmp : Fin (1 + realizes_reserve.scratchSize + 1) → Fin r) (hi : Function.Injective tmp) (hd : ∀ (i : Fin 1) (j : Fin (1 + realizes_reserve.scratchSize + 1)), src i ≠ tmp j) (hf : ∀ (j : Fin (1 + realizes_reserve.scratchSize + 1)), tmp j ≠ frontier) (hs : ∀ (i : Fin 1), src i ≠ frontier) {w n : ℕ} (x : WordRAM.Data r) (hx : WordRAM.Data.Bounded w x) (hin : x.regs ∘ src = WordRAM.Realization.WordEncoding.encode n) (hfit : WordRAM.Fits w (x.regs frontier + space n)) :
∃ (y : WordRAM.Data r), WordRAM.ExecWithin w (realizes_reserve.code frontier src tmp) x y (2 * realizes_reserve.factor) ∧ WordRAM.Data.Bounded w y ∧ y.mem = x.mem ∧ y.regs frontier = x.regs frontier + space n ∧ y.regs (tmp (Fin.castAdd (realizes_reserve.scratchSize + 1) 0)) = x.regs frontier ∧ ∀ (i : Fin r), i ≠ frontier → (∀ (j : Fin (1 + realizes_reserve.scratchSize + 1)), tmp j ≠ i) → y.regs i = x.regs i

One selected reservation emitter returns the old frontier, preserves every cell and moves the frontier by exactly the old span. Its one memory charge plus the fixed exit unit cost at most 2*factor; caller inputs and all non-scratch registers are framed.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Native.Initialize.reserve_empty {r n : ℕ} (frontier : Fin r) {x y : WordRAM.Data r} (hm : y.mem = x.mem) (hfr : y.regs frontier = x.regs frontier + space n) (hz : ∀ (a : ℕ), { base := x.regs frontier, length := space n }.Owns a → x.mem a = 0) :

Reserve-only initialization of a locally zero old span gives the existing empty invariant. This is a one-run source/representation statement, not a generic allocator precondition.

Compute three future layout registers from faithfully encoded (n,base). The result vector is itself faithfully encoded, with the original input registers framed. Every coordinate and copy is paid through the existing pure-glue rule; no host computation installs the result.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Native.Initialize.metadata_exec {r : ℕ} (frontier : Fin r) (src : Fin 2 → Fin r) (tmp : Fin (3 + realizes_metadata.scratchSize + 1) → Fin r) (hi : Function.Injective tmp) (hd : ∀ (i : Fin 2) (j : Fin (3 + realizes_metadata.scratchSize + 1)), src i ≠ tmp j) (hf : ∀ (j : Fin (3 + realizes_metadata.scratchSize + 1)), tmp j ≠ frontier) (hs : ∀ (i : Fin 2), src i ≠ frontier) {w : ℕ} (p : ℕ × ℕ) (x : WordRAM.Data r) (hx : WordRAM.Data.Bounded w x) (hin : x.regs ∘ src = WordRAM.Realization.WordEncoding.encode p) (hfit : WordRAM.Fits w (p.2 + space p.1)) :
∃ (y : WordRAM.Data r), WordRAM.ExecWithin w (realizes_metadata.code frontier src tmp) x y realizes_metadata.factor ∧ WordRAM.Data.Bounded w y ∧ WordRAM.Realization.memoryState frontier y = WordRAM.Realization.memoryState frontier x ∧ (∀ (i : Fin 3), y.regs (tmp (Fin.castAdd (realizes_metadata.scratchSize + 1) i)) = ![p.1, p.2 + p.1, 1] i) ∧ y.regs ∘ src = WordRAM.Realization.WordEncoding.encode p ∧ ∀ (i : Fin r), i ≠ frontier → (∀ (j : Fin (3 + realizes_metadata.scratchSize + 1)), tmp j ≠ i) → y.regs i = x.regs i

The selected metadata code performs its charged computation even though its source history is empty. It leaves the frontier, whole memory and both live input words unchanged.

The canonical source allocator keeps its two dirty queries and its extra count word. Only its result is placed in the existing fixed Sigma carrier for uniform native code.

Realize the unchanged canonical allocator. This is the 3*n+2 source used by Dijkstra, not the compact reserve-only primitive; no global zero claim is hidden in its native domain.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Native.Initialize.allocLayout_exec {r : ℕ} (frontier : Fin r) (src : Fin 1 → Fin r) (tmp : Fin (4 + realizes_allocLayout.scratchSize + 1) → Fin r) (hi : Function.Injective tmp) (hd : ∀ (i : Fin 1) (j : Fin (4 + realizes_allocLayout.scratchSize + 1)), src i ≠ tmp j) (hf : ∀ (j : Fin (4 + realizes_allocLayout.scratchSize + 1)), tmp j ≠ frontier) (hs : ∀ (i : Fin 1), src i ≠ frontier) {w n : ℕ} (x : WordRAM.Data r) (hx : WordRAM.Data.Bounded w x) (hin : x.regs ∘ src = WordRAM.Realization.WordEncoding.encode n) (hfit : WordRAM.Fits w (x.regs frontier + 1 + space n)) :
∃ (y : WordRAM.Data r), WordRAM.ExecWithin w (realizes_allocLayout.code frontier src tmp) x y (3 * realizes_allocLayout.factor) ∧ WordRAM.Data.Bounded w y ∧ y.mem = x.mem ∧ y.regs frontier = x.regs frontier + 1 + space n ∧ (∀ (i : Fin 4), y.regs (tmp (Fin.castAdd (realizes_allocLayout.scratchSize + 1) i)) = layoutWords ⟨n, Memory.allocated n (x.regs frontier)⟩ i) ∧ ∀ (i : Fin r), i ≠ frontier → (∀ (j : Fin (4 + realizes_allocLayout.scratchSize + 1)), tmp j ≠ i) → y.regs i = x.regs i

One selected code before all runtime sizes/frontiers/widths runs the actual two-query allocator. Memory is literally unchanged; the original allocated layout is really staged in four output words, and the source's two memory charges give the native 3*factor bound.