Debian Codegraph @

src/Util/Bool.v

raw
1 (*** Boolean Utility Lemmas and Databases *)
2 From Coq Require Import Bool.
3 Require Import Crypto.Util.Notations.
4
5 (** For equalities of booleans *)
6 Create HintDb bool_congr discriminated.
7 (** For properties of booleans, with, e.g., [iff] *)
8 Create HintDb bool_congr_setoid discriminated.
9 (** For generic simplifications of things involving booleans, e.g., if-statements *)
10 Create HintDb boolsimplify discriminated.
11
12 Global Hint Extern 1 => progress autorewrite with boolsimplify in * : boolsimplify.
13 Global Hint Extern 1 => progress autorewrite with bool_congr in * : bool_congr.
14 Global Hint Extern 1 => progress autorewrite with bool_congr_setoid in * : bool_congr_setoid.
15 Global Hint Extern 2 => progress rewrite_strat topdown hints bool_congr_setoid : bool_congr_setoid.
16
17 #[global]
18 Hint Rewrite Bool.andb_diag Bool.orb_diag Bool.eqb_reflx Bool.negb_involutive Bool.eqb_negb1 Bool.eqb_negb2 Bool.orb_true_r Bool.orb_true_l Bool.orb_false_r Bool.orb_false_l Bool.orb_negb_r Bool.andb_false_r Bool.andb_false_l Bool.andb_true_r Bool.andb_false_r Bool.andb_negb_r Bool.xorb_false_r Bool.xorb_false_l Bool.xorb_true_r Bool.xorb_true_l Bool.xorb_nilpotent : bool_congr.
19 #[global]
20 Hint Rewrite Bool.negb_if : boolsimplify.
21 #[global]
22 Hint Rewrite <- Bool.andb_if Bool.andb_lazy_alt Bool.orb_lazy_alt : boolsimplify.
23 #[global]
24 Hint Rewrite Bool.not_true_iff_false Bool.not_false_iff_true Bool.eqb_true_iff Bool.eqb_false_iff Bool.negb_true_iff Bool.negb_false_iff Bool.orb_true_iff Bool.orb_false_iff Bool.andb_true_iff Bool.andb_false_iff Bool.xorb_negb_negb : bool_congr_setoid.
25
26 Create HintDb push_orb discriminated.
27 Create HintDb pull_orb discriminated.
28 Create HintDb push_andb discriminated.
29 Create HintDb pull_andb discriminated.
30 Create HintDb push_negb discriminated.
31 Create HintDb pull_negb discriminated.
32 Global Hint Extern 1 => progress autorewrite with push_orb in * : push_orb.
33 Global Hint Extern 1 => progress autorewrite with pull_orb in * : pull_orb.
34 Global Hint Extern 1 => progress autorewrite with push_andb in * : push_andb.
35 Global Hint Extern 1 => progress autorewrite with pull_andb in * : pull_andb.
36 Global Hint Extern 1 => progress autorewrite with push_negb in * : push_negb.
37 Global Hint Extern 1 => progress autorewrite with pull_negb in * : pull_negb.
38 #[global]
39 Hint Rewrite Bool.negb_orb Bool.negb_andb : push_negb.
40 #[global]
41 Hint Rewrite Bool.xorb_negb_negb : pull_negb.
42 #[global]
43 Hint Rewrite <- Bool.negb_orb Bool.negb_andb Bool.negb_xorb_l Bool.negb_xorb_r : pull_negb.
44 #[global]
45 Hint Rewrite Bool.andb_orb_distrib_r Bool.andb_orb_distrib_l : push_andb.
46 #[global]
47 Hint Rewrite <- Bool.orb_andb_distrib_r Bool.orb_andb_distrib_l : push_andb.
48 #[global]
49 Hint Rewrite Bool.orb_andb_distrib_r Bool.orb_andb_distrib_l : pull_andb.
50 #[global]
51 Hint Rewrite <- Bool.andb_orb_distrib_r Bool.andb_orb_distrib_l : pull_andb.
52 #[global]
53 Hint Rewrite Bool.orb_andb_distrib_r Bool.orb_andb_distrib_l : push_orb.
54 #[global]
55 Hint Rewrite <- Bool.andb_orb_distrib_r Bool.andb_orb_distrib_l : push_orb.
56 #[global]
57 Hint Rewrite <- Bool.orb_andb_distrib_r Bool.orb_andb_distrib_l : pull_orb.
58 #[global]
59 Hint Rewrite Bool.andb_orb_distrib_r Bool.andb_orb_distrib_l : pull_orb.
60
61 Definition pull_bool_if_dep {A B} (f : forall b : bool, A b -> B b) (b : bool) (x : A true) (y : A false)
62 : (if b return B b then f _ x else f _ y) = f b (if b return A b then x else y)
63 := if b return ((if b return B b then f _ x else f _ y) = f b (if b return A b then x else y))
64 then eq_refl
65 else eq_refl.
66
67 Definition pull_bool_if {A B} (f : A -> B) (b : bool) (x : A) (y : A)
68 : (if b then f x else f y) = f (if b then x else y)
69 := @pull_bool_if_dep (fun _ => A) (fun _ => B) (fun _ => f) b x y.
70
71 Definition reflect_iff_gen {P b} : reflect P b -> forall b' : bool, (if b' then P else ~P) <-> b = b'.
72 Proof.
73 intros H; apply reflect_iff in H; intro b'; destruct b, b';
74 intuition congruence.
75 Qed.
76
77 Definition andb_prop : forall a b : bool, a && b = true -> a = true /\ b = true. (* transparent version *)
78 Proof. destruct a, b; simpl; split; try reflexivity; assumption. Defined.
79
80 Definition andb_true_intro : forall a b : bool, a = true /\ b = true -> a && b = true. (* transparent version *)
81 Proof. destruct a, b; simpl; try reflexivity; intros [? ?]; assumption. Defined.
82
83 Definition andb_true_rect {a b : bool} (P : a && b = true -> Type) (f : forall p q, P (andb_true_intro a b (conj p q)))
84 : forall p, P p.
85 Proof.
86 destruct a, b; try specialize (f eq_refl eq_refl); cbn in *; intro p;
87 first [ refine match p with eq_refl => f end
88 | refine match p with eq_refl => I end ].
89 Defined.
90 Definition andb_true_rec {a b : bool} (P : a && b = true -> Set) := @andb_true_rect a b P.
91 Definition andb_true_ind {a b : bool} (P : a && b = true -> Prop) := @andb_true_rec a b P.
92
93 Definition andb_is_true_intro : forall a b : bool, is_true a /\ is_true b -> is_true (a && b)
94 := andb_true_intro.
95
96 Definition andb_is_true_rect {a b : bool} (P : is_true (a && b) -> Type) (f : forall p q, P (andb_is_true_intro a b (conj p q)))
97 : forall p, P p
98 := @andb_true_rect a b P f.
99 Definition andb_is_true_rec {a b : bool} (P : is_true (a && b) -> Set) := @andb_is_true_rect a b P.
100 Definition andb_is_true_ind {a b : bool} (P : is_true (a && b) -> Prop) := @andb_is_true_rec a b P.
101
102 Ltac split_andb_step :=
103 match goal with
104 | [ H : andb _ _ = true |- _ ] => induction H using (@andb_true_rect _ _)
105 | [ H : is_true (andb _ _) |- _ ] => induction H using (@andb_is_true_rect _ _)
106 end.
107 Ltac split_andb := repeat split_andb_step.
108 Ltac split_andb_in_context_step :=
109 match goal with
110 | _ => split_andb_step
111 | [ H : context[andb ?x ?y = true] |- _ ]
112 => rewrite (Bool.andb_true_iff x y) in H
113 | [ H : context[is_true (andb ?x ?y)] |- _ ]
114 => change (is_true (andb x y)) with (andb x y = true) in H;
115 rewrite Bool.andb_true_iff in H;
116 change (x = true /\ y = true) with (is_true x /\ is_true y) in H
117 end.
118 Ltac split_andb_in_context := repeat split_andb_in_context_step.
119
120 Lemma if_const A (b : bool) (x : A) : (if b then x else x) = x.
121 Proof. case b; reflexivity. Qed.
122
123 Lemma ex_bool_iff_or P : @ex bool P <-> (or (P true) (P false)).
124 Proof.
125 split; [ intros [ [] ? ] | intros [?|?]; eexists ]; eauto.
126 Qed.
127
128 Lemma eqb_true_l x : Bool.eqb x true = x. Proof. now destruct x. Qed.
129 Lemma eqb_true_r x : Bool.eqb true x = x. Proof. now destruct x. Qed.
130 Lemma eqb_false_l x : Bool.eqb x false = negb x. Proof. now destruct x. Qed.
131 Lemma eqb_false_r x : Bool.eqb false x = negb x. Proof. now destruct x. Qed.
132 #[global]
133 Hint Rewrite eqb_true_l eqb_true_r eqb_false_l eqb_false_r : boolsimplify.
134
135 Module Thunked.
136 Local Notation lift0 f := (fun (v : unit) => f) (only parsing).
137 Local Notation lift1 f := (fun (x : unit -> bool) (v : unit) => f (x v)) (only parsing).
138 Local Notation lift2 f := (fun (x y : unit -> bool) (v : unit) => f (x v) (y v)) (only parsing).
139 Local Notation lift3 f := (fun (x y z : unit -> bool) (v : unit) => f (x v) (y v) (z v)) (only parsing).
140 Definition bool := unit -> bool.
141 Definition true : bool := lift0 true.
142 Definition false : bool := lift0 false.
143 Definition orb (x y : bool) : bool
144 := fun _ => if x tt then Datatypes.true else y tt.
145 Definition xorb : bool -> bool -> bool := lift2 xorb.
146 Definition andb (x y : bool) : bool
147 := fun _ => if x tt then y tt else Datatypes.false.
148 Definition implb (x y : bool) : bool
149 := fun _ => if x tt then y tt else Datatypes.true.
150 Definition eqb : bool -> bool -> bool := lift2 eqb.
151 Definition ifb (c t f : bool) : bool
152 := fun _ => if c tt then t tt else f tt.
153 Definition eq : bool -> bool -> Prop := fun a b => a tt = b tt.
154
155 Module Export Notations.
156 Declare Scope thunked_bool_scope.
157 Delimit Scope thunked_bool_scope with thunked_bool.
158 Bind Scope thunked_bool_scope with bool.
159 Infix "&&" := andb : thunked_bool_scope.
160 Infix "||" := orb : thunked_bool_scope.
161 End Notations.
162
163 Local Infix "==" := eq.
164
165 Create HintDb thunked_bool_helper discriminated.
166
167 Local Ltac t :=
168 cbv [true false bool orb xorb andb implb eqb ifb eq
169 Datatypes.orb Datatypes.xorb Datatypes.andb Datatypes.implb Bool.eqb Bool.ifb] in *;
170 repeat first [ progress intros
171 | progress subst
172 | reflexivity
173 | congruence
174 | match goal with
175 | [ x : unit |- _ ] => destruct x
176 | [ x : unit -> Datatypes.bool |- _ ] => generalize dependent (x tt); clear x; intros
177 | [ H : context[match ?x with _ => _ end] |- _ ] => is_var x; destruct x
178 | [ |- context[match ?x with _ => _ end] ] => is_var x; destruct x
179 | [ H : and _ _ |- _ ] => destruct H
180 | [ |- and _ _ ] => split
181 end ].
182
183 Lemma unthunk_true : true tt = Datatypes.true.
184 Proof. t. Qed.
185 #[global]
186 Hint Rewrite unthunk_true : unthunk_bool.
187 Lemma unthunk_false : false tt = Datatypes.false.
188 Proof. t. Qed.
189 #[global]
190 Hint Rewrite unthunk_false : unthunk_bool.
191 Lemma unthunk_orb {x y} : orb x y tt = Datatypes.orb (x tt) (y tt).
192 Proof. t. Qed.
193 #[global]
194 Hint Rewrite @unthunk_orb : unthunk_bool.
195 Lemma unthunk_xorb {x y} : xorb x y tt = Datatypes.xorb (x tt) (y tt).
196 Proof. t. Qed.
197 #[global]
198 Hint Rewrite @unthunk_xorb : unthunk_bool.
199 Lemma unthunk_andb {x y} : andb x y tt = Datatypes.andb (x tt) (y tt).
200 Proof. t. Qed.
201 #[global]
202 Hint Rewrite @unthunk_andb : unthunk_bool.
203 Lemma unthunk_implb {x y} : implb x y tt = Datatypes.implb (x tt) (y tt).
204 Proof. t. Qed.
205 #[global]
206 Hint Rewrite @unthunk_implb : unthunk_bool.
207 Lemma unthunk_eqb {x y} : eqb x y tt = Bool.eqb (x tt) (y tt).
208 Proof. t. Qed.
209 #[global]
210 Hint Rewrite @unthunk_eqb : unthunk_bool.
211 Lemma unthunk_ifb {x y z} : ifb x y z tt = Bool.ifb (x tt) (y tt) (z tt).
212 Proof. t. Qed.
213 #[global]
214 Hint Rewrite @unthunk_ifb : unthunk_bool.
215 Lemma unthunk_eq {x y} : eq x y = (x tt = y tt).
216 Proof. t. Qed.
217 #[global]
218 Hint Rewrite @unthunk_eq : unthunk_bool.
219
220 Lemma andb_prop : forall a b : bool, a && b == true -> a == true /\ b == true.
221 Proof. t. Qed.
222 Lemma andb_true_intro : forall b1 b2 : bool, b1 == true /\ b2 == true -> b1 && b2 == true.
223 Proof. t. Qed.
224 End Thunked.