1 (* Copyright (C) 2003, HELM Team.
4 * This file is part of HELM, an Hypertextual, Electronic
5 * Library of Mathematics, developed at the Computer Science
6 * Department, University of Bologna, Italy.
8 * HELM is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * HELM is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with HELM; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * For details, see the HELM World-Wide-Web page,
24 * http://cs.unibo.it/helm/.
33 let deref_counter = ref 0
34 let apply_subst_context_counter = ref 0
35 let apply_subst_metasenv_counter = ref 0
36 let lift_counter = ref 0
37 let subst_counter = ref 0
38 let whd_counter = ref 0
39 let are_convertible_counter = ref 0
40 let metasenv_length = ref 0
41 let context_length = ref 0
42 let reset_counters () =
43 apply_subst_counter := 0;
44 apply_subst_context_counter := 0;
45 apply_subst_metasenv_counter := 0;
49 are_convertible_counter := 0;
52 let print_counters () =
53 debug_print (lazy (Printf.sprintf
55 apply_subst_context: %d
56 apply_subst_metasenv: %d
61 metasenv length: %d (avg = %.2f)
62 context length: %d (avg = %.2f)
64 !apply_subst_counter !apply_subst_context_counter
65 !apply_subst_metasenv_counter !lift_counter !subst_counter !whd_counter
66 !are_convertible_counter !metasenv_length
67 ((float !metasenv_length) /. (float !apply_subst_metasenv_counter))
69 ((float !context_length) /. (float !apply_subst_context_counter))
74 exception MetaSubstFailure of string Lazy.t
75 exception Uncertain of string Lazy.t
76 exception AssertFailure of string Lazy.t
77 exception DeliftingARelWouldCaptureAFreeVariable;;
79 let debug_print = fun _ -> ()
81 type substitution = (int * (Cic.context * Cic.term)) list
85 let third _,_,a = a in
90 (CicSubstitution.subst_meta
91 l (third (CicUtil.lookup_subst n subst)))
93 CicUtil.Subst_not_found _ -> t)
98 let lookup_subst = CicUtil.lookup_subst
101 (* clean_up_meta take a metasenv and a term and make every local context
102 of each occurrence of a metavariable consistent with its canonical context,
103 with respect to the hidden hipothesis *)
106 let clean_up_meta subst metasenv t =
107 let module C = Cic in
112 | C.Implicit _ -> assert false
113 | C.Meta (n,l) as t ->
116 let (cc,_) = lookup_subst n subst in cc
117 with CicUtil.Subst_not_found _ ->
119 let (_,cc,_) = CicUtil.lookup_meta n metasenv in cc
120 with CicUtil.Meta_not_found _ -> assert false) in
129 Invalid_argument _ -> assert false) in
131 | C.Cast (te,ty) -> C.Cast (aux te, aux ty)
132 | C.Prod (name,so,dest) -> C.Prod (name, aux so, aux dest)
133 | C.Lambda (name,so,dest) -> C.Lambda (name, aux so, aux dest)
134 | C.LetIn (name,so,dest) -> C.LetIn (name, aux so, aux dest)
135 | C.Appl l -> C.Appl (List.map aux l)
136 | C.Var (uri,exp_named_subst) ->
137 let exp_named_subst' =
138 List.map (fun (uri,t) -> (uri, aux t)) exp_named_subst
140 C.Var (uri, exp_named_subst')
141 | C.Const (uri, exp_named_subst) ->
142 let exp_named_subst' =
143 List.map (fun (uri,t) -> (uri, aux t)) exp_named_subst
145 C.Const (uri, exp_named_subst')
146 | C.MutInd (uri,tyno,exp_named_subst) ->
147 let exp_named_subst' =
148 List.map (fun (uri,t) -> (uri, aux t)) exp_named_subst
150 C.MutInd (uri, tyno, exp_named_subst')
151 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
152 let exp_named_subst' =
153 List.map (fun (uri,t) -> (uri, aux t)) exp_named_subst
155 C.MutConstruct (uri, tyno, consno, exp_named_subst')
156 | C.MutCase (uri,tyno,out,te,pl) ->
157 C.MutCase (uri, tyno, aux out, aux te, List.map aux pl)
161 (fun (name,j,ty,bo) -> (name, j, aux ty, aux bo)) fl
167 (fun (name,ty,bo) -> (name, aux ty, aux bo)) fl
173 (*** Functions to apply a substitution ***)
175 let apply_subst_gen ~appl_fun subst term =
177 let module C = Cic in
178 let module S = CicSubstitution in
181 | C.Var (uri,exp_named_subst) ->
182 let exp_named_subst' =
183 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
185 C.Var (uri, exp_named_subst')
188 let (_, t,_) = lookup_subst i subst in
189 um_aux (S.subst_meta l t)
190 with CicUtil.Subst_not_found _ ->
191 (* unconstrained variable, i.e. free in subst*)
193 List.map (function None -> None | Some t -> Some (um_aux t)) l
197 | C.Implicit _ as t -> t
198 | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
199 | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
200 | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
201 | C.LetIn (n,s,t) -> C.LetIn (n, um_aux s, um_aux t)
202 | C.Appl (hd :: tl) -> appl_fun um_aux hd tl
203 | C.Appl _ -> assert false
204 | C.Const (uri,exp_named_subst) ->
205 let exp_named_subst' =
206 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
208 C.Const (uri, exp_named_subst')
209 | C.MutInd (uri,typeno,exp_named_subst) ->
210 let exp_named_subst' =
211 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
213 C.MutInd (uri,typeno,exp_named_subst')
214 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
215 let exp_named_subst' =
216 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
218 C.MutConstruct (uri,typeno,consno,exp_named_subst')
219 | C.MutCase (sp,i,outty,t,pl) ->
220 let pl' = List.map um_aux pl in
221 C.MutCase (sp, i, um_aux outty, um_aux t, pl')
224 List.map (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo)) fl
229 List.map (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo)) fl
237 let appl_fun um_aux he tl =
238 let tl' = List.map um_aux tl in
241 Cic.Appl l -> Cic.Appl (l@tl')
242 | he' -> Cic.Appl (he'::tl')
246 Cic.Meta (m,_) -> CicReduction.head_beta_reduce t'
251 (* incr apply_subst_counter; *)
252 apply_subst_gen ~appl_fun s t
255 let profiler = HExtlib.profile "U/CicMetaSubst.apply_subst"
256 let apply_subst s t =
257 profiler.HExtlib.profile (apply_subst s) t
260 let rec apply_subst_context subst context =
262 incr apply_subst_context_counter;
263 context_length := !context_length + List.length context;
268 | Some (n, Cic.Decl t) ->
269 let t' = apply_subst subst t in
270 Some (n, Cic.Decl t') :: context
271 | Some (n, Cic.Def (t, ty)) ->
275 | Some ty -> Some (apply_subst subst ty)
277 let t' = apply_subst subst t in
278 Some (n, Cic.Def (t', ty')) :: context
279 | None -> None :: context)
282 let apply_subst_metasenv subst metasenv =
284 incr apply_subst_metasenv_counter;
285 metasenv_length := !metasenv_length + List.length metasenv;
288 (fun (n, context, ty) ->
289 (n, apply_subst_context subst context, apply_subst subst ty))
291 (fun (i, _, _) -> not (List.mem_assoc i subst))
294 (***** Pretty printing functions ******)
296 let ppterm ~metasenv subst term =
297 CicPp.ppterm ~metasenv (apply_subst subst term)
299 let ppterm_in_name_context ~metasenv subst term name_context =
300 CicPp.pp ~metasenv (apply_subst subst term) name_context
302 let ppterm_in_context ~metasenv subst term context =
304 List.map (function None -> None | Some (n,_) -> Some n) context
306 ppterm_in_name_context ~metasenv subst term name_context
308 let ppcontext' ~metasenv ?(sep = "\n") subst context =
309 let separate s = if s = "" then "" else s ^ sep in
311 (fun context_entry (i,name_context) ->
312 match context_entry with
313 Some (n,Cic.Decl t) ->
314 sprintf "%s%s : %s" (separate i) (CicPp.ppname n)
315 (ppterm_in_name_context ~metasenv subst t name_context),
316 (Some n)::name_context
317 | Some (n,Cic.Def (bo,ty)) ->
318 sprintf "%s%s : %s := %s" (separate i) (CicPp.ppname n)
321 | Some ty -> ppterm_in_name_context ~metasenv subst ty name_context)
322 (ppterm_in_name_context ~metasenv subst bo name_context), (Some n)::name_context
324 sprintf "%s_ :? _" (separate i), None::name_context
327 let ppsubst_unfolded ~metasenv subst =
330 (fun (idx, (c, t,_)) ->
331 let context,name_context = ppcontext' ~metasenv ~sep:"; " subst c in
332 sprintf "%s |- ?%d:= %s" context idx
333 (ppterm_in_name_context ~metasenv subst t name_context))
336 Printf.sprintf "?%d := %s" idx (CicPp.ppterm term))
340 let ppsubst ~metasenv subst =
343 (fun (idx, (c, t, _)) ->
344 let context,name_context = ppcontext' ~metasenv ~sep:"; " [] c in
345 sprintf "%s |- ?%d:= %s" context idx
346 (ppterm_in_name_context ~metasenv [] t name_context))
350 let ppcontext ~metasenv ?sep subst context =
351 fst (ppcontext' ~metasenv ?sep subst context)
353 let ppmetasenv ?(sep = "\n") subst metasenv =
357 let context,name_context = ppcontext' ~metasenv ~sep:"; " subst c in
358 sprintf "%s |- ?%d: %s" context i
359 (ppterm_in_name_context ~metasenv subst t name_context))
361 (fun (i, _, _) -> not (List.mem_assoc i subst))
364 let tempi_type_of_aux_subst = ref 0.0;;
365 let tempi_subst = ref 0.0;;
366 let tempi_type_of_aux = ref 0.0;;
369 (* the delift function takes in input a metavariable index, an ordered list of
370 * optional terms [t1,...,tn] and a term t, and substitutes every tk = Some
371 * (rel(nk)) with rel(k). Typically, the list of optional terms is the explicit
372 * substitution that is applied to a metavariable occurrence and the result of
373 * the delift function is a term the implicit variable can be substituted with
374 * to make the term [t] unifiable with the metavariable occurrence. In general,
375 * the problem is undecidable if we consider equivalence in place of alpha
376 * convertibility. Our implementation, though, is even weaker than alpha
377 * convertibility, since it replace the term [tk] if and only if [tk] is a Rel
378 * (missing all the other cases). Does this matter in practice?
379 * The metavariable index is the index of the metavariable that must not occur
380 * in the term (for occur check).
383 exception NotInTheList;;
388 [] -> raise NotInTheList
389 | (Some (Cic.Rel m))::_ when m=n -> k
390 | _::tl -> aux (k+1) tl in
396 let rec force_does_not_occur subst to_be_restricted t =
397 let module C = Cic in
398 let more_to_be_restricted = ref [] in
399 let rec aux k = function
400 C.Rel r when List.mem (r - k) to_be_restricted -> raise Occur
403 | C.Implicit _ -> assert false
405 (* we do not retrieve the term associated to ?n in subst since *)
406 (* in this way we can restrict if something goes wrong *)
418 more_to_be_restricted := (n,!i) :: !more_to_be_restricted;
423 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty)
424 | C.Prod (name,so,dest) -> C.Prod (name, aux k so, aux (k+1) dest)
425 | C.Lambda (name,so,dest) -> C.Lambda (name, aux k so, aux (k+1) dest)
426 | C.LetIn (name,so,dest) -> C.LetIn (name, aux k so, aux (k+1) dest)
427 | C.Appl l -> C.Appl (List.map (aux k) l)
428 | C.Var (uri,exp_named_subst) ->
429 let exp_named_subst' =
430 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
432 C.Var (uri, exp_named_subst')
433 | C.Const (uri, exp_named_subst) ->
434 let exp_named_subst' =
435 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
437 C.Const (uri, exp_named_subst')
438 | C.MutInd (uri,tyno,exp_named_subst) ->
439 let exp_named_subst' =
440 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
442 C.MutInd (uri, tyno, exp_named_subst')
443 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
444 let exp_named_subst' =
445 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
447 C.MutConstruct (uri, tyno, consno, exp_named_subst')
448 | C.MutCase (uri,tyno,out,te,pl) ->
449 C.MutCase (uri, tyno, aux k out, aux k te, List.map (aux k) pl)
451 let len = List.length fl in
452 let k_plus_len = k + len in
455 (fun (name,j,ty,bo) -> (name, j, aux k ty, aux k_plus_len bo)) fl
459 let len = List.length fl in
460 let k_plus_len = k + len in
463 (fun (name,ty,bo) -> (name, aux k ty, aux k_plus_len bo)) fl
468 (!more_to_be_restricted, res)
470 let rec restrict subst to_be_restricted metasenv =
471 let names_of_context_indexes context indexes =
476 match List.nth context (i-1) with
477 | None -> assert false
478 | Some (n, _) -> CicPp.ppname n
480 Failure _ -> assert false
483 let force_does_not_occur_in_context to_be_restricted = function
485 | Some (name, Cic.Decl t) ->
486 let (more_to_be_restricted, t') =
487 force_does_not_occur subst to_be_restricted t
489 more_to_be_restricted, Some (name, Cic.Decl t')
490 | Some (name, Cic.Def (bo, ty)) ->
491 let (more_to_be_restricted, bo') =
492 force_does_not_occur subst to_be_restricted bo
494 let more_to_be_restricted, ty' =
496 | None -> more_to_be_restricted, None
498 let more_to_be_restricted', ty' =
499 force_does_not_occur subst to_be_restricted ty
501 more_to_be_restricted @ more_to_be_restricted',
504 more_to_be_restricted, Some (name, Cic.Def (bo', ty'))
506 let rec erase i to_be_restricted n = function
507 | [] -> [], to_be_restricted, []
509 let more_to_be_restricted,restricted,tl' =
510 erase (i+1) to_be_restricted n tl
512 let restrict_me = List.mem i restricted in
514 more_to_be_restricted, restricted, None:: tl'
517 let more_to_be_restricted', hd' =
518 let delifted_restricted =
522 | j::tl when j > i -> (j - i)::aux tl
527 force_does_not_occur_in_context delifted_restricted hd
529 more_to_be_restricted @ more_to_be_restricted',
530 restricted, hd' :: tl'
532 more_to_be_restricted, (i :: restricted), None :: tl')
534 let (more_to_be_restricted, metasenv) = (* restrict metasenv *)
536 (fun (n, context, t) (more, metasenv) ->
537 let to_be_restricted =
538 List.map snd (List.filter (fun (m, _) -> m = n) to_be_restricted)
540 let (more_to_be_restricted, restricted, context') =
541 (* just an optimization *)
542 if to_be_restricted = [] then
545 erase 1 to_be_restricted n context
548 let more_to_be_restricted', t' =
549 force_does_not_occur subst restricted t
551 let metasenv' = (n, context', t') :: metasenv in
552 (more @ more_to_be_restricted @ more_to_be_restricted',
555 raise (MetaSubstFailure (lazy (sprintf
556 "Cannot restrict the context of the metavariable ?%d over the hypotheses %s since metavariable's type depends on at least one of them"
557 n (names_of_context_indexes context to_be_restricted)))))
560 let (more_to_be_restricted', subst) = (* restrict subst *)
562 (* TODO: cambiare dopo l'aggiunta del ty *)
563 (fun (n, (context, term,ty)) (more, subst') ->
564 let to_be_restricted =
565 List.map snd (List.filter (fun (m, _) -> m = n) to_be_restricted)
568 let (more_to_be_restricted, restricted, context') =
569 (* just an optimization *)
570 if to_be_restricted = [] then
573 erase 1 to_be_restricted n context
575 let more_to_be_restricted', term' =
576 force_does_not_occur subst restricted term
578 let more_to_be_restricted'', ty' =
579 force_does_not_occur subst restricted ty in
580 let subst' = (n, (context', term',ty')) :: subst' in
582 more @ more_to_be_restricted
583 @ more_to_be_restricted'@more_to_be_restricted'' in
586 let error_msg = lazy (sprintf
587 "Cannot restrict the context of the metavariable ?%d over the hypotheses %s since ?%d is already instantiated with %s and at least one of the hypotheses occurs in the substituted term"
588 n (names_of_context_indexes context to_be_restricted) n
589 (ppterm ~metasenv subst term))
592 debug_print (lazy error_msg);
593 debug_print (lazy ("metasenv = \n" ^ (ppmetasenv metasenv subst)));
594 debug_print (lazy ("subst = \n" ^ (ppsubst subst)));
595 debug_print (lazy ("context = \n" ^ (ppcontext subst context))); *)
596 raise (MetaSubstFailure error_msg)))
599 match more_to_be_restricted @ more_to_be_restricted' with
600 | [] -> (metasenv, subst)
601 | l -> restrict subst l metasenv
604 (*CSC: maybe we should rename delift in abstract, as I did in my dissertation *)(*Andrea: maybe not*)
606 let delift n subst context metasenv l t =
607 (* INVARIANT: we suppose that t is not another occurrence of Meta(n,_),
608 otherwise the occur check does not make sense *)
611 debug_print (lazy ("sto deliftando il termine " ^ (CicPp.ppterm t) ^ " rispetto
612 al contesto locale " ^ (CicPp.ppterm (Cic.Meta(0,l)))));
615 let module S = CicSubstitution in
617 let (_, canonical_context, _) = CicUtil.lookup_meta n metasenv in
618 List.map2 (fun ct lt ->
624 let to_be_restricted = ref [] in
625 let rec deliftaux k =
626 let module C = Cic in
633 match List.nth context (m-k-1) with
634 Some (_,C.Def (t,_)) ->
635 (*CSC: Hmmm. This bit of reduction is not in the spirit of *)
636 (*CSC: first order unification. Does it help or does it harm? *)
637 (*CSC: ANSWER: it hurts performances since it is possible to *)
638 (*CSC: have an exponential explosion of the size of the proof.*)
639 (*CSC: However, without this bit of reduction some "apply" in *)
640 (*CSC: the library fail (e.g. nat/nth_prime.ma). *)
641 deliftaux k (S.lift m t)
642 | Some (_,C.Decl t) ->
643 C.Rel ((position (m-k) l) + k)
644 | None -> raise (MetaSubstFailure (lazy "RelToHiddenHypothesis"))
647 raise (MetaSubstFailure (lazy "Unbound variable found in deliftaux"))
649 | C.Var (uri,exp_named_subst) ->
650 let exp_named_subst' =
651 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
653 C.Var (uri,exp_named_subst')
654 | C.Meta (i, l1) as t ->
656 let (_,t,_) = CicUtil.lookup_subst i subst in
657 deliftaux k (CicSubstitution.subst_meta l1 t)
658 with CicUtil.Subst_not_found _ ->
659 (* see the top level invariant *)
661 raise (MetaSubstFailure (lazy (sprintf
662 "Cannot unify the metavariable ?%d with a term that has as subterm %s in which the same metavariable occurs (occur check)"
663 i (ppterm ~metasenv subst t))))
666 (* I do not consider the term associated to ?i in subst since *)
667 (* in this way I can restrict if something goes wrong. *)
671 | None::tl -> None::(deliftl (j+1) tl)
673 let l1' = (deliftl (j+1) tl) in
675 Some (deliftaux k t)::l1'
678 | MetaSubstFailure _ ->
680 (i,j)::!to_be_restricted ; None::l1'
682 let l' = deliftl 1 l1 in
686 | C.Implicit _ as t -> t
687 | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
688 | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
689 | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
690 | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
691 | C.Appl l -> C.Appl (List.map (deliftaux k) l)
692 | C.Const (uri,exp_named_subst) ->
693 let exp_named_subst' =
694 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
696 C.Const (uri,exp_named_subst')
697 | C.MutInd (uri,typeno,exp_named_subst) ->
698 let exp_named_subst' =
699 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
701 C.MutInd (uri,typeno,exp_named_subst')
702 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
703 let exp_named_subst' =
704 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
706 C.MutConstruct (uri,typeno,consno,exp_named_subst')
707 | C.MutCase (sp,i,outty,t,pl) ->
708 C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
709 List.map (deliftaux k) pl)
711 let len = List.length fl in
714 (fun (name, i, ty, bo) ->
715 (name, i, deliftaux k ty, deliftaux (k+len) bo))
720 let len = List.length fl in
723 (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
726 C.CoFix (i, liftedfl)
733 (* This is the case where we fail even first order unification. *)
734 (* The reason is that our delift function is weaker than first *)
735 (* order (in the sense of alpha-conversion). See comment above *)
736 (* related to the delift function. *)
737 (* debug_print (lazy "First Order UnificationFailure during delift") ;
738 debug_print(lazy (sprintf
739 "Error trying to abstract %s over [%s]: the algorithm only tried to abstract over bound variables"
743 (function Some t -> ppterm subst t | None -> "_") l
745 let msg = (lazy (sprintf
746 "Error trying to abstract %s over [%s]: the algorithm only tried to abstract over bound variables"
747 (ppterm ~metasenv subst t)
750 (function Some t -> ppterm ~metasenv subst t | None -> "_")
756 Some t -> CicUtil.is_meta_closed (apply_subst subst t)
759 raise (Uncertain msg)
761 raise (MetaSubstFailure msg)
763 let (metasenv, subst) = restrict subst !to_be_restricted metasenv in
767 (* delifts a term t of n levels strating from k, that is changes (Rel m)
768 * to (Rel (m - n)) when m > (k + n). if k <= m < k + n delift fails
770 let delift_rels_from subst metasenv k n =
771 let rec liftaux subst metasenv k =
772 let module C = Cic in
776 C.Rel m, subst, metasenv
777 else if m < k + n then
778 raise DeliftingARelWouldCaptureAFreeVariable
780 C.Rel (m - n), subst, metasenv
781 | C.Var (uri,exp_named_subst) ->
782 let exp_named_subst',subst,metasenv =
784 (fun (uri,t) (l,subst,metasenv) ->
785 let t',subst,metasenv = liftaux subst metasenv k t in
786 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
788 C.Var (uri,exp_named_subst'),subst,metasenv
791 let (_, t,_) = lookup_subst i subst in
792 liftaux subst metasenv k (CicSubstitution.subst_meta l t)
793 with CicUtil.Subst_not_found _ ->
794 let l',to_be_restricted,subst,metasenv =
795 let rec aux con l subst metasenv =
797 [] -> [],[],subst,metasenv
799 let tl',to_be_restricted,subst,metasenv =
800 aux (con + 1) tl subst metasenv in
801 let he',more_to_be_restricted,subst,metasenv =
803 None -> None,[],subst,metasenv
806 let t',subst,metasenv = liftaux subst metasenv k t in
807 Some t',[],subst,metasenv
809 DeliftingARelWouldCaptureAFreeVariable ->
810 None,[i,con],subst,metasenv
812 he'::tl',more_to_be_restricted@to_be_restricted,subst,metasenv
814 aux 1 l subst metasenv in
815 let metasenv,subst = restrict subst to_be_restricted metasenv in
816 C.Meta(i,l'),subst,metasenv)
817 | C.Sort _ as t -> t,subst,metasenv
818 | C.Implicit _ as t -> t,subst,metasenv
820 let te',subst,metasenv = liftaux subst metasenv k te in
821 let ty',subst,metasenv = liftaux subst metasenv k ty in
822 C.Cast (te',ty'),subst,metasenv
824 let s',subst,metasenv = liftaux subst metasenv k s in
825 let t',subst,metasenv = liftaux subst metasenv (k+1) t in
826 C.Prod (n,s',t'),subst,metasenv
827 | C.Lambda (n,s,t) ->
828 let s',subst,metasenv = liftaux subst metasenv k s in
829 let t',subst,metasenv = liftaux subst metasenv (k+1) t in
830 C.Lambda (n,s',t'),subst,metasenv
832 let s',subst,metasenv = liftaux subst metasenv k s in
833 let t',subst,metasenv = liftaux subst metasenv (k+1) t in
834 C.LetIn (n,s',t'),subst,metasenv
836 let l',subst,metasenv =
838 (fun t (l,subst,metasenv) ->
839 let t',subst,metasenv = liftaux subst metasenv k t in
840 t'::l,subst,metasenv) l ([],subst,metasenv) in
841 C.Appl l',subst,metasenv
842 | C.Const (uri,exp_named_subst) ->
843 let exp_named_subst',subst,metasenv =
845 (fun (uri,t) (l,subst,metasenv) ->
846 let t',subst,metasenv = liftaux subst metasenv k t in
847 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
849 C.Const (uri,exp_named_subst'),subst,metasenv
850 | C.MutInd (uri,tyno,exp_named_subst) ->
851 let exp_named_subst',subst,metasenv =
853 (fun (uri,t) (l,subst,metasenv) ->
854 let t',subst,metasenv = liftaux subst metasenv k t in
855 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
857 C.MutInd (uri,tyno,exp_named_subst'),subst,metasenv
858 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
859 let exp_named_subst',subst,metasenv =
861 (fun (uri,t) (l,subst,metasenv) ->
862 let t',subst,metasenv = liftaux subst metasenv k t in
863 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
865 C.MutConstruct (uri,tyno,consno,exp_named_subst'),subst,metasenv
866 | C.MutCase (sp,i,outty,t,pl) ->
867 let outty',subst,metasenv = liftaux subst metasenv k outty in
868 let t',subst,metasenv = liftaux subst metasenv k t in
869 let pl',subst,metasenv =
871 (fun t (l,subst,metasenv) ->
872 let t',subst,metasenv = liftaux subst metasenv k t in
873 t'::l,subst,metasenv) pl ([],subst,metasenv)
875 C.MutCase (sp,i,outty',t',pl'),subst,metasenv
877 let len = List.length fl in
878 let liftedfl,subst,metasenv =
880 (fun (name, i, ty, bo) (l,subst,metasenv) ->
881 let ty',subst,metasenv = liftaux subst metasenv k ty in
882 let bo',subst,metasenv = liftaux subst metasenv (k+len) bo in
883 (name,i,ty',bo')::l,subst,metasenv
884 ) fl ([],subst,metasenv)
886 C.Fix (i, liftedfl),subst,metasenv
888 let len = List.length fl in
889 let liftedfl,subst,metasenv =
891 (fun (name, ty, bo) (l,subst,metasenv) ->
892 let ty',subst,metasenv = liftaux subst metasenv k ty in
893 let bo',subst,metasenv = liftaux subst metasenv (k+len) bo in
894 (name,ty',bo')::l,subst,metasenv
895 ) fl ([],subst,metasenv)
897 C.CoFix (i, liftedfl),subst,metasenv
899 liftaux subst metasenv k
901 let delift_rels subst metasenv n t =
902 delift_rels_from subst metasenv 1 n t
905 (**** END OF DELIFT ****)
908 (** {2 Format-like pretty printers} *)
911 Format.pp_print_string ppf s;
912 Format.pp_print_newline ppf ();
913 Format.pp_print_flush ppf ()
915 let fppsubst ppf subst = fpp_gen ppf (ppsubst ~metasenv:[] subst)
916 let fppterm ppf term = fpp_gen ppf (CicPp.ppterm term)
917 let fppmetasenv ppf metasenv = fpp_gen ppf (ppmetasenv [] metasenv)