Skip to content

Commit 07b7a49

Browse files
committed
clanup
1 parent bf926b3 commit 07b7a49

File tree

2 files changed

+80
-76
lines changed

2 files changed

+80
-76
lines changed

hb.elpi

Lines changed: 76 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,6 @@ gref->modname GR ModName :-
204204
coq.gref->path GR Path,
205205
if (std.rev Path [_,ModName|_]) true (coq.error "No enclosing module for " GR).
206206

207-
pred term->modname i:structure, o:id.
208-
term->modname T ModName :- gref->modname {term->gref T} ModName.
209-
210207
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211208
% function to predicate generic constructions %
212209
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -375,6 +372,14 @@ pred mixin-src_src i:prop, o:term.
375372
mixin-src_src (mixin-src _ _ S) S.
376373
mixin-src_src (pi c\ mixin-src _ _ c :- F c) T :- F T. % this is a bit pulp...
377374

375+
pred class_name i:class, o:classname.
376+
class_name (class N _ _) N.
377+
378+
pred class-def_name i:prop, o:classname.
379+
class-def_name (class-def (class N _ _)) N.
380+
381+
pred classname->def i:classname, o:class.
382+
classname->def CN (class CN S ML) :- class-def (class CN S ML), !.
378383

379384
pred extract-builder i:prop, o:builder.
380385
extract-builder (builder-decl B) B.
@@ -466,20 +471,21 @@ toposort-proj.acc Proj ES Acc [A|In] Out :- std.do![
466471
].
467472

468473
% Classes can be topologically sorted according to the subclass relation
469-
pred toposort-classes.mk-class-edge i:prop, o:pair class class.
474+
pred toposort-classes.mk-class-edge i:prop, o:pair classname classname.
470475
toposort-classes.mk-class-edge (sub-class C1 C2) (pr C2 C1).
471-
pred toposort-classes i:list class, o:list class.
476+
pred toposort-classes i:list classname, o:list classname.
472477
toposort-classes In Out :- std.do! [
473478
std.findall (sub-class C1_ C2_) SubClasses,
474479
std.map SubClasses toposort-classes.mk-class-edge ES,
475480
toposort ES In Out,
476481
].
477482

