| 1 |
From elpi Require Import tc. |
| 2 |
|
| 3 |
Set TC NameShortPath. |
| 4 |
|
| 5 |
Module FO_prod. Section XX. |
| 6 |
Context (A B : Type) (y : B) (Q : A -> Prop). |
| 7 |
|
| 8 |
Class Ccc (i : Prop). |
| 9 |
Global Instance i P : Ccc (forall (x: A), P x y). Qed. |
| 10 |
|
| 11 |
|
| 12 |
Goal forall (P : nat -> A -> B -> Prop), Ccc (forall x, P 0 x y). |
| 13 |
apply _. |
| 14 |
Qed. |
| 15 |
End XX. End FO_prod. |
| 16 |
|
| 17 |
|
| 18 |
Module FO_app. |
| 19 |
|
| 20 |
Class nice_predicate {T : Type} (P : T -> Prop). |
| 21 |
|
| 22 |
Instance partial_app: forall (T : Type) (P : T -> T -> Prop), forall x, nice_predicate (P x). Qed. |
| 23 |
|
| 24 |
(* |
| 25 |
Unification is done between `p 0 x` and `P X` (The latter is not in PF) |
| 26 |
The former's elpi representation is `app [p, {{0}}, x]` while the latter is `P t p x (X t p x)` |
| 27 |
- `t` stands for T : Type |
| 28 |
- `P` is the unif variable `P` in partial_app |
| 29 |
- `X` is the unif variable `x` in partial_app |
| 30 |
We are outside the pattern fragment. |
| 31 |
The heuristics splits the arguments of `P` into `[t, p, x]` and `[(X t p x)]`, |
| 32 |
where `[t,p,x]` is the longest prefix in PF and `(X t p x)` is the remaining |
| 33 |
tail. We call the former PF and the latter NPF |
| 34 |
Len N the length of NPF and M the length of `[p, {{0}}, x]`, |
| 35 |
then we split `[p, {{0}}, x]` at position `M - N`. We obtain the sublists: |
| 36 |
`[p, {{0}}]` and `[x]`. We then unify `[x]` with `[(X t p x)]`. |
| 37 |
Let `L` the concatenation of `PF` and `NPF`, then the head P of the elpi unification |
| 38 |
variable is obtained by adding 4 lambda abstraction (the length of `L`), |
| 39 |
and for each abstraction `x` at depth `i` we add the local clause `copy L.(i) x`. |
| 40 |
The final result is `P = (x\y\z\w\ app[y, {{0}}, w])` |
| 41 |
*) |
| 42 |
Lemma ex1 (T : Type) (p : nat -> T -> T -> Prop) (x : T) : nice_predicate (p 0 x). |
| 43 |
apply _. |
| 44 |
Show Proof. |
| 45 |
Unshelve. |
| 46 |
Defined. |
| 47 |
|
| 48 |
Check eq_refl : ex1 = fun T p x => @partial_app T (p 0) x. |
| 49 |
(* Check eq_refl : ex1 = fun T p x => @partial_app T (fun _ => p 0 x) x. *) |
| 50 |
|
| 51 |
Lemma ex2 (T : Type) (p : nat -> T -> T -> Prop) y : nice_predicate (fun x => p 0 y x). |
| 52 |
apply _. |
| 53 |
Unshelve. |
| 54 |
(* auto. *) |
| 55 |
Defined. |
| 56 |
Check eq_refl : ex2 = fun T p y => @partial_app T (p 0) y. |
| 57 |
(* Check eq_refl : ex2 = fun T p y => @partial_app T (fun _ => p 0 y) y. *) |
| 58 |
|
| 59 |
Elpi TC Solver Override TC.Solver None. |
| 60 |
|
| 61 |
Lemma ex3 (T : Type) (p : nat -> T -> T -> Prop) y : nice_predicate (fun x => p 0 x y). |
| 62 |
Fail apply _. (* Coq KO *) |
| 63 |
Fail apply partial_app. (* Coq KO *) |
| 64 |
apply (@partial_app T (fun a b => p 0 b a) y). |
| 65 |
Abort. |
| 66 |
|
| 67 |
Lemma ex4 (T : Type) (p : nat -> T -> T -> Prop) y : nice_predicate (fun x => p 0 y x). |
| 68 |
Fail apply _. (* Coq KO *) |
| 69 |
Succeed apply partial_app. (* Coq eta! *) |
| 70 |
apply (@partial_app T (p 0) y). |
| 71 |
Abort. |
| 72 |
|
| 73 |
End FO_app. |
| 74 |
|
| 75 |
Elpi TC Solver Override TC.Solver All. |
| 76 |
|
| 77 |
Module FO_app1. |
| 78 |
|
| 79 |
Class Singleton (B: Type). |
| 80 |
Class Singleton1 (B: Type). |
| 81 |
|
| 82 |
Instance s M: (forall A : Type, Singleton1 (M A)) -> forall A : Type, Singleton (M A). Qed. |
| 83 |
|
| 84 |
Goal forall M, (forall A : Type, Singleton1 (M A)) -> forall A : Type, Singleton (M A). |
| 85 |
apply _. |
| 86 |
(* Unshelve. *) |
| 87 |
(* apply nat. *) |
| 88 |
Qed. |
| 89 |
|
| 90 |
End FO_app1. |
| 91 |
|
| 92 |
Module FO_app2. Section XX. |
| 93 |
|
| 94 |
Context (A B : Type). |
| 95 |
|
| 96 |
Class Functional (B: Type). |
| 97 |
|
| 98 |
Instance s1 F: Functional (F B) -> Functional (F B) -> Functional (F A). Qed. |
| 99 |
|
| 100 |
|
| 101 |
Definition f (x : Type) := Type. |
| 102 |
Context (H : Functional (f B)). |
| 103 |
|
| 104 |
Goal Functional (f A). |
| 105 |
apply _. |
| 106 |
Abort. |
| 107 |
|
| 108 |
End XX. End FO_app2. |
| 109 |
|
| 110 |
Module FO_app3. |
| 111 |
Definition X := Type -> Type. |
| 112 |
Axiom f : X. |
| 113 |
Class C (I : Type -> Type). |
| 114 |
|
| 115 |
Instance I : C (fun _ => f nat). Qed. |
| 116 |
|
| 117 |
Goal exists (R : Type -> Type) , forall (T:Type), C (fun x => R T) /\ R bool = f nat. |
| 118 |
eexists. |
| 119 |
intros. |
| 120 |
split. |
| 121 |
(* Here we commit the only existing solution for R, that is, |
| 122 |
R := fun _ => f nat, |
| 123 |
note that R does not see T *) |
| 124 |
apply _. |
| 125 |
reflexivity. |
| 126 |
Qed. |
| 127 |
|
| 128 |
Goal exists (R : Type -> Type) , C (fun x => R nat) /\ R bool = f nat. |
| 129 |
eexists. |
| 130 |
split. |
| 131 |
(* Here there is no mgu: there are in fact two solutions for R |
| 132 |
1. R := fun _ => f nat |
| 133 |
2. R := fun x => f x == f, |
| 134 |
in our case we commit the second *) |
| 135 |
apply _. |
| 136 |
Show Proof. |
| 137 |
Fail reflexivity. |
| 138 |
Abort. |
| 139 |
(* ============= We restart and try the good sol ============= *) |
| 140 |
|
| 141 |
Goal exists (R : Type -> Type) , C (fun x => R nat) /\ R bool = f nat. |
| 142 |
exists (fun x => f nat). |
| 143 |
split. |
| 144 |
apply _. |
| 145 |
reflexivity. |
| 146 |
Qed. |
| 147 |
|
| 148 |
Goal exists (R : Type -> Type) , C (fun x => R unit) /\ R bool = f nat. |
| 149 |
eexists. |
| 150 |
(* Here we fail, even though there exists the solution R := fun _ => f nat *) |
| 151 |
Fail apply _. |
| 152 |
Unshelve. |
| 153 |
2:{ refine (fun x => f nat). } |
| 154 |
split. |
| 155 |
apply _. |
| 156 |
reflexivity. |
| 157 |
Qed. |
| 158 |
|
| 159 |
Goal exists (R : Type -> Type) , C (fun x => R nat) /\ R bool = f bool. |
| 160 |
eexists. |
| 161 |
split. |
| 162 |
apply _. |
| 163 |
reflexivity. |
| 164 |
Qed. |
| 165 |
End FO_app3. |
| 166 |
|
| 167 |
Module HO_PF. |
| 168 |
|
| 169 |
Class Extensionality (T : Type). |
| 170 |
|
| 171 |
Instance fun_1 (A1 : Type) (A2 : A1 -> Type) : Extensionality (forall a : A1, A2 a). Qed. |
| 172 |
|
| 173 |
Lemma ex1 : Extensionality (nat -> nat). apply _. Defined. |
| 174 |
Check eq_refl : ex1 = @fun_1 nat (fun _ => nat). |
| 175 |
|
| 176 |
Lemma ex2 : Extensionality (forall x : nat, x = x + 1). apply _. Defined. |
| 177 |
Check eq_refl : ex2 = @fun_1 nat (fun a => a = a + 1). |
| 178 |
|
| 179 |
Axiom odd : nat -> Type. |
| 180 |
|
| 181 |
Lemma ex3 : Extensionality (forall x : nat, odd x). apply _. Defined. |
| 182 |
Goal ex3 = ex3. unfold ex3. match goal with |- @fun_1 nat odd = _ => idtac end. reflexivity. Abort. |
| 183 |
|
| 184 |
(* Instance for multiple lambdas *) |
| 185 |
Instance fun_2 (A1 : Type) (A2 : A1 -> A1 -> Type) : Extensionality (forall a b : A1, A2 b a). Qed. |
| 186 |
Lemma ex4 : Extensionality (nat -> nat -> nat). apply _. Qed. |
| 187 |
|
| 188 |
End HO_PF. |
| 189 |
|
| 190 |
Module HO_PF1. |
| 191 |
Parameter A : Type. |
| 192 |
Class Decision (P : Type). |
| 193 |
(* Global Hint Mode Decision ! : typeclass_instances. *) |
| 194 |
|
| 195 |
Section sol_in_hyp. |
| 196 |
Goal forall (P1: A -> Prop), |
| 197 |
exists (P : A -> A -> A -> Prop), forall z y , (forall x, Decision (P1 x)) |
| 198 |
-> forall x, Decision (P z y x). |
| 199 |
Proof. |
| 200 |
eexists; intros. |
| 201 |
Elpi Bound Steps 30000. |
| 202 |
Set Typeclasses Debug. |
| 203 |
apply _. |
| 204 |
Unshelve. |
| 205 |
auto. |
| 206 |
Qed. |
| 207 |
End sol_in_hyp. |
| 208 |
|
| 209 |
|
| 210 |
Class Exists (P : A -> Type) (l : A). |
| 211 |
Instance Exists_dec (P : A -> Type): (forall x, Decision (P x)) -> forall l, Decision (Exists P l). Qed. |
| 212 |
|
| 213 |
Section test. |
| 214 |
|
| 215 |
Goal forall P (l:A) , Decision (Exists P l). |
| 216 |
Proof. |
| 217 |
intros. |
| 218 |
Fail apply _. (* We fail without infinite loop thanks to ho-links *) |
| 219 |
Abort. |
| 220 |
|
| 221 |
End test. |
| 222 |
|
| 223 |
Goal forall (P1: A -> Prop) l, |
| 224 |
exists (P : A -> A -> A -> Prop), forall z y , (forall x, Decision (P1 x)) |
| 225 |
-> Decision (Exists (P z y) l) /\ P z y y = P1 z. |
| 226 |
Proof. |
| 227 |
eexists; intros. |
| 228 |
split. |
| 229 |
(* forall x : A, Decision (P1 x) = forall x : A, Decision ((?P z y) x) *) |
| 230 |
(* x |- Decision (P1 ?x) = Decision ((?P z y) x) *) |
| 231 |
(* We take the most general solution for P, it picks P = (fun a b c => P1 ?x) *) |
| 232 |
apply _. |
| 233 |
simpl. |
| 234 |
(* Reflexivity fix ?x = a hence (fun a b c => P1 a) z y y = P1 z is solvable *) |
| 235 |
reflexivity. |
| 236 |
Qed. |
| 237 |
|
| 238 |
Lemma ho_in_coq (P1: A -> Prop) l: |
| 239 |
exists (P : A -> A -> A -> Prop), forall z y , (forall x, Decision (P1 x)) |
| 240 |
-> Decision (Exists (P z y) l) /\ P z y y = P1 z. |
| 241 |
Proof. |
| 242 |
Elpi TC Solver Override TC.Solver None. |
| 243 |
eexists; intros. |
| 244 |
(* epose (H _). *) |
| 245 |
(* clearbody d. *) |
| 246 |
(* clear H. *) |
| 247 |
split. |
| 248 |
(* Print HintDb typeclass_instances. *) |
| 249 |
(* Set Elpi Typeclasses Debug. *) |
| 250 |
(* Coq doesn't give the most general solution for P, it picks P = (fun _ _ x => P1 x) *) |
| 251 |
apply _. |
| 252 |
Fail reflexivity. |
| 253 |
Abort. |
| 254 |
Elpi TC Solver Override TC.Solver All. |
| 255 |
|
| 256 |
Section test. |
| 257 |
|
| 258 |
Context (P1: Type -> Prop). |
| 259 |
Context (H : Decision (P1 nat)). |
| 260 |
Goal exists P, forall (x y:A) , Decision (P x y). |
| 261 |
Proof. |
| 262 |
eexists; intros. |
| 263 |
apply _. |
| 264 |
Abort. |
| 265 |
|
| 266 |
End test. |
| 267 |
|
| 268 |
End HO_PF1. |
| 269 |
|
| 270 |
Section HO_PF2. |
| 271 |
Class cl1 (i : Type). |
| 272 |
Class cl2 {i : Type} (y : cl1 i). |
| 273 |
Class cl3 {i : Type} (y : cl1 i). |
| 274 |
Instance i1 : |
| 275 |
forall (H : forall x, cl1 x), |
| 276 |
cl2 (H nat) -> cl3 (H bool). Qed. |
| 277 |
|
| 278 |
Goal forall (H : forall x, cl1 x), |
| 279 |
cl2 (H nat) -> cl3 (H bool). |
| 280 |
Proof. |
| 281 |
apply _. |
| 282 |
Qed. |
| 283 |
|
| 284 |
Goal forall (H : forall x, cl1 x), |
| 285 |
cl2 (H nat) -> exists x (i_cl1: cl1 x), cl3 i_cl1. |
| 286 |
Proof. |
| 287 |
intros. |
| 288 |
do 2 eexists. |
| 289 |
apply _. |
| 290 |
Qed. |
| 291 |
End HO_PF2. |
| 292 |
|
| 293 |
Module D. |
| 294 |
|
| 295 |
Class C1 (T : Type -> Type) (i: forall x, T x). |
| 296 |
|
| 297 |
Class D. |
| 298 |
Instance I : forall (T : Type -> Type) (H : forall x, T x), |
| 299 |
C1 T (fun x => H x) -> D . Qed. |
| 300 |
|
| 301 |
Instance J: forall (T : Type -> Type) (H : forall x, T x), C1 T H. Qed. |
| 302 |
|
| 303 |
Goal D. |
| 304 |
intros. |
| 305 |
apply _. |
| 306 |
Unshelve. |
| 307 |
all: try apply 3; try apply nat. |
| 308 |
Qed. |
| 309 |
|
| 310 |
End D. |
| 311 |
|
| 312 |
|
| 313 |
Module F. |
| 314 |
|
| 315 |
Class C1 (T : Type -> Type) (i: forall x, T x). |
| 316 |
|
| 317 |
Class D. |
| 318 |
Instance I : forall (T : Type -> Type) (H : forall x, T x), |
| 319 |
C1 T (fun x => H x) -> D . Qed. |
| 320 |
|
| 321 |
Goal forall (T : Type -> Type) (H : forall x, T x), C1 T H -> D. |
| 322 |
intros. |
| 323 |
Set Typeclasses Debug. |
| 324 |
Set Debug "tactic-unification". |
| 325 |
Elpi TC Solver Override TC.Solver None. |
| 326 |
Fail apply _. (* Here coq's unfication algorithm fails: |
| 327 |
it is not able to solce H =~ fun x => ?H x, |
| 328 |
even though it is sufficient to eta-expand the lhs *) |
| 329 |
Elpi TC Solver Override TC.Solver All. |
| 330 |
apply _. |
| 331 |
Qed. |
| 332 |
|
| 333 |
End F. |
| 334 |
|
| 335 |
Module F'. |
| 336 |
|
| 337 |
Class C2 (T : Type -> Type) (i: forall x, T x). |
| 338 |
|
| 339 |
Class D. |
| 340 |
Instance I : forall (T : Type -> Type) (H : forall x, T x), |
| 341 |
C2 T H -> D . Qed. |
| 342 |
|
| 343 |
Goal forall (T : Type -> Type) (H : forall x, T x), C2 T (fun x => H x) -> D. |
| 344 |
intros. |
| 345 |
Set Debug "tactic-unification". |
| 346 |
Elpi TC Solver Override TC.Solver None. |
| 347 |
apply _. (* Here coq succeds: it is able to solce ?H =~ fun x => H x *) |
| 348 |
Abort. |
| 349 |
|
| 350 |
Goal forall (T : Type -> Type) (H : forall x, T x), C2 T (fun x => H x) -> D. |
| 351 |
Elpi TC Solver Override TC.Solver None. |
| 352 |
apply _. |
| 353 |
Qed. |
| 354 |
|
| 355 |
End F'. |
| 356 |
|
| 357 |
Module E. |
| 358 |
Class C3 (i : nat -> nat -> nat). |
| 359 |
Instance I : C3 (plus). Qed. |
| 360 |
|
| 361 |
Class D3 (i : Prop). |
| 362 |
|
| 363 |
Instance I2 (F : nat -> nat -> nat) : C3 F -> D3 (forall x y, F x y = F y x) . Qed. |
| 364 |
Goal D3 (forall n m, n + m = m + n). |
| 365 |
apply _. |
| 366 |
Qed. |
| 367 |
End E. |