Skip to content

Commit 9822b52

Browse files
authored
Merge pull request #135 from math-comp/fix-elab-params
Elaboration of parameters in HB.structure and HB.builders
2 parents f1a7698 + d916430 commit 9822b52

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

hb.elpi

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ if-MC-compat P :- get-option "mathcomp.axiom" S, !,
104104
P (some GR).
105105
if-MC-compat _.
106106

107-
108-
109107
% TODO: Should this only be used for gref that are factories? (and check in the first/second branch so?)
110108
% Should we make this an HO predicate, eg "located->gref S L is-factory? GR"
111109
pred located->gref i:string, i:list located, o:gref.
@@ -696,7 +694,7 @@ phant-fun-struct T Name S Params PF Out :- std.do! [
696694
mk-app (global S) Params SParams,
697695
mk-app SortProj Params SortProjParams,
698696
% Msg = {{lib:hb.nomsg}},
699-
Msg = {{lib:hb.some (lib:hb.pair "is not canonically a"%string lp:SParams)}},
697+
Msg = {{lib:hb.some (lib:hb.pair lib:hb.not_a_msg lp:SParams)}},
700698
(@pi-decl Name SParams s\ phant-fun-unify Msg T {mk-app SortProjParams [s]} (PF s) (UnifSI s)),
701699
phant-fun-implicit Name SParams UnifSI Out
702700
].
@@ -1853,31 +1851,39 @@ declare-old-constant (some C) :-
18531851
std.forall {coq.locate-all Id} (declare-old-located Id).
18541852
declare-old-constant _ :- true.
18551853

1856-
pred context->factory i:context-decl, o:factoryname.
1857-
context->factory (context-item IDT _ TTySkel none t\ context-item _ _ (TF t) none _\ context-end) GRF :- !,
1858-
coq.id->name IDT NameT,
1859-
std.assert-ok! (coq.elaborate-ty-skeleton TTySkel _ TTy) "context entry illtyped",
1860-
@pi-decl NameT TTy t\
1861-
std.assert! (factory? (TF t) (triple GRF _Params t))
1862-
"the last argument must be a factory applied to the type variable".
1863-
context->factory (context-item ID _ TSkel none Factories) GRF :- !,
1864-
coq.id->name ID Name,
1865-
std.assert-ok! (coq.elaborate-ty-skeleton TSkel _ T) "context entry illtyped",
1866-
@pi-decl Name T x\ context->factory (Factories x) GRF.
1867-
context->factory (context-item ID _ _ (some _) _) _ :-
1854+
pred elaborate-context-skel->factory i:context-decl, o:context-decl, o:factoryname, o:diagnostic.
1855+
elaborate-context-skel->factory
1856+
(context-item IDT IT TTySkel none t\ context-item IDF IF (TFSkel t) none _\ context-end)
1857+
(context-item IDT IT TTy none t\ context-item IDF IF (TFSkel t) none _\ context-end) GRF Diag
1858+
:- !, std.do-ok! Diag [
1859+
coq.elaborate-ty-skeleton TTySkel _ TTy,
1860+
(d\ coq.id->name IDT NameT),
1861+
(d\ @pi-decl NameT TTy t\ purge-id (TFSkel t) (TFSkel1 t), coq.elaborate-ty-skeleton (TFSkel1 t) _ (TF1 t) d),
1862+
(d\ @pi-decl NameT TTy t\ std.assert! (factory? (TF1 t) (triple GRF _Params t)) "the last argument must be a factory applied to the type variable"),
1863+
].
1864+
elaborate-context-skel->factory (context-item ID I TSkel none C) (context-item ID I T none C1) GRF Diag :- !, std.do-ok! Diag [
1865+
coq.elaborate-ty-skeleton TSkel _ T,
1866+
(d\ coq.id->name ID Name),
1867+
(d\ @pi-decl Name T x\ elaborate-context-skel->factory (C x) (C1 x) GRF d),
1868+
].
1869+
elaborate-context-skel->factory (context-item ID _ _ (some _) _) _ _ _ :-
18681870
coq.error "context item cannot be given a body:" ID.
18691871

