| 1 |
From Coq Require Import RelationClasses Relation_Definitions Morphisms. |
| 2 |
Require Import Crypto.Util.Telescope.Core. |
| 3 |
Require Import Crypto.Util.Telescope.Instances. |
| 4 |
Require Import Crypto.Util.Equality. |
| 5 |
|
| 6 |
Module Export Telescope. |
| 7 |
Fixpoint flattenT_apply_unapply {t : Telescope} {X : Type} {struct t} |
| 8 |
: forall (f : flattenT_sig t -> X) |
| 9 |
(x : flattenT_sig t), |
| 10 |
f x = flattenT_apply (flattenT_unapply f) x |
| 11 |
:= match t return forall (f : flattenT_sig t -> X) |
| 12 |
(x : flattenT_sig t), |
| 13 |
f x = flattenT_apply (flattenT_unapply f) x |
| 14 |
with |
| 15 |
| bottom => fun f x => match x with |
| 16 |
| tt => eq_refl |
| 17 |
end |
| 18 |
| tele A B => fun f x => eq_trans (f_equal f match x return x = existT (fun a => flattenT_sig (B a)) (projT1 x) (projT2 x) with |
| 19 |
| existT _ _ => eq_refl |
| 20 |
end) |
| 21 |
(@flattenT_apply_unapply |
| 22 |
(B (projT1 x)) X |
| 23 |
(fun p => f (existT (fun x' => flattenT_sig (B x')) (projT1 x) p)) |
| 24 |
(projT2 x)) |
| 25 |
end. |
| 26 |
|
| 27 |
Fixpoint flattenT_unapply_apply {t : Telescope} {X : Type} {struct t} |
| 28 |
: forall (f : flattenT t X), |
| 29 |
flattenT_eq f (flattenT_unapply (flattenT_apply f)) |
| 30 |
:= match t return forall (f : flattenT t X), |
| 31 |
flattenT_eq f (flattenT_unapply (flattenT_apply f)) |
| 32 |
with |
| 33 |
| bottom => fun f => eq_refl |
| 34 |
| tele A B => fun f x => @flattenT_unapply_apply (B x) X (f x) |
| 35 |
end. |
| 36 |
|
| 37 |
Fixpoint flatten_forall_apply_unapply {t : Telescope} {struct t} |
| 38 |
: forall {P : flattenT_sig t -> Type} |
| 39 |
(f : forall x, P x) |
| 40 |
(x : flattenT_sig t), |
| 41 |
eq_rect _ (fun Px => Px) (f x) _ (flattenT_apply_unapply P x) |
| 42 |
= flatten_forall_apply (flatten_forall_unapply f) x. |
| 43 |
Proof. |
| 44 |
refine match t return forall {P : flattenT_sig t -> Type} |
| 45 |
(f : forall x, P x) |
| 46 |
(x : flattenT_sig t), |
| 47 |
eq_rect _ (fun Px => Px) (f x) _ (flattenT_apply_unapply P x) |
| 48 |
= flatten_forall_apply (flatten_forall_unapply f) x |
| 49 |
with |
| 50 |
| bottom => fun f x ttt => match ttt with |
| 51 |
| tt => eq_refl |
| 52 |
end |
| 53 |
| tele A B => fun f x x' => eq_trans |
| 54 |
_ |
| 55 |
(@flatten_forall_apply_unapply (B (projT1 x')) _ _ _) |
| 56 |
end. |
| 57 |
destruct x'; simpl in *. |
| 58 |
refine (f_equal _ (concat_1p _)). |
| 59 |
Defined. |
| 60 |
|
| 61 |
Fixpoint flatten_forall_unapply_apply {t : Telescope} {struct t} |
| 62 |
: forall {P : flattenT t Type} |
| 63 |
(f : flatten_forall P), |
| 64 |
flatten_forall_eq |
| 65 |
(flatten_forall_eq_rect (flattenT_unapply_apply P) f) |
| 66 |
(flatten_forall_unapply (flatten_forall_apply f)) |
| 67 |
:= match t return forall {P : flattenT t Type} |
| 68 |
(f : flatten_forall P), |
| 69 |
flatten_forall_eq |
| 70 |
(flatten_forall_eq_rect (flattenT_unapply_apply P) f) |
| 71 |
(flatten_forall_unapply (flatten_forall_apply f)) |
| 72 |
with |
| 73 |
| bottom => fun P f => eq_refl |
| 74 |
| tele A B => fun P f a => @flatten_forall_unapply_apply (B a) (P a) (f a) |
| 75 |
end. |
| 76 |
|
| 77 |
Fixpoint flatten_forall_eq_rect_trans {t : Telescope} |
| 78 |
: forall {P Q R : flattenT t Type} |
| 79 |
(p : flattenT_eq P Q) |
| 80 |
(q : flattenT_eq Q R) |
| 81 |
(f : flatten_forall P), |
| 82 |
flatten_forall_eq (flatten_forall_eq_rect (transitivity p q) f) |
| 83 |
(flatten_forall_eq_rect q (flatten_forall_eq_rect p f)). |
| 84 |
Proof. |
| 85 |
refine match t return forall {P Q R : flattenT t Type} |
| 86 |
(p : flattenT_eq P Q) |
| 87 |
(q : flattenT_eq Q R) |
| 88 |
(f : flatten_forall P), |
| 89 |
flatten_forall_eq (flatten_forall_eq_rect (transitivity p q) f) |
| 90 |
(flatten_forall_eq_rect q (flatten_forall_eq_rect p f)) |
| 91 |
with |
| 92 |
| bottom => fun P Q H k => _ |
| 93 |
| tele A B => fun P Q R p q f a => @flatten_forall_eq_rect_trans (B a) _ _ _ _ _ _ |
| 94 |
end; |
| 95 |
simpl in *. |
| 96 |
{ intros; subst; simpl; reflexivity. } |
| 97 |
Defined. |
| 98 |
|
| 99 |
Fixpoint flatten_forall_eq_rect_flattenT_unapply_Proper {t} |
| 100 |
: forall {P Q : flattenT_sig t -> Type} |
| 101 |
(H : forall x, P x = Q x) |
| 102 |
(f : forall x, P x), |
| 103 |
flatten_forall_eq |
| 104 |
(flatten_forall_eq_rect (@flattenT_unapply_Proper t _ P (fun x => Q x) H) (flatten_forall_unapply f)) |
| 105 |
(flatten_forall_unapply (fun x => eq_rect _ (fun P => P) (f x) _ (H x))) |
| 106 |
:= match t return forall {P Q : flattenT_sig t -> Type} |
| 107 |
(H : forall x, P x = Q x) |
| 108 |
(f : forall x, P x), |
| 109 |
flatten_forall_eq |
| 110 |
(flatten_forall_eq_rect (@flattenT_unapply_Proper t _ P (fun x => Q x) H) (flatten_forall_unapply f)) |
| 111 |
(flatten_forall_unapply (fun x => eq_rect _ (fun P => P) (f x) _ (H x))) |
| 112 |
with |
| 113 |
| bottom => fun P Q H k => eq_refl |
| 114 |
| tele A B => fun P Q H f a => @flatten_forall_eq_rect_flattenT_unapply_Proper (B a) _ _ _ _ |
| 115 |
end. |
| 116 |
|
| 117 |
Fixpoint eq_rect_symmetry_flattenT_apply_unapply {t} |
| 118 |
: forall {f x k}, |
| 119 |
eq_rect _ (fun T => T) (flatten_forall_apply (flatten_forall_unapply k) x) _ (eq_sym (@flattenT_apply_unapply t Type f x)) |
| 120 |
= k x |
| 121 |
:= match t return forall {f x k}, |
| 122 |
eq_rect _ (fun T => T) (flatten_forall_apply (flatten_forall_unapply k) x) _ (eq_sym (@flattenT_apply_unapply t Type f x)) |
| 123 |
= k x |
| 124 |
with |
| 125 |
| bottom => fun f x k => match x with |
| 126 |
| tt => eq_refl |
| 127 |
end |
| 128 |
| tele A'' B'' => fun f x k => |
| 129 |
match x with |
| 130 |
| existT x1 x2 |
| 131 |
=> eq_trans |
| 132 |
(y := eq_rect |
| 133 |
_ (fun T => T) |
| 134 |
(flatten_forall_apply |
| 135 |
(flatten_forall_unapply (fun x' => k (existT (fun x'' => flattenT_sig (B'' x'')) x1 x'))) |
| 136 |
x2) |
| 137 |
_ |
| 138 |
(eq_sym (flattenT_apply_unapply (fun p => f (existT (fun x' => flattenT_sig (B'' x')) x1 p)) x2))) |
| 139 |
(f_equal (fun p' => eq_rect |
| 140 |
_ (fun T => T) |
| 141 |
(flatten_forall_apply |
| 142 |
(flatten_forall_unapply (fun x' => k (existT (fun x'' => flattenT_sig (B'' x'')) x1 x'))) |
| 143 |
x2) |
| 144 |
_ |
| 145 |
(eq_sym p')) |
| 146 |
(concat_1p _)) |
| 147 |
(@eq_rect_symmetry_flattenT_apply_unapply (B'' x1) (fun x2' => f (existT (fun a => flattenT_sig (B'' a)) x1 x2')) x2 (fun x2' => k (existT (fun a => flattenT_sig (B'' a)) x1 x2'))) |
| 148 |
end |
| 149 |
end. |
| 150 |
|
| 151 |
Fixpoint flatten_forall_eq_rect_symmetry_flattenT_unapply_apply {t} |
| 152 |
: forall {f k}, |
| 153 |
flatten_forall_eq |
| 154 |
(flatten_forall_eq_rect |
| 155 |
(symmetry (@flattenT_unapply_apply t Type f)) |
| 156 |
(flatten_forall_unapply (flatten_forall_apply k))) |
| 157 |
k |
| 158 |
:= match t return forall {f k}, |
| 159 |
flatten_forall_eq |
| 160 |
(flatten_forall_eq_rect |
| 161 |
(symmetry (@flattenT_unapply_apply t Type f)) |
| 162 |
(flatten_forall_unapply (flatten_forall_apply k))) |
| 163 |
k |
| 164 |
with |
| 165 |
| bottom => fun f k => eq_refl |
| 166 |
| tele A'' B'' => fun f k a => @flatten_forall_eq_rect_symmetry_flattenT_unapply_apply (B'' a) (f a) (k a) |
| 167 |
end. |
| 168 |
|
| 169 |
Lemma flatten_forall_apply_of_unapply_eq {t X} |
| 170 |
f x |
| 171 |
: eq_rect |
| 172 |
_ |
| 173 |
(fun T : Type => T) |
| 174 |
(flatten_forall_apply f x) |
| 175 |
_ |
| 176 |
(eq_sym |
| 177 |
(flattenT_apply_unapply (fun _ => X) _)) |
| 178 |
= @flatten_forall_apply_of_unapply t X f x. |
| 179 |
Proof. |
| 180 |
induction t as [|A B IHt]; simpl in *. |
| 181 |
{ destruct x; simpl; reflexivity. } |
| 182 |
{ specialize (IHt (projT1 x) (f (projT1 x)) (projT2 x)). |
| 183 |
etransitivity; [ clear IHt | exact IHt ]. |
| 184 |
repeat apply f_equal. |
| 185 |
etransitivity; [ | apply concat_1p ]. |
| 186 |
apply f_equal2; [ | reflexivity ]. |
| 187 |
apply ap_const. } |
| 188 |
Defined. |
| 189 |
End Telescope. |