-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinsertion_sort.v
58 lines (41 loc) · 1.49 KB
/
insertion_sort.v
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
From Coq Require Import List Lia Arith Permutation.
Import ListNotations.
(* Let's try to verify a simple sorting algorithm! *)
(* If anyone wants to know why one would verify such a simple thing, try googling "timsort bug"... *)
Section Sorting.
Check (list nat).
(* This is one of many possible definitions of Sorted.
It's a useful exercise to prove they are equivalent! *)
Inductive Sorted : list nat -> Prop :=
| nil_sorted : Sorted []
| singleton_sorted : forall a, Sorted [a]
| pair_sorted : forall a b l, a <= b -> Sorted (b::l) -> Sorted (a::b::l).
Fixpoint insert (a : nat) (l : list nat) : list nat :=
match l with
| [] => [a]
| b::bs => if (a <=? b) then a::l
else b::(insert a bs)
end.
Fixpoint sort (l : list nat) : list nat :=
match l with
| [] => []
| b :: bs => insert b (sort bs)
end.
Eval compute in (sort [3;4;5;2;5;6;3;2;2;46;87;8;0]).
(* Search (_ <=? _). *)
(* Search (BoolSpec). *)
(* Search Bool.reflect. *)
(* Search (_ < _ -> _ <= _). *)
(* Our goal is this: *)
Theorem sorted_sort : forall l, Sorted (sort l).
Proof.
Abort.
(* If you're struggling, that's normal! We need an extra lemma to make this proof go through... *)
(* Same remark as for Sorted: there are many possible equivalent definitions here! *)
Print Permutation.
(* Search Permutation. *)
Notation "l1 ~ l2" := (Permutation l1 l2)(at level 80).
Theorem perm_sort : forall l, l ~ sort l.
Proof.
Abort.
End Sorting.