Debian Codegraph @

src/Util/ZUtil/Tactics/DivModToQuotRem.v

raw
1 From Coq Require Import ZArith.
2 Require Import Crypto.Util.ZUtil.Div.Bootstrap.
3 Require Import Crypto.Util.ZUtil.Modulo.Bootstrap.
4 Require Import Crypto.Util.ZUtil.Hints.Core.
5 Local Open Scope Z_scope.
6
7 Module Z.
8 (** [div_mod_to_quot_rem] replaces [x / y] and [x mod y] with new
9 variables [q] and [r] while simultaneously adding facts that
10 uniquely specify [q] and [r] to the context (roughly, that [y *
11 q + r = x] and that [0 <= r < y]. *)
12 Ltac div_mod_to_quot_rem_inequality_solver :=
13 zutil_arith_more_inequalities.
14 Ltac generalize_div_eucl x y :=
15 let H := fresh in
16 let H' := fresh in
17 first
18 [ assert (H' : y <> 0) by div_mod_to_quot_rem_inequality_solver;
19 generalize (Z.div_mod x y H'); clear H';
20 first [ assert (H' : 0 < y) by div_mod_to_quot_rem_inequality_solver;
21 generalize (Z.mod_pos_bound x y H'); clear H'
22 | assert (H' : y < 0) by div_mod_to_quot_rem_inequality_solver;
23 generalize (Z.mod_neg_bound x y H'); clear H'
24 | assert (H' : y = 0) by div_mod_to_quot_rem_inequality_solver;
25 generalize (Z.div_0_r_eq x y H') (Z.mod_0_r_eq x y H'); clear H'
26 | assert (H' : y < 0 \/ 0 < y) by (apply Z.neg_pos_cases; div_mod_to_quot_rem_inequality_solver);
27 let H'' := fresh in
28 assert (H'' : y < x mod y <= 0 \/ 0 <= x mod y < y)
29 by (destruct H'; [ left; apply Z.mod_neg_bound; assumption
30 | right; apply Z.mod_pos_bound; assumption ]);
31 clear H'; revert H'' ]
32 | generalize (Z.div_mod_cases x y) ];
33 let q := fresh "q" in
34 let r := fresh "r" in
35 set (q := x / y) in *;
36 set (r := x mod y) in *;
37 clearbody q r.
38
39 Ltac div_mod_to_quot_rem_step :=
40 match goal with
41 | [ |- context[?x / ?y] ] => generalize_div_eucl x y
42 | [ |- context[?x mod ?y] ] => generalize_div_eucl x y
43 end.
44
45 Ltac div_mod_to_quot_rem_hyps_step :=
46 match goal with
47 | [ H : context[?x / ?y] |- _ ] => generalize_div_eucl x y
48 | [ H : context[?x mod ?y] |- _ ] => generalize_div_eucl x y
49 end.
50
51 Ltac div_mod_to_quot_rem := repeat first [ div_mod_to_quot_rem_step | div_mod_to_quot_rem_hyps_step ]; intros.
52 Ltac div_mod_to_quot_rem_in_goal := repeat div_mod_to_quot_rem_step; intros.
53 End Z.