| 1 |
From elpi Require Import elpi. |
| 2 |
|
| 3 |
(* Elpi does not feature tabling (memoization) but provides |
| 4 |
a very limited non-logical feature that can be used to store some (closed) data |
| 5 |
across backtracking. *) |
| 6 |
|
| 7 |
Elpi Tactic auto2. |
| 8 |
Elpi Accumulate lp:{{ |
| 9 |
% Ex falso |
| 10 |
pred exf i:goal, o:list sealed-goal. |
| 11 |
exf (goal Ctx _ Ty _ _ as G) [] :- |
| 12 |
std.exists Ctx (x\ sigma w\ x = decl V w {{False}}), |
| 13 |
refine {{ match lp:V in False return lp:Ty with end }} G []. |
| 14 |
|
| 15 |
% Constructor |
| 16 |
pred kon i:goal, o:list sealed-goal. |
| 17 |
kon (goal _ _ Ty _ _ as G) GS :- |
| 18 |
coq.safe-dest-app Ty (global (indt GR)) _, |
| 19 |
coq.env.indt GR _ _ _ _ Ks Kt, |
| 20 |
std.exists2 Ks Kt (k\ t\ |
| 21 |
coq.saturate t (global (indc k)) P, |
| 22 |
refine P G GS). |
| 23 |
|
| 24 |
% entry point; we assert no goals are left |
| 25 |
solve (goal _ _ _ _ [] as G) [] :- |
| 26 |
coq.ltac.repeat (coq.ltac.or [coq.ltac.open exf, coq.ltac.open kon]) (seal G) []. |
| 27 |
|
| 28 |
% Here we cache proved goals |
| 29 |
type item term -> term -> item. |
| 30 |
pred memo-db o:safe. |
| 31 |
|
| 32 |
pred memo-lookup i:safe, i:term, o:term. |
| 33 |
memo-lookup Safe Ty P :- open_safe Safe L, std.exists L (i\ i = item Ty P). |
| 34 |
|
| 35 |
solve (goal _ _ _ _ [str "memo"] as G) [] :- |
| 36 |
new_safe S, |
| 37 |
memo-db S => |
| 38 |
repeat-memo (coq.ltac.or[coq.ltac.open exf, coq.ltac.open kon]) G []. |
| 39 |
|
| 40 |
pred repeat-memo i:tactic, i:goal, o:list sealed-goal. |
| 41 |
|
| 42 |
repeat-memo _ (goal _ _ Ty P _) [] :- |
| 43 |
memo-db DB, memo-lookup DB Ty P, coq.say "hit" Ty, !. |
| 44 |
|
| 45 |
repeat-memo T (goal _ _ Ty Proof _ as G) GS :- |
| 46 |
T (seal G) New, coq.ltac.all (coq.ltac.open (repeat-memo T)) New GS, |
| 47 |
if (GS = []) (memo-db DB, stash_in_safe DB (item Ty Proof)) true. |
| 48 |
|
| 49 |
}}. |
| 50 |
|
| 51 |
|
| 52 |
Lemma l4 : |
| 53 |
(False \/ True) |
| 54 |
/\ (False \/ True) |
| 55 |
/\ (False \/ True) |
| 56 |
/\ (False \/ True) |
| 57 |
/\ (False \/ True) |
| 58 |
/\ (False \/ True) |
| 59 |
/\ (False \/ True) |
| 60 |
. |
| 61 |
Proof. |
| 62 |
Time elpi auto2 memo. |
| 63 |
Qed. |
| 64 |
|