| 1 |
From Coq Require Ncring. |
| 2 |
From Coq Require Cring. |
| 3 |
From Coq Require Import Morphisms. |
| 4 |
From Coq Require Import Lia. |
| 5 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 6 |
Require Import Crypto.Util.Tactics.OnSubterms. |
| 7 |
Require Import Crypto.Util.Tactics.Revert. |
| 8 |
Require Import Crypto.Util.Tactics.RewriteHyp. |
| 9 |
Require Import Crypto.Algebra.Hierarchy Crypto.Algebra.Group Crypto.Algebra.Monoid. |
| 10 |
From Coq Require ZArith PArith. |
| 11 |
|
| 12 |
|
| 13 |
Section Ring. |
| 14 |
Context {T eq zero one opp add sub mul} `{@ring T eq zero one opp add sub mul}. |
| 15 |
Local Infix "=" := eq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 16 |
Local Notation "0" := zero. Local Notation "1" := one. |
| 17 |
Local Infix "+" := add. Local Infix "-" := sub. Local Infix "*" := mul. |
| 18 |
|
| 19 |
Global Instance Ncring_Ring_ops : @Ncring.Ring_ops T zero one add mul sub opp eq := {}. |
| 20 |
Global Instance Ncring_Ring : @Ncring.Ring T zero one add mul sub opp eq Ncring_Ring_ops. |
| 21 |
Proof using Type*. |
| 22 |
split; exact _ || cbv; intros; |
| 23 |
pose left_identity; pose right_identity; pose commutative; pose associative; pose right_inverse; pose left_distributive; pose right_distributive; pose ring_sub_definition; |
| 24 |
eauto with core typeclass_instances. |
| 25 |
- (* TODO: why does [eauto using @left_identity with typeclass_instances] not work? *) |
| 26 |
eapply @left_identity; eauto with typeclass_instances. |
| 27 |
- eapply @right_identity; eauto with typeclass_instances. |
| 28 |
- eapply associative. |
| 29 |
Qed. |
| 30 |
|
| 31 |
|
| 32 |
Lemma mul_0_l : forall x, 0 * x = 0. |
| 33 |
Proof using Type*. apply Ncring.ring_mul_0_l. Qed. |
| 34 |
|
| 35 |
Lemma mul_0_r : forall x, x * 0 = 0. |
| 36 |
Proof using Type*. apply Ncring.ring_mul_0_r. Qed. |
| 37 |
|
| 38 |
Lemma sub_0_l x : 0 - x = opp x. |
| 39 |
Proof using Type*. rewrite ring_sub_definition. rewrite left_identity. reflexivity. Qed. |
| 40 |
|
| 41 |
Lemma mul_opp_r x y : x * opp y = opp (x * y). |
| 42 |
Proof using Type*. symmetry. apply Ncring.ring_opp_mul_r. Qed. |
| 43 |
|
| 44 |
Lemma mul_opp_l x y : opp x * y = opp (x * y). |
| 45 |
Proof using Type*. symmetry. apply Ncring.ring_opp_mul_l. Qed. |
| 46 |
|
| 47 |
Definition opp_zero_iff : forall x, opp x = 0 <-> x = 0 := Group.inv_id_iff. |
| 48 |
|
| 49 |
Global Instance is_left_distributive_sub : is_left_distributive (eq:=eq)(add:=sub)(mul:=mul). |
| 50 |
Proof using Type*. |
| 51 |
split; intros. rewrite !ring_sub_definition, left_distributive. |
| 52 |
eapply Group.cancel_left, mul_opp_r. |
| 53 |
Qed. |
| 54 |
|
| 55 |
Global Instance is_right_distributive_sub : is_right_distributive (eq:=eq)(add:=sub)(mul:=mul). |
| 56 |
Proof using Type*. |
| 57 |
split; intros. rewrite !ring_sub_definition, right_distributive. |
| 58 |
eapply Group.cancel_left, mul_opp_l. |
| 59 |
Qed. |
| 60 |
|
| 61 |
Lemma sub_zero_iff x y : x - y = 0 <-> x = y. |
| 62 |
Proof using Type*. |
| 63 |
split; intro E. |
| 64 |
{ rewrite <-(right_identity y), <- E, ring_sub_definition. |
| 65 |
rewrite commutative, <-associative, commutative. |
| 66 |
rewrite left_inverse, left_identity. reflexivity. } |
| 67 |
{ rewrite E, ring_sub_definition, right_inverse; reflexivity. } |
| 68 |
Qed. |
| 69 |
|
| 70 |
Lemma neq_sub_neq_zero x y (Hxy:x<>y) : x-y <> 0. |
| 71 |
Proof using Type*. |
| 72 |
intro Hsub. apply Hxy. rewrite <-(left_identity y), <-Hsub, ring_sub_definition. |
| 73 |
rewrite <-associative, left_inverse, right_identity. reflexivity. |
| 74 |
Qed. |
| 75 |
|
| 76 |
Lemma zero_product_iff_zero_factor {Hzpzf:@is_zero_product_zero_factor T eq zero mul} : |
| 77 |
forall x y : T, eq (mul x y) zero <-> eq x zero \/ eq y zero. |
| 78 |
Proof using Type*. |
| 79 |
split; eauto using zero_product_zero_factor; []. |
| 80 |
intros [Hz|Hz]; rewrite Hz; eauto using mul_0_l, mul_0_r. |
| 81 |
Qed. |
| 82 |
|
| 83 |
Lemma nonzero_product_iff_nonzero_factor {Hzpzf:@is_zero_product_zero_factor T eq zero mul} : |
| 84 |
forall x y : T, not (eq (mul x y) zero) <-> (not (eq x zero) /\ not (eq y zero)). |
| 85 |
Proof using Type*. intros; rewrite zero_product_iff_zero_factor; tauto. Qed. |
| 86 |
|
| 87 |
|
| 88 |
End Ring. |
| 89 |
|
| 90 |
Section Homomorphism. |
| 91 |
Context {R EQ ZERO ONE OPP ADD SUB MUL} `{@ring R EQ ZERO ONE OPP ADD SUB MUL}. |
| 92 |
Context {S eq zero one opp add sub mul} `{@ring S eq zero one opp add sub mul}. |
| 93 |
Context {phi:R->S}. |
| 94 |
Local Infix "=" := eq. Local Infix "=" := eq : type_scope. |
| 95 |
|
| 96 |
Class is_homomorphism := |
| 97 |
{ |
| 98 |
homomorphism_is_homomorphism : Monoid.is_homomorphism (phi:=phi) (OP:=ADD) (op:=add) (EQ:=EQ) (eq:=eq); |
| 99 |
homomorphism_mul : forall x y, phi (MUL x y) = mul (phi x) (phi y); |
| 100 |
homomorphism_one : phi ONE = one |
| 101 |
}. |
| 102 |
Global Existing Instance homomorphism_is_homomorphism. |
| 103 |
|
| 104 |
Context `{phi_homom:is_homomorphism}. |
| 105 |
|
| 106 |
Lemma homomorphism_zero : phi ZERO = zero. |
| 107 |
Proof using Type*. apply Group.homomorphism_id. Qed. |
| 108 |
|
| 109 |
Lemma homomorphism_add : forall x y, phi (ADD x y) = add (phi x) (phi y). |
| 110 |
Proof using phi_homom. apply Monoid.homomorphism. Qed. |
| 111 |
|
| 112 |
Definition homomorphism_opp : forall x, phi (OPP x) = opp (phi x) := |
| 113 |
(Group.homomorphism_inv (INV:=OPP) (inv:=opp)). |
| 114 |
|
| 115 |
Lemma homomorphism_sub : forall x y, phi (SUB x y) = sub (phi x) (phi y). |
| 116 |
Proof using Type*. |
| 117 |
intros. |
| 118 |
rewrite !ring_sub_definition, Monoid.homomorphism, homomorphism_opp. reflexivity. |
| 119 |
Qed. |
| 120 |
|
| 121 |
Global Instance monoid_homomorphism_mul : |
| 122 |
Monoid.is_homomorphism (phi:=phi) (OP:=MUL) (op:=mul) (EQ:=EQ) (eq:=eq). |
| 123 |
Proof using phi_homom. split; destruct phi_homom; assumption || exact _. Qed. |
| 124 |
End Homomorphism. |
| 125 |
|
| 126 |
(* TODO: file a Coq bug for rewrite_strat -- it should accept ltac variables *) |
| 127 |
Ltac push_homomorphism phi := |
| 128 |
let H := constr:(_ : @is_homomorphism _ _ _ _ _ _ _ _ _ _ phi) in |
| 129 |
pose proof (@homomorphism_zero _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ H) |
| 130 |
as _push_homomrphism_0; |
| 131 |
pose proof (@homomorphism_one _ _ _ _ _ _ _ _ _ _ _ H) |
| 132 |
as _push_homomrphism_1; |
| 133 |
pose proof (@homomorphism_add _ _ _ _ _ _ _ _ _ _ _ H) |
| 134 |
as _push_homomrphism_p; |
| 135 |
pose proof (@homomorphism_opp _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ H) |
| 136 |
as _push_homomrphism_o; |
| 137 |
pose proof (@homomorphism_sub _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ H) |
| 138 |
as _push_homomrphism_s; |
| 139 |
pose proof (@homomorphism_mul _ _ _ _ _ _ _ _ _ _ _ H) |
| 140 |
as _push_homomrphism_m; |
| 141 |
(rewrite_strat bottomup (terms _push_homomrphism_0 _push_homomrphism_1 _push_homomrphism_p _push_homomrphism_o _push_homomrphism_s _push_homomrphism_m)); |
| 142 |
clear _push_homomrphism_0 _push_homomrphism_1 _push_homomrphism_p _push_homomrphism_o _push_homomrphism_s _push_homomrphism_m. |
| 143 |
|
| 144 |
Ltac pull_homomorphism phi := |
| 145 |
let H := constr:(_ : @is_homomorphism _ _ _ _ _ _ _ _ _ _ phi) in |
| 146 |
pose proof (@homomorphism_zero _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ H) |
| 147 |
as _pull_homomrphism_0; |
| 148 |
pose proof (@homomorphism_one _ _ _ _ _ _ _ _ _ _ _ H) |
| 149 |
as _pull_homomrphism_1; |
| 150 |
pose proof (@homomorphism_add _ _ _ _ _ _ _ _ _ _ _ H) |
| 151 |
as _pull_homomrphism_p; |
| 152 |
pose proof (@homomorphism_opp _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ H) |
| 153 |
as _pull_homomrphism_o; |
| 154 |
pose proof (@homomorphism_sub _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ H) |
| 155 |
as _pull_homomrphism_s; |
| 156 |
pose proof (@homomorphism_mul _ _ _ _ _ _ _ _ _ _ _ H) |
| 157 |
as _pull_homomrphism_m; |
| 158 |
symmetry in _pull_homomrphism_0; |
| 159 |
symmetry in _pull_homomrphism_1; |
| 160 |
symmetry in _pull_homomrphism_p; |
| 161 |
symmetry in _pull_homomrphism_o; |
| 162 |
symmetry in _pull_homomrphism_s; |
| 163 |
symmetry in _pull_homomrphism_m; |
| 164 |
(rewrite_strat bottomup (terms _pull_homomrphism_0 _pull_homomrphism_1 _pull_homomrphism_p _pull_homomrphism_o _pull_homomrphism_s _pull_homomrphism_m)); |
| 165 |
clear _pull_homomrphism_0 _pull_homomrphism_1 _pull_homomrphism_p _pull_homomrphism_o _pull_homomrphism_s _pull_homomrphism_m. |
| 166 |
|
| 167 |
|
| 168 |
Section Isomorphism. |
| 169 |
Context {F EQ ZERO ONE OPP ADD SUB MUL} {ringF:@ring F EQ ZERO ONE OPP ADD SUB MUL}. |
| 170 |
Context {H} {eq : H -> H -> Prop} {zero one : H} {opp : H -> H} {add sub mul : H -> H -> H} {inv : H -> H} {div : H -> H -> H}. |
| 171 |
Context {phi:F->H} {phi':H->F}. |
| 172 |
Local Infix "=" := EQ. Local Infix "=" := EQ : type_scope. |
| 173 |
Context (phi'_phi_id : forall A, phi' (phi A) = A) |
| 174 |
(phi'_eq : forall a b, EQ (phi' a) (phi' b) <-> eq a b) |
| 175 |
{phi'_zero : phi' zero = ZERO} |
| 176 |
{phi'_one : phi' one = ONE} |
| 177 |
{phi'_opp : forall a, phi' (opp a) = OPP (phi' a)} |
| 178 |
(phi'_add : forall a b, phi' (add a b) = ADD (phi' a) (phi' b)) |
| 179 |
(phi'_sub : forall a b, phi' (sub a b) = SUB (phi' a) (phi' b)) |
| 180 |
(phi'_mul : forall a b, phi' (mul a b) = MUL (phi' a) (phi' b)). |
| 181 |
|
| 182 |
Lemma ring_by_isomorphism |
| 183 |
: @ring H eq zero one opp add sub mul |
| 184 |
/\ @is_homomorphism F EQ ONE ADD MUL H eq one add mul phi |
| 185 |
/\ @is_homomorphism H eq one add mul F EQ ONE ADD MUL phi'. |
| 186 |
Proof using phi'_add phi'_eq phi'_mul phi'_one phi'_opp phi'_phi_id phi'_sub phi'_zero ringF. |
| 187 |
repeat match goal with |
| 188 |
| [ H : field |- _ ] => destruct H; try clear H |
| 189 |
| [ H : commutative_ring |- _ ] => destruct H; try clear H |
| 190 |
| [ H : ring |- _ ] => destruct H; try clear H |
| 191 |
| [ H : commutative_group |- _ ] => destruct H; try clear H |
| 192 |
| [ H : group |- _ ] => destruct H; try clear H |
| 193 |
| [ H : monoid |- _ ] => destruct H; try clear H |
| 194 |
| [ H : is_commutative |- _ ] => destruct H; try clear H |
| 195 |
| [ H : is_left_distributive |- _ ] => destruct H; try clear H |
| 196 |
| [ H : is_right_distributive |- _ ] => destruct H; try clear H |
| 197 |
| [ H : is_zero_neq_one |- _ ] => destruct H; try clear H |
| 198 |
| [ H : is_associative |- _ ] => destruct H; try clear H |
| 199 |
| [ H : is_left_identity |- _ ] => destruct H; try clear H |
| 200 |
| [ H : is_right_identity |- _ ] => destruct H; try clear H |
| 201 |
| [ H : Equivalence _ |- _ ] => destruct H; try clear H |
| 202 |
| [ H : is_left_inverse |- _ ] => destruct H; try clear H |
| 203 |
| [ H : is_right_inverse |- _ ] => destruct H; try clear H |
| 204 |
| _ => intro |
| 205 |
| _ => split |
| 206 |
| [ H : eq _ _ |- _ ] => apply phi'_eq in H |
| 207 |
| [ |- eq _ _ ] => apply phi'_eq |
| 208 |
| [ H : (~eq _ _)%type |- _ ] => pose proof (fun pf => H (proj1 (@phi'_eq _ _) pf)); clear H |
| 209 |
| [ H : EQ _ _ |- _ ] => rewrite H |
| 210 |
| _ => progress erewrite ?phi'_zero, ?phi'_one, ?phi'_opp, ?phi'_add, ?phi'_sub, ?phi'_mul, ?phi'_phi_id by reflexivity |
| 211 |
| [ H : _ |- _ ] => progress erewrite ?phi'_zero, ?phi'_one, ?phi'_opp, ?phi'_add, ?phi'_sub, ?phi'_mul, ?phi'_phi_id in H by reflexivity |
| 212 |
| _ => solve [ eauto ] |
| 213 |
end. |
| 214 |
Qed. |
| 215 |
End Isomorphism. |
| 216 |
|
| 217 |
Section TacticSupportCommutative. |
| 218 |
Context {T eq zero one opp add sub mul} `{@commutative_ring T eq zero one opp add sub mul}. |
| 219 |
|
| 220 |
Global Instance Cring_Cring_commutative_ring : |
| 221 |
@Cring.Cring T zero one add mul sub opp eq Ncring_Ring_ops Ncring_Ring. |
| 222 |
Proof using Type. unfold Cring.Cring; intros; cbv. eapply commutative. Qed. |
| 223 |
|
| 224 |
Lemma ring_theory_for_stdlib_tactic : Ring_theory.ring_theory zero one add mul sub opp eq. |
| 225 |
Proof using Type*. |
| 226 |
constructor; intros. (* TODO(automation): make [auto] do this? *) |
| 227 |
- apply left_identity. |
| 228 |
- apply commutative. |
| 229 |
- apply associative. |
| 230 |
- apply left_identity. |
| 231 |
- apply commutative. |
| 232 |
- apply associative. |
| 233 |
- apply right_distributive. |
| 234 |
- apply ring_sub_definition. |
| 235 |
- apply right_inverse. |
| 236 |
Qed. |
| 237 |
End TacticSupportCommutative. |
| 238 |
|
| 239 |
Section Z. |
| 240 |
Import ZArith. |
| 241 |
Global Instance ring_Z : @ring Z Logic.eq 0%Z 1%Z Z.opp Z.add Z.sub Z.mul. |
| 242 |
Proof. repeat split; auto using Z.eq_dec with zarith typeclass_instances. Qed. |
| 243 |
|
| 244 |
Global Instance commutative_ring_Z : @commutative_ring Z Logic.eq 0%Z 1%Z Z.opp Z.add Z.sub Z.mul. |
| 245 |
Proof. eauto using @commutative_ring, @is_commutative, ring_Z with zarith. Qed. |
| 246 |
|
| 247 |
Global Instance integral_domain_Z : @integral_domain Z Logic.eq 0%Z 1%Z Z.opp Z.add Z.sub Z.mul. |
| 248 |
Proof. |
| 249 |
split. |
| 250 |
{ apply commutative_ring_Z. } |
| 251 |
{ split. intros. eapply Z.eq_mul_0; assumption. } |
| 252 |
{ split. discriminate. } |
| 253 |
Qed. |
| 254 |
End Z. |
| 255 |
|
| 256 |
Section of_Z. |
| 257 |
Import ZArith PArith. Local Open Scope Z_scope. |
| 258 |
Context {R Req Rzero Rone Ropp Radd Rsub Rmul} |
| 259 |
{Rring : @ring R Req Rzero Rone Ropp Radd Rsub Rmul}. |
| 260 |
Local Infix "=" := Req. Local Infix "=" := Req : type_scope. |
| 261 |
|
| 262 |
Fixpoint of_nat (n:nat) : R := |
| 263 |
match n with |
| 264 |
| O => Rzero |
| 265 |
| S n' => Radd (of_nat n') Rone |
| 266 |
end. |
| 267 |
Definition of_Z (x:Z) : R := |
| 268 |
match x with |
| 269 |
| Z0 => Rzero |
| 270 |
| Zpos p => of_nat (Pos.to_nat p) |
| 271 |
| Zneg p => Ropp (of_nat (Pos.to_nat p)) |
| 272 |
end. |
| 273 |
|
| 274 |
Lemma of_Z_0 : of_Z 0 = Rzero. |
| 275 |
Proof using Type*. reflexivity. Qed. |
| 276 |
|
| 277 |
Lemma of_nat_add x : |
| 278 |
of_nat (Nat.add x 1) = Radd (of_nat x) Rone. |
| 279 |
Proof using Type*. destruct x; rewrite ?Nat.add_1_r; reflexivity. Qed. |
| 280 |
|
| 281 |
Lemma of_nat_sub x (H: (0 < x)%nat): |
| 282 |
of_nat (Nat.sub x 1) = Rsub (of_nat x) Rone. |
| 283 |
Proof using Type*. |
| 284 |
induction x; [lia|simpl]. |
| 285 |
rewrite <-of_nat_add. |
| 286 |
rewrite Nat.sub_0_r, Nat.add_1_r. |
| 287 |
simpl of_nat. |
| 288 |
rewrite ring_sub_definition, <-associative. |
| 289 |
rewrite right_inverse, right_identity. |
| 290 |
reflexivity. |
| 291 |
Qed. |
| 292 |
|
| 293 |
Lemma of_Z_add_1_r : |
| 294 |
forall x, of_Z (Z.add x 1) = Radd (of_Z x) Rone. |
| 295 |
Proof using Type*. |
| 296 |
destruct x; [reflexivity| | ]; simpl of_Z. |
| 297 |
{ rewrite Pos2Nat.inj_add, of_nat_add. |
| 298 |
reflexivity. } |
| 299 |
{ rewrite Z.pos_sub_spec; break_match; |
| 300 |
match goal with |
| 301 |
| H : _ |- _ => rewrite Pos.compare_eq_iff in H |
| 302 |
| H : _ |- _ => rewrite Pos.compare_lt_iff in H |
| 303 |
| H : _ |- _ => rewrite Pos.compare_gt_iff in H; |
| 304 |
apply Pos.nlt_1_r in H; tauto |
| 305 |
end; |
| 306 |
subst; simpl of_Z; simpl of_nat. |
| 307 |
{ rewrite left_identity, left_inverse; reflexivity. } |
| 308 |
{ rewrite Pos2Nat.inj_sub by assumption. |
| 309 |
rewrite of_nat_sub by apply Pos2Nat.is_pos. |
| 310 |
rewrite ring_sub_definition, Group.inv_op, Group.inv_inv. |
| 311 |
rewrite commutative; reflexivity. } } |
| 312 |
Qed. |
| 313 |
|
| 314 |
Lemma of_Z_sub_1_r : |
| 315 |
forall x, of_Z (Z.sub x 1) = Rsub (of_Z x) Rone. |
| 316 |
Proof using Type*. |
| 317 |
induction x as [|p|]. |
| 318 |
{ simpl; rewrite ring_sub_definition, !left_identity; |
| 319 |
reflexivity. } |
| 320 |
{ case_eq (1 ?= p)%positive; intros; |
| 321 |
match goal with |
| 322 |
| H : _ |- _ => rewrite Pos.compare_eq_iff in H |
| 323 |
| H : _ |- _ => rewrite Pos.compare_lt_iff in H |
| 324 |
| H : _ |- _ => rewrite Pos.compare_gt_iff in H; |
| 325 |
apply Pos.nlt_1_r in H; tauto |
| 326 |
end. |
| 327 |
{ subst. simpl; rewrite ring_sub_definition, !left_identity, |
| 328 |
right_inverse; reflexivity. } |
| 329 |
{ rewrite <-Pos2Z.inj_sub by assumption; simpl of_Z. |
| 330 |
rewrite Pos2Nat.inj_sub by assumption. |
| 331 |
rewrite of_nat_sub by apply Pos2Nat.is_pos. |
| 332 |
reflexivity. } } |
| 333 |
{ simpl. rewrite Pos2Nat.inj_add, of_nat_add. |
| 334 |
rewrite ring_sub_definition, Group.inv_op, commutative. |
| 335 |
reflexivity. } |
| 336 |
Qed. |
| 337 |
|
| 338 |
Lemma of_Z_opp : forall a, |
| 339 |
of_Z (Z.opp a) = Ropp (of_Z a). |
| 340 |
Proof using Type*. |
| 341 |
destruct a; simpl; rewrite ?Group.inv_id, ?Group.inv_inv; |
| 342 |
reflexivity. |
| 343 |
Qed. |
| 344 |
|
| 345 |
Lemma of_Z_add : forall a b, |
| 346 |
of_Z (Z.add a b) = Radd (of_Z a) (of_Z b). |
| 347 |
Proof using Type*. |
| 348 |
intros a b. |
| 349 |
let x := match goal with |- ?x => x end in |
| 350 |
let f := match (eval pattern b in x) with ?f _ => f end in |
| 351 |
apply (Z.peano_ind f); intros. |
| 352 |
{ rewrite !right_identity. reflexivity. } |
| 353 |
{ match goal with |
| 354 |
| [ |- context[?a + Z.succ ?x'] ] |
| 355 |
=> rename x' into x |
| 356 |
end. |
| 357 |
replace (a + Z.succ x) with ((a + x) + 1) by ring. |
| 358 |
replace (Z.succ x) with (x+1) by ring. |
| 359 |
rewrite !of_Z_add_1_r; rewrite_hyp *. |
| 360 |
rewrite associative; reflexivity. } |
| 361 |
{ match goal with |
| 362 |
| [ |- context[?a + Z.pred ?x'] ] |
| 363 |
=> rename x' into x |
| 364 |
end. |
| 365 |
replace (a + Z.pred x) with ((a+x)-1) |
| 366 |
by (rewrite <-Z.sub_1_r; ring). |
| 367 |
replace (Z.pred x) with (x-1) by apply Z.sub_1_r. |
| 368 |
rewrite !of_Z_sub_1_r; rewrite_hyp *. |
| 369 |
rewrite !ring_sub_definition. |
| 370 |
rewrite associative; reflexivity. } |
| 371 |
Qed. |
| 372 |
|
| 373 |
Lemma of_Z_mul : forall a b, |
| 374 |
of_Z (Z.mul a b) = Rmul (of_Z a) (of_Z b). |
| 375 |
Proof using Type*. |
| 376 |
intros a b. |
| 377 |
let x := match goal with |- ?x => x end in |
| 378 |
let f := match (eval pattern b in x) with ?f _ => f end in |
| 379 |
apply (Z.peano_ind f); intros *; try intro IHb. |
| 380 |
{ rewrite !mul_0_r; reflexivity. } |
| 381 |
{ rewrite Z.mul_succ_r, <-Z.add_1_r. |
| 382 |
rewrite of_Z_add, of_Z_add_1_r. |
| 383 |
rewrite IHb. |
| 384 |
rewrite left_distributive, right_identity. |
| 385 |
reflexivity. } |
| 386 |
{ rewrite Z.mul_pred_r, <-Z.sub_1_r. |
| 387 |
rewrite of_Z_sub_1_r. |
| 388 |
rewrite <-Z.add_opp_r. |
| 389 |
rewrite of_Z_add, of_Z_opp. |
| 390 |
rewrite IHb. |
| 391 |
rewrite ring_sub_definition, left_distributive. |
| 392 |
rewrite mul_opp_r,right_identity. |
| 393 |
reflexivity. } |
| 394 |
Qed. |
| 395 |
|
| 396 |
|
| 397 |
Global Instance homomorphism_of_Z : |
| 398 |
@is_homomorphism |
| 399 |
Z Logic.eq Z.one Z.add Z.mul |
| 400 |
R Req Rone Radd Rmul |
| 401 |
of_Z. |
| 402 |
Proof using Type*. |
| 403 |
repeat constructor; intros. |
| 404 |
{ apply of_Z_add. } |
| 405 |
{ repeat intro; subst; reflexivity. } |
| 406 |
{ apply of_Z_mul. } |
| 407 |
{ simpl. rewrite left_identity; reflexivity. } |
| 408 |
Qed. |
| 409 |
End of_Z. |
| 410 |
|
| 411 |
Section of_Z_absorbs_homomorphism. |
| 412 |
Context {R Req Rzero Rone Ropp Radd Rsub Rmul} |
| 413 |
{Rring : @ring R Req Rzero Rone Ropp Radd Rsub Rmul}. |
| 414 |
Context {R' R'eq R'zero R'one R'opp R'add R'sub R'mul} |
| 415 |
{R'ring : @ring R' R'eq R'zero R'one R'opp R'add R'sub R'mul}. |
| 416 |
Context {phi} |
| 417 |
{Hphi:@is_homomorphism R Req Rone Radd Rmul |
| 418 |
R' R'eq R'one R'add R'mul phi}. |
| 419 |
|
| 420 |
Local Notation R_of_nat := (@of_nat R Rzero Rone Radd) (only parsing). |
| 421 |
Local Notation R'_of_nat := (@of_nat R' R'zero R'one R'add) (only parsing). |
| 422 |
Local Notation R_of_Z := (@of_Z R Rzero Rone Ropp Radd) (only parsing). |
| 423 |
Local Notation R'_of_Z := (@of_Z R' R'zero R'one R'opp R'add) (only parsing). |
| 424 |
|
| 425 |
Lemma of_nat_absorbs_homomorphism x |
| 426 |
: R'eq (phi (R_of_nat x)) (R'_of_nat x). |
| 427 |
Proof. |
| 428 |
induction x as [|x IHx]; cbn [of_nat]; |
| 429 |
repeat first [ rewrite homomorphism_zero |
| 430 |
| rewrite homomorphism_add |
| 431 |
| rewrite homomorphism_one |
| 432 |
| rewrite IHx |
| 433 |
| reflexivity ]. |
| 434 |
Qed. |
| 435 |
|
| 436 |
Lemma of_Z_absorbs_homomorphism x |
| 437 |
: R'eq (phi (R_of_Z x)) (R'_of_Z x). |
| 438 |
Proof. |
| 439 |
cbv [of_Z]; break_innermost_match; |
| 440 |
repeat first [ rewrite homomorphism_zero |
| 441 |
| rewrite homomorphism_opp |
| 442 |
| rewrite of_nat_absorbs_homomorphism |
| 443 |
| reflexivity ]. |
| 444 |
Qed. |
| 445 |
End of_Z_absorbs_homomorphism. |
| 446 |
|
| 447 |
Definition char_ge |
| 448 |
{R eq zero one opp add} {sub:R->R->R} {mul:R->R->R} |
| 449 |
C := |
| 450 |
@Hierarchy.char_ge R eq zero (fun p => (@of_Z R zero one opp add) (BinInt.Z.pos p)) C. |
| 451 |
Existing Class char_ge. |
| 452 |
|
| 453 |
(*** Tactics for ring equations *) |
| 454 |
From Coq Require Export Ring_tac. |
| 455 |
Ltac ring_simplify_subterms := tac_on_subterms ltac:(fun t => ring_simplify t). |
| 456 |
|
| 457 |
Ltac ring_simplify_subterms_in_all := |
| 458 |
reverse_nondep; ring_simplify_subterms; intros. |
| 459 |
|
| 460 |
Create HintDb ring_simplify discriminated. |
| 461 |
Create HintDb ring_simplify_subterms discriminated. |
| 462 |
Create HintDb ring_simplify_subterms_in_all discriminated. |
| 463 |
Global Hint Extern 1 => progress ring_simplify : ring_simplify. |
| 464 |
Global Hint Extern 1 => progress ring_simplify_subterms : ring_simplify_subterms. |
| 465 |
Global Hint Extern 1 => progress ring_simplify_subterms_in_all : ring_simplify_subterms_in_all. |