Skip to content

Commit 4d90196

Browse files
committed
separate definition of stability into ListComparisonSort.lean
1 parent 4afb85c commit 4d90196

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

Cslib/AlgorithmsTheory/Algorithms/MergeSort.lean

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,13 @@ private lemma mergeSortNaive_stable_equiv
265265
rw [List.take_append_drop]
266266

267267
theorem mergeSort_stable
268-
(xs : List α) (le : α → α → Bool)
268+
(xs : List α)
269+
(le : α → α → Bool)
269270
[Std.Total (fun x y => le x y = true)]
270271
[IsTrans _ (fun x y => le x y = true)] :
271-
let ys := (mergeSort xs).eval (sortModelNat le)
272-
∀ k : α, ys.filter (fun x => le x k && le k x) = xs.filter (fun x => le x k && le k x) := by
273-
simpa [mergeSort_eval] using (mergeSortNaive_stable_equiv xs le)
272+
IsStableSort (fun xs => (mergeSort xs).eval (sortModelNat le)) xs le := by
273+
intro k
274+
simpa [mergeSort_eval] using (mergeSortNaive_stable_equiv xs le k)
274275

275276
end Stability
276277

Cslib/AlgorithmsTheory/Models/ListComparisonSort.lean

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public import Mathlib.Tactic.FastInstance
2020
In this file we define two query types `SortOps` which is suitable for insertion sort, and
2121
`SortOps`for comparison based searching in Lists. We define a model `sortModel` for `SortOps`
2222
which uses a custom cost structure `SortOpsCost`. We define a model `sortModelCmp` for `SortOpsCmp`
23-
which defines a `ℕ` based cost structure.
23+
which defines a `ℕ` based cost structure. We also define a notion of stability for sorting
24+
algorithms in lists.
2425
--
2526
## Definitions
2627
@@ -30,6 +31,7 @@ which defines a `ℕ` based cost structure.
3031
- `SortOpsCmp`: A query type for comparison based sorting that only includes a comparison query.
3132
This is more suitable for comparison based sorts for which it is only desirable to count
3233
comparisons
34+
- `IsStableSort`: A definition of stability for sorting algorithms in lists.
3335
3436
-/
3537
namespace Cslib
@@ -145,6 +147,18 @@ def sortModelNat {α : Type*}
145147

146148
end NatModel
147149

150+
section SortStability
151+
152+
/-- Definition of a stable sorting algorithm. -/
153+
def IsStableSort
154+
(sortAlg : List α → List α)
155+
(xs : List α)
156+
(le : α → α → Bool) : Prop :=
157+
let ys := sortAlg xs
158+
∀ (k : α), ys.filter (fun x => le x k && le k x) = xs.filter (fun x => le x k && le k x)
159+
160+
end SortStability
161+
148162
end Algorithms
149163

150164
end Cslib

0 commit comments

Comments
 (0)