| 1 |
/* equality test generation */ |
| 2 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 3 |
/* ------------------------------------------------------------------------- */ |
| 4 |
|
| 5 |
macro @pi-trm N T F :- |
| 6 |
pi x xx\ decl x N T => (feqb.trm->term xx x :- !) => F xx x. |
| 7 |
|
| 8 |
func derive.eqb.main gref, string -> list prop. |
| 9 |
derive.eqb.main (indt I) Prefix CL :- std.do! [ |
| 10 |
std.assert! (eqType (indt I) FI) "this inductive is not supported", |
| 11 |
|
| 12 |
derive.eqb.eqbf.main FI FI [] [] R, |
| 13 |
std.assert-ok! (coq.typecheck R Rty) "derive.eqbf generates illtyped term", |
| 14 |
Name is Prefix ^ "eqb_fields", |
| 15 |
coq.ensure-fresh-global-id Name FName, |
| 16 |
coq.env.add-const FName R Rty ff C, |
| 17 |
EQBF = (global (const C) : term), |
| 18 |
|
| 19 |
derive.eqb.eqb.main FI FI [] [] EQBF R1Skel, |
| 20 |
std.assert-ok! (coq.elaborate-skeleton R1Skel R1ty R1) "derive.eqb generates illtyped term", % need elaborate for prim record |
| 21 |
Name1 is Prefix ^ "eqb", |
| 22 |
coq.ensure-fresh-global-id Name1 FName1, |
| 23 |
coq.env.add-const FName1 R1 R1ty ff C1, |
| 24 |
EQB = (global (const C1) : term), |
| 25 |
|
| 26 |
% populate dbs |
| 27 |
derive.eqb.eqbf.do-clause FI FI [] [] EQBF [] CL1, |
| 28 |
derive.eqb.eqb.do-clause FI FI [] [] EQB [] CL2, |
| 29 |
CL = [CL1,CL2,eqb-done (indt I)], |
| 30 |
std.forall CL (x\ coq.elpi.accumulate _ "derive.eqb.db" (clause _ _ x)), |
| 31 |
]. |
| 32 |
derive.eqb.main (const C) Prefix CL :- std.do! [ |
| 33 |
coq.env.const C (some T) _, |
| 34 |
std.assert! (eqb-for T T EQB) "cannot derive eqb", |
| 35 |
Name is Prefix ^ "eqb", |
| 36 |
coq.ensure-fresh-global-id Name FName, |
| 37 |
X = (global (const C) : term), |
| 38 |
coq.env.add-const FName EQB {{ lp:X -> lp:X -> bool }} @transparent! EQBC, |
| 39 |
CL = [eqb-done (const C), eqb-for (global (const C)) (global (const C)) (global (const EQBC))], |
| 40 |
std.forall CL (x\ coq.elpi.accumulate _ "derive.eqb.db" (clause _ (before "eqb-for:whd") x)), |
| 41 |
]. |
| 42 |
% derive.eqb.main (indc _) _ _ :- stop "eqrive.eqb cannot be called on constructors". |
| 43 |
|
| 44 |
namespace derive.eqb.eqb { |
| 45 |
|
| 46 |
% ----------------------------------------------------------------------------- |
| 47 |
func main eqb.eqType, eqb.eqType, list term, list term, term -> term. |
| 48 |
|
| 49 |
main (eqb.type-param FI) (eqb.type-param FJ) PI PJ EF {{ fun (x : Type) (eqx : x -> x -> bool) => lp:(R x eqx) }} :- |
| 50 |
@pi-trm `x` {{ Type }} y\x\ |
| 51 |
@pi-decl `eqx` {{ lp:x -> lp:x -> bool }} eqx\ |
| 52 |
main (FI y) (FJ y) [x|PI] [x|PJ] {coq.mk-app EF [x,eqx]} (R x eqx). |
| 53 |
|
| 54 |
main (eqb.value-param TYI FI) (eqb.value-param TYJ FJ) PI PJ EF {{ fun (x : lp:TI) (y : lp:TJ) => lp:(R x y) }} :- |
| 55 |
feqb.trm->term TYI TI, |
| 56 |
feqb.trm->term TYJ TJ, |
| 57 |
@pi-trm `x` TI xx\x\ |
| 58 |
@pi-trm `y` TJ yy\y\ |
| 59 |
main (FI xx) (FJ yy) [x|PI] [y|PJ] {coq.mk-app EF [x,y]} (R x y). |
| 60 |
|
| 61 |
main (eqb.inductive Ind _) (eqb.inductive Ind _) PI PJ EF {{ fix rec (x1 : lp:I) (x2 : lp:J) {struct x1} : bool := lp:(R rec x1 x2) }} :- coq.env.recursive? Ind, !, |
| 62 |
coq.mk-app (global (indt Ind)) {std.rev PI} I, |
| 63 |
coq.mk-app (global (indt Ind)) {std.rev PJ} J, |
| 64 |
@pi-decl `rec` {{ lp:I -> lp:J -> bool }} rec\ |
| 65 |
@pi-decl `x1` I x1\ |
| 66 |
@pi-decl `x2` J x2\ |
| 67 |
do-match x1 I x2 J {coq.mk-app EF [rec]} (R rec x1 x2). |
| 68 |
|
| 69 |
main (eqb.inductive Ind _) (eqb.inductive Ind _) PI PJ EF {{ fun (x1 : lp:I) (x2 : lp:J) => lp:(R x1 x2) }} :- |
| 70 |
coq.mk-app (global (indt Ind)) {std.rev PI} I, |
| 71 |
coq.mk-app (global (indt Ind)) {std.rev PJ} J, |
| 72 |
@pi-decl `x1` I x1\ |
| 73 |
@pi-decl `x2` J x2\ |
| 74 |
do-match x1 I x2 J {coq.mk-app EF [{{fun (_ : lp:I) (_ : lp:J) => true}}]} (R x1 x2). |
| 75 |
|
| 76 |
% ----------------------------------------------------------------------------- |
| 77 |
func do-match term, term, term, term, term -> term. |
| 78 |
do-match X1 I X2 J F R :- |
| 79 |
coq.build-match X1 I |
| 80 |
(_\_\_\r\ r = {{ bool }}) |
| 81 |
(do-branch X2 J F) |
| 82 |
R. |
| 83 |
|
| 84 |
% ----------------------------------------------------------------------------- |
| 85 |
func do-branch term, term, term, term, term, list term, list term -> term. |
| 86 |
do-branch X2 J F K KTY Vars _ {{ @eqb_core_defs.eqb_body _ _ _ _ lp:FLDP lp:F lp:TAG lp:X lp:X2 }} :- std.do! [ |
| 87 |
coq.safe-dest-app KTY (global (indt I)) ParamsI, |
| 88 |
std.assert! (fields-for I _ FLD _ _) "derive.eqb: run derive.fields before", |
| 89 |
coq.safe-dest-app J _ ParamsJ, |
| 90 |
coq.mk-app (global (const FLD)) ParamsJ FLDP, |
| 91 |
|
| 92 |
std.assert! (tag-for I T) "derive.eqb: run derive.tag before", |
| 93 |
coq.mk-app (global (const T)) {std.append ParamsI [{coq.mk-app K Vars}]} TAG, |
| 94 |
|
| 95 |
coq.safe-dest-app K (global (indc KI)) _, |
| 96 |
std.assert! (box-for KI _ BK) "derive.eqb: run derive.fields before", |
| 97 |
coq.mk-app (global (indc BK)) {std.append ParamsI Vars} X, |
| 98 |
]. |
| 99 |
|
| 100 |
% ----------------------------------------------------------------------------- |
| 101 |
% example: eqb-for {{ list lp:A }} {{ @list_eqb lp:A lp:F }} :- eqb-for A F. |
| 102 |
|
| 103 |
:index (1) |
| 104 |
func do-clause eqb.eqType, eqb.eqType, list term, list term, term, list prop -> prop. |
| 105 |
|
| 106 |
do-clause (eqb.type-param AI) (eqb.type-param AJ) PI PJ F Todo (pi a ea\ C a ea) :- !, |
| 107 |
pi x a ea\ |
| 108 |
do-clause (AI x) (AJ x) [a|PI] [a|PJ] {coq.mk-app F [a,ea]} [eqb-for a a ea|Todo] (C a ea). |
| 109 |
|
| 110 |
do-clause (eqb.value-param _ AI) (eqb.value-param _ AJ) PI PJ F Todo (pi a b\ C a b) :- !, |
| 111 |
pi x a b\ |
| 112 |
do-clause (AI x) (AJ x) [a|PI] [b|PJ] {coq.mk-app F [a,b]} Todo (C a b). |
| 113 |
|
| 114 |
do-clause (eqb.inductive Ind _) (eqb.inductive Ind _) PI PJ F Todo (eqb-for I J F :- Todo) :- |
| 115 |
coq.mk-app (global (indt Ind)) {std.rev PI} I, |
| 116 |
coq.mk-app (global (indt Ind)) {std.rev PJ} J. |
| 117 |
|
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
namespace derive.eqb.eqbf { |
| 122 |
|
| 123 |
% ----------------------------------------------------------------------------- |
| 124 |
:index (1 1) |
| 125 |
func main eqb.eqType, eqb.eqType, list term, list term -> term. |
| 126 |
|
| 127 |
main (eqb.type-param FI) (eqb.type-param FJ) PI PJ {{ fun (p : Type) (eqp : p -> p -> bool) => lp:(R p eqp) }} :- |
| 128 |
@pi-trm `P` {{ Type }} x\p\ |
| 129 |
@pi-decl `eqP` {{ lp:p -> lp:p -> bool }} eqP\ |
| 130 |
eqb-for p p eqP => |
| 131 |
main (FI x) (FJ x) [p|PI] [p|PJ] (R p eqP). |
| 132 |
|
| 133 |
main (eqb.value-param TYI FI) (eqb.value-param TYJ FJ) PI PJ {{ fun (x y : lp:T) => lp:(R x y) }} :- |
| 134 |
feqb.trm->term TYI TI, |
| 135 |
feqb.trm->term TYJ TJ, |
| 136 |
@pi-trm `P` TI xx\x\ |
| 137 |
@pi-trm `P` TJ yy\y\ |
| 138 |
main (FI xx) (FJ yy) [x|PI] [y|PJ] (R x y). |
| 139 |
|
| 140 |
main (eqb.inductive Ind F) (eqb.inductive Ind G) PI PJ {{ fun (rec : lp:I -> lp:J -> bool) (x : positive) => lp:(R rec x) }} :- std.do! [ |
| 141 |
std.rev PI ParamsI, |
| 142 |
std.rev PJ ParamsJ, |
| 143 |
coq.mk-app (global (indt Ind)) ParamsI I, |
| 144 |
coq.mk-app (global (indt Ind)) ParamsJ J, |
| 145 |
std.assert! (fields-for Ind F_t _ _ _) "derive.eqb: run derive.fields before", |
| 146 |
coq.mk-app (global (const F_t)) ParamsI Fields_t_I, |
| 147 |
coq.mk-app (global (const F_t)) ParamsJ Fields_t_J, |
| 148 |
(@pi-decl `rec` {{ lp:I -> lp:J -> bool }} rec\ |
| 149 |
@pi-decl `x` {{ positive }} x\ |
| 150 |
eqb-for I J rec => |
| 151 |
pi i j\ |
| 152 |
(feqb.trm->term i I :- !) => |
| 153 |
(feqb.trm->term j J :- !) => |
| 154 |
derive.fields.splay-over-positive x {std.zip (F i) (G j)} (rty Fields_t_I Fields_t_J) |
| 155 |
{{ fun (_ : lib:elpi.derive.unit) (_ : lib:elpi.derive.unit) => true }} |
| 156 |
(fields ParamsI ParamsJ) |
| 157 |
(R rec x)), |
| 158 |
]. |
| 159 |
func rty term, term, term -> term. |
| 160 |
rty Fields_t_I Fields_t_J X {{ lp:Fields_t_I lp:X -> lp:Fields_t_J lp:X -> bool }}. |
| 161 |
|
| 162 |
% ----------------------------------------------------------------------------- |
| 163 |
func fields list term, list term, pair eqb.constructor eqb.constructor -> term. |
| 164 |
|
| 165 |
fields ParamsI ParamsJ (pr (eqb.constructor K (eqb.stop _)) (eqb.constructor K (eqb.stop _))) {{ fun (a : lp:BoxTy1) (b : lp:BoxTy2) => true }} :- !, std.do! [ |
| 166 |
std.assert! (box-for K IB _) "derive.eqb: run derive.fields before", |
| 167 |
coq.mk-app (global (indt IB)) ParamsI BoxTy1, |
| 168 |
coq.mk-app (global (indt IB)) ParamsJ BoxTy2, |
| 169 |
]. |
| 170 |
|
| 171 |
fields ParamsI ParamsJ (pr (eqb.constructor K Args) (eqb.constructor K Args2)) {{ fun (a : lp:BoxTy1) (b : lp:BoxTy2) => lp:(R a b) }} :- std.do! [ |
| 172 |
std.assert! (box-for K IB _) "derive.eqb: run derive.fields before", |
| 173 |
coq.mk-app (global (indt IB)) ParamsI BoxTy1, |
| 174 |
coq.mk-app (global (indt IB)) ParamsJ BoxTy2, |
| 175 |
@pi-decl `a` BoxTy a\ |
| 176 |
@pi-decl `b` BoxTy b\ |
| 177 |
coq.build-match a BoxTy1 fields.rty1 (fields.branch1 b BoxTy2 Args Args2) (R a b) |
| 178 |
]. |
| 179 |
|
| 180 |
func fields.rty1 term, list term, list term -> term. |
| 181 |
fields.rty1 _ _ _ {{ bool }}. |
| 182 |
|
| 183 |
:index (1) |
| 184 |
func fields.branch1 term, term, eqb.arguments, eqb.arguments, term, term, list term, list term -> term. |
| 185 |
fields.branch1 B BoxTy2 Args Args2 _ _ VarsA _ R :- |
| 186 |
coq.build-match B BoxTy2 fields.rty2 (fields.branch2 Args Args2 VarsA) R. |
| 187 |
|
| 188 |
func fields.rty2 term, list term, list term -> term. |
| 189 |
fields.rty2 _ _ _ {{ bool }}. |
| 190 |
|
| 191 |
:index (1) |
| 192 |
func fields.branch2 eqb.arguments, eqb.arguments, list term, term, term, list term, list term -> term. |
| 193 |
fields.branch2 Args Args2 VarsA _ _ VarsB _ R :- |
| 194 |
fields.aux Args Args2 VarsA VarsB R. |
| 195 |
|
| 196 |
|
| 197 |
func mk-eqb-for term, term -> term. |
| 198 |
mk-eqb-for T1 T2 R :- eqb-for T1 T2 R, !. |
| 199 |
mk-eqb-for T1 _ _ :- |
| 200 |
Msg is "derive.eqb: missing boolean equality for " ^ {coq.term->string T1} ^ ", maybe use derive.eqb first", |
| 201 |
stop Msg. |
| 202 |
|
| 203 |
func fields.aux eqb.arguments, eqb.arguments, list term, list term -> term. |
| 204 |
|
| 205 |
fields.aux (eqb.dependent TYX FX) (eqb.dependent TYY FY) [X|XS] [Y|YS] {{ lib:elpi.andb (lp:EQB lp:X lp:Y) lp:R1 }} :- |
| 206 |
feqb.trm->term TYX TX, |
| 207 |
feqb.trm->term TYY TY, |
| 208 |
mk-eqb-for TX TY EQB, |
| 209 |
(@pi-decl `p` TX n\ pi a\ (feqb.trm->term a n :- !) ==> |
| 210 |
@pi-decl `p` TY m\ pi b\ (feqb.trm->term b m :- !) ==> |
| 211 |
fields.aux (FX a) (FY b) XS YS (R n m)), |
| 212 |
R1 = R X Y. |
| 213 |
|
| 214 |
fields.aux (eqb.regular TYX FX) (eqb.regular TYY FY) [X|XS] [Y|YS] {{ lib:elpi.andb (lp:EQB lp:X lp:Y) lp:R }} :- |
| 215 |
feqb.trm->term TYX TX, |
| 216 |
feqb.trm->term TYY TY, |
| 217 |
mk-eqb-for TX TY EQB, |
| 218 |
fields.aux FX FY XS YS R. |
| 219 |
|
| 220 |
fields.aux (eqb.irrelevant _ FX) (eqb.irrelevant _ FY) [_|XS] [_|YS] R :- fields.aux FX FY XS YS R. |
| 221 |
|
| 222 |
fields.aux (eqb.stop _) (eqb.stop _) [] [] {{ true }}. |
| 223 |
|
| 224 |
% ----------------------------------------------------------------------------- |
| 225 |
% example: |
| 226 |
% eqb-fields {{ list lp:A }} {{ @list_eqb_fields lp:A lp:EA lp:ELA }} :- |
| 227 |
% eqb-for A EA, eqb-for {{ list lp:A }} ELA. |
| 228 |
func do-clause eqb.eqType, eqb.eqType, list term, list term, term, list prop -> prop. |
| 229 |
|
| 230 |
do-clause (eqb.type-param AI) (eqb.type-param AJ) PI PJ F Todo (pi a ea\ C a ea) :- !, |
| 231 |
pi x a ea\ |
| 232 |
do-clause (AI x) (AJ x) [a|PI] [a|PJ] {coq.mk-app F [a,ea]} [eqb-for a a ea|Todo] (C a ea). |
| 233 |
|
| 234 |
do-clause (eqb.value-param _ AI) (eqb.value-param _ AJ) PI PJ F Todo (pi a b\ C a b) :- !, |
| 235 |
pi x a b\ |
| 236 |
do-clause (AI x) (AJ x) [a|PI] [b|PJ] {coq.mk-app F [a,b]} Todo (C a b). |
| 237 |
|
| 238 |
do-clause (eqb.inductive Ind _) (eqb.inductive Ind _) PI PJ F Todo (pi ela\ eqb-fields I J (F1 ela) :- [C ela|Todo]) :- !, |
| 239 |
coq.mk-app (global (indt Ind)) {std.rev PI} I, |
| 240 |
coq.mk-app (global (indt Ind)) {std.rev PJ} J, |
| 241 |
pi ela\ |
| 242 |
(coq.mk-app F [ela] (F1 ela), C ela = eqb-for I J ela). |
| 243 |
|
| 244 |
} |