-
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
-
69
-
70
import Mathlib.Order.Lattice
variable [LinearOrder α]
def ins (a : α) : List α → List α
| [] => [a]
| x :: xs =>
if a ≤ x then a :: x :: xs else x :: ins a xs
def List.insSort : List α → List α
| [] => []
| x :: xs => ins x xs.insSort
def Sorted : List α → Prop
| [] | [_] => True
| x :: x' :: xs => x ≤ x' ∧ Sorted (x' :: xs)
theorem insSorted (a : α) (xs : List α) : Sorted xs → Sorted (ins a xs) := by
induction xs
case nil => simp [Sorted, ins]
case cons x xs ih =>
cases xs
case nil =>
by_cases h₁ : a ≤ x
· simp [ins, h₁, Sorted]
· simp only [Sorted, ins, h₁, ↓reduceIte, and_true, forall_const]
exact le_of_lt <| lt_of_not_ge h₁
case cons x' t =>
by_cases h₁ : a ≤ x
· simp [ins, h₁, Sorted]
· by_cases h₂ : a ≤ x'
· simp only [Sorted, ins, h₁, ↓reduceIte, h₂, true_and, and_imp]
intro h₃ h₄
constructor
· exact le_of_lt <| lt_of_not_ge h₁
· exact h₄
· simp only [Sorted, ins, h₁, ↓reduceIte, h₂, and_imp]
intro h₃ h₄
constructor
· exact h₃
· have ih₂ := ih h₄
simp only [ins, h₂, ↓reduceIte] at ih₂
exact ih₂
theorem insSortSorted (xs : List α) : Sorted xs.insSort := by
induction xs
case nil => simp [List.insSort, Sorted]
case cons x xs ih =>
rw [List.insSort]
exact (insSorted x xs.insSort) ih
theorem insPerm (xs : List α) (a : α) : (a :: xs).Perm (ins a xs) := by
induction xs
case nil => rw [ins]
case _ x xs ih =>
rw [ins]
by_cases h₁ : a ≤ x
· simp [h₁]
· simp only [h₁]
exact .trans (.swap x a xs) (.cons x ih)
theorem insSortPerm (xs : List α) : xs.Perm xs.insSort := by
induction xs
case nil => rw [List.insSort]
case cons h t ih =>
rw [List.insSort]
exact .trans (.cons h ih) (insPerm t.insSort h)
theorem insSortCorrect (xs : List α) : Sorted xs.insSort ∧ xs.Perm xs.insSort :=
⟨insSortSorted xs, insSortPerm xs⟩