Debian Codegraph @

apps/tc/tests/test.v

raw
1 From elpi Require Import tc.
2
3 Section test_max_arity.
4 Elpi Query TC.Solver lp:{{
5 T = (c1\ prod `c` _ c2 \
6 prod `_`
7 (prod `a` _ c3 \
8 app [global _, app [c1, c3], c2]) c3 \
9 app [global _, c1, c2]),
10 pi x\ tc.precomp.instance.get-range-arity x _ (T x) (tc.r-ar z (s z)).
11 }}.
12 End test_max_arity.
13
14 Module test_link_eta_generation.
15 Class c (T : Type -> Type -> Type -> Type).
16 Class d (T : Type) (T : Type -> Type -> Type -> Type).
17 Elpi Accumulate TC.Solver lp:{{
18 :after "0"
19 tc.compile.instance.compile-conclusion _ (app [H|_]) _ _ _ Premises _ :-
20 H = {{test_link_eta_generation.c}}, !,
21 std.assert! (Premises = [do [tc.link.eta _ _] | _]) "[TC] Wrong number of eta links",
22 coq.say "Good padding from here",
23 fail.
24 }}.
25 Elpi Query TC.Solver lp:{{
26 ToCompile = {{forall (T : Type -> Type -> Type -> Type), (forall (a: Type), d a T) -> c T}},
27 not (tc.compile.instance ToCompile _ _).
28 }}.
29 End test_link_eta_generation.
30
31 Module simpleHO.
32 Class A (t : nat -> nat) (t' : Type).
33 Class B (t : nat) (t' : Type).
34 Instance I1: forall F c, (forall a, B (F a) c) -> A F c. Qed.
35 Instance I2 : B 3 bool. Qed.
36 Goal exists x, A x bool.
37 Proof.
38 eexists.
39 apply _.
40 Qed.
41 End simpleHO.
42
43 Module HO_1.
44 Axiom (f : bool -> unit -> nat -> nat).
45
46 Class A (t : bool -> unit -> nat -> nat).
47 Class B (t : unit -> nat -> nat).
48 Class C (t : nat).
49
50 Instance I1: forall a b, (C (f true a b)). Qed.
51 Instance I2: forall F, (forall x y, C (F x y)) -> B F. Qed.
52 Instance I3: forall F, (forall x, B (F x)) -> A F. Qed.
53
54 Goal B (fun x y => f true x y).
55 apply _.
56 Qed.
57
58 Goal exists x, A x.
59 eexists.
60 Time apply _.
61 Unshelve.
62 (* Note: here we find a most general solution than Coq's one *)
63 apply tt.
64 apply 3.
65 Qed.
66
67 End HO_1.
68
69 Module HO_2.
70 Axiom f : Type -> Type -> Type.
71
72 Class A (t : Type -> Type -> Type) (t : Type -> Type -> Type).
73
74 Instance I: forall f, A f (fun x y => f y x). Qed.
75
76 Goal A f (fun x y => f y x).
77 apply _.
78 Qed.
79 End HO_2.
80
81 Module HO_3.
82 Axiom (f : nat -> nat -> nat).
83
84 Class A (t : nat -> nat -> nat -> nat).
85 Class B (t : nat -> nat -> nat).
86 Class C (t : nat -> nat).
87
88 Instance I1: (C (f 3)). Qed.
89 Instance I2: forall F, (forall x, C (F x)) -> B F. Qed.
90 Instance I3: forall F, (forall x, B (F x)) -> A F. Qed.
91
92 Goal exists x,B x.
93 eexists.
94 apply _.
95 Qed.
96 End HO_3.
97
98
99 Module HO_4.
100 Axiom (f : nat -> nat -> nat -> nat).
101
102 Class A (t : nat -> nat -> nat -> nat -> nat -> nat).
103 Class B (t : nat -> nat -> nat -> nat -> nat).
104 Class C (t : nat -> nat -> nat).
105
106 Instance I1: C (fun x => f x 3). Qed.
107 Instance I2: forall F, (forall x y, C (F x y)) -> B (fun a b => F b a). Qed.
108 Instance I3: forall F, (forall x, B (F x)) -> A F. Qed.
109
110 Goal exists x, A x.
111 eexists.
112 apply _.
113 Qed.
114 End HO_4.
115
116
117 Module HO_swap.
118 Axiom (f : Type -> Type -> Type).
119 Elpi Query TC.Solver lp:{{
120 tc.link.eta.maybe-eta (fun `x` _ c0 \ fun `y` _ c1 \ A2 c1 c0),
121 tc.link.eta.maybe-eta (fun `x` _ c0 \ fun `y` _ c1 \ A2 (A c1) c0),
122 tc.link.eta.maybe-eta {{fun x y => f x y}}.
123 }}.
124
125 Class c1 (T : (Type -> Type -> Type)).
126 Class c2 (T : (Type -> Type -> Type)).
127
128 Elpi Query TC.Solver lp:{{
129 @pi-decl `x` {{Type -> Type}} f\ tc.precomp.instance.is-uvar f =>
130 sigma T\
131 tc.precomp.instance {{c1 (fun x y => lp:f y x)}} T N _ _,
132 std.assert! (T = app[{{c1}}, tc.maybe-eta-tm _ _]) "[TC] invalid precomp".
133 }}.
134
135 Instance a1 : forall (F : Type -> Type -> Type),
136 c2 (fun x y => F y x) -> c1 F. Qed.
137
138 Instance b1 : c2 f. Qed.
139
140 Goal c1 (fun x y => f y x).
141 apply _.
142 Qed.
143 End HO_swap.
144
145 Module HO_5.
146 Axiom (f : Type -> Type -> Type).
147
148 Class c1 (T : (Type -> Type -> Type)).
149 Class c2 (T : (Type -> Type -> Type)).
150 Class c3 (T : (Type -> Type -> Type)).
151
152 Instance a1 : forall (F : Type -> Type -> Type),
153 (c2 (fun x y => F y x) -> c3 (fun x y => F y x)) -> c1 F. Qed.
154
155 Instance a2 : c2 f -> c3 f. Qed.
156
157 Goal c1 (fun x y => f y x).
158 apply _.
159 Qed.
160 End HO_5.
161
162
163 Module HO_6.
164 Axiom (f : Type -> Type -> Type).
165
166 Class c1 (T : (Type -> Type -> Type)).
167 Class c2 (T : (Type -> Type -> Type)).
168 Class c3 (G : nat -> nat) (T : (Type -> Type -> Type)).
169
170 Instance a1 : forall (F : Type -> Type -> Type),
171 (c2 (fun x y => F y x) ->
172 forall G, c3 G (fun x y => F y x)) ->
173 c1 F.
174 Qed.
175
176 Instance a2 : forall F, c2 f -> c3 F f. Qed.
177
178 Goal c1 (fun x y => f y x).
179 apply _.
180 Qed.
181 End HO_6.
182
183 Module HO_7.
184 (* Here maybe-eta is in the goal, with a flexible head *)
185 Axiom f : Type -> Type -> Type.
186 Class c1 (T : Type -> Type -> Type) (T : Type -> Type -> Type).
187 Instance i1: c1 f (fun x y => f y x). Qed.
188
189 (*
190 TODO: decl M _ {{Type -> Type -> Type}} => coq.typecheck M Ty ok.
191 Ty is flex, I would like it to be (prod _ _ (x\ prod _ _ _)))
192 to make the following test succeed
193 Elpi Query TC.Solver lp:{{
194 decl M _ {{Type -> Type -> Type}} =>
195 tc.compile.goal {{c1 (fun x y => lp:M y x) lp:M}} G L,
196 std.length L Len,
197 std.assert! (Len = 5).
198 }}. *)
199
200 Goal exists M, c1 (fun x y => M y x) M.
201 eexists.
202 apply _.
203 Qed.
204 End HO_7.
205
206 Module HO_81.
207 Class c1 (T : Type).
208 Instance i1 F : c1 F. Qed.
209
210 Elpi Accumulate TC.Solver lp:{{
211 :before "compile-goal"
212 tc.compile.goal Goal _ _ :-
213 Goal = {{HO_81.c1 lp:_}}, !,
214 tc.precomp.goal Goal _ Vars, !,
215 tc.compile.goal.make-pairs Vars Pairs,
216 std.assert! (Pairs = []) "", fail.
217 }}.
218
219
220 Goal exists X, c1 X.
221 eexists.
222 (* Failure is good, since here we simply check that the number of
223 uvar-pair built by tc.precomp is zero. This is because the type
224 of ?X is Type (i.e. it has `arity` zero) *)
225 Fail apply _.
226 Abort.
227 End HO_81.
228
229 Module HO_8.
230 Class c1 (T : Type -> Type -> Type).
231 Instance i1 F : c1 (fun x => F x). Qed.
232
233 Goal exists X, c1 X.
234 eexists.
235 apply _.
236 Unshelve.
237 apply nat.
238 Qed.
239 End HO_8.
240
241 Module HO_9.
242 Axiom f : Type -> Type -> Type.
243
244 Class c1 (T : Type -> Type).
245 Instance i1 A: c1 (fun x => f (A x) (A x)). Qed.
246
247 Elpi Query TC.Solver lp:{{
248 pi F\ sigma T\ decl F `x` {{Type -> Type}} ==> tc.precomp.instance.is-uvar F ==>
249 tc.precomp.instance {{c1 (fun x => f (lp:F x) (lp:F x))}} T N _ _,
250 std.assert! (T = app [{{c1}}, tc.maybe-eta-tm _ _]) "Invalid precompilation".
251 }}.
252
253 Goal exists X, c1 X.
254 eexists.
255 (* TODO: here good solution, but universe problem!!! *)
256 eapply (i1 (fun x => _)).
257 (* apply _. *)
258 Unshelve.
259 auto.
260 Qed.
261 End HO_9.
262
263 Module HO_10.
264 Axiom f : Type -> Type -> Type.
265
266 Class c1 (T : Type -> Type -> Type).
267 Instance i1 A: c1 (fun x y => f (A x y) (A x y)). Qed.
268
269 (* Note: here interesting link-dedup *)
270 Goal exists X, c1 X.
271 eexists.
272 (* TODO: here good solution, but universe problem!!! *)
273 (* apply _. *)
274 eapply (i1 (fun _ _ => _)).
275 Unshelve.
276 auto.
277 Qed.
278 End HO_10.
279
280 Module HO_scope_check1.
281 Axiom f : Type -> (Type -> Type) -> Type.
282 Axiom g : Type -> Type -> Type.
283 Axiom a : Type.
284
285 Class c1 (T : Type -> Type).
286
287 Instance i1 : forall X, c1 (fun x => f x (fun y => g x (X y))). Qed.
288
289 Goal c1 (fun x => f x (g x)).
290 apply _.
291 Qed.
292
293 Elpi Query TC.Solver lp:{{
294 sigma X Q\ % To avoid printing in console
295 tc.build-query-from-goal {{c1 (fun x => f x lp:X)}} _ Q _,
296 (pi A L T\
297 tc.link.eta.scope-check (uvar _ L) (fun _ _ (x\ app [{{g}}|_] as T)) :- !,
298 std.assert! (not (prune A L, A = T)) "[TC] Should fail by Scope Check",
299 fail) =>
300 not Q.
301 }}.
302
303 (* Here fail on scope check *)
304 Goal exists X, c1 (fun x => f x X).
305 eexists.
306 Fail apply _.
307 Abort.
308 End HO_scope_check1.
309
310 Module beta_reduce_preprocess.
311 Axiom f : Type -> Type -> Type.
312
313 Module in_instance.
314 Class c1 (T:Type).
315 Instance i1 : c1 ((fun x y => f y x) nat bool). Qed.
316
317 Goal c1 (f bool nat).
318 apply _.
319 Qed.
320 End in_instance.
321
322 Module in_goal.
323 Class c1 (T : Type).
324 Instance i1 : c1 (f nat bool). Qed.
325 Goal c1 ((fun x y => f y x) bool nat).
326 apply _.
327 Qed.
328 End in_goal.
329 End beta_reduce_preprocess.
330
331 Module Llam_1.
332
333 Class A (i: nat -> nat).
334 Class B (i: nat -> nat).
335
336 Elpi Query TC.Solver lp:{{
337 @pi-decl `x` {{Type -> Type}} f\ tc.precomp.instance.is-uvar f =>
338 @pi-decl `x` {{Type -> Type}} g\ tc.precomp.instance.is-uvar g =>
339 sigma T\
340 tc.precomp.instance {{A (fun x => lp:f (lp:g x))}} T N _ _,
341 std.assert! (T = app[{{A}}, tc.maybe-eta-tm (fun _ _ (x\ tc.maybe-llam-tm _ _)) _]) "[TC] invalid precomp".
342 }}.
343
344 Instance I1: forall F G, B G -> A (fun x => F (G x)). Qed.
345 Instance I2: B (fun x => x). Qed.
346
347 (* HERE progress-llam-refine! *)
348 (* While back-chaining `I1`, the eta-link for `F (G x)` is triggered,
349 and the `llam-link` for `F (G x)` becomes `S =llam F (G x)`
350 the premise `B G` assigns `G` to the identity (thanks to I2),
351 this updates the `llam-link` to `S = llam F x`.
352 `F x` is in PF, and can safely be unfied to `S`.
353 The finaly substitution is therefore: I1 S (fun x => x) I2 *)
354 Goal A S.
355 apply _.
356 Qed.
357
358 End Llam_1.
359
360 Module Llam_2.
361 Axiom a : nat.
362
363 Class c1 (i: nat).
364 Class c2 (i: nat -> nat).
365
366 Instance I1: forall F, c2 F -> c1 (F a). Qed.
367 Instance I2: c2 (fun x => x). Qed.
368
369 (* HERE progress-rhs! *)
370 (* While back-chaining, the goal unify with I1.
371 `c2 F` is unified with `c2 (fun x => x)` due to I2.
372 F is now rigid can be beta-reduced to a *)
373 Goal c1 a.
374 (* Note : if, as coq, we force the llam link immediately, then apply _ fails *)
375 Fail apply _.
376 Abort.
377
378 Goal exists X, c1 X.
379 eexists.
380 apply _.
381 Qed.
382 End Llam_2.
383
384 Module Llam_3.
385 Axiom f: bool -> unit -> nat -> nat -> nat -> nat.
386 Class c1 (i : nat).
387 Class c2 (i: nat).
388 Class c3 (i: bool -> unit -> nat -> nat -> nat -> nat).
389 Instance I1 : forall (F: bool -> unit -> nat -> nat -> nat) G,
390 c3 G ->
391 (forall a b c d, c2 (G a b c d (F a b c d))) -> c1 0. Qed.
392
393 Instance I2 : c3 f. Qed.
394 Instance I3 a b c d F: c2 (f a b c d F). Qed.
395
396 Goal c1 0.
397 apply _.
398 Unshelve.
399 auto.
400 Qed.
401 End Llam_3.
402
403 Module Llam_4.
404 Axiom f : Type -> (Type -> Type) -> Type.
405 Axiom g : Type -> Type -> Type.
406 Axiom a : Type.
407
408 Class c1 (T : Type -> Type).
409
410 Instance i1 : forall X, c1 (fun x => f x (X x (fun (_: nat) => a) x)). Qed.
411
412 Fail Elpi Query TC.Solver lp:{{
413 sigma X Q\ % To avoid printing in console
414 tc.build-query-from-goal {{c1 (fun x => f x lp:X)}} _ Q _,
415 not Q.
416 }}.
417 End Llam_4.
418
419 Module Llam_5.
420 Definition NN := nat -> nat.
421 Class c1 (T : nat).
422 (* Instance has a uvar whose type is hidden behind a definition *)
423 Instance i : forall (x : NN), c1 (x 3). Qed.
424
425 Goal c1 (id 3).
426 apply _.
427 Qed.
428 End Llam_5.
429
430 Module Llam_6.
431
432 Class B (i: nat -> nat -> nat).
433
434 Elpi Query TC.Solver lp:{{
435 (pi x\ tc.unify-eq {{fun _ => 0}} (P x x)),
436 coq.say (P {{1}} {{2}}),
437 std.assert! (P {{1}} {{2}} = {{ fun _ => 0}}) "Heuristic error".
438 }}.
439
440 Axiom (f : nat -> nat).
441 Instance instB: B (fun _ _ => f 3) := {}.
442
443 Class A.
444 Instance instA : forall X, B (fun x => X x x) -> A := {}.
445
446 Goal A.
447 apply _.
448 Qed.
449
450 End Llam_6.
451
452 Module CoqUvar.
453 Class c1 (i:Type -> Type -> Type).
454
455 Elpi Query TC.Solver lp:{{
456 tc.precomp.instance {{c1 (fun x y => lp:F y x)}} T _ _ _,
457 coq.say T,
458 Expected = app[{{c1}}, tc.maybe-eta-tm (fun _ _ Inn) []],
459 std.assert! (T = Expected) "[TC] invalid precompile1",
460 pi x\ sigma ExpectedInn\
461 ExpectedInn = tc.maybe-eta-tm (A x) [x],
462 std.assert! ((Inn x) = ExpectedInn) "[TC] invalid precompile2".
463 }}.
464
465 Goal exists F, c1 (fun x y => F y x) -> c1 F.
466 (* exists (fun x y => nat); auto. *)
467 eexists.
468 apply _.
469 Unshelve.
470 apply nat.
471 Qed.
472 End CoqUvar.
473
474 Module CoqUvar1.
475 Class c1.
476 Class c2 (i:Type -> Type -> Type).
477
478 Axiom f : Type -> Type -> Type.
479
480 Instance i1: c2 f -> c1. Qed.
481
482 Goal exists F, c2 (fun x y => F y x) -> c1.
483 (* exists (fun x y => f y x); apply i1. *)
484 eexists.
485 intros.
486 apply _.
487 Qed.
488 End CoqUvar1.
489
490 Module CoqUvar2.
491 Axiom t : Type.
492 Class c1 (T : Type).
493 Instance i1 (F: Type -> Type): c1 (F t). Qed.
494
495 Goal exists F, c1 (F t).
496 eexists.
497 (* Set Debug "unification". *)
498 (* TODO: here we produce a eta-expanded proof, which produce a coq unification between `?F a` and `fun x => X x`
499 if we eta-reduce then coq has to unify `?F a` against `X` which succeeds *)
500 apply _.
501 Fail Check (i1 (fun x => _)) : c1 ( (fun x => _) t). (* ??? *)
502 Unshelve.
503 auto.
504 Qed.
505 End CoqUvar2.
506
507
508 Module CoqUvar3.
509 Axiom f : Type -> Type -> Type.
510
511 Class c1 (T : Type -> Type -> Type).
512 Instance i1 A: c1 (fun x y => f (A x y) (A x y)). Qed.
513
514 Elpi Query TC.Solver lp:{{
515 tc.precomp.goal {{c1 (fun x y => lp:X (lp:A x y) y)}} C _,
516 Expected = app [{{c1}}, tc.maybe-eta-tm (fun _ _ Body1) _],
517 Body1 = (x\ tc.maybe-eta-tm (fun _ _ (Body2 x)) [x]),
518 Body2 = (x\y\ tc.maybe-llam-tm (app [app [X], (Y x y), y]) [x,y]),
519 std.assert! (C = Expected) "[TC] invalid compilation".
520 }}.
521
522 (* Note: here interesting link-dedup *)
523 Goal exists X (A: Type -> Type -> Type), c1 (fun x y => X (A x y) y).
524 (*
525 do 2 eexists.
526 apply _.
527 Show Proof.
528 Unshelve.
529 apply T. apply T.
530 Show Proof. *) (* proof is OK, but for universes!!!! *)
531 apply (ex_intro
532 (fun X : Type -> Type -> Type =>
533 exists A : Type -> Type -> Type, c1 (fun x y : Type => X (A x y) y))
534 (fun _ H : Type => f H H)
535 (ex_intro
536 (fun A : Type -> Type -> Type =>
537 c1 (fun x y : Type => (fun _ H : Type => f H H) (A x y) y))
538 (fun H _ : Type => H) (i1 (fun _ H : Type => H)))).
539 Qed.
540
541 Axiom g : Type -> Type -> Type.
542 (* Note: here interesting failing link-dedup *)
543 Goal exists (A: Type -> Type -> Type), c1 (fun x y => g (A x y) y).
544 do 1 eexists.
545 Fail apply _.
546 Abort.
547 End CoqUvar3.
548
549 Module CoqUvar4.
550 Axiom f : Type -> Type -> Type.
551
552 Class c1 (T : Type -> Type -> Type).
553
554 Elpi Query TC.Solver lp:{{
555 tc.precomp.instance {{c1 (fun x y => lp:X (lp:A x y) y)}} C _ _ _,
556 Expected = app [{{c1}}, tc.maybe-eta-tm (fun _ _ Body1) _],
557 Body1 = (x\ tc.maybe-eta-tm (fun _ _ (Body2 x)) [x]),
558 Body2 = (x\y\ tc.maybe-llam-tm (app [app [X], (Y x y), y]) [y,x]),
559 std.assert! (C = Expected) "[TC] invalid compilation".
560 }}.
561
562 (* Note: here interesting failtc-c1ing link-dedup *)
563 Goal forall f, exists X, c1 (X nat) ->
564 c1 (f nat nat).
565 do 1 eexists.
566 apply _.
567 Qed.
568 End CoqUvar4.
569
570 (* TODO: add test with negative premise having a variable with type (M A) where M and A are coq uvar,
571 this is in order to clean-term with llam *)