-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.v
392 lines (351 loc) · 8.65 KB
/
Util.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
Require Import List.
Require Import Arith Omega.
Require Import Wf_nat.
Set Implicit Arguments.
Class Decidable A :=
{
eq_dec : forall (x y : A), {x = y} + {x <> y}
}.
Instance nat_Decidable : Decidable nat.
constructor.
decide equality.
Defined.
Section ListUtil.
Variable A B : Type.
Context `{DA : Decidable A}.
Definition In_dec : forall (x : A) (xs : list A), {In x xs} + {~In x xs}.
intro x.
induction xs as [| a xs]; simpl; auto.
destruct IHxs; auto.
destruct (eq_dec a x); tauto.
Defined.
Definition NoDup_dec : forall xs : list A, {NoDup xs} + {~NoDup xs}.
induction xs as [| x xs].
- left. apply NoDup_nil.
- destruct IHxs.
+ destruct (In_dec x xs).
* right.
now inversion 1.
* left.
now constructor.
+ right.
now inversion 1.
Defined.
Fixpoint map_index_rec
(f : nat -> A -> B) (xs : list A) (i : nat) : list B :=
match xs with
| nil => nil
| x :: xs' => f i x :: map_index_rec f xs' (S i)
end.
Definition map_index (f : nat -> A -> B) (xs : list A) : list B :=
map_index_rec f xs 0.
Lemma map_index_equation : forall f xs,
map_index f xs =
match xs with
| nil => nil
| x :: xs' => f 0 x :: map_index (fun i => f (S i)) xs'
end.
Proof.
intros f xs.
unfold map_index.
generalize 0 as n.
induction xs as [| x xs].
- auto.
- intro n.
simpl.
f_equal.
rewrite (IHxs (S n)).
destruct xs as [| y xs].
+ auto.
+ simpl.
reflexivity.
Qed.
Lemma map_index_length : forall f xs, length (map_index f xs) = length xs.
Proof.
intros f xs.
revert f.
induction xs.
- auto.
- intro f.
rewrite map_index_equation.
simpl.
rewrite IHxs.
reflexivity.
Qed.
Lemma map_index_nth : forall f xs n db da,
n < length xs -> nth n (map_index f xs) db = f n (nth n xs da).
Proof.
intros until da.
revert f n.
induction xs as [| x xs].
- intros f n Hlt.
inversion Hlt.
- destruct n as [| n].
+ reflexivity.
+ intro Hlt.
rewrite map_index_equation.
simpl.
rewrite IHxs; auto.
simpl in Hlt.
omega.
Qed.
End ListUtil.
Section NthMod.
Variable A : Type.
Definition nth_mod (n: nat) (xs: list A) (d : A) : A :=
nth (n mod length xs) xs d.
Lemma nth_mod_indep : forall n xs d d',
xs <> nil ->
nth_mod n xs d = nth_mod n xs d'.
Proof.
intros until d'. intro H.
unfold nth_mod.
apply nth_indep.
apply Nat.mod_upper_bound.
destruct xs; auto.
discriminate.
Qed.
End NthMod.
Ltac cut_hyp H :=
refine ((fun p pq => pq (H p)) _ _); clear H; [| intro H].
Fixpoint nat_sum xs :=
match xs with
| nil => 0
| x :: xs' => x + nat_sum xs'
end.
Lemma nat_sum_app : forall xs ys, nat_sum (xs ++ ys) = nat_sum xs + nat_sum ys.
Proof.
intros.
induction xs; simpl; auto.
omega.
Qed.
Lemma nat_sum_in : forall xs x ys, nat_sum (xs ++ x :: ys) = x + nat_sum xs + nat_sum ys.
Proof.
intros.
rewrite nat_sum_app.
simpl.
omega.
Qed.
Lemma nat_sum_rev : forall xs, nat_sum (rev xs) = nat_sum xs.
Proof.
intros.
induction xs; simpl; auto.
rewrite nat_sum_in; simpl.
omega.
Qed.
Lemma map_sum_add {A} : forall (f g : A -> nat) xs,
nat_sum (map (fun x => f x + g x) xs) = nat_sum (map f xs) + nat_sum (map g xs).
Proof.
intros.
induction xs; simpl; auto.
rewrite IHxs.
omega.
Qed.
Lemma map_sum_sub {A} : forall (f g : A -> nat) xs,
(forall x, In x xs -> g x <= f x) ->
nat_sum (map (fun x => f x - g x) xs) = nat_sum (map f xs) - nat_sum (map g xs).
Proof.
intros f g.
induction xs; intro Hle; simpl; auto.
rewrite IHxs.
- rewrite Nat.sub_add_distr.
rewrite Nat.add_sub_swap.
+ rewrite Nat.add_sub_assoc; auto.
cut (forall x,In x xs -> g x <= f x); try solve [intuition].
clear.
induction xs; simpl; auto.
intro H.
apply le_trans with (f a + nat_sum (map g xs)).
* apply Nat.add_le_mono_r.
intuition.
* apply Nat.add_le_mono_l.
apply IHxs.
intuition.
+ apply Hle.
simpl.
auto.
- intros x HIn.
apply Hle.
simpl.
auto.
Qed.
Lemma map_sum_mul_distr {A} : forall (f : A -> nat) xs a,
nat_sum (map f xs) * a = nat_sum (map (fun x => f x * a) xs).
Proof.
intros f xs a.
induction xs as [| x xs]; simpl; auto.
rewrite <- IHxs.
ring.
Qed.
Lemma list_length_ind {A} :
forall
(P : list A -> Prop)
(H : forall (l : nat) (IH : forall xs, length xs < l -> P xs),
forall xs, length xs = l -> P xs),
forall xs, P xs.
Proof.
intros P H.
intro xs.
remember (length xs) as l.
revert xs Heql.
induction l using lt_wf_ind.
intros xs Hl.
specialize (H l).
apply H; auto.
intros l' Hlt.
apply (H0 (length l')); auto.
Qed.
Lemma pigeon : forall xs n,
n < length xs -> (forall x, In x xs -> x < n) -> ~NoDup xs.
Proof.
induction xs using list_length_ind.
intros n Hlt Hbound.
destruct l as [| l].
- destruct xs; simpl in *; omega.
- destruct n as [| n].
+ destruct xs; try discriminate.
specialize (Hbound n).
simpl in Hbound.
intuition.
+ destruct (In_dec n xs) as [HIn | HnIn].
* intro HND.
apply in_split in HIn.
destruct HIn as (xl & xr & Heqxs). subst.
apply NoDup_remove in HND.
specialize (IH (xl ++ xr)).
rewrite app_length in *.
simpl in *.
cut_hyp IH; try omega.
specialize (IH n).
cut_hyp IH; try omega.
cut_hyp IH; try tauto.
intros x HIn.
specialize (Hbound x).
rewrite in_app_iff in *.
simpl in *.
cut_hyp Hbound; try tauto.
cut (x <> n); try omega.
intro.
now subst.
* inversion 1 as [|x xs' HnIn' HND]; subst; try discriminate.
simpl in *.
specialize (IH xs').
cut_hyp IH; try omega.
specialize (IH n).
cut_hyp IH; try omega.
cut_hyp IH; try tauto.
intros y HIny.
specialize (Hbound y).
intuition.
cut (y <> n); try omega.
intro.
now subst.
Qed.
Lemma inv_pigeon : forall xs,
NoDup xs -> (forall x, In x xs -> x < length xs) ->
forall x, x < length xs -> In x xs.
Proof.
intro xs.
remember (length xs) as len.
revert xs Heqlen.
induction len.
- intros.
omega.
- intros xs Hlen HND Hbound x Hlt.
assert (In len xs) as HIn. {
destruct (In_dec len xs); auto.
contradict HND.
apply pigeon with (n := len); try omega.
intros x' HIn.
specialize (Hbound x' HIn).
cut (x' <> len); try omega.
intro.
now subst.
}
inversion Hlt; subst; auto.
apply in_split in HIn.
destruct HIn as (xl & xr & Hxs).
subst.
specialize (IHlen (xl ++ xr)).
rewrite app_length in *.
simpl in *.
cut_hyp IHlen; try omega.
apply NoDup_remove in HND.
cut_hyp IHlen; try tauto.
cut_hyp IHlen.
+ intros y HIny.
specialize (Hbound y).
rewrite in_app_iff in *.
simpl in *.
cut (y <> len).
* intros.
cut_hyp Hbound; try tauto.
omega.
* intro.
subst.
tauto.
+ specialize (IHlen x).
rewrite in_app_iff in *.
simpl.
destruct IHlen; auto.
Qed.
Fixpoint maximum (xs : list nat) : nat :=
match xs with
| nil => 0
| x :: xs' => Nat.max x (maximum xs')
end.
Lemma maximum_spec : forall xs x, In x xs -> x <= maximum xs.
Proof.
induction xs as [| x xs]; intro y; simpl; try tauto.
destruct 1.
- subst.
apply Nat.le_max_l.
- apply le_trans with (maximum xs); auto.
apply Nat.le_max_r.
Qed.
Lemma map_nth' : forall {A B} (f : A -> B) xs n da db,
n < length xs ->
nth n (map f xs) da = f (nth n xs db).
Proof.
intros until db.
revert xs.
induction n; intros xs Hlt.
- destruct xs.
+ inversion Hlt.
+ reflexivity.
- destruct xs.
+ inversion Hlt.
+ simpl.
rewrite IHn; auto.
simpl in Hlt.
omega.
Qed.
Lemma list_elem_eq {A} : forall xs ys (d : A),
length xs = length ys ->
(forall i, i < length xs -> nth i xs d = nth i ys d) ->
xs = ys.
Proof.
induction xs.
- destruct ys; simpl in *; try discriminate.
reflexivity.
- intros ys d Hlen Heq.
destruct ys as [|b ys]; simpl in Hlen; try discriminate.
specialize (IHxs ys d).
cut_hyp IHxs; auto.
replace a with b.
+ rewrite IHxs; auto.
intros i Hi.
specialize (Heq (S i)).
simpl in Heq.
apply Heq.
omega.
+ specialize (Heq 0).
simpl in Heq.
rewrite Heq; auto.
omega.
Qed.
Lemma sub_sub_add : forall x y z, z <= y -> (x - (y - z)) = x + z - y.
Proof.
intros.
omega.
Qed.