Skip to content

Commit ce0707b

Browse files
committed
kind of works
1 parent 8cdb9d2 commit ce0707b

File tree

4 files changed

+66
-17
lines changed

4 files changed

+66
-17
lines changed

HB/about.elpi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ main-located S (loc-gref (indt I)) :- factory-constructor (indt I) _, !,
2626
main-located S (loc-gref (const C)) :- factory-constructor (const C) _, !,
2727
private.main-factory-alias S C.
2828

29-
main-located S (loc-gref (const C)) :- exported-op M _ C, !,
29+
main-located S (loc-gref (const C)) :- exported-op M _ _ C, !,
3030
private.main-operation S M C.
3131

3232
main-located S (loc-gref GR) :- factory-alias->gref GR F, not (F = GR), !,
@@ -189,8 +189,8 @@ main-structure S Class Structure MLwP :-
189189
list-w-params_list MLwP ML,
190190
std.map-filter ML (m\r\ sigma P O OPS\
191191
mixin-first-class m Class,
192-
std.findall (exported-op m P O) OPS,
193-
std.map OPS (c\out\ sigma p\ c = exported-op m p out) r) OPLL,
192+
std.findall (exported-op m P _ O) OPS,
193+
std.map OPS (c\out\ sigma p\ c = exported-op m p _ out) r) OPLL,
194194
std.flatten OPLL Operations,
195195
std.map {std.findall (sub-class Class CS_ CoeS_ NS_)}
196196
(x\r\ x = sub-class Class r _ _) SubClasses,
@@ -272,7 +272,7 @@ compute-arg-type (w-params.nil ID Ty Rest) F Acc [] ID FN [pr ID PPTy|Xs] :-
272272
@pi-parameter ID Ty x\ compute-arg-type.fields F {std.length (Rest x)} {std.rev [x|Acc]} Xs FN.
273273
pred compute-arg-type.fields i:list constant, i:int, i:list term, o:list (pair id coq.pp), o:list id.
274274
compute-arg-type.fields [] _ _ [] [].
275-
compute-arg-type.fields [C|Cs] NDeps Args [pr ID PPTy|Xs] [ID|FN] :- exported-op _ C OP, !,
275+
compute-arg-type.fields [C|Cs] NDeps Args [pr ID PPTy|Xs] [ID|FN] :- exported-op _ C _ OP, !,
276276
coq.env.typeof (const OP) Ty,
277277
coq.gref->id (const OP) ID,
278278
coq.subst-prod Args Ty TyArgs,

HB/structure.elpi

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,51 @@ shorten coq.{ term->gref, subst-fun, safe-dest-app, mk-app, mk-eta, subst-prod }
198198
% const Po : forall p1 .. pm T m1 .. mn, Extra (Eg Extra = forall x y, x + y = y + z)
199199
% const C : forall p1 .. pm s, Extra
200200
% Po P1 .. PM T M1 .. MN PoArgs -> C P1 .. PM S PoArgs
201+
pred copy-term i:term, o:term.
202+
copy-term X Y :- name X, !, X = Y, !. % avoid loading "copy-term x x" at binders
203+
copy-term (global _ as C) C :- !.
204+
copy-term (pglobal _ _ as C) C :- !.
205+
copy-term (sort _ as C) C :- !.
206+
copy-term (fun N T F) (fun N T1 F1) :- !,
207+
copy-term T T1, @pi-decl N T1 x\ copy-term (F x) (F1 x).
208+
copy-term (let N T B F) (let N T1 B1 F1) :- !,
209+
copy-term T T1, copy-term B B1, @pi-def N T1 B1 x\ copy-term (F x) (F1 x).
210+
copy-term (prod N T F) (prod N T1 F1) :- !,
211+
copy-term T T1, (@pi-decl N T1 x\ copy-term (F x) (F1 x)).
212+
copy-term (app L) (app L1) :- !, std.map L copy-term L1.
213+
copy-term (fix N Rno Ty F) (fix N Rno Ty1 F1) :- !,
214+
copy-term Ty Ty1, @pi-decl N Ty1 x\ copy-term (F x) (F1 x).
215+
copy-term (match T Rty B) (match T1 Rty1 B1) :- !,
216+
copy-term T T1, copy-term Rty Rty1, std.map B copy-term B1.
217+
copy-term (primitive _ as C) C :- !.
218+
copy-term (uvar M L as X) W :- var X, !, std.map L copy-term L1, coq.mk-app-uvar M L1 W.
219+
% when used in CHR rules
220+
copy-term (uvar X L) (uvar X L1) :- std.map L copy-term L1.
221+
222+
201223
pred clean-op-ty i:list prop, i:term, i:term, o:term.
202-
clean-op-ty [] _ T1 T2 :- copy T1 T2.
203-
clean-op-ty [exported-op _ Po C|Ops] S T1 T2 :-
224+
clean-op-ty [] _ T1 T2 :- copy-term T1 T2.
225+
clean-op-ty [exported-op _ Po PrimPop C|Ops] S T1 T2 :-
204226
gref-deps (const Po) MLwP,
205227
w-params.nparams MLwP NParams,
206228
std.length {list-w-params_list MLwP} NMixins,
207229

