-
Notifications
You must be signed in to change notification settings - Fork 3
/
grobner.v
1614 lines (1436 loc) · 57 KB
/
grobner.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
Require Import Inverse_Image.
From mathcomp Require Import all_ssreflect all_algebra order.
From mathcomp Require Import ssrcomplements freeg mpoly.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import Monoid GRing.Theory.
Open Scope ring_scope.
(******************************************************************************)
(******************************************************************************)
(* *)
(* This file contains a proof that membership to an ideal defined by a *)
(* sequence of polynomials can be decided using Buchberger's algorithm *)
(* It introduces the following definitions: *)
(* *)
(* plt p p' : p is lexicographically smaller than p' for the order <%O *)
(* ideal L p : p is in the ideal generated by L *)
(* mdiv m p q : remove the monomial m in p using the polynomial q *)
(* mreduce L p q : p can be reduced to p by a division of a poynomial in L *)
(* irreducible L p : p is irreducible by a polynomial in L *)
(* mreducef L p : return an option type that gives a witness of a reduction *)
(* of p by a polynomial in L *)
(* mreduceplus L p q : p can be reduced in multiple steps to q *)
(* mreducestar L p q : p can be reduced in multiple steps to q and q is *)
(* irreducible *)
(* mreduceplusf L p : compute one irreducible reduction of p *)
(* grobner L : L is a grobner basis *)
(* mconfluent L : the reduction defined for L is confluent *)
(* spoly p q : the S-polynomial of p and q *)
(* spoly_red L : all the S-polynomials of L reduce to 0 *)
(* splt L L' : the sequence of polynomial L' is smaller than L iff *)
(* L'is p :: L where p cannot be reduced by L *)
(* mbuch_all L : complete the sequence L to build a Grobner basis using *)
(* Buchberger's algorithm *)
(* idealf L p : check if p is in the ideal generated by L *)
(* *)
(******************************************************************************)
(******************************************************************************)
Import Order.TTheory.
Section Grobner.
(******************************************************************************)
(* Ideals with respect to a list of polynomials *)
(******************************************************************************)
Section Ideal.
Variable R : ringType.
Variable n : nat.
Implicit Types p q : {mpoly R[n]}.
Implicit Types m : 'X_{1..n}.
Variable L : seq {mpoly R[n]}.
Definition ideal p : Prop :=
exists (t : (size L).-tuple _), p = \sum_(i < size L) t`_i * L`_i.
Lemma ideal0 : ideal 0.
Proof.
exists (nseq_tuple _ 0).
rewrite big1 // => i /=.
by rewrite nth_nseq if_same mul0r.
Qed.
Lemma idealZ a p : ideal p -> ideal (a *: p).
Proof.
case=>t ->.
exists [tuple (a *: t`_i) | i < size L] => //.
rewrite scaler_sumr; apply: eq_bigr => i _ /=.
by rewrite (nth_map i) ?size_enum_ord // nth_enum_ord // mpoly_scaleAl.
Qed.
Lemma idealN p : ideal p -> ideal (-p).
Proof.
case=>t ->.
exists [tuple - t`_i | i < size L] => //.
rewrite -sumrN; apply: eq_bigr => i _ /=.
by rewrite (nth_map i) ?size_enum_ord // nth_enum_ord // -mulNr.
Qed.
Lemma idealD p q : ideal p -> ideal q -> ideal (p + q).
Proof.
move=> [t1 ->][t2 ->].
exists [tuple (t1`_i + t2`_i) | i < size L] => //.
rewrite -big_split; apply: eq_bigr => i _ /=.
by rewrite (nth_map i) ?size_enum_ord // nth_enum_ord // mulrDl.
Qed.
Lemma idealB p q : ideal p -> ideal q -> ideal (p - q).
Proof. by move=> Ip Iq; apply: idealD => //; apply: idealN. Qed.
Lemma idealM p q : ideal q -> ideal (p * q).
Proof.
case=>t ->.
exists [tuple (p * t`_i) | i < size L] => //.
rewrite mulr_sumr; apply: eq_bigr => i _ /=.
by rewrite (nth_map i) ?size_enum_ord // nth_enum_ord // mulrA.
Qed.
Lemma ideal_mem p : p \in L -> ideal p.
Proof.
move=> Ip.
have Hp : (index p L < size L)%nat by rewrite index_mem.
pose j := Ordinal Hp.
exists [tuple if i == j then 1 else 0 | i < size L].
rewrite (bigD1 j) //= big1 /= => [|[i /= Hi] /= iDj]; last first.
- rewrite (nth_map j) ?size_enum_ord //=.
case: ifP; last by rewrite mul0r.
move/eqP/val_eqP; rewrite /= nth_enum_ord => //= HH.
by case/eqP: iDj; apply/val_eqP => /=.
rewrite (nth_map j) ?size_enum_ord //=.
case: ifP; last first.
- by move/eqP/val_eqP; rewrite /= nth_enum_ord // eqxx.
by rewrite nth_index // addr0 mul1r.
Qed.
End Ideal.
Lemma ideal_consr (R : ringType) n l (p q : {mpoly R[n]}) :
ideal l p -> ideal (q::l) p.
Proof.
case=> t ->; exists [tuple of 0 :: t] => /=.
by rewrite big_ord_recl /= mul0r add0r.
Qed.
Lemma ideal_consl (R : ringType) n l (p q : {mpoly R[n]}) :
ideal l p -> ideal (p::l) q -> ideal l q.
Proof.
case=> [t1] ->; case=> /= t2 ->.
exists [tuple (t2`_0 * t1`_i + t2`_(fintype.lift ord0 i))| i < size l].
rewrite big_ord_recl [X in _ * X + _ = _]/=.
rewrite mulr_sumr -big_split.
apply: eq_bigr => i _ /=.
rewrite (nth_map i); last by rewrite size_enum_ord ltn_ord.
rewrite nth_enum_ord; last by apply: ltn_ord.
by rewrite mulrDl mulrA.
Qed.
(******************************************************************************)
(* Order on polynomials derived from (_ < _)%O *)
(******************************************************************************)
Section Order.
Variable R : ringType.
Variable n : nat.
Implicit Types p q : {mpoly R[n]}.
Implicit Types m : 'X_{1..n}.
Definition plt p q : bool :=
has (fun m2 =>
[&& m2 \notin msupp p,
all (fun m1 => ((m1 < m2)%O || (m1 \in msupp q))) (msupp p) &
all (fun m1 => ((m1 <= m2)%O || (m1 \in msupp p))) (msupp q)])
(msupp q).
Local Notation "a < b" := (plt a b).
Lemma pltP p q :
reflect (exists m,
[/\ m \in msupp q, m \notin msupp p &
forall m1, (m < m1)%O -> (m1 \in msupp p) = (m1 \in msupp q)])
(p < q).
Proof.
apply: (iffP hasP)=> [[m Im /and3P[NIm /allP /=Hq /allP Hp]]|[m [Im NIm HA]]].
- exists m; split=> // m1 Lm.
have := Hq m1; have := Hp m1; do 2 case: (_ \in _) => //=.
- by rewrite leNgt Lm => /(_ is_true_true).
by rewrite ltNge [(m <= _)%O]ltW // => _ /(_ is_true_true).
exists m => //; apply/and3P; split=>//; apply/allP=> m1 Im1.
- case: ltP=>//=.
by rewrite le_eqVlt => /orP[/eqP<-|/HA<-].
by case: leP=>//= /HA->.
Qed.
Lemma plt_mlead p q : (mlead p < mlead q)%O -> (p < q).
Proof.
have [/eqP->|Zq] := boolP (q == 0); first by rewrite mlead0 ltx0.
move=> Lp; apply/pltP; exists (mlead q); split=> [||m1 Lm1].
- by apply: mlead_supp.
- by rewrite mcoeff_msupp negbK mcoeff_gt_mlead.
rewrite !mcoeff_msupp !mcoeff_gt_mlead //.
by apply: lt_trans Lm1.
Qed.
Lemma plt_anti p : p < p = false.
Proof. by apply/idP=> /hasP[x ->]. Qed.
Lemma plt0 p : (0 < p) = (p != 0).
Proof.
apply/pltP/idP=> [[m [Im NIm HA]]|Zp].
- by apply/eqP=> Zp; move: Im; rewrite Zp msupp0 in_nil.
exists (mlead p); rewrite msupp0 ?in_nil; split=> [||m1 HA] //=.
- by apply: mlead_supp.
by rewrite in_nil mcoeff_msupp mcoeff_gt_mlead ?eqxx.
Qed.
Lemma plt0r p : p < 0 = false.
Proof. by case: (boolP (_ < 0)) => // /hasP[m]; rewrite msupp0 // inE. Qed.
Lemma plt_trans : transitive plt.
Proof.
move=> r p q /pltP[m1 [Im1 NIm1 HAm1]] /pltP[m2 [Im2 NIm2 HAm2]].
have [Lm|Lm] := leP m1 m2.
- apply/pltP; exists m2; split=> [||m3 Lm3] //.
- by move: Lm; rewrite le_eqVlt => /orP[/eqP<-//|/HAm1->].
by rewrite -HAm2 // HAm1 // (le_lt_trans Lm).
apply/pltP; exists m1; split=> [||m3 Lm3] //.
- by rewrite -(HAm2 _ Lm).
by rewrite HAm1 // -HAm2 // (lt_trans Lm).
Qed.
Lemma plt_lead (p q : {mpoly R[n]}) : (mlead p < mlead q)%O -> p < q.
Proof.
have [/eqP->|Zq] := boolP (q == 0); first by rewrite mlead0 ltx0.
move=> H; apply/pltP; exists (mlead q); split=> [||m1 Lm] //.
- by apply: mlead_supp.
- by rewrite mcoeff_msupp mcoeff_gt_mlead ?negbK.
by rewrite !mcoeff_msupp !mcoeff_gt_mlead // (lt_trans _ Lm).
Qed.
Lemma plt_leadE (p q : {mpoly R[n]}) : p != 0 -> (p < q) ->
(mlead p < mlead q)%O ||
((mlead p == mlead q) &&
(p - p@_(mlead q) *: 'X_[mlead q] < q - q@_(mlead q) *: 'X_[mlead q])).
Proof.
have [/eqP->|Zq] := boolP (q == 0); first by rewrite plt0r.
move=> Zp Lp.
have/pltP[m [Im NIm Lm]] := Lp; apply/orP.
have [/eqP Eq|Dq] := boolP (mlead q == m).
- left; have [/Lm HH|] := boolP (m < mlead p)%O.
- have := mlead_supp Zp; rewrite HH => /msupp_le_mlead.
case: ltgtP=>// Ep _.
by case/negP: NIm; rewrite -Eq -Ep mlead_supp.
rewrite -leNgt le_eqVlt Eq => /orP[/eqP Ep|] //.
by case/negP: NIm; rewrite -Ep mlead_supp.
right; apply/andP; split; last first.
- apply/pltP; exists m; split=> [||m1 Lm1].
- rewrite (perm_mem (msupp_rem _ _)) (rem_filter _ (msupp_uniq _)).
by rewrite mem_filter /= eq_sym Dq.
- rewrite (perm_mem (msupp_rem _ _)) (rem_filter _ (msupp_uniq _)).
by rewrite mem_filter /= eq_sym Dq.
rewrite !(perm_mem (msupp_rem _ _)) !(rem_filter _ (msupp_uniq _)).
by rewrite !mem_filter /= Lm.
have: (mlead q <= mlead p)%O.
- apply: msupp_le_mlead; rewrite Lm ?mlead_supp //.
by rewrite lt_neqAle eq_sym Dq msupp_le_mlead.
rewrite le_eqVlt => /orP[/eqP->|] // /plt_mlead /(plt_trans Lp).
by rewrite plt_anti.
Qed.
Lemma plt_mlast p q :
(p < q) ->
(exists p1 p2, [/\ p = p1 + p2, perm_eq (msupp p1) (rem (mlast q) (msupp q))
& p2 < 'X_[mlast q]])
\/
(p < q - q@_(mlast q) *: 'X_[mlast q]).
Proof.
(* Why this proof is so long? *)
have [/eqP->|Zq] := boolP (q == 0); first by rewrite plt0r.
case/pltP=> m [Im NIm Lm].
have [/eqP Eq|Dq] := boolP (mlast q == m); last first.
- right; apply/pltP; exists m; split=> [||m3 Im3]//.
- rewrite (perm_mem (msupp_rem _ _)) rem_filter ?msupp_uniq //.
by rewrite mem_filter ?msupp_uniq //= eq_sym Dq.
rewrite (perm_mem (msupp_rem _ _)) rem_filter ?msupp_uniq //.
rewrite mem_filter ?msupp_uniq /=.
rewrite Lm // andbC; case: (boolP (_ \in _)) => //=.
have: (mlast q < m3)%O.
- by apply: le_lt_trans Im3; apply: mlast_lemc.
by rewrite lt_neqAle [_ == m3]eq_sym => /andP[->].
pose p1 := \sum_(i <- msupp p | (m < i)%O) p@_i *: 'X_[i].
pose p2 := \sum_(i <- msupp p | (i < m)%O) p@_i *: 'X_[i].
left; exists p1, p2; split=> //.
- rewrite [p]mpolyE (bigID (fun i => (m < i)%O)) /=; congr (_ + _).
rewrite big_seq_cond [p2]big_seq_cond.
apply: eq_bigl=> // m1.
rewrite -leNgt le_eqVlt.
by have [/eqP->|] := boolP (_ == _); first by rewrite (negPf NIm).
- apply: uniq_perm=> [||m1]; first by apply: msupp_uniq.
- by apply/rem_uniq/msupp_uniq.
rewrite (rem_filter _ (msupp_uniq _)).
rewrite mem_filter /= (perm_mem (msupp_sum _ _ _))=>
[||m2 m3 Im2 Im3 Dm2m3 m4 /=].
- apply/flattenP/andP=>[[m2 /mapP[m3]]|[Dm LL]].
rewrite mem_filter => /andP[H1 H2] -> /msuppZ_le.
rewrite mcoeff_msupp mcoeffX.
have [/eqP<- _|] := boolP (_ == m1); last by rewrite eqxx.
split; last by rewrite -Lm.
have: (mlast q < m3)%O by rewrite (le_lt_trans (mlast_lemc _) H1).
by rewrite eq_sym lt_neqAle; case/andP.
exists [::m1]; last by rewrite inE.
apply/mapP; exists m1.
- rewrite mem_filter ?LL.
suff F : (m < m1)%O by rewrite F Lm.
by have := mlast_lemc LL; rewrite le_eqVlt eq_sym (negPf Dm) Eq.
by rewrite msuppMCX // -mcoeff_msupp Lm //
lt_neqAle -Eq eq_sym Dm mlast_lemc.
- by apply: msupp_uniq.
rewrite !msuppMCX -?mcoeff_msupp // !inE.
by case: (boolP (_ == m2)) => // /eqP->; rewrite (negPf Dm2m3).
apply/pltP; exists (mlast q); split=> //.
- by rewrite msuppX inE.
- apply/negP=> /msupp_sum_le /flattenP[p3 /mapP[m1]].
rewrite mem_filter =>/andP[H1 H2] ->.
rewrite !msuppMCX -?mcoeff_msupp // inE => /eqP Eq1.
by case/negP: NIm; rewrite -Eq Eq1.
move=> m1 Lm1.
rewrite msuppX inE.
have: m1 \notin msupp p2.
- apply/negP=> /msupp_sum_le /flatten_mapP[m2].
rewrite mem_filter=> /andP[H1 H2].
rewrite msuppMCX -?mcoeff_msupp // inE => /eqP Em1.
have : (m2 < m1)%O by rewrite (lt_trans H1) // -Eq.
by rewrite Em1 ltxx.
move/negPf->.
by move: Lm1; rewrite eq_sym lt_neqAle => /andP[/negPf->].
Qed.
Lemma mlast_ind P :
P 0 ->
(forall q, P (q - q@_(mlast q) *: 'X_[mlast q]) -> P q) ->
(forall p, P p).
Proof.
move=> HP IH p.
have [k sEk] : {k | size (msupp p) = k} by eexists; apply: refl_equal.
elim: k p sEk => [p Ls| n1 IH1 p ES].
- suff /eqP->: p == 0 by [].
by rewrite -msupp_eq0; case: msupp Ls.
apply/IH/IH1.
rewrite (perm_size (msupp_rem _ _)) size_rem ?(eqP H) ?ES//.
by apply/mlast_supp; rewrite -msupp_eq0; case: msupp ES.
Qed.
Lemma plt_msuppl (p q r : {mpoly R[n]}) :
perm_eq (msupp p) (msupp q) -> (p < r) = (q < r).
Proof.
move=> HS; apply/pltP/pltP.
- case=> m [H1 H2 H3]; exists m; split=>//; first by rewrite -(perm_mem HS).
by move=> m1 Lm1; rewrite -(perm_mem HS) H3.
case=> m [H1 H2 H3]; exists m; split=>//; first by rewrite (perm_mem HS).
by move=> m1 Lm1; rewrite (perm_mem HS) H3.
Qed.
Lemma plt_msuppr (p q r : {mpoly R[n]}) :
perm_eq (msupp p) (msupp q) -> (r < p) = (r < q).
Proof.
move=> HS; apply/pltP/pltP.
- case=> m [H1 H2 H3]; exists m; split=>//; first by rewrite -(perm_mem HS).
by move=> m1 Lm1; rewrite -(perm_mem HS) H3.
case=> m [H1 H2 H3]; exists m; split=>//; first by rewrite (perm_mem HS).
by move=> m1 Lm1; rewrite (perm_mem HS) H3.
Qed.
Lemma plt_wf : well_founded plt.
Proof.
move=> p; apply: Acc_intro.
move: p; apply: mlast_ind => [q|q].
- by rewrite plt0r.
move: {1}(mlast q) (eqxx (mlast q))=> a; move: a q.
apply: (well_founded_induction (@ltom_wf n))=> /= m IH q Em H q1.
move=> /plt_mlast [[/= p1 [p2 [-> H1 H2]]]|]; last by apply: H.
have HA : Acc (fun p q0 : mpoly n R => p < q0) p1.
- apply: Acc_intro=> q2 Lq2.
have: q2 < (q - q@_(mlast q) *: 'X_[(mlast q)]).
- rewrite (plt_msuppr _ (_ : perm_eq _ (msupp p1))) //.
by rewrite (perm_trans (msupp_rem _ _)) // perm_sym.
by apply: H.
move: p2 H2; apply: mlast_ind => [_|q2 IH1 Lq2].
- by rewrite addr0.
have [/eqP->|Zq2] := boolP (q2 == 0).
- by rewrite addr0.
have Lp1 : forall m1, m1 \in msupp p1 -> (mlast q < m1)%O.
- move=> m1; rewrite (perm_mem H1) (rem_filter _ (msupp_uniq _)) mem_filter.
by case/andP=> /= HH /mlast_lemc; rewrite le_eqVlt eq_sym (negPf HH).
have Lp2 : forall m1, m1 \in msupp q2 -> (m1 < mlast q)%O.
- move=> m1 Lm2.
case/pltP : Lq2 => m2 [].
rewrite msuppX !inE => /eqP-> Lq HH.
have Dm1 : mlast q != m1 by apply: contra Lq => /eqP->.
rewrite ltNge le_eqVlt negb_or Dm1.
apply/negP=> //.
by move=>/HH; rewrite Lm2 inE eq_sym (negPf Dm1).
have F0 : mlast q2 \in msupp q2 by apply: mlast_supp.
have F1 : mlast q2 \notin msupp p1.
- apply/negP=> HH;
suff: (mlast q < mlast q)%O by rewrite ltxx.
by apply: lt_trans (Lp1 _ _) (Lp2 _ F0).
have F2 : (mlast q2 < m)%O by rewrite (eqP Em) Lp2.
have F3 : mlast (p1 + q2) = mlast q2.
- apply: mlastE=> [|m1 /msuppD_le].
- rewrite (perm_mem (msuppD _)) ?mem_cat ?mlast_supp ?orbT //.
move=> m1; apply/negP=> /andP[/Lp1 O1 /Lp2 O2].
suff: (m1 < m1)%O by rewrite ltxx.
by apply: lt_trans O1.
rewrite mem_cat=> /orP[/Lp1 O1|/mlast_lemc//].
by apply: ltW; apply: lt_trans O1; rewrite -(eqP Em).
have F4 : (p1 + q2)@_(mlast q2) = q2@_(mlast q2).
- have [/eqP->|Zp1] := boolP (p1 == 0); first by rewrite add0r.
rewrite mcoeffD mcoeff_lt_mlast ?add0r //.
by apply: lt_trans F2 _; rewrite (eqP Em) (Lp1 _ (mlast_supp _)).
apply: Acc_intro => q3.
apply: (IH _ F2)=> [|q4]; first by rewrite F3 eqxx.
rewrite F3 F4 -addrA.
suff: Acc (fun p q0 : mpoly n R => p < q0)
(p1 + (q2 - q2@_(mlast q2) *: 'X_[(mlast q2)])).
- by case=> JJ; apply: JJ.
apply: IH1.
apply/pltP; exists (mlast q); split=>//=.
- by rewrite msuppX inE eqxx.
- rewrite (perm_mem (msupp_rem _ _)) mem_rem_uniq ?msupp_uniq // !inE negb_and.
by rewrite mcoeff_msupp !negbK mcoeff_gt_mlead ?eqxx ?orbT //
Lp2 // mlead_supp.
move=> m1 Lm1.
rewrite msuppX inE (perm_mem (msupp_rem _ _)) mem_rem_uniq ?msupp_uniq // !inE.
have [/Lp2|] := boolP (m1 \in msupp q2); first by rewrite ltNge ltW.
by move: Lm1; rewrite andbF lt_neqAle eq_sym => /andP[/negPf->].
Qed.
End Order.
Section OrderIDomain.
Variable R : idomainType.
Variable n : nat.
Implicit Types p q : {mpoly R[n]}.
Implicit Types m : 'X_{1..n}.
Local Notation "p < q" := (plt p q).
Lemma plt_scalerl a p q : a != 0 -> (a *: p < q) = (p < q).
Proof.
move=> Za; apply/pltP/pltP=> [] [m [Im NIm H]]; exists m; split=>//.
- by rewrite -(perm_mem (msuppZ _ Za)).
- by move=> m1 /H; rewrite (perm_mem (msuppZ _ Za)).
- by rewrite (perm_mem (msuppZ _ Za)).
by move=> m1 /H; rewrite (perm_mem (msuppZ _ Za)).
Qed.
Lemma plt_scalerr a p q : a != 0 -> (p < a *: q) = (p < q).
Proof.
move=> Za; apply/pltP/pltP=> [] [m [Im NIm H]]; exists m; split=>//.
- by rewrite -(perm_mem (msuppZ _ Za)).
- by move=> m1 /H; rewrite (perm_mem (msuppZ _ Za)).
- by rewrite (perm_mem (msuppZ _ Za)).
by move=> m1 /H; rewrite (perm_mem (msuppZ _ Za)).
Qed.
End OrderIDomain.
Section Main.
Variable R : fieldType.
Variable n : nat.
Implicit Types p q : {mpoly R[n]}.
Implicit Types m : 'X_{1..n}.
Local Notation "p < q" := (plt p q).
Variable L : seq {mpoly R[n]}.
(******************************************************************************)
(* Division of a polynomial by another polynomial *)
(******************************************************************************)
Definition mdiv m p q : {mpoly R[n]} :=
p - (p@_m/ mleadc q) *: 'X_[m - mlead q] * q.
Lemma mdiv_not_supp m p q :
q != 0 -> (mlead q <= m)%MM ->
m \notin msupp (mdiv m p q).
Proof.
move=> Zq Lq; rewrite mcoeff_msupp negbK mcoeffB -scalerAl mcoeffZ.
by rewrite -{3}(submK Lq) [_ * q]mulrC mcoeffMX divfK ?mleadc_eq0 // subrr.
Qed.
Lemma mdiv_not_supp_id m p q : m \notin msupp p -> mdiv m p q = p.
Proof.
by rewrite /mdiv => /memN_msupp_eq0->; rewrite mul0r scale0r mul0r subr0.
Qed.
Lemma mdiv_coef_id m p q : q != 0 -> (mlead q <= m)%MM -> (mdiv m p q)@_m = 0.
Proof.
move=> Zq Lq.
rewrite /mdiv -scalerAl mcoeffB mcoeffZ.
by rewrite -{3}(submK Lq) [_ * q]mulrC mcoeffMX divfK ?mleadc_eq0 // subrr.
Qed.
Lemma mdiv_coef_more m p q m1 :
(mlead q <= m)%MM -> (m < m1)%O -> (mdiv m p q)@_m1 = p@_m1.
Proof.
move=> Lq Lm.
rewrite /mdiv -scalerAl mcoeffB !mcoeffZ [_ * q]mulrC.
rewrite [X in _ - _ * X = _]mcoeff_gt_mlead.
- by rewrite mulr0 subr0.
have [/eqP->|ZX] := boolP ('X_[(m - mlead q)] == 0 :> {mpoly R[n]}).
- by rewrite mulr0 mlead0 (le_lt_trans (le0x _) Lm).
have [/eqP->|Zq] := boolP (q == 0).
- by rewrite mul0r mlead0 (le_lt_trans (le0x _) Lm).
by rewrite mleadM // mleadXm mpoly.addmC submK.
Qed.
Lemma mdiv_scalel a m p q : mdiv m (a *: p) q = a *: mdiv m p q.
Proof.
by rewrite /mdiv mcoeffZ -mulrA -scalerA -scalerAl -scalerBr.
Qed.
Lemma mdivX m1 m p q : q != 0 -> (mlead q <= m)%MM ->
mdiv (m1 + m)%MM ('X_[m1] * p) q = 'X_[m1] * mdiv m p q.
Proof.
move=> Zq Lq; rewrite /mdiv.
by rewrite [_ * p]mulrC mcoeffMX mulrBr [p * _]mulrC
-!scalerAl !scalerAr mulrA -mpolyXD addmBA.
Qed.
Lemma mdivB m1 m2 p q1 q2 :
mdiv m1 p q1 - mdiv m2 p q2 =
(p@_m2/ mleadc q2) *: 'X_[m2 - mlead q2] * q2 -
(p@_m1/ mleadc q1) *: 'X_[m1 - mlead q1] * q1.
Proof. by rewrite /mdiv addrAC opprD addrA subrr sub0r opprK. Qed.
Lemma mdiv_lead m p q :
m \in msupp p -> (mlead q <= m)%MM -> (mlead (mdiv m p q) <= mlead p)%O.
Proof.
move=> Im Lq.
apply: le_trans (mleadB_le _ _) _.
have [/eqP->|Zlp] := boolP (p@_m / mleadc q == 0).
by rewrite scale0r mul0r mlead0 /= joinx0.
rewrite leEjoin joinAC joinxx joinC -leEjoin.
rewrite -scalerAl mleadZ //.
apply: le_trans (mleadM_le _ _) _.
by rewrite mleadXm submK // msupp_le_mlead.
Qed.
(******************************************************************************)
(* Division as a reduction relation *)
(******************************************************************************)
Fact red_key : unit. Proof. by []. Qed.
Definition mreduce_lock p q : bool :=
has (fun m =>
has (fun r =>
[&& r != 0, (mlead r <= m)%MM & q == mdiv m p r])
L)
(msupp p).
Definition mreduce : rel {mpoly R[n]} :=
locked_with red_key mreduce_lock.
Canonical mreduce_unlockable := [unlockable fun mreduce].
Notation "a ->_1 b " := (mreduce a b) (at level 52).
Lemma mreduceP p q :
reflect (exists m r,
[/\ m \in msupp p, r \in L, r != 0, (mlead r <= m)%MM &
q = mdiv m p r])
(p ->_1 q).
Proof.
rewrite unlock.
apply: (iffP hasP)=> [[m Im /hasP[r Ir /and3P[Zr Lm /eqP->]]]|
[m [r [Im Ir Zr Lm ->]]]].
- by exists m, r.
by exists m=>//; apply/hasP; exists r=>//; rewrite Zr Lm /=.
Qed.
Lemma mreduce_mdiv m p q :
m \in msupp p -> q \in L -> q != 0 -> (mlead q <= m)%MM ->
p ->_1 mdiv m p q.
Proof.
by move=> Im Iq Zq Lq; apply/mreduceP; exists m, q=>//; rewrite Zq Lq /=.
Qed.
Lemma mreduce_lt p q : p ->_1 q -> q < p.
Proof.
case/mreduceP=> m [r [Im Ir Zr Lm ->]].
apply/pltP; exists m; split=> [||m1 Lm1] //.
- by rewrite mcoeff_msupp negbK mdiv_coef_id.
by rewrite !mcoeff_msupp mdiv_coef_more.
Qed.
Lemma mreduce_lead p q : p ->_1 q -> (mlead q <= mlead p)%O.
Proof. by case/mreduceP=> m [r [Im Ir H1 H2 ->]]; apply: mdiv_lead. Qed.
Lemma mreduce_neq0 p q : p ->_1 q -> p != 0.
Proof. by rewrite unlock -msupp_eq0 /=; case: (msupp p). Qed.
Lemma mreduce_scale a p q : a != 0 -> p ->_1 q -> a *: p ->_1 a *: q.
Proof.
move=> Za /mreduceP[m [r [Im Ir Zr Lr ->]]].
apply/mreduceP; exists m, r; split=>//.
- by rewrite mcoeff_msupp mcoeffZ mulf_neq0 // -mcoeff_msupp.
by rewrite mdiv_scalel.
Qed.
Lemma mreduceXm m p q : p ->_1 q -> 'X_[m] * p ->_1 'X_[m] * q.
Proof.
case/mreduceP=> m1 [r [Im1 Ir Zr Lr ->]].
apply/mreduceP; exists (m + m1)%MM, r; split=>//; last by rewrite mdivX.
- by rewrite mcoeff_msupp [_ * p]mulrC mcoeffMX -mcoeff_msupp.
by rewrite (lepm_trans Lr) // lem_addl.
Qed.
Lemma mreduce_compatX a m p q :
(mlead p < m)%O -> p ->_1 q ->
(a *: 'X_[m]) + p ->_1 (a *: 'X_[m]) + q.
Proof.
move=> Lp /mreduceP[m1 [r [Im1 Ir Zr Lr ->]]].
have Dmm1 : m != m1.
- move: Im1; rewrite mcoeff_msupp; apply: contra => /eqP<-.
by apply/eqP/mcoeff_gt_mlead.
apply/mreduceP; exists m1, r; split=>//.
- rewrite !mcoeff_msupp mcoeffD mcoeffZ mcoeffX (negPf Dmm1).
by rewrite mulr0 add0r -mcoeff_msupp.
by rewrite /mdiv mcoeffD mcoeffZ mcoeffX (negPf Dmm1) mulr0 add0r !addrA.
Qed.
Lemma ideal_reduce p q : p ->_1 q -> (ideal L p <-> ideal L q).
Proof.
case/mreduceP=> m [r [Im Ir Zr Lr ->]].
rewrite /mdiv; split => H.
- by apply: idealB =>//; apply/idealM/ideal_mem.
rewrite -[p](subrK ((p@_m / mleadc r) *: 'X_[(m - mlead r)] * r)).
by apply: idealD =>//; apply/idealM/ideal_mem.
Qed.
(******************************************************************************)
(* Realization of divisibilty and irreducibility *)
(******************************************************************************)
Definition mreducef p : option {mpoly R[n]} :=
let L1 := [seq if (mlead r <= m)%MM
then Some (mdiv m p r) else None |
m <- msupp p, r <- [seq x <- L | x != 0]] in
nth None L1 (find isSome L1).
Definition irreducible_lock p : bool := ~~ mreducef p.
Definition irreducible : pred {mpoly R[n]} :=
locked_with red_key irreducible_lock.
Canonical irreducible_unlockable := [unlockable fun irreducible].
Lemma irreducibleP p : reflect (forall q, ~ p ->_1 q) (irreducible p).
Proof.
rewrite [irreducible]unlock /mreducef.
set L1 := [seq _ | _ <- _, _ <- _].
apply: (iffP idP)=> [H1 q /mreduceP[m [r [Im Ir Zr Lr Er]]]|H1].
- suff /(nth_find None) : has isSome L1 by apply: negP.
apply/hasP; exists (Some q)=>//.
apply/allpairsP; exists (m,r)=>/=; split=>//.
- by rewrite mem_filter Zr.
by rewrite Lr Er.
have : ~~ has isSome L1.
- apply/hasPn => /= [[q|] // /allpairsP[[/= m r [Im]]]].
rewrite mem_filter; case/andP=>Zr Ir; case: ifP=>// Lr [Er].
by case: (H1 q); apply/mreduceP; exists m, r.
by rewrite has_find -leqNgt => /(nth_default None)->.
Qed.
Lemma irreducible0 : irreducible 0.
Proof. by apply/irreducibleP => m /mreduce_lt; rewrite plt0r. Qed.
Lemma mreducefE p :
if mreducef p is Some q then p ->_1 q else irreducible p.
Proof.
rewrite [irreducible]unlock /mreducef.
set L1 := [seq _ | _ <- _, _ <- _].
have [H|] := boolP (has isSome L1); last first.
- by rewrite has_find -leqNgt=> /(nth_default None)->.
case E: nth (nth_find None H) => [a|] // _.
move: H; rewrite has_find => /(mem_nth None); rewrite E.
move/allpairsP=> [/=[m r]/= [Im]].
rewrite mem_filter; case/andP=>Zr Ir; case: ifP=>// Lr [Er].
by apply/mreduceP; exists m, r.
Qed.
(******************************************************************************)
(* Multi-step reduction *)
(******************************************************************************)
Definition mr q p f : bool :=
(p == q) ||
let g y :=
(if y < p as b return ((b -> _) -> _)
then fun f => f is_true_true
else fun f => false) (f y) in
has (fun m =>
has (fun r =>
[&& r != 0, (mlead r <= m)%MM & g (mdiv m p r)])
L)
(msupp p).
Lemma mr_ext p q f g :
(forall r (H : r < p), f r H = g r H) ->
mr q f = mr q g.
Proof.
rewrite /mr => HH; case: (_ == _) => //=.
elim: L => //= a l IH.
apply: eq_in_has => /= m Om; congr ([&& _, _ & _] || _).
by set a1 := mdiv _ _ _; case: (_ < _) (f a1) (g a1) (HH a1).
apply: eq_in_has => // r Hr; congr [&& _, _ & _].
by set a1 := mdiv _ _ _; case: (_ < _) (f a1) (g a1) (HH a1).
Qed.
Definition mreduceplus p q : bool := Fix (@plt_wf R n) _ (mr q) p.
Notation " a ->_+ b " := (mreduceplus a b) (at level 52).
Lemma mreduceplusP p q :
reflect (p = q \/ exists2 r, p ->_1 r & r ->_+ q) (p ->_+ q).
Proof.
rewrite {2}/mreduceplus Fix_eq //; last by move=> *; apply: mr_ext.
rewrite {1}/mr.
have [/eqP E1|E1] := boolP (_ == _).
- by apply: (iffP idP) => //=; left.
apply: (iffP hasP) => [/= [m Im]|].
- case/hasP=>/= r Ir /and3P[Zr Lr].
rewrite mreduce_lt => [HH|].
- by right; exists (mdiv m p r)=>//; apply/mreduceP; exists m, r.
by apply/mreduceP; exists m, r.
case => [/eqP| [r /mreduceP[m [r1 [Im Ir1 Zr1 Lr1 ->]]] HH]].
- by rewrite (negPf E1).
exists m =>//; apply/hasP; exists r1 =>//=.
rewrite Zr1 Lr1 mreduce_lt //.
by apply/mreduceP; exists m, r1.
Qed.
Lemma mreduceplus_ref : reflexive mreduceplus.
Proof. by move=> p; apply/mreduceplusP; left. Qed.
Lemma mreduceplusW p q : p ->_1 q -> p ->_+ q.
Proof.
move=> H; apply/mreduceplusP; right; exists q=>//.
by apply: mreduceplus_ref.
Qed.
Lemma mreduceplus_trans : transitive mreduceplus.
Proof.
move=> q p r H1 H2.
move: p H1; apply: (well_founded_induction (@plt_wf R n)) =>
p IH /mreduceplusP[->//|[r1 H1r1 H2r1]].
apply/mreduceplusP; right; exists r1 =>//.
apply: IH =>//.
by apply: mreduce_lt.
Qed.
Lemma mreduceplus_scale a p q : p ->_+ q -> a *: p ->_+ a *: q.
Proof.
have [/eqP->_|Za] := boolP (a == 0).
- by rewrite !scale0r mreduceplus_ref.
move: p q; apply: (well_founded_induction (@plt_wf R n))
=> p IH q /mreduceplusP[<-|[r1]].
- by apply: mreduceplus_ref.
move=> Ra /IH R1a; apply/mreduceplusP; right; exists (a *: r1).
- by apply: mreduce_scale.
by apply: R1a; apply: mreduce_lt.
Qed.
Lemma mreduceplusXm m p q : p ->_+ q -> 'X_[m] * p ->_+ 'X_[m] * q.
Proof.
move: p q; apply: (well_founded_induction (@plt_wf R n))
=> p IH q /mreduceplusP[<-|[r1]].
- by apply: mreduceplus_ref.
move=> Ra /IH R1a; apply/mreduceplusP; right; exists ('X_[m] * r1) => //.
- by apply: mreduceXm.
apply: R1a.
by apply: mreduce_lt.
Qed.
Lemma mreduceplus_compatX a m p q :
(mlead p < m)%O -> p ->_+ q ->
(a *: 'X_[m]) + p ->_+ (a *: 'X_[m]) + q.
Proof.
move: p q; apply: (well_founded_induction (@plt_wf R n)) => p IH q Lm.
case/mreduceplusP=> [<-|[r Rp Rr]].
- by apply: mreduceplus_ref.
apply: mreduceplus_trans (IH r _ _ _ _) => //.
- by apply: mreduceplusW; apply: mreduce_compatX.
- by apply: mreduce_lt.
rewrite ltNge.
apply/negP=> HH.
have: p < p.
- apply: plt_trans (mreduce_lt Rp).
apply: plt_mlead.
by apply: lt_le_trans HH.
by rewrite plt_anti.
Qed.
Lemma mreduceplus_0_mem p r : r \in L -> p * r ->_+ 0.
Proof.
move=> Ir.
have [/eqP->|Zr] := boolP (r == 0).
- by rewrite mulr0 mreduceplus_ref.
have Zlr : mleadc r != 0 by rewrite mleadc_eq0.
move: p; apply: (well_founded_induction (@plt_wf _ _)) => p IH.
have [/eqP->|Zp] := boolP (p == 0).
- by rewrite mul0r mreduceplus_ref.
pose p1 := p - mleadc p *: 'X_[mlead p].
have /mreduceplus_trans -> //: p * r ->_+ p1 * r.
- apply: mreduceplusW.
apply/mreduceP; exists (mlead (p * r)); exists r; split=> //.
- by apply: mlead_supp; rewrite mulf_eq0 negb_or Zp.
- by rewrite mleadM // lem_addl.
rewrite /mdiv /p1 mulrBl mleadM_proper; last first.
- by rewrite mulf_neq0 // mleadc_eq0.
by rewrite mleadcM mulfK // addmK.
apply/IH/pltP; exists (mlead p); split=>[||m1 Lm1].
- by apply: mlead_supp.
- by rewrite mcoeff_msupp mcoeffB mcoeffZ mcoeffX eqxx mulr1 subrr eqxx.
rewrite !mcoeff_msupp mcoeffB mcoeffZ mcoeffX.
rewrite mcoeff_gt_mlead //.
move: Lm1; rewrite lt_neqAle => /andP[/negPf-> _].
by rewrite mulr0 subrr eqxx.
Qed.
Lemma ideal_reduceplus p q : p ->_+ q -> (ideal L p <-> ideal L q).
Proof.
move: p; apply: (well_founded_induction (@plt_wf _ _)) => p1 IH.
move/mreduceplusP=> [->//|[q1 H1 H2]].
have [H3 H4] := IH _ (mreduce_lt H1) H2.
split=> H5; first by apply/H3/(ideal_reduce H1).
by apply/(ideal_reduce H1)/H4.
Qed.
Lemma ideal_reduceplus_0 p : p ->_+ 0 -> ideal L p.
Proof. by case/ideal_reduceplus => _ /(_ (ideal0 _)). Qed.
Lemma reduceB_distr p q r :
p - q ->_1 r ->
exists p1 q1,
[/\ p ->_+ p1, q ->_+ q1 & r = p1 - q1].
Proof.
case/mreduceP=> m [r1 [Im Ir1 Zr1 Lr1 ->]].
have Zmr1 : mleadc r1 != 0.
- by move: Zr1; rewrite mleadc_eq0 /mdiv; case: (_ == _).
exists (if m \in msupp p then mdiv m p r1 else p).
exists (if m \in msupp q then mdiv m q r1 else q); split.
- case: (boolP (_ \in _)) => Imp; last by apply: mreduceplus_ref.
by apply/mreduceplusW; apply/mreduceP; exists m, r1.
- case: (boolP (_ \in _)) => Imq; last by apply: mreduceplus_ref.
by apply/mreduceplusW; apply/mreduceP; exists m, r1.
move/msuppB_le: Im; rewrite /mdiv mem_cat mcoeffB.
have [H1 _|/memN_msupp_eq0-> //= ->] := boolP (_ \in _); last first.
- by rewrite sub0r -!scalerAl mulNr scaleNr opprK opprB -!addrA [-_ + _]addrC.
have [H2|/memN_msupp_eq0->] := boolP (_ \in _); last first.
- by rewrite subr0 -!addrA [-_ + _]addrC.
rewrite mulrBl -!scalerAl scalerBl !opprD !opprK.
rewrite !addrA; congr (_ + _); rewrite -!addrA; congr (_ + _).
by rewrite addrC.
Qed.
Lemma reduceplusB_distr p q :
p - q ->_+ 0 -> exists2 r, p ->_+ r & q ->_+ r.
Proof.
move: (p - q) {2 4}p {2 4}q (eqxx (p -q)).
apply: (well_founded_induction (@plt_wf _ _)) => r1 IH p1 q1 /eqP HH.
move/mreduceplusP => [Zr1|].
- exists p1; first by apply: mreduceplus_ref.
by rewrite -[p1](subrK q1) -HH Zr1 add0r mreduceplus_ref.
rewrite HH => [[r2 Hr2]].
have FF : r2 < r1 by apply: mreduce_lt; rewrite HH.
move: Hr2 => /reduceB_distr[p2 [q2 [H1 H2 H3]]] H4.
case: (IH r2 _ p2 q2)=> //; first by apply/eqP.
move=> r3 H5 H6; exists r3; first by apply: mreduceplus_trans H5.
by apply: mreduceplus_trans H6.
Qed.
Lemma reduceB_compat p q r :
p ->_1 q -> exists2 r1, p - r ->_+ r1 & q - r ->_+ r1.
Proof.
case/mreduceP=> m [r1 [Im Ir1 Zr1 Lr1 Er1]].
have Zmr1 : mleadc r1 != 0.
- by move: Zr1; rewrite mleadc_eq0 /mdiv; case: (_ == _).
have Zqm : q@_m = 0.
- apply: memN_msupp_eq0.
by rewrite Er1; apply: mdiv_not_supp.
have [I1m|I1m] := boolP (m \in msupp (p - r)); last first.
- have F : p@_m = r@_m.
- by move: I1m; rewrite !mcoeff_msupp negbK mcoeffB subr_eq0 => /eqP.
exists (p - r); first by apply: mreduceplus_ref.
apply/mreduceplusW/mreduceP; exists m, r1; split=> //.
- by move: Im; rewrite !mcoeff_msupp mcoeffB Zqm sub0r oppr_eq0 F.
rewrite /mdiv mcoeffB Zqm sub0r Er1 /mdiv -F mulNr scaleNr mulNr opprK.
by rewrite addrAC subrK.
exists (mdiv m (p - r) r1).
by apply/mreduceplusW/mreduceP; exists m, r1; split.
have [I2m|I2m] := boolP (m \in msupp r); last first.
- suff->: mdiv m (p - r) r1 = q - r by apply: mreduceplus_ref.
rewrite Er1 /mdiv mcoeffB.
move: I2m; rewrite mcoeff_msupp negbK => /eqP->.
by rewrite subr0 addrAC.
suff->: mdiv m (p - r) r1 = mdiv m (q - r) r1.
- apply/mreduceplusW/mreduceP; exists m, r1; split=> //.
by move: I2m; rewrite !mcoeff_msupp mcoeffB Zqm sub0r oppr_eq0.
rewrite /mdiv !mcoeffB Zqm sub0r Er1 /mdiv [_ - _ - r]addrAC.
by rewrite mulrBl scalerBl mulrBl mulNr scaleNr mulNr opprD !opprK -!addrA.
Qed.
(******************************************************************************)
(* Reduction till irreducibility *)
(******************************************************************************)
Definition mreducestar p q : bool := (p ->_+ q) && irreducible q.
Notation " a ->_* b " := (mreducestar a b) (at level 40).
Definition mfr p f : {mpoly R[n]} :=
if mreducef p is Some q then
(if q < p as b return ((b -> _) -> _)
then fun f => f is_true_true
else fun f => 0) (f q)
else p.
Lemma mfr_ext p f g :
(forall r (H : r < p), f r H = g r H) ->
mfr f = mfr g.
Proof.
rewrite /mfr => HH; case: mreducef => //= a.
by case: (_ < _) (f a) (g a) (HH a).
Qed.
(* Realisation of reduction till irreducibility *)
Definition mreduceplusf p : {mpoly R[n]} :=
Fix (@plt_wf _ _) _ mfr p.
Lemma mreducestar0W p : p ->_+ 0 -> p ->_* 0.
Proof.
move=> H; apply/andP; split=>//.
by apply: irreducible0.
Qed.
Lemma mreducestar0 : 0 ->_* 0.
Proof. by apply/mreducestar0W/mreduceplus_ref. Qed.
Lemma mreducestar_trans r p q : p ->_+ r -> r ->_* q -> p ->_* q.
Proof.
move=> H1 /andP[H2 H3]; apply/andP; split => //.
by apply: mreduceplus_trans H2.
Qed.
Lemma mreducestarfE p : p ->_* mreduceplusf p.
Proof.
move: p; apply: (well_founded_induction (@plt_wf _ _)) => p1 IH.
rewrite /mreduceplusf Fix_eq /mfr //=.
- case E: (mreducef p1) (mreducefE p1) => [r|] // Hr; last first.
- by rewrite /mreducestar mreduceplus_ref.
rewrite mreduce_lt /mreducestar //=.
have/andP[H1 /= ->]:= IH r (mreduce_lt Hr).
by rewrite (mreduceplus_trans (mreduceplusW Hr)).
move=> p f g H.
case E: (mreducef p) (mreducefE p) => [r|] //= Hr.
by move: (f r) (g r) (H r); rewrite mreduce_lt.
Qed.
Lemma mreduceplusfE p : p ->_+ mreduceplusf p.
Proof. by case/andP: (mreducestarfE p). Qed.
Lemma ideal_reducestar p q : p ->_* q -> (ideal L p <-> ideal L q).
Proof. by case/andP=>/ideal_reduceplus. Qed.
Lemma ideal_reducestar_0 p : p ->_* 0 -> ideal L p.
Proof. by case/ideal_reducestar=>_ /(_ (ideal0 _)). Qed.
(******************************************************************************)
(* Grobner Basis *)
(******************************************************************************)
Definition grobner : Prop := forall p, ideal L p -> p ->_+ 0.
(******************************************************************************)
(* Confluence *)
(******************************************************************************)
Definition mconfluent : Prop :=
forall p q r, p ->_* q -> p ->_* r -> q = r.
Lemma mconfluent_grobner: mconfluent -> grobner.
Proof.
move=> HC p [t ->].
suff F L1 (t1 : (size L1).-tuple _) :
{subset L1 <= L} -> \sum_(i < size L1) t1`_i * L1`_i ->_+ 0 by apply: F.
elim: L1 {t}t1 => /= [t _ |r L1 IH t HS].
- by rewrite big_ord0 mreduceplus_ref.
rewrite big_ord_recl.
set q := \sum_(_ < _) _.
pose q1 := \sum_(i < size L1) [tuple of behead t]`_i * L1`_i.
have F : q = q1.
- by apply: eq_bigr => /= {q q1}i; case: t => [[]].
have F1 : q ->_* 0.