Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrapper mixin step0 #370

Draft
wants to merge 64 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 16 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
23 changes: 22 additions & 1 deletion 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 Down Expand Up @@ -232,6 +233,7 @@ declare-mixin-or-factory MixinSrcClauses SectionCanonicalInstance

% coq.say RDecl RDeclClosed,


if (get-option "primitive" tt)
(@primitive! => log.coq.env.add-indt RDeclClosed R)
(log.coq.env.add-indt RDeclClosed R),
Expand All @@ -250,6 +252,25 @@ 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,
% 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
%

if (get-option "wrapper" tt)
((factory-alias->gref (indt R) X),
(WrapperClauses = [wrapper-mixin X (indt R) (indt R)]))
(WrapperClauses = []),

coq.say "aggiungiamo " WrapperClauses,

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

Expand All @@ -271,7 +292,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
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
7 changes: 7 additions & 0 deletions structures.v
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,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
105 changes: 105 additions & 0 deletions tests/enriched_cat.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
(* testing enriched categories *)

From HB Require Import structures.
From Coq Require Import ssreflect ssrfun.

HB.mixin Record isQuiver (Obj: Type) : Type := { hom : Obj -> Obj -> Type }.

HB.structure Definition Quiver : Type := { Obj of isQuiver Obj }.

HB.mixin Record isMon (A: Type) : Type := {
zero : A;
add : A -> A -> A;
addrA : associative add;
add0r : left_id zero add;
addr0 : right_id zero add;
}.

HB.structure
Definition Monoid : Type := { A of isMon A }.

(* original wrapper *)
HB.mixin Record hom_isMon T of Quiver T :=
{ private : forall A B, isMon (@hom T A B) }.

(* just an abbreviation with a parameter for homset *)
Definition hom_isMon_ty {T} (H: T -> T -> Type) (A B: T) :
Type := isMon (H A B).

(* write two versions of Monoid_enriched_quiver: one using hom_isMon
(a mixin, hence a record), the other one using hom_isMon_ty as naked
field type. The former corresponds to the wrapped version, the latter
to the intuitive, wrapper-less version. Now the latter should agree
with the former... (broadly corresponding to 2?) *)

(* alternative wrapper definition *)
HB.mixin Record hom_isMon1 T of Quiver T :=
{ private : forall A B, hom_isMon_ty (@hom (Quiver.clone T _)) A B }.

(* abbreviation with parameters for homset and monoid *)
Definition hom_isM_ty {T} (H: T -> T -> Type) (M: Type -> Type) (A B: T) :
Type := M (H A B).

(* three parameter relation *)
Definition wrapper_spec {T} (H: T -> T -> Type) (M HM: Type -> Type) :
Prop := HM T = forall A B, hom_isM_ty H M A B.

(* one more wrapper definition (no real change) *)
HB.mixin Record hom_isMon2 T of Quiver T :=
{ private : forall A B, hom_isM_ty (@hom (Quiver.clone T _))
(fun X => isMon X)
A B }.

