Documentation

CslibFrontier.Foundations.Data.List.Swap

Adjacent exchange and mapping #

These characterizations use the existing total List.swap; out-of-range exchanges retain all data. They connect stable sorting traces to their accompanying value words.

theorem List.swap_adjacent_eq_take_drop {α : Type u_1} (xs : List α) (j : ℕ) :
xs.swap j (j + 1) = if h : j + 1 < xs.length then take j xs ++ [xs[j + 1], xs[j]] ++ drop (j + 2) xs else xs

An adjacent exchange has the usual take/two-entries/drop form, with whole-list identity when the second position is out of range.

theorem List.map_swap {α : Type u_1} {β : Type u_2} (f : α → β) (xs : List α) (i j : ℕ) :
map f (xs.swap i j) = (map f xs).swap i j

Mapping a payload commutes with the existing total exchange, including its invalid index identity branches.