Skip to content

Commit 4c7c83d

Browse files
authored
Merge pull request #380 from proux01/perf_compress_coercion
A few performance improvements
2 parents a23b4fc + 9116e20 commit 4c7c83d

16 files changed

+109
-150
lines changed

HB/about.elpi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,10 @@ main-structure S Class Structure MLwP :-
192192
std.findall (exported-op m P O) OPS,
193193
std.map OPS (c\out\ sigma p\ c = exported-op m p out) r) OPLL,
194194
std.flatten OPLL Operations,
195-
std.map {std.findall (sub-class Class C_)} (x\r\ x = sub-class Class r) SubClasses,
196-
std.map {std.findall (sub-class C_ Class)} (x\r\ x = sub-class r Class) SuperClasses,
195+
std.map {std.findall (sub-class Class CS_ CoeS_ NS_)}
196+
(x\r\ x = sub-class Class r _ _) SubClasses,
197+
std.map {std.findall (sub-class Cs_ Class Coes_ Ns_)}
198+
(x\r\ x = sub-class r Class _ _) SuperClasses,
197199
% format
198200
PpOrigin = box (hov 4) [
199201
str "HB: ", str S, str " is a structure", spc,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pred acc-clauses i:scope, i:list prop.
2+
acc-clauses Scope CL :- std.forall CL (acc-clause Scope).
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pred acc-clauses i:scope, i:list prop.
2+
acc-clauses Scope CL :- coq.elpi.accumulate-clauses Scope "hb.db" {std.map CL (c\r\ r = clause _ _ c)}.

HB/common/database.elpi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ toposort-proj.acc Proj ES Acc [A|In] Out :- std.do![
155155

156156
% Classes can be topologically sorted according to the subclass relation
157157
pred toposort-classes.mk-class-edge i:prop, o:pair classname classname.
158-
toposort-classes.mk-class-edge (sub-class C1 C2) (pr C2 C1).
158+
toposort-classes.mk-class-edge (sub-class C1 C2 _ _) (pr C2 C1).
159159
pred toposort-classes i:list classname, o:list classname.
160160
toposort-classes In Out :- std.do! [
161-
std.findall (sub-class C1_ C2_) SubClasses,
161+
std.findall (sub-class C1_ C2_ _ _) SubClasses,
162162
std.map SubClasses toposort-classes.mk-class-edge ES,
163163
std.toposort ES In Out,
164164
].

HB/common/log.elpi

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,6 @@ log.coq.CS.declare-instance C :- std.do! [
181181
log.private.log-vernac (log.private.coq.vernac.canonical Name Local),
182182
].
183183

184-
% Since "accumulate" is a keyword we can't use it as a predicate name
185-
% in the namespace, so we just define it here with the full name
186-
pred log.coq.env.accumulate i:scope, i:string, i:clause.
187-
log.coq.env.accumulate S DB CL :- std.do! [
188-
coq.elpi.accumulate S DB CL,
189-
if-verbose (log.private.log-vernac (log.private.coq.vernac.comment CL)),
190-
].
191-
192184
pred log.coq.check i:term, o:term, o:term, o:diagnostic.
193185
log.coq.check Skel Ty T D :- std.do! [
194186
coq.elaborate-skeleton Skel Ty T D,

HB/common/synthesis.elpi

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,21 @@ mixin-for T M MICompressed :- mixin-src T M Tm, !, std.do! [
191191
compress-coercion-paths MI MICompressed,
192192
].
193193

194+
pred drop i:int, i:list A, o:list A.
195+
drop 0 L L :- !.
196+
drop N [_|XS] L :- !, N1 is N - 1, drop N1 XS L.
197+
194198
pred compress-copy o:term, o:term.
195-
compress-copy X Y :- compress X Y, !.
199+
compress-copy (app [global (const C) | L]) R :-
200+
sub-class C2 C3 C NparamsC,
201+
drop NparamsC L [app [global (const C') | L']],
202+
sub-class C1 C2 C' NparamsC',
203+
drop NparamsC' L' L'',
204+
sub-class C1 C3 C'' NparamsC'',
205+
std.append {coq.mk-n-holes NparamsC''} L'' HL'',
206+
CHL'' = app [global (const C'') | HL''],
207+
coq.typecheck CHL'' _ ok, !,
208+
compress-copy CHL'' R.
196209
compress-copy (app L) (app L1) :- !, std.map L compress-copy L1.
197210
compress-copy X X.
198211

HB/common/utils.elpi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ with-locality P :-
7474
pred acc-clause i:scope, i:prop.
7575
acc-clause Scope C :- coq.elpi.accumulate Scope "hb.db" (clause _ _ C).
7676

77+
/* Uncomment and remove HB/common/compat_acc_clauses_*.elpi once requiring coq-elpi >= 1.18.0,
78+
which implies Coq >= 8.17
7779
pred acc-clauses i:scope, i:list prop.
78-
acc-clauses Scope CL :- std.forall CL (acc-clause Scope).
80+
acc-clauses Scope CL :- coq.elpi.accumulate-clauses Scope "hb.db" {std.map CL (c\r\ r = clause _ _ c)}.
81+
*/
7982

8083
pred save-docstring.
8184
save-docstring :-

HB/context.elpi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ declare.mixins TheType TheParamsSection MLwPRaw MLwP MSL CL :- std.do! [
3939
std.map TheParamsSection triple_2 TheParams,
4040
apply-w-params MLwPRaw TheParams TheType MLwAllArgsRaw,
4141
std.fold MLwAllArgsRaw (triple [] [] []) (private.postulate-mixin TheType) (triple CL MSL MLwPRev),
42-
std.forall CL (cs\ acc-clause current (local-canonical cs)),
42+
acc-clauses current {std.map CL (cs\r\ r = local-canonical cs)},
4343
std.rev MLwPRev MLwPSection,
4444
build-list-w-params TheParamsSection TheType MLwPSection MLwP,
4545
acc-clauses current MSL,

HB/structure.elpi

Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -160,37 +160,14 @@ declare Module BSkel Sort :- std.do! [
160160

161161
@global! => log.coq.notation.add-abbreviation "on" 1
162162
{{fun T => (lp:{{ ClassOfAbbrev_ {{_}} }} : (lp:AppClassHoles T)) }} tt
163-
OnAbbrev,
164-
165-
log.coq.env.begin-module "EtaAndMixinExports" none,
166-
167-
if (get-option "primitive" tt) true (
168-
if-verbose (coq.say {header} "eta expanded instances"),
169-
NewClauses => std.do! [
170-
w-params.fold MLwP mk-fun
171-
(private.mk-hb-eta.on Structure SortProjection OnAbbrev) EtaInstanceBody,
172-
w-params.fold MLwP (mk-parameter explicit)
173-
(private.mk-hb-eta.arity Structure ClassName SortProjection)
174-
EtaInstanceArity,
175-
instance.declare-const "_" EtaInstanceBody EtaInstanceArity _
176-
]
177-
),
178-
179-
% std.flatten {std.map NewMixins mixin->factories} NewFactories,
180-
% NewClauses => std.forall NewFactories instance.declare-factory-sort-factory,
181-
182-
log.coq.env.end-module-name "EtaAndMixinExports" EtaExports,
163+
_OnAbbrev,
183164

184165
log.coq.env.end-module-name Module ModulePath,
185166

186167
if-verbose (coq.say {header} "end modules; export" Exports),
187168

188169
export.module {calc (Module ^ ".Exports")} Exports,
189170

190-
if-verbose (coq.say {header} "export" EtaExports),
191-
192-
export.module {calc (Module ^ ".EtaAndMixinExports")} EtaExports,
193-
194171
if-verbose (coq.say {header} "exporting operations"),
195172
ClassAlias => Factories => GRDepsClauses =>
196173
private.export-operations Structure SortProjection ClassProjection MLwP [] EX MLToExport,
@@ -360,8 +337,6 @@ pred declare-coercion i:term, i:term, i:class, i:class.
360337
declare-coercion SortProjection ClassProjection
361338
(class FC StructureF FMLwP) (class TC StructureT TMLwP) :- std.do! [
362339

363-
acc-clause current (sub-class FC TC),
364-
365340
gref->modname StructureF 2 "_" ModNameF,
366341
gref->modname StructureT 2 "_" ModNameT,
367342
CName is ModNameF ^ "_class__to__" ^ ModNameT ^ "_class",
@@ -393,41 +368,10 @@ declare-coercion SortProjection ClassProjection
393368

394369
log.coq.CS.declare-instance SC,
395370

396-
if-verbose (coq.say {header} "declare coercion path compression rules"),
397-
398-
findall-classes All,
399-
CurrentTgtClass = (class TC StructureT TMLwP),
400-
std.filter All (sub-class? CurrentTgtClass) AllTgtSuper,
401-
std.map AllTgtSuper class_structure AllTgtSuperStructures,
402-
403-
mk-compression-clauses StructureF StructureT AllTgtSuperStructures AllCompressionClauses,
404-
std.forall AllCompressionClauses (c\ log.coq.env.accumulate current "hb.db" (clause _ _ c)),
371+
w-params.nparams FMLwP NparamsSC,
372+
acc-clause current (sub-class FC TC SC NparamsSC)
405373
].
406374

407-
408-
pred mk-compression-clauses i:gref, i:gref, i:list gref, o:list prop.
409-
mk-compression-clauses _ _ [] [].
410-
mk-compression-clauses StructureF StructureT [StructureE|Rest] Res :- std.do! [
411-
std.assert! (coq.coercion.db-for (grefclass StructureF) (grefclass StructureT) [pr C1 Nparams1]) "wrong number of coercions",
412-
std.assert! (coq.coercion.db-for (grefclass StructureT) (grefclass StructureE) [pr C2 Nparams2]) "wrong number of coercions",
413-
std.assert! (coq.coercion.db-for (grefclass StructureF) (grefclass StructureE) [pr C3 Nparams3]) "wrong number of coercions",
414-
coq.mk-app (global C1) {coq.mk-n-holes Nparams1} F,
415-
coq.mk-app (global C2) {coq.mk-n-holes Nparams2} G,
416-
coq.mk-app (global C3) {coq.mk-n-holes Nparams3} H,
417-
RuleSkel = {{ fun x => lp:G (lp:F x) = lp:H x}},
418-
std.assert-ok! (coq.elaborate-skeleton RuleSkel _ Rule) "coercion composition fails",
419-
(((pi X L\ coq.fold-map X L X [X|L] :- var X, not(std.exists L (same_var X))) => coq.fold-map Rule [] Rule Holes,
420-
mk-compression-clause Holes Rule Clause,
421-
mk-compression-clauses StructureF StructureT Rest Clauses,
422-
Res = [Clause|Clauses]) ; (Res = [])),
423-
].
424-
425-
pred mk-compression-clause i:list term, i:term, o:prop.
426-
mk-compression-clause [] (fun _ _ x\ app[_,_,LHS x,RHS x]) (pi x\ C x) :-
427-
pi x\ copy (LHS x) (L x), copy (RHS x) (R x), C x = (pi tmp\ compress (L x) (R x)).
428-
mk-compression-clause [UV|Rest] T (pi v\ R v) :-
429-
pi v\ (pi U\ copy U v :- same_var U UV, !) => mk-compression-clause Rest T (R v).
430-
431375
pred join-body i:int, i:int, i:structure, i:term, i:term, i:term, i:term, i:term,
432376
i:list term, i:name, i:term, i:(term -> A), o:term.
433377
join-body N1 N2 S3 S2_Pack S1_sort S3_to_S1 S2_class S3_to_S2
@@ -714,32 +658,4 @@ sigT->list-w-params {{ lib:@hb.sigT _ lp:{{ fun N Ty B }} }} L C :-
714658
@pi-decl N Ty t\
715659
product->triples (B t) (Rest t) C.
716660

717-
pred mk-hb-eta.on i:structure, i:term, i:abbreviation,
718-
i:list term, i:name, i:term, i:A, o:term.
719-
mk-hb-eta.on Structure SortProjection OnAbbrev
720-
Params NT _T _ (fun NT Ty Body) :- !, std.do! [
721-
coq.mk-app (global Structure) Params Ty,
722-
@pi-decl NT Ty s\ sigma Tm\ std.do! [
723-
coq.mk-app {{lib:@hb.eta}}
724-
[_, {coq.mk-app SortProjection {std.append Params [s]}}]
725-
Tm,
726-
std.assert-ok! (coq.typecheck Tm _) "HB: eta illtyped",
727-
coq.notation.abbreviation OnAbbrev [Tm] (Body s)
728-
]
729-
].
730-
731-
pred mk-hb-eta.arity i:structure, i:classname, i:term, i:list term,
732-
i:name, i:term, i:A, o:arity.
733-
mk-hb-eta.arity Structure ClassName SortProjection
734-
Params NT _T _ Out :- !, std.do! [
735-
coq.mk-app (global Structure) Params Ty,
736-
(@pi-decl NT Ty s\ sigma Tm\ std.do! [
737-
coq.mk-app {{lib:@hb.eta}}
738-
[_, {coq.mk-app SortProjection {std.append Params [s]}}] Tm,
739-
std.assert-ok! (coq.typecheck Tm _) "HB: eta illtyped",
740-
coq.mk-app (global ClassName) {std.append Params [Tm]} (Concl s)
741-
]),
742-
Out = parameter {coq.name->id NT} explicit Ty s\ arity (Concl s)
743-
].
744-
745661
}}

0 commit comments

Comments
 (0)