Debian Codegraph @

elpi/coq-lib.elpi

raw
1 /* rocq-elpi: Coq terms as the object language of elpi */
2 /* license: GNU Lesser General Public License Version 2.1 or later */
3 /* ------------------------------------------------------------------------- */
4
5 accumulate elpi_elpi/coq-lib-common.
6
7 shorten std.{fatal-error, fatal-error-w-data, debug-print, unsafe-cast}.
8 shorten std.{rev, map, append, appendR, map2, forall-ok, take, do-ok!, lift-ok}.
9 shorten std.{ omap, take-last, intersperse, map-ok, string.concat }.
10
11 :before "stop:begin"
12 stop S :- get-option "ltac:fail" N, !, coq.ltac.fail N S.
13
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15 % Helpers
16 %
17 % Pure LP code that works with the data types and API above.
18 % Named clauses are natural extension points, eg one can extend
19 % subst-prod to perform reduction in order to expose a "prod" node.
20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21
22 % Term surgery %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
23
24 func coq.subst-prod list term, term -> term.
25 coq.subst-prod [] P P :- !.
26 coq.subst-prod [X|XS] (prod _ _ F) P :- !, coq.subst-prod XS (F X) P.
27 coq.subst-prod XS (let _ _ X F) P :- !, coq.subst-prod XS (F X) P.
28 :name "subst-prod:fail"
29 coq.subst-prod [_|_] T _ :- !,
30 fatal-error-w-data "subst-prod: not a product" T.
31
32 func coq.subst-fun list term, term -> term.
33 coq.subst-fun [] T T :- !.
34 coq.subst-fun [X|XS] (fun _ _ F) T :- !, coq.subst-fun XS (F X) T.
35 coq.subst-fun XS (let _ _ X F) T :- !, coq.subst-fun XS (F X) T.
36 :name "subst-fun:fail"
37 coq.subst-fun [_|_] T _ :- !,
38 fatal-error-w-data "subst-fun: not a lambda" T.
39
40 func coq.prod-R-fun -> term, term.
41 coq.prod-R-fun (prod N T F) (fun N T R) :- !, pi x\ coq.prod-R-fun (F x) (R x).
42 coq.prod-R-fun (let N T B F) (let N T B R) :- !, pi x\ coq.prod-R-fun (F x) (R x).
43 coq.prod-R-fun X X.
44
45 func coq.prod->fun term -> term.
46 coq.prod->fun (prod N T F) (fun N T R) :- !, pi x\ coq.prod->fun (F x) (R x).
47 coq.prod->fun (let N T B F) (let N T B R) :- !, pi x\ coq.prod->fun (F x) (R x).
48 coq.prod->fun X X.
49
50 func coq.count-prods term -> int.
51 coq.count-prods (prod N T B) C :- !,
52 (@pi-decl N T x\ coq.count-prods (B x) C'), C is C' + 1.
53 coq.count-prods (let N T V B) C :- !,
54 (@pi-def N T V x\ coq.count-prods (B x) C).
55 coq.count-prods T C :- !,
56 coq.reduction.lazy.whd T Tr,
57 if (T == Tr) (C = 0) (coq.count-prods Tr C).
58
59 func coq.mk-n-holes int -> list A.
60 coq.mk-n-holes 0 [] :- !.
61 coq.mk-n-holes N [HOLE_|R] :- !, M is N - 1, coq.mk-n-holes M R.
62
63 func coq.safe-dest-app term -> term, list term.
64 coq.safe-dest-app (app [X|XS]) HD AllArgs :- !,
65 coq.safe-dest-app X HD ARGS, append ARGS XS AllArgs.
66 coq.safe-dest-app X X [].
67
68 % coq.mk-eta n Ty T: performs up to n (when >= 0) eta expasion of T
69 % according to its type Ty. If n < 0 it makes as many step as
70 % products in Ty. There be dragons if T has not type Ty.
71 func coq.mk-eta int, term, term -> term.
72 coq.mk-eta 0 _ B B :- !.
73 coq.mk-eta N (prod Name Ty P) (fun _ _ F) (fun Name Ty F1) :- !, N1 is N - 1,
74 pi x \ coq.mk-eta N1 (P x) (F x) (F1 x).
75 coq.mk-eta N (prod Name Ty P) B (fun Name Ty B1) :- !, N1 is N - 1,
76 pi x \ coq.mk-eta N1 (P x) {coq.mk-app B [x]} (B1 x).
77 :name "mk-eta:end"
78 coq.mk-eta _ _ B B :- !.
79
80 func coq.saturate term, term -> term.
81 coq.saturate Ty T O :- whd Ty [] (prod N Src Tgt) [], !,
82 coq.mk-app T [Hole_] R,
83 @pi-decl N Src x\ coq.saturate (Tgt x) R O.
84 coq.saturate _ X X.
85
86 % [copy A B] can be used to perform a replacement, eg
87 % (copy (const "foo") (const "bar") :- !) => copy A B
88 % traverses A replacing foo with bar.
89 func copy term -> term.
90 :name "copy:start"
91 copy X Y :- name X, !, X = Y, !. % avoid loading "copy x x" at binders
92 copy (global _ as C) C :- !.
93 copy (pglobal _ _ as C) C :- !.
94 copy (sort _ as C) C :- !.
95 copy (fun N T F) (fun N T1 F1) :- !,
96 copy T T1, pi x\ copy (F x) (F1 x).
97 copy (let N T B F) (let N T1 B1 F1) :- !,
98 copy T T1, copy B B1, pi x\ copy (F x) (F1 x).
99 copy (prod N T F) (prod N T1 F1) :- !,
100 copy T T1, (pi x\ copy (F x) (F1 x)).
101 copy (app L) (app L1) :- !, map L copy L1.
102 copy (fix N Rno Ty F) (fix N Rno Ty1 F1) :- !,
103 copy Ty Ty1, pi x\ copy (F x) (F1 x).
104 copy (mfix F Rno MF) (mfix F Rno MF1) :- !,
105 copy-mfix-block MF MF1.
106 copy (match T Rty B) (match T1 Rty1 B1) :- !,
107 copy T T1, copy Rty Rty1, map B copy B1.
108 copy (primitive _ as C) C :- !.
109 copy (uvar M L as X) W :- var X, !, map L copy L1, coq.mk-app-uvar M L1 W.
110 % when used in CHR rules
111 copy (uvar X L) (uvar X L1) :- !, map L copy L1.
112
113 func copy-mfix-block mfix-block -> mfix-block.
114 copy-mfix-block (mfix-ty N Rno Ty F) (mfix-ty N Rno Ty1 F1) :- !,
115 copy Ty Ty1, pi x\ copy-mfix-block (F x) (F1 x).
116 copy-mfix-block (mfix-bo Bs) (mfix-bo Bs1) :- !,
117 map Bs copy Bs1.
118
119 func copy-ctx-item prop -> prop.
120 copy-ctx-item (decl X N T) (decl X1 N T1) :- copy X X1, copy T T1.
121 copy-ctx-item (def X N T B) (def X1 N T1 B1) :-
122 copy X X1, copy T T1, copy B B1.
123
124 func copy-arity arity -> arity.
125 copy-arity (parameter ID IMP T R) (parameter ID IMP T1 R1) :-
126 copy T T1, pi x\ copy-arity (R x) (R1 x).
127 copy-arity (arity T) (arity T1) :- copy T T1.
128
129 func copy-indt-decl indt-decl -> indt-decl.
130 copy-indt-decl (parameter ID I Ty D) (parameter ID I Ty1 D1) :-
131 copy Ty Ty1,
132 @pi-parameter ID Ty1 x\ copy-indt-decl (D x) (D1 x).
133 copy-indt-decl (inductive ID CO A D) (inductive ID CO A1 D1) :-
134 copy-arity A A1,
135 @pi-inductive ID A1 i\ std.map (D i) copy-constructor (D1 i).
136 copy-indt-decl (minductive ID CO A D) (minductive ID CO A1 D1) :-
137 copy-arity A A1,
138 @pi-inductive ID A1 i\ copy-indt-decl (D i) (D1 i).
139 copy-indt-decl (mblock Bs) (mblock Bs1) :- map Bs copy-constructor-block Bs1.
140 copy-indt-decl (record ID T IDK F) (record ID T1 IDK F1) :-
141 copy T T1,
142 copy-fields F F1.
143
144 func copy-constructor-block list indc-decl -> list indc-decl.
145 copy-constructor-block Ks Ks1 :- map Ks copy-constructor Ks1.
146
147 func copy-fields record-decl -> record-decl.
148 copy-fields end-record end-record.
149 copy-fields (field Att ID T F) (field Att ID T1 F1) :-
150 copy T T1,
151 @pi-parameter ID T1 x\ copy-fields (F x) (F1 x).
152
153 func copy-constructor indc-decl -> indc-decl.
154 copy-constructor (constructor ID A) (constructor ID A1) :- copy-arity A A1.
155
156 func fold-map term, A -> term, A.
157 :name "fold-map:start"
158 fold-map X A Y A :- name X, !, X = Y, !. % avoid loading "fold-map x A x A" at binders
159 fold-map (global _ as C) A C A :- !.
160 fold-map (pglobal _ _ as C) A C A :- !.
161 fold-map (sort _ as C) A C A :- !.
162 fold-map (fun N T F) A (fun N T1 F1) A2 :- !,
163 fold-map T A T1 A1, pi x\ fold-map (F x) A1 (F1 x) A2.
164 fold-map (let N T B F) A (let N T1 B1 F1) A3 :- !,
165 fold-map T A T1 A1, fold-map B A1 B1 A2, pi x\ fold-map (F x) A2 (F1 x) A3.
166 fold-map (prod N T F) A (prod N T1 F1) A2 :- !,
167 fold-map T A T1 A1, (pi x\ fold-map (F x) A1 (F1 x) A2).
168 fold-map (app L) A (app L1) A1 :- !, std.fold-map L A fold-map L1 A1.
169 fold-map (fix N Rno Ty F) A (fix N Rno Ty1 F1) A2 :- !,
170 fold-map Ty A Ty1 A1, pi x\ fold-map (F x) A1 (F1 x) A2.
171 fold-map (mfix F Rno MF) A (mfix F Rno MF1) A1 :- !,
172 fold-map-mfix-block MF A MF1 A1.
173 fold-map (match T Rty B) A (match T1 Rty1 B1) A3 :- !,
174 fold-map T A T1 A1, fold-map Rty A1 Rty1 A2, std.fold-map B A2 fold-map B1 A3.
175 fold-map (primitive _ as C) A C A :- !.
176 fold-map (uvar M L as X) A W A1 :- var X, !, std.fold-map L A fold-map L1 A1, coq.mk-app-uvar M L1 W.
177 % when used in CHR rules
178 fold-map (uvar X L) A (uvar X L1) A1 :- std.fold-map L A fold-map L1 A1.
179
180 func fold-map-mfix-block mfix-block, A -> mfix-block, A.
181 fold-map-mfix-block (mfix-ty N Rno Ty F) A (mfix-ty N Rno Ty1 F1) A2 :- !,
182 fold-map Ty A Ty1 A1, pi x\ fold-map-mfix-block (F x) A1 (F1 x) A2.
183 fold-map-mfix-block (mfix-bo Bs) A (mfix-bo Bs1) A1 :- !,
184 std.fold-map Bs A fold-map Bs1 A1.
185
186 func fold-map-ctx-item prop, A -> prop, A.
187 fold-map-ctx-item (decl X N T) A (decl X1 N T1) A2 :- fold-map X A X1 A1, fold-map T A1 T1 A2.
188 fold-map-ctx-item (def X N T B) A (def X1 N T1 B1) A3 :-
189 fold-map X A X1 A1, fold-map T A1 T1 A2, fold-map B A2 B1 A3.
190
191 func fold-map-arity arity, A -> arity, A.
192 fold-map-arity (parameter ID IMP T R) A (parameter ID IMP T1 R1) A2 :-
193 fold-map T A T1 A1, pi x\ fold-map-arity (R x) A1 (R1 x) A2.
194 fold-map-arity (arity T) A (arity T1) A1 :- fold-map T A T1 A1.
195
196
197 % Bridges the gap between the data types used to read/write inductives.
198 % The arguments are the same of coq.env.indt plus an an extra one being
199 % the output (of type indt-decl).
200
201 func coq.upoly-decl->attribute any -> prop.
202 coq.upoly-decl->attribute (upoly-decl A B C D) (@udecl! A B C D).
203 coq.upoly-decl->attribute (upoly-decl-cumul A B C D) (@udecl-cumul! A B C D).
204
205 func coq.upoly-decl.complete-constraints upoly-decl -> upoly-decl.
206 coq.upoly-decl.complete-constraints (upoly-decl VS LV CS LC) (upoly-decl VS LV CS1 LC) :- std.do! [
207 std.map VS coq.univ.variable.constraints ExtraL,
208 std.flatten ExtraL Extra,
209 std.filter Extra (c\not(std.mem CS c)) New,
210 std.append CS New CS1,
211 ].
212 coq.upoly-decl.complete-constraints (upoly-decl-cumul VS LV CS LC) (upoly-decl-cumul VS LV CS1 LC) :- std.do! [
213 std.map VS coq.upoly-decl-cumul.complete-constraints.aux ExtraL,
214 std.flatten ExtraL Extra,
215 std.filter Extra (c\not(std.mem CS c)) New,
216 std.append CS New CS1,
217 ].
218 func coq.upoly-decl-cumul.complete-constraints.aux univ-variance -> list univ-constraint.
219 coq.upoly-decl-cumul.complete-constraints.aux (auto V) CS :- coq.univ.variable.constraints V CS.
220 coq.upoly-decl-cumul.complete-constraints.aux (covariant V) CS :- coq.univ.variable.constraints V CS.
221 coq.upoly-decl-cumul.complete-constraints.aux (invariant V) CS :- coq.univ.variable.constraints V CS.
222 coq.upoly-decl-cumul.complete-constraints.aux (irrelevant V) CS :- coq.univ.variable.constraints V CS.
223
224
225 kind coq.indt-spec type.
226 type coq.indt-spec inductive -> id -> bool -> term -> list (pair constructor id) -> list term -> coq.indt-spec.
227
228 func coq.build-indt-decl coq.indt-spec, int, int -> indt-decl.
229
230 coq.build-indt-decl (coq.indt-spec I ID IsInd Arity Kns Ktys) Pno UPno Decl :-
231 coq.build-indt-decl-aux (pr I ID) IsInd Pno UPno Arity Kns Ktys [] Decl.
232
233 func coq.build-indt-decl-aux
234 pair inductive id, bool, int, int, term, list (pair constructor id), list term, list term -> indt-decl.
235 coq.build-indt-decl-aux (pr GR I) IsInd NUPno 0 Ty Kns KtysNu Params (inductive I IsInd Arity Ks) :- !,
236 coq.term->arity Ty NUPno Arity,
237 std.map KtysNu (k\coq.term->arity k NUPno) Ktys,
238 rev Params ParamsR,
239 (pi i\ Sub i = [ % we factor uniform parameters
240 (pi x l\ copy (app[global (indt GR)|l]) (app[i|x]):- !, appendR ParamsR x l, !),
241 (pi x l ui\ copy (app[pglobal (indt GR) ui|l]) (app[i|x]):- !, appendR ParamsR x l, !),
242 (copy (global (indt GR)) i :- !),
243 (pi ui\ copy (pglobal (indt GR) ui) i :- !)
244 ]),
245 pi i\
246 map2 Kns Ktys (gr_name\ ty\ res\
247 sigma tmp name\
248 (Sub i ==> copy-arity ty tmp),
249 gr_name = pr _ name,
250 res = constructor name tmp)
251 (Ks i).
252
253 coq.build-indt-decl-aux GR IsInd Pno UPno (prod N S T) Kns Ktys Params (parameter NS explicit S Res) :- Pno > 0, UPno > 0, !,
254 coq.name->id N NS,
255 Pno1 is Pno - 1,
256 UPno1 is UPno - 1,
257 pi p\
258 map Ktys (coq.subst-prod [p]) (Ktys1 p),
259 coq.build-indt-decl-aux GR IsInd Pno1 UPno1 (T p) Kns (Ktys1 p) [p|Params] (Res p).
260
261 :name "coq.build-indt-decl-aux:fail"
262 coq.build-indt-decl-aux _ _ _ _ _ _ _ _ _ :- !,
263 fatal-error "coq.build-indt-decl-aux: invalid declaration".
264
265 func coq.build-mindt-decl list coq.indt-spec, int, int -> indt-decl.
266 coq.build-mindt-decl Specs Pno UPno Decl :-
267 coq.build-mindt-decl-aux Specs Pno UPno [] Decl.
268
269 func coq.build-mindt-decl-aux list coq.indt-spec, int, int, list term -> indt-decl.
270 coq.build-mindt-decl-aux Specs 0 0 Params Decl :- !,
271 rev Params ParamsR,
272 coq.build-mindt-decl-binders Specs Specs ParamsR [] Decl.
273 coq.build-mindt-decl-aux ([coq.indt-spec _ _ _ (prod N S _) _ _|_] as Specs) Pno UPno Params (parameter NS explicit S Res) :- Pno > 0, UPno > 0, !,
274 coq.name->id N NS,
275 Pno1 is Pno - 1,
276 UPno1 is UPno - 1,
277 pi p\
278 std.map Specs (coq.build-mindt-decl-subst-param p) Specs1,
279 coq.build-mindt-decl-aux Specs1 Pno1 UPno1 [p|Params] (Res p).
280 :name "coq.build-mindt-decl-aux:fail"
281 coq.build-mindt-decl-aux _ _ _ _ _ :- !,
282 fatal-error "coq.build-mindt-decl-aux: invalid mutual declaration".
283
284 func coq.build-mindt-decl-subst-param term, coq.indt-spec -> coq.indt-spec.
285 coq.build-mindt-decl-subst-param P (coq.indt-spec GR ID IsInd (prod _ _ T) Kns Ktys) (coq.indt-spec GR ID IsInd (T P) Kns Ktys1) :- !,
286 std.map Ktys (coq.subst-prod [P]) Ktys1.
287 coq.build-mindt-decl-subst-param _ _ _ :- !,
288 fatal-error "coq.build-mindt-decl-subst-param: parameter mismatch".
289
290 func coq.build-mindt-decl-binders list coq.indt-spec, list coq.indt-spec, list term, list (pair inductive term) -> indt-decl.
291 coq.build-mindt-decl-binders All [] ParamsR Acc (mblock Blocks) :- !,
292 rev Acc Pairs,
293 std.map All (coq.build-mindt-decl-block Pairs ParamsR) Blocks.
294 coq.build-mindt-decl-binders All [coq.indt-spec GR ID IsInd Ty _ _|Rest] ParamsR Acc (minductive ID IsInd Arity More) :- !,
295 coq.term->arity Ty 0 Arity,
296 @pi-inductive ID Arity i\
297 coq.build-mindt-decl-binders All Rest ParamsR [pr GR i|Acc] (More i).
298
299 func coq.build-mindt-decl-block list (pair inductive term), list term, coq.indt-spec -> list indc-decl.
300 coq.build-mindt-decl-block Pairs ParamsR (coq.indt-spec _ _ _ _ Kns Ktys) Block :-
301 coq.build-mindt-decl-copy-clauses Pairs ParamsR Sub,
302 std.map2 Kns Ktys (gr_name\ ty\ res\ sigma tmp name ar\
303 (Sub ==> copy ty tmp),
304 coq.term->arity tmp 0 ar,
305 gr_name = pr _ name,
306 res = constructor name ar) Block.
307
308 func coq.build-mindt-decl-copy-clauses list (pair inductive term), list term -> list prop.
309 coq.build-mindt-decl-copy-clauses [] _ [].
310 coq.build-mindt-decl-copy-clauses [pr GR I|Rest] ParamsR [
311 (pi l rest r\ copy (app[global (indt GR)|l]) r :- !, std.append ParamsR rest l, coq.mk-app I rest r, !),
312 (pi l rest r ui\ copy (app[pglobal (indt GR) ui|l]) r :- !, std.append ParamsR rest l, coq.mk-app I rest r, !),
313 (copy (global (indt GR)) I :- !),
314 (pi ui\ copy (pglobal (indt GR) ui) I :- !)
315 | More] :-
316 coq.build-mindt-decl-copy-clauses Rest ParamsR More.
317
318 % [coq.indt-unif->indt-nonunif I O] takes an inductive declaration I which may
319 % contain uniform parameters (i.e. be headed by the `parameter` constructor),
320 % and returns in O the inductive declaration obtained by pushing the uniform
321 % parameters into the arities of the inductive and the constructors.
322 %
323 % In rocq-elpi's encoding this makes them non-uniform parameters, but beware
324 % that when passed to coq.env.add-indt, Rocq reassigns the uniformity of
325 % parameters as it sees fit, so reading back the declaration of an inductive
326 % added with coq.env.add-indt might give you an inductive with uniform
327 % parameters anyway.
328 %
329 % When the inductive passed is a record, it is interpreted as an inductive
330 % with one constructor.
331 func coq.indt-unif->indt-nonunif indt-decl -> indt-decl.
332 coq.indt-unif->indt-nonunif (parameter N Expl Ty RestI) (inductive IN IsInd IA Cs) :-
333 % Reparameterize the inductive and constructors' arities by a fresh variable
334 % standing for this parameter
335 (pi p\
336 coq.indt-unif->indt-nonunif (RestI p) (inductive IN IsInd (InnerIA p) (InnerCs p))),
337
338 % This way the continuation of the new arity is already abstracted over the parameter
339 IA = parameter N Expl Ty InnerIA,
340
341 % InnerCs is parameterized by two terms: first standing for the current
342 % parameter, second standing for the inductive already applied to this
343 % parameter. This makes it have the type (term -> term -> list constructor).
344 % Since we want to bind the current parameter in each constructor
345 % individually, we need to push those two arguments inside the list. The shape
346 % of the list doesn't depend on these arguments, so we can map over it under
347 % binders, and then only allow the elements of the list to refer to the fresh
348 % variables, but not the list itself.
349 % We end up with the more useful (InnerCsList : list (term -> term -> constructor)).
350 (pi sI sP\ map (InnerCs sP sI) (i\ o\ o sP sI = i) InnerCsList),
351
352 % Abstract over the inductive we're producing. In the constructor arities,
353 % this variable is supposed to be applied to the inner parameters.
354 (pi i\
355 map InnerCsList
356 (ic\ oc\ sigma cname carr\
357 % Decompose the inner constructor; the name doesn't depend on the terms
358 (pi sP sI\ ic sP sI = constructor cname (carr sP sI)),
359 % Add the parameter to the constructor, instantiating the arguments as expected
360 oc = constructor cname (parameter N Expl Ty a\ carr a (app [i, a])))
361 (Cs i)).
362
363 coq.indt-unif->indt-nonunif (inductive _ _ _ _ as I) I.
364
365 coq.indt-unif->indt-nonunif (record N Ty CN Decl) (inductive N tt (arity Ty) C) :-
366 coq.record-decl->arity Decl Ar,
367 pi c\ C c = [constructor CN (arity (Ar c))].
368
369 % [coq.record-decl->arity R T] takes a record declaration R and returns in T
370 % the type of the the record's constructor, parameterized by a term standing
371 % for the record type.
372 func coq.record-decl->arity record-decl -> (term -> term).
373 coq.record-decl->arity (field _ Id Ty RestF) (r\ prod N Ty (RestT r)) :- !,
374 coq.id->name Id N,
375 pi x\ coq.record-decl->arity (RestF x) (r\ RestT r x).
376 coq.record-decl->arity end-record (r\ r) :- !.
377
378 :index (_ 1)
379 func coq.rename-arity
380 (func id -> id),
381 arity ->
382 arity.
383 coq.rename-arity RP (parameter ID I TY In) (parameter ID1 I TY Out) :-
384 RP ID ID1,
385 @pi-parameter ID TY p\
386 coq.rename-arity RP (In p) (Out p).
387 coq.rename-arity _ (arity T) (arity T).
388
389 % [coq.rename-indt-decl RenameParam RenameIndType RenameIndConstr D D1]
390 % can be used to rename all [id] part of an inductive type declaration
391 :index (_ _ _ 1)
392 func coq.rename-indt-decl
393 (func id -> id),
394 (func id -> id),
395 (func id -> id),
396 indt-decl -> indt-decl.
397 coq.rename-indt-decl RP RI RK (parameter ID I TY In) (parameter ID1 I TY Out) :-
398 RP ID ID1,
399 @pi-parameter ID TY p\
400 coq.rename-indt-decl RP RI RK (In p) (Out p).
401 coq.rename-indt-decl RP RI RK (inductive ID Ind A In) (inductive ID1 Ind A1 Out) :-
402 RI ID ID1,
403 coq.rename-arity RP A A1,
404 coq.id->name ID Name,
405 coq.arity->term A TY,
406 @pi-decl Name TY i\
407 std.map (In i) (coq.rename-indt-decl.aux RP RI RK) (Out i).
408 coq.rename-indt-decl RP RI RK (minductive ID Ind A In) (minductive ID1 Ind A1 Out) :-
409 RI ID ID1,
410 coq.rename-arity RP A A1,
411 coq.id->name ID Name,
412 coq.arity->term A TY,
413 @pi-decl Name TY i\
414 coq.rename-indt-decl RP RI RK (In i) (Out i).
415 coq.rename-indt-decl RP RI RK (mblock Blocks) (mblock Blocks1) :-
416 map Blocks (coq.rename-indt-decl.block RP RI RK) Blocks1.
417 coq.rename-indt-decl _ RI RK (record ID A KID F) (record ID1 A KID1 F) :-
418 RI ID ID1,
419 RK KID KID1.
420
421 func coq.rename-indt-decl.block
422 (func id -> id),
423 (func id -> id),
424 (func id -> id),
425 list indc-decl -> list indc-decl.
426 coq.rename-indt-decl.block RP RI RK Ks Ks1 :- map Ks (coq.rename-indt-decl.aux RP RI RK) Ks1.
427
428 func coq.rename-indt-decl.aux
429 (func id -> id),
430 (func id -> id),
431 (func id -> id),
432 indc-decl -> indc-decl.
433 coq.rename-indt-decl.aux RP _ RK (constructor ID A) (constructor ID1 A1) :-
434 RK ID ID1,
435 coq.rename-arity RP A A1.
436
437 func coq.ensure-fresh-global-id string -> string.
438 coq.ensure-fresh-global-id Exp S :-
439 Name is Exp,
440 coq.env.fresh-global-id Name S,
441 if (Name = S) true
442 (coq.warning "elpi" "elpi.renamed" "Global name" Name "is taken, using" S "instead").
443
444 % Lifts coq.typecheck to inductive declarations
445 func coq.typecheck-indt-decl.heuristic-var-type term -> diagnostic.
446 coq.typecheck-indt-decl.heuristic-var-type (uvar _ _ as X) D :- !,
447 coq.univ.new U, coq.unify-eq X (sort (typ U)) D.
448 coq.typecheck-indt-decl.heuristic-var-type _ ok.
449
450 func coq.typecheck-indt-arity arity -> term, int, diagnostic.
451 coq.typecheck-indt-arity (parameter ID _ T D) (prod N T F) NU1 Diag :- do-ok! Diag [
452 coq.typecheck-ty T _,
453 (d\ @pi-parameter ID T x\ coq.typecheck-indt-arity (D x) (F x) NU d),
454 lift-ok (NU1 is NU + 1) "",
455 lift-ok (coq.id->name ID N) "",
456 ].
457 coq.typecheck-indt-arity (arity T) T 0 Diag :- do-ok! Diag [
458 coq.typecheck-ty T _,
459 coq.typecheck-indt-decl.heuristic-var-type T,
460 ].
461
462 func coq.typecheck-indt-decl indt-decl -> diagnostic.
463 coq.typecheck-indt-decl (parameter ID _ T Decl) Diag :- do-ok! Diag [
464 coq.typecheck-ty T _,
465 (d\ @pi-parameter ID T x\ coq.typecheck-indt-decl (Decl x) d),
466 ].
467 coq.typecheck-indt-decl (inductive ID _ Arity KDecl) Diag :- do-ok! Diag [
468 coq.typecheck-indt-arity Arity A NUPNO,
469 d\ @pi-parameter ID A i\ forall-ok (KDecl i) (coq.typecheck-indt-decl-c i A NUPNO) d
470 ].
471 coq.typecheck-indt-decl (minductive ID _ Arity Rest) Diag :- do-ok! Diag [
472 coq.typecheck-indt-arity Arity A NUPNO,
473 d\ @pi-parameter ID A i\ coq.typecheck-indt-decl-mutual [pr i (pr A NUPNO)] (Rest i) d
474 ].
475 coq.typecheck-indt-decl (record ID A _IDK FDecl) Diag :- do-ok! Diag [
476 coq.typecheck-ty A _,
477 d\ @pi-parameter ID A i\ do-ok! d [
478 lift-ok (coq.typecheck-indt-decl-field i FDecl (K i)) "",
479 coq.typecheck-indt-decl-c i A 0 (constructor "fields" (arity (K i)))
480 ]
481 ].
482
483 func coq.typecheck-indt-decl-mutual list (pair term (pair term int)), indt-decl -> diagnostic.
484 coq.typecheck-indt-decl-mutual Acc (minductive ID _ Arity Rest) Diag :- !, do-ok! Diag [
485 coq.typecheck-indt-arity Arity A NUPNO,
486 d\ @pi-parameter ID A i\ coq.typecheck-indt-decl-mutual [pr i (pr A NUPNO)|Acc] (Rest i) d
487 ].
488 coq.typecheck-indt-decl-mutual Acc (mblock Blocks) Diag :- !,
489 rev Acc Infos,
490 coq.typecheck-indt-decl-blocks Infos Blocks Diag.
491 coq.typecheck-indt-decl-mutual _ _ (error "minductive/mblock expected") :- !.
492
493 func coq.typecheck-indt-decl-blocks list (pair term (pair term int)), list (list indc-decl) -> diagnostic.
494 coq.typecheck-indt-decl-blocks [] [] ok :- !.
495 coq.typecheck-indt-decl-blocks [pr I (pr A NUPNO)|Infos] [Ks|Blocks] Diag :- !, do-ok! Diag [
496 forall-ok Ks (coq.typecheck-indt-decl-c I A NUPNO),
497 d\ coq.typecheck-indt-decl-blocks Infos Blocks d
498 ].
499 coq.typecheck-indt-decl-blocks _ _ (error "mblock constructor block count mismatch") :- !.
500
501 func coq.typecheck-indc-arity arity, int -> term, sort, diagnostic.
502 coq.typecheck-indc-arity A 0 T S Diag :- !,
503 coq.arity->term A T,
504 coq.typecheck-ty T S Diag.
505 coq.typecheck-indc-arity (parameter ID _ T D) NUPNO (prod N T F) S Diag :- do-ok! Diag [
506 coq.typecheck-ty T _,
507 lift-ok (NUPNO1 is NUPNO - 1) "",
508 (d\ @pi-parameter ID T x\ coq.typecheck-indc-arity (D x) NUPNO1 (F x) S d),
509 lift-ok (coq.id->name ID N) "",
510 ].
511
512 func coq.typecheck-indt-decl-c term, term, int, indc-decl -> diagnostic.
513 coq.typecheck-indt-decl-c I S NUPNO (constructor _ID Arity) Diag :- do-ok! Diag [
514 coq.typecheck-indc-arity Arity NUPNO T KS,
515 coq.typecheck-indt-decl-c.unify-arrow-tgt I 0 S T,
516 lift-ok (coq.arity->sort S IS) "",
517 lift-ok (coq.sort.leq KS IS) "constructor universe too large"
518 ].
519
520 :index (_ _ _ 1)
521 func coq.typecheck-indt-decl-c.unify-arrow-tgt term, int, term, term -> diagnostic.
522 coq.typecheck-indt-decl-c.unify-arrow-tgt I P A (prod N S T) D :- !,
523 @pi-decl N S x\ coq.typecheck-indt-decl-c.unify-arrow-tgt I P A (T x) D.
524 coq.typecheck-indt-decl-c.unify-arrow-tgt I P A (let N S B T) D :- !,
525 @pi-def N S B x\ coq.typecheck-indt-decl-c.unify-arrow-tgt I P A (T x) D.
526 coq.typecheck-indt-decl-c.unify-arrow-tgt I P A Concl D :-
527 coq.count-prods A N,
528 coq.mk-n-holes {calc (N + P)} Args,
529 coq.mk-app I Args IArgs,
530 coq.unify-eq Concl IArgs D.
531
532 :index (_ 2)
533 func coq.typecheck-indt-decl-field term, record-decl -> term.
534 coq.typecheck-indt-decl-field I end-record I.
535 coq.typecheck-indt-decl-field I (field _ ID T F) (prod N T F1) :-
536 coq.id->name ID N,
537 @pi-decl N T a\ coq.typecheck-indt-decl-field I (F a) (F1 a).
538
539 % Lifts coq.elaborate-skeleton to inductive declarations
540 :index (1)
541 func coq.elaborate-indt-decl-skeleton indt-decl -> indt-decl, diagnostic.
542 coq.elaborate-indt-decl-skeleton (parameter ID Imp T Decl) (parameter ID Imp T1 Decl1) Diag :- do-ok! Diag [
543 coq.elaborate-ty-skeleton T _ T1,
544 (d\ @pi-parameter ID T1 x\ coq.elaborate-indt-decl-skeleton (Decl x) (Decl1 x) d),
545 ].
546 coq.elaborate-indt-decl-skeleton (inductive ID I Arity KDecl) (inductive ID I Arity1 KDecl1) Diag :- do-ok! Diag [
547 coq.elaborate-arity-skeleton Arity _ Arity1,
548 lift-ok (coq.arity->nparams Arity1 NUPNO) "",
549 d\ coq.arity->term Arity1 A1, do-ok! d [
550 coq.typecheck-indt-decl.heuristic-var-type A1,
551 d\ @pi-parameter ID A1 i\ map-ok (KDecl i) (coq.elaborate-indt-decl-skeleton-c i Arity1 NUPNO) (KDecl1 i) d
552 ]
553 ].
554 coq.elaborate-indt-decl-skeleton (minductive ID I Arity Rest) (minductive ID I Arity1 Rest1) Diag :- do-ok! Diag [
555 coq.elaborate-arity-skeleton Arity _ Arity1,
556 lift-ok (coq.arity->nparams Arity1 NUPNO) "",
557 d\ coq.arity->term Arity1 A1, do-ok! d [
558 coq.typecheck-indt-decl.heuristic-var-type A1,
559 d\ @pi-parameter ID A1 i\ coq.elaborate-indt-decl-skeleton-mutual [pr i (pr Arity1 NUPNO)] (Rest i) (Rest1 i) d
560 ]
561 ].
562 coq.elaborate-indt-decl-skeleton (record ID A IDK FDecl) (record ID A1 IDK FDecl1) Diag :- do-ok! Diag [
563 coq.elaborate-ty-skeleton A _ A1,
564 lift-ok (A1 = sort U) "record type is not a sort",
565 d\ @pi-parameter ID A1 i\ coq.elaborate-indt-decl-skeleton-fields U FDecl FDecl1 d
566 ].
567
568 :index (_ 1)
569 func coq.elaborate-indt-decl-skeleton-mutual list (pair term (pair arity int)), indt-decl -> indt-decl, diagnostic.
570 coq.elaborate-indt-decl-skeleton-mutual Acc (minductive ID I Arity Rest) (minductive ID I Arity1 Rest1) Diag :- !, do-ok! Diag [
571 coq.elaborate-arity-skeleton Arity _ Arity1,
572 lift-ok (coq.arity->nparams Arity1 NUPNO) "",
573 d\ coq.arity->term Arity1 A1, do-ok! d [
574 coq.typecheck-indt-decl.heuristic-var-type A1,
575 d\ @pi-parameter ID A1 i\ coq.elaborate-indt-decl-skeleton-mutual [pr i (pr Arity1 NUPNO)|Acc] (Rest i) (Rest1 i) d
576 ]
577 ].
578 coq.elaborate-indt-decl-skeleton-mutual Acc (mblock Blocks) (mblock Blocks1) Diag :- !,
579 rev Acc Infos,
580 coq.elaborate-indt-decl-skeleton-blocks Infos Blocks Blocks1 Diag.
581 coq.elaborate-indt-decl-skeleton-mutual _ _ _ (error "minductive/mblock expected") :- !.
582
583 func coq.elaborate-indt-decl-skeleton-blocks list (pair term (pair arity int)), list (list indc-decl) -> list (list indc-decl), diagnostic.
584 coq.elaborate-indt-decl-skeleton-blocks [] [] [] ok :- !.
585 coq.elaborate-indt-decl-skeleton-blocks [pr I (pr Arity NUPNO)|Infos] [Ks|Blocks] [Ks1|Blocks1] Diag :- !, do-ok! Diag [
586 map-ok Ks (coq.elaborate-indt-decl-skeleton-c I Arity NUPNO) Ks1,
587 d\ coq.elaborate-indt-decl-skeleton-blocks Infos Blocks Blocks1 d
588 ].
589 coq.elaborate-indt-decl-skeleton-blocks _ _ _ (error "mblock constructor block count mismatch") :- !.
590
591 :index (_ 1)
592 func coq.elaborate-indt-decl-skeleton-fields sort, record-decl -> record-decl, diagnostic.
593 coq.elaborate-indt-decl-skeleton-fields _ end-record end-record ok.
594 coq.elaborate-indt-decl-skeleton-fields U (field Att ID A Fields) (field Att ID A1 Fields1) Diag :- do-ok! Diag [
595 coq.elaborate-ty-skeleton A UA A1,
596 lift-ok (coq.sort.leq UA U) "constructor universe too large",
597 d\ @pi-parameter ID A1 p\
598 coq.elaborate-indt-decl-skeleton-fields U (Fields p) (Fields1 p) d
599 ].
600
601 func coq.elaborate-indt-decl-skeleton-c term, arity, int, indc-decl -> indc-decl, diagnostic.
602 coq.elaborate-indt-decl-skeleton-c I SA NUPNO (constructor ID Arity) (constructor ID Arity1) Diag :- do-ok! Diag [
603 coq.elaborate-arity-skeleton-nuparams Arity NUPNO KS Arity1,
604 coq.typecheck-indt-decl-c.unify-arity I 0 SA Arity1,
605 lift-ok (coq.arity->sort {coq.arity->term SA} IS) "",
606 lift-ok (coq.sort.leq KS IS) "constructor universe too large"
607 ].
608
609 :index (_ _ 1 1)
610 func coq.typecheck-indt-decl-c.unify-arity term, int, arity, arity -> diagnostic.
611 coq.typecheck-indt-decl-c.unify-arity I PNO (parameter _ _ T1 A) (parameter ID _ T C) D :- do-ok! D [
612 coq.unify-eq T1 T,
613 lift-ok (PNO1 is PNO + 1) "",
614 d\ @pi-parameter ID T p\ coq.typecheck-indt-decl-c.unify-arity I PNO1 (A p) (C p) d
615 ].
616 coq.typecheck-indt-decl-c.unify-arity I PNO (arity A) (parameter ID _ T C) D :-
617 @pi-parameter ID T p\ coq.typecheck-indt-decl-c.unify-arity I PNO (arity A) (C p) D.
618 coq.typecheck-indt-decl-c.unify-arity I PNO (arity A) (arity C) D :-
619 coq.typecheck-indt-decl-c.unify-arrow-tgt I PNO A C D.
620
621 % Lifts coq.elaborate-skeleton to arity
622 func coq.elaborate-arity-skeleton arity -> sort, arity, diagnostic.
623 coq.elaborate-arity-skeleton (parameter ID Imp T A) U3 (parameter ID Imp T1 A1) Diag :- do-ok! Diag [
624 coq.elaborate-ty-skeleton T U1 T1,
625 (d\ @pi-parameter ID T1 i\ coq.elaborate-arity-skeleton (A i) U2 (A1 i) d),
626 lift-ok (coq.sort.pts-triple U1 U2 U3) "coq.elaborate-arity-skeleton: should not happen",
627 ].
628 coq.elaborate-arity-skeleton (arity A) U (arity A1) Diag :-
629 coq.elaborate-ty-skeleton A U A1 Diag.
630
631 :index (1)
632 func coq.elaborate-arity-skeleton-nuparams arity, int -> sort, arity, diagnostic.
633 coq.elaborate-arity-skeleton-nuparams (parameter ID Imp T A) 0 U3 (parameter ID Imp T1 A1) Diag :- !, do-ok! Diag [
634 coq.elaborate-ty-skeleton T U1 T1,
635 (d\ @pi-parameter ID T1 i\ coq.elaborate-arity-skeleton-nuparams (A i) 0 U2 (A1 i) d),
636 lift-ok (coq.sort.pts-triple U1 U2 U3) "coq.elaborate-arity-skeleton-nuparams: should not happen",
637 ].
638 coq.elaborate-arity-skeleton-nuparams (parameter ID Imp T A) N U (parameter ID Imp T1 A1) Diag :- do-ok! Diag [
639 coq.elaborate-ty-skeleton T _ T1,
640 lift-ok (M is N - 1) "",
641 (d\ @pi-parameter ID T1 i\ coq.elaborate-arity-skeleton-nuparams (A i) M U (A1 i) d),
642 ].
643 coq.elaborate-arity-skeleton-nuparams (arity A) _ U (arity A1) Diag :-
644 coq.elaborate-ty-skeleton A U A1 Diag.
645
646 func coq.term->arity term, int -> arity.
647 coq.term->arity T 0 (arity T) :- !.
648 coq.term->arity (prod Name S T) N (parameter ID explicit S R) :- N > 0,
649 M is N - 1,
650 coq.name->id Name ID,
651 @pi-decl Name S x\ coq.term->arity (T x) M (R x).
652
653 % extracts the sort at the end of an arity
654 func coq.arity->sort term -> sort.
655 coq.arity->sort (prod N S X) Y :- !, @pi-decl N S x\ coq.arity->sort (X x) Y.
656 coq.arity->sort (let N T V X) Y :- !, @pi-def N T V x\ coq.arity->sort (X x) Y.
657 coq.arity->sort (sort X) X :- !.
658 coq.arity->sort X Y :- coq.reduction.lazy.whd X Xr, not (X == Xr), !,
659 coq.arity->sort Xr Y.
660 coq.arity->sort T _ :- fatal-error-w-data "arity->sort: not a sort or prod" T.
661
662 % Counts how many parameters are there
663 func coq.arity->nparams arity -> int.
664 coq.arity->nparams (parameter _ _ _ In) O :-
665 pi x\ coq.arity->nparams (In x) O1, O is O1 + 1.
666 coq.arity->nparams (arity _) 0.
667
668 % Prints an arity
669 func coq.arity->pp arity -> coq.pp.
670 coq.arity->pp (parameter ID Imp T Arity) (coq.pp.glue Res) :-
671 Res = [coq.pp.box (coq.pp.hv 2)
672 [coq.pp.str A, coq.pp.str ID,
673 coq.pp.str " :", coq.pp.spc,TPP,coq.pp.str B],
674 coq.pp.spc, Rest],
675 if2 (Imp = explicit) (A = "(", B = ")")
676 (Imp = maximal) (A = "{", B = "}")
677 (A = "[", B = "]"),
678 coq.term->pp T TPP,
679 @pi-parameter ID T x\ coq.arity->pp (Arity x) Rest.
680 coq.arity->pp (arity T) (coq.pp.glue [coq.pp.str" : ",TPP]) :- coq.term->pp T TPP.
681
682 % Get impargs setting from an arity
683 func coq.arity->implicits arity -> list implicit_kind.
684 coq.arity->implicits (parameter Id I Ty F) [I|Is] :-
685 @pi-parameter Id Ty x\ coq.arity->implicits (F x) Is.
686 coq.arity->implicits (arity _) [].
687
688 func coq.constructor-block->implicits list indc-decl -> list (list implicit_kind).
689 coq.constructor-block->implicits Ks R :-
690 std.map Ks (c\i\sigma a\ c = constructor _ a, coq.arity->implicits a i) R.
691
692 % Get impargs settings from all inductives in an indt-decl, and from their
693 % constructor blocks, in declaration order.
694 func coq.indt-decl->implicits-all indt-decl -> list (list implicit_kind), list (list (list implicit_kind)).
695 coq.indt-decl->implicits-all (parameter Id I Ty F) Inds1 Ks1 :-
696 @pi-parameter Id Ty x\ coq.indt-decl->implicits-all (F x) Inds Ks,
697 std.map Inds (l\r\ r = [I|l]) Inds1,
698 std.map Ks (b\r\ std.map b (l\r\ r = [I|l]) r) Ks1.
699 coq.indt-decl->implicits-all (record _ _ _ _) [[]] [[[]]].
700 coq.indt-decl->implicits-all (inductive Id _ A Ks) [Is] [R] :-
701 coq.arity->implicits A Is,
702 @pi-inductive Id A x\ coq.constructor-block->implicits (Ks x) R.
703 coq.indt-decl->implicits-all (minductive Id _ A Rest) [Is|Inds] Ks :-
704 coq.arity->implicits A Is,
705 @pi-inductive Id A x\ coq.indt-decl->implicits-all (Rest x) Inds Ks.
706 coq.indt-decl->implicits-all (mblock Ks) [] R :-
707 std.map Ks coq.constructor-block->implicits R.
708
709 % Get impargs setting from the first inductive in an indt-decl.
710 func coq.indt-decl->implicits indt-decl -> list implicit_kind, list (list implicit_kind).
711 coq.indt-decl->implicits D Is Ks :-
712 coq.indt-decl->implicits-all D [Is|_] [Ks|_].
713
714 % Check if some implicits are set
715 func coq.any-implicit? list implicit_kind ->.
716 coq.any-implicit? L :- std.exists! L (x\not(x = explicit)).
717
718 % extract gref from terms that happen to have one
719 func coq.term->gref term -> gref.
720 coq.term->gref (global GR) GR :- !.
721 coq.term->gref (pglobal GR _) GR :- !.
722 coq.term->gref (app [Hd|_]) GR :- !, coq.term->gref Hd GR.
723 coq.term->gref (let _ _ T x\x) GR :- !, coq.term->gref T GR.
724 coq.term->gref (primitive (proj Proj _)) (const C) :- coq.env.primitive-projection? Proj C _, !.
725 :name "term->gref:fail"
726 coq.term->gref Term _ :-
727 fatal-error-w-data "term->gref: input has no global reference" Term.
728
729 func coq.fresh-type -> term.
730 coq.fresh-type (sort (typ U)) :- coq.univ.new U.
731
732 func coq.sort? term -> .
733 coq.sort? (sort _) :- !.
734 coq.sort? T :- whd1 T T1, coq.sort? T1.
735
736 % Map the term under a spine of fun nodes
737 func coq.map-under-fun term,
738 % InputTermUnderLams LamBoundVars TheirTypes Result
739 (func term, list term, list term -> term) ->
740 term.
741 coq.map-under-fun T F R :- map-under-fun.aux T [] [] F R.
742
743 :index (1)
744 func map-under-fun.aux term,
745 list term, list term,
746 (func term, list term, list term -> term)
747 -> term.
748 map-under-fun.aux (fun N T B) AccT AccTy F (fun N T R) :- !,
749 @pi-decl N T x\ map-under-fun.aux (B x) [x|AccT] [T|AccTy] F (R x).
750 map-under-fun.aux (let N T X B) AccT AccTy F (let N T X R) :- !,
751 @pi-def N T X x\ map-under-fun.aux (B x) AccT AccTy F (R x).
752 map-under-fun.aux End AccT AccTy F R :- F End {rev AccT} {rev AccTy} R.
753
754 func coq.iter-under-fun term,
755 % InputTermUnderLams LamBoundVars TheirTypes
756 (func term, list term, list term ->).
757 coq.iter-under-fun T F :- iter-under-fun.aux T [] [] F.
758
759 func iter-under-fun.aux term,
760 list term, list term,
761 (func term, list term, list term ->) ->.
762 iter-under-fun.aux (fun N T B) AccT AccTy F :- !,
763 @pi-decl N T x\ iter-under-fun.aux (B x) [x|AccT] [T|AccTy] F.
764 iter-under-fun.aux (let _ _ X B) AccT AccTy F :- !,
765 iter-under-fun.aux (B X) AccT AccTy F.
766 iter-under-fun.aux End AccT AccTy F :- F End {rev AccT} {rev AccTy}.
767
768 % Build a match given the term and function to build the return type and the
769 % branches
770 func coq.build-match
771 term, % T, the term being matched
772 term, % the type of T, expected to be an inductive, eventually applied
773 % MkRty: IndSort LamBoundVars TheirTypes Result
774 (func term, list term, list term -> term),
775 % MkBranch: Constructor ConstructorTyUnderLams LamBoundVars TheirTypes Result
776 (func term, term, list term, list term -> term)
777 -> term. % match T (.. MkRty) [ .. MkBranch K1, .. MkBranch K2, ..]
778 coq.build-match T Tty RtyF BranchF (match T Rty Bs) :-
779 whd Tty [] HD Args,
780 if2 (HD = global (indt GR)) true
781 (HD = pglobal (indt GR) I) true
782 fail,
783 (@uinstance! I ==> coq.env.indt GR _ Lno _ Arity Kn Kt),
784 take Lno Args LArgs,
785 (@uinstance! I ==> coq.mk-app {coq.env.global (indt GR)} LArgs IndtLArgs),
786 % Rty
787 coq.subst-prod LArgs Arity ArityArgs,
788 coq.bind-ind-arity-no-let IndtLArgs ArityArgs RtyF Rty,
789 % Bs
790 map Kt (coq.subst-prod LArgs) KtArgs,
791 map KtArgs hd-beta-zeta-reduce KtArgsNorm,
792 map KtArgsNorm coq.prod->fun KtArgsLam,
793 map Kn (k\r\ sigma K\ coq.env.global (indc k) K, coq.mk-app K LArgs r) KnArgs,
794 map2 KnArgs KtArgsLam (k\t\coq.map-under-fun t (BranchF k)) Bs.
795
796 % XXX the list of arguments are often needed in reverse order
797 :functional
798 pred coq.bind-ind-arity % calls K under (fun Arity (x : Ity Arity) =>..)
799 i:term, % the inductive type
800 i:term, % the arity
801 i:(func term, list term, list term -> term), % Sort Vars Tys Out
802 o:term. %
803 :functional
804 pred coq.bind-ind-arity.aux
805 i:term,
806 o:term,
807 i:list term, i:list term, i:term,
808 i:(func term, list term, list term -> term).
809 coq.bind-ind-arity.aux (prod N T B) (fun N T F) AccT AccTy IT K :- !,
810 @pi-decl N T x\ coq.bind-ind-arity.aux (B x) (F x) [x|AccT] [T|AccTy] IT K.
811 coq.bind-ind-arity.aux (let N T X B) (let N T X F) AccT AccTy IT K :- !,
812 @pi-def N T X x\ coq.bind-ind-arity.aux (B x) (F x) AccT AccTy IT K.
813 coq.bind-ind-arity.aux (sort _ as Sort) (fun `i` ITy F) AccT AccTy IT K :-
814 rev AccT Vars,
815 coq.mk-app IT Vars ITy,
816 @pi-decl `i` ITy x\ K Sort {append Vars [x]} {rev [ITy|AccTy]} (F x).
817 coq.bind-ind-arity IT Arity F R :- coq.bind-ind-arity.aux Arity R [] [] IT F.
818 % As above but let-ins are reduced
819 func coq.bind-ind-arity-no-let term, term, (func term, list term, list term -> term) -> term.
820 coq.bind-ind-arity-no-let IT Arity F R :-
821 (pi N T X B F AccT AccTy IT K\
822 coq.bind-ind-arity.aux (let N T X B) F AccT AccTy IT K :- !,
823 coq.bind-ind-arity.aux (B X) F AccT AccTy IT K) =>
824 coq.bind-ind-arity.aux Arity R [] [] IT F.
825
826 func coq.bind-ind-parameters inductive, (func term, list term, list term -> term) -> term.
827 coq.bind-ind-parameters I K O :-
828 coq.env.indt I _ _ N A _ _,
829 coq.bind-ind-parameters.aux N A [] [] K O.
830 func coq.bind-ind-parameters.aux int, term, list term, list term, (func term, list term, list term -> term) -> term.
831 coq.bind-ind-parameters.aux 0 Ty Vars Tys K O :- !, K Ty {std.rev Vars} {std.rev Tys} O.
832 coq.bind-ind-parameters.aux I (prod N T F) Vs Ts K (fun N T G) :- I > 0, !, J is I - 1,
833 @pi-decl N T x\
834 coq.bind-ind-parameters.aux J (F x) [x|Vs] [T|Ts] K (G x).
835 coq.bind-ind-parameters.aux I (let N T B F) Vs Ts K (fun N T G) :- I > 0, !, J is I - 1,
836 @pi-def N T B x\
837 coq.bind-ind-parameters.aux J (F x) [x|Vs] [T|Ts] K (G x).
838 coq.bind-ind-parameters.aux I T Vs Ts K O :- I > 0, whd1 T T', !,
839 coq.bind-ind-parameters.aux I T' Vs Ts K O.
840
841 % coq.with-TC Class Instance->Clause Code: runs Code under a context augmented with
842 % all instances for Class transformed by Instance->Clause.
843 func coq.with-TC term, (func tc-instance -> prop), (func) ->.
844 coq.with-TC Class Instance->Clause Code :-
845 coq.TC.db-for {coq.term->gref Class} Instances,
846 map Instances Instance->Clause Hyps, !,
847 Hyps => Code.
848
849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850
851 func coq.replay-synterp-action synterp-action.
852 coq.replay-synterp-action (begin-module ID) :- coq.env.begin-module ID _.
853 coq.replay-synterp-action (end-module MP) :- coq.env.end-module MP.
854 coq.replay-synterp-action (begin-module-type ID) :- coq.env.begin-module-type ID.
855 coq.replay-synterp-action (end-module-type MTP) :- coq.env.end-module-type MTP.
856 coq.replay-synterp-action (apply-module-functor ID) :- coq.env.apply-module-functor ID _ _ _ _ _.
857 coq.replay-synterp-action (apply-module-type-functor ID) :- coq.env.apply-module-type-functor ID _ _ _ _.
858 coq.replay-synterp-action (include-module MP) :- coq.env.include-module MP _.
859 coq.replay-synterp-action (include-module-type MP) :- coq.env.include-module-type MP _.
860 coq.replay-synterp-action (import-module MP) :- coq.env.import-module MP.
861 coq.replay-synterp-action (export-module MP) :- coq.env.export-module MP.
862 coq.replay-synterp-action (begin-section ID) :- coq.env.begin-section ID.
863 coq.replay-synterp-action (end-section) :- coq.env.end-section.
864
865 func coq.replay-next-synterp-actions.
866 coq.replay-next-synterp-actions :-
867 coq.next-synterp-action Action, !,
868 coq.replay-synterp-action Action, !,
869 coq.replay-next-synterp-actions.
870 coq.replay-next-synterp-actions.