| 1 |
From Coq Require Integral_domain. |
| 2 |
Require Crypto.Algebra.Nsatz. |
| 3 |
Require Import Crypto.Util.Factorize. |
| 4 |
Require Import Crypto.Algebra.Hierarchy Crypto.Algebra.Ring. |
| 5 |
Require Import Crypto.Util.Tactics.RewriteHyp. |
| 6 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 7 |
|
| 8 |
Module IntegralDomain. |
| 9 |
Section IntegralDomain. |
| 10 |
Context {T eq zero one opp add sub mul} `{@integral_domain T eq zero one opp add sub mul}. |
| 11 |
|
| 12 |
Lemma nonzero_product_iff_nonzero_factors : |
| 13 |
forall x y : T, ~ eq (mul x y) zero <-> ~ eq x zero /\ ~ eq y zero. |
| 14 |
Proof using Type*. setoid_rewrite Ring.zero_product_iff_zero_factor; intuition. Qed. |
| 15 |
|
| 16 |
Global Instance Integral_domain : |
| 17 |
@Integral_domain.Integral_domain T zero one add mul sub opp eq Ring.Ncring_Ring_ops |
| 18 |
Ring.Ncring_Ring Ring.Cring_Cring_commutative_ring. |
| 19 |
Proof using Type. split; cbv; pose zero_product_zero_factor; pose one_neq_zero; eauto. Qed. |
| 20 |
End IntegralDomain. |
| 21 |
|
| 22 |
Module _LargeCharacteristicReflective. |
| 23 |
Section ReflectiveNsatzSideConditionProver. |
| 24 |
Import BinInt BinNat BinPos. |
| 25 |
Context {R eq zero one opp add sub mul} |
| 26 |
{ring:@Hierarchy.ring R eq zero one opp add sub mul} |
| 27 |
{zpzf:@Hierarchy.is_zero_product_zero_factor R eq zero mul}. |
| 28 |
Local Infix "=" := eq. Local Notation "a <> b" := (not (a = b)). |
| 29 |
Local Infix "=" := eq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 30 |
Local Infix "+" := add. Local Infix "-" := sub. Local Infix "*" := mul. |
| 31 |
|
| 32 |
Inductive coef := |
| 33 |
| Coef_one |
| 34 |
| Coef_opp (_:coef) |
| 35 |
| Coef_add (_ _:coef) |
| 36 |
| Coef_mul (_ _:coef). |
| 37 |
Fixpoint denote (c:coef) : R := |
| 38 |
match c with |
| 39 |
| Coef_one => one |
| 40 |
| Coef_opp c => opp (denote c) |
| 41 |
| Coef_add c1 c2 => add (denote c1) (denote c2) |
| 42 |
| Coef_mul c1 c2 => mul (denote c1) (denote c2) |
| 43 |
end. |
| 44 |
Fixpoint CtoZ (c:coef) : Z := |
| 45 |
match c with |
| 46 |
| Coef_one => Z.one |
| 47 |
| Coef_opp c => Z.opp (CtoZ c) |
| 48 |
| Coef_add c1 c2 => Z.add (CtoZ c1) (CtoZ c2) |
| 49 |
| Coef_mul c1 c2 => Z.mul (CtoZ c1) (CtoZ c2) |
| 50 |
end. |
| 51 |
Let of_Z := (@Ring.of_Z R zero one opp add). |
| 52 |
|
| 53 |
Lemma CtoZ_correct c : of_Z (CtoZ c) = denote c. |
| 54 |
Proof using ring. |
| 55 |
induction c; simpl CtoZ; simpl denote; |
| 56 |
repeat (rewrite_hyp ?* || Ring.push_homomorphism of_Z); reflexivity. |
| 57 |
Qed. |
| 58 |
|
| 59 |
(* TODO: move *) |
| 60 |
Lemma nonzero_of_Z_abs z : of_Z (Z.abs z) <> zero <-> of_Z z <> zero. |
| 61 |
Proof using ring. |
| 62 |
destruct z; simpl Z.abs; [reflexivity..|]. |
| 63 |
simpl of_Z. setoid_rewrite opp_zero_iff. reflexivity. |
| 64 |
Qed. |
| 65 |
|
| 66 |
Section WithChar. |
| 67 |
Context C (char_ge_C:@Ring.char_ge R eq zero one opp add sub mul C) (HC: Pos.lt xH C). |
| 68 |
|
| 69 |
Definition is_factor_nonzero (n:N) : bool := |
| 70 |
match n with N0 => false | N.pos p => BinPos.Pos.ltb p C end. |
| 71 |
Lemma is_factor_nonzero_correct (n:N) (refl:Logic.eq (is_factor_nonzero n) true) |
| 72 |
: of_Z (Z.of_N n) <> zero. |
| 73 |
Proof using char_ge_C. |
| 74 |
destruct n; [discriminate|]; rewrite Znat.positive_N_Z; apply char_ge_C, Pos.ltb_lt, refl. |
| 75 |
Qed. |
| 76 |
|
| 77 |
Lemma RZN_product_nonzero l (H : forall x : N, List.In x l -> of_Z (Z.of_N x) <> zero) |
| 78 |
: of_Z (Z.of_N (List.fold_right N.mul 1%N l)) <> zero. |
| 79 |
Proof using HC char_ge_C ring zpzf. |
| 80 |
rewrite <-List.Forall_forall in H; induction H; simpl List.fold_right. |
| 81 |
{ eapply char_ge_C; assumption. } |
| 82 |
{ rewrite Znat.N2Z.inj_mul; Ring.push_homomorphism of_Z. |
| 83 |
eapply Ring.nonzero_product_iff_nonzero_factor; eauto. } |
| 84 |
Qed. |
| 85 |
|
| 86 |
Definition is_constant_nonzero (z:Z) : bool := |
| 87 |
match factorize_or_fail (Z.abs_N z) with |
| 88 |
| Some factors => List.forallb is_factor_nonzero factors |
| 89 |
| None => false |
| 90 |
end. |
| 91 |
Lemma is_constant_nonzero_correct z (refl:Logic.eq (is_constant_nonzero z) true) |
| 92 |
: of_Z z <> zero. |
| 93 |
Proof using HC char_ge_C ring zpzf. |
| 94 |
rewrite <-nonzero_of_Z_abs, <-Znat.N2Z.inj_abs_N. |
| 95 |
repeat match goal with |
| 96 |
| _ => progress cbv [is_constant_nonzero] in * |
| 97 |
| _ => progress break_match_hyps; try congruence; [] |
| 98 |
| _ => progress rewrite ?List.forallb_forall in * |
| 99 |
|H:_|-_ => apply product_factorize_or_fail in H; rewrite <- H in * |
| 100 |
| _ => solve [eauto using RZN_product_nonzero, is_factor_nonzero_correct] |
| 101 |
end. |
| 102 |
Qed. |
| 103 |
|
| 104 |
Fixpoint is_nonzero (c:coef) : bool := |
| 105 |
match c with |
| 106 |
| Coef_one => true |
| 107 |
| Coef_opp c => is_nonzero c |
| 108 |
| Coef_mul c1 c2 => andb (is_nonzero c1) (is_nonzero c2) |
| 109 |
| _ => is_constant_nonzero (CtoZ c) |
| 110 |
end. |
| 111 |
Lemma is_nonzero_correct' c (refl:Logic.eq (is_nonzero c) true) : denote c <> zero. |
| 112 |
Proof using HC char_ge_C ring zpzf. |
| 113 |
induction c; |
| 114 |
repeat match goal with |
| 115 |
| H:_|-_ => progress rewrite Bool.andb_true_iff in H; destruct H |
| 116 |
| H:_|-_ => progress apply is_constant_nonzero_correct in H |
| 117 |
| _ => progress (unfold is_nonzero in *; fold is_nonzero in * ) |
| 118 |
| _ => progress (change (denote (Coef_one)) with (of_Z 1) in * ) |
| 119 |
| _ => progress (change (denote (Coef_opp c)) with (opp (denote c)) in * ) |
| 120 |
| _ => progress (change (denote (Coef_mul c1 c2)) with (denote c1 * denote c2) in * ) |
| 121 |
| |- (opp _ <> zero)%type => setoid_rewrite Ring.opp_zero_iff |
| 122 |
| |- (mul _ _ <> zero)%type => eapply Ring.nonzero_product_iff_nonzero_factor |
| 123 |
| _ => solve [eauto using is_constant_nonzero_correct, Pos.le_1_l] |
| 124 |
| _ => progress rewrite <-CtoZ_correct |
| 125 |
end. |
| 126 |
Qed. |
| 127 |
End WithChar. |
| 128 |
|
| 129 |
Lemma is_nonzero_correct |
| 130 |
C |
| 131 |
(char_ge_C:@Ring.char_ge R eq zero one opp add sub mul C) |
| 132 |
c (refl:Logic.eq (andb (Pos.ltb xH C) (is_nonzero C c)) true) |
| 133 |
: denote c <> zero. |
| 134 |
Proof using ring zpzf. |
| 135 |
rewrite Bool.andb_true_iff in refl; destruct refl. |
| 136 |
eapply @is_nonzero_correct'; try apply Pos.ltb_lt; eauto. |
| 137 |
Qed. |
| 138 |
|
| 139 |
End ReflectiveNsatzSideConditionProver. |
| 140 |
|
| 141 |
Ltac reify one opp add mul x := |
| 142 |
match x with |
| 143 |
| one => constr:(Coef_one) |
| 144 |
| opp ?a => |
| 145 |
let a := reify one opp add mul a in |
| 146 |
constr:(Coef_opp a) |
| 147 |
| add ?a ?b => |
| 148 |
let a := reify one opp add mul a in |
| 149 |
let b := reify one opp add mul b in |
| 150 |
constr:(Coef_add a b) |
| 151 |
| mul ?a ?b => |
| 152 |
let a := reify one opp add mul a in |
| 153 |
let b := reify one opp add mul b in |
| 154 |
constr:(Coef_mul a b) |
| 155 |
end. |
| 156 |
End _LargeCharacteristicReflective. |
| 157 |
|
| 158 |
Ltac solve_constant_nonzero := |
| 159 |
match goal with |
| 160 |
| |- not (?eq ?x _) => |
| 161 |
let cg := constr:(_:Ring.char_ge (eq:=eq) _) in |
| 162 |
match type of cg with |
| 163 |
@Ring.char_ge ?R eq ?zero ?one ?opp ?add ?sub ?mul ?C => |
| 164 |
let x' := _LargeCharacteristicReflective.reify one opp add mul x in |
| 165 |
let x' := constr:(@_LargeCharacteristicReflective.denote R one opp add mul x') in |
| 166 |
change (not (eq x' zero)); |
| 167 |
apply (_LargeCharacteristicReflective.is_nonzero_correct(eq:=eq)(zero:=zero) C cg); |
| 168 |
(vm_cast_no_check (eq_refl true)) |
| 169 |
end |
| 170 |
end. |
| 171 |
End IntegralDomain. |
| 172 |
|
| 173 |
(** Tactics for polynomial equations over integral domains *) |
| 174 |
|
| 175 |
Ltac dropAlgebraSyntax := |
| 176 |
cbv beta delta [ |
| 177 |
Algebra_syntax.zero |
| 178 |
Algebra_syntax.one |
| 179 |
Algebra_syntax.addition |
| 180 |
Algebra_syntax.multiplication |
| 181 |
Algebra_syntax.subtraction |
| 182 |
Algebra_syntax.opposite |
| 183 |
Algebra_syntax.equality |
| 184 |
Algebra_syntax.bracket |
| 185 |
Algebra_syntax.power |
| 186 |
] in *. |
| 187 |
|
| 188 |
Ltac dropRingSyntax := |
| 189 |
dropAlgebraSyntax; |
| 190 |
cbv beta delta [ |
| 191 |
Ncring.zero_notation |
| 192 |
Ncring.one_notation |
| 193 |
Ncring.add_notation |
| 194 |
Ncring.mul_notation |
| 195 |
Ncring.sub_notation |
| 196 |
Ncring.opp_notation |
| 197 |
Ncring.eq_notation |
| 198 |
] in *. |
| 199 |
Ltac nsatz := Algebra.Nsatz.nsatz; dropRingSyntax. |