| 1 |
(** * The Structure of the [eq] Type *) |
| 2 |
(** As described by the project of homotopy type theory, there is a |
| 3 |
lot of structure inherent in the type of propositional equality, |
| 4 |
[eq]. We build up enough lemmas about this structure to deal |
| 5 |
nicely with proofs of equality that come up in practice in this |
| 6 |
project. *) |
| 7 |
From Coq Require Import Morphisms. |
| 8 |
Require Import Crypto.Util.Isomorphism. |
| 9 |
Require Import Crypto.Util.HProp. |
| 10 |
|
| 11 |
Import EqNotations. |
| 12 |
|
| 13 |
Definition f_equal_dep {A B} (f : forall a : A, B a) (x y : A) (H : x = y) : rew [B] H in f x = f y |
| 14 |
:= match H with |
| 15 |
| eq_refl => eq_refl |
| 16 |
end. |
| 17 |
|
| 18 |
(** Most of the structure of proofs of equalities fits into what |
| 19 |
mathematicians call a weak ∞-groupoid. (In fact, one way of |
| 20 |
*defining* a weak ∞-groupoid is via what's called the J-rule, |
| 21 |
which exactly matches the eliminator [eq_rect] for the equality |
| 22 |
type [eq].) The first level of this groupoid is given by the |
| 23 |
identity [eq_refl], the binary operation [eq_trans], and the |
| 24 |
inverse of [p : x = y] given by [eq_sym p : y = x]. The second |
| 25 |
level, which we provide here, says that [id ∘ p = p = p ∘ id], |
| 26 |
that [(p ∘ q)⁻¹ = q⁻¹ ∘ p⁻¹], that [(p⁻¹)⁻¹ = p], and that [p ∘ |
| 27 |
p⁻¹ = p⁻¹ ∘ p = id]. We prove these here, as well as a few lemmas |
| 28 |
about functoriality of functions over equality. *) |
| 29 |
Definition concat_1p {A x y} (p : x = y :> A) : eq_trans eq_refl p = p. |
| 30 |
Proof. case p; reflexivity. Defined. |
| 31 |
Definition concat_p1 {A x y} (p : x = y :> A) : eq_trans p eq_refl = p. |
| 32 |
Proof. case p; reflexivity. Defined. |
| 33 |
Definition concat_pV {A x y} (p : x = y :> A) : eq_trans p (eq_sym p) = eq_refl. |
| 34 |
Proof. case p; reflexivity. Defined. |
| 35 |
Definition concat_Vp {A x y} (p : x = y :> A) : eq_trans (eq_sym p) p = eq_refl. |
| 36 |
Proof. case p; reflexivity. Defined. |
| 37 |
Definition transport_pp {A} {P : A -> Type} {x y z} (p : x = y) (q : y = z) (k : P x) |
| 38 |
: eq_rect _ P k _ (eq_trans p q) = eq_rect _ P (eq_rect _ P k _ p) _ q. |
| 39 |
Proof. case q; simpl; reflexivity. Defined. |
| 40 |
Lemma transport_const {A P x y} (p : x = y :> A) k |
| 41 |
: eq_rect _ (fun _ : A => P) k _ p = k. |
| 42 |
Proof. case p; reflexivity. Defined. |
| 43 |
Lemma ap_const {A B x y} (b : B) (p : x = y :> A) |
| 44 |
: f_equal (fun _ => b) p = eq_refl. |
| 45 |
Proof. case p; reflexivity. Defined. |
| 46 |
Lemma inv_pp {A x y z} (p : x = y :> A) (q : y = z :> A) |
| 47 |
: eq_sym (eq_trans p q) = eq_trans (eq_sym q) (eq_sym p). |
| 48 |
Proof. case q; case p; reflexivity. Defined. |
| 49 |
Lemma inv_V {A x y} (p : x = y :> A) |
| 50 |
: eq_sym (eq_sym p) = p. |
| 51 |
Proof. case p; reflexivity. Defined. |
| 52 |
Definition transport_idmap_ap A (P : A -> Type) x y (p : x = y) (u : P x) |
| 53 |
: eq_rect _ P u _ p = eq_rect _ (fun T => T) u _ (f_equal P p). |
| 54 |
Proof. case p; reflexivity. Defined. |
| 55 |
Definition ap_transport {A} {P Q : A -> Type} {x y : A} (p : x = y) (f : forall x, P x -> Q x) (z : P x) |
| 56 |
: f _ (@eq_rect A x P z y p) = @eq_rect A x Q (f _ z) y p. |
| 57 |
Proof. case p; reflexivity. Defined. |
| 58 |
(** TODO: Remove [rew_map] when we drop support for Coq 8.9, since it's in the standard library there *) |
| 59 |
Lemma rew_map A B P (f : A -> B) x1 x2 (H : x1 = x2) y |
| 60 |
: rew [fun x => P (f x)] H in y = rew [P] f_equal f H in y. |
| 61 |
Proof. case H; reflexivity. Defined. |
| 62 |
|
| 63 |
(** To classify the equalities of a type [A] at a point [a : A], we |
| 64 |
must give a "code" that stands in for the type [a = x] for each |
| 65 |
[x], and a way of "encoding" proofs of equality into the code that |
| 66 |
we claim represents this type. To prove that the code is good, it |
| 67 |
turns out that it sufficies to prove that it is freely generated |
| 68 |
by the encoding of [eq_refl], i.e., that [{ x : A & a = x }] and |
| 69 |
[{ x : A & code x }] are equivalent, i.e., that [{ x : A & code x |
| 70 |
}] is an hProp (has at most one element). When this is the case, |
| 71 |
we have fully classified the type of equalities in [A] at [a] up |
| 72 |
to isomorphism. This lets us replace proofs of equalities in [A] |
| 73 |
with their codes, which are frequently easier to manipulate. (For |
| 74 |
example, the code for [x = y :> A * B] is [(fst x = fst y) * (snd |
| 75 |
x = snd y)], whence we can destruct the pair.) |
| 76 |
|
| 77 |
This method of proof, introduced in homotopy type theory, is |
| 78 |
called encode-decode. *) |
| 79 |
Section gen. |
| 80 |
Context {A : Type} {x : A} (code : A -> Type) |
| 81 |
(encode : forall y, x = y -> code y) |
| 82 |
{code_hprop : IsHProp { a : A & code a } }. |
| 83 |
|
| 84 |
Definition decode' {y} (p : code y) : x = y |
| 85 |
:= f_equal (@projT1 _ _) (code_hprop (existT _ x (encode x eq_refl)) (existT _ y p)). |
| 86 |
|
| 87 |
Definition decode {y} (H : code y) : x = y |
| 88 |
:= eq_trans (eq_sym (decode' (encode x eq_refl))) (decode' H). |
| 89 |
|
| 90 |
Definition deencode {y} (H : x = y) : decode (encode y H) = H. |
| 91 |
Proof. |
| 92 |
destruct H. |
| 93 |
unfold decode. |
| 94 |
edestruct (decode' (encode x eq_refl)). |
| 95 |
reflexivity. |
| 96 |
Defined. |
| 97 |
|
| 98 |
Global Instance isiso_encode : forall {y}, IsIso (encode y). |
| 99 |
Proof. |
| 100 |
intro y. |
| 101 |
exists (@decode y). |
| 102 |
{ intro H. |
| 103 |
unfold decode. |
| 104 |
set (yH := (existT _ y H)). |
| 105 |
change H with (projT2 yH). |
| 106 |
change y with (projT1 yH). |
| 107 |
clearbody yH; clear y H. |
| 108 |
destruct (code_hprop (existT _ x (encode x eq_refl)) yH); simpl. |
| 109 |
abstract (rewrite concat_Vp; reflexivity). } |
| 110 |
{ intro p. |
| 111 |
apply deencode. } |
| 112 |
Defined. |
| 113 |
End gen. |
| 114 |
|
| 115 |
(** We use the encode-decode method to prove that if [forall x y : A, |
| 116 |
x = y], then [forall (x y : A) (p q : x = y), p = q]. *) |
| 117 |
(* It's not clear whether this should be in this file, or in HProp. |
| 118 |
But we require [IsHProp] above, so we leave it here for now. *) |
| 119 |
Section hprop. |
| 120 |
Context {A} `{IsHProp A}. |
| 121 |
|
| 122 |
Let hprop_encode {x y : A} (p : x = y) : unit := tt. |
| 123 |
|
| 124 |
Local Hint Extern 1 => simple apply (fun x => @isiso_encode A x (fun _ => unit)) : typeclass_instances. |
| 125 |
|
| 126 |
Global Instance ishprop_path_hprop : IsHPropRel (@eq A). |
| 127 |
Proof. |
| 128 |
intros x y p q. |
| 129 |
pose proof (is_left_inv (@hprop_encode x y)) as H'. |
| 130 |
rewrite <- (H' p), <- (H' q). |
| 131 |
apply f_equal; apply allpath_hprop. |
| 132 |
Qed. |
| 133 |
End hprop. |
| 134 |
|
| 135 |
|
| 136 |
Lemma commute_eq_rect {A} (P Q : A -> Type) (f : forall a, P a -> Q a) a b (H : a = b :> A) |
| 137 |
(v : P a) |
| 138 |
: f b (eq_rect _ P v _ H) |
| 139 |
= eq_rect _ Q (f a v) _ H. |
| 140 |
Proof. destruct H; reflexivity. Defined. |
| 141 |
|
| 142 |
Lemma commute_eq_match {A} (P Q : A -> Type) (f : forall a, P a -> Q a) a b (H : a = b :> A) |
| 143 |
(v : P a) |
| 144 |
: f b (match H in _ = y return P y with eq_refl => v end) |
| 145 |
= (match H in _ = y return Q y with eq_refl => f a v end). |
| 146 |
Proof. destruct H; reflexivity. Defined. |
| 147 |
Lemma commute_eq_match2 {A} (P1 P2 Q : A -> Type) (f : forall a, P1 a -> P2 a -> Q a) a b (H : a = b :> A) |
| 148 |
(v1 : P1 a) (v2 : P2 a) |
| 149 |
: f b |
| 150 |
(match H in _ = y return P1 y with eq_refl => v1 end) |
| 151 |
(match H in _ = y return P2 y with eq_refl => v2 end) |
| 152 |
= (match H in _ = y return Q y with eq_refl => f a v1 v2 end). |
| 153 |
Proof. destruct H; reflexivity. Defined. |
| 154 |
Lemma eq_match_const {A P x y} (p : x = y :> A) k |
| 155 |
: match p return P with eq_refl => k end = k. |
| 156 |
Proof. case p; reflexivity. Defined. |
| 157 |
|
| 158 |
Lemma fg_equal {A B} (f g : A -> B) (x y : A) |
| 159 |
: f = g -> x = y -> f x = g y. |
| 160 |
Proof. intros; subst; reflexivity. Defined. |
| 161 |
|
| 162 |
Lemma fg_equal_rel {A B R} (f g : A -> B) (x y : A) |
| 163 |
: (pointwise_relation _ R) f g -> x = y -> R (f x) (g y). |
| 164 |
Proof. cbv [pointwise_relation]; intros; subst; trivial. Qed. |
| 165 |
|
| 166 |
Lemma push_rew_fun_dep A P Q a b (pf : a = b) f x |
| 167 |
: (rew [fun x : A => P x -> Q x] pf in f) x = (rew [Q] pf in (f (rew <- [P] pf in x))). |
| 168 |
Proof. subst; reflexivity. Defined. |
| 169 |
|
| 170 |
Lemma rew_r_moveL A P x y (pf : x = y :> A) a b |
| 171 |
: (rew [P] pf in a) = b -> a = (rew <- [P] pf in b). |
| 172 |
Proof. subst; exact id. Defined. |
| 173 |
Lemma rew_moveL A P x y (pf : x = y :> A) a b |
| 174 |
: (rew <- [P] pf in a) = b -> a = (rew [P] pf in b). |
| 175 |
Proof. subst; exact id. Defined. |
| 176 |
|
| 177 |
Lemma rew_moveR A P x y (pf : x = y :> A) a b |
| 178 |
: a = (rew <- [P] pf in b) -> (rew [P] pf in a) = b. |
| 179 |
Proof. subst; exact id. Defined. |
| 180 |
Lemma rew_r_moveR A P x y (pf : x = y :> A) a b |
| 181 |
: a = (rew [P] pf in b) -> (rew <- [P] pf in a) = b. |
| 182 |
Proof. subst; exact id. Defined. |