| 1 |
(** * Definition and classification of the [Σ] Type, with primitive projections *) |
| 2 |
(** In this file, we classify the basic structure of Σ types ([sigT] |
| 3 |
in Coq). In particular, we classify equalities of dependent pairs |
| 4 |
(inhabitants of Σ types), so that when we have an equality between |
| 5 |
two such pairs, or when we want such an equality, we have a |
| 6 |
systematic way of reducing such equalities to equalities at |
| 7 |
simpler types. *) |
| 8 |
From Coq Require Import Morphisms. |
| 9 |
Require Import Crypto.Util.IffT. |
| 10 |
Require Import Crypto.Util.Equality. |
| 11 |
Require Import Crypto.Util.GlobalSettings. |
| 12 |
Require Import Crypto.Util.Notations. |
| 13 |
|
| 14 |
Local Set Primitive Projections. |
| 15 |
|
| 16 |
Declare Scope primproj_type_scope. |
| 17 |
Declare Scope primproj_scope. |
| 18 |
Delimit Scope primproj_type_scope with primproj_type. |
| 19 |
Delimit Scope primproj_scope with primproj. |
| 20 |
|
| 21 |
Import EqNotations. |
| 22 |
Module Import Primitive. |
| 23 |
Record sigT {A} P := existT { projT1 : A ; projT2 : P projT1 }. |
| 24 |
Global Arguments existT {_} _ _ _. |
| 25 |
Global Arguments projT1 {_ _} _. |
| 26 |
Global Arguments projT2 {_ _} _. |
| 27 |
|
| 28 |
Module Export Notations. |
| 29 |
Notation "{ x & P }" := (sigT (fun x => P)) : type_scope. |
| 30 |
Notation "{ x : A & P }" := (sigT (A:=A) (fun x => P)) : type_scope. |
| 31 |
Add Printing Let sigT. |
| 32 |
End Notations. |
| 33 |
|
| 34 |
Add Printing Let sigT. |
| 35 |
|
| 36 |
Local Set Implicit Arguments. |
| 37 |
|
| 38 |
Definition sigT_beq A P (eq_A : A -> A -> bool) (eq_P : forall a a', P a -> P a' -> bool) (X Y : @sigT A P) : bool |
| 39 |
:= let (a, b) := X in let (a', b') := Y in (eq_A a a' && eq_P _ _ b b')%bool. |
| 40 |
Global Arguments sigT_beq / . |
| 41 |
Lemma sigT_dec_bl A P eq_A eq_P |
| 42 |
(A_bl : forall x y : A, eq_A x y = true -> x = y) |
| 43 |
(P_bl : forall a a' x y (pf : eq_A a a' = true), eq_P a a' x y = true -> rew [P] (A_bl _ _ pf) in x = y) |
| 44 |
(x y : @sigT A P) |
| 45 |
: sigT_beq eq_A eq_P x y = true -> x = y. |
| 46 |
Proof. |
| 47 |
destruct x as [a b], y as [a' b']. |
| 48 |
specialize (P_bl a a' b b'); cbn. |
| 49 |
generalize dependent (A_bl a a'); clear A_bl; intros A_bl P_bl H. |
| 50 |
edestruct eq_A; cbn in *; |
| 51 |
destruct (eq_A a a'); |
| 52 |
first [ specialize (P_bl eq_refl); generalize dependent (A_bl eq_refl) |
| 53 |
| specialize (P_bl H); generalize dependent (A_bl H) ]; |
| 54 |
clear A_bl; intros A_bl P_bl; try specialize (P_bl H); subst; |
| 55 |
cbn [eq_rect] in *; |
| 56 |
try reflexivity. |
| 57 |
all: edestruct eq_P; first [ specialize (P_bl eq_refl) | specialize (P_bl H) ]; subst; |
| 58 |
reflexivity. |
| 59 |
Defined. |
| 60 |
Lemma sigT_dec_bl_nd A P eq_A eq_P |
| 61 |
(A_bl : forall x y : A, eq_A x y = true -> x = y) |
| 62 |
(P_bl : forall a a' x y (pf : a = a'), rew [P] pf in x = y) |
| 63 |
(x y : @sigT A P) |
| 64 |
: sigT_beq eq_A eq_P x y = true -> x = y. |
| 65 |
Proof. |
| 66 |
apply (@sigT_dec_bl A P eq_A eq_P A_bl); intros; apply P_bl. |
| 67 |
Defined. |
| 68 |
|
| 69 |
Lemma sigT_dec_lb A P eq_A (eq_P : forall a a', P a -> P a' -> bool) |
| 70 |
(A_lb : forall x y : A, x = y -> eq_A x y = true) |
| 71 |
(P_lb : forall a (x y : P a), x = y -> eq_P a a x y = true) |
| 72 |
(x y : @sigT A P) |
| 73 |
: x = y -> sigT_beq eq_A eq_P x y = true. |
| 74 |
Proof. |
| 75 |
intro; subst y; specialize (A_lb _ _ (eq_refl (projT1 x))); specialize (P_lb _ _ _ (eq_refl (projT2 x))); cbn in *. |
| 76 |
edestruct eq_A, eq_P; (reflexivity + assumption). |
| 77 |
Defined. |
| 78 |
Definition sigT_eq_dec A P eq_A eq_P |
| 79 |
(A_bl : forall x y : A, eq_A x y = true -> x = y) |
| 80 |
(P_bl : forall a a' x y (pf : eq_A a a' = true), eq_P a a' x y = true -> rew [P] (A_bl _ _ pf) in x = y) |
| 81 |
(A_lb : forall x y : A, x = y -> eq_A x y = true) |
| 82 |
(P_lb : forall a (x y : P a), x = y -> eq_P a a x y = true) |
| 83 |
(x y : @sigT A P) |
| 84 |
: {x = y} + {x <> y}. |
| 85 |
Proof. |
| 86 |
refine (let H := let b := @sigT_beq A P eq_A eq_P x y in if b as b0 return ({b0 = true} + {b0 = false}) then left eq_refl else right eq_refl in |
| 87 |
match H with |
| 88 |
| left e => left (@sigT_dec_bl A P eq_A eq_P A_bl P_bl x y e) |
| 89 |
| right H' => right (fun e => _ (@sigT_dec_lb A P eq_A eq_P A_lb P_lb x y e)) |
| 90 |
end). |
| 91 |
congruence. |
| 92 |
Defined. |
| 93 |
End Primitive. |
| 94 |
Notation "{ x & P }" := (sigT (fun x => P%primproj_type)) : primproj_type_scope. |
| 95 |
Notation "{ x : A & P }" := (sigT (A:=A%primproj_type) (fun x => P%primproj_type)) : primproj_type_scope. |
| 96 |
Add Printing Let sigT. |
| 97 |
|
| 98 |
Local Arguments f_equal {_ _} _ {_ _} _. |
| 99 |
|
| 100 |
Definition projT1_existT {A B} (a:A) (b:B a) : projT1 (existT B a b) = a := eq_refl. |
| 101 |
Definition projT2_existT {A B} (a:A) (b:B a) : projT2 (existT B a b) = b := eq_refl. |
| 102 |
Create HintDb cancel_primsig discriminated. #[global] Hint Rewrite @projT1_existT @projT2_existT : cancel_primsig. |
| 103 |
|
| 104 |
(** ** Equality for [sigT] *) |
| 105 |
Section sigT. |
| 106 |
|
| 107 |
(** *** Projecting an equality of a pair to equality of the first components *) |
| 108 |
Definition pr1_path {A} {P : A -> Type} {u v : sigT P} (p : u = v) |
| 109 |
: projT1 u = projT1 v |
| 110 |
:= f_equal (@projT1 _ _) p. |
| 111 |
|
| 112 |
(** *** Projecting an equality of a pair to equality of the second components *) |
| 113 |
Definition pr2_path {A} {P : A -> Type} {u v : sigT P} (p : u = v) |
| 114 |
: eq_rect _ _ (projT2 u) _ (pr1_path p) = projT2 v. |
| 115 |
Proof. |
| 116 |
destruct p; reflexivity. |
| 117 |
Defined. |
| 118 |
|
| 119 |
(** *** Equality of [sigT] is itself a [sigT] *) |
| 120 |
Definition path_sigT_uncurried {A : Type} {P : A -> Type} (u v : sigT P) |
| 121 |
(pq : sigT (fun p : projT1 u = projT1 v => eq_rect _ _ (projT2 u) _ p = projT2 v)) |
| 122 |
: u = v. |
| 123 |
Proof. |
| 124 |
destruct u as [u1 u2], v as [v1 v2]; simpl in *. |
| 125 |
destruct pq as [p q]. |
| 126 |
destruct q; simpl in *. |
| 127 |
destruct p; reflexivity. |
| 128 |
Defined. |
| 129 |
|
| 130 |
(** *** Curried version of proving equality of sigma types *) |
| 131 |
Definition path_sigT {A : Type} {P : A -> Type} (u v : sigT P) |
| 132 |
(p : projT1 u = projT1 v) (q : eq_rect _ _ (projT2 u) _ p = projT2 v) |
| 133 |
: u = v |
| 134 |
:= path_sigT_uncurried u v (existT _ p q). |
| 135 |
|
| 136 |
(** *** Equality of [sigT] when the property is an hProp *) |
| 137 |
Definition path_sigT_hprop {A P} (P_hprop : forall (x : A) (p q : P x), p = q) |
| 138 |
(u v : @sigT A P) |
| 139 |
(p : projT1 u = projT1 v) |
| 140 |
: u = v |
| 141 |
:= path_sigT u v p (P_hprop _ _ _). |
| 142 |
|
| 143 |
(** *** Equivalence of equality of [sigT] with a [sigT] of equality *) |
| 144 |
(** We could actually use [IsIso] here, but for simplicity, we |
| 145 |
don't. If we wanted to deal with proofs of equality of Σ types |
| 146 |
in dependent positions, we'd need it. *) |
| 147 |
Definition path_sigT_uncurried_iff {A P} |
| 148 |
(u v : @sigT A P) |
| 149 |
: u = v <-> (sigT (fun p : projT1 u = projT1 v => eq_rect _ _ (projT2 u) _ p = projT2 v)). |
| 150 |
Proof. |
| 151 |
split; [ intro; subst; exists eq_refl; reflexivity | apply path_sigT_uncurried ]. |
| 152 |
Defined. |
| 153 |
|
| 154 |
(** *** Induction principle for [@eq (sigT _)] *) |
| 155 |
Definition path_sigT_rect {A P} {u v : @sigT A P} (Q : u = v -> Type) |
| 156 |
(f : forall p q, Q (path_sigT u v p q)) |
| 157 |
: forall p, Q p. |
| 158 |
Proof. intro p; specialize (f (pr1_path p) (pr2_path p)); destruct p; exact f. Defined. |
| 159 |
Definition path_sigT_rec {A P u v} (Q : u = v :> @sigT A P -> Set) := path_sigT_rect Q. |
| 160 |
Definition path_sigT_ind {A P u v} (Q : u = v :> @sigT A P -> Prop) := path_sigT_rec Q. |
| 161 |
|
| 162 |
(** *** Equivalence of equality of [sigT] involving hProps with equality of the first components *) |
| 163 |
Definition path_sigT_hprop_iff {A P} (P_hprop : forall (x : A) (p q : P x), p = q) |
| 164 |
(u v : @sigT A P) |
| 165 |
: u = v <-> (projT1 u = projT1 v) |
| 166 |
:= conj (f_equal projT1) (path_sigT_hprop P_hprop u v). |
| 167 |
|
| 168 |
(** *** Non-dependent classification of equality of [sigT] *) |
| 169 |
Definition path_sigT_nondep {A B : Type} (u v : @sigT A (fun _ => B)) |
| 170 |
(p : projT1 u = projT1 v) (q : projT2 u = projT2 v) |
| 171 |
: u = v |
| 172 |
:= @path_sigT _ _ u v p (eq_trans (transport_const _ _) q). |
| 173 |
|
| 174 |
(** *** Classification of transporting across an equality of [sigT]s *) |
| 175 |
Lemma eq_rect_sigT {A x} {P : A -> Type} (Q : forall a, P a -> Prop) (u : sigT (Q x)) {y} (H : x = y) |
| 176 |
: eq_rect x (fun a => sigT (Q a)) u y H |
| 177 |
= existT |
| 178 |
(Q y) |
| 179 |
(eq_rect x P (projT1 u) y H) |
| 180 |
match H in (_ = y) return Q y (eq_rect x P (projT1 u) y H) with |
| 181 |
| eq_refl => projT2 u |
| 182 |
end. |
| 183 |
Proof. |
| 184 |
destruct H; reflexivity. |
| 185 |
Defined. |
| 186 |
|
| 187 |
(** *** η Principle for [sigT] *) |
| 188 |
Definition sigT_eta {A P} (p : { a : A & P a }) |
| 189 |
: p = existT _ (projT1 p) (projT2 p) |
| 190 |
:= eq_refl. |
| 191 |
End sigT. |
| 192 |
|
| 193 |
(** ** Useful Tactics *) |
| 194 |
(** *** [inversion_sigma] *) |
| 195 |
(** The built-in [inversion] will frequently leave equalities of |
| 196 |
dependent pairs. When the first type in the pair is an hProp or |
| 197 |
otherwise simplifies, [inversion_sigma] is useful; it will replace |
| 198 |
the equality of pairs with a pair of equalities, one involving a |
| 199 |
term casted along the other. This might also prove useful for |
| 200 |
writing a version of [inversion] / [dependent destruction] which |
| 201 |
does not lose information, i.e., does not turn a goal which is |
| 202 |
provable into one which requires axiom K / UIP. *) |
| 203 |
Ltac simpl_proj_exist_in H := |
| 204 |
repeat match type of H with |
| 205 |
| context G[projT1 (existT _ ?x ?p)] |
| 206 |
=> let G' := context G[x] in change G' in H |
| 207 |
| context G[projT2 (existT _ ?x ?p)] |
| 208 |
=> let G' := context G[p] in change G' in H |
| 209 |
end. |
| 210 |
Ltac induction_sigma_in_as_using H H0 H1 rect := |
| 211 |
induction H as [H0 H1] using (rect _ _ _ _); |
| 212 |
simpl_proj_exist_in H0; |
| 213 |
simpl_proj_exist_in H1. |
| 214 |
Ltac induction_sigma_in_using H rect := |
| 215 |
let H0 := fresh H in |
| 216 |
let H1 := fresh H in |
| 217 |
induction_sigma_in_as_using H H0 H1 rect. |
| 218 |
Ltac inversion_sigma_step := |
| 219 |
match goal with |
| 220 |
| [ H : _ = existT _ _ _ |- _ ] |
| 221 |
=> induction_sigma_in_using H @path_sigT_rect |
| 222 |
| [ H : existT _ _ _ = _ |- _ ] |
| 223 |
=> induction_sigma_in_using H @path_sigT_rect |
| 224 |
end. |
| 225 |
Ltac inversion_sigma := repeat inversion_sigma_step. |