| 1 |
/* fields type description and accessor */ |
| 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 |
namespace derive.fields { |
| 9 |
|
| 10 |
pred fields_t. % chose between fields_t and construct |
| 11 |
pred self o:term. |
| 12 |
|
| 13 |
|
| 14 |
func std.stop-do! list prop ->. |
| 15 |
std.stop-do! []. |
| 16 |
std.stop-do! [P|PS] :- coq.say P, ((pi x\ stop x :- !, fail) ==> P), !, std.stop-do! PS. |
| 17 |
std.stop-do! [P|_] :- coq.say "STOP" P. |
| 18 |
|
| 19 |
func main inductive, string -> list prop. |
| 20 |
main I Prefix AllCL :- std.do! [ |
| 21 |
|
| 22 |
std.assert! (tag-for I Tag) "no tag for this inductive, run that derivation first", |
| 23 |
|
| 24 |
std.assert! (eqType(indt I) FI) "this inductive is not supported", |
| 25 |
coq.env.indt I _ _ _ Arity KS _, |
| 26 |
|
| 27 |
box (global (indt I)) KS FI CLB, |
| 28 |
|
| 29 |
(CLB =!=> fields_t.main FI (global (indt I)) Body_t), |
| 30 |
std.assert-ok! (coq.typecheck Body_t Ty_t) "derive.fields generates illtyped fields_t", |
| 31 |
Name_t is Prefix ^ "fields_t", |
| 32 |
coq.ensure-fresh-global-id Name_t FName_t, |
| 33 |
coq.env.add-const FName_t Body_t Ty_t ff Fields_t, |
| 34 |
|
| 35 |
(CLB =!=> fields.main FI (global (indt I)) (global (const Fields_t)) (global (const Tag)) BodySkel), |
| 36 |
% we elaborate only for primitive records... |
| 37 |
std.assert-ok! (coq.elaborate-skeleton BodySkel Ty Body) "derive.fields generates illtyped fields", |
| 38 |
Name is Prefix ^ "fields", |
| 39 |
coq.ensure-fresh-global-id Name FName, |
| 40 |
coq.env.add-const FName Body Ty ff Fields, |
| 41 |
|
| 42 |
(CLB =!=> construct.main FI (global (indt I)) Fields_t Body_c), |
| 43 |
std.assert-ok! (coq.typecheck Body_c Ty_c) "derive.fields generates illtyped construct", |
| 44 |
Name_c is Prefix ^ "construct", |
| 45 |
coq.ensure-fresh-global-id Name_c FName_c, |
| 46 |
coq.env.add-const FName_c Body_c Ty_c ff Construct, |
| 47 |
|
| 48 |
coq.bind-ind-arity (global (indt I)) Arity (case-refl Tag Fields Construct) Body_PSkel, |
| 49 |
std.assert-ok! (coq.elaborate-skeleton Body_PSkel Ty_P Body_P) "derive.fields generates illtyped constructP", |
| 50 |
Name_P is Prefix ^ "constructP", |
| 51 |
coq.ensure-fresh-global-id Name_P FName_P, |
| 52 |
coq.env.add-const FName_P Body_P Ty_P @opaque! ConstructP, |
| 53 |
|
| 54 |
AllCL = [fields-for I Fields_t Fields Construct ConstructP|CLB], |
| 55 |
std.forall AllCL (x\ coq.elpi.accumulate _ "derive.fields.db" (clause _ _ x)), |
| 56 |
|
| 57 |
]. |
| 58 |
|
| 59 |
% ---------------------------------------------------------------------- |
| 60 |
|
| 61 |
:index (1) |
| 62 |
func fields_t.main eqb.eqType, term -> term. |
| 63 |
fields_t.main (eqb.type-param F) I {{ fun p : Type => lp:(R p) }} :- !, |
| 64 |
@pi-trm `p` {{ Type }} x\p\ |
| 65 |
fields_t.main (F x) {coq.mk-app I [p]} (R p). |
| 66 |
|
| 67 |
fields_t.main (eqb.value-param TY F) I {{ fun p : lp:Ty => lp:(R p) }} :- !, |
| 68 |
feqb.trm->term TY Ty, |
| 69 |
@pi-trm `p` Ty x\p\ |
| 70 |
fields_t.main (F x) {coq.mk-app I [p]} (R p). |
| 71 |
|
| 72 |
fields_t.main (eqb.inductive _ F) I {{ fun p : lib:elpi.derive.positive => lp:(R p) }} :- |
| 73 |
coq.safe-dest-app I _ Params, |
| 74 |
@pi-decl `p` {{ lib:elpi.derive.positive }} p\ pi i\ |
| 75 |
splay-over-positive p (F i) fields_t.rty |
| 76 |
{{ unit }} |
| 77 |
(fields_t.k Params) |
| 78 |
(R p). |
| 79 |
|
| 80 |
func fields_t.rty term -> term. |
| 81 |
fields_t.rty _ {{ Type }}. |
| 82 |
|
| 83 |
func fields_t.k list term, eqb.constructor -> term. |
| 84 |
fields_t.k Params (eqb.constructor K _) T :- |
| 85 |
box-for K I _, coq.mk-app (global (indt I)) Params T. |
| 86 |
|
| 87 |
% ---------------------------------------------------------------------- |
| 88 |
func fields.main eqb.eqType, term, term, term -> term. |
| 89 |
fields.main (eqb.type-param F) I F_t Tag {{ fun p : Type => lp:(R p) }} :- !, |
| 90 |
@pi-trm `p` {{ Type }} x\p\ |
| 91 |
fields.main (F x) {coq.mk-app I [p]} {coq.mk-app F_t [p]} {coq.mk-app Tag [p]} (R p). |
| 92 |
|
| 93 |
fields.main (eqb.value-param TY F) I F_t Tag {{ fun p : lp:Ty => lp:(R p) }} :- !, |
| 94 |
feqb.trm->term TY Ty, |
| 95 |
@pi-trm `p` Ty x\p\ |
| 96 |
fields.main (F x) {coq.mk-app I [p]} {coq.mk-app F_t [p]} {coq.mk-app Tag [p]} (R p). |
| 97 |
|
| 98 |
fields.main (eqb.inductive _ _) I F_t Tag {{ fun i : lp:I => lp:(R i) }} :- |
| 99 |
@pi-trm `i` I _\i\ |
| 100 |
coq.build-match i I |
| 101 |
(fields.rty F_t Tag) |
| 102 |
fields.branch |
| 103 |
(R i). |
| 104 |
|
| 105 |
func fields.rty term, term, term, list term, list term -> term. |
| 106 |
fields.rty F_t Tag _ Vars _ {{ lp:F_t (lp:Tag lp:X) }} :- std.last Vars X. |
| 107 |
|
| 108 |
func fields.branch term, term, list term, list term -> term. |
| 109 |
fields.branch K _ Vars _ R :- |
| 110 |
coq.safe-dest-app K (global (indc C)) Params, |
| 111 |
box-for C _ B, |
| 112 |
coq.mk-app (global (indc B)) {std.append Params Vars} R. |
| 113 |
|
| 114 |
% ------------------------------------------------------------------------ |
| 115 |
func box term, list constructor, eqb.eqType -> list prop. |
| 116 |
box I KL Decl CL :- std.do! [ |
| 117 |
box.aux Decl I ILDr, |
| 118 |
box.declare KL ILDr CL, |
| 119 |
]. |
| 120 |
|
| 121 |
func box.declare list constructor, list box-spec -> list prop. |
| 122 |
box.declare [] [] []. |
| 123 |
box.declare [K|Ks] [real-box I|Bs] [C|Cs] :- |
| 124 |
std.assert-ok! (coq.elaborate-indt-decl-skeleton I D) "ILD", |
| 125 |
coq.env.add-indt D IB, |
| 126 |
coq.env.indt IB _ _ _ _ [KB] _, |
| 127 |
C = box-for K IB KB, |
| 128 |
(C ==> box.declare Ks Bs Cs). |
| 129 |
box.declare [K|Ks] [same-box K1|Bs] [box-for K IB KB|Cs] :- |
| 130 |
box-for K1 IB KB, |
| 131 |
box.declare Ks Bs Cs. |
| 132 |
|
| 133 |
kind box-spec type. |
| 134 |
type real-box indt-decl -> box-spec. |
| 135 |
type same-box constructor -> box-spec. |
| 136 |
|
| 137 |
func box.aux eqb.eqType, term -> list box-spec. |
| 138 |
box.aux (eqb.type-param F) I L :- |
| 139 |
(@pi-trm `p` {{ Type }} x\y\ |
| 140 |
box.aux (F x) {coq.mk-app I [y]} (L1 y)), |
| 141 |
distribute L1 (f\r\ sigma U\ r = parameter "A" explicit (sort (typ U)) f) L. |
| 142 |
|
| 143 |
box.aux (eqb.value-param TY F) I L :- |
| 144 |
feqb.trm->term TY Ty, |
| 145 |
(@pi-trm `p` Ty x\y\ box.aux (F x) {coq.mk-app I [y]} (L1 y)), |
| 146 |
distribute L1 (f\r\ r = parameter "v" explicit Ty f) L. |
| 147 |
|
| 148 |
box.aux (eqb.inductive Ind F) I L :- pi x\ box.aux2 I Ind x (F x) L. |
| 149 |
|
| 150 |
pred args-of o:constructor, o:eqb.arguments. |
| 151 |
|
| 152 |
:index(_ _ _ 2) |
| 153 |
func box.aux2 term, inductive, eqb.trm, list eqb.constructor -> list box-spec. |
| 154 |
box.aux2 _ _ _ [] []. |
| 155 |
box.aux2 I Ind X [eqb.constructor _ Args|MoreKs] [same-box K|MoreBoxes] :- args-of K Args, !, |
| 156 |
box.aux2 I Ind X MoreKs MoreBoxes. |
| 157 |
box.aux2 I Ind X [eqb.constructor K Args|MoreKs] [real-box (record ID1 S ID2 Fields)|MoreBoxes] :- |
| 158 |
if (coq.env.informative? Ind) (S = {{ Type }}) (S = {{ Prop }}), |
| 159 |
ID1 is "box_" ^ {coq.gref->id (indt Ind)} ^ "_" ^ {coq.gref->id (indc K)}, |
| 160 |
ID2 is "Box_" ^ {coq.gref->id (indt Ind)} ^ "_" ^ {coq.gref->id (indc K)}, |
| 161 |
((feqb.trm->term X I :- !) ==> box.box-argument ID2 0 Args Fields), |
| 162 |
args-of K Args ==> box.aux2 I Ind X MoreKs MoreBoxes. |
| 163 |
|
| 164 |
:index(_ _ 1) |
| 165 |
func box.box-argument string, int, eqb.arguments -> record-decl. |
| 166 |
box.box-argument _ _ (eqb.stop _) end-record. |
| 167 |
box.box-argument S M (eqb.regular T Args) (field [] N TY _\A) :- N is S ^ "_" ^ {std.any->string M}, |
| 168 |
feqb.trm->term T TY, box.box-argument S {calc (M + 1)} Args A. |
| 169 |
box.box-argument S M (eqb.irrelevant T Args) (field [] N TY _\A) :- N is S ^ "_" ^ {std.any->string M}, |
| 170 |
feqb.trm->term T TY, box.box-argument S {calc (M + 1)} Args A. |
| 171 |
box.box-argument S M (eqb.dependent T Args) (field [] N TY A) :- N is S ^ "_" ^ {std.any->string M}, |
| 172 |
feqb.trm->term T TY, |
| 173 |
pi x y\ (feqb.trm->term x y :- !) ==> box.box-argument S {calc (M + 1)} (Args x) (A y). |
| 174 |
|
| 175 |
func distribute (A -> list box-spec), (func (A -> indt-decl) -> indt-decl) -> list box-spec. |
| 176 |
distribute (_\ []) _ [] :- !. |
| 177 |
distribute (x\ [real-box (X x)| XS x]) F [real-box F_X|R] :- !, F X F_X, distribute XS F R. |
| 178 |
distribute (x\ [same-box K| XS x]) F [same-box K|R] :- distribute XS F R. |
| 179 |
|
| 180 |
% ---------------------------------------------------------------------- |
| 181 |
:index(1) |
| 182 |
func construct.main eqb.eqType, term, constant -> term. |
| 183 |
construct.main (eqb.type-param F) I F_t {{ fun p : Type => lp:(R p) }} :- |
| 184 |
@pi-trm `p` {{ Type }} x\p\ |
| 185 |
construct.main (F x) {coq.mk-app I [p]} F_t (R p). |
| 186 |
|
| 187 |
construct.main (eqb.value-param TY F) I F_t {{ fun p : lp:Ty => lp:(R p) }} :- |
| 188 |
feqb.trm->term TY Ty, |
| 189 |
@pi-trm `p` Ty x\p\ |
| 190 |
construct.main (F x) {coq.mk-app I [p]} F_t (R p). |
| 191 |
|
| 192 |
construct.main (eqb.inductive _ F) I F_t {{ fun p : lib:elpi.derive.positive => lp:(R p) }} :- |
| 193 |
coq.safe-dest-app I _ Params, |
| 194 |
coq.mk-app (global (const F_t)) Params Fields_t, |
| 195 |
@pi-decl `p` {{ lib:elpi.derive.positive }} p\ pi i\ |
| 196 |
splay-over-positive p (F i) (construct.rty1 Fields_t I) |
| 197 |
{{ fun (_:lib:elpi.derive.unit) => @None lp:I }} |
| 198 |
(construct.k I Params) |
| 199 |
(R p). |
| 200 |
|
| 201 |
func construct.rty1 term, term, term -> term. |
| 202 |
construct.rty1 Fields_t I X {{ lp:Fields_t lp:X -> option lp:I }}. |
| 203 |
|
| 204 |
func construct.k term, list term, eqb.constructor -> term. |
| 205 |
construct.k _ Params (eqb.constructor K (eqb.stop _)) {{ fun b : lp:BoxTy => Some lp:B }} :- !, |
| 206 |
box-for K BT _, |
| 207 |
coq.mk-app (global (indt BT)) Params BoxTy, |
| 208 |
coq.mk-app (global (indc K)) Params B. |
| 209 |
construct.k I Params (eqb.constructor K _) {{ fun b : lp:BoxTy => lp:(R b) }} :- |
| 210 |
box-for K BT _, |
| 211 |
coq.mk-app (global (indt BT)) Params BoxTy, |
| 212 |
@pi-decl `b` BoxTy b\ |
| 213 |
coq.build-match b BoxTy |
| 214 |
(construct.rty I) |
| 215 |
(construct.branch {coq.mk-app (global (indc K)) Params}) |
| 216 |
(R b). |
| 217 |
|
| 218 |
func construct.rty term, term, list term, list term -> term. |
| 219 |
construct.rty I _ _ _ {{ option lp:I }}. |
| 220 |
func construct.branch term, term, term, list term, list term -> term. |
| 221 |
construct.branch B _ _ Vars _ {{ Some lp:BVars }} :- coq.mk-app B Vars BVars. |
| 222 |
|
| 223 |
% ------------------------------------------------------------------------- |
| 224 |
|
| 225 |
% match x return construct (fields x) = Some x with _ => erefl |
| 226 |
func case-refl constant, constant, constant, term, list term, list term -> term. |
| 227 |
case-refl Tag Fields Construct _ ParamsX Tys R :- |
| 228 |
std.appendR Params [X] ParamsX, !, |
| 229 |
coq.mk-app (global (const Tag)) Params TP, |
| 230 |
coq.mk-app (global (const Fields)) Params FP, |
| 231 |
coq.mk-app (global (const Construct)) Params CP, |
| 232 |
coq.build-match X {std.last Tys} |
| 233 |
(case-refl-rty TP FP CP) |
| 234 |
case-refl-branch |
| 235 |
R. |
| 236 |
|
| 237 |
func case-refl-rty term, term, term, term, list term,list term -> term. |
| 238 |
case-refl-rty Tag Fields Construct _ Vs _ {{ lp:Construct (lp:Tag lp:X) (lp:Fields lp:X) = Some lp:X }} :- |
| 239 |
std.last Vs X. |
| 240 |
|
| 241 |
func case-refl-branch term, term, list term,list term -> term. |
| 242 |
case-refl-branch _ _ _ _ {{ refl_equal }}. |
| 243 |
|
| 244 |
func splay-over-positive term, list A, (func term -> term), term, (pred i:A, o:term) -> term. |
| 245 |
splay-over-positive X L DoRty Def DoBranch R :- |
| 246 |
splay-over-positive.aux X (x\x) L DoRty DoBranch Def R. |
| 247 |
func splay-over-positive.aux term, (term -> term), list A, (func term -> term), (pred i:A, o:term), term -> term. |
| 248 |
splay-over-positive.aux _ _ [] _ _ Def Def :- !. |
| 249 |
splay-over-positive.aux _ _ [X] _ DoBranch _ R :- DoBranch X R, !. |
| 250 |
splay-over-positive.aux X XCtx KL DoRty DoBranch Def R :- |
| 251 |
coq.build-match X {{ lib:elpi.derive.positive }} (do-rty XCtx DoRty) (do-branch XCtx DoRty DoBranch Def KL) R. |
| 252 |
|
| 253 |
func do-rty (term -> term), (func term -> term), term, list term, list term -> term. |
| 254 |
do-rty Ctx DoRty _ Vs _ R :- P = Ctx {std.last Vs}, DoRty P R. |
| 255 |
|
| 256 |
:index (1) |
| 257 |
func list-bitmask list A -> list A, list A. |
| 258 |
list-bitmask [] [] []. |
| 259 |
list-bitmask [X] [X] []. |
| 260 |
list-bitmask [X,Y|L] [X|A] [Y|B] :- list-bitmask L A B. |
| 261 |
|
| 262 |
func do-branch (term -> term), (func term -> term), (pred i:A, o:term), term, list A, term, term, list term, list term -> term. |
| 263 |
do-branch PCtx DoRty DoBranch Def [_|KS] {{ xO }} _ [P] _ R :- !, |
| 264 |
list-bitmask KS KODD _, |
| 265 |
splay-over-positive.aux P (x\ PCtx {{ xO lp:x }}) KODD DoRty DoBranch Def R. |
| 266 |
do-branch PCtx DoRty DoBranch Def [_|KS] {{ xI }} _ [P] _ R :- !, |
| 267 |
list-bitmask KS _ KEVEN, |
| 268 |
splay-over-positive.aux P (x\ PCtx {{ xI lp:x }}) KEVEN DoRty DoBranch Def R. |
| 269 |
do-branch _ _ DoBranch _ [X|_] {{ xH }} _ _ _ R :- DoBranch X R, !. |
| 270 |
|
| 271 |
func prod->tuple term -> term. |
| 272 |
prod->tuple (prod N Ty F) {{ { x:lp:Ty & lp:(X x)}%type }} :- |
| 273 |
/*(F = x\prod _ _ _),*/ (pi x\ occurs x (F x)), !, % not the last one and dependent |
| 274 |
@pi-decl N Ty x\ prod->tuple (F x) (X x). |
| 275 |
prod->tuple (prod N Ty F) {{ (lp:Ty * lp:X)%type }} :- /*(F = x\prod _ _ _),*/ !, % not the last one |
| 276 |
@pi-decl N Ty x\ prod->tuple (F x) X. |
| 277 |
prod->tuple (prod _ Ty _) Ty :- !. |
| 278 |
prod->tuple _ {{ unit }}. % other branches |
| 279 |
|
| 280 |
func repack-as-tuple constant, constant, term, list term, list term -> term. |
| 281 |
repack-as-tuple C_t Tag _ Vars Tys R :- |
| 282 |
std.appendR Params [X] Vars, !, |
| 283 |
std.last Tys XTy, |
| 284 |
coq.mk-app (global (const C_t)) Params C_tp, |
| 285 |
coq.mk-app (global (const Tag)) Params Tagp, |
| 286 |
coq.build-match X XTy (do-rty_t C_tp Tagp) args->tuple R. |
| 287 |
|
| 288 |
:index (1) |
| 289 |
func do-rty_t term, term, term, list term, list term -> term. |
| 290 |
do-rty_t C_t Tag _ Vars _ {{ lp:C_t (lp:Tag lp:X) }} :- std.last Vars X. |
| 291 |
|
| 292 |
:index (_ _ 1) |
| 293 |
func args->tuple term, term, list term, list term -> term. |
| 294 |
args->tuple _ _ [] _ {{ tt }}. |
| 295 |
/*args->tuple _ _ [X] _ X.*/ |
| 296 |
args->tuple A B [X|XS] [T|TS] {{ @existT lp:T _ lp:X lp:R }} :- |
| 297 |
occurs X TS, !, |
| 298 |
args->tuple A B XS TS R. |
| 299 |
args->tuple A B [X|XS] [_T|TS] {{ ( lp:X , lp:R ) }} :- |
| 300 |
args->tuple A B XS TS R. |
| 301 |
|
| 302 |
} |