This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathProofs.v
303 lines (276 loc) · 7.59 KB
/
Proofs.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
Require Import QuickSort.
Require Import Prelude.
Require Import GHC.DeferredFix.
Require Import Coq.Lists.List.
Import ListNotations.
Inductive AlreadySorted {a} `{Ord a} : list a -> Prop :=
| ASNil: AlreadySorted []
| ASCons: forall x xs,
AlreadySorted xs ->
Forall (fun y => (op_zl__ y x) = false) xs ->
AlreadySorted (x::xs).
Axiom unroll_deferred_fix: forall a r `{Default r} (f : (a -> r) -> (a -> r)),
deferredFix1 f = f (deferredFix1 f).
Lemma Forall_partition:
forall a p (xs : list a),
Forall (fun y => (p y) = false) xs ->
OldList.partition p xs = ([], xs).
Proof.
intros.
induction H.
* reflexivity.
* unfold OldList.partition in *.
simpl in *. rewrite IHForall.
unfold OldList.select.
rewrite H. reflexivity.
Qed.
Theorem quicksort_already_sorted:
forall a `(Ord a) (xs : list a),
AlreadySorted xs -> quicksort xs = xs.
Proof.
intros.
induction H1.
* unfold quicksort.
rewrite unroll_deferred_fix. reflexivity.
* change (quicksort (x :: xs) = [] ++ [x] ++ xs).
unfold quicksort.
rewrite unroll_deferred_fix.
rewrite Forall_partition by assumption.
f_equal;[|f_equal].
+ rewrite unroll_deferred_fix. reflexivity.
+ apply IHAlreadySorted.
Qed.
Require Import Coq.Sorting.Permutation.
Lemma partition_permutation:
forall a p (xs ys zs : list a),
OldList.partition p xs = (ys, zs) ->
Permutation xs (ys ++ zs).
Proof.
induction xs; intros.
* simpl in *. inversion_clear H. constructor.
* simpl in *. unfold OldList.select in *.
destruct (p a0).
- destruct (OldList.partition p xs) eqn:?.
inversion H.
apply Permutation_cons; [reflexivity|].
apply IHxs.
congruence.
- destruct (OldList.partition p xs) eqn:?.
inversion H.
apply Permutation_cons_app.
apply IHxs.
congruence.
Qed.
Lemma Forall_partition_l:
forall a p (xs ys zs : list a),
OldList.partition p xs = (ys, zs) ->
Forall (fun y => (p y) = true) ys.
Proof.
induction xs; intros.
* simpl in H. inversion H. auto.
* simpl in *. unfold OldList.select in *.
destruct (p a0) eqn:?.
- destruct (OldList.partition p xs) eqn:?.
inversion H. simpl.
constructor; try assumption.
apply (IHxs l zs).
congruence.
- destruct (OldList.partition p xs) eqn:?.
inversion H. simpl.
destruct zs. try congruence.
apply (IHxs ys zs).
congruence.
Qed.
Lemma Forall_partition_r:
forall a p (xs ys zs : list a),
OldList.partition p xs = (ys, zs) ->
Forall (fun y => (p y) = false) zs.
Proof.
induction xs; intros.
* simpl in H. inversion H. auto.
* simpl in *. unfold OldList.select in *.
destruct (p a0) eqn:?.
- destruct (OldList.partition p xs) eqn:?.
inversion H. simpl.
destruct ys. try congruence.
apply (IHxs ys zs).
congruence.
- destruct (OldList.partition p xs) eqn:?.
inversion H. simpl.
constructor; try assumption.
apply (IHxs ys l0).
congruence.
Qed.
Lemma partition_length_l:
forall a p (xs ys zs : list a),
OldList.partition p xs = (ys, zs) ->
Peano.le (length ys) (length xs).
Proof.
induction xs; intros.
* simpl in *. inversion_clear H. constructor.
* simpl in *. unfold OldList.select in *.
destruct (p a0).
- destruct (OldList.partition p xs) eqn:?.
inversion H.
simpl.
apply le_n_S.
apply (IHxs l zs).
congruence.
- destruct (OldList.partition p xs) eqn:?.
inversion H.
destruct zs. try congruence.
inversion H2.
simpl.
rewrite (IHxs ys zs).
omega.
congruence.
Qed.
Lemma partition_length_r:
forall a p (xs ys zs : list a),
OldList.partition p xs = (ys, zs) ->
Peano.le (length zs) (length xs).
Proof.
induction xs; intros.
* simpl in *. inversion_clear H. constructor.
* simpl in *. unfold OldList.select in *.
destruct (p a0).
- destruct (OldList.partition p xs) eqn:?.
inversion H.
destruct ys. try congruence.
inversion H2.
simpl.
rewrite (IHxs ys zs).
omega.
congruence.
- destruct (OldList.partition p xs) eqn:?.
inversion H.
simpl.
apply le_n_S.
apply (IHxs ys l0).
congruence.
Qed.
Theorem quicksort_permutation:
forall a `(Ord a) (xs : list a),
Permutation (quicksort xs) xs.
Proof.
intros.
remember (length xs) as n.
generalize dependent xs.
induction n using lt_wf_ind.
intros.
unfold quicksort.
rewrite unroll_deferred_fix.
destruct xs.
* apply perm_nil.
* destruct (OldList.partition (fun arg_1__ : a => _<_ arg_1__ a0) xs) eqn:?.
simpl app.
rewrite <- Permutation_middle.
apply Permutation_cons; [reflexivity|].
rewrite (partition_permutation _ _ _ _ _ Heqp).
apply Permutation_app.
- apply (H1 (length l)).
+ pose (partition_length_l _ _ _ _ _ Heqp).
subst n. simpl.
omega.
+ reflexivity.
- apply (H1 (length l0)).
+ pose (partition_length_r _ _ _ _ _ Heqp).
subst n. simpl.
omega.
+ reflexivity.
Qed.
Require Import Coq.Sorting.Sorted.
Require Import Coq.Sets.Relations_1.
Require Import Coq.Lists.List.
Lemma Forall_app:
forall a P (xs ys : list a),
Forall P xs -> Forall P ys ->
Forall P (xs ++ ys).
Proof.
intros. induction xs; try assumption. constructor.
inversion H; assumption.
apply IHxs. inversion H; assumption.
Qed.
Lemma Forall_Permutation:
forall a P (xs ys : list a),
Forall P ys -> Permutation xs ys ->
Forall P xs.
Proof.
intros.
induction H0.
* auto.
* constructor.
+ inversion_clear H. assumption.
+ apply IHPermutation. inversion_clear H. assumption.
* inversion_clear H. inversion_clear H1.
repeat (constructor; try assumption).
* auto.
Qed.
Section sorted.
Variable a : Type.
Variable eq : Eq_ a.
Variable ord : Ord a.
Definition R x y := x < y = true.
Variable trans : Transitive R.
Variable total : forall a b, R a b \/ R b a.
Lemma StronglySorted_app_cons:
forall (xs ys : list a) p,
StronglySorted R xs ->
StronglySorted R ys ->
Forall (fun x => R x p) xs ->
Forall (fun y => R p y) ys ->
StronglySorted R (xs ++ p :: ys).
Proof.
intros.
induction xs.
* simpl.
apply SSorted_cons.
- assumption.
- assumption.
* simpl. apply SSorted_cons.
- apply IHxs.
+ inversion H; assumption.
+ inversion H1; assumption.
- apply Forall_app.
inversion_clear H; assumption.
constructor.
inversion_clear H1; assumption.
refine (Forall_impl _ _ H2). intros.
refine (trans _ _ _ _ H3).
inversion_clear H1; assumption.
Qed.
Theorem quicksort_sorted:
forall (xs : list a), StronglySorted R (quicksort xs).
Proof.
intros.
remember (length xs) as n.
generalize dependent xs.
induction n using lt_wf_ind.
intros.
unfold quicksort.
rewrite unroll_deferred_fix.
destruct xs.
* apply SSorted_nil.
* destruct (OldList.partition (fun arg_1__ : a => _<_ arg_1__ a0) xs) eqn:?.
simpl app.
apply StronglySorted_app_cons.
- apply (H (length l)).
+ pose (partition_length_l _ _ _ _ _ Heqp).
subst n. simpl.
omega.
+ reflexivity.
- apply (H (length l0)).
+ pose (partition_length_r _ _ _ _ _ Heqp).
subst n. simpl.
omega.
+ reflexivity.
- refine (Forall_Permutation _ _ _ _ _ (quicksort_permutation _ _ _)).
apply (Forall_partition_l _ _ _ _ _ Heqp).
- refine (Forall_Permutation _ _ _ _ _ (quicksort_permutation _ _ _)).
specialize (Forall_partition_r _ _ _ _ _ Heqp).
apply Forall_impl.
intros.
destruct (total a0 a1); [assumption|congruence].
Qed.
End sorted.
Print Assumptions quicksort_sorted.