-
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
variable [LE α] [DecidableLE α] [Std.IsLinearOrder α] [BEq α] [LawfulBEq α] (xs : List α)
@[grind]
def insert (a : α)
| [] => [a]
| x :: xs =>
if a ≤ x then
a :: x :: xs
else
x :: insert a xs
@[grind]
def insertionSort : List α → List α
| [] => []
| x :: xs => insert x (insertionSort xs)
@[grind]
def Sorted : List α → Prop
| [] | [_] => True
| x :: x' :: xs => x ≤ x' ∧ Sorted (x' :: xs)
/-
This also works:
inductive Sorted : List α → Prop where
| nil : Sorted []
| single x : Sorted [x]
| cons_cons x x' xs : x ≤ x' → Sorted (x' :: xs) → Sorted (x :: x' :: xs)
-/
theorem insertCorrect x : (Sorted xs → Sorted (insert x xs)) ∧ (x :: xs).Perm (insert x xs) := by
induction xs with
| nil => grind
| cons _ t => cases t <;> grind
theorem insertionSortCorrect : Sorted (insertionSort xs) ∧ xs.Perm (insertionSort xs) := by
induction xs with
| nil => grind
| cons h t => grind [insertCorrect (insertionSort t) h]