| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Names |
| 13 |
open Context |
| 14 |
open Constr |
| 15 |
open Environ |
| 16 |
open Evd |
| 17 |
open Termops |
| 18 |
open Namegen |
| 19 |
|
| 20 |
module ERelevance = EConstr.ERelevance |
| 21 |
|
| 22 |
module RelDecl = Context.Rel.Declaration |
| 23 |
module NamedDecl = Context.Named.Declaration |
| 24 |
|
| 25 |
let create_clos_infos env sigma flags = |
| 26 |
let open CClosure in |
| 27 |
let evars = Evd.evar_handler sigma in |
| 28 |
create_clos_infos ~univs:(Evd.universes sigma) ~evars flags env |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
let finalize ?abort_on_undefined_evars sigma f = |
| 36 |
let sigma = minimize_universes sigma in |
| 37 |
let uvars = ref Univ.Level.Set.empty in |
| 38 |
let nf_constr c = |
| 39 |
let _, varsc = EConstr.universes_of_constr sigma c in |
| 40 |
let c = EConstr.to_constr ?abort_on_undefined_evars sigma c in |
| 41 |
uvars := Univ.Level.Set.union !uvars varsc; |
| 42 |
c |
| 43 |
in |
| 44 |
let v = f nf_constr in |
| 45 |
let sigma = restrict_universe_context sigma !uvars in |
| 46 |
sigma, v |
| 47 |
|
| 48 |
|
| 49 |
let kind_of_term_upto = EConstr.kind_upto |
| 50 |
|
| 51 |
let nf_evars_universes sigma t = EConstr.to_constr ~abort_on_undefined_evars:false sigma (EConstr.of_constr t) |
| 52 |
let whd_evar = EConstr.whd_evar |
| 53 |
|
| 54 |
let nf_evar = Evd.MiniEConstr.nf_evar |
| 55 |
|
| 56 |
let j_nf_evar sigma j = |
| 57 |
{ uj_val = nf_evar sigma j.uj_val; |
| 58 |
uj_type = nf_evar sigma j.uj_type } |
| 59 |
let jl_nf_evar sigma jl = List.map (j_nf_evar sigma) jl |
| 60 |
let jv_nf_evar sigma = Array.map (j_nf_evar sigma) |
| 61 |
let tj_nf_evar sigma {utj_val=v;utj_type=t} = |
| 62 |
{utj_val=nf_evar sigma v;utj_type=t} |
| 63 |
|
| 64 |
let nf_relevance sigma r = |
| 65 |
UState.nf_relevance (Evd.ustate sigma) r |
| 66 |
|
| 67 |
let nf_named_context_evar sigma ctx = |
| 68 |
Context.Named.map_with_relevance (nf_relevance sigma) (nf_evars_universes sigma) ctx |
| 69 |
|
| 70 |
let nf_rel_context_evar sigma ctx = |
| 71 |
let nf_relevance r = ERelevance.make (ERelevance.kind sigma r) in |
| 72 |
Context.Rel.map_with_relevance nf_relevance (nf_evar sigma) ctx |
| 73 |
|
| 74 |
let nf_env_evar sigma env = |
| 75 |
let nc' = nf_named_context_evar sigma (Environ.named_context env) in |
| 76 |
let rel' = nf_rel_context_evar sigma (EConstr.rel_context env) in |
| 77 |
EConstr.push_rel_context rel' (reset_with_named_context (val_of_named_context nc') env) |
| 78 |
|
| 79 |
let nf_evar_info evc info = map_evar_info (nf_evar evc) info |
| 80 |
|
| 81 |
let nf_evar_map evm = |
| 82 |
Evd.raw_map { map = fun _ evi -> nf_evar_info evm evi } evm |
| 83 |
|
| 84 |
let nf_evar_map_undefined evm = |
| 85 |
Evd.raw_map_undefined (fun _ evi -> nf_evar_info evm evi) evm |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
let has_undefined_evars evd t = |
| 92 |
let rec f h t = |
| 93 |
let (h, knd) = EConstr.Expand.kind evd h t in |
| 94 |
match knd with |
| 95 |
| Evar _ -> raise NotInstantiatedEvar |
| 96 |
| _ -> EConstr.Expand.iter evd f h knd |
| 97 |
in |
| 98 |
let h, t = EConstr.Expand.make t in |
| 99 |
try let _ = f h t in false |
| 100 |
with (Not_found | NotInstantiatedEvar) -> true |
| 101 |
|
| 102 |
let has_undefined_evars_or_metas evd t = |
| 103 |
let rec has_ev t = |
| 104 |
match EConstr.kind evd t with |
| 105 |
| Evar _ | Meta _ -> raise NotInstantiatedEvar |
| 106 |
| _ -> EConstr.iter evd has_ev t in |
| 107 |
try let _ = has_ev t in false |
| 108 |
with (Not_found | NotInstantiatedEvar) -> true |
| 109 |
|
| 110 |
let is_ground_term evd t = |
| 111 |
not (has_undefined_evars evd t) |
| 112 |
|
| 113 |
let is_ground_env evd env = |
| 114 |
let is_ground_rel_decl = function |
| 115 |
| RelDecl.LocalDef (_,b,_) -> is_ground_term evd (EConstr.of_constr b) |
| 116 |
| _ -> true in |
| 117 |
let is_ground_named_decl = function |
| 118 |
| NamedDecl.LocalDef (_,b,_) -> is_ground_term evd (EConstr.of_constr b) |
| 119 |
| _ -> true in |
| 120 |
List.for_all is_ground_rel_decl (rel_context env) && |
| 121 |
List.for_all is_ground_named_decl (named_context env) |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
let whd_head_evar_stack sigma c = |
| 127 |
let rec whrec (c, l) = |
| 128 |
match EConstr.kind sigma c with |
| 129 |
| Cast (c,_,_) -> whrec (c, l) |
| 130 |
| App (f,args) -> whrec (f, args :: l) |
| 131 |
| c -> (EConstr.of_kind c, l) |
| 132 |
in |
| 133 |
whrec (c, []) |
| 134 |
|
| 135 |
let whd_head_evar sigma c = |
| 136 |
let open EConstr in |
| 137 |
let (f, args) = whd_head_evar_stack sigma c in |
| 138 |
match args with |
| 139 |
| [arg] -> mkApp (f, arg) |
| 140 |
| _ -> mkApp (f, Array.concat args) |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
let meta_counter_summary_name = "meta counter" |
| 147 |
|
| 148 |
|
| 149 |
let meta_ctr, meta_counter_summary_tag = |
| 150 |
Summary.ref_tag 0 ~name:meta_counter_summary_name |
| 151 |
|
| 152 |
let new_meta () = incr meta_ctr; !meta_ctr |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
let next_ident_away id avoid = |
| 190 |
let avoid id = Id.Set.mem id avoid in |
| 191 |
next_ident_away_from id avoid |
| 192 |
|
| 193 |
type subst_val = |
| 194 |
| SRel of int |
| 195 |
| SVar of Id.t |
| 196 |
|
| 197 |
type csubst = { |
| 198 |
csubst_len : int; |
| 199 |
|
| 200 |
csubst_var : Constr.t Id.Map.t; |
| 201 |
|
| 202 |
|
| 203 |
csubst_rel : Constr.t Int.Map.t; |
| 204 |
|
| 205 |
csubst_rev : subst_val Id.Map.t; |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
let empty_csubst = { |
| 214 |
csubst_len = 0; |
| 215 |
csubst_rel = Int.Map.empty; |
| 216 |
csubst_var = Id.Map.empty; |
| 217 |
csubst_rev = Id.Map.empty; |
| 218 |
} |
| 219 |
|
| 220 |
let csubst_subst sigma { csubst_len = k; csubst_var = v; csubst_rel = s } c = |
| 221 |
|
| 222 |
let c = EConstr.Unsafe.to_constr c in |
| 223 |
let rec subst n c = match Constr.kind c with |
| 224 |
| Rel m -> |
| 225 |
if m <= n then c |
| 226 |
else if m - n <= k then Int.Map.find (k - m + n) s |
| 227 |
else mkRel (m - k) |
| 228 |
| Var id -> |
| 229 |
begin try Id.Map.find id v with Not_found -> c end |
| 230 |
| Evar (evk, args) -> |
| 231 |
let EvarInfo evi = Evd.find sigma evk in |
| 232 |
let args' = subst_instance n (evar_filtered_context evi) args in |
| 233 |
if args' == args then c else Constr.mkEvar (evk, args') |
| 234 |
| _ -> Constr.map_with_binders succ subst n c |
| 235 |
|
| 236 |
and subst_instance n ctx args = match ctx, SList.view args with |
| 237 |
| [], None -> SList.empty |
| 238 |
| decl :: ctx, Some (c, args) -> |
| 239 |
let c' = match c with |
| 240 |
| None -> begin try Some (Id.Map.find (NamedDecl.get_id decl) v) with Not_found -> c end |
| 241 |
| Some c -> |
| 242 |
let c' = subst n c in |
| 243 |
if isVarId (NamedDecl.get_id decl) c' then None else Some c' |
| 244 |
in |
| 245 |
SList.cons_opt c' (subst_instance n ctx args) |
| 246 |
| _ :: _, None | [], Some _ -> assert false |
| 247 |
in |
| 248 |
let c = if k = 0 && Id.Map.is_empty v then c else subst 0 c in |
| 249 |
EConstr.of_constr c |
| 250 |
|
| 251 |
type ext_named_context = { |
| 252 |
ext_subst : csubst; |
| 253 |
ext_avoid : Id.Set.t; |
| 254 |
ext_ctx : named_context_val; |
| 255 |
} |
| 256 |
|
| 257 |
let ext_named_context_val ext = ext.ext_ctx |
| 258 |
let ext_csubst ext = ext.ext_subst |
| 259 |
|
| 260 |
let push_var id { csubst_len = n; csubst_var = v; csubst_rel = s; csubst_rev = r } = |
| 261 |
let s = Int.Map.add n (Constr.mkVar id) s in |
| 262 |
let r = Id.Map.add id (SRel n) r in |
| 263 |
{ csubst_len = succ n; csubst_var = v; csubst_rel = s; csubst_rev = r } |
| 264 |
|
| 265 |
*) |
| 266 |
let update_var src tgt subst = |
| 267 |
let cur = |
| 268 |
try Some (Id.Map.find src subst.csubst_rev) |
| 269 |
with Not_found -> None |
| 270 |
in |
| 271 |
match cur with |
| 272 |
| None -> |
| 273 |
*) |
| 274 |
let csubst_var = Id.Map.add src (Constr.mkVar tgt) subst.csubst_var in |
| 275 |
let csubst_rev = Id.Map.add tgt (SVar src) subst.csubst_rev in |
| 276 |
{ subst with csubst_var; csubst_rev } |
| 277 |
| Some bnd -> |
| 278 |
let csubst_rev = Id.Map.add tgt bnd (Id.Map.remove src subst.csubst_rev) in |
| 279 |
match bnd with |
| 280 |
| SRel m -> |
| 281 |
let csubst_rel = Int.Map.add m (Constr.mkVar tgt) subst.csubst_rel in |
| 282 |
{ subst with csubst_rel; csubst_rev } |
| 283 |
| SVar id -> |
| 284 |
let csubst_var = Id.Map.add id (Constr.mkVar tgt) subst.csubst_var in |
| 285 |
{ subst with csubst_var; csubst_rev } |
| 286 |
|
| 287 |
module VarSet = |
| 288 |
struct |
| 289 |
type t = Id.t -> bool |
| 290 |
let empty _ = false |
| 291 |
let full _ = true |
| 292 |
let variables env id = is_section_variable env id |
| 293 |
end |
| 294 |
|
| 295 |
type naming_mode = VarSet.t |
| 296 |
|
| 297 |
let push_rel_decl_to_named_context |
| 298 |
~hypnaming |
| 299 |
sigma decl (ext : ext_named_context) = |
| 300 |
let open EConstr in |
| 301 |
let open Vars in |
| 302 |
let map_decl f d = |
| 303 |
NamedDecl.map_constr f d |
| 304 |
in |
| 305 |
let rec replace_var_named_declaration id0 id nc = match match_named_context_val nc with |
| 306 |
| None -> empty_named_context_val |
| 307 |
| Some (decl, nc) -> |
| 308 |
if Id.equal id0 (NamedDecl.get_id decl) then |
| 309 |
|
| 310 |
push_named_context_val (NamedDecl.set_id id decl) nc |
| 311 |
else |
| 312 |
let nc = replace_var_named_declaration id0 id nc in |
| 313 |
let vsubst = [id0 , mkVar id] in |
| 314 |
push_named_context_val (map_decl (fun c -> replace_vars sigma vsubst c) decl) nc |
| 315 |
in |
| 316 |
let extract_if_neq id = function |
| 317 |
| Anonymous -> None |
| 318 |
| Name id' when Id.compare id id' = 0 -> None |
| 319 |
| Name id' -> Some id' |
| 320 |
in |
| 321 |
let na = RelDecl.get_name decl in |
| 322 |
let id = |
| 323 |
|
| 324 |
|
| 325 |
next_ident_away (id_of_name_using_hdchar empty_env sigma (RelDecl.get_type decl) na) ext.ext_avoid |
| 326 |
in |
| 327 |
match extract_if_neq id na with |
| 328 |
| Some id0 -> |
| 329 |
if hypnaming id0 then |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
let d = decl |> NamedDecl.of_rel_decl (fun _ -> id) |> map_decl (csubst_subst sigma ext.ext_subst) in |
| 335 |
{ ext_subst = push_var id ext.ext_subst; |
| 336 |
ext_avoid = Id.Set.add id ext.ext_avoid; |
| 337 |
ext_ctx = push_named_context_val d ext.ext_ctx } |
| 338 |
else |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
let subst = update_var id0 id ext.ext_subst in |
| 344 |
let d = decl |> NamedDecl.of_rel_decl (fun _ -> id0) |> map_decl (csubst_subst sigma subst) in |
| 345 |
let nc = replace_var_named_declaration id0 id ext.ext_ctx in |
| 346 |
let avoid = Id.Set.add id (Id.Set.add id0 ext.ext_avoid) in |
| 347 |
{ ext_subst = push_var id0 subst; |
| 348 |
ext_avoid = avoid; |
| 349 |
ext_ctx = push_named_context_val d nc } |
| 350 |
| None -> |
| 351 |
let d = decl |> NamedDecl.of_rel_decl (fun _ -> id) |> map_decl (csubst_subst sigma ext.ext_subst) in |
| 352 |
{ ext_subst = push_var id ext.ext_subst; |
| 353 |
ext_avoid = Id.Set.add id ext.ext_avoid; |
| 354 |
ext_ctx = push_named_context_val d ext.ext_ctx } |
| 355 |
|
| 356 |
let csubst_instance subst ctx = |
| 357 |
let fold decl accu = match Id.Map.find (NamedDecl.get_id decl) subst.csubst_rev with |
| 358 |
| SRel n -> SList.cons (EConstr.mkRel (subst.csubst_len - n)) accu |
| 359 |
| SVar id -> SList.cons (EConstr.mkVar id) accu |
| 360 |
| exception Not_found -> SList.default accu |
| 361 |
in |
| 362 |
List.fold_right fold ctx SList.empty |
| 363 |
|
| 364 |
let ext_rev_subst { ext_subst = subst } id0 = |
| 365 |
match Id.Map.find id0 subst.csubst_rev with |
| 366 |
| SRel n -> EConstr.mkRel (subst.csubst_len - n) |
| 367 |
| SVar id -> EConstr.mkVar id |
| 368 |
|
| 369 |
let default_ext_instance { ext_subst = subst; ext_ctx = ctx } = |
| 370 |
csubst_instance subst (named_context_of_val ctx) |
| 371 |
|
| 372 |
let push_rel_context_to_named_context ~hypnaming env sigma typ = |
| 373 |
|
| 374 |
let open EConstr in |
| 375 |
let ctx = named_context_val env in |
| 376 |
if List.is_empty (Environ.rel_context env) then |
| 377 |
let inst = SList.defaultn (List.length @@ named_context_of_val ctx) SList.empty in |
| 378 |
(ctx, typ, inst, empty_csubst) |
| 379 |
else |
| 380 |
let avoid = Environ.ids_of_named_context_val (named_context_val env) in |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
let init = { ext_subst = empty_csubst; ext_avoid = avoid; ext_ctx = ctx } in |
| 385 |
let ext = |
| 386 |
Context.Rel.fold_outside (fun d acc -> push_rel_decl_to_named_context ~hypnaming sigma d acc) |
| 387 |
(rel_context env) ~init in |
| 388 |
let inst = default_ext_instance ext in |
| 389 |
(ext.ext_ctx, csubst_subst sigma ext.ext_subst typ, inst, ext.ext_subst) |
| 390 |
|
| 391 |
let ext_named_context_of_env ~hypnaming env sigma = |
| 392 |
let avoid = Environ.ids_of_named_context_val (Environ.named_context_val env) in |
| 393 |
let init = { ext_subst = empty_csubst; ext_avoid = avoid; ext_ctx = named_context_val env } in |
| 394 |
Context.Rel.fold_outside (fun d acc -> push_rel_decl_to_named_context ~hypnaming sigma d acc) |
| 395 |
(EConstr.rel_context env) ~init |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
let new_pure_evar = Evd.new_pure_evar |
| 402 |
|
| 403 |
let next_evar_name naming = match naming with |
| 404 |
| IntroAnonymous -> None |
| 405 |
| IntroFresh id -> Some (id, true) |
| 406 |
| IntroIdentifier id -> Some (id, false) |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
let new_evar ?src ?filter ?relevance ?abstract_arguments ?candidates ?(naming = IntroAnonymous) ?parent ?typeclass_candidate |
| 411 |
?rrpat ?hypnaming env evd typ = |
| 412 |
let name = next_evar_name naming in |
| 413 |
let hypnaming = match hypnaming with |
| 414 |
| Some n -> n |
| 415 |
| None -> VarSet.variables (Global.env ()) |
| 416 |
in |
| 417 |
let sign,typ',instance,subst = push_rel_context_to_named_context ~hypnaming env evd typ in |
| 418 |
let map c = csubst_subst evd subst c in |
| 419 |
let candidates = Option.map (fun l -> List.map map l) candidates in |
| 420 |
let instance = |
| 421 |
match filter with |
| 422 |
| None -> instance |
| 423 |
| Some filter -> Filter.filter_slist filter instance in |
| 424 |
let relevance = match relevance with |
| 425 |
| Some r -> r |
| 426 |
| None -> ERelevance.relevant |
| 427 |
in |
| 428 |
let (evd, evk) = new_pure_evar sign evd typ' ?src ?rrpat ?filter ~relevance ?abstract_arguments ?candidates ?name ?parent |
| 429 |
?typeclass_candidate in |
| 430 |
(evd, EConstr.mkEvar (evk, instance)) |
| 431 |
|
| 432 |
let new_type_evar ?src ?filter ?naming ?hypnaming env evd rigid = |
| 433 |
let (evd', s) = new_sort_variable rigid evd in |
| 434 |
let relevance = EConstr.ESorts.relevance_of_sort s in |
| 435 |
let (evd', e) = new_evar env evd' ?src ?filter ~relevance ?naming ~typeclass_candidate:false ?hypnaming (EConstr.mkSort s) in |
| 436 |
evd', (e, s) |
| 437 |
|
| 438 |
let new_Type ?(rigid=Evd.univ_flexible) evd = |
| 439 |
let open EConstr in |
| 440 |
let (evd, s) = new_sort_variable rigid evd in |
| 441 |
(evd, mkSort s) |
| 442 |
|
| 443 |
|
| 444 |
type unification_pb = conv_pb * env * EConstr.constr * EConstr.constr |
| 445 |
|
| 446 |
let eq_unification_pb evd (pbty,env,t1,t2) (pbty',env',t1',t2') = |
| 447 |
pbty == pbty' && env == env' && |
| 448 |
EConstr.eq_constr evd t1 t1' && |
| 449 |
EConstr.eq_constr evd t2 t2' |
| 450 |
|
| 451 |
let add_unification_pb ?(tail=false) pb evd = |
| 452 |
let conv_pbs = Evd.conv_pbs evd in |
| 453 |
if not (List.exists (eq_unification_pb evd pb) conv_pbs) then |
| 454 |
let (pbty,env,t1,t2) = pb in |
| 455 |
Evd.add_conv_pb ~tail (pbty,env,t1,t2) evd |
| 456 |
else evd |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
let generalize_evar_over_rels sigma (ev,args) = |
| 461 |
let open EConstr in |
| 462 |
let evi = Evd.find_undefined sigma ev in |
| 463 |
let args = Evd.expand_existential sigma (ev, args) in |
| 464 |
let sign = named_context_of_val (Evd.evar_hyps evi) in |
| 465 |
List.fold_left2 |
| 466 |
(fun (c,inst as x) a d -> |
| 467 |
if isRel sigma a then (mkNamedProd_or_LetIn sigma d c,a::inst) else x) |
| 468 |
(Evd.evar_concl evi,[]) args sign |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
type clear_dependency_error = |
| 475 |
| OccurHypInSimpleClause of Id.t option |
| 476 |
| EvarTypingBreak of EConstr.existential |
| 477 |
| NoCandidatesLeft of Evar.t |
| 478 |
|
| 479 |
exception ClearDependencyError of Id.t * clear_dependency_error * GlobRef.t option |
| 480 |
|
| 481 |
exception Depends of Id.t |
| 482 |
|
| 483 |
let set_of_evctx l = |
| 484 |
List.fold_left (fun s decl -> Id.Set.add (NamedDecl.get_id decl) s) Id.Set.empty l |
| 485 |
|
| 486 |
let filter_effective_candidates evd evi filter candidates = |
| 487 |
let ids = set_of_evctx (Filter.filter_list filter (evar_context evi)) in |
| 488 |
List.filter (fun a -> Id.Set.subset (collect_vars evd a) ids) candidates |
| 489 |
|
| 490 |
let restrict_evar evd evk filter candidates = |
| 491 |
let evar_info = Evd.find_undefined evd evk in |
| 492 |
let candidates = Option.map (filter_effective_candidates evd evar_info filter) candidates in |
| 493 |
match candidates with |
| 494 |
| Some [] -> raise (ClearDependencyError (Id.of_string "blah", (NoCandidatesLeft evk), None)) |
| 495 |
| _ -> Evd.restrict evk filter ?candidates evd |
| 496 |
|
| 497 |
|
| 498 |
let vars_of_global env sigma gr = |
| 499 |
let open GlobRef in |
| 500 |
match gr with |
| 501 |
| ConstRef cst -> |
| 502 |
let eff = Evd.eval_side_effects sigma in |
| 503 |
if Safe_typing.is_empty_private_constants @@ Evd.seff_private @@ eff then |
| 504 |
Environ.vars_of_global env gr |
| 505 |
else |
| 506 |
let senv = Evd.get_senv_side_effects eff in |
| 507 |
Environ.vars_of_global (Safe_typing.env_of_safe_env senv) gr |
| 508 |
| VarRef _ | IndRef _ | ConstructRef _ -> Environ.vars_of_global env gr |
| 509 |
|
| 510 |
let check_vars env sigma ids c = |
| 511 |
let rec check_rec c = |
| 512 |
match EConstr.destRef sigma c with |
| 513 |
| gr, _ -> |
| 514 |
let vars = vars_of_global env sigma gr in |
| 515 |
Id.Map.iter (fun id _ -> if Id.Set.mem id vars then raise (Depends id)) ids |
| 516 |
| exception DestKO -> EConstr.iter sigma check_rec c |
| 517 |
in |
| 518 |
check_rec c |
| 519 |
|
| 520 |
let rec check_and_clear_in_constr ~is_section_variable env evdref err ids ~global c = |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
match EConstr.kind !evdref c with |
| 526 |
| Var id' -> |
| 527 |
if Id.Set.mem id' ids then raise (ClearDependencyError (id', err, None)) else c |
| 528 |
|
| 529 |
| ( Const _ | Ind _ | Construct _ ) as ref -> |
| 530 |
let () = if global then |
| 531 |
let r = match ref with |
| 532 |
| Const (c, _) -> GlobRef.ConstRef c |
| 533 |
| Ind (ind, _) -> IndRef ind |
| 534 |
| Construct (c, _) -> ConstructRef c |
| 535 |
| _ -> assert false |
| 536 |
in |
| 537 |
let check id' = |
| 538 |
if Id.Set.mem id' ids then |
| 539 |
raise (ClearDependencyError (id',err,Some r)) |
| 540 |
in |
| 541 |
Id.Set.iter check (vars_of_global env !evdref r) |
| 542 |
in |
| 543 |
c |
| 544 |
|
| 545 |
| Evar (evk,l as ev) -> |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
let evi = Evd.find_undefined !evdref evk in |
| 552 |
let ctxt = Evd.evar_filtered_context evi in |
| 553 |
let rec fold accu ctxt args = match ctxt, SList.view args with |
| 554 |
| [], Some _ | _ :: _, None -> assert false |
| 555 |
| [], None -> accu |
| 556 |
| h :: ctxt, Some (a, args) -> |
| 557 |
let (ri, filter) = fold accu ctxt args in |
| 558 |
try |
| 559 |
|
| 560 |
|
| 561 |
let check id = if Id.Set.mem id ids then raise (Depends id) in |
| 562 |
let a = match a with |
| 563 |
| None -> Id.Set.singleton (NamedDecl.get_id h) |
| 564 |
| Some a -> collect_vars !evdref a |
| 565 |
in |
| 566 |
let () = Id.Set.iter check a in |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
let () = |
| 571 |
if not @@ Id.Map.is_empty ri then |
| 572 |
NamedDecl.iter_constr (fun c -> check_vars env !evdref ri c) h |
| 573 |
in |
| 574 |
|
| 575 |
(ri, true::filter) |
| 576 |
with Depends id -> (Id.Map.add (NamedDecl.get_id h) id ri, false::filter) |
| 577 |
in |
| 578 |
let (rids, filter) = fold (Id.Map.empty, []) ctxt l in |
| 579 |
|
| 580 |
if Id.Map.is_empty rids then c |
| 581 |
else |
| 582 |
|
| 583 |
|
| 584 |
let _nconcl : EConstr.t = |
| 585 |
try |
| 586 |
let nids = Id.Map.domain rids in |
| 587 |
let global = Id.Set.exists is_section_variable nids in |
| 588 |
let concl = evar_concl evi in |
| 589 |
check_and_clear_in_constr ~is_section_variable env evdref (EvarTypingBreak ev) nids ~global concl |
| 590 |
with ClearDependencyError (rid,err,where) -> |
| 591 |
raise (ClearDependencyError (Id.Map.find rid rids,err,where)) |
| 592 |
in |
| 593 |
let origfilter = Evd.evar_filter evi in |
| 594 |
let filter = Evd.Filter.apply_subfilter origfilter filter in |
| 595 |
let evd = !evdref in |
| 596 |
let candidates = Evd.evar_candidates evi in |
| 597 |
let (evd,_) = restrict_evar evd evk filter candidates in |
| 598 |
evdref := evd; |
| 599 |
Evd.existential_value !evdref ev |
| 600 |
|
| 601 |
| _ -> EConstr.map !evdref (check_and_clear_in_constr ~is_section_variable env evdref err ids ~global) c |
| 602 |
|
| 603 |
let clear_hyps_in_evi_main env sigma hyps terms ids = |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
let evdref = ref sigma in |
| 608 |
let is_section_variable id = is_section_variable (Global.env ()) id in |
| 609 |
let global = Id.Set.exists is_section_variable ids in |
| 610 |
let terms = |
| 611 |
List.map (check_and_clear_in_constr ~is_section_variable env evdref (OccurHypInSimpleClause None) ids ~global) terms in |
| 612 |
let nhyps = |
| 613 |
let check_context decl = |
| 614 |
let decl = EConstr.of_named_decl decl in |
| 615 |
let err = OccurHypInSimpleClause (Some (NamedDecl.get_id decl)) in |
| 616 |
EConstr.Unsafe.to_named_decl @@ NamedDecl.map_constr (check_and_clear_in_constr ~is_section_variable env evdref err ids ~global) decl |
| 617 |
in |
| 618 |
remove_hyps ids check_context hyps |
| 619 |
in |
| 620 |
(!evdref, nhyps, terms) |
| 621 |
|
| 622 |
let check_and_clear_in_constr env evd err ids c = |
| 623 |
let evdref = ref evd in |
| 624 |
let _ : EConstr.constr = check_and_clear_in_constr |
| 625 |
~is_section_variable:(fun _ -> true) ~global:true |
| 626 |
env evdref err ids c |
| 627 |
in |
| 628 |
!evdref |
| 629 |
|
| 630 |
let clear_hyps_in_evi env sigma hyps concl ids = |
| 631 |
match clear_hyps_in_evi_main env sigma hyps [concl] ids with |
| 632 |
| (sigma,nhyps,[nconcl]) -> (sigma,nhyps,nconcl) |
| 633 |
| _ -> assert false |
| 634 |
|
| 635 |
let clear_hyps2_in_evi env sigma hyps t concl ids = |
| 636 |
match clear_hyps_in_evi_main env sigma hyps [t;concl] ids with |
| 637 |
| (sigma,nhyps,[t;nconcl]) -> (sigma,nhyps,t,nconcl) |
| 638 |
| _ -> assert false |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
let rec advance sigma evk = |
| 652 |
if Evd.is_undefined sigma evk then Some evk |
| 653 |
else |
| 654 |
match is_aliased_evar sigma evk with |
| 655 |
| Some evk -> advance sigma evk |
| 656 |
| None -> None |
| 657 |
|
| 658 |
let reachable_from_evars sigma evars = |
| 659 |
let aliased = Evd.get_aliased_evars sigma in |
| 660 |
let rec search evk visited = |
| 661 |
if Evar.Set.mem evk visited then visited |
| 662 |
else |
| 663 |
let visited = Evar.Set.add evk visited in |
| 664 |
match Evar.Map.find evk aliased with |
| 665 |
| evk' -> search evk' visited |
| 666 |
| exception Not_found -> visited |
| 667 |
in |
| 668 |
Evar.Set.fold (fun evk visited -> search evk visited) evars Evar.Set.empty |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
let undefined_evars_of_term evd t = |
| 676 |
let rec evrec acc c = |
| 677 |
match EConstr.kind evd c with |
| 678 |
| Evar (n, l) -> |
| 679 |
let acc = Evar.Set.add n acc in |
| 680 |
SList.Skip.fold evrec acc l |
| 681 |
| _ -> EConstr.fold evd evrec acc c |
| 682 |
in |
| 683 |
evrec Evar.Set.empty t |
| 684 |
|
| 685 |
let undefined_evars_of_named_context evd nc = |
| 686 |
Context.Named.fold_outside |
| 687 |
(NamedDecl.fold_constr (fun c s -> Evar.Set.union s (undefined_evars_of_term evd (EConstr.of_constr c)))) |
| 688 |
nc |
| 689 |
~init:Evar.Set.empty |
| 690 |
|
| 691 |
type undefined_evars_cache = { |
| 692 |
mutable cache : (EConstr.named_declaration * Evar.Set.t) ref Id.Map.t; |
| 693 |
} |
| 694 |
|
| 695 |
let create_undefined_evars_cache () = { cache = Id.Map.empty; } |
| 696 |
|
| 697 |
let cached_evar_of_hyp cache sigma decl accu = match cache with |
| 698 |
| None -> |
| 699 |
let fold c acc = |
| 700 |
let evs = undefined_evars_of_term sigma c in |
| 701 |
Evar.Set.union evs acc |
| 702 |
in |
| 703 |
NamedDecl.fold_constr fold decl accu |
| 704 |
| Some cache -> |
| 705 |
let id = NamedDecl.get_annot decl in |
| 706 |
let r = |
| 707 |
try Id.Map.find id.binder_name cache.cache |
| 708 |
with Not_found -> |
| 709 |
|
| 710 |
let r = ref (NamedDecl.LocalAssum (id, EConstr.mkProp), Evar.Set.empty) in |
| 711 |
let () = cache.cache <- Id.Map.add id.binder_name r cache.cache in |
| 712 |
r |
| 713 |
in |
| 714 |
let (decl', evs) = !r in |
| 715 |
let evs = |
| 716 |
if NamedDecl.equal (==) (==) decl decl' then snd !r |
| 717 |
else |
| 718 |
let fold c acc = |
| 719 |
let evs = undefined_evars_of_term sigma c in |
| 720 |
Evar.Set.union evs acc |
| 721 |
in |
| 722 |
let evs = NamedDecl.fold_constr fold decl Evar.Set.empty in |
| 723 |
let () = r := (decl, evs) in |
| 724 |
evs |
| 725 |
in |
| 726 |
Evar.Set.fold Evar.Set.add evs accu |
| 727 |
|
| 728 |
let filtered_undefined_evars_of_evar_info (type a) ?cache sigma (evi : a evar_info) = |
| 729 |
let evars_of_named_context cache accu nc = |
| 730 |
let fold decl accu = cached_evar_of_hyp cache sigma (EConstr.of_named_decl decl) accu in |
| 731 |
Context.Named.fold_outside fold nc ~init:accu |
| 732 |
in |
| 733 |
let accu = match Evd.evar_body evi with |
| 734 |
| Evar_empty -> undefined_evars_of_term sigma (Evd.evar_concl evi) |
| 735 |
| Evar_defined b -> evars_of_term sigma b |
| 736 |
in |
| 737 |
let ctxt = EConstr.Unsafe.to_named_context (evar_filtered_context evi) in |
| 738 |
evars_of_named_context cache accu ctxt |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
let occur_evar_upto sigma n c = |
| 745 |
let rec occur_rec c = match EConstr.kind sigma c with |
| 746 |
| Evar (evk, _) -> if Evar.equal evk n then raise Occur |
| 747 |
| _ -> EConstr.iter sigma occur_rec c |
| 748 |
in |
| 749 |
try occur_rec c; false with Occur -> true |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
let judge_of_new_Type evd = |
| 755 |
let open EConstr in |
| 756 |
let (evd', s) = new_sort_variable univ_rigid evd in |
| 757 |
(evd', { uj_val = mkSort s; uj_type = mkSort (ESorts.super evd s) }) |
| 758 |
|
| 759 |
let subterm_source evk ?where (loc,k) = |
| 760 |
let evk = match k with |
| 761 |
| Evar_kinds.SubEvar (None,evk) when where = None -> evk |
| 762 |
| _ -> evk in |
| 763 |
(loc,Evar_kinds.SubEvar (where,evk)) |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
let compare_cumulative_instances cv_pb variances u u' sigma = |
| 768 |
let open UnivProblem in |
| 769 |
let cstrs = Univ.UnivConstraints.empty in |
| 770 |
let soft = Set.empty in |
| 771 |
let qs, us = UVars.Instance.to_array u |
| 772 |
and qs', us' = UVars.Instance.to_array u' in |
| 773 |
let qcstrs = enforce_eq_qualities qs qs' Set.empty in |
| 774 |
match Evd.add_constraints sigma qcstrs with |
| 775 |
| exception UGraph.UniverseInconsistency p -> Inr p |
| 776 |
| sigma -> |
| 777 |
let cstrs, soft = Array.fold_left3 (fun (cstrs, soft) v u u' -> |
| 778 |
let open UVars.Variance in |
| 779 |
match v with |
| 780 |
| Irrelevant -> cstrs, Set.add (UWeak (u,u')) soft |
| 781 |
| Covariant when cv_pb == Conversion.CUMUL -> |
| 782 |
Univ.UnivConstraints.add (u,Univ.UnivConstraint.Le,u') cstrs, soft |
| 783 |
| Covariant | Invariant -> Univ.UnivConstraints.add (u,Univ.UnivConstraint.Eq,u') cstrs, soft) |
| 784 |
(cstrs,soft) variances us us' |
| 785 |
in |
| 786 |
match Evd.add_univ_constraints sigma cstrs with |
| 787 |
| sigma -> |
| 788 |
Inl (Evd.add_constraints sigma soft) |
| 789 |
| exception UGraph.UniverseInconsistency p -> Inr p |
| 790 |
|
| 791 |
let compare_constructor_instances evd u u' = |
| 792 |
let open UnivProblem in |
| 793 |
let qs, us = UVars.Instance.to_array u |
| 794 |
and qs', us' = UVars.Instance.to_array u' in |
| 795 |
let qcstrs = enforce_eq_qualities qs qs' Set.empty in |
| 796 |
match Evd.add_constraints evd qcstrs with |
| 797 |
| exception UGraph.UniverseInconsistency p -> Inr p |
| 798 |
| evd -> |
| 799 |
let soft = |
| 800 |
Array.fold_left2 (fun cs u u' -> Set.add (UWeak (u,u')) cs) |
| 801 |
Set.empty us us' |
| 802 |
in |
| 803 |
Inl (Evd.add_constraints evd soft) |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
let eq_constr_univs_test ~evd ~extended_evd t u = |
| 811 |
|
| 812 |
let open Evd in |
| 813 |
let t = EConstr.Unsafe.to_constr t |
| 814 |
and u = EConstr.Unsafe.to_constr u in |
| 815 |
let sigma = ref extended_evd in |
| 816 |
let eq_universes _ u1 u2 = |
| 817 |
let u1 = EConstr.EInstance.(kind !sigma (make u1)) in |
| 818 |
let u2 = EConstr.EInstance.(kind !sigma (make u2)) in |
| 819 |
let check_qeq q1 q2 = UState.check_eq_quality (Evd.ustate !sigma) q1 q2 in |
| 820 |
UGraph.check_eq_instances check_qeq (universes !sigma) u1 u2 |
| 821 |
in |
| 822 |
let eq_sorts s1 s2 = |
| 823 |
if Sorts.equal s1 s2 then true |
| 824 |
else |
| 825 |
try sigma := add_constraints !sigma UnivProblem.(Set.singleton (UEq (s1, s2))); true |
| 826 |
with UGraph.UniverseInconsistency _ | UniversesDiffer -> false |
| 827 |
in |
| 828 |
let eq_existential eq e1 e2 = |
| 829 |
let eq c1 c2 = eq 0 (EConstr.Unsafe.to_constr c1) (EConstr.Unsafe.to_constr c2) in |
| 830 |
EConstr.eq_existential evd eq (EConstr.of_existential e1) (EConstr.of_existential e2) |
| 831 |
in |
| 832 |
let kind1 = kind_of_term_upto evd in |
| 833 |
let kind2 = kind_of_term_upto extended_evd in |
| 834 |
let rec eq_constr' nargs m n = |
| 835 |
Constr.compare_head_gen_with kind1 kind2 eq_universes eq_sorts (eq_existential eq_constr') eq_constr' nargs m n |
| 836 |
in |
| 837 |
Constr.compare_head_gen_with kind1 kind2 eq_universes eq_sorts (eq_existential eq_constr') eq_constr' 0 t u |