Documentation

CslibFrontier.DataStructure.PriorityQueue.BinaryHeap.Native.SiftUp

Native realization of the memory heap's sift-up #

The source is the existing memory-oracle sift-up, including its four reads and four-write swap. A finite while view uses an input logarithmic cutoff only to prove termination; the native loop exits on its actual index guard. This primitive supports the binary-heap migration; it does not by itself supply the complete heap interface or its initialization contract. Bounds count unit-cost .exact word-RAM instructions, with fitting words and the explicit installed-argument ABI. They are not bit-operation bounds.

Main results #

The heap source and its invariant proofs are reused from BinaryHeap.Memory; no heap algorithm or machine semantics is redefined here.

@[reducible, inline]

The loop environment: the runtime layout and a prepared constant-one word.

@[reducible, inline]

The number of handles.

Four original reads, followed by the original priority test, swap or stop index.

The loop state is a one-based position; returning 1 terminates without another read.

Instances For

    The while view preserves the entire existing siftUp free-monad program.

    A certified constant-one register. The preparation rule below emits this value; it is not a preinstalled or free host argument to the native loop.

    The existing heap-shape invariant is sufficient for each sift step. Order repair remains in the source's siftUp_triple, rather than being reproved for a native implementation.

    theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Native.SiftUp.step_triple {w : ℕ} {d : Env} {k : ℕ} {s : Memory.State} (hi : Shape w d k s) (hk : 2 ≤ k) :
    Memory.Triple w s (step (d, k)) 8 fun (a : ℕ) (s' : Memory.State) => Shape w d a s' ∧ Nat.log 2 a < Nat.log 2 k

    One real step preserves heap shape and drops the logarithmic rank. Its eight-unit bound uses the original four-read/four-write source trace, including the no-swap early exit.

    Eight source-memory units and one continuing-control unit per logarithmic rank level. Unused cutoff steps are not charged as iterations; exit potential is retained.

    One fixed compiler-generated step body before all runtime layouts, widths and indices. Local straight-line computation is absorbed into the uniform realization factor.

    The actual index guard selects a native early exit; the logarithmic cutoff is proof data. No register contains a fuel counter or a computed logarithm. The source guard 2 ≤ k is tested as 1 < k, using the prepared constant-one word.

    Actual reached loop controls, plus two binds. Constant preparation and the Unit return are certified native glue, absorbed into the same fixed exchange factor.

    The queryful source pays every executed loop control. Bounded preparation/finish work is absorbed into one fixed factor, so call sites may use the standard zero-work certificate. This does not discard a query-free or arbitrary grading. The local loop certificate lets realize prepare the constant-one word and wire the call.

    The actual combined currency is logarithmic. The premise is admissibility of the original program's complete history, not an assumed cost of a different implementation.

    theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Native.SiftUp.exec_siftUp {r : ℕ} (frontier : Fin r) (src : Fin 5 → Fin r) (tmp : Fin (0 + realizes_siftUp.scratchSize + 1) → Fin r) (hi : Function.Injective tmp) (hd : ∀ (i : Fin 5) (j : Fin (0 + realizes_siftUp.scratchSize + 1)), src i ≠ tmp j) (hf : ∀ (j : Fin (0 + realizes_siftUp.scratchSize + 1)), tmp j ≠ frontier) (hs : ∀ (i : Fin 5), src i ≠ frontier) {w n S k : ℕ} (L : Memory.Layout n) (x : WordRAM.Data r) (hx : WordRAM.Data.Bounded w x) (hin : x.regs ∘ src = indexWords (⟨n, L⟩, k)) (hA : L.arrays.Allocated (WordRAM.Realization.memoryState frontier x)) (hk : 1 ≤ k) (hkS : k ≤ S) (hshape : HeapShape n L.base S x.mem) (hord : UpOrdered (heapEntry n L.base x.mem) S k) :
    ∃ (y : WordRAM.Data r), WordRAM.ExecWithin w (realizes_siftUp.code frontier src tmp) x y (9 * realizes_siftUp.factor * (Nat.log 2 n + 1)) ∧ WordRAM.Realization.memoryState frontier y = (Memory.spec w).clientState (WordRAM.Realization.memoryState frontier x) (Memory.siftUp L k) ∧ HeapInv n L.base S y.mem ∧ Memory.Rearranges L (WordRAM.Realization.memoryState frontier x) (WordRAM.Realization.memoryState frontier y) ∧ ∀ (i : Fin r), i ≠ frontier → (∀ (j : Fin (0 + realizes_siftUp.scratchSize + 1)), tmp j ≠ i) → y.regs i = x.regs i

    One fixed native emitter repairs a sift-up defect in logarithmic instruction time. The complete whole-memory result and live-register frame come from Realizes; order repair and history admissibility come from the original source theorem. Arguments are already installed in the stated register ABI. This bound does not include loading an arbitrary external heap.