Skip to content

Commit 90e1538

Browse files
kim-emfgdorais
authored andcommitted
fix: patch for std4#579 (#11347)
This if @fgdorais's patch for leanprover-community/batteries#579. Co-authored-by: F. G. Dorais <[email protected]> Co-authored-by: Scott Morrison <[email protected]>
1 parent d5bdfba commit 90e1538

File tree

2 files changed

+15
-33
lines changed

2 files changed

+15
-33
lines changed

Mathlib/Data/List/Sort.lean

+11-29
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,6 @@ theorem perm_split : ∀ {l l₁ l₂ : List α}, split l = (l₁, l₂) → l ~
412412
exact ((perm_split e).trans perm_append_comm).cons a
413413
#align list.perm_split List.perm_split
414414

415-
/-- Merge two sorted lists into one in linear time.
416-
417-
merge [1, 2, 4, 5] [0, 1, 3, 4] = [0, 1, 1, 2, 3, 4, 4, 5] -/
418-
def merge : List α → List α → List α
419-
| [], l' => l'
420-
| l, [] => l
421-
| a :: l, b :: l' => if a ≼ b then a :: merge l (b :: l') else b :: merge (a :: l) l'
422-
termination_by l₁ l₂ => length l₁ + length l₂
423415
#align list.merge List.merge
424416

425417
/-- Implementation of a merge sort algorithm to sort a list. -/
@@ -433,28 +425,18 @@ def mergeSort : List α → List α
433425
have h := length_split_lt e
434426
have := h.1
435427
have := h.2
436-
exact merge r (mergeSort ls.1) (mergeSort ls.2)
428+
exact merge (r · ·) (mergeSort ls.1) (mergeSort ls.2)
437429
termination_by l => length l
438430
#align list.merge_sort List.mergeSort
439431

440432
@[nolint unusedHavesSuffices] -- Porting note: false positive
441433
theorem mergeSort_cons_cons {a b} {l l₁ l₂ : List α} (h : split (a :: b :: l) = (l₁, l₂)) :
442-
mergeSort r (a :: b :: l) = merge r (mergeSort r l₁) (mergeSort r l₂) := by
434+
mergeSort r (a :: b :: l) = merge (r · ·) (mergeSort r l₁) (mergeSort r l₂) := by
443435
simp only [mergeSort, h]
444436
#align list.merge_sort_cons_cons List.mergeSort_cons_cons
445437

446438
section Correctness
447439

448-
theorem perm_merge : ∀ l l' : List α, merge r l l' ~ l ++ l'
449-
| [], [] => by simp [merge]
450-
| [], b :: l' => by simp [merge]
451-
| a :: l, [] => by simp [merge]
452-
| a :: l, b :: l' => by
453-
by_cases h : a ≼ b
454-
· simpa [merge, h] using perm_merge _ _
455-
· suffices b :: merge r (a :: l) l' ~ a :: (l ++ b :: l') by simpa [merge, h]
456-
exact ((perm_merge _ _).cons _).trans ((swap _ _ _).trans (perm_middle.symm.cons _))
457-
termination_by l₁ l₂ => length l₁ + length l₂
458440
#align list.perm_merge List.perm_merge
459441

460442
theorem perm_mergeSort : ∀ l : List α, mergeSort r l ~ l
@@ -464,7 +446,7 @@ theorem perm_mergeSort : ∀ l : List α, mergeSort r l ~ l
464446
cases' e : split (a :: b :: l) with l₁ l₂
465447
cases' length_split_lt e with h₁ h₂
466448
rw [mergeSort_cons_cons r e]
467-
apply (perm_merge r _ _).trans
449+
apply (perm_merge (r · ·) _ _).trans
468450
exact
469451
((perm_mergeSort l₁).append (perm_mergeSort l₂)).trans (perm_split e).symm
470452
termination_by l => length l
@@ -479,14 +461,14 @@ section TotalAndTransitive
479461

480462
variable {r} [IsTotal α r] [IsTrans α r]
481463

