| 1 |
From Coq Require Import Lia ZArith. |
| 2 |
Require Import Crypto.Util.Tactics.Contains. |
| 3 |
Require Import Crypto.Util.Tactics.Not. |
| 4 |
Local Open Scope Z_scope. |
| 5 |
|
| 6 |
Module Z. |
| 7 |
Lemma move_R_pX x y z : x + y = z -> x = z - y. |
| 8 |
Proof. lia. Qed. |
| 9 |
Lemma move_R_mX x y z : x - y = z -> x = z + y. |
| 10 |
Proof. lia. Qed. |
| 11 |
Lemma move_R_Xp x y z : y + x = z -> x = z - y. |
| 12 |
Proof. lia. Qed. |
| 13 |
Lemma move_R_Xm x y z : y - x = z -> x = y - z. |
| 14 |
Proof. lia. Qed. |
| 15 |
Lemma move_R_nX x y : -x = y -> x = -y. |
| 16 |
Proof. lia. Qed. |
| 17 |
Lemma move_L_pX x y z : z = x + y -> z - y = x. |
| 18 |
Proof. lia. Qed. |
| 19 |
Lemma move_L_mX x y z : z = x - y -> z + y = x. |
| 20 |
Proof. lia. Qed. |
| 21 |
Lemma move_L_Xp x y z : z = y + x -> z - y = x. |
| 22 |
Proof. lia. Qed. |
| 23 |
Lemma move_L_Xm x y z : z = y - x -> y - z = x. |
| 24 |
Proof. lia. Qed. |
| 25 |
Lemma move_L_nX x y : y = -x -> -y = x. |
| 26 |
Proof. lia. Qed. |
| 27 |
|
| 28 |
(** [linear_substitute x] attempts to maipulate equations using only |
| 29 |
addition and subtraction to put [x] on the left, and then |
| 30 |
eliminates [x]. Currently, it only handles equations where [x] |
| 31 |
appears once; it does not yet handle equations like [x - x + x = |
| 32 |
5]. *) |
| 33 |
Ltac linear_solve_for_in_step for_var H := |
| 34 |
let LHS := match type of H with ?LHS = ?RHS => LHS end in |
| 35 |
let RHS := match type of H with ?LHS = ?RHS => RHS end in |
| 36 |
first [ match RHS with |
| 37 |
| ?a + ?b |
| 38 |
=> first [ contains for_var b; apply move_L_pX in H |
| 39 |
| contains for_var a; apply move_L_Xp in H ] |
| 40 |
| ?a - ?b |
| 41 |
=> first [ contains for_var b; apply move_L_mX in H |
| 42 |
| contains for_var a; apply move_L_Xm in H ] |
| 43 |
| -?a |
| 44 |
=> contains for_var a; apply move_L_nX in H |
| 45 |
| for_var |
| 46 |
=> progress symmetry in H |
| 47 |
end |
| 48 |
| match LHS with |
| 49 |
| ?a + ?b |
| 50 |
=> first [ not contains for_var b; apply move_R_pX in H |
| 51 |
| not contains for_var a; apply move_R_Xp in H ] |
| 52 |
| ?a - ?b |
| 53 |
=> first [ not contains for_var b; apply move_R_mX in H |
| 54 |
| not contains for_var a; apply move_R_Xm in H ] |
| 55 |
| -?a |
| 56 |
=> contains for_var a; apply move_R_nX in H |
| 57 |
end ]. |
| 58 |
Ltac linear_solve_for_in for_var H := repeat linear_solve_for_in_step for_var H. |
| 59 |
Ltac linear_solve_for for_var := |
| 60 |
match goal with |
| 61 |
| [ H : for_var = _ |- _ ] => idtac |
| 62 |
| [ H : context[for_var] |- _ ] |
| 63 |
=> linear_solve_for_in for_var H; |
| 64 |
lazymatch type of H with |
| 65 |
| for_var = _ => idtac |
| 66 |
| ?T => fail 0 "Could not fully solve for" for_var "in" T "(hypothesis" H ")" |
| 67 |
end |
| 68 |
end. |
| 69 |
Ltac linear_substitute for_var := linear_solve_for for_var; subst for_var. |
| 70 |
Ltac linear_substitute_all := |
| 71 |
repeat match goal with |
| 72 |
| [ v : Z |- _ ] => linear_substitute v |
| 73 |
end. |
| 74 |
End Z. |