/* Derive a function "projnK t -> x" iif t is "K ..x.." */ /* license: GNU Lesser General Public License Version 2.1 or later */ /* ------------------------------------------------------------------------- */ shorten std.{append, any->string, zip, map, map2, nth, do!, assert!, flatten}. % TODO: instead of using a context and integers one could % generate the product for the projected argument and use via subst-prod % rather than using copy like crazy namespace derive.projK { :index (1) func exT-close list int, list (pair term term), list term, term, term -> term, term. exT-close [] _ _ T Ty T Ty1 :- copy Ty Ty1. exT-close [N|Mask] Ctx Args T Ty {{ @existT lp:S lp:P lp:A lp:R1 }} {{ @sigT lp:S lp:P }} :- nth N Ctx (pr X TX), copy TX S, P = fun {coq.name-suffix `i` N} S B, nth N Args A, (pi x\ (copy X x :- !) => exT-close Mask Ctx Args T Ty (R x) (B x)), R1 = R A. :index (1) func sigT-close list int, list (pair term term), term -> term. sigT-close [] _ Ty Ty1 :- copy Ty Ty1. sigT-close [N|Mask] Ctx Ty {{ @sigT lp:S lp:P }} :- nth N Ctx (pr X TX), copy TX S, P = fun {coq.name-suffix `i` N} S B, pi x\ (copy X x :- !) => sigT-close Mask Ctx Ty (B x). func body-branch int, constructor, term, term, list term, list term -> term. body-branch J K K1 _ VS TS R :- default-output J Mask DfltCtx Dflt DfltTy, !, if (coq.safe-dest-app K1 K1HD _, coq.env.global (indc K) K1HD) (nth J VS X, nth J TS XT, zip VS TS Ctx, Args = VS) (X = Dflt, XT = DfltTy, Ctx = DfltCtx, map DfltCtx fst Args), exT-close Mask Ctx Args X XT R _RT. func body-ty int, term, list term, list term -> term. body-ty J _ _ _ SigT :- default-output J Mask Args _ T, !, sigT-close Mask Args T SigT. :index (1) func occurs-list list (pair term term), int, term -> list int. occurs-list [] _ _ []. occurs-list [pr X _|XS] I T [I|IS] :- occurs X T, !, J is I + 1, occurs-list XS J T IS. occurs-list [_|XS] I T IS :- J is I + 1, occurs-list XS J T IS. func mask-for term, list (pair term term) -> list int. mask-for T Args Mask :- occurs-list Args 0 T Mask. pred default-output i:int, % gather infos to generate the match o:list int, % mask: the position of the arguments that occur in the type % of the projected one o:list (pair term term), % a context (term,type) of default values o:term, % the default value for the projected argument o:term. % its type func body-default % makes lambdas for all default arguments + the projected term, % constructor type (begin processed) int, % current argument int, % argument to project term, % inductive type applied to parameters and indexes list (pair term term), % variables for default arguments constructor % constructor to project -> term. body-default (prod N T F) J OJ IT Args K (fun N T B) :- !, mask-for T Args Mask, J1 is J + 1, @pi-decl N T x\ sigma Def\ if (J = OJ) (Def = [default-output J Mask Args x T]) (Def = []), Def ==> body-default (F x) J1 OJ IT {append Args [pr x T]} K (B x). body-default X J OJ IT Args K F :- whd1 X X1, !, body-default X1 J OJ IT Args K F. body-default _ _ J IT _ K (fun `i` IT B) :- !, @pi-decl `i` IT i\ coq.build-match i IT (body-ty J) (body-branch J K) (B i). func body-param % makes lambdas for all parameters and indexes term, % arity of the inductive type term, % type of the constructor to project term, % inductive type applied to the arity processed so far int, % number of Parameters left to process int, % argument to project constructor % constructor to project -> term. body-param (sort _) KTy IT 0 J K R :- !, body-default KTy 0 J IT [] K R. body-param (prod N T F) KTy IT 0 J K (fun N T B) :- !, @pi-decl N T x\ body-param (F x) KTy {coq.mk-app IT [x]} 0 J K (B x). body-param (prod N T F) (prod _ _ FK) IT Pno J K (fun N T B) :- !, Pno1 is Pno - 1, @pi-decl N T x\ body-param (F x) (FK x) {coq.mk-app IT [x]} Pno1 J K (B x). body-param (prod _ _ _ as X) KTy IT N J K R :- whd1 KTy KTy1, !, body-param X KTy1 IT N J K R. body-param X (prod _ _ _ as KTy) IT N J K R :- whd1 X X1, !, body-param X1 KTy IT N J K R. body-param X KTy IT N J K R :- whd1 X X1, whd1 KTy KTy1, !, body-param X1 KTy1 IT N J K R. % --------------------------------------------------------------------- func allK-projs string, int, int, int, term, term, constructor, term -> list prop. allK-projs _ J J _ _ _ _ _ [] :- !. allK-projs Prefix J JN Paramsno Arity ITy K KTy Clauses :- do! [ (@dropunivs! => coq.elaborate-skeleton ITy _ IT ok), body-param Arity KTy IT Paramsno J K RSkel, J1 is J + 1, Name is Prefix ^ {coq.gref->id (indc K)} ^ {any->string J1}, if (coq.elaborate-skeleton RSkel TyR R ok) ((@dropunivs! => coq.env.add-const Name R TyR _ P), Clause = (projK-db K J1 (global (const P)) :- !), coq.elpi.accumulate _ "derive.projK.db" (clause _ (before "projK-db:fail") Clause), Clauses = [Clause|ClausesRest]) (coq.say "skip" Name R, Clauses = ClausesRest), allK-projs Prefix J1 JN Paramsno Arity IT K KTy ClausesRest ]. func for-K string, int, term, term, constructor, term -> list prop. for-K Prefix Paramsno Arity IT K KT Clauses :- do! [ coq.count-prods KT N, Argsno is N - Paramsno, allK-projs Prefix 0 Argsno Paramsno Arity IT K KT Clauses ]. func main inductive, string -> list prop. main GR Prefix Clauses :- do! [ coq.env.global (indt GR) T, coq.env.indt GR _ Paramsno _ Arity Kn Kt, map2 Kn Kt (for-K Prefix Paramsno Arity T) ClausesList, flatten ClausesList Clauses ]. }