-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqsort_sub.v
59 lines (53 loc) · 1.53 KB
/
qsort_sub.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
59
(********************************************************************************)
(* Quicksort (subset types) *)
From sortalgs Require Import sorted.
Open Scope list_scope.
Import List.ListNotations.
Require Import Program.
Lemma lem_partition `{DecTotalOrder} :
forall x l1 l2, Sorted l1 -> Sorted l2 -> GeLst x l1 -> LeLst x l2 ->
Sorted (l1 ++ x :: l2).
Proof.
induction l1.
- inversion 4; sauto lq: on.
- inversion 1; inversion 2; hauto use: lem_sorted_tail ctrs: Sorted.
Qed.
Program Fixpoint partition {A} `{DecTotalOrder A} (x : A) (l : list A)
{measure (length l)} :
{ (l1, l2) : list A * list A |
GeLst x l1 /\ LeLst x l2 /\ Permutation l (l1 ++ l2) } :=
match l with
| [] => ([], [])
| h :: t =>
match partition x t with
| (t1, t2) =>
if leb_total_dec h x then
(h :: t1, t2)
else
(t1, h :: t2)
end
end.
Solve Obligations with sauto use: Permutation_middle.
Program Fixpoint qsort {A} `{DecTotalOrder A} (l : list A) {measure (length l)}
: {l' | Sorted l' /\ Permutation l l'} :=
match l with
| [] => []
| h :: t =>
match partition h t with
| (t1, t2) => qsort t1 ++ [h] ++ qsort t2
end
end.
Next Obligation.
sauto.
Qed.
Next Obligation.
hauto use: Permutation_length db: list.
Qed.
Next Obligation.
hauto use: Permutation_length db: list.
Qed.
Next Obligation.
destruct_sigma; simp_hyps; split.
- eauto using lem_gelst_perm, lem_lelst_perm, lem_partition.
- eauto using perm_trans, perm_skip, Permutation_middle, Permutation_app.
Qed.