Skip to content

Commit 7b4463d

Browse files
authored
Merge pull request #147 from math-comp/exports
Managing Exports
2 parents 6230b10 + b75c174 commit 7b4463d

File tree

4 files changed

+144
-9
lines changed

4 files changed

+144
-9
lines changed

_CoqProject

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ tests/duplicate_structure.v
5252
tests/instance_params_no_type.v
5353
tests/test_CS_db_filtering.v
5454
tests/subtype.v
55+
tests/exports.v
5556

5657
-R . HB

hb.elpi

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,13 +1241,20 @@ main-declare-builder (builder _ SrcFactory TgtMixin B) FromClauses MoreFromClaus
12411241
].
12421242

12431243
% [export Module] exports a Module now adds it to the collection of
1244-
% modules to export in the end
1244+
% modules to export in the end of the current enclosing module,
1245+
% by the command HB.Exports
1246+
12451247
pred to-export o:modpath.
12461248
pred export i:modpath.
12471249
export Module :- !,
12481250
coq.env.export-module Module,
12491251
acc current (clause _ _ (to-export Module)).
12501252

1253+
pred main-reexport.
1254+
main-reexport :- !,
1255+
std.findall (to-export Module_) Mods,
1256+
std.forall Mods (x\ sigma y\ to-export y = x, coq.env.export-module y).
1257+
12511258
pred mk-mixin-fun i:list-w-params mixinname, i:term, o:term.
12521259
mk-mixin-fun ML X MLX :- mk-mixin-fun.then ML (p\ t\ body\ body = X) MLX.
12531260

