-
Notifications
You must be signed in to change notification settings - Fork 28
/
finmap.v
4057 lines (3211 loc) · 149 KB
/
finmap.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-2019 Microsoft Corporation and Inria. *)
(* Distributed under the terms of CeCILL-B. *)
From HB Require Import structures.
Set Warnings "-notation-incompatible-format".
From mathcomp Require Import ssreflect ssrbool ssrnat eqtype ssrfun seq.
Set Warnings "notation-incompatible-format".
From mathcomp Require Import choice path finset finfun fintype bigop.
(*****************************************************************************)
(* This file provides representations for finite sets over a choiceType K, *)
(* finite maps with keys in a choiceType K and the values in an arbitrary *)
(* type V, and total functions from K to V with finite support. *)
(* The domain (resp. support) of a finite map (resp. fintely supported *)
(* function) is a finite set, and so is the codomain (resp. image) when V *)
(* is a choice type. *)
(* *)
(* {fset K} == finite sets of elements of K *)
(* {fmap K -> V} == finitely supported maps from K to V. *)
(* {fsfun K -> T for dflt} == finitely supported functions *)
(* with default value dflt : K -> V outside the support *)
(* *)
(********* finite sets *******************************************************)
(* *)
(* In the remainder, A and B are of type {fset K}. *)
(* *)
(* - {fset K} is provided with a canonical structure of predType, in order *)
(* to enable the notation "a \in A" *)
(* - There is a coercion from {fset K} to Type in order to interpret any *)
(* finset A as the subType of elements a : K such that a \in A. *)
(* Because of this coercion, writing a : A makes sense. *)
(* *)
(* The following notations are in the %fset scope *)
(* fset0 == the empty finite set *)
(* [fset k] == the singleton finite set {k} *)
(* A `&` B == the intersection of A and B *)
(* A `|` B == the union of A and B *)
(* a |` B == the union of singleton a and B *)
(* A `\` B == the complement of B in A *)
(* A `\ b == A without b *)
(* A `*` B == the cartesian product of A and B *)
(* [disjoint A & B] := A `&` B == 0 *)
(* A `<=` B == A is a subset of B *)
(* A `<` B == A is a proper subset of B *)
(* #|`A| == cardinal of A *)
(* fincl AsubB a == turns a : A into an element of B *)
(* using a proof AsubB of A \fsubset B *)
(* fsub B A == turns A : {fset K} into a {set B} *)
(* 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. *)
(* [` aA] == an element a of A such that val [` aA] = a *)
(* where aA is a proof of a \in A *)
(* *)
(* In order to support the following notations, we introduce three canonical *)
(* structure that reflect the finiteness of a predicate, in the following *)
(* notations, p (resp q) are such finite predicates, which are ultimately *)
(* represented by elements A (resp B) from {fset K}. *)
(* *)
(* [fset x in p | P] == the set of all x of type K, such that *)
(* x \in p and P x where P is a predicate on K *)
(* [fset x in p | P & Q] := [set x in p | P && Q]. *)
(* *)
(* [fset E | x in p] == the set of all the values of the expression E, for x *)
(* drawn from the collective finite predicate p. *)
(* [fset E | x in p & P] == the set of values of E for x drawn from p, such *)
(* that P is true. *)
(* [fset E | x in p, y in q] == the set of values of E for x drawn from p and*)
(* and y drawn from q; q may depend on x. *)
(* [fset E | x in p, y in q & P] == the set of values of E for x drawn from *)
(* p and y drawn from q; such that P is true. *)
(* [fsetval x in p] == the set of (val x) for x in the finite predicate p *)
(* [fsetval x in p | P ] == the set of (val x) for x in p, such that P *)
(* [fsetval x in p | P & Q] := [fsetval x in p | P && Q] *)
(* *)
(* For each notation above, there is an additional one with ':' instead of *)
(* 'in' which is used to range over the finite type A instead of the finite *)
(* set A, and the optional predicate is over A instead of K *)
(* For example: *)
(* [fset x : A | P] := [fset x in {: A} | P] *)
(* == the set of all x of type A, such that P x *)
(* [fset E | x : A] == the set of all the values of the expression E, for x *)
(* drawn from the finite type A *)
(* *)
(* For each [fset ...] or [fsetval ...] notation, there is a keyed variant *)
(* written [fset[key] ...] or [fsetval[key] ...] for locking *)
(* *)
(******* finite maps *********************************************************)
(* *)
(* Finite maps are finite functions (from finfun) where the domain is *)
(* obtained by the coercion of a {fset A} to the finType of its elements *)
(* Operations on finmap: *)
(* The following notations are in the %fmap scope *)
(* *)
(* f.[? k] == returns Some v if k maps to v, otherwise None *)
(* f.[p] == returns v if p has type k \in f, and k maps to v *)
(* f.[k <- v] == f extended with the mapping k -> v *)
(* domf f == finite set (of type {fset K}) of keys of f *)
(* codomf f == finite set (of type {fset V}) of values of f *)
(* k \in f == k is a key of f *)
(* := k \in domf f *)
(* [fmap] == the empty finite map *)
(* [fmap x : S => E] == the finmap defined by E on the support S *)
(* f.[& A] == f restricted to A (intersected with domf f) *)
(* f.[\ A] := f.[& domf `\` A] *)
(* == f where all the keys in A have been removed *)
(* f.[~ k] := f.[\ [fset k]] *)
(* f + g == concatenation of f and g, *)
(* the keys of g override the keys of f *)
(* *)
(******* finitely supported functions ****************************************)
(* *)
(* Operation on function with finite support, i.e. finmap with default value *)
(* for elements outside of the support. Contrarly to finmap, these are total *)
(* function, so we provide a coercion to Funclass *)
(* *)
(* {fsfun K -> T for dflt_fun} == finitely supported functions with default *)
(* value dflt : K -> V outside the support *)
(* {fsfun K -> T of x => dflt} := {fsfun K -> T for fun x => dflt} *)
(* {fsfun K -> T with dflt} := {fsfun K -> T for fun=> dflt} *)
(* {fsfun K -> K} := {fsfun K -> T for fun x => x} *)
(* *)
(* [fsfun for dflt_fun] == the default fsfun *)
(* [fsfun of x => dflt] == the default fsfun *)
(* [fsfun x : A => F | dflt] == the fsfun which takes value F on type A *)
(* x has type A : {fset T} *)
(* [fsfun x in A => F | dflt] == the fsfun which takes value F on set A *)
(* x has type T, and x in A : {fset T} *)
(* we also provide untyped variants and variants where default is ommitted *)
(* e.g. [fsfun x : A => F] [fsfun x => F | default] [fsfun]... *)
(* and many variants to give the possibility to insert a key : unit *)
(* to prevent conversion from diverging, e.g. *)
(* [fsfun[key] x : A => F | default] and [fsfun[key] x in A => F | default] *)
(* ... *)
(* (f \o g)%fsfun == composition of fsfun *)
(* fsinjectiveb f == boolean predicate of injectivity of f *)
(*****************************************************************************)
Set Implicit Arguments.
Unset Strict Implicit.
Import Prenex Implicits.
Reserved Notation "A `=` B" (at level 70, no associativity).
Reserved Notation "A `<>` B" (at level 70, no associativity).
Reserved Notation "A `==` B" (at level 70, no associativity).
Reserved Notation "A `!=` B" (at level 70, no associativity).
Reserved Notation "A `=P` B" (at level 70, no associativity).
Reserved Notation "f @`[ key ] A" (at level 24, key at level 0).
Reserved Notation "f @2`[ key ] ( A , B )"
(at level 24, format "f @2`[ key ] ( A , B )").
Reserved Notation "f @` A" (at level 24).
Reserved Notation "f @2` ( A , B )" (at level 24, format "f @2` ( A , B )").
(* unary *)
Reserved Notation "[ 'fset' E | x : T 'in' A ]" (at level 0, E, x at level 99).
Reserved Notation "[ 'fset' E | x 'in' A ]" (at level 0, E, x at level 99).
Reserved Notation "[ 'fset' E | x : A ]" (at level 0, E, x at level 99).
Reserved Notation "[ 'fset' x : T 'in' A ]" (at level 0, x at level 99).
Reserved Notation "[ 'fset' x : T 'in' A | P ]"
(at level 0, x at level 99).
Reserved Notation "[ 'fset' x 'in' A | P ]" (at level 0, x at level 99).
Reserved Notation "[ 'fset' x 'in' A ]" (at level 0, x at level 99).
Reserved Notation "[ 'fset' x : T | P ]" (at level 0, x at level 99).
Reserved Notation "[ 'fset' x : T | P & Q ]" (at level 0, x at level 99).
Reserved Notation "[ 'fset' x : T 'in' A | P & Q ]"
(at level 0, x at level 99).
Reserved Notation "[ 'fset' x 'in' A | P & Q ]" (at level 0, x at level 99).
(* binary *)
Reserved Notation "[ 'fset' E | x : T 'in' A , y : T' 'in' B ]"
(at level 0, E, x at level 99, A at level 200, y at level 99).
Reserved Notation "[ 'fset' E | x 'in' A , y 'in' B ]"
(at level 0, E, x at level 99, A at level 200, y at level 99).
(* keyed parse only *)
Reserved Notation "[ 'fset[' key ] E | x : T 'in' A ]"
(at level 0, E, x at level 99).
Reserved Notation "[ 'fset[' key ] E | x 'in' A ]"
(at level 0, E, x at level 99).
Reserved Notation "[ 'fset[' key ] E | x : A ]" (at level 0, E, x at level 99).
Reserved Notation "[ 'fset[' key ] E | x 'in' A & P ]"
(at level 0, E, x at level 99).
Reserved Notation "[ 'fset[' key ] E | x : A & P ]"
(at level 0, E, x at level 99).
Reserved Notation "[ 'fset[' key ] E | x : T 'in' A , y : T' 'in' B ]"
(at level 0, E, x at level 99, A at level 200, y at level 99).
Reserved Notation "[ 'fset[' key ] E | x 'in' A , y 'in' B ]"
(at level 0, E, x at level 99, A at level 200, y at level 99).
Reserved Notation "[ 'fset[' key ] E | x : A , y : B ]"
(at level 0, E, x at level 99, A at level 200, y at level 99).
Reserved Notation "[ 'fset[' key ] E | x : A , y 'in' B ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fset[' key ] E | x 'in' A , y : B ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fset[' key ] E | x 'in' A , y 'in' B & P ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fset[' key ] E | x : A , y 'in' B & P ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fset[' key ] E | x 'in' A , y : B & P ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fset[' key ] E | x : A , y : B & P ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fset[' key ] x : T 'in' A ]"
(at level 0, x at level 99).
Reserved Notation "[ 'fset[' key ] x : T 'in' A | P ]"
(at level 0, x at level 99).
Reserved Notation "[ 'fset[' key ] x 'in' A | P ]" (at level 0, x at level 99).
Reserved Notation "[ 'fset[' key ] x 'in' A ]" (at level 0, x at level 99).
Reserved Notation "[ 'fset[' key ] x : T | P ]" (at level 0, x at level 99).
Reserved Notation "[ 'fset[' key ] x : T | P & Q ]" (at level 0, x at level 99).
Reserved Notation "[ 'fset[' key ] x : T 'in' A | P & Q ]"
(at level 0, x at level 99).
Reserved Notation "[ 'fset[' key ] x 'in' A | P & Q ]"
(at level 0, x at level 99).
(* printing only *)
Reserved Notation "[ 'f' 'set' E | x 'in' A ]"
(at level 0, E, x at level 99,
format "[ '[hv' 'f' 'set' E '/ ' | x 'in' A ] ']'").
Reserved Notation "[ 'f' 'set' E | x : A ]"
(at level 0, E, x at level 99,
format "[ '[hv' 'f' 'set' E '/ ' | x : A ] ']'").
Reserved Notation "[ 'f' 'set' x 'in' A | P ]"
(at level 0, x at level 99, format "[ 'f' 'set' x 'in' A | P ]").
Reserved Notation "[ 'f' 'set' x 'in' A ]"
(at level 0, x at level 99, format "[ 'f' 'set' x 'in' A ]").
Reserved Notation "[ 'f' 'set' x : T | P ]"
(at level 0, x at level 99, format "[ 'f' 'set' x : T | P ]").
Reserved Notation "[ 'f' 'set' x : T | P & Q ]"
(at level 0, x at level 99, format "[ 'f' 'set' x : T | P & Q ]").
Reserved Notation "[ 'f' 'set' x 'in' A | P & Q ]"
(at level 0, x at level 99, format "[ 'f' 'set' x 'in' A | P & Q ]").
(* binary printing only *)
Reserved Notation "[ 'f' 'set' E | x 'in' A , y 'in' B ]"
(at level 0, E, x at level 99, A at level 200, y at level 99,
format "[ '[hv' 'f' 'set' E '/ ' | x 'in' A , '/' y 'in' B ] ']'").
Reserved Notation "{fset K }" (at level 0, format "{fset K }").
Reserved Notation "[ 'fset' a ]"
(at level 0, a at level 99, format "[ 'fset' a ]").
Reserved Notation "[ 'fset' a : T ]"
(at level 0, a at level 99, format "[ 'fset' a : T ]").
Reserved Notation "A `&` B" (at level 48, left associativity).
Reserved Notation "A `*` B" (at level 46, left associativity).
Reserved Notation "A `+` B" (at level 54, left associativity).
Reserved Notation "A +` B" (at level 54, left associativity).
Reserved Notation "A `|` B" (at level 52, left associativity).
Reserved Notation "a |` A" (at level 52, left associativity).
Reserved Notation "A `\` B" (at level 50, left associativity).
Reserved Notation "A `\ b" (at level 50, left associativity).
Reserved Notation "A `<=` B" (at level 70, no associativity).
Reserved Notation "A `<` B" (at level 70, no associativity).
(* This is left-associative due to historical limitations of the .. Notation. *)
Reserved Notation "[ 'fset' a1 ; a2 ; .. ; an ]"
(at level 0, a1 at level 99, format "[ 'fset' a1 ; a2 ; .. ; an ]").
Reserved Notation "[ 'disjoint' A & B ]".
(* Comprehensions *)
Reserved Notation "[ 'fset' E | x 'in' A & P ]" (at level 0, E, x at level 99).
Reserved Notation "[ 'fset' E | x : A & P ]" (at level 0, E, x at level 99).
Reserved Notation "[ 'fset' E | x : A , y 'in' B ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fset' E | x 'in' A , y : B ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fset' E | x : A , y : B ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fset' E | x 'in' A , y 'in' B & P ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fset' E | x : A , y 'in' B & P ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fset' E | x 'in' A , y : B & P ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fset' E | x : A , y : B & P ]"
(at level 0, E, x, y at level 99).
Reserved Notation "[ 'fsetval' x 'in' A ]" (at level 0, x at level 99).
Reserved Notation "[ 'fsetval' x 'in' A | P ]" (at level 0, x at level 99).
Reserved Notation "[ 'fsetval' x 'in' A | P & Q ]" (at level 0, x at level 99).
Reserved Notation "[ 'fsetval' x : A ]" (at level 0, x at level 99).
Reserved Notation "[ 'fsetval' x : A | P ]" (at level 0, x at level 99).
Reserved Notation "[ 'fsetval' x : A | P & Q ]" (at level 0, x at level 99).
(* keyed parse only *)
Reserved Notation "[ 'fsetval[' key ] x 'in' A ]" (at level 0, x at level 99).
Reserved Notation "[ 'fsetval[' key ] x 'in' A | P ]"
(at level 0, x at level 99).
Reserved Notation "[ 'fsetval[' key ] x 'in' A | P & Q ]"
(at level 0, x at level 99).
Reserved Notation "[ 'fsetval[' key ] x : A ]" (at level 0, x at level 99).
Reserved Notation "[ 'fsetval[' key ] x : A | P ]" (at level 0, x at level 99).
Reserved Notation "[ 'fsetval[' key ] x : A | P & Q ]"
(at level 0, x at level 99).
(* Print-only variants to work around the Coq pretty-printer K-term kink. *)
Reserved Notation "[ 'f' 'set' E | x 'in' A & P ]"
(at level 0, E, x at level 99,
format "[ '[hv' 'f' 'set' E '/ ' | x 'in' A '/ ' & P ] ']'").
Reserved Notation "[ 'f' 'set' E | x : A & P ]"
(at level 0, E, x at level 99,
format "[ '[hv' 'f' 'set' E '/ ' | x : A '/ ' & P ] ']'").
Reserved Notation "[ 'f' 'set' E | x : A , y 'in' B ]"
(at level 0, E, x, y at level 99, format
"[ '[hv' 'f' 'set' E '/ ' | x : A , '/ ' y 'in' B ] ']'").
Reserved Notation "[ 'f' 'set' E | x 'in' A , y : B ]"
(at level 0, E, x, y at level 99, format
"[ '[hv' 'f' 'set' E '/ ' | x 'in' A , '/ ' y : B ] ']'").
Reserved Notation "[ 'f' 'set' E | x : A , y : B ]"
(at level 0, E, x, y at level 99, format
"[ '[hv' 'f' 'set' E '/ ' | x : A , '/ ' y : B ] ']'").
Reserved Notation "[ 'f' 'set' E | x 'in' A , y 'in' B & P ]"
(at level 0, E, x, y at level 99, format
"[ '[hv' 'f' 'set' E '/ ' | x 'in' A , '/ ' y 'in' B '/ ' & P ] ']'").
Reserved Notation "[ 'f' 'set' E | x : A , y 'in' B & P ]"
(at level 0, E, x, y at level 99, format
"[ '[hv' 'f' 'set' E '/ ' | x : A , '/ ' y 'in' B & P ] ']'").
Reserved Notation "[ 'f' 'set' E | x 'in' A , y : B & P ]"
(at level 0, E, x, y at level 99, format
"[ '[hv' 'f' 'set' E '/ ' | x 'in' A , '/ ' y : B & P ] ']'").
Reserved Notation "[ 'f' 'set' E | x : A , y : B & P ]"
(at level 0, E, x, y at level 99, format
"[ '[hv' 'f' 'set' E '/ ' | x : A , '/ ' y : B & P ] ']'").
Reserved Notation "[ 'f' 'setval' x 'in' A ]"
(at level 0, x at level 99, format "[ 'f' 'setval' x 'in' A ]").
Reserved Notation "[ 'f' 'setval' x 'in' A | P ]"
(at level 0, x at level 99, format "[ 'f' 'setval' x 'in' A | P ]").
Reserved Notation "[ 'f' 'setval' x 'in' A | P & Q ]"
(at level 0, x at level 99,
format "[ 'f' 'setval' x 'in' A | P & Q ]").
Reserved Notation "[ 'f' 'setval' x : A ]"
(at level 0, x at level 99, format "[ 'f' 'setval' x : A ]").
Reserved Notation "[ 'f' 'setval' x : A | P ]"
(at level 0, x at level 99, format "[ 'f' 'setval' x : A | P ]").
Reserved Notation "[ 'f' 'setval' x : A | P & Q ]"
(at level 0, x at level 99, format "[ 'f' 'setval' x : A | P & Q ]").
Reserved Notation "\bigcup_ ( i <- r | P ) F"
(at level 41, F at level 41, i, r at level 50,
format "'[' \bigcup_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\bigcup_ ( i <- r ) F"
(at level 41, F at level 41, i, r at level 50,
format "'[' \bigcup_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\bigcup_ ( i | P ) F"
(at level 41, F at level 41, i at level 50,
format "'[' \bigcup_ ( i | P ) '/ ' F ']'").
Reserved Notation "\bigcup_ ( i 'in' A | P ) F"
(at level 41, F at level 41, i, A at level 50,
format "'[' \bigcup_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\bigcup_ ( i 'in' A ) F"
(at level 41, F at level 41, i, A at level 50,
format "'[' \bigcup_ ( i 'in' A ) '/ ' F ']'").
Reserved Notation "{fmap T }" (at level 0, format "{fmap T }").
Reserved Notation "x .[ k <- v ]"
(at level 2, left associativity, format "x .[ k <- v ]").
Reserved Notation "x .[~ k ]" (at level 2, k at level 200, format "x .[~ k ]").
Reserved Notation "x .[& k ]" (at level 2, k at level 200, format "x .[& k ]").
Reserved Notation "x .[\ k ]" (at level 2, k at level 200, format "x .[\ k ]").
Reserved Notation "x .[? k ]" (at level 2, k at level 200, format "x .[? k ]").
Reserved Infix "`~`" (at level 52).
Reserved Notation "[ 'fset' k ]" (at level 0, k at level 99, format "[ 'fset' k ]").
(* Copy of the ssrbool shim to ensure compatibility with MathComp v1.8.0. *)
Definition PredType : forall T pT, (pT -> pred T) -> predType T.
exact PredType || exact mkPredType.
Defined.
Arguments PredType [T pT] toP.
Local Notation predOfType T := (pred_of_simpl (@pred_of_argType T)).
Definition oextract (T : Type) (o : option T) : o -> T :=
if o is Some t return o -> T then fun=> t else False_rect T \o notF.
Lemma oextractE (T : Type) (x : T) (xP : Some x) : oextract xP = x.
Proof. by []. Qed.
Lemma Some_oextract T (x : option T) (x_ex : x) : Some (oextract x_ex) = x.
Proof. by case: x x_ex. Qed.
Definition ojoin T (x : option (option T)) :=
if x is Some y then y else None.
Lemma Some_ojoin T (x : option (option T)) : x -> Some (ojoin x) = x.
Proof. by case : x. Qed.
Lemma ojoinT T (x : option (option T)) : ojoin x -> x.
Proof. by case: x. Qed.
Lemma TaggedP (T1 : Type) (T2 : T1 -> Type) (P : forall x, T2 x -> Type) :
(forall t : {x : T1 & T2 x}, P _ (tagged t)) ->
forall (x : T1) (y : T2 x), P x y.
Proof. by move=> /(_ (Tagged _ _)). Qed.
Arguments TaggedP {T1} T2.
Module Type SortKeysSig.
Section SortKeys.
Variable (K : choiceType).
Implicit Types (k : K) (ks : seq K).
Axiom f : seq K -> seq K.
Axiom perm : forall s, perm_eq (f s) (undup s).
Axiom uniq : forall s, uniq (f s).
Axiom E : forall (s : seq K), f s =i s.
Axiom eq : forall (s s' : seq K), s =i s' <-> f s = f s'.
End SortKeys.
End SortKeysSig.
Module SortKeys : SortKeysSig.
Section SortKeys.
Variable (K : choiceType).
Implicit Types (k : K) (ks : seq K).
Definition f (s : seq K) := choose (perm_eq (undup s)) (undup s).
Fact perm s : perm_eq (f s) (undup s).
Proof. by rewrite perm_sym chooseP. Qed.
Fact uniq s : uniq (f s).
Proof. by rewrite (perm_uniq (perm _)) undup_uniq. Qed.
Fact E (s : seq K) : f s =i s.
Proof. by move=> x; rewrite (perm_mem (perm _)) mem_undup. Qed.
Lemma eq (s s' : seq K) : s =i s' <-> f s = f s'.
Proof.
split=> [eq_ss'|eq_ss' k]; last by rewrite -E eq_ss' E.
rewrite /f; have peq_ss' : perm_eq (undup s) (undup s').
by apply: uniq_perm; rewrite ?undup_uniq // => x; rewrite !mem_undup.
rewrite (@choose_id _ _ _ (undup s')) //=; apply: eq_choose => x /=.
by apply: sym_left_transitive; [exact: perm_sym | exact: (@perm_trans)|].
Qed.
End SortKeys.
End SortKeys.
#[global] Hint Resolve SortKeys.perm SortKeys.uniq SortKeys.E : core.
Notation sort_keys := SortKeys.f.
Notation sort_keys_perm := SortKeys.perm.
Notation sort_keys_uniq := SortKeys.uniq.
Notation sort_keysE := SortKeys.E.
Notation eq_sort_keys := SortKeys.eq.
Section ChoiceKeys.
Variable (K : choiceType).
Implicit Types (k : K) (ks : seq K).
Lemma mem_sort_keys ks k : k \in ks -> k \in sort_keys ks.
Proof. by rewrite sort_keysE. Qed.
Lemma mem_sort_keys_intro ks k : k \in sort_keys ks -> k \in ks.
Proof. by rewrite sort_keysE. Qed.
Lemma sort_keys_nil : sort_keys [::] = [::] :> seq K.
Proof.
have := sort_keysE ([::] : seq K).
by case: sort_keys => //= a l /(_ a); rewrite mem_head.
Qed.
Lemma sort_keys_id ks : sort_keys (sort_keys ks) = sort_keys ks.
Proof. by have /eq_sort_keys := sort_keysE ks. Qed.
Definition canonical_keys ks := sort_keys ks == ks.
Lemma canonical_uniq ks : canonical_keys ks -> uniq ks.
Proof. by move=> /eqP <-; exact: sort_keys_uniq. Qed.
Lemma canonical_sort_keys ks : canonical_keys (sort_keys ks).
Proof. by rewrite /canonical_keys sort_keys_id. Qed.
Lemma canonical_eq_keys ks ks' :
canonical_keys ks -> canonical_keys ks' ->
ks =i ks' -> ks = ks'.
Proof.
move=> /eqP; case: _ /; move=> /eqP; case: _ / => eq_ks_ks'.
by apply/eq_sort_keys => x; rewrite -sort_keysE eq_ks_ks' sort_keysE.
Qed.
Lemma size_sort_keys ks : size (sort_keys ks) = size (undup ks).
Proof. exact: perm_size. Qed.
End ChoiceKeys.
Arguments eq_sort_keys {K s s'}.
(***************)
(* Finite sets *)
(***************)
Section Def.
Variables (K : choiceType).
Structure finSet : Type := mkFinSet {
enum_fset :> seq K;
_ : canonical_keys enum_fset
}.
Definition finset_of (_ : phant K) := finSet.
End Def.
Identity Coercion type_of_finset : finset_of >-> finSet.
Notation "{fset T }" := (@finset_of _ (Phant T)) : type_scope.
Definition pred_of_finset (K : choiceType)
(f : finSet K) : pred K := fun k => k \in (enum_fset f).
Canonical finSetPredType (K : choiceType) := PredType (@pred_of_finset K).
Section FinSetCanonicals.
Variable (K : choiceType).
HB.instance Definition _ := [isSub for (@enum_fset K)].
HB.instance Definition _ := [Choice of {fset K} by <:].
End FinSetCanonicals.
Section FinTypeSet.
Variables (K : choiceType) (A : finSet K).
Lemma keys_canonical : canonical_keys (enum_fset A).
Proof. by case: A. Qed.
Lemma fset_uniq : uniq (enum_fset A).
Proof. by rewrite canonical_uniq // keys_canonical. Qed.
Record fset_sub_type : predArgType :=
FSetSub {fsval : K; fsvalP : in_mem fsval (@mem K _ A)}.
HB.instance Definition _ := [isSub for fsval].
HB.instance Definition _ := [Choice of fset_sub_type by <:].
HB.instance Definition _ (T : countType) := [Countable of {fset T} by <:].
Definition fset_sub_enum : seq fset_sub_type :=
undup (pmap insub (enum_fset A)).
Lemma mem_fset_sub_enum x : x \in fset_sub_enum.
Proof. by rewrite mem_undup mem_pmap -valK map_f // fsvalP. Qed.
Lemma val_fset_sub_enum : map val fset_sub_enum = enum_fset A.
Proof.
rewrite /fset_sub_enum undup_id ?pmap_sub_uniq ?fset_uniq//.
rewrite (pmap_filter (@insubK _ _ _)); apply/all_filterP.
by apply/allP => x; rewrite isSome_insub.
Qed.
Definition fset_sub_pickle x := index x fset_sub_enum.
Definition fset_sub_unpickle n := nth None (map some fset_sub_enum) n.
Lemma fset_sub_pickleK : pcancel fset_sub_pickle fset_sub_unpickle.
Proof.
rewrite /fset_sub_unpickle => x.
by rewrite (nth_map x) ?nth_index ?index_mem ?mem_fset_sub_enum.
Qed.
HB.instance Definition _ :=
Countable.copy fset_sub_type (pcan_type fset_sub_pickleK).
HB.instance Definition _ := isFinite.Build fset_sub_type
(Finite.uniq_enumP (undup_uniq _) mem_fset_sub_enum).
Lemma enum_fsetE : enum_fset A = [seq val i | i <- enum fset_sub_type].
Proof. by rewrite enumT unlock val_fset_sub_enum. Qed.
Lemma cardfE : size (enum_fset A) = #|fset_sub_type|.
Proof. by rewrite cardE enum_fsetE size_map. Qed.
End FinTypeSet.
Identity Coercion finSet_sub_type : finset_of >-> finSet.
Coercion fset_sub_type : finSet >-> predArgType.
#[global] Hint Resolve fsvalP fset_uniq mem_fset_sub_enum : core.
Declare Scope fset_scope.
Delimit Scope fset_scope with fset.
Local Open Scope fset_scope.
Notation "[` kf ]" := (FSetSub kf) (format "[` kf ]") : fset_scope.
Lemma fsetsubE (T : choiceType) (A : {fset T}) (x : A) (xA : val x \in A) :
[` xA] = x.
Proof. by apply/val_inj => /=. Qed.
Notation "#|` A |" := (size (enum_fset A))
(at level 0, A at level 99, format "#|` A |") : nat_scope.
Definition fset_predT {T : choiceType} {A : {fset T}} : simpl_pred A := @predT A.
Definition set_of_fset (K : choiceType) (A : {fset K}) : {set A} :=
[set x in {: A}].
Arguments pred_of_finset : simpl never.
Section SeqFset.
Variable finset_key : unit.
Definition seq_fset : forall K : choiceType, seq K -> {fset K} :=
locked_with finset_key (fun K s => mkFinSet (@canonical_sort_keys K s)).
Variable (K : choiceType) (s : seq K).
Lemma seq_fsetE : seq_fset s =i s.
Proof. by move=> a; rewrite [seq_fset]unlock sort_keysE. Qed.
Lemma size_seq_fset : size (seq_fset s) = size (undup s).
Proof. by rewrite [seq_fset]unlock /= size_sort_keys. Qed.
Lemma seq_fset_uniq : uniq (seq_fset s).
Proof. by rewrite [seq_fset]unlock /= sort_keys_uniq. Qed.
Lemma seq_fset_perm : perm_eq (seq_fset s) (undup s).
Proof. by rewrite [seq_fset]unlock //= sort_keys_perm. Qed.
End SeqFset.
#[global] Hint Resolve keys_canonical sort_keys_uniq : core.
HB.instance Definition _ K := [isSub for (@enum_fset K)].
HB.instance Definition _ (K : choiceType) := [Choice of {fset K} by <:].
Section FinPredStruct.
Structure finpredType (T : eqType) := FinPredType {
finpred_sort :> Type;
tofinpred : finpred_sort -> pred T;
_ : {finpred_seq : finpred_sort -> seq T |
((forall p, uniq (finpred_seq p))
* forall p x, x \in finpred_seq p = tofinpred p x)%type}
}.
Canonical finpredType_predType (T : eqType) (fpT : finpredType T) :=
@PredType T (finpred_sort fpT) (@tofinpred T fpT).
Definition enum_finpred (T : eqType) (fpT : finpredType T) :
fpT -> seq T :=
let: FinPredType _ _ (exist s _) := fpT in s.
Lemma enum_finpred_uniq (T : eqType) (fpT : finpredType T) (p : fpT) :
uniq (enum_finpred p).
Proof. by case: fpT p => ?? [s sE] p; rewrite sE. Qed.
Lemma enum_finpredE (T : eqType) (fpT : finpredType T) (p : fpT) :
enum_finpred p =i p.
Proof. by case: fpT p => ?? [s sE] p x; rewrite sE -topredE. Qed.
Lemma mkFinPredType_of_subproof (T : eqType) (pT : predType T)
(fpred_seq : pT -> seq T) (pred_fsetE : forall p, fpred_seq p =i p) :
forall p x, x \in fpred_seq p = topred p x.
Proof. by move=> p x; rewrite topredE pred_fsetE. Qed.
Definition mkFinPredType_of (T : eqType) (U : Type) :=
fun (pT : predType T) & pred_sort pT -> U =>
fun a (pT' := @PredType T U a) & phant_id pT' pT =>
fun (fpred_seq : pT' -> seq T)
(fpred_seq_uniq : forall p, uniq (fpred_seq p))
(fpred_seqE : forall p, fpred_seq p =i p) =>
@FinPredType T U a (exist _ fpred_seq
(fpred_seq_uniq, (mkFinPredType_of_subproof fpred_seqE))).
Definition clone_finpredType (T : eqType) (U : Type) :=
fun (pT : finpredType T) & finpred_sort pT -> U =>
fun a pP (pT' := @FinPredType T U a pP) & phant_id pT' pT => pT'.
Structure is_finite (T : eqType) (P : pred T) := IsFinite {
seq_of_is_finite :> seq T;
_ : uniq seq_of_is_finite;
_ : forall x, x \in seq_of_is_finite = P x;
}.
Lemma is_finite_uniq (T : eqType) (P : pred T) (p : is_finite P) : uniq p.
Proof. by case: p. Qed.
Lemma is_finiteE (T : eqType) (P : pred T) (p : is_finite P) x :
x \in (seq_of_is_finite p) = P x.
Proof. by case: p. Qed.
Structure finpred (T : eqType) (pT : predType T) := FinPred {
pred_of_finpred :> pT;
_ : is_finite [pred x in pred_of_finpred]
}.
Definition enum_fin (T : eqType) (pT : predType T) (p : finpred pT) : seq T :=
let: FinPred _ fp := p in fp.
Lemma enum_fin_uniq (T : eqType) (pT : predType T) (p : finpred pT) :
uniq (enum_fin p).
Proof. by case: p => ?[]. Qed.
Lemma enum_finE (T : eqType) (pT : predType T) (p : finpred pT) :
enum_fin p =i (pred_of_finpred p).
Proof. by case: p => ?[]. Qed.
Canonical fin_finpred (T : eqType) (pT : finpredType T) (p : pT) :=
@FinPred _ _ p (@IsFinite _ _ (enum_finpred p)
(enum_finpred_uniq p) (enum_finpredE p)).
Definition finpred_of (T : eqType) (pT : predType T) (p : pT)
(fp : finpred pT) & phantom pT fp : finpred pT := fp.
Structure finmempred (T : eqType) := FinMemPred {
pred_of_finmempred :> mem_pred T;
_ : is_finite (fun x => in_mem x pred_of_finmempred)
}.
Definition enum_finmem (T : eqType) (p : finmempred T) : seq T :=
let: FinMemPred _ fp := p in fp.
Lemma enum_finmem_uniq (T : eqType) (p : finmempred T) :
uniq (enum_finmem p).
Proof. by case: p => ?[]. Qed.
Lemma enum_finmemE (T : eqType) (p : finmempred T) :
enum_finmem p =i p.
Proof. by case: p => ?[]. Qed.
Definition finmempred_of (T : eqType) (P : pred T)
(mP : finmempred T) & phantom (mem_pred T) mP : finmempred T := mP.
Canonical mem_fin (T : eqType) (pT : predType T) (p : finpred pT) :=
@FinMemPred _ (mem p)
(@IsFinite _ _ (enum_fin p) (enum_fin_uniq p) (enum_finE p)).
End FinPredStruct.
Notation "[ 'finpredType' 'of' T ]" := (@clone_finpredType _ T _ id _ _ id)
(at level 0, format "[ 'finpredType' 'of' T ]") : form_scope.
Notation mkFinPredType T s s_uniq sE :=
(@mkFinPredType_of _ T _ id _ id s s_uniq sE).
Section CanonicalFinPred.
Canonical fset_finpredType (T: choiceType) :=
mkFinPredType (finSet T) (@enum_fset T) (@fset_uniq T) (fun _ _ => erefl).
Canonical pred_finpredType (T : finType) :=
mkFinPredType (pred T) (fun P => enum_mem (mem P)) (@enum_uniq T) (@mem_enum T).
Canonical simpl_pred_finpredType (T : finType) :=
mkFinPredType (simpl_pred T) (fun P => enum_mem (mem P)) (@enum_uniq T) (@mem_enum T).
Canonical fset_finpred (T: choiceType) (A : finSet T) :=
@FinPred _ _ (enum_fset A)
(@IsFinite _ _ (enum_fset A) (fset_uniq _) (fun=> erefl)).
Program Canonical subfinset_finpred (T : choiceType)
(A : finmempred T) (Q : pred T) :=
@FinPred _ _ [pred x in A | Q x]
(@IsFinite _ _ [seq x <- enum_finmem A | Q x] _ _).
Next Obligation. by rewrite filter_uniq// enum_finmem_uniq. Qed.
Next Obligation. by rewrite !inE !mem_filter andbC enum_finmemE. Qed.
Canonical seq_finpredType (T : eqType) :=
mkFinPredType (seq T) undup (@undup_uniq T) (@mem_undup T).
End CanonicalFinPred.
Local Notation imfset_def key :=
(fun (T K : choiceType) (f : T -> K) (p : finmempred T)
of phantom (mem_pred T) p => seq_fset key [seq f x | x <- enum_finmem p]).
Local Notation imfset2_def key :=
(fun (K T1 : choiceType) (T2 : T1 -> choiceType)
(f : forall x : T1, T2 x -> K)
(p1 : finmempred T1) (p2 : forall x : T1, finmempred (T2 x))
of phantom (mem_pred T1) p1 & phantom (forall x, mem_pred (T2 x)) p2 =>
seq_fset key [seq f x y | x <- enum_finmem p1, y <- enum_finmem (p2 x)]).
Module Type ImfsetSig.
Parameter imfset : forall (key : unit) (T K : choiceType)
(f : T -> K) (p : finmempred T),
phantom (mem_pred T) p -> {fset K}.
Parameter imfset2 : forall (key : unit) (K T1 : choiceType)
(T2 : T1 -> choiceType)(f : forall x : T1, T2 x -> K)
(p1 : finmempred T1) (p2 : forall x : T1, finmempred (T2 x)),
phantom (mem_pred T1) p1 -> phantom (forall x, mem_pred (T2 x)) p2 -> {fset K}.
Axiom imfsetE : forall key, imfset key = imfset_def key.
Axiom imfset2E : forall key, imfset2 key = imfset2_def key.
End ImfsetSig.
Module Imfset : ImfsetSig.
Definition imfset key := imfset_def key.
Definition imfset2 key := imfset2_def key.
Lemma imfsetE key : imfset key = imfset_def key. Proof. by []. Qed.
Lemma imfset2E key : imfset2 key = imfset2_def key. Proof. by []. Qed.
End Imfset.
Notation imfset key f p :=
(Imfset.imfset key f (Phantom _ (pred_of_finmempred p))).
Notation imfset2 key f p q :=
(Imfset.imfset2 key f (Phantom _ (pred_of_finmempred p))
(Phantom _ (fun x => (pred_of_finmempred (q x))))).
Canonical imfset_unlock k := Unlockable (Imfset.imfsetE k).
Canonical imfset2_unlock k := Unlockable (Imfset.imfset2E k).
Notation "A `=` B" := (A = B :> {fset _}) (only parsing) : fset_scope.
Notation "A `<>` B" := (A <> B :> {fset _}) (only parsing) : fset_scope.
Notation "A `==` B" := (A == B :> {fset _}) (only parsing) : fset_scope.
Notation "A `!=` B" := (A != B :> {fset _}) (only parsing) : fset_scope.
Notation "A `=P` B" := (A =P B :> {fset _}) (only parsing) : fset_scope.
Notation "f @`[ key ] A" :=
(Imfset.imfset key f (Phantom _ (mem A))) : fset_scope.
Notation "f @2`[ key ] ( A , B )" :=
(Imfset.imfset2 key f (Phantom _ (mem A)) (Phantom _ (fun x => (mem (B x)))))
: fset_scope.
Fact imfset_key : unit. Proof. exact: tt. Qed.
Notation "f @` A" := (f @`[imfset_key] A) : fset_scope.
Notation "f @2` ( A , B )" := (f @2`[imfset_key] (A, B)) : fset_scope.
(* unary *)
Notation "[ 'fset' E | x : T 'in' A ]" :=
((fun x : T => E) @` A) (only parsing) : fset_scope.
Notation "[ 'fset' E | x 'in' A ]" :=
[fset E | x : _ in A] (only parsing) : fset_scope.
Notation "[ 'fset' E | x : A ]" :=
[fset E | x : _ in {: A} ] (only parsing) : fset_scope.
Notation "[ 'fset' x : T 'in' A ]" :=
[fset (x : T) | x in A] (only parsing) : fset_scope.
Notation "[ 'fset' x : T 'in' A | P ]" :=
[fset (x : T) | x in [pred x in A | P]] (only parsing) : fset_scope.
Notation "[ 'fset' x 'in' A | P ]" :=
[fset x : _ in A | P] (only parsing) : fset_scope.
Notation "[ 'fset' x 'in' A ]" :=
[fset x : _ in A ] (only parsing) : fset_scope.
Notation "[ 'fset' x : T | P ]" :=
[fset x in {: T} | P] (only parsing) : fset_scope.
Notation "[ 'fset' x : T | P & Q ]" :=
[fset x : T | P && Q] (only parsing) : fset_scope.
Notation "[ 'fset' x : T 'in' A | P & Q ]" :=
[fset x : T in A | P && Q] (only parsing) : fset_scope.
Notation "[ 'fset' x 'in' A | P & Q ]" :=
[fset x in A | P && Q] (only parsing) : fset_scope.
(* binary *)
Notation "[ 'fset' E | x : T 'in' A , y : T' 'in' B ]" :=
((fun (x : T) (y : T') => E) @2` (A, fun x => B)) (only parsing) : fset_scope.
Notation "[ 'fset' E | x 'in' A , y 'in' B ]" :=
[fset E | x : _ in A, y : _ in B] (only parsing) : fset_scope.
(* keyed parse only *)
Notation "[ 'fset[' key ] E | x : T 'in' A ]" :=
((fun x : T => E) @`[key] A) (only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x 'in' A ]" :=
[fset[key] E | x : _ in A] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : A ]" :=
[fset[key] E | x : _ in {: A} ] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] x : T 'in' A ]" :=
[fset[key] (x : T) | x in A] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] x : T 'in' A | P ]" :=
[fset[key] (x : T) | x in [pred x in A | P]] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] x 'in' A | P ]" :=
[fset[key] x : _ in A | P] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] x 'in' A ]" :=
[fset[key] x : _ in A ] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] x : T | P ]" :=
[fset[key] x in {: T} | P] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] x : T | P & Q ]" :=
[fset[key] x : T | P && Q] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] x : T 'in' A | P & Q ]" :=
[fset[key] x : T in A | P && Q] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] x 'in' A | P & Q ]" :=
[fset[key] x in A | P && Q] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : T 'in' A , y : T' 'in' B ]" :=
((fun (x : T) (y : T') => E) @2` (A, fun x => B))
(only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x 'in' A , y 'in' B ]" :=
[fset[key] E | x : _ in A, y : _ in B] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : A , y : B ]" :=
[fset[key] E | x : _ in {: A}, y : _ in {: B}] (only parsing) : fset_scope.
(* printing only *)
Notation "[ 'f' 'set' E | x 'in' A ]" := [fset[_] E | x in A] : fset_scope.
Notation "[ 'f' 'set' E | x : A ]" := [fset[_] E | x : A] : fset_scope.
Notation "[ 'f' 'set' x 'in' A | P ]" := [fset[_] x in A | P] : fset_scope.
Notation "[ 'f' 'set' x 'in' A ]" := [fset[_] x in A] : fset_scope.
Notation "[ 'f' 'set' x : T | P ]" := [fset[_] x : T | P] : fset_scope.
Notation "[ 'f' 'set' x : T | P & Q ]" := [fset[_] x : T | P & Q] : fset_scope.
Notation "[ 'f' 'set' x 'in' A | P & Q ]" :=
[fset[_] x in A | P & Q] : fset_scope.
(* binary printing only*)
Notation "[ 'f' 'set' E | x 'in' A , y 'in' B ]" :=
[fset[_] E | x in A, y in B] : fset_scope.
Section Ops.
Context {K K': choiceType}.
Implicit Types (a b c : K) (A B C D : {fset K}) (E : {fset K'}) (s : seq K).
Definition fset0 : {fset K} :=
@mkFinSet K [::] (introT eqP (@sort_keys_nil K)).
(* very transparent, to ensure x \in fset0 is convertible to false *)
Fact fset1_key : unit. Proof. exact: tt. Qed.
Definition fset1 a : {fset K} := [fset[fset1_key] x in [:: a]].
Fact fsetU_key : unit. Proof. exact: tt. Qed.
Definition fsetU A B := [fset[fsetU_key] x in enum_fset A ++ enum_fset B].
Fact fsetI_key : unit. Proof. exact: tt. Qed.
Definition fsetI A B := [fset[fsetI_key] x in A | x \in B].
Fact fsetD_key : unit. Proof. exact: tt. Qed.
Definition fsetD A B := [fset[fsetD_key] x in A | x \notin B].
Fact fsetM_key : unit. Proof. exact: tt. Qed.
Definition fsetM A E := [fset[fsetM_key] (x, y) | x : K in A, y : K' in E].
Definition fsubset A B := fsetI A B == A.
Definition fproper A B := fsubset A B && ~~ fsubset B A.
Definition fdisjoint A B := (fsetI A B == fset0).
End Ops.
Notation "[ 'fset' a ]" := (fset1 a) : fset_scope.
Notation "[ 'fset' a : T ]" := [fset (a : T)] : fset_scope.
Notation "A `|` B" := (fsetU A B) : fset_scope.
Notation "a |` A" := ([fset a] `|` A) : fset_scope.
(* This is left-associative due to historical limitations of the .. Notation. *)
Notation "[ 'fset' a1 ; a2 ; .. ; an ]" :=
(fsetU .. (a1 |` [fset a2]) .. [fset an]) : fset_scope.
Notation "A `&` B" := (fsetI A B) : fset_scope.
Notation "A `*` B" := (fsetM A B) : fset_scope.
Notation "A `\` B" := (fsetD A B) : fset_scope.
Notation "A `\ a" := (A `\` [fset a]) : fset_scope.
Notation "A `<=` B" := (fsubset A B) : fset_scope.
Notation "A `<` B" := (fproper A B) : fset_scope.
Notation "[ 'disjoint' A & B ]" := (fdisjoint A B) : fset_scope.
(* Comprehensions *)
Notation "[ 'fset' E | x 'in' A & P ]" :=
[fset E | x in [pred x in A | P]] (only parsing) : fset_scope.
Notation "[ 'fset' E | x : A & P ]" :=
[fset E | x in {: A} & P] (only parsing) : fset_scope.
Notation "[ 'fset' E | x : A , y 'in' B ]" :=
[fset E | x in {: A}, y in B] (only parsing) : fset_scope.
Notation "[ 'fset' E | x 'in' A , y : B ]" :=
[fset E | x in A, y in {: B}] (only parsing) : fset_scope.
Notation "[ 'fset' E | x : A , y : B ]" :=
[fset E | x in {: A}, y in {: B}] (only parsing) : fset_scope.
Notation "[ 'fset' E | x 'in' A , y 'in' B & P ]" :=
[fset E | x in A, y in [pred y in B | P]] (only parsing) : fset_scope.
Notation "[ 'fset' E | x : A , y 'in' B & P ]" :=
[fset E | x in {: A}, y in B & P] (only parsing) : fset_scope.
Notation "[ 'fset' E | x 'in' A , y : B & P ]" :=
[fset E | x in A, y in {: B} & P] (only parsing) : fset_scope.
Notation "[ 'fset' E | x : A , y : B & P ]" :=
[fset E | x in {: A}, y in {: B} & P] (only parsing) : fset_scope.
Notation "[ 'fsetval' x 'in' A ]" :=
[fset val x | x in A] (only parsing) : fset_scope.
Notation "[ 'fsetval' x 'in' A | P ]" :=
[fset val x | x in A & P] (only parsing) : fset_scope.
Notation "[ 'fsetval' x 'in' A | P & Q ]" :=
[fsetval x in A | (P && Q)] (only parsing) : fset_scope.
Notation "[ 'fsetval' x : A ]" :=
[fset val x | x in {: A}] (only parsing) : fset_scope.
Notation "[ 'fsetval' x : A | P ]" :=
[fset val x | x in {: A} & P] (only parsing) : fset_scope.
Notation "[ 'fsetval' x : A | P & Q ]" :=
[fsetval x in {: A} | (P && Q)] (only parsing) : fset_scope.
(* keyed parse only *)
Notation "[ 'fset[' key ] E | x 'in' A & P ]" :=
[fset[key] E | x in [pred x in A | P]] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : A & P ]" :=
[fset[key] E | x in {: A} & P] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : A , y 'in' B ]" :=
[fset[key] E | x in {: A}, y in B] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x 'in' A , y : B ]" :=
[fset[key] E | x in A, y in {: B}] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : A , y : B ]" :=
[fset[key] E | x in {: A}, y in {: B}] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x 'in' A , y 'in' B & P ]" :=
[fset[key] E | x in A, y in [pred y in B | P]] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : A , y 'in' B & P ]" :=
[fset[key] E | x in {: A}, y in B & P] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x 'in' A , y : B & P ]" :=
[fset[key] E | x in A, y in {: B} & P] (only parsing) : fset_scope.
Notation "[ 'fset[' key ] E | x : A , y : B & P ]" :=
[fset[key] E | x in {: A}, y in {: B} & P] (only parsing) : fset_scope.
Notation "[ 'fsetval[' key ] x 'in' A ]" :=
[fset[key] val x | x in A] (only parsing) : fset_scope.
Notation "[ 'fsetval[' key ] x 'in' A | P ]" :=
[fset[key] val x | x in A & P] (only parsing) : fset_scope.
Notation "[ 'fsetval[' key ] x 'in' A | P & Q ]" :=
[fsetval[key] x in A | (P && Q)] (only parsing) : fset_scope.
Notation "[ 'fsetval[' key ] x : A ]" :=
[fset[key] val x | x in {: A}] (only parsing) : fset_scope.
Notation "[ 'fsetval[' key ] x : A | P ]" :=
[fset[key] val x | x in {: A} & P] (only parsing) : fset_scope.
Notation "[ 'fsetval[' key ] x : A | P & Q ]" :=
[fsetval[key] x in {: A} | (P && Q)] (only parsing) : fset_scope.
(* Print-only variants to work around the Coq pretty-printer K-term kink. *)
Notation "[ 'f' 'set' E | x 'in' A & P ]" :=
[fset[_] E | x in A & P] : fset_scope.
Notation "[ 'f' 'set' E | x : A & P ]" := [fset[_] E | x : A & P] : fset_scope.
Notation "[ 'f' 'set' E | x : A , y 'in' B ]" :=