-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListUtil.v
More file actions
3262 lines (2654 loc) · 77.5 KB
/
ListUtil.v
File metadata and controls
3262 lines (2654 loc) · 77.5 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Time-stamp: <Wed 11/22/23 12:16 Dan Dougherty ListUtil.v>
Some material is from Smolka and Brown library.
*)
From Coq Require Import List Lia
FunctionalExtensionality
PropExtensionality.
Export ListNotations.
From RC Require Import
Utilities
Decidability
CpdtTactics .
Global Set Implicit Arguments.
Global Unset Strict Implicit.
Open Scope list_scope.
(** ** Bounded sigma *)
(** Get an element of ls satisying g OR
a proof that everything in ls satisfies ~g
*)
Definition list_sigma X ls
(g : X -> Prop)
(g_dec : forall x, Decision (g x)) :
{x | In x ls /\ g x} + {forall x, In x ls -> ~ g x}.
Proof.
induction ls as [ | x ls' IH]; simpl.
- tauto.
- destruct IH as [[y [D E]]|D].
+ eauto.
+ destruct (g_dec x) as [E|E].
* eauto.
* right. intros y [[]|F]; auto.
Defined.
Arguments list_sigma {X} ls g {g_dec}.
(** Perhaps more reliable for computation than the decidable predicate [g] version *)
Definition list_sigma_bool X ls
(b : X -> bool) :
{x | In x ls /\ b x = true} + {forall x, In x ls -> b x = false}.
Proof.
induction ls as [ | x ls' IH]; simpl.
- tauto.
- destruct IH as [[y [D E]]|D].
+ eauto.
+ destruct (b x) eqn:E.
* eauto.
* right. intros y [[]|F]; auto.
Defined.
Arguments list_sigma_bool {X} ls b .
(** Get a pair (x,y) with [g x = Some y] OR
a proof that g x = None for all x in ls
*)
Definition list_opt_sigma
X Y (ls: list X)
(g : X -> option Y) :
{ p | In (fst p) ls /\
g (fst p) = Some (snd p)} +
{forall x, In x ls -> g x = None}.
Proof.
induction ls as [ | x ls' IH]; simpl.
- tauto.
- destruct IH as [[y [D E]]|D].
+ eauto.
+ destruct (g x) eqn:gx.
* left.
exists (x,y).
simpl. tauto.
* right. intros y [[]|F]; auto.
Defined.
Arguments list_opt_sigma {X} {Y} ls g.
(* ========================== *)
(** ** Decidability for Lists *)
(** These two use [list_sigma] just above *)
(** *** Universal quantification *)
#[export]
Instance list_forall_dec X A (p : X -> Prop) (p_dec : forall x, Decision (p x)) :
Decision (forall x, In x A -> p x).
Proof.
destruct (list_sigma A (fun x => ~ p x)) as [[x [D E]]|D].
- right. auto.
- left. intros x E. apply dec_DN; auto.
Defined.
(** *** Existential quantification *)
#[export]
Instance list_exists_dec X A (p : X -> Prop) (p_dec : forall x, Decision (p x)) :
Decision (exists x, In x A /\ p x).
Proof.
destruct (list_sigma A p) as [[x [D E]]|D].
- left. eauto.
- right. intros [x [E F]]. exact (D x E F).
Defined.
#[export] Hint Unfold list_forall_dec list_exists_dec : core.
(* -------------------------------------------------- *)
Definition equi X (A B : list X) : Prop :=
incl A B /\ incl B A.
#[export] Hint Unfold equi : core.
Notation "| A |" := (List.length A) (at level 65).
Notation "x 'el' A" := (In x A) (at level 70, only parsing) .
Notation "A <<= B" := (incl A B) (at level 70).
Notation "A === B" := (equi A B) (at level 70).
Notation "A =/= B" := ((equi A B) -> False) (at level 70).
(* The following comments are for coqdoc *)
(** printing el #∊# *)
(** printing <<= #⊆# *)
(** printing === #≡# *)
Lemma incl_tl' {X:Type} (a: X) ls :
ls <<= a :: ls.
Proof. now apply incl_tl.
Qed.
(* A useful lemma *)
Lemma list_cycle (X : Type) (A : list X) x :
x::A <> A.
Proof.
intros B.
assert (C: |x::A| <> |A|) by (simpl; lia).
apply C. now rewrite B.
Qed.
Lemma list_exists_DM X A (p : X -> Prop) :
(forall x, Decision (p x)) ->
~ (forall x, x el A -> ~ p x) -> exists x, x el A /\ p x.
Proof.
intros D E.
destruct (list_sigma A p) as [F|F].
+ destruct F as [x F]. eauto.
+ contradiction (E F).
Qed.
(** @@ This must have an easier proof *)
Lemma list_exists X A (p : X -> Prop) :
(forall x, Decision (p x)) ->
~ (forall x, x el A -> p x) -> exists x, x el A /\ ~ p x.
Proof.
intros HD H.
set (np := (fun (x : X) => ~ (p x))).
assert (npdec: (∀ x , Decision (np x))).
{ intros x.
unfold Decision.
destruct (decide (p x)) eqn:e.
- right. auto.
- left. auto. }
assert (a:= @list_exists_DM X A np).
apply a.
apply npdec.
enough ( ¬ (∀ x : X, In x A → p x)).
- intros HF.
firstorder.
- apply H.
Qed.
Lemma list_cc X (p : X -> Prop) A :
(forall x, Decision (p x)) ->
(exists x, x el A /\ p x) -> {x | x el A /\ p x}.
Proof.
intros D E.
destruct (list_sigma A p) as [[x [F G]]|F].
- eauto.
- exfalso. destruct E as [x [G H]]. apply (F x); auto.
Defined.
(** Membership
We use the following facts from the standard library List.
- [in_eq : x el x::A]
- [in_nil : ~ x el nil]
- [in_cons : x el A -> x el y::A]
- [in_or_app : x el A \/ x el B -> x el A++B]
- [in_app_iff : x el A++B <-> x el A \/ x el B]
- [in_map_iff : y el map f A <-> exists x, f x = y /\ x el A]
*)
#[export] Hint Resolve in_eq in_nil in_cons in_or_app : core.
Lemma in_sing X (x y : X) :
x el [y] -> x = y.
Proof. simpl. intros [[]|[]]. reflexivity. Qed.
Lemma in_cons_neq X (x y : X) A :
x el y::A -> x <> y -> x el A.
Proof. simpl. intros [[]|D] E; congruence. Qed.
Definition disjoint (X : Type) (A B : list X) :=
~ exists x, x el A /\ x el B.
Lemma disjoint_forall X (A B : list X) :
disjoint A B <-> forall x, x el A -> ~ x el B.
Proof.
split.
- intros D x E F. apply D. exists x. auto.
- intros D [x [E F]]. exact (D x E F).
Qed.
Lemma disjoint_cons X (x : X) A B :
disjoint (x::A) B <-> ~ x el B /\ disjoint A B.
Proof.
split.
- intros D. split.
+ intros E. apply D. eauto.
+ intros [y [E F]]. apply D. eauto.
- intros [D E] [y [[F|F] G]].
+ congruence.
+ apply E. eauto.
Qed.
(** StdLib [in_map_iff] is awkward as an iff *)
Lemma in_map_only_if:
∀ (A B : Type) (f : A → B) (l : list A) (y : B),
In y (map f l) -> (∃ x : A, f x = y ∧ In x l).
Proof.
apply in_map_iff.
Qed.
(** my map_opt *)
Fixpoint map_opt {A B} (f: A -> option B) (l: list A): list B :=
match l with
| [] => []
| x :: xs =>
match f x with
None => map_opt f xs
| Some y => y :: map_opt f xs
end end.
(** A commutative diagram lemma involving mixed
[amp] and [map_opt]
draw the picture :-)
*)
Lemma map_map_opt_commute
{X Y X' Y' : Type}
(f: X -> Y)
(f': X' -> Y')
(g: X -> option X')
(g': Y -> option Y')
:
(forall (x :X) (x': X') ,
(g x = Some x')
->
(g' (f x) = Some (f' x') ))
->
(forall (x :X),
(g x = None)
->
(g' (f x) = None)
)
->
(forall x,
(compose (map_opt g') (map f)) x =
(compose (map f')(map_opt g)) x).
Proof.
intros H1 H2 xs.
unfold compose.
induction xs as [| x rest IH].
- simpl; auto.
- simpl.
destruct (g' (f x)) eqn:eq1.
+ destruct (g x) eqn:eq2.
-- specialize (H1 x x0 eq2).
rewrite H1 in eq1.
inv eq1.
simpl. rewrite IH. easy.
-- specialize (H2 x eq2).
rewrite H2 in eq1; inv eq1.
+ destruct (g x) eqn:eq2.
-- specialize (H1 x x0 eq2).
rewrite H1 in eq1.
inv eq1.
-- apply IH.
Qed.
Lemma in_map_opt_then:
∀ (A B : Type)
`{EqDecision B}
(f : A -> option B) (l : list A) ,
forall (y : B),
In y (map_opt f l) ->
exists x : A, In x l /\ f x = Some y.
Proof.
intros A B Q f l .
induction l; simpl; auto.
- firstorder.
- intros b H.
(* specialize (IHl b). *)
destruct (decide ((f a) = Some b)) eqn:e.
+ exists a. firstorder.
+ destruct (f a).
* specialize (IHl b).
assert (h: In b (map_opt f l)).
{ assert (hb : b <> b0).
{ congruence . }
assert (hneq := in_cons_neq H hb); easy. }
assert (h1:= IHl h).
destruct h1 as [x [P1 P2]].
exists x. tauto.
* firstorder.
Qed.
(** Inclusion
We use the following facts from the standard library List.
- [A <<= B = forall y, x el A -> x el B]
- [incl_refl : A <<= A]
- [incl_tl : A <<= B -> A <<= x::B]
- [incl_cons : x el B -> A <<= B -> x::A <<= B]
- [incl_appl : A <<= B -> A <<= B++C]
- [incl_appr : A <<= C -> A <<= B++C]
- [incl_app : A <<= C -> B <<= C -> A++B <<= C]
*)
#[export] Hint Resolve incl_refl incl_tl incl_cons incl_appl incl_appr incl_app : core.
Lemma incl_nil X (A : list X) :
nil <<= A.
Proof. intros x []. Qed.
#[export] Hint Resolve incl_nil : core.
Lemma incl_map X Y A B (f : X -> Y) :
A <<= B -> map f A <<= map f B.
Proof.
intros D y E. apply in_map_iff in E as [x [E E']].
subst y. apply in_map_iff. eauto.
Qed.
Section Inclusion.
Variable X : Type.
Implicit Types A B : list X.
Lemma incl_nil_eq A :
A <<= nil -> A=nil.
Proof using Type.
intros D. destruct A as [ |x A].
- reflexivity.
- exfalso. apply (D x). auto.
Qed.
Lemma incl_shift x A B :
A <<= B -> x::A <<= x::B.
Proof using Type. intros D y E. destruct E; subst; auto. Qed.
Lemma incl_lcons x A B :
x::A <<= B <-> x el B /\ A <<= B.
Proof using Type.
split.
- intros D. split; hnf; auto.
- intros [D E] z [F|F]; subst; auto.
Qed.
Lemma incl_rcons x A B :
A <<= x::B -> ~ x el A -> A <<= B.
Proof using Type. intros C D y E. destruct (C y E) as [F|F].
congruence. apply F.
Qed.
Lemma incl_lrcons x A B :
x::A <<= x::B -> ~ x el A -> A <<= B.
Proof using Type.
intros C D y E.
assert (F: y el x::B) by auto.
destruct F as [F|F]; congruence.
Qed.
End Inclusion.
#[export] Hint Resolve incl_shift : core.
Lemma incl_not {X:Type} (s1 s2: list X) (x: X) :
s1 <<= s2 ->
~ In x s2 ->
~ In x s1.
Proof.
unfold incl; intros; firstorder.
Qed.
Definition inclp (X : Type) (A : list X) (p : X -> Prop) : Prop :=
forall x, x el A -> p x.
(** Setoid rewriting with list inclusion and list equivalence *)
#[export] Instance in_equi_proper X :
Proper (eq ==> @equi X ==> iff) (@In X).
Proof. hnf. intros x y []. hnf. firstorder. Defined.
#[export] Instance incl_equi_proper X :
Proper (@equi X ==> @equi X ==> iff) (@incl X).
Proof. hnf. intros x y D. hnf. firstorder. Defined.
#[export] Instance incl_preorder X : PreOrder (@incl X).
Proof. constructor; hnf; unfold incl; auto. Defined.
#[export] Instance equi_Equivalence X : Equivalence (@equi X).
Proof. constructor; hnf; firstorder. Defined.
#[export] Instance cons_equi_proper X :
Proper (eq ==> @equi X ==> @equi X) (@cons X).
Proof. hnf. intros x y []. hnf. firstorder. Defined.
#[export] Instance app_equi_proper X :
Proper (@equi X ==> @equi X ==> @equi X) (@app X).
Proof.
hnf. intros A B D. hnf. intros A' B' E.
destruct D, E; auto.
Defined.
(** Equivalence *)
Section Equi.
Variable X : Type.
Implicit Types A B : list X.
Lemma equi_push x A :
x el A -> A === x::A.
Proof using Type. auto. Qed.
Lemma equi_dup x A :
x::A === x::x::A.
Proof using Type. auto. Qed.
Lemma equi_swap x y A:
x::y::A === y::x::A.
Proof using Type. split; intros z; simpl; tauto. Qed.
Lemma equi_shift x A B :
x::A++B === A++x::B.
Proof using Type.
split; intros y.
- intros [D|D].
+ subst; auto.
+ apply in_app_iff in D as [D|D]; auto.
- intros D. apply in_app_iff in D as [D|D].
+ auto.
+ destruct D; subst; auto.
Qed.
Lemma equi_rotate x A :
x::A === A++[x].
Proof using Type.
split; intros y; simpl.
- intros [D|D]; subst; auto.
- intros D. apply in_app_iff in D as [D|D].
+ auto.
+ apply in_sing in D. auto.
Qed.
End Equi.
(** * Filterp *)
Definition filterp (X : Type) (p : X -> Prop)
(p_dec : forall x, Decision (p x)) : list X -> list X :=
fix f A := match A with
| nil => nil
| x::A' => if decide (p x) then x :: f A' else f A'
end.
Arguments filterp [X] p {p_dec} A.
(* # *)
Section FilterpLemmas.
Variable X : Type.
Variable p : X -> Prop.
Context {p_dec : forall x, Decision (p x)}.
Lemma in_filterp_iff x A :
x el filterp p A <-> x el A /\ p x.
Proof using Type.
induction A as [ |y A]; simpl.
- tauto.
- destruct (decide (p y)) as [B|B]; simpl;
rewrite IHA; firstorder; subst; tauto.
Qed.
Lemma filterp_incl A :
filterp p A <<= A.
Proof using Type.
intros x D. apply in_filterp_iff in D. apply D.
Qed.
Lemma filterp_mono A B :
A <<= B -> filterp p A <<= filterp p B.
Proof using Type.
intros D x E. apply in_filterp_iff in E as [E E'].
apply in_filterp_iff. auto.
Qed.
Lemma filterp_fst x A :
p x -> filterp p (x::A) = x::filterp p A.
Proof using Type.
simpl. destruct (decide (p x)); tauto.
Qed.
Lemma filterp_app A B :
filterp p (A ++ B) = filterp p A ++ filterp p B.
Proof using Type.
induction A as [ |y A]; simpl.
- reflexivity.
- rewrite IHA. destruct (decide (p y)); reflexivity.
Qed.
Lemma filterp_fst' x A :
~ p x -> filterp p (x::A) = filterp p A.
Proof using Type.
simpl. destruct (decide (p x)); tauto.
Qed.
End FilterpLemmas.
Section FilterpLemmas_pq.
Variable X : Type.
Variable p q : X -> Prop.
Context {p_dec : forall x, Decision (p x)}.
Context {q_dec : forall x, Decision (q x)}.
Lemma filterp_pq_mono A :
(forall x, x el A -> p x -> q x) -> filterp p A <<= filterp q A.
Proof using Type.
intros D x E. apply in_filterp_iff in E as [E E'].
apply in_filterp_iff. auto.
Qed.
Lemma filterp_pq_eq A :
(forall x, x el A -> (p x <-> q x)) -> filterp p A = filterp q A.
Proof using Type.
intros C; induction A as [ |x A]; simpl.
- reflexivity.
- destruct (decide (p x)) as [D|D]; destruct (decide (q x)) as [E|E].
+ f_equal. auto.
+ exfalso. apply E, (C x); auto.
+ exfalso. apply D, (C x); auto.
+ auto.
Qed.
Lemma filterp_and A :
filterp p (filterp q A) = filterp (fun x => p x /\ q x) A.
Proof using Type.
set (r x := p x /\ q x).
induction A as [ |x A]; simpl. reflexivity.
destruct (decide (q x)) as [E|E]; simpl; rewrite IHA.
- destruct (decide (p x)); destruct (decide (r x)); unfold r in *|-; auto; tauto.
- destruct (decide (r x)); unfold r in *|-; auto; tauto.
Qed.
End FilterpLemmas_pq.
Section FilterpComm.
Variable X : Type.
Variable p q : X -> Prop.
Context {p_dec : forall x, Decision (p x)}.
Context {q_dec : forall x, Decision (q x)}.
Lemma filterp_comm A :
filterp p (filterp q A) = filterp q (filterp p A).
Proof using Type.
do 2 rewrite filterp_and. apply filterp_pq_eq. tauto.
Qed.
End FilterpComm.
(** * Element removal *)
Section Removal.
Variable X : Type.
Context {eq_X_dec : EqDecision X}.
Definition rem (A : list X) (x : X) : list X :=
filterp (fun z => ~ (z = x)) A.
Lemma in_rem_iff x A y :
x el rem A y <-> x el A /\ x <> y.
Proof using Type.
apply in_filterp_iff.
Qed.
Lemma rem_not_in x y A :
x = y \/ ~ x el A -> ~ x el rem A y.
Proof using Type.
intros D E. apply in_rem_iff in E. tauto.
Qed.
Lemma rem_incl A x :
rem A x <<= A.
Proof using Type.
apply filterp_incl.
Qed.
Lemma rem_mono A B x :
A <<= B -> rem A x <<= rem B x.
Proof using Type.
apply filterp_mono.
Qed.
Lemma rem_cons A B x :
A <<= B -> rem (x::A) x <<= B.
Proof using Type.
intros E y F. apply E. apply in_rem_iff in F.
destruct F as [[ | ]]; congruence.
Qed.
Lemma rem_cons' A B x y :
x el B -> rem A y <<= B -> rem (x::A) y <<= B.
Proof using Type.
intros E F u G.
apply in_rem_iff in G as [[[]|G] H]. exact E.
apply F. apply in_rem_iff. auto.
Qed.
(* for some reason adding this into the core Hint datbase causes at leat one eauto to stacj overflow *)
Lemma rem_in x y A :
x el rem A y -> x el A.
Proof using Type.
apply rem_incl.
Qed.
Lemma rem_neq x y A :
x <> y -> x el A -> x el rem A y.
Proof using Type.
intros E F. apply in_rem_iff. auto.
Qed.
Lemma rem_app x A B :
x el A -> B <<= A ++ rem B x.
Proof using Type.
intros E y F. destruct (decide (x=y)) as [[]| ];
auto using rem_neq.
Qed.
Lemma rem_app' x A B C :
rem A x <<= C -> rem B x <<= C -> rem (A ++ B) x <<= C.
Proof using Type.
unfold rem; rewrite filterp_app; auto.
Qed.
Lemma rem_equi x A :
x::A === x::rem A x.
Proof using Type.
split; intros y;
intros [[]|E]; destruct (decide (x=y)) as [[]|D];
eauto using rem_in, rem_neq.
Qed.
Lemma rem_comm A x y :
rem (rem A x) y = rem (rem A y) x.
Proof using Type.
apply filterp_comm.
Qed.
Lemma rem_fst x A :
rem (x::A) x = rem A x.
Proof using Type.
unfold rem. rewrite filterp_fst'; auto.
Qed.
Lemma rem_fst' x y A :
x <> y -> rem (x::A) y = x::rem A y.
Proof using Type.
intros E. unfold rem. rewrite filterp_fst; auto.
Qed.
End Removal.
#[export] Hint Resolve
rem_not_in
rem_incl
rem_mono
rem_cons
rem_cons'
rem_app
rem_app'
rem_neq
(* rem_in *)
: core.
(** * Duplicate-free lists *)
(** @@ NOTE *)
(* Cf stdlib [nodup]
Inductive dupfree (X : Type) : list X -> Prop :=
| dupfreeN : dupfree nil
| dupfreeC x A : ~ x el A -> dupfree A -> dupfree (x::A).
Cf stdlib [NoDup]
Inductive NoDup (A : Type) : list A → Prop :=
NoDup_nil : NoDup []
| NoDup_cons : ∀ (x : A) (l : list A), ¬ In x l → NoDup l → NoDup (x :: l).
*)
Section Dupfree.
Variable X : Type.
Implicit Types A B : list X.
Lemma NoDup_inv x A :
NoDup (x::A) <-> ~ x el A /\ NoDup A.
Proof using Type.
split; intros D.
- inversion D; auto.
- apply NoDup_cons; tauto.
Qed.
Lemma NoDup_app A B :
disjoint A B -> NoDup A -> NoDup B -> NoDup (A++B).
Proof using Type.
intros D E F. induction E as [ |x A E' E]; simpl.
- exact F.
- apply disjoint_cons in D as [D D'].
constructor; [ |exact (IHE D')].
intros G. apply in_app_iff in G; tauto.
Qed.
Lemma NoDup_map Y A (f : X -> Y) :
(forall x y, x el A -> y el A -> f x = f y -> x=y) ->
NoDup A -> NoDup (map f A).
Proof using Type.
intros D E. induction E as [ |x A E' E]; simpl.
- constructor.
-
constructor;
[ |now auto].
intros F. apply in_map_iff in F as [y [F F']].
rewrite (D y x) in F'; auto.
Qed.
Lemma NoDup_filterp p (p_dec : forall x, Decision (p x)) A :
NoDup A -> NoDup (filterp p A).
Proof using Type.
intros D. induction D as [ |x A C D]; simpl.
- left.
- destruct (decide (p x)) as [E|E]; [ |exact IHD].
right; [ |exact IHD].
intros F. apply C. apply filterp_incl in F. exact F.
Qed.
Lemma NoDup_dec A :
EqDecision X -> Decision (NoDup A).
Proof using Type.
intros D. induction A as [ |x A].
- left. left.
- destruct (decide (x el A)) as [E|E].
+ right. intros F. inversion F; tauto.
+ destruct (IHA) as [F|F].
-- left. now apply NoDup_cons.
-- right. intros HF.
now apply NoDup_cons_iff in HF.
Qed.
End Dupfree.
Section Undup.
Variable X : Type.
Context {eq_X_dec : EqDecision X}.
Implicit Types A B : list X.
Fixpoint undup (A : list X) : list X :=
match A with
| nil => nil
| x::A' => if decide (x el A') then undup A' else x :: undup A'
end.
Lemma undup_fp_equi A :
undup A === A.
Proof using Type.
induction A as [ |x A]; simpl.
- reflexivity.
- destruct (decide (x el A)) as [E|E]; rewrite IHA; auto.
Qed.
Lemma NoDup_undup A :
NoDup (undup A).
Proof using Type.
induction A as [ |x A]; simpl.
- left.
- destruct (decide (x el A)) as [E|E]; auto.
right; auto. now rewrite undup_fp_equi.
Qed.
Lemma undup_incl A B :
A <<= B <-> undup A <<= undup B.
Proof using Type.
now do 2 rewrite undup_fp_equi.
Qed.
Lemma undup_equi A B :
A === B <-> undup A === undup B.
Proof using Type.
now do 2 rewrite undup_fp_equi.
Qed.
Lemma undup_eq A :
NoDup A -> undup A = A.
Proof using Type.
intros E. induction E as [ |x A E F]; simpl.
- reflexivity.
- rewrite IHF. destruct (decide (x el A)) as [G|G]; tauto.
Qed.
Lemma undup_idempotent A :
undup (undup A) = undup A.
Proof using Type. apply undup_eq, NoDup_undup. Qed.
End Undup.
Section DupfreeLength.
Variable X : Type.
Implicit Types A B : list X.
Lemma NoDup_reorder A x :
NoDup A -> x el A ->
exists A', A === x::A' /\ |A'| < |A| /\ NoDup (x::A').
Proof using Type.
intros E. revert x. induction E as [ |y A H]; intros x F.
- contradiction F.
- destruct F as [F|F].
+ subst y. exists A. auto using NoDup.
+ specialize (IHE x F). destruct IHE as [A' [G [K1 K2]]].
exists (y::A'). split; [ |split].
* rewrite G. apply equi_swap.
* simpl. lia.
* { apply NoDup_inv in K2 as [K2 K3]. right.
- intros [M|M]; subst; auto.
- right; [ |exact K3].
intros M; apply H. apply G. auto. }
Qed.
Lemma NoDup_le A B :
NoDup A -> NoDup B -> A <<= B -> |A| <= |B|.
Proof using Type.
intros E; revert B.
induction A as [ |x A]; simpl; intros B F G.
- lia.
- apply incl_lcons in G as [G H].
destruct (NoDup_reorder F G) as [B' [K [L M]]].
apply NoDup_inv in E as [E1 E2].
apply NoDup_inv in M as [M1 M2].
cut (A <<= B').
{ intros N. specialize (IHA E2 B' M2 N). lia. }
apply incl_rcons with (x:=x); [ |exact E1].
rewrite H. apply K.
Qed.
Lemma NoDup_eq A B :
NoDup A -> NoDup B -> A === B -> |A|=|B|.
Proof using Type.
intros D E [F G].
apply (NoDup_le D E) in F.
apply (NoDup_le E D) in G.
lia.
Qed.
Lemma NoDup_lt A B x :
NoDup A -> NoDup B -> A <<= B ->
x el B -> ~ x el A -> |A| < |B|.
Proof using Type.
intros E F G H K.
destruct (NoDup_reorder F H) as [B' [L [M N]]].
rewrite (NoDup_eq F N L).
cut (|A|<=|B'|). { simpl; lia. }
apply NoDup_le.
- exact E.
- now inversion N.
- apply incl_rcons with (x:=x).
+ rewrite G. apply L.
+ exact K.
Qed.
(* Stack overflow here *)
Lemma NoDup_ex A B :
EqDecision X ->
NoDup A ->
NoDup B ->
|A| < |B| ->
(exists x, In x B /\ ~ (In x A)).
Proof using Type.
intros D E F H.
destruct (list_sigma B (fun x => ~ x el A)) as [[x K]|K].
- exists x; exact K.
- exfalso.
assert (L : B <<= A).
{
intros x L.
apply dec_DN; auto.
(* Print Hint. *)
(* unfold Decision. *)
now apply list_in_dec. }
apply NoDup_le in L; auto; lia.
Qed.
Lemma NoDup_equi A B :
EqDecision X -> NoDup A -> NoDup B -> A <<= B -> |A|=|B| -> A === B.
Proof using Type.
intros C D E F G. split. exact F.
destruct (list_sigma B (fun x => ~ x el A)) as [[x [H K]]|H].
- exfalso. assert (L:=NoDup_lt D E F H K). lia.
- intros x L. apply dec_DN; auto. now apply list_in_dec.
Qed.
End DupfreeLength.
(** * Cardinality *)
Section Cardinality.
Variable X : Type.
Context {eq_X_dec : EqDecision X}.
Implicit Types A B : list X.
Definition card (A : list X) : nat := |undup A|.
Lemma card_le A B :
A <<= B -> card A <= card B.