Documentation

CslibFrontier.Algorithms.Lean.Sort.AdjacentTrace

A computed stable adjacent-exchange trace #

AdjacentSortTrace.compute instruments the existing List.orderedInsert: sort the tail, shift its requests, then move the original head past strictly smaller keys. Equal keys are never crossed. Requests act on arbitrary accompanying payloads, not changing variable names.

The first substantive property is replay_compute: the actual computed requests replay the existing stable insertion sort on any payload/key map. components_compute supplies valid indices and a triangular bound; indices_payload identifies the actual original-index permutation. This is original trace instrumentation, reusing the library's sorting proofs.

The actual accumulator and output: sorted original binary keys, followed by the adjacent positions to execute in order. Repeated keys remain repeated occurrences.

  • sortedKeys : List ℕ

    Original keys in stable nondecreasing order.

  • requests : List ℕ

    Binary positions of adjacent exchanges, in execution order.

Insert one original head key. The existing tail trace is shifted below it; the new requests cross exactly the strictly smaller prefix, and hence never cross equal keys.

Compute a stable trace by a literal fold over the original keys from right to left. All traversal clocks come from lists; binary key magnitudes are only compared.

theorem Cslib.AdjacentSortTrace.components_compute (keys : List ℕ) :
(compute keys).sortedKeys = List.insertionSort (fun (x1 x2 : ℕ) => x1 ≤ x2) keys ∧ (compute keys).requests.length ≤ keys.length.choose 2 ∧ ∀ j ∈ (compute keys).requests, j + 1 < keys.length

Exact sorted original keys, at most choose P 2 requests, and every emitted request valid for the original length P. These facts hold for every raw binary key list.

theorem Cslib.AdjacentSortTrace.replay_compute {α : Type u_1} (key : α → ℕ) (xs : List α) :
List.foldl (fun (ys : List α) (j : ℕ) => ys.swap j (j + 1)) xs (compute (List.map key xs)).requests = List.insertionSort (fun (a b : α) => key a ≤ key b) xs

The literal requests replay stable insertion sort on any accompanying payload. Only its original key map is inspected; payloads, including equal-key occurrences, are retained.

theorem Cslib.AdjacentSortTrace.indices_payload (keys : List ℕ) :
have ids := List.foldl (fun (ys : List ℕ) (j : ℕ) => ys.swap j (j + 1)) (List.range keys.length) (compute keys).requests; ids.Perm (List.range keys.length) ∧ (∀ {α : Type u_1} (f : ℕ → α), List.foldl (fun (ys : List α) (j : ℕ) => ys.swap j (j + 1)) (List.map f (List.range keys.length)) (compute keys).requests = List.map f ids) ∧ ∀ (i j : ℕ), [i, j].Sublist (List.range keys.length) → keys.getD i 0 = keys.getD j 0 → [i, j].Sublist ids

The computed index word is a permutation of all original indices. Applying the SAME requests to any indexed payload gives that word mapped by the payload, not a chosen sorting permutation. Equal-key original-index pairs remain sublists, by the existing List.pair_sublist_insertionSort stability theorem.

theorem Cslib.AdjacentSortTrace.requests_eq_nil_of_pairwise (keys : List ℕ) (h : List.Pairwise (fun (x1 x2 : ℕ) => x1 ≤ x2) keys) :

Already sorted keys produce no requests at all; execution therefore preserves the ENTIRE raw payload State, rather than just canceling its value permutation.