/* map over a container */ /* license: GNU Lesser General Public License Version 2.1 or later */ /* ------------------------------------------------------------------------- */ shorten std.{split-at, length, rev, append, do!, drop-last, assert!}. map-db (app[global (indt GR1)|A1]) (app[global (indt GR2)|A2]) R :- coq.env.indt GR1 _ Lno1 _ _ _ _, coq.env.indt GR2 _ Lno2 _ _ _ _, {length A1} > Lno1, {length A2} > Lno2, split-at Lno1 A1 Args1 Points1, split-at Lno2 A2 Args2 Points2, Points1 = Points2, !, map-db {coq.mk-app (global (indt GR1)) Args1} {coq.mk-app (global (indt GR2)) Args2} F, coq.mk-app F Points1 R. namespace derive.map { % Building the body %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% pred bo-idx i:term, % inductive arity (input) i:term, % inductive type (input) applied to params & idx handled so far i:term, % inductive type (output) applied to params & idx handled so far i:int, % current index no o:int, % Recno i:list term, % rev list of (output) parameters o:term, % body o:term. % type bo-idx (prod _ S1 T1) Ity1 Ity2 N M Ps (fun `x` S1 Bo) (prod `x` S1 Ty) :- !, pi x\ sigma Ity1x Ity2x\ coq.mk-app Ity1 [x] Ity1x, coq.mk-app Ity2 [x] Ity2x, N1 is N + 1, decl x `x` S1 => bo-idx (T1 x) Ity1x Ity2x N1 M Ps (Bo x) (Ty x). bo-idx (sort _) Ity1 Ity2 N N Ps (fun `x` Ity1 Bo) (prod `_` Ity1 _\ Ity2) :- !, @pi-decl `x` Ity1 x\ coq.build-match x Ity1 (bo-idx-rty Ps Ity2) (bo-k-args Ps) (Bo x). bo-idx X Ity1 Ity2 N M Ps R1 R2 :- whd1 X X1, !, bo-idx X1 Ity1 Ity2 N M Ps R1 R2. pred bo-idx-rty i:list term, i:term, i:term, i:list term, i:list term, o:term. bo-idx-rty Ps ItyArgs _ Vs _ R :- rev Vs [_|IdxsRev], rev IdxsRev Idxs, coq.safe-dest-app ItyArgs HD _, coq.mk-app HD {append {rev Ps} Idxs} R. pred bo-k-args i:list term, i:term, i:term, i:list term, i:list term, o:term. bo-k-args ParamsRev K _ Args Tys R :- rev ParamsRev Params, coq.safe-dest-app K (global (indc GR)) _, coq.env.typeof (indc GR) T, coq.subst-prod Params T KT, (bo-k-args.aux {coq.mk-app (global (indc GR)) Params} Args Tys KT R). pred bo-k-args.aux i:term, i:list term, i:list term, i:term, o:term. bo-k-args.aux R [] [] _ R. bo-k-args.aux K [A|As] [T|Ts] (prod _ S Ty) R :- map-db T S F, coq.mk-app F [A] FA, bo-k-args.aux {coq.mk-app K [FA]} As Ts (Ty FA) R. bo-k-args.aux K [A|As] [_|Ts] (prod _ _ Ty) R :- !, bo-k-args.aux {coq.mk-app K [A]} As Ts (Ty A) R. bo-k-args.aux K As Ts X R :- whd1 X X1, !, bo-k-args.aux K As Ts X1 R. pred map-pi i:any, o:list prop. map-pi (x\ []) []. map-pi (x\ [X x| XS x]) [pi x\ X x | YS] :- map-pi XS YS. pred mk-map-ty i:term, % input variable i:term, % and its type i:term, % output variable i:term, % an its type o:term, % type of a mapping function from input to output i:term, % map function (having the type above) o:int, % arity of the predicate o:list prop. % map-db clause for map function mk-map-ty A (prod _ SA T1) B (prod _ SB T2) (prod `x` SA x\ R x) F N C1 :- map-db SA SB Fa, !, (pi x\ sigma Ax Fx BFx \ coq.mk-app A [x] Ax, coq.mk-app Fa [x] Fx, coq.mk-app B [Fx] BFx, mk-map-ty Ax (T1 x) BFx (T2 BFx) (R x) {coq.mk-app F [x]} M (C x), N is M + 1), map-pi C C1. mk-map-ty A (prod _ S1 T1) B (prod _ _ T2) (prod `x` S1 x\ R x) F N C1 :- (pi x\ sigma Ax Bx \ coq.mk-app A [x] Ax, coq.mk-app B [x] Bx, mk-map-ty Ax (T1 x) Bx (T2 x) (R x) {coq.mk-app F [x]} M (C x), N is M + 1), map-pi C C1. mk-map-ty (app[X|XS] as A) _ (app[Y|YS] as B) _ (prod `x` A _\ B) (app [G|GS] as F) 0 [map-db PLA PLB PLF,map-db A B F] :- drop-last 1 XS LA, drop-last 1 YS LB, drop-last 1 GS LF, coq.mk-app X LA PLA, coq.mk-app Y LB PLB, coq.mk-app G LF PLF. mk-map-ty A _ B _ (prod `x` A _\ B) F 0 [map-db A B F]. % Build a clause %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% pred mk-clause i:int, % current parameter i:int, % number of parameters i:term, % inductive type (input) i:term, % inductive type (output) i:term, % arity of the inductive i:list prop, % premises of the clause i:term, % map function o:prop. % clause for map-db mk-clause N N Ity1 Ity2 _ Todo Map (map-db Ity1 Ity2 Map :- Todo). mk-clause N Lno Ity1 Ity2 (prod _ _ T) Todo Map (pi x y f\ C x y f) :- !, N1 is N + 1, pi x y f\ sigma Ity1x Ity2y Mapf\ coq.mk-app Ity1 [x] Ity1x, coq.mk-app Ity2 [y] Ity2y, coq.mk-app Map [x,y,f] Mapf, mk-clause N1 Lno Ity1x Ity2y (T x) [map-db x y f|Todo] Mapf (C x y f). mk-clause N Lno Ity1 Ity2 X Todo Map C :- whd1 X X1, !, mk-clause N Lno Ity1 Ity2 X1 Todo Map C. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% kind mutual-spec type. type mutual-spec inductive -> string -> term -> term -> term -> term -> mutual-spec. func build-mutual-spec bool, string, inductive -> mutual-spec. build-mutual-spec Mutual Prefix GR (mutual-spec GR Prefix1 T T Arity Arity) :- T = global (indt GR), if (Mutual = tt) (Prefix1 is {coq.gref->id (indt GR)} ^ "_") (Prefix1 is Prefix), coq.env.indt GR _ _ _ Arity _ _. func mutual-spec-apply-param term, term, mutual-spec -> mutual-spec. mutual-spec-apply-param A B (mutual-spec GR Prefix Ity1 Ity2 (prod _ _ F1) (prod _ _ F2)) (mutual-spec GR Prefix Ity1A Ity2B (F1 A) (F2 B)) :- !, coq.mk-app Ity1 [A] Ity1A, coq.mk-app Ity2 [B] Ity2B. mutual-spec-apply-param _ _ _ _ :- stop "derive.map: mutual parameter mismatch". func mutual-map-clause mutual-spec, term -> prop. mutual-map-clause (mutual-spec _ _ Ity1 Ity2 _ _) F (map-db Ity1 Ity2 F). pred bo-fix-aux list term, mutual-spec -> triple term term int. bo-fix-aux _ (mutual-spec _ _ Ity1 Ity2 Arity _) (triple Body Ty Recno) :- coq.safe-dest-app Ity2 _ Params, std.rev Params ParamsRev, bo-idx Arity Ity1 Ity2 0 Recno ParamsRev Body Ty. func bo-mfix list mutual-spec, list mutual-spec, list term, list term, list term, list int -> mfix-block, list term, list int. bo-mfix All [] Ps RevF TysRev RecnosRev (mfix-bo Bodies) Tys Recnos :- !, std.do! [ std.rev RevF Funs, std.map2 All Funs mutual-map-clause Clauses, Clauses =!=> std.map All (bo-fix-aux Ps) Bodies_Tys_Recnos, std.map Bodies_Tys_Recnos triple_1 Bodies, std.map Bodies_Tys_Recnos triple_2 Tys, std.map Bodies_Tys_Recnos triple_3 Recnos, std.rev Recnos RecnosRev, std.rev Tys TysRev, ]. bo-mfix All [S|SS] Ps RevF Tys Recnos (mfix-ty N Recno Ty Rest) TysR RecnosR :- S = mutual-spec GR _ _ _ _ _, ID is {coq.gref->id (indt GR)} ^ "_map_rec", coq.id->name ID N, @pi-decl N Ty f\ bo-mfix All SS Ps [f|RevF] [Ty|Tys] [Recno|Recnos] (Rest f) TysR RecnosR. func bo-fix list mutual-spec, list term, int -> term. bo-fix [(mutual-spec GR _ _ _ _ _ as S)] Ps _ (fix N Recno Ty Bo) :- coq.env.recursive? GR, !, ID is {coq.gref->id (indt GR)} ^ "_map_rec", coq.id->name ID N, @pi-decl N Ty f\ mutual-map-clause S f (C f), C f =!=> bo-fix-aux Ps S (triple (Bo f) Ty Recno), coq.typecheck (Bo f) _ ok, !. % the first that typechecks bo-fix [S] Ps _ Bo :- !, std.assert! (bo-fix-aux Ps S (triple BoS _ _), coq.elaborate-skeleton BoS _ Bo ok) "derive.map: failed to generate mutual map body". % primitive records need elaboration bo-fix Specs Ps Focus (mfix Focus Recno Block) :- bo-mfix Specs Specs Ps [] [] [] Block Tys_ Recnos, std.nth Focus Recnos Recno, coq.typecheck (mfix Focus Recno Block) _ ok, !. % the first that typechecks func bo-params int, int, list mutual-spec, int, list term -> term. bo-params Lno Lno Specs Focus Ps R :- !, bo-fix Specs Ps Focus R. bo-params N Lno ([S|_] as Specs) Focus Ps R :- N < Lno, S = mutual-spec _ _ _ _ (prod A Sty1 _Rty1) (prod _ Sty2 _Rty2), !, coq.name-suffix A 1 A1, coq.name-suffix A 2 A2, coq.name-suffix A "f" Af, N1 is N + 1, (pi a b f\ std.assert! (mk-map-ty a Sty1 b Sty2 (FAB a b) f _ (Clause a b f)) "derive.map: failed to generate parameter map clause"), R = (fun A1 Sty1 a\ fun A2 Sty2 b\ fun Af (FAB a b) f\ Bo a b f), pi a b f\ sigma Specs1\ std.map Specs (mutual-spec-apply-param a b) Specs1, Clause a b f => decl a A1 Sty1 => decl b A2 Sty2 => decl f Af (FAB a b) => bo-params N1 Lno Specs1 Focus [b|Ps] (Bo a b f). bo-params _ _ _ _ _ _ :- stop "derive.map: mutual indexed data types not supported". func add-mutual-map-consts list mutual-spec, list mutual-spec, int, int -> list prop. add-mutual-map-consts _ [] _ _ [] :- !. add-mutual-map-consts All [S|SS] Lno Focus [map-done GR,Clause|CS] :- std.do! [ S = mutual-spec GR Prefix T T Arity _, bo-params 0 Lno All Focus [] RSkel, std.assert-ok! (coq.elaborate-skeleton RSkel Ty R) "derive.map generates illtyped mutual term", Name is Prefix ^ "map", coq.ensure-fresh-global-id Name FName, coq.env.add-const FName R Ty @transparent! Funct, mk-clause 0 Lno T T Arity [] (global (const Funct)) Clause, coq.elpi.accumulate _ "derive.map.db" (clause _ _ Clause), coq.elpi.accumulate _ "derive.map.db" (clause _ _ (map-done GR)), Focus1 is Focus + 1, add-mutual-map-consts All SS Lno Focus1 CS ]. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% func main-mutual int, list inductive, string -> list prop. main-mutual Lno GRS Prefix C :- std.do! [ if (std.length GRS 1) (IsMutual = ff) (IsMutual = tt), std.map GRS (build-mutual-spec IsMutual Prefix) Specs, add-mutual-map-consts Specs Specs Lno 0 C ]. func main inductive, string -> list prop. main GR Prefix C :- coq.env.indt-block GR _ Lno Luno GRS _ _ _, assert! (Lno = Luno) "derive.map: Non-uniform parameters not supported", main-mutual Lno GRS Prefix C. }