| 1 |
|
| 2 |
(** |
| 3 |
This file builds a reflexive tactic solving equalities in a monoid. |
| 4 |
|
| 5 |
We use elpi to: |
| 6 |
- reify an expression into a syntax tree |
| 7 |
- query a Db of known monoids |
| 8 |
- call a few ltac primitives in order to apply the corrctness theorem |
| 9 |
|
| 10 |
*) |
| 11 |
|
| 12 |
From elpi Require elpi. |
| 13 |
From Stdlib Require Arith ZArith Psatz List ssreflect. |
| 14 |
|
| 15 |
Import elpi. |
| 16 |
Import Arith ZArith Psatz List ssreflect. |
| 17 |
|
| 18 |
(* This module contains the reflexive normalizer and its correctness proof *) |
| 19 |
Module MonoidTheory. |
| 20 |
|
| 21 |
(* The syntax of terms *) |
| 22 |
Inductive lang := |
| 23 |
| var (i : nat) (* i-th variable in the context *) |
| 24 |
| zero : lang (* Neutral element *) |
| 25 |
| add (x y : lang). (* binary operation *) |
| 26 |
|
| 27 |
(* Interpretation to T *) |
| 28 |
Fixpoint interp T (e : T) (op : T -> T -> T) (gamma : list T) (t : lang) : T := |
| 29 |
match t with |
| 30 |
| var v => nth v gamma e |
| 31 |
| zero => e |
| 32 |
| add x y => op (interp T e op gamma x) (interp T e op gamma y) |
| 33 |
end. |
| 34 |
|
| 35 |
|
| 36 |
(* normalization of parentheses and units *) |
| 37 |
Fixpoint normAx t1 acc := |
| 38 |
match t1 with |
| 39 |
| add t1 t2 => normAx t1 (normAx t2 acc) |
| 40 |
| _ => add t1 acc |
| 41 |
end. |
| 42 |
Fixpoint normA t := |
| 43 |
match t with |
| 44 |
| add s1 s2 => normAx s1 (normA s2) |
| 45 |
| _ => t |
| 46 |
end. |
| 47 |
Fixpoint norm1 t := |
| 48 |
match t with |
| 49 |
| var _ => t |
| 50 |
| zero => t |
| 51 |
| add t1 t2 => |
| 52 |
match norm1 t1 with |
| 53 |
| zero => norm1 t2 |
| 54 |
| t1 => |
| 55 |
match norm1 t2 with |
| 56 |
| zero => t1 |
| 57 |
| t2 => add t1 t2 |
| 58 |
end |
| 59 |
end |
| 60 |
end. |
| 61 |
Definition norm t := normA (norm1 t). |
| 62 |
|
| 63 |
(* Correctness theorem. This is not the point of this demo, please don't |
| 64 |
look at the proofs ;-) *) |
| 65 |
Section assoc_reflection_proofs. |
| 66 |
Variable T : Type. |
| 67 |
Variable unit : T. |
| 68 |
Variable op : T -> T -> T. |
| 69 |
Hypothesis op_assoc : forall a b c, op a (op b c) = op (op a b) c. |
| 70 |
Hypothesis unit_l : forall a, op unit a = a. |
| 71 |
Hypothesis unit_r : forall a, op a unit = a. |
| 72 |
|
| 73 |
Lemma normAxA t1 t2 : normAx t1 (normA t2) = normA (add t1 t2). |
| 74 |
Proof. by []. Qed. |
| 75 |
|
| 76 |
Lemma normAx_add {t1 t2 s} : normAx t1 t2 = s -> exists s1 s2, s = add s1 s2. |
| 77 |
Proof. |
| 78 |
elim: t1 t2 s => /= [i||w1 H1 w2 H2] t2 s <-; try by do 2 eexists; reflexivity. |
| 79 |
by case E: (normAx _ _); case/H1: E => s1 [s2 ->]; do 2 eexists; reflexivity. |
| 80 |
Qed. |
| 81 |
|
| 82 |
Lemma norm1_zero {l t} : norm1 t = zero -> interp T unit op l t = unit. |
| 83 |
Proof. |
| 84 |
by elim: t => [||s1 + s2] //=; case E1: (norm1 s1); case E2: (norm1 s2) => //= -> // ->. |
| 85 |
Qed. |
| 86 |
|
| 87 |
Lemma norm_zero {l t} : norm t = zero -> interp T unit op l t = unit. |
| 88 |
Proof. |
| 89 |
rewrite /norm; case E: (norm1 t) => [||x y] //; first by rewrite (norm1_zero E). |
| 90 |
by move/normAx_add=> [] ? []. |
| 91 |
Qed. |
| 92 |
|
| 93 |
Lemma norm1_var {l t j} : norm1 t = var j -> interp T unit op l t = nth j l unit. |
| 94 |
Proof. |
| 95 |
elim: t => [i [->]||s1 + s2] //=; case E1: (norm1 s1); case E2: (norm1 s2) => //=. |
| 96 |
by rewrite (norm1_zero E2) unit_r => + _ H => ->. |
| 97 |
by rewrite (norm1_zero E1) unit_l => _ + H => ->. |
| 98 |
Qed. |
| 99 |
|
| 100 |
Lemma norm_var {l t j} : norm t = var j -> interp T unit op l t = nth j l unit. |
| 101 |
Proof. |
| 102 |
rewrite /norm; case E: (norm1 t); rewrite /= ?(norm1_var E) //; first by move=> [->]. |
| 103 |
by move/normAx_add=> [] ? []. |
| 104 |
Qed. |
| 105 |
|
| 106 |
Lemma norm1_add {l t s1 s2} : norm1 t = add s1 s2 -> interp T unit op l t = op (interp T unit op l s1) (interp T unit op l s2). |
| 107 |
Proof. |
| 108 |
elim: t s1 s2 => [||w1 + w2 + s1 s2] //=. |
| 109 |
by case E1: (norm1 w1); case E2: (norm1 w2) => //= ++ [<- <-] /=; |
| 110 |
do 2! [ move=> /(_ _ _ (refl_equal _)) -> | move=> _]; |
| 111 |
rewrite ?(norm1_zero E1) ?(norm1_zero E2) ?(norm1_var E1) ?(norm1_var E2). |
| 112 |
Qed. |
| 113 |
|
| 114 |
Lemma normAxP {l t1 t2} : interp T unit op l (normAx t1 t2) = op (interp T unit op l t1) (interp T unit op l t2). |
| 115 |
Proof. by elim: t1 t2 => [||s1 Hs1 s2 Hs2] t2 //=; rewrite Hs1 Hs2 !op_assoc. Qed. |
| 116 |
|
| 117 |
Lemma normAP {l} t : interp T unit op l (normA t) = interp T unit op l t. |
| 118 |
Proof. by elim: t => //= x Hx y Hy; rewrite normAxP Hy. Qed. |
| 119 |
|
| 120 |
Lemma norm_add {l t s1 s2} : norm t = add s1 s2 -> interp T unit op l t = op (interp T unit op l s1) (interp T unit op l s2). |
| 121 |
Proof. |
| 122 |
rewrite /norm; case E: (norm1 t) => [||x y]; rewrite //= (norm1_add E). |
| 123 |
elim: x y s1 s2 {E} => //= [>[<- <- //=]|>[<- <-]|x + y + w s1 s2]; rewrite ?normAP //= ?unit_l //. |
| 124 |
by rewrite normAxA -!op_assoc => ++ E => /(_ _ _ _ E) ->. |
| 125 |
Qed. |
| 126 |
|
| 127 |
Lemma normP_ l t1 t2 : norm t1 = norm t2 -> interp T unit op l t1 = interp T unit op l t2. |
| 128 |
Proof. |
| 129 |
case E: (norm t2) => [?||??] => [/norm_var|/norm_zero|/norm_add] ->. |
| 130 |
by rewrite (norm_var E). |
| 131 |
by rewrite (norm_zero E). |
| 132 |
by rewrite (norm_add E). |
| 133 |
Qed. |
| 134 |
|
| 135 |
End assoc_reflection_proofs. |
| 136 |
End MonoidTheory. |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
(* Finally, let's build the tactic *) |
| 141 |
|
| 142 |
Import MonoidTheory. |
| 143 |
|
| 144 |
(* This is the correctness theorem of our normalizer *) |
| 145 |
Definition normP {T e op gamma t1 t2} p1 p2 p3 H := @normP_ T e op p1 p2 p3 gamma t1 t2 H. |
| 146 |
About normP. |
| 147 |
Open Scope Z_scope. |
| 148 |
|
| 149 |
Goal forall x y z t, (x + y) + (z + 0 + t) = x + (y + z) + t. |
| 150 |
Proof. |
| 151 |
intros. |
| 152 |
|
| 153 |
change (interp Z Z.zero Z.add (x::y::z::t::nil) |
| 154 |
(add (add (var 0) (var 1)) (add (add (var 2) zero) (var 3))) |
| 155 |
= |
| 156 |
interp Z Z.zero Z.add (x::y::z::t::nil) |
| 157 |
(add (add (var 0) (add (var 1) (var 2))) (var 3))). |
| 158 |
|
| 159 |
apply: normP Z.add_assoc Z.add_0_l Z.add_0_r _. |
| 160 |
|
| 161 |
reflexivity. |
| 162 |
Qed. |
| 163 |
|
| 164 |
(** Now, let's implement a tactic that does for us: |
| 165 |
- the change step (reification) |
| 166 |
- apply the correctness lemma |
| 167 |
*) |
| 168 |
|
| 169 |
(* This is for later *) |
| 170 |
Elpi Db monoid.db lp:{{ |
| 171 |
pred is_monoid |
| 172 |
i:term, % type |
| 173 |
o:term, % zero |
| 174 |
o:term, % op |
| 175 |
o:term, % assoc |
| 176 |
o:term, % unit_l |
| 177 |
o:term. % unit_r |
| 178 |
}}. |
| 179 |
|
| 180 |
Elpi Tactic monoid. |
| 181 |
Elpi Accumulate Db monoid.db. |
| 182 |
Elpi Accumulate lp:{{ |
| 183 |
|
| 184 |
% [mem L X N] asserts that X is at position N in L. |
| 185 |
% The list is open-ended, that is it terminates with an Elpi unification |
| 186 |
% variable, so that the list can be extended with new elements if needed. |
| 187 |
% e.g. mem (3 :: L) 4 N ---> L = 4 :: L1, N = 1 |
| 188 |
% Note: we build a Coq list, since we have to generate that anyway. We could |
| 189 |
% use an Elpi list or any other data structure here, but then we would need |
| 190 |
% to convert back anyway. |
| 191 |
pred mem o:term, o:term, o:term. |
| 192 |
mem {{ lp:X :: _ }} X {{ O }} :- !. |
| 193 |
mem {{ _ :: lp:XS }} X {{ S lp:N }} :- mem XS X N. |
| 194 |
|
| 195 |
% Give that [mem] works with open ended lists we need a way to close it (assign |
| 196 |
% nil to the tail) at the very end of reification. |
| 197 |
pred close o:term. |
| 198 |
close {{ nil }} :- !. |
| 199 |
close {{ _ :: lp:XS }} :- close XS. |
| 200 |
|
| 201 |
% [quote Zero Op T AstT L] recognizes Zero and Op in T and generates the |
| 202 |
% corresponding AstT using the "context" L for variables standing for |
| 203 |
% terms that are not Zero nor Op |
| 204 |
pred quote i:term, i:term, i:term, o:term, o:term. |
| 205 |
quote Zero Op {{ lp:Op lp:T1 lp:T2 }} {{ add lp:R1 lp:R2 }} L :- !, |
| 206 |
quote Zero Op T1 R1 L, |
| 207 |
quote Zero Op T2 R2 L. |
| 208 |
quote Zero _ Zero {{ zero }} _ :- !. |
| 209 |
quote _ _ T {{ var lp:R }} L :- mem L T R. |
| 210 |
|
| 211 |
% This preliminary version of the tactic takes as arguments the monoid signature |
| 212 |
% and changes the goal [A = B] into [interp L AstA = interp L AstB] |
| 213 |
solve (goal _ _ {{ @eq lp:T lp:A lp:B }} _ [trm Zero, trm Op] as G) GS :- |
| 214 |
quote Zero Op A AstA L, |
| 215 |
quote Zero Op B AstB L, |
| 216 |
close L, |
| 217 |
% We are very low level here, we assign a term directly to the goal handler |
| 218 |
% while one could use ltac primitives (as we do later) |
| 219 |
Ty = {{ (interp lp:T lp:Zero lp:Op lp:L lp:AstA) |
| 220 |
= (interp lp:T lp:Zero lp:Op lp:L lp:AstB) }}, |
| 221 |
% This implements the "change" tactic |
| 222 |
refine {{ (_ : lp:Ty) }} G GS. |
| 223 |
|
| 224 |
:name "error" |
| 225 |
solve _ _ :- coq.error "Not an equality / no signature provided". |
| 226 |
|
| 227 |
}}. |
| 228 |
|
| 229 |
Tactic Notation "monoid" constr(zero) constr(add) := elpi monoid (zero) (add). |
| 230 |
|
| 231 |
(** Let's test the tactic *) |
| 232 |
Goal forall x y z t, (x + y) + (z + 0 + t) = x + (y + z) + t. |
| 233 |
Proof. |
| 234 |
intros. |
| 235 |
monoid 0 Z.add. |
| 236 |
(* OK, the goal was reified for us, we can use normP now *) |
| 237 |
apply: normP Z.add_assoc Z.add_0_l Z.add_0_r _. |
| 238 |
reflexivity. |
| 239 |
Qed. |
| 240 |
|
| 241 |
(** Now let's register in the Db the monoid for Z *) |
| 242 |
Elpi Accumulate monoid.db lp:{{ |
| 243 |
|
| 244 |
is_monoid {{ Z }} |
| 245 |
{{ 0 }} {{ Z.add }} |
| 246 |
{{ Z.add_assoc }} {{ Z.add_0_l }} {{ Z.add_0_r }}. |
| 247 |
|
| 248 |
}}. |
| 249 |
|
| 250 |
(** Now let's improve the tactic. This time we don't expect the signature but |
| 251 |
rather look it up in the Db. *) |
| 252 |
Ltac my_compute := vm_compute. |
| 253 |
|
| 254 |
Elpi Accumulate monoid lp:{{ |
| 255 |
|
| 256 |
:before "error" |
| 257 |
solve (goal _ _ {{ @eq lp:T lp:A lp:B }} _ [] as G) GL :- |
| 258 |
is_monoid T Zero Op Assoc Ul Ur, |
| 259 |
quote Zero Op A AstA L, |
| 260 |
quote Zero Op B AstB L, |
| 261 |
close L, |
| 262 |
% This time we use higher level combinators, closer to the standard ltac1 ones |
| 263 |
refine {{ @normP lp:T lp:Zero lp:Op lp:L lp:AstA lp:AstB lp:Assoc lp:Ul lp:Ur _ }} G [SubG], |
| 264 |
coq.ltac.thenl [ |
| 265 |
coq.ltac "my_compute", % https://github.com/coq/coq/issues/10769 |
| 266 |
coq.ltac "reflexivity", |
| 267 |
] SubG GL. |
| 268 |
|
| 269 |
}}. |
| 270 |
|
| 271 |
Tactic Notation "monoid" := elpi monoid. |
| 272 |
|
| 273 |
(** Let's test it once more *) |
| 274 |
Goal forall x y z t, (x + y) + (z + 0 + t) = x + (y + z) + t. |
| 275 |
Proof. |
| 276 |
intros. |
| 277 |
monoid. |
| 278 |
Show Proof. |
| 279 |
Qed. |
| 280 |
|
| 281 |
Elpi Accumulate monoid.db lp:{{ |
| 282 |
|
| 283 |
is_monoid {{ Z }} |
| 284 |
{{ 1 }} {{ Z.mul }} |
| 285 |
{{ Z.mul_assoc }} {{ Z.mul_1_l }} {{ Z.mul_1_r }}. |
| 286 |
|
| 287 |
}}. |
| 288 |
|
| 289 |
Goal forall x y z t, (x * y) * (1 * (z + t)) = x * y * (z + t). |
| 290 |
Proof. |
| 291 |
intros. |
| 292 |
monoid. |
| 293 |
Qed. |