Documentation

Examples.Ram.MergeSort

Recursive merge sort on the actual RAM machine #

The main block invokes one fixed recursive function on a preloaded source array and an equally sized disjoint scratch allocation. It includes the initial call and final halt, but does not include an unimplemented loader or allocator. Both recursive calls, merging, copying and stack frame work are counted by the compiler-backed contracts.

The Function namespace below exposes the source-facing two-array declaration, its actual executable shared-state result, ordinary list/StateM specifications, and a separate full-invocation bound. It does not wrap the older three-argument main block above it.

Ordinary recursive source. The mathematical canonical sort is nowhere called by this program: only RAM calls, word operations, loads and stores occur.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    theorem Ram.Examples.MergeSort.main_contract {w heapLimit : ℕ} {base scratch : Word w} {xs : List (Word w)} (hw : 2 ≤ w) :
    Source.RelContract 9 Source.Array.MergeSort.program heapLimit (Nat.clog 2 xs.length + 1) main (Source.Array.MergeSort.Pre heapLimit base scratch xs) (fun (entry finish : Source.State w) => Source.Array.MergeSort.Post heapLimit base scratch xs entry finish ∧ (∀ (r : Reg), r ≠ 3 → finish.regs r = entry.regs r) ∧ finish.regs 3 = 0) fun (x : Source.State w) => Source.Array.MergeSort.budget xs.length + 81

    Source-level main contract, retaining caller locals across the initial ordinary function call. Register 3 receives the function's zero return value.

    theorem Ram.Examples.MergeSort.block_runs {w heapLimit : ℕ} {base scratch : Word w} {xs : List (Word w)} {s : Source.State w} {start : State w} (hw : 2 ≤ w) (hpre : Source.Array.MergeSort.Pre heapLimit base scratch xs s) (hmatch : Source.State.Matches heapLimit 9 s start) (hpc : start.pc = 1) (hheap : heapLimit ≤ BitVec.toNat (start.regs (ABI.sp 9))) (hcodefit : List.length code < 2 ^ w) (hstack : Compiler.StackFits 9 (Nat.clog 2 xs.length + 1) start) :
    ∃ (sourceFinal : Source.State w) (finish : State w), TerminatesWithin code (Source.Array.MergeSort.budget xs.length + 82) start finish ∧ (Source.Array.MergeSort.Post heapLimit base scratch xs s sourceFinal ∧ (∀ (r : Reg), r ≠ 3 → sourceFinal.regs r = s.regs r) ∧ sourceFinal.regs 3 = 0) ∧ Source.State.Observes heapLimit 9 sourceFinal finish ∧ FramePreserved 9 heapLimit start finish

    The preloaded main block pays for its initial call and the actual halt. The depth premise includes that initial call, not just recursive body depth.

    structure Ram.Examples.MergeSort.Result {w : ℕ} (heapLimit : ℕ) (base scratch : Word w) (xs : List (Word w)) (entry finish : State w) :

    Every assertion observes the same halted machine, including preservation outside both buffers and restoration of caller-visible registers.

    Instances For
      theorem Ram.Examples.MergeSort.sort_on_machine {w heapLimit : ℕ} {base scratch : Word w} {xs : List (Word w)} {s : Source.State w} {start : State w} (hw : 2 ≤ w) (hpre : Source.Array.MergeSort.Pre heapLimit base scratch xs s) (hmatch : Source.State.Matches heapLimit 9 s start) (hpc : start.pc = 1) (hheap : heapLimit ≤ BitVec.toNat (start.regs (ABI.sp 9))) (hcodefit : List.length code < 2 ^ w) (hstack : Compiler.StackFits 9 (Nat.clog 2 xs.length + 1) start) :
      ∃ (finish : State w), TerminatesWithin code (Source.Array.MergeSort.budget xs.length + 82) start finish ∧ Result heapLimit base scratch xs start finish
      theorem Ram.Examples.MergeSort.budget_isBigO :
      (fun (n : ℕ) => ↑(Source.Array.MergeSort.budget n + 82)) =O[Filter.atTop] fun (n : ℕ) => ↑((n + 1) * Nat.clog 2 (n + 1))

      Mathlib's IsBigO applies to the very budget in sort_on_machine, not to the host-side canonical sort. Size counts source-array words.

      def Ram.Examples.MergeSort.Function.Safe (array scratch : ArrayRef 32) (heapLimit : ℕ) (entry : Source.State 32) :

      Existing disjoint arrays of equal extent. The lists are erased witnesses of the preloaded heap, not runtime arguments or an assumed execution.

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

        Budget-free recursive correctness supplies termination of the actual call. Capacity includes the nested merge helper and the outer function frame.

        def Ram.Examples.MergeSort.Function.sort (array scratch : ArrayRef 32) (heapLimit : ℕ) (entry : Source.State 32) (safe : Safe array scratch heapLimit entry) (hstack : heapLimit + (Nat.clog 2 (BitVec.toNat array.length) + 2) * ABI.frameSize Source.Array.MergeSort.Function.sortFunctions.registers < 2 ^ 32) :

        Run the recursive two-array function and return its actual shared state. No time bound, mathematical answer, loader or host-side recursive sort is used.

        Equations
        Instances For
          theorem Ram.Examples.MergeSort.Function.sort_spec {array scratch : ArrayRef 32} {heapLimit : ℕ} {entry : Source.State 32} (safe : Safe array scratch heapLimit entry) (hstack : heapLimit + (Nat.clog 2 (BitVec.toNat array.length) + 2) * ABI.frameSize Source.Array.MergeSort.Function.sortFunctions.registers < 2 ^ 32) {xs workspace : List (Word 32)} (length : workspace.length = xs.length) (sourceArray : array.Rep heapLimit xs entry) (scratchArray : scratch.Rep heapLimit workspace entry) (disjoint : ArraysDisjoint array.base xs.length scratch.base xs.length) :
          have finish := sort array scratch heapLimit entry safe hstack; array.Rep heapLimit (Source.Array.MergeSort.sorted xs) finish ∧ (∃ (now : List (Word 32)), scratch.Rep heapLimit now finish) ∧ TwoBufferFrame array.base xs.length scratch.base xs.length entry.mem finish.mem ∧ finish.input = entry.input ∧ finish.outputRev = entry.outputRev

          The mathematical sorted result, scratch extent and frame describe the same state returned by ordinary application of the compiled function.

          theorem Ram.Examples.MergeSort.Function.sort_contents {array scratch : ArrayRef 32} {heapLimit : ℕ} {entry : Source.State 32} (safe : Safe array scratch heapLimit entry) (hstack : heapLimit + (Nat.clog 2 (BitVec.toNat array.length) + 2) * ABI.frameSize Source.Array.MergeSort.Function.sortFunctions.registers < 2 ^ 32) {xs workspace : List (Word 32)} (length : workspace.length = xs.length) (sourceArray : array.Rep heapLimit xs entry) (scratchArray : scratch.Rep heapLimit workspace entry) (disjoint : ArraysDisjoint array.base xs.length scratch.base xs.length) :
          arrayContents (sort array scratch heapLimit entry safe hstack).mem array.base (BitVec.toNat array.length) = Source.Array.MergeSort.sorted xs

          Actual source-array contents equal the ordinary canonical list sort.

          theorem Ram.Examples.MergeSort.Function.sort_sorted {array scratch : ArrayRef 32} {heapLimit : ℕ} {entry : Source.State 32} (safe : Safe array scratch heapLimit entry) (hstack : heapLimit + (Nat.clog 2 (BitVec.toNat array.length) + 2) * ABI.frameSize Source.Array.MergeSort.Function.sortFunctions.registers < 2 ^ 32) {xs workspace : List (Word 32)} (length : workspace.length = xs.length) (sourceArray : array.Rep heapLimit xs entry) (scratchArray : scratch.Rep heapLimit workspace entry) (disjoint : ArraysDisjoint array.base xs.length scratch.base xs.length) :
          Source.Array.SortedPerm xs (arrayContents (sort array scratch heapLimit entry safe hstack).mem array.base (BitVec.toNat array.length))

          The executable result is a sorted permutation by the existing list theorem, without reopening recursion, word bindings or the RAM calling convention.

          theorem Ram.Examples.MergeSort.Function.sort_stateM {array scratch : ArrayRef 32} {heapLimit : ℕ} {entry : Source.State 32} (safe : Safe array scratch heapLimit entry) (hstack : heapLimit + (Nat.clog 2 (BitVec.toNat array.length) + 2) * ABI.frameSize Source.Array.MergeSort.Function.sortFunctions.registers < 2 ^ 32) {xs workspace : List (Word 32)} (length : workspace.length = xs.length) (sourceArray : array.Rep heapLimit xs entry) (scratchArray : scratch.Rep heapLimit workspace entry) (disjoint : ArraysDisjoint array.base xs.length scratch.base xs.length) :
          arrayContents (sort array scratch heapLimit entry safe hstack).mem array.base (BitVec.toNat array.length) = (StateT.run Source.Array.MergeSort.sortState xs).2

          The same observed contents implement the existing ordinary StateM model. The model is a specification; it is never called by the RAM implementation.

          theorem Ram.Examples.MergeSort.Function.runTotal_steps_le {array scratch : ArrayRef 32} {heapLimit : ℕ} {entry : Source.State 32} (safe : Safe array scratch heapLimit entry) (hstack : heapLimit + (Nat.clog 2 (BitVec.toNat array.length) + 2) * ABI.frameSize Source.Array.MergeSort.Function.sortFunctions.registers < 2 ^ 32) {xs workspace : List (Word 32)} (length : workspace.length = xs.length) (sourceArray : array.Rep heapLimit xs entry) (scratchArray : scratch.Rep heapLimit workspace entry) (disjoint : ArraysDisjoint array.base xs.length scratch.base xs.length) :

          The same actual invocation counts recursive calls, descriptor arithmetic, merge, copy, the outer frame and halt. Storage is already represented.

          An asymptotic theorem for the natural-number reserve. Each fixed-width run still requires its represented input and genuine code/stack capacity.