Skip to content

Commit 11efe82

Browse files
Generalize DeclareScheme to use GlobRef instead of inductive
prevent registering scheme for section variables without the local attribute
1 parent 183249b commit 11efe82

File tree

14 files changed

+69
-45
lines changed

14 files changed

+69
-45
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
overlay elpi https://github.com/thomas-lamiaux/coq-elpi genScheme 21811
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- **Changed:**
2+
Generalize DeclareScheme to be able to register schemes for any GlobRef,
3+
and not just for inductive types
4+
(`#21811 <https://github.com/rocq-prover/rocq/pull/21811>`_,
5+
by Thomas Lamiaux).

tactics/allScheme.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,22 +293,22 @@ let warn_lookup_not_found =
293293
Raise a warning if none is found. *)
294294
let lookup_all ind ind_nested args_are_nested =
295295
let (_, (pred, _)) = partial_suffix args_are_nested in
296-
match DeclareScheme.lookup_scheme_opt pred ind_nested with
296+
match DeclareScheme.lookup_scheme_opt pred (GlobRef.IndRef ind_nested) with
297297
| Some ref_pred ->
298298
Some (true, ref_pred)
299299
| None ->
300300
let (_, (pred, _)) = default_suffix in
301-
match DeclareScheme.lookup_scheme_opt pred ind_nested with
301+
match DeclareScheme.lookup_scheme_opt pred (GlobRef.IndRef ind_nested) with
302302
| Some ref_pred -> Some (false, ref_pred)
303303
| None -> warn_lookup_not_found (pred, ind, ind_nested); None
304304

305305
(** Lookup the [all] predicate, and its theorem *)
306306
let lookup_all_theorem_aux ind ind_nested =
307307
let (_, (pred, thm)) = default_suffix in
308-
match DeclareScheme.lookup_scheme_opt pred ind_nested with
308+
match DeclareScheme.lookup_scheme_opt pred (GlobRef.IndRef ind_nested) with
309309
| None -> warn_lookup_not_found (pred, ind, ind_nested); None
310310
| Some ref_pred ->
311-
match DeclareScheme.lookup_scheme_opt thm ind_nested with
311+
match DeclareScheme.lookup_scheme_opt thm (GlobRef.IndRef ind_nested) with
312312
| None -> warn_lookup_not_found (thm, ind, ind_nested); None
313313
| Some ref_thm -> Some (false, ref_pred, ref_thm)
314314

@@ -318,10 +318,10 @@ let lookup_all_theorem_aux ind ind_nested =
318318
Raise a warning if none is found. *)
319319
let lookup_all_theorem ind ind_nested args_are_nested =
320320
let (_, (pred, thm)) = partial_suffix args_are_nested in
321-
match DeclareScheme.lookup_scheme_opt pred ind_nested with
321+
match DeclareScheme.lookup_scheme_opt pred (GlobRef.IndRef ind_nested) with
322322
| None -> lookup_all_theorem_aux ind ind_nested
323323
| Some ref_pred ->
324-
match DeclareScheme.lookup_scheme_opt thm ind_nested with
324+
match DeclareScheme.lookup_scheme_opt thm (GlobRef.IndRef ind_nested) with
325325
| Some ref_thm ->
326326
Some (true, ref_pred, ref_thm)
327327
| None ->

tactics/declareScheme.ml

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,45 @@
1010

1111
open Names
1212

13-
let scheme_map = Summary.ref Indmap_env.empty ~name:"Schemes"
13+
let scheme_map = Summary.ref GlobRef.Map_env.empty ~name:"Schemes"
1414