1872+
pred purge-id i:term, o:term.
1873+
purge-id T T1 :-
1874+
(pi fresh t v\ copy {{lib:@hb.id lp:t lp:v}} fresh :- !) => copy T T1.
1875+
18701876
pred main-begin-declare-builders i:context-decl.
18711877
main-begin-declare-builders CtxSkel :- std.do! [
18721878
Name is "Builders_" ^ {term_to_string {new_int}}, % TODO?
1873-
context->factory CtxSkel GRF,
1879+
std.assert-ok! (elaborate-context-skel->factory CtxSkel Ctx GRF) "Context illtyped",
18741880
coq.env.begin-module Name none,
18751881
if (GRF = indt FRecord) (std.do! [
18761882
coq.env.begin-module "Super" none,
18771883
std.forall {coq.CS.canonical-projections FRecord} declare-old-constant,
18781884
coq.env.end-module _]) (true),
18791885
coq.env.begin-section Name,
1880-
builders-postulate-factories CtxSkel,
1886+
builders-postulate-factories Ctx,
18811887
].
18821888

18831889
pred postulate-factory-abbrev i:term, i:list term, i:id, i:factoryname, o:term.
@@ -1936,7 +1942,7 @@ builders-postulate-factories (context-item IDT _ TySkel none t\ context-item IDF
19361942

19371943
builders-postulate-factories (context-item ID _ TSkel none Factories) :- std.do! [
19381944
if-verbose (coq.say "HB: postulating" ID),
1939-
std.assert-ok! (coq.elaborate-ty-skeleton TSkel _ T) "builders-postulate-factorie: illtyped context",
1945+
std.assert-ok! (coq.elaborate-ty-skeleton TSkel _ T) "builders-postulate-factories: illtyped context",
19401946
if (var T) (coq.fresh-type T) true,
19411947
@local! => hb-add-const ID _ T @opaque! P, % no body, local -> a variable
19421948
TheParam = global (const P),

structures.v

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ Definition unify T1 T2 (t1 : T1) (t2 : T2) (s : option (string * Type)) :=
77
phantom T1 t1 -> phantom T2 t2.
88
Definition id_phant {T} {t : T} (x : phantom T t) := x.
99
Definition nomsg : option (string * Type) := None.
10+
Definition is_not_canonically_a : string := "is not canonically a".
1011

1112
Register unify as hb.unify.
1213
Register id_phant as hb.id.
1314
Register Coq.Init.Datatypes.None as hb.none.
1415
Register nomsg as hb.nomsg.
16+
Register is_not_canonically_a as hb.not_a_msg.
1517
Register Coq.Init.Datatypes.Some as hb.some.
1618
Register Coq.Init.Datatypes.pair as hb.pair.
1719
Register Coq.Init.Datatypes.prod as hb.prod.
@@ -290,7 +292,8 @@ sigT->list-w-params {{ lib:@hb.sigT _ lp:{{ fun N Ty B }} }} L C :-
290292
product->triples (B t) (Rest t) C.
291293

292294
main [const-decl Module (some B) _] :- !, std.do! [
293-
sigT->list-w-params B GRFS ClosureCheck, !,
295+
purge-id B B1, std.assert-ok! (coq.elaborate-skeleton B1 _ B2) "illtyped structure definition",
296+
sigT->list-w-params B2 GRFS ClosureCheck, !,
294297
with-attributes (main-declare-structure Module GRFS ClosureCheck),
295298
].
296299
main _ :- coq.error "Usage: HB.structure Definition <ModuleName> := { A of <Factory1> A & … & <FactoryN> A }".
@@ -507,6 +510,9 @@ Elpi Typecheck.
507510
Notation "`Error_cannot_unify: t1 'with' t2" := (unify t1 t2 None)
508511
(at level 0, format "`Error_cannot_unify: t1 'with' t2", only printing) :
509512
form_scope.
513+
Notation "`Error: t `is_not_canonically_a T" := (unify t _ (Some (is_not_canonically_a, T)))
514+
(at level 0, T at level 0, format "`Error: t `is_not_canonically_a T", only printing) :
515+
form_scope.
510516
Notation "`Error: t msg T" := (unify t _ (Some (msg%string, T)))
511517
(at level 0, msg, T at level 0, format "`Error: t msg T", only printing) :
512518
form_scope.

tests/subtype.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ SubK : forall x Px, val (@Sub x Px) = x
2222

2323
HB.structure Definition SUB (T : Type) (P : pred T) := { S of is_SUB T P S }.
2424

25-
HB.structure Definition SubInhab T P := { sT of is_inhab T & is_SUB T P sT }.
25+
HB.structure Definition SubInhab (T : Type) P := { sT of is_inhab T & is_SUB T P sT }.
2626

2727
HB.structure Definition SubNontrivial T P := { sT of is_nontrivial sT & is_SUB T P sT }.
2828

0 commit comments

Comments
 (0)