208230
(pi L L1 L2 Params Rest PoArgs\
209-
copy (app [global (const Po)| L]) (app [global (const C) | L2]) :-
231+
copy-term (app [global (const Po)| L]) (app [global (const C) | L2]) :-
210232
std.split-at NParams L Params [_|Rest],
211233
std.drop NMixins Rest PoArgs,
212234
std.append Params [S|PoArgs] L1,
213-
std.map L1 copy L2) =>
235+
std.map L1 copy-term L2) =>
236+
237+
(pi P N L1 L2 L3 TheMixin Params Ty\
238+
% even if NMixins > 1, they are parameters of the primitive projection so
239+
% they are not present
240+
copy-term (app [primitive (proj P N), TheMixin|L1]) (app [global (const C) | L3]) :- PrimPop = some (pr P N), !,
241+
coq.mk-n-holes NParams Params,
242+
std.append Params [S|L1] L2,
243+
std.map L2 copy-term L3,
244+
std.assert-ok! (coq.typecheck (app [global (const C) | L3]) Ty) "clean-op-ty") =>
245+
214246
clean-op-ty Ops S T1 T2.
215247

216248
pred operation-body-and-ty i:list prop, i:constant, i:structure, i:term, i:term,
@@ -237,9 +269,9 @@ operation-body-and-ty EXI Poperation Struct Psort Pclass Params _T (triple _ Par
237269
% same operation out of the package structure (out of the class field of the
238270
% structure). We also provide all the other mixin dependencies (other misins)
239271
% of the package structure.
240-
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.
241-
export-1-operation _ _ _ _ _ none EX EX :- !. % not a projection, no operation
242-
export-1-operation M Struct Psort Pclass MwP (some Poperation) EXI EXO :- !, std.do! [
272+
pred export-1-operation i:mixinname, i:structure, i:term, i:term, i:one-w-params mixinname, i:option constant, i:option (pair projection int), i:list prop, o:list prop.
273+
export-1-operation _ _ _ _ _ none _ EX EX :- !. % not a projection, no operation
274+
export-1-operation M Struct Psort Pclass MwP (some Poperation) PrimPop EXI EXO :- !, std.do! [
243275
coq.gref->id (const Poperation) Name,
244276

245277
w-params.then MwP mk-fun-prod ignore (operation-body-and-ty EXI Poperation Struct Psort Pclass) (pr Body BodyTy),
@@ -253,15 +285,16 @@ export-1-operation M Struct Psort Pclass MwP (some Poperation) EXI EXO :- !, std
253285
std.map INI (_\r\ r = maximal) Implicits,
254286
@global! => log.coq.arguments.set-implicit (const C) [Implicits],
255287

256-
EXO = [exported-op M Poperation C|EXI]
288+
EXO = [exported-op M Poperation PrimPop C|EXI]
257289
].
258290

259291
% Given a list of mixins, it exports all operations in there
260292
pred export-operations.aux i:structure, i:term, i:term, i:one-w-params mixinname, i:list prop, o:list prop.
261293
export-operations.aux Struct ProjSort ProjClass MwP EX1 EX2 :- !, std.do! [
262294
w-params_1 MwP (indt M),
263295
coq.env.projections M Poperations,
264-
std.fold Poperations EX1 (export-1-operation (indt M) Struct ProjSort ProjClass MwP) EX2,
296+
coq.env.primitive-projections M PrimPoperations,
297+
std.fold2 Poperations PrimPoperations EX1 (export-1-operation (indt M) Struct ProjSort ProjClass MwP) EX2,
265298
].
266299

267300
pred mixin-not-already-declared i:one-w-params mixinname.

structures.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ pred mixin-mem i:term, o:gref.
171171
% that contains the mixin M
172172
pred mixin-first-class o:mixinname, o:classname.
173173

174-
% memory of exported operations (TODO: document fiels)
175-
pred exported-op o:mixinname, o:constant, o:constant.
174+
% memory of exported operations [exported-op Mixin MixinProj PrimitiveMixinProj StructureProj]
175+
pred exported-op o:mixinname, o:constant, o:option (pair projection int), o:constant.
176176

177177
% memory of factory sort coercion
178178
pred factory-sort o:coercion.

tests/test_primproj_ty.v

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,26 @@ HB.mixin Record AddMonoid_of_TYPE S := {
1212

1313
HB.structure Definition AddMonoid := { A of AddMonoid_of_TYPE A }.
1414

15-
Set Printing All.
16-
1715
Goal let x := @addrA in True.
1816
match goal with
1917
| |- let x : forall s : AddMonoid.type, @associative (AddMonoid.sort s) (@add s) := @addrA in True => idtac "OK"
2018
end.
2119
Abort.
20+
21+
#[primitive]
22+
HB.mixin Record Scale_of_AddMonoid (P : Type) S of AddMonoid_of_TYPE S := {
23+
scale : P -> S -> S;
24+
scaleBad : forall p (x y : S), (* HUMMM, if I don't put S here, it infers the eta expansion of S *)
25+
scale p (add x y) = add (scale p x) (scale p y);
26+
}.
27+
28+
About Scale_of_AddMonoid.scale.
29+
30+
HB.structure Definition ScaleMonoid P := { A of Scale_of_AddMonoid P A }.
31+
32+
Goal let a := @scaleBad in True.
33+
match goal with
34+
| |- let a : forall P (s : ScaleMonoid.type P), forall p : P, forall x y : ScaleMonoid.sort P s, scale p (add x y) = add (scale p x) (scale p y) := @scaleBad in True => idtac "OK"
35+
end.
36+
Abort.
37+

0 commit comments

Comments
 (0)