| 1 |
(* This is a rewrite of the Ltac parts of standard library nsatz. We should |
| 2 |
periodically check whether we still need it -- once enough bugs get fixed |
| 3 |
in mailine, we hope to drop this implementation *) |
| 4 |
|
| 5 |
From Coq Require NsatzTactic. |
| 6 |
From Coq Require Import List. |
| 7 |
|
| 8 |
(** For compat with https://github.com/coq/coq/pull/12073 *) |
| 9 |
Module Nsatz. |
| 10 |
Import Coq.nsatz.NsatzTactic. |
| 11 |
Notation check_correct := check_correct. |
| 12 |
Notation PEevalR := PEevalR. |
| 13 |
Notation psos_r1 := psos_r1. |
| 14 |
Notation psos_r1b := psos_r1b. |
| 15 |
End Nsatz. |
| 16 |
|
| 17 |
Generalizable All Variables. |
| 18 |
Lemma cring_sub_diag_iff {R zero eq sub} `{cring:Cring.Cring (R:=R) (ring0:=zero) (ring_eq:=eq) (sub:=sub)} |
| 19 |
: forall x y, eq (sub x y) zero <-> eq x y. |
| 20 |
Proof. |
| 21 |
split;intros Hx. |
| 22 |
{ eapply Nsatz.psos_r1b. eapply Hx. } |
| 23 |
{ eapply Nsatz.psos_r1. eapply Hx. } |
| 24 |
Qed. |
| 25 |
|
| 26 |
Ltac get_goal := lazymatch goal with |- ?g => g end. |
| 27 |
|
| 28 |
Ltac nsatz_equation_implications_to_list eq zero g := |
| 29 |
lazymatch g with |
| 30 |
| eq ?p zero => constr:(p::nil) |
| 31 |
| eq ?p zero -> ?g => let l := nsatz_equation_implications_to_list eq zero g in constr:(p::l) |
| 32 |
end. |
| 33 |
|
| 34 |
Ltac nsatz_reify_equations eq zero := |
| 35 |
let g := get_goal in |
| 36 |
let lb := nsatz_equation_implications_to_list eq zero g in |
| 37 |
lazymatch (eval red in (Ncring_tac.list_reifyl (lterm:=lb))) with |
| 38 |
(?variables, ?le) => |
| 39 |
lazymatch (eval compute in (List.rev le)) with |
| 40 |
| ?reified_goal::?reified_givens => constr:((variables, reified_givens, reified_goal)) |
| 41 |
end |
| 42 |
end. |
| 43 |
|
| 44 |
Ltac nsatz_get_free_variables reified_package := |
| 45 |
lazymatch reified_package with (?fv, _, _) => fv end. |
| 46 |
|
| 47 |
Ltac nsatz_get_reified_givens reified_package := |
| 48 |
lazymatch reified_package with (_, ?givens, _) => givens end. |
| 49 |
|
| 50 |
Ltac nsatz_get_reified_goal reified_package := |
| 51 |
lazymatch reified_package with (_, _, ?goal) => goal end. |
| 52 |
|
| 53 |
From Coq Require Import Ring_polynom. |
| 54 |
(* Kludge for 8.4/8.5 compatibility *) |
| 55 |
Module Import mynsatz_compute. |
| 56 |
Import Coq.nsatz.NsatzTactic. |
| 57 |
Global Ltac mynsatz_compute x := nsatz_compute x. |
| 58 |
End mynsatz_compute. |
| 59 |
Ltac nsatz_compute x := mynsatz_compute x. |
| 60 |
|
| 61 |
Ltac nsatz_compute_to_goal sugar nparams reified_goal power reified_givens := |
| 62 |
nsatz_compute (PEc sugar :: PEc nparams :: PEpow reified_goal power :: reified_givens). |
| 63 |
|
| 64 |
Ltac nsatz_compute_get_leading_coefficient := |
| 65 |
lazymatch goal with |
| 66 |
|- Logic.eq ((?a :: _ :: ?b) :: ?c) _ -> _ => a |
| 67 |
end. |
| 68 |
|
| 69 |
Ltac nsatz_compute_get_certificate := |
| 70 |
lazymatch goal with |
| 71 |
|- Logic.eq ((?a :: _ :: ?b) :: ?c) _ -> _ => constr:((c,b)) |
| 72 |
end. |
| 73 |
|
| 74 |
Ltac nsatz_rewrite_and_revert domain := |
| 75 |
lazymatch type of domain with |
| 76 |
| @Integral_domain.Integral_domain ?F ?zero _ _ _ _ _ ?eq ?Fops ?FRing ?FCring => |
| 77 |
lazymatch goal with |
| 78 |
| |- eq _ zero => idtac |
| 79 |
| |- eq _ _ => rewrite <-(cring_sub_diag_iff (cring:=FCring)) |
| 80 |
end; |
| 81 |
repeat match goal with |
| 82 |
| [H : eq _ zero |- _ ] => revert H |
| 83 |
| [H : eq _ _ |- _ ] => rewrite <-(cring_sub_diag_iff (cring:=FCring)) in H; revert H |
| 84 |
end |
| 85 |
end. |
| 86 |
|
| 87 |
(** As per https://coq.inria.fr/bugs/show_bug.cgi?id=4851, [nsatz] |
| 88 |
cannot handle duplicate hypotheses. So we clear them. *) |
| 89 |
Ltac nsatz_clear_duplicates_for_bug_4851 domain := |
| 90 |
lazymatch type of domain with |
| 91 |
| @Integral_domain.Integral_domain _ _ _ _ _ _ _ ?eq _ _ _ => |
| 92 |
repeat match goal with |
| 93 |
| [ H : eq ?x ?y, H' : eq ?x ?y |- _ ] => clear H' |
| 94 |
end |
| 95 |
end. |
| 96 |
|
| 97 |
Ltac nsatz_nonzero := |
| 98 |
try solve [apply Integral_domain.integral_domain_one_zero |
| 99 |
|apply Integral_domain.integral_domain_minus_one_zero |
| 100 |
|trivial |
| 101 |
|assumption]. |
| 102 |
|
| 103 |
Ltac nsatz_domain_sugar_power domain sugar power := |
| 104 |
let nparams := constr:(BinInt.Zneg BinPos.xH) in (* some symbols can be "parameters", treated as coefficients *) |
| 105 |
lazymatch type of domain with |
| 106 |
| @Integral_domain.Integral_domain ?F ?zero _ _ _ _ _ ?eq ?Fops ?FRing ?FCring => |
| 107 |
nsatz_clear_duplicates_for_bug_4851 domain; |
| 108 |
nsatz_rewrite_and_revert domain; |
| 109 |
let reified_package := nsatz_reify_equations eq zero in |
| 110 |
let fv := nsatz_get_free_variables reified_package in |
| 111 |
let interp := constr:(@Nsatz.PEevalR _ _ _ _ _ _ _ _ Fops fv) in |
| 112 |
let reified_givens := nsatz_get_reified_givens reified_package in |
| 113 |
let reified_goal := nsatz_get_reified_goal reified_package in |
| 114 |
nsatz_compute_to_goal sugar nparams reified_goal power reified_givens; |
| 115 |
let a := nsatz_compute_get_leading_coefficient in |
| 116 |
let crt := nsatz_compute_get_certificate in |
| 117 |
intros _ (* discard [nsatz_compute] output *); intros; |
| 118 |
apply (fun Haa refl cond => @Integral_domain.Rintegral_domain_pow _ _ _ _ _ _ _ _ _ _ _ domain (interp a) _ (BinNat.N.to_nat power) Haa (@Nsatz.check_correct _ _ _ _ _ _ _ _ _ _ FCring fv reified_givens (PEmul a (PEpow reified_goal power)) crt refl cond)); |
| 119 |
[ nsatz_nonzero; cbv iota beta delta [Nsatz.PEevalR PEeval InitialRing.gen_phiZ InitialRing.gen_phiPOS] |
| 120 |
| solve [vm_cast_no_check (eq_refl true)] (* if this fails, the prover returned a bad certificate *) |
| 121 |
| solve [repeat (split; [assumption|]); exact I] ] |
| 122 |
end. |
| 123 |
|
| 124 |
Ltac nsatz_guess_domain := |
| 125 |
match goal with |
| 126 |
| |- ?eq _ _ => constr:(_:Integral_domain.Integral_domain (ring_eq:=eq)) |
| 127 |
| |- not (?eq _ _) => constr:(_:Integral_domain.Integral_domain (ring_eq:=eq)) |
| 128 |
| [H: ?eq _ _ |- _ ] => constr:(_:Integral_domain.Integral_domain (ring_eq:=eq)) |
| 129 |
| [H: not (?eq _ _) |- _] => constr:(_:Integral_domain.Integral_domain (ring_eq:=eq)) |
| 130 |
end. |
| 131 |
|
| 132 |
Ltac nsatz_sugar_power sugar power := |
| 133 |
let domain := nsatz_guess_domain in |
| 134 |
nsatz_domain_sugar_power domain sugar power. |
| 135 |
|
| 136 |
Ltac nsatz_power power := |
| 137 |
let power_N := (eval compute in (BinNat.N.of_nat power)) in |
| 138 |
nsatz_sugar_power BinInt.Z0 power_N. |
| 139 |
|
| 140 |
Ltac nsatz := nsatz_power 1%nat. |
| 141 |
|
| 142 |
Tactic Notation "nsatz" := nsatz. |
| 143 |
Tactic Notation "nsatz" constr(n) := nsatz_power n. |
| 144 |
|
| 145 |
(** If the goal is of the form [?x <> ?y] and assuming [?x = ?y] |
| 146 |
contradicts any hypothesis of the form [?x' <> ?y'], we turn this |
| 147 |
problem about inequalities into one about equalities and give it |
| 148 |
to [nsatz]. *) |
| 149 |
Ltac nsatz_contradict_single_hypothesis domain := |
| 150 |
lazymatch type of domain with |
| 151 |
| @Integral_domain.Integral_domain _ ?zero ?one _ _ _ _ ?eq ?Fops ?FRing ?FCring => |
| 152 |
unfold not in *; |
| 153 |
match goal with |
| 154 |
| [ H : eq _ _ -> False |- eq _ _ -> False ] |
| 155 |
=> intro; apply H; nsatz |
| 156 |
| [ H : eq _ _ -> False |- False ] |
| 157 |
=> apply H; nsatz |
| 158 |
end |
| 159 |
end. |
| 160 |
|
| 161 |
Ltac nsatz_contradict := |
| 162 |
let domain := nsatz_guess_domain in |
| 163 |
nsatz_contradict_single_hypothesis domain |
| 164 |
|| (unfold not; |
| 165 |
intros; |
| 166 |
lazymatch type of domain with |
| 167 |
| @Integral_domain.Integral_domain _ ?zero ?one _ _ _ _ ?eq ?Fops ?FRing ?FCring => |
| 168 |
assert (eq one zero) as Hbad; |
| 169 |
[nsatz; nsatz_nonzero |
| 170 |
|destruct (Integral_domain.integral_domain_one_zero (Integral_domain:=domain) Hbad)] |
| 171 |
end). |