15-
let cache_one_scheme kind (ind,const) =
16-
scheme_map := Indmap_env.update ind (function
15+
let cache_one_scheme kind (gr,const) =
16+
scheme_map := GlobRef.Map_env.update gr (function
1717
| None -> Some (CString.Map.singleton kind const)
1818
| Some map -> Some (CString.Map.add kind const map))
1919
!scheme_map
2020

2121
let cache_scheme (kind,l) =
2222
cache_one_scheme kind l
2323

24-
let subst_one_scheme subst (ind,const) =
25-
(* Remark: const is a def: the result of substitution is a constant *)
26-
(Mod_subst.subst_ind subst ind, Globnames.subst_global_reference subst const)
24+
let subst_one_scheme subst (gr,const) =
25+
(Globnames.subst_global_reference subst gr, Globnames.subst_global_reference subst const)
2726

2827
let subst_scheme (subst,(kind,l)) =
2928
(kind, subst_one_scheme subst l)
3029

31-
let inScheme : Libobject.locality * (string * (inductive * GlobRef.t)) -> Libobject.obj =
30+
let inScheme : Libobject.locality * (string * (GlobRef.t * GlobRef.t)) -> Libobject.obj =
3231
let open Libobject in
3332
declare_object @@ object_with_locality "SCHEME"
3433
~cache:cache_scheme
3534
~subst:(Some subst_scheme)
3635
~discharge:(fun x -> x)
3736

38-
let declare_scheme local kind indcl =
39-
Lib.add_leaf (inScheme (local,(kind,indcl)))
40-
41-
let lookup_scheme kind ind = CString.Map.find kind (Indmap_env.find ind !scheme_map)
42-
43-
let lookup_scheme_opt kind ind =
44-
try Some (lookup_scheme kind ind) with Not_found -> None
37+
let declare_scheme local kind (gr, _ as grcl) =
38+
let () = match local, gr with
39+
| (Libobject.Export | Libobject.SuperGlobal), GlobRef.VarRef id ->
40+
if Global.is_in_section gr then
41+
CErrors.user_err
42+
Pp.(str "Cannot register a non-local scheme for section variable "
43+
++ Names.Id.print id
44+
++ str "; use the #[local] attribute.")
45+
| _, _ -> ()
46+
in
47+
Lib.add_leaf (inScheme (local,(kind,grcl)))
48+
49+
let lookup_scheme kind gr = CString.Map.find kind (GlobRef.Map_env.find gr !scheme_map)
50+
51+
let lookup_scheme_opt kind gr =
52+
try Some (lookup_scheme kind gr) with Not_found -> None
4553

4654
let all_schemes () = !scheme_map

tactics/declareScheme.mli

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
open Names
1212

13-
val declare_scheme : Libobject.locality -> string -> (inductive * GlobRef.t) -> unit
14-
val lookup_scheme : string -> inductive -> GlobRef.t
15-
val lookup_scheme_opt : string -> inductive -> GlobRef.t option
16-
val all_schemes : unit -> GlobRef.t CString.Map.t Indmap_env.t
13+
val declare_scheme : Libobject.locality -> string -> (GlobRef.t * GlobRef.t) -> unit
14+
val lookup_scheme : string -> GlobRef.t -> GlobRef.t
15+
val lookup_scheme_opt : string -> GlobRef.t -> GlobRef.t option
16+
val all_schemes : unit -> GlobRef.t CString.Map.t GlobRef.Map_env.t

tactics/ind_tables.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ let register_definition_scheme = ref (fun ~internal ~name ~const ~univs ?loc ()
104104
CErrors.anomaly (Pp.str "scheme registering not registered"))
105105

106106
let lookup_scheme kind ind =
107-
try Some (DeclareScheme.lookup_scheme kind ind) with Not_found -> None
107+
try Some (DeclareScheme.lookup_scheme kind (GlobRef.IndRef ind)) with Not_found -> None
108108

109109
type schemes = {
110110
sch_eff : Evd.side_effects;
@@ -120,11 +120,11 @@ let redeclare_schemes { sch_eff = eff } =
120120
let fold c role accu = match role with
121121
| Evd.Schema (ind, kind) ->
122122
try
123-
let _ = DeclareScheme.lookup_scheme kind ind in
123+
let _ = DeclareScheme.lookup_scheme kind (GlobRef.IndRef ind) in
124124
accu
125125
with Not_found ->
126126
let old = try String.Map.find kind accu with Not_found -> [] in
127-
String.Map.add kind ((ind, GlobRef.ConstRef c) :: old) accu
127+
String.Map.add kind ((GlobRef.IndRef ind, GlobRef.ConstRef c) :: old) accu
128128
in
129129
let schemes = Cmap_env.fold fold (Evd.seff_roles eff) String.Map.empty in
130130
let iter kind defs = List.iter (DeclareScheme.declare_scheme SuperGlobal kind) defs in
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(** Test that scheme registrations for section variables require #[local]. *)
2+
3+
Axiom foo : Type.
4+
5+
Section S1.
6+
Variable A : Type.
7+
Fail #[export] Register Scheme foo as rew_r_dep for A.
8+
Fail Global Register Scheme foo as rew_r_dep for A.
9+
#[local] Register Scheme foo as rew_r_dep for A.
10+
End S1.

vernac/comSearch.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ let kind_searcher env = Decls.(function
5656
| IsDefinition Scheme ->
5757
let schemes = DeclareScheme.all_schemes () in
5858
let schemes = lazy begin
59-
Indmap_env.fold (fun _ schemes acc ->
59+
GlobRef.Map_env.fold (fun _ schemes acc ->
6060
CString.Map.fold (fun _ c acc ->
6161
GlobRef.Set_env.add c acc) schemes acc)
6262
schemes GlobRef.Set_env.empty

vernac/declare.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ let register_side_effect (c, body, role, univs) =
484484
in
485485
match role with
486486
| None -> ()
487-
| Some (Evd.Schema (ind, kind)) -> DeclareScheme.declare_scheme SuperGlobal kind (ind, GlobRef.ConstRef c)
487+
| Some (Evd.Schema (ind, kind)) -> DeclareScheme.declare_scheme SuperGlobal kind (GlobRef.IndRef ind, GlobRef.ConstRef c)
488488

489489
let get_roles export eff =
490490
let eff = SideEff.obj eff in

vernac/g_vernac.mlg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ GRAMMAR EXTEND Gram
296296
| IDENT "Register"; g = global; "as"; quid = qualid ->
297297
{ VernacRegister(g, RegisterCoqlib quid) }
298298
| IDENT "Register"; IDENT "Scheme"; g = global; "as"; qid = qualid; IDENT "for"; g' = global ->
299-
{ VernacRegister(g, RegisterScheme {inductive = g'; scheme_kind = qid}) }
299+
{ VernacRegister(g, RegisterScheme {ref = g'; scheme_kind = qid}) }
300300
| IDENT "Register"; IDENT "Inline"; g = global ->
301301
{ VernacRegister(g, RegisterInline) }
302302
| IDENT "Primitive"; id = ident_decl; typopt = OPT [ ":"; typ = lconstr -> { typ } ]; ":="; r = register_token ->

0 commit comments

Comments
 (0)