| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
l |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
open Util |
| 19 |
open Names |
| 20 |
open Term |
| 21 |
open Constr |
| 22 |
open UVars |
| 23 |
open Context |
| 24 |
|
| 25 |
module NamedDecl = Context.Named.Declaration |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
c) |
| 31 |
|
| 32 |
,z]] |
| 33 |
|
| 34 |
↦z] |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
type abstr_info = { |
| 39 |
abstr_ctx : Constr.named_context; |
| 40 |
|
| 41 |
abstr_auctx : UVars.AbstractContext.t; |
| 42 |
|
| 43 |
abstr_ausubst : Instance.t; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
C) |
| 51 |
|
| 52 |
n)] |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
type abstr_inst_info = { |
| 58 |
abstr_rev_inst : Id.t list; |
| 59 |
|
| 60 |
abstr_uinst : UVars.Instance.t; |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
n] |
| 69 |
|
| 70 |
|
| 71 |
type 'a entry_map = 'a Cmap_env.t * 'a Mindmap_env.t |
| 72 |
type expand_info = abstr_inst_info entry_map |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
C) |
| 78 |
|
| 79 |
x2])] |
| 80 |
], |
| 81 |
|
| 82 |
|
| 83 |
type cooking_info = { |
| 84 |
expand_info : expand_info; |
| 85 |
abstr_info : abstr_info; |
| 86 |
} |
| 87 |
|
| 88 |
let empty_cooking_info = { |
| 89 |
expand_info = (Cmap_env.empty, Mindmap_env.empty); |
| 90 |
abstr_info = { |
| 91 |
abstr_ctx = []; |
| 92 |
abstr_auctx = AbstractContext.empty; |
| 93 |
abstr_ausubst = Instance.empty; |
| 94 |
}; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
type my_global_reference = |
| 100 |
| ConstRef of Constant.t |
| 101 |
| IndRef of inductive |
| 102 |
| ConstructRef of constructor |
| 103 |
|
| 104 |
module RefHash = |
| 105 |
struct |
| 106 |
type t = my_global_reference |
| 107 |
let equal gr1 gr2 = match gr1, gr2 with |
| 108 |
| ConstRef c1, ConstRef c2 -> Constant.UserOrd.equal c1 c2 |
| 109 |
| IndRef i1, IndRef i2 -> Ind.UserOrd.equal i1 i2 |
| 110 |
| ConstructRef c1, ConstructRef c2 -> Construct.UserOrd.equal c1 c2 |
| 111 |
| _ -> false |
| 112 |
open Hashset.Combine |
| 113 |
let hash = function |
| 114 |
| ConstRef c -> combinesmall 1 (Constant.UserOrd.hash c) |
| 115 |
| IndRef i -> combinesmall 2 (Ind.UserOrd.hash i) |
| 116 |
| ConstructRef c -> combinesmall 3 (Construct.UserOrd.hash c) |
| 117 |
end |
| 118 |
|
| 119 |
module RefTable = Hashtbl.Make(RefHash) |
| 120 |
type internal_abstr_inst_info = UVars.Instance.t * int list * int |
| 121 |
|
| 122 |
type cooking_cache = { |
| 123 |
cache : internal_abstr_inst_info RefTable.t; |
| 124 |
info : cooking_info; |
| 125 |
rel_ctx : rel_context Lazy.t; |
| 126 |
} |
| 127 |
|
| 128 |
let instantiate_my_gr gr u = |
| 129 |
match gr with |
| 130 |
| ConstRef c -> mkConstU (c, u) |
| 131 |
| IndRef i -> mkIndU (i, u) |
| 132 |
| ConstructRef c -> mkConstructU (c, u) |
| 133 |
|
| 134 |
let discharge_inst top_abst_subst sub_abst_rev_inst = |
| 135 |
let rec aux k relargs top_abst_subst sub_abst_rev_inst = |
| 136 |
match top_abst_subst, sub_abst_rev_inst with |
| 137 |
| decl :: top_abst_subst, id' :: sub_abst_rev_inst' -> |
| 138 |
if Id.equal (NamedDecl.get_id decl) id' then |
| 139 |
aux (k+1) (k :: relargs) top_abst_subst sub_abst_rev_inst' |
| 140 |
else |
| 141 |
aux (k+1) relargs top_abst_subst sub_abst_rev_inst |
| 142 |
| _, [] -> relargs |
| 143 |
| [], _ -> assert false in |
| 144 |
aux 1 [] top_abst_subst sub_abst_rev_inst |
| 145 |
|
| 146 |
let rec find_var k id = function |
| 147 |
| [] -> raise Not_found |
| 148 |
| decl :: subst -> |
| 149 |
if Id.equal id (NamedDecl.get_id decl) then k+1 |
| 150 |
else find_var (k+1) id subst |
| 151 |
|
| 152 |
let share cache top_abst_subst r (cstl,knl) = |
| 153 |
try RefTable.find cache r |
| 154 |
with Not_found -> |
| 155 |
let {abstr_uinst;abstr_rev_inst} = |
| 156 |
match r with |
| 157 |
| IndRef (kn,_i) -> Mindmap_env.find kn knl |
| 158 |
| ConstructRef ((kn,_i),_j) -> Mindmap_env.find kn knl |
| 159 |
| ConstRef cst -> Cmap_env.find cst cstl |
| 160 |
in |
| 161 |
let inst = (abstr_uinst, discharge_inst top_abst_subst abstr_rev_inst, List.length abstr_rev_inst) in |
| 162 |
RefTable.add cache r inst; |
| 163 |
inst |
| 164 |
|
| 165 |
let make_inst k abstr_inst_rel = |
| 166 |
Array.map_of_list (fun n -> mkRel (k+n)) abstr_inst_rel |
| 167 |
|
| 168 |
let share_univs cache top_abst_subst k r u l = |
| 169 |
let (abstr_uinst,abstr_inst_rel,_) = share cache top_abst_subst r l in |
| 170 |
mkApp (instantiate_my_gr r (Instance.append abstr_uinst u), make_inst k abstr_inst_rel) |
| 171 |
|
| 172 |
let discharge_proj_repr r p = |
| 173 |
let nnewpars = List.count NamedDecl.is_local_assum r.abstr_info.abstr_ctx in |
| 174 |
let map npars = npars + nnewpars in |
| 175 |
Projection.Repr.map_npars map p |
| 176 |
|
| 177 |
let discharge_proj (_,_,abstr_inst_length) p = |
| 178 |
let map npars = npars + abstr_inst_length in |
| 179 |
Projection.map_npars map p |
| 180 |
|
| 181 |
let is_empty_modlist (cm, mm) = |
| 182 |
Cmap_env.is_empty cm && Mindmap_env.is_empty mm |
| 183 |
|
| 184 |
let expand_constr cache modlist top_abst_subst c = |
| 185 |
let share_univs = share_univs cache top_abst_subst in |
| 186 |
let rec substrec k c = |
| 187 |
match kind c with |
| 188 |
| Case (ci, u, pms, p, iv, t, br) -> |
| 189 |
begin match share cache top_abst_subst (IndRef ci.ci_ind) modlist with |
| 190 |
| (abstr_uinst, abstr_inst_rel, abstr_inst_length) -> |
| 191 |
let u = Instance.append abstr_uinst u in |
| 192 |
let pms = Array.append (make_inst k abstr_inst_rel) pms in |
| 193 |
let ci = { ci with ci_npar = ci.ci_npar + abstr_inst_length } in |
| 194 |
Constr.map_with_binders succ substrec k (mkCase (ci,u,pms,p,iv,t,br)) |
| 195 |
| exception Not_found -> |
| 196 |
Constr.map_with_binders succ substrec k c |
| 197 |
end |
| 198 |
|
| 199 |
| Ind (ind,u) -> |
| 200 |
(try |
| 201 |
share_univs k (IndRef ind) u modlist |
| 202 |
with |
| 203 |
| Not_found -> Constr.map_with_binders succ substrec k c) |
| 204 |
|
| 205 |
| Construct (cstr,u) -> |
| 206 |
(try |
| 207 |
share_univs k (ConstructRef cstr) u modlist |
| 208 |
with |
| 209 |
| Not_found -> Constr.map_with_binders succ substrec k c) |
| 210 |
|
| 211 |
| Const (cst,u) -> |
| 212 |
(try |
| 213 |
share_univs k (ConstRef cst) u modlist |
| 214 |
with |
| 215 |
| Not_found -> Constr.map_with_binders succ substrec k c) |
| 216 |
|
| 217 |
| Proj (p, r, c') -> |
| 218 |
let ind = Names.Projection.inductive p in |
| 219 |
let p' = |
| 220 |
try |
| 221 |
let exp = share cache top_abst_subst (IndRef ind) modlist in |
| 222 |
discharge_proj exp p |
| 223 |
with Not_found -> p in |
| 224 |
let c'' = substrec k c' in |
| 225 |
if p == p' && c' == c'' then c else mkProj (p', r, c'') |
| 226 |
|
| 227 |
| Var id -> |
| 228 |
(try |
| 229 |
mkRel (find_var k id top_abst_subst) |
| 230 |
with Not_found -> c) |
| 231 |
|
| 232 |
| _ -> Constr.map_with_binders succ substrec k c |
| 233 |
|
| 234 |
in |
| 235 |
if is_empty_modlist modlist && List.is_empty top_abst_subst then c |
| 236 |
else substrec 0 c |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
let expand_subst0 cache expand_info abstr_ctx abstr_ausubst c = |
| 248 |
let c = expand_constr cache expand_info abstr_ctx c in |
| 249 |
let c = Vars.subst_univs_level_constr (make_instance_subst abstr_ausubst) c in |
| 250 |
c |
| 251 |
|
| 252 |
let expand_subst cache c = |
| 253 |
expand_subst0 cache.cache cache.info.expand_info cache.info.abstr_info.abstr_ctx cache.info.abstr_info.abstr_ausubst c |
| 254 |
|
| 255 |
|
| 256 |
let abstract_as_type cache t = |
| 257 |
let ctx = Lazy.force cache.rel_ctx in |
| 258 |
it_mkProd_wo_LetIn (expand_subst cache t) ctx |
| 259 |
|
| 260 |
|
| 261 |
let abstract_as_body cache c = |
| 262 |
let ctx = Lazy.force cache.rel_ctx in |
| 263 |
it_mkLambda_or_LetIn (expand_subst cache c) ctx |
| 264 |
|
| 265 |
|
| 266 |
let abstract_as_sort cache s = |
| 267 |
destSort (expand_subst cache (mkSort s)) |
| 268 |
|
| 269 |
|
| 270 |
τ])], that is, |
| 271 |
d [τ] |
| 272 |
Ω.J)[σ][τ])] |
| 273 |
'][τ])] *) |
| 274 |
let abstract_named_context expand_info abstr_ausubst hyps = |
| 275 |
let fold decl abstr_ctx = |
| 276 |
let cache = RefTable.create 13 in |
| 277 |
let decl = match decl with |
| 278 |
| NamedDecl.LocalDef (id, b, t) -> |
| 279 |
let id = Context.map_annot_relevance (UVars.subst_sort_level_relevance (make_instance_subst abstr_ausubst)) id in |
| 280 |
let b = expand_subst0 cache expand_info abstr_ctx abstr_ausubst b in |
| 281 |
let t = expand_subst0 cache expand_info abstr_ctx abstr_ausubst t in |
| 282 |
NamedDecl.LocalDef (id, b, t) |
| 283 |
| NamedDecl.LocalAssum (id, t) -> |
| 284 |
let id = Context.map_annot_relevance (UVars.subst_sort_level_relevance (make_instance_subst abstr_ausubst)) id in |
| 285 |
let t = expand_subst0 cache expand_info abstr_ctx abstr_ausubst t in |
| 286 |
NamedDecl.LocalAssum (id, t) |
| 287 |
in |
| 288 |
decl :: abstr_ctx |
| 289 |
in |
| 290 |
Context.Named.fold_outside fold hyps ~init:[] |
| 291 |
|
| 292 |
let create_cache info = |
| 293 |
let cache = RefTable.create 13 in |
| 294 |
let abstr_info = info.abstr_info in |
| 295 |
let named_ctx = lazy (abstract_named_context info.expand_info abstr_info.abstr_ausubst abstr_info.abstr_ctx) in |
| 296 |
let rel_ctx = lazy (List.map NamedDecl.to_rel_decl (Lazy.force named_ctx)) in |
| 297 |
{ cache; info; rel_ctx } |
| 298 |
|
| 299 |
t |
| 300 |
|
| 301 |
e |
| 302 |
then |
| 303 |
tutions |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
let make_cooking_info ~recursive expand_info hyps uctx = |
| 309 |
let abstr_rev_inst = List.rev (Named.instance_list (fun id -> id) hyps) in |
| 310 |
let abstr_ausubst, abstr_auctx = abstract_universes uctx in |
| 311 |
let abstr_info = { abstr_ctx = hyps; abstr_auctx; abstr_ausubst } in |
| 312 |
let abstr_inst_info = { |
| 313 |
abstr_rev_inst = abstr_rev_inst; |
| 314 |
abstr_uinst = abstr_ausubst; |
| 315 |
} in |
| 316 |
let info = { expand_info; abstr_info } in |
| 317 |
let info = match recursive with |
| 318 |
| None -> info |
| 319 |
| Some ind -> |
| 320 |
let (cmap, imap) = info.expand_info in |
| 321 |
{ info with expand_info = (cmap, Mindmap_env.add ind abstr_inst_info imap) } |
| 322 |
in |
| 323 |
info, abstr_inst_info |
| 324 |
|
| 325 |
let names_info info = |
| 326 |
let fold accu id = Id.Set.add (NamedDecl.get_id id) accu in |
| 327 |
List.fold_left fold Id.Set.empty info.abstr_info.abstr_ctx |
| 328 |
|
| 329 |
let rel_context_of_cooking_cache cache = |
| 330 |
Lazy.force cache.rel_ctx |
| 331 |
|
| 332 |
let universe_context_of_cooking_info info = |
| 333 |
info.abstr_info.abstr_auctx |
| 334 |
|
| 335 |
let instance_of_cooking_info info = |
| 336 |
Named.instance mkVar info.abstr_info.abstr_ctx |
| 337 |
|
| 338 |
let instance_of_cooking_cache { info; _ } = |
| 339 |
instance_of_cooking_info info |
| 340 |
|
| 341 |
let discharge_abstract_universe_context abstr auctx = |
| 342 |
abstract |
| 343 |
|
| 344 |
|
| 345 |
m - 1}], |
| 346 |
|
| 347 |
|
| 348 |
∪ |
| 349 |
|
| 350 |
|
| 351 |
ar (n + m - 1)]. |
| 352 |
|
| 353 |
let n = AbstractContext.size abstr.abstr_auctx in |
| 354 |
if (UVars.eq_sizes n (0,0)) then |
| 355 |
|
| 356 |
abstr, n, AbstractContext.union abstr.abstr_auctx auctx |
| 357 |
else |
| 358 |
let subst = abstr.abstr_ausubst in |
| 359 |
let suff = UVars.make_abstract_instance auctx in |
| 360 |
let ainst = Instance.append subst suff in |
| 361 |
let substf = make_instance_subst ainst in |
| 362 |
let auctx = UVars.subst_univs_level_abstract_universe_context substf auctx in |
| 363 |
let auctx' = AbstractContext.union abstr.abstr_auctx auctx in |
| 364 |
{ abstr with abstr_ausubst = ainst }, n, auctx' |
| 365 |
|
| 366 |
let lift_mono_univs info ctx = |
| 367 |
assert (AbstractContext.is_empty info.abstr_info.abstr_auctx); |
| 368 |
info, ctx |
| 369 |
|
| 370 |
let lift_poly_univs info auctx = |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
r] = |
| 376 |
nto |
| 377 |
the new |
| 378 |
e [τ'] |
| 379 |
|
| 380 |
into |
| 381 |
(J[σ][ττ'])] *) |
| 382 |
let abstr_info, n, auctx = discharge_abstract_universe_context info.abstr_info auctx in |
| 383 |
{ info with abstr_info }, n, auctx |
| 384 |
|
| 385 |
let lift_private_mono_univs info a = |
| 386 |
let () = assert (AbstractContext.is_empty info.abstr_info.abstr_auctx) in |
| 387 |
let () = assert (Instance.is_empty info.abstr_info.abstr_ausubst) in |
| 388 |
a |
| 389 |
|
| 390 |
let lift_private_poly_univs info (inst, cstrs) = |
| 391 |
let cstrs = UVars.subst_univs_constraints (snd @@ make_instance_subst info.abstr_info.abstr_ausubst) cstrs in |
| 392 |
(inst, cstrs) |
| 393 |
|
| 394 |
let lift_quality info q = |
| 395 |
UVars.subst_sort_level_quality (make_instance_subst info.abstr_info.abstr_ausubst) q |
| 396 |
|
| 397 |
let lift_relevance info relevance = |
| 398 |
UVars.subst_sort_level_relevance (make_instance_subst info.abstr_info.abstr_ausubst) relevance |