| 1 |
From elpi Require Import elpi. |
| 2 |
|
| 3 |
(** Closing a term with holes with binders *) |
| 4 |
|
| 5 |
(* |
| 6 |
|
| 7 |
The operation consists in replacing all occurrences of the same hole |
| 8 |
in a term by a bound variable. |
| 9 |
|
| 10 |
We first traverse the term and count how many distinct hole are there |
| 11 |
and replace them by a placeholder to be later abstracted. |
| 12 |
Once we know how many binders we need, we generate the spine of binders |
| 13 |
and replace the placeholders by the bound variables. |
| 14 |
|
| 15 |
This example is interesting because it uses the constraint store |
| 16 |
to attach data to holes, in particular if the hole has been seen before, |
| 17 |
and to attach to each hole a unique number. |
| 18 |
|
| 19 |
*) |
| 20 |
|
| 21 |
Elpi Tactic abs_evars. |
| 22 |
Elpi Accumulate lp:{{ |
| 23 |
|
| 24 |
% we add a new constructor to terms to represent terms to be abstracted |
| 25 |
type abs int -> term. |
| 26 |
|
| 27 |
% bind back abstracted subterms |
| 28 |
pred bind i:int, i:int, i:term, o:term. |
| 29 |
bind I M T T1 :- M > I, !, |
| 30 |
T1 = {{ forall x, lp:(B x) }}, |
| 31 |
N is I + 1, |
| 32 |
pi x\ % we allocate the fresh symbol for (abs M) |
| 33 |
(copy (abs N) x :- !) ==> % we schedule the replacement (abs M) -> x |
| 34 |
bind N M T (B x). |
| 35 |
bind M M T T1 :- copy T T1. % we perform all the replacements |
| 36 |
|
| 37 |
% for a term with M holes, returns a term with M variables to fill these holes |
| 38 |
% the clause see is only generated for a term if it hasn't been seen before |
| 39 |
% the term might need to be typechecked first or main generates extra holes for the |
| 40 |
% type of the parameters |
| 41 |
pred abs-evars i:term, o:term, o:int. |
| 42 |
abs-evars T1 T3 M :- std.do! [ |
| 43 |
% we put (abs N) in place of each occurrence of the same hole |
| 44 |
((pi T Ty N N' M \ fold-map T N (abs M) M :- var T, not (seen? T _), !, coq.typecheck T Ty ok, fold-map Ty N _ N', M is N' + 1, seen! T M) ==> |
| 45 |
(pi T N M \ fold-map T N (abs M) N :- var T, seen? T M, !) ==> |
| 46 |
fold-map T1 0 T2 M), |
| 47 |
% we abstract M holes (M abs nodes) |
| 48 |
bind 0 M T2 T3, |
| 49 |
% cleanup constraint store |
| 50 |
purge-seen!, |
| 51 |
]. |
| 52 |
|
| 53 |
% all constraints are also on _ so that they share |
| 54 |
% a variable with the constraint to purge the store |
| 55 |
|
| 56 |
% we query if the hole was seen before, and if so |
| 57 |
% we fetch its number |
| 58 |
func seen? term, int ->. |
| 59 |
seen? X Y :- declare_constraint (seen? X Y) [X,_]. |
| 60 |
|
| 61 |
% we declare it is now seen and label it with a number |
| 62 |
func seen! term, int ->. |
| 63 |
seen! X Y :- declare_constraint (seen! X Y) [X,_]. |
| 64 |
|
| 65 |
% to empty the store |
| 66 |
func purge-seen!. |
| 67 |
purge-seen! :- declare_constraint purge-seen! [_]. |
| 68 |
|
| 69 |
constraint seen? seen! purge-seen! { |
| 70 |
% a succesful query, give the label back via M |
| 71 |
rule (seen! X N) \ (seen? X M) <=> (M = N). |
| 72 |
% an unsuccesful query |
| 73 |
rule \ (seen? X _) <=> false. |
| 74 |
|
| 75 |
rule purge-seen! \ (seen! _ _). |
| 76 |
rule \ purge-seen!. |
| 77 |
} |
| 78 |
|
| 79 |
% if we pass an argument this is what we use to refine the goal |
| 80 |
solve (goal _ _ _ _ [trm T] as G) GL :- |
| 81 |
std.assert-ok! (coq.elaborate-ty-skeleton T _ T1) "illtyped", |
| 82 |
std.assert! (abs-evars T1 ClosedT1 _) "closure fails", |
| 83 |
refine ClosedT1 G GL. |
| 84 |
|
| 85 |
% if we pass no argument, then we abstract the goal. |
| 86 |
% the first subgoal is a proof of the abstracted goal, while |
| 87 |
% the other goals are for the abstracted premises |
| 88 |
solve (goal _ _ T _ [] as G) GL :- |
| 89 |
std.assert! (abs-evars T ClosedT N) "closure fails", |
| 90 |
coq.mk-app {{ (fun x : lp:ClosedT => x) _ }} {coq.mk-n-holes N} R, |
| 91 |
refine R G GL. |
| 92 |
|
| 93 |
}}. |
| 94 |
|
| 95 |
Elpi Export abs_evars. |
| 96 |
|
| 97 |
Fail Lemma test : forall x, x = x. |
| 98 |
Lemma test : abs_evars (forall x, x = x). |
| 99 |
intros t x; reflexivity. |
| 100 |
Abort. |
| 101 |
|
| 102 |
Lemma test : abs_evars (forall x y, x = y). |
| 103 |
intros t x y; symmetry. |
| 104 |
Abort. |
| 105 |
|
| 106 |
Lemma test : True. |
| 107 |
assert (abs_evars (tt = _)) as H. |
| 108 |
intro x; destruct x; reflexivity. |
| 109 |
Abort. |
| 110 |
|
| 111 |
Lemma test : exists x, x = tt. |
| 112 |
econstructor. |
| 113 |
(* it is silly, but shows the code above performs the abstraction *) |
| 114 |
elpi abs_evars. |
| 115 |
now intros []. |
| 116 |
exact tt. |
| 117 |
Qed. |