Skip to content

Commit 021e2bc

Browse files
authored
Merge pull request #179 from math-comp/demo-2021-03-05
hierarchies of morphism
2 parents 275c9f6 + e99487e commit 021e2bc

File tree

13 files changed

+167
-42
lines changed

13 files changed

+167
-42
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Makefile.test-suite.coq.conf
3939
.coqdeps.test-suite
4040
*.v.hb
4141
*.v.hb.old
42-
hb
42+
coq.hb
4343
HB/common/log.compat.elpi
4444

4545
_minted-*

Changelog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
(re)declared by `HB.reexport`.
77
- `#[compress_coercions]` attribute (off by default) to shorten coercions paths
88
in the synthesis of instances.
9+
- `HB.check` is like `Check` but supports logging and can be disabled on a
10+
selection of Coq versions.
11+
- Experimental support for structures with a function as the carrier, that is
12+
hierarchies of morphisms.
913
- `HB.instance K F` deprecated in favor of `HB.instance Definition`
1014
- `#[log]` and `#[log(raw)]` to get printed Coq commands equivalent to what HB
1115
is doing. The raw print has higher changes to be reparsable.
@@ -17,8 +21,9 @@
1721
the key of the mixin/factory
1822
- `#[infer(P)]` can be used to tell `HB.structure` to set things up so that
1923
parameter `P` is automatically inferred. E.g. if `P : Ring.type` then
20-
`Structure.type` will take a `t : Type` and trigger a canonical inference
24+
`Structure.type` will take a `t : Type` and trigger a canonical inference.
2125
to infer the `t_is_a_Ring : Ring.type` associated to `t`.
26+
If `Structure` has a function carrier, one has to write `#[infer(P = "_ -> _")]`.
2227

2328
## [1.0.0] - 2020-12-16
2429

