Documentation

Complexity.Computability.Ram.Array.Search.Total

Budget-free total correctness of lower-bound search #

The body follows the existing parameterized search step. The loop terminates by strict descent of the natural interval length hi - lo; the stronger halving property comes from the shared search invariant. No instruction count, compiler register boundary or proposed time bound appears in these proofs.

The loop accepts the full search invariant, so a caller can initialize its own named endpoints and retain the original shared state in the postcondition. Array-read safety, representable midpoint arithmetic, sortedness and the actual exit specification are the same as in the search core.

theorem Ram.Source.Array.Search.body_total {w : ℕ} (registers : Registers) {heapLimit depth : ℕ} {program : Program} (hw : 0 < w) (s : State w) (haddress : BitVec.toNat (s.regs registers.base + midpointWord registers s) < heapLimit) :
TotalContract program heapLimit depth (body registers) (fun (t : State w) => t = s) fun (t : State w) => t = step registers s

One safe search step, proved directly through budget-free statement rules. The midpoint read must lie in the source heap; no time contract is erased here.

theorem Ram.Source.Array.Search.iteration_total {w : ℕ} (registers : Registers) {heapLimit depth : ℕ} {program : Program} {base key : Word w} {xs : List (Word w)} {original : State w} (hw : 2 ≤ w) (sorted : List.Pairwise (fun (a b : Word w) => BitVec.toNat a ≤ BitVec.toNat b) xs) :
TotalRelContract program heapLimit depth (body registers) (fun (s : State w) => Invariant registers heapLimit base key xs original s ∧ s.eval (condition registers) ≠ 0) fun (s t : State w) => Invariant registers heapLimit base key xs original t ∧ BitVec.toNat (t.regs registers.hi) - BitVec.toNat (t.regs registers.lo) ≤ (BitVec.toNat (s.regs registers.hi) - BitVec.toNat (s.regs registers.lo)) / 2

An active iteration preserves the shared search invariant and halves its interval. This is a functional fact about the actual safe successor state.

theorem Ram.Source.Array.Search.loop_total {w : ℕ} (registers : Registers) {heapLimit depth : ℕ} {program : Program} {base key : Word w} {xs : List (Word w)} {original : State w} (hw : 2 ≤ w) (sorted : List.Pairwise (fun (a b : Word w) => BitVec.toNat a ≤ BitVec.toNat b) xs) :
TotalContract program heapLimit depth (loop registers) (Invariant registers heapLimit base key xs original) (Post registers heapLimit base key xs original)

The initialized search interval terminates with the lower-bound result and the original shared-state frame. Its variant measures progress, not runtime.