-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListExtras.v
334 lines (294 loc) · 6.27 KB
/
ListExtras.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
Require Export List.
Require Import CpdtTactics.
Require Import LibTactics.
(*
==================
Lemmas about find
==================
*)
Lemma find_notNone :
forall A f l,
find f l <> None <->
exists (e : A), find f l = Some e.
Proof with eauto.
induction l.
+ crush.
+ simpl. cases_if...
split... congruence.
Qed.
Lemma find_in :
forall A f l (x : A),
find f l = Some x ->
In x l.
Proof with auto.
induction l as [| h t]; crush.
cases_if; crush.
Qed.
Lemma find_true :
forall A f l (x : A),
find f l = Some x ->
f x = true.
Proof with auto.
introv Hfind. induction l.
+ inversion Hfind.
+ simpls. cases_if; crush.
Qed.
Lemma find_app :
forall A f l1 l2 (x : A),
find f l1 = Some x ->
find f (l1 ++ l2) = Some x.
Proof with auto.
introv Hfind. induction l1; simpl.
+ inversion Hfind.
+ simpls. cases_if; crush.
Qed.
Lemma find_app2 :
forall A f l1 l2 (x : A),
find f l2 = Some x ->
find f l1 = None ->
find f (l1 ++ l2) = Some x.
Proof with auto.
induction l1; simpls...
cases_if; crush.
Qed.
(*
==================
Lemmas about snoc
==================
*)
Fixpoint snoc {A:Type} (l : list A) (x : A):=
match l with
| nil => x :: nil
| head :: tail => cons head (snoc tail x)
end.
Lemma snoc_length :
forall A l (x : A),
length (snoc l x) = S (length l).
Proof.
induction l; crush.
Qed.
Lemma snoc_in :
forall A l (x : A),
List.In x (snoc l x).
Proof with auto.
induction l; crush.
Qed.
Lemma in_snoc :
forall A l (x y : A),
List.In x (snoc l y) <->
List.In x l \/ x = y.
Proof.
intros. induction l; crush.
Qed.
(*
====================
Lemmas about remove
====================
*)
Lemma remove_app :
forall A l1 l2 (x:A) eq_dec,
remove eq_dec x (l1 ++ l2) =
remove eq_dec x l1 ++ remove eq_dec x l2.
Proof with auto.
induction l1; simpls... intros.
cases_if; crush.
Qed.
Hint Rewrite remove_app : remove.
Lemma remove_idempotence :
forall A l (x:A) eq_dec,
remove eq_dec x (remove eq_dec x l) =
remove eq_dec x l.
Proof with auto.
induction l; simpls; intros...
repeat (elim eq_dec; simpl); crush.
Qed.
Hint Rewrite remove_idempotence : remove.
Lemma remove_commutative :
forall A l (x y:A) eq_dec,
remove eq_dec x (remove eq_dec y l) =
remove eq_dec y (remove eq_dec x l).
Proof with auto.
induction l; simpl; intros...
repeat (elim eq_dec; simpl); crush.
Qed.
Hint Rewrite remove_commutative : remove.
Lemma remove_in :
forall A l (x y : A) eq_dec,
In x (remove eq_dec y l) ->
In x l.
Proof with eauto.
induction l; simpl; intros...
cases_if; crush...
Qed.
Lemma remove_not_in :
forall A l (x y : A) eq_dec,
~ In x l ->
~ In x (remove eq_dec y l).
Proof with eauto.
induction l; simpl; intros...
cases_if; crush...
Qed.
Hint Constructors NoDup.
Lemma remove_NoDup :
forall A l (x : A) eq_dec,
NoDup l ->
NoDup (remove eq_dec x l).
Proof with eauto using remove_not_in.
induction l; simpl; introv Hdup...
inverts Hdup.
cases_if...
Qed.
Lemma remove_in_eq :
forall A l (x : A) eq_dec,
~In x (remove eq_dec x l).
Proof with eauto.
introv.
induction l...
simpl. cases_if; crush.
Qed.
Lemma remove_in_neq :
forall A l (x y : A) eq_dec,
In x l ->
x <> y ->
In x (remove eq_dec y l).
Proof with eauto.
introv HIn Hneq.
induction l...
simpl. cases_if; crush.
Qed.
(*
==================
Lemmas about app
==================
*)
Lemma Forall_app :
forall A P l1 l2,
@Forall A P l1 ->
Forall P l2 ->
Forall P (l1 ++ l2).
Proof with auto.
induction l1...
introv Hforall.
inversion Hforall.
constructor...
Qed.
Lemma app_eq_nil :
forall A l1 l2,
l1 ++ l2 = nil <-> l1 = nil /\ l2 = @nil A.
Proof with auto using app_eq_nil.
introv. split... crush.
Qed.
Corollary app_eq_nil_trivial :
forall A l1 l2,
l1 = @nil A ->
l2 = @nil A ->
l1 ++ l2 = @nil A.
Proof with auto.
introv Hnil1 Hnil2. apply app_eq_nil...
Qed.
Hint Immediate app_eq_nil_trivial.
Lemma not_in_app :
forall A l1 l2 (x : A),
~ In x (l1 ++ l2) <->
(~ In x l1) /\ (~ In x l2).
Proof with eauto using in_or_app, in_cons, in_eq.
intros A l1 l2 x.
split.
+ introv Hin.
induction l1...
simpl in Hin.
simpl.
split.
- intros contra.
apply Hin. inversion contra...
- intros contra...
+ introv HIn.
inverts HIn as HIn1 HIn2.
induction l1; simpl...
intros contra.
inversion contra.
- apply HIn1. subst...
- apply IHl1...
Qed.
Hint Constructors NoDup.
Lemma NoDup_app :
forall A (l1 l2 : list A),
NoDup (l1 ++ l2) ->
NoDup l1 /\ NoDup l2.
Proof with eauto.
introv Hdup.
induction l1.
+ inversion Hdup...
+ inversion Hdup as [|? ? Hin Hdup'].
apply IHl1 in Hdup' as (Hdup1 & Hdup2).
split...
econstructor...
apply not_in_app in Hin as []...
Qed.
(*
===================
Lemmas about filter
===================
*)
Lemma filter_in :
forall A (x : A) p l,
(forall x y, p x y = true <-> x = y) ->
(In x l <-> In x (filter (p x) l)).
Proof with eauto.
introv Hp.
split.
+ introv HIn. induction l;
simpl; try(cases_if); crush.
assert (Heq: x = x)...
apply Hp in Heq. congruence.
+ introv HIn. induction l;
simpls; try(cases_if); crush.
Qed.
Lemma filter_not_in :
forall A (x : A) p l,
(forall x y, p x y = true <-> x = y) ->
(~In x l <-> (filter (p x) l) = nil).
Proof with eauto.
introv Hp.
split.
+ introv HnIn.
induction l...
simpl. cases_if; crush.
apply Hp in H. false.
+ introv Hneq.
induction l...
simpls. cases_if; try(congruence).
intros contra. inverts contra.
- assert(p x x = true) by (apply Hp; eauto).
congruence.
- apply IHl...
Qed.
Lemma filter_length :
forall A (l : list A) p,
length (filter p l) <= length l.
Proof with eauto.
introv.
induction l...
simpl. cases_if; crush.
Qed.
Lemma filter_app :
forall A (l1 l2 : list A) p,
filter p (l1 ++ l2) = (filter p l1) ++ (filter p l2).
Proof with eauto.
introv.
induction l1...
simpl. cases_if; crush.
Qed.
(*
================
Lemmas about in
================
*)
Lemma in_comm :
forall A l1 l2 (x : A),
In x (l1 ++ l2) ->
In x (l2 ++ l1).
Proof with eauto using in_or_app.
introv HIn.
apply in_app_or in HIn as []...
Qed.