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.
Equations
Instances For
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
Source-level main contract, retaining caller locals across the initial ordinary function call. Register 3 receives the function's zero return value.
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.
Every assertion observes the same halted machine, including preservation outside both buffers and restoration of caller-visible registers.
- source_array : ArrayRep finish.mem base (Source.Array.MergeSort.sorted xs)
- frame (address : Word w) : BitVec.toNat address < heapLimit → BitVec.toNat address < BitVec.toNat base ∨ BitVec.toNat base + xs.length ≤ BitVec.toNat address → BitVec.toNat address < BitVec.toNat scratch ∨ BitVec.toNat scratch + xs.length ≤ BitVec.toNat address → finish.mem address = entry.mem address
Instances For
Mathlib's IsBigO applies to the very budget in sort_on_machine, not
to the host-side canonical sort. Size counts source-array words.
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.
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
- Ram.Examples.MergeSort.Function.sort array scratch heapLimit entry safe hstack = (Ram.Source.Array.MergeSort.Function.sortFunctions.applyState.sort array scratch heapLimit entry ⋯).2
Instances For
The mathematical sorted result, scratch extent and frame describe the same state returned by ordinary application of the compiled function.
Actual source-array contents equal the ordinary canonical list sort.
The executable result is a sorted permutation by the existing list theorem, without reopening recursion, word bindings or the RAM calling convention.
The same observed contents implement the existing ordinary StateM model.
The model is a specification; it is never called by the RAM implementation.
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.