Skip to content

Commit 84c4b5d

Browse files
committed
Discharge factories by hand
In this way we can ensure all mixin sources are mentioned
1 parent ba49448 commit 84c4b5d

File tree

1 file changed

+74
-25
lines changed

1 file changed

+74
-25
lines changed

hb.elpi

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,8 +1105,8 @@ params->holes (w-params.cons _ _ F) [_|PS] :- pi x\ params->holes (F x) PS.
11051105
% rest. For each fulfilled class it declares a local constant inhabiting the
11061106
% corresponding structure and declares it canonical.
11071107
% Each mixin used in order to fulfill a class is returned together with its name.
1108-
pred declare-instances i:term, i:list class.
1109-
declare-instances T [class Class Struct MLwP|Rest] :-
1108+
pred declare-instances i:term, i:list class, o:list constant.
1109+
declare-instances T [class Class Struct MLwP|Rest] [CS|L] :-
11101110
params->holes MLwP Params,
11111111
get-constructor Class KC,
11121112

@@ -1135,9 +1135,9 @@ declare-instances T [class Class Struct MLwP|Rest] :-
11351135

11361136
hb-add-const Name S STy @transparent! CS, % Bug coq/coq#11155, could be a Let
11371137
coq.CS.declare-instance (const CS), % Bug coq/coq#11155, should be local
1138-
declare-instances T Rest.
1139-
declare-instances T [_|Rest] :- declare-instances T Rest.
1140-
declare-instances _ [].
1138+
declare-instances T Rest L.
1139+
declare-instances T [_|Rest] L :- declare-instances T Rest L.
1140+
declare-instances _ [] [].
11411141

11421142
kind factory-abbrev type.
11431143
type factory-by-classname gref -> factory-abbrev.
@@ -1216,12 +1216,12 @@ postulate-mixin (triple M Ps T) MSL [mixin-src T M (global (const C))|MSL] :- MS
12161216
% (section) context with all the mixins provided by the factories and all
12171217
% the structure instances we can derive on TheType from these. Clauses
12181218
% contain mixin-src for each postulated mixin
1219-
pred main-declare-context i:term, i:list term, i:list-w-params factoryname, o:list prop.
1220-
main-declare-context TheType TheParams GRFSwP MSL :- std.do! [
1219+
pred main-declare-context i:term, i:list term, i:list-w-params factoryname, o:list prop, o:list constant.
1220+
main-declare-context TheType TheParams GRFSwP MSL CL :- std.do! [
12211221
factories-provide GRFSwP MLwP,
12221222
apply-w-params MLwP TheParams TheType MLwAllArgs,
12231223
std.fold MLwAllArgs [] postulate-mixin MSL,
1224-
MSL => declare-instances TheType {findall-classes},
1224+
MSL => declare-instances TheType {findall-classes} CL,
12251225
std.forall MSL (ms\ acc current (clause _ _ ms)),
12261226
].
12271227

@@ -1811,7 +1811,7 @@ declare-canonical-instances-from-factory-and-local-builders T F _TheFactory FGR
18111811
{coq.term->string F} "on" {coq.term->string T} ":"
18121812
{std.map MissingMixins nice-gref->string}),
18131813
MixinSrcFromF => add-all-mixins T FGR MissingMixins ff Clauses,
1814-
Clauses => declare-instances T {findall-classes}
1814+
Clauses => declare-instances T {findall-classes} _
18151815
].
18161816

18171817
% [declare-canonical-instances-from-factory T F] given a factory F
@@ -1824,7 +1824,7 @@ declare-canonical-instances-from-factory T F :-
18241824
under-canonical-mixins-of.do! T [
18251825
under-mixin-src-from-factory.do! T F [
18261826
add-all-mixins T GRF {list-w-params_list {factory-provides GRF}} tt _,
1827-
declare-instances T {findall-classes}
1827+
declare-instances T {findall-classes} _
18281828
]
18291829
].
18301830

