Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions HB/common/database.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,15 @@ func mixin-instance-type->mixin-src.aux
list prop % Cond list
-> prop.
mixin-instance-type->mixin-src.aux [] T M I Cond (mixin-src T M I :- Cond).
mixin-instance-type->mixin-src.aux [A|Args] T M I Cond (pi a \ C a) :-
pi a \
% The argument A is typechecked before being unified: when it is the primitive
% projection of a parameter of the instance, e.g. (list X.(sort)), the type
% of the parameter X must be known for A to be well typed
mixin-instance-type->mixin-src.aux [A|Args] T M I Cond (pi a aty diag \ C a aty diag) :-
pi a aty diag \
sigma Ta\
coq.mk-app T [a] Ta,
mixin-instance-type->mixin-src.aux Args Ta M I [coq.unify-eq A a ok|Cond] (C a).
mixin-instance-type->mixin-src.aux Args Ta M I
[coq.typecheck A aty diag, coq.unify-eq A a ok|Cond] (C a aty diag).


% transforms the type of a mixin instance into a
Expand Down
21 changes: 12 additions & 9 deletions HB/common/log.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ env.add-const-noimplicits Name Bo Ty Opaque C :- std.do! [
":" {coq.term->string Ty} ":=" {coq.term->string Bo})
true,
avoid-name-collision Name Name1,
coq.env.add-const Name1 Bo Ty Opaque C,
fold-projections Bo Bo1,
if (var Ty) (Ty? = none) (fold-projections Ty Ty1, Ty? = some Ty1),
coq.env.add-const Name1 Bo1 Ty1 Opaque C,
env.add-location (const C),
if (var Ty) (Ty? = none) (Ty? = some Ty),
log.private.log-vernac (log.private.coq.vernac.definition Name1 Ty? Bo),
log.private.log-vernac (log.private.coq.vernac.definition Name1 Ty? Bo1),
@local! => arguments.set-implicit (const C) [[]],
].

Expand All @@ -51,10 +52,11 @@ env.add-const Name Bo Ty Opaque C :- std.do! [
":" {coq.term->string Ty} ":=" {coq.term->string Bo})
true,
avoid-name-collision Name Name1,
coq.env.add-const Name1 Bo Ty Opaque C,
fold-projections Bo Bo1,
if (var Ty) (Ty? = none) (fold-projections Ty Ty1, Ty? = some Ty1),
coq.env.add-const Name1 Bo1 Ty1 Opaque C,
env.add-location (const C),
if (var Ty) (Ty? = none) (Ty? = some Ty),
log.private.log-vernac (log.private.coq.vernac.definition Name1 Ty? Bo),
log.private.log-vernac (log.private.coq.vernac.definition Name1 Ty? Bo1),
].

func env.add-const-noimplicits-failondup id, term, term, opaque? -> constant.
Expand All @@ -63,10 +65,11 @@ env.add-const-noimplicits-failondup Name Bo Ty Opaque C :- std.do! [
(coq.error "HB: cannot infer some information in" Name
":" {coq.term->string Ty} ":=" {coq.term->string Bo})
true,
coq.env.add-const Name Bo Ty Opaque C,
fold-projections Bo Bo1,
if (var Ty) (Ty? = none) (fold-projections Ty Ty1, Ty? = some Ty1),
coq.env.add-const Name Bo1 Ty1 Opaque C,
env.add-location (const C),
if (var Ty) (Ty? = none) (Ty? = some Ty),
log.private.log-vernac (log.private.coq.vernac.definition Name Ty? Bo),
log.private.log-vernac (log.private.coq.vernac.definition Name Ty? Bo1),
@local! => arguments.set-implicit (const C) [[]],
].

Expand Down
8 changes: 8 additions & 0 deletions HB/common/synthesis.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ mixin-for T M MICompressed :- mixin-src T M Tm, !,

%if-verbose (coq.say {header} "Trying to compress mixin for" {coq.term->string MI}),
compress-coercion-paths MI MICompressed.
% The mixins of the sort of an instance of a primitive structure are recorded
% for the primitive projection, not for its compatibility constant
mixin-for T M MICompressed :-
coq.safe-dest-app T (global (const C)) Args,
coq.env.primitive-projection? P C N,
std.length Args NArgs, NArgs > N,
std.drop N Args [S|Rest], !,
mixin-for {coq.mk-app (app [primitive (proj P N), S]) Rest} M MICompressed.

func drop int, list A -> list A.
drop 0 L L :- !.
Expand Down
20 changes: 20 additions & 0 deletions HB/common/utils.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ func if-arg-sort (func) ->.
if-arg-sort P :- get-option "arg_sort" tt, !, P.
if-arg-sort _.

% Records (mixins, factories, structures) are declared as primitive records
% when the attribute #[primitive] is given
func primitive-records.
primitive-records :- get-option "primitive" tt, !.