478483
pred findall-classes o:list class.
479-
findall-classes CLSorted :- std.do! [
484+
findall-classes CLSortedDef :- std.do! [
480485
std.findall (class-def C_) All,
481-
std.map All (x\r\ x = class-def r) CL,
482-
toposort-classes CL CLSorted
486+
std.map All class-def_name CL,
487+
toposort-classes CL CLSorted,
488+
std.map CLSorted classname->def CLSortedDef,
483489
].
484490

485491
pred findall-builders o:list builder.
@@ -534,55 +540,54 @@ findall-newjoins CurrentClass AllSuper TodoJoins :-
534540

535541
% [get-structure-coercion S1 S2 F] finds the coecion F from the structure S1 to S2
536542
pred get-structure-coercion i:structure, i:structure, o:term.
537-
get-structure-coercion (global S _) (global T _) FU :-
543+
get-structure-coercion S T FU :-
538544
coq.coercion.db-for (grefclass S) (grefclass T) L,
539545
if (L = [pr F _]) true (coq.error "No one step coercion from" S "to" T),
540546
coq.env.global F FU.
541547

542-
% TODO
543548
pred get-structure-sort-projection i:structure, o:term.
544-
get-structure-sort-projection T Proj :-
545-
safe-dest-app T (global (indt S) _) Params,
549+
get-structure-sort-projection (indt S) Proj :- !,
546550
coq.CS.canonical-projections S L,
547551
if (L = [some P, _]) true (coq.error "No canonical sort projection for" S),
548-
mk-app {coq.env.global (const P)} Params Proj.
552+
coq.env.global (const P) Proj.
553+
get-structure-sort-projection S _ :- coq.error "get-structure-sort-projection: not a structure" S.
549554

550555
pred get-structure-class-projection i:structure, o:term.
551-
get-structure-class-projection (global (indt S) _) T :-
556+
get-structure-class-projection (indt S) T :- !,
552557
coq.CS.canonical-projections S L,
553558
if (L = [_, some P]) true (coq.error "No canonical class projection for" S),
554559
coq.env.global (const P) T.
560+
get-structure-class-projection S _ :- coq.error "get-structure-class-projection: not a structure" S.
555561

556-
pred get-constructor i:term, o:gref.
557-
get-constructor (global (indt R) UI as S) (indc K) :- !,
558-
if (coq.env.indt R UI _ _ _ _ [K] _) true (coq.error "Not a record" S).
562+
pred get-constructor i:gref, o:gref.
563+
get-constructor (indt R) (indc K) :- !,
564+
if (coq.env.indt R _ _ _ _ _ [K] _) true (coq.error "Not a record" R).
565+
get-constructor I _ :- coq.error "get-constructor: not an inductive" I.
559566

560-
pred safe-head i:term, o:term.
561-
safe-head (prod N T Body) Hd :-
562-
@pi-decl N T x\
563-
safe-head (Body x) (Hd' x),
564-
std.assert! (Hd' x = Hd) "safe-head: the head symbol is a bound variable".
565-
safe-head T Hd :- whd1 T T', safe-head T' Hd.
566-
safe-head T Hd :- safe-dest-app T Hd _.
567+
pred head-gref-under-prods i:term, o:gref.
568+
head-gref-under-prods (prod N T Body) Hd :-
569+
@pi-decl N T x\ head-gref-under-prods (Body x) Hd.
570+
head-gref-under-prods T Hd :- whd1 T T', head-gref-under-prods T' Hd.
571+
head-gref-under-prods T Hd :- safe-dest-app T (global Hd _) _.
567572

568573
%% finding for locally defined structures
569-
pred get-cs-structure i:cs-instance, o:term.
570-
get-cs-structure (cs-instance _ _ (global Inst UI)) Struct :- std.do! [
571-
coq.env.typeof Inst UI InstTy,
572-
safe-head InstTy Struct
574+
pred get-cs-structure i:cs-instance, o:structure.
575+
get-cs-structure (cs-instance _ _ Inst) Struct :- std.do! [
576+
coq.env.typeof Inst _ InstTy,
577+
head-gref-under-prods InstTy Struct
573578
].
574579

575580
pred has-cs-instance i:gref, i:cs-instance.
576581
has-cs-instance GTy (cs-instance _ (cs-gref GTy) _).
577582

578-
pred get-local-structures i:term, o:list term.
583+
pred get-local-structures i:term, o:list structure.
579584
get-local-structures TyTrm StructL :- std.do! [
580585
std.filter {coq.CS.db} (has-cs-instance {term->gref TyTrm}) DBGTyL,
581586
std.map DBGTyL get-cs-structure RecL,
582587
std.filter RecL is-structure StructL
583588
].
584589

585-
pred local-cs? i:term, i:term.
590+
pred local-cs? i:term, i:structure.
586591
local-cs? TyTerm Struct :-
587592
get-local-structures TyTerm StructL,
588593
std.mem! StructL Struct.
@@ -679,16 +684,17 @@ phant-fun-unify-mixin T N Ty PF Out :- !, std.do! [
679684
phant-fun-implicit N Ty PFM Out
680685
].
681686

682-
% [phant-fun-struct T SI PF PSF] states that PSF is a phant-term
687+
% [phant-fun-struct T SI SIParams PF PSF] states that PSF is a phant-term
683688
% which postulate a structure [s : SI] such that [T = sort s]
684689
% and then outputs [PF s]
685-
pred phant-fun-struct i:term, i:name, i:term, i:(term -> phant-term), o:phant-term.
686-
phant-fun-struct T Name SI PF Out :- std.do! [
690+
pred phant-fun-struct i:term, i:name, i:structure, i:list term, i:(term -> phant-term), o:phant-term.
691+
phant-fun-struct T Name SI Params PF Out :- std.do! [
687692
get-structure-sort-projection SI Sort,
693+
mk-app {coq.env.global SI} Params SITerm,
688694
% Msg = {{lib:hb.nomsg}},
689-
Msg = {{lib:hb.some (lib:hb.pair "is not canonically a"%string lp:SI)}},
690-
(@pi-decl Name SI s\ phant-fun-unify Msg T {mk-app Sort [s]} (PF s) (UnifSI s)),
691-
phant-fun-implicit Name SI UnifSI Out
695+
Msg = {{lib:hb.some (lib:hb.pair "is not canonically a"%string lp:SITerm)}},
696+
(@pi-decl Name SITerm s\ phant-fun-unify Msg T {mk-app Sort [s]} (PF s) (UnifSI s)),
697+
phant-fun-implicit Name SITerm UnifSI Out
692698
].
693699

694700
% [builder->term Params T Src Tgt MF] provides a term which is
@@ -977,7 +983,7 @@ pred mk-phant-term.mixins i:term, i:classname, i:phant-term,
977983
i:list term, i:name, i:term, i:(term -> list (w-args mixinname)), o:phant-term.
978984
mk-phant-term.mixins T CN PF Params N Ty MLwA Out :- std.do! [
979985
class-def (class CN SI _),
980-
mk-app SI Params SIParams,
986+
mk-app {coq.env.global SI} Params SIParams,
981987
NoMsg = {{lib:hb.nomsg}},
982988
coq.name-suffix N "local" Nlocal,
983989
(@pi-decl Nlocal Ty t\ sigma SK KC ML\ std.do! [
@@ -989,12 +995,12 @@ mk-phant-term.mixins T CN PF Params N Ty MLwA Out :- std.do! [
989995
under-mixins.then (MLwA t) (phant-fun-unify-mixin T) (mk-phant-term.mixins.aux t Params c CN PF) PF2,
990996
phant-fun-unify NoMsg s {mk-app SKPT [c]} PF2 (PFU t s c)])
991997
]),
992-
Out = {phant-fun-struct T `s` SIParams s\
998+
Out = {phant-fun-struct T `s` SI Params s\
993999
{phant-fun-implicit `c` ClassTy (PFU T s)}}
9941000
].
9951001

9961002
mk-phant-term.mixins.aux T Params C CN PF X :- std.do![
997-
get-constructor {coq.env.global CN} KC,
1003+
get-constructor CN KC,
9981004
mgref->term Params T KC KCM,
9991005
phant-fun-unify {{lib:hb.nomsg}} KCM C PF X,
10001006
].
@@ -1100,24 +1106,24 @@ params->holes (w-params.cons _ _ F) [_|PS] :- pi x\ params->holes (F x) PS.
11001106
pred declare-instances i:term, i:list class.
11011107
declare-instances T [class Class Struct MLwP|Rest] :-
11021108
params->holes MLwP Params,
1103-
get-constructor {coq.env.global Class} KC,
1109+
get-constructor Class KC,
11041110

11051111
if (not(local-cs? T Struct))
11061112
true % we build it
11071113
(if-verbose (coq.say "HB: skipping alreay existing"
1108-
{coq.term->string Struct} "instance on"
1114+
{nice-gref->string Struct} "instance on"
11091115
{coq.term->string T}),
11101116
fail),
11111117

11121118
if (mgref->term Params T KC KCApp)
1113-
(if-verbose (coq.say "HB: we can build a" {coq.term->string Struct} "on"
1119+
(if-verbose (coq.say "HB: we can build a" {nice-gref->string Struct} "on"
11141120
{coq.term->string T}))
11151121
fail,
11161122

11171123
!,
11181124
term->gref T TGR,
11191125
coq.gref->id TGR TID,
1120-
Name is TID ^ "_is_a_" ^ {term->modname Struct},
1126+
Name is TID ^ "_is_a_" ^ {gref->modname Struct},
11211127

11221128
if-verbose (coq.say "HB: declare canonical structure instance" Name),
11231129

@@ -1261,10 +1267,10 @@ clean-op-ty [exported-op _ Po C|Ops] S T1 T2 :-
12611267

12621268
clean-op-ty Ops S T1 T2.
12631269

1264-
pred operation-body-and-ty i:list prop, i:constant, i:term, i:term, i:term,
1270+
pred operation-body-and-ty i:list prop, i:constant, i:structure, i:term, i:term,
12651271
i:list term, i:term, i:w-args A, o:pair term term.
12661272
operation-body-and-ty EXI Poperation Struct Psort Pclass Params _T (triple _ Params _) (pr Bo Ty) :- std.do! [
1267-
mk-app Struct Params StructType,
1273+
mk-app {coq.env.global Struct} Params StructType,
12681274
mk-app Psort Params PsortP,
12691275
mk-app Pclass Params PclassP,
12701276
Bo = fun `s` StructType Body,
@@ -1285,7 +1291,7 @@ operation-body-and-ty EXI Poperation Struct Psort Pclass Params _T (triple _ Par
12851291
% same operation out of the package structure (out of the class field of the
12861292
% structure). We also provide all the other mixin dependencies (other misins)
12871293
% of the package structure.
1288-
pred export-1-operation i:mixinname, i:term, i:term, i:term, i:one-w-params mixinname, i:option constant, i:list prop, o:list prop.
1294+
pred export-1-operation i:mixinname, i:structure, i:term, i:term, i:one-w-params mixinname, i:option constant, i:list prop, o:list prop.
12891295
export-1-operation _ _ _ _ _ none EX EX :- !. % not a projection, no operation
12901296
export-1-operation M Struct Psort Pclass MwP (some Poperation) EXI EXO :- !, std.do! [
12911297
coq.gref->id (const Poperation) Name,
@@ -1304,7 +1310,7 @@ export-1-operation M Struct Psort Pclass MwP (some Poperation) EXI EXO :- !, std
13041310
].
13051311

13061312
% Given a list of mixins, it exports all operations in there
1307-
pred export-operations.aux i:term, i:term, i:term, i:one-w-params mixinname, i:list prop, o:list prop.
1313+
pred export-operations.aux i:structure, i:term, i:term, i:one-w-params mixinname, i:list prop, o:list prop.
13081314
export-operations.aux Struct ProjSort ProjClass MwP EX1 EX2 :- !, std.do! [
13091315
w-params_1 MwP (indt M),
13101316
coq.CS.canonical-projections M Poperations,
@@ -1315,7 +1321,7 @@ pred mixin-not-already-declared i:one-w-params mixinname.
13151321
mixin-not-already-declared MwP :-
13161322
w-params_1 MwP M, not(mixin-first-class M _), M = indt _.
13171323

1318-
pred export-operations i:term, i:term, i:term, i:list-w-params mixinname, i:list prop, o:list prop, o:list mixinname.
1324+
pred export-operations i:structure, i:term, i:term, i:list-w-params mixinname, i:list prop, o:list prop, o:list mixinname.
13191325
export-operations Structure ProjSort ProjClass MLwP EX1 EX2 MLToExport :- std.do! [
13201326
distribute-w-params MLwP LMwP,
13211327
std.filter LMwP mixin-not-already-declared LMwPToExport,
@@ -1337,7 +1343,7 @@ mk-coe-class-body FC TC TMLwP Params T _ CoeBody :- std.do! [
13371343
std.map TML (from FC) Builders,
13381344
std.map Builders (x\r\sigma t\ coq.env.global x t, mk-app t Params r) BuildersP,
13391345

1340-
mk-app {coq.env.global {get-constructor {coq.env.global TC}}}
1346+
mk-app {coq.env.global {get-constructor TC}}
13411347
{coq.mk-n-holes {factory-nparams TC}} KCHoles,
13421348

13431349
(pi c\ sigma Mixes\
@@ -1360,10 +1366,10 @@ pred mk-coe-structure-body
13601366
mk-coe-structure-body StructureF StructureT TC Coercion SortProjection ClassProjection
13611367
Params _T _ SCoeBody :- std.do! [
13621368

1363-
mk-app StructureF Params StructureP,
1364-
mk-app SortProjection Params SortP,
1365-
mk-app ClassProjection Params ClassP,
1366-
mk-app Coercion Params CoercionP,
1369+
mk-app {coq.env.global StructureF} Params StructureP,
1370+
mk-app SortProjection Params SortP,
1371+
mk-app ClassProjection Params ClassP,
1372+
mk-app Coercion Params CoercionP,
13671373

13681374
mk-app {coq.env.global {get-constructor StructureT}}
13691375
{coq.mk-n-holes {factory-nparams TC}} PackPH,
@@ -1377,12 +1383,12 @@ mk-coe-structure-body StructureF StructureT TC Coercion SortProjection ClassProj
13771383
% from C1 to C2 given P1 P2 the two projections from the structure of C1
13781384
pred declare-coercion i:term, i:term, i:class, i:class.
13791385
declare-coercion SortProjection ClassProjection
1380-
(class FC StructureF FMLwP as FCDef) (class TC StructureT TMLwP as TCDef) :- std.do! [
1386+
(class FC StructureF FMLwP) (class TC StructureT TMLwP) :- std.do! [
13811387

1382-
acc current (clause _ _ (sub-class FCDef TCDef)),
1388+
acc current (clause _ _ (sub-class FC TC)),
13831389

1384-
term->modname StructureF ModNameF,
1385-
term->modname StructureT ModNameT,
1390+
gref->modname StructureF ModNameF,
1391+
gref->modname StructureT ModNameT,
13861392
CName is ModNameF ^ "_class_to_" ^ ModNameT ^ "_class",
13871393
SName is ModNameF ^ "_to_" ^ ModNameT,
13881394

@@ -1408,15 +1414,15 @@ declare-coercion SortProjection ClassProjection
14081414
if-verbose (coq.say "HB: declare unification hint" SName),
14091415

14101416
hb-add-const SName SCoeBody STy @transparent! SC,
1411-
@global! => coq.coercion.declare (coercion (const SC) 0 {term->gref StructureF} (grefclass {term->gref StructureT})),
1417+
@global! => coq.coercion.declare (coercion (const SC) 0 StructureF (grefclass StructureT)),
14121418
coq.CS.declare-instance (const SC), % TODO: API in Elpi, take a @constant instead of gref
14131419
].
14141420

1415-
pred join-body i:int, i:int, i:term, i:term, i:term, i:term, i:term, i:term,
1421+
pred join-body i:int, i:int, i:structure, i:term, i:term, i:term, i:term, i:term,
14161422
i:list term, i:name, i:term, i:(term -> A), o:term.
14171423
join-body N1 N2 S3 S2_Pack S1_sort S3_to_S1 S2_class S3_to_S2
14181424
P N _Ty _F (fun N S3P Pack) :- !,
1419-
mk-app S3 P S3P, !,
1425+
mk-app {coq.env.global S3} P S3P, !,
14201426
coq.mk-n-holes N2 Holes2, !,
14211427
coq.mk-n-holes N1 Holes1, !,
14221428
@pi-decl N S3P s\
@@ -1430,8 +1436,8 @@ join-body N1 N2 S3 S2_Pack S1_sort S3_to_S1 S2_class S3_to_S2
14301436

14311437
pred declare-join i:class, i:pair class class, o:prop.
14321438
declare-join (class C3 S3 MLwP3) (pr (class C1 S1 _) (class C2 S2 _)) (join C1 C2 C3) :-
1433-
Name is "join_" ^ {term->modname S3} ^
1434-
"_between_" ^ {term->modname S1} ^ "_and_" ^ {term->modname S2},
1439+
Name is "join_" ^ {gref->modname S3} ^
1440+
"_between_" ^ {gref->modname S1} ^ "_and_" ^ {gref->modname S2},
14351441

14361442
get-structure-coercion S3 S2 S3_to_S2,
14371443
get-structure-coercion S3 S1 S3_to_S1,
@@ -1493,8 +1499,8 @@ mk-class-field ClassName Params T _ (field _ "class" (app [C|Args]) _\end-record
14931499

14941500
% Builds the axioms record and the factories from this class to each mixin
14951501
% TODO params
1496-
pred declare-class+structure i:list-w-params mixinname, o:factoryname, o:term, o:term, o:term, o:list prop.
1497-
declare-class+structure MLwP (indt ClassInd) Structure SortProjection ClassProjection AllFactories :- std.do! [
1502+
pred declare-class+structure i:list-w-params mixinname, o:factoryname, o:structure, o:term, o:term, o:list prop.
1503+
declare-class+structure MLwP (indt ClassInd) (indt StructureInd) SortProjection ClassProjection AllFactories :- std.do! [
14981504

14991505
if-verbose (coq.say "HB: declare axioms record"),
15001506

@@ -1517,17 +1523,16 @@ declare-class+structure MLwP (indt ClassInd) Structure SortProjection ClassProje
15171523
(mk-class-field (indt ClassInd)) StructureDeclaration,
15181524

15191525
std.assert-ok! (coq.typecheck-indt-decl StructureDeclaration) "declare-structure: illtyped",
1520-
coq.env.add-indt StructureDeclaration StructureName,
1526+
hb-add-indt StructureDeclaration StructureInd,
15211527

1522-
coq.CS.canonical-projections StructureName [some SortP, some ClassP],
1523-
coq.env.global (indt StructureName) Structure,
1528+
coq.CS.canonical-projections StructureInd [some SortP, some ClassP],
15241529
coq.env.global (const SortP) SortProjection,
15251530
coq.env.global (const ClassP) ClassProjection,
15261531
].
15271532

15281533
% Declares "sort" as a coercion Structurename >-> Sortclass
1529-
pred declare-sort-coercion i:term, i:term.
1530-
declare-sort-coercion (global StructureName _) (global Proj _) :-
1534+
pred declare-sort-coercion i:structure, i:term.
1535+
declare-sort-coercion StructureName (global Proj _) :-
15311536

15321537
if-verbose (coq.say "HB: declare sort coercion"),
15331538

@@ -1538,15 +1543,14 @@ if-class-already-exists-error _ [] _.
15381543
if-class-already-exists-error N [class _ S ML1wP|CS] ML2 :-
15391544
list-w-params_list ML1wP ML1,
15401545
if (list-eq-set ML1 ML2)
1541-
(coq.error "Structure" {coq.term->string S} "contains the same mixins of" N)
1546+
(coq.error "Structure" {nice-gref->string S} "contains the same mixins of" N)
15421547
(if-class-already-exists-error N CS ML2).
15431548

15441549
pred export-mixin-coercion i:classname, i:option constant.
15451550
export-mixin-coercion _ none.
15461551
export-mixin-coercion ClassName (some C) :-
15471552
coq.env.typeof (const C) _ CTy,
1548-
safe-dest-app {safe-head CTy} Mixin _,
1549-
coq.term->gref Mixin MixinGR,
1553+
head-gref-under-prods CTy MixinGR,
15501554
if-verbose (coq.say "HB: export class to mixin coercion for mixin" {nice-gref->string MixinGR}),
15511555
@global! =>
15521556
coq.coercion.declare (coercion (const C) _ ClassName (grefclass MixinGR)).

0 commit comments

Comments
 (0)