Skip to content

Commit 2cb6f0b

Browse files
committed
validate structure declaration and wrap is the wrapper already exists
1 parent 9e9358f commit 2cb6f0b

2 files changed

Lines changed: 72 additions & 15 deletions

File tree

HB/structure.elpi

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ declare Module BSkel Sort :- std.do! [
1313
re-enable-id-phant BNoId B,
1414
private.sigT->list-w-params B GRFSwP_or_ThingtoBeWrapped ClosureCheck,
1515

16-
% do some work to go back to
17-
GRFSwP_or_ThingtoBeWrapped = GRFSwP,
16+
% do some work to go back to factories on a single subject
17+
private.lift-to-the-subject GRFSwP_or_ThingtoBeWrapped GRFSwP,
1818

1919
factories-provide GRFSwP PMLwP,
2020

@@ -713,17 +713,35 @@ if-coverage-not-good-error.one _ _. % new class is the first covering M
713713
% use that structure to synthesize the type of the wrapper, eg
714714
% hom belongs to Quiver, hence hom_isMon takes a "T of Quiver T"
715715

716-
pred product->triples i:term, o:list (w-args factoryname), o:bool.
717-
product->triples {{ lib:hb.prod lp:A lp:B }} L ClosureCheck :- !,
718-
product->triples B GRB ClosureCheck,
719-
product->triples A GRA _,
716+
% checks if the term is forall A B C, Factory ... (Op A B C) ...
717+
pred factory-on-some-structure-op? i:term, i:list term, o:gref, o:constant.
718+
factory-on-some-structure-op? (prod N Ty Bo) VS F OP :-
719+
@pi-decl N Ty x\
720+
factory-on-some-structure-op? (Bo x) [x|VS] F OP.
721+
factory-on-some-structure-op? T VS F OP :-
722+
factory? T (triple F _ Subject),
723+
coq.safe-dest-app Subject (global (const OP)) Args,
724+
exported-op _ _ OP,
725+
std.appendR _ {std.rev VS} Args.
726+
727+
kind factory-on-subject type.
728+
type factory-on-the-type w-args factoryname -> factory-on-subject.
729+
type factory-on-structure-op term -> factoryname -> constant -> factory-on-subject.
730+
731+
pred product->triples i:term, i:term, o:list factory-on-subject, o:bool.
732+
product->triples {{ lib:hb.prod lp:A lp:B }} T L ClosureCheck :- !,
733+
product->triples B T GRB ClosureCheck,
734+
product->triples A T GRA _,
720735
std.append GRA GRB L.
721-
product->triples {{ True }} [] tt :- !.
722-
product->triples {{ False }} [] ff :- !.
723-
product->triples A [GR] tt :- std.assert! (factory? A GR) "A structure can only mention known factories".
724-
% product->triples A [GR] tt :- factory-on-some-structure-op? A GR.
725-
726-
pred sigT->list-w-params i:term, o:list-w-params factoryname, o:bool.
736+
product->triples {{ True }} _ [] tt :- !.
737+
product->triples {{ False }} _ [] ff :- !.
738+
product->triples A T [factory-on-the-type F] tt :- factory? A F, F = triple _ _ T, !.
739+
product->triples A _ [factory-on-structure-op A F OP] tt :- factory-on-some-structure-op? A [] F OP, !.
740+
product->triples A T _ _ :-
741+
coq.error "HB: expecting a factory on" {coq.term->string T}
742+
"or a factory on a structure operation. Got:" {coq.term->string A}.
743+
744+
pred sigT->list-w-params i:term, o:w-params (list factory-on-subject), o:bool.
727745
sigT->list-w-params (fun N T B) L C :-
728746
coq.name->id N ID, % TODO: we should read the ID from the definition type which is an arity containing ids
729747
L = w-params.cons ID T Rest,
@@ -733,7 +751,28 @@ sigT->list-w-params {{ lib:@hb.sigT _ lp:{{ fun N Ty B }} }} L C :-
733751
coq.name->id N ID, % TODO: we should read the ID from the definition type which is an arity containing ids
734752
L = w-params.nil ID Ty Rest,
735753
@pi-decl N Ty t\
736-
product->triples (B t) (Rest t) C.
754+
product->triples (B t) t (Rest t) C.
755+
756+
pred lift-to-the-subject i:w-params (list factory-on-subject), o:list-w-params factoryname.
757+
lift-to-the-subject (w-params.cons ID T Rest) (w-params.cons ID T Rest1) :-
758+
@pi-parameter ID T x\
759+
lift-to-the-subject (Rest x) (Rest1 x).
760+
lift-to-the-subject (w-params.nil ID T Rest) (w-params.nil ID T Rest1) :-
761+
@pi-parameter ID T x\
762+
lift-to-the-subject.aux (Rest x) x (Rest1 x).
763+
lift-to-the-subject.aux [] _ [].
764+
lift-to-the-subject.aux [factory-on-the-type F|Rest] T [F|Rest1] :-
765+
lift-to-the-subject.aux Rest T Rest1.
766+
lift-to-the-subject.aux [factory-on-structure-op Expr F OP|Rest] T [WF|Rest1] :-
767+
wrapper-mixin Wrapper (const OP) F, !,
768+
factory-nparams Wrapper NParams,
769+
coq.mk-app {coq.env.global Wrapper} {std.append {coq.mk-n-holes NParams} [T]} W,
770+
factory? W WF,
771+
coq.say Expr "->" WF,
772+
lift-to-the-subject.aux Rest T Rest1.
773+
lift-to-the-subject.aux [factory-on-structure-op Expr _ _|_] _ _ :-
774+
coq.error "NYI: automatic wrapping for" {coq.term->string Expr}.
775+
737776

738777
pred mk-hb-eta.on i:structure, i:term, i:abbreviation,
739778
i:list term, i:name, i:term, i:A, o:term.

tests/monoid_enriched_cat.v

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,35 @@ Fail HB.structure
5151
HB.mixin Record hom_isMon T of Quiver T :=
5252
{ hom_isMon_private : forall A B, isMon (@hom T A B) }.
5353

54+
55+
Fail HB.structure
56+
Definition Monoid_enriched_quiver :=
57+
{ Obj of isQuiver Obj &
58+
(forall A B : Obj, isMon (hom A B)) }.
59+
60+
(* which one is best? *)
61+
Succeed HB.structure
62+
Definition Monoid_enriched_quiver :=
63+
{ Obj of isQuiver Obj &
64+
(forall A B : Obj, isMon (@hom (Quiver.clone Obj _) A B)) }.
65+
HB.structure
66+
Definition Monoid_enriched_quiver :=
67+
{ Obj of isQuiver Obj &
68+
(forall A B (* : Quiver.sort _ *), isMon (hom A B)) }.
69+
70+
5471
(* Print Canonical Projections. *)
5572
(* About hom_isMon.hom_isMon_private. *)
5673
(* About hom_isMon_private. *)
5774

5875
(* Step 2: at structure declaration, export the main and only projection
5976
of each declared wrapper as an instance of the wrapped structure on
60-
its subject *)
77+
its subject
6178
#[verbose] HB.structure
6279
Definition Monoid_enriched_quiver :=
6380
{ Obj of isQuiver Obj & hom_isMon Obj }.
64-
81+
*)
82+
6583
(* About hom_isMon.hom_isMon_private. *)
6684
(* About hom_isMon_private. *)
6785

0 commit comments

Comments
 (0)