Debian Codegraph @

tests/test_tactic.v

raw
1 From elpi Require Import elpi.
2
3 Elpi Tactic double_open.
4 Elpi Accumulate lp:{{
5 solve (goal _Ctx _Trigger Type _Proof [] as G) GL :-
6 (@no-tc! ==> refine {{ id _ }} G [G2]),
7 coq.ltac.open (refine {{ id _ }}) G2 GL,
8 coq.say G2.
9 }}.
10
11
12 Lemma foo (P : Prop) :
13 P -> P.
14 Proof.
15 elpi double_open.
16 match goal with |- P -> P => idtac end. (* no renaming *)
17 Abort.
18
19 Elpi Command foo.
20 Elpi Print foo "elpi.tests/foo".
21 Elpi Tactic foobar.
22 Elpi Print foobar "elpi.tests/foobar".
23
24
25
26 Elpi Tactic print_goal.
27 Elpi Accumulate lp:{{
28
29 solve (goal L _ T _ As as G) [seal G] :-
30 print_constraints,
31 coq.say "Goal: ", coq.say As, coq.say "\n",
32 coq.say L,
33 coq.say "------------",
34 coq.say T {coq.term->string T}.
35
36 }}.
37
38
39
40 Section foo.
41
42 Variables n m : nat.
43 Let o := m.
44
45 Lemma test_print (T : Type) (x : forall y : T, Type) (h : o = m) (w : T) :
46 forall e : x w = Type, forall j : x w, exists a : x w, a = a.
47 Proof.
48
49 elpi print_goal.
50
51 elpi print_goal.
52
53 intros; unshelve(eexists ?[foo]); shelve_unifiable.
54
55 elpi print_goal.
56
57 all: cycle 1; elpi print_goal; shelve_unifiable.
58
59 exact (refl_equal j).
60 Qed.
61
62 End foo.
63
64 Elpi Tactic id.
65 Elpi Accumulate lp:{{
66
67 solve (goal _Ctx _Solution _T _RefinedSolution _ as G) [seal G].
68
69 }}.
70
71
72 Elpi Tactic intro.
73 Elpi Accumulate lp:{{
74
75 solve (goal _ _ _ _ [str Name] as G) GS :-
76 coq.string->name Name N,
77 refine (fun N Src_ Tgt_) G GS.
78
79 }}.
80
81
82 Elpi Tactic refl.
83 Elpi Accumulate lp:{{
84
85 solve (goal _Ctx Solution _ _ _) [] :-
86 Solution = {{refl_equal _}}.
87
88 }}.
89
90
91 Lemma test_tactics (T : Type) (x : T) : forall e : x=x, e = e.
92 Proof.
93 elpi id.
94 elpi intro "elpi_rocks".
95 elpi refl.
96 Qed.
97
98 (* An assignement of a term containing a hole: of is triggered
99 and puts a typing constraint on FRESH_ *)
100
101 Elpi Tactic sloppy.
102 Elpi Accumulate lp:{{
103
104 solve (goal _ S Ty _ _) _ :-
105 print_constraints,
106 coq.say S Ty,
107 S = app[{{S}}, FRESH_ ],
108 print_constraints,
109 coq.say "hello".
110
111 }}.
112
113
114 Definition one : nat.
115 Proof.
116 elpi sloppy.
117 exact 0.
118 Defined.
119 Check eq_refl : one = 1.
120
121
122 Elpi Tactic test_typecheck_in_ctx.
123 Elpi Accumulate lp:{{
124
125 solve (goal Ctx _Ev (prod _ T x\ app[G x,B x,_]) _ _) _ :-
126 pi x\ decl x `f` T ==> sigma H HT\
127 coq.typecheck (B x) (Ty x) ok,
128 coq.typecheck (G x) (GTy x) ok,
129 coq.say [B,Ty,G,GTy],
130 Ctx = [decl X _ _|_],
131 H = {{lp:X = 2}},
132 coq.typecheck H HT ok, % X is restricted wrt x
133 coq.say [H,HT]
134 .
135 }}.
136
137 Elpi Print test_typecheck_in_ctx "elpi.tests/test_typecheck_in_ctx".
138
139 Section T.
140 Variable a : nat.
141 Lemma test_elab T (f : forall x :nat, T x) x : forall g, g (f x) a.
142 Proof.
143 elpi test_typecheck_in_ctx.
144 Abort.
145
146 End T.
147
148
149 (* Arguments *)
150
151 Elpi Tactic test_args_exact.
152 Elpi Accumulate lp:{{
153
154 solve (goal _ Ev T _ [str Msg, int N, trm X, str Q]) _ :-
155 coq.say Msg N X T Q,
156 Ev = X.
157
158 }}.
159
160
161 Section T1.
162 Variable a : nat.
163
164
165 Lemma test_elab2 T (f : forall x :nat, T x) x : forall g, (forall y, g y a) -> g (f x) a.
166 Proof.
167 intros g H.
168 Check 1356.
169 elpi test_args_exact "this" 3 (H _) foo.bar .
170 Qed.
171
172
173 End T1.
174
175 (* purity of tactics *)
176
177 Elpi Tactic test_impure.
178 Elpi Accumulate lp:{{
179
180 solve (goal _ _ _ _ []) _ :-
181 coq.env.add-const "xxx" _ {{ nat }} _ _.
182
183 }}.
184
185
186 Lemma test_impure : True.
187 Proof.
188 Fail elpi test_impure.
189 Abort.
190
191 (* ltac notations *)
192
193 Elpi Tactic test.notation.
194 Elpi Accumulate lp:{{
195
196 solve (goal _ _ _ _ A) _ :- coq.say A, print_constraints.
197
198 }}.
199
200
201 Tactic Notation "test_cl" constr_list(X) := elpi test.notation ltac_term_list:(X).
202 Tactic Notation "test_ocl" open_constr_list(X) := elpi test.notation ltac_term_list:(X).
203 Tactic Notation "test_ucl" uconstr_list(X) := elpi test.notation ltac_term_list:(X).
204 Tactic Notation "test_hl" hyp_list(X) := elpi test.notation ltac_term_list:(X).
205
206 Tactic Notation "test_c" constr(X) := elpi test.notation ltac_term:(X).
207 Tactic Notation "test_oc" open_constr(X) := elpi test.notation ltac_term:(X).
208 Tactic Notation "test_uc" uconstr(X) := elpi test.notation ltac_term:(X).
209 Tactic Notation "test_h" hyp(X) := elpi test.notation ltac_term:(X).
210 Tactic Notation "test_i" int(X) := elpi test.notation ltac_int:(X).
211 Tactic Notation "test_i2" integer(X) := elpi test.notation ltac_int:(X).
212 Tactic Notation "test_s" string(X) := elpi test.notation ltac_string:(X).
213 Tactic Notation "test_s2" ident(X) := elpi test.notation ltac_string:(X).
214 Tactic Notation "test_s3" hyp(X) := elpi test.notation ltac_string:(X).
215
216 Tactic Notation "legacy_test_c" constr(X) := elpi test.notation (X).
217 Tactic Notation "legacy_test_oc" open_constr(X) := elpi test.notation (X).
218 Tactic Notation "legacy_test_uc" uconstr(X) := elpi test.notation (X).
219 Tactic Notation "legacy_test_h" hyp(X) := elpi test.notation (X).
220
221
222 Lemma test_notation (x y : nat) : True.
223 Proof.
224
225 test_cl x (x + y).
226 Fail test_cl x (_ + y).
227 test_ocl x (_ + y).
228 test_ucl x (_ + y).
229 test_hl x y.
230
231 test_c (x + y).
232 Fail test_c (_ + y).
233 test_oc (_ + y).
234 Fail test_oc (0 0).
235 test_uc (_ + y).
236 test_uc (0 0).
237 test_h x.
238 Fail test_h z.
239 test_i 1.
240 test_i2 -1.
241 test_s "a".
242 test_s2 a.
243 test_s3 x.
244 Fail test_s3 z.
245
246 legacy_test_c (x + y).
247 Fail legacy_test_c (_ + y).
248 legacy_test_oc (_ + y).
249 legacy_test_uc (_ + y).
250 legacy_test_h x.
251 Fail legacy_test_h z.
252
253 Abort.
254
255
256 Elpi Tactic test_sideeff.
257 Elpi Accumulate lp:{{
258 pred myexists i:goal, o:list sealed-goal.
259 myexists (goal _ RawEv _ Ev _) GS1 :-
260 RawEv = {{ ex_intro (fun x : nat => x = 1) _ _ }},
261 coq.ltac.collect-goals Ev GS Shelved,
262 std.append GS Shelved GS1,
263 std.assert! (std.length GS1 2) "not 2 goals".
264
265 pred myrefl i:goal, o:list sealed-goal.
266 myrefl (goal _ _ _ P _ as G) GL :-
267 std.assert! (var P) "second goal was not skipped",
268 refine {{ eq_refl _ }} G GL.
269
270 solve G GL :-
271 coq.ltac.thenl [coq.ltac.open myexists, coq.ltac.open myrefl] (seal G) GL.
272 }}.
273
274
275 Goal exists x, x = 1.
276 elpi test_sideeff.
277 Abort.
278
279 Elpi Tactic test_att.
280 Elpi Accumulate lp:{{
281 solve _ _ :-
282 coq.say "YYYYYYYYYYYYYYYYYYYYYYYYYYYY" {attributes},
283 coq.parse-attributes {attributes} [ att "foo" bool, att "bar" bool ] Opts,
284 coq.say "XXXXXXXXXXXXXXXXXXXXXXXXXXX" Opts,
285 (Opts ==> get-option "foo" tt).
286 }}.
287
288
289 Tactic Notation "#[" attributes(A) "]" "testatt" := ltac_attributes:(A) elpi test_att.
290 Tactic Notation "testatt" "#[" attributes(A) "]" := ltac_attributes:(A) elpi test_att.
291
292 Goal True.
293 (#[ foo ] testatt).
294 idtac; #[ foo ] testatt.
295 Fail (#[ bar ] testatt).
296 Fail (#[ foo2 ] testatt).
297 testatt #[ foo ].
298 Abort.
299
300 (* ***************** *)
301
302 Elpi Tactic test.m.
303 Elpi Accumulate lp:{{
304 type type-arg open-tactic.
305 type-arg (goal _ _ _ _ [trm T|_] as G) GL :-
306 refine T G GL.
307 type-arg (goal A B C D [X|R]) GL :-
308 coq.say "skip" X,
309 type-arg (goal A B C D R) GL.
310
311 msolve GL New :- coq.say {attributes},
312 coq.ltac.all (coq.ltac.open type-arg) GL New.
313 }}.
314
315
316 Goal (forall x : nat, x = x) /\ (forall x : bool, x = x).
317 split; intro x.
318 all: elpi test.m (@eq_refl _ x).
319 Qed.
320
321 Elpi Query lp:{{
322 coq.notation.add-abbreviation-for-tactic "XX.xxx" "test.m" [int 1, str "33", trm {{bool}}]
323 }}.
324
325 Print Grammar constr.
326
327 Goal (forall x : nat, x = x) /\ (forall x : bool, x = x).
328 split; intro x.
329 all: exact (XX.xxx (@eq_refl _ x)).
330 Qed.
331
332 Check forall xxx : nat, forall XX : bool, True.
333
334 Elpi Export test.m.
335
336 Goal (forall x : nat, x = x) /\ (forall x : bool, x = x).
337 split; intro x.
338 all: exact (test.m (@eq_refl _ x)).
339 Qed.
340
341 Set Warnings "-non-reversible-notation".
342 Notation Foo pp := ltac:(elpi test.m (pp)).
343
344 Goal (forall x : nat, x = x) /\ (forall x : bool, x = x).
345 split; intro x.
346 all: exact (Foo (@eq_refl _ x)).
347 Qed.
348
349 Tactic Notation "Bar" open_constr(pp) :=
350 elpi test.m (pp).
351 Notation Bar qq := ltac:(Bar (@eq_refl _ qq)).
352
353 Goal (forall x : nat, x = x) /\ (forall x : bool, x = x).
354 split; intro x.
355 all: exact (Bar x).
356 Qed.
357
358 Elpi Tactic t.
359 Elpi Accumulate lp:{{
360 solve G _ :- coq.say G.
361 }}.
362
363
364 Tactic Notation "t1" constr(h) := idtac h.
365 Tactic Notation "t2" constr(h) := elpi t ltac_term:(h).
366 Section Test.
367 Hypothesis H : True.
368 Lemma foo : True.
369 Proof.
370 t1 H. (* first tactic, works *)
371 elpi t (H). (* expected equivalent of t2, works *)
372 t2 (H = H). (* t2 called with a term containing H, works *)
373 t2 H. (* called just with H, fails *)
374 Abort.
375 End Test.
376
377 (* we test we can pass ltac values around *)
378
379 Elpi Tactic app.
380 Elpi Accumulate lp:{{
381 solve (goal C R T P [tac F, tac X]) GL :-
382 coq.ltac.call-ltac1 F (goal C R T P [tac X]) GL ok.
383 }}.
384
385
386 Tactic Notation "foo" simple_intropattern_list(l) :=
387 elpi app ltac_tactic:(let f x := x in f) ltac_tactic:( intros l ).
388
389 Goal forall n, n + 1 = 1.
390 foo [|m].
391 trivial.
392 match goal with |- S m + 1 = 1 => idtac end.
393 Abort.
394
395
396
397 Elpi Tactic fresh1.
398 Elpi Accumulate lp:{{
399 solve (goal _ _ {{ forall x : lp:Ty, _ }} _ _ as G) GL :-
400 coq.ltac.fresh-id "x" Ty ID,
401 coq.id->name ID N,
402 refine (fun N _ _) G GL.
403 }}.
404
405 Goal forall x z y, x = 1 + y + z.
406 intros x x0.
407 elpi fresh1.
408 Check x1.
409 Abort.
410
411 Implicit Type (w : nat).
412
413 Goal forall x z y, x = 1 + y + z.
414 intros ??.
415 elpi fresh1.
416 Check w.
417 Abort.
418
419
420 Elpi Tactic create_goal.
421 Elpi Accumulate lp:{{
422 solve G GL :- std.spy-do! [
423 coq.elaborate-skeleton _ {{nat}} NewGoal' ok,
424 coq.ltac.collect-goals NewGoal' GL _,
425 ].
426 }}.
427
428 Goal forall x: nat, x =x.
429 Proof.
430 elpi create_goal.
431 Abort.
432
433
434 (** test simple call *)
435
436 Elpi Tactic simple_call.
437 Elpi Accumulate lp:{{
438 solve (goal C R Ty E [str T] as G) [seal G'] :-
439 std.assert-ok! (coq.ltac.call-simple-ltac1 T (goal C R Ty E []) [G']) T.
440 }}.
441
442 Goal forall x y: nat, x = y.
443 Proof.
444 intros.
445 elpi simple_call "symmetry".
446 match goal with |- y = x => idtac end.
447 Abort.
448
449 Ltac myintros := intros.
450 Ltac myrefl := reflexivity.
451
452 Goal forall x y: nat, x = y.
453 Proof.
454 Fail elpi simple_call "myintros".
455 Abort.
456
457 Goal forall x y: nat, x = y.
458 Proof.
459 intros.
460 Fail elpi simple_call "myrefl".
461 Abort.