Documentation

Complexity.Computability.Ram.Array.Merge.Ordering

Standard merge specifications for RAM arrays #

unsignedMerge is only an abbreviation for the standard List.merge, with unsigned word comparisons and left priority for equal heads. The step lemmas do not require sorted inputs. Sortedness is used only to derive SortedPerm and equality with the standard sorting functions.

These are mathematical specifications, not uncharged RAM operations.

@[reducible, inline]
abbrev Ram.Source.Array.unsignedMerge {w : ℕ} (xs ys : List (Word w)) :

Standard merge with the unsigned comparison used by the RAM program.

Equations
Instances For
    theorem Ram.Source.Array.unsignedMerge_cons_left {w : ℕ} {a b : Word w} {xs ys : List (Word w)} (h : unsignedLE a b) :
    unsignedMerge (a :: xs) (b :: ys) = a :: unsignedMerge xs (b :: ys)

    Equal heads are emitted from the left input first.

    theorem Ram.Source.Array.unsignedMerge_cons_right {w : ℕ} {a b : Word w} {xs ys : List (Word w)} (h : BitVec.toNat b < BitVec.toNat a) :
    unsignedMerge (a :: xs) (b :: ys) = b :: unsignedMerge (a :: xs) ys

    The right head is emitted only when it is strictly smaller.

    theorem Ram.Source.Array.unsignedMerge_drop_left {w : ℕ} {xs ys : List (Word w)} {i j : ℕ} (hi : i < xs.length) (hchoose : ∀ (hj : j < ys.length), unsignedLE xs[i] ys[j]) :

    Emit the left cursor's element. The comparison premise is vacuous when the right cursor is past the end; no sortedness premise is needed.

    theorem Ram.Source.Array.unsignedMerge_drop_right {w : ℕ} {xs ys : List (Word w)} {i j : ℕ} (hj : j < ys.length) (hchoose : ∀ (hi : i < xs.length), BitVec.toNat ys[j] < BitVec.toNat xs[i]) :

    Emit the right cursor's element. The left input may already be exhausted.

    @[simp]
    theorem Ram.Source.Array.unsignedMerge_perm {w : ℕ} (xs ys : List (Word w)) :
    (unsignedMerge xs ys).Perm (xs ++ ys)

    Standard merge preserves both input multisets, even for unsorted inputs.

    Sorted inputs give a sorted result by mathlib's existing merge theorem.

    theorem Ram.Source.Array.SortedPerm.merge {w : ℕ} {xs ys left right : List (Word w)} (hleft : SortedPerm xs left) (hright : SortedPerm ys right) :
    SortedPerm (xs ++ ys) (unsignedMerge left right)

    Merge independently verified sorted outputs into a sorted permutation of the concatenation of their original inputs.

    theorem Ram.Source.Array.SortedPerm.mergeSort {w : ℕ} (xs : List (Word w)) :
    SortedPerm xs (xs.mergeSort fun (a b : Word w) => decide (unsignedLE a b))

    The standard merge sort satisfies the same implementation-independent sorting specification as insertion sort.

    theorem Ram.Source.Array.SortedPerm.eq_mergeSort {w : ℕ} {xs output : List (Word w)} (h : SortedPerm xs output) :
    output = xs.mergeSort fun (a b : Word w) => decide (unsignedLE a b)

    A verified sorted permutation also equals standard merge sort.

    Merging sorted lists returns the canonical sorting of their concatenation.

    theorem Ram.Source.Array.unsignedMerge_eq_mergeSort {w : ℕ} {xs ys : List (Word w)} (hxs : List.Pairwise unsignedLE xs) (hys : List.Pairwise unsignedLE ys) :
    unsignedMerge xs ys = (xs ++ ys).mergeSort fun (a b : Word w) => decide (unsignedLE a b)

    The same merge result agrees with standard merge sort.