HB/builders.elpi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ pred postulate-factories i:id, i:context-decl.
147147
postulate-factories ModName (context-item IDT _ TySkel none t\ context-item IDF _ (TF t) none _\ context-end) :- !, std.do! [
148148
% TODO we should allow T to be anything.
149149
std.assert-ok! (coq.elaborate-ty-skeleton TySkel _ Ty) "builders-postulate-factory: illtyped context",
150-
if (var Ty) (fresh-type Ty)
151-
(std.assert-ok! (coq.unify-eq Ty {{Type}}) "The last context item before the factory must be a type variable"),
150+
if (var Ty) (fresh-type Ty) true,
152151
if-verbose (coq.say "HB: postulating type" IDT),
153152
log.coq.env.add-section-variable-noimplicits IDT Ty C,
154153
TheType = global (const C),

HB/common/log.compat.current.elpi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ log.private.log-vernac _.
3535

3636
shorten log.private.coq.vernac.{ begin-module , end-module , begin-section, end-section }.
3737
shorten log.private.coq.vernac.{ import-module , export-module }.
38-
shorten log.private.coq.vernac.{ definition , variable , comment }.
38+
shorten log.private.coq.vernac.{ definition , variable , comment , check }.
3939
shorten log.private.coq.{ vernac.inductive , vernac.implicit }.
4040
shorten log.private.coq.vernac.{ canonical , abbreviation , notation , coercion }.
4141
shorten log.private.{ coq.vernac }.
@@ -100,6 +100,8 @@ coq.vernac->pp1 (vernac.implicit Local Name [L]) (box h [Locality, str "Argument
100100
std.intersperse spc PP1 PP.
101101
coq.vernac->pp1 (comment A) (box (hov 2) [str"(*", str S, str"*)"]) :-
102102
std.any->string A S.
103+
coq.vernac->pp1 (check T) (box (hov 2) [str"Check", spc, PPT, str"."]) :-
104+
@holes! => coq.term->pp T PPT.
103105

104106
pred local->locality i:bool, o:coq.pp.
105107
local->locality tt (str "Local ").

HB/common/log.elpi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ log.coq.env.accumulate S DB CL :- std.do! [
125125
if-verbose (log.private.log-vernac (log.private.coq.vernac.comment CL)),
126126
].
127127

128+
pred log.coq.check i:term, o:term, o:term.
129+
log.coq.check Skel Ty T :- std.do! [
130+
std.assert-ok! (coq.elaborate-skeleton Skel Ty T) "Check fails",
131+
log.private.log-vernac (log.private.coq.vernac.check Skel),
132+
].
133+
128134
namespace log.private {
129135

130136
%%%%% Logging Utils %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -160,6 +166,7 @@ type coq.vernac.coercion string -> gref -> class -> coq.vernac.
160166
type coq.vernac.canonical string -> coq.vernac.
161167
type coq.vernac.implicit bool -> string -> list (list implicit_kind) -> coq.vernac.
162168
type coq.vernac.comment A -> coq.vernac.
169+
type coq.vernac.check term -> coq.vernac.
163170

164171
}
165172

HB/common/phant-abbreviation.elpi

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,16 @@ fun-implicit N Ty (t\private.phant-term AL (F t))
7777
% [fun-infer-type N T Ph Ph1] Adds an argument N of type T such that one passes
7878
% a value V of type {{ Type }} the corresponding canonical VC of type T is passed
7979
% for N , eg `fun T (phT : phant T) => Ph`
80-
pred fun-infer-type i:name, i:term, i:(term -> phant-term), o:phant-term.
81-
fun-infer-type N Ty (t\private.phant-term AL (Bo t)) Out :-
80+
pred fun-infer-type i:class, i:name, i:term, i:(term -> phant-term), o:phant-term.
81+
fun-infer-type sortclass N Ty (t\private.phant-term AL (Bo t)) Out :-
8282
coq.name-suffix N "ph" PhN,
83-
fun-implicit N Ty (t\private.phant-term [private.infer-type N|AL] (fun PhN {{ lib:@hb.phant lp:t }} _\ Bo t)) Out.
83+
fun-implicit N Ty (t\private.phant-term [private.infer-type N sortclass|AL]
84+
(fun PhN {{ lib:@hb.phant lp:t }} _\ Bo t)) Out.
85+
fun-infer-type funclass N Ty (t\private.phant-term AL (Bo t)) Out :-
86+
coq.name-suffix N "ph" PhN,
87+
fun-implicit N Ty (t\private.phant-term [private.infer-type N funclass|AL]
88+
(fun PhN {{ lib:@hb.phantom (_ -> _) lp:t }} _\ Bo t)) Out.
89+
fun-infer-type C _ _ _ _ :- coq.error "HB: inference of parameters of call" C "not implemented yet".
8490

8591
% TODO: this looks like a hack to remove
8692
pred append-fun-unify i:phant-term, o:phant-term.
@@ -104,7 +110,7 @@ type phant-term list phant-arg -> term -> phant-term.
104110
% - [Phant x] using [infer-type]
105111
kind phant-arg type.
106112
type real name -> phant-arg.
107-
type infer-type name -> phant-arg.
113+
type infer-type name -> class -> phant-arg.
108114
type implicit phant-arg.
109115
type unify phant-arg.
110116

@@ -162,9 +168,13 @@ build-abbreviation K F [] K F.
162168
build-abbreviation K F [real N|AL] K'' (fun N _ AbbrevFx) :- !,
163169
pi x\ build-abbreviation K {mk-app F [x]} AL K' (AbbrevFx x),
164170
K'' is K' + 1.
165-
build-abbreviation K F [infer-type N|AL] K'' (fun N _ AbbrevFx) :- !,
171+
build-abbreviation K F [infer-type N sortclass|AL] K'' (fun N _ AbbrevFx) :- !,
166172
pi x\ build-abbreviation K {mk-app F [{{ lib:hb.Phant lp:x }}]} AL K' (AbbrevFx x),
167173
K'' is K' + 1.
174+
build-abbreviation K F [infer-type N funclass|AL] K'' (fun N _ AbbrevFx) :- !,
175+
pi x\ build-abbreviation K {mk-app F [{{ lib:hb.Phantom (_ -> _) lp:x }}]} AL K' (AbbrevFx x),
176+
K'' is K' + 1.
177+
build-abbreviation _ _ [infer-type _ (grefclass _)|_] _ _ :- coq.error "TODO".
168178
build-abbreviation K F [implicit|AL] K' FAbbrev :- !,
169179
build-abbreviation K {mk-app F [_]} AL K' FAbbrev.
170180
build-abbreviation K F [unify|AL] K' FAbbrev :- !,
@@ -240,4 +250,4 @@ mk-phant-term.classes EtaF CNF PL T MLwA PhF :- !, std.do! [
240250
].
241251

242252

243-
}}
253+
}}

