Debian Codegraph @

src/Util/Bool/Reflect.v

raw
1 (** * Some lemmas about [Bool.reflect] *)
2 From Coq Require Import CMorphisms.
3 From Coq Require Import String.
4 From Coq Require Import Ascii.
5 From Coq Require Import Bool.
6 From Coq Require Import RelationClasses.
7 From Coq Require Import Arith.
8 From Coq Require Import BinInt ZArith_dec.
9 From Coq Require Import BinNat.
10 Require Import Crypto.Util.HProp.
11 Require Import Crypto.Util.Decidable.
12 Require Import Crypto.Util.Prod.
13 Require Import Crypto.Util.Option.
14 Require Import Crypto.Util.Sigma.
15 Require Import Crypto.Util.ListUtil.
16 Require Import Crypto.Util.ListUtil.SetoidList.
17 Require Import Crypto.Util.Sum.
18 Require Import Crypto.Util.Comparison.
19 Require Import Crypto.Util.Tactics.DestructHead.
20 Require Import Crypto.Util.Tactics.SplitInContext.
21 Require Crypto.Util.PrimitiveProd.
22
23 Lemma reflect_to_dec_iff {P b1 b2} : reflect P b1 -> (b1 = b2) <-> (if b2 then P else ~P).
24 Proof.
25 intro H; destruct H, b2; split; trivial; repeat intro; abstract (exfalso; intuition congruence).
26 Qed.
27
28 Lemma reflect_to_dec {P b1 b2} : reflect P b1 -> (b1 = b2) -> (if b2 then P else ~P).
29 Proof. intro; apply reflect_to_dec_iff; assumption. Qed.
30
31 Lemma reflect_of_dec {P} {b1 b2 : bool} : reflect P b1 -> (if b2 then P else ~P) -> (b1 = b2).
32 Proof. intro; apply reflect_to_dec_iff; assumption. Qed.
33
34 Lemma reflect_of_brel {A A' R Rb} (bl : forall (a : A) (a' : A'), Rb a a' = true -> (R a a' : Prop))
35 (lb : forall a a', R a a' -> Rb a a' = true)
36 : forall x y, reflect (R x y) (Rb x y).
37 Proof.
38 intros x y; specialize (bl x y); specialize (lb x y).
39 destruct (Rb x y); constructor; auto; repeat intro; abstract (exfalso; intuition congruence).
40 Qed.
41
42 Lemma reflect_to_brel {A A' R Rb} (H : forall (x : A) (y : A'), reflect (R x y) (Rb x y))
43 : (forall a a', Rb a a' = true -> R a a')
44 /\ (forall a a', R a a' -> Rb a a' = true).
45 Proof.
46 split; intros x y; specialize (H x y); destruct H; trivial; repeat intro; abstract (exfalso; intuition congruence).
47 Qed.
48
49 Lemma reflect_of_beq {A beq} (bl : forall a a' : A, beq a a' = true -> a = a')
50 (lb : forall a a' : A, a = a' -> beq a a' = true)
51 : forall x y, reflect (x = y) (beq x y).
52 Proof. apply reflect_of_brel; assumption. Qed.
53
54 Lemma reflect_to_beq {A beq} (H : forall x y : A, reflect (x = y) (beq x y))
55 : (forall a a' : A, beq a a' = true -> a = a')
56 /\ (forall a a' : A, a = a' -> beq a a' = true).
57 Proof. apply reflect_to_brel; assumption. Qed.
58
59 Lemma reflect_rect_dep {P b} (Q : reflect P b -> Type)
60 (H : forall pf : if b then P else ~P,
61 (if b return (reflect P b -> Type) -> (if b then P else ~P) -> Type
62 then fun Q pf => Q (@ReflectT _ pf)
63 else fun Q pf => Q (@ReflectF _ pf))
64 Q pf)
65 : forall x, Q x.
66 Proof. intro x; destruct x; apply H. Defined.
67
68 Definition reflect_of_BoolSpec {P b} (H : BoolSpec P (~P) b) : reflect P b.
69 Proof. destruct b; constructor; inversion H; assumption. Defined.
70 Definition BoolSpec_of_reflect {P b} (H : reflect P b) : BoolSpec P (~P) b.
71 Proof. destruct b; constructor; inversion H; assumption. Defined.
72
73 Definition mark {T} (v : T) := v.
74
75 Ltac induction_reflect x :=
76 let G := lazymatch goal with |- ?G => G end in
77 change (mark G);
78 generalize dependent x;
79 let Q := lazymatch goal with |- forall y, @?Q y => Q end in
80 refine (@reflect_rect_dep _ _ Q _);
81 intros; cbv beta delta [mark].
82
83 Section encode_decode.
84 Context {P : Prop}.
85
86 Definition code (b : bool) : Type
87 := if b then P else ~P.
88 Definition encode {b} : reflect P b -> code b
89 := fun r => match r with
90 | ReflectT x => x
91 | ReflectF x => x
92 end.
93 Definition decode {b} : code b -> reflect P b
94 := if b return code b -> reflect P b then @ReflectT P else @ReflectF P.
95 Lemma endecode {b} x : encode (@decode b x) = x.
96 Proof. destruct b; reflexivity. Defined.
97 Lemma deencode {b} x : decode (@encode b x) = x.
98 Proof. destruct x; reflexivity. Defined.
99 End encode_decode.
100
101 Ltac beq_to_eq beq bl lb :=
102 let lem := constr:(@reflect_of_beq _ beq bl lb) in
103 repeat match goal with
104 | [ |- context[bl ?x ?y ?pf] ] => generalize dependent (bl x y pf); try clear pf; intros
105 | [ H : beq ?x ?y = true |- _ ] => apply (@reflect_to_dec _ _ true (lem x y)) in H; cbv beta iota in H
106 | [ H : beq ?x ?y = false |- _ ] => apply (@reflect_to_dec _ _ false (lem x y)) in H; cbv beta iota in H
107 | [ |- beq ?x ?y = true ] => refine (@reflect_of_dec _ _ true (lem x y) _)
108 | [ |- beq ?x ?y = false ] => refine (@reflect_of_dec _ _ false (lem x y) _)
109 | [ H : beq ?x ?y = true |- ?G ]
110 => change (mark G); generalize dependent (bl x y H); clear H;
111 intros; cbv beta delta [mark]
112 | [ H : context[beq ?x ?x] |- _ ] => rewrite (lb x x eq_refl) in H
113 | [ |- context[beq ?x ?x] ] => rewrite (lb x x eq_refl)
114 end.
115
116 Existing Class reflect.
117 Definition decb (P : Prop) {b : bool} {H : reflect P b} := b.
118 Notation reflect_rel P b := (forall x y, reflect (P x y) (b x y)).
119 Definition decb_rel {A B} (P : A -> B -> Prop) {b : A -> B -> bool} {H : reflect_rel P b} := b.
120
121 Definition reflect_rel_of_BoolSpec {A R Rb} (H : forall a b : A, BoolSpec (R a b) (~R a b) (Rb a b)) : reflect_rel R Rb.
122 Proof. intros; now apply reflect_of_BoolSpec. Defined.
123 Definition BoolSpec_of_reflect_rel {A R Rb} (H : reflect_rel R Rb) : forall a b : A, BoolSpec (R a b) (~R a b) (Rb a b).
124 Proof. intros; now apply BoolSpec_of_reflect. Defined.
125
126 Lemma decb_true_iff P {b} {H : reflect P b} : @decb P b H = true <-> P.
127 Proof. symmetry; apply reflect_iff, H. Qed.
128 Lemma decb_bp P {b} {H : reflect P b} : @decb P b H = true -> P.
129 Proof. apply decb_true_iff. Qed.
130 Lemma decb_pb P {b} {H : reflect P b} : P -> @decb P b H = true.
131 Proof. apply decb_true_iff. Qed.
132 Lemma decb_false_iff P {b} {H : reflect P b} : @decb P b H = false <-> ~P.
133 Proof. generalize (decb P), (decb_true_iff P); clear; intros []; intros; intuition congruence. Qed.
134 Lemma decb_false_bp P {b} {H : reflect P b} : @decb P b H = false -> ~P.
135 Proof. apply decb_false_iff. Qed.
136 Lemma decb_false_pb P {b} {H : reflect P b} : ~P -> @decb P b H = false.
137 Proof. apply decb_false_iff. Qed.
138
139 Global Instance dec_of_reflect P {b} {H : reflect P b} : Decidable P | 15
140 := (if b as b return reflect P b -> Decidable P
141 then fun H => left (decb_bp P eq_refl)
142 else fun H => right (decb_false_bp P eq_refl)) H.
143
144 Global Instance eq_decb_hprop {A} {x y : A} {hp : IsHProp A} : reflect (@eq A x y) true | 5.
145 Proof. left; auto. Qed.
146
147 Lemma ishprop_reflect_all_eq_gen {A} {beq} {H : forall x y : A, reflect (x = y) (beq x y)}
148 : forall x y, (beq x y = true \/ IsHProp (x <> y)) -> IsHProp (reflect (x = y) (beq x y)).
149 Proof.
150 intros x y H' p q.
151 induction_reflect p; induction_reflect q.
152 destruct (beq x y); f_equal.
153 { apply Eqdep_dec.UIP_dec; intros x' y'; specialize (H x' y'); eapply @dec_of_reflect; eassumption. }
154 { destruct H' as [H'|H']; [ congruence | now apply H' ]. }
155 Qed.
156
157 Lemma eq_reflect_to_dec_true_gen {A beq} {H : forall x y : A, reflect (x = y) (beq x y)}
158 {x y pf H'}
159 : @reflect_to_dec (x = y) (beq x y) true H' pf = @reflect_to_dec (x = y) (beq x y) true (H _ _) pf.
160 Proof.
161 apply f_equal2; [ | reflexivity ].
162 apply ishprop_reflect_all_eq_gen; left; assumption.
163 Qed.
164
165 Lemma eq_reflect_to_dec_true_refl {A beq} {H : forall x y : A, reflect (x = y) (beq x y)}
166 {x pf H'}
167 : @reflect_to_dec (x = x) (beq x x) true H' pf = eq_refl.
168 Proof.
169 apply Eqdep_dec.UIP_dec; intros; eapply @dec_of_reflect; eauto.
170 Qed.
171
172 Ltac generalize_reflect_to_dec_rel_refl_using beq R :=
173 let A := lazymatch type of beq with ?A -> _ -> _ => A end in
174 let do_gen b x R' H :=
175 (let G := match goal with |- ?G => G end in
176 change (mark G); generalize (@eq_reflect_to_dec_true_refl A beq R x H R');
177 change (beq x x) with b in *;
178 generalize dependent (@reflect_to_dec (@eq A x x) b true R' H);
179 try clear H;
180 let H' := fresh in
181 intro H'; intros; subst H'; cbv beta delta [mark]) in
182 repeat match goal with
183 | [ H : context[@reflect_to_dec (@eq A ?x ?x) ?b true ?R' ?H'] |- _ ]
184 => do_gen b x R' H'
185 | [ |- context[@reflect_to_dec (@eq A ?x ?x) ?b true ?R' ?H'] ]
186 => do_gen b x R' H'
187 end.
188
189 Ltac generalize_reflect_to_dec :=
190 repeat match goal with
191 | [ H : context[@reflect_to_dec ?P ?b true ?R ?H] |- ?G ]
192 => change (mark G); generalize dependent (@reflect_to_dec P b true R H); try clear H; intros; cbv beta delta [mark]
193 | [ |- context[@reflect_to_dec ?P ?b true ?R ?H] ]
194 => let G := match goal with |- ?G => G end in
195 change (mark G); generalize dependent (@reflect_to_dec P b true R H); try clear H; intros; cbv beta delta [mark]
196 end.
197
198 Ltac reflect_beq_to_eq_using R :=
199 let beq := lazymatch type of R with forall x y, reflect (x = y) (?beq x y) => beq end in
200 let lem_to_dec := constr:(fun b x y => @reflect_to_dec (x = y) (beq x y) b (R x y)) in
201 let lem_of_dec := constr:(fun b x y => @reflect_of_dec (x = y) (beq x y) b (R x y)) in
202 generalize_reflect_to_dec_rel_refl_using beq R;
203 generalize_reflect_to_dec;
204 repeat match goal with
205 | [ H : beq ?x ?y = true |- _ ] => apply (lem_to_dec true x y) in H; cbv beta iota in H
206 | [ H : beq ?x ?y = false |- _ ] => apply (lem_to_dec false x y) in H; cbv beta iota in H
207 | [ |- beq ?x ?y = true ] => refine (lem_of_dec true x y _)
208 | [ |- beq ?x ?y = false ] => refine (lem_of_dec false x y _)
209 | [ H : context[beq ?x ?x] |- _ ] => rewrite (lem_of_dec true x x eq_refl) in H
210 | [ |- context[beq ?x ?x] ] => rewrite (lem_of_dec true x x eq_refl)
211 | [ H : context[@reflect_to_dec ?P (beq ?x ?y) true ?R ?H] |- ?G ]
212 => change (mark G); generalize dependent (@reflect_to_dec P (beq x y) true R H); try clear H; intros; cbv beta delta [mark]
213 | [ |- context[@reflect_to_dec ?P (beq ?x ?y) true ?R ?H] ]
214 => let G := match goal with |- ?G => G end in
215 change (mark G); generalize dependent (@reflect_to_dec P (beq x y) true R H); try clear H; intros; cbv beta delta [mark]
216 end.
217
218 Ltac reflect_beq_to_eq beq :=
219 let R := constr:(_ : forall x y, reflect (x = y) (beq x y)) in
220 reflect_beq_to_eq_using R.
221
222 Ltac solve_reflect_step :=
223 first [ match goal with
224 | [ H : reflect _ ?b |- _ ] => tryif is_var b then destruct H else (inversion H; clear H)
225 | [ |- reflect _ _ ] => constructor
226 | [ |- reflect (?R ?x ?y) (?Rb ?x ?y) ] => apply (@reflect_of_brel _ _ R Rb)
227 | [ H : forall x y, reflect (?R x y) (?Rb x y) |- _ ] => apply (@reflect_to_brel _ _ R Rb) in H
228 | [ H : forall a x y, reflect (@?R a x y) (@?Rb a x y) |- _ ]
229 => let H' := fresh in
230 pose proof (fun a => @reflect_to_brel _ _ (R a) (Rb a) (H a)) as H'; clear H;
231 rename H' into H; cbv beta in H
232 end
233 | progress destruct_head'_and
234 | progress split_and
235 | progress intros
236 | progress subst
237 | solve [ eauto ]
238 | progress destruct_head'_or
239 | apply conj
240 | repeat intro; abstract (exfalso; firstorder (auto; try discriminate))
241 (*
242 | [ H :
243 | [ H : forall x y : ?A, reflect (x = y) _, x0 : ?A, y0 : ?A |- _ ]
244 => no_equalities_about x0 y0; let H' := fresh in pose proof (H x0 y0) as H'; inversion H'; clear H'
245 | [ H : forall a0 (x y : _), reflect (x = y) _, x0 : ?A, y0 : ?A |- _ ]
246 => no_equalities_about x0 y0; let H' := fresh in pose proof (H _ x0 y0) as H'; inversion H'; clear H'
247 | [ H : true = ?x |- context[?x] ] => rewrite <- H
248 | [ H : false = ?x |- context[?x] ] => rewrite <- H
249 end
250 | progress intros
251 | split
252 | progress inversion_sigma
253 | progress inversion_prod
254 | progress pre_decide_destruct_sigma
255 | progress destruct_head'_bool
256 | progress destruct_head'_prod
257 | progress subst
258 | progress cbn in *
259 | progress hnf
260 | progress pre_hprop
261 | solve [ left; firstorder (auto; try discriminate)
262 | right; firstorder (auto; try discriminate)
263 | firstorder (auto; try discriminate) ] *) ].
264
265
266 Ltac skip_solve_reflect := constr:(false).
267 Ltac solve_reflect :=
268 let b := skip_solve_reflect in
269 lazymatch b with
270 | true => fail
271 | false => repeat solve_reflect_step
272 end.
273
274 #[global]
275 Hint Constructors reflect : typeclass_instances.
276
277 Local Hint Resolve -> Bool.eqb_true_iff : core.
278 Local Hint Resolve <- Bool.eqb_true_iff : core.
279 Local Hint Resolve internal_prod_dec_bl internal_prod_dec_lb
280 PrimitiveProd.Primitive.prod_dec_bl PrimitiveProd.Primitive.prod_dec_lb
281 internal_option_dec_bl internal_option_dec_lb
282 internal_list_dec_bl internal_list_dec_lb
283 internal_sum_dec_bl internal_sum_dec_lb
284 sigT_dec_bl sigT_dec_lb
285 sigT_dec_bl_hprop sigT_dec_lb_hprop
286 sig_dec_bl sig_dec_lb
287 sig_dec_bl_hprop sig_dec_lb_hprop
288 internal_comparison_dec_bl internal_comparison_dec_lb
289 prod_bl_hetero prod_lb_hetero prod_bl_hetero_eq prod_lb_hetero_eq
290 option_bl_hetero option_lb_hetero option_bl_hetero_eq option_lb_hetero_eq
291 list_bl_hetero list_lb_hetero list_bl_hetero_eq list_lb_hetero_eq
292 eqlistA_lb eqlistA_bl
293 : core.
294
295 Local Hint Extern 0 => solve [ solve_reflect ] : typeclass_instances.
296 Local Hint Extern 1 => progress inversion_sigma : core.
297
298 Global Instance reflect_True : reflect True true | 0 := @ReflectT _ I.
299 Global Instance reflect_False : reflect False false | 0 := @ReflectF _ (fun x => x).
300 Global Instance reflect_or {A B a b} `{reflect A a, reflect B b} : reflect (A \/ B) (orb a b) | 10. exact _. Qed.
301 Global Instance reflect_and {A B a b} `{reflect A a, reflect B b} : reflect (A /\ B) (andb a b) | 10. exact _. Qed.
302 Global Instance reflect_impl_or {A B bona} `{reflect (B \/ ~A) bona} : reflect (A -> B) bona | 15. exact _. Qed.
303 Global Instance reflect_impl {A B a b} `{reflect A a, reflect B b} : reflect (A -> B) (implb a b) | 10. exact _. Qed.
304 Global Instance reflect_iff {A B a b} `{reflect A a, reflect B b} : reflect (A <-> B) (Bool.eqb a b) | 10. exact _. Qed.
305 Lemma reflect_not {A a} `{reflect A a} : reflect (~A) (negb a).
306 Proof. exact _. Qed.
307
308 (** Disallow infinite loops of reflect_not *)
309 Global Hint Extern 0 (reflect (~?A) _) => eapply (@reflect_not A) : typeclass_instances.
310 Global Hint Extern 0 (reflect _ (negb ?b)) => eapply (@reflect_not _ b) : typeclass_instances.
311
312 Global Instance reflect_eq_unit : reflect_rel (@eq unit) (fun _ _ => true) | 10. exact _. Qed.
313 Global Instance reflect_eq_bool : reflect_rel (@eq bool) Bool.eqb | 10. exact _. Qed.
314 Global Instance reflect_eq_Empty_set : reflect_rel (@eq Empty_set) (fun _ _ => true) | 10. exact _. Qed.
315 Global Existing Instances Ascii.eqb_spec String.eqb_spec | 10.
316 Global Instance reflect_eq_prod {A B eqA eqB} `{reflect_rel (@eq A) eqA, reflect_rel (@eq B) eqB} : reflect_rel (@eq (A * B)) (prod_beq A B eqA eqB) | 10. exact _. Qed.
317 Global Instance reflect_eq_prod_hetero {A1 B1 A2 B2 bA bB RA RB} `{reflect_rel RA bA, reflect_rel RB bB}
318 : reflect_rel (fun (x : A1 * B1) (y : A2 * B2) => RA (fst x) (fst y) /\ RB (snd x) (snd y))
319 (prod_beq_hetero bA bB) | 20.
320 Proof. exact _. Qed.
321 Global Instance reflect_eq_prod_hetero_uniform {A B eqA eqB} `{reflect_rel (@eq A) eqA, reflect_rel (@eq B) eqB} : reflect_rel (@eq (A * B)) (prod_beq_hetero eqA eqB) | 15. exact _. Qed.
322 Global Instance reflect_eq_option {A eqA} `{reflect_rel (@eq A) eqA} : reflect_rel (@eq (option A)) (option_beq eqA) | 10. exact _. Qed.
323 Global Instance reflect_eq_option_hetero {A1 A2 bA RA} `{reflect_rel RA bA} : reflect_rel (option_eq RA) (@option_beq_hetero A1 A2 bA) | 20. exact _. Qed.
324 Global Instance reflect_eq_option_hetero_uniform {A eqA} `{reflect_rel (@eq A) eqA} : reflect_rel (@eq (option A)) (option_beq_hetero eqA) | 15. exact _. Qed.
325 Global Instance reflect_eq_list {A eqA} `{reflect_rel (@eq A) eqA} : reflect_rel (@eq (list A)) (list_beq A eqA) | 10. exact _. Qed.
326 Global Instance reflect_eq_list_hetero {A1 A2 RA bA} `{reflect_rel RA bA} : reflect_rel (list_eq RA) (@list_beq_hetero A1 A2 bA) | 20. exact _. Qed.
327 Global Instance reflect_eq_list_hetero_uniform {A eqA} `{reflect_rel (@eq A) eqA} : reflect_rel (@eq (list A)) (list_beq_hetero eqA) | 15. exact _. Qed.
328 Global Instance reflect_eq_list_nil_r {A eqA} {ls} : reflect (ls = @nil A) (list_beq A eqA ls (@nil A)) | 10.
329 Proof. destruct ls; [ left; reflexivity | right; abstract congruence ]. Qed.
330 Global Instance reflect_eq_list_nil_l {A eqA} {ls} : reflect (@nil A = ls) (list_beq A eqA (@nil A) ls) | 10.
331 Proof. destruct ls; [ left; reflexivity | right; abstract congruence ]. Qed.
332 Global Instance reflect_eq_list_nil_r_hetero {A1 A2 eqA} {ls} : reflect (ls = nil) (@list_beq_hetero A1 A2 eqA ls nil) | 10.
333 Proof. destruct ls; [ left; reflexivity | right; abstract congruence ]. Qed.
334 Global Instance reflect_eq_list_nil_l_hetero {A1 A2 eqA} {ls} : reflect (nil = ls) (@list_beq_hetero A1 A2 eqA nil ls) | 10.
335 Proof. destruct ls; [ left; reflexivity | right; abstract congruence ]. Qed.
336 Global Instance reflect_eqlistA {A R eqA} `{reflect_rel R eqA} : reflect_rel (@SetoidList.eqlistA A R) (list_beq A eqA) | 100. exact _. Qed.
337 Global Instance reflect_eq_sum {A B eqA eqB} `{reflect_rel (@eq A) eqA, reflect_rel (@eq B) eqB} : reflect_rel (@eq (A + B)) (sum_beq A B eqA eqB) | 10. exact _. Qed.
338 Global Instance reflect_sumwise {A B RA RB eqA eqB} `{reflect_rel RA eqA, reflect_rel RB eqB} : reflect_rel (@sumwise A B RA RB) (@sum_beq A B eqA eqB) | 100.
339 Proof. intros [?|?] [?|?]; cbn; exact _. Qed.
340 Global Instance reflect_sum_le {A B RA RB leA leB} `{reflect_rel RA leA, reflect_rel RB leB} : reflect_rel (@sum_le A B RA RB) (@sum_leb A B leA leB) | 10.
341 Proof. intros [?|?] [?|?]; cbn; exact _. Qed.
342 Global Instance reflect_eq_sigT {A P eqA} {eqP : forall a a', P a -> P a' -> bool}
343 `{reflect_rel (@eq A) eqA, forall a : A, reflect_rel (@eq (P a)) (eqP a a)}
344 : reflect_rel (@eq (@sigT A P)) (sigT_beq eqA eqP) | 15.
345 Proof. exact _. Qed.
346 Global Instance reflect_eq_sigT_hprop {A P eqA} `{reflect_rel (@eq A) eqA, forall a : A, IsHProp (P a)} : reflect_rel (@eq (@sigT A P)) (sigT_beq eqA (fun _ _ _ _ => true)) | 10. exact _. Qed.
347 Global Instance reflect_eq_sig_hprop {A eqA} {P : A -> Prop} `{reflect_rel (@eq A) eqA, forall a : A, IsHProp (P a)} : reflect_rel (@eq (@sig A P)) (sig_beq eqA (fun _ _ _ _ => true)) | 10. exact _. Qed.
348 Global Instance reflect_eq_comparison : reflect_rel (@eq comparison) comparison_beq | 10. exact _. Qed.
349 Global Instance reflect_eq_None_r {A x} : reflect (x = @None A) (is_None x) | 100.
350 Proof. destruct x; cbv; constructor; congruence. Qed.
351 Global Instance reflect_eq_None_l {A x} : reflect (@None A = x) (is_None x) | 100.
352 Proof. destruct x; cbv; constructor; congruence. Qed.
353 Global Instance reflect_eq_nil_r {A x} : reflect (x = @nil A) (is_nil x) | 100.
354 Proof. destruct x; cbv; constructor; congruence. Qed.
355 Global Instance reflect_eq_nil_l {A x} : reflect (@nil A = x) (is_nil x) | 100.
356 Proof. destruct x; cbv; constructor; congruence. Qed.
357
358 Module Export Primitive.
359 Import PrimitiveProd.
360 Import PrimitiveProd.Primitive.
361 Global Instance reflect_eq_prod {A B eqA eqB} `{reflect_rel (@eq A) eqA, reflect_rel (@eq B) eqB} : reflect_rel (@eq (A * B)) (@Primitive.prod_beq A B eqA eqB) | 10. exact _. Qed.
362 End Primitive.
363
364 Module Nat.
365 Definition geb_spec0 : reflect_rel ge (fun x y => Nat.leb y x) := fun _ _ => Nat.leb_spec0 _ _.
366 Definition gtb_spec0 : reflect_rel gt (fun x y => Nat.ltb y x) := fun _ _ => Nat.ltb_spec0 _ _.
367 End Nat.
368 Global Existing Instance Nat.eqb_spec | 10.
369 Global Existing Instances Nat.leb_spec0 Nat.ltb_spec0 Nat.geb_spec0 Nat.gtb_spec0.
370
371 Module N.
372 Lemma geb_spec0 : reflect_rel N.ge (fun x y => N.leb y x).
373 Proof. intros x y; generalize (N.leb_spec0 y x); intro H; destruct H; constructor; rewrite N.ge_le_iff; assumption. Qed.
374 Lemma gtb_spec0 : reflect_rel N.gt (fun x y => N.ltb y x).
375 Proof. intros x y; generalize (N.ltb_spec0 y x); intro H; destruct H; constructor; rewrite N.gt_lt_iff; assumption. Qed.
376 End N.
377 Global Existing Instance N.eqb_spec | 10.
378 Global Existing Instances N.leb_spec0 N.ltb_spec0 N.geb_spec0 N.gtb_spec0.
379
380 Module Z.
381 Lemma geb_spec0 : reflect_rel Z.ge Z.geb.
382 Proof. intros x y; generalize (Z.leb_spec0 y x); rewrite Z.geb_leb; intro H; destruct H; constructor; rewrite Z.ge_le_iff; assumption. Qed.
383 Lemma gtb_spec0 : reflect_rel Z.gt Z.gtb.
384 Proof. intros x y; generalize (Z.ltb_spec0 y x); rewrite Z.gtb_ltb; intro H; destruct H; constructor; rewrite Z.gt_lt_iff; assumption. Qed.
385 End Z.
386 Global Existing Instance Z.eqb_spec | 10.
387 Global Existing Instances Z.leb_spec0 Z.ltb_spec0 Z.geb_spec0 Z.gtb_spec0.
388
389 Module Pos.
390 Lemma geb_spec0 : reflect_rel Pos.ge (fun x y => Pos.leb y x).
391 Proof. intros x y; generalize (Pos.leb_spec0 y x); intro H; destruct H; constructor; rewrite Pos.ge_le_iff; assumption. Qed.
392 Lemma gtb_spec0 : reflect_rel Pos.gt (fun x y => Pos.ltb y x).
393 Proof. intros x y; generalize (Pos.ltb_spec0 y x); intro H; destruct H; constructor; rewrite Pos.gt_lt_iff; assumption. Qed.
394 End Pos.
395 Global Existing Instance Pos.eqb_spec | 10.
396 Global Existing Instances Pos.leb_spec0 Pos.ltb_spec0 Pos.geb_spec0 Pos.gtb_spec0.
397
398 Global Instance reflect_Forall {A P Pb} {HD : forall x : A, reflect (P x) (Pb x)} {ls}
399 : reflect (List.Forall P ls) (List.forallb Pb ls) | 10.
400 Proof.
401 induction ls as [|x xs IHxs]; cbn [List.forallb]; [ left | destruct (HD x), IHxs; [ left | right.. ] ];
402 [ constructor; assumption | constructor; assumption | intro Hn.. ];
403 clear HD;
404 try abstract (inversion Hn; subst; tauto).
405 Qed.
406 Global Instance reflect_Exists {A P Pb} {HD : forall x : A, reflect (P x) (Pb x)} {ls}
407 : reflect (List.Exists P ls) (List.existsb Pb ls) | 10.
408 Proof.
409 induction ls as [|x xs IHxs]; cbn [List.existsb]; [ right | destruct (HD x), IHxs; [ left.. | right ] ];
410 [ intro Hn | constructor; assumption.. | intro Hn ];
411 clear HD;
412 try abstract (inversion Hn; subst; tauto).
413 Qed.
414
415 Ltac skip_solve_reflect ::= constr:(true).
416 Global Instance reflect_NoDupA {A R eqA} {HR : Equivalence R}
417 {HR' : Morphisms.Proper (Morphisms.respectful R (Morphisms.respectful R Basics.impl)) R}
418 {H : reflect_rel R eqA} {ls : list A}
419 : reflect (SetoidList.NoDupA R ls) (list_beq _ eqA (remove_duplicates eqA ls) ls) | 100.
420 Proof.
421 destruct (reflect_eqlistA (remove_duplicates eqA ls) ls) as [H'|H']; constructor.
422 { rewrite <- H'.
423 apply NoDupA_remove_duplicates; try assumption; intros x y; specialize (H x y); destruct H.
424 all: intros; subst; congruence. }
425 { intro H''; eapply remove_duplicates_eq_NoDupA in H''; try assumption.
426 { rewrite H'' in H'; specialize (H' (reflexivity _)); assumption. }
427 { intros x y; specialize (H x y); destruct H.
428 all: intros; subst; congruence. } }
429 Qed.
430
431 Global Instance reflect_NoDup {A eqA} {H : reflect_rel (@eq A) eqA} {ls : list A}
432 : reflect (List.NoDup ls) (list_beq _ eqA (remove_duplicates eqA ls) ls) | 10.
433 Proof.
434 destruct (@reflect_NoDupA A _ eqA _ _ H ls); constructor.
435 all: rewrite @NoDupA_eq_NoDup in *; assumption.
436 Qed.
437 Ltac skip_solve_reflect ::= constr:(false).
438
439 Global Instance reflect_match_pair {A B} {P : A -> B -> Prop} {Pb : A -> B -> bool} {x : A * B}
440 {HD : reflect (P (fst x) (snd x)) (Pb (fst x) (snd x))}
441 : reflect (let '(a, b) := x in P a b) (let '(a, b) := x in Pb a b) | 1.
442 Proof. edestruct (_ : _ * _); assumption. Qed.
443
444 Definition reflect_if_bool {x : bool} {A B a b} {HA : reflect A a} {HB : reflect B b}
445 : reflect (if x then A else B) (if x then a else b)
446 := if x return reflect (if x then A else B) (if x then a else b)
447 then HA
448 else HB.
449
450 Global Hint Extern 1 (reflect _ (match ?x with true => ?a | false => ?b end))
451 => eapply (@reflect_if_bool x _ _ a b) : typeclass_instances.
452 Global Hint Extern 1 (reflect (match ?x with true => ?A | false => ?B end) _)
453 => eapply (@reflect_if_bool x A B) : typeclass_instances.
454
455 Global Instance reflect_ex_forall_not : forall T (P:T->Prop) (exPb:bool) {d:reflect (exists b, P b) exPb}, reflect (forall b, ~ P b) (negb exPb).
456 Proof.
457 intros T P exPb d; destruct d; constructor; firstorder.
458 Qed.
459
460 Global Polymorphic Instance reflect_Proper_iff
461 : Proper (iff ==> eq ==> iffT) reflect | 10.
462 Proof.
463 intros A B H b b' ?; subst b'; split; intro H'; destruct H, H'; constructor; auto.
464 Qed.
465
466 Global Polymorphic Instance reflect_Proper_arrow
467 : Proper (iff ==> eq ==> arrow) reflect | 10.
468 Proof.
469 repeat intro; eapply reflect_Proper_iff; try eassumption; easy.
470 Qed.
471
472 Global Polymorphic Instance reflect_Proper_flip_arrow
473 : Proper (iff ==> eq ==> flip arrow) reflect | 10.
474 Proof.
475 repeat intro; eapply reflect_Proper_iff; try eassumption; easy.
476 Qed.
477
478 Lemma reflect_f_equal2_inverts {A B C a1 a2 b1 b2 Aeqb Beqb}
479 {HA : Bool.reflect (@eq A a1 a2) Aeqb}
480 {HB : Bool.reflect (@eq B b1 b2) Beqb}
481 (f : A -> B -> C)
482 (finv : f a1 b1 = f a2 b2 -> a1 = a2 /\ b1 = b2)
483 : Bool.reflect (f a1 b1 = f a2 b2) (andb Aeqb Beqb).
484 Proof.
485 destruct HA, HB; constructor; subst; try reflexivity.
486 all: intro H; destruct (finv H); eauto with nocore.
487 Defined.
488
489 (** We register the typeclass instances explicitly for the following
490 instances, so that typeclass resolution doesn't unfold things and
491 doesn't pick the wrong equalities for random conjunctions *)
492 Lemma reflect_eq_pair {A B a1 a2 b1 b2 Aeqb Beqb}
493 {HA : Bool.reflect (@eq A a1 a2) Aeqb}
494 {HB : Bool.reflect (@eq B b1 b2) Beqb}
495 : Bool.reflect ((a1, b1) = (a2, b2)) (andb Aeqb Beqb).
496 Proof. apply reflect_f_equal2_inverts; now inversion 1. Defined.
497 Global Hint Extern 2 (reflect (@pair ?A ?B _ _ = pair _ _) _) => simple eapply (@reflect_eq_pair A B) : typeclass_instances.
498
499 Lemma reflect_eq_cons {A x y xs ys eqb eqbs}
500 {HA : Bool.reflect (@eq A x y) eqb}
501 {HB : Bool.reflect (@eq (list A) xs ys) eqbs}
502 : Bool.reflect (cons x xs = cons y ys) (andb eqb eqbs).
503 Proof. apply reflect_f_equal2_inverts; now inversion 1. Defined.
504 Global Hint Extern 2 (reflect (@cons ?T _ _ = cons _ _) _) => simple eapply (@reflect_eq_cons T) : typeclass_instances.
505
506 Lemma reflect_bool : forall {P b} {Preflect:reflect P b}, b = true -> P.
507 Proof. intros P b Preflect; destruct Preflect; solve [ auto | discriminate ]. Qed.
508
509 Lemma reflect_bool_neg : forall {P b} {Preflect:reflect P b}, b = false -> ~P.
510 Proof. intros P b Preflect; destruct Preflect; solve [ auto | discriminate ]. Qed.
511
512 Lemma unreflect_bool : forall {P b} {Preflect:reflect P b}, P -> b = true.
513 Proof. intros P b Preflect; destruct Preflect; solve [ auto | discriminate ]. Qed.
514
515 Lemma unreflect_bool_neg : forall {P b} {Preflect:reflect P b}, ~P -> b = false.
516 Proof. intros P b Preflect; destruct Preflect; solve [ auto | discriminate ]. Qed.
517
518 Ltac reflect_hyps_step :=
519 match goal with
520 | [ H : negb ?b = false |- _ ] => rewrite negb_false_iff in H
521 | [ H : ?b = true |- _ ] => apply (@reflect_bool _ b _) in H
522 | [ H : ?b = false |- _ ] => apply (@reflect_bool_neg _ b _) in H
523 end.
524 Ltac reflect_hyps := repeat reflect_hyps_step.
525
526 Ltac vm_reflect_no_check := apply reflect_bool; vm_cast_no_check (eq_refl true).
527 Ltac lazy_reflect_no_check := apply reflect_bool; exact_no_check (eq_refl true).
528
529 Ltac vm_reflect := abstract vm_reflect_no_check.
530 Ltac lazy_reflect := abstract lazy_reflect_no_check.