File tree Expand file tree Collapse file tree 4 files changed +45
-0
lines changed Expand file tree Collapse file tree 4 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -2912,6 +2912,11 @@ theorem toList_toFinset [DecidableEq α] (s : Finset α) : s.toList.toFinset = s
2912
2912
ext
2913
2913
simp
2914
2914
2915
+ theorem _root_.List.toFinset_toList [DecidableEq α] {s : List α} (hs : s.Nodup) :
2916
+ s.toFinset.toList.Perm s := by
2917
+ apply List.perm_of_nodup_nodup_toFinset_eq (nodup_toList _) hs
2918
+ rw [toList_toFinset]
2919
+
2915
2920
@[simp]
2916
2921
theorem toList_eq_singleton_iff {a : α} {s : Finset α} : s.toList = [a] ↔ s = {a} := by
2917
2922
rw [toList, Multiset.toList_eq_singleton_iff, val_eq_singleton_iff]
Original file line number Diff line number Diff line change @@ -32,6 +32,10 @@ variable (r : α → α → Prop) [DecidableRel r] [IsTrans α r] [IsAntisymm α
32
32
def sort (s : Finset α) : List α :=
33
33
Multiset.sort r s.1
34
34
35
+ @[simp]
36
+ theorem sort_val (s : Finset α) : Multiset.sort r s.val = sort r s :=
37
+ rfl
38
+
35
39
@[simp]
36
40
theorem sort_sorted (s : Finset α) : List.Sorted r (sort r s) :=
37
41
Multiset.sort_sorted _ _
@@ -64,11 +68,27 @@ theorem sort_empty : sort r ∅ = [] :=
64
68
theorem sort_singleton (a : α) : sort r {a} = [a] :=
65
69
Multiset.sort_singleton r a
66
70
71
+ theorem sort_cons {a : α} {s : Finset α} (h₁ : ∀ b ∈ s, r a b) (h₂ : a ∉ s) :
72
+ sort r (cons a s h₂) = a :: sort r s := by
73
+ rw [sort, cons_val, Multiset.sort_cons r a _ h₁, sort_val]
74
+
75
+ theorem sort_insert [DecidableEq α] {a : α} {s : Finset α} (h₁ : ∀ b ∈ s, r a b) (h₂ : a ∉ s) :
76
+ sort r (insert a s) = a :: sort r s := by
77
+ rw [← cons_eq_insert _ _ h₂, sort_cons r h₁]
78
+
67
79
open scoped List in
68
80
theorem sort_perm_toList (s : Finset α) : sort r s ~ s.toList := by
69
81
rw [← Multiset.coe_eq_coe]
70
82
simp only [coe_toList, sort_eq]
71
83
84
+ theorem _root_.List.toFinset_sort [DecidableEq α] {l : List α} (hl : l.Nodup) :
85
+ sort r l.toFinset = l ↔ l.Sorted r := by
86
+ refine ⟨?_, List.eq_of_perm_of_sorted ((sort_perm_toList r _).trans (List.toFinset_toList hl))
87
+ (sort_sorted r _)⟩
88
+ intro h
89
+ rw [← h]
90
+ exact sort_sorted r _
91
+
72
92
end sort
73
93
74
94
section SortLinearOrder
Original file line number Diff line number Diff line change @@ -198,6 +198,10 @@ def orderedInsert (a : α) : List α → List α
198
198
| [] => [a]
199
199
| b :: l => if a ≼ b then a :: b :: l else b :: orderedInsert a l
200
200
201
+ theorem orderedInsert_of_le {a b : α} (l : List α) (h : a ≼ b) :
202
+ orderedInsert r a (b :: l) = a :: b :: l :=
203
+ dif_pos h
204
+
201
205
/-- `insertionSort l` returns `l` sorted using the insertion sort algorithm. -/
202
206
@[simp]
203
207
def insertionSort : List α → List α
@@ -281,6 +285,17 @@ theorem mem_insertionSort {l : List α} {x : α} : x ∈ l.insertionSort r ↔ x
281
285
theorem length_insertionSort (l : List α) : (insertionSort r l).length = l.length :=
282
286
(perm_insertionSort r _).length_eq
283
287
288
+ theorem insertionSort_cons {a : α} {l : List α} (h : ∀ b ∈ l, r a b) :
289
+ insertionSort r (a :: l) = a :: insertionSort r l := by
290
+ rw [insertionSort]
291
+ cases hi : insertionSort r l with
292
+ | nil => rfl
293
+ | cons b m =>
294
+ rw [orderedInsert_of_le]
295
+ apply h b <| (mem_insertionSort r).1 _
296
+ rw [hi]
297
+ exact mem_cons_self b m
298
+
284
299
theorem map_insertionSort (f : α → β) (l : List α) (hl : ∀ a ∈ l, ∀ b ∈ l, a ≼ b ↔ f a ≼ f b) :
285
300
(l.insertionSort r).map f = (l.map f).insertionSort s := by
286
301
induction l with
Original file line number Diff line number Diff line change @@ -62,6 +62,11 @@ theorem map_sort (f : α → β) (s : Multiset α)
62
62
revert s
63
63
exact Quot.ind fun _ => List.map_mergeSort' _ _ _ _
64
64
65
+ theorem sort_cons (a : α) (s : Multiset α) :
66
+ (∀ b ∈ s, r a b) → sort r (a ::ₘ s) = a :: sort r s := by
67
+ refine Quot.inductionOn s fun l => ?_
68
+ simpa [mergeSort'_eq_insertionSort] using insertionSort_cons r
69
+
65
70
end sort
66
71
67
72
-- TODO: use a sort order if available, gh-18166
You can’t perform that action at this time.
0 commit comments