HB/common/stdpp.elpi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pred copy-constructor i:indc-decl, o:indc-decl.
133133
copy-constructor (constructor ID A) (constructor ID A1) :- copy-arity A A1.
134134

135135
% TODO: move to coq-elpi proper
136-
pred coq.gref.list->set i:list mixinname, o:coq.gref.set.
136+
pred coq.gref.list->set i:list gref, o:coq.gref.set.
137137
coq.gref.list->set L S :-
138138
std.fold L {coq.gref.set.empty} coq.gref.set.add S.
139139

HB/common/utils.elpi

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ with-attributes P :-
3434
att "log.raw" bool,
3535
att "compress_coercions" bool,
3636
att "export" bool,
37+
att "skip" string,
3738
] Opts, !,
3839
Opts => P.
3940

@@ -88,7 +89,7 @@ nice-gref->string X Mod :-
8889
nice-gref->string X S :-
8990
coq.term->string (global X) S.
9091

91-
pred gref->modname i:mixinname, o:id.
92+
pred gref->modname i:gref, o:id.
9293
gref->modname GR ModName :-
9394
coq.gref->path GR Path,
9495
if (std.rev Path [_,ModName|_]) true (coq.error "No enclosing module for " GR).
@@ -162,9 +163,11 @@ w-params.fold.params (w-params.cons ID PTy F) Cons Nil RevPs Out :- !, std.do! [
162163
coq.id->name ID N,
163164
(@pi-decl N PTy p\ w-params.fold.params (F p) Cons Nil [p|RevPs] (Body p)),
164165
Cons N PTy Body Out].
165-
w-params.fold.params (w-params.nil ID TTy F) _ Nil RevParams Out :- !,
166+
w-params.fold.params (w-params.nil ID TTy F) _ Nil RevParams Out :- !, std.do! [
166167
coq.id->name ID N,
167-
std.rev RevParams Params, !, Nil Params N TTy F Out.
168+
std.rev RevParams Params,
169+
Nil Params N TTy F Out,
170+
].
168171

169172
% [w-params.then AwP Cons Nil Out] states that Out has shape
170173
% Cons `x_1` T_1 p_1 \ .. \ Nil [p_1 .. p_n] `T` Ty t \ Body

