Skip to content

Commit 2ddc066

Browse files
committed
[api] coq.env.module precise description of contents
1 parent 7c37d15 commit 2ddc066

File tree

8 files changed

+119
-21
lines changed

8 files changed

+119
-21
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Change `coq.env.module` and `coq.env.module-type` do not fail if the
77
module (type) contains a mutual inductive. The resulting `gref` is going
88
to me unusable with most APIs, though.
9+
- Change `coq.env.module` returns a ADT describing the module contents
910
- Change `coq.gref->path` and `coq.gref->id` do work on `gref` which point
1011
to mutual inductives.
1112
- New `coq.env.term-dependencies` computing all the `grefs` occurring in a term.

apps/locker/elpi/locker.elpi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ make-key-unlockable ID DefBo Ty LockedDef Key :- std.do! [
106106

107107
pred make-module-unlockable i:id, i:modpath.
108108
make-module-unlockable ID Module :- std.do! [
109-
coq.env.module Module [_,UnlockEQ],
109+
coq.env.module Module [_,gref UnlockEQ],
110110
coq.env.global UnlockEQ UnlockEQT,
111111
Unlock = {{ Unlockable lp:UnlockEQT }},
112112
make-unlockable ID Unlock,

coq-builtin.elpi

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,17 @@ external pred coq.locate-module i:id, o:modpath.
679679
% error if ModName cannot be located. *E*
680680
external pred coq.locate-module-type i:id, o:modtypath.
681681

682+
% Contents of a module
683+
kind module-item type.
684+
type submodule modpath -> list module-item -> module-item.
685+
type module-type modtypath -> module-item.
686+
type gref gref -> module-item.
687+
type module-functor modpath -> list modtypath -> module-item.
688+
type module-type-functor modtypath -> list modtypath -> module-item.
689+
682690
% [coq.env.module MP Contents] lists the contents of a module (recurses on
683691
% submodules) *E*
684-
external pred coq.env.module i:modpath, o:list gref.
692+
external pred coq.env.module i:modpath, o:list module-item.
685693

686694
% [coq.env.module-type MTP Entries] lists the items made visible by module
687695
% type (does not recurse on submodules) *E*

examples/example_reduction_surgery.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ gref->redflag (const C) (coq.redflags.const C).
1414
solve (goal _ _ Ty _ [str M] as G) GS :-
1515
coq.locate-module M MP,
1616
coq.env.module MP GREFS,
17-
std.filter GREFS (x\x = const _) CONSTANTS,
17+
std.map-filter GREFS (x\r\x = gref r, r = (const _)) CONSTANTS,
1818
std.map CONSTANTS (gr\r\ coq.env.transitive-dependencies gr _ r) DEPS,
1919
std.fold DEPS {coq.gref.set.empty} coq.gref.set.union ALLDEPS,
2020
std.append CONSTANTS {coq.gref.set.elements ALLDEPS} All,

src/coq_elpi_HOAS.ml

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3413,15 +3413,32 @@ let lp2skeleton ~depth coq_ctx constraints state t =
34133413

34143414
(* {{{ Declarations.module_body -> elpi ********************************** *)
34153415

3416-
let rec in_elpi_module_item ~depth path state (name, item) = match item with
3417-
| Declarations.SFBconst _ ->
3418-
[GlobRef.ConstRef (Constant.make2 path name)]
3419-
| Declarations.SFBmind { Declarations.mind_packets } ->
3420-
CList.init (Array.length mind_packets) (fun i -> GlobRef.IndRef (MutInd.make2 path name,i))
3421-
| Declarations.SFBmodule mb -> in_elpi_module ~depth state mb
3422-
| Declarations.SFBmodtype _ -> []
3423-
3424-
and in_elpi_module : 'a. depth:int -> API.Data.state -> 'a Declarations.generic_module_body -> GlobRef.t list =
3416+
type module_item =
3417+
| Module of Names.ModPath.t * module_item list
3418+
| ModuleType of Names.ModPath.t
3419+
| Gref of Names.GlobRef.t
3420+
| Functor of Names.ModPath.t * Names.ModPath.t list
3421+
| FunctorType of Names.ModPath.t * Names.ModPath.t list
3422+
3423+
let rec in_elpi_module_item ~depth path state (name, item) =
3424+
let open Declarations in
3425+
match item with
3426+
| SFBconst _ ->
3427+
[Gref (GlobRef.ConstRef (Constant.make2 path name))]
3428+
| SFBmind { mind_packets } ->
3429+
CList.init (Array.length mind_packets) (fun i -> Gref (GlobRef.IndRef (MutInd.make2 path name,i)))
3430+
| SFBmodule ({ mod_mp; mod_type = NoFunctor _ } as b) -> [Module (mod_mp,in_elpi_module ~depth state b) ]
3431+
| SFBmodule { mod_mp; mod_type = MoreFunctor _ as l } -> [Functor(mod_mp,functor_params l)]
3432+
| SFBmodtype { mod_mp; mod_type = NoFunctor _ } -> [ModuleType mod_mp]
3433+
| SFBmodtype { mod_mp; mod_type = MoreFunctor _ as l } -> [FunctorType (mod_mp,functor_params l)]
3434+
3435+
and functor_params x =
3436+
let open Declarations in
3437+
match x with
3438+
| MoreFunctor(_,{ mod_type_alg = Some (NoFunctor (MEident mod_mp)) },rest) -> mod_mp :: functor_params rest
3439+
| _ -> [] (* XXX non trivial functors, eg P : X with type a = nat, are badly described (no params) *)
3440+
3441+
and in_elpi_module : 'a. depth:int -> API.Data.state -> 'a Declarations.generic_module_body -> module_item list =
34253442
fun ~depth state { Declarations.
34263443
mod_mp; (* Names.module_path *)
34273444
mod_expr; (* Declarations.module_implementation *)

src/coq_elpi_HOAS.mli

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,14 @@ val modtypath : Names.ModPath.t Conversion.t
231231
val module_inline_default : Declaremods.inline Conversion.t
232232
val reduction_flags : CClosure.RedFlags.reds Conversion.t
233233

234-
val in_elpi_module : depth:int -> State.t -> Declarations.module_body -> GlobRef.t list
234+
type module_item =
235+
| Module of Names.ModPath.t * module_item list
236+
| ModuleType of Names.ModPath.t
237+
| Gref of Names.GlobRef.t
238+
| Functor of Names.ModPath.t * Names.ModPath.t list
239+
| FunctorType of Names.ModPath.t * Names.ModPath.t list
240+
241+
val in_elpi_module : depth:int -> State.t -> Declarations.module_body -> module_item list
235242
val in_elpi_module_type : Declarations.module_type_body -> string list
236243

237244
val coercion_status : coercion_status Conversion.t

src/coq_elpi_builtins.ml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,30 @@ let reduction_kind = let open API.AlgebraicData in let open CClosure.RedFlags in
616616
]
617617
} |> CConv.(!<)
618618

619+
620+
let module_item = let open API.AlgebraicData in declare {
621+
ty = Conv.TyName "module-item";
622+
doc = "Contents of a module";
623+
pp = (fun fmt a -> Format.fprintf fmt "TODO");
624+
constructors = [
625+
K("submodule","",A(modpath,C(CConv.(!>>) B.list,N)),
626+
B (fun s l -> Module(s,l) ),
627+
M (fun ~ok ~ko -> function Module(s,l) -> ok s l | _ -> ko ()));
628+
K("module-type","",A(modtypath,N),
629+
B (fun s -> ModuleType s),
630+
M (fun ~ok ~ko -> function ModuleType s -> ok s | _ -> ko ()));
631+
K("gref","",A(gref,N),
632+
B (fun s -> Gref s),
633+
M (fun ~ok ~ko -> function Gref x -> ok x | _ -> ko ()));
634+
K("module-functor","",A(modpath,A(B.list modtypath,N)),
635+
B (fun s a -> Functor(s,a)),
636+
M (fun ~ok ~ko -> function Functor(s,a) -> ok s a | _ -> ko ()));
637+
K("module-type-functor","",A(modtypath,A(B.list modtypath,N)),
638+
B (fun s a -> FunctorType(s,a)),
639+
M (fun ~ok ~ko -> function FunctorType(s,a) -> ok s a | _ -> ko ()));
640+
]
641+
} |> CConv.(!<)
642+
619643
let attribute a = let open API.AlgebraicData in declare {
620644
ty = Conv.TyName "attribute";
621645
doc = "Generic attribute";
@@ -1619,9 +1643,11 @@ Supported attributes:
16191643
!:mp)),
16201644
DocAbove);
16211645

1646+
MLData module_item;
1647+
16221648
MLCode(Pred("coq.env.module",
16231649
In(modpath, "MP",
1624-
Out(list gref, "Contents",
1650+
Out(list module_item, "Contents",
16251651
Read(global, "lists the contents of a module (recurses on submodules) *E*"))),
16261652
(fun mp _ ~depth {env} _ state ->
16271653
let t = in_elpi_module ~depth state (Environ.lookup_module mp env) in

tests/test_API_module.v

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ End X.
1818

1919
Elpi Query lp:{{
2020
coq.locate-module "X" MP,
21-
coq.env.module MP [
22-
(indt Xi), (const _), (const _), (const _), (const _),
23-
(const _),
24-
(indt XYi), (const XYr), (const _), (const _), (const _),
25-
(const _)
26-
],
21+
coq.env.module MP L,
22+
std.assert! (L = [
23+
gref (indt Xi), gref (const _), gref (const _), gref (const _), gref (const _),
24+
gref (const _),
25+
submodule _ [
26+
gref (indt XYi), gref (const XYr), gref (const _), gref (const _), gref (const _),
27+
gref (const _)]
28+
]) "bad module",
2729
coq.say {coq.gref->string (indt Xi)},
2830
rex_match "\\(Top.\\|.*test_API_module\\)\\.X\\.i$" {coq.gref->string (indt Xi)},
2931
rex_match "\\(Top.\\|.*test_API_module\\)\\.X\\.Y\\.i$" {coq.gref->string (indt XYi)},
@@ -43,7 +45,7 @@ End Y.
4345

4446
Elpi Query lp:{{
4547
coq.locate-module "Y" MP,
46-
coq.env.module MP [indt I, indt J, |_],
48+
coq.env.module MP [gref (indt I), gref (indt J), |_],
4749
coq.gref->path (indt I) P,
4850
coq.gref->id (indt J) ID
4951
}}.
@@ -120,3 +122,40 @@ Elpi Query lp:{{
120122

121123
Print ITA.
122124

125+
126+
Module R.
127+
Module S.
128+
Definition x := 3.
129+
End S.
130+
Module Type P1.
131+
Parameter x : nat.
132+
End P1.
133+
Module Type P2.
134+
Parameter y : nat.
135+
End P2.
136+
Module F(A : P1)(B : P2).
137+
Definition z := A.x + B.y.
138+
End F.
139+
Module Type FT(A : P2)(B : P1).
140+
Definition z := A.y + B.x.
141+
End FT.
142+
Definition a := 1.
143+
End R.
144+
145+
Elpi Query lp:{{
146+
coq.locate-module "R" R,
147+
coq.locate-module "R.S" S,
148+
coq.locate-module-type "R.P1" P1,
149+
coq.locate-module-type "R.P2" P2,
150+
coq.locate-module "R.F" F,
151+
coq.locate-module-type "R.FT" FT,
152+
coq.env.module R L,
153+
std.assert! (L = [
154+
submodule S [gref (const _)],
155+
module-type P1,
156+
module-type P2,
157+
module-functor F [P1,P2],
158+
module-type-functor FT [P2,P1],
159+
gref (const _)
160+
]) "bad module"
161+
}}.

0 commit comments

Comments
 (0)