-
Notifications
You must be signed in to change notification settings - Fork 0
/
NDL.v
417 lines (372 loc) · 9.26 KB
/
NDL.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
Require Import List SetoidClass.
Require Import Omega.
Require Import Util.
Set Implicit Arguments.
Section NDL.
Variable A : Type.
Context `{DA : Decidable A}.
Record NDL :=
{
elems :> list A;
nodup : NoDup elems
}.
Hint Resolve nodup.
Definition nd_In x (xs: NDL) := In x (elems xs).
Lemma nd_In_In : forall x xs, nd_In x xs <-> In x xs.
Proof.
intros.
reflexivity.
Qed.
Definition same_set (xs ys : NDL) : Prop := forall x,
nd_In x xs <-> nd_In x ys.
Global Program Instance NDL_Setoid : Setoid NDL := {| equiv := same_set |}.
Next Obligation.
constructor; autounfold; unfold same_set.
- reflexivity.
- intros xs ys H.
intro x.
now symmetry.
- intros xs ys zs.
intros Hxy Hyz.
intro x.
now rewrite Hxy.
Qed.
Global Instance nd_in_proper :
Proper (eq ==> equiv ==> equiv) nd_In.
Proof.
simpl.
intros x x' Heqx xs xs' Heqxs'.
unfold same_set in *.
subst.
unfold nd_In in *.
rewrite Heqxs'.
reflexivity.
Qed.
Definition nd_included (xs ys : NDL) : Prop :=
forall x, nd_In x xs -> nd_In x ys.
Global Instance nd_included_proper :
Proper (equiv ==> equiv ==> equiv) nd_included.
Proof.
simpl.
intros xs xs' Heqxs ys ys' Heqys.
unfold nd_included.
intuition.
- specialize (H x).
rewrite Heqxs, Heqys in H.
auto.
- specialize (H x).
rewrite <- Heqxs, <- Heqys in H.
auto.
Qed.
Definition NDL_iff (P : A -> Prop) (xs : NDL) := forall x, nd_In x xs <-> P x.
Definition count (xs : NDL) : nat := length (elems xs).
Lemma nd_count_length : forall xs, count xs = length xs.
Proof.
reflexivity.
Qed.
Global Instance count_proper : Proper (equiv ==> eq) count.
Proof.
simpl.
intros [xs NDxs] [ys NDys].
unfold count.
unfold same_set.
unfold nd_In.
simpl.
revert ys NDys.
induction NDxs.
- intros.
destruct ys; auto.
specialize (H a).
simpl in H.
tauto.
- intros ys Hys HIn.
simpl in *.
assert (In x ys) as HInx. {
rewrite <- HIn.
auto.
}
apply in_split in HInx.
destruct HInx as (yl & yr & Heqys). subst.
simpl.
specialize (IHNDxs (yl ++ yr)).
rewrite app_length in *.
simpl.
rewrite IHNDxs; auto.
+ apply NoDup_remove in Hys.
tauto.
+ intro y.
specialize (HIn y).
split; intro HIny.
* intuition; subst; try tauto.
rewrite in_app_iff in *.
simpl in *.
intuition.
now subst.
* assert (x <> y) as HNe. {
intro.
subst.
now apply NoDup_remove in Hys.
}
rewrite in_app_iff in *.
simpl in *.
tauto.
Qed.
Program Definition nd_add (x : A) (xs : NDL) : NDL :=
if In_dec x xs
then xs
else {| elems := cons x xs |}.
Next Obligation.
now constructor.
Qed.
Lemma nodup_app :
forall (xs ys : list A),
NoDup xs -> NoDup ys -> (forall x, In x xs -> ~In x ys) ->
NoDup (xs ++ ys).
Proof.
intros xs ys Hxs Hys Hnin.
induction xs.
- now simpl.
- simpl.
constructor.
+ rewrite in_app_iff.
intros [HInx | HIny].
* now inversion Hxs.
* apply (Hnin a); simpl; auto.
+ apply IHxs.
* now inversion Hxs.
* intros x HInx.
apply Hnin.
simpl. auto.
Qed.
Hint Resolve nodup_app.
Lemma nd_add_in : forall x xs a, nd_In a (nd_add x xs) <-> x = a \/ nd_In a xs.
Proof.
intros.
unfold nd_add.
destruct In_dec.
- intuition. subst. auto.
- simpl.
reflexivity.
Qed.
Global Instance nd_add_proper : Proper (eq ==> equiv ==> equiv) nd_add.
Proof.
simpl.
intros x y Hxy xs ys Heqv.
subst.
unfold same_set in *.
intro z.
repeat rewrite nd_add_in.
rewrite Heqv.
reflexivity.
Qed.
Lemma nodup_filter : forall f (xs : list A), NoDup xs -> NoDup (filter f xs).
Proof.
intros f xs H.
induction xs; simpl; auto.
inversion H. subst. intuition.
destruct (f a); simpl; auto.
constructor; auto.
rewrite filter_In.
tauto.
Qed.
Hint Resolve nodup_filter.
Lemma filter_length : forall f (xs : list A), length (filter f xs) <= length xs.
Proof.
intros.
induction xs; simpl; auto.
destruct (f a); simpl; auto.
omega.
Qed.
Program Definition nd_filter (f : A -> bool) (xs : NDL) : NDL :=
{| elems := filter f xs |}.
Lemma nd_filter_in : forall f xs x,
nd_In x (nd_filter f xs) <-> nd_In x xs /\ f x = true.
Proof.
intros.
unfold nd_In, nd_filter.
simpl.
apply filter_In.
Qed.
Global Instance nd_filter_propre : Proper (eq ==> equiv ==> equiv) nd_filter.
Proof.
simpl.
intros f f' Heqf xs xs' Heqxs.
subst.
intro x.
repeat rewrite nd_filter_in.
rewrite Heqxs.
reflexivity.
Qed.
Program Definition nd_intersection (xs ys : NDL) : NDL :=
nd_filter (fun x => if In_dec x ys then true else false) xs.
Lemma nd_intersection_in : forall xs ys a, nd_In a (nd_intersection xs ys) <->
nd_In a xs /\ nd_In a ys.
Proof.
intros.
cbn.
rewrite filter_In.
destruct In_dec; try tauto.
intuition.
Qed.
Global Instance nd_intersection_proper :
Proper (equiv ==> equiv ==> equiv) nd_intersection.
Proof.
simpl.
intros xs xs' Heqx ys ys' Heqy.
intro x.
repeat rewrite nd_intersection_in.
rewrite Heqx, Heqy.
reflexivity.
Qed.
Lemma nd_intersection_comm :
forall xs ys, nd_intersection xs ys == nd_intersection ys xs.
Proof.
intros.
intro x.
repeat rewrite nd_intersection_in.
tauto.
Qed.
Lemma nd_intersection_included :
forall xs ys, nd_included xs ys ->
nd_intersection xs ys == xs.
Proof.
intros.
intro x.
rewrite nd_intersection_in.
specialize (H x).
intuition.
Qed.
Program Definition nd_diff (xs ys : NDL) : NDL :=
nd_filter (fun x => if In_dec x ys then false else true) xs.
Lemma nd_diff_in : forall xs ys a, nd_In a (nd_diff xs ys) <->
nd_In a xs /\ ~nd_In a ys.
Proof.
intros.
cbn.
rewrite filter_In.
destruct In_dec; try tauto.
intuition.
Qed.
Global Instance nd_diff_proper :
Proper (equiv ==> equiv ==> equiv) nd_diff.
Proof.
simpl.
intros xs xs' Heqx ys ys' Heqy.
intro x.
repeat rewrite nd_diff_in.
rewrite Heqx, Heqy.
reflexivity.
Qed.
Lemma nd_diff_count : forall xs ys,
count (nd_diff xs ys) = count xs - count (nd_intersection xs ys).
Proof.
intros.
unfold count.
destruct xs as [xs xs_prf].
cbn.
clear xs_prf.
induction xs as [| x xs]; auto.
simpl filter.
destruct In_dec; auto.
simpl length.
rewrite IHxs.
rewrite Nat.sub_succ_l; auto.
apply filter_length.
Qed.
Program Definition nd_union (xs ys : NDL) : NDL :=
{| elems :=
nd_diff xs ys ++ ys
|}.
Next Obligation.
apply nodup_app; auto.
intro x.
rewrite filter_In.
destruct In_dec; try tauto.
now destruct 1.
Qed.
Lemma nd_union_in : forall xs ys a, nd_In a (nd_union xs ys) <-> nd_In a xs \/ nd_In a ys.
Proof.
intros.
unfold nd_union.
simpl.
unfold nd_In. simpl.
rewrite in_app_iff, filter_In.
intuition.
destruct In_dec; tauto.
Qed.
Global Instance nd_union_proper :
Proper (equiv ==> equiv ==> equiv) nd_union.
Proof.
simpl.
intros xs xs' Heqx ys ys' Heqy.
intro x.
repeat rewrite nd_union_in.
rewrite Heqx, Heqy.
reflexivity.
Qed.
Lemma nd_union_count: forall xs ys,
count (nd_union xs ys) =
count xs + count ys - count (nd_intersection xs ys).
Proof.
intros.
replace (count (nd_union xs ys)) with (count (nd_diff xs ys) + count ys).
- rewrite nd_diff_count.
rewrite Nat.add_sub_swap; auto.
apply filter_length.
- unfold nd_union, count.
simpl.
rewrite app_length.
reflexivity.
Qed.
Program Definition empty : NDL := {| elems := nil |}.
Next Obligation.
constructor.
Qed.
Lemma empty_in : forall x, ~In x empty.
Proof.
intros.
now simpl.
Qed.
Lemma count_0_empty : forall xs, count xs = 0 <-> xs == empty.
Proof.
intros.
split; intro H.
- destruct xs as [xs Hnd].
unfold count in *.
destruct xs as [| x xs]; simpl in *; try discriminate.
unfold same_set, nd_In. simpl.
tauto.
- rewrite H.
reflexivity.
Qed.
Fixpoint nd_union_many (xss : list NDL) : NDL :=
match xss with
| nil => empty
| xs :: xss' => nd_union xs (nd_union_many xss')
end.
Lemma nd_union_many_in :
forall xss a, nd_In a (nd_union_many xss) <-> (exists xs, In xs xss /\ nd_In a xs).
Proof.
induction xss as [| xs xss].
- simpl.
intros.
intuition.
now destruct H.
- intro a.
simpl.
rewrite nd_union_in.
rewrite IHxss.
split.
+ intros [HIn | [xs' Hxs']].
* exists xs.
tauto.
* exists xs'.
tauto.
+ intros [xs' Hxs'].
destruct Hxs' as [[Heq | HIn] HIna].
* subst.
auto.
* eauto.
Qed.
End NDL.
Arguments empty {A}.