| 1 |
From elpi Require Import elpi. |
| 2 |
From Stdlib Require Import Arith ZArith List FunctionalExtensionality. |
| 3 |
|
| 4 |
Axiom map2 : (nat -> nat -> nat) -> list nat -> list nat -> list nat. |
| 5 |
|
| 6 |
(* This example make use of the open-trm argument type in order to |
| 7 |
implement a tactic that takes in input expressions mentioning |
| 8 |
variables that are bound in the goal (and not in the proof context) *) |
| 9 |
|
| 10 |
Lemma easy_example : forall x, x + 1 = 1 + x. |
| 11 |
Proof. |
| 12 |
intros x. |
| 13 |
(* easy: x is bound by the proof context *) |
| 14 |
replace (x + 1) with (1 + x) by ring. |
| 15 |
reflexivity. |
| 16 |
Qed. |
| 17 |
|
| 18 |
Lemma hard_example : forall l, map (fun x => x + 1) l = map (fun x => 1 + x) l. |
| 19 |
Proof. |
| 20 |
intros l. |
| 21 |
(* hard: x is bound by a lambda in the goal *) |
| 22 |
Fail replace (x + 1) with (1 + x) by ring. |
| 23 |
(* fails and prints: The variable x was not found in the current environment.*) |
| 24 |
|
| 25 |
(* This example implements a new repl tactic that will instead succeed. *) |
| 26 |
Abort. |
| 27 |
|
| 28 |
(* Elpi provides a type of tactic argument called open-trm for terms |
| 29 |
with free variables. Since there is no such a thing as free variables |
| 30 |
in Elpi, a pre-processor closes the term under binders and pairs the |
| 31 |
closed term with the number of such "technical binders". *) |
| 32 |
|
| 33 |
Elpi Tactic show_open_term. |
| 34 |
Elpi Accumulate lp:{{ |
| 35 |
solve (goal _ _ _ _ [open-trm N F]) _ :- |
| 36 |
coq.say "The argument " {coq.term->string F} "was closed under" N "binders". |
| 37 |
}}. |
| 38 |
|
| 39 |
Goal map (fun x => x + x) nil = nil :> list nat. |
| 40 |
elpi show_open_term `(x + _). |
| 41 |
(* prints: The argument fun x : ?e => x + ?e1 was closed under 1 binders *) |
| 42 |
Abort. |
| 43 |
|
| 44 |
(* A key step in the implementation of repl is to align the technical binders |
| 45 |
of the open term with actual binders present in the goal. For this |
| 46 |
we use the names as written by the user. We call this operation instantiate *) |
| 47 |
|
| 48 |
(* We put the code into a file, so that we can reuse it later *) |
| 49 |
Elpi File instantiate.code lp:{{ |
| 50 |
|
| 51 |
% [instantiate-replacement N Ty x L R L1 R1] is called when crossing |
| 52 |
% a goal binder for the variable x named N and of type Ty. The replacement |
| 53 |
% L with R is instantiated (if possible) to L1 with R1 |
| 54 |
pred instantiate-replacement i:name, i:term, i:term, i:argument, i:argument, o:argument, o:argument. |
| 55 |
instantiate-replacement N Ty C L R L1 R1 :- std.do! [ |
| 56 |
instantiate N Ty C L L1, |
| 57 |
instantiate N Ty C R R1, |
| 58 |
]. |
| 59 |
|
| 60 |
pred instantiate i:name, i:term, i:term, i:argument, o:argument. |
| 61 |
instantiate _ _ _ (open-trm 0 A) (open-trm 0 A) :- !. |
| 62 |
instantiate N T C (open-trm I F) (open-trm J F1) :- remove-binder-for N T C F F1, !, |
| 63 |
J is I - 1. |
| 64 |
instantiate _ _ _ X X. |
| 65 |
|
| 66 |
pred remove-binder-for i:name, i:term, i:term, i:term, o:term. |
| 67 |
% we found the binder |
| 68 |
remove-binder-for N _ C (fun N1 _ F) Res :- {coq.name->id N} = {coq.name->id N1}, !, |
| 69 |
% Remember that in Elpi all names are the same, eg `x` = `y` |
| 70 |
% coq.name->id gives a string, the string used for printing, and as one expects |
| 71 |
% not("x" = "y") |
| 72 |
Res = (F C). |
| 73 |
% we cross the binder and look deeper |
| 74 |
remove-binder-for N T C (fun N1 T1 F) (fun N1 T1 F1) :- |
| 75 |
@pi-decl N1 T1 x \ remove-binder-for N T C (F x) (F1 x). |
| 76 |
|
| 77 |
}}. |
| 78 |
|
| 79 |
|
| 80 |
Elpi Tactic test_instantiate. |
| 81 |
Elpi Accumulate File instantiate.code. |
| 82 |
Elpi Accumulate lp:{{ |
| 83 |
solve (goal _ _ {{ map2 lp:F _ _ = _ }} _ [(open-trm _ LF as L), (open-trm _ RF as R)]) _ :- |
| 84 |
coq.say "old replacement:" {coq.term->string LF} "with" {coq.term->string RF}, |
| 85 |
F = fun N Ty _, |
| 86 |
@pi-decl N Ty x\ |
| 87 |
instantiate-replacement N Ty x L R (open-trm _ (L1 x)) (open-trm _ (R1 x)), |
| 88 |
coq.say "new replacement:" {coq.term->string (L1 x)} "with" {coq.term->string (R1 x)}. |
| 89 |
|
| 90 |
}}. |
| 91 |
|
| 92 |
|
| 93 |
Goal map2 (fun x y => x - y) nil nil = nil. |
| 94 |
elpi test_instantiate `(x - y) `(y + x). |
| 95 |
(* prints: |
| 96 |
old replacement: |
| 97 |
fun (x : ?e) (y : ?e0) => x - y |
| 98 |
with |
| 99 |
fun (y : ?e1) (x : ?e2) => y + x |
| 100 |
|
| 101 |
new replacement: |
| 102 |
fun y : ?e0 => x - y |
| 103 |
with |
| 104 |
fun y : ?e1 => y + x |
| 105 |
|
| 106 |
Remark that the technical binders are generated following a left-to-right |
| 107 |
traversal of the term, hence their order varies. This is the reason why we |
| 108 |
need the second rule for remove-binder-for. |
| 109 |
|
| 110 |
Also remark that L1 and R1 need to see the variable we crossed, and that |
| 111 |
is bound by pi: we are capturing the free variable by it (or better removing |
| 112 |
the techincal binding and replacing that bound variable by the variable bound |
| 113 |
by pi). |
| 114 |
|
| 115 |
*) |
| 116 |
Abort. |
| 117 |
|
| 118 |
|
| 119 |
(* We need a few helpers to cross the context *) |
| 120 |
|
| 121 |
Lemma congrP (A : Type) (B : A -> Type) (f g : forall x : A, B x) : |
| 122 |
f = g -> forall v1 v2 (p : v1 = v2), match p in _ = x return B x with eq_refl => f v1 end = g v2. |
| 123 |
Proof. now intros fg v1 v2 v1v2; rewrite fg, v1v2. Qed. |
| 124 |
|
| 125 |
Lemma congrA (A B : Type) (f g : A -> B) : |
| 126 |
f = g -> forall v1 v2 (p : v1 = v2), f v1 = g v2. |
| 127 |
Proof. now intros fg v1 v2 v1v2; rewrite fg, v1v2. Qed. |
| 128 |
|
| 129 |
Elpi File congruence.code lp:{{ |
| 130 |
|
| 131 |
% iterates congrA and congrP to prove f x1..xn = f y1..yn from proofs of xi = yi |
| 132 |
% [congruence F G PFG Ty XS YS PXYS R] starting from a proof PFG that F = G |
| 133 |
% it consumes X Y and PXY : X = Y to build a proof R : (F X = G Y). It needs to |
| 134 |
% look at the type of F (and G) in order to decide which congruence lemma to apply |
| 135 |
pred conguence i:term, i:term, i:term, i:term, i:list term, i:list term, i: list term, o:term. |
| 136 |
conguence _ _ P _ [] [] [] P :- !. |
| 137 |
conguence F G PFG {{ _ -> lp:Ty }} [X|XS] [Y|YS] [PXY|PS] Q :- !, |
| 138 |
PFXGY = {{ congrA _ _ lp:F lp:G lp:PFG lp:X lp:Y lp:PXY }}, |
| 139 |
conguence {coq.mk-app F [X]} {coq.mk-app G [Y]} PFXGY Ty XS YS PS Q. |
| 140 |
conguence F G PFG {{ forall x, lp:(Ty x) }} [X|XS] [Y|YS] [PXY|PS] Q :- |
| 141 |
PFXGY = {{ congrP _ _ lp:F lp:G lp:PFG lp:X lp:Y lp:PXY }}, |
| 142 |
conguence {coq.mk-app F [X]} {coq.mk-app G [Y]} PFXGY (Ty X) XS YS PS Q. |
| 143 |
|
| 144 |
}}. |
| 145 |
|
| 146 |
Elpi Tactic test_congruence. |
| 147 |
Elpi Accumulate File congruence.code. |
| 148 |
Elpi Accumulate lp:{{ |
| 149 |
pred mk-refl i:term, o:term. |
| 150 |
mk-refl T {{ @refl_equal Type lp:T }} :- coq.typecheck-ty T _ ok, !. % we don't like Set |
| 151 |
mk-refl T {{ refl_equal lp:T }}. |
| 152 |
|
| 153 |
solve (goal _ _ {{ lp:A = lp:B }} _ _ as G) GL :- std.do! [ |
| 154 |
A = app[HD|Args1], |
| 155 |
B = app[HD|Args2], |
| 156 |
std.map2 Args1 Args2 (_\ mk-refl) Proofs, |
| 157 |
coq.typecheck HD Ty ok, |
| 158 |
conguence HD HD {{ refl_equal lp:HD }} Ty Args1 Args2 Proofs P, |
| 159 |
refine P G GL, |
| 160 |
]. |
| 161 |
}}. |
| 162 |
|
| 163 |
Goal forall l, map (fun x => x) l = map (fun x => x) l :> list nat. |
| 164 |
intros. |
| 165 |
elpi test_congruence. |
| 166 |
Qed. |
| 167 |
|
| 168 |
(* We now build the final tactic *) |
| 169 |
|
| 170 |
Elpi Tactic replace. |
| 171 |
Elpi Accumulate File instantiate.code. |
| 172 |
Elpi Accumulate File congruence.code. |
| 173 |
Elpi Accumulate lp:{{ |
| 174 |
|
| 175 |
|
| 176 |
% [replace L R X Y P] replaces L by R in X obtaining Y and |
| 177 |
% a proof P that X = Y. P will contain holes (sub goals) for sub proofs |
| 178 |
% of L = R. |
| 179 |
pred replace i:argument, i:argument, i:term, o:term, o:term. |
| 180 |
|
| 181 |
% all binders are crossed and we find a term identical to L. |
| 182 |
% the proof is a hole of type L = R. |
| 183 |
replace (open-trm 0 L) (open-trm 0 R) L R P :- !, |
| 184 |
TY = {{ lp:L = lp:R }}, |
| 185 |
coq.typecheck-ty TY _ ok, |
| 186 |
coq.typecheck P TY ok. |
| 187 |
|
| 188 |
% we cross the binder by functional extensionality |
| 189 |
replace L R (fun N T F) (fun N T F1) {{ functional_extensionality lp:{{ fun N T F }} lp:{{ fun N T F1 }} lp:{{ fun N T Prf }}}} :- !, |
| 190 |
@pi-decl N T x\ |
| 191 |
instantiate-replacement N T x L R (L1 x) (R1 x), |
| 192 |
replace (L1 x) (R1 x) (F x) (F1 x) (Prf x). |
| 193 |
|
| 194 |
replace L R (app [HD|TS]) (app [HD|TS1]) Prf :- |
| 195 |
replace-list L R TS TS1 PS, |
| 196 |
coq.typecheck HD Ty ok, |
| 197 |
conguence HD HD {{ refl_equal lp:HD }} Ty TS TS1 PS Prf. |
| 198 |
|
| 199 |
% base cases |
| 200 |
replace _ _ X X {{ refl_equal lp:X }} :- name X, !. |
| 201 |
replace _ _ (global _ as C) C {{ @refl_equal Type lp:C }} :- coq.typecheck-ty C _ ok, !. % we don't like Set |
| 202 |
replace _ _ (global _ as C) C {{ refl_equal lp:C }} :- !. |
| 203 |
% we omit rules for primitive constants, fixpoints, let, forall, ... |
| 204 |
|
| 205 |
pred replace-list i:argument, i:argument, i:list term, o:list term, o:list term. |
| 206 |
replace-list _ _ [] [] []. |
| 207 |
replace-list L R [X|XS] [Y|YS] [P|PS] :- replace L R X Y P, replace-list L R XS YS PS. |
| 208 |
|
| 209 |
solve (goal _ _ {{ lp:X = lp:Y }} _ [(open-trm _ _ as L), (open-trm _ _ as R)] as G) GL :- !, std.do! [ |
| 210 |
replace L R X X1 P, |
| 211 |
refine {{ @eq_trans _ lp:X lp:X1 lp:Y lp:P _ }} G GL, |
| 212 |
]. |
| 213 |
|
| 214 |
solve (goal _ _ {{ _ = _ }} _ _) _ :- !, coq.error "elpi: replace: the goal is not an equality". |
| 215 |
solve _ _ :- !, coq.error "elpi: replace: the two arguments must be open terms". |
| 216 |
|
| 217 |
}}. |
| 218 |
|
| 219 |
Tactic Notation (at level 0) "repl" uconstr(x) "with" uconstr(y) := |
| 220 |
elpi replace ltac_open_term:(x) ltac_open_term:(y); simpl. |
| 221 |
|
| 222 |
Tactic Notation (at level 0) "repl" uconstr(x) "with" uconstr(y) "by" tactic(t) := |
| 223 |
elpi replace ltac_open_term:(x) ltac_open_term:(y); try (simpl; t). |
| 224 |
|
| 225 |
Lemma hard_example : forall l, map (fun x => x + 1) l = map (fun x => 1 + x) l. |
| 226 |
Proof. |
| 227 |
intros l. |
| 228 |
repl (x + 1) with (1 + x) by ring. |
| 229 |
reflexivity. |
| 230 |
Qed. |
| 231 |
|
| 232 |
Lemma hard_example2 : forall l, map2 (fun x y => x + 0 + y) l l = map2 (fun x y => y + x) l l. |
| 233 |
Proof. |
| 234 |
intros l. |
| 235 |
repl (x + 0 + y) with (y + x) by ring. |
| 236 |
reflexivity. |
| 237 |
Qed. |
| 238 |
|
| 239 |
(* An extended version of this tactic by Y. Bertot can be found in the tests/ |
| 240 |
directory under the name test_open_terms.v *) |