Skip to content

Commit b3a9eeb

Browse files
committed
Use variable status to decide if a variable is section variable
Termops.is_section_variable is deprecated and a new API Termops.is_section_variable' is added because their types are the same but they need different env arguments so changing in place would be too footgunny. ssr still uses the old "is it in global env" because IDK what it is doing. Fix #18858
1 parent 0dc2518 commit b3a9eeb

25 files changed

Lines changed: 130 additions & 153 deletions

engine/evarutil.ml

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,7 @@ let update_var src tgt subst =
285285
let csubst_var = Id.Map.add id (Constr.mkVar tgt) subst.csubst_var in
286286
{ subst with csubst_var; csubst_rev }
287287

288-
module VarSet =
289-
struct
290-
type t = Id.t -> bool
291-
let empty _ = false
292-
let full _ = true
293-
let variables env id = is_section_variable env id
294-
end
295-
296-
type naming_mode = VarSet.t
297-
298288
let push_rel_decl_to_named_context
299-
~hypnaming
300289
sigma decl (ext : ext_named_context) =
301290
let open EConstr in
302291
let open Vars in
@@ -327,7 +316,7 @@ let push_rel_decl_to_named_context
327316
in
328317
match extract_if_neq id na with
329318
| Some id0 ->
330-
if hypnaming id0 then
319+
if Id.Map.mem id0 ext.ext_ctx.env_named_map && is_section_variable_sign ext.ext_ctx id0 then
331320
(* spiwack: if [id0] is a section variable renaming it is
332321
incorrect. We revert to a less robust behaviour where
333322
the new binder has name [id]. Which amounts to the same
@@ -370,7 +359,7 @@ let ext_rev_subst { ext_subst = subst } id0 =
370359
let default_ext_instance { ext_subst = subst; ext_ctx = ctx } =
371360
csubst_instance subst (named_context_of_val ctx)
372361

373-
let push_rel_context_to_named_context ~hypnaming env sigma typ =
362+
let push_rel_context_to_named_context env sigma typ =
374363
(* compute the instances relative to the named context and rel_context *)
375364
let open EConstr in
376365
let ctx = named_context_val env in
@@ -384,15 +373,15 @@ let push_rel_context_to_named_context ~hypnaming env sigma typ =
384373
(* We do keep the instances corresponding to local definition (see above) *)
385374
let init = { ext_subst = empty_csubst; ext_avoid = avoid; ext_ctx = ctx } in
386375
let ext =
387-
Context.Rel.fold_outside (fun d acc -> push_rel_decl_to_named_context ~hypnaming sigma d acc)
376+
Context.Rel.fold_outside (fun d acc -> push_rel_decl_to_named_context sigma d acc)
388377
(rel_context env) ~init in
389378
let inst = default_ext_instance ext in
390379
(ext.ext_ctx, csubst_subst sigma ext.ext_subst typ, inst, ext.ext_subst)
391380

392-
let ext_named_context_of_env ~hypnaming env sigma =
381+
let ext_named_context_of_env env sigma =
393382
let avoid = Environ.ids_of_named_context_val (Environ.named_context_val env) in
394383
let init = { ext_subst = empty_csubst; ext_avoid = avoid; ext_ctx = named_context_val env } in
395-
Context.Rel.fold_outside (fun d acc -> push_rel_decl_to_named_context ~hypnaming sigma d acc)
384+
Context.Rel.fold_outside (fun d acc -> push_rel_decl_to_named_context sigma d acc)
396385
(EConstr.rel_context env) ~init
397386

398387
(*------------------------------------*
@@ -409,13 +398,9 @@ let next_evar_name naming = match naming with
409398
(* [new_evar] declares a new existential in an env env with type typ *)
410399
(* Converting the env into the sign of the evar to define *)
411400
let new_evar ?src ?filter ?relevance ?abstract_arguments ?candidates ?(naming = IntroAnonymous) ?parent ?typeclass_candidate
412-
?rrpat ?hypnaming env evd typ =
401+
?rrpat env evd typ =
413402
let name = next_evar_name naming in
414-
let hypnaming = match hypnaming with
415-
| Some n -> n
416-
| None -> VarSet.variables (Global.env ())
417-
in
418-
let sign,typ',instance,subst = push_rel_context_to_named_context ~hypnaming env evd typ in
403+
let sign,typ',instance,subst = push_rel_context_to_named_context env evd typ in
419404
let map c = csubst_subst evd subst c in
420405
let candidates = Option.map (fun l -> List.map map l) candidates in
421406
let instance =
@@ -430,10 +415,10 @@ let new_evar ?src ?filter ?relevance ?abstract_arguments ?candidates ?(naming =
430415
?typeclass_candidate in
431416
(evd, EConstr.mkEvar (evk, instance))
432417

433-
let new_type_evar ?src ?filter ?naming ?hypnaming env evd rigid =
418+
let new_type_evar ?src ?filter ?naming env evd rigid =
434419
let (evd', s) = new_sort_variable rigid evd in
435420
let relevance = EConstr.ESorts.relevance_of_sort s in
436-
let (evd', e) = new_evar env evd' ?src ?filter ~relevance ?naming ~typeclass_candidate:false ?hypnaming (EConstr.mkSort s) in
421+
let (evd', e) = new_evar env evd' ?src ?filter ~relevance ?naming ~typeclass_candidate:false (EConstr.mkSort s) in
437422
evd', (e, s)
438423

439424
let new_Type ?(rigid=Evd.univ_flexible) evd =
@@ -518,7 +503,7 @@ let check_vars env sigma ids c =
518503
in
519504
check_rec c
520505

521-
let rec check_and_clear_in_constr ~is_section_variable env evdref err ids ~global c =
506+
let rec check_and_clear_in_constr env evdref err ids ~global c =
522507
(* returns a new constr where all the evars have been 'cleaned'
523508
(ie the hypotheses ids have been removed from the contexts of
524509
evars). [global] should be true iff there is some variable of [ids] which
@@ -585,9 +570,10 @@ let rec check_and_clear_in_constr ~is_section_variable env evdref err ids ~globa
585570
let _nconcl : EConstr.t =
586571
try
587572
let nids = Id.Map.domain rids in
588-
let global = Id.Set.exists is_section_variable nids in
573+
let env = evar_filtered_env env evi in
574+
let global = Id.Set.exists (is_section_variable' env) nids in
589575
let concl = evar_concl evi in
590-
check_and_clear_in_constr ~is_section_variable env evdref (EvarTypingBreak ev) nids ~global concl
576+
check_and_clear_in_constr env evdref (EvarTypingBreak ev) nids ~global concl
591577
with ClearDependencyError (rid,err,where) ->
592578
raise (ClearDependencyError (Id.Map.find rid rids,err,where))
593579
in
@@ -599,31 +585,31 @@ let rec check_and_clear_in_constr ~is_section_variable env evdref err ids ~globa
599585
evdref := evd;
600586
Evd.existential_value !evdref ev
601587

602-
| _ -> EConstr.map !evdref (check_and_clear_in_constr ~is_section_variable env evdref err ids ~global) c
588+
| _ -> EConstr.map !evdref (check_and_clear_in_constr env evdref err ids ~global) c
603589

604590
let clear_hyps_in_evi_main env sigma hyps terms ids =
605591
(* clear_hyps_in_evi erases hypotheses ids in hyps, checking if some
606592
hypothesis does not depend on a element of ids, and erases ids in
607593
the contexts of the evars occurring in evi *)
608594
let evdref = ref sigma in
609-
let is_section_variable id = is_section_variable (Global.env ()) id in
610-
let global = Id.Set.exists is_section_variable ids in
595+
let global = Id.Set.exists (is_section_variable' env) ids in
611596
let terms =
612-
List.map (check_and_clear_in_constr ~is_section_variable env evdref (OccurHypInSimpleClause None) ids ~global) terms in
597+
List.map (check_and_clear_in_constr env evdref (OccurHypInSimpleClause None) ids ~global) terms in
613598
let nhyps =
614599
let check_context decl =
615600
let decl = EConstr.of_named_decl decl in
616601
let err = OccurHypInSimpleClause (Some (NamedDecl.get_id decl)) in
617-
EConstr.Unsafe.to_named_decl @@ NamedDecl.map_constr (check_and_clear_in_constr ~is_section_variable env evdref err ids ~global) decl
602+
EConstr.Unsafe.to_named_decl @@ NamedDecl.map_constr (check_and_clear_in_constr env evdref err ids ~global) decl
618603
in
619604
remove_hyps ids check_context hyps
620605
in
621606
(!evdref, nhyps, terms)
622607

623608
let check_and_clear_in_constr env evd err ids c =
624609
let evdref = ref evd in
610+
let global = Id.Set.exists (is_section_variable' env) ids in
625611
let _ : EConstr.constr = check_and_clear_in_constr
626-
~is_section_variable:(fun _ -> true) ~global:true
612+
~global
627613
env evdref err ids c
628614
in
629615
!evdref

engine/evarutil.mli

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ val new_meta : unit -> metavariable
2626

2727
val next_evar_name : intro_pattern_naming_expr -> (Id.t * bool) option
2828

29-
module VarSet :
30-
sig
31-
type t
32-
val empty : t
33-
val full : t
34-
val variables : Environ.env -> t
35-
end
36-
37-
type naming_mode = VarSet.t
38-
3929
val new_evar :
4030
?src:Evar_kinds.t Loc.located -> ?filter:Filter.t ->
4131
?relevance:ERelevance.t ->
@@ -44,7 +34,6 @@ val new_evar :
4434
?parent:Evar.t ->
4535
?typeclass_candidate:bool ->
4636
?rrpat:bool ->
47-
?hypnaming:naming_mode ->
4837
env -> evar_map -> types -> evar_map * EConstr.t
4938

5039
(** Alias of {!Evd.new_pure_evar} *)
@@ -63,7 +52,6 @@ val new_pure_evar :
6352
val new_type_evar :
6453
?src:Evar_kinds.t Loc.located -> ?filter:Filter.t ->
6554
?naming:intro_pattern_naming_expr ->
66-
?hypnaming:naming_mode ->
6755
env -> evar_map -> rigid ->
6856
evar_map * (constr * ESorts.t)
6957

@@ -240,7 +228,7 @@ val csubst_subst : Evd.evar_map -> csubst -> constr -> constr
240228

241229
type ext_named_context
242230

243-
val ext_named_context_of_env : hypnaming:naming_mode -> env -> evar_map -> ext_named_context
231+
val ext_named_context_of_env : env -> evar_map -> ext_named_context
244232

245233
val ext_named_context_val : ext_named_context -> named_context_val
246234
val ext_csubst : ext_named_context -> csubst
@@ -249,10 +237,10 @@ val default_ext_instance : ext_named_context -> constr SList.t
249237

250238
val ext_rev_subst : ext_named_context -> Id.t -> constr
251239

252-
val push_rel_decl_to_named_context : hypnaming:naming_mode ->
240+
val push_rel_decl_to_named_context :
253241
evar_map -> rel_declaration -> ext_named_context -> ext_named_context
254242

255-
val push_rel_context_to_named_context : hypnaming:naming_mode ->
243+
val push_rel_context_to_named_context :
256244
Environ.env -> evar_map -> types ->
257245
named_context_val * types * constr SList.t * csubst
258246

engine/namegen.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ let is_constructor id =
9999
with Not_found ->
100100
false
101101

102-
let is_section_variable env id =
103-
try let _ = Environ.lookup_named id env in true
104-
with Not_found -> false
105-
106102
(**********************************************************************)
107103
(* Generating "intuitive" names from its type *)
108104

@@ -401,15 +397,15 @@ let next_name_away_in_cases_pattern gen sigma env_t na avoid =
401397

402398
let next_ident_away_in_goal env id avoid =
403399
let id = if Id.Set.mem id avoid then restart_subscript id else id in
404-
let bad id = Id.Set.mem id avoid || (is_global id && not (is_section_variable env id)) in
400+
let bad id = Id.Set.mem id avoid || (is_global id && not (Environ.mem_named id env)) in
405401
next_ident_away_from id bad
406402

407403
let next_name_away_in_goal (type a) (gen : a Generator.t) env na (avoid : a) =
408404
let id = match na with
409405
| Name id -> id
410406
| Anonymous -> default_non_dependent_ident in
411407
let id = if Generator.is_fresh gen id avoid then id else restart_subscript id in
412-
let bad id = is_global id && not (is_section_variable env id) in
408+
let bad id = is_global id && not (Environ.mem_named id env) in
413409
Generator.gen_ident ~filter:bad gen id avoid
414410

415411
(* 3- Looks for next fresh name outside a list that is moreover valid

engine/termops.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,10 +1007,13 @@ let ids_of_context env =
10071007
let names_of_rel_context env =
10081008
List.map RelDecl.get_name (rel_context env)
10091009

1010-
(* XXX use var status (NOT IN THIS ENV) *)
1011-
let is_section_variable env id =
1012-
try let _ = Environ.lookup_named id env in true
1013-
with Not_found -> false
1010+
let is_section_variable_sign sign id =
1011+
match NamedDecl.get_status @@ Environ.lookup_named_ctxt id sign with
1012+
| exception Not_found -> assert false
1013+
| SecVar -> true
1014+
| ProofVar -> false
1015+
1016+
let is_section_variable' env id = is_section_variable_sign (Environ.named_context_val env) id
10141017

10151018
let is_template_polymorphic_ref env sigma f =
10161019
match EConstr.kind sigma f with
@@ -1424,3 +1427,8 @@ and hash_branches bl =
14241427
Array.fold_left (fun acc t -> combine acc (hash_under_context t)) 0 bl
14251428

14261429
end
1430+
1431+
(* deprecated *)
1432+
let is_section_variable env id =
1433+
try let _ = Environ.lookup_named id env in true
1434+
with Not_found -> false

engine/termops.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,13 @@ val global_app_of_constr : Evd.evar_map -> constr -> (GlobRef.t * EInstance.t) *
206206
containing a given set *)
207207
val dependency_closure : env -> Evd.evar_map -> named_context -> Id.Set.t -> Id.t list
208208

209-
(** Test if an identifier is the basename of a global reference *)
209+
(** This tests if the ident is known in the given env, indented to be used with the global env. *)
210210
val is_section_variable : env -> Id.t -> bool
211+
[@@deprecated "Use is_section_variable' on the local env instead of is_section_variable on the global env."]
212+
213+
(** Check if the ident has [SecVar] status in this enviroment. [false] if it is not bound. *)
214+
val is_section_variable_sign : Environ.named_context_val -> Id.t -> bool
215+
val is_section_variable' : env -> Id.t -> bool
211216

212217
val is_template_polymorphic_ref : env -> Evd.evar_map -> constr -> bool
213218
val is_template_polymorphic_ind : env -> Evd.evar_map -> constr -> bool

kernel/context.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ struct
355355

356356
let get_status (LocalAssum (s,_,_) | LocalDef (s,_,_,_)) = s
357357

358+
let is_secvar d = match get_status d with
359+
| SecVar -> true
360+
| ProofVar -> false
361+
358362
let get_annot = function
359363
| LocalAssum (_,na,_) | LocalDef (_,na,_,_) -> na
360364

kernel/context.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ sig
235235

236236
val get_status : _ pt -> status
237237

238+
val is_secvar : _ pt -> bool
239+
238240
val get_annot : (_,_,'r) pt -> (Id.t,'r) pbinder_annot
239241

240242
(** Return the identifier bound by a given declaration. *)

plugins/funind/functional_principles_proofs.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ let generalize_non_dep hyp =
800800
Id.List.mem hyp hyps
801801
|| List.exists (Termops.occur_var_in_decl env sigma hyp) keep
802802
|| Termops.occur_var env sigma hyp hyp_typ
803-
|| Termops.is_section_variable (Global.env ()) hyp
803+
|| Termops.is_section_variable' env hyp
804804
(* should be dangerous *)
805805
then (clear, decl :: keep)
806806
else (hyp :: clear, keep))

plugins/funind/glob_termops.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,7 @@ let resolve_and_replace_implicits exptyp env sigma rt =
589589
undeclared_evars_rr = false;
590590
unconstrained_sorts = false;
591591
} in
592-
let hypnaming = Evarutil.VarSet.variables (Global.env ()) in
593-
let genv = GlobEnv.make ~hypnaming env sigma Glob_ops.empty_lvar in
592+
let genv = GlobEnv.make env sigma Glob_ops.empty_lvar in
594593
let pretyper = { default_pretyper with pretype_hole; pretype_type } in
595594
let sigma', _ = eval_pretyper pretyper ~flags:pretype_flags (Some exptyp) genv sigma rt in
596595
solve_remaining_evars flags env ~initial:sigma sigma'

plugins/ltac/leminv.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,11 @@ let inversion_scheme ~name ~poly env sigma t sort dep_option inv_op =
201201
let pf = Proof.start ~name ~poly (Evd.from_ustate (ustate sigma)) [invEnv,invGoal] in
202202
let pf, _, () = Proof.run_tactic env (tclTHEN intro (onLastHypId inv_op)) pf in
203203
let pfterm = List.hd (Proof.partial_proof pf) in
204-
let global_named_context = Global.named_context_val () in
205204
let ownSign = ref begin
206205
fold_named_context
207206
(fun env d sign ->
208207
let d = EConstr.of_named_decl d in
209-
if mem_named_context_val (NamedDecl.get_id d) global_named_context then sign
208+
if NamedDecl.is_secvar d then sign
210209
else Context.Named.add d sign)
211210
invEnv ~init:Context.Named.empty
212211
end in

0 commit comments

Comments
 (0)