% [fold-projections T T1] T1 is T where each full application of the
% compatibility constant of a primitive projection is replaced by the
% primitive projection itself, as Rocq does when it elaborates a term: the
% terms HB declares then have the same shape as the ones written by the user
func fold-projections term -> term.
fold-projections T T1 :-
(pi C Args P N NParams NArgs S Rest S1 Rest1\
copy (app [global (const C) | Args]) (app [primitive (proj P N), S1 | Rest1]) :-
coq.env.primitive-projection? P C N,
coq.env.projection? C NParams,
std.length Args NArgs, NArgs > NParams, !,
std.drop NParams Args [S|Rest],
copy S S1, std.map Rest copy Rest1) =>
copy T T1, !.

func if-MC-compat (func option gref ->) ->.
if-MC-compat P :- get-option "mathcomp" tt, !, P none.
if-MC-compat P :- get-option "mathcomp.axiom" S, !,
Expand Down
2 changes: 1 addition & 1 deletion HB/factory.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ declare-mixin-or-factory MixinSrcClauses SectionCanonicalInstance
abstract-over-section TheParams TheType MixinSrcClauses SectionCanonicalInstance coq.abstract-indt-decl RDecl RDeclClosed _,


if (get-option "primitive" tt)
if (primitive-records)
(@primitive! => log.coq.env.add-indt RDeclClosed R)
(log.coq.env.add-indt RDeclClosed R),

Expand Down
11 changes: 10 additions & 1 deletion HB/instance.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,20 @@ declare-mixin-name T (global (const C)) [mixin-mem T (const C)] :- std.do! [
log.coq.env.add-const-noimplicits Name T _ @transparent! C,
].

% [sort-projection-head T C] holds when T is an application of the constant C,
% or of the primitive projection whose compatibility constant is C (the sort
% of an instance of a primitive structure)
func sort-projection-head term -> constant.
sort-projection-head T C :- coq.safe-dest-app T (global (const C)) _, !.
sort-projection-head T C :-
coq.safe-dest-app T (primitive (proj P _)) _,
coq.env.primitive-projection? P C _, !.

