Skip to content

Commit ad2768b

Browse files
Merge PR coq#20374: Small algorithmic enhancements in variable clearing
Reviewed-by: SkySkimmer Co-authored-by: SkySkimmer <[email protected]>
2 parents d3045ce + 5fecc6a commit ad2768b

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

engine/evarutil.ml

+24-14
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,16 @@ let restrict_evar evd evk filter candidates =
481481
| Some [] -> raise (ClearDependencyError (*FIXME*)(Id.of_string "blah", (NoCandidatesLeft evk), None))
482482
| _ -> Evd.restrict evk filter ?candidates evd
483483

484+
let check_vars env sigma ids c =
485+
let rec check_rec c =
486+
match EConstr.destRef sigma c with
487+
| gr, _ ->
488+
let vars = vars_of_global env gr in
489+
Id.Map.iter (fun id _ -> if Id.Set.mem id vars then raise (Depends id)) ids
490+
| exception DestKO -> EConstr.iter sigma check_rec c
491+
in
492+
check_rec c
493+
484494
let rec check_and_clear_in_constr ~is_section_variable env evdref err ids ~global c =
485495
(* returns a new constr where all the evars have been 'cleaned'
486496
(ie the hypotheses ids have been removed from the contexts of
@@ -531,29 +541,29 @@ let rec check_and_clear_in_constr ~is_section_variable env evdref err ids ~globa
531541
(* Check if some rid to clear in the context of ev
532542
has dependencies in another hyp of the context of ev
533543
and transitively remember the dependency *)
534-
let check id _ =
535-
if occur_var_in_decl env !evdref id h
536-
then raise (Depends id)
544+
let () =
545+
if not @@ Id.Map.is_empty ri then
546+
NamedDecl.iter_constr (fun c -> check_vars env !evdref ri c) h
537547
in
538-
let () = Id.Map.iter check ri in
539548
(* No dependency at all, we can keep this ev's context hyp *)
540549
(ri, true::filter)
541550
with Depends id -> (Id.Map.add (NamedDecl.get_id h) id ri, false::filter)
542551
in
543552
let (rids, filter) = fold (Id.Map.empty, []) ctxt l in
544-
(* Check if some rid to clear in the context of ev has dependencies
545-
in the type of ev and adjust the source of the dependency *)
546-
let _nconcl : EConstr.t =
547-
try
548-
let nids = Id.Map.domain rids in
549-
let global = Id.Set.exists is_section_variable nids in
550-
let concl = evar_concl evi in
551-
check_and_clear_in_constr ~is_section_variable env evdref (EvarTypingBreak ev) nids ~global concl
552-
with ClearDependencyError (rid,err,where) ->
553-
raise (ClearDependencyError (Id.Map.find rid rids,err,where)) in
554553

555554
if Id.Map.is_empty rids then c
556555
else
556+
(* Check if some rid to clear in the context of ev has dependencies
557+
in the type of ev and adjust the source of the dependency *)
558+
let _nconcl : EConstr.t =
559+
try
560+
let nids = Id.Map.domain rids in
561+
let global = Id.Set.exists is_section_variable nids in
562+
let concl = evar_concl evi in
563+
check_and_clear_in_constr ~is_section_variable env evdref (EvarTypingBreak ev) nids ~global concl
564+
with ClearDependencyError (rid,err,where) ->
565+
raise (ClearDependencyError (Id.Map.find rid rids,err,where))
566+
in
557567
let origfilter = Evd.evar_filter evi in
558568
let filter = Evd.Filter.apply_subfilter origfilter filter in
559569
let evd = !evdref in

0 commit comments

Comments
 (0)