-
Notifications
You must be signed in to change notification settings - Fork 112
/
finset.v
2654 lines (2082 loc) · 97.5 KB
/
finset.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
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
(* (c) Copyright 2006-2016 Microsoft Corporation and Inria. *)
(* Distributed under the terms of CeCILL-B. *)
From HB Require Import structures.
From mathcomp Require Import ssreflect ssrbool ssrfun eqtype ssrnat div seq.
From mathcomp Require Import choice fintype finfun bigop.
(******************************************************************************)
(* This file defines a type for sets over a finite Type, similar to the type *)
(* of functions over a finite Type defined in finfun.v (indeed, based in it): *)
(* {set T} where T must have a finType structure. *)
(* We equip {set T} itself with a finType structure, hence Leibnitz and *)
(* extensional equalities coincide on {set T}, and we can form {set {set T}}. *)
(* If A, B : {set T} and P : {set {set T}}, we define: *)
(* x \in A == x belongs to A (i.e., {set T} implements predType, *)
(* by coercion to pred_sort) *)
(* mem A == the predicate corresponding to A *)
(* finset p == the set corresponding to a predicate p *)
(* [set x | P] == the set containing the x such that P is true (x may *)
(* appear in P) *)
(* [set x | P & Q] := [set x | P && Q] *)
(* [set x in A] == the set containing the x in a collective predicate A *)
(* [set x in A | P] == the set containing the x in A such that P is true *)
(* [set x in A | P & Q] := [set x in A | P && Q] *)
(* All these have typed variants [set x : T | P], [set x : T in A], etc. *)
(* set0 == the empty set *)
(* [set: T] or setT == the full set (the A containing all x : T) *)
(* [set x] == the singleton {x} *)
(* [set~ x] == the complement of the singleton {x} *)
(* [set:: s] == the set spanned by the sequence s *)
(* [set a1; a2;...; an] := a1 |: [set a2] :|: ... :|: [set an] *)
(* A :|: B == the union of A and B *)
(* x |: A == A with the element x added (:= [set x] :|: A) *)
(* A :&: B == the intersection of A and B *)
(* ~: A == the complement of A *)
(* A :\: B == the difference A minus B *)
(* A :\ x == A with the element x removed (:= A :\: [set x]) *)
(* \bigcup_<range> A == the union of all A, for i in <range> (i is bound in *)
(* A, see bigop.v) *)
(* \bigcap_<range> A == the intersection of all A, for i in <range> *)
(* cover P == the union of the set of sets P *)
(* trivIset P <=> the elements of P are pairwise disjoint *)
(* partition P A <=> P is a partition of A *)
(* pblock P x == a block of P containing x, or else set0 *)
(* equivalence_partition R D == the partition induced on D by the relation R *)
(* (provided R is an equivalence relation in D) *)
(* preim_partition f D == the partition induced on D by the equivalence *)
(* [rel x y | f x == f y] *)
(* is_transversal X P D <=> X is a transversal of the partition P of D *)
(* transversal P D == a transversal of P, provided P is a partition of D *)
(* transversal_repr x0 X B == a representative of B \in P selected by the *)
(* transversal X of P, or else x0 *)
(* powerset A == the set of all subset of the set A *)
(* P ::&: A == those sets in P that are subsets of the set A *)
(* setX A1 A2 == cartesian product of A1 and A2 *)
(* := [set u | u.1 \in A1 & u.2 \in A2] *)
(* setXn I f A == indexed cartesian product of *)
(* A : forall i : I, {set f i} *)
(* f @^-1: A == the preimage of the collective predicate A under f *)
(* f @: A == the image set of the collective predicate A by f *)
(* f @2:(A, B) == the image set of A x B by the binary function f *)
(* [set E | x in A] == the set of all the values of the expression E, for x *)
(* drawn from the collective predicate A *)
(* [set E | x in A & P] == the set of values of E for x drawn from A, such *)
(* that P is true *)
(* [set E | x in A, y in B] == the set of values of E for x drawn from A and *)
(* and y drawn from B; B may depend on x *)
(* [set E | x in A, y in B & P] == the set of values of E for x drawn from A *)
(* y drawn from B, such that P is true *)
(* [set E | x : T] == the set of all values of E, with x in type T *)
(* [set E | x : T & P] == the set of values of E for x : T s.t. P is true *)
(* [set E | x : T, y : U in B], [set E | x : T, y : U in B & P], *)
(* [set E | x : T in A, y : U], [set E | x : T in A, y : U & P], *)
(* [set E | x : T, y : U], [set E | x : T, y : U & P] *)
(* == type-ranging versions of the binary comprehensions *)
(* [set E | x : T in A], [set E | x in A, y], [set E | x, y & P], etc. *)
(* == typed and untyped variants of the comprehensions above*)
(* The types may be required as type inference processes *)
(* E before considering A or B. Note that type casts in *)
(* the binary comprehension must either be both present *)
(* or absent and that there are no untyped variants for *)
(* single-type comprehension as Coq parsing confuses *)
(* [x | P] and [E | x]. *)
(* minset p A == A is a minimal set satisfying p *)
(* maxset p A == A is a maximal set satisfying p *)
(* Provided a monotonous function F : {set T} -> {set T}, we get fixpoints *)
(* fixset F := iter #|T| F set0 *)
(* == the least fixpoint of F *)
(* == the minimal set such that F X == X *)
(* fix_order F x == the minimum number of iterations so that *)
(* x is in iter (fix_order F x) F set0 *)
(* funsetC F := fun X => ~: F (~: X) *)
(* cofixset F == the greatest fixpoint of F *)
(* == the maximal set such that F X == X *)
(* := ~: fixset (funsetC F) *)
(* We also provide notations A :=: B, A :<>: B, A :==: B, A :!=: B, A :=P: B *)
(* that specialize A = B, A <> B, A == B, etc., to {set _}. This is useful *)
(* for subtypes of {set T}, such as {group T}, that coerce to {set T}. *)
(* We give many lemmas on these operations, on card, and on set inclusion. *)
(* In addition to the standard suffixes described in ssrbool.v, we associate *)
(* the following suffixes to set operations: *)
(* 0 -- the empty set, as in in_set0 : (x \in set0) = false *)
(* T -- the full set, as in in_setT : x \in [set: T] *)
(* 1 -- a singleton set, as in in_set1 : (x \in [set a]) = (x == a) *)
(* 2 -- an unordered pair, as in *)
(* in_set2 : (x \in [set a; b]) = (x == a) || (x == b) *)
(* C -- complement, as in setCK : ~: ~: A = A *)
(* I -- intersection, as in setIid : A :&: A = A *)
(* U -- union, as in setUid : A :|: A = A *)
(* D -- difference, as in setDv : A :\: A = set0 *)
(* S -- a subset argument, as in *)
(* setIS: B \subset C -> A :&: B \subset A :&: C *)
(* These suffixes are sometimes preceded with an `s' to distinguish them from *)
(* their basic ssrbool interpretation, e.g., *)
(* card1 : #|pred1 x| = 1 and cards1 : #|[set x]| = 1 *)
(* We also use a trailing `r' to distinguish a right-hand complement from *)
(* commutativity, e.g., *)
(* setIC : A :&: B = B :&: A and setICr : A :&: ~: A = set0. *)
(******************************************************************************)
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Declare Scope set_scope.
Section SetType.
Variable T : finType.
Inductive set_type : predArgType := FinSet of {ffun pred T}.
Definition finfun_of_set A := let: FinSet f := A in f.
Definition set_of := set_type.
Identity Coercion type_of_set_of : set_of >-> set_type.
Definition set_isSub := Eval hnf in [isNew for finfun_of_set].
HB.instance Definition _ := set_isSub.
HB.instance Definition _ := [Finite of set_type by <:].
End SetType.
Delimit Scope set_scope with SET.
Bind Scope set_scope with set_type.
Bind Scope set_scope with set_of.
Open Scope set_scope.
Arguments set_of T%type.
Arguments finfun_of_set {T} A%SET.
Notation "{ 'set' T }" := (set_of T)
(at level 0, format "{ 'set' T }") : type_scope.
(* We later define several subtypes that coerce to set; for these it is *)
(* preferable to state equalities at the {set _} level, even when comparing *)
(* subtype values, because the primitive "injection" tactic tends to diverge *)
(* on complex types (e.g., quotient groups). We provide some parse-only *)
(* notation to make this technicality less obstructive. *)
Notation "A :=: B" := (A = B :> {set _})
(at level 70, no associativity, only parsing) : set_scope.
Notation "A :<>: B" := (A <> B :> {set _})
(at level 70, no associativity, only parsing) : set_scope.
Notation "A :==: B" := (A == B :> {set _})
(at level 70, no associativity, only parsing) : set_scope.
Notation "A :!=: B" := (A != B :> {set _})
(at level 70, no associativity, only parsing) : set_scope.
Notation "A :=P: B" := (A =P B :> {set _})
(at level 70, no associativity, only parsing) : set_scope.
HB.lock
Definition finset (T : finType) (P : pred T) : {set T} := @FinSet T (finfun P).
Canonical finset_unlock := Unlockable finset.unlock.
(* The weird type of pred_of_set is imposed by the syntactic restrictions on *)
(* coercion declarations; it is unfortunately not possible to use a functor *)
(* to retype the declaration, because this triggers an ugly bug in the Coq *)
(* coercion chaining code. *)
HB.lock
Definition pred_of_set T (A : set_type T) : fin_pred_sort (predPredType T)
:= val A.
Canonical pred_of_set_unlock := Unlockable pred_of_set.unlock.
Notation "[ 'set' x : T | P ]" := (finset (fun x : T => P%B))
(at level 0, x at level 99, only parsing) : set_scope.
Notation "[ 'set' x | P ]" := [set x : _ | P]
(at level 0, x, P at level 99, format "[ 'set' x | P ]") : set_scope.
Notation "[ 'set' x 'in' A ]" := [set x | x \in A]
(at level 0, x at level 99, format "[ 'set' x 'in' A ]") : set_scope.
Notation "[ 'set' x : T 'in' A ]" := [set x : T | x \in A]
(at level 0, x at level 99, only parsing) : set_scope.
Notation "[ 'set' x : T | P & Q ]" := [set x : T | P && Q]
(at level 0, x at level 99, only parsing) : set_scope.
Notation "[ 'set' x | P & Q ]" := [set x | P && Q ]
(at level 0, x, P at level 99, format "[ 'set' x | P & Q ]") : set_scope.
Notation "[ 'set' x : T 'in' A | P ]" := [set x : T | x \in A & P]
(at level 0, x at level 99, only parsing) : set_scope.
Notation "[ 'set' x 'in' A | P ]" := [set x | x \in A & P]
(at level 0, x at level 99, format "[ 'set' x 'in' A | P ]") : set_scope.
Notation "[ 'set' x 'in' A | P & Q ]" := [set x in A | P && Q]
(at level 0, x at level 99,
format "[ 'set' x 'in' A | P & Q ]") : set_scope.
Notation "[ 'set' x : T 'in' A | P & Q ]" := [set x : T in A | P && Q]
(at level 0, x at level 99, only parsing) : set_scope.
Notation "[ 'set' :: s ]" := (finset [in pred_of_seq s])
(at level 0, format "[ 'set' :: s ]") : set_scope.
(* This lets us use set and subtypes of set, like group or coset_of, both as *)
(* collective predicates and as arguments of the \pi(_) notation. *)
Coercion pred_of_set: set_type >-> fin_pred_sort.
(* Declare pred_of_set as a canonical instance of topred, but use the *)
(* coercion to resolve mem A to @mem (predPredType T) (pred_of_set A). *)
Canonical set_predType T := @PredType _ (unkeyed (set_type T)) (@pred_of_set T).
Section BasicSetTheory.
Variable T : finType.
Implicit Types (x : T) (A B : {set T}) (pA : pred T).
HB.instance Definition _ := Finite.on {set T}.
Lemma in_set pA x : x \in finset pA = pA x.
Proof. by rewrite [@finset]unlock unlock [x \in _]ffunE. Qed.
Lemma setP A B : A =i B <-> A = B.
Proof.
by split=> [eqAB|-> //]; apply/val_inj/ffunP=> x; have:= eqAB x; rewrite unlock.
Qed.
Definition set0 := [set x : T | false].
Definition setTfor := [set x : T | true].
Lemma in_setT x : x \in setTfor.
Proof. by rewrite in_set. Qed.
Lemma eqsVneq A B : eq_xor_neq A B (B == A) (A == B).
Proof. exact: eqVneq. Qed.
Lemma eq_finset (pA pB : pred T) : pA =1 pB -> finset pA = finset pB.
Proof. by move=> eq_p; apply/setP => x; rewrite !(in_set, inE) eq_p. Qed.
End BasicSetTheory.
Arguments eqsVneq {T} A B, {T A B}.
Arguments set0 {T}.
Arguments setTfor T%type.
Arguments eq_finset {T} [pA] pB eq_pAB.
#[global] Hint Resolve in_setT : core.
Notation "[ 'set' : T ]" := (setTfor T)
(at level 0, format "[ 'set' : T ]") : set_scope.
Notation setT := [set: _] (only parsing).
HB.lock
Definition set1 (T : finType) (a : T) := [set x | x == a].
Section setOpsDefs.
Variable T : finType.
Implicit Types (a x : T) (A B D : {set T}) (P : {set {set T}}).
Definition setU A B := [set x | (x \in A) || (x \in B)].
Definition setI A B := [set x in A | x \in B].
Definition setC A := [set x | x \notin A].
Definition setD A B := [set x | x \notin B & x \in A].
Definition ssetI P D := [set A in P | A \subset D].
Definition powerset D := [set A : {set T} | A \subset D].
End setOpsDefs.
Notation "[ 'set' a ]" := (set1 a)
(at level 0, a at level 99, format "[ 'set' a ]") : set_scope.
Notation "[ 'set' a : T ]" := [set (a : T)]
(at level 0, a at level 99, format "[ 'set' a : T ]") : set_scope.
Notation "A :|: B" := (setU A B) : set_scope.
Notation "a |: A" := ([set a] :|: A) : set_scope.
(* This is left-associative due to historical limitations of the .. Notation. *)
Notation "[ 'set' a1 ; a2 ; .. ; an ]" := (setU .. (a1 |: [set a2]) .. [set an])
(at level 0, a1 at level 99,
format "[ 'set' a1 ; a2 ; .. ; an ]") : set_scope.
Notation "A :&: B" := (setI A B) : set_scope.
Notation "~: A" := (setC A) (at level 35, right associativity) : set_scope.
Notation "[ 'set' ~ a ]" := (~: [set a])
(at level 0, format "[ 'set' ~ a ]") : set_scope.
Notation "A :\: B" := (setD A B) : set_scope.
Notation "A :\ a" := (A :\: [set a]) : set_scope.
Notation "P ::&: D" := (ssetI P D) (at level 48) : set_scope.
Section setOps.
Variable T : finType.
Implicit Types (a x : T) (A B C D : {set T}) (pA pB pC : pred T).
Lemma eqEsubset A B : (A == B) = (A \subset B) && (B \subset A).
Proof. by apply/eqP/subset_eqP=> /setP. Qed.
Lemma subEproper A B : A \subset B = (A == B) || (A \proper B).
Proof. by rewrite eqEsubset -andb_orr orbN andbT. Qed.
Lemma eqVproper A B : A \subset B -> A = B \/ A \proper B.
Proof. by rewrite subEproper => /predU1P. Qed.
Lemma properEneq A B : A \proper B = (A != B) && (A \subset B).
Proof. by rewrite andbC eqEsubset negb_and andb_orr andbN. Qed.
Lemma proper_neq A B : A \proper B -> A != B.
Proof. by rewrite properEneq; case/andP. Qed.
Lemma eqEproper A B : (A == B) = (A \subset B) && ~~ (A \proper B).
Proof. by rewrite negb_and negbK andb_orr andbN eqEsubset. Qed.
Lemma eqEcard A B : (A == B) = (A \subset B) && (#|B| <= #|A|).
Proof.
rewrite eqEsubset; apply: andb_id2l => sAB.
by rewrite (geq_leqif (subset_leqif_card sAB)).
Qed.
Lemma properEcard A B : (A \proper B) = (A \subset B) && (#|A| < #|B|).
Proof. by rewrite properEneq ltnNge andbC eqEcard; case: (A \subset B). Qed.
Lemma subset_leqif_cards A B : A \subset B -> (#|A| <= #|B| ?= iff (A == B)).
Proof. by move=> sAB; rewrite eqEsubset sAB; apply: subset_leqif_card. Qed.
Lemma in_set0 x : x \in set0 = false.
Proof. by rewrite in_set. Qed.
Lemma sub0set A : set0 \subset A.
Proof. by apply/subsetP=> x; rewrite in_set. Qed.
Lemma subset0 A : (A \subset set0) = (A == set0).
Proof. by rewrite eqEsubset sub0set andbT. Qed.
Lemma proper0 A : (set0 \proper A) = (A != set0).
Proof. by rewrite properE sub0set subset0. Qed.
Lemma subset_neq0 A B : A \subset B -> A != set0 -> B != set0.
Proof. by rewrite -!proper0 => sAB /proper_sub_trans->. Qed.
Lemma set_0Vmem A : (A = set0) + {x : T | x \in A}.
Proof.
case: (pickP (mem A)) => [x Ax | A0]; [by right; exists x | left].
by apply/setP=> x; rewrite in_set; apply: A0.
Qed.
Lemma set_enum A : [set x | x \in enum A] = A.
Proof. by apply/setP => x; rewrite in_set mem_enum. Qed.
Lemma enum_set0 : enum set0 = [::] :> seq T.
Proof. by rewrite (eq_enum (in_set _)) enum0. Qed.
Lemma subsetT A : A \subset setT.
Proof. by apply/subsetP=> x; rewrite in_set. Qed.
Lemma subsetT_hint mA : subset mA (mem [set: T]).
Proof. by rewrite unlock; apply/pred0P=> x; rewrite !inE in_set. Qed.
Hint Resolve subsetT_hint : core.
Lemma subTset A : (setT \subset A) = (A == setT).
Proof. by rewrite eqEsubset subsetT. Qed.
Lemma properT A : (A \proper setT) = (A != setT).
Proof. by rewrite properEneq subsetT andbT. Qed.
Lemma set1P x a : reflect (x = a) (x \in [set a]).
Proof. by rewrite set1.unlock in_set; apply: eqP. Qed.
Lemma enum_setT : enum [set: T] = Finite.enum T.
Proof. by rewrite (eq_enum (in_set _)) enumT. Qed.
Lemma in_set1 x a : (x \in [set a]) = (x == a).
Proof. by rewrite set1.unlock in_set. Qed.
Definition inE := (in_set, in_set1, inE).
Lemma set11 x : x \in [set x].
Proof. by rewrite !inE. Qed.
Lemma set1_inj : injective (@set1 T).
Proof. by move=> a b eqsab; apply/set1P; rewrite -eqsab set11. Qed.
Lemma enum_set1 a : enum [set a] = [:: a].
Proof. by rewrite set1.unlock (eq_enum (in_set _)) enum1. Qed.
Lemma setU1P x a B : reflect (x = a \/ x \in B) (x \in a |: B).
Proof. by rewrite !inE; apply: predU1P. Qed.
Lemma in_setU1 x a B : (x \in a |: B) = (x == a) || (x \in B).
Proof. by rewrite !inE. Qed.
Lemma set_nil : [set:: nil] = @set0 T. Proof. by rewrite -enum_set0 set_enum. Qed.
Lemma set_seq1 a : [set:: [:: a]] = [set a].
Proof. by rewrite -enum_set1 set_enum. Qed.
Lemma set_cons a s : [set:: a :: s] = a |: [set:: s].
Proof. by apply/setP=> x; rewrite !inE. Qed.
Lemma setU11 x B : x \in x |: B.
Proof. by rewrite !inE eqxx. Qed.
Lemma setU1r x a B : x \in B -> x \in a |: B.
Proof. by move=> Bx; rewrite !inE predU1r. Qed.
(* We need separate lemmas for the explicit enumerations since they *)
(* associate on the left. *)
Lemma set1Ul x A b : x \in A -> x \in A :|: [set b].
Proof. by move=> Ax; rewrite !inE Ax. Qed.
Lemma set1Ur A b : b \in A :|: [set b].
Proof. by rewrite !inE eqxx orbT. Qed.
Lemma in_setC1 x a : (x \in [set~ a]) = (x != a).
Proof. by rewrite !inE. Qed.
Lemma setC11 x : (x \in [set~ x]) = false.
Proof. by rewrite !inE eqxx. Qed.
Lemma setD1P x A b : reflect (x != b /\ x \in A) (x \in A :\ b).
Proof. by rewrite !inE; apply: andP. Qed.
Lemma in_setD1 x A b : (x \in A :\ b) = (x != b) && (x \in A) .
Proof. by rewrite !inE. Qed.
Lemma setD11 b A : (b \in A :\ b) = false.
Proof. by rewrite !inE eqxx. Qed.
Lemma setD1K a A : a \in A -> a |: (A :\ a) = A.
Proof. by move=> Aa; apply/setP=> x /[!inE]; case: eqP => // ->. Qed.
Lemma setU1K a B : a \notin B -> (a |: B) :\ a = B.
Proof.
by move/negPf=> nBa; apply/setP=> x /[!inE]; case: eqP => // ->.
Qed.
Lemma set2P x a b : reflect (x = a \/ x = b) (x \in [set a; b]).
Proof. by rewrite !inE; apply: pred2P. Qed.
Lemma in_set2 x a b : (x \in [set a; b]) = (x == a) || (x == b).
Proof. by rewrite !inE. Qed.
Lemma set21 a b : a \in [set a; b].
Proof. by rewrite !inE eqxx. Qed.
Lemma set22 a b : b \in [set a; b].
Proof. by rewrite !inE eqxx orbT. Qed.
Lemma setUP x A B : reflect (x \in A \/ x \in B) (x \in A :|: B).
Proof. by rewrite !inE; apply: orP. Qed.
Lemma in_setU x A B : (x \in A :|: B) = (x \in A) || (x \in B).
Proof. exact: in_set. Qed.
Lemma setUC A B : A :|: B = B :|: A.
Proof. by apply/setP => x; rewrite !inE orbC. Qed.
Lemma setUS A B C : A \subset B -> C :|: A \subset C :|: B.
Proof.
move=> sAB; apply/subsetP=> x; rewrite !inE.
by case: (x \in C) => //; apply: (subsetP sAB).
Qed.
Lemma setSU A B C : A \subset B -> A :|: C \subset B :|: C.
Proof. by move=> sAB; rewrite -!(setUC C) setUS. Qed.
Lemma setUSS A B C D : A \subset C -> B \subset D -> A :|: B \subset C :|: D.
Proof. by move=> /(setSU B) /subset_trans sAC /(setUS C)/sAC. Qed.
Lemma set0U A : set0 :|: A = A.
Proof. by apply/setP => x; rewrite !inE orFb. Qed.
Lemma setU0 A : A :|: set0 = A.
Proof. by rewrite setUC set0U. Qed.
Lemma setUA A B C : A :|: (B :|: C) = A :|: B :|: C.
Proof. by apply/setP => x; rewrite !inE orbA. Qed.
Lemma setUCA A B C : A :|: (B :|: C) = B :|: (A :|: C).
Proof. by rewrite !setUA (setUC B). Qed.
Lemma setUAC A B C : A :|: B :|: C = A :|: C :|: B.
Proof. by rewrite -!setUA (setUC B). Qed.
Lemma setUACA A B C D : (A :|: B) :|: (C :|: D) = (A :|: C) :|: (B :|: D).
Proof. by rewrite -!setUA (setUCA B). Qed.
Lemma setTU A : setT :|: A = setT.
Proof. by apply/setP => x; rewrite !inE orTb. Qed.
Lemma setUT A : A :|: setT = setT.
Proof. by rewrite setUC setTU. Qed.
Lemma setUid A : A :|: A = A.
Proof. by apply/setP=> x; rewrite inE orbb. Qed.
Lemma setUUl A B C : A :|: B :|: C = (A :|: C) :|: (B :|: C).
Proof. by rewrite setUA !(setUAC _ C) -(setUA _ C) setUid. Qed.
Lemma setUUr A B C : A :|: (B :|: C) = (A :|: B) :|: (A :|: C).
Proof. by rewrite !(setUC A) setUUl. Qed.
(* intersection *)
(* setIdP is a generalisation of setIP that applies to comprehensions. *)
Lemma setIdP x pA pB : reflect (pA x /\ pB x) (x \in [set y | pA y & pB y]).
Proof. by rewrite !inE; apply: andP. Qed.
Lemma setId2P x pA pB pC :
reflect [/\ pA x, pB x & pC x] (x \in [set y | pA y & pB y && pC y]).
Proof. by rewrite !inE; apply: and3P. Qed.
Lemma setIdE A pB : [set x in A | pB x] = A :&: [set x | pB x].
Proof. by apply/setP=> x; rewrite !inE. Qed.
Lemma setIP x A B : reflect (x \in A /\ x \in B) (x \in A :&: B).
Proof. exact: (iffP (@setIdP _ _ _)). Qed.
Lemma in_setI x A B : (x \in A :&: B) = (x \in A) && (x \in B).
Proof. exact: in_set. Qed.
Lemma setIC A B : A :&: B = B :&: A.
Proof. by apply/setP => x; rewrite !inE andbC. Qed.
Lemma setIS A B C : A \subset B -> C :&: A \subset C :&: B.
Proof.
move=> sAB; apply/subsetP=> x; rewrite !inE.
by case: (x \in C) => //; apply: (subsetP sAB).
Qed.
Lemma setSI A B C : A \subset B -> A :&: C \subset B :&: C.
Proof. by move=> sAB; rewrite -!(setIC C) setIS. Qed.
Lemma setISS A B C D : A \subset C -> B \subset D -> A :&: B \subset C :&: D.
Proof. by move=> /(setSI B) /subset_trans sAC /(setIS C) /sAC. Qed.
Lemma setTI A : setT :&: A = A.
Proof. by apply/setP => x; rewrite !inE andTb. Qed.
Lemma setIT A : A :&: setT = A.
Proof. by rewrite setIC setTI. Qed.
Lemma set0I A : set0 :&: A = set0.
Proof. by apply/setP => x; rewrite !inE andFb. Qed.
Lemma setI0 A : A :&: set0 = set0.
Proof. by rewrite setIC set0I. Qed.
Lemma setIA A B C : A :&: (B :&: C) = A :&: B :&: C.
Proof. by apply/setP=> x; rewrite !inE andbA. Qed.
Lemma setICA A B C : A :&: (B :&: C) = B :&: (A :&: C).
Proof. by rewrite !setIA (setIC A). Qed.
Lemma setIAC A B C : A :&: B :&: C = A :&: C :&: B.
Proof. by rewrite -!setIA (setIC B). Qed.
Lemma setIACA A B C D : (A :&: B) :&: (C :&: D) = (A :&: C) :&: (B :&: D).
Proof. by rewrite -!setIA (setICA B). Qed.
Lemma setIid A : A :&: A = A.
Proof. by apply/setP=> x; rewrite inE andbb. Qed.
Lemma setIIl A B C : A :&: B :&: C = (A :&: C) :&: (B :&: C).
Proof. by rewrite setIA !(setIAC _ C) -(setIA _ C) setIid. Qed.
Lemma setIIr A B C : A :&: (B :&: C) = (A :&: B) :&: (A :&: C).
Proof. by rewrite !(setIC A) setIIl. Qed.
(* distribute /cancel *)
Lemma setIUr A B C : A :&: (B :|: C) = (A :&: B) :|: (A :&: C).
Proof. by apply/setP=> x; rewrite !inE andb_orr. Qed.
Lemma setIUl A B C : (A :|: B) :&: C = (A :&: C) :|: (B :&: C).
Proof. by apply/setP=> x; rewrite !inE andb_orl. Qed.
Lemma setUIr A B C : A :|: (B :&: C) = (A :|: B) :&: (A :|: C).
Proof. by apply/setP=> x; rewrite !inE orb_andr. Qed.
Lemma setUIl A B C : (A :&: B) :|: C = (A :|: C) :&: (B :|: C).
Proof. by apply/setP=> x; rewrite !inE orb_andl. Qed.
Lemma setUK A B : (A :|: B) :&: A = A.
Proof. by apply/setP=> x; rewrite !inE orbK. Qed.
Lemma setKU A B : A :&: (B :|: A) = A.
Proof. by apply/setP=> x; rewrite !inE orKb. Qed.
Lemma setIK A B : (A :&: B) :|: A = A.
Proof. by apply/setP=> x; rewrite !inE andbK. Qed.
Lemma setKI A B : A :|: (B :&: A) = A.
Proof. by apply/setP=> x; rewrite !inE andKb. Qed.
(* complement *)
Lemma setCP x A : reflect (~ x \in A) (x \in ~: A).
Proof. by rewrite !inE; apply: negP. Qed.
Lemma in_setC x A : (x \in ~: A) = (x \notin A).
Proof. exact: in_set. Qed.
Lemma setCK : involutive (@setC T).
Proof. by move=> A; apply/setP=> x; rewrite !inE negbK. Qed.
Lemma setC_inj : injective (@setC T).
Proof. exact: can_inj setCK. Qed.
Lemma subsets_disjoint A B : (A \subset B) = [disjoint A & ~: B].
Proof. by rewrite subset_disjoint; apply: eq_disjoint_r => x; rewrite !inE. Qed.
Lemma disjoints_subset A B : [disjoint A & B] = (A \subset ~: B).
Proof. by rewrite subsets_disjoint setCK. Qed.
Lemma powersetCE A B : (A \in powerset (~: B)) = [disjoint A & B].
Proof. by rewrite inE disjoints_subset. Qed.
Lemma setCS A B : (~: A \subset ~: B) = (B \subset A).
Proof. by rewrite !subsets_disjoint setCK disjoint_sym. Qed.
Lemma setCU A B : ~: (A :|: B) = ~: A :&: ~: B.
Proof. by apply/setP=> x; rewrite !inE negb_or. Qed.
Lemma setCI A B : ~: (A :&: B) = ~: A :|: ~: B.
Proof. by apply/setP=> x; rewrite !inE negb_and. Qed.
Lemma setUCr A : A :|: ~: A = setT.
Proof. by apply/setP=> x; rewrite !inE orbN. Qed.
Lemma setICr A : A :&: ~: A = set0.
Proof. by apply/setP=> x; rewrite !inE andbN. Qed.
Lemma setC0 : ~: set0 = [set: T].
Proof. by apply/setP=> x; rewrite !inE. Qed.
Lemma setCT : ~: [set: T] = set0.
Proof. by rewrite -setC0 setCK. Qed.
Lemma properC A B : (~: B \proper ~: A) = (A \proper B).
Proof. by rewrite !properE !setCS. Qed.
(* difference *)
Lemma setDP A B x : reflect (x \in A /\ x \notin B) (x \in A :\: B).
Proof. by rewrite inE andbC; apply: andP. Qed.
Lemma in_setD A B x : (x \in A :\: B) = (x \notin B) && (x \in A).
Proof. exact: in_set. Qed.
Lemma setDE A B : A :\: B = A :&: ~: B.
Proof. by apply/setP => x; rewrite !inE andbC. Qed.
Lemma setSD A B C : A \subset B -> A :\: C \subset B :\: C.
Proof. by rewrite !setDE; apply: setSI. Qed.
Lemma setDS A B C : A \subset B -> C :\: B \subset C :\: A.
Proof. by rewrite !setDE -setCS; apply: setIS. Qed.
Lemma setDSS A B C D : A \subset C -> D \subset B -> A :\: B \subset C :\: D.
Proof. by move=> /(setSD B) /subset_trans sAC /(setDS C) /sAC. Qed.
Lemma setD0 A : A :\: set0 = A.
Proof. by apply/setP=> x; rewrite !inE. Qed.
Lemma set0D A : set0 :\: A = set0.
Proof. by apply/setP=> x; rewrite !inE andbF. Qed.
Lemma setDT A : A :\: setT = set0.
Proof. by apply/setP=> x; rewrite !inE. Qed.
Lemma setTD A : setT :\: A = ~: A.
Proof. by apply/setP=> x; rewrite !inE andbT. Qed.
Lemma setDv A : A :\: A = set0.
Proof. by apply/setP=> x; rewrite !inE andNb. Qed.
Lemma setCD A B : ~: (A :\: B) = ~: A :|: B.
Proof. by rewrite !setDE setCI setCK. Qed.
Lemma setID A B : A :&: B :|: A :\: B = A.
Proof. by rewrite setDE -setIUr setUCr setIT. Qed.
Lemma setDUl A B C : (A :|: B) :\: C = (A :\: C) :|: (B :\: C).
Proof. by rewrite !setDE setIUl. Qed.
Lemma setDUr A B C : A :\: (B :|: C) = (A :\: B) :&: (A :\: C).
Proof. by rewrite !setDE setCU setIIr. Qed.
Lemma setDIl A B C : (A :&: B) :\: C = (A :\: C) :&: (B :\: C).
Proof. by rewrite !setDE setIIl. Qed.
Lemma setIDA A B C : A :&: (B :\: C) = (A :&: B) :\: C.
Proof. by rewrite !setDE setIA. Qed.
Lemma setIDAC A B C : (A :\: B) :&: C = (A :&: C) :\: B.
Proof. by rewrite !setDE setIAC. Qed.
Lemma setDIr A B C : A :\: (B :&: C) = (A :\: B) :|: (A :\: C).
Proof. by rewrite !setDE setCI setIUr. Qed.
Lemma setDDl A B C : (A :\: B) :\: C = A :\: (B :|: C).
Proof. by rewrite !setDE setCU setIA. Qed.
Lemma setDDr A B C : A :\: (B :\: C) = (A :\: B) :|: (A :&: C).
Proof. by rewrite !setDE setCI setIUr setCK. Qed.
(* powerset *)
Lemma powersetE A B : (A \in powerset B) = (A \subset B).
Proof. by rewrite inE. Qed.
Lemma powersetS A B : (powerset A \subset powerset B) = (A \subset B).
Proof.
apply/subsetP/idP=> [sAB | sAB C /[!inE]/subset_trans->//].
by rewrite -powersetE sAB // inE.
Qed.
Lemma powerset0 : powerset set0 = [set set0] :> {set {set T}}.
Proof. by apply/setP=> A; rewrite set1.unlock !inE subset0. Qed.
Lemma powersetT : powerset [set: T] = [set: {set T}].
Proof. by apply/setP=> A; rewrite !inE subsetT. Qed.
Lemma setI_powerset P A : P :&: powerset A = P ::&: A.
Proof. by apply/setP=> B; rewrite !inE. Qed.
(* cardinal lemmas for sets *)
Lemma cardsE pA : #|[set x in pA]| = #|pA|.
Proof. exact/eq_card/in_set. Qed.
Lemma sum1dep_card pA : \sum_(x | pA x) 1 = #|[set x | pA x]|.
Proof. by rewrite sum1_card cardsE. Qed.
Lemma sum_nat_cond_const pA n : \sum_(x | pA x) n = #|[set x | pA x]| * n.
Proof. by rewrite sum_nat_const cardsE. Qed.
Lemma cards0 : #|@set0 T| = 0.
Proof. by rewrite cardsE card0. Qed.
Lemma cards_eq0 A : (#|A| == 0) = (A == set0).
Proof. by rewrite (eq_sym A) eqEcard sub0set cards0 leqn0. Qed.
Lemma set0Pn A : reflect (exists x, x \in A) (A != set0).
Proof. by rewrite -cards_eq0; apply: existsP. Qed.
Lemma card_gt0 A : (0 < #|A|) = (A != set0).
Proof. by rewrite lt0n cards_eq0. Qed.
Lemma cards0_eq A : #|A| = 0 -> A = set0.
Proof. by move=> A_0; apply/setP=> x; rewrite inE (card0_eq A_0). Qed.
Lemma cards1 x : #|[set x]| = 1.
Proof. by rewrite set1.unlock cardsE card1. Qed.
Lemma cardsUI A B : #|A :|: B| + #|A :&: B| = #|A| + #|B|.
Proof. by rewrite !cardsE cardUI. Qed.
Lemma cardsU A B : #|A :|: B| = (#|A| + #|B| - #|A :&: B|)%N.
Proof. by rewrite -cardsUI addnK. Qed.
Lemma cardsI A B : #|A :&: B| = (#|A| + #|B| - #|A :|: B|)%N.
Proof. by rewrite -cardsUI addKn. Qed.
Lemma cardsT : #|[set: T]| = #|T|.
Proof. by rewrite cardsE. Qed.
Lemma cardsID B A : #|A :&: B| + #|A :\: B| = #|A|.
Proof. by rewrite !cardsE cardID. Qed.
Lemma cardsD A B : #|A :\: B| = (#|A| - #|A :&: B|)%N.
Proof. by rewrite -(cardsID B A) addKn. Qed.
Lemma cardsC A : #|A| + #|~: A| = #|T|.
Proof. by rewrite cardsE cardC. Qed.
Lemma cardsCs A : #|A| = #|T| - #|~: A|.
Proof. by rewrite -(cardsC A) addnK. Qed.
Lemma cardsU1 a A : #|a |: A| = (a \notin A) + #|A|.
Proof. by rewrite -cardU1; apply: eq_card=> x; rewrite !inE. Qed.
Lemma cards2 a b : #|[set a; b]| = (a != b).+1.
Proof. by rewrite -card2; apply: eq_card=> x; rewrite !inE. Qed.
Lemma cardsC1 a : #|[set~ a]| = #|T|.-1.
Proof. by rewrite -(cardC1 a); apply: eq_card=> x; rewrite !inE. Qed.
Lemma cardsD1 a A : #|A| = (a \in A) + #|A :\ a|.
Proof.
by rewrite (cardD1 a); congr (_ + _); apply: eq_card => x; rewrite !inE.
Qed.
(* other inclusions *)
Lemma subsetIl A B : A :&: B \subset A.
Proof. by apply/subsetP=> x /[!inE] /andP[]. Qed.
Lemma subsetIr A B : A :&: B \subset B.
Proof. by apply/subsetP=> x /[!inE] /andP[]. Qed.
Lemma subsetUl A B : A \subset A :|: B.
Proof. by apply/subsetP=> x /[!inE] ->. Qed.
Lemma subsetUr A B : B \subset A :|: B.
Proof. by apply/subsetP=> x; rewrite inE orbC => ->. Qed.
Lemma subsetU1 x A : A \subset x |: A.
Proof. exact: subsetUr. Qed.
Lemma subsetDl A B : A :\: B \subset A.
Proof. by rewrite setDE subsetIl. Qed.
Lemma subD1set A x : A :\ x \subset A.
Proof. by rewrite subsetDl. Qed.
Lemma subsetDr A B : A :\: B \subset ~: B.
Proof. by rewrite setDE subsetIr. Qed.
Lemma sub1set A x : ([set x] \subset A) = (x \in A).
Proof. by rewrite -subset_pred1; apply: eq_subset=> y; rewrite !inE. Qed.
Variant cards_eq_spec A : seq T -> {set T} -> nat -> Type :=
| CardEq (s : seq T) & uniq s : cards_eq_spec A s [set x | x \in s] (size s).
Lemma cards_eqP A : cards_eq_spec A (enum A) A #|A|.
Proof.
by move: (enum A) (cardE A) (set_enum A) (enum_uniq A) => s -> <-; constructor.
Qed.
Lemma cards1P A : reflect (exists x, A = [set x]) (#|A| == 1).
Proof.
apply: (iffP idP) => [|[x ->]]; last by rewrite cards1.
by have [[|x []]// _] := cards_eqP; exists x; apply/setP => y; rewrite !inE.
Qed.
Lemma cards2P A : reflect (exists x y : T, x != y /\ A = [set x; y])
(#|A| == 2).
Proof.
apply: (iffP idP) => [|[x] [y] [xy ->]]; last by rewrite cards2 xy.
have [[|x [|y []]]//=] := cards_eqP; rewrite !inE andbT => neq_xy.
by exists x, y; split=> //; apply/setP => z; rewrite !inE.
Qed.
Lemma subset1 A x : (A \subset [set x]) = (A == [set x]) || (A == set0).
Proof.
rewrite eqEcard cards1 -cards_eq0 orbC andbC.
by case: posnP => // A0; rewrite (cards0_eq A0) sub0set.
Qed.
Lemma powerset1 x : powerset [set x] = [set set0; [set x]].
Proof. by apply/setP=> A; rewrite inE subset1 orbC set1.unlock !inE. Qed.
Lemma setIidPl A B : reflect (A :&: B = A) (A \subset B).
Proof.
apply: (iffP subsetP) => [sAB | <- x /setIP[] //].
by apply/setP=> x /[1!inE]; apply/andb_idr/sAB.
Qed.
Arguments setIidPl {A B}.
Lemma setIidPr A B : reflect (A :&: B = B) (B \subset A).
Proof. by rewrite setIC; apply: setIidPl. Qed.
Lemma cardsDS A B : B \subset A -> #|A :\: B| = (#|A| - #|B|)%N.
Proof. by rewrite cardsD => /setIidPr->. Qed.
Lemma setUidPl A B : reflect (A :|: B = A) (B \subset A).
Proof.
by rewrite -setCS (sameP setIidPl eqP) -setCU (inj_eq setC_inj); apply: eqP.
Qed.
Lemma setUidPr A B : reflect (A :|: B = B) (A \subset B).
Proof. by rewrite setUC; apply: setUidPl. Qed.
Lemma setDidPl A B : reflect (A :\: B = A) [disjoint A & B].
Proof. by rewrite setDE disjoints_subset; apply: setIidPl. Qed.
Lemma subIset A B C : (B \subset A) || (C \subset A) -> (B :&: C \subset A).
Proof. by case/orP; apply: subset_trans; rewrite (subsetIl, subsetIr). Qed.
Lemma subsetI A B C : (A \subset B :&: C) = (A \subset B) && (A \subset C).
Proof.
rewrite !(sameP setIidPl eqP) setIA; have [-> //|] := eqVneq (A :&: B) A.
by apply: contraNF => /eqP <-; rewrite -setIA -setIIl setIAC.
Qed.
Lemma subsetIP A B C : reflect (A \subset B /\ A \subset C) (A \subset B :&: C).
Proof. by rewrite subsetI; apply: andP. Qed.
Lemma subsetIidl A B : (A \subset A :&: B) = (A \subset B).
Proof. by rewrite subsetI subxx. Qed.
Lemma subsetIidr A B : (B \subset A :&: B) = (B \subset A).
Proof. by rewrite setIC subsetIidl. Qed.
Lemma powersetI A B : powerset (A :&: B) = powerset A :&: powerset B.
Proof. by apply/setP=> C; rewrite !inE subsetI. Qed.
Lemma subUset A B C : (B :|: C \subset A) = (B \subset A) && (C \subset A).
Proof. by rewrite -setCS setCU subsetI !setCS. Qed.
Lemma subsetU A B C : (A \subset B) || (A \subset C) -> A \subset B :|: C.
Proof. by rewrite -!(setCS _ A) setCU; apply: subIset. Qed.
Lemma subUsetP A B C : reflect (A \subset C /\ B \subset C) (A :|: B \subset C).
Proof. by rewrite subUset; apply: andP. Qed.
Lemma subsetC A B : (A \subset ~: B) = (B \subset ~: A).
Proof. by rewrite -setCS setCK. Qed.
Lemma subCset A B : (~: A \subset B) = (~: B \subset A).
Proof. by rewrite -setCS setCK. Qed.
Lemma subsetD A B C : (A \subset B :\: C) = (A \subset B) && [disjoint A & C].
Proof. by rewrite setDE subsetI -disjoints_subset. Qed.
Lemma subDset A B C : (A :\: B \subset C) = (A \subset B :|: C).
Proof.
apply/subsetP/subsetP=> sABC x; rewrite !inE.
by case Bx: (x \in B) => // Ax; rewrite sABC ?inE ?Bx.
by case Bx: (x \in B) => // /sABC; rewrite inE Bx.
Qed.
Lemma subsetDP A B C :
reflect (A \subset B /\ [disjoint A & C]) (A \subset B :\: C).
Proof. by rewrite subsetD; apply: andP. Qed.
Lemma setU_eq0 A B : (A :|: B == set0) = (A == set0) && (B == set0).
Proof. by rewrite -!subset0 subUset. Qed.
Lemma setD_eq0 A B : (A :\: B == set0) = (A \subset B).
Proof. by rewrite -subset0 subDset setU0. Qed.
Lemma setI_eq0 A B : (A :&: B == set0) = [disjoint A & B].
Proof. by rewrite disjoints_subset -setD_eq0 setDE setCK. Qed.
Lemma disjoint_setI0 A B : [disjoint A & B] -> A :&: B = set0.
Proof. by rewrite -setI_eq0; move/eqP. Qed.
Lemma disjoints1 A x : [disjoint [set x] & A] = (x \notin A).
Proof. by rewrite (@eq_disjoint1 _ x) // => y; rewrite !inE. Qed.
Lemma subsetD1 A B x : (A \subset B :\ x) = (A \subset B) && (x \notin A).
Proof. by rewrite setDE subsetI subsetC sub1set inE. Qed.
Lemma subsetD1P A B x : reflect (A \subset B /\ x \notin A) (A \subset B :\ x).
Proof. by rewrite subsetD1; apply: andP. Qed.
Lemma properD1 A x : x \in A -> A :\ x \proper A.
Proof.
move=> Ax; rewrite properE subsetDl; apply/subsetPn; exists x=> //.
by rewrite in_setD1 Ax eqxx.
Qed.
Lemma properIr A B : ~~ (B \subset A) -> A :&: B \proper B.
Proof. by move=> nsAB; rewrite properE subsetIr subsetI negb_and nsAB. Qed.
Lemma properIl A B : ~~ (A \subset B) -> A :&: B \proper A.
Proof. by move=> nsBA; rewrite properE subsetIl subsetI negb_and nsBA orbT. Qed.
Lemma properUr A B : ~~ (A \subset B) -> B \proper A :|: B.
Proof. by rewrite properE subsetUr subUset subxx /= andbT. Qed.
Lemma properUl A B : ~~ (B \subset A) -> A \proper A :|: B.
Proof. by move=> not_sBA; rewrite setUC properUr. Qed.
Lemma proper1set A x : ([set x] \proper A) -> (x \in A).
Proof. by move/proper_sub; rewrite sub1set. Qed.
Lemma properIset A B C : (B \proper A) || (C \proper A) -> (B :&: C \proper A).
Proof. by case/orP; apply: sub_proper_trans; rewrite (subsetIl, subsetIr). Qed.
Lemma properI A B C : (A \proper B :&: C) -> (A \proper B) && (A \proper C).
Proof.
move=> pAI; apply/andP.
by split; apply: (proper_sub_trans pAI); rewrite (subsetIl, subsetIr).
Qed.
Lemma properU A B C : (B :|: C \proper A) -> (B \proper A) && (C \proper A).
Proof.
move=> pUA; apply/andP.
by split; apply: sub_proper_trans pUA; rewrite (subsetUr, subsetUl).
Qed.
Lemma properD A B C : (A \proper B :\: C) -> (A \proper B) && [disjoint A & C].
Proof. by rewrite setDE disjoints_subset => /properI/andP[-> /proper_sub]. Qed.
Lemma properCr A B : (A \proper ~: B) = (B \proper ~: A).
Proof. by rewrite -properC setCK. Qed.
Lemma properCl A B : (~: A \proper B) = (~: B \proper A).
Proof. by rewrite -properC setCK. Qed.
(* relationship with seq *)
Lemma enum_setU A B : perm_eq (enum (A :|: B)) (undup (enum A ++ enum B)).
Proof.
apply: uniq_perm; rewrite ?enum_uniq ?undup_uniq//.
by move=> i; rewrite mem_undup mem_enum inE mem_cat !mem_enum.
Qed.