Documentation

CslibFrontier.DataStructure.PriorityQueue.BinaryHeap.Native.SiftDown

Source-faithful native sift-down #

The existing memory program supplies the array reads, swap and order proof. A finite while view preserves its complete free-monad program. Its logarithmic cutoff is proof data, while native code tests the actual index against a charged prepared threshold. No runtime logarithm or fuel register is supplied for free. The initially-zero index exits before threshold preparation. The compiler handles the original named priority and non-strict right-child guards directly.

Main results #

The source heap invariants and order proof are reused, not a new heap algorithm or model.

@[reducible, inline]

Layout, size and one control word: a threshold inside the loop, an index at entry.

@[reducible, inline]

The number of occupied heap positions.

@[reducible, inline]

The control word: the prepared threshold inside the loop, the start index at entry.

The original four-write sink, returning the selected child or a stop sentinel.

Four reads, two additional right-child reads when present, and exactly the source swap.

Actual scalar native guard; the threshold is computed by preceding certified glue.

Instances For

    Positive positions turn the source multiplication test into a native threshold test.

    Forgetting the final index preserves the exact source, including every query and write. The cutoff is an input-only logarithmic bound, and it never truncates on this domain.

    Reached loop indices are occupied positions or the stop sentinel S+1. The original whole-memory heap shape and allocated array block remain explicit.

    The logarithmic distance to the last occupied level.

    theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Native.SiftDown.step_triple {w : ℕ} {d : Argument} {k : ℕ} {s : Memory.State} (hi : Shape w d k s) (hg : guard d k) :
    Memory.Triple w s (step (d, k)) 10 fun (a : ℕ) (s' : Memory.State) => Shape w d a s' ∧ rank d a < rank d k

    Each continuing body keeps the existing shape and strictly decreases its ghost rank. It uses at most four ordinary reads, two right reads and the original four-write swap.

    @[reducible, inline]

    The layout/size, then the charged prepared threshold.

    The shared compiler certifies the exact physical body; all fit obligations follow from allocated storage and the original heap shape, including the sentinel S+1.

    The source cutoff suffices: rank zero stops, and every real body strictly drops rank. The actual returned index may be the fitted sentinel, never a hidden truncated continuation.

    The queryful body pays each real loop iteration's control, including all early exits.

    The original source storage/shape domain, admitting index zero as an immediate exit.

    The existing source sift-down has one fixed native emitter before every runtime layout, size, start index, width and memory. Zero exits before division; otherwise threshold preparation, all guard/control operations, body glue and final Unit return are certified and charged. The local loop certificate lets realize prepare the threshold and wire the call.

    theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Native.SiftDown.exec_siftDown {r : ℕ} (frontier : Fin r) (src : Fin 6 → Fin r) (tmp : Fin (0 + realizes_siftDown.scratchSize + 1) → Fin r) (hi : Function.Injective tmp) (hd : ∀ (i : Fin 6) (j : Fin (0 + realizes_siftDown.scratchSize + 1)), src i ≠ tmp j) (hf : ∀ (j : Fin (0 + realizes_siftDown.scratchSize + 1)), tmp j ≠ frontier) (hs : ∀ (i : Fin 6), 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 = inputWords ((⟨n, L⟩, S), k)) (hA : L.arrays.Allocated (WordRAM.Realization.memoryState frontier x)) (hkS : k ≤ S) (hshape : HeapShape n L.base S x.mem) (hord : DownOrdered (heapEntry n L.base x.mem) S k) :

    The exact source's logarithmic order repair and rearrangement lift to one uniform native code witness. The caller supplies already installed arguments, word bounds and an allocated heap; loading arbitrary external storage is outside this operation clock.