-
Notifications
You must be signed in to change notification settings - Fork 2
/
auxresults.v
2335 lines (2036 loc) · 81 KB
/
auxresults.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 Microsoft Corporation and Inria. All rights reserved. *)
From mathcomp Require Import ssreflect ssrfun ssrbool eqtype ssrnat seq choice.
From mathcomp Require Import order fintype generic_quotient path ssrint.
From mathcomp Require Import div tuple bigop ssralg ssrnum matrix poly polydiv.
From mathcomp Require Import interval finmap mpoly polyorder polyrcf normedtype.
From mathcomp Require Import complex classical_sets topology qe_rcf_th.
Import numFieldTopology.Exports.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import GRing.Theory Order.TotalTheory Order.POrderTheory Num.Theory.
Ltac mp :=
match goal with
| |- (?x -> _) -> _ => have /[swap]/[apply]: x
end.
Section MoreLogic.
Fact aux_equivb (P : Prop) (b c : bool) : reflect P b -> b = c -> reflect P c.
Proof. by move => reflect_P_b b_eq_c ; rewrite b_eq_c in reflect_P_b. Qed.
Variables (A B C : Prop).
Lemma if_iff_compat_l : B <-> C -> (A -> B) <-> (A -> C).
Proof. by move=> h; split => h1 h2; apply/h/h1. Qed.
Lemma if_iff_compat_r : B <-> C -> (B -> A) <-> (C -> A).
Proof. by move=> h; split => h1 h2; apply/h1/h. Qed.
Lemma bool_eq_arrow {b b' : bool} : b = b' -> b -> b'.
Proof. by case: b' => // /negP. Qed.
Lemma forallb_all [n : nat] (a : pred 'I_n) :
[forall i, a i] = all a (enum 'I_n).
Proof.
apply/forallP/allP => /= aT i //.
by apply/aT; rewrite mem_enum.
Qed.
Lemma forall_ord0 (P : pred 'I_0) : [forall i, P i] = true.
Proof. by apply/forallP; case. Qed.
Lemma forall_ord1 (p : pred 'I_1) :
[forall i : 'I_1, p i] = p ord0.
Proof. by rewrite forallb_all enum_ordSl enum_ord0/= andbT. Qed.
(* Alternative proof:
apply/forallP/idP => [/(_ ord0) //|p0].
by case; case=> // ilt; move: p0; congr p; apply/val_inj.
*)
Lemma forall_ord2 (P : 'I_2 -> bool) :
[forall i, P i] = (P ord0 && P ord_max).
Proof.
rewrite forallb_all !enum_ordSl enum_ord0/= andbT.
by congr (_ && P _); apply/val_inj.
Qed.
(* Alternative proof:
apply/forallP/andP => [p //|[] p0 p1 /=].
case; case=> [ilt|[ilt|//]].
by move: p0; congr P; apply/val_inj.
by move: p1; congr P; apply/val_inj.
*)
Lemma eq_mem_sym (T : Type) (p1 p2 :mem_pred T) : p1 =i p2 -> p2 =i p1.
Proof. by move=> h x; rewrite h. Qed.
End MoreLogic.
Section MoreNatTheory.
Lemma lt_predn n : (n.-1 < n) = (n != 0).
Proof. by case: n => [//|n]; rewrite ltnSn. Qed.
Lemma ltn_neq (n m : nat) : (n < m)%N -> n != m.
Proof. by rewrite ltn_neqAle => /andP[]. Qed.
Fact n_eq1 n : n != 0 -> n < 2 -> n = 1.
Proof. by case: n => [?|[?|[]]]. Qed.
Fact leq_npred m n : m > 0 -> (m <= n.-1) = (m < n).
Proof. by move: m n => [|m] [|n]. Qed.
Lemma leq_predn n m : (n <= m)%N -> (n.-1 <= m.-1)%N.
Proof.
case: n => [//|n]; case: m => [//|m].
by rewrite !succnK ltnS.
Qed.
Fact predn_sub m n : (m - n).-1 = (m.-1 - n).
Proof. by case: m => //= m; rewrite subSKn. Qed.
Lemma geq_subn m n : m <= n -> m - n = 0.
Proof. by rewrite -subn_eq0 => /eqP. Qed.
Lemma ltn_subLR m n p : 0 < p -> (m - n < p) = (m < n + p).
Proof. by case: p => // p _; rewrite addnS !ltnS leq_subLR. Qed.
Lemma leq_subRL m n p : 0 < n -> (n <= p - m) = (m + n <= p).
Proof. by case: n => // n _; rewrite addnS ltn_subRL. Qed.
Fact ltpredn a b c : a != 0 -> ((a + b).-1 < c + b) = (a.-1 < c).
Proof. by rewrite -lt0n => a_gt0; rewrite !prednK ?ltn_addr // leq_add2r. Qed.
Lemma leq_leq_subRL m n p : m <= p -> (n <= p - m) = (m + n <= p).
Proof. by move=> ?; case: n => [|n]; rewrite ?leq0n ?addn0 ?leq_subRL. Qed.
Lemma leq_ltn_subLR m n p : n <= m -> (m - n < p) = (m < n + p).
Proof.
move=> le_nm; case: p => [|p]; last by rewrite ltn_subLR.
by rewrite addn0 ltn0 ltnNge le_nm.
Qed.
Lemma ltnpredn m n : (m < n.-1) = (m.+1 < n).
Proof. by case: n => [//|n]; rewrite succnK. Qed.
Lemma ltn_subCl m n p : 0 < p -> 0 < n -> (m - n < p) = (m - p < n).
Proof. by move=> ??; rewrite !ltn_subLR // addnC. Qed.
Lemma leq_ltn_subCl m n p : n <= m -> p <= m -> (m - n < p) = (m - p < n).
Proof. by move=> ??; rewrite !leq_ltn_subLR // addnC. Qed.
Lemma ltn_subCr m n p : (p < m - n) = (n < m - p).
Proof. by rewrite !ltn_subRL // addnC. Qed.
Lemma leq_subCr m n p : 0 < p -> 0 < n -> (p <= m - n) = (n <= m - p).
Proof. by move=> ??; rewrite !leq_subRL // addnC. Qed.
Lemma leq_leq_subCr m n p : n <= m -> p <= m -> (p <= m - n) = (n <= m - p).
Proof. by move=> ??; rewrite !leq_leq_subRL // addnC. Qed.
Lemma leq_subCl m n p : (m - n <= p) = (m - p <= n).
Proof. by rewrite !leq_subLR // addnC. Qed.
Lemma cross_leq_add m n p q :
(m <= n)%N -> (n + p <= m + q)%N -> (p <= q)%N.
Proof.
move=> leq_mn; rewrite addnC -leq_subLR => h.
by rewrite (leq_trans _ h) // -addnBA // leq_addr.
Qed.
Lemma lift_inord (n : nat) (i : 'I_n) :
lift ord0 i = inord i.+1.
Proof. by apply/val_inj; rewrite /= inordK ?ltnS. Qed.
Lemma subn_pred n m : (0 < m)%N -> (m <= n)%N -> (n - m.-1)%N = (n - m).+1.
Proof.
case: m => [//|m _]; case: n => [//|n].
by rewrite ltnS succnK subSS => /subSn.
Qed.
End MoreNatTheory.
Section MoreSeq.
Section GeneralBaseType.
Variable (T : Type).
Lemma nseqS (n : nat) (x : T) : nseq n.+1 x = rcons (nseq n x) x.
Proof. by elim: n => //= n <-. Qed.
Definition nrcons (n : nat) (x : T) := iter n (fun s => rcons s x).
Lemma nseq_cat (n : nat) (x : T) (s : seq T) : s ++ nseq n x = nrcons n x s.
Proof.
elim: n => [|n ih]; first by rewrite cats0.
by rewrite nseqS -rcons_cat ih.
Qed.
Lemma addn_nseq (m n : nat) (x : T) : (nseq m x) ++ (nseq n x) = nseq (m + n) x.
Proof. by elim: m => // m ih; rewrite /= ih. Qed.
Lemma nrcons_cons (n : nat) (x : T) (s : seq T) (z : T) :
nrcons n z (x :: s) = x :: nrcons n z s.
Proof.
move: x s z; elim: n => // n ih x s z /=.
by rewrite ih rcons_cons.
Qed.
Lemma rcons_nrcons (n : nat) (x : T) (s : seq T) :
rcons (nrcons n x s) x = nrcons n.+1 x s.
Proof. by []. Qed.
Fact head_rev (s : seq T) (x : T) : head x (rev s) = last x s.
Proof. by case/lastP: s => [//= |t y]; rewrite rev_rcons last_rcons //=. Qed.
Fact last_rev (s : seq T) (x : T) : last x (rev s) = head x s.
Proof. case: s => [//= |t y /=]; rewrite rev_cons last_rcons //=. Qed.
Lemma rev_nseq (n : nat) (x : T) : rev (nseq n x) = nseq n x.
Proof. by elim: n => // n; rewrite {1}nseqS rev_rcons => ->. Qed.
Lemma rev_ncons (n : nat) (x : T) (s : seq T) :
rev (ncons n x s) = rev s ++ nseq n x.
Proof. by rewrite -cat_nseq rev_cat rev_nseq. Qed.
Lemma set_nth_rcons (d : T) (i : nat) (e : seq T) (x y : T) :
(i < size e)%N -> set_nth d (rcons e y) i x = rcons (set_nth d e i x) y.
Proof.
move: i x y; elim: e => //.
move=> a e ihe i; elim: i => //.
move=> i ihi x y /=.
by rewrite ltnS => lt_ie; rewrite ihe.
Qed.
Lemma rcons_set_nth (x y : T) (s : seq T) :
(set_nth y s (size s) x) = rcons s x.
Proof. by elim: s => //= a s <-. Qed.
Fact set_nthS (e : seq T) (i : nat) (x y : T) :
(size e <= i)%N -> set_nth x e i y = set_nth x (rcons e x) i y.
Proof.
move: {2}(i - size e)%N (erefl (i - size e))%N x y => n.
move: e i; elim: n.
move=> e i.
move/eqP; rewrite subn_eq0 => leq_ie x y leq_ei.
have -> : i = size e by apply/eqP; rewrite eqn_leq; apply/andP.
move=> {leq_ie} {leq_ei} {i}; move: x y.
elim: e => // a e ihe x y /=.
by rewrite ihe.
move=> n ihn e.
elim: e.
move=> i /=. rewrite subn0 => -> x y _ //=.
by rewrite set_nth_nil.
move=> a e ihe i h x y ltaei.
move: h ltaei.
case: i => //= i.
rewrite subSS => h.
rewrite ltnS=> ltaei.
congr cons.
by rewrite ihe.
Qed.
(* to be replaced by set_nth_over *)
Lemma set_nth_nrcons (e : seq T) (i : nat) (x y : T) :
(size e <= i)%N -> (set_nth x e i y) = rcons (nrcons (i - (size e)) x e) y.
Proof.
move: {2}(i - size e)%N (erefl (i - size e))%N x y => n.
move: e i; elim: n => [e i|n ihn e].
move/eqP; rewrite subn_eq0 => h x y leq_ei.
have -> : i = size e by apply/eqP; rewrite eqn_leq; apply/andP.
rewrite subnn /=.
by move=> {h} {leq_ei}; elim: e => //= a e ->.
elim: e => [i|a e ihe i h x y ltaei].
rewrite subn0 => -> x y _.
by rewrite set_nth_nil -cat_nseq cats1 -nseq_cat cat0s.
move: h ltaei; case: i => //= i.
rewrite subSS => h; rewrite ltnS => ltaei.
by rewrite ihe // -rcons_cons nrcons_cons.
Qed.
Lemma set_nth_over (e : seq T) (i : nat) (x y : T) :
(size e <= i)%N -> (set_nth x e i y) =
rcons (e ++ (nseq (i - (size e))%N x)) y.
Proof.
by move=> h; rewrite set_nth_nrcons //; congr rcons; rewrite nseq_cat.
Qed.
Lemma set_nth_nseq (i j : nat) (x y z : T) : (i <= j)%N ->
set_nth x (nseq j y) i z = (rcons (nseq i y) z) ++ (nseq (j - i).-1 y).
Proof.
move: i x y z; elim: j => [|j ih] i x y z; first by rewrite leqn0 => /eqP ->.
case: i => [_|i leq_ij] //=.
by rewrite ih.
Qed.
(* Fact fv_nquantify (m n i : nat) (f : formula F) : *)
(* (m <= i < m+n)%N -> i \notin formula_fv (nquantify m n Exists f). *)
(* Proof. *)
(* rewrite formula_vf_nquantify. *)
(* by rewrite formula_vf_mnquantify -mnfsetE in_fsetD negb_and negbK => ->. *)
(* Qed. *)
Lemma set_nth_catr (i : nat) (e1 e2 : seq T) (x y : T) :
(size e1 <= i)%N ->
set_nth x (e1 ++ e2) i y = e1 ++ (set_nth x e2 (i - (size e1)) y).
Proof.
move: i e2 y; elim/last_ind: e1 => [i e2 y _ |e1 b ih i e2 y].
by rewrite subn0.
rewrite size_rcons=> h; rewrite cat_rcons.
rewrite ih; last by rewrite ltnW.
by rewrite cat_rcons -[(i - size e1)%N]prednK ?subn_gt0 // subnS.
Qed.
Lemma set_nth_catl (i : nat) (e1 e2 : seq T) (x y : T) :
(i < size e1)%N -> set_nth x (e1 ++ e2) i y = set_nth x e1 i y ++ e2.
Proof.
move: i e1 y; elim/last_ind : e2 => [i e1| e2 z ih i e1] y h; rewrite ?cats0 //.
rewrite -rcons_cat set_nth_rcons ?size_cat ?(leq_trans h) // ?leq_addr //.
by rewrite ih // rcons_cat //.
Qed.
Lemma set_nth_cat (i : nat) (e1 e2 : seq T) (x y : T) :
set_nth x (e1 ++ e2) i y = if (i < size e1)%N then set_nth x e1 i y ++ e2
else e1 ++ (set_nth x e2 (i - (size e1)) y).
Proof.
have [leq_e1i|lt_ie1] := leqP (size e1) i; first by rewrite set_nth_catr.
by rewrite set_nth_catl.
Qed.
Lemma rcons_is_cat (e : seq T) (x : T) : rcons e x = e ++ [::x].
Proof. by rewrite -cat_rcons cats0. Qed.
Lemma take_rcons (i : nat) (e : seq T) (x : T) :
take i (rcons e x) = if (i <= size e)%N then take i e
else rcons (take i e) x.
Proof.
have [leq_ie|lt_ei] := leqP i (size e); last first.
by rewrite take_oversize ?size_rcons // take_oversize // ltnW.
rewrite rcons_is_cat take_cat.
rewrite leq_eqVlt in leq_ie.
move/orP : leq_ie => [/eqP eq_ie | ->] => //.
by rewrite eq_ie ltnn subnn take_size cats0.
Qed.
Lemma set_nth_takeC (i : nat) (e : seq T) (j : nat) (x y : T) :
(j < i)%N -> set_nth y (take i e) j x = take i (set_nth y e j x).
Proof.
move=> lt_ji.
have [leq_ei|lt_ie] := leqP (size e) i.
by rewrite ?take_oversize // ;
last by rewrite size_set_nth geq_max; apply/andP; split.
move: i j lt_ji lt_ie; elim: e => // a e ihe i.
elim: i => // i ihi j; elim: j => // j ihj.
rewrite ltnS => lt_ji.
by rewrite /= ltnS => lt_ie; rewrite ihe.
Qed.
Lemma set_nth_take (i : nat) (e : seq T) (j : nat) (x y : T) : (i <= j)%N ->
set_nth x (take i e) j y
= rcons ((take i (set_nth x e j y)) ++ (nseq (j - i) x)) y.
Proof.
move: i j; elim: e => // [i j leq_ij | a e ihe i].
- rewrite //= !set_nth_nil -cat_nseq take_cat size_nseq.
rewrite leq_eqVlt in leq_ij.
move/orP : leq_ij => [/eqP eq_ij|lt_ij].
by rewrite -eq_ij ltnn subnn /= !cats0 -rcons_is_cat.
rewrite lt_ij -rcons_is_cat -{2}[j](@subnKC i); last by rewrite ltnW.
rewrite -addn_nseq take_size_cat ?size_nseq // addn_nseq.
by rewrite subnKC; last by rewrite ltnW.
- elim: i => [j _| i ihi j].
by rewrite subn0 !take0 /= set_nth_nil rcons_is_cat cat_nseq.
elim: j => // j ihj.
by rewrite ltnS => lt_iSj /=; rewrite ihe.
Qed.
Lemma eq_iotar (a c b d : nat) : iota a b =i iota c d -> b = d.
Proof.
move=> eq_ab_cd; rewrite -(size_iota a b) -(size_iota c d).
by apply/eqP; rewrite -uniq_size_uniq ?iota_uniq.
Qed.
Lemma eq_mem_nil (U : eqType) (s : seq U) : reflect (s =i [::]) (s == [::]).
Proof.
apply: (iffP idP); first by move/eqP ->.
move=> h; apply/eqP/nilP; rewrite /nilp -all_pred0.
by apply/allP => /= x; rewrite h.
Qed.
Lemma eq_iotal (b d a c : nat) : b != O -> iota a b =i iota c d -> a = c.
Proof.
case: b => // b _; case: d => [/eq_mem_nil//|d eq_ab_cd].
wlog suff hwlog : b d a c eq_ab_cd / (a <= c)%N.
by apply/eqP; rewrite eqn_leq (hwlog b d) ?(hwlog d b).
have := eq_ab_cd c; rewrite !in_cons eqxx /= mem_iota.
by case: ltngtP => [| /ltnW leq_ac|->].
Qed.
Arguments eq_iotal {_} _ {_ _} _ _.
Lemma iotanS (n m : nat) :
iota n m.+1 = rcons (iota n m) (n + m)%N.
Proof.
elim: m n => /= [|m IHm] n; first by rewrite addn0.
by rewrite IHm addSn addnS.
Qed.
Lemma nth_enum_ord (n : nat) (i j : 'I_n) : nth i (enum 'I_n) j = j.
Proof. by apply/val_inj => /=; rewrite nth_enum_ord. Qed.
Lemma enum_ordD (n m : nat) :
enum 'I_(n+m) =
map (@lshift n m) (enum 'I_n) ++ map (@rshift n m) (enum 'I_m).
Proof.
elim: n => [|n IHn].
rewrite enum_ord0/=.
elim: m => [|m IHm]; first by rewrite enum_ord0.
rewrite enum_ordSl IHm/=; congr (_ :: _); first exact/val_inj.
rewrite -[LHS]map_id.
by apply/eq_map => i; apply/val_inj.
rewrite !enum_ordSl IHn/=; congr (_ :: _); first exact/val_inj.
by rewrite map_cat -!map_comp; congr (_ ++ _); apply/eq_map => i; apply/val_inj.
Qed.
Lemma iotaE0 (i n : nat) : iota i n = [seq i+j | j <- iota 0 n].
Proof. by elim: n => // n IHn; rewrite -addn1 !iotaD/= map_cat IHn/= add0n. Qed.
Lemma map_ord_iota (f : nat -> T) (n : nat) :
[seq f i | i : 'I_n] = [seq f i | i <- iota 0 n].
Proof.
by rewrite [LHS](eq_map (g:=f \o (val : 'I_n -> nat)))// map_comp val_enum_ord.
Qed.
Lemma nth_map_ord (x : T) n (f : 'I_n -> T) (i : 'I_n) :
nth x [seq f i | i <- enum 'I_n] i = f i.
Proof. by rewrite (nth_map i) ?nth_enum_ord// size_enum_ord. Qed.
Lemma index_iota n m i :
index i (iota n m) = if (i < n)%N then m else minn (i - n)%N m.
Proof.
elim: m i n => /= [|m IHm] i n; first by rewrite minn0 if_same.
have [->|/negPf ni] := eqVneq n i; first by rewrite ltnn subnn min0n.
rewrite IHm ltnS leq_eqVlt eq_sym ni/=.
case: (ltnP i n) => [//|] ni'.
by rewrite -minnSS subnS prednK// subn_gt0 ltn_neqAle ni.
Qed.
Lemma nth_catr (x0 : T) (s1 s2 : seq T) (p n : nat) :
p = size s1 ->
nth x0 (s1 ++ s2) (p + n) = nth x0 s2 n.
Proof.
move=> ->.
by rewrite nth_cat -[X in (_ < X)%N]addn0 ltn_add2l ltn0 subDnCA// subnn addn0.
Qed.
(* Why does size_take not use minn? *)
Lemma size_take (n0 : nat) (s : seq T) :
size (take n0 s) = minn n0 (size s).
Proof. by rewrite size_take. Qed.
Lemma mktupleE (n : nat) (T' : Type) (f : 'I_n -> T') :
tval (mktuple f) = [seq f i | i <- enum 'I_n].
Proof.
case: n f => [|n] f.
by rewrite enum_ord0/=; apply/size0nil; rewrite size_tuple card_ord.
by apply/(@eq_from_nth _ (f ord0)) => [|i]; rewrite size_tuple.
Qed.
Definition resize (x : T) (u : seq T) (n : nat) :=
take n (u ++ [seq x | i <- iota 0 (n - size u)]).
Lemma size_resize (x : T) (u : seq T) (n : nat) :
size (resize x u n) = n.
Proof.
rewrite size_take size_cat size_map size_iota.
case: (ltnP n (size u)) => [/ltnW|] nu.
by rewrite geq_subn// addn0; apply/minn_idPl.
by rewrite -subDnCA// subDnAC// subnn minnn.
Qed.
Lemma nth_resize (x : T) (u : seq T) (n i : nat) :
(i < n)%N -> nth x (resize x u n) i = nth x u i.
Proof.
rewrite /resize => ilt.
rewrite nth_take// nth_cat.
case: ifP => [//|] /negP/negP; rewrite -leqNgt => ui.
rewrite [RHS]nth_default//.
rewrite nth_map// size_iota; apply/ltn_sub2r => //.
exact/(leq_ltn_trans ui ilt).
Qed.
Lemma resize_id (x : T) (u : seq T) : resize x u (size u) = u.
Proof.
apply/(@eq_from_nth _ x); first exact/size_resize.
move=> i; rewrite size_resize => iu.
by rewrite nth_resize.
Qed.
End GeneralBaseType.
Section WithEqType.
Variables (T : eqType) (a1 a2 : pred T) (s : seq T).
Lemma resize_idE (x : T) (u : seq T) n :
(resize x u n == u) = (n == size u).
Proof.
have [->|/eqP nu] := eqVneq n (size u); first exact/eqP/resize_id.
by apply/negP => /eqP/(congr1 size); rewrite size_resize.
Qed.
Lemma sub_filter :
subpred a1 a2 -> {subset [seq x <- s | a1 x] <= [seq x <- s | a2 x]}.
Proof.
move=> sub_a1_a2 x ; rewrite !mem_filter.
by move/andP => [a1x ->] ; rewrite andbT sub_a1_a2.
Qed.
Lemma sub_map_filter (U : eqType) (f : T -> U) : subpred a1 a2 ->
{subset [seq f x | x <- s & a1 x] <= [seq f x | x <- s & a2 x]}.
Proof.
move=> sub_a1_a2 x.
move/mapP => [y hy] eq_x_fy ; apply/mapP ; exists y => //.
exact: sub_filter.
Qed.
Lemma eq_map_seq [U : Type] [f g : T -> U] (r : seq T) :
{in r, forall x, f x = g x} -> map f r = map g r.
Proof.
elim: r => //= x r IHr fg; congr cons; first exact/fg/mem_head.
by apply/IHr => y yr; apply/fg; rewrite in_cons yr orbT.
Qed.
Lemma subseq_drop_index (x : T) (s1 s2 : seq T) :
subseq (x :: s1) s2 = subseq (x :: s1) (drop (index x s2) s2).
Proof.
move nE: (index _ _) => n.
elim: n s2 nE => [|n IHn] s2 nE; first by rewrite drop0.
case: s2 nE => [//|y s2].
have [->|/negPf /=] := eqVneq y x; first by rewrite /= eqxx.
by rewrite eq_sym => -> /succn_inj; apply/IHn.
Qed.
End WithEqType.
Lemma subseq_nth_iota (T : eqType) (x : T) (s1 s2 : seq T) :
reflect
(exists t, subseq t (iota 0 (size s2)) /\ s1 = [seq nth x s2 i | i <- t])
(subseq s1 s2).
Proof.
elim: s1 s2 => [|x1 s1 IHs1] s2/=.
rewrite sub0seq; apply/Bool.ReflectT.
by exists [::]; split=> //; apply/sub0seq.
apply/(iffP idP) => [|[]].
move=> /[dup] /mem_subseq/(_ x1 (mem_head _ _)) x12.
rewrite subseq_drop_index drop_index//= eqxx => /IHs1[/=] t [].
rewrite size_drop => tsub ->.
exists ((index x1 s2) :: [seq (index x1 s2).+1 + i | i <- t]); split=> /=.
rewrite -[size s2](@subnKC (index x1 s2).+1) ?index_mem// -cat1s iotaD.
apply/cat_subseq; first by rewrite sub1seq mem_iota/=.
by rewrite iotaE0; apply/map_subseq.
rewrite nth_index//; congr cons.
rewrite -map_comp; apply/eq_map => k.
by rewrite nth_drop/=.
case=> [[] //|i t] [] /[dup] /mem_subseq/(_ i (mem_head _ _)).
rewrite mem_iota/= => /[dup] ilt /ltnW/minn_idPl is2.
rewrite [subseq (i :: t) _]subseq_drop_index index_iota/= subn0.
rewrite is2 drop_iota.
case jE: (size s2 - i)%N => [//|j] /=.
rewrite eqxx => tsub [] -> s12.
rewrite -[s2](cat_take_drop i) nth_cat size_take is2 ltnn subnn.
apply/(subseq_trans _ (suffix_subseq _ _)).
case s2E: (drop i s2) => /= [|y s3].
by move: ilt; rewrite -[s2](cat_take_drop i) s2E cats0 size_take is2 ltnn.
rewrite eqxx; apply/IHs1; exists [seq (j - i.+1)%N | j <- t].
move: jE; rewrite -size_drop s2E/= => /succn_inj jE.
rewrite jE; split.
move: tsub; rewrite iotaE0 => /(map_subseq (fun x => x - i.+1)%N).
congr subseq; rewrite -map_comp -[RHS]map_id; apply/eq_map => k /=.
by rewrite subDnCA// subnn addn0.
rewrite s12 -map_comp; apply/eq_in_map => k /= /(mem_subseq tsub).
rewrite mem_iota => /andP[] ik _.
rewrite -[s2](cat_take_drop i) nth_cat size_take is2 ltnNge (ltnW ik)/=.
by rewrite s2E -[(k - i)%N]prednK ?subn_gt0//= subnS.
Qed.
End MoreSeq.
Section MoreSeqEqType.
Variable (T : eqType).
Local Notation "x =p y" := (perm_eq x y) (at level 70, no associativity).
Lemma perm_eq_nil (s : seq T) : (s =p [::]) = (s == [::]).
Proof.
by apply/idP/eqP => /perm_nilP.
Qed.
Lemma rem_cons (s : seq T) (a : T) : rem a (a :: s) = s.
Proof. by rewrite /= eqxx. Qed.
Lemma rcons_nil (a : T) : rcons [::] a = [:: a].
Proof. by rewrite -cats1 cat0s. Qed.
Fact cat_nil (s1 s2 : seq T) :
s1 ++ s2 == [::] = ((s1 == [::]) && (s2 == [::])).
Proof. by case: s1 => //= ->. Qed.
Lemma rem_is_nil (x : T) (s : seq T) : rem x s == [::] ->
((s == [::]) || (s == [:: x])).
Proof. by case: s => //= y s; rewrite eqseq_cons; case: (y == x). Qed.
Lemma undup_catl (s1 s2 : seq T) :
undup ((undup s1) ++ s2) = undup (s1 ++ s2).
Proof.
elim: s1 => // x s /= ih.
have [x_in_s | /negbTE x_notin_s] := boolP (x \in s).
by rewrite mem_cat x_in_s.
rewrite mem_cat x_notin_s /= !ih mem_cat.
have [x_in_s2 | /negbTE x_notin_s2] := boolP (x \in s2).
by rewrite orbT.
by rewrite orbF mem_undup x_notin_s.
Qed.
Lemma in_rcons (s : seq T) (x y : T) :
(x \in rcons s y) = (x == y) || (x \in s).
Proof. by elim: s => // z s ih; rewrite rcons_cons !in_cons ih orbCA. Qed.
(* not used *)
(* Lemma undup_rlast (s : seq T) (x : T) : *)
(* undup (rcons s x) = rcons (rem x (undup s)) x. *)
(* Proof. *)
(* elim: s => // y s ih. *)
(* rewrite rcons_cons /= in_rcons ih. *)
(* have [ <- | neq_xy] := eqVneq x y. *)
(* rewrite eqxx fun_if /= eqxx. *)
(* have [x_in_s | x_notin_s] := boolP (x \in s) => //. *)
(* by rewrite rem_id // mem_undup. *)
(* rewrite eq_sym. *)
(* move/negbTE : neq_xy => neq_xy ; rewrite neq_xy. *)
(* have [y_in_s | y_notin_s] := boolP (y \in s) => //. *)
(* by rewrite /= eq_sym neq_xy rcons_cons. *)
(* Qed. *)
Lemma undup_catr (s1 s2 : seq T) :
undup (s1 ++ (undup s2)) = undup (s1 ++ s2).
Proof.
elim: s1 => // [| x s ih]; first by rewrite !cat0s //undup_id // undup_uniq.
by rewrite /= ih !mem_cat mem_undup.
Qed.
Lemma subset_cons (x : T) (s1 s2 : seq T) :
{subset x :: s1 <= s2} <-> (x \in s2) /\ {subset s1 <= s2}.
Proof.
split => [subx12 | [x_in_s2 sub12] y].
split; first by rewrite subx12 // mem_head.
move=> y y_in_s1.
by rewrite subx12 // in_cons y_in_s1 orbT.
rewrite in_cons => /orP [/eqP -> | y_in_s1] //.
by rewrite sub12.
Qed.
Lemma undup_cat (s1 s2 : seq T) :
sub_mem (mem s1) (mem s2) -> undup (s1 ++ s2) = undup s2.
Proof.
elim: s1 => // x s1 ih /=.
move/subset_cons => [x_in_s2 sub12].
by rewrite ih // mem_cat x_in_s2 orbT.
Qed.
Example undup_cat_ss (s : seq T) : undup (s ++ s) = undup s.
Proof. exact: undup_cat. Qed.
(* Fact undup_uniq (x : R) (s : seq T) : *)
(* undup (s ++ (x :: s2)) = if x \in s then . *)
(* x \in s => undup s = rem x s. *)
Fact undup_cat_1312 (s1 s2 s3 : seq T) :
undup ((s1 ++ s3) ++ s2 ++ s3) = undup (s1 ++ s2 ++ s3).
Proof.
elim: s1 => // [|x s1 /= ->].
rewrite !cat0s undup_cat // => x.
by rewrite mem_cat => ->; rewrite orbT.
by rewrite !mem_cat orbACA orbb !orbA.
Qed.
Lemma rem_undup (x : T) (s : seq T) :
rem x (undup s) = undup (seq.filter (predC1 x) s).
Proof.
by rewrite rem_filter ?undup_uniq// filter_undup.
Qed.
Local Open Scope ring_scope.
Lemma set_nth_id (e : seq T) (i : nat) (a x : T)
: (i < size e)%N -> (set_nth x e i (nth a e i)) = e.
Proof.
move: e x; elim: i => [| i ih] e x; first by rewrite lt0n size_eq0; case: e.
by case: e => //= b e; rewrite ltnS => h; rewrite ih.
Qed.
Lemma set_nth_nth (e : seq T) (i : nat) (a : T) :
set_nth a e i (nth a e i) = e ++ (nseq (i.+1 - (size e) ) a).
Proof.
have [lt_ie|leq_ei] := ltnP i (size e).
rewrite set_nth_id //; move: lt_ie; rewrite -subn_eq0=> /eqP ->.
by rewrite cats0.
by rewrite set_nth_over // rcons_cat subSn // nseqS nth_default //.
Qed.
End MoreSeqEqType.
Lemma in_itv' (disp : unit) (T : porderType disp) (x : T) (i : interval T) :
(x \in i) = let 'Interval l u := i in
((l <= (BLeft x)) && ((BRight x) <= u))%O.
Proof.
case: i => l u; rewrite in_itv; congr andb.
by case: l => //=; case.
Qed.
Section MoreFinmap.
Local Open Scope fset_scope.
Lemma finSet_ind (T : choiceType) (P : {fset T} -> Prop) :
P fset0 -> (forall s x, P s -> P (x |` s)) -> forall s, P s.
Proof.
move=> Hfset0 HfsetU s.
move: {2}(#|`s|) (erefl #|`s|) => r.
move: s; elim: r => [s| r ih s hs]; first by move/cardfs0_eq ->.
have s_neq0 : s != fset0 by rewrite -cardfs_gt0 hs.
move: s_neq0 hs => /fset0Pn [x x_in_s].
rewrite -(fsetD1K x_in_s) cardfsU1 in_fsetD1 x_in_s eqxx [in LHS]/= add1n.
move/eqP; rewrite eqSS; move/eqP => hs.
by apply: HfsetU; apply: ih.
Qed.
Lemma neq_fset10 (i : nat) : ([fset i] == fset0) = false.
Proof.
apply/negbTE; rewrite -fproper0 fproperEcard cardfs0 cardfs1 andbT.
by apply/fsubsetP => j; rewrite in_fset0.
Qed.
Lemma imfset1 (T U : choiceType) (f : T -> U) (x : T) :
[fset f x | x in [fset x]] = [fset f x].
Proof.
apply/fsetP => y; rewrite inE; apply/imfsetP/eqP => [[z]|yE].
by rewrite inE => /eqP ->.
by exists x; rewrite // inE.
Qed.
Lemma imfset0 [T U : choiceType] (f : T -> U) :
[fset f x | x in fset0] = fset0.
Proof.
have [-> //|[x]] := fset_0Vmem [fset f x | x in fset0].
by move=> /imfsetP[y] /=; rewrite inE.
Qed.
Lemma imfsetU [T U : choiceType] (f : T -> U) (s t : {fset T}) :
[fset f x | x in s `|` t] = [fset f x | x in s] `|` [fset f x | x in t].
Proof.
apply/fsetP => x; rewrite in_fsetU; apply/imfsetP/orP => [[y] /= + ->|].
by rewrite in_fsetU => /orP [ys|yt]; [left|right]; apply/imfsetP; exists y.
by case=> /imfsetP [y] /= ys ->; exists y => //; rewrite in_fsetU ys// orbT.
Qed.
Lemma imfset_bigfcup [I T U : choiceType] (r : seq I) (P : pred I)
(F : I -> {fset T}) (f : T -> U) :
[fset f x | x in \bigcup_(i <- r | P i) F i] =
\bigcup_(i <- r | P i) [fset f x | x in F i].
Proof.
elim: r => [|i r IHr]; first by rewrite !big_nil imfset0.
by rewrite !big_cons; case: (P i) => //; rewrite imfsetU IHr.
Qed.
Lemma fsubset_trans (T : choiceType) (B A C : {fset T}) :
A `<=` B -> B `<=` C -> A `<=` C.
Proof. by move=> /fsubsetP AB /fsubsetP BC; apply/fsubsetP => x /AB /BC. Qed.
Lemma seq_fset_sub (d : unit) (T : choiceType) (s1 s2 : seq T) :
reflect {subset s1 <= s2} (seq_fset d s1 `<=` seq_fset d s2).
Proof.
apply: (@equivP _ _ _ (@fsubsetP _ _ _)).
by split => h x; move/(_ x) : h; rewrite !seq_fsetE.
Qed.
Lemma seq_fset_nil (K : choiceType) (k : unit) : seq_fset k [::] = (@fset0 K).
Proof. by apply/eqP; rewrite -cardfs_eq0 size_seq_fset. Qed.
Lemma seq_fset_cons (K : choiceType) (k : unit) (a : K) (s : seq K) :
seq_fset k (a :: s) = a |` (seq_fset k s).
Proof. by apply/fsetP => x; rewrite !in_fsetE !seq_fsetE inE. Qed.
Lemma seq_fset_cat (K : choiceType) (k : unit) (s1 s2 : seq K) :
seq_fset k (s1 ++ s2) = (seq_fset k s1) `|` (seq_fset k s2).
Proof.
elim: s1 s2 => [s1|a s1 ih s2]; first by rewrite seq_fset_nil fset0U.
by rewrite /= !seq_fset_cons ih fsetUA.
Qed.
Lemma eq_fsetD (K : choiceType) (A B C : finSet K) :
(A `\` B == C) = fdisjoint C B && ((C `<=` A) && (A `<=` B `|` C)).
Proof. by rewrite eqEfsubset fsubDset fsubsetD andbCA andbA andbC. Qed.
Lemma fset1D1 (K : choiceType) (a' a : K) :
[fset a] `\ a' = if (a' == a) then fset0 else [fset a].
Proof.
apply/fsetP=> b; rewrite 2!fun_if !in_fsetE; have [->|] := altP (a' =P a).
exact/andNb.
by have [//->|]// := altP (b =P a); rewrite ?andbF // eq_sym => ->.
Qed.
End MoreFinmap.
Section MoreRelation.
Variables (T : eqType) (P : pred T) (sT : subType P) (r : equiv_rel T).
Definition sub_r (x y : sT) := r (val x) (val y).
Lemma sub_r_refl : reflexive sub_r.
Proof. by rewrite /sub_r. Qed.
Lemma sub_r_sym : ssrbool.symmetric sub_r.
Proof. by move=> x y; rewrite /sub_r equiv_sym. Qed.
Lemma sub_r_trans : transitive sub_r.
Proof. by move=> x y z hyx; apply: equiv_trans. Qed.
Fail Check [equiv_rel of sub_r].
Canonical sub_r_equiv := EquivRel sub_r sub_r_refl sub_r_sym sub_r_trans.
Check [equiv_rel of sub_r].
End MoreRelation.
Section TestMoreRelation.
Variables (T : eqType) (P : pred T) (sT : subType P) (r : equiv_rel T).
Definition r2 := @sub_r _ _ sT r.
Check [equiv_rel of r].
Check [equiv_rel of r2].
End TestMoreRelation.
Section MoreBigop.
Lemma big_morph_in (R1 R2 : Type)
(f : R2 -> R1) (id1 : R1) (op1 : R1 -> R1 -> R1)
(id2 : R2) (op2 : R2 -> R2 -> R2) (D : pred R2) :
(forall x y, x \in D -> y \in D -> op2 x y \in D) ->
id2 \in D ->
{in D &, {morph f : x y / op2 x y >-> op1 x y}} ->
f id2 = id1 ->
forall (I : Type) (r : seq I) (P : pred I) (F : I -> R2),
all D [seq F x | x <- r & P x] ->
f (\big[op2/id2]_(i <- r | P i) F i) = \big[op1/id1]_(i <- r | P i) f (F i).
Proof.
move=> Dop2 Did2 f_morph f_id I r P F.
elim: r => [|x r ihr /= DrP]; rewrite ?(big_nil, big_cons) //.
set b2 := \big[_/_]_(_ <- _ | _) _; set b1 := \big[_/_]_(_ <- _ | _) _.
have fb2 : f b2 = b1 by rewrite ihr; move: (P x) DrP => [/andP[]|].
case: (boolP (P x)) DrP => //= Px /andP[Dx allD].
rewrite f_morph ?fb2 // /b2 {b2 fb2 ihr b1 x Px Dx f_morph f_id}.
elim: r allD => [|x r ihr /=]; rewrite ?(big_nil, big_cons) //.
by case: (P x) => //= /andP [??]; rewrite Dop2 // ihr.
Qed.
Variables (R : Type) (idx : R).
Fact big_ord_exchange_cond {op : Monoid.law idx} {a b : nat}
(P : pred nat) (F : nat -> R) :
\big[op/idx]_(i < a | P i && (i < b)) F i =
\big[op/idx]_(i < b | P i && (i < a)) F i.
Proof.
wlog le_b_a : a b / b <= a => [hwlog|].
have /orP [le_a_b|le_b_a] := leq_total a b; last exact: hwlog.
by symmetry; apply: hwlog.
rewrite big_ord_narrow_cond /=; apply: eq_big => // i.
by rewrite (leq_trans _ le_b_a) ?andbT.
Qed.
Fact big_ord_exchange {op : Monoid.law idx} {a b : nat} (F : nat -> R) :
\big[op/idx]_(i < a | i < b) F i =
\big[op/idx]_(i < b | i < a) F i.
Proof. exact: (big_ord_exchange_cond xpredT). Qed.
Fact big_ord1 (op : Monoid.law idx) (F : nat -> R) :
\big[op/idx]_(i < 1) F i = F 0.
Proof. by rewrite big_ord_recl big_ord0 Monoid.mulm1. Qed.
Lemma big_nat_widen_l (op : Monoid.law idx)
(m1 m2 n : nat) (P : pred nat) (F : nat -> R) :
m2 <= m1 ->
\big[op/idx]_(m1 <= i < n | P i) F i =
\big[op/idx]_(m2 <= i < n | P i && (m1 <= i)) F i.
Proof.
move=> le_m2m1; have [ltn_m1n|geq_m1n] := ltnP m1 n; last first.
rewrite big_geq // big_nat_cond big_pred0 // => i.
by apply/negP => /and3P[/andP [_ /leq_trans]]; rewrite leqNgt => ->.
rewrite [RHS](@big_cat_nat _ _ _ m1) // 1?ltnW //.
rewrite [X in op X]big_nat_cond [X in op X]big_pred0; last first.
by move=> i; have [] := ltnP i m1; rewrite ?(andbT, andbF).
rewrite Monoid.mul1m [LHS]big_nat_cond [RHS]big_nat_cond.
by apply/eq_bigl => i; have [] := ltnP i m1; rewrite ?(andbT, andbF).
Qed.
Lemma big_mknat (op : Monoid.law idx) (a b : nat) (F : nat -> R) :
\big[op/idx]_(i < b | a <= i) F i = \big[op/idx]_(a <= i < b) F i.
Proof.
rewrite -(big_mkord (fun i => a <= i) F).
by rewrite -(big_nat_widen_l _ _ predT) ?leq0n.
Qed.
Lemma sum1_ord (n : nat) :
(\sum_(i < n) 1)%N = n.
Proof. by rewrite big_const_ord iter_addn_0 mul1n. Qed.
Lemma big_ord_iota (op : Monoid.law idx) (n : nat)
(P : pred nat) (F : nat -> R) :
\big[op/idx]_(i < n | P i) F i = \big[op/idx]_(i <- iota 0 n | P i) F i.
Proof.
elim: n P F => [|n IHn] P F; first by rewrite big_ord0 big_nil.
rewrite [LHS]big_mkcond big_ord_recr iotanS.
rewrite -cats1 big_cat big_cons big_nil add0n Monoid.mulm1/=; congr (op _ _).
by rewrite -big_mkcond IHn.
Qed.
Lemma prodr_const_seq (F : semiRingType) (I : Type) (r : seq I) (x : F) :
(\prod_(i <- r) x = x ^+ (size r))%R.
Proof.
elim: r => [|y r IHr].
by rewrite big_nil expr0.
by rewrite big_cons IHr/= exprS.
Qed.
Lemma bigmin_le {disp : unit} {T : orderType disp} (I : Type) (r : seq I)
(x : T) (P : pred I) (F : I -> T) (y : T) :
(\big[Order.min/x]_(i <- r | P i) F i <= y)%O =
(x <= y)%O || has (fun i => P i && (F i <= y)%O) r.
Proof.
elim: r => [|i r IHr]; first by rewrite big_nil orbF.
rewrite big_cons/=; case: (P i) => //=.
by rewrite ge_min IHr !orbA; congr (_ || _); apply/orbC.
Qed.
Lemma bigmin_lt {disp : unit} {T : orderType disp} (I : Type) (r : seq I)
(x : T) (P : pred I) (F : I -> T) (y : T) :
(\big[Order.min/x]_(i <- r | P i) F i < y)%O =
(x < y)%O || has (fun i => P i && (F i < y)%O) r.
Proof.
elim: r => [|i r IHr]; first by rewrite big_nil orbF.
rewrite big_cons/=; case: (P i) => //=.
by rewrite gt_min IHr !orbA; congr (_ || _); apply/orbC.
Qed.
Lemma le_bigmin {disp : unit} {T : orderType disp} (I : Type) (r : seq I)
(x : T) (P : pred I) (F : I -> T) (y : T) :
(y <= \big[Order.min/x]_(i <- r | P i) F i)%O =
(y <= x)%O && all (fun i => P i ==> (y <= F i)%O) r.
Proof.
elim: r => [|i r IHr]; first by rewrite big_nil andbT.
rewrite big_cons/=; case: (P i) => //=.
by rewrite le_min IHr !andbA; congr (_ && _); apply/andbC.
Qed.
Lemma lt_bigmin {disp : unit} {T : orderType disp} (I : Type) (r : seq I)
(x : T) (P : pred I) (F : I -> T) (y : T) :
(y < \big[Order.min/x]_(i <- r | P i) F i)%O =
(y < x)%O && all (fun i => P i ==> (y < F i)%O) r.
Proof.
elim: r => [|i r IHr]; first by rewrite big_nil andbT.
rewrite big_cons/=; case: (P i) => //=.
by rewrite lt_min IHr !andbA; congr (_ && _); apply/andbC.
Qed.
Lemma le_bigmax {disp : unit} {T : orderType disp} (I : Type) (r : seq I)
(x : T) (P : pred I) (F : I -> T) (y : T) :
(y <= \big[Order.max/x]_(i <- r | P i) F i)%O =
(y <= x)%O || has (fun i => P i && (y <= F i)%O) r.
Proof.
elim: r => [|i r IHr]; first by rewrite big_nil orbF.
rewrite big_cons/=; case: (P i) => //=.
rewrite le_max IHr !orbA; congr (_ || _); exact/orbC.
Qed.
Lemma big_hasE (I J : Type) (op : Monoid.com_law idx)
(r : seq I) (P : pred I) (F : I -> R) (s : seq J) (a : I -> pred J) :
(forall i, P i -> (count (a i) s <= 1)%N) ->
\big[op/idx]_(i <- r | P i && has (a i) s) F i =
\big[op/idx]_(j <- s) \big[op/idx]_(i <- r | P i && a i j) F i.
Proof.
move=> s1.
elim: r => [|i r IHr].
under [in RHS]eq_bigr do rewrite big_nil.
rewrite big_nil big_const_idem//.
exact/Monoid.mulm1.
under [in RHS]eq_bigr do rewrite big_cons.
rewrite big_cons; case /boolP: (P i) => //= Pi.
case/boolP: (has (a i) s) => [si|]; last first.
rewrite -all_predC.
rewrite {}IHr; elim: s s1 => /= [|j s IHs] s1 si; first by rewrite !big_nil.
rewrite !big_cons.
move/andP: si => [] /negPf -> /IHs -> // k /s1.
by case: (a k j) => //=; rewrite add1n ltnS leqn0 => /eqP ->.
rewrite {}IHr; elim: s s1 si => /= [//|] j s IHs s1.
rewrite !big_cons Monoid.mulmA.
case: (a i j) (s1 i Pi) => /= [|_].
rewrite add1n ltnS leqNgt -has_count => ais _; congr (op _ _).
elim: s ais {IHs s1} => [_|k s IHs] /=.
by rewrite !big_nil.
by rewrite negb_or !big_cons => /andP[] /negPf -> /IHs ->.
move=> /IHs <-.
by rewrite Monoid.mulmCA Monoid.mulmA.
move=> k /s1.
by case: (a k j) => //=; rewrite add1n ltnS leqn0 => /eqP ->.
Qed.
Lemma big_pred1_seq (op : Monoid.law idx)
[I : eqType] (r : seq I) (i : I) (F : I -> R) :
uniq r ->
\big[op/idx]_(j <- r | j == i) F j = if i \in r then F i else idx.
Proof.
elim: r => [_|j r IHr /= /andP[] jr runiq]; first by rewrite big_nil.
rewrite big_cons in_cons eq_sym.
move: jr; have [<- /= /negP jr|ij _ /=] := eqVneq i j; last exact/IHr.