Debian Codegraph @

src/Util/Prod.v

raw
1 (** * Classification of the [×] Type *)
2 (** In this file, we classify the basic structure of [×] types ([prod]
3 in Coq). In particular, we classify equalities of non-dependent
4 pairs (inhabitants of [×] types), so that when we have an equality
5 between two such pairs, or when we want such an equality, we have
6 a systematic way of reducing such equalities to equalities at
7 simpler types. *)
8 From Coq Require Import Morphisms.
9 From Coq Require Import Setoid.
10 From Coq Require Import Bool.
11 Require Import Crypto.Util.IffT.
12 Require Import Crypto.Util.Equality.
13 Require Import Crypto.Util.GlobalSettings.
14
15 Local Arguments fst {_ _} _.
16 Local Arguments snd {_ _} _.
17 Local Arguments f_equal {_ _} _ {_ _} _.
18
19 Scheme Equality for prod.
20
21 Definition prod_beq_hetero {A1 B1 A2 B2} (A_beq_hetero : A1 -> A2 -> bool) (B_beq_hetero : B1 -> B2 -> bool)
22 (x : A1 * B1) (y : A2 * B2) : bool
23 := (A_beq_hetero (fst x) (fst y) && B_beq_hetero (snd x) (snd y))%bool.
24
25 Arguments prod_beq_hetero {_ _ _ _} _ _ x y / .
26
27 Lemma prod_bl_hetero {A1 B1 A2 B2}
28 {A_beq_hetero : A1 -> A2 -> bool} {A_R : A1 -> A2 -> Prop}
29 {B_beq_hetero : B1 -> B2 -> bool} {B_R : B1 -> B2 -> Prop}
30 (A_bl : forall x y, A_beq_hetero x y = true -> A_R x y)
31 (B_bl : forall x y, B_beq_hetero x y = true -> B_R x y)
32 {x y}
33 : prod_beq_hetero A_beq_hetero B_beq_hetero x y = true -> A_R (fst x) (fst y) /\ B_R (snd x) (snd y).
34 Proof using Type.
35 destruct x, y; cbn in *; rewrite ?Bool.andb_true_iff; intuition auto.
36 Qed.
37
38 Lemma prod_lb_hetero {A1 B1 A2 B2}
39 {A_beq_hetero : A1 -> A2 -> bool} {A_R : A1 -> A2 -> Prop}
40 {B_beq_hetero : B1 -> B2 -> bool} {B_R : B1 -> B2 -> Prop}
41 (A_lb : forall x y, A_R x y -> A_beq_hetero x y = true)
42 (B_lb : forall x y, B_R x y -> B_beq_hetero x y = true)
43 {x y}
44 : A_R (fst x) (fst y) /\ B_R (snd x) (snd y) -> prod_beq_hetero A_beq_hetero B_beq_hetero x y = true.
45 Proof using Type.
46 destruct x, y; cbn in *; rewrite ?Bool.andb_true_iff; intuition auto.
47 Qed.
48
49 Lemma prod_beq_hetero_uniform {A B : Type} A_beq B_beq {x y}
50 : prod_beq_hetero A_beq B_beq x y = @prod_beq A B A_beq B_beq x y.
51 Proof. destruct x, y; reflexivity. Qed.
52
53 Lemma prod_bl_hetero_eq {A B}
54 {A_beq : A -> A -> bool}
55 {B_beq : B -> B -> bool}
56 (A_bl : forall x y, A_beq x y = true -> x = y)
57 (B_bl : forall x y, B_beq x y = true -> x = y)
58 {x y}
59 : prod_beq_hetero A_beq B_beq x y = true -> x = y.
60 Proof using Type. rewrite prod_beq_hetero_uniform; now apply internal_prod_dec_bl. Qed.
61
62 Lemma prod_lb_hetero_eq {A B}
63 {A_beq : A -> A -> bool}
64 {B_beq : B -> B -> bool}
65 (A_lb : forall x y, x = y -> A_beq x y = true)
66 (B_lb : forall x y, x = y -> B_beq x y = true)
67 {x y}
68 : x = y -> prod_beq_hetero A_beq B_beq x y = true.
69 Proof using Type. rewrite prod_beq_hetero_uniform; now apply internal_prod_dec_lb. Qed.
70
71 Definition fst_pair {A B} (a:A) (b:B) : fst (a,b) = a := eq_refl.
72 Definition snd_pair {A B} (a:A) (b:B) : snd (a,b) = b := eq_refl.
73 Create HintDb cancel_pair discriminated. #[global] Hint Rewrite @fst_pair @snd_pair : cancel_pair.
74
75 (** ** Equality for [prod] *)
76 Section prod.
77 (** *** Projecting an equality of a pair to equality of the first components *)
78 Definition fst_path {A B} {u v : prod A B} (p : u = v)
79 : fst u = fst v
80 := f_equal fst p.
81
82 (** *** Projecting an equality of a pair to equality of the second components *)
83 Definition snd_path {A B} {u v : prod A B} (p : u = v)
84 : snd u = snd v
85 := f_equal snd p.
86
87 (** *** Equality of [prod] is itself a [prod] *)
88 Definition path_prod_uncurried {A B : Type} (u v : prod A B)
89 (pq : prod (fst u = fst v) (snd u = snd v))
90 : u = v.
91 Proof.
92 destruct u as [u1 u2], v as [v1 v2]; simpl in *.
93 destruct pq as [p q].
94 destruct p, q; simpl in *.
95 reflexivity.
96 Defined.
97
98 (** *** Equality of [pair]s is itself a [prod] *)
99 Definition path_pair_uncurried {A B : Type} (a1 a2 : A) (b1 b2 : B)
100 (pq : prod (a1 = a2) (b1 = b2))
101 : (a1, b1) = (a2, b2)
102 := path_prod_uncurried (_, _) (_, _) pq.
103
104 (** *** Curried version of proving equality of prod types *)
105 Definition path_prod {A B : Type} (u v : prod A B)
106 (p : fst u = fst v) (q : snd u = snd v)
107 : u = v
108 := path_prod_uncurried u v (pair p q).
109
110 (** *** Curried version of proving equality of prod types *)
111 Definition path_pair {A B : Type} (a1 a2 : A) (b1 b2 : B)
112 (p : a1 = a2) (q : b1 = b2)
113 : (a1, b1) = (a2, b2)
114 := path_pair_uncurried _ _ _ _ (pair p q).
115
116 (** *** Equivalence of equality of [prod] with a [prod] of equality *)
117 (** We could actually use [IsIso] here, but for simplicity, we
118 don't. If we wanted to deal with proofs of equality of × types
119 in dependent positions, we'd need it. *)
120 Definition path_prod_uncurried_iff {A B}
121 (u v : @prod A B)
122 : u = v <-> (prod (fst u = fst v) (snd u = snd v)).
123 Proof.
124 split; [ intro; subst; split; reflexivity | apply path_prod_uncurried ].
125 Defined.
126
127 Definition path_pair_uncurried_iff {A B}
128 (a1 a2 : A) (b1 b2 : B)
129 : (a1, b1) = (a2, b2) <-> (prod (a1 = a2) (b1 = b2))
130 := path_prod_uncurried_iff (_, _) (_, _).
131
132 (** *** Eta-expansion of [@eq (prod _ _)] *)
133 Definition path_prod_eta {A B} {u v : @prod A B} (p : u = v)
134 : p = path_prod_uncurried u v (fst_path p, snd_path p).
135 Proof. destruct u, p; reflexivity. Defined.
136
137 (** *** Induction principle for [@eq (prod _ _)] *)
138 Definition path_prod_rect {A B} {u v : @prod A B} (P : u = v -> Type)
139 (f : forall p q, P (path_prod_uncurried u v (p, q)))
140 : forall p, P p.
141 Proof. intro p; specialize (f (fst_path p) (snd_path p)); destruct u, p; exact f. Defined.
142 Definition path_prod_rec {A B u v} (P : u = v :> @prod A B -> Set) := path_prod_rect P.
143 Definition path_prod_ind {A B u v} (P : u = v :> @prod A B -> Prop) := path_prod_rec P.
144
145 Definition path_pair_rect {A B} {a1 a2 : A} {b1 b2 : B} (P : (a1, b1) = (a2, b2) -> Type)
146 (f : forall p q, P (path_pair_uncurried _ _ _ _ (p, q)))
147 : forall p, P p
148 := path_prod_rect P f.
149 Definition path_pair_rec {A B a1 a2 b1 b2} (P : (a1, b1) = (a2, b2) :> @prod A B -> Set) := path_pair_rect P.
150 Definition path_pair_ind {A B a1 a2 b1 b2} (P : (a1, b1) = (a2, b2) :> @prod A B -> Prop) := path_pair_rec P.
151 End prod.
152
153 Lemma prod_iff_and (A B : Prop) : (A /\ B) <-> (A * B).
154 Proof. repeat (intros [? ?] || intro || split); assumption. Defined.
155
156 Global Instance iff_prod_Proper
157 : Proper (iff ==> iff ==> iff) (fun A B => prod A B).
158 Proof. repeat intro; tauto. Defined.
159 Global Instance iff_iffTp_prod_Proper
160 : Proper (iff ==> iffTp ==> iffTp) (fun A B => prod A B) | 1.
161 Proof.
162 intros ?? [?] ?? [?]; constructor; tauto.
163 Defined.
164 Global Instance iffTp_iff_prod_Proper
165 : Proper (iffTp ==> iff ==> iffTp) (fun A B => prod A B) | 1.
166 Proof.
167 intros ?? [?] ?? [?]; constructor; tauto.
168 Defined.
169 Global Instance iffTp_iffTp_prod_Proper
170 : Proper (iffTp ==> iffTp ==> iffTp) (fun A B => prod A B) | 1.
171 Proof.
172 intros ?? [?] ?? [?]; constructor; tauto.
173 Defined.
174 Global Hint Extern 2 (Proper _ prod) => apply iffTp_iffTp_prod_Proper : typeclass_instances.
175 Global Hint Extern 2 (Proper _ (fun A => prod A)) => refine iff_iffTp_prod_Proper : typeclass_instances.
176 Global Hint Extern 2 (Proper _ (fun A B => prod A B)) => refine iff_prod_Proper : typeclass_instances.
177
178 (** ** Useful Tactics *)
179 (** *** [inversion_prod] *)
180 Ltac simpl_proj_pair_in H :=
181 repeat match type of H with
182 | context G[fst (pair ?x ?p)]
183 => let G' := context G[x] in change G' in H
184 | context G[snd (pair ?x ?p)]
185 => let G' := context G[p] in change G' in H
186 end.
187 Ltac induction_path_prod H :=
188 let H0 := fresh H in
189 let H1 := fresh H in
190 induction H as [H0 H1] using path_prod_rect;
191 simpl_proj_pair_in H0;
192 simpl_proj_pair_in H1.
193 Ltac inversion_prod_step :=
194 match goal with
195 | [ H : _ = pair _ _ |- _ ]
196 => induction_path_prod H
197 | [ H : pair _ _ = _ |- _ ]
198 => induction_path_prod H
199 end.
200 Ltac inversion_prod := repeat inversion_prod_step.
201
202 (** *** [inversion_pair] *)
203 (** A version of [inversion_prod] that occurs only when there are pairs on both sides *)
204 Ltac induction_path_pair H :=
205 let H0 := fresh H in
206 let H1 := fresh H in
207 induction H as [H0 H1] using path_pair_rect.
208 Ltac inversion_pair_step :=
209 match goal with
210 | [ H : pair _ _ = pair _ _ |- _ ]
211 => induction_path_pair H
212 | [ H : pair _ _ = pair _ _ |- _ ]
213 => induction_path_pair H
214 end.
215 Ltac inversion_pair := repeat inversion_pair_step.
216
217
218 (** This turns a goal like [x = let v := p in let '(x, y) := f v in x
219 + y)] into a goal like [x = fst (f p) + snd (f p)]. Note that it
220 inlines [let ... in ...] as well as destructuring lets. *)
221 Ltac only_eta_expand_and_contract_step :=
222 match goal with
223 | [ |- context[let '(x, y) := ?e in _] ]
224 => rewrite (surjective_pairing e)
225 | [ H : context[let '(x, y) := ?e in _] |- _ ]
226 => rewrite (surjective_pairing e) in H
227 | _ => rewrite <- !surjective_pairing
228 | [ H : context[(fst ?e, snd ?e)] |- _ ]
229 => rewrite <- !surjective_pairing in H
230 end.
231 Ltac only_eta_expand_and_contract := repeat only_eta_expand_and_contract_step.
232 Ltac eta_expand :=
233 repeat first [ progress cbv beta iota zeta
234 | only_eta_expand_and_contract_step ].
235
236 (** *** [subst_prod] *)
237 (** The tactic [subst_prod] is like [subst], but it works on equations
238 of the form [_ = (x, y)] and [(x, y) = _] for [x] and [y]
239 identifiers (or recursively pairs of identitifers). *)
240 Ltac on_pair_vars tac x :=
241 lazymatch x with
242 | pair ?x ?y => on_pair_vars tac x; on_pair_vars tac y
243 | _ => tac x
244 end.
245 Ltac is_pair_var x := on_pair_vars is_var x.
246 Ltac do_subst_prod v :=
247 lazymatch v with
248 | @pair ?A ?B ?x ?y
249 => let H := fresh in
250 let xy := fresh v in
251 remember v as xy eqn:H;
252 assert (fst xy = x) by (subst xy; reflexivity);
253 assert (snd xy = y) by (subst xy; reflexivity);
254 clear H;
255 do_subst_prod x; do_subst_prod y;
256 try subst xy
257 | _ => subst v
258 end.
259 Ltac subst_prod_step :=
260 first [ inversion_pair_step
261 | match goal with
262 | [ H : _ = ?v |- _ ] => is_pair_var v; do_subst_prod v
263 | [ H : ?v = _ |- _ ] => is_pair_var v; do_subst_prod v
264 end ].
265 Ltac subst_prod := repeat subst_prod_step.