Documentation

Complexity.Computability.Ram.Array.MergeSort.Total

Functional correctness of the existing recursive merge-sort function #

The recursion argument is the ordinary list, ordered by length. The proof uses callable total specifications for both smaller halves and the existing array/frame interfaces for their mathematical results. Initialization and merge/copy-back use independently proved total contracts, down to the actual copy and merge iterations. No fuel, continuation reserve or instruction arithmetic is needed.

This is the same fixed function and the same Pre/Post as the measured interface. The logarithmic call-depth obligations still justify its stack usage; a separate time proof can later be combined with this total result.

theorem Ram.Source.Array.MergeSort.condition_ne_zero_iff {w heapLimit : ℕ} {base scratch : Word w} {xs : List (Word w)} {entry : State w} (hw : 0 < w) (hp : Pre heapLimit base scratch xs entry) :
entry.eval condition ≠ 0 ↔ 1 < xs.length

The source-level guard follows the ordinary list's nontrivial-length test.

theorem Ram.Source.Array.MergeSort.recursive_total {w heapLimit selfFn : ℕ} {functions : Program} (hw : 2 ≤ w) (lookup : functions[selfFn]? = some (function selfFn)) (xs : List (Word w)) :
(recursionSpec heapLimit selfFn w).toTotal.Correct functions heapLimit xs

Functional recursion preserves the sorted-array, scratch, memory-frame and I/O postconditions, independently of any proposed running-time bound.