forked from LPCIC/coq-elpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coq-lib.elpi
509 lines (441 loc) · 20.8 KB
/
coq-lib.elpi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/* coq-elpi: Coq terms as the object language of elpi */
/* license: GNU Lesser General Public License Version 2.1 or later */
/* ------------------------------------------------------------------------- */
shorten std.{fatal-error, fatal-error-w-data, debug-print, unsafe-cast}.
shorten std.{rev, map, append, appendR, map2, forall-ok, take, do-ok!, lift-ok}.
:before "default-fatal-error"
fatal-error Msg :- !, M is "elpi: " ^ Msg, stop M.
:before "default-fatal-error-w-data"
fatal-error-w-data Msg Data :- !,
term_to_string Data DataS,
M is "elpi: " ^ Msg ^ ": " ^ DataS, stop M.
:before "default-debug-print"
debug-print Msg Data :- !, M is "elpi: " ^ Msg, coq.say M Data.
% HACK: elpi's stop has no argument
type stop string -> prop.
stop S :- halt S.
% TODO: move to elpi proper
pred map-ok i:list A, i:(A -> B -> diagnostic -> prop), o:list A, o:diagnostic.
map-ok [X|L] P [Y|YS] S :- P X Y S0, if (S0 = ok) (map-ok L P YS S) (S = S0).
map-ok [] _ [] ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Helpers
%
% Pure LP code that works with the data types and API above.
% Named clauses are natural extension points, eg one can extend
% subst-prod to perform reduction in order to expose a "prod" node.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Term surgery %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pred coq.subst-prod i:list term, i:term, o:term.
coq.subst-prod [] P P :- !.
coq.subst-prod [X|XS] (prod _ _ F) P :- !, coq.subst-prod XS (F X) P.
coq.subst-prod XS (let _ _ X F) P :- !, coq.subst-prod XS (F X) P.
:name "subst-prod:fail"
coq.subst-prod [_|_] T _ :- !,
fatal-error-w-data "subst-prod: not a product" T.
pred coq.subst-fun i:list term, i:term, o:term.
coq.subst-fun [] T T :- !.
coq.subst-fun [X|XS] (fun _ _ F) T :- !, coq.subst-fun XS (F X) T.
coq.subst-fun XS (let _ _ X F) T :- !, coq.subst-fun XS (F X) T.
:name "subst-fun:fail"
coq.subst-fun [_|_] T _ :- !,
fatal-error-w-data "subst-fun: not a lambda" T.
pred coq.prod-R-fun o:term, o:term.
coq.prod-R-fun (prod N T F) (fun N T R) :- !, pi x\ coq.prod-R-fun (F x) (R x).
coq.prod-R-fun (let N T B F) (let N T B R) :- !, pi x\ coq.prod-R-fun (F x) (R x).
coq.prod-R-fun X X.
pred coq.prod->fun i:term, o:term.
coq.prod->fun (prod N T F) (fun N T R) :- !, pi x\ coq.prod->fun (F x) (R x).
coq.prod->fun (let N T B F) (let N T B R) :- !, pi x\ coq.prod->fun (F x) (R x).
coq.prod->fun X X.
pred coq.count-prods i:term, o:int.
coq.count-prods (prod _ _ B) N :- !, (pi x\ coq.count-prods (B x) M), N is M + 1.
coq.count-prods (let _ _ _ B) N :- !, (pi x\ coq.count-prods (B x) N).
:name "count-prod:end"
coq.count-prods _ 0 :- !.
pred coq.mk-n-holes i:int, o:list A.
coq.mk-n-holes 0 [] :- !.
coq.mk-n-holes N [HOLE_|R] :- !, M is N - 1, coq.mk-n-holes M R.
pred coq.safe-dest-app i:term, o:term, o:list term.
coq.safe-dest-app (app [X|XS]) HD AllArgs :- !,
coq.safe-dest-app X HD ARGS, append ARGS XS AllArgs.
coq.safe-dest-app X X [].
pred coq.mk-app i:term, i:list term, o:term.
coq.mk-app HD [] HD :- !.
coq.mk-app (app L) Args (app LArgs) :- !, append L Args LArgs.
coq.mk-app (fun _ _ F) [A|Args] R :- !, coq.mk-app (F A) Args R.
coq.mk-app (let _ _ A F) Args R :- !, coq.mk-app (F A) Args R.
coq.mk-app HD Args (app [HD|Args]).
pred coq.mk-app-uvar i:term, i:list term, o:term.
coq.mk-app-uvar HD [] HD :- !.
coq.mk-app-uvar (uvar as K) [A|Args] R :- !, unsafe-cast K K', coq.mk-app-uvar (K' A) Args R.
% coq.mk-eta n Ty T: performs up to n (when >= 0) eta expasion of T
% according to its type Ty. If n < 0 it makes as many step as
% products in Ty. There be dragons if T has not type Ty.
pred coq.mk-eta i:int, i:term, i:term, o:term.
coq.mk-eta 0 _ B B :- !.
coq.mk-eta N (prod Name Ty P) (fun _ _ F) (fun Name Ty F1) :- !, N1 is N - 1,
pi x \ coq.mk-eta N1 (P x) (F x) (F1 x).
coq.mk-eta N (prod Name Ty P) B (fun Name Ty B1) :- !, N1 is N - 1,
pi x \ coq.mk-eta N1 (P x) {coq.mk-app B [x]} (B1 x).
:name "mk-eta:end"
coq.mk-eta _ _ B B :- !.
% copy can be used to perform a replacement, eg
% (copy (const "foo") (const "bar) :- !) => copy T T1
% traverses T replacing foo with bar.
pred copy i:term, o:term.
:name "copy:start"
copy X Y :- name X, !, X = Y, !. % avoid loading "copy x x" at binders
copy (global _ as C) C :- !.
copy (sort _ as C) C :- !.
copy (fun N T F) (fun N T1 F1) :- !,
copy T T1, pi x\ copy (F x) (F1 x).
copy (let N T B F) (let N T1 B1 F1) :- !,
copy T T1, copy B B1, pi x\ copy (F x) (F1 x).
copy (prod N T F) (prod N T1 F1) :- !,
copy T T1, (pi x\ copy (F x) (F1 x)).
copy (app L) (app L1) :- !, map L copy L1.
copy (fix N Rno Ty F) (fix N Rno Ty1 F1) :- !,
copy Ty Ty1, pi x\ copy (F x) (F1 x).
copy (match T Rty B) (match T1 Rty1 B1) :- !,
copy T T1, copy Rty Rty1, map B copy B1.
copy (primitive _ as C) C :- !.
copy (uvar M L as X) W :- var X, !, map L copy L1, coq.mk-app-uvar M L1 W.
% when used in CHR rules
copy (uvar X L) (uvar X L1) :- map L copy L1.
pred copy-ctx-item i:prop, o:prop.
copy-ctx-item (decl X N T) (decl X1 N T1) :- copy X X1, copy T T1.
copy-ctx-item (def X N T B) (def X1 N T1 B1) :-
copy X X1, copy T T1, copy B B1.
pred copy-arity i:arity, o:arity.
copy-arity (parameter ID IMP T R) (parameter ID IMP T1 R1) :-
copy T T1, pi x\ copy-arity (R x) (R1 x).
copy-arity (arity T) (arity T1) :- copy T T1.
% Bridges the gap between the data types used to read/write inductives.
% The arguments are the same of coq.env.indt plus an an extra one being
% the output (of type indt-decl).
pred coq.build-indt-decl
i:(pair inductive id), i:bool, i:int, i:int, i:term, i:list (pair constructor id), i:list term, o:indt-decl.
coq.build-indt-decl GR IsInd Pno UPno Arity Kns Ktys Decl :-
coq.build-indt-decl-aux GR IsInd Pno UPno Arity Kns Ktys [] Decl.
pred coq.build-indt-decl-aux
i:pair inductive id, i:bool, i:int, i:int, i:term, i:list (pair constructor id), i:list term, i:list term, o:indt-decl.
coq.build-indt-decl-aux (pr GR I) IsInd NUPno 0 Ty Kns KtysNu Params (inductive I IsInd Arity Ks) :- !,
coq.term->arity Ty NUPno Arity,
std.map KtysNu (k\coq.term->arity k NUPno) Ktys,
rev Params ParamsR,
(pi i\ Sub i = [ % we factor uniform parameters
(pi x l\ copy (app[global (indt GR)|l]) (app[i|x]):- !, appendR ParamsR x l),
(copy (global (indt GR)) i :- !) ]),
pi i\
map2 Kns Ktys (gr_name\ ty\ res\
sigma tmp name\
Sub i => copy-arity ty tmp,
gr_name = pr _ name,
res = constructor name tmp)
(Ks i).
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, !,
coq.name->id N NS,
Pno1 is Pno - 1,
UPno1 is UPno - 1,
pi p\
map Ktys (coq.subst-prod [p]) (Ktys1 p),
coq.build-indt-decl-aux GR IsInd Pno1 UPno1 (T p) Kns (Ktys1 p) [p|Params] (Res p).
:name "coq.build-indt-decl-aux:fail"
coq.build-indt-decl-aux _ _ _ _ _ _ _ _ _ :- !,
fatal-error "coq.build-indt-decl-aux: invalid declaration".
pred coq.rename-arity
i:(id -> id -> prop),
i:arity,
o:arity.
coq.rename-arity RP (parameter ID I TY In) (parameter ID1 I TY Out) :-
RP ID ID1,
@pi-parameter ID TY p\
coq.rename-arity RP (In p) (Out p).
coq.rename-arity _ (arity T) (arity T).
% [coq.rename-indt-decl RenameParam RenameIndType RenameIndConstr D D1]
% can be used to rename all [id] part of an inductive type declaration
pred coq.rename-indt-decl
i:(id -> id -> prop),
i:(id -> id -> prop),
i:(id -> id -> prop),
i:indt-decl, o:indt-decl.
coq.rename-indt-decl RP RI RK (parameter ID I TY In) (parameter ID1 I TY Out) :-
RP ID ID1,
@pi-parameter ID TY p\
coq.rename-indt-decl RP RI RK (In p) (Out p).
coq.rename-indt-decl RP RI RK (inductive ID Ind A In) (inductive ID1 Ind A1 Out) :-
RI ID ID1,
coq.rename-arity RP A A1,
coq.id->name ID Name,
coq.arity->term A TY,
@pi-decl Name TY i\
std.map (In i) (coq.rename-indt-decl.aux RP RI RK) (Out i).
coq.rename-indt-decl _ RI RK (record ID A KID F) (record ID1 A KID1 F) :-
RI ID ID1,
RK KID KID1.
coq.rename-indt-decl.aux RP _ RK (constructor ID A) (constructor ID1 A1) :-
RK ID ID1,
coq.rename-arity RP A A1.
% Lifts coq.typecheck to inductive declarations
pred coq.typecheck-indt-decl.heuristic-var-type i:term, o:diagnostic.
coq.typecheck-indt-decl.heuristic-var-type (uvar _ _ as X) D :- !,
coq.univ.new [] U, coq.unify-eq X (sort (typ U)) D.
coq.typecheck-indt-decl.heuristic-var-type _ ok.
pred coq.typecheck-indt-decl i:indt-decl, o:diagnostic.
coq.typecheck-indt-decl (parameter ID _ T Decl) Diag :- do-ok! Diag [
coq.typecheck-ty T _,
(d\ @pi-parameter ID T x\ coq.typecheck-indt-decl (Decl x) d),
].
coq.typecheck-indt-decl (inductive ID _ Arity KDecl) Diag :- coq.arity->term Arity A, do-ok! Diag [
coq.typecheck-ty A _,
coq.typecheck-indt-decl.heuristic-var-type A,
d\ @pi-parameter ID A i\ forall-ok (KDecl i) (coq.typecheck-indt-decl-c i A) d
].
coq.typecheck-indt-decl (record ID A _IDK FDecl) Diag :- do-ok! Diag [
coq.typecheck-ty A _,
d\ @pi-parameter ID A i\ do-ok! d [
lift-ok (coq.typecheck-indt-decl-field i FDecl (K i)) "",
coq.typecheck-indt-decl-c i A (constructor "fields" (arity (K i)))
]
].
pred coq.typecheck-indt-decl-c i:term, i:term, i:indc-decl, o:diagnostic.
coq.typecheck-indt-decl-c I S (constructor _ID Arity) Diag :- coq.arity->term Arity T, do-ok! Diag [
coq.typecheck-ty T KS,
coq.typecheck-indt-decl-c.unify-arrow-tgt I S T,
lift-ok (coq.arity->sort S IS) "",
lift-ok (if (IS = typ U1, KS = typ U2) (coq.univ.leq U2 U1) true) "constructor universe too large"
].
pred coq.typecheck-indt-decl-c.unify-arrow-tgt i:term, i:term, i:term, o:diagnostic.
coq.typecheck-indt-decl-c.unify-arrow-tgt I A (prod N S T) D :-
@pi-decl N S x\ coq.typecheck-indt-decl-c.unify-arrow-tgt I A (T x) D.
coq.typecheck-indt-decl-c.unify-arrow-tgt I A (let N S B T) D :-
@pi-def N S B x\ coq.typecheck-indt-decl-c.unify-arrow-tgt I A (T x) D.
coq.typecheck-indt-decl-c.unify-arrow-tgt I A Concl D :-
coq.count-prods A N,
coq.mk-n-holes N Args,
coq.mk-app I Args IArgs,
coq.unify-eq Concl IArgs D.
pred coq.typecheck-indt-decl-field i:term, i:record-decl, o:term.
coq.typecheck-indt-decl-field I end-record I.
coq.typecheck-indt-decl-field I (field _ ID T F) (prod N T F1) :-
coq.id->name ID N,
@pi-decl N T a\ coq.typecheck-indt-decl-field I (F a) (F1 a).
% Lifts coq.elaborate-skeleton to inductive declarations
pred coq.elaborate-indt-decl-skeleton i:indt-decl, o:indt-decl, o:diagnostic.
coq.elaborate-indt-decl-skeleton (parameter ID Imp T Decl) (parameter ID Imp T1 Decl1) Diag :- do-ok! Diag [
coq.elaborate-ty-skeleton T _ T1,
(d\ @pi-parameter ID T1 x\ coq.elaborate-indt-decl-skeleton (Decl x) (Decl1 x) d),
].
coq.elaborate-indt-decl-skeleton (inductive ID I Arity KDecl) (inductive ID I Arity1 KDecl1) Diag :- do-ok! Diag [
coq.elaborate-arity-skeleton Arity _ Arity1,
d\ coq.arity->term Arity1 A1, do-ok! d [
coq.typecheck-indt-decl.heuristic-var-type A1,
d\ @pi-parameter ID A1 i\ map-ok (KDecl i) (coq.elaborate-indt-decl-skeleton-c i A1) (KDecl1 i) d
]
].
coq.elaborate-indt-decl-skeleton (record ID A IDK FDecl) (record ID A1 IDK FDecl1) Diag :- do-ok! Diag [
coq.elaborate-ty-skeleton A _ A1,
lift-ok (A1 = sort U) "record type is not a sort",
d\ @pi-parameter ID A1 i\ coq.elaborate-indt-decl-skeleton-fields U FDecl FDecl1 d
].
pred coq.elaborate-indt-decl-skeleton-fields i:universe, i:record-decl, o:record-decl, o:diagnostic.
coq.elaborate-indt-decl-skeleton-fields _ end-record end-record ok.
coq.elaborate-indt-decl-skeleton-fields U (field Att ID A Fields) (field Att ID A1 Fields1) Diag :- do-ok! Diag [
coq.elaborate-ty-skeleton A UA A1,
lift-ok (if (U = typ U1, UA = typ U2) (coq.univ.leq U2 U1) true) "constructor universe too large",
d\ @pi-parameter ID A1 p\
coq.elaborate-indt-decl-skeleton-fields U (Fields p) (Fields1 p) d
].
pred coq.elaborate-indt-decl-skeleton-c i:term, i:term, i:indc-decl, o:indc-decl, o:diagnostic.
coq.elaborate-indt-decl-skeleton-c I S (constructor ID Arity) (constructor ID Arity1) Diag :- do-ok! Diag [
coq.elaborate-arity-skeleton Arity KS Arity1,
coq.typecheck-indt-decl-c.unify-arity-tgt I S Arity1,
lift-ok (coq.arity->sort S IS) "",
lift-ok (if (IS = typ U1, KS = typ U2) (coq.univ.leq U2 U1) true) "constructor universe too large"
].
pred coq.typecheck-indt-decl-c.unify-arity-tgt i:term, i:term, i:arity, o:diagnostic.
coq.typecheck-indt-decl-c.unify-arity-tgt I A (parameter ID _ T C) D :-
@pi-parameter ID T p\ coq.typecheck-indt-decl-c.unify-arity-tgt I A (C p) D.
coq.typecheck-indt-decl-c.unify-arity-tgt I A (arity C) D :-
coq.typecheck-indt-decl-c.unify-arrow-tgt I A C D.
% Lifts coq.elaborate-skeleton to arity
pred coq.elaborate-arity-skeleton i:arity, o:universe, o:arity, o:diagnostic.
coq.elaborate-arity-skeleton (parameter ID Imp T A) U (parameter ID Imp T1 A1) Diag :- do-ok! Diag [
coq.elaborate-ty-skeleton T _ T1, % parameters don't count
d\ @pi-parameter ID T1 i\ coq.elaborate-arity-skeleton (A i) U (A1 i) d
].
coq.elaborate-arity-skeleton (arity A) U (arity A1) Diag :-
coq.elaborate-ty-skeleton A U A1 Diag.
% Converts an arity to a term
pred coq.arity->term i:arity, o:term.
coq.arity->term (parameter ID _ Ty Rest) (prod Name Ty R) :-
coq.id->name ID Name,
@pi-decl Name Ty x\ coq.arity->term (Rest x) (R x).
coq.arity->term (arity A) A.
pred coq.term->arity i:term, i:int, o:arity.
coq.term->arity T 0 (arity T).
coq.term->arity (prod Name S T) N (parameter ID explicit S R) :-
M is N - 1,
coq.name->id Name ID,
@pi-decl Name S x\ coq.term->arity (T x) M (R x).
% extracts the sort at the end of an arity
pred coq.arity->sort i:term, o:universe.
coq.arity->sort (prod N S X) Y :- !, @pi-decl N S x\ coq.arity->sort (X x) Y.
coq.arity->sort (sort X) X :- !.
:name "arity->sort:fail"
coq.arity->sort T _ :- fatal-error-w-data "arity->sort: not a sort or prod" T.
% Counts how many parameters are there
pred coq.arity->nparams i:arity, o:int.
coq.arity->nparams (parameter _ _ _ In) O :-
pi x\ coq.arity->nparams (In x) O1, O is O1 + 1.
coq.arity->nparams (arity _) 0.
% extract gref from terms that happen to have one
pred coq.term->gref i:term, o:gref.
coq.term->gref (global GR) GR :- !.
coq.term->gref (app [Hd|_]) GR :- !, coq.term->gref Hd GR.
:name "term->gref:fail"
coq.term->gref Term _ :-
fatal-error-w-data "term->gref: input has no global reference" Term.
pred coq.fresh-type o:term.
coq.fresh-type (sort (typ U)) :- coq.univ.new [] U.
% Map the term under a spine of fun nodes
pred coq.map-under-fun i:term,
% InputTermUnderLams LamBoundVars TheirTypes Result
i:(term -> list term -> list term -> term -> prop),
o:term.
coq.map-under-fun T F R :- map-under-fun.aux T [] [] F R.
map-under-fun.aux (fun N T B) AccT AccTy F (fun N T R) :- !,
@pi-decl N T x\ map-under-fun.aux (B x) [x|AccT] [T|AccTy] F (R x).
map-under-fun.aux (let _ _ X B) AccT AccTy F R :- !,
map-under-fun.aux (B X) AccT AccTy F R.
map-under-fun.aux End AccT AccTy F R :- F End {rev AccT} {rev AccTy} R.
pred coq.iter-under-fun i:term,
% InputTermUnderLams LamBoundVars TheirTypes
i:(term -> list term -> list term -> prop).
coq.iter-under-fun T F :- iter-under-fun.aux T [] [] F.
iter-under-fun.aux (fun N T B) AccT AccTy F :- !,
@pi-decl N T x\ iter-under-fun.aux (B x) [x|AccT] [T|AccTy] F.
iter-under-fun.aux (let _ _ X B) AccT AccTy F :- !,
iter-under-fun.aux (B X) AccT AccTy F.
iter-under-fun.aux End AccT AccTy F :- F End {rev AccT} {rev AccTy}.
% Build a match given the term and function to build the return type and the
% branches
pred coq.build-match
i:term, % T, the term being matched
i:term, % the type of T, expected to be an inductive, eventually applied
% MkRty: InputTermUnderLams LamBoundVars TheirTypes Result
i:(term -> list term -> list term -> term -> prop),
% MkBranch: Constructor ConstructorTyUnderLams LamBoundVars TheirTypes Result
i:(term -> term -> list term -> list term -> term -> prop),
o:term. % match T (.. MkRty) [ .. MkBranch K1, .. MkBranch K2, ..]
coq.build-match T Tty RtyF BranchF (match T Rty Bs) :-
whd Tty [] (global (indt GR)) Args,
coq.env.indt GR _ Lno _ Arity Kn Kt,
take Lno Args LArgs,
coq.mk-app (global (indt GR)) LArgs IndtLArgs,
% Rty
coq.subst-prod LArgs Arity ArityArgs,
coq.bind-ind-arity IndtLArgs ArityArgs RtyF Rty,
% Bs
map Kt (coq.subst-prod LArgs) KtArgs,
map KtArgs coq.prod->fun KtArgsLam,
map Kn (k\ coq.mk-app (global (indc k)) LArgs) KnArgs,
map2 KnArgs KtArgsLam (k\t\coq.map-under-fun t (BranchF k)) Bs.
% XXX the list of arguments are often needed in reverse order
pred coq.bind-ind-arity % calls K under (fun Arity (x : Ity Arity) =>..)
i:term, % the inductive type
i:term, % the arity
i:(term -> list term -> list term -> term -> prop), % Sort Vars Tys Out
o:term. %
bind-ind-arity.aux (prod N T B) (fun N T F) AccT AccTy IT K :- !,
@pi-decl N T x\ bind-ind-arity.aux (B x) (F x) [x|AccT] [T|AccTy] IT K.
bind-ind-arity.aux (let _ _ X B) F AccT AccTy IT K :- !,
bind-ind-arity.aux (B X) F AccT AccTy IT K.
bind-ind-arity.aux (sort _ as Sort) (fun `i` ITy F) AccT AccTy IT K :-
rev AccT Vars,
coq.mk-app IT Vars ITy,
@pi-decl `i` ITy x\ K Sort {append Vars [x]} {rev [ITy|AccTy]} (F x).
coq.bind-ind-arity IT Arity F R :- bind-ind-arity.aux Arity R [] [] IT F.
% coq.with-TC Class Instance->Clause Code: runs Code under a context augmented with
% all instances for Class transformed by Instance->Clause.
pred coq.with-TC i:term, i:(tc-instance -> prop -> prop), i:prop.
coq.with-TC Class Instance->Clause Code :-
coq.TC.db-for {coq.term->gref Class} Instances,
map Instances Instance->Clause Hyps, !,
Hyps => Code.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Coq attribute parser, eg [#[attribute=value] Command]
%
% Usage:
% main _ :-
% attributes A, % fetch
% coq.parse-attributes A Spec Opts, % parse/validate
% Opts => (mycode, get-option "foo" V, mycode). % use
%
% where [Opts] is a list of clauses [get-option StringName Value], where value
% can have any type and [Spec] is a list of [attribute-sigmature].
% Example of an attribute signature:
% [
% att "this" bool,
% att "that.thing" int,
% att "algebraic" (oneof ["foo" `-> foo-thing, "bar" `-> barbar]),
% ]
type get-option string -> A -> prop.
kind attribute-signature type.
type att string -> attribute-type -> attribute-signature.
type att-ignore-unknown attribute-signature.
type supported-attribute attribute-signature -> prop.
kind attribute-type type.
type int attribute-type.
type string attribute-type.
type bool attribute-type.
type oneof list attribute-mapping -> attribute-type.
kind attribute-mapping type.
type (`->) string -> any -> attribute-mapping.
pred coq.valid-attribute i:string, i:string, o:option any, o:diagnostic.
coq.valid-attribute Name Value V Diag :-
if (supported-attribute (att Name Type))
(coq.typecheck-attribute Name Type Value LPV Diag, V = some LPV)
(if (supported-attribute att-ignore-unknown) (V = none, Diag = ok)
(Diag = error {calc ( "Attribute " ^ Name ^ " is not supported")})).
:index (_ 1 1)
pred coq.typecheck-attribute i:string, i:attribute-type, i:string, o:any, o:diagnostic.
coq.typecheck-attribute _ int Value V ok :- V is string_to_int Value, !.
coq.typecheck-attribute N int Value _ (error Msg) :-
Msg is "Attribute " ^ N ^ " takes an integer, got: " ^ Value.
coq.typecheck-attribute _ string V V ok.
coq.typecheck-attribute _ bool "true" tt ok.
coq.typecheck-attribute _ bool "tt" tt ok.
coq.typecheck-attribute _ bool "True" tt ok.
coq.typecheck-attribute _ bool "on" tt ok.
coq.typecheck-attribute _ bool "" tt ok.
coq.typecheck-attribute _ bool "false" ff ok.
coq.typecheck-attribute _ bool "False" ff ok.
coq.typecheck-attribute _ bool "off" ff ok.
coq.typecheck-attribute _ bool "ff" ff ok.
coq.typecheck-attribute N bool Value _ (error Msg) :-
Msg is "Attribute " ^ N ^ " takes an boolean, got: " ^ Value.
pred coq.is-one-of i:string, o:any, i:attribute-mapping.
coq.is-one-of K V (K `-> V).
coq.typecheck-attribute _ (oneof L) K V ok :- std.exists L (coq.is-one-of K V), !.
coq.typecheck-attribute N (oneof L) K _ (error Msg) :-
std.map L (x\r\ sigma tmp\ x = r `-> tmp) S,
std.fold S "" (s\ a\ calc (a ^ " " ^ s)) OneOf,
Msg is "Attribute " ^ N ^ " takes one of " ^ OneOf ^ ", got: " ^ K.
pred coq.parse-attributes i:list attribute, i:list attribute-signature, o:list prop.
coq.parse-attributes L S O :-
std.map S (x\r\ r = supported-attribute x) CS,
CS => parse-attributes.aux L "" O, !.
parse-attributes.aux [] _ [].
parse-attributes.aux [attribute S (node L)|AS] Prefix R :- !,
parse-attributes.aux AS Prefix R1,
if (Prefix = "") (PS = S) (PS is Prefix ^ "." ^ S),
parse-attributes.aux L PS R2,
std.append R1 R2 R.
parse-attributes.aux [attribute S (leaf V)|AS] Prefix CLS :- !,
if (Prefix = "") (PS = S) (PS is Prefix ^ "." ^ S),
coq.valid-attribute PS V V1 Diag,
if (Diag = error Msg) (coq.error Msg) true,
if (V1 = some Val) (CLS = [get-option PS Val|R]) (CLS = R), % ignored
parse-attributes.aux AS Prefix R.