@@ -1673,11 +1680,6 @@ main-declare-structure Module GRFSwP ClosureCheck :- std.do! [
16731680

16741681
coq.env.begin-module "Exports" none,
16751682
declare-sort-coercion Structure SortProjection,
1676-
if-verbose (coq.say "HB: exporting operations"),
1677-
ClassAlias => ClassRequires => Factories =>
1678-
export-operations Structure SortProjection ClassProjection MLwP [] EX MLToExport,
1679-
% TODO: issue an Arguments op T : rename, where T is the name written by
1680-
% the user in Definition foo := { T of ... }
16811683

16821684
if-verbose (coq.say "HB: exporting unification hints"),
16831685
ClassAlias => ClassRequires => Factories =>
@@ -1693,10 +1695,8 @@ main-declare-structure Module GRFSwP ClosureCheck :- std.do! [
16931695
(coq.say "declare-structure:" ClassName "should be an inductive", fail),
16941696

16951697
if-verbose (coq.say "HB: accumulating various props"),
1696-
std.map MLToExport (m\r\ r = mixin-first-class m ClassName) MixinFirstClass,
16971698
std.flatten [
1698-
MixinFirstClass, EX, Factories,
1699-
[ClassRequires], [ClassAlias], [is-structure Structure],
1699+
Factories, [ClassRequires], [ClassAlias], [is-structure Structure],
17001700
NewJoins, [class-def CurrentClass]
17011701
]
17021702
NewClauses,
@@ -1711,6 +1711,20 @@ main-declare-structure Module GRFSwP ClosureCheck :- std.do! [
17111711

17121712
export Exports,
17131713

1714+
if-verbose (coq.say "HB: exporting operations"),
1715+
ClassAlias => ClassRequires => Factories =>
1716+
export-operations Structure SortProjection ClassProjection MLwP [] EX MLToExport,
1717+
% TODO: issue an Arguments op T : rename, where T is the name written by
1718+
% the user in Definition foo := { T of ... }
1719+
1720+
if-verbose (coq.say "HB: start operations meta-data module ElpiOperations"),
1721+
ElpiOperationModName is "ElpiOperations" ^ {std.any->string {new_int}},
1722+
coq.env.begin-module ElpiOperationModName none,
1723+
std.map MLToExport (m\r\ r = mixin-first-class m ClassName) MixinFirstClass,
1724+
std.forall {std.append EX MixinFirstClass} (c\ acc current (clause _ _ c)),
1725+
coq.env.end-module ElpiOperations,
1726+
export ElpiOperations,
1727+
17141728
declare-factory-abbrev Module (factory-by-classname ClassName),
17151729

17161730
NewClauses => if-MC-compat (mc-compat-structure Module ModulePath MLToExport {w-params.nparams MLwP} ClassName ClassProjection),

structures.v

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,51 @@ main _ :- coq.error "Usage: HB.end.".
454454
Elpi Typecheck.
455455
Elpi Export HB.end.
456456

457+
(* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *)
458+
(* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *)
459+
(* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *)
460+
461+
(** [HB.export Modname] does the work of [Export Modname] but also schedules [Modname]
462+
to be exported later on, when [HB.reexport] is called.
463+
Note that the list of modules to be exported is stored in the current module,
464+
hence the recommended way to do is
465+
<<<
466+
Module Algebra.
467+
HB.mixin .... HB.structure ...
468+
Module MoreExports. ... End MoreExports. HB.export MoreExports.
469+
...
470+
Module Export. HB.reexport. End Exports.
471+
End Algebra.
472+
Export Algebra.Exports.
473+
>>> *)
474+
475+
Elpi Command HB.export.
476+
Elpi Accumulate File "hb.elpi".
477+
Elpi Accumulate Db hb.db.
478+
Elpi Accumulate lp:{{
479+
main [str M] :- !, with-attributes (export {coq.locate-module M}).
480+
main _ :- coq.error "Usage: HB.export M.".
481+
}}.
482+
Elpi Typecheck.
483+
Elpi Export HB.export.
484+
485+
(* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *)
486+
(* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *)
487+
(* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *)
488+
489+
(** [HB.reexport] Exports all modules that were previously exported via [HB.export].
490+
It is useful to create one big module with all exports at the end of a file. *)
491+
492+
Elpi Command HB.reexport.
493+
Elpi Accumulate File "hb.elpi".
494+
Elpi Accumulate Db hb.db.
495+
Elpi Accumulate lp:{{
496+
main [] :- !, with-attributes (main-reexport).
497+
main _ :- coq.error "Usage: HB.reexport.".
498+
}}.
499+
Elpi Typecheck.
500+
Elpi Export HB.reexport.
501+
457502
(*
458503
459504
(* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *)

tests/exports.v

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
From Coq Require Import ssreflect ssrfun.
2+
From HB Require Import structures.
3+
4+
Module Enclosing.
5+
(**************************************************************************)
6+
(* Stage 0: +Ring+ *)
7+
(**************************************************************************)
8+
9+
HB.mixin Record Ring_of_TYPE A := {
10+
zero : A;
11+
one : A;
12+
add : A -> A -> A;
13+
opp : A -> A;
14+
mul : A -> A -> A;
15+
addrA : associative add;
16+
addrC : commutative add;
17+
add0r : left_id zero add;
18+
addNr : left_inverse zero opp add;
19+
mulrA : associative mul;
20+
mul1r : left_id one mul;
21+
mulr1 : right_id one mul;
22+
mulrDl : left_distributive mul add;
23+
mulrDr : right_distributive mul add;
24+
}.
25+
HB.structure Definition Ring := { A of Ring_of_TYPE A }.
26+
27+
(* Notations *)
28+
29+
Module RingExports.
30+
Declare Scope hb_scope.
31+
Delimit Scope hb_scope with G.
32+
Local Open Scope hb_scope.
33+
Notation "0" := zero : hb_scope.
34+
Notation "1" := one : hb_scope.
35+
Infix "+" := (@add _) : hb_scope.
36+
Notation "- x" := (@opp _ x) : hb_scope.
37+
Infix "*" := (@mul _) : hb_scope.
38+
Notation "x - y" := (x + - y) : hb_scope.
39+
End RingExports.
40+
HB.export RingExports.
41+
42+
(* Theory *)
43+
44+
Section Theory.
45+
Local Open Scope hb_scope.
46+
Variable R : Ring.type.
47+
Implicit Type (x : R).
48+
49+
Lemma addr0 : right_id (@zero R) add.
50+
Proof. by move=> x; rewrite addrC add0r. Qed.
51+
52+
Lemma addrN : right_inverse (@zero R) opp add.
53+
Proof. by move=> x; rewrite addrC addNr. Qed.
54+
55+
Lemma subrr x : x - x = 0.
56+
Proof. by rewrite addrN. Qed.
57+
58+
Lemma addrNK x y : x + y - y = x.
59+
Proof. by rewrite -addrA subrr addr0. Qed.
60+
61+
End Theory.
62+
63+
Module Exports.
64+
HB.reexport.
65+
End Exports.
66+
End Enclosing.
67+
68+
(* We miss the coercions, canonical and elpi metadata *)
69+
Fail Check forall (R : Enclosing.Ring.type) (x : R), x = x.
70+
Fail Check 0%G.
71+
72+
Export Enclosing.Exports.
73+
74+
Check forall (R : Enclosing.Ring.type) (x : R), x = x.
75+
Check 0%G.

0 commit comments

Comments
 (0)