Debian Codegraph @

tests/test_HOAS.v

raw
1 From elpi Require Import elpi.
2
3 Elpi Tactic test1.
4 Elpi Accumulate lp:{{
5
6 solve G GS :- pi x\
7 coq.sigma.print,
8 print_constraints,
9 refine {{ fun w : _ => _ }} G GS.
10 }}.
11
12
13 Lemma test (x : nat) : bool -> True.
14 Proof.
15
16 elpi test1.
17
18 Abort.
19
20 Ltac foobar x := idtac x; eapply x.
21
22 (* TODO: test evar type with a binder *)
23
24 Elpi Tactic test2.
25 Elpi Accumulate lp:{{
26
27 solve (goal [decl T _ _ | _ ] _ _ _ _ as G) GS :-
28 coq.ltac.call "foobar" [trm T] G GS ok,
29 coq.say GS.
30
31 }}.
32
33
34 Lemma test : (forall b: ( forall b : bool, b = b), True) -> True.
35 Proof.
36 intro.
37 elpi test2.
38 intro; reflexivity.
39 Qed.
40
41
42
43 Module kwd.
44
45 Parameter x : bool.
46
47 Elpi Command kwd.
48 Elpi Accumulate lp:{{
49 main L :- coq.say L.
50 }}.
51
52
53 Elpi kwd fun in as 4 end match return => : := { } ; , | "x" 1 H (fun x => match x as y in False return nat with end).
54
55 End kwd.
56
57 Elpi Command test.
58
59 Elpi Query lp:{{
60 std.do! [coq.env.begin-section "xxxxx", coq.env.end-section]
61 }} lp:{{
62 coq.env.begin-section "xxxxx",
63 coq.univ.new U,
64 T = sort (typ U),
65 coq.env.add-section-variable "a" _ T _,
66 coq.env.end-section
67 }}.
68
69 Elpi Db univs.db lp:{{ pred u o:univ. }}.
70 Elpi Command test_u.
71 Elpi Accumulate Db univs.db.
72 Elpi Query lp:{{
73 coq.univ.new U,
74 coq.elpi.accumulate current "univs.db" (clause _ _ (u U))
75 }}.
76
77 Universe foo.
78
79 Elpi Query lp:{{
80 {{ Type@{foo} }} = sort (typ U),
81 coq.elpi.accumulate current "univs.db" (clause _ _ (u U))
82 }}.
83
84
85 Axiom B : bool -> Type.
86 Axiom N : nat -> Type.
87
88 (* restriction *)
89 Elpi Query lp:{{
90 pi w\
91 @pi-decl `a` {{ bool }} a\
92 pi e\
93 @pi-decl `b` {{ B lp:a }} b\
94 coq.typecheck {{ fun x (y z : N x) => lp:{{ X a {{x}} {{z}} }} }} _ ok.
95 }}.
96
97
98 (* option *)
99 Fail Elpi Query lp:{{
100 @pi-decl `a` {{ bool }} a\
101 coq.typecheck (X a a) _ ok
102 }}.
103 Elpi Query lp:{{
104 @pi-decl `a` {{ bool }} a\
105 coq.say "----------------------------------",
106 (@holes! => coq.typecheck (X a a) TY ok),
107 coq.sigma.print,
108 coq.say (X a a) ":" TY.
109 }}.
110
111 (* primitive *)
112
113 Module Pint.
114
115 Elpi Command primitive.
116 Elpi Accumulate lp:{{
117 main [trm T] :-
118 std.assert! (coq.reduction.native.norm T _ T1) "normal form is not an opinion",
119 std.assert! (coq.reduction.vm.norm T _ T1) "normal form is not an opinion",
120 std.assert! (coq.reduction.cbv.norm T T1) "normal form is not an opinion",
121 std.assert! (coq.reduction.lazy.norm T T1) "normal form is not an opinion",
122 std.assert! (coq.reduction.lazy.whd_all T T1) "normal form is not an opinion",
123 coq.say "Raw term:" T "\nNice term:" {coq.term->string T} "\nRed:" {coq.term->string T1}.
124 }}.
125
126
127 From Corelib Require Import PrimInt63.
128 Elpi primitive (PrimInt63.add 2000000003333002 1).
129
130 From Corelib Require Import PrimFloat.
131 Open Scope float_scope.
132 Elpi primitive (2.4e13 + 1).
133 End Pint.
134
135 (* redflags *)
136
137 Elpi Query lp:{{
138 T1 = {{ 1 + 2 }},
139 T2 = {{ (fix plus n m {struct n} := match n with O => m | S p => S (plus p m) end) 1 2 }},
140 coq.locate "plus" (const C),
141 coq.redflags.add coq.redflags.nored [
142 coq.redflags.const C,
143 ] F,
144 (@redflags! F => coq.reduction.cbv.norm T1 T),
145 std.assert! (T = T2) "normal form is not an opinion".
146 }}.
147
148 Elpi Query lp:{{
149 T1 = {{ 1 + 2 }},
150 T2 = {{ (fun n m => match n with O => m | S p => S ((fix plus n m {struct n} := match n with O => m | S p => S (plus p m) end) p m) end) 1 2 }},
151 coq.locate "plus" (const C),
152 coq.redflags.add coq.redflags.nored [
153 coq.redflags.const C,
154 coq.redflags.fix,
155 ] F,
156 (@redflags! F => coq.reduction.cbv.norm T1 T),
157 std.assert! (T = T2) "normal form is not an opinion".
158 }}.
159
160 Elpi Query lp:{{
161 T1 = {{ 1 + 2 }},
162 T2 = {{ match 1 with O => 2 | S p => S ((fix plus n m {struct n} := match n with O => m | S p => S (plus p m) end) p 2) end }},
163 coq.locate "plus" (const C),
164 coq.redflags.add coq.redflags.nored [
165 coq.redflags.const C,
166 coq.redflags.fix,
167 coq.redflags.beta,
168 ] F,
169 (@redflags! F => coq.reduction.cbv.norm T1 T),
170 std.assert! (T = T2) "normal form is not an opinion".
171 }}.
172
173 Elpi Query lp:{{
174 T1 = {{ 1 + 2 }},
175 T2 = {{ 3 }},
176 coq.locate "plus" (const C),
177 coq.redflags.add coq.redflags.nored [
178 coq.redflags.const C,
179 coq.redflags.fix,
180 coq.redflags.beta,
181 coq.redflags.match,
182 ] F,
183 (@redflags! F => coq.reduction.cbv.norm T1 T),
184 std.assert! (T = T2) "normal form is not an opinion".
185 }}.
186
187 Module RP.
188 Set Primitive Projections.
189 Record r := mk_r { f : nat }.
190
191 Elpi Query lp:{{ std.spy-do! [
192 app[primitive (proj P _)|_] = {{ (_).(f) }},
193 coq.redflags.add coq.redflags.nored [
194 coq.redflags.delta,
195 coq.redflags.beta,
196 coq.redflags.match,
197 ] F,
198 (@redflags! F => coq.reduction.cbv.norm {{ (mk_r 3).(f) }} R0),
199 std.assert! (R0 = {{ (mk_r 3).(f) }}) "nored expected",
200 coq.redflags.add coq.redflags.nored [
201 coq.redflags.proj P,
202 coq.redflags.delta,
203 coq.redflags.beta,
204 coq.redflags.match,
205 ] FP,
206 (@redflags! FP => coq.reduction.cbv.norm {{ (mk_r 3).(f) }} R1),
207 std.assert! (R1 = {{ 3 }}) "red expected",
208 ].
209 }}.
210 End RP.
211
212
213 (* ------------------------------------ *)
214
215 Module P.
216 Set Primitive Projections.
217
218 Unset Auto Template Polymorphism.
219 Record foo {A : Type} := { p1 : nat; p2 : A }.
220 Definition x : foo := {| p1 := 3; p2 := false |}.
221
222 Unset Primitive Projections.
223 End P.
224
225
226 Module P'.
227 Record s (T : Type) := {
228 proj1 : nat;
229 proj2 : nat
230 }.
231
232 Elpi Query lp:{{
233 global (const C) = {{proj1}},
234 coq.env.projection? C 1.
235 }}.
236 End P'.
237
238
239 Module P''.
240 #[local] Set Primitive Projections.
241 Record s1 (T : Type) := {
242 proj1 : nat;
243 proj2 : nat
244 }.
245
246 Axiom (X : s1 nat).
247
248 Elpi Query lp:{{
249 app[primitive (proj P _) | _] = {{X.(proj1 _)}},
250 coq.env.primitive-projection? P C _,
251 global (const C) = {{proj1}}.
252 }}.
253
254 End P''.
255
256
257 Elpi Command primitive_proj.
258 Elpi Accumulate lp:{{
259 main [str Kind, trm (global (indt I)), trm T, int N, trm V] :- std.do! [
260 coq.env.projections I [_,_],
261 coq.env.primitive-projections I [some (pr _ 1), some (pr _ 2)],
262 coq.env.projections I [some P1, some P2],
263 if (Kind = "primitive")
264 (std.assert! (T = app[primitive (proj P N),A]) "not prim proj", coq.say P N A, coq.say {coq.term->string (primitive (proj P N))})
265 (std.assert! (T = app[global(const X), _, A], (X = P1 ; X = P2)) "not regular proj"), coq.say X A,
266 coq.say {coq.term->string T},
267 std.assert! ( {{:gref P.p1 }} = const C) "wrong gref",
268 std.assert! ( {{ @P.p1 }} = global (const C)) "wrong global",
269 coq.env.const C BO _,
270 coq.say BO,
271 std.assert! (unwind {whd T []} V) "wrong value",
272 ].
273 }}.
274
275
276 Elpi primitive_proj "primitive" (@P.foo) (P.x.(P.p1)) 1 (3%nat).
277 Elpi primitive_proj "primitive" (@P.foo) (P.x.(P.p2)) 2 (false).
278 (* FIXME, in raw mode this works
279 Elpi primitive_proj "regular" (@P.foo) (@P.p1 _ P.x) 1 (3%nat).
280 Elpi primitive_proj "regular" (@P.foo) (P.p2 P.x) 2 (false).
281 Elpi primitive_proj "regular" (@P.foo) (P.x.(@P.p1 bool)) 1 (3%nat).
282 Elpi primitive_proj "regular" (@P.foo) (P.x.(@P.p2 _)) 2 (false).
283 *)
284
285 (* glob of ifte *)
286
287 Elpi Command ifte.
288 Elpi Accumulate lp:{{
289
290 main [trm X] :-
291 coq.elaborate-skeleton X _ _ ok.
292
293 }}.
294
295 Elpi ifte (if true then 1 else 2).
296
297 (* gref quotations *)
298
299 Register nat as elpi.test.nat.
300
301 Elpi Query lp:{{
302
303 coq.locate "lib:elpi.test.nat" {{:gref nat }},
304 coq.locate "nat" {{:gref lib:elpi.test.nat }}.
305
306 }}.
307
308 Polymorphic Definition toto (T1 T2 : Type) (x : T1) := x.
309 Polymorphic Definition titi := toto.
310
311 Elpi Query lp:{{
312 coq.locate "titi" (const C),
313 coq.env.const C Body Term,
314 coq.say Body,
315 coq.say Term.
316 }}.
317
318 Universes u1 u2.
319
320 Elpi Query lp:{{
321 coq.say {{ toto }}, % pglobal (const «toto») X
322 coq.say {{ toto@{u1 u2} }}, % pglobal (const «toto») «u1 u2»
323 coq.say {coq.term->string {{ toto }}}.
324 }}.
325
326 Polymorphic Record F (T : Type) := Build_F { t : T }.
327 Polymorphic Definition fnat : F nat := {| t := 0%nat |}.
328
329 Elpi Query lp:{{
330 T = {{ @t nat fnat }},
331 coq.say T,
332 coq.typecheck T Ty ok,
333 coq.say T.
334 }}.
335
336 Section UPolyVar.
337
338 Polymorphic Variable n : nat.
339
340 Elpi Query lp:{{
341 coq.locate "F" GRF,
342 coq.typecheck (pglobal GRF I) TyF ok,
343 GRF = indt Ind,
344
345 % coq.env.indt
346 (@uinstance! I => coq.env.indt Ind _ _ _ Arity K KTys).
347 (@uinstance! I => coq.env.indt Ind _ _ _ Arity K _).
348 (@uinstance! I => coq.env.indt Ind _ _ _ _ K KTys).
349 (@uinstance! I => coq.env.indt Ind _ _ _ _ K _).
350 (@uinstance! I => coq.env.indt Ind _ _ _ _ _ _).
351
352 (@uinstance! _ => coq.env.indt Ind _ _ _ Arity1 K KTys1).
353 (@uinstance! _ => coq.env.indt Ind _ _ _ Arity2 K _).
354 (@uinstance! _ => coq.env.indt Ind _ _ _ _ K KTys3).
355 (@uinstance! _ => coq.env.indt Ind _ _ _ _ K _).
356 (@uinstance! _ => coq.env.indt Ind _ _ _ _ _ _).
357
358 (@uinstance! A4 => coq.env.indt Ind _ _ _ Arity4 K KTys4).
359 (@uinstance! A5 => coq.env.indt Ind _ _ _ Arity5 K _).
360 (@uinstance! A6 => coq.env.indt Ind _ _ _ _ K KTys6).
361 (@uinstance! A7 => coq.env.indt Ind _ _ _ _ K _).
362 (@uinstance! A8 => coq.env.indt Ind _ _ _ _ _ _).
363
364 (@uinstance! A4 => coq.env.indt Ind _ _ _ Arity4 K KTys4).
365 (@uinstance! A5 => coq.env.indt Ind _ _ _ Arity5 K _).
366 (@uinstance! A6 => coq.env.indt Ind _ _ _ _ K KTys6).
367
368 coq.locate "Build_F" GRB,
369 coq.typecheck (pglobal GRB I2) TyB ok,
370 GRB = indc B,
371
372 % coq.env.indc
373 (@uinstance! I2 => coq.env.indc B _ _ _ TyB).
374 (@uinstance! I2 => coq.env.indc B _ _ _ _).
375
376 (@uinstance! _ => coq.env.indc B _ _ _ BTy2).
377 (@uinstance! _ => coq.env.indc B _ _ _ _).
378
379 (@uinstance! B1 => coq.env.indc B _ _ _ BTy3).
380 (@uinstance! B2 => coq.env.indc B _ _ _ _).
381
382 (@uinstance! B1 => coq.env.indc B _ _ _ BTy3).
383
384 coq.locate "t" GRt,
385 coq.typecheck (pglobal GRt I3) Tyt ok,
386 GRt = const T,
387
388 % coq.env.const
389 % constant
390 (@uinstance! I3 => coq.env.const T BoT TyT).
391 (@uinstance! I3 => coq.env.const T BoT _).
392 (@uinstance! I3 => coq.env.const T _ TyT).
393 (@uinstance! I3 => coq.env.const T _ _).
394
395 (@uinstance! _ => coq.env.const T BoT1 TyT1).
396 (@uinstance! _ => coq.env.const T BoT2 _).
397 (@uinstance! _ => coq.env.const T _ TyT3).
398 (@uinstance! _ => coq.env.const T _ _).
399
400 (@uinstance! D1 => coq.env.const T BoT4 TyT4).
401 (@uinstance! D2 => coq.env.const T BoT5 _).
402 (@uinstance! D3 => coq.env.const T _ TyT5).
403 (@uinstance! D4 => coq.env.const T _ _).
404
405 (@uinstance! D1 => coq.env.const T BoT4 TyT4).
406 (@uinstance! D2 => coq.env.const T BoT5 _).
407 (@uinstance! D3 => coq.env.const T _ TyT5).
408
409 coq.locate "n" GRn,
410 GRn = const N,
411
412 % variable (non polymorphic)
413 (@uinstance! _ => coq.env.const N BoN TyN).
414 (@uinstance! _ => coq.env.const N BoN _).
415 (@uinstance! _ => coq.env.const N _ TyN).
416 (@uinstance! _ => coq.env.const N _ _).
417
418 (@uinstance! I4 => coq.env.const N BoN TyN).
419 (@uinstance! I4 => coq.env.const N BoN _).
420 (@uinstance! I4 => coq.env.const N _ TyN).
421 (@uinstance! I4 => coq.env.const N _ _).
422
423 % coq.env.typeof
424 % indt
425 (@uinstance! I => coq.env.typeof GRF TyF).
426 (@uinstance! I => coq.env.typeof GRF _).
427
428 (@uinstance! _ => coq.env.typeof GRF TyF2).
429 (@uinstance! _ => coq.env.typeof GRF _).
430
431 (@uinstance! C1 => coq.env.typeof GRF TyF3).
432 (@uinstance! C2 => coq.env.typeof GRF _).
433
434 (@uinstance! C1 => coq.env.typeof GRF TyF3).
435
436 % indc
437 (@uinstance! I2 => coq.env.typeof GRB TyB).
438 (@uinstance! I2 => coq.env.typeof GRB _).
439
440 (@uinstance! _ => coq.env.typeof GRB TyB2).
441 (@uinstance! _ => coq.env.typeof GRB _).
442
443 (@uinstance! C3 => coq.env.typeof GRB TyB3).
444 (@uinstance! C4 => coq.env.typeof GRB _).
445
446 (@uinstance! C3 => coq.env.typeof GRB TyB3).
447
448 % const
449 (@uinstance! I3 => coq.env.typeof GRt TyT).
450 (@uinstance! I3 => coq.env.typeof GRt _).
451
452 (@uinstance! _ => coq.env.typeof GRt TyT6).
453 (@uinstance! _ => coq.env.typeof GRt _).
454
455 (@uinstance! C5 => coq.env.typeof GRt TyT7).
456 (@uinstance! C6 => coq.env.typeof GRt _).
457
458 (@uinstance! C5 => coq.env.typeof GRt TyT7).
459
460 % var
461 (@uinstance! I4 => coq.env.typeof GRn TyN).
462 (@uinstance! I4 => coq.env.typeof GRn _).
463
464 (@uinstance! _ => coq.env.typeof GRn TyN).
465 (@uinstance! _ => coq.env.typeof GRn _).
466
467 % coq.env.const-body
468 % const
469 (@uinstance! I3 => coq.env.const-body T BoT).
470 (@uinstance! I3 => coq.env.const-body T _).
471
472 (@uinstance! _ => coq.env.const-body T BoT6).
473 (@uinstance! _ => coq.env.const-body T _).
474
475 (@uinstance! E5 => coq.env.const-body T BoT7).
476 (@uinstance! E6 => coq.env.const-body T _).
477
478 (@uinstance! E5 => coq.env.const-body T BoT7).
479
480 % var
481 (@uinstance! I4 => coq.env.const-body N BoN).
482 (@uinstance! I4 => coq.env.const-body N _).
483
484 (@uinstance! _ => coq.env.const-body N BoN).
485 @uinstance! _ => coq.env.const-body N _.
486 }}.
487
488 Elpi Query lp:{{
489 coq.locate "F" GRF,
490 GRF = indt Ind,
491
492 % coq.env.indt
493 (@uinstance! I => coq.env.indt-decl Ind Decl).
494 coq.say I Decl,
495 (@uinstance! I => coq.env.indt-decl Ind Decl1).
496 coq.say I Decl1,
497
498 (@uinstance! _ => coq.env.indt-decl Ind Decl2).
499 coq.say Decl2.
500
501 }}.
502
503 End UPolyVar.
504
505 Elpi Query lp:{{
506 coq.locate "F" GRF,
507 coq.typecheck (pglobal GRF I1) _ ok,
508 coq.typecheck (pglobal GRF I2) _ ok,
509 coq.say I1 I2,
510 coq.univ.print,
511 coq.univ-instance.unify-eq GRF I1 I2 ok,
512 coq.univ.print.
513 }}.
514
515 Elpi Query lp:{{
516 coq.locate "F" GRF,
517 coq.locate "fnat" GRfnat,
518 coq.typecheck (pglobal GRF I1) _ ok,
519 coq.typecheck (pglobal GRfnat I2) _ ok,
520 coq.say I1 I2,
521 coq.univ.print,
522 coq.univ-instance.unify-eq GRF I1 I2 (error E),
523 coq.say E,
524 coq.univ.print.
525 }}.
526
527 Elpi Query lp:{{
528 coq.locate "F" GRF,
529 coq.typecheck (pglobal GRF I1) _ ok,
530 coq.univ-instance I1 UL1,
531 coq.univ-instance I1 [U],
532 coq.univ-instance I2 [U].
533 }}.
534
535 Elpi Query lp:{{
536 coq.locate "F" GRF,
537 coq.typecheck (pglobal GRF I1) _ ok,
538 coq.typecheck (pglobal GRF I2) _ ok,
539 coq.univ-instance I1 [L1],
540 coq.univ-instance I2 [L2],
541 coq.univ.variable U1 L1,
542 coq.univ.variable U2 L2,
543 coq.sort.sup (typ U1) (typ U2),
544 coq.univ-instance.unify-eq GRF I1 I2 (error E),
545 coq.say E.
546 }}.
547
548 Cumulative Polymorphic Record F2@{+x} (T : Type@{x}) := Build_F2 { t2 : T }.
549
550 Elpi Query lp:{{
551 coq.locate "F2" GRF,
552 coq.typecheck (pglobal GRF I1) _ ok,
553 coq.typecheck (pglobal GRF I2) _ ok,
554 coq.univ-instance I1 [L1],
555 coq.univ-instance I2 [L2],
556 coq.univ.variable U1 L1,
557 coq.univ.variable U2 L2,
558 coq.sort.sup (typ U1) (typ U2),
559 coq.univ.print,
560 coq.univ-instance.unify-leq GRF I1 I2 ok. % why does this add a = not a <= ?
561 }}.
562
563 Elpi Query lp:{{
564 coq.locate "F" GRF,
565 coq.env.global GRF (pglobal GRF I1),
566 coq.typecheck (pglobal GRF I2) _ ok,
567 coq.univ-instance I1 [L1],
568 coq.univ-instance I2 [L2],
569 coq.univ.variable U1 L1,
570 coq.univ.variable U2 L2,
571 coq.sort.sup (typ U1) (typ U2),
572 coq.univ.print,
573 coq.univ-instance.unify-leq GRF I2 I1 (error E),
574 coq.say E.
575 }}.
576
577 Elpi Query lp:{{
578 coq.locate "nat" GR,
579 coq.env.global GR (global GR)
580 }}.
581
582 Elpi Query lp:{{
583 coq.locate "F" GR,
584 coq.env.global GR (pglobal GR I)
585 }}.
586
587
588 Elpi Query lp:{{
589 coq.locate "F" GR,
590 not(coq.env.global GR (global GR))
591 }}.
592
593 Elpi Query lp:{{
594 coq.locate "F" GR,
595 (@uinstance! I => coq.say {coq.env.global GR}).
596 coq.locate "Build_F" GR1,
597 coq.say I,
598 @uinstance! I => coq.say {coq.env.global GR1}.
599
600 }}.
601
602 Elpi Query lp:{{
603 coq.univ-instance I [U,U],
604 coq.say I
605 }}.
606
607
608 (*
609
610 #[universes(polymorphic)]
611 Definition poly@{x y | x < y } : Type@{y} := Type@{x}.
612 Set Printing Universes.
613 About poly.
614
615 #[universes(polymorphic)]
616 Inductive Box@{x y z} : Type@{z} := K : (Type@{x} -> Type@{y}) -> (Type@{y} -> Type@{x}) -> Box.
617 Set Printing Universes.
618 About Box.
619 *)
620
621 Elpi Query lp:{{
622
623 Body = (sort (typ UX)),
624 Type = (sort (typ UY)),
625 coq.univ.print,
626 coq.say "------------------",
627 coq.typecheck Body Type ok,
628 coq.univ.print,
629
630 coq.univ.variable UX LX,
631 coq.univ.variable UY LY,
632 coq.univ.print,
633
634 @udecl! [LX,LY] ff [lt LX LY] ff =>
635 coq.env.add-const "poly" Body Type _ _.
636
637 /*
638 @udecl-cumul! [invariant UX,auto UY,covar UZ| _] _ =>
639 coq.env.add-indt (inductive "Box" tt (arity (sort (typ UZ))) i\[
640 constructor "K" (arity
641 prod `_` (prod `_` (sort (typ UX)) (_\sort (typ UY))) _\
642 prod `_` (prod `_` (sort (typ UY)) (_\sort (typ UX))) _\
643 i)
644 ]) _.
645 */
646 }}.
647
648 Set Printing Universes.
649 About poly.
650 Check poly@{Set Type}.
651 About Box.
652
653 Elpi Query lp:{{
654 coq.say {{ Set }},
655 coq.unify-eq {{ Set }} (sort (typ U)) ok
656 }}.
657
658 Polymorphic Inductive tree (A : Type) :=
659 | leaf : A -> tree A
660 | node : A -> list (tree A) -> tree A.
661 Set Printing Universes.
662 Print tree.
663 Elpi Query lp:{{
664 std.do! [coq.env.begin-module "M" none, coq.env.end-module _]
665 }} lp:{{
666 pglobal (indt I) _ = {{ tree }},
667 coq.env.indt-decl I D,
668 coq.env.begin-module "M" none,
669 coq.say D,
670 std.assert-ok! (coq.typecheck-indt-decl D) "D illtyped",
671 coq.univ.print,
672 coq.env.add-indt D _,
673 coq.env.end-module _
674 }}.
675
676 Elpi Command Comm.
677 Elpi Accumulate lp:{{
678 main [indt-decl X] :- coq.say X,
679 coq.env.add-indt X _.
680 }}.
681 Elpi Comm Class c {A : Type} (x : A -> A).
682 Goal c S. Abort.
683
684 Elpi Query lp:{{
685 coq.typecheck-ty (sort (typ X)) A ok,
686 A = typ TX,
687 not(coq.univ.alg-super X TX),
688 coq.say X ":" TX,
689 (@keep-alg-univs! => coq.typecheck-ty (sort (typ Y)) B ok),
690 B = typ TY,
691 coq.say Y ":" TY,
692 coq.univ.alg-super Y TY,
693 coq.say Y ":" TY
694 .
695
696 }}.
697
698 (* mfix: mutual fixpoints *)
699
700 Fixpoint myeven (n : nat) : bool :=
701 match n with O => true | S n' => myodd n' end
702 with myodd (n : nat) : bool :=
703 match n with O => false | S n' => myeven n' end.
704
705 Fixpoint f3_0 (n : nat) : nat :=
706 match n with O => 0 | S n' => f3_1 n' end
707 with f3_1 (n : nat) : nat :=
708 match n with O => 1 | S n' => f3_2 n' end
709 with f3_2 (n : nat) : nat :=
710 match n with O => 2 | S n' => f3_0 n' end.
711
712 Elpi Command test_mfix.
713 Elpi Accumulate lp:{{
714 main [trm T] :-
715 coq.term->gref T (const C),
716 coq.env.const C (some Body) _Ty,
717 std.assert! (Body = mfix _ _ _) "expected mfix",
718 coq.elaborate-skeleton Body ETy _ ok,
719 coq.say {coq.gref->string (const C)} ":" {coq.term->string ETy}.
720 }}.
721
722 Elpi test_mfix (myeven).
723 Elpi test_mfix (myodd).
724 Elpi test_mfix (f3_0).
725 Elpi test_mfix (f3_1).
726 Elpi test_mfix (f3_2).
727
728 Elpi Command test_mfix_copy.
729 Elpi Accumulate lp:{{
730 main [trm T] :-
731 coq.term->gref T (const C),
732 coq.env.const C (some Body) _Ty,
733 copy Body Body1,
734 std.assert! (Body = Body1) "copy of mfix changed the term",
735 coq.say "copy ok for" {coq.gref->string (const C)}.
736 }}.
737
738 Elpi test_mfix_copy (myeven).
739 Elpi test_mfix_copy (myodd).
740 Elpi test_mfix_copy (f3_0).
741 Elpi test_mfix_copy (f3_1).
742 Elpi test_mfix_copy (f3_2).
743
744 Elpi Command test_mfix_fold_map.
745 Elpi Accumulate lp:{{
746 :before "fold-map:start"
747 fold-map {{ true }} A {{ false }} A :- !.
748
749 main [trm T] :-
750 coq.term->gref T (const C),
751 coq.env.const C (some Body) _Ty,
752 fold-map Body [] Body1 _,
753 std.assert! (not(Body = Body1)) "fold-map did not traverse mfix bodies",
754 coq.typecheck Body1 _ ok,
755 coq.say "fold-map ok for" {coq.gref->string (const C)}.
756 }}.
757
758 Elpi test_mfix_fold_map (myeven).
759
760 (* constr2lp must read all mutual-fixpoint component types in the
761 original outer context, before pushing any self-reference. *)
762 Elpi Command test_mfix_constr2lp_outer_ref.
763 Elpi Accumulate lp:{{
764 main [trm T] :-
765 copy T T1,
766 std.assert-ok! (coq.typecheck T1 _) "copy of mfix under binders is illtyped".
767 }}.
768
769 Elpi test_mfix_constr2lp_outer_ref
770 (fun (B : Type) (d : B) =>
771 fix f (n : nat) {struct n} : B := d
772 with g (n : nat) {struct n} : B := d
773 for f).
774
775 (* mfix component types must be read in the outer context,
776 before self-refs are pushed. *)
777 Elpi Command test_mfix_outer_ref.
778 Elpi Accumulate lp:{{
779 main _ :-
780 T = fun `B` (sort (typ _)) (bv\
781 fun `default` bv (dv\
782 mfix 0 0
783 (mfix-ty `f` 0 (prod `n` {{ nat }} (_\ bv)) (fv\
784 mfix-ty `g` 0 (prod `n` {{ nat }} (_\ bv)) (gv\
785 mfix-bo [
786 fun `n` {{ nat }} (_\ dv),
787 fun `n` {{ nat }} (_\ dv)
788 ]))))),
789 coq.typecheck T _ ok.
790 }}.
791
792 Elpi test_mfix_outer_ref.
793
794 (* lp2constr must reject a mfix whose top-level recarg disagrees with
795 the focused component's recarg. Elpi reduction reads the top-level
796 recarg, while Coq fixpoints store recargs per component. *)
797 Elpi Command test_mfix_bad_top_rno.
798 Elpi Accumulate lp:{{
799 main _ :-
800 T = mfix 0 1
801 (mfix-ty `f` 0 (prod `n` {{ nat }} (_\ {{ nat }})) (fv\
802 mfix-ty `g` 0 (prod `n` {{ nat }} (_\ {{ nat }})) (gv\
803 mfix-bo [
804 fun `n` {{ nat }} (_\ {{ O }}),
805 fun `n` {{ nat }} (_\ {{ O }})
806 ]))),
807 coq.typecheck T _ ok.
808 }}.
809
810 Fail Elpi test_mfix_bad_top_rno.
811
812 Elpi Query lp:{{
813
814 @pi-decl `x` {{nat}} x\ sigma Term Stack Reduct\
815 whd {{ myeven }} [{{ S lp:x }}] Term Stack,
816 unwind Term Stack Reduct,
817 std.assert! (Reduct =
818 {{ fix xx (n : nat) {struct n} : bool :=
819 match n with O => true | S n' => yy n' end
820 with yy (n : nat) {struct n} : bool :=
821 match n with O => false | S n' => xx n' end
822 for yy lp:x }}
823 ) "bad reduction",
824 coq.unify-eq Reduct {{ myodd lp:x }} ok
825
826 }}.
827
828 Elpi Query lp:{{
829
830 @pi-decl `x` {{nat}} x\ sigma Term Stack Reduct\
831 whd {{ myeven }} [{{ lp:x }}] Term Stack,
832 unwind Term Stack Reduct,
833 std.assert! (Reduct =
834 {{ fix xx (n : nat) {struct n}: bool :=
835 match n with O => true | S n' => yy n' end
836 with yy (n : nat) {struct n}: bool :=
837 match n with O => false | S n' => xx n' end
838 for xx lp:x }}
839 ) "bad reduction",
840 coq.unify-eq Reduct {{ myeven lp:x }} ok
841
842 }}.
843
844 Elpi Query lp:{{
845
846 @pi-decl `x` {{nat}} x\ sigma Term Stack Reduct\
847 whd {{ f3_0 }} [{{ lp:x }}] Term Stack,
848 unwind Term Stack Reduct,
849 std.assert! (Reduct =
850 {{ fix xx (n : nat) {struct n}: nat :=
851 match n with O => 0 | S n' => yy n' end
852 with yy (n : nat) {struct n}: nat :=
853 match n with O => 1 | S n' => zz n' end
854 with zz (n : nat) {struct n} : nat :=
855 match n with O => 2 | S n' => xx n' end
856 for xx lp:x }}
857 ) "bad reduction",
858 coq.unify-eq Reduct {{ f3_0 lp:x }} ok
859
860 }}.
861
862 Elpi Query lp:{{
863
864 @pi-decl `x` {{nat}} x\ sigma Term Stack Reduct\
865 whd {{ f3_1 }} [{{ S lp:x }}] Term Stack,
866 unwind Term Stack Reduct,
867 std.assert! (Reduct =
868 {{ fix xx (n : nat) {struct n}: nat :=
869 match n with O => 0 | S n' => yy n' end
870 with yy (n : nat) {struct n}: nat :=
871 match n with O => 1 | S n' => zz n' end
872 with zz (n : nat) {struct n}: nat :=
873 match n with O => 2 | S n' => xx n' end
874 for zz lp:x }}
875 ) "bad reduction",
876 coq.unify-eq Reduct {{ f3_2 lp:x }} ok
877
878 }}.
879
880 Elpi Query lp:{{
881
882 @pi-decl `x` {{nat}} x\ sigma Term Stack Reduct\
883 whd {{ f3_2 }} [{{ S lp:x }}] Term Stack,
884 unwind Term Stack Reduct,
885 std.assert! (Reduct =
886 {{ fix xx (n : nat) {struct n}: nat :=
887 match n with O => 0 | S n' => yy n' end
888 with yy (n : nat) {struct n}: nat :=
889 match n with O => 1 | S n' => zz n' end
890 with zz (n : nat) {struct n} : nat :=
891 match n with O => 2 | S n' => xx n' end
892 for xx lp:x }}
893 ) "bad reduction",
894 coq.unify-eq Reduct {{ f3_0 lp:x }} ok
895
896 }}.
897
898 Elpi Query lp:{{
899
900 sigma Term Stack Reduct\
901 whd {{ f3_0 3 }} [] Term Stack,
902 unwind Term Stack Reduct,
903 coq.unify-eq Reduct {{ 0 }} ok
904
905 }}.
906
907 Elpi Query lp:{{
908
909 sigma Term Stack Reduct\
910 whd {{ f3_1 3 }} [] Term Stack,
911 unwind Term Stack Reduct,
912 coq.unify-eq Reduct {{ 1 }} ok
913
914 }}.
915
916 Elpi Query lp:{{
917
918 sigma Term Stack Reduct\
919 whd {{ f3_2 3 }} [] Term Stack,
920 unwind Term Stack Reduct,
921 coq.unify-eq Reduct {{ 2 }} ok
922
923 }}.
924
925 Elpi Query lp:{{
926
927 T = {{
928
929 fun x : nat =>
930 fix x (n : nat) {struct n}: nat :=
931 match n with O => 0 | S n' => y n' end
932 with y (n : nat) {struct n}: nat :=
933 match n with O => 1 | S n' => x n' end
934 for x
935
936 }},
937 coq.say {coq.term->string T},
938 std.assert-ok! (coq.typecheck T _) "illtyped"
939
940
941 }}.
942
943 (************** *)
944
945 Inductive i1 (A: Type) (B : Type) : (forall x : nat, x + 1 = x) -> Prop :=
946 | K11 : forall p, forall x : bool, x = x -> i2 A B -> i1 A B p -> i1 A B p
947 with i2 (A:Type) (B : Type) : Type := K21.
948
949 Elpi Query lp:{{
950 {{ i1 }} = global (indt I),
951 coq.env.indt-block I tt NP NUP [I,I2] SL KNL KTL,
952 std.assert! (NP = 2) "NP",
953 std.assert! (NUP = 2) "NUP",
954 std.assert! (SL = [_,_]) "SL",
955 std.assert! (KNL = [[_],[_]]) "KNL",
956 std.assert! (KTL = [[_],[_]]) "KTL",
957 coq.env.indt-decl I D,
958 coq.env.indt-decl I2 D2,
959 std.assert! (D = D2) "different inductives",
960 coq.typecheck-indt-decl D ok.
961 }}.