/
hb.elpi
2523 lines (2129 loc) · 103 KB
/
hb.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Hierarchy Builder: algebraic hierarchies made easy
This software is released under the terms of the MIT license */
%%%%%%% Naming converntions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*
- under-foobar.do! Arg [ Code ]
enriches the context with foobar, the runs std.do! [ Code ]
- under-foobar.then Arg F Out
enriches the context with foobar, the runs F Out, as a consequence
the spilling expression {under-foobar.then Arg F} can be used
- foo_bar
projection from foo to its field bar
- foo->bar
conversion from type foo to type bar (it can be arbitrarily complex)
- get-foobar
reads foobar from the Coq world
- findall-foobar
reads foobar from hb.db, the output is sorted whenever it makes sense
- main-foobar
main entry point for a user facing (or almost user facing) command foobar
- declare-foobar
predicate adding to the Coq ennvironment a foobar
- postulate-foobar
predicate assuming a foobar (declaring a Coq section variable)
- TheType, TheClass, TheFoobar
the thing the current code is working on, eg the type of the structure
begin defined
- FooAlias
see phant-abbrev, used to talk about the non canonical name of Foo
- when foo is the constructor of a data type with type A1 -> .. -> AN -> t
we define mk-foo as:
mk-foo A1 .. AN (foo A1 .. AN)
*/
shorten coq.{ term->gref, subst-fun, safe-dest-app, mk-app, mk-eta, subst-prod }.
%%%%%%%%% Elpi Utils %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This code could be moved in Elpi's standard library
% printing the local mixin context
pred print-ctx.
print-ctx :- declare_constraint print-ctx [].
constraint print-ctx mixin-src {
rule \ (G ?- print-ctx) | (coq.say "The context is:" G).
}
% TODO: pred toposort i:(A -> A -> prop), i:list A, o:list A.
% pred edge? i:int, i:int.
% toposort edge? [1,2,3,4] TopoList
pred topovisit i: list (pair A A), i: A, i: list A, i: list A, o: list A, o: list A.
topovisit _ X VS PS VS PS :- std.mem PS X, !.
topovisit _ X VS _ _ _ :- std.mem VS X, !, halt "cycle detected.".
topovisit ES X VS PS VS' [X|PS'] :-
toporec ES {std.map {std.filter ES (e\ fst e X)} snd} [X|VS] PS VS' PS'.
pred toporec i: list (pair A A), i: list A, i: list A, i: list A, o: list A, o: list A.
toporec _ [] VS PS VS PS.
toporec ES [X|XS] VS PS VS'' PS'' :-
topovisit ES X VS PS VS' PS', toporec ES XS VS' PS' VS'' PS''.
pred toposort i: list (pair A A), i: list A, o: list A.
toposort ES XS XS'' :-
toporec ES XS [] [] _ XS',
std.filter XS' (std.mem XS) XS''.
pred bubblesort i:list A, i:(A -> A -> prop), o:list A.
bubblesort [] _ [] :- !.
bubblesort [X] _ [X] :- !.
bubblesort [X,Y|TL] Rel [X|Rest1] :- Rel X Y, !, bubblesort [Y|TL] Rel Rest1.
bubblesort [X,Y|TL] Rel [Y|Rest1] :- bubblesort [X|TL] Rel Rest1.
pred list-diff i:list A, i:list A, o:list A.
list-diff X [] X.
list-diff L [D|DS] R :-
std.filter L (x\ not(x = D)) L1,
list-diff L1 DS R.
pred list-eq-set i:list A, i:list A.
list-eq-set L1 L2 :- list-diff L1 L2 [], list-diff L2 L1 [].
pred under.do! i:((A -> Prop) -> A -> prop), i:list prop.
under.do! Then LP :- Then (_\ std.do! LP) _.
%%%%% Logging Utils %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pred logger o:list coq.pp, o:bool.
pred logger-extend i:list coq.pp, i:coq.pp.
logger-extend [] _ :- coq.error "HB: logger was closed".
logger-extend (uvar as X) V :- X = [V|FRESH_].
logger-extend [_|XS] V :- logger-extend XS V.
pred logger-close i:list coq.pp.
logger-close (uvar as X) :- X = [].
logger-close [_|XS] :- logger-close XS.
pred log-vernac i:coq.vernac.
log-vernac V :- logger L Nice, !,
if (Nice = tt) (PPALL = []) (PPALL = [@ppmost!]),
logger-extend L {PPALL => coq.vernac->pp [V]}.
log-vernac _.
pred with-logging i:prop.
with-logging P :- get-option "log" tt,
logger L tt => P,
logger-close L,
std.intersperse coq.pp.spc L PP,
coq.pp->string (coq.pp.box (coq.pp.v 0) PP) S,
coq.say "(* \n" S "\n*)".
with-logging P :- get-option "elpi.log_hb" _, % env variable
logger L ff => P,
logger-close L,
std.intersperse coq.pp.spc L PP,
coq.pp->string (coq.pp.box (coq.pp.v 0) PP) S,
get-option "elpi.loc" Loc,
rex_split "," Loc [FILE|_],
FILENAME is FILE ^ ".hb",
open_append FILENAME OC,
std.string.concat "\n" ["","HIERARCHY BUILDER PATCH",Loc,S] PATCH,
output OC PATCH,
close_out OC.
with-logging P :- P.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Coq document
% This is a very minimalistic AST to represent a Coq document equipped with
% pretty printing facilities.
%
% When "complete enough" this should be moved to coq-elpi proper.
kind coq.vernac type.
type coq.vernac.begin-module string -> option string -> coq.vernac.
type coq.vernac.end-module string -> coq.vernac.
type coq.vernac.begin-section string -> coq.vernac.
type coq.vernac.end-section string -> coq.vernac.
type coq.vernac.import-module string -> coq.vernac.
type coq.vernac.export-module string -> coq.vernac.
type coq.vernac.definition string -> option term -> term -> coq.vernac.
type coq.vernac.variable string -> term -> coq.vernac.
type coq.vernac.inductive indt-decl -> coq.vernac.
type coq.vernac.abbreviation string -> int -> term -> coq.vernac.
type coq.vernac.coercion string -> gref -> class -> coq.vernac.
type coq.vernac.canonical string -> coq.vernac.
type coq.vernac.implicit string -> list implicit_kind -> coq.vernac.
type coq.vernac.comment A -> coq.vernac.
% The main entry point to print vernacular commands is coq.vernac->pp
{
shorten coq.vernac.{ begin-module , end-module , begin-section, end-section }.
shorten coq.vernac.{ import-module , export-module }.
shorten coq.vernac.{ definition , variable , comment }.
shorten coq.{ vernac.inductive , vernac.implicit }.
shorten coq.vernac.{ canonical , abbreviation , coercion }.
shorten coq.pp.{ box , h , spc , v , str , hv , hov, glue, brk }.
pred coq.vernac->pp i:list coq.vernac, o:coq.pp.
coq.vernac->pp L (box (v 0) L2) :-
std.map L coq.vernac->pp1 L1,
std.intersperse spc L1 L2.
pred coq.vernac->pp1 i:coq.vernac, o:coq.pp.
coq.vernac->pp1 (begin-module Name none) PP :-
PP = box h [str "Module ", str Name, str "."].
coq.vernac->pp1 (begin-module Name (some TyName)) PP :-
PP = box h [str "Module ", str Name, str " : ", str TyName, str "."].
coq.vernac->pp1 (end-module Name) PP :-
PP = box h [str "End ", str Name, str "."].
coq.vernac->pp1 (begin-section Name) PP :-
PP = box h [str "Section ", str Name, str "."].
coq.vernac->pp1 (end-section Name) PP :-
PP = box h [str "End ", str Name, str "."].
coq.vernac->pp1 (definition Name none Body) PP :-
PP = box (hv 2) [str "Definition ", str Name, str " :=", spc, B, str "."],
coq.term->pp Body B.
coq.vernac->pp1 (definition Name (some Ty) Body) PP :-
PP = box (hv 2) [str "Definition ", str Name, str " : ", T, str " :=", spc, B, str "."],
coq.term->pp Ty T,
coq.term->pp Body B.
coq.vernac->pp1 (variable Name Ty) (box (hv 2) [box h [str "Variable ", str Name, str " :"], spc, TY, str "."]) :-
coq.term->pp Ty TY.
coq.vernac->pp1 (import-module Name) (box h [str "Import ", str Name, str "."]).
coq.vernac->pp1 (export-module Name) (box h [str "Export ", str Name, str "."]).
coq.vernac->pp1 (abbreviation Name NParams Term) (box (hv 2) [box h [str "Notation ",str Name|StrParams], str " :=", spc, B, str "."]) :-
coq.vernac->ppabbrterm NParams Term StrParams B.
coq.vernac->pp1 (canonical Name) (box h [str "Canonical ", str Name, str "."]).
coq.vernac->pp1 (coercion Name SRC TGT) (box h [str "Coercion ", str Name, str " : ", str S, str " >-> ", str T, str "."]) :-
coq.gref->path SRC SP, std.string.concat "." {std.take-last 2 SP} S,
if2 (TGT = sortclass) (T = "Sortclass")
(TGT = funclass) (T = "Funclass")
(TGT = grefclass GR, coq.gref->path GR GRP, std.string.concat "." {std.take-last 2 GRP} T).
coq.vernac->pp1 (vernac.inductive I) PP :-
coq.vernac->ppinductive I [] PP.
coq.vernac->pp1 (vernac.implicit Name []) (box h [str "Arguments ", str Name, str " : clear implicits."]).
coq.vernac->pp1 (vernac.implicit Name L) (box h [str "Arguments ", str Name, spc, glue PP, str "."]) :-
std.map L coq.vernac->ppimparg PP1,
std.intersperse spc PP1 PP.
coq.vernac->pp1 (comment A) (box (hov 2) [str"(*", str S, str"*)"]) :-
std.any->string A S.
pred coq.vernac->ppimparg i:implicit_kind, o:coq.pp.
coq.vernac->ppimparg explicit (str "_").
coq.vernac->ppimparg maximal (str "{_}").
coq.vernac->ppimparg implicit (str "[_]").
pred coq.vernac->ppinductive i:indt-decl, i:list (pair implicit_kind term), o:coq.pp.
coq.vernac->ppinductive (parameter ID IMPL TY I) Acc R :-
@pi-parameter ID TY p\ coq.vernac->ppinductive (I p) [pr IMPL p|Acc] R.
coq.vernac->ppinductive (record ID SORT KID RD) ParamsRev PP :-
PP = (box (hov 0) [
box (hov 0) [str "Record", spc, str ID, brk 1 4, glue ParamsPP,
str " : ", SortPP, brk 1 2, str":= ", str KID],
brk 1 2,
box (hv 2) [str"{", spc, glue FieldsPP, str"}"],
str"."]),
std.rev ParamsRev Params,
coq.vernac->ppinductiveparams Params ParamsPP,
coq.term->pp SORT SortPP,
coq.vernac->pprecordfields RD FieldsPP.
coq.vernac->ppinductive (inductive ID IsInd Arity Ks) ParamsRev PP :-
PP = (box (hov 0) [
str CO,str "Inductive", spc,
box (hov 0) [
str ID, brk 1 4, glue ParamsPP, ArityPP, str " :="],
brk 0 2,
box (hv 2) [str" ", glue KsPp],
str "."]),
std.rev ParamsRev Params,
coq.vernac->ppinductiveparams Params ParamsPP,
std.map Params snd ParamsAsArgs,
if (IsInd = tt) (CO = "") (CO = "Co"),
coq.arity->pp Arity ArityPP,
@pi-inductive ID Arity x\
coq.mk-app x ParamsAsArgs (X x),
coq.vernac->ppinductiveconstructor (Ks (X x)) KsPp.
pred coq.vernac->ppinductiveconstructor i:list indc-decl, o:list coq.pp.
coq.vernac->ppinductiveconstructor [] [].
coq.vernac->ppinductiveconstructor [constructor ID Arity|Ks] PP :-
PP = [str ID,{coq.arity->pp Arity},SEP|Rest],
if (Ks = []) (SEP = str"") (SEP = glue [brk 1 0, str "| "]),
coq.vernac->ppinductiveconstructor Ks Rest.
pred coq.vernac->ppinductiveparams i:list (pair implicit_kind term), o:list coq.pp.
coq.vernac->ppinductiveparams [] [].
coq.vernac->ppinductiveparams [pr Imp T|Rest] PP :-
PP = [box (hov 2) [str A,str ID,str " : ", TY,str B]|PPRest],
decl T Name Ty, coq.name->id Name ID, coq.term->pp Ty TY,
if2 (Imp = explicit) (A = "(", B = ")")
(Imp = maximal) (A = "{", B = "}")
(A = "[", B = "]"),
coq.vernac->ppinductiveparams Rest PPRest.
pred coq.vernac->pprecordfields i:record-decl, o:list coq.pp.
coq.vernac->pprecordfields end-record [].
coq.vernac->pprecordfields (field _ ID TY F) [ str ID, str " : ", TYPP, str ";", spc|FPP] :- % TODO attributes
coq.term->pp TY TYPP,
@pi-parameter ID TY p\ coq.vernac->pprecordfields (F p) FPP.
pred coq.vernac->ppabbrterm i:int, i:term, o:list coq.pp, o:coq.pp.
coq.vernac->ppabbrterm 0 T [] B :- !, @holes! => coq.term->pp T B.
coq.vernac->ppabbrterm N (fun _ _ F) [spc,str ID|StrParams] B :-
ID is "X" ^ {std.any->string N},
coq.id->name ID Name,
M is N - 1,
@pi-decl Name (sort prop) x\ coq.vernac->ppabbrterm M (F x) StrParams B.
}
%%%%% HB Utils %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HACK: move to coq-elpi proper, remove when coq-elpi > 1.9.2
type attmap attribute-type.
% runs P in a context where Coq #[attributes] are parsed
pred with-attributes i:prop.
with-attributes P :-
attributes A,
% HACK: move to coq-elpi proper, remove when coq-elpi > 1.9.2
(pi S L AS Prefix R R1 Map PS\
parse-attributes.aux [attribute S (node L)|AS] Prefix R :-
if (Prefix = "") (PS = S) (PS is Prefix ^ "." ^ S), supported-attribute (att PS attmap), !,
parse-attributes.aux AS Prefix R1,
(pi x\ supported-attribute (att x string) :- !) => parse-attributes.aux L "" Map,
std.append R1 [get-option PS Map] R
) =>
coq.parse-attributes A [
att "verbose" bool,
att "mathcomp" bool,
att "mathcomp.axiom" string,
att "infer" attmap,
att "log" bool,
] Opts, !,
Opts => P.
pred if-verbose i:prop.
if-verbose P :- get-option "verbose" tt, !, P.
if-verbose _.
pred if-MC-compat i:(option gref -> prop).
if-MC-compat P :- get-option "mathcomp" tt, !, P none.
if-MC-compat P :- get-option "mathcomp.axiom" S, !,
std.assert! (coq.locate S GR) "The name passed to the mathcomp.axiom attribute does not exist",
P (some GR).
if-MC-compat _.
% TODO: Should this only be used for gref that are factories? (and check in the first/second branch so?)
% Should we make this an HO predicate, eg "located->gref S L is-factory? GR"
pred located->gref i:string, i:list located, o:gref.
located->gref _ [loc-gref GR|_] GR.
located->gref _ [loc-abbreviation Abbrev|_] GR :- phant-abbrev GR _ Abbrev, !.
located->gref S [loc-abbreviation _|_] _ :- coq.error S "is an abbreviation out of the control of HB".
located->gref S [loc-modpath _|_] _ :- coq.error S "should be a factory, but is a module".
located->gref S [loc-modtypath _|_] _ :- coq.error S "should be a factory, but is a module type".
located->gref S [] _ :- coq.error "Could not locate name" S.
% TODO: generalize/rename when we support parameters
pred argument->gref i:argument, o:gref.
argument->gref (str S) GR :- located->gref S {coq.locate-all S} GR.
argument->gref X _ :- coq.error "Argument" X "is expected to be a string".
pred argument->term i:argument, o:term.
argument->term (str S) (global GR) :- !, argument->gref (str S) GR.
argument->term (trm T) T1 :- !, std.assert-ok! (coq.elaborate-skeleton T _ T1) "not well typed term".
argument->term X _ :- coq.error "Argument" X " is expected to be a term or a string".
pred argument->ty i:argument, o:term.
argument->ty (str S) T1 :- !, argument->gref (str S) GR, std.assert-ok! (coq.elaborate-ty-skeleton (global GR) _ T1) "global reference is not a type".
argument->ty (trm T) T1 :- !, std.assert-ok! (coq.elaborate-ty-skeleton T _ T1) "not well typed type".
argument->ty X _ :- coq.error "Argument" X " is expected to be a type or a string".
% Type to share code between HB.mixin and HB.factory (that supports alias factories)
kind asset type.
type asset-mixin asset.
type asset-factory asset.
kind asset-decl type.
type asset-parameter id -> term -> (term -> asset-decl) -> asset-decl.
type asset-record id -> term -> id -> record-decl -> asset-decl.
type asset-alias id -> term -> asset-decl.
pred name-of-asset-decl i:asset-decl, o:string.
name-of-asset-decl (asset-parameter _ _ R) X :-
pi x\ name-of-asset-decl (R x) X.
name-of-asset-decl (asset-record X _ _ _) X.
name-of-asset-decl (asset-alias X _) X.
pred argument->asset i:argument, o:asset-decl.
argument->asset (indt-decl (parameter ID _ImplicitStatus TySkel I)) (asset-parameter ID Ty A) :- !,
% Should we check that _ImplicitStatus is explicit?
coq.string->name ID Name,
std.assert-ok! (coq.elaborate-ty-skeleton TySkel _ Ty) "parameter illtyped",
@pi-decl Name Ty a\
argument->asset (indt-decl (I a)) (A a).
argument->asset (indt-decl (record Rid Ty Kid F)) (asset-record Rid Ty Kid F) :- !.
argument->asset (const-decl Id (some (fun _ _ Bo)) (parameter ID _ SrcSkel Ty)) (asset-parameter ID Src A) :- !,
coq.id->name ID Name,
std.assert-ok! (coq.elaborate-ty-skeleton SrcSkel _ Src) "parameter illtyped",
@pi-decl Name Src a\
argument->asset (const-decl Id (some (Bo a)) (Ty a)) (A a).
argument->asset (const-decl Id (some Bo) (arity Ty)) (asset-alias Id Bo) :- !,
std.assert! (var Ty) "Factories aliases should not be given a type".
argument->asset X _ :- coq.error "Unsupported asset:" X.
pred builder->string i:builder, o:string.
builder->string (builder _ _ _ B) S :- coq.term->string (global B) S.
pred nice-gref->string i:gref, o:string.
nice-gref->string X Mod :-
coq.gref->path X Path,
std.rev Path [_,Mod|_], !.
nice-gref->string X S :-
coq.term->string (global X) S.
pred target-gref i:term, o:gref.
target-gref T GR :- whd1 T T1, !, target-gref T1 GR.
target-gref (prod N Src Tgt) GR :- !, @pi-decl N Src x\ target-gref (Tgt x) GR.
target-gref End GR :- term->gref End GR.
pred append-phant-unify i:phant-term, o:phant-term.
append-phant-unify (phant-term LP T) (phant-term LPU T) :-
std.append LP [unify-arg] LPU.
pred copy-fields i:record-decl, o:record-decl.
copy-fields end-record end-record.
copy-fields (field C N T R) (field C N T1 R1) :-
copy T T1,
pi x\ copy x x => copy-fields (R x) (R1 x).
pred copy-triple i:(A -> A1 -> prop), i:(B -> B1 -> prop), i:(C -> C1 -> prop), i:triple A B C, o:triple A1 B1 C1.
copy-triple F G H (triple X Y Z) (triple X1 Y1 Z1) :- F X X1, G Y Y1, H Z Z1.
pred triple_1 i:triple A B C, o:A.
triple_1 (triple A _ _) A.
pred copy-list i:(A -> A1 -> prop), i:list A, o: list A1.
copy-list _ [] [].
copy-list F [X|XS] [Y|YS] :- F X Y, copy-list F XS YS.
pred gref->modname i:mixinname, o:id.
gref->modname GR ModName :-
coq.gref->path GR Path,
if (std.rev Path [_,ModName|_]) true (coq.error "No enclosing module for " GR).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function to predicate generic constructions %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pred mk-nil o:any.
mk-nil [].
pred mk0 i:any, o:any.
mk0 F R :- constant R F [].
pred mk1 i:any, i:any, o:any.
mk1 F X1 R :- constant R F [X1].
pred mk2 i:any, i:any, i:any, o:any.
mk2 F X1 X2 R :- constant R F [X1, X2].
pred mk3 i:any, i:any, i:any, i:any, o:any.
mk3 F X1 X2 X3 R :- constant R F [X1, X2, X3].
pred mk4 i:any, i:any, i:any, i:any, i:any, o:any.
mk4 F X1 X2 X3 X4 R :- constant R F [X1, X2, X3,X4].
pred mk-fun i:name, i:term, i:(term -> term), o:term.
mk-fun N Ty Body (fun N Ty Body).
% generic argument to pass to w-params
pred ignore i:name, i:term, i:(term -> A), o:A.
ignore _ _ F X :- (pi x y\ F x = F y), X = F (sort prop).
% combining body and type
pred mk-fun-prod i:name, i:term, o:(term -> pair term term), o:pair term term.
mk-fun-prod N Ty (x\ pr (Body x) (Type x)) (pr (fun N Ty Body) (prod N Ty Type)).
pred mk-parameter i:implicit_kind, i:name, i:term, i:(term -> indt-decl), o:indt-decl.
mk-parameter IK Name X F Decl :- !, Decl = parameter {coq.name->id Name} IK X F.
%%%%%%%%%%%%%%%%%%%%%%
% w-params interface %
%%%%%%%%%%%%%%%%%%%%%%
pred apply-w-params i:w-params A, i:list term, i:term, o:A.
apply-w-params (w-params.cons _ _ PL) [P|PS] T R :- !, apply-w-params (PL P) PS T R.
apply-w-params (w-params.nil _ _ L) [] T R :- !, R = L T.
apply-w-params _ _ _ _ :- coq.error "apply-w-params".
pred w-params.nparams i:w-params A, o:int.
w-params.nparams (w-params.cons _ _ F) N :- pi x\ w-params.nparams (F x) M, N is M + 1.
w-params.nparams (w-params.nil _ _ _) 0.
% [w-params.fold AwP Cons Nil Out] states that Out has shape
% Cons `x_1` T_1 p_1 \ .. \ Nil [p_1 .. p_n] `T` Ty F
% where AwP = w-params.cons `x_1` T_1 p_1 \ ... \ w-params.nil `T` Ty F
pred w-params.fold i:w-params A, i:(name -> term -> (term -> B) -> B -> prop),
i:(list term -> name -> term -> (term -> A) -> B -> prop), o:B.
w-params.fold L Cons Nil Out :- w-params.fold.params L Cons Nil [] Out.
pred w-params.fold.params i:w-params A,
i:(name -> term -> (term -> B) -> B -> prop),
i:(list term -> name -> term -> (term -> A) -> B -> prop),
i:list term, % accumulator
o:B.
w-params.fold.params (w-params.cons N PTy F) Cons Nil RevPs Out :- !, std.do! [
(@pi-decl N PTy p\ w-params.fold.params (F p) Cons Nil [p|RevPs] (Body p)),
Cons N PTy Body Out].
w-params.fold.params (w-params.nil NT TTy F) _ Nil RevParams Out :- !,
std.rev RevParams Params, !, Nil Params NT TTy F Out.
% [w-params.then AwP Cons Nil Out] states that Out has shape
% Cons `x_1` T_1 p_1 \ .. \ Nil [p_1 .. p_n] `T` Ty t \ Body
% where Pred [p_1 .. p_n] T Body
% and AwP = w-params.cons `x_1` T_1 p_1 \ ... \ w-params.nil `T` Ty F
pred w-params.then i:w-params A,
i:(name -> term -> (term -> C) -> C -> prop),
i:(name -> term -> (term -> B) -> C -> prop),
i:(list term -> term -> A -> B -> prop),
o:C.
w-params.then L Cons Nil Pred Out :-
w-params.fold L Cons (ps\ n\ ty\ f\ out\ sigma Body\
(@pi-decl n ty t\ Pred ps t (f t) (Body t)),
Nil n ty Body out) Out.
pred w-params.map i:w-params A, i:(list term -> term -> A -> B -> prop), o:w-params B.
w-params.map AL F BL :- w-params.then AL (mk3 w-params.cons) (mk3 w-params.nil) F BL.
% on the fly abstraction
pred bind-nil i:name, i:term, i:term, i:A, o:w-params A.
bind-nil N T X V (w-params.nil N T A) :- V = A X.
pred bind-cons i:name, i:term, i:term, i:w-params A, o:w-params A.
bind-cons N T X V (w-params.cons N T A) :- V = A X.
% Specific to list-w-params
pred list-w-params_list i:list-w-params A, o:list A.
list-w-params_list AwP R :- w-params.then AwP ignore ignore
(p\ t\ x\ std.map x triple_1) R.
pred list-w-params.append i:list-w-params A, i:list-w-params A, o:list-w-params A.
list-w-params.append (w-params.nil N T ML1) (w-params.nil N T ML2) (w-params.nil N T ML) :-
pi x\ std.append (ML1 x) (ML2 x) (ML x).
list-w-params.append (w-params.cons N Ty ML1) (w-params.cons N Ty ML2) (w-params.cons N Ty ML) :-
pi x\ list-w-params.append (ML1 x) (ML2 x) (ML x).
pred list-w-params.flatten-map
i:list-w-params A,
i:(A -> list-w-params B -> prop),
o:list-w-params B.
list-w-params.flatten-map (w-params.cons N T L) F (w-params.cons N T L1) :-
@pi-decl N T p\
list-w-params.flatten-map (L p) F (L1 p).
list-w-params.flatten-map (w-params.nil N TTy L) F (w-params.nil N TTy L1) :-
@pi-decl N TTy t\
list-w-params.flatten-map.aux (L t) F (L1 t).
pred list-w-params.flatten-map.aux
i:list (w-args A), i:(A -> list-w-params B -> prop), o:list (w-args B).
list-w-params.flatten-map.aux [] _ [].
list-w-params.flatten-map.aux [triple M Ps T|L] F Res1 :-
F M MwP,
apply-w-params MwP Ps T ML,
list-w-params.flatten-map.aux L F Res,
std.append ML Res Res1.
% [build-list-w-params TheParams TheType Factorties ListWParams]
% Params is a list of pairs (section variable, its type).
% ListWParams has as many w-params.cons as TheParams and the terms
% in Factories are abstracted wrt the first component of TheParams.
pred build-list-w-params i:list (pair term term), i:term, i:list (w-args A), o: list-w-params A.
build-list-w-params [pr P Pty|PS] TheType Factories (w-params.cons `p` Pty1 R) :- std.do! [
copy Pty Pty1,
(pi p\ (copy P p :- !) => build-list-w-params PS TheType Factories (R p)),
].
build-list-w-params [] TheType Factories (w-params.nil `t` TT R) :- std.do! [
std.assert-ok! (coq.typecheck TheType TT) "BUG: TheType does not typecheck",
(pi t\ (copy TheType t :- !) =>
std.map Factories (copy-triple (=) (copy-list copy) copy) (R t)),
].
pred distribute-w-params i:list-w-params A, o:list (one-w-params A).
distribute-w-params (w-params.cons N T F) L :-
pi x\ distribute-w-params (F x) (L1 x), std.map (L1 x) (bind-cons N T x) L.
distribute-w-params (w-params.nil N T F) L :-
pi x\ std.map (F x) (bind-nil N T x) L.
% Specific to one-w-params
pred w-params_1 i:one-w-params A, o:A.
w-params_1 X Y :- w-params.then X ignore ignore (p\ t\ triple_1) Y.
pred hb-set-implicit i:gref, i:list implicit_kind.
hb-set-implicit GR I :- std.do! [
coq.arguments.set-implicit GR [I],
log-vernac (coq.vernac.implicit {coq.gref->id GR} I),
].
pred hb-add-const i:id, i:term, i:term, i:opaque?, o:constant.
hb-add-const Name Bo Ty Opaque C :- std.do! [
coq.env.add-const Name Bo Ty Opaque C,
if (var Ty) (Ty? = none) (Ty? = some Ty),
log-vernac (coq.vernac.definition Name Ty? Bo),
hb-set-implicit (const C) [],
].
pred hb-add-variable i:id, i:term, o:constant.
hb-add-variable Name Ty C :- std.do! [
if (Name = "_") (ID is "fresh_name_" ^ {std.any->string {new_int}}) (ID = Name),
coq.env.add-section-variable ID Ty C,
log-vernac (coq.vernac.variable ID Ty),
hb-set-implicit (const C) [],
].
pred hb-add-indt i:indt-decl, o:inductive.
hb-add-indt Decl I :- std.do! [
coq.env.add-indt Decl I,
log-vernac (coq.vernac.inductive Decl),
].
pred hb-begin-module i:id.
hb-begin-module Name :- std.do! [
coq.env.begin-module Name none,
log-vernac (coq.vernac.begin-module Name none),
].
pred hb-end-module i:id, o:modpath.
hb-end-module Name M :- std.do! [
coq.env.end-module M,
log-vernac (coq.vernac.end-module Name),
].
pred hb-begin-section i:id.
hb-begin-section Name :- std.do! [
coq.env.begin-section Name,
log-vernac (coq.vernac.begin-section Name),
].
pred hb-end-section i:id.
hb-end-section Name :- std.do! [
coq.env.end-section,
log-vernac (coq.vernac.end-section Name),
].
pred hb-declare-instance i:gref.
hb-declare-instance GR :- std.do! [
coq.CS.declare-instance GR,
coq.gref->id GR Name,
log-vernac (coq.vernac.canonical Name),
].
pred hb-add-abbreviation i:id, i:int, i:term, i:bool, o:abbreviation.
hb-add-abbreviation Name NArgs Body OnlyParsing O :- std.do! [
coq.notation.add-abbreviation Name NArgs Body OnlyParsing O,
log-vernac (coq.vernac.abbreviation Name NArgs Body),
].
pred hb-export-module i:modpath.
hb-export-module M :- std.do! [
coq.env.export-module M,
coq.modpath->path M MP,
std.take-last 2 MP MPNice,
log-vernac (coq.vernac.export-module {std.string.concat "." MPNice}),
].
pred hb-coercion-declare i:coercion.
hb-coercion-declare C :- std.do! [
coq.coercion.declare C,
C = coercion GR _ SRCGR TGTCL,
coq.gref->id GR Name,
log-vernac (coq.vernac.coercion Name SRCGR TGTCL),
].
%%%%%%%%% HB database %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Specialize coq.elpi.accumulate to "hiearchy.db"
pred hb-accumulate i:scope, i:clause.
hb-accumulate S CL :- std.do! [
coq.elpi.accumulate S "hb.db" CL,
if-verbose (log-vernac (coq.vernac.comment CL)),
].
pred from_mixin i:prop, o:mixinname.
from_mixin (from _ X _) X.
pred from_builder i:prop, o:term.
from_builder (from _ _ X) (global X).
pred mixin-src_mixin i:prop, o:mixinname.
mixin-src_mixin (mixin-src _ M _) M.
pred mixin-src_src i:prop, o:term.
mixin-src_src (mixin-src _ _ S) S.
pred class_name i:class, o:classname.
class_name (class N _ _) N.
pred class-def_name i:prop, o:classname.
class-def_name (class-def (class N _ _)) N.
pred classname->def i:classname, o:class.
classname->def CN (class CN S ML) :- class-def (class CN S ML), !.
pred extract-builder i:prop, o:builder.
extract-builder (builder-decl B) B.
pred leq-builder i:builder, i:builder.
leq-builder (builder N _ _ _) (builder M _ _ _) :- N =< M.
% [factory-alias->gref X GR] when X is already a factory X = GR
% however, when X is a phantom abbreviated gref, we find the underlying
% factory gref GR associated to it.
pred factory-alias->gref i:gref, o:gref.
factory-alias->gref PhGR GR :- phant-abbrev GR PhGR _, !.
factory-alias->gref GR GR :- phant-abbrev GR _ _, !.
pred sub-class? i:class, i:class.
sub-class? (class _ _ ML1P) (class _ _ ML2P) :-
list-w-params_list ML1P ML1,
list-w-params_list ML2P ML2,
std.forall ML2 (m2\ std.exists ML1 (m1\ m1 = m2)).
% TODO: maybe the right API is to have this
% pred factory-provides i:factoryname, i:list-w-params mixiname.
% one can use w-params.then now!
% [factory-provides F ML] computes the mixins ML generated by F
pred factory-provides i:factoryname, o:list-w-params mixinname.
factory-provides FactoryAlias MLwP :- std.do! [
factory-alias->gref FactoryAlias Factory,
factory-requires Factory RMLwP,
w-params.map RMLwP (factory-provides.base Factory) MLwP
].
pred factory-provides.base i:factoryname, i:list term, i: term,
i:list (w-args mixinname), o:list (w-args mixinname).
factory-provides.base Factory Params T _RMLwP MLwP :- std.do! [
std.findall (from Factory T_ F_) All,
std.map All from_mixin ML,
std.map All from_builder BL,
std.map2 BL ML (factory-provides.one Params T) MLwP,
].
pred factory-provides.one i:list term, i:term, i:term, i:mixinname, o:w-args mixinname.
factory-provides.one Params T B M (triple M PL T) :- std.do! [
std.assert-ok! (coq.typecheck B Ty) "Builder illtyped",
subst-prod [T] {subst-prod Params Ty} TyParams,
std.assert! (extract-conclusion-params TyParams PL) "The conclusion of a builder is a mixin whose parameters depend on other mixins",
].
pred extract-conclusion-params i:term, o:list term.
extract-conclusion-params (prod _ S T) R :- !,
@pi-decl _ S x\ extract-conclusion-params (T x) R.
extract-conclusion-params (app [global GR|Args]) R :- !,
factory-alias->gref GR Factory,
factory-nparams Factory NP,
std.take NP Args R.
extract-conclusion-params T R :- whd1 T T1, !, extract-conclusion-params T1 R.
% [factories-provide FL ML] computes the mixins ML generated by all F in FL
%
% cons tp p\ nil t\ [pr f1 [p,t]]
% f1 p t = m1 t, m2 p t
% cons tp p\ nil t\ [pr m1 [t], pr m2 [p,t]]
pred factories-provide i:list-w-params factoryname, o:list-w-params mixinname.
factories-provide FLwP MLwP :-
list-w-params.flatten-map FLwP factory-provides UnsortedMLwP,
w-params.map UnsortedMLwP (p\t\ toposort-mixins) MLwP.
% Mixins can be topologically sorted according to their dependencies
pred toposort-mixins.mk-mixin-edge i:prop, o:list (pair mixinname mixinname).
toposort-mixins.mk-mixin-edge (factory-requires M Deps) L :-
std.map {list-w-params_list Deps} (d\r\ r = pr d M) L.
pred toposort-mixins i:list (w-args mixinname), o:list (w-args mixinname).
toposort-mixins In Out :- std.do! [
std.findall (factory-requires M_ Deps_) AllMixins,
std.flatten {std.map AllMixins toposort-mixins.mk-mixin-edge} ES,
toposort-proj triple_1 ES In Out,
].
pred toposort-proj i:(A -> B -> prop), i:list (pair B B), i:list A, o:list A.
toposort-proj Proj ES In Out :- !, toposort-proj.hb-accumulate Proj ES [] In Out.
pred topo-find i:B, o:A.
pred toposort-proj.hb-accumulate i:(A -> B -> prop), i:list (pair B B), i:list B, i:list A, o:list A.
toposort-proj.hb-accumulate _ ES Acc [] Out :- !,
std.map {toposort ES Acc} topo-find Out.
toposort-proj.hb-accumulate Proj ES Acc [A|In] Out :- std.do![
Proj A B,
topo-find B A => toposort-proj.hb-accumulate Proj ES [B|Acc] In Out
].
% Classes can be topologically sorted according to the subclass relation
pred toposort-classes.mk-class-edge i:prop, o:pair classname classname.
toposort-classes.mk-class-edge (sub-class C1 C2) (pr C2 C1).
pred toposort-classes i:list classname, o:list classname.
toposort-classes In Out :- std.do! [
std.findall (sub-class C1_ C2_) SubClasses,
std.map SubClasses toposort-classes.mk-class-edge ES,
toposort ES In Out,
].
pred findall-classes o:list class.
findall-classes CLSortedDef :- std.do! [
std.findall (class-def C_) All,
std.map All class-def_name CL,
toposort-classes CL CLSorted,
std.map CLSorted classname->def CLSortedDef,
].
pred findall-builders o:list builder.
findall-builders LFIL :-
std.map {std.findall (builder-decl B_)} extract-builder LFILunsorted,
bubblesort LFILunsorted leq-builder LFIL.
% [distinct-pairs-below C AllSuper C1 C2] finds C1 and C2 in
% AllSuper (all super classes of C) such that C1 != C2
% and for which there is no join C3.
% If there exists a join C3 of C1 and C2 then C is a subclass
% of C3 (otherwise C should have been declared before C3)
%
% / --- /-- C1
% C -- no C3 !=
% \ --- \-- C2
%
% [findall-newjoins C AllSuper] finds all C1 and C2 such that C is a (new) join for
% them
pred distinct-pairs-below i:class, i:list class, o:class, o:class.
distinct-pairs-below CurrentClass AllSuper C1 C2 :-
std.mem AllSuper C1, std.mem AllSuper C2,
% no cut until here, since we don't know which C1 and C2 to pick
std.do! [
cmp_term C1 C2 lt,
C1 = class C1n _ _,
C2 = class C2n _ _ ,
not(sub-class? C1 C2),
not(sub-class? C2 C1),
if (join C1n C2n C3n)
(assert-building-bottom-up CurrentClass C3n, fail) % a join, not a valid pair
true, % no join, valid pair
].
pred assert-building-bottom-up i:class, i:classname.
assert-building-bottom-up CurrentClass C3n :-
class-def (class C3n X Y),
CurrentClass = class CC _ _,
if (not (sub-class? CurrentClass (class C3n X Y)))
(coq.error "You must declare the current class" CC "before" C3n)
true.
pred distinct-pairs_pair i:prop, o:pair class class.
distinct-pairs_pair (distinct-pairs-below _ _ X Y) (pr X Y).
pred findall-newjoins i:class, i:list class, o:list (pair class class).
findall-newjoins CurrentClass AllSuper TodoJoins :-
std.findall (distinct-pairs-below CurrentClass AllSuper C1_ C2_) JoinOf,
std.map JoinOf distinct-pairs_pair TodoJoins.
%%%%% Coq Database %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [get-structure-coercion S1 S2 F] finds the coecion F from the structure S1 to S2
pred get-structure-coercion i:structure, i:structure, o:term.
get-structure-coercion S T (global F) :-
coq.coercion.db-for (grefclass S) (grefclass T) L,
if (L = [pr F _]) true (coq.error "No one step coercion from" S "to" T).
pred get-structure-sort-projection i:structure, o:term.
get-structure-sort-projection (indt S) Proj :- !,
coq.CS.canonical-projections S L,
if (L = [some P, _]) true (coq.error "No canonical sort projection for" S),
Proj = global (const P).
get-structure-sort-projection S _ :- coq.error "get-structure-sort-projection: not a structure" S.
pred get-structure-class-projection i:structure, o:term.
get-structure-class-projection (indt S) T :- !,
coq.CS.canonical-projections S L,
if (L = [_, some P]) true (coq.error "No canonical class projection for" S),
T = global (const P).
get-structure-class-projection S _ :- coq.error "get-structure-class-projection: not a structure" S.
pred get-constructor i:gref, o:gref.
get-constructor (indt R) (indc K) :- !,
if (coq.env.indt R _ _ _ _ [K] _) true (coq.error "Not a record" R).
get-constructor I _ :- coq.error "get-constructor: not an inductive" I.
pred head-gref-under-prods i:term, o:gref.
head-gref-under-prods (prod N T Body) Hd :-
@pi-decl N T x\ head-gref-under-prods (Body x) Hd.
head-gref-under-prods T Hd :- whd1 T T', head-gref-under-prods T' Hd.
head-gref-under-prods T Hd :- safe-dest-app T (global Hd) _.
%% finding for locally defined structures
pred get-cs-structure i:cs-instance, o:structure.
get-cs-structure (cs-instance _ _ (global Inst)) Struct :- std.do! [
coq.env.typeof Inst InstTy,
head-gref-under-prods InstTy Struct
].
pred has-cs-instance i:gref, i:cs-instance.
has-cs-instance GTy (cs-instance _ (cs-gref GTy) _).
pred get-local-structures i:term, o:list structure.
get-local-structures TyTrm StructL :- std.do! [
std.filter {coq.CS.db} (has-cs-instance {term->gref TyTrm}) DBGTyL,
std.map DBGTyL get-cs-structure RecL,
std.filter RecL is-structure StructL
].
pred local-cs? i:term, i:structure.
local-cs? TyTerm Struct :-
get-local-structures TyTerm StructL,
std.mem! StructL Struct.
pred structure-nparams i:structure, o:int.
structure-nparams Structure NParams :-
class-def (class Class Structure _),
factory-nparams Class NParams.
pred get-canonical-mixins-of i:term, i:structure, o:list prop.
get-canonical-mixins-of T S MSL :- std.do! [
get-structure-sort-projection S Sort,
structure-nparams S NParams,
coq.mk-n-holes NParams Holes,
coq.mk-app Sort {std.append Holes [ST]} SortHolesST,
if (coq.unify-eq T SortHolesST ok) (
% Hum, this unification problem is not super trivial. TODO replace by something simpler
get-constructor S KS,
coq.mk-app (global KS) {std.append Holes [T, C]} KSHolesC,
std.assert-ok! (coq.unify-eq ST KSHolesC) "HB: get-canonical-mixins-of: ST = _ _ C",
C = app Stuff,
std.drop {calc (NParams + 2)} Stuff MIL,
std.map MIL (mixin-srcs T) MSLL,
std.flatten MSLL MSL
)
(MSL = [])
].
pred under-canonical-mixins-of.do! i:term, i:list prop.
under-canonical-mixins-of.do! T P :-
get-local-structures T CS,
std.map CS (get-canonical-mixins-of T) MSLL,
std.flatten MSLL MSL,
MSL => std.do! P.
%%%%% mterm %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% mterm is always of the form [mterm P T ML F], which is the data of
% parameters PL a type T, and a list of mixins ML and a term F
% where F should applied to PL, T and instances of the mixins in ML
kind mterm type.
type mterm list term -> term -> list mixinname -> term -> mterm.
% Notations /à la/ *pack* are always of the shape
% [Notation N x_0 .. x_n := C x_0 .. _ _ id .. x_i .. _ id _ _ id]
% with a variable number of [_] between each [id], and where
% - [x_i] is given by the user
% - [_] correspond to arguments that are left implicit,
% - [id] trigger unification as described in
% /Canonical Structures for the working Coq user/ by Mahboubi and Tassi
%
% phant-arg encode these three kind of arguments
% - [x_i] is encoded using [real-arg x_i]
% - [_] using [implicit-arg]
% - [id] using [unify-arg]
kind phant-arg type.
type real-arg name -> phant-arg.
type infer-type name -> phant-arg.
type implicit-arg phant-arg.
type unify-arg phant-arg.
% phant-term is a pair of a list of argument kinds together with a term
kind phant-term type.
type phant-term list phant-arg -> term -> phant-term.
pred phant-fun i:phant-arg, i:term, i:(term -> phant-term), o:phant-term.
phant-fun Arg Ty PhF (phant-term [Arg|ArgL] (fun N Ty F)) :-
if (Arg = real-arg N) true (N = `_`),
@pi-decl N Ty x\ PhF x = phant-term ArgL (F x).
pred phant-fun-real i:name, i:term, i:(term -> phant-term), o:phant-term.
phant-fun-real N T F Res :- !, phant-fun (real-arg N) T F Res.
% [phant-fun-unify N X1 X2 PF PUF] states that PUF is a phant-term that
% is starts with unifing X1 and X2 and then outputs PF.
% N is ignored
pred phant-fun-unify i:term, i:term, i:term, i:phant-term, o:phant-term.
phant-fun-unify Msg X1 X2 (phant-term AL F) (phant-term [unify-arg|AL] UF) :-
std.assert-ok! (coq.typecheck X1 T1) "phant-fun-unify: X1 illtyped",
std.assert-ok! (coq.typecheck X2 T2) "phant-fun-unify: X2 illtyped",
UF = {{fun unif_arbitrary : lib:hb.unify lp:T1 lp:T2 lp:X1 lp:X2 lp:Msg => lp:F}}.
% [phant-fun-implicit N Ty PF PUF] states that PUF is a phant-term
% which quantifies [PF x] over [x : Ty] (with name N)
pred phant-fun-implicit i:name, i:term, i:(term -> phant-term), o:phant-term.
phant-fun-implicit N Ty PF (phant-term [implicit-arg|AL] (fun N Ty F)) :- !,
@pi-decl N Ty t\ PF t = phant-term AL (F t).
pred phant-fun-unify-mixin i:term, i:name, i:term, i:(term -> phant-term), o:phant-term.
phant-fun-unify-mixin T N Ty PF Out :- !, std.do! [
safe-dest-app Ty (global M) _,
Msg is "phant-fun-unify-mixin: No mixin-src on " ^ {coq.term->string T},
std.assert! (mixin-src T M Msrc) Msg,
(@pi-decl `m` Ty m\ phant-fun-unify {{lib:hb.nomsg}} m Msrc (PF m) (PFM m)),
phant-fun-implicit N Ty PFM Out
].
% [phant-fun-struct T S Params PF PSF] states that PSF is a phant-term
% which postulate a structure [s : S Params] such that [T = sort s]
% and then outputs [PF s]
pred phant-fun-struct i:term, i:name, i:structure, i:list term, i:(term -> phant-term), o:phant-term.
phant-fun-struct T Name S Params PF Out :- std.do! [
get-structure-sort-projection S SortProj,
mk-app (global S) Params SParams,
mk-app SortProj Params SortProjParams,
% Msg = {{lib:hb.nomsg}},
Msg = {{lib:hb.some (lib:hb.pair lib:hb.not_a_msg lp:SParams)}},
(@pi-decl Name SParams s\ phant-fun-unify Msg T {mk-app SortProjParams [s]} (PF s) (UnifSI s)),
phant-fun-implicit Name SParams UnifSI Out
].
% [builder->term Params T Src Tgt MF] provides a term which is
% a function to transform Src into Tgt under the right mixin-src.
pred builder->term i:list term, i:term, i:factoryname, i:mixinname, o:term.
builder->term Ps T Src Tgt B :- !, std.do! [
from Src Tgt FGR,
F = global FGR,
factory-requires Src MLwP,
list-w-params_list MLwP ML,
mterm->term (mterm Ps T ML F) B,
].
% [instantiate-mixin T F M_i TFX] where mixin-for T M_i X_i states that
% if F ~ fun xs (m_0 : M_0 T) .. (m_n : M_n T ..) ys
% => F xs m_0 .. m_{i-1} m_i m_{i+1} .. m_n ys
% then TFX := fun xs m_0 .. m_{i-1} m_{i+1} .. m_n ys
% => F xs m_0 .. m_{i-1} X_i m_{i+1} .. m_n ys
% thus instanciating an abstraction on mixin M_i with X_i