Documentation

Examples.Ram.BinarySearch

A compiled sorted-array lower-bound search #

The ordinary named program below lowers to the reusable search contract. Execution starts at the main block with the array preloaded in the heap and base, key and length already in registers. The theorem counts every executed search instruction and the final halt, not an unimplemented input loader.

The returned index is observed in the actual machine register, the original array is observed in machine memory, and every source-heap address is unchanged. The compiler's private stack frame is tracked separately by FramePreserved.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    theorem Ram.Examples.BinarySearch.block_runs {w heapLimit : ℕ} {base key : Word w} {xs : List (Word w)} {s : Source.State w} {start : State w} (hw : 2 ≤ w) (hsorted : List.Pairwise (fun (a b : Word w) => BitVec.toNat a ≤ BitVec.toNat b) xs) (hpre : Source.Array.LowerBoundPre heapLimit base key xs s) (hmatch : Source.State.Matches heapLimit 5 s start) (hpc : start.pc = 1) (hheap : heapLimit ≤ BitVec.toNat (start.regs (ABI.sp 5))) (hcodefit : List.length code < 2 ^ w) :
    ∃ (sourceFinal : Source.State w) (finish : State w), TerminatesWithin code (25 * Nat.clog 2 (xs.length + 1) + 7) start finish ∧ Source.Array.LowerBoundPost heapLimit base key xs s sourceFinal ∧ Source.State.Observes heapLimit 5 sourceFinal finish ∧ FramePreserved 5 heapLimit start finish

    Run the checked block from a matching preloaded state at PC 1, then execute its halt. The budget includes no skipped initialization prologue.

    theorem Ram.Examples.BinarySearch.lower_bound_on_machine {w heapLimit : ℕ} {base key : Word w} {xs : List (Word w)} {s : Source.State w} {start : State w} (hw : 2 ≤ w) (hsorted : List.Pairwise (fun (a b : Word w) => BitVec.toNat a ≤ BitVec.toNat b) xs) (hpre : Source.Array.LowerBoundPre heapLimit base key xs s) (hmatch : Source.State.Matches heapLimit 5 s start) (hpc : start.pc = 1) (hheap : heapLimit ≤ BitVec.toNat (start.regs (ABI.sp 5))) (hcodefit : List.length code < 2 ^ w) :
    ∃ (finish : State w), TerminatesWithin code (25 * Nat.clog 2 (xs.length + 1) + 7) start finish ∧ Source.Array.LowerBoundSpec xs key (BitVec.toNat (finish.regs 2)) ∧ ArrayRep finish.mem base xs ∧ (∀ (address : Word w), BitVec.toNat address < heapLimit → finish.mem address = start.mem address) ∧ finish.regs 0 = base ∧ finish.regs 1 = key ∧ finish.input = start.input ∧ finish.outputRev = start.outputRev

    The actual halted machine contains the correct insertion index and the unchanged sorted array. Heap and I/O preservation are stated against the actual entry machine, not just an intermediate source-level result.