Skip to content
Draft
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
6f1dee6
test for step 0 of CoREACT
gares Jun 2, 2023
1ceb6ea
update
gares Jun 2, 2023
f6c1970
more
gares Jun 2, 2023
17d6561
more
gares Jun 2, 2023
d2bc00f
Update tests/monoid_enriched_cat.v
CohenCyril Jun 19, 2023
6726472
Update tests/monoid_enriched_cat.v
CohenCyril Jun 19, 2023
0821793
Update tests/monoid_enriched_cat.v
CohenCyril Jun 19, 2023
234e3f5
Update tests/monoid_enriched_cat.v
CohenCyril Jun 19, 2023
92e1f68
Update tests/monoid_enriched_cat.v
CohenCyril Jun 19, 2023
b7f3789
Update tests/monoid_enriched_cat.v
CohenCyril Jun 19, 2023
80e17f2
added enriched_cat.v
ptorrx Jun 19, 2023
314c6b9
copied Cyril's monoid_enriched_cat.v to enriched_cat.v
ptorrx Jun 19, 2023
67526eb
added monoid_enriched_cat.v as the original pull request by Cyril, in…
ptorrx Jun 20, 2023
a3bcee5
add enriched_cat to _CoqProject
ptorrx Jun 21, 2023
f472ce6
changes to enriched_cat.v
ptorrx Jun 21, 2023
b3ba8c1
changes related to monoid_enriched_cat.v and wrappers in various file…
ptorrx Jun 21, 2023
9f0bde5
change to enriched_cat.v
ptorrx Jun 22, 2023
056f6e0
minor changes
ptorrx Jun 22, 2023
b78c77c
added Elpi code in monoid_enriched_cat.v
ptorrx Jun 23, 2023
0d0e9b7
changes in monoid_enriched_cat.v
ptorrx Jun 26, 2023
b6b8278
added improved version of extraction predicates in monoid_enriched_cat.v
ptorrx Jun 27, 2023
145aa0d
minor changes in monoid_enriched_cat.v
ptorrx Jun 27, 2023
8d0310e
moved Elpi code from monoid_enriched_cat.v to factory.v. monoid-enric…
ptorrx Jun 27, 2023
0b2b79f
minor changes in monoid_enriched_cat.v
ptorrx Jun 28, 2023
7fbd129
added instance in monoid_enriched_cat.v
ptorrx Jun 28, 2023
5ded009
added some tests to monoid_enriched_cat.v
ptorrx Jun 28, 2023
2ea2be6
pseudo-code to add new instances for each wrapper mixin
CohenCyril Jun 28, 2023
2fccea4
added some comment
ptorrx Jun 29, 2023
1ffc93d
tentative changes in structure.elpi (reexport-wrapper-as-instance is …
ptorrx Jun 29, 2023
d9c7980
changes in structure.elpi and monoid_enriched_cat.v - compiles
ptorrx Jun 30, 2023
7158c54
added comments in structure.elpi
ptorrx Jun 30, 2023
7ec530a
changes in structure.elpi, to prepare the derivation of the wrapper i…
ptorrx Jul 3, 2023
7e83bd9
added comments in structure.elpi
ptorrx Jul 3, 2023
40eef02
tentative changes to structure.elpi
ptorrx Jul 3, 2023
a26cf4b
changes in structure.elpi - mainly comments
ptorrx Jul 4, 2023
09b0222
changes in structure.elpi - mainly comments
ptorrx Jul 4, 2023
e5f56a1
main changes in instance.elpi
ptorrx Jul 4, 2023
36dc631
added commments in instance.elpi
ptorrx Jul 4, 2023
b46a369
minor changes in monoid_enriched_cat.v (wrapper instance definition n…
ptorrx Jul 4, 2023
d93a11d
Update tests/monoid_enriched_cat.v
ptorrx Jul 5, 2023
e4c3732
added summary of today's discussion at the bottom of instance.elpi
ptorrx Jul 5, 2023
8c30d1a
Merge remote-tracking branch 'origin/wrapper' into wrapper
ptorrx Jul 5, 2023
911ede1
setup nix
CohenCyril Jul 3, 2023
4e47908
blind attempt
CohenCyril Jul 6, 2023
3d0961c
minor changes to comments in instance.elpi
ptorrx Jul 6, 2023
314dadc
added debug printouts
ptorrx Jul 13, 2023
5f18ee4
revised comments and printouts, minor change in structure.elpi to nam…
ptorrx Jul 17, 2023
ce51cc4
Switched to tentive version of add-all-mixins with extra parameter. B…
ptorrx Jul 18, 2023
641d35d
gone back to old add-all-mixins (without std.forall), revised comment…
ptorrx Jul 19, 2023
6bdfd59
gone back to old add-all-mixins (without std.forall), revised comment…
ptorrx Jul 19, 2023
790ca44
added predicates to handle cumulative output over forall, avoiding du…
ptorrx Jul 20, 2023
f0975bb
added redirect-instances (instances.elpi), in progress
ptorrx Jul 20, 2023
73e614b
instance.elpi, redirect-instances - in progress (section problem)
ptorrx Jul 21, 2023
ab87dd8
instance.elpi, redirect-instances - in progress (section problem)
ptorrx Jul 21, 2023
11baf58
instance.elpi: redirect-instances in progress, still need to add call…
ptorrx Jul 21, 2023
8e22bb3
instance.elpi: redirect-instances in progress, added call to declare-…
ptorrx Jul 24, 2023
3dbe65e
instance.elpi: redirect-instances in progress, added call to declare-…
ptorrx Jul 24, 2023
e7361bf
more comments in instance.elpi, added lemma in enriched_cat.v and fac…
ptorrx Jul 24, 2023
9d80cdf
enriched_cat.v: minor changes
ptorrx Jul 26, 2023
7835883
enriched_cat.v: minor changes
ptorrx Jul 26, 2023
f8da435
test commit, minor changes
ptorrx Aug 3, 2023
2338a1f
adding examples in cmon_enriched_cat.v
ptorrx Aug 4, 2023
7907b7f
updated cmonoid_enriched_cat.v with new examples; some problems
ptorrx Aug 9, 2023
ce88b3d
updated cmon_enriched_cat.v - now the file contains three examples; c…
ptorrx Aug 10, 2023
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
1 change: 1 addition & 0 deletions HB/common/utils.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ with-attributes P :-
att "primitive" bool,
att "non_forgetful_inheritance" bool,
att "hnf" bool,
att "wrapper" bool,
] Opts, !,
Opts => (save-docstring, P).

Expand Down
72 changes: 70 additions & 2 deletions HB/factory.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ mk-factory-abbrev Str GR Aliases FactAbbrev :- !, std.do! [
pred declare-asset i:argument, i:asset.
declare-asset Arg AssetKind :- std.do! [
argument-name Arg Module,

if-verbose (coq.say {header} "start module and section" Module),
log.coq.env.begin-module Module none,
log.coq.env.begin-section Module,
Expand All @@ -218,6 +219,49 @@ declare-asset Arg AssetKind :- std.do! [
)
].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% auxiliary code for wrapper-mixin

pred extract_from_record_decl i: (term -> gref -> prop), i:indt-decl, o:gref.
extract_from_record_decl P (parameter ID _ _ R) Out :-
pi p\
extract_from_record_decl P (R p) Out.
extract_from_record_decl P (record ID _ KID (field _ _ Ty (x\end-record))) GR0 :-
P Ty GR0.

pred extract_from_rtty i: (term -> gref -> prop), i: term, o:gref.
extract_from_rtty P (prod _ _ TF) Out1 :-
pi p\
extract_from_rtty P (TF p) Out1.
extract_from_rtty P Ty Gr :- P Ty Gr.

pred xtr_fst_op i:term, o:gref.
xtr_fst_op Ty Gr1 :-
Ty = (app [global Gr0| _]),
factory-alias->gref Gr0 Gr1.

pred xtr_snd_op i:term, o:gref.
xtr_snd_op Ty Gr :-
Ty = (app [global _, app [global Gr| _]]).

pred extract_wrapped i:indt-decl, o:gref.
extract_wrapped In Out :-
extract_from_record_decl (extract_from_rtty xtr_fst_op) In Out.

pred extract_subject i:indt-decl, o:gref.
extract_subject In Out :-
extract_from_record_decl (extract_from_rtty xtr_snd_op) In Out.

pred wrapper_mixin_aux i:gref, o:gref, o:gref.
wrapper_mixin_aux XX Gr1 Gr2 :-
XX = (indt I),
coq.env.indt-decl I D,
extract_subject D Gr1,
extract_wrapped D Gr2.

%%% end auxiliary code for wrapper-mixin
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

pred declare-mixin-or-factory i:list prop, i:list constant, i:list term, i:term,
i:term, i:record-decl, i:list-w-params factoryname, i:id, i:asset.
declare-mixin-or-factory MixinSrcClauses SectionCanonicalInstance
Expand All @@ -230,7 +274,9 @@ declare-mixin-or-factory MixinSrcClauses SectionCanonicalInstance

abstract-over-section TheParams TheType MixinSrcClauses SectionCanonicalInstance coq.abstract-indt-decl RDecl RDeclClosed _,

% coq.say RDecl RDeclClosed,
% coq.say "TEST" RDecl RDeclClosed,
% coq.say "TEST" RDecl,
% coq.say "TEST" (indt R),

if (get-option "primitive" tt)
(@primitive! => log.coq.env.add-indt RDeclClosed R)
Expand All @@ -250,6 +296,28 @@ declare-mixin-or-factory MixinSrcClauses SectionCanonicalInstance
build-deps-for-projections R MLwP GRDepsClausesProjs,
GRDepsClauses = [gref-deps (indt R) MLwP, gref-deps (indc K) MLwP|GRDepsClausesProjs],

coq.say "TODO: extract useful info:" RDecl,
% coq.say "TODO: extract useful info:" RDecl "AND THEN:" RDeclClosed,
% record-decl in https://github.com/LPCIC/coq-elpi/blob/master/coq-builtin.elpi#L429
% per trovare il vero nome del mixin database.elpi:factory-alias->gref
% per sapere quanti argomenti skippare prima del soggetto del mixin
% factory-nparams
%
% in generale hai:
% forall ....., app[mixin_alias, p1 ... pn | [ app[subject, ...] , extra]]
% mixin_alias -> mixin via factory-alias->gref
% factory-nparams mixin -> n
%

% coq.say "TEST 2" (indt R),

if (get-option "wrapper" tt)
((wrapper_mixin_aux (indt R) NSbj WMxn),
(WrapperClauses = [wrapper-mixin (indt R) NSbj WMxn]))
(WrapperClauses = []),

% coq.say "aggiungiamo " WrapperClauses,

% GRDepsClauses => mk-factory-sort MLwP (indt R) _ FactorySortCoe,
% FactorySortCoe = coercion GRFSort _ _ _,

Expand All @@ -271,7 +339,7 @@ declare-mixin-or-factory MixinSrcClauses SectionCanonicalInstance
if-verbose (coq.say {header} "start module Exports"),

log.coq.env.begin-module "Exports" none,
std.flatten [Clauses, GRDepsClauses, [
std.flatten [Clauses, GRDepsClauses, WrapperClauses, [
factory-constructor (indt R) GRK,
factory-nparams (indt R) NParams,
factory-builder-nparams BuildConst NParams,
Expand Down
125 changes: 124 additions & 1 deletion HB/instance.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,57 @@ declare-const Name BodySkel TyWPSkel CSL :- std.do! [

private.declare-instance Factory TheType TheFactory Clauses CSL,

% In a WRAPPING-RELEVANT INSTANCE,
% Factory is the wrapped mixin, TheType is the new subject,
% TheFactory is the wrapped instance for the new subject (in fact,
% it is the term for the instance that is being processed).
% E.g. in connection with our example in monoid_enriched_cat.v:
%
% HB.instance Definition funQ_isMon (A B: Type) : isMon (hom A B) :=
% funQ_isMonF A B.
%
% Factory = isMon
% TheFactory = funQ_isMon
% TheType = hom A B

coq.say "wrapping instance ???",
coq.say "Factory = " Factory,
coq.say "TheFactory = " TheFactory,
coq.say "TheType = " TheType,

% extracts the head subterm (e.g. hom)
coq.safe-dest-app TheType TheTypeKey _,

coq.say "TheTypeKey = " TheTypeKey,

% handle parameters via a section -- end
if (TyWP = arity _) true (
if-verbose (coq.say {header} "closing instance section"),
log.coq.env.end-section-name SectionName
),

% we accumulate clauses now that the section is over
acc-clauses current Clauses
acc-clauses current Clauses,

% As a general idea, we want to add wrapper instances
% for the original subjects that are covered by the current instances.
% Here we are processing a specific instance. We want to check
% whether it corresponds to a wrapper with respect to the new subject
% (associated with TheTypeKey and the Factory mixin).
% To this purpose,
% 1) we find all the wrappers that agree with the given parameters,
% using the wrapper-mixin predicate.
% 2) we then apply derive_wrapper_instances to each wrapper, together
% with the current instance, to create the derived wrapper instances.
% Notice that derive_wrapper_instances calls declare-const.
if (TheTypeKey = global TheTypeKeyGR)
(std.do! [
std.findall (wrapper-mixin _ TheTypeKeyGR Factory) Wrappers,
coq.say "WRAPPERS:" Wrappers,
std.forall Wrappers (private.derive_wrapper_instances TheFactory)
]
)
true,
].

% [declare-all T CL MCSTL] given a type T and a list of class definition
Expand Down Expand Up @@ -375,4 +418,84 @@ check-non-forgetful-inheritance T Factory :- std.do! [
) true
].

% The type of the wrapper projection is...
% hom_isMon.hom_isMon_private :
% forall (T : Type)
% (local_mixin_monoid_enriched_cat_isQuiver : isQuiver.axioms_ T),
% hom_isMon.axioms_ T local_mixin_monoid_enriched_cat_isQuiver ->
% forall
% A
% B : {|
% Quiver.sort := T;
% Quiver.class :=
% {|
% Quiver.monoid_enriched_cat_isQuiver_mixin :=
% local_mixin_monoid_enriched_cat_isQuiver
% |}
% |}, isMon.phant_axioms (hom A B)

% Not used, but possible useful
pred extract_string_name i:mixinname i:term o:string.
extract_string_name W Trm Str.

% Instance has morally type 'Mixin Subject'
pred derive_wrapper_instances i:term, i:prop.
derive_wrapper_instances Instance (wrapper-mixin WrapperMixin Subject Mixin) :- std.do! [
% K is the mixin constructor (Build) for WrapperMixin
factory-constructor WrapperMixin K,
factory-nparams WrapperMixin NParams,
std.assert! (NParams = 0) "TODO support parameters",

% We are only interested in the last parameter of the constructor
% type, which is the current instance
% (which is a Mixin instance on the new Subject).
% In monoid_enriched_cat.v, we are targeting the code
%
% HB.instance Definition funQ_hom_isMon :=
% hom_isMon.Axioms_ _ _ funQ_isMon.
% which Coq can compute to stand for
% hom_isMon.Axioms_ Type funQ funQ_isMon.
%
% We compute the number of the underscores and we pass
% them as arguments followed by Instance.
coq.env.typeof K KTy,
coq.count-prods KTy KN,
KN0 = KN - 1,
coq.mk-n-holes KN0 Holes,

std.append Holes [Instance] Args,

% the body of the new wrapper instance
NewInstance = app[global K| Args],

coq.say "NewInstance=" {coq.term->string NewInstance},

coq.typecheck NewInstance Ty Dgn,
if (Dgn = error S) (coq.say "errore" S)
(coq.count-prods Ty N0,
% we then call declare-const to add the new instance.
% the name needs to be fixed.
instance.declare-const "_" NewInstance {coq.term->arity Ty N0} _
)

/*
% 2) The existing instances of W correspond to the functions of type (Ty _)
% that are already defined for different original subjects;
% or in other words, they correspond to wrapped instances,
% given a specific original subject T and two generic objects of that type.
% Should we consider only one function for each T?
% This means that e.g. there is only one way a homset can be instance of the wrapped
% (here, a monoid).
% Notice that it's good to assume all relevant functions have been
% defined using generic type syntax (otherwise recovering them would be much harder).
% This means for a carrier there is only a quiver.
% The instances whould be filtered out from the current context, maybe EX?
filter_out_unique_defs_of_type Ty WrapperProjections,
% filter_out_unique_defs_of_type Ty AllInstances WrapperProjections,
% 3) We can define the wrapper instances based on Ty, using the projection definitions.
std.forall WrapperProjections (export-wrapper-instance W)
*/
].
derive_wrapper_instances _ _.

}}
57 changes: 57 additions & 0 deletions HB/structure.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,46 @@ declare Module BSkel Sort :- std.do! [
log.coq.env.end-module-name ElpiOperationModName ElpiOperations,
export.module ElpiOperationModName ElpiOperations,

% factories-provide (line 16) gives the mixins provided from a factory.
% line 23 (list-w-params_list) associates these mixins with their names (ML).
% we need to filter the wrappers out of ML.
% then export the projection of each mixin wrapper.
% can use exported-op to this purpose

%% tentative pseudo-code::
% std.filter ML (x\ wrapper-mixin x _ _) WrapperML,
% std.forall WrapperML private.rexport-wrapper-as-instance,

%% write private code below:
% pred rexport-wrapper-as-instance i:mixinname.
% reexport-wrapper-as-instance M :-
% exported-op M _ C,
% declare-existing-instance C %fixme.

std.filter ML (x\ wrapper-mixin x _ _) WrapperML,
% we need to assert locally the clauses in EX, which is
% [exported-op (indt «hom_isMon.axioms_») «hom_isMon.hom_isMon_private»
% «hom_isMon_private»]
% crucially containing hom_isMon_private.
% In fact, these clauses get added to the global database by Accumulate,
% but this makes them available only at the end of the program run, and
% this is too late.
% Then given WrapperML (the list of the wrapper mixins as grefs)
% i.e. [indt «hom_isMon.axioms_»]
% we can use reexport-wrapper-as-instance to get the projection and build
% the instances.
coq.say "???? WrapperML " WrapperML,
coq.say "???? EX " EX,
EX => std.forall WrapperML private.reexport-wrapper-as-instance,

coq.say "???? NewClauses " NewClauses,

% tentative: generating wrapper instances from the available definitions.
% Wrong approach: we actually need to check each new defined instance to
% see if it triggers a wrapper instance too. We do this in instance.elpi.
% EX => std.forall WrapperML private.derive_wrapper_instances,
% NewClauses => std.forall WrapperML private.derive_wrapper_instances,

if-verbose (coq.say {header} "abbreviation factory-by-classname"),

NewClauses => factory.declare-abbrev Module (factory.by-classname ClassName),
Expand Down Expand Up @@ -738,4 +778,21 @@ mk-hb-eta.arity Structure ClassName SortProjection
Out = parameter {coq.name->id NT} explicit Ty s\ arity (Concl s)
].

% M is the gref of the wrapper mixin.
% C gets now instantiated to the projection, i.e. hom_isMon_private.
% we need to count the parameters, we can get that from the type.
% we then can construct the instance, using
% instance.declare-const (notably used in the API, i.e. in structures.v,
% HB.instance)
pred reexport-wrapper-as-instance i:mixinname.
reexport-wrapper-as-instance M :- std.spy-do! [
exported-op M _ C,
coq.say "???? C=" C,
B = (global (const C)),
coq.env.typeof (const C) Ty,
coq.count-prods Ty N0,
coq.term->arity Ty N0 Arity,
instance.declare-const "xx" B Arity _
].

}}
4 changes: 4 additions & 0 deletions _CoqProject.test-suite
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ tests/hnf.v
tests/fun_instance.v
tests/issue284.v
tests/issue287.v
tests/monoid_law_structure.v
tests/monoid_law_structure_clone.v
tests/enriched_cat.v
tests/monoid_enriched_cat.v