HB/structure.elpi

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ declare Module B :- std.do! [
6161
if-verbose (coq.say "HB: start module Exports"),
6262

6363
log.coq.env.begin-module "Exports",
64-
if-arg-sort (private.declare-sort-coercion Structure (global (const ArgSortCst))),
65-
private.declare-sort-coercion Structure SortProjection,
64+
65+
private.infer-coercion-tgt MLwP CoeClass,
66+
if-arg-sort (private.declare-sort-coercion CoeClass Structure (global (const ArgSortCst))),
67+
private.declare-sort-coercion CoeClass Structure SortProjection,
6668

6769
if-verbose (coq.say "HB: exporting unification hints"),
6870
ClassAlias => Factories => GRDepsClauses =>
@@ -92,7 +94,7 @@ declare Module B :- std.do! [
9294

9395
if-verbose (coq.say "HB: declaring on_ abbreviation"),
9496

95-
private.mk-infer-key ClassProjection NilwP (global Structure) PhClass,
97+
private.mk-infer-key CoeClass ClassProjection NilwP (global Structure) PhClass,
9698

9799
phant.add-abbreviation "on_" PhClass _ ClassOfAbbrev,
98100
(pi c\ coq.notation.abbreviation ClassOfAbbrev [c] (ClassOfAbbrev_ c)),
@@ -101,13 +103,12 @@ declare Module B :- std.do! [
101103

102104
coq.mk-app (global ClassName) {params->holes NilwP} AppClassHoles,
103105
@global! => log.coq.notation.add-abbreviation "Build" 2
104-
{{fun T C => (lp:(ClassOfAbbrev_ C) : (lp:AppClassHoles T))}} tt ClassForAbbrev,
106+
{{fun T C => (lp:(ClassOfAbbrev_ C) : (lp:AppClassHoles T)) }} tt _,
105107

106108
if-verbose (coq.say "HB: declaring on abbreviation"),
107109

108-
(pi t\ coq.notation.abbreviation ClassForAbbrev [t, t] (ClassForAbbrevDiag t)),
109110
@global! => log.coq.notation.add-abbreviation "on" 1
110-
{{fun T => lp:(ClassForAbbrevDiag T)}} tt _,
111+
{{fun T => (lp:{{ ClassOfAbbrev_ {{_}} }} : (lp:AppClassHoles T)) }} tt _,
111112

112113
log.coq.env.end-module-name Module ModulePath,
113114

@@ -146,6 +147,14 @@ namespace private {
146147

147148
shorten coq.{ term->gref, subst-fun, safe-dest-app, mk-app, mk-eta, subst-prod }.
148149

150+
151+
pred infer-coercion-tgt i:list-w-params mixinname, o:class.
152+
infer-coercion-tgt (w-params.cons ID Ty F) CoeClass :-
153+
@pi-parameter ID Ty x\ infer-coercion-tgt (F x) CoeClass.
154+
infer-coercion-tgt (w-params.nil _ {{ Type }} _) sortclass.
155+
infer-coercion-tgt (w-params.nil _ {{ _ -> _ }} _) funclass.
156+
infer-coercion-tgt (w-params.nil _ _ _) _ :- coq.error "TODO1".
157+
149158
% const Po : forall p1 .. pm T m1 .. mn, Extra (Eg Extra = forall x y, x + y = y + z)
150159
% const C : forall p1 .. pm s, Extra
151160
% Po P1 .. PM T M1 .. MN PoArgs -> C P1 .. PM S PoArgs
@@ -166,7 +175,7 @@ clean-op-ty [exported-op _ Po C|Ops] S T1 T2 :-
166175

167176
pred operation-body-and-ty i:list prop, i:constant, i:structure, i:term, i:term,
168177
i:list term, i:term, i:w-args A, o:pair term term.
169-
operation-body-and-ty EXI Poperation Struct Psort Pclass Params _T (triple _ Params _) (pr Bo Ty) :- std.do! [
178+
operation-body-and-ty EXI Poperation Struct Psort Pclass Params _T (triple _ ParamsOp _) (pr Bo Ty) :- std.do! [
170179
mk-app (global Struct) Params StructType,
171180
mk-app Psort Params PsortP,
172181
mk-app Pclass Params PclassP,
@@ -177,7 +186,7 @@ operation-body-and-ty EXI Poperation Struct Psort Pclass Params _T (triple _ Par
177186
mk-app PclassP [s] Class,
178187
synthesis.under-mixin-src-from-factory.do! Carrier Class [
179188
% just in case..
180-
synthesis.infer-all-mixin-args Params Carrier (const Poperation) (Body s),
189+
synthesis.infer-all-mixin-args ParamsOp Carrier (const Poperation) (Body s),
181190
std.assert-ok! (coq.typecheck (Body s) (DirtyTy s)) "export-1-operation: Body illtyped",
182191
clean-op-ty EXI s (DirtyTy s) (BodyTy s),
183192
],
@@ -272,7 +281,7 @@ mk-coe-structure-body StructureF StructureT TC Coercion SortProjection ClassProj
272281
{coq.mk-n-holes {factory-nparams TC}} PackPH,
273282

274283
SCoeBody = {{ fun s : lp:StructureP =>
275-
let T : Type := lp:SortP s in
284+
let T := lp:SortP s in
276285
lp:PackPH T (lp:CoercionP T (lp:ClassP s)) }},
277286
].
278287

@@ -461,13 +470,13 @@ declare-class+structure MLwP (indt ClassInd) (indt StructureInd) SortProjection
461470
global (const ClassP) = ClassProjection,
462471
].
463472

464-
% Declares "sort" as a coercion Structurename >-> Sortclass
465-
pred declare-sort-coercion i:structure, i:term.
466-
declare-sort-coercion StructureName (global Proj) :-
473+
% Declares "sort" as a Coercion Proj : Structurename >-> CoeClass.
474+
pred declare-sort-coercion i:class, i:structure, i:term.
475+
declare-sort-coercion CoeClass StructureName (global Proj) :-
467476

468477
if-verbose (coq.say "HB: declare sort coercion"),
469478

470-
log.coq.coercion.declare (coercion Proj 0 StructureName sortclass).
479+
log.coq.coercion.declare (coercion Proj 0 StructureName CoeClass).
471480

472481
pred if-class-already-exists-error i:id, i:list class, i:list mixinname.
473482
if-class-already-exists-error _ [] _.
@@ -566,19 +575,22 @@ pred mk-infer i:term, i:list-w-params mixinname, o:phant-term.
566575
mk-infer T (w-params.nil _ _ _) PH :- phant.init T PH.
567576
mk-infer T (w-params.cons ID Ty W) R :- (get-option ID "Type" ; get-option ID ""), !,
568577
@pi-parameter ID Ty t\ mk-infer {mk-app T [t]} (W t) (PhT t),
569-
phant.fun-infer-type {coq.id->name ID} Ty PhT R.
578+
phant.fun-infer-type sortclass {coq.id->name ID} Ty PhT R.
579+
mk-infer T (w-params.cons ID Ty W) R :- (get-option ID "_ -> _"), !,
580+
@pi-parameter ID Ty t\ mk-infer {mk-app T [t]} (W t) (PhT t),
581+
phant.fun-infer-type funclass {coq.id->name ID} Ty PhT R.
570582
mk-infer T (w-params.cons ID Ty W) R :- not (get-option ID _), !,
571583
@pi-parameter ID Ty t\ mk-infer {mk-app T [t]} (W t) (PhT t),
572584
phant.fun-real {coq.id->name ID} Ty PhT R.
573585
mk-infer _ (w-params.cons ID _ _) _ :- get-option ID Infer,
574586
coq.error "Automatic inference of paramter" ID "from" Infer "not supported".
575587

576-
pred mk-infer-key i:term, i:list-w-params mixinname, i:term, o:phant-term.
577-
mk-infer-key K (w-params.nil ID _ _) St PhK :-
588+
pred mk-infer-key i:class, i:term, i:list-w-params mixinname, i:term, o:phant-term.
589+
mk-infer-key CoeClass K (w-params.nil ID _ _) St PhK :-
578590
@pi-parameter ID St t\ phant.init {mk-app K [t]} (PhKBo t),
579-
phant.fun-infer-type {coq.id->name ID} St PhKBo PhK.
580-
mk-infer-key K (w-params.cons ID Ty W) St R :-
581-
@pi-parameter ID Ty t\ mk-infer-key {mk-app K [t]} (W t) {mk-app St [t]} (PhT t),
591+
phant.fun-infer-type CoeClass {coq.id->name ID} St PhKBo PhK.
592+
mk-infer-key CoeClass K (w-params.cons ID Ty W) St R :-
593+
@pi-parameter ID Ty t\ mk-infer-key CoeClass {mk-app K [t]} (W t) {mk-app St [t]} (PhT t),
582594
phant.fun-implicit {coq.id->name ID} Ty PhT R.
583595

584596
pred if-coverage-not-good-error i:list mixinname.

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ Proof. by rewrite example. Qed.
7171

7272
The software is beta-quality, it works but error messages should be improved.
7373

74-
The current version forces the carrier to be a type, ruling hierarchies of morphisms out.
75-
7674
This [draft paper](https://hal.inria.fr/hal-02478907) describes the language
7775
in full detail.
7876

0 commit comments

Comments
 (0)