Debian Codegraph @

src/Util/ZUtil/Tactics/LtbToLt.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import Lia.
3 Require Import Crypto.Util.Bool.
4 Local Open Scope Z_scope.
5
6 Module Z.
7 Lemma eqb_cases x y : if x =? y then x = y else x <> y.
8 Proof.
9 pose proof (Z.eqb_spec x y) as H.
10 inversion H; trivial.
11 Qed.
12
13 Lemma geb_spec0 : forall x y : Z, Bool.reflect (x >= y) (x >=? y).
14 Proof.
15 intros x y; pose proof (Zge_cases x y) as H; destruct (Z.geb x y); constructor; lia.
16 Qed.
17 Lemma gtb_spec0 : forall x y : Z, Bool.reflect (x > y) (x >? y).
18 Proof.
19 intros x y; pose proof (Zgt_cases x y) as H; destruct (Z.gtb x y); constructor; lia.
20 Qed.
21
22 Ltac ltb_to_lt_with_hyp H lem :=
23 let H' := fresh in
24 rename H into H';
25 pose proof lem as H;
26 rewrite H' in H;
27 clear H'.
28
29 Ltac ltb_to_lt_in_goal b' lem :=
30 refine (proj1 (@reflect_iff_gen _ _ lem b') _);
31 cbv beta iota.
32
33 Ltac ltb_to_lt_hyps_step :=
34 match goal with
35 | [ H : (?x <? ?y) = ?b |- _ ]
36 => ltb_to_lt_with_hyp H (Zlt_cases x y)
37 | [ H : (?x <=? ?y) = ?b |- _ ]
38 => ltb_to_lt_with_hyp H (Zle_cases x y)
39 | [ H : (?x >? ?y) = ?b |- _ ]
40 => ltb_to_lt_with_hyp H (Zgt_cases x y)
41 | [ H : (?x >=? ?y) = ?b |- _ ]
42 => ltb_to_lt_with_hyp H (Zge_cases x y)
43 | [ H : (?x =? ?y) = ?b |- _ ]
44 => ltb_to_lt_with_hyp H (eqb_cases x y)
45 end.
46 Ltac ltb_to_lt_goal_step :=
47 match goal with
48 | [ |- (?x <? ?y) = ?b ]
49 => ltb_to_lt_in_goal b (Z.ltb_spec0 x y)
50 | [ |- (?x <=? ?y) = ?b ]
51 => ltb_to_lt_in_goal b (Z.leb_spec0 x y)
52 | [ |- (?x >? ?y) = ?b ]
53 => ltb_to_lt_in_goal b (Z.gtb_spec0 x y)
54 | [ |- (?x >=? ?y) = ?b ]
55 => ltb_to_lt_in_goal b (Z.geb_spec0 x y)
56 | [ |- (?x =? ?y) = ?b ]
57 => ltb_to_lt_in_goal b (Z.eqb_spec x y)
58 end.
59 Ltac ltb_to_lt_step :=
60 first [ ltb_to_lt_hyps_step
61 | ltb_to_lt_goal_step ].
62 Ltac ltb_to_lt := repeat ltb_to_lt_step.
63
64 Section R_Rb.
65 Local Ltac t := intros ? ? []; split; intro; ltb_to_lt; lia.
66 Local Notation R_Rb Rb R nR := (forall x y b, Rb x y = b <-> if b then R x y else nR x y).
67 Lemma ltb_lt_iff : R_Rb Z.ltb Z.lt Z.ge. Proof. t. Qed.
68 Lemma leb_le_iff : R_Rb Z.leb Z.le Z.gt. Proof. t. Qed.
69 Lemma gtb_gt_iff : R_Rb Z.gtb Z.gt Z.le. Proof. t. Qed.
70 Lemma geb_ge_iff : R_Rb Z.geb Z.ge Z.lt. Proof. t. Qed.
71 Lemma eqb_eq_iff : R_Rb Z.eqb (@Logic.eq Z) (fun x y => x <> y). Proof. t. Qed.
72 End R_Rb.
73 #[global]
74 Hint Rewrite ltb_lt_iff leb_le_iff gtb_gt_iff geb_ge_iff eqb_eq_iff : ltb_to_lt.
75 Ltac ltb_to_lt_in_context :=
76 repeat autorewrite with ltb_to_lt in *;
77 cbv beta iota in *.
78 End Z.