-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssort_sub.v
77 lines (71 loc) · 1.65 KB
/
ssort_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(* Selection sort (subset types) *)
From sortalgs Require Import sorted.
Require List.
Import ListNotations.
Require Import Program.
Ltac solve_select :=
destruct_sigma;
simp_hyps;
split; [
match goal with
| [ H : Permutation _ _ |- _ ] =>
clear -H; hauto ctrs: Permutation
end
| match goal with
| [H : Permutation (?x :: ?t) (?x1 :: ?x2) |- _ ] =>
assert (leb x1 x);
[ let H0 := fresh "H" in
assert (H0: LeLst x1 (x1 :: x2)) by sauto use: (leb_refl x1);
assert (LeLst x1 (x :: t));
[ clear -H0 H; hauto db: lelst | sauto ] | sauto ]
end ].
Program Fixpoint select {A} `{DecTotalOrder A} (x : A) (l : list A)
{measure (length l)}
: { (y, l') : A * list A |
Permutation (x :: l) (y :: l') /\ LeLst y l' /\
length l' = length l } :=
match l with
| h :: t =>
if leb_total_dec x h then
match select x t with
| (y, l2) => (y, h :: l2)
end
else
match select h t with
| (y, l2) => (y, x :: l2)
end
| [] =>
(x, [])
end.
Next Obligation.
solve_select.
Qed.
Next Obligation.
solve_select.
Qed.
Next Obligation.
sauto.
Qed.
Program Fixpoint ssort {A} `{DecTotalOrder A} (l : list A) {measure (length l)}
: { l' : list A | Sorted l' /\ Permutation l' l } :=
match l with
| h :: t =>
match select h t with
| (x, l2) => x :: ssort l2
end
| [] => []
end.
Next Obligation.
sauto.
Qed.
Next Obligation.
destruct_sigma.
simp_hyps.
split.
- rewrite lem_lelst_sorted.
qauto use: Permutation_sym db: lelst.
- hauto use: Permutation_sym, Permutation_cons, perm_trans.
Qed.
Next Obligation.
sauto.
Qed.