Debian Codegraph @

src/Util/ZUtil/Tactics/SimplifyFractionsLe.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import Lia.
3 Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds.
4 Require Import Crypto.Util.ZUtil.Div.
5 Local Open Scope Z_scope.
6
7 Module Z.
8 (** * [Z.simplify_fractions_le] *)
9 (** The culmination of this series of tactics,
10 [Z.simplify_fractions_le], will use the fact that [a * (b / c) <=
11 (a * b) / c], and do some reasoning modulo associativity and
12 commutativity in [Z] to perform such a reduction. It may leave
13 over goals if it cannot prove that some denominators are non-zero.
14 If the rewrite [a * (b / c)] → [(a * b) / c] is safe to do on the
15 LHS of the goal, this tactic should not turn a solvable goal into
16 an unsolvable one.
17
18 After running, the tactic does some basic rewriting to simplify
19 fractions, e.g., that [a * b / b = a]. *)
20 Ltac split_sums_step :=
21 match goal with
22 | [ |- _ + _ <= _ ]
23 => etransitivity; [ eapply Z.add_le_mono | ]
24 | [ |- _ - _ <= _ ]
25 => etransitivity; [ eapply Z.sub_le_mono | ]
26 end.
27 Ltac split_sums :=
28 try (split_sums_step; [ split_sums.. | ]).
29 Ltac pre_reorder_fractions_step :=
30 match goal with
31 | [ |- context[?x / ?y * ?z] ]
32 => lazymatch z with
33 | context[_ / _] => fail
34 | _ => idtac
35 end;
36 rewrite (Z.mul_comm (x / y) z)
37 | _ => let LHS := match goal with |- ?LHS <= ?RHS => LHS end in
38 match LHS with
39 | context G[?x * (?y / ?z)]
40 => let G' := context G[(x * y) / z] in
41 transitivity G'
42 end
43 end.
44 Ltac pre_reorder_fractions :=
45 try first [ split_sums_step; [ pre_reorder_fractions.. | ]
46 | pre_reorder_fractions_step; [ .. | pre_reorder_fractions ] ].
47 Ltac split_comparison :=
48 match goal with
49 | [ |- ?x <= ?x ] => reflexivity
50 | [ H : _ >= _ |- _ ]
51 => apply Z.ge_le_iff in H
52 | [ |- ?x * ?y <= ?z * ?w ]
53 => lazymatch goal with
54 | [ H : 0 <= x |- _ ] => idtac
55 | [ H : x < 0 |- _ ] => fail
56 | _ => destruct (Z_lt_le_dec x 0)
57 end;
58 [ ..
59 | lazymatch goal with
60 | [ H : 0 <= y |- _ ] => idtac
61 | [ H : y < 0 |- _ ] => fail
62 | _ => destruct (Z_lt_le_dec y 0)
63 end;
64 [ ..
65 | apply Zmult_le_compat; [ | | assumption | assumption ] ] ]
66 | [ |- ?x / ?y <= ?z / ?y ]
67 => lazymatch goal with
68 | [ H : 0 < y |- _ ] => idtac
69 | [ H : y <= 0 |- _ ] => fail
70 | _ => destruct (Z_lt_le_dec 0 y)
71 end;
72 [ apply Z_div_le; [ apply Z.gt_lt_iff; assumption | ]
73 | .. ]
74 | [ |- ?x / ?y <= ?x / ?z ]
75 => lazymatch goal with
76 | [ H : 0 <= x |- _ ] => idtac
77 | [ H : x < 0 |- _ ] => fail
78 | _ => destruct (Z_lt_le_dec x 0)
79 end;
80 [ ..
81 | lazymatch goal with
82 | [ H : 0 < z |- _ ] => idtac
83 | [ H : z <= 0 |- _ ] => fail
84 | _ => destruct (Z_lt_le_dec 0 z)
85 end;
86 [ apply Z.div_le_compat_l; [ assumption | split; [ assumption | ] ]
87 | .. ] ]
88 | [ |- _ + _ <= _ + _ ]
89 => apply Z.add_le_mono
90 | [ |- _ - _ <= _ - _ ]
91 => apply Z.sub_le_mono
92 | [ |- ?x * (?y / ?z) <= (?x * ?y) / ?z ]
93 => apply Z.div_mul_le
94 end.
95 Ltac split_comparison_fin_step :=
96 match goal with
97 | _ => assumption
98 | _ => lia
99 | _ => progress subst
100 | [ H : ?n * ?m < 0 |- _ ]
101 => apply (proj1 (Z.lt_mul_0 n m)) in H; destruct H as [ [??]|[??] ]
102 | [ H : ?n / ?m < 0 |- _ ]
103 => apply (proj1 (Z.lt_div_0 n m)) in H; destruct H as [ [ [??]|[??] ] ? ]
104 | [ H : (?x^?y) <= ?n < _, H' : ?n < 0 |- _ ]
105 => assert (0 <= x^y) by Z.zero_bounds; lia
106 | [ H : (?x^?y) < 0 |- _ ]
107 => assert (0 <= x^y) by Z.zero_bounds; lia
108 | [ H : (?x^?y) <= 0 |- _ ]
109 => let H' := fresh in
110 assert (H' : 0 <= x^y) by Z.zero_bounds;
111 assert (x^y = 0) by lia;
112 clear H H'
113 | [ H : _^_ = 0 |- _ ]
114 => apply Z.pow_eq_0_iff in H; destruct H as [ ?|[??] ]
115 | [ H : 0 <= ?x, H' : ?x - 1 < 0 |- _ ]
116 => assert (x = 0) by lia; clear H H'
117 | [ |- ?x <= ?y ] => is_evar x; reflexivity
118 | [ |- ?x <= ?y ] => is_evar y; reflexivity
119 end.
120 Ltac split_comparison_fin := repeat split_comparison_fin_step.
121 Ltac simplify_fractions_step :=
122 match goal with
123 | _ => rewrite Z.div_mul by (try apply Z.pow_nonzero; Z.zero_bounds)
124 | [ |- context[?x * ?y / ?x] ]
125 => rewrite (Z.mul_comm x y)
126 | [ |- ?x <= ?x ] => reflexivity
127 end.
128 Ltac simplify_fractions := repeat simplify_fractions_step.
129 Ltac simplify_fractions_le :=
130 pre_reorder_fractions;
131 [ repeat split_comparison; split_comparison_fin; Z.zero_bounds..
132 | simplify_fractions ].
133 End Z.