Skip to content

Commit 63c47a6

Browse files
authored
Merge pull request #963 from LPCIC/section-inspection
coq.env.section-contents (Fix #950)
2 parents dcfaf09 + 2873b99 commit 63c47a6

File tree

6 files changed

+95
-12
lines changed

6 files changed

+95
-12
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
### API:
1010
- Fix `coq.sort.*` to not rely on puring algebraic universes that will become
1111
first class in Rocq 10
12+
- Rename `coq.env.section`, use `coq.env.section-variables`
13+
- New `coq.env.section-contents` listing the contents of sections.
1214

1315
# [3.2.0] 19/09/2025
1416

apps/tc/elpi/compiler1.elpi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace tc {
1111

1212
func has-context-deps gref -> .
1313
has-context-deps GR :-
14-
coq.env.section SectionVars,
14+
coq.env.section-variables SectionVars,
1515
coq.env.dependencies GR _ Deps,
1616
std.exists! SectionVars (x\ coq.gref.set.mem (const x) Deps).
1717

apps/tc/elpi/tc_aux.elpi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace tc {
6363
instances-of-current-section InstsFiltered :-
6464
coq.env.current-section-path SectionPath,
6565
std.findall (tc.instance SectionPath _ _ _) Insts,
66-
coq.env.section SectionVars,
66+
coq.env.section-variables SectionVars,
6767
std.map-filter Insts (x\r\ sigma X\ tc.instance _ r _ _ = x, const X = r, not(std.mem SectionVars X)) InstsFiltered.
6868

6969
func is-instance-gr gref ->.
@@ -182,7 +182,7 @@ namespace tc {
182182
% [section-var->decl L] decl representing seciton variables with their types
183183
func section-var->decl -> list prop.
184184
section-var->decl L :-
185-
section-var->decl.aux {coq.env.section} L.
185+
section-var->decl.aux {coq.env.section-variables} L.
186186

187187
func time-is-active (func (list string) ->) ->.
188188
:name "time-is-active"

builtin-doc/coq-builtin.elpi

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -749,9 +749,21 @@ external func coq.env.module modpath -> list module-item.
749749
% type (does not recurse on submodules) *E*
750750
external func coq.env.module-type modtypath -> list id.
751751

752-
% [coq.env.section GlobalObjects] lists the global objects that are marked
753-
% as to be abstracted at the end of the enclosing sections
754-
external func coq.env.section -> list constant.
752+
% [coq.env.section-variables ConstantsRepresentingVariables] lists all the
753+
% section variables, i.e. the global objects that are marked as to be
754+
% abstracted at the end of the enclosing sections
755+
external func coq.env.section-variables -> list constant.
756+
757+
% [coq.env.section-contents GlobalObjects] lists the global objects that are
758+
% defined withing the current section
759+
external func coq.env.section-contents -> list gref.
760+
761+
% Deprecated, use coq.env.section-variables
762+
func coq.env.section -> list constant.
763+
coq.env.section L :-
764+
coq.warning "elpi.deprecated" "elpi.env.section" "use coq.env.section-variables in place of coq.env.section",
765+
coq.env.section-variables L.
766+
755767

756768
% [coq.env.dependencies GR MP Deps] Computes the direct dependencies of GR.
757769
% If MP is given, Deps only contains grefs from that module
@@ -1688,7 +1700,7 @@ external func coq.option.available? list string -> bool.
16881700
% and for all in a .v file which your clients will load. Eg.
16891701
%
16901702
% Elpi Query lp:{{ coq.option.add ... }}.
1691-
%
1703+
%
16921704
%
16931705
external func coq.option.add list string, coq.option, bool.
16941706

src/rocq_elpi_builtins.ml

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,19 @@ let apply_proof ~name ~poly env tac pf =
15621562
[%%endif]
15631563

15641564

1565+
[%%if coq = "9.0"]
1566+
let section_close_section x =
1567+
let a,b,_,_ = Section.close_section x in
1568+
a, b
1569+
[%%elif coq = "9.1"]
1570+
let section_close_section x =
1571+
let a,b,_,_,_ = Section.close_section x in
1572+
a, b
1573+
[%%else]
1574+
let section_close_section x =
1575+
let a,b,_,_ = Section.close_section x in
1576+
a, b
1577+
[%%endif]
15651578

15661579

15671580

@@ -2135,14 +2148,45 @@ Supported attributes:
21352148
!: (in_elpi_module_type (Environ.lookup_modtype mp env)))),
21362149
DocAbove);
21372150

2138-
MLCode(Pred("coq.env.section",
2139-
Out(list constant, "GlobalObjects",
2140-
Read(unit_ctx, "lists the global objects that are marked as to be abstracted at the end of the enclosing sections")),
2151+
MLCode(Pred("coq.env.section-variables",
2152+
Out(list constant, "ConstantsRepresentingVariables",
2153+
Read(unit_ctx, "lists all the section variables, i.e. the global objects that are marked as to be abstracted at the end of the enclosing sections")),
21412154
(fun _ ~depth _ _ state ->
21422155
let { section } = mk_coq_context ~options:(default_options ()) state in
21432156
!: (section |> List.map (fun x -> Variable x)) )),
21442157
DocAbove);
21452158

2159+
MLCode(Pred("coq.env.section-contents",
2160+
Out(list (list gref), "GlobalObjects",
2161+
Read(unit_ctx, "lists the global objects that are defined withing the open sections. Inner section first.")),
2162+
(fun _ ~depth _ _ state ->
2163+
let l =
2164+
let safe_env = Global.safe_env () in
2165+
let section = Safe_typing.sections_of_safe_env safe_env in
2166+
let rec aux section =
2167+
match section with
2168+
| None -> []
2169+
| Some s ->
2170+
let section,l = section_close_section s in
2171+
List.concat_map (function
2172+
| Section.SecDefinition c -> [GlobRef.ConstRef c]
2173+
| Section.SecInductive m ->
2174+
let { Declarations.mind_packets } = Environ.lookup_mind m (Safe_typing.env_of_safe_env safe_env) in
2175+
List.init (Array.length mind_packets) (fun i -> GlobRef.IndRef(m,i))
2176+
) l :: aux section
2177+
in
2178+
aux section
2179+
in
2180+
!: l)),
2181+
DocAbove);
2182+
2183+
LPCode {|% Deprecated, use coq.env.section-variables
2184+
func coq.env.section -> list constant.
2185+
coq.env.section L :-
2186+
coq.warning "elpi.deprecated" "elpi.env.section" "use coq.env.section-variables in place of coq.env.section",
2187+
coq.env.section-variables L.
2188+
|};
2189+
21462190
MLCode(Pred("coq.env.dependencies",
21472191
In(gref, "GR",
21482192
In(B.unspec modpath, "MP",

tests/test_API_section.v

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Section SB.
1212
Variable b : nat.
1313
Let c := b.
1414
Elpi Query lp:{{
15-
coq.env.section [CA, CB, CC],
15+
coq.env.section-variables [CA, CB, CC],
1616
coq.locate "a" (const CA),
1717
coq.locate "b" (const CB),
1818
coq.locate "c" (const CC),
@@ -34,7 +34,7 @@ Elpi Query lp:{{
3434
}} lp:{{
3535
coq.env.begin-section "Foo",
3636
coq.env.add-section-variable "x" _ {{ nat }} X,
37-
coq.env.section [X],
37+
coq.env.section-variables [X],
3838
coq.env.add-const "fx" (global (const X)) _ _ _,
3939
coq.env.end-section.
4040
}}.
@@ -63,3 +63,28 @@ End Using.
6363
Check foo : nat.
6464
Check bar : bool -> nat.
6565

66+
(* section inspection *)
67+
68+
Section Foo.
69+
70+
Variable x : nat.
71+
Definition a := x + 1.
72+
73+
Section Bar.
74+
75+
Definition b := a.
76+
Inductive C := .
77+
78+
Elpi Query lp:{{
79+
coq.env.section-contents L,
80+
std.assert! (L = [
81+
82+
[ {{:gref C_sind }}, {{:gref C_rec }}, {{:gref C_ind }}, {{:gref C_rect }}, {{:gref C }}, {{:gref b }}, ],
83+
84+
[ {{:gref a }}]
85+
86+
]) "bad section listing".
87+
}}.
88+
89+
End Bar.
90+
End Foo.

0 commit comments

Comments
 (0)