| 1 |
From Coq Require Import Relation_Definitions Morphisms. |
| 2 |
Require Export Crypto.Util.FixCoqMistakes. |
| 3 |
|
| 4 |
Module Export Telescope. |
| 5 |
Inductive Telescope := bottom | tele (A : Type) (B : A -> Telescope). |
| 6 |
|
| 7 |
Fixpoint flattenT (t : Telescope) (X : Type) |
| 8 |
:= match t with |
| 9 |
| bottom => X |
| 10 |
| tele A B => forall a : A, flattenT (B a) X |
| 11 |
end. |
| 12 |
|
| 13 |
Global Arguments tele : clear implicits. |
| 14 |
|
| 15 |
Fixpoint flattenT_sig (t : Telescope) |
| 16 |
:= match t return Type with |
| 17 |
| bottom => unit |
| 18 |
| tele A B => { a : A & flattenT_sig (B a) } |
| 19 |
end. |
| 20 |
|
| 21 |
Section R. |
| 22 |
Context {X : Type} (R : relation X). |
| 23 |
|
| 24 |
Fixpoint flattenT_R_relation {t : Telescope} : relation (flattenT t X) |
| 25 |
:= match t return relation (flattenT t X) with |
| 26 |
| bottom => R |
| 27 |
| tele A B => forall_relation (fun a => flattenT_R_relation) |
| 28 |
end. |
| 29 |
End R. |
| 30 |
|
| 31 |
Definition flattenT_eq {X : Type} : forall {t : Telescope}, relation (flattenT t X) |
| 32 |
:= Eval unfold flattenT_R_relation, forall_relation in @flattenT_R_relation X eq. |
| 33 |
|
| 34 |
Fixpoint flattenT_apply {t : Telescope} {X : Type} |
| 35 |
: flattenT t X -> flattenT_sig t -> X |
| 36 |
:= match t return flattenT t X -> flattenT_sig t -> X with |
| 37 |
| bottom => fun x _ => x |
| 38 |
| tele A B => fun f p => flattenT_apply (f (projT1 p)) (projT2 p) |
| 39 |
end. |
| 40 |
|
| 41 |
Fixpoint flattenT_unapply {t : Telescope} {X : Type} |
| 42 |
: (flattenT_sig t -> X) -> flattenT t X |
| 43 |
:= match t return (flattenT_sig t -> X) -> flattenT t X with |
| 44 |
| bottom => fun f => f tt |
| 45 |
| tele A B => fun f x => flattenT_unapply (fun p => f (existT (fun x' => flattenT_sig (B x')) x p)) |
| 46 |
end. |
| 47 |
|
| 48 |
Fixpoint Telescope_append_nondep (t t' : Telescope) |
| 49 |
: Telescope |
| 50 |
:= match t with |
| 51 |
| bottom => t' |
| 52 |
| tele A B => @tele A (fun a => Telescope_append_nondep (B a) t') |
| 53 |
end. |
| 54 |
|
| 55 |
Global Arguments Telescope_append_nondep : clear implicits. |
| 56 |
|
| 57 |
Fixpoint Telescope_append (t : Telescope) |
| 58 |
: flattenT t Type -> Telescope |
| 59 |
:= match t return flattenT t _ -> _ with |
| 60 |
| bottom => fun X => @tele X (fun _ => bottom) |
| 61 |
| tele A B => fun X => @tele A (fun a => Telescope_append (B a) (X a)) |
| 62 |
end. |
| 63 |
|
| 64 |
Global Arguments Telescope_append : clear implicits. |
| 65 |
|
| 66 |
Fixpoint flatten_forall {t : Telescope} : flattenT t Type -> Type |
| 67 |
:= match t return flattenT t Type -> Type with |
| 68 |
| bottom => fun P => P |
| 69 |
| tele A B => fun P => forall a : A, flatten_forall (P a) |
| 70 |
end. |
| 71 |
|
| 72 |
Fixpoint flatten_forall_eq_rect {t : Telescope} |
| 73 |
: forall {P Q : flattenT t Type}, |
| 74 |
flattenT_eq P Q |
| 75 |
-> flatten_forall P |
| 76 |
-> flatten_forall Q |
| 77 |
:= match t return forall {P Q : flattenT t Type}, |
| 78 |
flattenT_eq P Q |
| 79 |
-> flatten_forall P |
| 80 |
-> flatten_forall Q |
| 81 |
with |
| 82 |
| bottom => fun P Q H k => eq_rect _ (fun x => x) k _ H |
| 83 |
| tele A B => fun P Q H k a => @flatten_forall_eq_rect (B a) (P a) (Q a) (H a) (k a) |
| 84 |
end. |
| 85 |
|
| 86 |
Fixpoint flatten_forall_eq_relation {t : Telescope} |
| 87 |
: forall {P : flattenT t Type}, |
| 88 |
relation (flatten_forall P) |
| 89 |
:= match t |
| 90 |
return forall {P : flattenT t _}, |
| 91 |
relation (flatten_forall P) |
| 92 |
with |
| 93 |
| bottom => fun P => eq |
| 94 |
| tele A B => fun P => forall_relation (fun a : A => @flatten_forall_eq_relation (B a) (P a)) |
| 95 |
end. |
| 96 |
|
| 97 |
Definition flatten_forall_eq {t : Telescope} |
| 98 |
: forall {P : flattenT t Type} |
| 99 |
(f g : flatten_forall P), |
| 100 |
Prop |
| 101 |
:= Eval unfold flatten_forall_eq_relation, forall_relation in @flatten_forall_eq_relation t. |
| 102 |
|
| 103 |
Fixpoint flatten_forall_eq_relation_with_assumption {t : Telescope} |
| 104 |
: forall {P : flattenT t Type} |
| 105 |
(Q : flattenT t Type), |
| 106 |
relation (flatten_forall P) |
| 107 |
:= match t |
| 108 |
return forall {P : flattenT t _} |
| 109 |
(Q : flattenT t Type), |
| 110 |
relation (flatten_forall P) |
| 111 |
with |
| 112 |
| bottom => fun P Q => fun x y => Q -> x = y |
| 113 |
| tele A B => fun P Q => forall_relation (fun a : A => @flatten_forall_eq_relation_with_assumption (B a) (P a) (Q a)) |
| 114 |
end. |
| 115 |
|
| 116 |
Definition flatten_forall_eq_with_assumption {t : Telescope} |
| 117 |
: forall {P : flattenT t Type} |
| 118 |
(Q : flattenT t Type) |
| 119 |
(f g : flatten_forall P), |
| 120 |
Prop |
| 121 |
:= Eval unfold flatten_forall_eq_relation, forall_relation in @flatten_forall_eq_relation_with_assumption t. |
| 122 |
|
| 123 |
Fixpoint flatten_forall_apply {t : Telescope} |
| 124 |
: forall {P : flattenT t Type}, |
| 125 |
flatten_forall P |
| 126 |
-> forall x : flattenT_sig t, |
| 127 |
flattenT_apply P x |
| 128 |
:= match t |
| 129 |
return forall {P : flattenT t Type}, |
| 130 |
flatten_forall P |
| 131 |
-> forall x : flattenT_sig t, |
| 132 |
flattenT_apply P x |
| 133 |
with |
| 134 |
| bottom => fun X x _ => x |
| 135 |
| tele A B => fun P f x => @flatten_forall_apply (B (projT1 x)) _ (f (projT1 x)) (projT2 x) |
| 136 |
end. |
| 137 |
|
| 138 |
Fixpoint flatten_forall_unapply {t : Telescope} |
| 139 |
: forall {P : flattenT_sig t -> Type}, |
| 140 |
(forall x : flattenT_sig t, P x) |
| 141 |
-> flatten_forall (flattenT_unapply P) |
| 142 |
:= match t |
| 143 |
return forall {P : flattenT_sig t -> Type}, |
| 144 |
(forall x : flattenT_sig t, P x) |
| 145 |
-> flatten_forall (flattenT_unapply P) |
| 146 |
with |
| 147 |
| bottom => fun X x => x tt |
| 148 |
| tele A B => fun P f x => @flatten_forall_unapply (B x) _ (fun x' => f _) |
| 149 |
end. |
| 150 |
|
| 151 |
Fixpoint flatten_forall_apply_of_unapply {t X} {struct t} |
| 152 |
: forall (f : flatten_forall |
| 153 |
(flattenT_unapply |
| 154 |
(fun _ : flattenT_sig t => X))) |
| 155 |
(x : flattenT_sig t), |
| 156 |
X |
| 157 |
:= match t return forall (f : flatten_forall |
| 158 |
(flattenT_unapply |
| 159 |
(fun _ : flattenT_sig t => X))) |
| 160 |
(x : flattenT_sig t), |
| 161 |
X |
| 162 |
with |
| 163 |
| bottom => fun x _ => x |
| 164 |
| tele A B => fun f x => @flatten_forall_apply_of_unapply (B (projT1 x)) X (f (projT1 x)) (projT2 x) |
| 165 |
end. |
| 166 |
|
| 167 |
Fixpoint flatten_append_forall {t : Telescope} |
| 168 |
: forall {t' : flattenT t Type}, |
| 169 |
flattenT (Telescope_append t t') Type -> flatten_forall t' -> Type |
| 170 |
:= match t return forall {t' : flattenT t _}, |
| 171 |
flattenT (Telescope_append t t') Type |
| 172 |
-> flatten_forall t' |
| 173 |
-> Type |
| 174 |
with |
| 175 |
| bottom => fun t' P v => P v |
| 176 |
| tele A B => fun t' P v => forall a, @flatten_append_forall (B a) (t' a) (P a) (v a) |
| 177 |
end. |
| 178 |
End Telescope. |