-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
import Std.Tactic.Do
import Mathlib
def BubbleSort [LT α] [DecidableLT α] (A : Array α) := Id.run do
let N := A.size
let mut A := A.toVector
for i in List.range (N - 1) do
for hj : j in List.range (N - i - 1) do
have := List.mem_range.mp hj
if A[j + 1] < A[j] then
A := A.swap j (j + 1)
return A.toArray
def ICan'tBelieveItCanSort [LT α] [DecidableLT α] (A : Array α) := Id.run do
let N := A.size
let mut A := A.toVector
for hi : i in [:N] do
for hj : j in [:N] do
if A[i] < A[j] then
A := A.swap i j
return A.toArray
open Std.Do
theorem perm_ICan'tBelieveItCanSort [LT α] [DecidableLT α] (A : Array α) : ICan'tBelieveItCanSort.{0} A |>.Perm A := by
suffices Multiset.ofList (ICan'tBelieveItCanSort.{0} A).toList = A.toList by
rw [Multiset.coe_eq_coe] at this
exact { toList := this }
generalize h : ICan'tBelieveItCanSort A = x
apply Id.of_wp_run_eq h
mvcgen
case inv1 => exact ⇓⟨_, A'⟩ => ⌜Multiset.ofList A.toList = A'.toList⌝
all_goals try grind
case vc1.step.isTrue =>
expose_names
mleave
simp_all only [Multiset.coe_eq_coe]
have h₁ := Membership.get_elem_helper (Spec.forIn'_range._proof_4 pref cur suff h_1) rfl
have h₂ := Membership.get_elem_helper (Spec.forIn'_range._proof_4 pref_1 cur_1 suff_1 h_3) rfl
exact h_5.trans <| .symm <| Vector.perm_iff_toList_perm.mp <| Vector.swap_perm h₁ h₂
theorem sorted_ICan'tBelieveItCanSort [LT α] [DecidableLT α] (A : Array α) : ICan'tBelieveItCanSort.{0} A |>.Pairwise (¬ · > ·) := by
generalize h : ICan'tBelieveItCanSort A = x
apply Id.of_wp_run_eq h
mvcgen
case inv1 => exact ⇓⟨xs, A'⟩ => ⌜A'.take xs.prefix.length |>.toArray.Pairwise (¬ · > ·)⌝
case vc1.step.isTrue =>
expose_names
simp_all
grind
sorry
case vc2.step.isFalse =>
expose_names
simp_all
grind
sorry
case vc3.step.pre => grind
case vc4.step.post.success =>
expose_names
simp_all
grind
sorry
case vc5.a.pre => grind
case vc6.a.post.success =>
expose_names
simp_all
have : r.toArray = r.toArray.extract 0 N := by grind
rwa [this]