(* trying parametric definition however doesn't work *)
Fail HB.mixin Record hom_isM2 T (M: Type -> Type) of Quiver T :=
{ private : forall (A B: T), @hom_isM_ty T (@hom (Quiver.clone T _))
M
A B }.

(* structure based on one of the wrappers *)
HB.structure Definition Monoid_enriched_quiver :=
{ Obj of isQuiver Obj & hom_isMon2 Obj }.

(******************)

(* unique projection from the wrapper *)
HB.instance Definition _ (T : Monoid_enriched_quiver.type) (A B : T) :
isMon (@hom T A B) := @private T A B.

(* quiver instance (simply typed functions between two types) *)
HB.instance Definition funQ := isQuiver.Build Type (fun A B => A -> B).

(* prove that for every two types the quiver is a monoid *)
Lemma funQ_isMonF (A B: Type) : isMon (A -> B).
Admitted.

(* use the lemma to instantiate isMon *)
HB.instance Definition funQ_isMon (A B: Type) : isMon (A -> B) :=
funQ_isMonF A B.

(* use the generic isMon instance to instantiate 'private' *)
HB.instance Definition funQ_hom_isMon :=
hom_isMon2.Build Type (fun A B => funQ_isMon A B).

(* HB.instance Definition _ := Monoid_enriched_quiver.Build ... *)

(********************)

(* without HB *)
Record isQuiverS (Obj: Type) : Type := { homS : Obj -> Obj -> Type }.

(* structure without wrapper and out of HB, trivially type-checks *)
Structure Monoid_enriched_quiverN := {
ObjN: Type;
iQ: isQuiverS ObjN;
hsM: forall A B, hom_isM_ty (homS ObjN iQ)
(fun X => isMon X) A B }.

(* but mixing with HB doesn't work *)
Fail Record Monoid_enriched_quiverN1 := {
ObjN: Type;
iQ: isQuiver ObjN;
hsM: forall A B, hom_isM_ty (@hom iQ)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails because hom expects a Quiver.type not a isQuiver. You can try to use HB.pack here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(fun X => isMon X) A B }.


72 changes: 72 additions & 0 deletions tests/monoid_enriched_cat.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
From HB Require Import structures.
From Coq Require Import ssreflect ssrfun.

HB.mixin Record isQuiver Obj := { hom : Obj -> Obj -> Type }.

HB.structure Definition Quiver := { Obj of isQuiver Obj }.

HB.mixin Record isMon A := {
zero : A;
add : A -> A -> A;
addrA : associative add;
add0r : left_id zero add;
addr0 : right_id zero add;
}.

HB.structure
Definition Monoid := { A of isMon A }.

Fail HB.structure
Definition Monoid_enriched_quiver :=
{ Obj of isQuiver Obj &
(forall A B : Obj, isMon (@hom (Quiver.clone Obj _) A B)) }.


(* Step 0: define a wrapper predicate in coq-elpi *)
(* 5 lines of documentation + 1 line of elpi code in structure.v
`pred wrapper-mixin o:mixinname, o:gref, o:mixinname`
*)
(* Step 1: add a wrapper attribute to declare wrappers,
they should index:
- the wrapped mixin (`isMon`)
- the wrapper mixin (`hom_isMon`)
- the new subject (`hom`)
This attribute will add an entry in the `wrapper-mixin` database.
As an addition substep, we should check that the wrapper has
exactly one field, which is the wrapped mixin.
*)

(* added wrapper attribute in coq-builtin.elpi.
added pred wrapper-mixin in structures.v.
added conditional rule for wrapper-mixin in factory.elpi.
tentative use of factory-alias->gref, but the parameters
aren't right yet -- see HB.structure.html.
*)
#[wrapper]
HB.mixin Record hom_isMon T of Quiver T :=
{ private : forall A B, isMon (@hom T A B) }.

Elpi Print HB.structure.

stop.

(* Step 2: at structure declaration, export the main and only projection
of each declared wrapper as an instance of the wrapped structure on
its subject *)
HB.structure
Definition Monoid_enriched_quiver :=
{ Obj of isQuiver Obj & hom_isMon Obj }.

HB.instance Definition _ (T : Monoid_enriched_quiver.type) (A B : T) : isMon (@hom T A B) :=
@private T A B.

(* each instance of isMon should be tried as an instance of hom_isMon *)

(* Step 3: for each instance of a wrapped mixin on a subject known
to be wrapped, automatically produce an instance of the wrapper mixin too. *)
HB.instance Definition _ := isQuiver.Build Type (fun A B => A -> B).
Fail HB.instance Definition homTypeMon (A B : Quiver.type) := isMon.Build (hom A B) (* ... *).
(* This last command should create a `Monoid_enriched_quiver`, in order to do so it should
automatically instanciate the wrapper `hom_isMon`:
HB.instance Definition _ := hom_isMon.Build Type homTypeMon.
*)
18 changes: 18 additions & 0 deletions tests/monoid_law_structure.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
From HB Require Import structures.

HB.mixin Record isMonLaw T (e : T) (op : T -> T -> T) := {
opmA : forall a b c, op (op a b) c = op a (op b c);
op1m : forall x, op e x = x;
opm1 : forall x, op x e = x;
}.

HB.structure Definition MonLaw T e := { op of isMonLaw T e op }.

HB.mixin Record isPreMonoid T := {
zero : T;
add : T -> T -> T;
}.
HB.structure Definition PreMonoid := { T of isPreMonoid T }.

HB.structure Definition Monoid :=
{ T of isPreMonoid T & isMonLaw T zero add }.
19 changes: 19 additions & 0 deletions tests/monoid_law_structure_clone.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
From HB Require Import structures.

HB.mixin Record isMonLaw T (e : T) (op : T -> T -> T) := {
opmA : forall a b c, op (op a b) c = op a (op b c);
op1m : forall x, op e x = x;
opm1 : forall x, op x e = x;
}.

HB.structure Definition MonLaw T e := { op of isMonLaw T e op }.

HB.mixin Record isPreMonoid T := {
zero : T;
add : T -> T -> T;
}.
HB.structure Definition PreMonoid := { T of isPreMonoid T }.

HB.structure Definition Monoid :=
{ T of isPreMonoid T &
isMonLaw T (@zero (PreMonoid.clone T _)) (@add (PreMonoid.clone T _)) }.