| 1 |
/* map over a container */ |
| 2 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 3 |
/* ------------------------------------------------------------------------- */ |
| 4 |
|
| 5 |
shorten std.{split-at, length, rev, append, do!, drop-last, assert!}. |
| 6 |
|
| 7 |
map-db (app[global (indt GR1)|A1]) (app[global (indt GR2)|A2]) R :- |
| 8 |
coq.env.indt GR1 _ Lno1 _ _ _ _, |
| 9 |
coq.env.indt GR2 _ Lno2 _ _ _ _, |
| 10 |
{length A1} > Lno1, {length A2} > Lno2, |
| 11 |
split-at Lno1 A1 Args1 Points1, |
| 12 |
split-at Lno2 A2 Args2 Points2, |
| 13 |
Points1 = Points2, !, |
| 14 |
map-db {coq.mk-app (global (indt GR1)) Args1} {coq.mk-app (global (indt GR2)) Args2} F, |
| 15 |
coq.mk-app F Points1 R. |
| 16 |
|
| 17 |
namespace derive.map { |
| 18 |
|
| 19 |
% Building the body %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 20 |
|
| 21 |
pred bo-idx |
| 22 |
i:term, % inductive arity (input) |
| 23 |
i:term, % inductive type (input) applied to params & idx handled so far |
| 24 |
i:term, % inductive type (output) applied to params & idx handled so far |
| 25 |
i:int, % current index no |
| 26 |
o:int, % Recno |
| 27 |
i:list term, % rev list of (output) parameters |
| 28 |
o:term, % body |
| 29 |
o:term. % type |
| 30 |
|
| 31 |
bo-idx (prod _ S1 T1) Ity1 Ity2 N M Ps (fun `x` S1 Bo) (prod `x` S1 Ty) :- !, |
| 32 |
pi x\ sigma Ity1x Ity2x\ |
| 33 |
coq.mk-app Ity1 [x] Ity1x, |
| 34 |
coq.mk-app Ity2 [x] Ity2x, |
| 35 |
N1 is N + 1, |
| 36 |
decl x `x` S1 => bo-idx (T1 x) Ity1x Ity2x N1 M Ps (Bo x) (Ty x). |
| 37 |
|
| 38 |
bo-idx (sort _) Ity1 Ity2 N N Ps (fun `x` Ity1 Bo) (prod `_` Ity1 _\ Ity2) :- !, |
| 39 |
@pi-decl `x` Ity1 x\ |
| 40 |
coq.build-match x Ity1 (bo-idx-rty Ps Ity2) (bo-k-args Ps) (Bo x). |
| 41 |
|
| 42 |
bo-idx X Ity1 Ity2 N M Ps R1 R2 :- whd1 X X1, !, |
| 43 |
bo-idx X1 Ity1 Ity2 N M Ps R1 R2. |
| 44 |
|
| 45 |
pred bo-idx-rty i:list term, i:term, i:term, i:list term, i:list term, o:term. |
| 46 |
bo-idx-rty Ps ItyArgs _ Vs _ R :- |
| 47 |
rev Vs [_|IdxsRev], |
| 48 |
rev IdxsRev Idxs, |
| 49 |
coq.safe-dest-app ItyArgs HD _, |
| 50 |
coq.mk-app HD {append {rev Ps} Idxs} R. |
| 51 |
|
| 52 |
pred bo-k-args i:list term, i:term, i:term, i:list term, i:list term, o:term. |
| 53 |
bo-k-args ParamsRev K _ Args Tys R :- |
| 54 |
rev ParamsRev Params, |
| 55 |
coq.safe-dest-app K (global (indc GR)) _, |
| 56 |
coq.env.typeof (indc GR) T, coq.subst-prod Params T KT, |
| 57 |
(bo-k-args.aux {coq.mk-app (global (indc GR)) Params} Args Tys KT R). |
| 58 |
|
| 59 |
pred bo-k-args.aux i:term, i:list term, i:list term, i:term, o:term. |
| 60 |
bo-k-args.aux R [] [] _ R. |
| 61 |
bo-k-args.aux K [A|As] [T|Ts] (prod _ S Ty) R :- |
| 62 |
map-db T S F, |
| 63 |
coq.mk-app F [A] FA, |
| 64 |
bo-k-args.aux {coq.mk-app K [FA]} As Ts (Ty FA) R. |
| 65 |
bo-k-args.aux K [A|As] [_|Ts] (prod _ _ Ty) R :- !, |
| 66 |
bo-k-args.aux {coq.mk-app K [A]} As Ts (Ty A) R. |
| 67 |
bo-k-args.aux K As Ts X R :- whd1 X X1, !, |
| 68 |
bo-k-args.aux K As Ts X1 R. |
| 69 |
|
| 70 |
|
| 71 |
pred map-pi i:any, o:list prop. |
| 72 |
map-pi (x\ []) []. |
| 73 |
map-pi (x\ [X x| XS x]) [pi x\ X x | YS] :- map-pi XS YS. |
| 74 |
|
| 75 |
pred mk-map-ty |
| 76 |
i:term, % input variable |
| 77 |
i:term, % and its type |
| 78 |
i:term, % output variable |
| 79 |
i:term, % an its type |
| 80 |
o:term, % type of a mapping function from input to output |
| 81 |
i:term, % map function (having the type above) |
| 82 |
o:int, % arity of the predicate |
| 83 |
o:list prop. % map-db clause for map function |
| 84 |
mk-map-ty A (prod _ SA T1) B (prod _ SB T2) (prod `x` SA x\ R x) F N C1 :- |
| 85 |
map-db SA SB Fa, !, |
| 86 |
(pi x\ sigma Ax Fx BFx \ |
| 87 |
coq.mk-app A [x] Ax, coq.mk-app Fa [x] Fx, coq.mk-app B [Fx] BFx, |
| 88 |
mk-map-ty Ax (T1 x) BFx (T2 BFx) (R x) {coq.mk-app F [x]} M (C x), |
| 89 |
N is M + 1), |
| 90 |
map-pi C C1. |
| 91 |
mk-map-ty A (prod _ S1 T1) B (prod _ _ T2) (prod `x` S1 x\ R x) F N C1 :- |
| 92 |
(pi x\ sigma Ax Bx \ coq.mk-app A [x] Ax, coq.mk-app B [x] Bx, |
| 93 |
mk-map-ty Ax (T1 x) Bx (T2 x) (R x) {coq.mk-app F [x]} M (C x), |
| 94 |
N is M + 1), |
| 95 |
map-pi C C1. |
| 96 |
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] :- |
| 97 |
drop-last 1 XS LA, |
| 98 |
drop-last 1 YS LB, |
| 99 |
drop-last 1 GS LF, |
| 100 |
coq.mk-app X LA PLA, |
| 101 |
coq.mk-app Y LB PLB, |
| 102 |
coq.mk-app G LF PLF. |
| 103 |
mk-map-ty A _ B _ (prod `x` A _\ B) F 0 [map-db A B F]. |
| 104 |
|
| 105 |
% Build a clause %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 106 |
|
| 107 |
pred mk-clause |
| 108 |
i:int, % current parameter |
| 109 |
i:int, % number of parameters |
| 110 |
i:term, % inductive type (input) |
| 111 |
i:term, % inductive type (output) |
| 112 |
i:term, % arity of the inductive |
| 113 |
i:list prop, % premises of the clause |
| 114 |
i:term, % map function |
| 115 |
o:prop. % clause for map-db |
| 116 |
mk-clause N N Ity1 Ity2 _ Todo Map (map-db Ity1 Ity2 Map :- Todo). |
| 117 |
mk-clause N Lno Ity1 Ity2 (prod _ _ T) Todo Map (pi x y f\ C x y f) :- !, |
| 118 |
N1 is N + 1, |
| 119 |
pi x y f\ sigma Ity1x Ity2y Mapf\ |
| 120 |
coq.mk-app Ity1 [x] Ity1x, |
| 121 |
coq.mk-app Ity2 [y] Ity2y, |
| 122 |
coq.mk-app Map [x,y,f] Mapf, |
| 123 |
mk-clause N1 Lno Ity1x Ity2y (T x) [map-db x y f|Todo] Mapf (C x y f). |
| 124 |
mk-clause N Lno Ity1 Ity2 X Todo Map C :- whd1 X X1, !, |
| 125 |
mk-clause N Lno Ity1 Ity2 X1 Todo Map C. |
| 126 |
|
| 127 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 128 |
|
| 129 |
kind mutual-spec type. |
| 130 |
type mutual-spec inductive -> string -> term -> term -> term -> term -> mutual-spec. |
| 131 |
|
| 132 |
func build-mutual-spec bool, string, inductive -> mutual-spec. |
| 133 |
build-mutual-spec Mutual Prefix GR (mutual-spec GR Prefix1 T T Arity Arity) :- |
| 134 |
T = global (indt GR), |
| 135 |
if (Mutual = tt) (Prefix1 is {coq.gref->id (indt GR)} ^ "_") |
| 136 |
(Prefix1 is Prefix), |
| 137 |
coq.env.indt GR _ _ _ Arity _ _. |
| 138 |
|
| 139 |
func mutual-spec-apply-param term, term, mutual-spec -> mutual-spec. |
| 140 |
mutual-spec-apply-param A B (mutual-spec GR Prefix Ity1 Ity2 (prod _ _ F1) (prod _ _ F2)) |
| 141 |
(mutual-spec GR Prefix Ity1A Ity2B (F1 A) (F2 B)) :- !, |
| 142 |
coq.mk-app Ity1 [A] Ity1A, |
| 143 |
coq.mk-app Ity2 [B] Ity2B. |
| 144 |
mutual-spec-apply-param _ _ _ _ :- stop "derive.map: mutual parameter mismatch". |
| 145 |
|
| 146 |
func mutual-map-clause mutual-spec, term -> prop. |
| 147 |
mutual-map-clause (mutual-spec _ _ Ity1 Ity2 _ _) F (map-db Ity1 Ity2 F). |
| 148 |
|
| 149 |
pred bo-fix-aux list term, mutual-spec -> triple term term int. |
| 150 |
bo-fix-aux _ (mutual-spec _ _ Ity1 Ity2 Arity _) (triple Body Ty Recno) :- |
| 151 |
coq.safe-dest-app Ity2 _ Params, |
| 152 |
std.rev Params ParamsRev, |
| 153 |
bo-idx Arity Ity1 Ity2 0 Recno ParamsRev Body Ty. |
| 154 |
|
| 155 |
func bo-mfix list mutual-spec, list mutual-spec, list term, list term, list term, list int -> mfix-block, list term, list int. |
| 156 |
bo-mfix All [] Ps RevF TysRev RecnosRev (mfix-bo Bodies) Tys Recnos :- !, std.do! [ |
| 157 |
std.rev RevF Funs, |
| 158 |
std.map2 All Funs mutual-map-clause Clauses, |
| 159 |
Clauses =!=> std.map All (bo-fix-aux Ps) Bodies_Tys_Recnos, |
| 160 |
std.map Bodies_Tys_Recnos triple_1 Bodies, |
| 161 |
std.map Bodies_Tys_Recnos triple_2 Tys, |
| 162 |
std.map Bodies_Tys_Recnos triple_3 Recnos, |
| 163 |
std.rev Recnos RecnosRev, |
| 164 |
std.rev Tys TysRev, |
| 165 |
]. |
| 166 |
bo-mfix All [S|SS] Ps RevF Tys Recnos (mfix-ty N Recno Ty Rest) TysR RecnosR :- |
| 167 |
S = mutual-spec GR _ _ _ _ _, |
| 168 |
ID is {coq.gref->id (indt GR)} ^ "_map_rec", |
| 169 |
coq.id->name ID N, |
| 170 |
@pi-decl N Ty f\ bo-mfix All SS Ps [f|RevF] [Ty|Tys] [Recno|Recnos] (Rest f) TysR RecnosR. |
| 171 |
|
| 172 |
func bo-fix list mutual-spec, list term, int -> term. |
| 173 |
bo-fix [(mutual-spec GR _ _ _ _ _ as S)] Ps _ (fix N Recno Ty Bo) :- coq.env.recursive? GR, !, |
| 174 |
ID is {coq.gref->id (indt GR)} ^ "_map_rec", |
| 175 |
coq.id->name ID N, |
| 176 |
@pi-decl N Ty f\ |
| 177 |
mutual-map-clause S f (C f), |
| 178 |
C f =!=> bo-fix-aux Ps S (triple (Bo f) Ty Recno), coq.typecheck (Bo f) _ ok, !. % the first that typechecks |
| 179 |
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 |
| 180 |
bo-fix Specs Ps Focus (mfix Focus Recno Block) :- |
| 181 |
bo-mfix Specs Specs Ps [] [] [] Block Tys_ Recnos, |
| 182 |
std.nth Focus Recnos Recno, |
| 183 |
coq.typecheck (mfix Focus Recno Block) _ ok, !. % the first that typechecks |
| 184 |
|
| 185 |
func bo-params int, int, list mutual-spec, int, list term -> term. |
| 186 |
bo-params Lno Lno Specs Focus Ps R :- !, |
| 187 |
bo-fix Specs Ps Focus R. |
| 188 |
bo-params N Lno ([S|_] as Specs) Focus Ps R :- N < Lno, |
| 189 |
S = mutual-spec _ _ _ _ (prod A Sty1 _Rty1) (prod _ Sty2 _Rty2), !, |
| 190 |
coq.name-suffix A 1 A1, |
| 191 |
coq.name-suffix A 2 A2, |
| 192 |
coq.name-suffix A "f" Af, |
| 193 |
N1 is N + 1, |
| 194 |
(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"), |
| 195 |
R = (fun A1 Sty1 a\ fun A2 Sty2 b\ fun Af (FAB a b) f\ Bo a b f), |
| 196 |
pi a b f\ sigma Specs1\ |
| 197 |
std.map Specs (mutual-spec-apply-param a b) Specs1, |
| 198 |
Clause a b f => |
| 199 |
decl a A1 Sty1 => |
| 200 |
decl b A2 Sty2 => |
| 201 |
decl f Af (FAB a b) => |
| 202 |
bo-params N1 Lno Specs1 Focus [b|Ps] (Bo a b f). |
| 203 |
bo-params _ _ _ _ _ _ :- stop "derive.map: mutual indexed data types not supported". |
| 204 |
|
| 205 |
func add-mutual-map-consts list mutual-spec, list mutual-spec, int, int -> list prop. |
| 206 |
add-mutual-map-consts _ [] _ _ [] :- !. |
| 207 |
add-mutual-map-consts All [S|SS] Lno Focus [map-done GR,Clause|CS] :- std.do! [ |
| 208 |
S = mutual-spec GR Prefix T T Arity _, |
| 209 |
bo-params 0 Lno All Focus [] RSkel, |
| 210 |
std.assert-ok! (coq.elaborate-skeleton RSkel Ty R) "derive.map generates illtyped mutual term", |
| 211 |
Name is Prefix ^ "map", |
| 212 |
coq.ensure-fresh-global-id Name FName, |
| 213 |
coq.env.add-const FName R Ty @transparent! Funct, |
| 214 |
mk-clause 0 Lno T T Arity [] (global (const Funct)) Clause, |
| 215 |
coq.elpi.accumulate _ "derive.map.db" (clause _ _ Clause), |
| 216 |
coq.elpi.accumulate _ "derive.map.db" (clause _ _ (map-done GR)), |
| 217 |
Focus1 is Focus + 1, |
| 218 |
add-mutual-map-consts All SS Lno Focus1 CS |
| 219 |
]. |
| 220 |
|
| 221 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 222 |
|
| 223 |
func main-mutual int, list inductive, string -> list prop. |
| 224 |
main-mutual Lno GRS Prefix C :- std.do! [ |
| 225 |
if (std.length GRS 1) (IsMutual = ff) (IsMutual = tt), |
| 226 |
std.map GRS (build-mutual-spec IsMutual Prefix) Specs, |
| 227 |
add-mutual-map-consts Specs Specs Lno 0 C |
| 228 |
]. |
| 229 |
|
| 230 |
func main inductive, string -> list prop. |
| 231 |
main GR Prefix C :- |
| 232 |
coq.env.indt-block GR _ Lno Luno GRS _ _ _, |
| 233 |
assert! (Lno = Luno) "derive.map: Non-uniform parameters not supported", |
| 234 |
main-mutual Lno GRS Prefix C. |
| 235 |
|
| 236 |
} |