func check-non-forgetful-inheritance term, gref.
check-non-forgetful-inheritance _ _ :-
get-option "non_forgetful_inheritance" tt, !.
check-non-forgetful-inheritance T Factory :- std.do! [
if (coq.safe-dest-app T (global (const HdSym)) _, structure-key HdSym _ Super) (
if (sort-projection-head T HdSym, structure-key HdSym _ Super) (
coq.warning "HB" "HB.non-forgetful-inheritance"
"non forgetful inheritance detected.\n"
"You have two solutions:"
Expand Down
21 changes: 19 additions & 2 deletions HB/pack.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@

namespace pack {

% [sort-projection-app T SortProj Params] holds when T is the sort SortProj of
% a structure instance, applied to Params (the parameters of the structure
% followed by the instance), either as a constant or as a primitive
% projection. In the latter case the parameters are the ones of the type of
% the instance, when it is syntactically the structure.
func sort-projection-app term -> constant, list term.
sort-projection-app (app [global (const SortProj)|ProjParams]) SortProj ProjParams :- !.
sort-projection-app (app [primitive (proj P _), S]) SortProj ProjParams :-
coq.env.primitive-projection? P SortProj _,
std.assert-ok! (coq.typecheck S STy) "HB.pack: ill-typed structure instance",
coq.safe-dest-app STy (global (indt _)) SParams,
std.append SParams [S] ProjParams.

pred main i:term, i:list argument, o:term.
main Ty Args Instance :- std.do! [
std.assert! (not(var Ty)) "HB.pack: the structure to pack cannot be unknown, use HB.pack_for",
Expand All @@ -23,7 +36,7 @@ main Ty Args Instance :- std.do! [

if (var T) (coq.error "HB.pack: you must pass a type or at least one factory") true,

if2 (T = app[global (const SortProj)|ProjParams], structure-key SortProj ClassProj _)
if2 (sort-projection-app T SortProj ProjParams, structure-key SortProj ClassProj _)
(AllFactories = [app[global (const ClassProj)|ProjParams] | Factories], Tkey = T) % already existing class on T
(def T _ _ Tkey) % we unfold letins if we can, they may hide constants with CS instances
(AllFactories = Factories)
Expand All @@ -50,9 +63,13 @@ synth-instance.aux Params KC KS T Tkey [] MLCano Instance :-
Instance = app[global KS | InstanceArgs]
].

func has-key term.
has-key T :- coq.safe-dest-app T (global _) _, !.
has-key T :- coq.safe-dest-app T (primitive (proj _ _)) _.

pred synth-instance i:list term, i:gref, i:gref, i:term, i:term, i:list term, o:term.
synth-instance Params KC KS T Tkey Factories Instance :-
if (coq.safe-dest-app Tkey (global _) _) (synthesis.local-canonical-mixins-of Tkey MLCano) (MLCano = []),
if (has-key Tkey) (synthesis.local-canonical-mixins-of Tkey MLCano) (MLCano = []),
synth-instance.aux Params KC KS T Tkey Factories MLCano Instance.

pred elab-factories i:list argument, i:term, o:list term.
Expand Down
21 changes: 15 additions & 6 deletions HB/structure.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,13 @@ shorten coq.{ term->gref, subst-fun, safe-dest-app, mk-app, mk-eta, subst-prod }
% const Po : forall p1 .. pm T m1 .. mn, Extra (Eg Extra = forall x y, x + y = y + z)
% const C : forall p1 .. pm s, Extra
% Po P1 .. PM T M1 .. MN PoArgs -> C P1 .. PM S PoArgs
pred clean-op-ty i:list prop, i:term, i:term, o:term.
clean-op-ty [] _ T1 T2 :- copy T1 T2.
clean-op-ty [exported-op _ Po C|Ops] S T1 T2 :-
% When the mixin is a primitive record, Po is also used as a primitive
% projection, applied to the mixin only:
% MN.(Po) PoArgs -> C P1 .. PM S PoArgs
% where P1 .. PM are the parameters of the structure S lives in
pred clean-op-ty i:list prop, i:list term, i:term, i:term, o:term.
clean-op-ty [] _ _ T1 T2 :- copy T1 T2.
clean-op-ty [exported-op _ Po C|Ops] SParams S T1 T2 :-
gref->deps (const Po) MLwP,
w-params.nparams MLwP NParams,
std.length {list-w-params_list MLwP} NMixins,
Expand All @@ -211,7 +215,12 @@ clean-op-ty [exported-op _ Po C|Ops] S T1 T2 :-
std.drop NMixins Rest PoArgs,
std.append Params [S|PoArgs] L1,
std.map L1 copy L2) =>
clean-op-ty Ops S T1 T2.
(pi P N M PoArgs L1 L2\
copy (app [primitive (proj P N), M | PoArgs]) (app [global (const C) | L2]) :-
coq.env.primitive-projection? P Po _, !,
std.append SParams [S|PoArgs] L1,
std.map L1 copy L2) =>
clean-op-ty Ops SParams S T1 T2.

pred operation-body-and-ty i:list prop, i:constant, i:structure, i:term, i:term,
i:list term, i:term, i:w-args A, o:pair term term.
Expand All @@ -228,7 +237,7 @@ operation-body-and-ty EXI Poperation Struct Psort Pclass Params _T (triple _ Par
% just in case..
synthesis.infer-all-gref->deps ParamsOp Carrier (const Poperation) (Body s),
std.assert-ok! (coq.typecheck (Body s) (DirtyTy s)) "export-1-operation: Body illtyped",
clean-op-ty EXI s (DirtyTy s) (BodyTy s),
clean-op-ty EXI Params s (DirtyTy s) (BodyTy s),
],
]),
].
Expand Down Expand Up @@ -478,7 +487,7 @@ declare-class+structure MLwP Sort
(mk-class-field (indt ClassInd)) StructureDeclaration,

std.assert-ok! (coq.typecheck-indt-decl StructureDeclaration) "declare: illtyped",
if (get-option "primitive" tt)
if (primitive-records)
(@primitive! => log.coq.env.add-indt StructureDeclaration StructureInd)
(log.coq.env.add-indt StructureDeclaration StructureInd),

Expand Down
10 changes: 5 additions & 5 deletions tests/unit/mk_src_map.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ Check list_foo.
Elpi Query HB.structure lp:{{

has-mixin-instance->mixin-src (has-mixin-instance (cs-gref{{:gref list}}) {{:gref is_foo.axioms_}} {{:gref list_foo}}) MS,
MS = (pi a b \
mixin-src (app [{{list}}, b]) ({{:gref is_foo.axioms_}}) (app [{{list_foo}}, a])
:- [coq.unify-eq a b ok])
MS = (pi a b aty diag \
mixin-src (app [{{list}}, b]) ({{:gref is_foo.axioms_}}) (app [{{list_foo}}, a])
:- [coq.typecheck a aty diag, coq.unify-eq a b ok])
}}.

Elpi Query HB.structure lp:{{

has-mixin-instance->mixin-src (has-mixin-instance (cs-gref{{:gref list}}) {{:gref is_foo.axioms_}} {{:gref list_foo'}}) MS',
MS' = (pi p a b \
MS' = (pi p a b aty diag \
mixin-src (app [{{list}}, b]) {{:gref is_foo.axioms_}} (app [{{list_foo'}}, p,a])
:- [coq.unify-eq a b ok]).
:- [coq.typecheck a aty diag, coq.unify-eq a b ok]).
}}.
Loading