| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
open Util |
| 20 |
open Names |
| 21 |
open Declarations |
| 22 |
open Mod_declarations |
| 23 |
|
| 24 |
module NamedDecl = Context.Named.Declaration |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
let modcache = ref (ModPath.Map.empty : structure_body ModPath.Map.t) |
| 32 |
|
| 33 |
let rec search_mod_label lab = function |
| 34 |
| [] -> raise Not_found |
| 35 |
| (l, SFBmodule mb) :: _ when Id.equal l lab -> mb |
| 36 |
| _ :: fields -> search_mod_label lab fields |
| 37 |
|
| 38 |
let rec search_cst_label lab = function |
| 39 |
| [] -> raise Not_found |
| 40 |
| (l, SFBconst cb) :: _ when Id.equal l lab -> cb |
| 41 |
| _ :: fields -> search_cst_label lab fields |
| 42 |
|
| 43 |
let rec search_mind_label lab = function |
| 44 |
| [] -> raise Not_found |
| 45 |
| (l, SFBmind mind) :: _ when Id.equal l lab -> mind |
| 46 |
| _ :: fields -> search_mind_label lab fields |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
let rec fields_of_functor f subs mp0 args = function |
| 57 |
| NoFunctor a -> f subs mp0 args a |
| 58 |
| MoreFunctor (mbid,_,e) -> |
| 59 |
let open Mod_subst in |
| 60 |
match args with |
| 61 |
| [] -> assert false |
| 62 |
| mpa :: args -> |
| 63 |
let subs = join (map_mbid mbid mpa (empty_delta_resolver mpa) ) subs in |
| 64 |
fields_of_functor f subs mp0 args e |
| 65 |
|
| 66 |
let rec lookup_module_in_impl mp = |
| 67 |
match mp with |
| 68 |
| MPfile _ -> Global.lookup_module mp |
| 69 |
| MPbound _ -> Global.lookup_module mp |
| 70 |
| MPdot (mp',lab') -> |
| 71 |
if ModPath.equal mp' (Global.current_modpath ()) then |
| 72 |
Global.lookup_module mp |
| 73 |
else |
| 74 |
let fields = memoize_fields_of_mp mp' in |
| 75 |
search_mod_label lab' fields |
| 76 |
|
| 77 |
and memoize_fields_of_mp mp = |
| 78 |
try ModPath.Map.find mp !modcache |
| 79 |
with Not_found -> |
| 80 |
let l = fields_of_mp mp in |
| 81 |
modcache := ModPath.Map.add mp l !modcache; |
| 82 |
l |
| 83 |
|
| 84 |
and fields_of_mp mp = |
| 85 |
let open Mod_subst in |
| 86 |
let mb = lookup_module_in_impl mp in |
| 87 |
let fields,inner_mp,subs = fields_of_mb empty_subst mp mb [] in |
| 88 |
let delta_mb = mod_delta mb in |
| 89 |
let subs = |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if ModPath.equal inner_mp mp then subs |
| 95 |
else if has_root_delta_resolver mp delta_mb then |
| 96 |
add_mp inner_mp mp delta_mb subs |
| 97 |
else |
| 98 |
add_mp inner_mp mp (empty_delta_resolver mp) subs |
| 99 |
in |
| 100 |
Modops.subst_structure subs mp fields |
| 101 |
|
| 102 |
and fields_of_mb subs mp mb args = match Mod_declarations.mod_expr mb with |
| 103 |
| Algebraic expr -> fields_of_expression subs mp args (mod_type mb) expr |
| 104 |
| Struct (_, sign) -> |
| 105 |
let sign = Modops.annotate_struct_body sign (mod_type mb) in |
| 106 |
fields_of_signature subs mp args sign |
| 107 |
| Abstract|FullStruct -> fields_of_signature subs mp args (mod_type mb) |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
and fields_of_signature x = |
| 112 |
fields_of_functor |
| 113 |
(fun subs mp0 args struc -> |
| 114 |
assert (List.is_empty args); |
| 115 |
(struc, mp0, subs)) x |
| 116 |
|
| 117 |
and fields_of_expr subs mp0 args = function |
| 118 |
| MEident mp -> |
| 119 |
let mp = Mod_subst.subst_mp subs mp in |
| 120 |
let mb = lookup_module_in_impl mp in |
| 121 |
fields_of_mb subs mp mb args |
| 122 |
| MEapply (me1,mp2) -> fields_of_expr subs mp0 (mp2::args) me1 |
| 123 |
| MEwith _ -> assert false |
| 124 |
|
| 125 |
and fields_of_expression subs mp args mty me = |
| 126 |
let me = Modops.annotate_module_expression me mty in |
| 127 |
fields_of_functor fields_of_expr subs mp args me |
| 128 |
|
| 129 |
let lookup_constant_in_impl cst fallback = |
| 130 |
try |
| 131 |
let mp,lab = KerName.repr (Constant.canonical cst) in |
| 132 |
let fields = memoize_fields_of_mp mp in |
| 133 |
|
| 134 |
|
| 135 |
search_cst_label lab fields |
| 136 |
with Not_found -> |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
match fallback with |
| 143 |
| Some cb -> cb |
| 144 |
| None -> |
| 145 |
CErrors.anomaly |
| 146 |
Pp.(str "Print Assumption: unknown constant " |
| 147 |
++ Constant.print cst ++ str ".") |
| 148 |
|
| 149 |
let lookup_constant cst = |
| 150 |
let env = Global.env() in |
| 151 |
if not (Environ.mem_constant cst env) |
| 152 |
then lookup_constant_in_impl cst None |
| 153 |
else |
| 154 |
let cb = Environ.lookup_constant cst env in |
| 155 |
if Declareops.constant_has_body cb then cb |
| 156 |
else lookup_constant_in_impl cst (Some cb) |
| 157 |
|
| 158 |
let lookup_mind_in_impl mind = |
| 159 |
try |
| 160 |
let mp,lab = KerName.repr (MutInd.canonical mind) in |
| 161 |
let fields = memoize_fields_of_mp mp in |
| 162 |
search_mind_label lab fields |
| 163 |
with Not_found -> |
| 164 |
CErrors.anomaly |
| 165 |
Pp.(str "Print Assumption: unknown inductive " |
| 166 |
++ MutInd.print mind ++ str ".") |
| 167 |
|
| 168 |
let lookup_mind mind = |
| 169 |
let env = Global.env() in |
| 170 |
if Environ.mem_mind mind env then Environ.lookup_mind mind env |
| 171 |
else lookup_mind_in_impl mind |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
let fold_with_full_binders g f n acc c = |
| 177 |
let open Context.Rel.Declaration in |
| 178 |
let open Constr in |
| 179 |
match kind c with |
| 180 |
| Rel _ | Meta _ | Var _ | Sort _ | Const _ | Ind _ | Construct _ | Int _ | Float _ | String _ -> acc |
| 181 |
| Cast (c,_, t) -> f n (f n acc c) t |
| 182 |
| Prod (na,t,c) -> f (g (LocalAssum (na,t)) n) (f n acc t) c |
| 183 |
| Lambda (na,t,c) -> f (g (LocalAssum (na,t)) n) (f n acc t) c |
| 184 |
| LetIn (na,b,t,c) -> f (g (LocalDef (na,b,t)) n) (f n (f n acc b) t) c |
| 185 |
| App (c,l) -> Array.fold_left (f n) (f n acc c) l |
| 186 |
| Proj (_,_,c) -> f n acc c |
| 187 |
| Evar _ -> assert false |
| 188 |
| Case (ci, u, pms, p, iv, c, bl) -> |
| 189 |
let mib = lookup_mind (fst ci.ci_ind) in |
| 190 |
let (ci, (p,_), iv, c, bl) = Inductive.expand_case_specif mib (ci, u, pms, p, iv, c, bl) in |
| 191 |
Array.fold_left (f n) (f n (fold_invert (f n) (f n acc p) iv) c) bl |
| 192 |
| Fix (_,(lna,tl,bl)) -> |
| 193 |
let n' = CArray.fold_left2_i (fun i c n t -> g (LocalAssum (n,lift i t)) c) n lna tl in |
| 194 |
let fd = Array.map2 (fun t b -> (t,b)) tl bl in |
| 195 |
Array.fold_left (fun acc (t,b) -> f n' (f n acc t) b) acc fd |
| 196 |
| CoFix (_,(lna,tl,bl)) -> |
| 197 |
let n' = CArray.fold_left2_i (fun i c n t -> g (LocalAssum (n,lift i t)) c) n lna tl in |
| 198 |
let fd = Array.map2 (fun t b -> (t,b)) tl bl in |
| 199 |
Array.fold_left (fun acc (t,b) -> f n' (f n acc t) b) acc fd |
| 200 |
| Array(_u,t,def,ty) -> f n (f n (Array.fold_left (f n) acc t) def) ty |
| 201 |
|
| 202 |
let get_constant_body access kn = |
| 203 |
let cb = lookup_constant kn in |
| 204 |
match cb.const_body with |
| 205 |
| Undef _ | Primitive _ | Symbol _ -> None |
| 206 |
| Def c -> Some c |
| 207 |
| OpaqueDef o -> |
| 208 |
match Global.force_proof access o with |
| 209 |
| c, _ -> Some c |
| 210 |
| exception e when CErrors.noncritical e -> None |
| 211 |
|
| 212 |
let rec traverse access (current:GlobRef.t) ctx accu t = |
| 213 |
let open GlobRef in |
| 214 |
let open Constr in |
| 215 |
match Constr.kind t with |
| 216 |
| Var id -> |
| 217 |
let body () = id |> Global.lookup_named |> NamedDecl.get_value in |
| 218 |
traverse_object access accu body (VarRef id) |
| 219 |
| Const (kn, _) -> |
| 220 |
let body () = get_constant_body access kn in |
| 221 |
traverse_object access accu body (ConstRef kn) |
| 222 |
| Ind ((mind, _) as ind, _) -> |
| 223 |
traverse_inductive access accu mind (IndRef ind) |
| 224 |
| Construct (((mind, _), _) as cst, _) -> |
| 225 |
traverse_inductive access accu mind (ConstructRef cst) |
| 226 |
| Meta _ | Evar _ -> assert false |
| 227 |
| Case (_, _, _, (([|_|], oty),_), _, c, [||]) when Vars.noccurn 1 oty -> |
| 228 |
|
| 229 |
begin match Constr.kind c with |
| 230 |
| Const (kn, _) when not (Declareops.constant_has_body (lookup_constant kn)) -> |
| 231 |
let (curr, data, ax2ty) = accu in |
| 232 |
let obj = ConstRef kn in |
| 233 |
let already_in = GlobRef.Map_env.mem obj data in |
| 234 |
let data = if not already_in then GlobRef.Map_env.add obj None data else data in |
| 235 |
let ty = (current, ctx, Vars.subst1 mkProp oty) in |
| 236 |
let ax2ty = |
| 237 |
try let l = GlobRef.Map_env.find obj ax2ty in GlobRef.Map_env.add obj (ty::l) ax2ty |
| 238 |
with Not_found -> GlobRef.Map_env.add obj [ty] ax2ty in |
| 239 |
(GlobRef.Set_env.add obj curr, data, ax2ty) |
| 240 |
| _ -> |
| 241 |
fold_with_full_binders |
| 242 |
Context.Rel.add (traverse access current) ctx accu t |
| 243 |
end |
| 244 |
| _ -> fold_with_full_binders |
| 245 |
Context.Rel.add (traverse access current) ctx accu t |
| 246 |
|
| 247 |
and traverse_object access (curr, data, ax2ty) body obj = |
| 248 |
let data, ax2ty = |
| 249 |
let already_in = GlobRef.Map_env.mem obj data in |
| 250 |
if already_in then data, ax2ty |
| 251 |
else match body () with |
| 252 |
| None -> |
| 253 |
|
| 254 |
let data = GlobRef.Map_env.add obj None data in |
| 255 |
begin match obj with |
| 256 |
| GlobRef.ConstRef kn -> |
| 257 |
let cb = lookup_constant kn in |
| 258 |
let typ = cb.Declarations.const_type in |
| 259 |
let _, data, ax2ty = |
| 260 |
traverse access obj Context.Rel.empty |
| 261 |
(GlobRef.Set_env.empty, data, ax2ty) typ in |
| 262 |
data, ax2ty |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
| GlobRef.VarRef _ | GlobRef.IndRef _ | GlobRef.ConstructRef _ -> data, ax2ty end |
| 267 |
| Some body -> |
| 268 |
let contents,data,ax2ty = |
| 269 |
traverse access obj Context.Rel.empty |
| 270 |
(GlobRef.Set_env.empty,data,ax2ty) body in |
| 271 |
GlobRef.Map_env.add obj (Some contents) data, ax2ty |
| 272 |
in |
| 273 |
(GlobRef.Set_env.add obj curr, data, ax2ty) |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
and traverse_inductive access (curr, data, ax2ty) mind obj = |
| 280 |
let firstind_ref = (GlobRef.IndRef (mind, 0)) in |
| 281 |
let data, ax2ty = |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
if |
| 286 |
GlobRef.Set_env.mem firstind_ref curr || |
| 287 |
GlobRef.Map_env.mem firstind_ref data |
| 288 |
then data, ax2ty |
| 289 |
else |
| 290 |
|
| 291 |
let curr = GlobRef.Set_env.add firstind_ref GlobRef.Set_env.empty in |
| 292 |
let accu = (curr, data, ax2ty) in |
| 293 |
let mib = lookup_mind mind in |
| 294 |
|
| 295 |
let param_ctx = mib.mind_params_ctxt in |
| 296 |
let nparam = List.length param_ctx in |
| 297 |
let accu = traverse_context access obj Context.Rel.empty accu param_ctx in |
| 298 |
|
| 299 |
|
| 300 |
let (contents, data, ax2ty) = Array.fold_left (fun accu oib -> |
| 301 |
let arity_wo_param = |
| 302 |
List.rev (List.skipn nparam (List.rev oib.mind_arity_ctxt)) |
| 303 |
in |
| 304 |
let accu = |
| 305 |
traverse_context |
| 306 |
access obj param_ctx accu arity_wo_param |
| 307 |
in |
| 308 |
Array.fold_left (fun accu cst_typ -> |
| 309 |
let param_ctx, cst_typ_wo_param = Term.decompose_prod_n_decls nparam cst_typ in |
| 310 |
traverse access obj param_ctx accu cst_typ_wo_param) |
| 311 |
accu oib.mind_user_lc) |
| 312 |
accu mib.mind_packets |
| 313 |
in |
| 314 |
|
| 315 |
let data = |
| 316 |
let contents = GlobRef.Set_env.remove firstind_ref contents in |
| 317 |
Array.fold_left_i (fun n data oib -> |
| 318 |
let ind = (mind, n) in |
| 319 |
let data = GlobRef.Map_env.add (GlobRef.IndRef ind) (Some contents) data in |
| 320 |
Array.fold_left_i (fun k data _ -> |
| 321 |
GlobRef.Map_env.add (GlobRef.ConstructRef (ind, k+1)) (Some contents) data |
| 322 |
) data oib.mind_consnames) data mib.mind_packets |
| 323 |
in |
| 324 |
(data, ax2ty) |
| 325 |
in |
| 326 |
(GlobRef.Set_env.add obj curr, data, ax2ty) |
| 327 |
|
| 328 |
|
| 329 |
and traverse_context access current ctx accu ctxt = |
| 330 |
snd (Context.Rel.fold_outside (fun decl (ctx, accu) -> |
| 331 |
match decl with |
| 332 |
| Context.Rel.Declaration.LocalDef (_,c,t) -> |
| 333 |
let accu = traverse access current ctx (traverse access current ctx accu t) c in |
| 334 |
let ctx = Context.Rel.add decl ctx in |
| 335 |
ctx, accu |
| 336 |
| Context.Rel.Declaration.LocalAssum (_,t) -> |
| 337 |
let accu = traverse access current ctx accu t in |
| 338 |
let ctx = Context.Rel.add decl ctx in |
| 339 |
ctx, accu) ctxt ~init:(ctx, accu)) |
| 340 |
|
| 341 |
let traverse access grs = |
| 342 |
let () = modcache := ModPath.Map.empty in |
| 343 |
let env = Global.env () in |
| 344 |
List.fold_left (fun accu gr -> |
| 345 |
let t, _ = UnivGen.fresh_global_instance env gr in |
| 346 |
traverse access gr Context.Rel.empty accu t |
| 347 |
) (GlobRef.Set_env.empty, GlobRef.Map_env.empty, GlobRef.Map_env.empty) grs |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
let type_of_constant cb = cb.Declarations.const_type |
| 353 |
|
| 354 |
let uses_uip mib = |
| 355 |
Array.exists (fun mip -> |
| 356 |
Option.is_empty mip.mind_squashed |
| 357 |
&& mip.mind_relevance == Sorts.Irrelevant |
| 358 |
&& Array.length mip.mind_nf_lc = 1 |
| 359 |
&& List.length (fst mip.mind_nf_lc.(0)) = List.length mib.mind_params_ctxt) |
| 360 |
mib.mind_packets |
| 361 |
|
| 362 |
let assumptions ?(add_opaque=false) ?(add_transparent=false) access st grs = |
| 363 |
let open Printer in |
| 364 |
|
| 365 |
let (_, graph, ax2ty) = traverse access grs in |
| 366 |
let open GlobRef in |
| 367 |
let fold obj contents accu = match obj with |
| 368 |
| VarRef id -> |
| 369 |
let decl = Global.lookup_named id in |
| 370 |
if Context.Named.Declaration.is_local_assum decl then |
| 371 |
let t = Context.Named.Declaration.get_type decl in |
| 372 |
ContextObjectMap.add (Variable id) t accu |
| 373 |
else accu |
| 374 |
| ConstRef kn -> |
| 375 |
let cb = lookup_constant kn in |
| 376 |
let accu = |
| 377 |
if cb.const_typing_flags.check_guarded then accu |
| 378 |
else |
| 379 |
let l = try GlobRef.Map_env.find obj ax2ty with Not_found -> [] in |
| 380 |
ContextObjectMap.add (Axiom (Guarded obj, l)) Constr.mkProp accu |
| 381 |
in |
| 382 |
let accu = |
| 383 |
if cb.const_typing_flags.check_universes then accu |
| 384 |
else |
| 385 |
let l = try GlobRef.Map_env.find obj ax2ty with Not_found -> [] in |
| 386 |
ContextObjectMap.add (Axiom (TypeInType obj, l)) Constr.mkProp accu |
| 387 |
in |
| 388 |
if not (Option.has_some contents) then |
| 389 |
let t = type_of_constant cb in |
| 390 |
let l = try GlobRef.Map_env.find obj ax2ty with Not_found -> [] in |
| 391 |
ContextObjectMap.add (Axiom (Constant kn,l)) t accu |
| 392 |
else if add_opaque && (Declareops.is_opaque cb || not (Structures.PrimitiveProjections.is_transparent_constant st kn)) then |
| 393 |
let t = type_of_constant cb in |
| 394 |
ContextObjectMap.add (Opaque kn) t accu |
| 395 |
else if add_transparent then |
| 396 |
let t = type_of_constant cb in |
| 397 |
ContextObjectMap.add (Transparent kn) t accu |
| 398 |
else |
| 399 |
accu |
| 400 |
| IndRef (m,_) | ConstructRef ((m,_),_) -> |
| 401 |
let mind = lookup_mind m in |
| 402 |
let accu = |
| 403 |
if mind.mind_typing_flags.check_positive then accu |
| 404 |
else |
| 405 |
let l = try GlobRef.Map_env.find obj ax2ty with Not_found -> [] in |
| 406 |
ContextObjectMap.add (Axiom (Positive m, l)) Constr.mkProp accu |
| 407 |
in |
| 408 |
let accu = |
| 409 |
if mind.mind_typing_flags.check_guarded then accu |
| 410 |
else |
| 411 |
let l = try GlobRef.Map_env.find obj ax2ty with Not_found -> [] in |
| 412 |
ContextObjectMap.add (Axiom (Guarded obj, l)) Constr.mkProp accu |
| 413 |
in |
| 414 |
let accu = |
| 415 |
if mind.mind_typing_flags.check_universes then accu |
| 416 |
else |
| 417 |
let l = try GlobRef.Map_env.find obj ax2ty with Not_found -> [] in |
| 418 |
ContextObjectMap.add (Axiom (TypeInType obj, l)) Constr.mkProp accu |
| 419 |
in |
| 420 |
let accu = |
| 421 |
if not (uses_uip mind) then accu |
| 422 |
else |
| 423 |
let l = try GlobRef.Map_env.find obj ax2ty with Not_found -> [] in |
| 424 |
ContextObjectMap.add (Axiom (UIP m, l)) Constr.mkProp accu |
| 425 |
in |
| 426 |
accu |
| 427 |
in GlobRef.Map_env.fold fold graph ContextObjectMap.empty |