@@ -1940,7 +1940,7 @@ builders-postulate-factories (context-item IDT _ TySkel none t\ context-item IDF
19401940
std.assert! (factory? (TF TheType) (triple GRF Params TheType))
19411941
"the last argument must be a factory applied to the type variable",
19421942
factory-requires GRF GRFMLwP, % TODO: remove, pass to main-declare-context the list-w-params-eta-expansion of GRF
1943-
main-declare-context TheType Params GRFMLwP _,
1943+
main-declare-context TheType Params GRFMLwP _ _,
19441944
postulate-factory-abbrev TheType Params IDF GRF TheFactory,
19451945
define-factory-operations TheType Params TheFactory GRF,
19461946
acc current (clause _ _ (current-mode (builder-from TheFactory GRF))),
@@ -2028,7 +2028,7 @@ declare-factory-alias Ty1Skel GRFSwP Module TheType TheParams :- std.do! [
20282028

20292029
% TODO maybe main-declare-context should just take GRFSwP and postulate
20302030
% the parameters and the type
2031-
main-declare-context TheType TheParams GRFSwP Hyps,
2031+
main-declare-context TheType TheParams GRFSwP Hyps _,
20322032

20332033
std.assert-ok! (coq.elaborate-ty-skeleton Ty1Skel _ Ty1) "Illtyped alias factory",
20342034
hb-add-const "axioms_" Ty1 _ @transparent! C,
@@ -2088,23 +2088,75 @@ hack-section-discharge-unused [T|TS] F (field _ "_" {{ lib:hb.unify lp:TT lp:TT
20882088
std.assert-ok! (coq.typecheck T TT) "wtf",
20892089
hack-section-discharge-unused TS F F0.
20902090

2091+
2092+
pred copy-indt-decl i:indt-decl, o:indt-decl.
2093+
copy-indt-decl (parameter ID I Ty D) (parameter ID I Ty1 D1) :-
2094+
copy Ty Ty1,
2095+
@pi-parameter ID Ty1 x\ copy-indt-decl (D x) (D1 x).
2096+
copy-indt-decl (inductive ID CO A D) (inductive ID CO A1 D1) :-
2097+
copy-arity A A1,
2098+
@pi-inductive ID A1 i\ std.map (D i) copy-constructor (D1 i).
2099+
copy-indt-decl (record ID T IDK F) (record ID T1 IDK F1) :-
2100+
copy T T1,
2101+
copy-fields F F1.
2102+
2103+
pred copy-fields i:record-decl, o:record-decl.
2104+
copy-fields end-record end-record.
2105+
copy-fields (field Att ID T F) (field Att ID T1 F1) :-
2106+
copy T T1,
2107+
@pi-parameter ID T1 x\ copy-fields (F x) (F1 x).
2108+
2109+
pred copy-constructor i:indc-decl, o:indc-decl.
2110+
copy-constructor (constructor ID A) (constructor ID A1) :- copy-arity A A1.
2111+
2112+
pred abstract-indt-decl i:list term, i:indt-decl, o:indt-decl.
2113+
abstract-indt-decl [] X X1 :- copy-indt-decl X X1.
2114+
abstract-indt-decl [global (const C)|CS] X (parameter ID explicit Ty1 X1) :-
2115+
coq.gref->string (const C) ID,
2116+
coq.env.typeof (const C) Ty,
2117+
copy Ty Ty1,
2118+
@pi-parameter ID Ty x\
2119+
(copy (global (const C)) x :- !) =>
2120+
abstract-indt-decl CS X (X1 x).
2121+
2122+
pred copy-clauses-for-unfold i:list constant, o:list prop.
2123+
copy-clauses-for-unfold [] [].
2124+
copy-clauses-for-unfold [C|CS] [ClauseApp,Clause|L] :-
2125+
coq.env.const C (some B) _,
2126+
ClauseApp = (pi B1 Args Args1 B2 Args2 R\
2127+
copy (app[global (const C)|Args]) R :- !,
2128+
copy B B1,
2129+
std.map Args copy Args1,
2130+
hd-beta B1 Args1 B2 Args2,
2131+
unwind B2 Args2 R),
2132+
Clause = (pi B1\
2133+
copy (global (const C)) B1 :- !, copy B B1),
2134+
copy-clauses-for-unfold CS L.
2135+
20912136
pred declare-mixin-or-factory i:term, i:record-decl, i:list-w-params factoryname, i:id, i:term, i:asset, i:list term.
2092-
declare-mixin-or-factory Sort1 Fields0 GRFSwP Module TheType D TheParams :- std.do! [
2093-
main-declare-context TheType TheParams GRFSwP MixinSrcClauses,
2137+
declare-mixin-or-factory Sort1 Fields GRFSwP Module TheType D TheParams :- std.do! [
2138+
main-declare-context TheType TheParams GRFSwP MixinSrcClauses SectionCanonicalInstance,
20942139

20952140
% HACK: work around section discharging unused stuff
2096-
if (D = asset-mixin) (Fields1 = Fields0, HackExtraPhantNo = 0)
2097-
(std.map MixinSrcClauses mixin-src_src TheHackCrap,
2098-
HackExtraPhantNo is {std.length TheHackCrap} + {std.length TheParams},
2099-
hack-section-discharge-unused [TheType|{std.append TheParams TheHackCrap}] Fields0 Fields1),
2141+
if (D = asset-mixin) (Section = [])
2142+
(std.map MixinSrcClauses mixin-src_src Mixins,
2143+
std.append TheParams [TheType|{std.rev Mixins}] Section),
21002144

21012145
if-verbose (coq.say "HB: declare record axioms_"),
21022146

21032147
Kname = "Axioms_",
2104-
RDeclSkel = record "axioms_" Sort1 Kname Fields1,
2148+
RDeclSkel = record "axioms_" Sort1 Kname Fields,
21052149
std.assert-ok! (coq.elaborate-indt-decl-skeleton RDeclSkel RDecl) "record declaration illtyped",
2106-
hb-add-indt RDecl R,
2107-
coq.env.end-section,
2150+
2151+
% We discharge by hand the record declaration so that we can be sure all
2152+
% parameters and mixins are abstracted (even if unused).
2153+
copy-clauses-for-unfold SectionCanonicalInstance CopyUnfold,
2154+
CopyUnfold => abstract-indt-decl Section RDecl RDeclClosed,
2155+
hb-add-indt RDeclClosed R,
2156+
coq.env.end-section, % We need to anyway declare the record inside the section
2157+
% since closing the section purges the unused universe level we may have
2158+
% allocated by typechecking the skeleton just above
2159+
21082160
coq.env.indt R tt _ _ _ [K] _,
21092161
GRK = indc K,
21102162
@global! => coq.arguments.set-implicit (indt R) [[]],
@@ -2113,13 +2165,10 @@ declare-mixin-or-factory Sort1 Fields0 GRFSwP Module TheType D TheParams :- std.
21132165
% TODO: should this be in the Exports module?
21142166
if-verbose (coq.say "HB: declare notation axioms"),
21152167

2116-
mk-phant-term (global GRK) PhGRK0,
2168+
mk-phant-term (global GRK) PhGRK,
21172169

21182170
if-verbose (coq.say "HB: declare notation Axioms"),
21192171

2120-
% We work around the fact that Coq sections automatically prune unused
2121-
% variables
2122-
if (D = asset-mixin) (PhGRK = PhGRK0) (hack-append-phant-unify HackExtraPhantNo PhGRK0 PhGRK),
21232172
mk-phant-abbrev "Build" PhGRK BuildConst BuildAbbrev,
21242173

21252174
% std.map Hyps mixin-src_mixin ML,

0 commit comments

Comments
 (0)