| 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 |
(** Basic specifications : sets that may contain logical information *) |
| 12 |
|
| 13 |
Set Implicit Arguments. |
| 14 |
Set Reversible Pattern Implicit. |
| 15 |
|
| 16 |
Require Import Notations. |
| 17 |
Require Import Ltac. |
| 18 |
Require Import Datatypes. |
| 19 |
Require Import Logic. |
| 20 |
|
| 21 |
(** Subsets and Sigma-types *) |
| 22 |
|
| 23 |
(** [(sig A P)], or more suggestively [{x:A | P x}], denotes the subset |
| 24 |
of elements of the type [A] which satisfy the predicate [P]. |
| 25 |
Similarly [(sig2 A P Q)], or [{x:A | P x & Q x}], denotes the subset |
| 26 |
of elements of the type [A] which satisfy both [P] and [Q]. *) |
| 27 |
|
| 28 |
#[universes(template)] |
| 29 |
Inductive sig (A:Type) (P:A -> Prop) : Type := |
| 30 |
exist : forall x:A, P x -> sig P. |
| 31 |
|
| 32 |
Register sig as core.sig.type. |
| 33 |
Register exist as core.sig.intro. |
| 34 |
Register sig_rect as core.sig.rect. |
| 35 |
|
| 36 |
#[universes(template)] |
| 37 |
Inductive sig2 (A:Type) (P Q:A -> Prop) : Type := |
| 38 |
exist2 : forall x:A, P x -> Q x -> sig2 P Q. |
| 39 |
|
| 40 |
(** [(sigT A P)], or more suggestively [{x:A & (P x)}] is a Sigma-type. |
| 41 |
Similarly for [(sigT2 A P Q)], also written [{x:A & (P x) & (Q x)}]. *) |
| 42 |
|
| 43 |
#[universes(template)] |
| 44 |
Inductive sigT (A:Type) (P:A -> Type) : Type := |
| 45 |
existT : forall x:A, P x -> sigT P. |
| 46 |
|
| 47 |
Register sigT as core.sigT.type. |
| 48 |
Register existT as core.sigT.intro. |
| 49 |
Register sigT_rect as core.sigT.rect. |
| 50 |
|
| 51 |
#[universes(template)] |
| 52 |
Inductive sigT2 (A:Type) (P Q:A -> Type) : Type := |
| 53 |
existT2 : forall x:A, P x -> Q x -> sigT2 P Q. |
| 54 |
|
| 55 |
(* Notations *) |
| 56 |
|
| 57 |
Arguments sig (A P)%_type. |
| 58 |
Arguments sig2 (A P Q)%_type. |
| 59 |
Arguments sigT (A P)%_type. |
| 60 |
Arguments sigT2 (A P Q)%_type. |
| 61 |
|
| 62 |
Notation "{ x | P }" := (sig (fun x => P%_type)) : type_scope. |
| 63 |
Notation "{ x | P & Q }" := (sig2 (fun x => P%_type) (fun x => Q%_type)) : type_scope. |
| 64 |
Notation "{ x : A | P }" := (sig (A:=A) (fun x => P%_type)) : type_scope. |
| 65 |
Notation "{ x : A | P & Q }" := (sig2 (A:=A) (fun x => P%_type) (fun x => Q%_type)) : |
| 66 |
type_scope. |
| 67 |
Notation "{ x & P }" := (sigT (fun x => P%_type)) : type_scope. |
| 68 |
Notation "{ x & P & Q }" := (sigT2 (fun x => P%_type) (fun x => Q%_type)) : type_scope. |
| 69 |
Notation "{ x : A & P }" := (sigT (A:=A) (fun x => P%_type)) : type_scope. |
| 70 |
Notation "{ x : A & P & Q }" := (sigT2 (A:=A) (fun x => P%_type) (fun x => Q)) : |
| 71 |
type_scope. |
| 72 |
|
| 73 |
Notation "{ ' pat | P }" := (sig (fun pat => P%_type)) : type_scope. |
| 74 |
Notation "{ ' pat | P & Q }" := (sig2 (fun pat => P%_type) (fun pat => Q%_type)) : type_scope. |
| 75 |
Notation "{ ' pat : A | P }" := (sig (A:=A) (fun pat => P%_type)) : type_scope. |
| 76 |
Notation "{ ' pat : A | P & Q }" := (sig2 (A:=A) (fun pat => P%_type) (fun pat => Q%_type)) : |
| 77 |
type_scope. |
| 78 |
Notation "{ ' pat & P }" := (sigT (fun pat => P%_type)) : type_scope. |
| 79 |
Notation "{ ' pat & P & Q }" := (sigT2 (fun pat => P%_type) (fun pat => Q%_type)) : type_scope. |
| 80 |
Notation "{ ' pat : A & P }" := (sigT (A:=A) (fun pat => P%_type)) : type_scope. |
| 81 |
Notation "{ ' pat : A & P & Q }" := (sigT2 (A:=A) (fun pat => P%_type) (fun pat => Q%_type)) : |
| 82 |
type_scope. |
| 83 |
|
| 84 |
Add Printing Let sig. |
| 85 |
Add Printing Let sig2. |
| 86 |
Add Printing Let sigT. |
| 87 |
Add Printing Let sigT2. |
| 88 |
|
| 89 |
|
| 90 |
(** Projections of [sig] |
| 91 |
|
| 92 |
An element [y] of a subset [{x:A | (P x)}] is the pair of an [a] |
| 93 |
of type [A] and of a proof [h] that [a] satisfies [P]. Then |
| 94 |
[(proj1_sig y)] is the witness [a] and [(proj2_sig y)] is the |
| 95 |
proof of [(P a)] *) |
| 96 |
|
| 97 |
(* Set Universe Polymorphism. *) |
| 98 |
Section Subset_projections. |
| 99 |
|
| 100 |
Variable A : Type. |
| 101 |
Variable P : A -> Prop. |
| 102 |
|
| 103 |
Definition proj1_sig (e:sig P) := match e with |
| 104 |
| exist _ a b => a |
| 105 |
end. |
| 106 |
|
| 107 |
Definition proj2_sig (e:sig P) := |
| 108 |
match e return P (proj1_sig e) with |
| 109 |
| exist _ a b => b |
| 110 |
end. |
| 111 |
|
| 112 |
Register proj1_sig as core.sig.proj1. |
| 113 |
Register proj2_sig as core.sig.proj2. |
| 114 |
|
| 115 |
End Subset_projections. |
| 116 |
|
| 117 |
|
| 118 |
(** [sig2] of a predicate can be projected to a [sig]. |
| 119 |
|
| 120 |
This allows [proj1_sig] and [proj2_sig] to be usable with [sig2]. |
| 121 |
|
| 122 |
The [let] statements occur in the body of the [exist] so that |
| 123 |
[proj1_sig] of a coerced [X : sig2 P Q] will unify with [let (a, |
| 124 |
_, _) := X in a] *) |
| 125 |
|
| 126 |
Definition sig_of_sig2 (A : Type) (P Q : A -> Prop) (X : sig2 P Q) : sig P |
| 127 |
:= exist P |
| 128 |
(let (a, _, _) := X in a) |
| 129 |
(let (x, p, _) as s return (P (let (a, _, _) := s in a)) := X in p). |
| 130 |
|
| 131 |
(** Projections of [sig2] |
| 132 |
|
| 133 |
An element [y] of a subset [{x:A | (P x) & (Q x)}] is the triple |
| 134 |
of an [a] of type [A], a of a proof [h] that [a] satisfies [P], |
| 135 |
and a proof [h'] that [a] satisfies [Q]. Then |
| 136 |
[(proj1_sig (sig_of_sig2 y))] is the witness [a], |
| 137 |
[(proj2_sig (sig_of_sig2 y))] is the proof of [(P a)], and |
| 138 |
[(proj3_sig y)] is the proof of [(Q a)]. *) |
| 139 |
|
| 140 |
Section Subset_projections2. |
| 141 |
|
| 142 |
Variable A : Type. |
| 143 |
Variables P Q : A -> Prop. |
| 144 |
|
| 145 |
Definition proj3_sig (e : sig2 P Q) := |
| 146 |
let (a, b, c) return Q (proj1_sig (sig_of_sig2 e)) := e in c. |
| 147 |
|
| 148 |
End Subset_projections2. |
| 149 |
|
| 150 |
|
| 151 |
(** Projections of [sigT] |
| 152 |
|
| 153 |
An element [x] of a sigma-type [{y:A & P y}] is a dependent pair |
| 154 |
made of an [a] of type [A] and an [h] of type [P a]. Then, |
| 155 |
[(projT1 x)] is the first projection and [(projT2 x)] is the |
| 156 |
second projection, the type of which depends on the [projT1]. *) |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
Section Projections. |
| 161 |
|
| 162 |
Variable A : Type. |
| 163 |
Variable P : A -> Type. |
| 164 |
|
| 165 |
Definition projT1 (x:sigT P) : A := match x with |
| 166 |
| existT _ a _ => a |
| 167 |
end. |
| 168 |
|
| 169 |
Definition projT2 (x:sigT P) : P (projT1 x) := |
| 170 |
match x return P (projT1 x) with |
| 171 |
| existT _ _ h => h |
| 172 |
end. |
| 173 |
|
| 174 |
Register projT1 as core.sigT.proj1. |
| 175 |
Register projT2 as core.sigT.proj2. |
| 176 |
|
| 177 |
End Projections. |
| 178 |
|
| 179 |
Module SigTNotations. |
| 180 |
Notation "( x ; y )" := (existT _ x y) (at level 0, format "( x ; '/ ' y )"). |
| 181 |
Notation "x .1" := (projT1 x) (at level 1, left associativity, format "x .1"). |
| 182 |
Notation "x .2" := (projT2 x) (at level 1, left associativity, format "x .2"). |
| 183 |
End SigTNotations. |
| 184 |
|
| 185 |
Import SigTNotations. |
| 186 |
|
| 187 |
(** [sigT2] of a predicate can be projected to a [sigT]. |
| 188 |
|
| 189 |
This allows [projT1] and [projT2] to be usable with [sigT2]. |
| 190 |
|
| 191 |
The [let] statements occur in the body of the [existT] so that |
| 192 |
[projT1] of a coerced [X : sigT2 P Q] will unify with [let (a, |
| 193 |
_, _) := X in a] *) |
| 194 |
|
| 195 |
Definition sigT_of_sigT2 (A : Type) (P Q : A -> Type) (X : sigT2 P Q) : sigT P |
| 196 |
:= existT P |
| 197 |
(let (a, _, _) := X in a) |
| 198 |
(let (x, p, _) as s return (P (let (a, _, _) := s in a)) := X in p). |
| 199 |
|
| 200 |
(** Projections of [sigT2] |
| 201 |
|
| 202 |
An element [x] of a sigma-type [{y:A & P y & Q y}] is a dependent |
| 203 |
pair made of an [a] of type [A], an [h] of type [P a], and an [h'] |
| 204 |
of type [Q a]. Then, [(projT1 (sigT_of_sigT2 x))] is the first |
| 205 |
projection, [(projT2 (sigT_of_sigT2 x))] is the second projection, |
| 206 |
and [(projT3 x)] is the third projection, the types of which |
| 207 |
depends on the [projT1]. *) |
| 208 |
|
| 209 |
Section Projections2. |
| 210 |
|
| 211 |
Variable A : Type. |
| 212 |
Variables P Q : A -> Type. |
| 213 |
|
| 214 |
Definition projT3 (e : sigT2 P Q) := |
| 215 |
let (a, b, c) return Q (projT1 (sigT_of_sigT2 e)) := e in c. |
| 216 |
|
| 217 |
End Projections2. |
| 218 |
|
| 219 |
Local Notation "x .3" := (projT3 x) (at level 1, left associativity, format "x .3"). |
| 220 |
|
| 221 |
(** [sigT] of a predicate is equivalent to [sig] *) |
| 222 |
|
| 223 |
Definition sig_of_sigT (A : Type) (P : A -> Prop) (X : sigT P) : sig P |
| 224 |
:= exist P (projT1 X) (projT2 X). |
| 225 |
|
| 226 |
Definition sigT_of_sig (A : Type) (P : A -> Prop) (X : sig P) : sigT P |
| 227 |
:= existT P (proj1_sig X) (proj2_sig X). |
| 228 |
|
| 229 |
(** [sigT2] of a predicate is equivalent to [sig2] *) |
| 230 |
|
| 231 |
Definition sig2_of_sigT2 (A : Type) (P Q : A -> Prop) (X : sigT2 P Q) : sig2 P Q |
| 232 |
:= exist2 P Q (projT1 (sigT_of_sigT2 X)) (projT2 (sigT_of_sigT2 X)) (projT3 X). |
| 233 |
|
| 234 |
Definition sigT2_of_sig2 (A : Type) (P Q : A -> Prop) (X : sig2 P Q) : sigT2 P Q |
| 235 |
:= existT2 P Q (proj1_sig (sig_of_sig2 X)) (proj2_sig (sig_of_sig2 X)) (proj3_sig X). |
| 236 |
|
| 237 |
(** [sig] of a predicate on [Prop]s can be turned into [ex] *) |
| 238 |
|
| 239 |
Definition ex_of_sig (A : Type) (P : A -> Prop) (X : sig P) : ex P |
| 240 |
:= ex_intro P (proj1_sig X) (proj2_sig X). |
| 241 |
|
| 242 |
(** [sigT] of a predicate on [Prop]s can be turned into [ex] *) |
| 243 |
|
| 244 |
Definition ex_of_sigT (A : Type) (P : A -> Prop) (X : sigT P) : ex P |
| 245 |
:= ex_of_sig (sig_of_sigT X). |
| 246 |
|
| 247 |
(** [sig2] of a predicate on [Prop]s can be turned into [ex2] *) |
| 248 |
|
| 249 |
Definition ex2_of_sig2 (A : Type) (P Q : A -> Prop) (X : sig2 P Q) : ex2 P Q |
| 250 |
:= ex_intro2 P Q (proj1_sig (sig_of_sig2 X)) (proj2_sig (sig_of_sig2 X)) (proj3_sig X). |
| 251 |
|
| 252 |
(** [sigT2] of a predicate on [Prop]s can be turned into [ex2] *) |
| 253 |
|
| 254 |
Definition ex2_of_sigT2 (A : Type) (P Q : A -> Prop) (X : sigT2 P Q) : ex2 P Q |
| 255 |
:= ex2_of_sig2 (sig2_of_sigT2 X). |
| 256 |
|
| 257 |
(** η Principles *) |
| 258 |
Definition sigT_eta {A P} (p : { a : A & P a }) |
| 259 |
: p = existT _ (projT1 p) (projT2 p). |
| 260 |
Proof. destruct p; reflexivity. Defined. |
| 261 |
|
| 262 |
Definition sig_eta {A P} (p : { a : A | P a }) |
| 263 |
: p = exist _ (proj1_sig p) (proj2_sig p). |
| 264 |
Proof. destruct p; reflexivity. Defined. |
| 265 |
|
| 266 |
Definition sigT2_eta {A P Q} (p : { a : A & P a & Q a }) |
| 267 |
: p = existT2 _ _ (projT1 (sigT_of_sigT2 p)) (projT2 (sigT_of_sigT2 p)) (projT3 p). |
| 268 |
Proof. destruct p; reflexivity. Defined. |
| 269 |
|
| 270 |
Definition sig2_eta {A P Q} (p : { a : A | P a & Q a }) |
| 271 |
: p = exist2 _ _ (proj1_sig (sig_of_sig2 p)) (proj2_sig (sig_of_sig2 p)) (proj3_sig p). |
| 272 |
Proof. destruct p; reflexivity. Defined. |
| 273 |
|
| 274 |
(** [exists x : A, B] is equivalent to [inhabited {x : A | B}] *) |
| 275 |
Lemma exists_to_inhabited_sig {A P} : (exists x : A, P x) -> inhabited {x : A | P x}. |
| 276 |
Proof. |
| 277 |
intros [x y]. exact (inhabits (exist _ x y)). |
| 278 |
Qed. |
| 279 |
|
| 280 |
Lemma inhabited_sig_to_exists {A P} : inhabited {x : A | P x} -> exists x : A, P x. |
| 281 |
Proof. |
| 282 |
intros [[x y]];exists x;exact y. |
| 283 |
Qed. |
| 284 |
|
| 285 |
(** Subtyping for prod *) |
| 286 |
|
| 287 |
Section ProdSigT. |
| 288 |
|
| 289 |
Variable A B : Type. |
| 290 |
|
| 291 |
Definition sigT_of_prod (p : A * B) := (fst p; snd p). |
| 292 |
Definition prod_of_sigT (s : { _ : A & B }) := (s.1, s.2). |
| 293 |
|
| 294 |
Lemma sigT_prod_sigT p : sigT_of_prod (prod_of_sigT p) = p. |
| 295 |
Proof. destruct p; reflexivity. Qed. |
| 296 |
|
| 297 |
Lemma prod_sigT_prod s : prod_of_sigT (sigT_of_prod s) = s. |
| 298 |
Proof. destruct s; reflexivity. Qed. |
| 299 |
|
| 300 |
End ProdSigT. |
| 301 |
|
| 302 |
(** Equality of sigma types *) |
| 303 |
|
| 304 |
Import EqNotations. |
| 305 |
|
| 306 |
(** Equality for [sigT] *) |
| 307 |
Section sigT. |
| 308 |
Local Unset Implicit Arguments. |
| 309 |
(** Projecting an equality of a pair to equality of the first components *) |
| 310 |
Definition projT1_eq {A} {P : A -> Type} {u v : { a : A & P a }} (p : u = v) |
| 311 |
: u.1 = v.1 |
| 312 |
:= f_equal (fun x => x.1) p. |
| 313 |
|
| 314 |
(** Projecting an equality of a pair to equality of the second components *) |
| 315 |
Definition projT2_eq {A} {P : A -> Type} {u v : { a : A & P a }} (p : u = v) |
| 316 |
: rew projT1_eq p in u.2 = v.2 |
| 317 |
:= rew dependent p in eq_refl. |
| 318 |
|
| 319 |
(** Equality of [sigT] is itself a [sigT] (forwards-reasoning version) *) |
| 320 |
Definition eq_existT_uncurried {A : Type} {P : A -> Type} {u1 v1 : A} {u2 : P u1} {v2 : P v1} |
| 321 |
(pq : { p : u1 = v1 & rew p in u2 = v2 }) |
| 322 |
: (u1; u2) = (v1; v2). |
| 323 |
Proof. |
| 324 |
destruct pq as [p q]. |
| 325 |
destruct q; simpl in *. |
| 326 |
destruct p; reflexivity. |
| 327 |
Defined. |
| 328 |
|
| 329 |
(** Equality of [sigT] is itself a [sigT] (backwards-reasoning version) *) |
| 330 |
Definition eq_sigT_uncurried {A : Type} {P : A -> Type} (u v : { a : A & P a }) |
| 331 |
(pq : { p : u.1 = v.1 & rew p in u.2 = v.2 }) |
| 332 |
: u = v. |
| 333 |
Proof. |
| 334 |
destruct u as [u1 u2], v as [v1 v2]; simpl in *. |
| 335 |
apply eq_existT_uncurried; exact pq. |
| 336 |
Defined. |
| 337 |
|
| 338 |
Lemma eq_existT_curried {A : Type} {P : A -> Type} {u1 v1 : A} {u2 : P u1} {v2 : P v1} |
| 339 |
(p : u1 = v1) (q : rew p in u2 = v2) : (u1; u2) = (v1; v2). |
| 340 |
Proof. |
| 341 |
apply eq_sigT_uncurried; exists p; exact q. |
| 342 |
Defined. |
| 343 |
|
| 344 |
Local Notation "(= u ; v )" := (eq_existT_curried u v) (at level 0, format "(= u ; '/ ' v )"). |
| 345 |
|
| 346 |
Lemma eq_existT_curried_map {A A' P P'} (f:A -> A') (g:forall u:A, P u -> P' (f u)) |
| 347 |
{u1 v1 : A} {u2 : P u1} {v2 : P v1} (p : u1 = v1) (q : rew p in u2 = v2) : |
| 348 |
f_equal (fun x => (f x.1; g x.1 x.2)) (= p; q) = |
| 349 |
(= f_equal f p; f_equal_dep2 f g p q). |
| 350 |
Proof. |
| 351 |
destruct p, q. reflexivity. |
| 352 |
Defined. |
| 353 |
|
| 354 |
Lemma eq_existT_curried_trans {A P} {u1 v1 w1 : A} {u2 : P u1} {v2 : P v1} {w2 : P w1} |
| 355 |
(p : u1 = v1) (q : rew p in u2 = v2) |
| 356 |
(p' : v1 = w1) (q': rew p' in v2 = w2) : |
| 357 |
eq_trans (= p; q) (= p'; q') = |
| 358 |
(= eq_trans p p'; eq_trans_map p p' q q'). |
| 359 |
Proof. |
| 360 |
destruct p', q'. reflexivity. |
| 361 |
Defined. |
| 362 |
|
| 363 |
Theorem eq_existT_curried_congr {A P} {u1 v1 : A} {u2 : P u1} {v2 : P v1} |
| 364 |
{p p' : u1 = v1} {q : rew p in u2 = v2} {q': rew p' in u2 = v2} |
| 365 |
(r : p = p') : rew [fun H => rew H in u2 = v2] r in q = q' -> (= p; q) = (= p'; q'). |
| 366 |
Proof. |
| 367 |
destruct r, 1. reflexivity. |
| 368 |
Qed. |
| 369 |
|
| 370 |
(** Curried version of proving equality of sigma types *) |
| 371 |
Definition eq_sigT {A : Type} {P : A -> Type} (u v : { a : A & P a }) |
| 372 |
(p : u.1 = v.1) (q : rew p in u.2 = v.2) |
| 373 |
: u = v |
| 374 |
:= eq_sigT_uncurried u v (existT _ p q). |
| 375 |
|
| 376 |
(** In order to have a performant [inversion_sigma], we define |
| 377 |
specialized versions for when we have constructors on one or |
| 378 |
both sides of the equality *) |
| 379 |
Definition eq_existT_l {A : Type} {P : A -> Type} {u1 : A} {u2 : P u1} {v : { a : A & P a }} |
| 380 |
(p : u1 = v.1) (q : rew p in u2 = v.2) : (u1; u2) = v |
| 381 |
:= eq_sigT (u1; u2) v p q. |
| 382 |
Definition eq_existT_r {A : Type} {P : A -> Type} {u : { a : A & P a }} {v1 : A} {v2 : P v1} |
| 383 |
(p : u.1 = v1) (q : rew p in u.2 = v2) : u = (v1; v2) |
| 384 |
:= eq_sigT u (v1; v2) p q. |
| 385 |
|
| 386 |
(** Equality of [sigT] when the property is an hProp *) |
| 387 |
Definition eq_sigT_hprop {A P} (P_hprop : forall (x : A) (p q : P x), p = q) |
| 388 |
(u v : { a : A & P a }) |
| 389 |
(p : u.1 = v.1) |
| 390 |
: u = v |
| 391 |
:= eq_sigT u v p (P_hprop _ _ _). |
| 392 |
|
| 393 |
(** Equivalence of equality of [sigT] with a [sigT] of equality *) |
| 394 |
(** We could actually prove an isomorphism here, and not just [<->], |
| 395 |
but for simplicity, we don't. *) |
| 396 |
Definition eq_sigT_uncurried_iff {A P} |
| 397 |
(u v : { a : A & P a }) |
| 398 |
: u = v <-> { p : u.1 = v.1 & rew p in u.2 = v.2 }. |
| 399 |
Proof. |
| 400 |
split; [ intro; subst; exists eq_refl; reflexivity | apply eq_sigT_uncurried ]. |
| 401 |
Defined. |
| 402 |
|
| 403 |
(** Induction principle for [@eq (sigT _)] *) |
| 404 |
Definition eq_sigT_rect {A P} {u v : { a : A & P a }} (Q : u = v -> Type) |
| 405 |
(f : forall p q, Q (eq_sigT u v p q)) |
| 406 |
: forall p, Q p. |
| 407 |
Proof. intro p; specialize (f (projT1_eq p) (projT2_eq p)); destruct u, p; exact f. Defined. |
| 408 |
Definition eq_sigT_rec {A P u v} (Q : u = v :> { a : A & P a } -> Set) := eq_sigT_rect Q. |
| 409 |
Definition eq_sigT_ind {A P u v} (Q : u = v :> { a : A & P a } -> Prop) := eq_sigT_rec Q. |
| 410 |
|
| 411 |
(** In order to have a performant [inversion_sigma], we define |
| 412 |
specialized versions for when we have constructors on one or |
| 413 |
both sides of the equality *) |
| 414 |
Definition eq_sigT_rect_existT_l {A P} {u1 u2 v} (Q : _ -> Type) |
| 415 |
(f : forall p q, Q (@eq_existT_l A P u1 u2 v p q)) |
| 416 |
: forall p, Q p |
| 417 |
:= eq_sigT_rect Q f. |
| 418 |
Definition eq_sigT_rect_existT_r {A P} {u v1 v2} (Q : _ -> Type) |
| 419 |
(f : forall p q, Q (@eq_existT_r A P u v1 v2 p q)) |
| 420 |
: forall p, Q p |
| 421 |
:= eq_sigT_rect Q f. |
| 422 |
Definition eq_sigT_rect_existT {A P} {u1 u2 v1 v2} (Q : _ -> Type) |
| 423 |
(f : forall p q, Q (@eq_existT_curried A P u1 v1 u2 v2 p q)) |
| 424 |
: forall p, Q p |
| 425 |
:= eq_sigT_rect Q f. |
| 426 |
|
| 427 |
(** We want uncurried versions so [inversion_sigma] can accept |
| 428 |
intropatterns, but we use [ex] types for the induction |
| 429 |
hypothesis to avoid extraction errors about informative |
| 430 |
inductive types having Prop instances *) |
| 431 |
Definition eq_sigT_rect_uncurried {A P} {u v : { a : A & P a }} (Q : u = v -> Type) |
| 432 |
(f : forall pq : exists p : u.1 = v.1, _, Q (eq_sigT u v (ex_proj1 pq) (ex_proj2 pq))) |
| 433 |
: forall p, Q p |
| 434 |
:= eq_sigT_rect Q (fun p q => f (ex_intro _ p q)). |
| 435 |
Definition eq_sigT_rec_uncurried {A P u v} (Q : u = v :> { a : A & P a } -> Set) := eq_sigT_rect_uncurried Q. |
| 436 |
Definition eq_sigT_ind_uncurried {A P u v} (Q : u = v :> { a : A & P a } -> Prop) := eq_sigT_rec_uncurried Q. |
| 437 |
|
| 438 |
(** Equivalence of equality of [sigT] involving hProps with equality of the first components *) |
| 439 |
Definition eq_sigT_hprop_iff {A P} (P_hprop : forall (x : A) (p q : P x), p = q) |
| 440 |
(u v : { a : A & P a }) |
| 441 |
: u = v <-> (u.1 = v.1) |
| 442 |
:= conj (fun p => f_equal (@projT1 _ _) p) (eq_sigT_hprop P_hprop u v). |
| 443 |
|
| 444 |
(** Non-dependent classification of equality of [sigT] *) |
| 445 |
Definition eq_sigT_nondep {A B : Type} (u v : { a : A & B }) |
| 446 |
(p : u.1 = v.1) (q : u.2 = v.2) |
| 447 |
: u = v |
| 448 |
:= @eq_sigT _ _ u v p (eq_trans (rew_const _ _) q). |
| 449 |
|
| 450 |
(** Classification of transporting across an equality of [sigT]s *) |
| 451 |
Lemma rew_sigT {A x} {P : A -> Type} (Q : forall a, P a -> Prop) (u : { p : P x & Q x p }) {y} (H : x = y) |
| 452 |
: rew [fun a => { p : P a & Q a p }] H in u |
| 453 |
= existT |
| 454 |
(Q y) |
| 455 |
(rew H in u.1) |
| 456 |
(rew dependent H in (u.2)). |
| 457 |
Proof. |
| 458 |
destruct H, u; reflexivity. |
| 459 |
Defined. |
| 460 |
End sigT. |
| 461 |
Global Arguments eq_existT_curried A P _ _ _ _ !p !q / . |
| 462 |
|
| 463 |
(** Equality for [sig] *) |
| 464 |
Section sig. |
| 465 |
(** We define this as a [Let] rather than a [Definition] to avoid |
| 466 |
extraction errors about informative inductive types having Prop |
| 467 |
instances *) |
| 468 |
Local Unset Implicit Arguments. |
| 469 |
(** Projecting an equality of a pair to equality of the first components *) |
| 470 |
Definition proj1_sig_eq {A} {P : A -> Prop} {u v : { a : A | P a }} (p : u = v) |
| 471 |
: proj1_sig u = proj1_sig v |
| 472 |
:= f_equal (@proj1_sig _ _) p. |
| 473 |
|
| 474 |
(** Projecting an equality of a pair to equality of the second components *) |
| 475 |
Definition proj2_sig_eq {A} {P : A -> Prop} {u v : { a : A | P a }} (p : u = v) |
| 476 |
: rew proj1_sig_eq p in proj2_sig u = proj2_sig v |
| 477 |
:= rew dependent p in eq_refl. |
| 478 |
|
| 479 |
(** Equality of [sig] is itself a [sig] (forwards-reasoning version) *) |
| 480 |
Definition eq_exist_uncurried {A : Type} {P : A -> Prop} {u1 v1 : A} {u2 : P u1} {v2 : P v1} |
| 481 |
(pq : { p : u1 = v1 | rew p in u2 = v2 }) |
| 482 |
: exist _ u1 u2 = exist _ v1 v2. |
| 483 |
Proof. |
| 484 |
destruct pq as [p q]. |
| 485 |
destruct q; simpl in *. |
| 486 |
destruct p; reflexivity. |
| 487 |
Defined. |
| 488 |
|
| 489 |
(** Equality of [sig] is itself a [sig] (backwards-reasoning version) *) |
| 490 |
Definition eq_sig_uncurried {A : Type} {P : A -> Prop} (u v : { a : A | P a }) |
| 491 |
(pq : { p : proj1_sig u = proj1_sig v | rew p in proj2_sig u = proj2_sig v }) |
| 492 |
: u = v. |
| 493 |
Proof. |
| 494 |
destruct u as [u1 u2], v as [v1 v2]; simpl in *. |
| 495 |
apply eq_exist_uncurried; exact pq. |
| 496 |
Defined. |
| 497 |
|
| 498 |
Lemma eq_exist_curried {A : Type} {P : A -> Prop} {u1 v1 : A} {u2 : P u1} {v2 : P v1} |
| 499 |
(p : u1 = v1) (q : rew p in u2 = v2) : exist P u1 u2 = exist P v1 v2. |
| 500 |
Proof. |
| 501 |
apply eq_sig_uncurried; exists p; exact q. |
| 502 |
Defined. |
| 503 |
|
| 504 |
(** Curried version of proving equality of sigma types *) |
| 505 |
Definition eq_sig {A : Type} {P : A -> Prop} (u v : { a : A | P a }) |
| 506 |
(p : proj1_sig u = proj1_sig v) (q : rew p in proj2_sig u = proj2_sig v) |
| 507 |
: u = v |
| 508 |
:= eq_sig_uncurried u v (exist _ p q). |
| 509 |
|
| 510 |
(** In order to have a performant [inversion_sigma], we define |
| 511 |
specialized versions for when we have constructors on one or |
| 512 |
both sides of the equality *) |
| 513 |
Definition eq_exist_l {A : Type} {P : A -> Prop} {u1 : A} {u2 : P u1} {v : { a : A | P a }} |
| 514 |
(p : u1 = proj1_sig v) (q : rew p in u2 = proj2_sig v) : exist _ u1 u2 = v |
| 515 |
:= eq_sig (exist _ u1 u2) v p q. |
| 516 |
Definition eq_exist_r {A : Type} {P : A -> Prop} {u : { a : A | P a }} {v1 : A} {v2 : P v1} |
| 517 |
(p : proj1_sig u = v1) (q : rew p in proj2_sig u = v2) : u = exist _ v1 v2 |
| 518 |
:= eq_sig u (exist _ v1 v2) p q. |
| 519 |
|
| 520 |
(** Induction principle for [@eq (sig _)] *) |
| 521 |
Definition eq_sig_rect {A P} {u v : { a : A | P a }} (Q : u = v -> Type) |
| 522 |
(f : forall p q, Q (eq_sig u v p q)) |
| 523 |
: forall p, Q p. |
| 524 |
Proof. intro p; specialize (f (proj1_sig_eq p) (proj2_sig_eq p)); destruct u, p; exact f. Defined. |
| 525 |
Definition eq_sig_rec {A P u v} (Q : u = v :> { a : A | P a } -> Set) := eq_sig_rect Q. |
| 526 |
Definition eq_sig_ind {A P u v} (Q : u = v :> { a : A | P a } -> Prop) := eq_sig_rec Q. |
| 527 |
|
| 528 |
(** In order to have a performant [inversion_sigma], we define |
| 529 |
specialized versions for when we have constructors on one or |
| 530 |
both sides of the equality *) |
| 531 |
Definition eq_sig_rect_exist_l {A P} {u1 u2 v} (Q : _ -> Type) |
| 532 |
(f : forall p q, Q (@eq_exist_l A P u1 u2 v p q)) |
| 533 |
: forall p, Q p |
| 534 |
:= eq_sig_rect Q f. |
| 535 |
Definition eq_sig_rect_exist_r {A P} {u v1 v2} (Q : _ -> Type) |
| 536 |
(f : forall p q, Q (@eq_exist_r A P u v1 v2 p q)) |
| 537 |
: forall p, Q p |
| 538 |
:= eq_sig_rect Q f. |
| 539 |
Definition eq_sig_rect_exist {A P} {u1 u2 v1 v2} (Q : _ -> Type) |
| 540 |
(f : forall p q, Q (@eq_exist_curried A P u1 v1 u2 v2 p q)) |
| 541 |
: forall p, Q p |
| 542 |
:= eq_sig_rect Q f. |
| 543 |
|
| 544 |
(** We want uncurried versions so [inversion_sigma] can accept |
| 545 |
intropatterns, but we use [ex] types for the induction |
| 546 |
hypothesis to avoid extraction errors about informative |
| 547 |
inductive types having Prop instances *) |
| 548 |
Definition eq_sig_rect_uncurried {A P} {u v : { a : A | P a }} (Q : u = v -> Type) |
| 549 |
(f : forall pq : exists p : proj1_sig u = proj1_sig v, _, Q (eq_sig u v (ex_proj1 pq) (ex_proj2 pq))) |
| 550 |
: forall p, Q p |
| 551 |
:= eq_sig_rect Q (fun p q => f (ex_intro _ p q)). |
| 552 |
Definition eq_sig_rec_uncurried {A P u v} (Q : u = v :> { a : A | P a } -> Set) := eq_sig_rect_uncurried Q. |
| 553 |
Definition eq_sig_ind_uncurried {A P u v} (Q : u = v :> { a : A | P a } -> Prop) := eq_sig_rec_uncurried Q. |
| 554 |
|
| 555 |
(** Equality of [sig] when the property is an hProp *) |
| 556 |
Definition eq_sig_hprop {A} {P : A -> Prop} (P_hprop : forall (x : A) (p q : P x), p = q) |
| 557 |
(u v : { a : A | P a }) |
| 558 |
(p : proj1_sig u = proj1_sig v) |
| 559 |
: u = v |
| 560 |
:= eq_sig u v p (P_hprop _ _ _). |
| 561 |
|
| 562 |
(** Equivalence of equality of [sig] with a [sig] of equality *) |
| 563 |
(** We could actually prove an isomorphism here, and not just [<->], |
| 564 |
but for simplicity, we don't. *) |
| 565 |
Definition eq_sig_uncurried_iff {A} {P : A -> Prop} |
| 566 |
(u v : { a : A | P a }) |
| 567 |
: u = v <-> { p : proj1_sig u = proj1_sig v | rew p in proj2_sig u = proj2_sig v }. |
| 568 |
Proof. |
| 569 |
split; [ intro; subst; exists eq_refl; reflexivity | apply eq_sig_uncurried ]. |
| 570 |
Defined. |
| 571 |
|
| 572 |
(** Equivalence of equality of [sig] involving hProps with equality of the first components *) |
| 573 |
Definition eq_sig_hprop_iff {A} {P : A -> Prop} (P_hprop : forall (x : A) (p q : P x), p = q) |
| 574 |
(u v : { a : A | P a }) |
| 575 |
: u = v <-> (proj1_sig u = proj1_sig v) |
| 576 |
:= conj (fun p => f_equal (@proj1_sig _ _) p) (eq_sig_hprop P_hprop u v). |
| 577 |
|
| 578 |
Lemma rew_sig {A x} {P : A -> Type} (Q : forall a, P a -> Prop) (u : { p : P x | Q x p }) {y} (H : x = y) |
| 579 |
: rew [fun a => { p : P a | Q a p }] H in u |
| 580 |
= exist |
| 581 |
(Q y) |
| 582 |
(rew H in proj1_sig u) |
| 583 |
(rew dependent H in proj2_sig u). |
| 584 |
Proof. |
| 585 |
destruct H, u; reflexivity. |
| 586 |
Defined. |
| 587 |
End sig. |
| 588 |
Global Arguments eq_exist_curried A P _ _ _ _ !p !q / . |
| 589 |
|
| 590 |
(** Equality for [sigT2] *) |
| 591 |
Section sigT2. |
| 592 |
(* We make [sigT_of_sigT2] a coercion so we can use [projT1], [projT2] on [sigT2] *) |
| 593 |
Local Coercion sigT_of_sigT2 : sigT2 >-> sigT. |
| 594 |
Local Coercion ex_of_ex2 : ex2 >-> ex. |
| 595 |
Local Unset Implicit Arguments. |
| 596 |
(** Projecting an equality of a pair to equality of the first components *) |
| 597 |
Definition sigT_of_sigT2_eq {A} {P Q : A -> Type} {u v : { a : A & P a & Q a }} (p : u = v) |
| 598 |
: u = v :> { a : A & P a } |
| 599 |
:= f_equal _ p. |
| 600 |
Definition projT1_of_sigT2_eq {A} {P Q : A -> Type} {u v : { a : A & P a & Q a }} (p : u = v) |
| 601 |
: u.1 = v.1 |
| 602 |
:= projT1_eq (sigT_of_sigT2_eq p). |
| 603 |
|
| 604 |
(** Projecting an equality of a pair to equality of the second components *) |
| 605 |
Definition projT2_of_sigT2_eq {A} {P Q : A -> Type} {u v : { a : A & P a & Q a }} (p : u = v) |
| 606 |
: rew projT1_of_sigT2_eq p in u.2 = v.2 |
| 607 |
:= rew dependent p in eq_refl. |
| 608 |
|
| 609 |
(** Projecting an equality of a pair to equality of the third components *) |
| 610 |
Definition projT3_eq {A} {P Q : A -> Type} {u v : { a : A & P a & Q a }} (p : u = v) |
| 611 |
: rew projT1_of_sigT2_eq p in u.3 = v.3 |
| 612 |
:= rew dependent p in eq_refl. |
| 613 |
|
| 614 |
(** Equality of [sigT2] is itself a [sigT2] (forwards-reasoning version) *) |
| 615 |
Definition eq_existT2_uncurried {A : Type} {P Q : A -> Type} |
| 616 |
{u1 v1 : A} {u2 : P u1} {v2 : P v1} {u3 : Q u1} {v3 : Q v1} |
| 617 |
(pqr : { p : u1 = v1 |
| 618 |
& rew p in u2 = v2 & rew p in u3 = v3 }) |
| 619 |
: existT2 _ _ u1 u2 u3 = existT2 _ _ v1 v2 v3. |
| 620 |
Proof. |
| 621 |
destruct pqr as [p q r]. |
| 622 |
destruct r, q, p; simpl. |
| 623 |
reflexivity. |
| 624 |
Defined. |
| 625 |
|
| 626 |
(** Equality of [sigT2] is itself a [sigT2] (backwards-reasoning version) *) |
| 627 |
Definition eq_sigT2_uncurried {A : Type} {P Q : A -> Type} (u v : { a : A & P a & Q a }) |
| 628 |
(pqr : { p : u.1 = v.1 |
| 629 |
& rew p in u.2 = v.2 & rew p in u.3 = v.3 }) |
| 630 |
: u = v. |
| 631 |
Proof. |
| 632 |
destruct u as [u1 u2 u3], v as [v1 v2 v3]; simpl in *. |
| 633 |
apply eq_existT2_uncurried; exact pqr. |
| 634 |
Defined. |
| 635 |
|
| 636 |
Lemma eq_existT2_curried {A : Type} {P Q : A -> Type} {u1 v1 : A} {u2 : P u1} {v2 : P v1} {u3 : Q u1} {v3 : Q v1} |
| 637 |
(p : u1 = v1) (q : rew p in u2 = v2) (r : rew p in u3 = v3) : existT2 P Q u1 u2 u3 = existT2 P Q v1 v2 v3. |
| 638 |
Proof. |
| 639 |
apply eq_sigT2_uncurried; exists p; exact q + exact r. |
| 640 |
Defined. |
| 641 |
|
| 642 |
(** Curried version of proving equality of sigma types *) |
| 643 |
Definition eq_sigT2 {A : Type} {P Q : A -> Type} (u v : { a : A & P a & Q a }) |
| 644 |
(p : u.1 = v.1) |
| 645 |
(q : rew p in u.2 = v.2) |
| 646 |
(r : rew p in u.3 = v.3) |
| 647 |
: u = v |
| 648 |
:= eq_sigT2_uncurried u v (existT2 _ _ p q r). |
| 649 |
|
| 650 |
(** In order to have a performant [inversion_sigma], we define |
| 651 |
specialized versions for when we have constructors on one or |
| 652 |
both sides of the equality *) |
| 653 |
Definition eq_existT2_l {A : Type} {P Q : A -> Type} {u1 : A} {u2 : P u1} {u3 : Q u1} {v : { a : A & P a & Q a }} |
| 654 |
(p : u1 = v.1) (q : rew p in u2 = v.2) (r : rew p in u3 = v.3) : existT2 P Q u1 u2 u3 = v |
| 655 |
:= eq_sigT2 (existT2 P Q u1 u2 u3) v p q r. |
| 656 |
Definition eq_existT2_r {A : Type} {P Q : A -> Type} {u : { a : A & P a & Q a }} {v1 : A} {v2 : P v1} {v3 : Q v1} |
| 657 |
(p : u.1 = v1) (q : rew p in u.2 = v2) (r : rew p in u.3 = v3) : u = existT2 P Q v1 v2 v3 |
| 658 |
:= eq_sigT2 u (existT2 P Q v1 v2 v3) p q r. |
| 659 |
|
| 660 |
(** Equality of [sigT2] when the second property is an hProp *) |
| 661 |
Definition eq_sigT2_hprop {A P Q} (Q_hprop : forall (x : A) (p q : Q x), p = q) |
| 662 |
(u v : { a : A & P a & Q a }) |
| 663 |
(p : u = v :> { a : A & P a }) |
| 664 |
: u = v |
| 665 |
:= eq_sigT2 u v (projT1_eq p) (projT2_eq p) (Q_hprop _ _ _). |
| 666 |
|
| 667 |
(** Equivalence of equality of [sigT2] with a [sigT2] of equality *) |
| 668 |
(** We could actually prove an isomorphism here, and not just [<->], |
| 669 |
but for simplicity, we don't. *) |
| 670 |
Definition eq_sigT2_uncurried_iff {A P Q} |
| 671 |
(u v : { a : A & P a & Q a }) |
| 672 |
: u = v |
| 673 |
<-> { p : u.1 = v.1 |
| 674 |
& rew p in u.2 = v.2 & rew p in u.3 = v.3 }. |
| 675 |
Proof. |
| 676 |
split; [ intro; subst; exists eq_refl; reflexivity | apply eq_sigT2_uncurried ]. |
| 677 |
Defined. |
| 678 |
|
| 679 |
(** Induction principle for [@eq (sigT2 _ _)] *) |
| 680 |
Definition eq_sigT2_rect {A P Q} {u v : { a : A & P a & Q a }} (R : u = v -> Type) |
| 681 |
(f : forall p q r, R (eq_sigT2 u v p q r)) |
| 682 |
: forall p, R p. |
| 683 |
Proof. |
| 684 |
intro p. |
| 685 |
specialize (f (projT1_of_sigT2_eq p) (projT2_of_sigT2_eq p) (projT3_eq p)). |
| 686 |
destruct u, p; exact f. |
| 687 |
Defined. |
| 688 |
Definition eq_sigT2_rec {A P Q u v} (R : u = v :> { a : A & P a & Q a } -> Set) := eq_sigT2_rect R. |
| 689 |
Definition eq_sigT2_ind {A P Q u v} (R : u = v :> { a : A & P a & Q a } -> Prop) := eq_sigT2_rec R. |
| 690 |
|
| 691 |
(** In order to have a performant [inversion_sigma], we define |
| 692 |
specialized versions for when we have constructors on one or |
| 693 |
both sides of the equality *) |
| 694 |
Definition eq_sigT2_rect_existT2_l {A P Q} {u1 u2 u3 v} (R : _ -> Type) |
| 695 |
(f : forall p q r, R (@eq_existT2_l A P Q u1 u2 u3 v p q r)) |
| 696 |
: forall p, R p |
| 697 |
:= eq_sigT2_rect R f. |
| 698 |
Definition eq_sigT2_rect_existT2_r {A P Q} {u v1 v2 v3} (R : _ -> Type) |
| 699 |
(f : forall p q r, R (@eq_existT2_r A P Q u v1 v2 v3 p q r)) |
| 700 |
: forall p, R p |
| 701 |
:= eq_sigT2_rect R f. |
| 702 |
Definition eq_sigT2_rect_existT2 {A P Q} {u1 u2 u3 v1 v2 v3} (R : _ -> Type) |
| 703 |
(f : forall p q r, R (@eq_existT2_curried A P Q u1 v1 u2 v2 u3 v3 p q r)) |
| 704 |
: forall p, R p |
| 705 |
:= eq_sigT2_rect R f. |
| 706 |
|
| 707 |
(** We want uncurried versions so [inversion_sigma] can accept |
| 708 |
intropatterns, but we use [ex2] types for the induction |
| 709 |
hypothesis to avoid extraction errors about informative |
| 710 |
inductive types having Prop instances *) |
| 711 |
Definition eq_sigT2_rect_uncurried {A P Q} {u v : { a : A & P a & Q a }} (R : u = v -> Type) |
| 712 |
(f : forall pqr : exists2 p : u.1 = v.1, _ & _, R (eq_sigT2 u v (ex_proj1 pqr) (ex_proj2 pqr) (ex_proj3 pqr))) |
| 713 |
: forall p, R p |
| 714 |
:= eq_sigT2_rect R (fun p q r => f (ex_intro2 _ _ p q r)). |
| 715 |
Definition eq_sigT2_rec_uncurried {A P Q u v} (R : u = v :> { a : A & P a & Q a } -> Set) := eq_sigT2_rect_uncurried R. |
| 716 |
Definition eq_sigT2_ind_uncurried {A P Q u v} (R : u = v :> { a : A & P a & Q a } -> Prop) := eq_sigT2_rec_uncurried R. |
| 717 |
|
| 718 |
(** Equivalence of equality of [sigT2] involving hProps with equality of the first components *) |
| 719 |
Definition eq_sigT2_hprop_iff {A P Q} (Q_hprop : forall (x : A) (p q : Q x), p = q) |
| 720 |
(u v : { a : A & P a & Q a }) |
| 721 |
: u = v <-> (u = v :> { a : A & P a }) |
| 722 |
:= conj (fun p => f_equal (@sigT_of_sigT2 _ _ _) p) (eq_sigT2_hprop Q_hprop u v). |
| 723 |
|
| 724 |
(** Non-dependent classification of equality of [sigT] *) |
| 725 |
Definition eq_sigT2_nondep {A B C : Type} (u v : { a : A & B & C }) |
| 726 |
(p : u.1 = v.1) (q : u.2 = v.2) (r : u.3 = v.3) |
| 727 |
: u = v |
| 728 |
:= @eq_sigT2 _ _ _ u v p (eq_trans (rew_const _ _) q) (eq_trans (rew_const _ _) r). |
| 729 |
|
| 730 |
(** Classification of transporting across an equality of [sigT2]s *) |
| 731 |
Lemma rew_sigT2 {A x} {P : A -> Type} (Q R : forall a, P a -> Prop) |
| 732 |
(u : { p : P x & Q x p & R x p }) |
| 733 |
{y} (H : x = y) |
| 734 |
: rew [fun a => { p : P a & Q a p & R a p }] H in u |
| 735 |
= existT2 |
| 736 |
(Q y) |
| 737 |
(R y) |
| 738 |
(rew H in u.1) |
| 739 |
(rew dependent H in u.2) |
| 740 |
(rew dependent H in u.3). |
| 741 |
Proof. |
| 742 |
destruct H, u; reflexivity. |
| 743 |
Defined. |
| 744 |
End sigT2. |
| 745 |
Global Arguments eq_existT2_curried A P Q _ _ _ _ _ _ !p !q !r / . |
| 746 |
|
| 747 |
(** Equality for [sig2] *) |
| 748 |
Section sig2. |
| 749 |
(* We make [sig_of_sig2] a coercion so we can use [proj1], [proj2] on [sig2] *) |
| 750 |
Local Coercion sig_of_sig2 : sig2 >-> sig. |
| 751 |
Local Coercion ex_of_ex2 : ex2 >-> ex. |
| 752 |
Local Unset Implicit Arguments. |
| 753 |
(** Projecting an equality of a pair to equality of the first components *) |
| 754 |
Definition sig_of_sig2_eq {A} {P Q : A -> Prop} {u v : { a : A | P a & Q a }} (p : u = v) |
| 755 |
: u = v :> { a : A | P a } |
| 756 |
:= f_equal _ p. |
| 757 |
Definition proj1_sig_of_sig2_eq {A} {P Q : A -> Prop} {u v : { a : A | P a & Q a }} (p : u = v) |
| 758 |
: proj1_sig u = proj1_sig v |
| 759 |
:= proj1_sig_eq (sig_of_sig2_eq p). |
| 760 |
|
| 761 |
(** Projecting an equality of a pair to equality of the second components *) |
| 762 |
Definition proj2_sig_of_sig2_eq {A} {P Q : A -> Prop} {u v : { a : A | P a & Q a }} (p : u = v) |
| 763 |
: rew proj1_sig_of_sig2_eq p in proj2_sig u = proj2_sig v |
| 764 |
:= rew dependent p in eq_refl. |
| 765 |
|
| 766 |
(** Projecting an equality of a pair to equality of the third components *) |
| 767 |
Definition proj3_sig_eq {A} {P Q : A -> Prop} {u v : { a : A | P a & Q a }} (p : u = v) |
| 768 |
: rew proj1_sig_of_sig2_eq p in proj3_sig u = proj3_sig v |
| 769 |
:= rew dependent p in eq_refl. |
| 770 |
|
| 771 |
(** Equality of [sig2] is itself a [sig2] (fowards-reasoning version) *) |
| 772 |
Definition eq_exist2_uncurried {A} {P Q : A -> Prop} |
| 773 |
{u1 v1 : A} {u2 : P u1} {v2 : P v1} {u3 : Q u1} {v3 : Q v1} |
| 774 |
(pqr : { p : u1 = v1 |
| 775 |
| rew p in u2 = v2 & rew p in u3 = v3 }) |
| 776 |
: exist2 _ _ u1 u2 u3 = exist2 _ _ v1 v2 v3. |
| 777 |
Proof. |
| 778 |
destruct pqr as [p q r]. |
| 779 |
destruct r, q, p; simpl. |
| 780 |
reflexivity. |
| 781 |
Defined. |
| 782 |
|
| 783 |
(** Equality of [sig2] is itself a [sig2] (backwards-reasoning version) *) |
| 784 |
Definition eq_sig2_uncurried {A} {P Q : A -> Prop} (u v : { a : A | P a & Q a }) |
| 785 |
(pqr : { p : proj1_sig u = proj1_sig v |
| 786 |
| rew p in proj2_sig u = proj2_sig v & rew p in proj3_sig u = proj3_sig v }) |
| 787 |
: u = v. |
| 788 |
Proof. |
| 789 |
destruct u as [u1 u2 u3], v as [v1 v2 v3]; simpl in *. |
| 790 |
apply eq_exist2_uncurried; exact pqr. |
| 791 |
Defined. |
| 792 |
|
| 793 |
Lemma eq_exist2_curried {A : Type} {P Q : A -> Prop} {u1 v1 : A} {u2 : P u1} {v2 : P v1} {u3 : Q u1} {v3 : Q v1} |
| 794 |
(p : u1 = v1) (q : rew p in u2 = v2) (r : rew p in u3 = v3) : exist2 P Q u1 u2 u3 = exist2 P Q v1 v2 v3. |
| 795 |
Proof. |
| 796 |
apply eq_sig2_uncurried; exists p; exact q + exact r. |
| 797 |
Defined. |
| 798 |
|
| 799 |
(** Curried version of proving equality of sigma types *) |
| 800 |
Definition eq_sig2 {A} {P Q : A -> Prop} (u v : { a : A | P a & Q a }) |
| 801 |
(p : proj1_sig u = proj1_sig v) |
| 802 |
(q : rew p in proj2_sig u = proj2_sig v) |
| 803 |
(r : rew p in proj3_sig u = proj3_sig v) |
| 804 |
: u = v |
| 805 |
:= eq_sig2_uncurried u v (exist2 _ _ p q r). |
| 806 |
|
| 807 |
(** In order to have a performant [inversion_sigma], we define |
| 808 |
specialized versions for when we have constructors on one or |
| 809 |
both sides of the equality *) |
| 810 |
Definition eq_exist2_l {A : Type} {P Q : A -> Prop} {u1 : A} {u2 : P u1} {u3 : Q u1} {v : { a : A | P a & Q a }} |
| 811 |
(p : u1 = proj1_sig v) (q : rew p in u2 = proj2_sig v) (r : rew p in u3 = proj3_sig v) : exist2 P Q u1 u2 u3 = v |
| 812 |
:= eq_sig2 (exist2 P Q u1 u2 u3) v p q r. |
| 813 |
Definition eq_exist2_r {A : Type} {P Q : A -> Prop} {u : { a : A | P a & Q a }} {v1 : A} {v2 : P v1} {v3 : Q v1} |
| 814 |
(p : proj1_sig u = v1) (q : rew p in proj2_sig u = v2) (r : rew p in proj3_sig u = v3) : u = exist2 P Q v1 v2 v3 |
| 815 |
:= eq_sig2 u (exist2 P Q v1 v2 v3) p q r. |
| 816 |
|
| 817 |
(** Equality of [sig2] when the second property is an hProp *) |
| 818 |
Definition eq_sig2_hprop {A} {P Q : A -> Prop} (Q_hprop : forall (x : A) (p q : Q x), p = q) |
| 819 |
(u v : { a : A | P a & Q a }) |
| 820 |
(p : u = v :> { a : A | P a }) |
| 821 |
: u = v |
| 822 |
:= eq_sig2 u v (proj1_sig_eq p) (proj2_sig_eq p) (Q_hprop _ _ _). |
| 823 |
|
| 824 |
(** Equivalence of equality of [sig2] with a [sig2] of equality *) |
| 825 |
(** We could actually prove an isomorphism here, and not just [<->], |
| 826 |
but for simplicity, we don't. *) |
| 827 |
Definition eq_sig2_uncurried_iff {A P Q} |
| 828 |
(u v : { a : A | P a & Q a }) |
| 829 |
: u = v |
| 830 |
<-> { p : proj1_sig u = proj1_sig v |
| 831 |
| rew p in proj2_sig u = proj2_sig v & rew p in proj3_sig u = proj3_sig v }. |
| 832 |
Proof. |
| 833 |
split; [ intro; subst; exists eq_refl; reflexivity | apply eq_sig2_uncurried ]. |
| 834 |
Defined. |
| 835 |
|
| 836 |
(** Induction principle for [@eq (sig2 _ _)] *) |
| 837 |
Definition eq_sig2_rect {A P Q} {u v : { a : A | P a & Q a }} (R : u = v -> Type) |
| 838 |
(f : forall p q r, R (eq_sig2 u v p q r)) |
| 839 |
: forall p, R p. |
| 840 |
Proof. |
| 841 |
intro p. |
| 842 |
specialize (f (proj1_sig_of_sig2_eq p) (proj2_sig_of_sig2_eq p) (proj3_sig_eq p)). |
| 843 |
destruct u, p; exact f. |
| 844 |
Defined. |
| 845 |
Definition eq_sig2_rec {A P Q u v} (R : u = v :> { a : A | P a & Q a } -> Set) := eq_sig2_rect R. |
| 846 |
Definition eq_sig2_ind {A P Q u v} (R : u = v :> { a : A | P a & Q a } -> Prop) := eq_sig2_rec R. |
| 847 |
|
| 848 |
(** In order to have a performant [inversion_sigma], we define |
| 849 |
specialized versions for when we have constructors on one or |
| 850 |
both sides of the equality *) |
| 851 |
Definition eq_sig2_rect_exist2_l {A P Q} {u1 u2 u3 v} (R : _ -> Type) |
| 852 |
(f : forall p q r, R (@eq_exist2_l A P Q u1 u2 u3 v p q r)) |
| 853 |
: forall p, R p |
| 854 |
:= eq_sig2_rect R f. |
| 855 |
Definition eq_sig2_rect_exist2_r {A P Q} {u v1 v2 v3} (R : _ -> Type) |
| 856 |
(f : forall p q r, R (@eq_exist2_r A P Q u v1 v2 v3 p q r)) |
| 857 |
: forall p, R p |
| 858 |
:= eq_sig2_rect R f. |
| 859 |
Definition eq_sig2_rect_exist2 {A P Q} {u1 u2 u3 v1 v2 v3} (R : _ -> Type) |
| 860 |
(f : forall p q r, R (@eq_exist2_curried A P Q u1 v1 u2 v2 u3 v3 p q r)) |
| 861 |
: forall p, R p |
| 862 |
:= eq_sig2_rect R f. |
| 863 |
|
| 864 |
(** We want uncurried versions so [inversion_sigma] can accept |
| 865 |
intropatterns, but we use [ex2] types for the induction |
| 866 |
hypothesis to avoid extraction errors about informative |
| 867 |
inductive types having Prop instances *) |
| 868 |
Definition eq_sig2_rect_uncurried {A P Q} {u v : { a : A | P a & Q a }} (R : u = v -> Type) |
| 869 |
(f : forall pqr : exists2 p : proj1_sig u = proj1_sig v, _ & _, R (eq_sig2 u v (ex_proj1 pqr) (ex_proj2 pqr) (ex_proj3 pqr))) |
| 870 |
: forall p, R p |
| 871 |
:= eq_sig2_rect R (fun p q r => f (ex_intro2 _ _ p q r)). |
| 872 |
Definition eq_sig2_rec_uncurried {A P Q u v} (R : u = v :> { a : A | P a & Q a } -> Set) := eq_sig2_rect_uncurried R. |
| 873 |
Definition eq_sig2_ind_uncurried {A P Q u v} (R : u = v :> { a : A | P a & Q a } -> Prop) := eq_sig2_rec_uncurried R. |
| 874 |
|
| 875 |
(** Equivalence of equality of [sig2] involving hProps with equality of the first components *) |
| 876 |
Definition eq_sig2_hprop_iff {A} {P Q : A -> Prop} (Q_hprop : forall (x : A) (p q : Q x), p = q) |
| 877 |
(u v : { a : A | P a & Q a }) |
| 878 |
: u = v <-> (u = v :> { a : A | P a }) |
| 879 |
:= conj (fun p => f_equal (@sig_of_sig2 _ _ _) p) (eq_sig2_hprop Q_hprop u v). |
| 880 |
|
| 881 |
(** Non-dependent classification of equality of [sig] *) |
| 882 |
Definition eq_sig2_nondep {A} {B C : Prop} (u v : @sig2 A (fun _ => B) (fun _ => C)) |
| 883 |
(p : proj1_sig u = proj1_sig v) (q : proj2_sig u = proj2_sig v) (r : proj3_sig u = proj3_sig v) |
| 884 |
: u = v |
| 885 |
:= @eq_sig2 _ _ _ u v p (eq_trans (rew_const _ _) q) (eq_trans (rew_const _ _) r). |
| 886 |
|
| 887 |
(** Classification of transporting across an equality of [sig2]s *) |
| 888 |
Lemma rew_sig2 {A x} {P : A -> Type} (Q R : forall a, P a -> Prop) |
| 889 |
(u : { p : P x | Q x p & R x p }) |
| 890 |
{y} (H : x = y) |
| 891 |
: rew [fun a => { p : P a | Q a p & R a p }] H in u |
| 892 |
= exist2 |
| 893 |
(Q y) |
| 894 |
(R y) |
| 895 |
(rew H in proj1_sig u) |
| 896 |
(rew dependent H in proj2_sig u) |
| 897 |
(rew dependent H in proj3_sig u). |
| 898 |
Proof. |
| 899 |
destruct H, u; reflexivity. |
| 900 |
Defined. |
| 901 |
End sig2. |
| 902 |
Global Arguments eq_exist2_curried A P Q _ _ _ _ _ _ !p !q !r / . |
| 903 |
|
| 904 |
|
| 905 |
(** [sumbool] is a boolean type equipped with the justification of |
| 906 |
their value *) |
| 907 |
|
| 908 |
Inductive sumbool (A B:Prop) : Set := |
| 909 |
| left : A -> {A} + {B} |
| 910 |
| right : B -> {A} + {B} |
| 911 |
where "{ A } + { B }" := (sumbool A B) : type_scope. |
| 912 |
|
| 913 |
Add Printing If sumbool. |
| 914 |
|
| 915 |
Arguments left {A B} _, [A] B _. |
| 916 |
Arguments right {A B} _ , A [B] _. |
| 917 |
|
| 918 |
Register sumbool as core.sumbool.type. |
| 919 |
|
| 920 |
(** [sumor] is an option type equipped with the justification of why |
| 921 |
it may not be a regular value *) |
| 922 |
|
| 923 |
#[universes(template)] |
| 924 |
Inductive sumor (A:Type) (B:Prop) : Type := |
| 925 |
| inleft : A -> A + {B} |
| 926 |
| inright : B -> A + {B} |
| 927 |
where "A + { B }" := (sumor A B) : type_scope. |
| 928 |
|
| 929 |
Add Printing If sumor. |
| 930 |
|
| 931 |
Arguments inleft {A B} _ , [A] B _. |
| 932 |
Arguments inright {A B} _ , A [B] _. |
| 933 |
|
| 934 |
(* Unset Universe Polymorphism. *) |
| 935 |
|
| 936 |
(** Various forms of the axiom of choice for specifications *) |
| 937 |
|
| 938 |
Section Choice_lemmas. |
| 939 |
|
| 940 |
Variables S S' : Set. |
| 941 |
Variable R : S -> S' -> Prop. |
| 942 |
Variable R' : S -> S' -> Set. |
| 943 |
Variables R1 R2 : S -> Prop. |
| 944 |
|
| 945 |
Lemma Choice : |
| 946 |
(forall x:S, {y:S' | R x y}) -> {f:S -> S' | forall z:S, R z (f z)}. |
| 947 |
Proof. |
| 948 |
intro H. |
| 949 |
exists (fun z => proj1_sig (H z)). |
| 950 |
intro z; destruct (H z); assumption. |
| 951 |
Defined. |
| 952 |
|
| 953 |
Lemma Choice2 : |
| 954 |
(forall x:S, {y:S' & R' x y}) -> {f:S -> S' & forall z:S, R' z (f z)}. |
| 955 |
Proof. |
| 956 |
intro H. |
| 957 |
exists (fun z => projT1 (H z)). |
| 958 |
intro z; destruct (H z); assumption. |
| 959 |
Defined. |
| 960 |
|
| 961 |
Lemma bool_choice : |
| 962 |
(forall x:S, {R1 x} + {R2 x}) -> |
| 963 |
{f:S -> bool | forall x:S, f x = true /\ R1 x \/ f x = false /\ R2 x}. |
| 964 |
Proof. |
| 965 |
intro H. |
| 966 |
exists (fun z:S => if H z then true else false). |
| 967 |
intro z; destruct (H z); auto. |
| 968 |
Defined. |
| 969 |
|
| 970 |
End Choice_lemmas. |
| 971 |
|
| 972 |
Section Dependent_choice_lemmas. |
| 973 |
|
| 974 |
Variable X : Type. |
| 975 |
Variable R : X -> X -> Prop. |
| 976 |
|
| 977 |
Lemma dependent_choice : |
| 978 |
(forall x:X, {y | R x y}) -> |
| 979 |
forall x0, {f : nat -> X | f O = x0 /\ forall n, R (f n) (f (S n))}. |
| 980 |
Proof. |
| 981 |
intros H x0. |
| 982 |
set (f:=fix f n := match n with O => x0 | S n' => proj1_sig (H (f n')) end). |
| 983 |
exists f. |
| 984 |
split. |
| 985 |
- reflexivity. |
| 986 |
- intro n; induction n; simpl; apply proj2_sig. |
| 987 |
Defined. |
| 988 |
|
| 989 |
End Dependent_choice_lemmas. |
| 990 |
|
| 991 |
|
| 992 |
(** A result of type [(Exc A)] is either a normal value of type [A] or |
| 993 |
an [error] : |
| 994 |
|
| 995 |
[Inductive Exc [A:Type] : Type := value : A->(Exc A) | error : (Exc A)]. |
| 996 |
|
| 997 |
It is implemented using the option type. *) |
| 998 |
Section Exc. |
| 999 |
Variable A : Type. |
| 1000 |
|
| 1001 |
Definition Exc := option A. |
| 1002 |
Definition value := @Some A. |
| 1003 |
Definition error := @None A. |
| 1004 |
End Exc. |
| 1005 |
Arguments error {A}. |
| 1006 |
|
| 1007 |
Definition except := False_rec. (* for compatibility with previous versions *) |
| 1008 |
|
| 1009 |
Arguments except [P] _. |
| 1010 |
|
| 1011 |
Theorem absurd_set : forall (A:Prop) (C:Set), A -> ~ A -> C. |
| 1012 |
Proof. |
| 1013 |
intros A C h1 h2. |
| 1014 |
apply False_rec. |
| 1015 |
apply (h2 h1). |
| 1016 |
Defined. |
| 1017 |
|
| 1018 |
#[global] |
| 1019 |
Hint Resolve left right inleft inright: core. |
| 1020 |
#[global] |
| 1021 |
Hint Resolve exist exist2 existT existT2: core. |