Debian Codegraph @

theories/Corelib/Init/Logic.v

raw
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 Set Implicit Arguments.
12
13 Require Export Notations.
14 Require Import Ltac.
15
16 (** * Propositional connectives *)
17
18 (** [True] is the always true proposition *)
19
20 Inductive True : Prop :=
21 I : True.
22
23 Register True as core.True.type.
24 Register I as core.True.I.
25
26 (** [False] is the always false proposition *)
27 Inductive False : Prop :=.
28
29 Register False as core.False.type.
30
31 (** [not A], written [~A], is the negation of [A] *)
32 Definition not (A:Prop) := A -> False.
33
34 Notation "~ x" := (not x) : type_scope.
35
36 Register not as core.not.type.
37
38 (** Negation of a type in [Type] *)
39
40 Definition notT (A:Type) := A -> False.
41
42 (** Create the "core" hint database, and set its transparent state for
43 variables and constants explicitly. *)
44
45 Create HintDb core.
46 #[global]
47 Hint Variables Opaque : core.
48 #[global]
49 Hint Constants Opaque : core.
50
51 #[global]
52 Hint Unfold not: core.
53
54 (** [and A B], written [A /\ B], is the conjunction of [A] and [B]
55
56 [conj p q] is a proof of [A /\ B] as soon as
57 [p] is a proof of [A] and [q] a proof of [B]
58
59 [proj1] and [proj2] are first and second projections of a conjunction *)
60
61 Inductive and (A B:Prop) : Prop :=
62 conj : A -> B -> A /\ B
63
64 where "A /\ B" := (and A B) : type_scope.
65
66 Register and as core.and.type.
67 Register conj as core.and.conj.
68
69 Section Conjunction.
70
71 Variables A B : Prop.
72
73 Theorem proj1 : A /\ B -> A.
74 Proof.
75 destruct 1; trivial.
76 Qed.
77
78 Theorem proj2 : A /\ B -> B.
79 Proof.
80 destruct 1; trivial.
81 Qed.
82
83 End Conjunction.
84
85 (** [or A B], written [A \/ B], is the disjunction of [A] and [B] *)
86
87 Inductive or (A B:Prop) : Prop :=
88 | or_introl : A -> A \/ B
89 | or_intror : B -> A \/ B
90
91 where "A \/ B" := (or A B) : type_scope.
92
93 Arguments or_introl [A B] _, [A] B _.
94 Arguments or_intror [A B] _, A [B] _.
95
96 Register or as core.or.type.
97
98 (** [iff A B], written [A <-> B], expresses the equivalence of [A] and [B] *)
99
100 Definition iff (A B:Prop) := (A -> B) /\ (B -> A).
101
102 Notation "A <-> B" := (iff A B) : type_scope.
103
104 Register iff as core.iff.type.
105 Register proj1 as core.iff.proj1.
106 Register proj2 as core.iff.proj2.
107
108 Section Equivalence.
109
110 Theorem iff_refl : forall A:Prop, A <-> A.
111 Proof.
112 split; auto.
113 Qed.
114
115 Theorem iff_trans : forall A B C:Prop, (A <-> B) -> (B <-> C) -> (A <-> C).
116 Proof.
117 intros A B C [H1 H2] [H3 H4]; split; auto.
118 Qed.
119
120 Theorem iff_sym : forall A B:Prop, (A <-> B) -> (B <-> A).
121 Proof.
122 intros A B [H1 H2]; split; auto.
123 Qed.
124
125 End Equivalence.
126
127 #[global]
128 Create HintDb extcore.
129
130 #[global]
131 Hint Unfold iff: extcore.
132
133 (** Backward direction of the equivalences above does not need assumptions *)
134
135 Theorem and_iff_compat_l : forall A B C : Prop,
136 (B <-> C) -> (A /\ B <-> A /\ C).
137 Proof.
138 intros ? ? ? [Hl Hr]; split; intros [? ?]; (split; [ assumption | ]);
139 [apply Hl | apply Hr]; assumption.
140 Qed.
141
142 Theorem and_iff_compat_r : forall A B C : Prop,
143 (B <-> C) -> (B /\ A <-> C /\ A).
144 Proof.
145 intros ? ? ? [Hl Hr]; split; intros [? ?]; (split; [ | assumption ]);
146 [apply Hl | apply Hr]; assumption.
147 Qed.
148
149 Theorem or_iff_compat_l : forall A B C : Prop,
150 (B <-> C) -> (A \/ B <-> A \/ C).
151 Proof.
152 intros ? ? ? [Hl Hr]; split; (intros [?|?]; [left; assumption| right]);
153 [apply Hl | apply Hr]; assumption.
154 Qed.
155
156 Theorem or_iff_compat_r : forall A B C : Prop,
157 (B <-> C) -> (B \/ A <-> C \/ A).
158 Proof.
159 intros ? ? ? [Hl Hr]; split; (intros [?|?]; [left| right; assumption]);
160 [apply Hl | apply Hr]; assumption.
161 Qed.
162
163 Theorem imp_iff_compat_l : forall A B C : Prop,
164 (B <-> C) -> ((A -> B) <-> (A -> C)).
165 Proof.
166 intros ? ? ? [Hl Hr]; split; intros H ?; [apply Hl | apply Hr]; apply H; assumption.
167 Qed.
168
169 Theorem imp_iff_compat_r : forall A B C : Prop,
170 (B <-> C) -> ((B -> A) <-> (C -> A)).
171 Proof.
172 intros ? ? ? [Hl Hr]; split; intros H ?; [apply H, Hr | apply H, Hl]; assumption.
173 Qed.
174
175 Theorem not_iff_compat : forall A B : Prop,
176 (A <-> B) -> (~ A <-> ~B).
177 Proof.
178 intros; apply imp_iff_compat_r; assumption.
179 Qed.
180
181
182 (** Some equivalences *)
183
184 Theorem neg_false : forall A : Prop, ~ A <-> (A <-> False).
185 Proof.
186 intro A; unfold not; split.
187 - intro H; split; [exact H | intro H1; elim H1].
188 - intros [H _]; exact H.
189 Qed.
190
191 Theorem and_cancel_l : forall A B C : Prop,
192 (B -> A) -> (C -> A) -> ((A /\ B <-> A /\ C) <-> (B <-> C)).
193 Proof.
194 intros A B C Hl Hr.
195 split; [ | apply and_iff_compat_l]; intros [HypL HypR]; split; intros.
196 + apply HypL; split; [apply Hl | ]; assumption.
197 + apply HypR; split; [apply Hr | ]; assumption.
198 Qed.
199
200 Theorem and_cancel_r : forall A B C : Prop,
201 (B -> A) -> (C -> A) -> ((B /\ A <-> C /\ A) <-> (B <-> C)).
202 Proof.
203 intros A B C Hl Hr.
204 split; [ | apply and_iff_compat_r]; intros [HypL HypR]; split; intros.
205 + apply HypL; split; [ | apply Hl ]; assumption.
206 + apply HypR; split; [ | apply Hr ]; assumption.
207 Qed.
208
209 Theorem and_comm : forall A B : Prop, A /\ B <-> B /\ A.
210 Proof.
211 intros; split; intros [? ?]; split; assumption.
212 Qed.
213
214 Theorem and_assoc : forall A B C : Prop, (A /\ B) /\ C <-> A /\ B /\ C.
215 Proof.
216 intros; split; [ intros [[? ?] ?]| intros [? [? ?]]]; repeat split; assumption.
217 Qed.
218
219 Theorem or_cancel_l : forall A B C : Prop,
220 (B -> ~ A) -> (C -> ~ A) -> ((A \/ B <-> A \/ C) <-> (B <-> C)).
221 Proof.
222 intros ? ? ? Fl Fr; split; [ | apply or_iff_compat_l]; intros [Hl Hr]; split; intros.
223 { destruct Hl; [ right | destruct Fl | ]; assumption. }
224 { destruct Hr; [ right | destruct Fr | ]; assumption. }
225 Qed.
226
227 Theorem or_cancel_r : forall A B C : Prop,
228 (B -> ~ A) -> (C -> ~ A) -> ((B \/ A <-> C \/ A) <-> (B <-> C)).
229 Proof.
230 intros ? ? ? Fl Fr; split; [ | apply or_iff_compat_r]; intros [Hl Hr]; split; intros.
231 { destruct Hl; [ left | | destruct Fl ]; assumption. }
232 { destruct Hr; [ left | | destruct Fr ]; assumption. }
233 Qed.
234
235 Theorem or_comm : forall A B : Prop, (A \/ B) <-> (B \/ A).
236 Proof.
237 intros; split; (intros [? | ?]; [ right | left ]; assumption).
238 Qed.
239
240 Theorem or_assoc : forall A B C : Prop, (A \/ B) \/ C <-> A \/ B \/ C.
241 Proof.
242 intros; split; [ intros [[?|?]|?]| intros [?|[?|?]]].
243 + left; assumption.
244 + right; left; assumption.
245 + right; right; assumption.
246 + left; left; assumption.
247 + left; right; assumption.
248 + right; assumption.
249 Qed.
250 Lemma iff_and : forall A B : Prop, (A <-> B) -> (A -> B) /\ (B -> A).
251 Proof.
252 intros A B []; split; trivial.
253 Qed.
254
255 Lemma iff_to_and : forall A B : Prop, (A <-> B) <-> (A -> B) /\ (B -> A).
256 Proof.
257 intros; split; intros [Hl Hr]; (split; intros; [ apply Hl | apply Hr]); assumption.
258 Qed.
259
260 (** * First-order quantifiers *)
261
262 (** [ex P], or simply [exists x, P x], or also [exists x:A, P x],
263 expresses the existence of an [x] of some type [A] in [Set] which
264 satisfies the predicate [P]. This is existential quantification.
265
266 [ex2 P Q], or simply [exists2 x, P x & Q x], or also
267 [exists2 x:A, P x & Q x], expresses the existence of an [x] of
268 type [A] which satisfies both predicates [P] and [Q].
269
270 Universal quantification is primitively written [forall x:A, Q]. By
271 symmetry with existential quantification, the construction [all P]
272 is provided too.
273 *)
274
275 Inductive ex (A:Type) (P:A -> Prop) : Prop :=
276 ex_intro : forall x:A, P x -> ex (A:=A) P.
277
278 Register ex as core.ex.type.
279 Register ex_intro as core.ex.intro.
280
281 Section Projections.
282
283 Variables (A:Prop) (P:A->Prop).
284
285 Definition ex_proj1 (x:ex P) : A :=
286 match x with ex_intro _ a _ => a end.
287
288 Definition ex_proj2 (x:ex P) : P (ex_proj1 x) :=
289 match x with ex_intro _ _ b => b end.
290
291 Register ex_proj1 as core.ex.proj1.
292 Register ex_proj2 as core.ex.proj2.
293
294 End Projections.
295
296 Inductive ex2 (A:Type) (P Q:A -> Prop) : Prop :=
297 ex_intro2 : forall x:A, P x -> Q x -> ex2 (A:=A) P Q.
298
299 (** [ex2] of a predicate can be projected to an [ex].
300
301 This allows [ex_proj1] and [ex_proj2] to be usable with [ex2].
302
303 We have two choices here: either we can set up the definition so
304 that [ex_proj1] of a coerced [X : ex2 P Q] will unify with [let
305 (a, _, _) := X in a] by restricting the first argument of [ex2] to
306 be a [Prop], or we can define a more general [ex_of_ex2] which
307 does not satisfy this conversion rule. We choose the former,
308 under the assumption that there is no reason to turn an [ex2] into
309 an [ex] unless it is to project out the components. *)
310
311 Definition ex_of_ex2 (A : Prop) (P Q : A -> Prop) (X : ex2 P Q) : ex P
312 := ex_intro P
313 (let (a, _, _) := X in a)
314 (let (x, p, _) as s return (P (let (a, _, _) := s in a)) := X in p).
315
316 Section ex2_Projections.
317
318 Variables (A:Prop) (P Q:A->Prop).
319
320 Definition ex_proj3 (x:ex2 P Q) : Q (ex_proj1 (ex_of_ex2 x)) :=
321 match x with ex_intro2 _ _ _ _ b => b end.
322
323 End ex2_Projections.
324
325 Definition all (A:Type) (P:A -> Prop) := forall x:A, P x.
326 Register all as core.all.
327
328 (* Rule order is important to give printing priority to fully typed exists *)
329
330 Notation "'exists' x .. y , p" := (ex (fun x => .. (ex (fun y => p)) ..))
331 (at level 200, x binder, right associativity,
332 format "'[' 'exists' '/ ' x .. y , '/ ' p ']'")
333 : type_scope.
334
335 Notation "'exists2' x , p & q" := (ex2 (fun x => p) (fun x => q))
336 (at level 200, x name, p at level 200, right associativity) : type_scope.
337 Notation "'exists2' x : A , p & q" := (ex2 (A:=A) (fun x => p) (fun x => q))
338 (at level 200, x name, A at level 200, p at level 200, right associativity,
339 format "'[' 'exists2' '/ ' x : A , '/ ' '[' p & '/' q ']' ']'")
340 : type_scope.
341
342 Notation "'exists2' ' x , p & q" := (ex2 (fun x => p) (fun x => q))
343 (at level 200, x strict pattern, p at level 200, right associativity) : type_scope.
344 Notation "'exists2' ' x : A , p & q" := (ex2 (A:=A) (fun x => p) (fun x => q))
345 (at level 200, x strict pattern, A at level 200, p at level 200, right associativity,
346 format "'[' 'exists2' '/ ' ' x : A , '/ ' '[' p & '/' q ']' ']'")
347 : type_scope.
348
349 (** Derived rules for universal quantification *)
350
351 Section universal_quantification.
352
353 Variable A : Type.
354 Variable P : A -> Prop.
355
356 Theorem inst : forall x:A, all (fun x => P x) -> P x.
357 Proof.
358 unfold all; auto.
359 Qed.
360
361 Theorem gen : forall (B:Prop) (f:forall y:A, B -> P y), B -> all P.
362 Proof.
363 red; auto.
364 Qed.
365
366 End universal_quantification.
367
368 (** * Equality *)
369
370 (** [eq x y], or simply [x=y] expresses the equality of [x] and
371 [y]. Both [x] and [y] must belong to the same type [A].
372 The definition is inductive and states the reflexivity of the equality.
373 The others properties (symmetry, transitivity, replacement of
374 equals by equals) are proved below. The type of [x] and [y] can be
375 made explicit using the notation [x = y :> A]. This is Leibniz equality
376 as it expresses that [x] and [y] are equal iff every property on
377 [A] which is true of [x] is also true of [y] *)
378
379 Inductive eq (A:Type) (x:A) : A -> Prop :=
380 eq_refl : x = x :>A
381
382 where "x = y :> A" := (@eq A x y) : type_scope.
383
384 Arguments eq {A} x _.
385 Arguments eq_refl {A x} , [A] x.
386
387 Arguments eq_ind [A] x P _ y _ : rename.
388 Arguments eq_rec [A] x P _ y _ : rename.
389 Arguments eq_rect [A] x P _ y _ : rename.
390
391 Notation "x = y" := (eq x y) : type_scope.
392 Notation "x <> y :> T" := (~ x = y :>T) : type_scope.
393 Notation "x <> y" := (~ (x = y)) : type_scope.
394
395 #[global]
396 Hint Resolve I conj or_introl or_intror : core.
397 #[global]
398 Hint Resolve eq_refl: core.
399 #[global]
400 Hint Resolve ex_intro ex_intro2: core.
401
402 Register eq as core.eq.type.
403 Register eq_refl as core.eq.refl.
404 Register eq_ind as core.eq.ind.
405 Register eq_sind as core.eq.sind.
406 Register eq_rect as core.eq.rect.
407 Register eq_rec as core.eq.rec.
408
409 Scheme Rewriting for eq.
410
411 Register eq_rew_dep as core.eq.rect_dep.
412 Register eq_rew_dep as core.eq.ind_dep.
413 Register eq_rew_r_dep as core.eq.rect_r_dep.
414 Register eq_rew_r_dep as core.eq.ind_r_dep.
415
416 Arguments eq_sym_involutive [A]%_type_scope [x y] e : rename.
417
418 Section Logic_lemmas.
419
420 Theorem absurd : forall A C:Prop, A -> ~ A -> C.
421 Proof.
422 unfold not; intros A C h1 h2.
423 destruct (h2 h1).
424 Qed.
425
426 Section equality.
427 Variables A B : Type.
428 Variable f : A -> B.
429 Variables x y z : A.
430
431 Register eq_sym as core.eq.sym.
432
433 Theorem eq_trans : x = y -> y = z -> x = z.
434 Proof.
435 destruct 2; trivial.
436 Defined.
437
438 Register eq_trans as core.eq.trans.
439
440 Theorem eq_trans_r : x = y -> z = y -> x = z.
441 Proof.
442 destruct 2; trivial.
443 Defined.
444
445 Theorem f_equal : x = y -> f x = f y.
446 Proof.
447 destruct 1; trivial.
448 Defined.
449
450 Register f_equal as core.eq.congr.
451
452 Theorem not_eq_sym : x <> y -> y <> x.
453 Proof.
454 red; intros h1 h2; apply h1; destruct h2; trivial.
455 Qed.
456
457 End equality.
458
459 Definition eq_sind_r :
460 forall (A:Type) (x:A) (P:A -> SProp), P x -> forall y:A, y = x -> P y.
461 Proof.
462 intros A x P H y H0. elim eq_sym with (1 := H0); assumption.
463 Defined.
464
465 Definition eq_ind_r :
466 forall (A:Type) (x:A) (P:A -> Prop), P x -> forall y:A, y = x -> P y.
467 intros A x P H y H0. elim eq_sym with (1 := H0); assumption.
468 Defined.
469
470 Register eq_ind_r as core.eq.ind_r.
471 Register eq_sind_r as core.eq.sind_r.
472
473 Definition eq_rec_r :
474 forall (A:Type) (x:A) (P:A -> Set), P x -> forall y:A, y = x -> P y.
475 intros A x P H y H0; elim eq_sym with (1 := H0); assumption.
476 Defined.
477
478 Definition eq_rect_r :
479 forall (A:Type) (x:A) (P:A -> Type), P x -> forall y:A, y = x -> P y.
480 intros A x P H y H0; elim eq_sym with (1 := H0); assumption.
481 Defined.
482
483 Register eq_rect_r as core.eq.rect_r.
484 Register eq_rec_r as core.eq.rec_r.
485
486 End Logic_lemmas.
487
488 Module EqNotations.
489 Notation "'rew' H 'in' H'" := (eq_rect _ _ H' _ H)
490 (at level 10, H' at level 10,
491 format "'[' 'rew' H in '/' H' ']'").
492 Notation "'rew' [ P ] H 'in' H'" := (eq_rect _ P H' _ H)
493 (at level 10, H' at level 10,
494 format "'[' 'rew' [ P ] '/ ' H in '/' H' ']'").
495 Notation "'rew' <- H 'in' H'" := (eq_rect_r _ H' H)
496 (at level 10, H' at level 10,
497 format "'[' 'rew' <- H in '/' H' ']'").
498 Notation "'rew' <- [ P ] H 'in' H'" := (eq_rect_r P H' H)
499 (at level 10, H' at level 10,
500 format "'[' 'rew' <- [ P ] '/ ' H in '/' H' ']'").
501 Notation "'rew' -> H 'in' H'" := (eq_rect _ _ H' _ H)
502 (at level 10, H' at level 10, only parsing).
503 Notation "'rew' -> [ P ] H 'in' H'" := (eq_rect _ P H' _ H)
504 (at level 10, H' at level 10, only parsing).
505
506 Notation "'rew' 'dependent' H 'in' H'"
507 := (match H with
508 | eq_refl => H'
509 end)
510 (at level 10, H' at level 10,
511 format "'[' 'rew' 'dependent' '/ ' H in '/' H' ']'").
512 Notation "'rew' 'dependent' -> H 'in' H'"
513 := (match H with
514 | eq_refl => H'
515 end)
516 (at level 10, H' at level 10, only parsing).
517 Notation "'rew' 'dependent' <- H 'in' H'"
518 := (match eq_sym H with
519 | eq_refl => H'
520 end)
521 (at level 10, H' at level 10,
522 format "'[' 'rew' 'dependent' <- '/ ' H in '/' H' ']'").
523 Notation "'rew' 'dependent' [ 'fun' y p => P ] H 'in' H'"
524 := (match H as p in (_ = y) return P with
525 | eq_refl => H'
526 end)
527 (at level 10, H' at level 10, y name, p name,
528 format "'[' 'rew' 'dependent' [ 'fun' y p => P ] '/ ' H in '/' H' ']'").
529 Notation "'rew' 'dependent' -> [ 'fun' y p => P ] H 'in' H'"
530 := (match H as p in (_ = y) return P with
531 | eq_refl => H'
532 end)
533 (at level 10, H' at level 10, y name, p name, only parsing).
534 Notation "'rew' 'dependent' <- [ 'fun' y p => P ] H 'in' H'"
535 := (match eq_sym H as p in (_ = y) return P with
536 | eq_refl => H'
537 end)
538 (at level 10, H' at level 10, y name, p name,
539 format "'[' 'rew' 'dependent' <- [ 'fun' y p => P ] '/ ' H in '/' H' ']'").
540 Notation "'rew' 'dependent' [ P ] H 'in' H'"
541 := (match H as p in (_ = y) return P y p with
542 | eq_refl => H'
543 end)
544 (at level 10, H' at level 10,
545 format "'[' 'rew' 'dependent' [ P ] '/ ' H in '/' H' ']'").
546 Notation "'rew' 'dependent' -> [ P ] H 'in' H'"
547 := (match H as p in (_ = y) return P y p with
548 | eq_refl => H'
549 end)
550 (at level 10, H' at level 10,
551 only parsing).
552 Notation "'rew' 'dependent' <- [ P ] H 'in' H'"
553 := (match eq_sym H as p in (_ = y) return P y p with
554 | eq_refl => H'
555 end)
556 (at level 10, H' at level 10,
557 format "'[' 'rew' 'dependent' <- [ P ] '/ ' H in '/' H' ']'").
558 End EqNotations.
559
560 Import EqNotations.
561
562 Section equality_dep.
563 Variable A : Type.
564 Variable B : A -> Type.
565 Variable f : forall x, B x.
566 Variables x y : A.
567
568 Theorem f_equal_dep (H: x = y) : rew H in f x = f y.
569 Proof.
570 destruct H; reflexivity.
571 Defined.
572
573 End equality_dep.
574
575 Lemma f_equal_dep2 {A A' B B'} (f : A -> A') (g : forall a:A, B a -> B' (f a))
576 {x1 x2 : A} {y1 : B x1} {y2 : B x2} (H : x1 = x2) :
577 rew H in y1 = y2 -> rew f_equal f H in g x1 y1 = g x2 y2.
578 Proof.
579 destruct H, 1. reflexivity.
580 Defined.
581
582 Lemma rew_opp_r A (P:A->Type) (x y:A) (H:x=y) (a:P y) : rew H in rew <- H in a = a.
583 Proof.
584 destruct H.
585 reflexivity.
586 Defined.
587
588 Lemma rew_opp_l A (P:A->Type) (x y:A) (H:x=y) (a:P x) : rew <- H in rew H in a = a.
589 Proof.
590 destruct H.
591 reflexivity.
592 Defined.
593
594 Theorem f_equal2 :
595 forall (A1 A2 B:Type) (f:A1 -> A2 -> B) (x1 y1:A1)
596 (x2 y2:A2), x1 = y1 -> x2 = y2 -> f x1 x2 = f y1 y2.
597 Proof.
598 destruct 1; destruct 1; reflexivity.
599 Qed.
600
601 Register f_equal2 as core.eq.congr2.
602
603 Theorem f_equal3 :
604 forall (A1 A2 A3 B:Type) (f:A1 -> A2 -> A3 -> B) (x1 y1:A1)
605 (x2 y2:A2) (x3 y3:A3),
606 x1 = y1 -> x2 = y2 -> x3 = y3 -> f x1 x2 x3 = f y1 y2 y3.
607 Proof.
608 destruct 1; destruct 1; destruct 1; reflexivity.
609 Qed.
610
611 Theorem f_equal4 :
612 forall (A1 A2 A3 A4 B:Type) (f:A1 -> A2 -> A3 -> A4 -> B)
613 (x1 y1:A1) (x2 y2:A2) (x3 y3:A3) (x4 y4:A4),
614 x1 = y1 -> x2 = y2 -> x3 = y3 -> x4 = y4 -> f x1 x2 x3 x4 = f y1 y2 y3 y4.
615 Proof.
616 destruct 1; destruct 1; destruct 1; destruct 1; reflexivity.
617 Qed.
618
619 Theorem f_equal5 :
620 forall (A1 A2 A3 A4 A5 B:Type) (f:A1 -> A2 -> A3 -> A4 -> A5 -> B)
621 (x1 y1:A1) (x2 y2:A2) (x3 y3:A3) (x4 y4:A4) (x5 y5:A5),
622 x1 = y1 ->
623 x2 = y2 ->
624 x3 = y3 -> x4 = y4 -> x5 = y5 -> f x1 x2 x3 x4 x5 = f y1 y2 y3 y4 y5.
625 Proof.
626 destruct 1; destruct 1; destruct 1; destruct 1; destruct 1; reflexivity.
627 Qed.
628
629 Theorem f_equal_compose A B C (a b:A) (f:A->B) (g:B->C) (e:a=b) :
630 f_equal g (f_equal f e) = f_equal (fun a => g (f a)) e.
631 Proof.
632 destruct e. reflexivity.
633 Defined.
634
635 (** The groupoid structure of equality *)
636
637 Theorem eq_trans_refl_l A (x y:A) (e:x=y) : eq_trans eq_refl e = e.
638 Proof.
639 destruct e. reflexivity.
640 Defined.
641
642 Theorem eq_trans_refl_r A (x y:A) (e:x=y) : eq_trans e eq_refl = e.
643 Proof.
644 destruct e. reflexivity.
645 Defined.
646
647 Theorem eq_trans_sym_inv_l A (x y:A) (e:x=y) : eq_trans (eq_sym e) e = eq_refl.
648 Proof.
649 destruct e; reflexivity.
650 Defined.
651
652 Theorem eq_trans_sym_inv_r A (x y:A) (e:x=y) : eq_trans e (eq_sym e) = eq_refl.
653 Proof.
654 destruct e; reflexivity.
655 Defined.
656
657 Theorem eq_trans_assoc A (x y z t:A) (e:x=y) (e':y=z) (e'':z=t) :
658 eq_trans e (eq_trans e' e'') = eq_trans (eq_trans e e') e''.
659 Proof.
660 destruct e''; reflexivity.
661 Defined.
662
663 Theorem rew_map A B (P:B->Type) (f:A->B) x1 x2 (H:x1=x2) (y:P (f x1)) :
664 rew [fun x => P (f x)] H in y = rew f_equal f H in y.
665 Proof.
666 destruct H; reflexivity.
667 Defined.
668
669 Theorem eq_trans_map {A B} {x1 x2 x3:A} {y1:B x1} {y2:B x2} {y3:B x3}
670 (H1:x1=x2) (H2:x2=x3) (H1': rew H1 in y1 = y2) (H2': rew H2 in y2 = y3) :
671 rew eq_trans H1 H2 in y1 = y3.
672 Proof.
673 destruct H2. exact (eq_trans H1' H2').
674 Defined.
675
676 Lemma map_subst {A} {P Q:A->Type} (f : forall x, P x -> Q x) {x y} (H:x=y) (z:P x) :
677 rew H in f x z = f y (rew H in z).
678 Proof.
679 destruct H. reflexivity.
680 Defined.
681
682 Lemma map_subst_map {A B} {P:A->Type} {Q:B->Type} (f:A->B) (g : forall x, P x -> Q (f x))
683 {x y} (H:x=y) (z:P x) :
684 rew f_equal f H in g x z = g y (rew H in z).
685 Proof.
686 destruct H. reflexivity.
687 Defined.
688
689 Lemma rew_swap A (P:A->Type) x1 x2 (H:x1=x2) (y1:P x1) (y2:P x2) : rew H in y1 = y2 -> y1 = rew <- H in y2.
690 Proof.
691 destruct H. trivial.
692 Defined.
693
694 Lemma rew_compose A (P:A->Type) x1 x2 x3 (H1:x1=x2) (H2:x2=x3) (y:P x1) :
695 rew H2 in rew H1 in y = rew (eq_trans H1 H2) in y.
696 Proof.
697 destruct H2. reflexivity.
698 Defined.
699
700 (** Extra properties of equality *)
701
702 Theorem eq_id_comm_l A (f:A->A) (Hf:forall a, a = f a) a : f_equal f (Hf a) = Hf (f a).
703 Proof.
704 unfold f_equal.
705 rewrite <- (eq_trans_sym_inv_l (Hf a)).
706 destruct (Hf a) at 1 2.
707 destruct (Hf a).
708 reflexivity.
709 Defined.
710
711 Theorem eq_id_comm_r A (f:A->A) (Hf:forall a, f a = a) a : f_equal f (Hf a) = Hf (f a).
712 Proof.
713 unfold f_equal.
714 rewrite <- (eq_trans_sym_inv_l (Hf (f (f a)))).
715 set (Hfsymf := fun a => eq_sym (Hf a)).
716 change (eq_sym (Hf (f (f a)))) with (Hfsymf (f (f a))).
717 pattern (Hfsymf (f (f a))).
718 destruct (eq_id_comm_l f Hfsymf (f a)).
719 destruct (eq_id_comm_l f Hfsymf a).
720 unfold Hfsymf.
721 destruct (Hf a). simpl.
722 rewrite eq_trans_refl_l.
723 reflexivity.
724 Defined.
725
726 Lemma eq_refl_map_distr A B x (f:A->B) : f_equal f (eq_refl x) = eq_refl (f x).
727 Proof.
728 reflexivity.
729 Qed.
730
731 Lemma eq_trans_map_distr A B x y z (f:A->B) (e:x=y) (e':y=z) : f_equal f (eq_trans e e') = eq_trans (f_equal f e) (f_equal f e').
732 Proof.
733 destruct e'.
734 reflexivity.
735 Defined.
736
737 Lemma eq_sym_map_distr A B (x y:A) (f:A->B) (e:x=y) : eq_sym (f_equal f e) = f_equal f (eq_sym e).
738 Proof.
739 destruct e.
740 reflexivity.
741 Defined.
742
743 Lemma eq_trans_sym_distr A (x y z:A) (e:x=y) (e':y=z) : eq_sym (eq_trans e e') = eq_trans (eq_sym e') (eq_sym e).
744 Proof.
745 destruct e, e'.
746 reflexivity.
747 Defined.
748
749 Lemma eq_trans_rew_distr A (P:A -> Type) (x y z:A) (e:x=y) (e':y=z) (k:P x) :
750 rew (eq_trans e e') in k = rew e' in rew e in k.
751 Proof.
752 destruct e, e'; reflexivity.
753 Qed.
754
755 Lemma rew_const A P (x y:A) (e:x=y) (k:P) :
756 rew [fun _ => P] e in k = k.
757 Proof.
758 destruct e; reflexivity.
759 Qed.
760
761
762 (* Aliases *)
763
764 Abbreviation sym_eq := eq_sym (only parsing).
765 Abbreviation trans_eq := eq_trans (only parsing).
766 Abbreviation sym_not_eq := not_eq_sym (only parsing).
767
768 Abbreviation refl_equal := eq_refl (only parsing).
769 Abbreviation sym_equal := eq_sym (only parsing).
770 Abbreviation trans_equal := eq_trans (only parsing).
771 Abbreviation sym_not_equal := not_eq_sym (only parsing).
772
773 #[global]
774 Hint Immediate eq_sym not_eq_sym: core.
775
776 (** Basic definitions about relations and properties *)
777
778 Definition subrelation (A B : Type) (R R' : A->B->Prop) :=
779 forall x y, R x y -> R' x y.
780
781 Definition unique (A : Type) (P : A->Prop) (x:A) :=
782 P x /\ forall (x':A), P x' -> x=x'.
783
784 Definition uniqueness (A:Type) (P:A->Prop) := forall x y, P x -> P y -> x = y.
785
786 (** Unique existence *)
787
788 Notation "'exists' ! x .. y , p" :=
789 (ex (unique (fun x => .. (ex (unique (fun y => p))) ..)))
790 (at level 200, x binder, right associativity,
791 format "'[' 'exists' ! '/ ' x .. y , '/ ' p ']'")
792 : type_scope.
793
794 Lemma unique_existence : forall (A:Type) (P:A->Prop),
795 ((exists x, P x) /\ uniqueness P) <-> (exists! x, P x).
796 Proof.
797 intros A P; split.
798 - intros ((x,Hx),Huni); exists x; red; auto.
799 - intros (x,(Hx,Huni)); split.
800 + exists x; assumption.
801 + intros x' x'' Hx' Hx''; transitivity x.
802 * symmetry; auto.
803 * auto.
804 Qed.
805
806 Lemma forall_exists_unique_domain_coincide :
807 forall A (P:A->Prop), (exists! x, P x) ->
808 forall Q:A->Prop, (forall x, P x -> Q x) <-> (exists x, P x /\ Q x).
809 Proof.
810 intros A P (x & Hp & Huniq); split.
811 - intro; exists x; auto.
812 - intros (x0 & HPx0 & HQx0) x1 HPx1.
813 assert (H : x0 = x1) by (transitivity x; [symmetry|]; auto).
814 destruct H.
815 assumption.
816 Qed.
817
818 Lemma forall_exists_coincide_unique_domain :
819 forall A (P:A->Prop),
820 (forall Q:A->Prop, (forall x, P x -> Q x) <-> (exists x, P x /\ Q x))
821 -> (exists! x, P x).
822 Proof.
823 intros A P H.
824 destruct (H P) as ((x & Hx & _),_); [trivial|].
825 exists x. split; [trivial|].
826 destruct (H (fun x'=>x=x')) as (_,Huniq).
827 apply Huniq. exists x; auto.
828 Qed.
829
830 (** * Being inhabited *)
831
832 (** The predicate [inhabited] can be used in different contexts. If [A] is
833 thought as a type, [inhabited A] states that [A] is inhabited. If [A] is
834 thought as a computationally relevant proposition, then
835 [inhabited A] weakens [A] so as to hide its computational meaning.
836 The so-weakened proof remains computationally relevant but only in
837 a propositional context.
838 *)
839
840 Inductive inhabited (A:Type) : Prop := inhabits : A -> inhabited A.
841
842 #[global]
843 Hint Resolve inhabits: core.
844
845 Lemma exists_inhabited : forall (A:Type) (P:A->Prop),
846 (exists x, P x) -> inhabited A.
847 Proof.
848 destruct 1; auto.
849 Qed.
850
851 Lemma inhabited_covariant (A B : Type) : (A -> B) -> inhabited A -> inhabited B.
852 Proof.
853 intros f [x];exact (inhabits (f x)).
854 Qed.
855
856 (** Declaration of stepl and stepr for eq and iff *)
857
858 Lemma eq_stepl : forall (A : Type) (x y z : A), x = y -> x = z -> z = y.
859 Proof.
860 intros A x y z H1 H2. rewrite <- H2; exact H1.
861 Qed.
862
863 Declare Left Step eq_stepl.
864 Declare Right Step eq_trans.
865
866 Lemma iff_stepl : forall A B C : Prop, (A <-> B) -> (A <-> C) -> (C <-> B).
867 Proof.
868 intros ? ? ? [? ?] [? ?]; split; intros; auto.
869 Qed.
870
871 Declare Left Step iff_stepl.
872 Declare Right Step iff_trans.
873
874 (** More properties of [ex] and [ex2] that rely on equality being present *)
875
876 (** We define restricted versions of [ex_rect] and [ex_rec] which
877 allow elimination into non-Prop sorts when the inductive is not
878 informative *)
879
880 (** η Principles *)
881 Definition ex_eta {A : Prop} {P} (p : exists a : A, P a)
882 : p = ex_intro _ (ex_proj1 p) (ex_proj2 p).
883 Proof. destruct p; reflexivity. Defined.
884
885 Definition ex2_eta {A : Prop} {P Q} (p : exists2 a : A, P a & Q a)
886 : p = ex_intro2 _ _ (ex_proj1 (ex_of_ex2 p)) (ex_proj2 (ex_of_ex2 p)) (ex_proj3 p).
887 Proof. destruct p; reflexivity. Defined.
888
889 Section ex_Prop.
890 Variables (A:Prop) (P:A->Prop).
891
892 Definition ex_rect (P0 : ex P -> Type) (f : forall x p, P0 (ex_intro P x p))
893 : forall e, P0 e
894 := fun e => rew <- ex_eta e in f _ _.
895 Definition ex_rec : forall (P0 : ex P -> Set) (f : forall x p, P0 (ex_intro P x p)),
896 forall e, P0 e
897 := ex_rect.
898
899 End ex_Prop.
900
901 (** Equality for [ex] *)
902 Section ex.
903 Local Unset Implicit Arguments.
904 (** Projecting an equality of a pair to equality of the first components *)
905 Definition ex_proj1_eq {A : Prop} {P : A -> Prop} {u v : exists a : A, P a} (p : u = v)
906 : ex_proj1 u = ex_proj1 v
907 := f_equal (@ex_proj1 _ _) p.
908
909 (** Projecting an equality of a pair to equality of the second components *)
910 Definition ex_proj2_eq {A : Prop} {P : A -> Prop} {u v : exists a : A, P a} (p : u = v)
911 : rew ex_proj1_eq p in ex_proj2 u = ex_proj2 v
912 := rew dependent p in eq_refl.
913
914 (** Equality of [ex] is itself a [ex] (forwards-reasoning version) *)
915 Definition eq_ex_intro_uncurried {A : Type} {P : A -> Prop} {u1 v1 : A} {u2 : P u1} {v2 : P v1}
916 (pq : exists p : u1 = v1, rew p in u2 = v2)
917 : ex_intro _ u1 u2 = ex_intro _ v1 v2.
918 Proof.
919 destruct pq as [p q].
920 destruct q; simpl in *.
921 destruct p; reflexivity.
922 Defined.
923
924 (** Equality of [ex] is itself a [ex] (backwards-reasoning version) *)
925 Definition eq_ex_uncurried {A : Prop} {P : A -> Prop} (u v : exists a : A, P a)
926 (pq : exists p : ex_proj1 u = ex_proj1 v, rew p in ex_proj2 u = ex_proj2 v)
927 : u = v.
928 Proof.
929 destruct u as [u1 u2], v as [v1 v2]; simpl in *.
930 apply eq_ex_intro_uncurried; exact pq.
931 Defined.
932
933 (** Curried version of proving equality of [ex] types *)
934 Definition eq_ex_intro {A : Type} {P : A -> Prop} {u1 v1 : A} {u2 : P u1} {v2 : P v1}
935 (p : u1 = v1) (q : rew p in u2 = v2)
936 : ex_intro _ u1 u2 = ex_intro _ v1 v2
937 := eq_ex_intro_uncurried (ex_intro _ p q).
938
939 (** Curried version of proving equality of [ex] types *)
940 Definition eq_ex {A : Prop} {P : A -> Prop} (u v : exists a : A, P a)
941 (p : ex_proj1 u = ex_proj1 v) (q : rew p in ex_proj2 u = ex_proj2 v)
942 : u = v
943 := eq_ex_uncurried u v (ex_intro _ p q).
944
945 (** In order to have a performant [inversion_sigma], we define
946 specialized versions for when we have constructors on one or
947 both sides of the equality *)
948 Definition eq_ex_intro_l {A : Prop} {P : A -> Prop} u1 u2 (v : exists a : A, P a)
949 (p : u1 = ex_proj1 v) (q : rew p in u2 = ex_proj2 v)
950 : ex_intro P u1 u2 = v
951 := eq_ex (ex_intro P u1 u2) v p q.
952 Definition eq_ex_intro_r {A : Prop} {P : A -> Prop} (u : exists a : A, P a) v1 v2
953 (p : ex_proj1 u = v1) (q : rew p in ex_proj2 u = v2)
954 : u = ex_intro P v1 v2
955 := eq_ex u (ex_intro P v1 v2) p q.
956
957 (** Induction principle for [@eq (ex _)] *)
958 Definition eq_ex_eta {A : Prop} {P : A -> Prop} {u v : exists a : A, P a} (p : u = v) : p = eq_ex u v (ex_proj1_eq p) (ex_proj2_eq p).
959 Proof. destruct p, u; reflexivity. Defined.
960 Definition eq_ex_rect {A : Prop} {P : A -> Prop} {u v : exists a : A, P a} (Q : u = v -> Type)
961 (f : forall p q, Q (eq_ex u v p q))
962 : forall p, Q p
963 := fun p => rew <- eq_ex_eta p in f _ _.
964 Definition eq_ex_rec {A : Prop} {P : A -> Prop} {u v} (Q : u = v :> (exists a : A, P a) -> Set) := eq_ex_rect Q.
965 Definition eq_ex_ind {A : Prop} {P : A -> Prop} {u v} (Q : u = v :> (exists a : A, P a) -> Prop) := eq_ex_rec Q.
966
967 (** In order to have a performant [inversion_sigma], we define
968 specialized versions for when we have constructors on one or
969 both sides of the equality *)
970 Definition eq_ex_rect_ex_intro_l {A : Prop} {P : A -> Prop} {u1 u2 v} (Q : _ -> Type)
971 (f : forall p q, Q (eq_ex_intro_l (P:=P) u1 u2 v p q))
972 : forall p, Q p
973 := eq_ex_rect Q f.
974 Definition eq_ex_rect_ex_intro_r {A : Prop} {P : A -> Prop} {u v1 v2} (Q : _ -> Type)
975 (f : forall p q, Q (eq_ex_intro_r (P:=P) u v1 v2 p q))
976 : forall p, Q p
977 := eq_ex_rect Q f.
978 Definition eq_ex_rect_ex_intro {A : Prop} {P : A -> Prop} {u1 u2 v1 v2} (Q : _ -> Type)
979 (f : forall p q, Q (@eq_ex_intro A P u1 v1 u2 v2 p q))
980 : forall p, Q p
981 := eq_ex_rect Q f.
982
983 Definition eq_ex_rect_uncurried {A : Prop} {P : A -> Prop} {u v : exists a : A, P a} (Q : u = v -> Type)
984 (f : forall pq, Q (eq_ex u v (ex_proj1 pq) (ex_proj2 pq)))
985 : forall p, Q p
986 := eq_ex_rect Q (fun p q => f (ex_intro _ p q)).
987 Definition eq_ex_rec_uncurried {A : Prop} {P : A -> Prop} {u v} (Q : u = v :> (exists a : A, P a) -> Set) := eq_ex_rect_uncurried Q.
988 Definition eq_ex_ind_uncurried {A : Prop} {P : A -> Prop} {u v} (Q : u = v :> (exists a : A, P a) -> Prop) := eq_ex_rec_uncurried Q.
989
990 (** Equality of [ex] when the property is an hProp *)
991 Definition eq_ex_hprop {A : Prop} {P : A -> Prop} (P_hprop : forall (x : A) (p q : P x), p = q)
992 (u v : exists a : A, P a)
993 (p : ex_proj1 u = ex_proj1 v)
994 : u = v
995 := eq_ex u v p (P_hprop _ _ _).
996
997 Definition eq_ex_intro_hprop {A : Type} {P : A -> Prop} (P_hprop : forall (x : A) (p q : P x), p = q)
998 {u1 v1 : A} {u2 : P u1} {v2 : P v1}
999 (p : u1 = v1)
1000 : ex_intro P u1 u2 = ex_intro P v1 v2
1001 := eq_ex_intro p (P_hprop _ _ _).
1002
1003 (** Equivalence of equality of [ex] with a [ex] of equality *)
1004 (** We could actually prove an isomorphism here, and not just [<->],
1005 but for simplicity, we don't. *)
1006 Definition eq_ex_uncurried_iff {A : Prop} {P : A -> Prop} (u v : exists a : A, P a)
1007 : u = v <-> exists p : ex_proj1 u = ex_proj1 v, rew p in ex_proj2 u = ex_proj2 v.
1008 Proof.
1009 split; [ intro; subst; exists eq_refl; reflexivity | apply eq_ex_uncurried ].
1010 Defined.
1011
1012 (** Equivalence of equality of [ex] involving hProps with equality of the first components *)
1013 Definition eq_ex_hprop_iff {A : Prop} {P : A -> Prop} (P_hprop : forall (x : A) (p q : P x), p = q)
1014 (u v : exists a : A, P a)
1015 : u = v <-> (ex_proj1 u = ex_proj1 v)
1016 := conj (fun p => f_equal (@ex_proj1 _ _) p) (eq_ex_hprop P_hprop u v).
1017
1018 Lemma rew_ex {A' : Type} {x} {P : A' -> Prop} (Q : forall a, P a -> Prop) (u : exists p : P x, Q x p) {y} (H : x = y)
1019 : rew [fun a => exists p : P a, Q a p] H in u
1020 = ex_intro
1021 (Q y)
1022 (rew H in ex_proj1 u)
1023 (rew dependent H in ex_proj2 u).
1024 Proof.
1025 destruct H, u; reflexivity.
1026 Defined.
1027 End ex.
1028 Global Arguments eq_ex_intro A P _ _ _ _ !p !q / .
1029
1030 Section ex2_Prop.
1031 Variables (A:Prop) (P Q:A->Prop).
1032
1033 Definition ex2_rect (P0 : ex2 P Q -> Type) (f : forall x p q, P0 (ex_intro2 P Q x p q))
1034 : forall e, P0 e
1035 := fun e => rew <- ex2_eta e in f _ _ _.
1036 Definition ex2_rec : forall (P0 : ex2 P Q -> Set) (f : forall x p q, P0 (ex_intro2 P Q x p q)),
1037 forall e, P0 e
1038 := ex2_rect.
1039
1040 End ex2_Prop.
1041
1042 (** Equality for [ex2] *)
1043 Section ex2.
1044 (* We make [ex_of_ex2] a coercion so we can use [proj1], [proj2] on [ex2] *)
1045 Local Coercion ex_of_ex2 : ex2 >-> ex.
1046 Local Unset Implicit Arguments.
1047 (** Projecting an equality of a pair to equality of the first components *)
1048 Definition ex_of_ex2_eq {A : Prop} {P Q : A -> Prop} {u v : exists2 a : A, P a & Q a} (p : u = v)
1049 : u = v :> exists a : A, P a
1050 := f_equal _ p.
1051 Definition ex_proj1_of_ex2_eq {A : Prop} {P Q : A -> Prop} {u v : exists2 a : A, P a & Q a} (p : u = v)
1052 : ex_proj1 u = ex_proj1 v
1053 := ex_proj1_eq (ex_of_ex2_eq p).
1054
1055 (** Projecting an equality of a pair to equality of the second components *)
1056 Definition ex_proj2_of_ex2_eq {A : Prop} {P Q : A -> Prop} {u v : exists2 a : A, P a & Q a} (p : u = v)
1057 : rew ex_proj1_of_ex2_eq p in ex_proj2 u = ex_proj2 v
1058 := rew dependent p in eq_refl.
1059
1060 (** Projecting an equality of a pair to equality of the third components *)
1061 Definition ex_proj3_eq {A : Prop} {P Q : A -> Prop} {u v : exists2 a : A, P a & Q a} (p : u = v)
1062 : rew ex_proj1_of_ex2_eq p in ex_proj3 u = ex_proj3 v
1063 := rew dependent p in eq_refl.
1064
1065 (** Equality of [ex2] is itself a [ex2] (fowards-reasoning version) *)
1066 Definition eq_ex_intro2_uncurried {A : Type} {P Q : A -> Prop} {u1 v1 : A} {u2 : P u1} {v2 : P v1} {u3 : Q u1} {v3 : Q v1}
1067 (pqr : exists2 p : u1 = v1, rew p in u2 = v2 & rew p in u3 = v3)
1068 : ex_intro2 _ _ u1 u2 u3 = ex_intro2 _ _ v1 v2 v3.
1069 Proof.
1070 destruct pqr as [p q r].
1071 destruct r, q, p; simpl.
1072 reflexivity.
1073 Defined.
1074
1075 (** Equality of [ex2] is itself a [ex2] (backwards-reasoning version) *)
1076 Definition eq_ex2_uncurried {A : Prop} {P Q : A -> Prop} (u v : exists2 a : A, P a & Q a)
1077 (pqr : exists2 p : ex_proj1 u = ex_proj1 v,
1078 rew p in ex_proj2 u = ex_proj2 v & rew p in ex_proj3 u = ex_proj3 v)
1079 : u = v.
1080 Proof.
1081 destruct u as [u1 u2 u3], v as [v1 v2 v3]; simpl in *.
1082 apply eq_ex_intro2_uncurried; exact pqr.
1083 Defined.
1084
1085 (** Curried version of proving equality of [ex] types *)
1086 Definition eq_ex2 {A : Prop} {P Q : A -> Prop} (u v : exists2 a : A, P a & Q a)
1087 (p : ex_proj1 u = ex_proj1 v)
1088 (q : rew p in ex_proj2 u = ex_proj2 v)
1089 (r : rew p in ex_proj3 u = ex_proj3 v)
1090 : u = v
1091 := eq_ex2_uncurried u v (ex_intro2 _ _ p q r).
1092
1093 Definition eq_ex_intro2 {A : Type} {P Q : A -> Prop} {u1 v1 : A} {u2 : P u1} {v2 : P v1} {u3 : Q u1} {v3 : Q v1}
1094 (p : u1 = v1)
1095 (q : rew p in u2 = v2)
1096 (r : rew p in u3 = v3)
1097 : ex_intro2 P Q u1 u2 u3 = ex_intro2 P Q v1 v2 v3
1098 := eq_ex_intro2_uncurried (ex_intro2 _ _ p q r).
1099
1100 (** In order to have a performant [inversion_sigma], we define
1101 specialized versions for when we have constructors on one or
1102 both sides of the equality *)
1103 Definition eq_ex_intro2_l {A : Prop} {P Q : A -> Prop} u1 u2 u3 (v : exists2 a : A, P a & Q a)
1104 (p : u1 = ex_proj1 v) (q : rew p in u2 = ex_proj2 v) (r : rew p in u3 = ex_proj3 v)
1105 : ex_intro2 P Q u1 u2 u3 = v
1106 := eq_ex2 (ex_intro2 P Q u1 u2 u3) v p q r.
1107 Definition eq_ex_intro2_r {A : Prop} {P Q : A -> Prop} (u : exists2 a : A, P a & Q a) v1 v2 v3
1108 (p : ex_proj1 u = v1) (q : rew p in ex_proj2 u = v2) (r : rew p in ex_proj3 u = v3)
1109 : u = ex_intro2 P Q v1 v2 v3
1110 := eq_ex2 u (ex_intro2 P Q v1 v2 v3) p q r.
1111
1112 (** Equality of [ex2] when the second property is an hProp *)
1113 Definition eq_ex2_hprop {A : Prop} {P Q : A -> Prop} (Q_hprop : forall (x : A) (p q : Q x), p = q)
1114 (u v : exists2 a : A, P a & Q a)
1115 (p : u = v :> exists a : A, P a)
1116 : u = v
1117 := eq_ex2 u v (ex_proj1_eq p) (ex_proj2_eq p) (Q_hprop _ _ _).
1118
1119 Definition eq_ex_intro2_hprop_nondep {A : Type} {P : A -> Prop} {Q : Prop} (Q_hprop : forall (p q : Q), p = q)
1120 {u1 v1 : A} {u2 : P u1} {v2 : P v1} {u3 v3 : Q}
1121 (p : ex_intro _ u1 u2 = ex_intro _ v1 v2)
1122 : ex_intro2 _ _ u1 u2 u3 = ex_intro2 _ _ v1 v2 v3
1123 := rew [fun v3 => _ = ex_intro2 _ _ _ _ v3] (Q_hprop u3 v3) in
1124 f_equal (fun u => match u with ex_intro _ u1 u2 => ex_intro2 _ _ u1 u2 u3 end) p.
1125
1126 Definition eq_ex_intro2_hprop {A : Type} {P Q : A -> Prop}
1127 (P_hprop : forall x (p q : P x), p = q)
1128 (Q_hprop : forall x (p q : Q x), p = q)
1129 {u1 v1 : A} {u2 : P u1} {v2 : P v1} {u3 : Q u1} {v3 : Q v1}
1130 (p : u1 = v1)
1131 : ex_intro2 P Q u1 u2 u3 = ex_intro2 P Q v1 v2 v3
1132 := eq_ex_intro2 p (P_hprop _ _ _) (Q_hprop _ _ _).
1133
1134 (** Equivalence of equality of [ex2] with a [ex2] of equality *)
1135 (** We could actually prove an isomorphism here, and not just [<->],
1136 but for simplicity, we don't. *)
1137 Definition eq_ex2_uncurried_iff {A : Prop} {P Q : A -> Prop} (u v : exists2 a : A, P a & Q a)
1138 : u = v
1139 <-> exists2 p : ex_proj1 u = ex_proj1 v,
1140 rew p in ex_proj2 u = ex_proj2 v & rew p in ex_proj3 u = ex_proj3 v.
1141 Proof.
1142 split; [ intro; subst; exists eq_refl; reflexivity | apply eq_ex2_uncurried ].
1143 Defined.
1144
1145 (** Induction principle for [@eq (ex2 _ _)] *)
1146 Definition eq_ex2_eta {A : Prop} {P Q : A -> Prop} {u v : exists2 a : A, P a & Q a} (p : u = v)
1147 : p = eq_ex2 u v (ex_proj1_of_ex2_eq p) (ex_proj2_of_ex2_eq p) (ex_proj3_eq p).
1148 Proof. destruct p, u; reflexivity. Defined.
1149 Definition eq_ex2_rect {A : Prop} {P Q : A -> Prop} {u v : exists2 a : A, P a & Q a} (R : u = v -> Type)
1150 (f : forall p q r, R (eq_ex2 u v p q r))
1151 : forall p, R p
1152 := fun p => rew <- eq_ex2_eta p in f _ _ _.
1153 Definition eq_ex2_rec {A : Prop} {P Q : A -> Prop} {u v} (R : u = v :> (exists2 a : A, P a & Q a) -> Set) := eq_ex2_rect R.
1154 Definition eq_ex2_ind {A : Prop} {P Q : A -> Prop} {u v} (R : u = v :> (exists2 a : A, P a & Q a) -> Prop) := eq_ex2_rec R.
1155
1156 (** In order to have a performant [inversion_sigma], we define
1157 specialized versions for when we have constructors on one or
1158 both sides of the equality *)
1159 Definition eq_ex2_rect_ex_intro2_l {A : Prop} {P Q : A -> Prop} {u1 u2 u3 v} (R : _ -> Type)
1160 (f : forall p q r, R (eq_ex_intro2_l (P:=P) (Q:=Q) u1 u2 u3 v p q r))
1161 : forall p, R p
1162 := eq_ex2_rect R f.
1163 Definition eq_ex2_rect_ex_intro2_r {A : Prop} {P Q : A -> Prop} {u v1 v2 v3} (R : _ -> Type)
1164 (f : forall p q r, R (eq_ex_intro2_r (P:=P) (Q:=Q) u v1 v2 v3 p q r))
1165 : forall p, R p
1166 := eq_ex2_rect R f.
1167 Definition eq_ex2_rect_ex_intro2 {A : Prop} {P Q : A -> Prop} {u1 u2 u3 v1 v2 v3} (R : _ -> Type)
1168 (f : forall p q r, R (@eq_ex_intro2 A P Q u1 v1 u2 v2 u3 v3 p q r))
1169 : forall p, R p
1170 := eq_ex2_rect R f.
1171
1172 Definition eq_ex2_rect_uncurried {A : Prop} {P Q : A -> Prop} {u v : exists2 a : A, P a & Q a} (R : u = v -> Type)
1173 (f : forall pqr : exists2 p : _ = _, _ & _, R (eq_ex2 u v (ex_proj1 pqr) (ex_proj2 pqr) (ex_proj3 pqr)))
1174 : forall p, R p
1175 := eq_ex2_rect R (fun p q r => f (ex_intro2 _ _ p q r)).
1176 Definition eq_ex2_rec_uncurried {A : Prop} {P Q : A -> Prop} {u v} (R : u = v :> (exists2 a : A, P a & Q a) -> Set) := eq_ex2_rect_uncurried R.
1177 Definition eq_ex2_ind_uncurried {A : Prop} {P Q : A -> Prop} {u v} (R : u = v :> (exists2 a : A, P a & Q a) -> Prop) := eq_ex2_rec_uncurried R.
1178
1179 (** Equivalence of equality of [ex2] involving hProps with equality of the first components *)
1180 Definition eq_ex2_hprop_iff {A : Prop} {P Q : A -> Prop} (Q_hprop : forall (x : A) (p q : Q x), p = q)
1181 (u v : exists2 a : A, P a & Q a)
1182 : u = v <-> (u = v :> exists a : A, P a)
1183 := conj (fun p => f_equal (@ex_of_ex2 _ _ _) p) (eq_ex2_hprop Q_hprop u v).
1184
1185 (** Non-dependent classification of equality of [ex] *)
1186 Definition eq_ex2_nondep {A : Prop} {B C : Prop} (u v : @ex2 A (fun _ => B) (fun _ => C))
1187 (p : ex_proj1 u = ex_proj1 v) (q : ex_proj2 u = ex_proj2 v) (r : ex_proj3 u = ex_proj3 v)
1188 : u = v
1189 := @eq_ex2 _ _ _ u v p (eq_trans (rew_const _ _) q) (eq_trans (rew_const _ _) r).
1190
1191 (** Classification of transporting across an equality of [ex2]s *)
1192 Lemma rew_ex2 {A' : Type} {x} {P : A' -> Prop} (Q R : forall a, P a -> Prop)
1193 (u : exists2 p : P x, Q x p & R x p)
1194 {y} (H : x = y)
1195 : rew [fun a => exists2 p : P a, Q a p & R a p] H in u
1196 = ex_intro2
1197 (Q y)
1198 (R y)
1199 (rew H in ex_proj1 u)
1200 (rew dependent H in ex_proj2 u)
1201 (rew dependent H in ex_proj3 u).
1202 Proof.
1203 destruct H, u; reflexivity.
1204 Defined.
1205 End ex2.
1206 Global Arguments eq_ex_intro2 A P Q _ _ _ _ _ _ !p !q !r / .