|
| 1 | +From Coq Require Import ZArith ZifyClasses Zify ZifyInst ZifyBool. |
| 2 | +From Coq Require Export Lia. |
| 3 | + |
| 4 | +From mathcomp Require Import ssreflect ssrfun ssrbool eqtype ssrnat seq path. |
| 5 | +From mathcomp Require Import div choice fintype tuple finfun bigop finset prime. |
| 6 | +From mathcomp Require Import order binomial ssralg countalg ssrnum ssrint. |
| 7 | +From mathcomp Require Import zify_ssreflect. |
| 8 | + |
| 9 | +Set Implicit Arguments. |
| 10 | +Unset Strict Implicit. |
| 11 | +Unset Printing Implicit Defensive. |
| 12 | + |
| 13 | +Import Order.Theory GRing.Theory Num.Theory. |
| 14 | + |
| 15 | +Definition int_of_Z (n : Z) : int := |
| 16 | + match n with |
| 17 | + | Z0 => Posz 0 |
| 18 | + | Zpos p => Posz (Pos.to_nat p) |
| 19 | + | Zneg p => Negz (Pos.to_nat p).-1 |
| 20 | + end. |
| 21 | + |
| 22 | +Definition Z_of_int (n : int) : Z := |
| 23 | + match n with |
| 24 | + | Posz n => Z.of_nat n |
| 25 | + | Negz n' => Z.opp (Z.of_nat (n' + 1)) |
| 26 | + end. |
| 27 | + |
| 28 | +Lemma int_of_ZK : cancel int_of_Z Z_of_int. |
| 29 | +Proof. by case=> //= p; lia. Qed. |
| 30 | + |
| 31 | +Lemma Z_of_intK : cancel Z_of_int int_of_Z. |
| 32 | +Proof. |
| 33 | +case=> [[|n]|n] //=. |
| 34 | + congr Posz; lia. |
| 35 | +rewrite addnC /=; congr Negz; lia. |
| 36 | +Qed. |
| 37 | + |
| 38 | +Module ZInstances. |
| 39 | + |
| 40 | +Implicit Types (m n : Z). |
| 41 | + |
| 42 | +Fact eqZP : Equality.axiom Z.eqb. |
| 43 | +Proof. by move=> x y; apply: (iffP idP); lia. Qed. |
| 44 | + |
| 45 | +Canonical Z_eqType := EqType Z (EqMixin eqZP). |
| 46 | +Canonical Z_choiceType := ChoiceType Z (CanChoiceMixin int_of_ZK). |
| 47 | +Canonical Z_countType := CountType Z (CanCountMixin int_of_ZK). |
| 48 | + |
| 49 | +Definition Z_zmodMixin := |
| 50 | + ZmodMixin Zplus_assoc Zplus_comm Zplus_0_l Zplus_opp_l. |
| 51 | +Canonical Z_zmodType := ZmodType Z Z_zmodMixin. |
| 52 | + |
| 53 | +Definition Z_ringMixin := |
| 54 | + RingMixin |
| 55 | + Zmult_assoc Zmult_1_l Zmult_1_r Zmult_plus_distr_l Zmult_plus_distr_r isT. |
| 56 | +Canonical Z_ringType := RingType Z Z_ringMixin. |
| 57 | +Canonical Z_comRingType := ComRingType Z Zmult_comm. |
| 58 | + |
| 59 | +Definition unitZ := [qualify a n : Z | (n == Z.pos xH) || (n == Z.neg xH)]. |
| 60 | +Definition invZ n := n. |
| 61 | + |
| 62 | +Fact mulVZ : {in unitZ, left_inverse 1%R invZ *%R}. |
| 63 | +Proof. by move=> n /pred2P[] ->. Qed. |
| 64 | + |
| 65 | +Fact unitZPl m n : (n * m = 1)%R -> m \is a unitZ. |
| 66 | +Proof. case: m n => [|[m|m|]|[m|m|]] [|n|n] //= []; lia. Qed. |
| 67 | + |
| 68 | +Fact invZ_out : {in [predC unitZ], invZ =1 id}. |
| 69 | +Proof. exact. Qed. |
| 70 | + |
| 71 | +Fact idomain_axiomZ m n : (m * n = 0)%R -> (m == 0%R) || (n == 0%R). |
| 72 | +Proof. by case: m n => [|m|m] []. Qed. |
| 73 | + |
| 74 | +Canonical Z_unitRingType := |
| 75 | + UnitRingType Z (ComUnitRingMixin mulVZ unitZPl invZ_out). |
| 76 | +Canonical Z_comUnitRing := [comUnitRingType of Z]. |
| 77 | +Canonical Z_idomainType := IdomainType Z idomain_axiomZ. |
| 78 | + |
| 79 | +Canonical Z_countZmodType := [countZmodType of Z]. |
| 80 | +Canonical Z_countRingType := [countRingType of Z]. |
| 81 | +Canonical Z_countComRingType := [countComRingType of Z]. |
| 82 | +Canonical Z_countUnitRingType := [countUnitRingType of Z]. |
| 83 | +Canonical Z_countComUnitRingType := [countComUnitRingType of Z]. |
| 84 | +Canonical Z_countIdomainType := [countIdomainType of Z]. |
| 85 | + |
| 86 | +Fact leZ_add m n : Z.leb 0 m -> Z.leb 0 n -> Z.leb 0 (m + n). Proof. lia. Qed. |
| 87 | +Fact leZ_mul m n : Z.leb 0 m -> Z.leb 0 n -> Z.leb 0 (m * n). Proof. lia. Qed. |
| 88 | +Fact leZ_anti m : Z.leb 0 m -> Z.leb m 0 -> m = Z0. Proof. lia. Qed. |
| 89 | + |
| 90 | +Fact subZ_ge0 m n : Z.leb 0 (n - m)%R = Z.leb m n. |
| 91 | +Proof. by rewrite /GRing.add /GRing.opp /=; lia. Qed. |
| 92 | + |
| 93 | +Fact leZ_total m n : Z.leb m n || Z.leb n m. Proof. lia. Qed. |
| 94 | + |
| 95 | +Fact normZN m : Z.abs (- m) = Z.abs m. Proof. lia. Qed. |
| 96 | + |
| 97 | +Fact geZ0_norm m : Z.leb 0 m -> Z.abs m = m. Proof. lia. Qed. |
| 98 | + |
| 99 | +Fact ltZ_def m n : (Z.ltb m n) = (n != m) && (Z.leb m n). |
| 100 | +Proof. by rewrite eqE /=; lia. Qed. |
| 101 | + |
| 102 | +Definition Mixin : realLeMixin [idomainType of Z] := |
| 103 | + RealLeMixin |
| 104 | + leZ_add leZ_mul leZ_anti subZ_ge0 (leZ_total 0) normZN geZ0_norm ltZ_def. |
| 105 | + |
| 106 | +Canonical Z_porderType := POrderType ring_display Z Mixin. |
| 107 | +Canonical Z_latticeType := LatticeType Z Mixin. |
| 108 | +Canonical Z_distrLatticeType := DistrLatticeType Z Mixin. |
| 109 | +Canonical Z_orderType := OrderType Z leZ_total. |
| 110 | +Canonical Z_numDomainType := NumDomainType Z Mixin. |
| 111 | +Canonical Z_normedZmodType := NormedZmodType Z Z Mixin. |
| 112 | +Canonical Z_realDomainType := [realDomainType of Z]. |
| 113 | + |
| 114 | +Fact Z_of_intE (n : int) : Z_of_int n = (n%:~R)%R. |
| 115 | +Proof. |
| 116 | +have Hnat (m : nat) : Z.of_nat m = (m%:R)%R. |
| 117 | + by elim: m => // m; rewrite Nat2Z.inj_succ -Z.add_1_l mulrS => ->. |
| 118 | +case: n => n; rewrite /intmul /=; first exact: Hnat. |
| 119 | +by congr Z.opp; rewrite Nat2Z.inj_add /= mulrSr Hnat. |
| 120 | +Qed. |
| 121 | + |
| 122 | +Fact Z_of_int_is_additive : additive Z_of_int. |
| 123 | +Proof. by move=> m n; rewrite !Z_of_intE raddfB. Qed. |
| 124 | + |
| 125 | +Canonical Z_of_int_additive := Additive Z_of_int_is_additive. |
| 126 | + |
| 127 | +Fact int_of_Z_is_additive : additive int_of_Z. |
| 128 | +Proof. exact: can2_additive Z_of_intK int_of_ZK. Qed. |
| 129 | + |
| 130 | +Canonical int_of_Z_additive := Additive int_of_Z_is_additive. |
| 131 | + |
| 132 | +Fact Z_of_int_is_multiplicative : multiplicative Z_of_int. |
| 133 | +Proof. by split => // n m; rewrite !Z_of_intE rmorphM. Qed. |
| 134 | + |
| 135 | +Canonical Z_of_int_rmorphism := AddRMorphism Z_of_int_is_multiplicative. |
| 136 | + |
| 137 | +Fact int_of_Z_is_multiplicative : multiplicative int_of_Z. |
| 138 | +Proof. exact: can2_rmorphism Z_of_intK int_of_ZK. Qed. |
| 139 | + |
| 140 | +Canonical int_of_Z_rmorphism := AddRMorphism int_of_Z_is_multiplicative. |
| 141 | + |
| 142 | +End ZInstances. |
| 143 | + |
| 144 | +Canonical ZInstances.Z_eqType. |
| 145 | +Canonical ZInstances.Z_choiceType. |
| 146 | +Canonical ZInstances.Z_countType. |
| 147 | +Canonical ZInstances.Z_zmodType. |
| 148 | +Canonical ZInstances.Z_ringType. |
| 149 | +Canonical ZInstances.Z_comRingType. |
| 150 | +Canonical ZInstances.Z_unitRingType. |
| 151 | +Canonical ZInstances.Z_comUnitRing. |
| 152 | +Canonical ZInstances.Z_idomainType. |
| 153 | +Canonical ZInstances.Z_countZmodType. |
| 154 | +Canonical ZInstances.Z_countRingType. |
| 155 | +Canonical ZInstances.Z_countComRingType. |
| 156 | +Canonical ZInstances.Z_countUnitRingType. |
| 157 | +Canonical ZInstances.Z_countComUnitRingType. |
| 158 | +Canonical ZInstances.Z_countIdomainType. |
| 159 | +Canonical ZInstances.Z_porderType. |
| 160 | +Canonical ZInstances.Z_latticeType. |
| 161 | +Canonical ZInstances.Z_distrLatticeType. |
| 162 | +Canonical ZInstances.Z_orderType. |
| 163 | +Canonical ZInstances.Z_numDomainType. |
| 164 | +Canonical ZInstances.Z_normedZmodType. |
| 165 | +Canonical ZInstances.Z_realDomainType. |
| 166 | +Canonical ZInstances.Z_of_int_additive. |
| 167 | +Canonical ZInstances.int_of_Z_additive. |
| 168 | +Canonical ZInstances.Z_of_int_rmorphism. |
| 169 | +Canonical ZInstances.int_of_Z_rmorphism. |
0 commit comments