| 1 |
Require Import Crypto.Util.Relations Crypto.Util.Notations. |
| 2 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 3 |
Require Import Crypto.Util.Tactics.DebugPrint. |
| 4 |
From Coq Require Import RelationClasses Morphisms. |
| 5 |
Require Import Crypto.Algebra.Hierarchy Crypto.Algebra.Ring Crypto.Algebra.IntegralDomain. |
| 6 |
From Coq Require Field_theory. |
| 7 |
|
| 8 |
Section Field. |
| 9 |
Context {T eq zero one opp add mul sub inv div} `{@field T eq zero one opp add sub mul inv div}. |
| 10 |
Local Infix "=" := eq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 11 |
Local Notation "0" := zero. Local Notation "1" := one. |
| 12 |
Local Infix "+" := add. Local Infix "*" := mul. |
| 13 |
|
| 14 |
Lemma right_multiplicative_inverse : forall x : T, ~ eq x zero -> eq (mul x (inv x)) one. |
| 15 |
Proof using Type*. |
| 16 |
intros. rewrite commutative. pose left_multiplicative_inverse; eauto. |
| 17 |
Qed. |
| 18 |
|
| 19 |
Lemma left_inv_unique x ix : ix * x = one -> ix = inv x. |
| 20 |
Proof using Type*. |
| 21 |
intro Hix. |
| 22 |
assert (H0 : ix*x*inv x = inv x). |
| 23 |
- rewrite Hix, left_identity; reflexivity. |
| 24 |
- rewrite <-associative, right_multiplicative_inverse, right_identity in H0; trivial. |
| 25 |
intro eq_x_0. rewrite eq_x_0, Ring.mul_0_r in Hix. |
| 26 |
apply (zero_neq_one(eq:=eq)). assumption. |
| 27 |
Qed. |
| 28 |
Definition inv_unique := left_inv_unique. |
| 29 |
|
| 30 |
Lemma right_inv_unique x ix : x * ix = one -> ix = inv x. |
| 31 |
Proof using Type*. rewrite commutative. apply left_inv_unique. Qed. |
| 32 |
|
| 33 |
Lemma div_one x : div x one = x. |
| 34 |
Proof using Type*. |
| 35 |
rewrite field_div_definition. |
| 36 |
rewrite <-(inv_unique 1 1); apply monoid_is_right_identity. |
| 37 |
Qed. |
| 38 |
|
| 39 |
Lemma mul_cancel_l_iff : forall x y, y <> 0 -> |
| 40 |
(x * y = y <-> x = one). |
| 41 |
Proof using Type*. |
| 42 |
intros x y H0. |
| 43 |
split; intros H1. |
| 44 |
+ rewrite <-(right_multiplicative_inverse y) by assumption. |
| 45 |
rewrite <-H1 at 1; rewrite <-associative. |
| 46 |
rewrite right_multiplicative_inverse by assumption. |
| 47 |
rewrite right_identity. |
| 48 |
reflexivity. |
| 49 |
+ rewrite H1; apply left_identity. |
| 50 |
Qed. |
| 51 |
|
| 52 |
Lemma field_theory_for_stdlib_tactic : Field_theory.field_theory 0 1 add mul sub opp div inv eq. |
| 53 |
Proof using Type*. |
| 54 |
constructor. |
| 55 |
{ apply Ring.ring_theory_for_stdlib_tactic. } |
| 56 |
{ intro H01. symmetry in H01. auto using (zero_neq_one(eq:=eq)). } |
| 57 |
{ apply field_div_definition. } |
| 58 |
{ apply left_multiplicative_inverse. } |
| 59 |
Qed. |
| 60 |
|
| 61 |
Context {eq_dec:DecidableRel eq}. |
| 62 |
|
| 63 |
Global Instance is_mul_nonzero_nonzero : @is_zero_product_zero_factor T eq 0 mul. |
| 64 |
Proof using Type*. |
| 65 |
split. intros x y Hxy. |
| 66 |
eapply not_not; try typeclasses eauto; []; intuition idtac; eapply (zero_neq_one(eq:=eq)). |
| 67 |
transitivity ((inv y * (inv x * x)) * y). |
| 68 |
- rewrite <-!associative, Hxy, !Ring.mul_0_r; reflexivity. |
| 69 |
- rewrite left_multiplicative_inverse, right_identity, left_multiplicative_inverse by trivial. |
| 70 |
reflexivity. |
| 71 |
Qed. |
| 72 |
|
| 73 |
Global Instance integral_domain : @integral_domain T eq zero one opp add sub mul. |
| 74 |
Proof using Type*. |
| 75 |
split; eauto using field_commutative_ring, field_is_zero_neq_one, is_mul_nonzero_nonzero. |
| 76 |
Qed. |
| 77 |
End Field. |
| 78 |
|
| 79 |
Section Homomorphism. |
| 80 |
Context {F EQ ZERO ONE OPP ADD MUL SUB INV DIV} `{@field F EQ ZERO ONE OPP ADD SUB MUL INV DIV}. |
| 81 |
Context {K eq zero one opp add mul sub inv div} `{@field K eq zero one opp add sub mul inv div}. |
| 82 |
Context {phi:F->K}. |
| 83 |
Local Infix "=" := eq. Local Infix "=" := eq : type_scope. |
| 84 |
Context `{@Ring.is_homomorphism F EQ ONE ADD MUL K eq one add mul phi}. |
| 85 |
|
| 86 |
Lemma homomorphism_multiplicative_inverse |
| 87 |
: forall x, not (EQ x ZERO) |
| 88 |
-> phi (INV x) = inv (phi x). |
| 89 |
Proof using Type*. |
| 90 |
intros. |
| 91 |
eapply inv_unique. |
| 92 |
rewrite <-Ring.homomorphism_mul. |
| 93 |
rewrite left_multiplicative_inverse; pose Ring.homomorphism_one; eauto. |
| 94 |
Qed. |
| 95 |
|
| 96 |
Lemma homomorphism_multiplicative_inverse_complete |
| 97 |
{ EQ_dec : DecidableRel EQ } |
| 98 |
: forall x, (EQ x ZERO -> phi (INV x) = inv (phi x)) |
| 99 |
-> phi (INV x) = inv (phi x). |
| 100 |
Proof using Type*. |
| 101 |
intros x ?; destruct (dec (EQ x ZERO)); auto using homomorphism_multiplicative_inverse. |
| 102 |
Qed. |
| 103 |
|
| 104 |
Lemma homomorphism_div |
| 105 |
: forall x y, not (EQ y ZERO) |
| 106 |
-> phi (DIV x y) = div (phi x) (phi y). |
| 107 |
Proof using Type*. |
| 108 |
intros. rewrite !field_div_definition. |
| 109 |
rewrite Ring.homomorphism_mul, homomorphism_multiplicative_inverse; |
| 110 |
(eauto || reflexivity). |
| 111 |
Qed. |
| 112 |
|
| 113 |
Lemma homomorphism_div_complete |
| 114 |
{ EQ_dec : DecidableRel EQ } |
| 115 |
: forall x y, (EQ y ZERO -> phi (INV y) = inv (phi y)) |
| 116 |
-> phi (DIV x y) = div (phi x) (phi y). |
| 117 |
Proof using Type*. |
| 118 |
intros. rewrite !field_div_definition. |
| 119 |
rewrite Ring.homomorphism_mul, homomorphism_multiplicative_inverse_complete; |
| 120 |
(eauto || reflexivity). |
| 121 |
Qed. |
| 122 |
End Homomorphism. |
| 123 |
|
| 124 |
Section Homomorphism_rev. |
| 125 |
Context {F EQ ZERO ONE OPP ADD SUB MUL INV DIV} {fieldF:@field F EQ ZERO ONE OPP ADD SUB MUL INV DIV}. |
| 126 |
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}. |
| 127 |
Context {phi:F->H} {phi':H->F}. |
| 128 |
Local Infix "=" := EQ. Local Infix "=" := EQ : type_scope. |
| 129 |
Context (phi'_phi_id : forall A, phi' (phi A) = A) |
| 130 |
(phi'_eq : forall a b, EQ (phi' a) (phi' b) <-> eq a b) |
| 131 |
{phi'_zero : phi' zero = ZERO} |
| 132 |
{phi'_one : phi' one = ONE} |
| 133 |
{phi'_opp : forall a, phi' (opp a) = OPP (phi' a)} |
| 134 |
(phi'_add : forall a b, phi' (add a b) = ADD (phi' a) (phi' b)) |
| 135 |
(phi'_sub : forall a b, phi' (sub a b) = SUB (phi' a) (phi' b)) |
| 136 |
(phi'_mul : forall a b, phi' (mul a b) = MUL (phi' a) (phi' b)) |
| 137 |
{phi'_inv : forall a, phi' (inv a) = INV (phi' a)} |
| 138 |
(phi'_div : forall a b, phi' (div a b) = DIV (phi' a) (phi' b)). |
| 139 |
|
| 140 |
Lemma field_and_homomorphism_from_redundant_representation |
| 141 |
: @field H eq zero one opp add sub mul inv div |
| 142 |
/\ @Ring.is_homomorphism F EQ ONE ADD MUL H eq one add mul phi |
| 143 |
/\ @Ring.is_homomorphism H eq one add mul F EQ ONE ADD MUL phi'. |
| 144 |
Proof using Type*. |
| 145 |
repeat match goal with |
| 146 |
| [ H : field |- _ ] => destruct H; try clear H |
| 147 |
| [ H : commutative_ring |- _ ] => destruct H; try clear H |
| 148 |
| [ H : ring |- _ ] => destruct H; try clear H |
| 149 |
| [ H : commutative_group |- _ ] => destruct H; try clear H |
| 150 |
| [ H : group |- _ ] => destruct H; try clear H |
| 151 |
| [ H : monoid |- _ ] => destruct H; try clear H |
| 152 |
| [ H : is_commutative |- _ ] => destruct H; try clear H |
| 153 |
| [ H : is_left_multiplicative_inverse |- _ ] => destruct H; try clear H |
| 154 |
| [ H : is_left_distributive |- _ ] => destruct H; try clear H |
| 155 |
| [ H : is_right_distributive |- _ ] => destruct H; try clear H |
| 156 |
| [ H : is_zero_neq_one |- _ ] => destruct H; try clear H |
| 157 |
| [ H : is_associative |- _ ] => destruct H; try clear H |
| 158 |
| [ H : is_left_identity |- _ ] => destruct H; try clear H |
| 159 |
| [ H : is_right_identity |- _ ] => destruct H; try clear H |
| 160 |
| [ H : Equivalence _ |- _ ] => destruct H; try clear H |
| 161 |
| [ H : is_left_inverse |- _ ] => destruct H; try clear H |
| 162 |
| [ H : is_right_inverse |- _ ] => destruct H; try clear H |
| 163 |
| _ => intro |
| 164 |
| _ => split |
| 165 |
| [ H : eq _ _ |- _ ] => apply phi'_eq in H |
| 166 |
| [ |- eq _ _ ] => apply phi'_eq |
| 167 |
| [ H : (~eq _ _)%type |- _ ] => pose proof (fun pf => H (proj1 (@phi'_eq _ _) pf)); clear H |
| 168 |
| [ H : EQ _ _ |- _ ] => rewrite H |
| 169 |
| _ => progress erewrite ?phi'_zero, ?phi'_one, ?phi'_opp, ?phi'_add, ?phi'_sub, ?phi'_mul, ?phi'_inv, ?phi'_div, ?phi'_phi_id by reflexivity |
| 170 |
| [ H : _ |- _ ] => progress erewrite ?phi'_zero, ?phi'_one, ?phi'_opp, ?phi'_add, ?phi'_sub, ?phi'_mul, ?phi'_inv, ?phi'_div, ?phi'_phi_id in H by reflexivity |
| 171 |
| _ => solve [ eauto ] |
| 172 |
end. |
| 173 |
Qed. |
| 174 |
End Homomorphism_rev. |
| 175 |
|
| 176 |
Ltac guess_field := |
| 177 |
match goal with |
| 178 |
| |- ?eq _ _ => constr:(_:Hierarchy.field (eq:=eq)) |
| 179 |
| |- not (?eq _ _) => constr:(_:Hierarchy.field (eq:=eq)) |
| 180 |
| [H: ?eq _ _ |- _ ] => constr:(_:Hierarchy.field (eq:=eq)) |
| 181 |
| [H: not (?eq _ _) |- _] => constr:(_:Hierarchy.field (eq:=eq)) |
| 182 |
end. |
| 183 |
|
| 184 |
Ltac goal_to_field_equality fld := |
| 185 |
let eq := match type of fld with Hierarchy.field(eq:=?eq) => eq end in |
| 186 |
match goal with |
| 187 |
| [ |- eq _ _] => idtac |
| 188 |
| [ |- not (eq ?x ?y) ] => apply not_exfalso; intro; goal_to_field_equality fld |
| 189 |
| _ => exfalso; |
| 190 |
match goal with |
| 191 |
| H: not (eq _ _) |- _ => apply not_exfalso in H; apply H |
| 192 |
| _ => apply (field_is_zero_neq_one(field:=fld)) |
| 193 |
end |
| 194 |
end. |
| 195 |
|
| 196 |
Ltac inequalities_to_inverse_equations fld := |
| 197 |
let eq := match type of fld with Hierarchy.field(eq:=?eq) => eq end in |
| 198 |
let zero := match type of fld with Hierarchy.field(zero:=?zero) => zero end in |
| 199 |
let div := match type of fld with Hierarchy.field(div:=?div) => div end in |
| 200 |
let sub := match type of fld with Hierarchy.field(sub:=?sub) => sub end in |
| 201 |
repeat match goal with |
| 202 |
| [H: not (eq _ _) |- _ ] => |
| 203 |
lazymatch type of H with |
| 204 |
| not (eq ?d zero) => |
| 205 |
unique pose proof (right_multiplicative_inverse(H:=fld) _ H) |
| 206 |
| not (eq zero ?d) => |
| 207 |
unique pose proof (right_multiplicative_inverse(H:=fld) _ (symmetry(R:=fun a b => not (eq a b)) H)) |
| 208 |
| not (eq ?x ?y) => |
| 209 |
unique pose proof (right_multiplicative_inverse(H:=fld) _ (Ring.neq_sub_neq_zero _ _ H)) |
| 210 |
end |
| 211 |
end. |
| 212 |
|
| 213 |
Ltac unique_pose_implication pf := |
| 214 |
let B := match type of pf with ?A -> ?B => B end in |
| 215 |
match goal with |
| 216 |
| [H:B|-_] => fail 1 |
| 217 |
| _ => unique pose proof pf |
| 218 |
end. |
| 219 |
|
| 220 |
Ltac inverses_to_conditional_equations fld := |
| 221 |
let eq := match type of fld with Hierarchy.field(eq:=?eq) => eq end in |
| 222 |
let inv := match type of fld with Hierarchy.field(inv:=?inv) => inv end in |
| 223 |
repeat match goal with |
| 224 |
| |- context[inv ?d] => |
| 225 |
unique_pose_implication constr:(right_multiplicative_inverse(H:=fld) d) |
| 226 |
| H: context[inv ?d] |- _ => |
| 227 |
unique_pose_implication constr:(right_multiplicative_inverse(H:=fld) d) |
| 228 |
end. |
| 229 |
|
| 230 |
Ltac clear_hypotheses_with_nonzero_requirements fld := |
| 231 |
let eq := match type of fld with Hierarchy.field(eq:=?eq) => eq end in |
| 232 |
let zero := match type of fld with Hierarchy.field(zero:=?zero) => zero end in |
| 233 |
repeat match goal with |
| 234 |
[H: not (eq _ zero) -> _ |- _ ] => clear H |
| 235 |
end. |
| 236 |
|
| 237 |
Ltac forward_nonzero fld solver_tac := |
| 238 |
let eq := match type of fld with Hierarchy.field(eq:=?eq) => eq end in |
| 239 |
let zero := match type of fld with Hierarchy.field(zero:=?zero) => zero end in |
| 240 |
repeat match goal with |
| 241 |
| [H: not (eq ?x zero) -> _ |- _ ] |
| 242 |
=> let H' := fresh in |
| 243 |
assert (H' : not (eq x zero)) by (clear_hypotheses_with_nonzero_requirements; solver_tac); specialize (H H') |
| 244 |
| [H: not (eq ?x zero) -> _ |- _ ] |
| 245 |
=> let H' := fresh in |
| 246 |
assert (H' : not (eq x zero)) by (clear H; solver_tac); specialize (H H') |
| 247 |
end. |
| 248 |
|
| 249 |
Ltac divisions_to_inverses fld := |
| 250 |
rewrite ?(field_div_definition(field:=fld)) in *. |
| 251 |
|
| 252 |
Ltac fsatz_solve_on fld := |
| 253 |
goal_to_field_equality fld; |
| 254 |
forward_nonzero fld ltac:(fsatz_solve_on fld); |
| 255 |
nsatz; |
| 256 |
solve_debugfail ltac:(IntegralDomain.solve_constant_nonzero). |
| 257 |
|
| 258 |
Ltac fsatz_solve := |
| 259 |
let fld := guess_field in |
| 260 |
fsatz_solve_on fld. |
| 261 |
|
| 262 |
Ltac fsatz_prepare_hyps_on fld := |
| 263 |
divisions_to_inverses fld; |
| 264 |
inequalities_to_inverse_equations fld; |
| 265 |
inverses_to_conditional_equations fld; |
| 266 |
forward_nonzero fld ltac:(fsatz_solve_on fld). |
| 267 |
|
| 268 |
Ltac fsatz_prepare_hyps := |
| 269 |
let fld := guess_field in |
| 270 |
fsatz_prepare_hyps_on fld. |
| 271 |
|
| 272 |
Ltac fsatz := |
| 273 |
let fld := guess_field in |
| 274 |
fsatz_prepare_hyps_on fld; |
| 275 |
fsatz_solve_on fld. |
| 276 |
|
| 277 |
|
| 278 |
Section FieldSquareRoot. |
| 279 |
Context {T eq zero one opp add mul sub inv div} `{@field T eq zero one opp add sub mul inv div} {eq_dec:DecidableRel eq}. |
| 280 |
Local Infix "=" := eq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 281 |
Local Infix "+" := add. Local Infix "*" := mul. |
| 282 |
Lemma only_two_square_roots_choice x y z : x * x = z -> y * y = z -> x = y \/ x = opp y. |
| 283 |
Proof using Type*. |
| 284 |
intros. |
| 285 |
setoid_rewrite <-sub_zero_iff. |
| 286 |
eapply zero_product_zero_factor. |
| 287 |
fsatz. |
| 288 |
Qed. |
| 289 |
End FieldSquareRoot. |