-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgauss_int.v
1867 lines (1578 loc) · 62 KB
/
gauss_int.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
From HB Require Import structures.
From mathcomp Require Import all_ssreflect all_algebra all_field.
From mathcomp Require Import archimedean.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import Order.TTheory GRing.Theory Num.Theory UnityRootTheory.
Open Scope ring_scope.
(** Starting from cyril exercise *)
Section PreliminaryLemmas.
(**
* Preliminaries
Let's extend the library on rings and algebraic numbers
with some easy lemmas first.
** Question -2: prove that if a product of natural numbers is 1 then each factor is 1.
Note that we do not consider nat but the copy of nat which is embeded
in the algebraic numbers algC. The theorem already exists for nat, and
we suggest you use a compatibility lemma numbers between nat and Cnat
*)
Lemma Cnat_mul_eq1 :
{in Num.nat &, forall x y : algC, (x * y == 1) = (x == 1) && (y == 1)}.
Proof.
by move=> x y /natrP [n ->] /natrP [m ->]; rewrite -natrM !pnatr_eq1 muln_eq1.
Qed.
Lemma Cnat_add_eq1 : {in Num.nat &, forall x y : algC,
(x + y == 1) = ((x == 1) && (y == 0)) || ((x == 0) && (y == 1))}.
Proof.
move=> x y /natrP [n ->] /natrP [m ->]; rewrite -natrD !pnatr_eq1 ?pnatr_eq0.
by move: n m => [|[|?]] [|[|?]].
Qed.
(**
** Question -1: The real part of product
*)
Lemma algReM (x y : algC) : 'Re (x * y) = 'Re x * 'Re y - 'Im x * 'Im y.
Proof.
rewrite {1}[x]algCrect {1}[y]algCrect mulC_rect Re_rect //;
by rewrite rpredD ?rpredN // rpredM // ?Creal_Re ?Creal_Im.
Qed.
(**
** Question 0: The imaginary part of product
*)
Lemma algImM (x y : algC) : 'Im (x * y) = 'Re x * 'Im y + 'Re y * 'Im x.
Proof.
rewrite {1}[x]algCrect {1}[y]algCrect mulC_rect Im_rect //;
by rewrite rpredD ?rpredN // rpredM // ?Creal_Re ?Creal_Im.
Qed.
Lemma algReV (x : algC) : 'Re (x^-1) = 'Re x / `|x| ^+ 2.
Proof.
have [|/mulfV H] := boolP (x^* == 0).
rewrite conjC_eq0 => /eqP->.
by rewrite invr0 normr0 (Creal_ReP _ _) ?mul0r.
rewrite -{1}[_ ^-1]mul1r -H -mulrA -invfM.
rewrite {1}[x]algCrect conjC_rect ?Creal_Re ?Creal_Im //.
have F : (x^* * x)^-1 \is Creal.
by rewrite rpredV CrealE rmorphM conjCK mulrC.
rewrite mulrBl -mulrN -['i * _ * _]mulrA Re_rect ?normCKC //.
by rewrite rpredM ?Creal_Re.
by rewrite mulrN rpredN rpredM // Creal_Im.
Qed.
Lemma algImV (x : algC) : 'Im (x^-1) = - ('Im x / `|x| ^+ 2).
Proof.
have [|/mulfV H] := boolP (x^* == 0).
rewrite conjC_eq0 => /eqP->.
by rewrite invr0 normr0 (Creal_ImP _ _) ?mul0r ?oppr0.
rewrite -{1}[_ ^-1]mul1r -H -mulrA -invfM.
rewrite {1}[x]algCrect conjC_rect ?Creal_Re ?Creal_Im //.
have F : (x^* * x)^-1 \is Creal.
by rewrite rpredV CrealE rmorphM conjCK mulrC.
rewrite mulrBl -mulrN -['i * _ * _]mulrA Im_rect ?normCKC //.
- by rewrite mulrN.
- by rewrite rpredM ?Creal_Re.
by rewrite mulrN rpredN rpredM // Creal_Im.
Qed.
Lemma algRe_div (x y : algC) :
'Re (x / y) = ('Re x * 'Re y + 'Im x * 'Im y) / `|y| ^+ 2.
Proof.
by rewrite algReM algReV algImV mulrN opprK !mulrA mulrDl.
Qed.
Lemma algIm_div (x y : algC) :
'Im (x / y) = ('Re y * 'Im x - 'Re x * 'Im y) / `|y| ^+ 2.
Proof.
rewrite algImM algReV algImV addrC mulrN [_ * (_ / _)]mulrA mulrBl.
by rewrite mulrAC.
Qed.
Definition cdivz (x y : int) : int :=
(let q := (x %/ y) in
if (y == 0) || (2%:R * (x %% y)%Z <= `|y|) then
q else q + (-1) ^+ (y < 0)%R)%Z.
Infix "%c/" := cdivz (at level 40) : int_scope.
Lemma cdivz0 x : (x %c/ 0)%Z = 0.
Proof. by rewrite /cdivz eqxx divz0. Qed.
Lemma cdiv0z y : (0 %c/ y)%Z = 0.
Proof. by rewrite /cdivz div0z mod0z mulr0 normr_ge0 orbT. Qed.
Lemma cdivz1 x : (x %c/ 1)%Z = x.
Proof. by rewrite /cdivz oner_eq0 divz1 modz1 normr1 mulr0. Qed.
Lemma cdivzz x : x != 0 -> (x %c/ x)%Z = 1.
Proof.
move=> xNz.
by rewrite /cdivz (negPf xNz) divzz xNz modzz mulr0.
Qed.
Definition cmodz (x y : int) : int := x - (x %c/ y)%Z * y.
Infix "%c%" := cmodz (at level 40) : int_scope.
Lemma cdivz_eq (x y : int) : x = (x %c/ y)%Z * y + (x %c% y)%Z.
Proof. by rewrite /cmodz addrC subrK. Qed.
Lemma cmodz0 x : (x %c% 0)%Z = x.
Proof. by rewrite {2}(cdivz_eq x 0) mulr0 add0r. Qed.
Lemma cmod0z y : (0 %c% y)%Z = 0.
Proof. by rewrite {2}(cdivz_eq 0 y) cdiv0z mul0r add0r. Qed.
Lemma cmodz1 x : (x %c% 1)%Z = 0.
Proof. by rewrite /cmodz cdivz1 mulr1 subrr. Qed.
Lemma cmodzz x : (x %c% x)%Z = 0.
Proof.
have [/eqP->|xNz] := boolP (x == 0); first by rewrite cmod0z.
by rewrite /cmodz cdivzz // mul1r subrr.
Qed.
Lemma cmodz_lt (x y : int) : y != 0 -> (2%:R * `|x %c% y| <= `|y|)%Z.
Proof.
move=> yNz; rewrite /cmodz /cdivz (negPf yNz) /=.
have [mLe|eLm] := boolP (2%:R * (_ %% _)%Z <= `|_|).
rewrite {1}(divz_eq x y) [_ * _ + _]addrC addrK.
by rewrite [`|(_ %% _)%Z|]ger0_norm // modz_ge0.
rewrite [_ * y]mulrDl opprD addrA {1}(divz_eq x y) [_ * _ + _]addrC addrK.
have F := ltz_mod x yNz.
rewrite -normrEsign ler0_norm; last first.
by rewrite subr_le0; apply: ltW.
rewrite mulrN mulrBr opprB lterBDl (_ : 2%:R = 1 + 1) //.
by rewrite mulrDl mul1r lerD // ltW // ltNge.
Qed.
End PreliminaryLemmas.
(**
----
* The ring of Gauss integers
- Ref: exercices de mathematiques oraux X-ENS algebre 1
- Exercice 3.10. ENS Lyon
*)
Section gaussInts.
(**
First we define a predicate for the algebraic numbers which are gauss integers.
*)
Definition gaussInt_subdef : pred algC :=
[pred x | ('Re x \is a Num.int) && ('Im x \is a Num.int)].
Definition gaussInt := [qualify a x | gaussInt_subdef x].
Lemma gaussIntE x :
x \is a gaussInt = (('Re x \is a Num.int) && ('Im x \is a Num.int)).
Proof. by []. Qed.
(**
** Question 1: Prove that integers are gauss integers
*)
Lemma Cint_GI (x : algC) : x \is a Num.int -> x \is a gaussInt.
Proof.
move=> x_int.
rewrite !gaussIntE (Creal_ReP _ _) ?(Creal_ImP _ _) ?Rreal_int //.
by rewrite x_int rpred0.
Qed.
(**
** Question 2: Prove that gauss integers form a subfield
*)
Lemma GI_subring : subring_closed gaussInt.
Proof.
split => [|x y /andP[??] /andP[??]|x y /andP[??] /andP[??]].
- by rewrite Cint_GI.
- by rewrite gaussIntE !raddfB /= ?rpredB.
by rewrite gaussIntE algReM algImM rpredB ?rpredD // rpredM.
Qed.
HB.instance Definition _ :=
GRing.isSubringClosed.Build algC gaussInt_subdef GI_subring.
(**
Finally, we define the type of Gauss Integer, as a sigma type of
algebraic numbers. We soon prove that this is in fact a sub type.
*)
Record GI := GIof {
algGI : algC;
algGIP : algGI \is a gaussInt }.
(** We make the defining property of GI a Hint *)
Hint Resolve algGIP : core.
(**
We provide the subtype property.
- This makes it possible to use the generic operator "val" to get an
algC from a Gauss Integer.
*)
#[verbose]
HB.instance Definition _ := [isSub for algGI].
(**
We deduce that the real and imaginary parts of a GI are integers
*)
Lemma GIRe (x : GI) : 'Re (val x) \is a Num.int.
Proof. by have /andP [] := algGIP x. Qed.
Lemma GIIm (x : GI) : 'Im (val x) \is a Num.int.
Proof. by have /andP [] := algGIP x. Qed.
Hint Resolve GIRe GIIm : core.
Canonical ReGI x := GIof (Cint_GI (GIRe x)).
Definition ImGI x := GIof (Cint_GI (GIIm x)).
(**
We provide a ring structure to the type GI, using the subring
canonical property for the predicate gaussInt
*)
HB.howto GI subComRingType.
HB.instance Definition _ := [Countable of GI by <:].
HB.instance Definition _ := [SubChoice_isSubComRing of GI by <:].
(**
- Now we build the unitRing and comUnitRing structure of gauss
integers. Contrarily to the previous structures, the operator is
not the same as on algebraics. Indeed the invertible algebraics are
not necessarily invertible gauss integers.
- Hence, we define the inverse of gauss integers as follow : if the
algebraic inverse happens to be a gauss integer we recover the
proof and package it together with the element and get a gauss
integer, otherwise, we default to the identity.
- A gauss integer is invertible if the algbraic inverse is a gauss
integer.
*)
Definition invGI (x : GI) := insubd x (val x)^-1.
Definition unitGI (x : GI) := (x != 0) && ((val x)^-1 \is a gaussInt).
(**
** Question 3: prove a few facts in order to find a comUnitRingMixin
for GI, and then instantiate the interfaces of unitRingType and
comUnitRingType.
*)
Fact mulGIr : {in unitGI, left_inverse 1 invGI *%R}.
Proof.
move=> x /andP [x_neq0 xVGI]; rewrite /invGI.
by apply: val_inj; rewrite /= insubdK // mulVr ?unitfE.
Qed.
Fact unitGIP (x y : GI) : y * x = 1 -> unitGI x.
Proof.
rewrite /unitGI => /(congr1 val) /=.
have [-> /eqP|x_neq0] := altP (x =P 0); first by rewrite mulr0 eq_sym oner_eq0.
by move=> /(canRL (mulfK x_neq0)); rewrite mul1r => <- /=.
Qed.
Fact unitGI_out : {in [predC unitGI], invGI =1 id}.
Proof.
move=> x; rewrite inE /= -topredE /= /unitGI.
rewrite negb_and negbK => /predU1P [->|/negPf xGIF];
by apply: val_inj; rewrite /invGI ?val_insubd /= ?xGIF // invr0 if_same.
Qed.
HB.instance Definition _ :=
GRing.ComRing_hasMulInverse.Build GI mulGIr unitGIP unitGI_out.
(**
** Question 4: Show that gauss integers are stable by conjugation.
*)
Lemma conjGIE x : (x^* \is a gaussInt) = (x \is a gaussInt).
Proof.
by rewrite gaussIntE Re_conj Im_conj rpredN.
Qed.
(**
We use this fact to build the conjugation of a gauss Integers
*)
Fact conjGI_subproof (x : GI) : (val x)^* \is a gaussInt.
Proof. by rewrite conjGIE. Qed.
Canonical conjGI x := GIof (conjGI_subproof x).
Fact conjGI_sub : {morph conjGI : a b / a - b}.
Proof. by move=> a b; apply/val_eqP; rewrite /= raddfB. Qed.
HB.instance Definition _ := GRing.isAdditive.Build GI GI conjGI conjGI_sub.
Fact conjGI_multiplicative : multiplicative conjGI.
Proof. by split=> [a b|]; apply/val_eqP; rewrite /= ?(rmorphM,rmorph1). Qed.
HB.instance Definition _ :=
GRing.isMultiplicative.Build GI GI conjGI conjGI_multiplicative.
Lemma algGI_nat n : algGI n%:R = n%:R.
Proof. by elim: n => //= n IH; rewrite -addn1 !natrD -IH. Qed.
Lemma conjGI_nat n : conjGI n%:R = n%:R.
Proof. by apply/val_eqP; rewrite /= algGI_nat conjC_nat. Qed.
Lemma conjGIK : involutive conjGI.
Proof. by move=> x; apply/val_eqP/eqP; exact: conjCK. Qed.
(**
We now define the norm (stasm) for gauss integer, we don't need to
specialize it to gauss integer so we define it over algebraic numbers
instead.
*)
Definition gaussNorm (x : algC) := x * x^*.
(**
** Question 4: Show that the gaussNorm of x is the square of the complex modulus of x
*)
Lemma gaussNormE x : gaussNorm x = `|x| ^+ 2.
Proof. by rewrite normCK. Qed.
(**
** Question 5: Show that the gaussNorm of an gauss integer is a natural number.
*)
Lemma gaussNormCnat (x : GI) : gaussNorm (val x) \is a Num.nat.
Proof. by rewrite /gaussNorm -normCK normC2_Re_Im rpredD // natr_exp_even. Qed.
Hint Resolve gaussNormCnat : core.
Declare Scope GI_scope.
Delimit Scope GI_scope with GI.
Open Scope GI_scope.
Definition normGI (x : GI) := Num.trunc (gaussNorm (val x)).
Local Notation "'N x" := (normGI x%R) (at level 10) : GI_scope.
(**
** Question 6: Show that gaussNorm is multiplicative (on all algC).
*)
Lemma gaussNorm0 : gaussNorm 0 = 0.
Proof. by rewrite /gaussNorm mul0r. Qed.
Lemma normGI0 : 'N 0 = 0%N.
Proof. by rewrite /normGI gaussNorm0 (natrK 0). Qed.
Lemma gaussNorm1 : gaussNorm 1 = 1.
Proof. by rewrite /gaussNorm rmorph1 mulr1. Qed.
Lemma normGI1 : 'N 1 = 1%N.
Proof. by rewrite /normGI gaussNorm1 (natrK 1). Qed.
Lemma gaussNormM : {morph gaussNorm : x y / x * y}.
Proof. by move=> x y; rewrite /gaussNorm rmorphM mulrACA. Qed.
Lemma normGIM x y : 'N (x * y) = ('N x * 'N y)%N.
Proof. by rewrite /normGI gaussNormM truncM. Qed.
Lemma normGIX x n : 'N (x ^+ n) = ('N x ^ n)%N.
Proof.
elim: n => [|n IH].
by rewrite expr0 normGI1 expn0.
by rewrite exprS normGIM IH expnS.
Qed.
Lemma gaussNorm_eq0 (x : GI) : (gaussNorm (algGI x) == 0) = (x == 0).
Proof. by rewrite gaussNormE sqrf_eq0 normr_eq0. Qed.
Lemma normGI_eq0 (x : GI) : ('N x == 0%N) = (x == 0).
Proof.
have /charf0P<- := Cchar.
by rewrite truncK // gaussNorm_eq0.
Qed.
Lemma normGI_gt0 (x : GI) : ('N x > 0)%N = (x != 0).
Proof. by rewrite ltn_neqAle andbT eq_sym normGI_eq0. Qed.
Lemma normGI_le (x y : GI) : y != 0 -> ('N x <= 'N (x * y))%N.
Proof.
rewrite -!normGI_eq0 normGIM; case: ('N _) => // n _.
by rewrite leq_pmulr.
Qed.
Lemma normGI_nat n : 'N n%:R = (n ^ 2)%N.
Proof.
by rewrite /normGI [val _]algGI_nat gaussNormE normr_nat truncX // natrK.
Qed.
Lemma normGIE (x : GI) : ('N(x) = Num.trunc (`|'Re (val x)|)%R ^ 2 +
Num.trunc (`|'Im (val x)|)%R ^ 2)%N.
Proof.
rewrite /normGI gaussNormE normC2_Re_Im truncD ?natr_exp_even //; last first.
by rewrite qualifE /= natr_ge0 // natr_exp_even.
by rewrite -!truncX ?natr_norm_int // !intr_normK.
Qed.
Lemma truncC_Cint (x : algC) :
x \is a Num.int -> x = (-1) ^+ (x < 0)%R * (Num.trunc `|x|)%:R.
Proof.
by move=> xCint; rewrite {1}[x]intrEsign // truncK // natr_norm_int.
Qed.
Lemma normGI_eq1 (x : GI) : ('N(x) == 1)%N = (val x \in [::1;-1;'i;-'i]).
Proof.
apply/idP/idP; last first.
by rewrite normGIE !inE => /or4P[] /eqP->;
rewrite ?raddfN /= ?(Creal_ReP 1 _) ?(Creal_ImP 1 _) ?Re_i ?Im_i //=
?normrN ?normr1 ?normr0 ?trunc0 ?trunc1.
rewrite [val x]algCrect normGIE.
have /andP[/truncC_Cint {2}-> /truncC_Cint {2}->] := algGIP x.
by case: Num.trunc => [|[|m]] //; case: Num.trunc => [|[|n]] // _;
rewrite !(mulr1, mulr0, add0r, addr0); case: (_ < _)%R;
rewrite ?(expr1, expr0, mulrN, mulr1, inE, eqxx, orbT).
Qed.
(**
** Question 7: Find the invertible elements of GI
- This is question 1 of the CPGE exercice
- Suggested strategy: sketch the proof on a paper first, don't let
Coq divert you from your proofsketch
*)
Lemma unitGIE (x : GI) : (x \in GRing.unit) =
(val x \in 4.-unity_root).
Proof.
have eq_algC (a b : algC) : (a == b) = ('Re a == 'Re b) && ('Im a == 'Im b).
rewrite {1}[a]algCrect {1}[b]algCrect -subr_eq0 opprD addrACA -mulrBr.
rewrite -normr_eq0 -sqrf_eq0 normC2_rect ?rpredB ?Creal_Re ?Creal_Im //.
rewrite paddr_eq0 ?real_exprn_even_ge0 // ?rpredB ?Creal_Re ?Creal_Im //.
by rewrite !expf_eq0 /= !subr_eq0.
have N1Creal : (-1: algC) \is Num.real.
by rewrite rpredN -[_ \in _]/(1 \is Num.real).
have oneE : 1 = 1 + 'i * 0 :> algC by rewrite mulr0 addr0.
have N1E : - 1 = - 1 + 'i * 0 :> algC by rewrite mulr0 addr0.
have iE : 'i = 0 + 'i * 1 :> algC by rewrite mulr1 add0r.
have NiE : - 'i = 0 + 'i * (- 1) :> algC by rewrite mulrN1 add0r.
have onerN1 : (1 == -1 :> algC) = false.
by rewrite -subr_eq0 opprK paddr_eq0 ?oner_eq0 ?ler01.
pose my := @id algC.
transitivity (gaussNorm (val x) == 1).
apply/idP/eqP; last first.
by move=> gNx; apply/unitrPr; exists (conjGI x); apply: val_inj.
move=> x_unit; have /(congr1 (gaussNorm \o val)) /= /eqP := mulrV x_unit.
by rewrite gaussNormM gaussNorm1 Cnat_mul_eq1 //= => /andP [/eqP].
rewrite (@mem_unity_roots _ 4 (map my [:: 1; -1; 'i; -'i])) //; last 2 first.
- rewrite /= !unity_rootE /= [(- 'i) ^+ _]exprNn expr1n -signr_odd ?expr0.
by rewrite -[4%N]/(2 * 2)%N exprM sqrCi -signr_odd ?expr0 mulr1 !eqxx.
- rewrite /= ![my _](iE, oneE, N1E, NiE).
rewrite /= !in_cons !in_nil /= !negb_or -!andbA !andbT /=.
rewrite ![_ + 'i * _ == _]eq_algC ?Re_rect ?Im_rect //.
rewrite ![_ == -1]eq_sym ![_ == 1]eq_sym oppr_eq0.
by rewrite eqxx onerN1 oner_eq0.
rewrite gaussNormE [val x]algCrect normC2_rect ?Creal_Re ?Creal_Im //.
rewrite Cnat_add_eq1 ?natr_exp_even ?expf_eq0 //=.
rewrite -intr_normK // -['Im _ ^+ 2]intr_normK //.
rewrite !expr2 !Cnat_mul_eq1 ?andbb ?natr_norm_int //.
rewrite !real_eqr_norml ?Creal_Re ?Creal_Im ?ler01 ?andbT //=.
rewrite !inE ![my _](iE, oneE, N1E, NiE).
rewrite ![_ + 'i * _ == _]eq_algC
?Re_rect ?Im_rect // ?Creal_Re ?Creal_Im //.
by rewrite andb_orl andb_orr -orbA.
Qed.
Lemma algC_eqE (x y : algC) : (x == y) = (('Re x == 'Re y) && ('Im x == 'Im y)).
Proof.
apply/eqP/andP=> [->//|[/eqP H1 /eqP H2]].
by rewrite [x]algCrect H1 H2 -algCrect.
Qed.
Lemma normGI_unit (x : GI) : ('N(x) == 1)%N = (x \in GRing.unit).
Proof.
rewrite normGI_eq1 unitGIE.
rewrite (@mem_unity_roots _ 4 (map id [:: 1; -1; 'i; -'i])) //.
rewrite /= !unity_rootE /= [(- 'i) ^+ _]exprNn expr1n -signr_odd ?expr0.
by rewrite -[4%N]/(2 * 2)%N exprM sqrCi -signr_odd ?expr0 mulr1 !eqxx.
rewrite /= !in_cons !in_nil /= !negb_or -!andbA !andbT /= eqr_opp.
rewrite -addr_eq0 (eqC_nat 2 0) andTb.
rewrite algC_eqE (Creal_ImP _ _) // Im_i (eqC_nat 0 1) andbF andTb.
rewrite algC_eqE raddfN (Creal_ReP _ _) //= Re_i oppr0 (eqC_nat 1 0) andFb andTb.
rewrite algC_eqE !raddfN /= (Creal_ImP _ _) // Im_i oppr0 (eqC_nat 0 1) andbF andTb.
by rewrite -addr_eq0 (@mulrn_eq0 _ 'i 2) negb_or neq0Ci.
Qed.
Fact GI_idomainAxiom (x y : GI) : x * y = 0 -> (x == 0) || (y == 0).
Proof.
move=> /(congr1 (gaussNorm \o val)) /= /eqP.
by rewrite gaussNorm0 gaussNormM mulf_eq0 !gaussNorm_eq0.
Qed.
HB.instance Definition _ := GRing.ComUnitRing_isIntegral.Build GI
GI_idomainAxiom.
Fact divGI_subproof (x y : int) : x%:~R + 'i * y%:~R \is a gaussInt.
Proof. by rewrite gaussIntE Re_rect ?Im_rect ?Rreal_int ?intr_int. Qed.
Definition divGI (x y : GI) : GI :=
let zr := Num.floor ('Re (val x) * 'Re (val y) + 'Im (val x) * 'Im (val y)) in
let zi := Num.floor ('Re (val y) * 'Im (val x) - 'Re (val x) * 'Im (val y)) in
let n := 'N y in
GIof (divGI_subproof (cdivz zr n) (cdivz zi n)).
Notation " x %/ y " := (divGI x y) : GI_scope.
Lemma divGI0 x : x %/ 0 = 0.
Proof.
apply/val_eqP=> /=.
by rewrite !raddf0 !(mul0r, mulr0, subrr, add0r, floor0, normGI0, cdiv0z).
Qed.
Lemma div0GI y : 0 %/ y = 0.
Proof.
apply/val_eqP=> /=.
by rewrite !raddf0 !(mul0r, mulr0, subrr, add0r, floor0, normGI0, cdiv0z).
Qed.
Lemma divGI1 x : x %/ 1 = x.
Proof.
apply/val_eqP=> /=.
have /andP[CR CI] := algGIP x.
rewrite normGI1 !cdivz1 (Creal_ReP 1 _) ?(Creal_ImP 1 _) //
!(mul1r, mulr1, mulr0, mul0r, addr0, add0r, subr0).
by rewrite 2?floorK ?realr_int // -algCrect.
Qed.
Lemma divGIxx (x : GI) : x != 0 -> x %/ x = 1.
Proof.
move=> xNz; apply/val_eqP => /=.
rewrite subrr floor0 cdiv0z mulr0 addr0.
have := xNz.
rewrite -normGI_eq0.
rewrite /normGI gaussNormE [val x]algCrect normC2_rect ?(realr_int, intr_int) //.
set u :=_ + _ * _ => uNz.
have->: Num.floor u = Num.trunc u.
apply: floor_def.
rewrite [(_+ 1)%Z]addrC -intS trunc_itv //.
rewrite addr_ge0 // -expr2 real_exprn_even_ge0 ?(realr_int, intr_int) //.
by rewrite cdivzz ?mul1r ?subrr.
Qed.
Definition modGI (x y : GI) : GI := x - (x %/ y)%GI * y.
Notation " x %% y " := (modGI x y) : GI_scope.
Lemma modGI0 x : x %% 0 = x.
Proof. by apply/val_eqP; rewrite /= !raddf0 !mulr0 subr0. Qed.
Lemma mod0GI y : 0 %% y = 0.
Proof.
apply/val_eqP.
by rewrite /= !(raddf0, mul0r, mulr0, add0r, floor0, cdiv0z).
Qed.
Lemma modGI1 x : x %% 1 = 0.
Proof. by rewrite /modGI divGI1 mulr1 subrr. Qed.
Lemma divGI_eq (x y : GI) : x = (x %/ y)%GI * y + (x %% y)%GI.
Proof. by rewrite /modGI addrC subrK. Qed.
Lemma ltn_modGI(x y : GI) : ('N (x %% y)%GI < 'N y)%N = (y != 0).
Proof.
have [/eqP->|yNz] := boolP (y == 0).
by rewrite normGI0 modGI0.
have /ltn_pmul2r<-: (0 < 'N(y) ^ 2)%N by rewrite sqrn_gt0 lt0n normGI_eq0.
rewrite -{1}normGI_nat -!normGIM /modGI /divGI.
set Ux := Num.floor _.
set Uy := Num.floor _.
have UxRe : Ux%:~R = 'Re (algGI x / algGI y * ('N y)%:R).
rewrite algReM ['Re _%:R](Creal_ReP _ _) ?qualifE /= ?ler0n //.
rewrite ?['Im _%:R](Creal_ImP _ _) ?qualifE /= ?ler0n // mulr0 subr0.
rewrite /normGI truncK // algRe_div -gaussNormE divfK; last first.
by rewrite gaussNorm_eq0.
by rewrite floorK // rpredD // rpredM.
have UyIm : Uy%:~R = 'Im (algGI x / algGI y * ('N(y))%GI%:R).
rewrite algImM ['Re _%:R](Creal_ReP _ _) ?qualifE /= ?ler0n //.
rewrite ?['Im _%:R](Creal_ImP _ _) ?qualifE /= ?ler0n // mulr0 add0r mulrC.
rewrite /normGI truncK // algIm_div -gaussNormE divfK; last first.
by rewrite gaussNorm_eq0.
by rewrite floorK // rpredB // rpredM.
rewrite ['N (_ * _)]/normGI /= -[algGI x](divfK yNz).
rewrite -mulrBl -mulrA -[algGI y * _]mulrC mulrA algGI_nat mulrBl.
rewrite [_ * _%:R]algCrect -UxRe -UyIm [_ * _%:R]mulrDl -['i * _ * _]mulrA.
rewrite {1}(cdivz_eq Ux ('N y)) {1}(cdivz_eq Uy ('N y)).
rewrite subC_rect ![_ + cmodz _ _]addrC.
rewrite rmorphD /= rmorphM /= addrK.
rewrite [(_ + _)%:~R]rmorphD /= rmorphM /= addrK.
rewrite !gaussNormM gaussNormE normC2_rect ?(Rreal_int, intr_int) //.
rewrite truncM //; last by rewrite rpredD // natr_exp_even // intr_int.
rewrite mulnC ltn_pmul2l; last by rewrite lt0n normGI_eq0.
rewrite -!rmorphXn /= -!rmorphD /=.
rewrite -[_ + _]gez0_abs ?natrK; last first.
by rewrite addr_ge0 // exprn_even_ge0.
set x1 := _ ^+ 2; set x2 := _ ^+ 2.
apply: leq_ltn_trans (_ : (`|x1| + `|x2| < _)%N).
have := leqD_dist (x1) (x1 - x2) (-x2).
by rewrite !opprK subrK opprB [_ + (_ - _)]addrC subrK [(`|_| + _)%N]addnC.
rewrite -(ltn_pmul2l (isT: (0 < 2 ^ 2)%N)) mulnDr.
apply: leq_ltn_trans (_ : 2 * 'N y ^ 2 < _)%N; last first.
by rewrite ltn_mul2r sqrn_gt0 lt0n normGI_eq0 yNz.
have F : Posz ('N y) != 0.
by rewrite eqz_nat normGI_eq0.
by rewrite mul2n -addnn leq_add // !abszX -!expnMn leq_exp2r //
-(@ler_nat (Num.NumDomain.clone _ int)) natrM !natz /= cmodz_lt.
Qed.
Lemma ltn_modGIN0 x y : y != 0 -> ('N (x %% y)%GI < 'N y)%N.
Proof. by rewrite ltn_modGI. Qed.
Lemma modGIxx x : (x %% x)%GI = 0.
Proof.
have [/eqP->|xNz] := boolP (x == 0); first by rewrite mod0GI.
by rewrite /modGI divGIxx // mul1r subrr.
Qed.
Lemma divGIKl (x y : GI) : x != 0 -> (y * x %/ x) = y.
Proof.
move=> xNz.
apply/eqP; rewrite eq_sym -subr_eq0.
have := xNz; rewrite -(ltn_modGI (y * x)).
have -> : ((y * x) %% x)%GI = (y - ((y * x) %/ x)%GI) * x.
by rewrite mulrBl {2}(divGI_eq (y * x) x) [_ + (_ %% _)%GI]addrC addrK.
by rewrite normGIM -{2}['N x]mul1n ltn_mul2r ltnS leqn0 normGI_eq0 => /andP[].
Qed.
Lemma divGIKr (x y : GI) : x != 0 -> (x * y %/ x) = y.
Proof. by rewrite mulrC; exact: divGIKl. Qed.
Lemma modGIKl (x y : GI) : (y * x %% x) = 0.
Proof.
have [/eqP->|xNz] := boolP (x == 0); first by rewrite modGI0 mulr0.
by rewrite /modGI divGIKl // subrr.
Qed.
Lemma modGIKr (x y : GI) : (x * y %% x) = 0.
Proof. by rewrite mulrC modGIKl. Qed.
Definition dvdGI x y := (y %% x)%GI == 0.
Notation " x %| y " := (dvdGI x y) : GI_scope.
Lemma dvdGI0 x : (x %| 0)%GI.
Proof. by rewrite /dvdGI mod0GI. Qed.
Lemma dvdGIP (x y : GI) :
reflect (exists q : GI, y = q * x) (x %| y)%GI.
Proof.
apply: (iffP idP) => [/eqP xDy|[q ->]].
exists (y %/ x)%GI; first by rewrite {1}(divGI_eq y x) xDy addr0.
by rewrite /dvdGI modGIKl.
Qed.
Lemma dvd0GI x : (0 %| x) = (x == 0).
Proof.
apply/dvdGIP/eqP => [[q ->]|->]; first by rewrite mulr0.
by exists 0; rewrite mulr0.
Qed.
Lemma dvdGI_mull z x y : (x %| y) -> (x %| z * y).
Proof. by move=> /dvdGIP[u ->]; apply/dvdGIP; exists (z * u); exact: mulrA. Qed.
Lemma dvdGI_mulr z x y : (x %| y) -> (x %| y * z).
Proof. by rewrite mulrC; exact: dvdGI_mull. Qed.
Lemma dvdGIxx x : x %| x.
Proof. by rewrite /dvdGI modGIxx. Qed.
Lemma dvdGI_norm x y : x %| y -> ('N x %| 'N y)%N.
Proof. by move=> /dvdGIP[z ->]; rewrite normGIM dvdn_mull // dvdnn. Qed.
Lemma dvd1GI x : (1 %| x) .
Proof. by apply/dvdGIP; exists x; rewrite mulr1. Qed.
Lemma dvdGI1 x : (x %| 1) = ('N x == 1%N).
Proof.
apply/idP/idP => [H|].
by have := dvdGI_norm H; rewrite normGI1 dvdn1.
rewrite normGI_unit => H; apply/dvdGIP; exists x^-1.
by apply/eqP; rewrite mulrC eq_sym -unitrE.
Qed.
Lemma divGIK (x y : GI) : x %| y -> (y %/ x)%GI * x = y.
Proof.
have [/eqP->|nZx /dvdGIP[q ->]] := boolP (x == 0).
by rewrite dvd0GI mulr0 => /eqP->.
by rewrite divGIKl.
Qed.
Lemma dvdGI_add x y z: (x %| y) -> (x %| z) -> (x %| y + z).
Proof.
move=> /dvdGIP[q1->] /dvdGIP[q2->]; apply/dvdGIP; exists (q1 + q2).
by rewrite mulrDl.
Qed.
Lemma dvdGI_nat_dvdz_Re n x :
n%:R %| x -> (n %| `|Num.floor ('Re (algGI x))|)%N.
Proof.
case/dvdGIP=> q /val_eqP/eqP /(congr1 (fun x => Re x)) /=.
case: x => /= ax; rewrite qualifE => /andP[Rx Ix].
case: q => /= aq; rewrite qualifE => /andP[Rq Iq].
rewrite [aq]algCrect mulrDl algGI_nat -['i * _ * _]mulrA Re_rect; last 2 first.
by rewrite rpredM // Rreal_int // intr_nat.
by rewrite rpredM // Rreal_int // intr_nat.
move=> /(congr1 Num.norm) /eqP.
rewrite [`|_* _%:R|]normrM -{1}(floorK Rx) -{1}(floorK Rq).
rewrite normr_nat -!intr_norm -(intrM _ _ (Posz n)) eqr_int.
rewrite -2![`|_|]abszE -PoszM => /eqP[H].
by apply/dvdnP; exists (`|Num.floor ('Re aq)|)%N.
Qed.
Lemma dvdGI_nat_dvdz_Im n x :
n%:R %| x -> (n %| `|Num.floor ('Im (algGI x))|)%N.
Proof.
case/dvdGIP=> q /val_eqP/eqP/(congr1 (fun x => Im x)) /=.
case: x => /= ax; rewrite qualifE => /andP[Rx Ix].
case: q => /= aq; rewrite qualifE => /andP[Rq Iq].
rewrite [aq]algCrect mulrDl algGI_nat -['i * _ * _]mulrA Im_rect; last 2 first.
by rewrite rpredM // Rreal_int // intr_nat.
by rewrite rpredM // Rreal_int // intr_nat.
move=> /(congr1 Num.norm) /eqP.
rewrite [`|_* _%:R|]normrM -{1}(floorK Ix) -{1}(floorK Iq).
rewrite normr_nat -!intr_norm -(intrM _ _ (Posz n)) eqr_int.
rewrite -2![`|_|]abszE -PoszM => /eqP[H].
by apply/dvdnP; exists (`|Num.floor ('Im aq)|)%N.
Qed.
Lemma conjGI_dvd x y : x %| y -> (conjGI x) %| (conjGI y).
Proof.
case/dvdGIP=> q ->; apply/dvdGIP; exists (conjGI q).
by rewrite rmorphM.
Qed.
Fact iGI_proof : 'i \is a gaussInt.
Proof. by rewrite gaussIntE Re_i Im_i int_num0 int_num1. Qed.
Definition iGI := GIof iGI_proof.
Lemma dvdGI_norm_even x : ~~ odd ('N x) = ((1 + iGI) %| x).
Proof.
apply/idP/idP => [Ex|/dvdGIP[u ->]]; last first.
rewrite normGIM {2}/normGI gaussNormE normC2_Re_Im.
rewrite !raddfD /= Re_i Im_i (Creal_ReP _ _) // (Creal_ImP _ _) //.
by rewrite add0r addr0 expr1n (natrK 2) oddM negb_and orbT.
apply/dvdGIP.
have := algGIP x; rewrite qualifE => / andP[].
have := Ex; rewrite normGIE oddD !oddX /= negb_add.
set m := 'Re _; set n := 'Im _ => /eqP Omn Cm Cn.
suff FF : (n + m)/2%:R + 'i * ((n - m)/2%:R) \is a gaussInt.
exists (GIof FF); apply/val_eqP => /=.
rewrite -{2}['i]mulr1 mulC_rect !mulr1 mul1r -mulrBl -mulrDl.
rewrite opprB [_ + (m - n)]addrC addrA subrK.
rewrite -addrA [_ + (_ - _)]addrC subrK.
have F u : (u * 2%:R = u + u) by rewrite mulrDr mulr1.
by rewrite -!F !mulfK 1?[algGI x]algCrect ?(eqC_nat _ 0).
rewrite gaussIntE Re_rect ?Im_rect; last 4 first.
- by rewrite !(rpredM, rpredV) 1? rpredD ?Rreal_int.
- by rewrite !(rpredM, rpredV) 1? rpredB ?rpredD ?Rreal_int.
- by rewrite !(rpredM, rpredV) 1? rpredD ?Rreal_int.
- by rewrite !(rpredM, rpredV) 1? rpredB ?rpredD ?Rreal_int.
rewrite (intrEsign Cm) (intrEsign Cn).
rewrite -(truncK (natr_norm_int Cm)) -(truncK (natr_norm_int Cn)).
rewrite -[Num.trunc `|m|]odd_double_half -[Num.trunc `|n|]odd_double_half.
rewrite Omn !natrD !mulrDr ![(-1) ^+ _]signrE.
set u := nat_of_bool _; set v := nat_of_bool _; set w := nat_of_bool _.
set x1 := _./2; set y1 := _./2.
have F : 2%:R != 0 :> algC by rewrite (eqC_nat 2 0).
apply/andP; split.
rewrite addrAC addrA -mulrDl -addrA.
rewrite addrAC !addrA -[1 + 1](natrD _ 1 1) addnn.
rewrite -!muln2 !natrM !mul1r [_ * v%:R]mulrC.
rewrite ![v%:R * _]mulrBr !mulrA !(mulrBl, mulrDl) !mulfK //.
by rewrite !mul1r !(rpredB, rpredD, rpredN, rpredM) // intr_nat // natr_nat.
rewrite addrAC opprD addrA -mulrBl opprB [(_ - _) + (_ - _)]addrC !addrA subrK.
rewrite -!muln2 !natrM [_ * v%:R]mulrC.
rewrite ![v%:R * _]mulrBr !mulrA !(mulrBl, mulrDl) !mulfK //.
by rewrite !mul1r !(rpredB, rpredD, rpredN, rpredM) // intr_nat // natr_nat.
Qed.
Fixpoint gcdGI_rec (n : nat) (xx yy : GI) {struct n} :=
let rr := modGI xx yy in
if rr == 0 then yy else
if n is n1.+1 then gcdGI_rec n1 yy rr else rr.
Definition gcdGI x y :=
let: (x1, y1) := if ('N x < 'N y)%N then (y, x) else (x, y) in
if x1 == 0 then y1 else
gcdGI_rec ('N x1) x1 y1.
Lemma gcd0GI : left_id 0 gcdGI.
Proof.
move=> x; rewrite /gcdGI.
have [/eqP->|xNz]:= boolP (x == 0).
by rewrite ltnn eqxx.
rewrite normGI0 normGI_gt0 xNz (negPf xNz).
have : 'N x != 0%N by rewrite normGI_eq0.
by case: ('N _) => [|[|v]]; rewrite //= !(mod0GI, modGI0) (negPf xNz) eqxx.
Qed.
Lemma gcdGI0 : right_id 0 gcdGI.
Proof.
move=> x; rewrite /gcdGI.
have [/eqP->|xNz]:= boolP (x == 0).
by rewrite ltnn eqxx.
rewrite normGI0 /= (negPf xNz).
by case: ('N _) => [|[|v]] //= ; rewrite !(modGI0,mod0GI) (negPf xNz) ?eqxx.
Qed.
Lemma gcdGI_recE m n x y : ('N y <= m)%N -> ('N y <= n)%N
-> ('N y < 'N x)%N -> gcdGI_rec m x y = gcdGI_rec n x y.
Proof.
elim: m n x y => [|m Hrec] [|n] //= x1 y1.
- rewrite leqn0 normGI_eq0 => /eqP=> -> _.
rewrite normGI0 normGI_gt0 modGI0 => /negPf-> /=.
by case: n => [|n]; rewrite /= mod0GI eqxx.
- rewrite leqn0 normGI_eq0 => _ /eqP=> -> _.
rewrite modGI0; case: (boolP (x1 == 0)) => // x1Nz.
by case: m {Hrec} =>[|m]; rewrite /= mod0GI eqxx.
case: ifP => Epq Sm Sn Sq //; rewrite ?Epq //.
case: (eqVneq y1 0) => [->|y1Nz].
by case: n m {Sm Sn Hrec} => [|m] [|n] //=; rewrite mod0GI eqxx.
apply: Hrec; last by rewrite ltn_modGI.
by rewrite -ltnS (leq_trans _ Sm) // ltn_modGI.
by rewrite -ltnS (leq_trans _ Sn) // ltn_modGI.
Qed.
Lemma gcdGIE x y :
gcdGI x y = if ('N x < 'N y)%N
then gcdGI (y %% x) x else gcdGI (x %% y) y.
Proof.
case: (eqVneq x 0) => [-> | xNz].
by rewrite mod0GI modGI0 gcd0GI gcdGI0 if_same.
case: (eqVneq y 0) => [-> | yNz].
by rewrite mod0GI modGI0 gcd0GI gcdGI0 if_same.
rewrite /gcdGI.
case: ltnP; rewrite (negPf xNz, negPf yNz) //=.
move=> ltxy; rewrite ltn_modGI (negPf xNz) //=.
rewrite -(ltn_predK ltxy) /=; case: eqP => [->|].
by case: ('N x) => [|[|s]]; rewrite /= modGI0 (negPf xNz) // mod0GI eqxx.
move/eqP=> yxNz; rewrite (negPf xNz).
apply: gcdGI_recE => //; last by rewrite ltn_modGI.
by rewrite -ltnS (ltn_predK ltxy) (leq_trans _ ltxy) ?leqW // ltn_modGI.
by rewrite ltnW // ltn_modGI.
move=> leyx; rewrite ltn_modGI (negPf yNz) //=.
have x_gt0: ('N x > 0)%N by rewrite normGI_gt0.
rewrite -(prednK x_gt0) /=; case: eqP => [->|].
by case: ('N y)%N => [|[|s]]; rewrite /= modGI0 (negPf yNz) // mod0GI eqxx.
move/eqP=> xyNz; rewrite (negPf yNz).
apply: gcdGI_recE => //; last by rewrite ltn_modGI.
by rewrite -ltnS (prednK x_gt0) (leq_trans _ leyx) // ltn_modGI.
by rewrite ltnW // ltn_modGI.
Qed.
Lemma gcd1GIE y :
gcdGI 1 y = if ('N y == 1)%N then y else 1.
Proof.
rewrite gcdGIE normGI1; case: leqP => [|H].
rewrite leq_eqVlt; case: eqP => [/eqP|/= _].
by rewrite -dvdGI1 => /eqP->; rewrite gcd0GI.
rewrite ltnS leqn0 normGI_eq0 => /eqP->.
by rewrite modGI0 gcdGI0.
rewrite modGI1 gcd0GI.
by move: H; rewrite ltnNge leq_eqVlt negb_or => /andP[/negPf->].
Qed.
Lemma gcd1GI_norm y : 'N(gcdGI 1 y) = 1%N.
Proof. by rewrite gcd1GIE; case: eqP; rewrite ?normGI1. Qed.
Lemma gcdGI1 y : gcdGI y 1 = 1.
Proof.
rewrite gcdGIE normGI1; case: leqP => [_|].
by rewrite modGI1 gcd0GI.
rewrite ltnS leqn0 normGI_eq0 => /eqP->.
by rewrite modGI0 gcdGI0.
Qed.
Lemma gcdGIxx : idempotent_op gcdGI.
Proof. by move=> x; rewrite gcdGIE ltnn modGIxx gcd0GI. Qed.
Lemma dvdGI_mod d x y : d %| x -> (d %| y) = (d %| y %% x).
Proof.
case: (altP (x =P 0)) => [-> | nZx]; first by rewrite modGI0.
case: (altP (d =P 0)) => [-> | nZd /dvdGIP[q1 ->]].
by rewrite dvd0GI => /eqP->; rewrite modGI0.
apply/dvdGIP/dvdGIP=> [] [q2 Hq2].
rewrite /modGI Hq2 !mulrA -mulrBl.
by set u := _ - _; exists u.
rewrite (divGI_eq y (q1 * d)) Hq2 mulrA -mulrDl.
by set u := _ + _; exists u.
Qed.
Lemma dvdGI_gcdlr x y : (gcdGI x y %| x) && (gcdGI x y %| y).
Proof.
elim: {x y}minn {-2}x {-2}y (leqnn (minn ('N y) ('N x))) => [x y|r IH x y].
rewrite geq_min !leqn0 !normGI_eq0.
by case/pred2P=>->;
rewrite (gcd0GI, gcdGI0) dvdGIxx ?andbT dvdGI0.
case: (eqVneq x 0) => [-> _|nZx].
by rewrite gcd0GI dvdGIxx andbT dvdGI0.
case: (eqVneq y 0) => [->|nZy].
by rewrite gcdGI0 dvdGIxx /= dvdGI0.
rewrite gcdGIE minnC /minn; case: ltnP => [lt_xy | le_xy] le_yr.
suffices /IH/andP[E1 E2]: (minn ('N x) ('N (y %% x)%GI) <= r)%N.
by rewrite E2 (dvdGI_mod _ E2).
by rewrite geq_min orbC -ltnS (leq_trans _ le_yr) ?ltn_modGI.
suffices /IH/andP[E1 E2] : (minn ('N y) ('N (x %% y)%GI) <= r)%N.
by rewrite E2 andbT (dvdGI_mod _ E2).
by rewrite geq_min orbC -ltnS (leq_trans _ le_yr) ?ltn_modGI.
Qed.
Lemma dvdGI_gcdl x y : gcdGI x y %| x.
Proof. by case/andP: (dvdGI_gcdlr x y). Qed.
Lemma dvdGI_gcdr x y : gcdGI x y %| y.
Proof. by case/andP: (dvdGI_gcdlr x y). Qed.
Lemma gcdGI_eq0 x y : (gcdGI x y == 0) = ((x == 0) && (y == 0)).
Proof.
have [/eqP->|/eqP nZx] := boolP (x == 0).
by rewrite gcd0GI.
have := dvdGI_gcdl x y.
by case:eqP => //->; rewrite dvd0GI => /eqP.
Qed.
Lemma dvdGI_leq x y : y != 0 -> x %| y -> ('N x <= 'N y)%N.
Proof.
move=> nZy /dvdGIP[q qE]; have := nZy.
rewrite qE -normGI_eq0 normGIM muln_eq0 negb_or => /andP[H1 H2].
by rewrite -[X in (X <= _)%N] mul1n leq_pmul2r lt0n.
Qed.
Lemma leq_gcdGIl (x y : GI) : x != 0 -> ('N (gcdGI x y) <= 'N x)%N.
Proof. by move=> nZx; apply: dvdGI_leq => //; exact: dvdGI_gcdl. Qed.
Lemma leq_gcdGIr (x y : GI) : y != 0 -> ('N (gcdGI x y) <= 'N y)%N.
Proof. by move=> nZy; move: (dvdGI_gcdr x y); apply: dvdGI_leq. Qed.
Lemma dvdGI_trans : transitive dvdGI.
Proof.
move=> x y z /dvdGIP[qx -> /dvdGIP[qy ->]].
by apply/dvdGIP; exists (qy * qx); rewrite mulrA.
Qed.
Lemma dvdGI_gcd x y z : x %| gcdGI y z = (x %| y) && (x %| z).
Proof.
apply/idP/andP=> [dv_xyz | [dv_xy dv_xz]].
by rewrite ?(dvdGI_trans dv_xyz) ?dvdGI_gcdl ?dvdGI_gcdr.
move: (leqnn (minn ('N z) ('N y))) dv_xy dv_xz.
elim: {y z}minn {-2}y {-2}z => [|r IH] y z.
rewrite geq_min !leqn0 !normGI_eq0.
by case/pred2P=> ->; rewrite ?(gcd0GI, gcdGI0).
case: (eqVneq y 0) => [-> _|nZy]; first by rewrite gcd0GI.
case: (eqVneq z 0) => [->|nZz]; first by rewrite gcdGI0.
rewrite gcdGIE minnC /minn; case: ltnP => Czy le_r dv_y dv_z.
apply: IH => //; last by rewrite -(dvdGI_mod _ dv_y).
by rewrite geq_min orbC -ltnS (leq_trans _ le_r) ?ltn_modGI.
apply: IH => //; last by rewrite -(dvdGI_mod _ dv_z).
by rewrite geq_min orbC -ltnS (leq_trans _ le_r) ?ltn_modGI.
Qed.