Debian Codegraph @

src/Util/Sigma.v

raw
1 (** * Classification of the Σ Type *)
2 (** In this file, we classify the basic structure of Σ types ([sigT],
3 [sig], and [ex], in Coq). In particular, we classify equalities
4 of dependent pairs (inhabitants of Σ types), so that when we have
5 an equality between two such pairs, or when we want such an
6 equality, we have a systematic way of reducing such equalities to
7 equalities at simpler types. *)
8 Require Import Crypto.Util.Equality.
9 Require Import Crypto.Util.GlobalSettings.
10
11 Local Arguments projT1 {_ _} _.
12 Local Arguments projT2 {_ _} _.
13 Local Arguments proj1_sig {_ _} _.
14 Local Arguments proj1_sig {_ _} _.
15 Local Arguments f_equal {_ _} _ {_ _} _.
16
17 (** ** Equality for [sigT] *)
18 Section sigT.
19
20 (** *** Projecting an equality of a pair to equality of the first components *)
21 Definition pr1_path {A} {P : A -> Type} {u v : sigT P} (p : u = v)
22 : projT1 u = projT1 v
23 := f_equal (@projT1 _ _) p.
24
25 (** *** Projecting an equality of a pair to equality of the second components *)
26 Definition pr2_path {A} {P : A -> Type} {u v : sigT P} (p : u = v)
27 : eq_rect _ _ (projT2 u) _ (pr1_path p) = projT2 v.
28 Proof.
29 destruct p; reflexivity.
30 Defined.
31
32 (** *** Equality of [sigT] is itself a [sigT] *)
33 Definition path_sigT_uncurried {A : Type} {P : A -> Type} (u v : sigT P)
34 (pq : sigT (fun p : projT1 u = projT1 v => eq_rect _ _ (projT2 u) _ p = projT2 v))
35 : u = v.
36 Proof.
37 destruct u as [u1 u2], v as [v1 v2]; simpl in *.
38 destruct pq as [p q].
39 destruct q; simpl in *.
40 destruct p; reflexivity.
41 Defined.
42
43 (** *** Curried version of proving equality of sigma types *)
44 Definition path_sigT {A : Type} {P : A -> Type} (u v : sigT P)
45 (p : projT1 u = projT1 v) (q : eq_rect _ _ (projT2 u) _ p = projT2 v)
46 : u = v
47 := path_sigT_uncurried u v (existT _ p q).
48
49 (** *** Equality of [sigT] when the property is an hProp *)
50 Definition path_sigT_hprop {A P} (P_hprop : forall (x : A) (p q : P x), p = q)
51 (u v : @sigT A P)
52 (p : projT1 u = projT1 v)
53 : u = v
54 := path_sigT u v p (P_hprop _ _ _).
55
56 (** *** Equivalence of equality of [sigT] with a [sigT] of equality *)
57 (** We could actually use [IsIso] here, but for simplicity, we
58 don't. If we wanted to deal with proofs of equality of Σ types
59 in dependent positions, we'd need it. *)
60 Definition path_sigT_uncurried_iff {A P}
61 (u v : @sigT A P)
62 : u = v <-> (sigT (fun p : projT1 u = projT1 v => eq_rect _ _ (projT2 u) _ p = projT2 v)).
63 Proof.
64 split; [ intro; subst; exists eq_refl; reflexivity | apply path_sigT_uncurried ].
65 Defined.
66
67 (** *** Induction principle for [@eq (sigT _)] *)
68 Definition path_sigT_rect {A P} {u v : @sigT A P} (Q : u = v -> Type)
69 (f : forall p q, Q (path_sigT u v p q))
70 : forall p, Q p.
71 Proof. intro p; specialize (f (pr1_path p) (pr2_path p)); destruct u, p; exact f. Defined.
72 Definition path_sigT_rec {A P u v} (Q : u = v :> @sigT A P -> Set) := path_sigT_rect Q.
73 Definition path_sigT_ind {A P u v} (Q : u = v :> @sigT A P -> Prop) := path_sigT_rec Q.
74
75 (** *** Equivalence of equality of [sigT] involving hProps with equality of the first components *)
76 Definition path_sigT_hprop_iff {A P} (P_hprop : forall (x : A) (p q : P x), p = q)
77 (u v : @sigT A P)
78 : u = v <-> (projT1 u = projT1 v)
79 := conj (f_equal projT1) (path_sigT_hprop P_hprop u v).
80
81 (** *** Non-dependent classification of equality of [sigT] *)
82 Definition path_sigT_nondep {A B : Type} (u v : @sigT A (fun _ => B))
83 (p : projT1 u = projT1 v) (q : projT2 u = projT2 v)
84 : u = v
85 := @path_sigT _ _ u v p (eq_trans (transport_const _ _) q).
86
87 (** *** Classification of transporting across an equality of [sigT]s *)
88 Lemma eq_rect_sigT {A x} {P : A -> Type} (Q : forall a, P a -> Prop) (u : sigT (Q x)) {y} (H : x = y)
89 : eq_rect x (fun a => sigT (Q a)) u y H
90 = existT
91 (Q y)
92 (eq_rect x P (projT1 u) y H)
93 match H in (_ = y) return Q y (eq_rect x P (projT1 u) y H) with
94 | eq_refl => projT2 u
95 end.
96 Proof.
97 destruct H, u; reflexivity.
98 Defined.
99
100 (** *** η Principle for [sigT] *)
101 Definition sigT_eta {A P} (p : { a : A & P a })
102 : p = existT _ (projT1 p) (projT2 p).
103 Proof. destruct p; reflexivity. Defined.
104
105 (** *** Boolean equality for [sigT] *)
106 Definition sigT_beq {A A' P P'} (eqA : A -> A' -> bool) (eqP : forall a a', P a -> P' a' -> bool) (p : { a : A & P a }) (q : { a' : A' & P' a' })
107 : bool
108 := andb (eqA (projT1 p) (projT1 q)) (eqP _ _ (projT2 p) (projT2 q)).
109
110 Lemma sigT_dec_bl {A P}
111 (eqA : A -> A -> bool) (eqP : forall a a', P a -> P a' -> bool)
112 (blA : forall a a', eqA a a' = true -> a = a')
113 (blP : forall a p p', eqP a a p p' = true -> p = p')
114 {p q}
115 : sigT_beq eqA eqP p q = true -> p = q.
116 Proof.
117 destruct p as [a p], q as [a' q]; cbv.
118 specialize (blA a a'); specialize (blP a p).
119 destruct (eqA a a') eqn:H'; [ specialize (blA eq_refl) | discriminate ]; subst.
120 intro H''; specialize (blP _ H''); subst; reflexivity.
121 Qed.
122
123 Lemma sigT_dec_lb {A P}
124 (eqA : A -> A -> bool) (eqP : forall a a', P a -> P a' -> bool)
125 (lbA : forall a a', a = a' -> eqA a a' = true)
126 (lbP : forall a p p', p = p' -> eqP a a p p' = true)
127 {p q}
128 : p = q -> sigT_beq eqA eqP p q = true.
129 Proof.
130 intro; subst q; destruct p as [a p]; cbv in *.
131 specialize (lbA a a eq_refl); specialize (lbP a p p eq_refl).
132 now rewrite lbA, lbP.
133 Qed.
134
135 Lemma sigT_dec_bl_hprop {A P}
136 (eqA : A -> A -> bool)
137 (blA : forall a a', eqA a a' = true -> a = a')
138 (P_hprop : forall (x : A) (p q : P x), p = q)
139 {p q}
140 : @sigT_beq A A P P eqA (fun _ _ _ _ => true) p q = true -> p = q.
141 Proof. apply sigT_dec_bl; eauto. Qed.
142
143 Lemma sigT_dec_lb_hprop {A P}
144 (eqA : A -> A -> bool)
145 (lbA : forall a a', a = a' -> eqA a a' = true)
146 (P_hprop : forall (x : A) (p q : P x), p = q)
147 {p q}
148 : p = q -> @sigT_beq A A P P eqA (fun _ _ _ _ => true) p q = true.
149 Proof. apply sigT_dec_lb; eauto. Qed.
150
151 Definition sigT_eq_dec {A P}
152 (eqA : A -> A -> bool) (eqP : forall a a', P a -> P a' -> bool)
153 (blA : forall a a', eqA a a' = true -> a = a')
154 (blP : forall a p p', eqP a a p p' = true -> p = p')
155 (lbA : forall a a', a = a' -> eqA a a' = true)
156 (lbP : forall a p p', p = p' -> eqP a a p p' = true)
157 (p q : { a : A & P a })
158 : {p = q} + {p <> q}.
159 Proof.
160 destruct (sigT_beq eqA eqP p q) eqn:H; [ left | right; intro H' ].
161 { eapply sigT_dec_bl; eassumption. }
162 { eapply sigT_dec_lb in H'; [ | eassumption.. ].
163 generalize (eq_trans (eq_sym H) H'); clear; abstract discriminate. }
164 Defined.
165 End sigT.
166
167 (** ** Equality for [sig] *)
168 Section sig.
169 Definition proj1_sig_path {A} {P : A -> Prop} {u v : sig P} (p : u = v)
170 : proj1_sig u = proj1_sig v
171 := f_equal (@proj1_sig _ _) p.
172
173 Definition proj2_sig_path {A} {P : A -> Prop} {u v : sig P} (p : u = v)
174 : eq_rect _ _ (proj2_sig u) _ (proj1_sig_path p) = proj2_sig v.
175 Proof.
176 destruct p; reflexivity.
177 Defined.
178
179 Definition path_sig_uncurried {A : Type} {P : A -> Prop} (u v : sig P)
180 (pq : {p : proj1_sig u = proj1_sig v | eq_rect _ _ (proj2_sig u) _ p = proj2_sig v})
181 : u = v.
182 Proof.
183 destruct u as [u1 u2], v as [v1 v2]; simpl in *.
184 destruct pq as [p q].
185 destruct q; simpl in *.
186 destruct p; reflexivity.
187 Defined.
188
189 Definition path_sig {A : Type} {P : A -> Prop} (u v : sig P)
190 (p : proj1_sig u = proj1_sig v) (q : eq_rect _ _ (proj2_sig u) _ p = proj2_sig v)
191 : u = v
192 := path_sig_uncurried u v (exist _ p q).
193
194 Definition path_sig_rect {A P} {u v : @sig A P} (Q : u = v -> Type)
195 (f : forall p q, Q (path_sig u v p q))
196 : forall p, Q p.
197 Proof. intro p; specialize (f (proj1_sig_path p) (proj2_sig_path p)); destruct u, p; exact f. Defined.
198 Definition path_sig_rec {A P u v} (Q : u = v :> @sig A P -> Set) := path_sig_rect Q.
199 Definition path_sig_ind {A P u v} (Q : u = v :> @sig A P -> Prop) := path_sig_rec Q.
200
201 Definition path_sig_hprop {A} {P : A -> Prop} (P_hprop : forall (x : A) (p q : P x), p = q)
202 (u v : @sig A P)
203 (p : proj1_sig u = proj1_sig v)
204 : u = v
205 := path_sig u v p (P_hprop _ _ _).
206
207 Definition path_sig_uncurried_iff {A} {P : A -> Prop}
208 (u v : @sig A P)
209 : u = v <-> (sig (fun p : proj1_sig u = proj1_sig v => eq_rect _ _ (proj2_sig u) _ p = proj2_sig v)).
210 Proof.
211 split; [ intro; subst; exists eq_refl; reflexivity | apply path_sig_uncurried ].
212 Defined.
213
214 Definition path_sig_hprop_iff {A} {P : A -> Prop} (P_hprop : forall (x : A) (p q : P x), p = q)
215 (u v : @sig A P)
216 : u = v <-> (proj1_sig u = proj1_sig v)
217 := conj (f_equal proj1_sig) (path_sig_hprop P_hprop u v).
218
219 Lemma eq_rect_sig {A x} {P : A -> Type} (Q : forall a, P a -> Prop) (u : sig (Q x)) {y} (H : x = y)
220 : eq_rect x (fun a => sig (Q a)) u y H
221 = exist
222 (Q y)
223 (eq_rect x P (proj1_sig u) y H)
224 match H in (_ = y) return Q y (eq_rect x P (proj1_sig u) y H) with
225 | eq_refl => proj2_sig u
226 end.
227 Proof.
228 destruct H, u; reflexivity.
229 Defined.
230
231 (** *** η Principle for [sig] *)
232 Definition sig_eta {A P} (p : { a : A | P a })
233 : p = exist _ (proj1_sig p) (proj2_sig p).
234 Proof. destruct p; reflexivity. Defined.
235
236 (** *** Boolean equality for [sig] *)
237 Definition sig_beq {A A'} {P P' : _ -> Prop} (eqA : A -> A' -> bool) (eqP : forall a a', P a -> P' a' -> bool) (p : { a : A | P a }) (q : { a' : A' | P' a' })
238 : bool
239 := andb (eqA (proj1_sig p) (proj1_sig q)) (eqP _ _ (proj2_sig p) (proj2_sig q)).
240
241 Lemma sig_dec_bl {A} {P : A -> Prop}
242 (eqA : A -> A -> bool) (eqP : forall a a', P a -> P a' -> bool)
243 (blA : forall a a', eqA a a' = true -> a = a')
244 (blP : forall a p p', eqP a a p p' = true -> p = p')
245 {p q}
246 : sig_beq eqA eqP p q = true -> p = q.
247 Proof.
248 destruct p as [a p], q as [a' q]; cbv.
249 specialize (blA a a'); specialize (blP a p).
250 destruct (eqA a a') eqn:H'; [ specialize (blA eq_refl) | discriminate ]; subst.
251 intro H''; specialize (blP _ H''); subst; reflexivity.
252 Qed.
253
254 Lemma sig_dec_lb {A} {P : A -> Prop}
255 (eqA : A -> A -> bool) (eqP : forall a a', P a -> P a' -> bool)
256 (lbA : forall a a', a = a' -> eqA a a' = true)
257 (lbP : forall a p p', p = p' -> eqP a a p p' = true)
258 {p q}
259 : p = q -> sig_beq eqA eqP p q = true.
260 Proof.
261 intro; subst q; destruct p as [a p]; cbv in *.
262 specialize (lbA a a eq_refl); specialize (lbP a p p eq_refl).
263 now rewrite lbA, lbP.
264 Qed.
265
266 Lemma sig_dec_bl_hprop {A} {P : A -> Prop}
267 (eqA : A -> A -> bool)
268 (blA : forall a a', eqA a a' = true -> a = a')
269 (P_hprop : forall (x : A) (p q : P x), p = q)
270 {p q}
271 : @sig_beq A A P P eqA (fun _ _ _ _ => true) p q = true -> p = q.
272 Proof. apply sig_dec_bl; eauto. Qed.
273
274 Lemma sig_dec_lb_hprop {A} {P : A -> Prop}
275 (eqA : A -> A -> bool)
276 (lbA : forall a a', a = a' -> eqA a a' = true)
277 (P_hprop : forall (x : A) (p q : P x), p = q)
278 {p q}
279 : p = q -> @sig_beq A A P P eqA (fun _ _ _ _ => true) p q = true.
280 Proof. apply sig_dec_lb; eauto. Qed.
281
282 Definition sig_eq_dec {A} {P : A -> Prop}
283 (eqA : A -> A -> bool) (eqP : forall a a', P a -> P a' -> bool)
284 (blA : forall a a', eqA a a' = true -> a = a')
285 (blP : forall a p p', eqP a a p p' = true -> p = p')
286 (lbA : forall a a', a = a' -> eqA a a' = true)
287 (lbP : forall a p p', p = p' -> eqP a a p p' = true)
288 (p q : { a : A | P a })
289 : {p = q} + {p <> q}.
290 Proof.
291 destruct (sig_beq eqA eqP p q) eqn:H; [ left | right; intro H' ].
292 { eapply sig_dec_bl; eassumption. }
293 { eapply sig_dec_lb in H'; [ | eassumption.. ].
294 generalize (eq_trans (eq_sym H) H'); clear; abstract discriminate. }
295 Defined.
296 End sig.
297
298 (** ** Equality for [ex] *)
299 Section ex.
300 Definition path_ex_uncurried' {A : Type} (P : A -> Prop) {u1 v1 : A} {u2 : P u1} {v2 : P v1}
301 (pq : exists p : u1 = v1, eq_rect _ _ u2 _ p = v2)
302 : ex_intro P u1 u2 = ex_intro P v1 v2.
303 Proof.
304 destruct pq as [p q].
305 destruct q; simpl in *.
306 destruct p; reflexivity.
307 Defined.
308
309 Definition path_ex' {A : Type} {P : A -> Prop} (u1 v1 : A) (u2 : P u1) (v2 : P v1)
310 (p : u1 = v1) (q : eq_rect _ _ u2 _ p = v2)
311 : ex_intro P u1 u2 = ex_intro P v1 v2
312 := path_ex_uncurried' P (ex_intro _ p q).
313
314 Definition path_ex'_hprop {A} {P : A -> Prop} (P_hprop : forall (x : A) (p q : P x), p = q)
315 (u1 v1 : A) (u2 : P u1) (v2 : P v1)
316 (p : u1 = v1)
317 : ex_intro P u1 u2 = ex_intro P v1 v2
318 := path_ex' u1 v1 u2 v2 p (P_hprop _ _ _).
319
320 Lemma eq_rect_ex {A x} {P : A -> Type} (Q : forall a, P a -> Prop) (u : ex (Q x)) {y} (H : x = y)
321 : eq_rect x (fun a => ex (Q a)) u y H
322 = match u with
323 | ex_intro u1 u2
324 => ex_intro
325 (Q y)
326 (eq_rect x P u1 y H)
327 match H in (_ = y) return Q y (eq_rect x P u1 y H) with
328 | eq_refl => u2
329 end
330 end.
331 Proof.
332 destruct H, u; reflexivity.
333 Defined.
334 End ex.
335
336 (** ** Useful Tactics *)
337 (** *** [inversion_sigma] *)
338 (** The built-in [inversion] will frequently leave equalities of
339 dependent pairs. When the first type in the pair is an hProp or
340 otherwise simplifies, [inversion_sigma] is useful; it will replace
341 the equality of pairs with a pair of equalities, one involving a
342 term casted along the other. This might also prove useful for
343 writing a version of [inversion] / [dependent destruction] which
344 does not lose information, i.e., does not turn a goal which is
345 provable into one which requires axiom K / UIP. *)
346 Ltac simpl_proj_exist_in H :=
347 repeat match type of H with
348 | context G[proj1_sig (exist _ ?x ?p)]
349 => let G' := context G[x] in change G' in H
350 | context G[proj2_sig (exist _ ?x ?p)]
351 => let G' := context G[p] in change G' in H
352 | context G[projT1 (existT _ ?x ?p)]
353 => let G' := context G[x] in change G' in H
354 | context G[projT2 (existT _ ?x ?p)]
355 => let G' := context G[p] in change G' in H
356 end.
357 Ltac induction_sigma_in_as_using H H0 H1 rect :=
358 induction H as [H0 H1] using (rect _ _ _ _);
359 simpl_proj_exist_in H0;
360 simpl_proj_exist_in H1.
361 Ltac induction_sigma_in_using H rect :=
362 let H0 := fresh H in
363 let H1 := fresh H in
364 induction_sigma_in_as_using H H0 H1 rect.
365 Ltac inversion_sigma_step :=
366 match goal with
367 | [ H : _ = exist _ _ _ |- _ ]
368 => induction_sigma_in_using H @path_sig_rect
369 | [ H : _ = existT _ _ _ |- _ ]
370 => induction_sigma_in_using H @path_sigT_rect
371 | [ H : exist _ _ _ = _ |- _ ]
372 => induction_sigma_in_using H @path_sig_rect
373 | [ H : existT _ _ _ = _ |- _ ]
374 => induction_sigma_in_using H @path_sigT_rect
375 end.
376 Ltac inversion_sigma := repeat inversion_sigma_step.