| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import QArith_base. |
| 3 |
From Coq Require Import Lia. |
| 4 |
From Coq Require Import List. |
| 5 |
Require Import Crypto.Util.SideConditions.ReductionPackages. |
| 6 |
Require Import Crypto.Util.Tuple. |
| 7 |
Require Import Crypto.Util.Decidable. |
| 8 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 9 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 10 |
Local Open Scope Z_scope. |
| 11 |
|
| 12 |
Create HintDb dec2bool discriminated. |
| 13 |
|
| 14 |
Lemma dec_Z_lt_to_bool a b |
| 15 |
: (if dec (a < b) then true else false) = Z.ltb a b. |
| 16 |
Proof. |
| 17 |
destruct (Z.ltb a b) eqn:?; break_match; Z.ltb_to_lt; try reflexivity; lia. |
| 18 |
Qed. |
| 19 |
#[global] |
| 20 |
Hint Rewrite dec_Z_lt_to_bool : dec2bool. |
| 21 |
Lemma dec_Z_le_to_bool a b |
| 22 |
: (if dec (a <= b) then true else false) = Z.leb a b. |
| 23 |
Proof. |
| 24 |
destruct (Z.leb a b) eqn:?; break_match; Z.ltb_to_lt; try reflexivity; lia. |
| 25 |
Qed. |
| 26 |
#[global] |
| 27 |
Hint Rewrite dec_Z_le_to_bool : dec2bool. |
| 28 |
Lemma dec_Z_eq_to_bool a b |
| 29 |
: (if dec (a = b) then true else false) = Z.eqb a b. |
| 30 |
Proof. |
| 31 |
destruct (Z.eqb a b) eqn:?; break_match; Z.ltb_to_lt; try reflexivity; lia. |
| 32 |
Qed. |
| 33 |
#[global] |
| 34 |
Hint Rewrite dec_Z_eq_to_bool : dec2bool. |
| 35 |
Lemma dec_nat_eq_to_bool a b |
| 36 |
: (if dec (a = b) then true else false) = Nat.eqb a b. |
| 37 |
Proof. |
| 38 |
destruct (Nat.eqb a b) eqn:H; break_match; try reflexivity. |
| 39 |
{ apply Nat.eqb_eq in H; congruence. } |
| 40 |
{ rewrite Nat.eqb_refl in H; congruence. } |
| 41 |
Qed. |
| 42 |
#[global] |
| 43 |
Hint Rewrite dec_nat_eq_to_bool : dec2bool. |
| 44 |
Lemma dec_bool_eq_to_bool_if a b |
| 45 |
: (if dec (a = b :> bool) then true else false) = if b then a else negb a. |
| 46 |
Proof. |
| 47 |
destruct a, b; reflexivity. |
| 48 |
Qed. |
| 49 |
#[global] |
| 50 |
Hint Rewrite dec_bool_eq_to_bool_if : dec2bool. |
| 51 |
Lemma dec_not_negb {P} {H : Decidable P} |
| 52 |
: (if dec (not P) then true else false) |
| 53 |
= negb (if dec P then true else false). |
| 54 |
Proof. |
| 55 |
do 2 edestruct dec; try reflexivity; tauto. |
| 56 |
Qed. |
| 57 |
#[global] |
| 58 |
Hint Rewrite @dec_not_negb : dec2bool. |
| 59 |
Lemma dec_Q_le_to_bool a b |
| 60 |
: (if dec (a <= b)%Q then true else false) = Qle_bool a b. |
| 61 |
Proof. |
| 62 |
destruct (Qle_bool a b) eqn:?; cbv [Qle Qle_bool] in *; |
| 63 |
break_match; Z.ltb_to_lt; try reflexivity; lia. |
| 64 |
Qed. |
| 65 |
#[global] |
| 66 |
Hint Rewrite dec_Q_le_to_bool : dec2bool. |
| 67 |
Lemma dec_True_to_bool |
| 68 |
: (if dec True then true else false) = true. |
| 69 |
Proof. reflexivity. Qed. |
| 70 |
#[global] |
| 71 |
Hint Rewrite dec_True_to_bool : dec2bool. |
| 72 |
Lemma dec_False_to_bool |
| 73 |
: (if dec False then true else false) = false. |
| 74 |
Proof. reflexivity. Qed. |
| 75 |
#[global] |
| 76 |
Hint Rewrite dec_False_to_bool : dec2bool. |
| 77 |
Lemma dec_ifb_to_bool {b : bool} {A B} {HA : Decidable A} {HB : Decidable B} |
| 78 |
: (if dec (if b then A else B) then true else false) |
| 79 |
= if b then (if dec A then true else false) else (if dec B then true else false). |
| 80 |
Proof. destruct b; reflexivity. Qed. |
| 81 |
#[global] |
| 82 |
Hint Rewrite @dec_ifb_to_bool : dec2bool. |
| 83 |
Lemma dec_and_to_bool {A B} {HA : Decidable A} {HB : Decidable B} |
| 84 |
: (if dec (A /\ B) then true else false) |
| 85 |
= andb (if dec A then true else false) (if dec B then true else false). |
| 86 |
Proof. do 3 edestruct dec; try reflexivity; tauto. Qed. |
| 87 |
#[global] |
| 88 |
Hint Rewrite @dec_and_to_bool : dec2bool. |
| 89 |
Lemma dec_or_to_bool {A B} {HA : Decidable A} {HB : Decidable B} |
| 90 |
: (if dec (A \/ B) then true else false) |
| 91 |
= orb (if dec A then true else false) (if dec B then true else false). |
| 92 |
Proof. do 3 edestruct dec; try reflexivity; tauto. Qed. |
| 93 |
#[global] |
| 94 |
Hint Rewrite @dec_or_to_bool : dec2bool. |
| 95 |
Lemma dec_fieldwise_to_bool {A B R n} {H : forall x y, Decidable (R x y)} x y |
| 96 |
: (if dec (@Tuple.fieldwise A B n R x y) then true else false) |
| 97 |
= @Tuple.fieldwiseb A B n (fun x y => if dec (R x y) then true else false) x y. |
| 98 |
Proof. |
| 99 |
destruct (dec _) as [H'|H']. |
| 100 |
{ symmetry; rewrite Tuple.fieldwiseb_fieldwise; [ eassumption | ]. |
| 101 |
intros; edestruct dec; split; auto; discriminate. } |
| 102 |
{ destruct (Tuple.fieldwiseb _ _ _) eqn:H''; [ | reflexivity ]. |
| 103 |
rewrite Tuple.fieldwiseb_fieldwise in H''. |
| 104 |
{ exfalso; apply H', H''. } |
| 105 |
{ intros; edestruct dec; split; auto; discriminate. } } |
| 106 |
Qed. |
| 107 |
#[global] |
| 108 |
Hint Rewrite @dec_fieldwise_to_bool : dec2bool. |
| 109 |
Lemma dec_tuple_eq_to_bool T {H : DecidableRel (@eq T)} n (x y : Tuple.tuple T n) |
| 110 |
: (if dec (x = y) then true else false) = Tuple.fieldwiseb (fun x y => if dec (x = y) then true else false) x y. |
| 111 |
Proof. |
| 112 |
destruct (dec _) as [H'|H'], (Tuple.fieldwiseb _ _ _) eqn:H''; try reflexivity; |
| 113 |
rewrite <- Tuple.fieldwise_eq_iff in H'; |
| 114 |
first [ exfalso; rewrite Tuple.fieldwiseb_fieldwise in H'' |
| 115 |
| rewrite <- H''; clear H''; symmetry; rewrite Tuple.fieldwiseb_fieldwise ]; |
| 116 |
try eassumption; eauto; intros; destruct dec; split; auto; congruence. |
| 117 |
Qed. |
| 118 |
#[global] |
| 119 |
Hint Rewrite dec_tuple_eq_to_bool : dec2bool. |
| 120 |
|
| 121 |
Require Import Crypto.Util.ListUtil.Forall. |
| 122 |
Lemma dec_Forall_to_bool {A P} {H : forall x, Decidable (P x)} ls |
| 123 |
: (if dec (@Forall A P ls) then true else false) |
| 124 |
= Forallb (fun x => if dec (P x) then true else false) ls. |
| 125 |
Proof. |
| 126 |
destruct (dec _) as [H'|H'], (Forallb _ _) eqn:H''; |
| 127 |
try reflexivity; [ symmetry; rewrite <- H''; clear H'' | exfalso; apply H' ]; |
| 128 |
[ rewrite Forall_Forallb_iff; [ eassumption | ] |
| 129 |
| rewrite <- Forall_Forallb_iff; [ eassumption | ] ]; |
| 130 |
intros; cbv beta; destruct (dec _); intuition; try congruence. |
| 131 |
Qed. |
| 132 |
#[global] |
| 133 |
Hint Rewrite @dec_Forall_to_bool : dec2bool. |
| 134 |
|
| 135 |
Ltac dec2bool_split_hyp H := |
| 136 |
lazymatch type of H with |
| 137 |
| _ /\ _ => destruct H as [H _]; dec2bool_split_hyp H |
| 138 |
| andb _ _ = true => apply Bool.andb_true_iff in H; destruct H as [H _]; dec2bool_split_hyp H |
| 139 |
| ?e = true => is_evar e; apply Bool.andb_true_iff in H; destruct H as [_ H] |
| 140 |
end. |
| 141 |
|
| 142 |
Ltac premake_parameter_package _ := |
| 143 |
let H := fresh in |
| 144 |
eexists; intro H; |
| 145 |
unshelve econstructor; |
| 146 |
repeat match goal with |
| 147 |
| [ |- context[cbv_minus_then_vm_decide_package ?x ?y] ] |
| 148 |
=> change (cbv_minus_then_vm_decide_package x y) with (vm_decide_package y) |
| 149 |
| [ |- context[vm_compute_reflexivity_package ?x] ] |
| 150 |
=> change (vm_compute_reflexivity_package ?x) with (vm_decide_package x) |
| 151 |
end; |
| 152 |
autounfold with dec2bool; |
| 153 |
repeat autorewrite with dec2bool. |
| 154 |
Ltac push_dec_to_hyps precheck := |
| 155 |
let rec check R := |
| 156 |
lazymatch R with |
| 157 |
| Z.ltb _ _ => idtac |
| 158 |
| Z.leb _ _ => idtac |
| 159 |
| Z.eqb _ _ => idtac |
| 160 |
| Qle_bool _ _ => idtac |
| 161 |
| Nat.eqb _ _ => idtac |
| 162 |
| negb ?b => check b |
| 163 |
| true => idtac |
| 164 |
| false => idtac |
| 165 |
| match ?b with true => ?a | false => ?c end |
| 166 |
=> check a; check b; check c |
| 167 |
| andb ?a ?b => check a; check b |
| 168 |
| orb ?a ?b => check a; check b |
| 169 |
| (fun (a : ?A) => ?R) |
| 170 |
=> let dummy := constr:(fun (a : A) => |
| 171 |
let R' := R in |
| 172 |
ltac:(let R'' := (eval cbv beta delta [R'] in R') in |
| 173 |
check R''; |
| 174 |
exact I)) in |
| 175 |
idtac |
| 176 |
| ?R => precheck R |
| 177 |
end in |
| 178 |
repeat lazymatch goal with |
| 179 |
| [ |- vm_decide_package (Tuple.fieldwiseb _ _ _ = true) ] |
| 180 |
=> cbv [vm_decide_package]; |
| 181 |
rewrite Tuple.fieldwiseb_fieldwise, <- Tuple.fieldwiseb_fieldwise |
| 182 |
by (intros; autorewrite with dec2bool; apply reflexivity) |
| 183 |
| [ |- vm_decide_package (Forallb _ _ = true) ] |
| 184 |
=> cbv [vm_decide_package]; |
| 185 |
rewrite Forall_Forallb_iff, <- Forall_Forallb_iff |
| 186 |
by (intros; autorewrite with dec2bool; apply reflexivity) |
| 187 |
| [ H : ?e = true |- vm_decide_package (?R = true) ] |
| 188 |
=> check R; dec2bool_split_hyp H; cbv [vm_decide_package]; exact H |
| 189 |
| [ H : ?e = true |- Tuple.fieldwiseb ?R ?t1 ?t2 = true ] |
| 190 |
=> check (fun a b => R a b); dec2bool_split_hyp H; cbv [vm_decide_package]; exact H |
| 191 |
| [ H : ?e = true |- Forallb ?R ?ls = true ] |
| 192 |
=> check (fun a => R a); dec2bool_split_hyp H; cbv [vm_decide_package]; exact H |
| 193 |
end. |
| 194 |
Ltac make_parameter_package_for_vm_decide precheck := |
| 195 |
premake_parameter_package (); |
| 196 |
unshelve (push_dec_to_hyps precheck); |
| 197 |
exact true. |