| 1 |
/* constructor name first class representation */ |
| 2 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 3 |
/* ------------------------------------------------------------------------- */ |
| 4 |
|
| 5 |
% not necessary, but gives pointers to relevant files |
| 6 |
shorten std.{ fold-map , do! , last }. % from elpi-builtin.elpi |
| 7 |
shorten coq.{ build-match , bind-ind-arity }. % from coq-lib.elpi |
| 8 |
shorten coq.{ elaborate-skeleton }. % from coq-builtin.elpi |
| 9 |
|
| 10 |
% if we load this file together with others files, we avoid chlashes |
| 11 |
namespace derive.tag { |
| 12 |
|
| 13 |
% we return the clauses for the tag.db since we may need them right away |
| 14 |
% if we run other derivations immediately |
| 15 |
func main inductive, string -> list prop. |
| 16 |
main I Prefix CL :- do! [ |
| 17 |
|
| 18 |
% build fun params (x : I params) => ... do-match ... |
| 19 |
coq.env.indt I _ _ _ Arity _ _, |
| 20 |
bind-ind-arity (global (indt I)) Arity do-match BodyR, |
| 21 |
|
| 22 |
% typecheck (and infer univ constraints) |
| 23 |
std.assert-ok! (elaborate-skeleton BodyR Ty Body) "derive.tag generates illtyped code", |
| 24 |
|
| 25 |
% save constant |
| 26 |
coq.ensure-fresh-global-id (Prefix ^ "tag") Name, |
| 27 |
coq.env.add-const Name Body Ty ff C, |
| 28 |
|
| 29 |
% store in the DB the tag function, so that other Elpi commands can find it |
| 30 |
CL = [tag-for I C], |
| 31 |
std.forall CL (x\ coq.elpi.accumulate _ "derive.tag.db" (clause _ _ x)), |
| 32 |
]. |
| 33 |
|
| 34 |
% We build the match with dummy branches (you get the lambdas for the |
| 35 |
% arguments of constructors, then Prop). Then we put the right number in place. |
| 36 |
func do-match term, list term, list term -> term. |
| 37 |
do-match _ Vars Tys (match X Rty BL1) :- do! [ |
| 38 |
last Vars X, % the last variable is the one for the inductive type |
| 39 |
last Tys XTy, |
| 40 |
build-match X XTy do-rty do-dummy-branch (match X Rty BL), |
| 41 |
fold-map BL {{ 1 }} do-branch BL1 _, |
| 42 |
]. |
| 43 |
|
| 44 |
% builds the return clause of the match |
| 45 |
func do-rty term, list term,list term -> term. |
| 46 |
do-rty _ _ _ {{ lib:elpi.derive.positive }}. |
| 47 |
|
| 48 |
% build each branch |
| 49 |
func do-dummy-branch term, term, list term, list term -> term. |
| 50 |
do-dummy-branch _ _ _ _ {{ Prop }}. % dummy |
| 51 |
|
| 52 |
% [do-branch InTerm Acc NewTem NewAcc] descends into a branch and puts Acc |
| 53 |
% in place of the dummy |
| 54 |
func do-branch term, term -> term, term. |
| 55 |
do-branch {{ Prop }} X X Y :- coq.reduction.lazy.norm {{ Pos.add lp:X 1 }} Y. |
| 56 |
do-branch (fun N T F) X (fun N T R) Y :- |
| 57 |
@pi-decl N T x\ do-branch (F x) X (R x) Y. |
| 58 |
|
| 59 |
} |