-R tests HB.tests
-R examples HB.examples
Expand Down
36 changes: 36 additions & 0 deletions structures.v
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ Definition ignore_disabled {T T'} (x : T) (x' : T') := x'.
(* ********************* structures ****************************** *)
From elpi Require Import elpi.

(******* simple example of Elpi command *)
Axiom m : Type -> Type.
Record r (P : Type) := { private : m P }.

(* command name *)
Elpi Command foo_example_command.
(* predicate definition *)
Elpi Accumulate lp:{{

pred extract i:indt-decl, o:gref.
extract (parameter ID _ _ R) Out :-
pi p\
extract (R p) Out.
extract (record ID _ KID (field _ _ Ty (x\end-record))) GR :-
Ty = app [global GR| _].

}}.
Elpi Typecheck.

(* predicate query *)
Elpi Query lp:{{

coq.locate "r" (indt I),
coq.env.indt-decl I D,
extract D GR.

}}.
(******* end simple example *)

Register unify as hb.unify.
Register id_phant as hb.id.
Register id_phant_disabled as hb.id_disabled.
Expand Down Expand Up @@ -159,6 +188,13 @@ pred join o:classname, o:classname, o:classname.
% in order to discover two mixins are the same)
pred mixin-mem i:term, o:gref.

% [wrapper-mixin Wrapper NewSubject WrappedMixin]
% #[wrapper] HB.mixin Record hom_isMon T of Quiver T :=
% { private : forall A B, isMon (@hom T A B) }.
% -->
% wrapper-mixin (indt "hom_isMon") (const "hom") (indt "isMon").
pred wrapper-mixin o:mixinname, o:gref, o:mixinname.

%%%%%% Memory of exported mixins (HB.structure) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Operations (named mixin fields) need to be exported exactly once,
% but the same mixin can be used in many structure, hence this memory
Expand Down
Loading