Debian Codegraph @

kernel/mod_subst.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Created by Claudio Sacerdoti from contents of term.ml, names.ml and
12 new support for constant inlining in functor application, Nov 2004 *)
13 (* Optimizations and bug fixes by Élie Soubiran, from Feb 2008 *)
14
15 (* This file provides types and functions for managing name
16 substitution in module constructions *)
17
18 open Pp
19 open Util
20 open Names
21 open Constr
22
23 (* For Inline, the int is an inlining level, and the constr (if present)
24 is the term into which we should inline.
25 Equiv gives the canonical name in the given context. *)
26
27 type delta_hint =
28 | Inline of int * constr UVars.univ_abstracted option
29 | Equiv of KerName.t
30
31 (* NB: earlier constructor Prefix_equiv of ModPath.t
32 is now stored in a separate table, see Deltamap.t below *)
33
34 module Deltamap = struct
35 type t = {
36 root : ModPath.t;
37 (** Common root of all keys in the deltamap *)
38 mmap : ModPath.t ModPath.Map.t;
39 (** All bindings [mp ↦ _] must satisfy [mp ⊆ root] *)
40 kmap : delta_hint KerName.Map.t;
41 (** All bindings [kn ↦ _] must satisfy [modpath(kn) ⊆ root] *)
42 }
43
44 let empty root = {
45 root;
46 mmap = ModPath.Map.empty;
47 kmap = KerName.Map.empty;
48 }
49
50 let root reso = reso.root
51
52 let add_kn kn hint reso =
53 let () = assert (ModPath.subpath reso.root (KerName.modpath kn)) in
54 { reso with kmap = KerName.Map.add kn hint reso.kmap }
55
56 let add_mp mp mp' reso =
57 let () = assert (ModPath.subpath reso.root mp) in
58 { reso with mmap = ModPath.Map.add mp mp' reso.mmap }
59
60 let find_mp mp reso = ModPath.Map.find mp reso.mmap
61 let find_kn kn reso = KerName.Map.find kn reso.kmap
62 let mem_mp mp reso = ModPath.Map.mem mp reso.mmap
63 let fold_kn f reso i = KerName.Map.fold f reso.kmap i
64 let fold fmp fkn reso accu =
65 ModPath.Map.fold fmp reso.mmap (KerName.Map.fold fkn reso.kmap accu)
66 let join map1 map2 = fold add_mp add_kn map1 map2
67
68 (** if mp0 ⊆ root, we can see a resolver on root as a resolver on mp *)
69 let upcast mp0 reso =
70 let () = assert (ModPath.subpath mp0 reso.root) in
71 { reso with root = mp0 }
72
73 (** keep only data that is relevant for names with a modpath ⊇ root *)
74 let reroot root reso =
75 let () = assert (ModPath.subpath reso.root root) in
76 let km = reso.kmap in
77 let mm = reso.mmap in
78 (* filter the modpaths *)
79 let fold_mp mp data (glb, accu) =
80 if ModPath.subpath root mp then
81 (* root ⊆ mp, keep it *)
82 glb, ModPath.Map.add mp data accu
83 else if ModPath.subpath mp root then
84 (* This is a subpath of the root. It may be relevant due to find_prefix,
85 but only when 1. root is not in mm, and 2. this is the most precise
86 path in mm above root, as find_prefix will always return this one
87 without considering the less precise ones. *)
88 let glb = match glb with
89 | None -> Some mp
90 | Some glb -> if ModPath.subpath glb mp then Some mp else Some glb
91 in
92 glb, accu
93 else
94 (* path that is incomparable, skip it *)
95 glb, accu
96 in
97 let glb, mm' = ModPath.Map.fold fold_mp mm (None, ModPath.Map.empty) in
98 let mm' = match glb with
99 | None -> mm'
100 | Some glb ->
101 if ModPath.Map.mem root mm then mm'
102 else
103 (* Add root to the resolver and map it to what find_prefix would have
104 returned on root *)
105 let rec diff accu mp =
106 if ModPath.equal mp glb then accu
107 else match mp with
108 | MPdot (mp, l) -> diff (l :: accu) mp
109 | MPbound _ | MPfile _ -> assert false
110 in
111 let diff = diff [] root in
112 let data = ModPath.Map.get glb mm in
113 let data' = List.fold_left (fun accu l -> MPdot (accu, l)) data diff in
114 ModPath.Map.add root data' mm'
115 in
116 (* filter the kernames *)
117 let filter_kn kn _ = ModPath.subpath root (KerName.modpath kn) in
118 let km' = KerName.Map.filter filter_kn km in
119 { kmap = km'; mmap = mm'; root = root }
120
121 end
122
123 (* Invariant: in the [delta_hint] map, an [Equiv] should only
124 relate [KerName.t] with the same label (and section dirpath). *)
125
126 type delta_resolver = Deltamap.t
127
128 let empty_delta_resolver = Deltamap.empty
129 let has_root_delta_resolver mp reso =
130 ModPath.equal mp (Deltamap.root reso)
131
132 let upcast_delta_resolver = Deltamap.upcast
133
134 module Umap :
135 sig
136 type 'a t
137 val empty : 'a t
138 val is_empty : 'a t -> bool
139 val add_mbi : MBId.t -> 'a -> 'a t -> 'a t
140 val add_mp : ModPath.t -> 'a -> 'a t -> 'a t
141 val find : ModPath.t -> 'a t -> 'a
142 val join : 'a t -> 'a t -> 'a t
143 val fold : (ModPath.t -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
144 end = struct
145 type 'a t = 'a ModPath.Map.t
146 let empty = ModPath.Map.empty
147 let is_empty m = ModPath.Map.is_empty m
148 let add_mbi mbi x m = ModPath.Map.add (MPbound mbi) x m
149 let add_mp mp x m = ModPath.Map.add mp x m
150 let find = ModPath.Map.find
151 let fold = ModPath.Map.fold
152 let join map1 map2 = fold add_mp map1 map2
153 end
154
155 type substitution = delta_resolver Umap.t
156
157 let empty_subst = Umap.empty
158
159 let is_empty_subst = Umap.is_empty
160
161 (* <debug> *)
162
163 let string_of_hint = function
164 | Inline (_,Some _) -> "inline(Some _)"
165 | Inline _ -> "inline()"
166 | Equiv kn -> KerName.to_string kn
167
168 let debug_string_of_delta resolve =
169 let kn_to_string kn hint l =
170 (KerName.to_string kn ^ "=>" ^ string_of_hint hint) :: l
171 in
172 let mp_to_string mp mp' l =
173 (ModPath.to_string mp ^ "=>" ^ ModPath.to_string mp') :: l
174 in
175 let l = Deltamap.fold mp_to_string kn_to_string resolve [] in
176 String.concat ", " (List.rev l)
177
178 let list_contents subst =
179 let one_pair reso = (ModPath.to_string (Deltamap.root reso), debug_string_of_delta reso) in
180 let mp_one_pair mp0 p l = (ModPath.to_string mp0, one_pair p)::l in
181 Umap.fold mp_one_pair subst []
182
183 let debug_string_of_subst subst =
184 let l = List.map (fun (s1,(s2,s3)) -> s1^"|->"^s2^"["^s3^"]")
185 (list_contents subst)
186 in
187 "{" ^ String.concat "; " l ^ "}"
188
189 let debug_pr_delta resolve =
190 strbrk (debug_string_of_delta resolve)
191
192 let debug_pr_subst subst =
193 let l = list_contents subst in
194 let f (s1,(s2,s3)) = hov 2 (str s1 ++ spc () ++ str "|-> " ++ str s2 ++
195 spc () ++ str "[" ++ str s3 ++ str "]")
196 in
197 str "{" ++ hov 2 (prlist_with_sep pr_comma f l) ++ str "}"
198
199 (* </debug> *)
200
201 (** Extending a [delta_resolver] *)
202
203 let add_inline_delta_resolver kn (lev,oc) = Deltamap.add_kn kn (Inline (lev,oc))
204
205 let add_kn_delta_resolver kn kn' =
206 assert (Id.equal (KerName.label kn) (KerName.label kn'));
207 Deltamap.add_kn kn (Equiv kn')
208
209 let add_mp_delta_resolver mp1 mp2 = Deltamap.add_mp mp1 mp2
210
211 (** Extending a [substitution] without sequential composition *)
212
213 let add_mbid mbid mp resolve s =
214 let () = assert (ModPath.equal mp (Deltamap.root resolve)) in
215 Umap.add_mbi mbid resolve s
216 let add_mp mp1 mp2 resolve s =
217 let () = assert (ModPath.equal mp2 (Deltamap.root resolve)) in
218 Umap.add_mp mp1 resolve s
219
220 let map_mbid mbid mp resolve =
221 add_mbid mbid mp resolve empty_subst
222 let map_mp mp1 mp2 resolve = add_mp mp1 mp2 resolve empty_subst
223
224 let mp_in_delta mp = Deltamap.mem_mp mp
225
226 let mp_of_delta resolve mp =
227 try Deltamap.find_mp mp resolve with Not_found -> mp
228
229 let find_prefix resolve mp =
230 let rec sub_mp = function
231 | MPdot(mp,l) as mp_sup ->
232 (try Deltamap.find_mp mp_sup resolve
233 with Not_found -> MPdot(sub_mp mp,l))
234 | p -> Deltamap.find_mp p resolve
235 in
236 try sub_mp mp with Not_found -> mp
237
238 (** Applying a resolver to a kernel name *)
239
240 let solve_delta_kn resolve kn =
241 match Deltamap.find_kn kn resolve with
242 | Equiv _ | Inline (_, Some _) as v -> v
243 | exception Not_found | Inline (_, None) ->
244 let mp,l = KerName.repr kn in
245 let new_mp = find_prefix resolve mp in
246 if mp == new_mp then
247 Equiv kn
248 else
249 Equiv (KerName.make new_mp l)
250
251 let kn_of_delta resolve kn =
252 match Deltamap.find_kn kn resolve with
253 | Equiv kn1 -> kn1
254 | exception Not_found | Inline _ ->
255 let mp,l = KerName.repr kn in
256 let new_mp = find_prefix resolve mp in
257 if mp == new_mp then
258 kn
259 else
260 (KerName.make new_mp l)
261
262 let constant_of_delta_kn resolve kn =
263 Constant.make kn (kn_of_delta resolve kn)
264
265 let mind_of_delta_kn resolve kn =
266 MutInd.make kn (kn_of_delta resolve kn)
267
268 let inline_of_delta inline resolver =
269 match inline with
270 | None -> []
271 | Some inl_lev ->
272 let extract kn hint l =
273 match hint with
274 | Inline (lev,_) -> if lev <= inl_lev then (lev,kn)::l else l
275 | _ -> l
276 in
277 Deltamap.fold_kn extract resolver []
278
279 let search_delta_inline resolve kn1 kn2 =
280 let find kn = match Deltamap.find_kn kn resolve with
281 | Inline (_,o) -> o
282 | Equiv _ -> raise Not_found
283 in
284 try find kn1
285 with Not_found ->
286 if kn1 == kn2 then None
287 else
288 try find kn2
289 with Not_found -> None
290
291 let subst_mp_opt subst mp = (* 's like subst *)
292 let repr r = Deltamap.root r, r in
293 let rec aux mp = match mp with
294 | MPfile _ | MPbound _ -> repr @@ Umap.find mp subst
295 | MPdot (mp1,l) as mp2 ->
296 begin
297 try repr @@ Umap.find mp2 subst
298 with Not_found ->
299 let mp1',resolve = aux mp1 in
300 MPdot (mp1',l),resolve
301 end
302 in
303 try Some (aux mp) with Not_found -> None
304
305 let subst_mp subst mp =
306 match subst_mp_opt subst mp with
307 None -> mp
308 | Some (mp',_) -> mp'
309
310 let subst_kn_delta subst kn =
311 let mp,l = KerName.repr kn in
312 match subst_mp_opt subst mp with
313 Some (mp',resolve) ->
314 solve_delta_kn resolve (KerName.make mp' l)
315 | None -> Equiv kn
316
317
318 let subst_kn subst kn =
319 let mp,l = KerName.repr kn in
320 match subst_mp_opt subst mp with
321 Some (mp',_) ->
322 (KerName.make mp' l)
323 | None -> kn
324
325 exception No_subst
326
327 let subst_dual_mp subst mp1 mp2 =
328 let o1 = subst_mp_opt subst mp1 in
329 let o2 = if mp1 == mp2 then o1 else subst_mp_opt subst mp2 in
330 match o1, o2 with
331 | None, None -> raise No_subst
332 | Some (mp1',resolve), None -> mp1', mp2, resolve, true
333 | None, Some (mp2',resolve) -> mp1, mp2', resolve, false
334 | Some (mp1',_), Some (mp2',resolve) -> mp1', mp2', resolve, false
335
336 let progress f x ~orelse =
337 let y = f x in
338 if y != x then y else orelse
339
340 let subst_mind subst mind =
341 let mpu,l = KerName.repr (MutInd.user mind) in
342 let mpc = KerName.modpath (MutInd.canonical mind) in
343 try
344 let mpu,mpc,resolve,user = subst_dual_mp subst mpu mpc in
345 let knu = KerName.make mpu l in
346 let knc = if mpu == mpc then knu else KerName.make mpc l in
347 let knc' =
348 progress (kn_of_delta resolve) (if user then knu else knc) ~orelse:knc
349 in
350 MutInd.make knu knc'
351 with No_subst -> mind
352
353 let subst_ind subst (ind,i as indi) =
354 let ind' = subst_mind subst ind in
355 if ind' == ind then indi else ind',i
356
357 let subst_constructor subst (ind,j as ref) =
358 let ind' = subst_ind subst ind in
359 if ind==ind' then ref
360 else (ind',j)
361
362 let subst_pind subst (ind,u) =
363 (subst_ind subst ind, u)
364
365 let subst_con0 subst cst =
366 let mpu,l = KerName.repr (Constant.user cst) in
367 let mpc = KerName.modpath (Constant.canonical cst) in
368 let mpu,mpc,resolve,user = subst_dual_mp subst mpu mpc in
369 let knu = KerName.make mpu l in
370 let knc = if mpu == mpc then knu else KerName.make mpc l in
371 let knc' =
372 progress (kn_of_delta resolve) (if user then knu else knc) ~orelse:knc in
373 let cst' = Constant.make knu knc' in
374 cst', search_delta_inline resolve knu knc
375
376 let subst_con subst cst =
377 try subst_con0 subst cst
378 with No_subst -> cst, None
379
380 let subst_pcon subst (con,u as pcon) =
381 try let con', _can = subst_con0 subst con in
382 con',u
383 with No_subst -> pcon
384
385 let subst_constant subst con =
386 try fst (subst_con0 subst con)
387 with No_subst -> con
388
389 let subst_proj_repr subst p =
390 Projection.Repr.map (subst_mind subst) p
391
392 let subst_proj subst p =
393 Projection.map (subst_mind subst) p
394
395 let subst_retro_action subst action =
396 let open Retroknowledge in
397 match action with
398 | Register_ind(prim,ind) ->
399 let ind' = subst_ind subst ind in
400 if ind == ind' then action else Register_ind(prim, ind')
401 | Register_type(prim,c) ->
402 let c' = subst_constant subst c in
403 if c == c' then action else Register_type(prim, c')
404
405 let rec map_kn f f' c =
406 let func = map_kn f f' in
407 match kind c with
408 | Const kn -> (try f' kn with No_subst -> c)
409 | Proj (p,r,t) ->
410 let p' = Projection.map f p in
411 let t' = func t in
412 if p' == p && t' == t then c
413 else mkProj (p', r, t')
414 | Ind ((kn,i),u) ->
415 let kn' = f kn in
416 if kn'==kn then c else mkIndU ((kn',i),u)
417 | Construct (((kn,i),j),u) ->
418 let kn' = f kn in
419 if kn'==kn then c else mkConstructU (((kn',i),j),u)
420 | Case (ci,u,pms,(p,r),iv,ct,l) ->
421 let ci_ind =
422 let (kn,i) = ci.ci_ind in
423 let kn' = f kn in
424 if kn'==kn then ci.ci_ind else kn',i
425 in
426 let f_ctx (nas, c as d) =
427 let c' = func c in
428 if c' == c then d else (nas, c')
429 in
430 let pms' = Array.Smart.map func pms in
431 let p' = f_ctx p in
432 let iv' = map_invert func iv in
433 let ct' = func ct in
434 let l' = Array.Smart.map f_ctx l in
435 if (ci.ci_ind==ci_ind && pms'==pms && p'==p && iv'==iv
436 && l'==l && ct'==ct)then c
437 else
438 mkCase ({ci with ci_ind = ci_ind}, u,
439 pms',(p',r),iv',ct', l')
440 | Cast (ct,k,t) ->
441 let ct' = func ct in
442 let t'= func t in
443 if (t'==t && ct'==ct) then c
444 else mkCast (ct', k, t')
445 | Prod (na,t,ct) ->
446 let ct' = func ct in
447 let t'= func t in
448 if (t'==t && ct'==ct) then c
449 else mkProd (na, t', ct')
450 | Lambda (na,t,ct) ->
451 let ct' = func ct in
452 let t'= func t in
453 if (t'==t && ct'==ct) then c
454 else mkLambda (na, t', ct')
455 | LetIn (na,b,t,ct) ->
456 let ct' = func ct in
457 let t'= func t in
458 let b'= func b in
459 if (t'==t && ct'==ct && b==b') then c
460 else mkLetIn (na, b', t', ct')
461 | App (ct,l) ->
462 let ct' = func ct in
463 let l' = Array.Smart.map func l in
464 if (ct'== ct && l'==l) then c
465 else mkApp (ct',l')
466 | Evar (e,l) ->
467 let l' = SList.Smart.map func l in
468 if (l'==l) then c
469 else mkEvar (e,l')
470 | Fix (ln,(lna,tl,bl)) ->
471 let tl' = Array.Smart.map func tl in
472 let bl' = Array.Smart.map func bl in
473 if (bl == bl'&& tl == tl') then c
474 else mkFix (ln,(lna,tl',bl'))
475 | CoFix(ln,(lna,tl,bl)) ->
476 let tl' = Array.Smart.map func tl in
477 let bl' = Array.Smart.map func bl in
478 if (bl == bl'&& tl == tl') then c
479 else mkCoFix (ln,(lna,tl',bl'))
480 | _ -> c
481
482 let subst_mps subst c =
483 let subst_pcon_term subst (con,u) =
484 let con', can = subst_con0 subst con in
485 match can with
486 | None -> mkConstU (con',u)
487 | Some t -> Vars.univ_instantiate_constr u t
488 in
489 if is_empty_subst subst then c
490 else map_kn (subst_mind subst) (subst_pcon_term subst) c
491
492 let subst_mps_aux subst = function
493 | Inl (con, u) ->
494 begin match subst_con0 subst con with
495 | con', None -> Inl (con', u)
496 | _, Some t -> Inr (Vars.univ_instantiate_constr u t)
497 | exception No_subst -> Inl (con, u)
498 end
499 | Inr t -> Inr (subst_mps subst t)
500
501 let subst_mps_list substs c =
502 if List.is_empty substs || List.for_all is_empty_subst substs then c
503 else
504 let cache_const = ref Cmap_env.empty in
505 let cache_ind = ref Mindmap_env.empty in
506 let subst_const (con, u as pcon) = match Cmap_env.find_opt con !cache_const with
507 | Some ans ->
508 if ans == con then raise No_subst else mkConstU (ans, u)
509 | None ->
510 let ans = List.fold_right subst_mps_aux substs (Inl pcon) in
511 (* Do not cache arbitrary inline terms *)
512 match ans with
513 | Inl (con', _ as ans) ->
514 let () = cache_const := Cmap_env.add con con' !cache_const in
515 if con' == con then raise No_subst else mkConstU ans
516 | Inr ans -> ans
517 in
518 let subst_mind ind = match Mindmap_env.find_opt ind !cache_ind with
519 | Some ans -> ans
520 | None ->
521 let ans = List.fold_right subst_mind substs ind in
522 let () = cache_ind := Mindmap_env.add ind ans !cache_ind in
523 ans
524 in
525 map_kn subst_mind subst_const c
526
527 let rec replace_mp_in_mp mpfrom mpto mp =
528 match mp with
529 | _ when ModPath.equal mp mpfrom -> mpto
530 | MPdot (mp1,l) ->
531 let mp1' = replace_mp_in_mp mpfrom mpto mp1 in
532 if mp1 == mp1' then mp
533 else MPdot (mp1',l)
534 | _ -> mp
535
536 let replace_mp_in_kn mpfrom mpto kn =
537 let mp,l = KerName.repr kn in
538 let mp'' = replace_mp_in_mp mpfrom mpto mp in
539 if mp==mp'' then kn
540 else KerName.make mp'' l
541
542 let mp_in_mp = ModPath.subpath
543
544 let subset_prefixed_by mp resolver =
545 let mp_prefix mkey mequ rslv =
546 if mp_in_mp mp mkey then Deltamap.add_mp mkey mequ rslv else rslv
547 in
548 let kn_prefix kn hint rslv =
549 match hint with
550 | Inline (_,None) -> rslv
551 | Equiv _ | Inline (_,Some _) ->
552 if mp_in_mp mp (KerName.modpath kn) then Deltamap.add_kn kn hint rslv else rslv
553 in
554 Deltamap.fold mp_prefix kn_prefix resolver (empty_delta_resolver mp)
555
556 let subst_dom_delta_resolver subst resolver =
557 let mp_apply_subst mkey mequ rslv =
558 Deltamap.add_mp (subst_mp subst mkey) mequ rslv
559 in
560 let kn_apply_subst kkey hint rslv =
561 Deltamap.add_kn (subst_kn subst kkey) hint rslv
562 in
563 let root = subst_mp subst (Deltamap.root resolver) in
564 Deltamap.fold mp_apply_subst kn_apply_subst resolver (empty_delta_resolver root)
565
566 let subst_mp_delta subst mp mkey =
567 match subst_mp_opt subst mp with
568 None -> empty_delta_resolver mp, mp
569 | Some (mp',resolve) ->
570 (* root(resolve) ⊆ mp' *)
571 let mp1 = find_prefix resolve mp' in
572 let resolve1 = subset_prefixed_by mp1 resolve in
573 (subst_dom_delta_resolver
574 (map_mp mp1 mkey (empty_delta_resolver mkey)) resolve1), mp1
575
576 let gen_subst_delta_resolver dom subst resolver =
577 let mp_apply_subst mkey mequ rslv =
578 let mkey' = if dom then subst_mp subst mkey else mkey in
579 let rslv',mequ' = subst_mp_delta subst mequ mkey' in
580 Deltamap.join rslv' (Deltamap.add_mp mkey' mequ' rslv)
581 in
582 let kn_apply_subst kkey hint rslv =
583 let kkey' = if dom then subst_kn subst kkey else kkey in
584 let hint' = match hint with
585 | Equiv kequ -> subst_kn_delta subst kequ
586 | Inline (lev,Some t) -> Inline (lev,Some (UVars.map_univ_abstracted (subst_mps subst) t))
587 | Inline (_,None) -> hint
588 in
589 Deltamap.add_kn kkey' hint' rslv
590 in
591 let root = if dom then subst_mp subst (Deltamap.root resolver) else Deltamap.root resolver in
592 Deltamap.fold mp_apply_subst kn_apply_subst resolver (empty_delta_resolver root)
593
594 let subst_codom_delta_resolver = gen_subst_delta_resolver false
595 let subst_dom_codom_delta_resolver = gen_subst_delta_resolver true
596
597 let update_delta_resolver resolver1 resolver2 =
598 let mp_apply_rslv mkey mequ rslv =
599 Deltamap.add_mp mkey (find_prefix resolver2 mequ) rslv
600 in
601 let kn_apply_rslv kkey hint1 rslv =
602 let hint = match hint1 with
603 | Equiv kequ -> solve_delta_kn resolver2 kequ
604 | Inline (_,Some _) -> hint1
605 | Inline (_,None) ->
606 (try Deltamap.find_kn kkey resolver2 with Not_found -> hint1)
607 in
608 Deltamap.add_kn kkey hint rslv
609 in
610 Deltamap.fold mp_apply_rslv kn_apply_rslv resolver1 resolver2
611
612 let add_delta_resolver resolver1 resolver2 =
613 let () =
614 if mp_in_mp (Deltamap.root resolver2) (Deltamap.root resolver1) then ()
615 else CErrors.anomaly Pp.(strbrk "Incompatible resolver roots: " ++
616 ModPath.print (Deltamap.root resolver2) ++ strbrk " is not a subpath of " ++
617 ModPath.print (Deltamap.root resolver1))
618 in
619 update_delta_resolver resolver1 resolver2
620
621 let substitution_prefixed_by k mp subst =
622 let mp_prefixmp kmp reso subst =
623 if mp_in_mp mp kmp && not (ModPath.equal mp kmp) then
624 let new_key = replace_mp_in_mp mp k kmp in
625 Umap.add_mp new_key reso subst
626 else subst
627 in
628 Umap.fold mp_prefixmp subst empty_subst
629
630 let join subst1 subst2 =
631 let apply_subst mpk resolve res =
632 let mp = Deltamap.root resolve in
633 let mp', resolve' = match subst_mp_opt subst2 mp with
634 | None ->
635 let resolve' = subst_codom_delta_resolver subst2 resolve in
636 mp, resolve'
637 | Some (mp', resolve') ->
638 (* root(resolve') ⊆ mp' = subst2(mp) = root(subst_dom_codom_delta_resolver subst2 resolve) *)
639 let resolve' =
640 add_delta_resolver
641 (subst_dom_codom_delta_resolver subst2 resolve) resolve'
642 in
643 (* We need to reroot, as in general we only have root(resolve'') ⊆ mp' *)
644 let resolve' = Deltamap.reroot mp' resolve' in
645 mp', resolve'
646 in
647 let prefixed_subst = substitution_prefixed_by mpk mp' subst2 in
648 Umap.join prefixed_subst (Umap.add_mp mpk resolve' res)
649 in
650 let subst = Umap.fold apply_subst subst1 empty_subst in
651 Umap.join subst2 subst