| 1 |
From Corelib Require Import Setoid ssreflect. |
| 2 |
Set Default Proof Using "Type". |
| 3 |
|
| 4 |
Local Set Universe Polymorphism. |
| 5 |
|
| 6 |
(** Telescopes *) |
| 7 |
Inductive tele : Type := |
| 8 |
| TeleO : tele |
| 9 |
| TeleS {X} (binder : X -> tele) : tele. |
| 10 |
|
| 11 |
Arguments TeleS {_} _. |
| 12 |
|
| 13 |
(** The telescope version of Coq's function type *) |
| 14 |
Fixpoint tele_fun (TT : tele) (T : Type) : Type := |
| 15 |
match TT with |
| 16 |
| TeleO => T |
| 17 |
| TeleS b => forall x, tele_fun (b x) T |
| 18 |
end. |
| 19 |
|
| 20 |
Notation "TT -t> A" := |
| 21 |
(tele_fun TT A) (at level 99, A at level 200, right associativity). |
| 22 |
|
| 23 |
(** A sigma-like type for an "element" of a telescope, i.e. the data it |
| 24 |
takes to get a [T] from a [TT -t> T]. *) |
| 25 |
Inductive tele_arg : tele -> Type := |
| 26 |
| TargO : tele_arg TeleO |
| 27 |
(* the [x] is the only relevant data here *) |
| 28 |
| TargS {X} {binder} (x : X) : tele_arg (binder x) -> tele_arg (TeleS binder). |
| 29 |
|
| 30 |
Definition tele_app {TT : tele} {T} (f : TT -t> T) : tele_arg TT -> T := |
| 31 |
fun a => (fix rec {TT} (a : tele_arg TT) : (TT -t> T) -> T := |
| 32 |
match a in tele_arg TT return (TT -t> T) -> T with |
| 33 |
| TargO => fun t : T => t |
| 34 |
| TargS x a => fun f => rec a (f x) |
| 35 |
end) TT a f. |
| 36 |
Arguments tele_app {!_ _} _ !_ /. |
| 37 |
|
| 38 |
Coercion tele_arg : tele >-> Sortclass. |
| 39 |
Coercion tele_app : tele_fun >-> Funclass. |
| 40 |
|
| 41 |
(** Inversion lemma for [tele_arg] *) |
| 42 |
Lemma tele_arg_inv {TT : tele} (a : TT) : |
| 43 |
match TT as TT return TT -> Prop with |
| 44 |
| TeleO => fun a => a = TargO |
| 45 |
| TeleS f => fun a => exists x a', a = TargS x a' |
| 46 |
end a. |
| 47 |
Proof. induction a; eauto. Qed. |
| 48 |
Lemma tele_arg_O_inv (a : TeleO) : a = TargO. |
| 49 |
Proof. exact (tele_arg_inv a). Qed. |
| 50 |
Lemma tele_arg_S_inv {X} {f : X -> tele} (a : TeleS f) : |
| 51 |
exists x a', a = TargS x a'. |
| 52 |
Proof. exact (tele_arg_inv a). Qed. |
| 53 |
|
| 54 |
(** Operate below [tele_fun]s with argument telescope [TT]. *) |
| 55 |
Fixpoint tele_bind {U} {TT : tele} : (TT -> U) -> TT -t> U := |
| 56 |
match TT as TT return (TT -> U) -> TT -t> U with |
| 57 |
| TeleO => fun F => F TargO |
| 58 |
| @TeleS X b => fun (F : TeleS b -> U) (x : X) => (* b x -t> U *) |
| 59 |
tele_bind (fun a => F (TargS x a)) |
| 60 |
end. |
| 61 |
Arguments tele_bind {_ !_} _ /. |
| 62 |
|
| 63 |
(* Show that tele_app ∘ tele_bind is the identity. *) |
| 64 |
Lemma tele_app_bind {U} {TT : tele} (f : TT -> U) x : |
| 65 |
(tele_app (tele_bind f)) x = f x. |
| 66 |
Proof. |
| 67 |
induction TT as [|X b IH]; simpl in *. |
| 68 |
- rewrite (tele_arg_O_inv x). auto. |
| 69 |
- destruct (tele_arg_S_inv x) as [x' [a' ->]]. simpl. |
| 70 |
rewrite IH. auto. |
| 71 |
Qed. |
| 72 |
|
| 73 |
(** Notation-compatible telescope mapping *) |
| 74 |
(* This adds (tele_app ∘ tele_bind), which is an identity function, around every |
| 75 |
binder so that, after simplifying, this matches the way we typically write |
| 76 |
notations involving telescopes. *) |
| 77 |
Notation "'λ..' x .. y , e" := |
| 78 |
(tele_app (tele_bind (fun x => .. (tele_app (tele_bind (fun y => e))) .. ))) |
| 79 |
(at level 200, x binder, y binder, right associativity, |
| 80 |
format "'[ ' 'λ..' x .. y ']' , e"). |
| 81 |
|
| 82 |
(* The testcase *) |
| 83 |
Lemma test {TA TB : tele} {X} (α' β' γ' : X -> Prop) (Φ : TA -> TB -> Prop) x' : |
| 84 |
(forall P Q, ((P /\ Q) = Q) * ((P -> Q) = Q)) -> |
| 85 |
forall a b, Φ a b = tele_bind (fun x : TA => tele_bind (fun y : TB => β' x' /\ (γ' x' -> Φ x y))) a b. |
| 86 |
Proof. |
| 87 |
intros cheat a b. |
| 88 |
rewrite !tele_app_bind. |
| 89 |
by rewrite !cheat. |
| 90 |
Qed. |