| 1 |
From elpi Require Import elpi. |
| 2 |
|
| 3 |
(** Abstract a term over something, like the generalize tactic *) |
| 4 |
|
| 5 |
Elpi Command generalize. |
| 6 |
Elpi Accumulate lp:{{ |
| 7 |
|
| 8 |
% we add a new constructor to terms to represent terms to be abstracted |
| 9 |
type abs int -> term. |
| 10 |
|
| 11 |
% bind back abstracted subterms |
| 12 |
pred bind i:int, i:term, o:term. |
| 13 |
bind M T T1 :- M > 0, |
| 14 |
T1 = {{ fun x => lp:(B x) }}, % we build a Coq "fun .. => " |
| 15 |
N is M - 1, |
| 16 |
pi x\ % we allocate the fresh symbol for (abs M) |
| 17 |
(copy (abs M) x :- !) ==> % we schedule the replacement (abs M) -> x |
| 18 |
bind N T (B x). |
| 19 |
bind 0 T T1 :- copy T T1. % we perform all the replacements |
| 20 |
|
| 21 |
main [trm T] :- std.do! [ |
| 22 |
% example rule, abstracts all 1s. |
| 23 |
((pi N M\ fold-map {{ 1 }} N (abs M) M :- !, M is N + 1) ==> fold-map T 0 T1 NVars), |
| 24 |
bind NVars T1 T2, |
| 25 |
coq.say {coq.term->string T} "becomes" {coq.term->string T2}, |
| 26 |
]. |
| 27 |
|
| 28 |
|
| 29 |
}}. |
| 30 |
|
| 31 |
|
| 32 |
Elpi generalize (3 + 7). |
| 33 |
(* prints: |
| 34 |
(3 + 7) becomes (fun (x : ?e) (x0 : ?e0) => S (S x0) + S (S (S (S (S (S x)))))) |
| 35 |
*) |