| 1 |
(** Typeclass for decidable propositions *) |
| 2 |
|
| 3 |
From Coq Require Import Eqdep_dec. |
| 4 |
From Coq Require Import List. |
| 5 |
Require Import Crypto.Util.FixCoqMistakes. |
| 6 |
Require Import Crypto.Util.Sigma. |
| 7 |
Require Import Crypto.Util.HProp. |
| 8 |
From Coq Require Import BinInt ZArith_dec. |
| 9 |
From Coq Require Import BinNat. |
| 10 |
|
| 11 |
Local Open Scope type_scope. |
| 12 |
|
| 13 |
Class Decidable (P : Prop) := dec : {P} + {~P}. |
| 14 |
Arguments dec _%type_scope {_}. |
| 15 |
Notation DecidableRel R := (forall x y, Decidable (R x y)). |
| 16 |
|
| 17 |
Global Instance hprop_eq_dec {A} `{DecidableRel (@eq A)} : IsHPropRel (@eq A) | 10. |
| 18 |
Proof. repeat intro; apply UIP_dec; trivial with nocore. Qed. |
| 19 |
|
| 20 |
Global Instance eq_dec_hprop {A} {x y : A} {hp : IsHProp A} : Decidable (@eq A x y) | 5. |
| 21 |
Proof. left; auto. Qed. |
| 22 |
|
| 23 |
Ltac no_equalities_about x0 y0 := |
| 24 |
lazymatch goal with |
| 25 |
| [ H' : x0 = y0 |- _ ] => fail |
| 26 |
| [ H' : y0 = x0 |- _ ] => fail |
| 27 |
| [ H' : x0 <> y0 |- _ ] => fail |
| 28 |
| [ H' : y0 <> x0 |- _ ] => fail |
| 29 |
| _ => idtac |
| 30 |
end. |
| 31 |
|
| 32 |
Ltac destruct_decidable_step := |
| 33 |
match goal with |
| 34 |
| [ H : Decidable _ |- _ ] => destruct H |
| 35 |
| [ H : forall x y : ?A, Decidable (x = y), x0 : ?A, y0 : ?A |- _ ] |
| 36 |
=> no_equalities_about x0 y0; destruct (H x0 y0) |
| 37 |
| [ H : forall a0 (x y : _), Decidable (x = y), x0 : ?A, y0 : ?A |- _ ] |
| 38 |
=> no_equalities_about x0 y0; destruct (H _ x0 y0) |
| 39 |
end. |
| 40 |
Ltac destruct_decidable := repeat destruct_decidable_step. |
| 41 |
|
| 42 |
Ltac pre_decide_destruct_sigma_step := |
| 43 |
match goal with |
| 44 |
| [ H : sigT _ |- _ ] => destruct H |
| 45 |
| [ H : sig _ |- _ ] => destruct H |
| 46 |
| [ H : ex _ |- _ ] => destruct H |
| 47 |
end. |
| 48 |
Ltac pre_decide_destruct_sigma := repeat pre_decide_destruct_sigma_step. |
| 49 |
|
| 50 |
Ltac pre_decide := |
| 51 |
repeat (intros |
| 52 |
|| destruct_decidable |
| 53 |
|| split |
| 54 |
|| pre_decide_destruct_sigma |
| 55 |
|| unfold Decidable in * |
| 56 |
|| hnf). |
| 57 |
|
| 58 |
(** Put the [subst] and reasoning about equalities after the [left] |
| 59 |
and [right]; opaque equality proofs should not block decidability |
| 60 |
proofs. *) |
| 61 |
Ltac post_decide := |
| 62 |
repeat (intros |
| 63 |
|| subst |
| 64 |
|| pre_hprop). |
| 65 |
|
| 66 |
Ltac solve_decidable_transparent_with tac := |
| 67 |
pre_decide; |
| 68 |
try solve [ left; abstract (post_decide; tac) |
| 69 |
| right; abstract (post_decide; tac) |
| 70 |
| decide equality; eauto with nocore ]. |
| 71 |
|
| 72 |
Ltac solve_decidable_transparent := solve_decidable_transparent_with firstorder. |
| 73 |
|
| 74 |
Local Hint Extern 0 => solve [ solve_decidable_transparent ] : typeclass_instances. |
| 75 |
Local Hint Extern 1 => progress inversion_sigma : core. |
| 76 |
|
| 77 |
Global Instance dec_True : Decidable True | 10 := left I. |
| 78 |
Global Instance dec_False : Decidable False | 10 := right (fun x => x). |
| 79 |
Global Instance dec_or {A B} `{Decidable A, Decidable B} : Decidable (A \/ B) | 10. exact _. Defined. |
| 80 |
Global Instance dec_and {A B} `{Decidable A, Decidable B} : Decidable (A /\ B) | 10. exact _. Defined. |
| 81 |
Global Instance dec_impl {A B} `{Decidable (B \/ ~A)} : Decidable (A -> B) | 10. exact _. Defined. |
| 82 |
Global Instance dec_impl_simple {A B} `{Decidable A, Decidable B} : Decidable (A -> B) | 10. exact _. Defined. |
| 83 |
Global Instance dec_iff {A B} `{Decidable A, Decidable B} : Decidable (A <-> B) | 10. exact _. Defined. |
| 84 |
Lemma dec_not {A} `{Decidable A} : Decidable (~A). |
| 85 |
Proof. solve_decidable_transparent. Defined. |
| 86 |
(** Disallow infinite loops of dec_not *) |
| 87 |
Global Hint Extern 0 (Decidable (~?A)) => apply (@dec_not A) : typeclass_instances. |
| 88 |
Global Instance dec_eq_unit : DecidableRel (@eq unit) | 10. exact _. Defined. |
| 89 |
Global Instance dec_eq_bool : DecidableRel (@eq bool) | 10. exact _. Defined. |
| 90 |
Global Instance dec_eq_Empty_set : DecidableRel (@eq Empty_set) | 10. exact _. Defined. |
| 91 |
Global Instance dec_eq_prod {A B} `{DecidableRel (@eq A), DecidableRel (@eq B)} : DecidableRel (@eq (A * B)) | 10. exact _. Defined. |
| 92 |
Global Instance dec_eq_option {A} `{DecidableRel (@eq A)} : DecidableRel (@eq (option A)) | 10. exact _. Defined. |
| 93 |
Global Instance dec_eq_list {A} `{DecidableRel (@eq A)} : DecidableRel (@eq (list A)) | 10. exact _. Defined. |
| 94 |
Global Instance dec_eq_list_nil_r {A} {ls} : Decidable (ls = @nil A) | 10. |
| 95 |
Proof. destruct ls; [ left; reflexivity | right; abstract congruence ]. Defined. |
| 96 |
Global Instance dec_eq_list_nil_l {A} {ls} : Decidable (@nil A = ls) | 10. |
| 97 |
Proof. destruct ls; [ left; reflexivity | right; abstract congruence ]. Defined. |
| 98 |
Global Instance dec_eq_sum {A B} `{DecidableRel (@eq A), DecidableRel (@eq B)} : DecidableRel (@eq (A + B)) | 10. exact _. Defined. |
| 99 |
Global Instance dec_eq_sigT_hprop {A P} `{DecidableRel (@eq A), forall a : A, IsHProp (P a)} : DecidableRel (@eq (@sigT A P)) | 10. exact _. Defined. |
| 100 |
Global Instance dec_eq_sig_hprop {A} {P : A -> Prop} `{DecidableRel (@eq A), forall a : A, IsHProp (P a)} : DecidableRel (@eq (@sig A P)) | 10. exact _. Defined. |
| 101 |
Global Instance dec_eq_comparison : DecidableRel (@eq comparison) | 10. exact _. Defined. |
| 102 |
|
| 103 |
Global Instance dec_eq_nat : DecidableRel (@eq nat) | 10. exact _. Defined. |
| 104 |
Global Instance dec_le_nat : DecidableRel le := Compare_dec.le_dec. |
| 105 |
Global Instance dec_lt_nat : DecidableRel lt := Compare_dec.lt_dec. |
| 106 |
Global Instance dec_ge_nat : DecidableRel ge := Compare_dec.ge_dec. |
| 107 |
Global Instance dec_gt_nat : DecidableRel gt := Compare_dec.gt_dec. |
| 108 |
|
| 109 |
Global Instance dec_eq_N : DecidableRel (@eq N) | 10 := N.eq_dec. |
| 110 |
|
| 111 |
Global Instance dec_eq_Z : DecidableRel (@eq Z) | 10 := Z.eq_dec. |
| 112 |
Global Instance dec_lt_Z : DecidableRel BinInt.Z.lt := ZArith_dec.Z_lt_dec. |
| 113 |
Global Instance dec_le_Z : DecidableRel BinInt.Z.le := ZArith_dec.Z_le_dec. |
| 114 |
Global Instance dec_gt_Z : DecidableRel BinInt.Z.gt := ZArith_dec.Z_gt_dec. |
| 115 |
Global Instance dec_ge_Z : DecidableRel BinInt.Z.ge := ZArith_dec.Z_ge_dec. |
| 116 |
|
| 117 |
Global Instance dec_Forall {A P} {HD : forall x : A, Decidable (P x)} {ls} |
| 118 |
: Decidable (Forall P ls) | 10. |
| 119 |
Proof. |
| 120 |
induction ls as [|x xs IHxs]; [ left | destruct (HD x), IHxs; [ left | right.. ] ]; |
| 121 |
[ constructor; assumption | constructor; assumption | intro Hn.. ]; |
| 122 |
clear HD; |
| 123 |
try abstract (inversion Hn; subst; tauto). |
| 124 |
Defined. |
| 125 |
Global Instance dec_Exists {A P} {HD : forall x : A, Decidable (P x)} {ls} |
| 126 |
: Decidable (Exists P ls) | 10. |
| 127 |
Proof. |
| 128 |
induction ls as [|x xs IHxs]; [ right | destruct (HD x), IHxs; [ left.. | right ] ]; |
| 129 |
[ intro Hn | constructor; assumption.. | intro Hn ]; |
| 130 |
clear HD; |
| 131 |
try abstract (inversion Hn; subst; tauto). |
| 132 |
Defined. |
| 133 |
|
| 134 |
Global Instance dec_match_pair {A B} {P : A -> B -> Prop} {x : A * B} |
| 135 |
{HD : Decidable (P (fst x) (snd x))} |
| 136 |
: Decidable (let '(a, b) := x in P a b) | 1. |
| 137 |
Proof. edestruct (_ : _ * _); assumption. Defined. |
| 138 |
|
| 139 |
Global Instance dec_match_sum {A B} {P : A -> Prop} {Q : B -> Prop} |
| 140 |
{HP : forall x, Decidable (P x)} {HQ : forall x, Decidable (Q x)} |
| 141 |
{x : A + B} |
| 142 |
: Decidable (match x with inl a => P a | inr b => Q b end) | 1. |
| 143 |
Proof. edestruct (_ : _ + _); eauto. Defined. |
| 144 |
|
| 145 |
Global Instance dec_if_bool {b : bool} {A B} {HA : Decidable A} {HB : Decidable B} |
| 146 |
: Decidable (if b then A else B) | 10 |
| 147 |
:= if b return Decidable (if b then A else B) |
| 148 |
then HA |
| 149 |
else HB. |
| 150 |
|
| 151 |
Global Instance dec_match_unit {P} {HP : Decidable P} {x : unit} |
| 152 |
: Decidable (match x with tt => P end) | 1. |
| 153 |
Proof. edestruct x. assumption. Defined. |
| 154 |
|
| 155 |
Lemma not_not P {d:Decidable P} : not (not P) <-> P. |
| 156 |
Proof. destruct (dec P); intuition. Qed. |
| 157 |
|
| 158 |
Global Instance dec_ex_forall_not : forall T (P:T->Prop) {d:Decidable (exists b, P b)}, Decidable (forall b, ~ P b). |
| 159 |
Proof. |
| 160 |
intros T P d. |
| 161 |
destruct (dec (~ exists b, P b)) as [Hd|Hd]; [left|right]; |
| 162 |
[abstract eauto | let Hd := Hd in abstract (rewrite not_not in Hd by eauto; destruct Hd; eauto) ]. |
| 163 |
Defined. |
| 164 |
|
| 165 |
Lemma eqsig_eq {T} {U} {Udec:DecidableRel (@eq U)} (f g:T->U) (x x':T) pf pf' : |
| 166 |
(exist (fun x => f x = g x) x pf) = (exist (fun x => f x = g x) x' pf') <-> (x = x'). |
| 167 |
Proof. apply path_sig_hprop_iff; auto using UIP_dec, Udec. Qed. |
| 168 |
|
| 169 |
Lemma Decidable_respects_iff A B (H : A <-> B) : (Decidable A -> Decidable B) * (Decidable B -> Decidable A). |
| 170 |
Proof. solve_decidable_transparent. Defined. |
| 171 |
|
| 172 |
Lemma Decidable_iff_to_impl A B (H : A <-> B) : Decidable A -> Decidable B. |
| 173 |
Proof. solve_decidable_transparent. Defined. |
| 174 |
|
| 175 |
Lemma Decidable_iff_to_flip_impl A B (H : A <-> B) : Decidable B -> Decidable A. |
| 176 |
Proof. solve_decidable_transparent. Defined. |
| 177 |
|
| 178 |
Global Instance dec_eq_positive : DecidableRel (@eq BinNums.positive) | 10 := BinPos.Pos.eq_dec. |
| 179 |
Global Instance dec_lt_positive : DecidableRel BinPos.Pos.lt. |
| 180 |
Proof. eauto using Decidable_iff_to_impl, Pos.ltb_lt with typeclass_instances. Defined. |
| 181 |
Global Instance dec_le_positive : DecidableRel BinPos.Pos.le. |
| 182 |
Proof. eauto using Decidable_iff_to_impl, Pos.leb_le with typeclass_instances. Defined. |
| 183 |
Global Instance dec_gt_positive : DecidableRel BinPos.Pos.gt. |
| 184 |
Proof. eauto using Decidable_iff_to_flip_impl, Pos.gt_lt_iff with typeclass_instances. Defined. |
| 185 |
Global Instance dec_ge_positive : DecidableRel BinPos.Pos.ge. |
| 186 |
Proof. eauto using Decidable_iff_to_flip_impl, Pos.ge_le_iff with typeclass_instances. Defined. |
| 187 |
|
| 188 |
Lemma dec_of_semidec {P : Prop} (H : option P) (H_complete : H = None -> ~P) : Decidable P. |
| 189 |
Proof. destruct H; [ left; assumption | right; apply H_complete, eq_refl ]. Defined. |
| 190 |
|
| 191 |
Lemma dec_rel_of_semidec_rel {A} {R : A -> A -> Prop} (H : forall x y, option (R x y)) |
| 192 |
(H_complete : forall x y, H x y = None -> ~R x y) : DecidableRel R. |
| 193 |
Proof. eauto using dec_of_semidec. Defined. |
| 194 |
|
| 195 |
Lemma dec_of_bool_dec {P : Prop} (b : bool) (Hbl : b = true -> P) (Hlb : P -> b = true) |
| 196 |
: Decidable P. |
| 197 |
Proof. destruct b; [ left; apply Hbl; reflexivity | right; intro p; apply Bool.diff_false_true, Hlb, p ]. Defined. |
| 198 |
|
| 199 |
Definition bool_of_dec (P : Prop) {H : Decidable P} : bool := if H then true else false. |
| 200 |
Lemma bp_of_dec (P : Prop) {H : Decidable P} : bool_of_dec P = true -> P. |
| 201 |
Proof. cbv [bool_of_dec]; destruct H; [ trivial with nocore | abstract congruence ]. Defined. |
| 202 |
Lemma pb_of_dec (P : Prop) {H : Decidable P} : P -> bool_of_dec P = true. |
| 203 |
Proof. cbv [bool_of_dec]; destruct H; [ reflexivity | abstract tauto ]. Defined. |
| 204 |
|
| 205 |
Lemma dec_rel_of_bool_dec_rel {A} {R : A -> A -> Prop} (b : A -> A -> bool) |
| 206 |
(Hbl : forall x y, b x y = true -> R x y) (Hlb : forall x y, R x y -> b x y = true) |
| 207 |
: DecidableRel R. |
| 208 |
Proof. eauto using dec_of_bool_dec. Defined. |
| 209 |
|
| 210 |
Definition bool_rel_of_dec_rel {A} (R : A -> A -> Prop) {H : DecidableRel R} : A -> A -> bool |
| 211 |
:= fun x y => if H x y then true else false. |
| 212 |
Lemma br_of_dec_rel {A} (R : A -> A -> Prop) {H : DecidableRel R} |
| 213 |
: forall x y, bool_rel_of_dec_rel R x y = true -> R x y. |
| 214 |
Proof. cbv [bool_rel_of_dec_rel]; intros x y; destruct (H x y); [ trivial with nocore | abstract congruence ]. Defined. |
| 215 |
Lemma rb_of_dec_rel {A} (R : A -> A -> Prop) {H : DecidableRel R} |
| 216 |
: forall x y, R x y -> bool_rel_of_dec_rel R x y = true. |
| 217 |
Proof. cbv [bool_rel_of_dec_rel]; intros x y; destruct (H x y); [ reflexivity | abstract tauto ]. Defined. |
| 218 |
|
| 219 |
Lemma dec_bool : forall {P} {Pdec:Decidable P}, (if dec P then true else false) = true -> P. |
| 220 |
Proof. intros. destruct dec; solve [ auto | discriminate ]. Qed. |
| 221 |
|
| 222 |
Ltac vm_decide_no_check := apply dec_bool; vm_cast_no_check (eq_refl true). |
| 223 |
Ltac lazy_decide_no_check := apply dec_bool; exact_no_check (eq_refl true). |
| 224 |
|
| 225 |
Ltac vm_decide := abstract vm_decide_no_check. |
| 226 |
Ltac lazy_decide := abstract lazy_decide_no_check. |
| 227 |
|
| 228 |
(** For dubious compatibility with [eauto using]. *) |
| 229 |
Global Hint Extern 2 (Decidable _) => progress unfold Decidable : typeclass_instances core. |
| 230 |
|
| 231 |
Definition cast_if_eq {T} `{DecidableRel (@eq T)} {P} (t t' : T) (v : P t) : option (P t') |
| 232 |
:= match dec (t = t'), dec (t' = t') with |
| 233 |
| left pf, left pf' => Some (eq_rect _ P v _ (eq_trans pf (eq_sym pf'))) |
| 234 |
| _, right pf' => match pf' eq_refl with end |
| 235 |
| right pf, _ => None |
| 236 |
end. |
| 237 |
|
| 238 |
Lemma cast_if_eq_refl {T H P} t v : @cast_if_eq T H P t t v = Some v. |
| 239 |
Proof. |
| 240 |
compute; clear; destruct (H t t) as [ [] |e]; |
| 241 |
[ reflexivity | destruct (e eq_refl) ]. |
| 242 |
Qed. |