Documentation

Complexity.Computability.Ram.Array.MergeSort.Vector

Native fixed-length models of the existing merge-sort call #

The existing list model preserves length, so it restricts to mathlib's List.Vector without a runtime check. Equiv.vectorEquivFin then presents that same native state transformation as a finite function, for example a matrix row. Both interfaces use ordinary modify; neither introduces a new RAM program or a host-side sorting primitive.

The callable refinements retain the full original shared-state assertion, including scratch storage, the two-buffer memory frame and I/O, together with the zero return value and the caller's non-destination registers. Only the mathematical representation changes; running-time proofs remain separate.

Canonical sorting on mathlib's fixed-length lists.

Equations
Instances For
    def Ram.Source.Array.MergeSort.sortedFin {w n : ℕ} (values : Fin n → Word w) :
    Fin n → Word w

    The same canonical result viewed through a finite index, not a new algorithm or a change to the physical array layout.

    Equations
    Instances For
      @[simp]
      theorem Ram.Source.Array.MergeSort.ofFn_sortedFin {w n : ℕ} (values : Fin n → Word w) :

      Ordinary list theorems about the canonical result apply directly to the finite-function model, including its sorted-permutation specification.

      theorem Ram.Source.Array.MergeSort.call_vector_stateM_refines {w heapLimit selfFn : ℕ} {functions : Program} {base scratch : Word w} (length : ℕ) (original : State w) (dst : Reg) (hw : 2 ≤ w) (lookup : functions[selfFn]? = some (function selfFn)) :
      Refines functions heapLimit (Nat.clog 2 length + 1) (Stmt.call [dst] selfFn [Expr.var 0, Expr.var 1, Expr.var 2]) (fun (values : List.Vector (Word w) length) (entry : State w) => Pre heapLimit base scratch (↑values) entry ∧ entry = original) (fun (result : PUnit.{1} × List.Vector (Word w) length) (finish : State w) => finish.regs dst = 0 ∧ SharedStateRep heapLimit base scratch length original (↑result.2) finish.mem finish.input finish.outputRev ∧ ∀ (r : Reg), r ≠ dst → finish.regs r = original.regs r) (StateT.run (modify sortedVector))

      The existing callable list implementation also implements native sorting of a fixed-length vector. Length preservation is supplied by length_sorted; the input needs no repeated length assertion.

      theorem Ram.Source.Array.MergeSort.call_fin_stateM_refines {w heapLimit selfFn : ℕ} {functions : Program} {base scratch : Word w} (length : ℕ) (original : State w) (dst : Reg) (hw : 2 ≤ w) (lookup : functions[selfFn]? = some (function selfFn)) :
      Refines functions heapLimit (Nat.clog 2 length + 1) (Stmt.call [dst] selfFn [Expr.var 0, Expr.var 1, Expr.var 2]) (fun (values : Fin length → Word w) (entry : State w) => Pre heapLimit base scratch (List.ofFn values) entry ∧ entry = original) (fun (result : PUnit.{1} × (Fin length → Word w)) (finish : State w) => finish.regs dst = 0 ∧ SharedStateRep heapLimit base scratch length original (List.ofFn result.2) finish.mem finish.input finish.outputRev ∧ ∀ (r : Reg), r ≠ dst → finish.regs r = original.regs r) (StateT.run (modify sortedFin))

      One fixed RAM call implements native sorting of a finite function. This interface composes directly with finite-indexed arrays and matrix rows while retaining every shared-state and caller-register guarantee of the list call.