Documentation

Examples.Ram.InsertionSort

The complete binary insertion sort on the actual machine #

The input array is preloaded and the base and length are in registers 0 and 5. These theorems explicitly start at PC 1, count all instructions of the sorting program and its halt, and make no claim to include a skipped loader.

One fixed program handles arbitrary representable arrays, including empty arrays and duplicates. Its observed heap is mathlib's insertion sort of the original words; the standard sortedness/permutation specification follows. Only the array interval and working registers 1–4 and 6 may change. A caller can choose any local-register bound at least seven and retain its higher source-visible registers. Compiler control and scratch registers are not mistakenly included in that preservation assertion.

Ordinary named source for the same compiled program. The inner search finds a lower bound; the backward loop then moves real heap words.

Equations
  • One or more equations did not get rendered due to their size.
Instances For

    The surface block associates sequential statements differently from the modular proof, but both produce literally the same machine instructions.

    theorem Ram.Examples.InsertionSort.block_runs {w control heapLimit : ℕ} {base : Word w} {xs : List (Word w)} {s : Source.State w} {start : State w} (hw : 2 ≤ w) (hcontrol : 7 ≤ control) (hpre : Source.Array.Sort.Pre heapLimit base xs s) (hmatch : Source.State.Matches heapLimit control s start) (hpc : start.pc = 1) (hheap : heapLimit ≤ BitVec.toNat (start.regs (ABI.sp control))) (hcodefit : List.length (code control) < 2 ^ w) :
    ∃ (sourceFinal : Source.State w) (finish : State w), TerminatesWithin (code control) (Source.Array.Sort.budget xs.length + 1) start finish ∧ Source.Array.Sort.Post heapLimit base xs s sourceFinal ∧ Source.State.Observes heapLimit control sourceFinal finish ∧ FramePreserved control heapLimit start finish

    Sorting plus halt from a preloaded matching machine state. The private stack-pointer/older-stack frame and all source-visible observations refer to the same terminating target run.

    theorem Ram.Examples.InsertionSort.block_runs_quadratic {w control heapLimit : ℕ} {base : Word w} {xs : List (Word w)} {s : Source.State w} {start : State w} (hw : 2 ≤ w) (hcontrol : 7 ≤ control) (hpre : Source.Array.Sort.Pre heapLimit base xs s) (hmatch : Source.State.Matches heapLimit control s start) (hpc : start.pc = 1) (hheap : heapLimit ≤ BitVec.toNat (start.regs (ABI.sp control))) (hcodefit : List.length (code control) < 2 ^ w) :
    ∃ (sourceFinal : Source.State w) (finish : State w), TerminatesWithin (code control) (44 * xs.length ^ 2 + 35 * xs.length + 7) start finish ∧ Source.Array.Sort.Post heapLimit base xs s sourceFinal ∧ Source.State.Observes heapLimit control sourceFinal finish ∧ FramePreserved control heapLimit start finish

    The same actual halted run, now with the quadratic complexity bound.

    theorem Ram.Examples.InsertionSort.sort_on_machine {w control heapLimit : ℕ} {base : Word w} {xs : List (Word w)} {s : Source.State w} {start : State w} (hw : 2 ≤ w) (hcontrol : 7 ≤ control) (hpre : Source.Array.Sort.Pre heapLimit base xs s) (hmatch : Source.State.Matches heapLimit control s start) (hpc : start.pc = 1) (hheap : heapLimit ≤ BitVec.toNat (start.regs (ABI.sp control))) (hcodefit : List.length (code control) < 2 ^ w) :
    ∃ (finish : State w), TerminatesWithin (code control) (xs.length * (25 * Nat.clog 2 (xs.length + 1) + 19 * xs.length + 35) + 7) start finish ∧ ArrayRep finish.mem base (List.insertionSort Source.Array.unsignedLE xs) ∧ (∀ (address : Word w), BitVec.toNat address < heapLimit → BitVec.toNat address < BitVec.toNat base ∨ BitVec.toNat base + xs.length ≤ BitVec.toNat address → finish.mem address = start.mem address) ∧ finish.regs 0 = base ∧ finish.regs 5 = start.regs 5 ∧ BitVec.toNat (finish.regs 6) = xs.length ∧ (∀ (r : ℕ), 7 ≤ r → r < control → finish.regs r = start.regs r) ∧ finish.input = start.input ∧ finish.outputRev = start.outputRev

    Direct heap/result observations: standard insertion sort, unchanged array-external heap and I/O, and preserved caller registers above the seven algorithm registers. The displayed budget includes the actual halt.