482-
theorem Sorted.merge : ∀ {l l' : List α}, Sorted r l → Sorted r l' → Sorted r (merge r l l')
483-
| [], [], _, _ => by simp [List.merge]
484-
| [], b :: l', _, h₂ => by simpa [List.merge] using h₂
485-
| a :: l, [], h₁, _ => by simpa [List.merge] using h₁
464+
theorem Sorted.merge : ∀ {l l' : List α}, Sorted r l → Sorted r l' → Sorted r (merge (r · ·) l l')
465+
| [], [], _, _ => by simp
466+
| [], b :: l', _, h₂ => by simpa using h₂
467+
| a :: l, [], h₁, _ => by simpa using h₁
486468
| a :: l, b :: l', h₁, h₂ => by
487469
by_cases h : a ≼ b
488-
· suffices ∀ b' ∈ List.merge r l (b :: l'), r a b' by
489-
simpa [List.merge, h, h₁.of_cons.merge h₂]
470+
· suffices ∀ b' ∈ List.merge (r · ·) l (b :: l'), r a b' by
471+
simpa [h, h₁.of_cons.merge h₂]
490472
intro b' bm
491473
rcases show b' = b ∨ b' ∈ l ∨ b' ∈ l' by
492474
simpa [or_left_comm] using (perm_merge _ _ _).subset bm with
@@ -495,8 +477,8 @@ theorem Sorted.merge : ∀ {l l' : List α}, Sorted r l → Sorted r l' → Sort
495477
assumption
496478
· exact rel_of_sorted_cons h₁ _ bl
497479
· exact _root_.trans h (rel_of_sorted_cons h₂ _ bl')
498-
· suffices ∀ b' ∈ List.merge r (a :: l) l', r b b' by
499-
simpa [List.merge, h, h₁.merge h₂.of_cons]
480+
· suffices ∀ b' ∈ List.merge (r · ·) (a :: l) l', r b b' by
481+
simpa [h, h₁.merge h₂.of_cons]
500482
intro b' bm
501483
have ba : b ≼ a := (total_of r _ _).resolve_left h
502484
have : b' = a ∨ b' ∈ l ∨ b' ∈ l' := by simpa using (perm_merge _ _ _).subset bm

lake-manifest.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[{"url": "https://github.com/leanprover/std4",
55
"type": "git",
66
"subDir": null,
7-
"rev": "ff9850c4726f6b9fb8d8e96980c3fcb2900be8bd",
7+
"rev": "f3447c3732c9d6e8df3bdad78e5ecf7e8b353bbc",
88
"name": "std",
99
"manifestFile": "lake-manifest.json",
1010
"inputRev": "main",
@@ -22,7 +22,7 @@
2222
{"url": "https://github.com/leanprover-community/aesop",
2323
"type": "git",
2424
"subDir": null,
25-
"rev": "056ca0fa8f5585539d0b940f532d9750c3a2270f",
25+
"rev": "8be30c25e3caa06937feeb62f7ca898370f80ee9",
2626
"name": "aesop",
2727
"manifestFile": "lake-manifest.json",
2828
"inputRev": "master",
@@ -40,7 +40,7 @@
4040
{"url": "https://github.com/leanprover/lean4-cli",
4141
"type": "git",
4242
"subDir": null,
43-
"rev": "a751d21d4b68c999accb6fc5d960538af26ad5ec",
43+
"rev": "be8fa79a28b8b6897dce0713ef50e89c4a0f6ef5",
4444
"name": "Cli",
4545
"manifestFile": "lake-manifest.json",
4646
"inputRev": "main",
@@ -49,7 +49,7 @@
4949
{"url": "https://github.com/leanprover-community/import-graph.git",
5050
"type": "git",
5151
"subDir": null,
52-
"rev": "64d082eeaad1a8e6bbb7c23b7a16b85a1715a02f",
52+
"rev": "bbcffbcc19d69e13d5eea716283c76169db524ba",
5353
"name": "importGraph",
5454
"manifestFile": "lake-manifest.json",
5555
"inputRev": "main",

0 commit comments

Comments
 (0)