1 (* Copyright (C) 2003, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
32 let deref_counter = ref 0
33 let apply_subst_context_counter = ref 0
34 let apply_subst_metasenv_counter = ref 0
35 let lift_counter = ref 0
36 let subst_counter = ref 0
37 let whd_counter = ref 0
38 let are_convertible_counter = ref 0
39 let metasenv_length = ref 0
40 let context_length = ref 0
41 let reset_counters () =
42 apply_subst_counter := 0;
43 apply_subst_context_counter := 0;
44 apply_subst_metasenv_counter := 0;
48 are_convertible_counter := 0;
51 let print_counters () =
52 debug_print (lazy (Printf.sprintf
54 apply_subst_context: %d
55 apply_subst_metasenv: %d
60 metasenv length: %d (avg = %.2f)
61 context length: %d (avg = %.2f)
63 !apply_subst_counter !apply_subst_context_counter
64 !apply_subst_metasenv_counter !lift_counter !subst_counter !whd_counter
65 !are_convertible_counter !metasenv_length
66 ((float !metasenv_length) /. (float !apply_subst_metasenv_counter))
68 ((float !context_length) /. (float !apply_subst_context_counter))
73 exception MetaSubstFailure of string Lazy.t
74 exception Uncertain of string Lazy.t
75 exception AssertFailure of string Lazy.t
76 exception DeliftingARelWouldCaptureAFreeVariable;;
78 let debug_print = fun _ -> ()
80 type substitution = (int * (Cic.context * Cic.term)) list
84 let third _,_,a = a in
89 (CicSubstitution.subst_meta
90 l (third (CicUtil.lookup_subst n subst)))
92 CicUtil.Subst_not_found _ -> t)
97 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 rec apply_subst_context subst context =
257 incr apply_subst_context_counter;
258 context_length := !context_length + List.length context;
263 | Some (n, Cic.Decl t) ->
264 let t' = apply_subst subst t in
265 Some (n, Cic.Decl t') :: context
266 | Some (n, Cic.Def (t, ty)) ->
270 | Some ty -> Some (apply_subst subst ty)
272 let t' = apply_subst subst t in
273 Some (n, Cic.Def (t', ty')) :: context
274 | None -> None :: context)
277 let apply_subst_metasenv subst metasenv =
279 incr apply_subst_metasenv_counter;
280 metasenv_length := !metasenv_length + List.length metasenv;
283 (fun (n, context, ty) ->
284 (n, apply_subst_context subst context, apply_subst subst ty))
286 (fun (i, _, _) -> not (List.mem_assoc i subst))
289 (***** Pretty printing functions ******)
291 let ppterm subst term = CicPp.ppterm (apply_subst subst term)
293 let ppterm_in_name_context subst term name_context =
294 CicPp.pp (apply_subst subst term) name_context
296 let ppterm_in_context subst term context =
298 List.map (function None -> None | Some (n,_) -> Some n) context
300 ppterm_in_name_context subst term name_context
302 let ppcontext' ?(sep = "\n") subst context =
303 let separate s = if s = "" then "" else s ^ sep in
305 (fun context_entry (i,name_context) ->
306 match context_entry with
307 Some (n,Cic.Decl t) ->
308 sprintf "%s%s : %s" (separate i) (CicPp.ppname n)
309 (ppterm_in_name_context subst t name_context), (Some n)::name_context
310 | Some (n,Cic.Def (bo,ty)) ->
311 sprintf "%s%s : %s := %s" (separate i) (CicPp.ppname n)
314 | Some ty -> ppterm_in_name_context subst ty name_context)
315 (ppterm_in_name_context subst bo name_context), (Some n)::name_context
317 sprintf "%s_ :? _" (separate i), None::name_context
320 let ppsubst_unfolded subst =
323 (fun (idx, (c, t,_)) ->
324 let context,name_context = ppcontext' ~sep:"; " subst c in
325 sprintf "%s |- ?%d:= %s" context idx
326 (ppterm_in_name_context subst t name_context))
329 Printf.sprintf "?%d := %s" idx (CicPp.ppterm term))
336 (fun (idx, (c, t, _)) ->
337 let context,name_context = ppcontext' ~sep:"; " [] c in
338 sprintf "%s |- ?%d:= %s" context idx
339 (ppterm_in_name_context [] t name_context))
343 let ppcontext ?sep subst context = fst (ppcontext' ?sep subst context)
345 let ppmetasenv ?(sep = "\n") subst metasenv =
349 let context,name_context = ppcontext' ~sep:"; " subst c in
350 sprintf "%s |- ?%d: %s" context i
351 (ppterm_in_name_context subst t name_context))
353 (fun (i, _, _) -> not (List.mem_assoc i subst))
356 let tempi_type_of_aux_subst = ref 0.0;;
357 let tempi_subst = ref 0.0;;
358 let tempi_type_of_aux = ref 0.0;;
361 (* the delift function takes in input a metavariable index, an ordered list of
362 * optional terms [t1,...,tn] and a term t, and substitutes every tk = Some
363 * (rel(nk)) with rel(k). Typically, the list of optional terms is the explicit
364 * substitution that is applied to a metavariable occurrence and the result of
365 * the delift function is a term the implicit variable can be substituted with
366 * to make the term [t] unifiable with the metavariable occurrence. In general,
367 * the problem is undecidable if we consider equivalence in place of alpha
368 * convertibility. Our implementation, though, is even weaker than alpha
369 * convertibility, since it replace the term [tk] if and only if [tk] is a Rel
370 * (missing all the other cases). Does this matter in practice?
371 * The metavariable index is the index of the metavariable that must not occur
372 * in the term (for occur check).
375 exception NotInTheList;;
380 [] -> raise NotInTheList
381 | (Some (Cic.Rel m))::_ when m=n -> k
382 | _::tl -> aux (k+1) tl in
388 let rec force_does_not_occur subst to_be_restricted t =
389 let module C = Cic in
390 let more_to_be_restricted = ref [] in
391 let rec aux k = function
392 C.Rel r when List.mem (r - k) to_be_restricted -> raise Occur
395 | C.Implicit _ -> assert false
397 (* we do not retrieve the term associated to ?n in subst since *)
398 (* in this way we can restrict if something goes wrong *)
410 more_to_be_restricted := (n,!i) :: !more_to_be_restricted;
415 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty)
416 | C.Prod (name,so,dest) -> C.Prod (name, aux k so, aux (k+1) dest)
417 | C.Lambda (name,so,dest) -> C.Lambda (name, aux k so, aux (k+1) dest)
418 | C.LetIn (name,so,dest) -> C.LetIn (name, aux k so, aux (k+1) dest)
419 | C.Appl l -> C.Appl (List.map (aux k) l)
420 | C.Var (uri,exp_named_subst) ->
421 let exp_named_subst' =
422 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
424 C.Var (uri, exp_named_subst')
425 | C.Const (uri, exp_named_subst) ->
426 let exp_named_subst' =
427 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
429 C.Const (uri, exp_named_subst')
430 | C.MutInd (uri,tyno,exp_named_subst) ->
431 let exp_named_subst' =
432 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
434 C.MutInd (uri, tyno, exp_named_subst')
435 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
436 let exp_named_subst' =
437 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
439 C.MutConstruct (uri, tyno, consno, exp_named_subst')
440 | C.MutCase (uri,tyno,out,te,pl) ->
441 C.MutCase (uri, tyno, aux k out, aux k te, List.map (aux k) pl)
443 let len = List.length fl in
444 let k_plus_len = k + len in
447 (fun (name,j,ty,bo) -> (name, j, aux k ty, aux k_plus_len bo)) fl
451 let len = List.length fl in
452 let k_plus_len = k + len in
455 (fun (name,ty,bo) -> (name, aux k ty, aux k_plus_len bo)) fl
460 (!more_to_be_restricted, res)
462 let rec restrict subst to_be_restricted metasenv =
463 let names_of_context_indexes context indexes =
468 match List.nth context (i-1) with
469 | None -> assert false
470 | Some (n, _) -> CicPp.ppname n
472 Failure _ -> assert false
475 let force_does_not_occur_in_context to_be_restricted = function
477 | Some (name, Cic.Decl t) ->
478 let (more_to_be_restricted, t') =
479 force_does_not_occur subst to_be_restricted t
481 more_to_be_restricted, Some (name, Cic.Decl t')
482 | Some (name, Cic.Def (bo, ty)) ->
483 let (more_to_be_restricted, bo') =
484 force_does_not_occur subst to_be_restricted bo
486 let more_to_be_restricted, ty' =
488 | None -> more_to_be_restricted, None
490 let more_to_be_restricted', ty' =
491 force_does_not_occur subst to_be_restricted ty
493 more_to_be_restricted @ more_to_be_restricted',
496 more_to_be_restricted, Some (name, Cic.Def (bo', ty'))
498 let rec erase i to_be_restricted n = function
499 | [] -> [], to_be_restricted, []
501 let more_to_be_restricted,restricted,tl' =
502 erase (i+1) to_be_restricted n tl
504 let restrict_me = List.mem i restricted in
506 more_to_be_restricted, restricted, None:: tl'
509 let more_to_be_restricted', hd' =
510 let delifted_restricted =
514 | j::tl when j > i -> (j - i)::aux tl
519 force_does_not_occur_in_context delifted_restricted hd
521 more_to_be_restricted @ more_to_be_restricted',
522 restricted, hd' :: tl'
524 more_to_be_restricted, (i :: restricted), None :: tl')
526 let (more_to_be_restricted, metasenv) = (* restrict metasenv *)
528 (fun (n, context, t) (more, metasenv) ->
529 let to_be_restricted =
530 List.map snd (List.filter (fun (m, _) -> m = n) to_be_restricted)
532 let (more_to_be_restricted, restricted, context') =
533 (* just an optimization *)
534 if to_be_restricted = [] then
537 erase 1 to_be_restricted n context
540 let more_to_be_restricted', t' =
541 force_does_not_occur subst restricted t
543 let metasenv' = (n, context', t') :: metasenv in
544 (more @ more_to_be_restricted @ more_to_be_restricted',
547 raise (MetaSubstFailure (lazy (sprintf
548 "Cannot restrict the context of the metavariable ?%d over the hypotheses %s since metavariable's type depends on at least one of them"
549 n (names_of_context_indexes context to_be_restricted)))))
552 let (more_to_be_restricted', subst) = (* restrict subst *)
554 (* TODO: cambiare dopo l'aggiunta del ty *)
555 (fun (n, (context, term,ty)) (more, subst') ->
556 let to_be_restricted =
557 List.map snd (List.filter (fun (m, _) -> m = n) to_be_restricted)
560 let (more_to_be_restricted, restricted, context') =
561 (* just an optimization *)
562 if to_be_restricted = [] then
565 erase 1 to_be_restricted n context
567 let more_to_be_restricted', term' =
568 force_does_not_occur subst restricted term
570 let more_to_be_restricted'', ty' =
571 force_does_not_occur subst restricted ty in
572 let subst' = (n, (context', term',ty')) :: subst' in
574 more @ more_to_be_restricted
575 @ more_to_be_restricted'@more_to_be_restricted'' in
578 let error_msg = lazy (sprintf
579 "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"
580 n (names_of_context_indexes context to_be_restricted) n
584 debug_print (lazy error_msg);
585 debug_print (lazy ("metasenv = \n" ^ (ppmetasenv metasenv subst)));
586 debug_print (lazy ("subst = \n" ^ (ppsubst subst)));
587 debug_print (lazy ("context = \n" ^ (ppcontext subst context))); *)
588 raise (MetaSubstFailure error_msg)))
591 match more_to_be_restricted @ more_to_be_restricted' with
592 | [] -> (metasenv, subst)
593 | l -> restrict subst l metasenv
596 (*CSC: maybe we should rename delift in abstract, as I did in my dissertation *)(*Andrea: maybe not*)
598 let delift n subst context metasenv l t =
599 (* INVARIANT: we suppose that t is not another occurrence of Meta(n,_),
600 otherwise the occur check does not make sense *)
603 debug_print (lazy ("sto deliftando il termine " ^ (CicPp.ppterm t) ^ " rispetto
604 al contesto locale " ^ (CicPp.ppterm (Cic.Meta(0,l)))));
607 let module S = CicSubstitution in
609 let (_, canonical_context, _) = CicUtil.lookup_meta n metasenv in
610 List.map2 (fun ct lt ->
616 let to_be_restricted = ref [] in
617 let rec deliftaux k =
618 let module C = Cic in
622 C.Rel m (*CSC: che succede se c'e' un Def? Dovrebbe averlo gia' *)
623 (*CSC: deliftato la regola per il LetIn *)
624 (*CSC: FALSO! La regola per il LetIn non lo fa *)
627 match List.nth context (m-k-1) with
628 Some (_,C.Def (t,_)) ->
629 (*CSC: Hmmm. This bit of reduction is not in the spirit of *)
630 (*CSC: first order unification. Does it help or does it harm? *)
631 deliftaux k (S.lift m t)
632 | Some (_,C.Decl t) ->
633 C.Rel ((position (m-k) l) + k)
634 | None -> raise (MetaSubstFailure (lazy "RelToHiddenHypothesis"))
637 raise (MetaSubstFailure (lazy "Unbound variable found in deliftaux"))
639 | C.Var (uri,exp_named_subst) ->
640 let exp_named_subst' =
641 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
643 C.Var (uri,exp_named_subst')
644 | C.Meta (i, l1) as t ->
646 let (_,t,_) = CicUtil.lookup_subst i subst in
647 deliftaux k (CicSubstitution.subst_meta l1 t)
648 with CicUtil.Subst_not_found _ ->
649 (* see the top level invariant *)
651 raise (MetaSubstFailure (lazy (sprintf
652 "Cannot unify the metavariable ?%d with a term that has as subterm %s in which the same metavariable occurs (occur check)"
653 i (ppterm subst t))))
656 (* I do not consider the term associated to ?i in subst since *)
657 (* in this way I can restrict if something goes wrong. *)
661 | None::tl -> None::(deliftl (j+1) tl)
663 let l1' = (deliftl (j+1) tl) in
665 Some (deliftaux k t)::l1'
668 | MetaSubstFailure _ ->
670 (i,j)::!to_be_restricted ; None::l1'
672 let l' = deliftl 1 l1 in
676 | C.Implicit _ as t -> t
677 | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
678 | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
679 | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
680 | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
681 | C.Appl l -> C.Appl (List.map (deliftaux k) l)
682 | C.Const (uri,exp_named_subst) ->
683 let exp_named_subst' =
684 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
686 C.Const (uri,exp_named_subst')
687 | C.MutInd (uri,typeno,exp_named_subst) ->
688 let exp_named_subst' =
689 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
691 C.MutInd (uri,typeno,exp_named_subst')
692 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
693 let exp_named_subst' =
694 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
696 C.MutConstruct (uri,typeno,consno,exp_named_subst')
697 | C.MutCase (sp,i,outty,t,pl) ->
698 C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
699 List.map (deliftaux k) pl)
701 let len = List.length fl in
704 (fun (name, i, ty, bo) ->
705 (name, i, deliftaux k ty, deliftaux (k+len) bo))
710 let len = List.length fl in
713 (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
716 C.CoFix (i, liftedfl)
723 (* This is the case where we fail even first order unification. *)
724 (* The reason is that our delift function is weaker than first *)
725 (* order (in the sense of alpha-conversion). See comment above *)
726 (* related to the delift function. *)
727 (* debug_print (lazy "First Order UnificationFailure during delift") ;
728 debug_print(lazy (sprintf
729 "Error trying to abstract %s over [%s]: the algorithm only tried to abstract over bound variables"
733 (function Some t -> ppterm subst t | None -> "_") l
735 raise (Uncertain (lazy (sprintf
736 "Error trying to abstract %s over [%s]: the algorithm only tried to abstract over bound variables"
740 (function Some t -> ppterm subst t | None -> "_")
743 let (metasenv, subst) = restrict subst !to_be_restricted metasenv in
747 (* delifts a term t of n levels strating from k, that is changes (Rel m)
748 * to (Rel (m - n)) when m > (k + n). if k <= m < k + n delift fails
750 let delift_rels_from subst metasenv k n =
751 let rec liftaux subst metasenv k =
752 let module C = Cic in
756 C.Rel m, subst, metasenv
757 else if m < k + n then
758 raise DeliftingARelWouldCaptureAFreeVariable
760 C.Rel (m - n), subst, metasenv
761 | C.Var (uri,exp_named_subst) ->
762 let exp_named_subst',subst,metasenv =
764 (fun (uri,t) (l,subst,metasenv) ->
765 let t',subst,metasenv = liftaux subst metasenv k t in
766 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
768 C.Var (uri,exp_named_subst'),subst,metasenv
771 let (_, t,_) = lookup_subst i subst in
772 liftaux subst metasenv k (CicSubstitution.subst_meta l t)
773 with CicUtil.Subst_not_found _ ->
774 let l',to_be_restricted,subst,metasenv =
775 let rec aux con l subst metasenv =
777 [] -> [],[],subst,metasenv
779 let tl',to_be_restricted,subst,metasenv =
780 aux (con + 1) tl subst metasenv in
781 let he',more_to_be_restricted,subst,metasenv =
783 None -> None,[],subst,metasenv
786 let t',subst,metasenv = liftaux subst metasenv k t in
787 Some t',[],subst,metasenv
789 DeliftingARelWouldCaptureAFreeVariable ->
790 None,[i,con],subst,metasenv
792 he'::tl',more_to_be_restricted@to_be_restricted,subst,metasenv
794 aux 1 l subst metasenv in
795 let metasenv,subst = restrict subst to_be_restricted metasenv in
796 C.Meta(i,l'),subst,metasenv)
797 | C.Sort _ as t -> t,subst,metasenv
798 | C.Implicit _ as t -> t,subst,metasenv
800 let te',subst,metasenv = liftaux subst metasenv k te in
801 let ty',subst,metasenv = liftaux subst metasenv k ty in
802 C.Cast (te',ty'),subst,metasenv
804 let s',subst,metasenv = liftaux subst metasenv k s in
805 let t',subst,metasenv = liftaux subst metasenv (k+1) t in
806 C.Prod (n,s',t'),subst,metasenv
807 | C.Lambda (n,s,t) ->
808 let s',subst,metasenv = liftaux subst metasenv k s in
809 let t',subst,metasenv = liftaux subst metasenv (k+1) t in
810 C.Lambda (n,s',t'),subst,metasenv
812 let s',subst,metasenv = liftaux subst metasenv k s in
813 let t',subst,metasenv = liftaux subst metasenv (k+1) t in
814 C.LetIn (n,s',t'),subst,metasenv
816 let l',subst,metasenv =
818 (fun t (l,subst,metasenv) ->
819 let t',subst,metasenv = liftaux subst metasenv k t in
820 t'::l,subst,metasenv) l ([],subst,metasenv) in
821 C.Appl l',subst,metasenv
822 | C.Const (uri,exp_named_subst) ->
823 let exp_named_subst',subst,metasenv =
825 (fun (uri,t) (l,subst,metasenv) ->
826 let t',subst,metasenv = liftaux subst metasenv k t in
827 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
829 C.Const (uri,exp_named_subst'),subst,metasenv
830 | C.MutInd (uri,tyno,exp_named_subst) ->
831 let exp_named_subst',subst,metasenv =
833 (fun (uri,t) (l,subst,metasenv) ->
834 let t',subst,metasenv = liftaux subst metasenv k t in
835 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
837 C.MutInd (uri,tyno,exp_named_subst'),subst,metasenv
838 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
839 let exp_named_subst',subst,metasenv =
841 (fun (uri,t) (l,subst,metasenv) ->
842 let t',subst,metasenv = liftaux subst metasenv k t in
843 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
845 C.MutConstruct (uri,tyno,consno,exp_named_subst'),subst,metasenv
846 | C.MutCase (sp,i,outty,t,pl) ->
847 let outty',subst,metasenv = liftaux subst metasenv k outty in
848 let t',subst,metasenv = liftaux subst metasenv k t in
849 let pl',subst,metasenv =
851 (fun t (l,subst,metasenv) ->
852 let t',subst,metasenv = liftaux subst metasenv k t in
853 t'::l,subst,metasenv) pl ([],subst,metasenv)
855 C.MutCase (sp,i,outty',t',pl'),subst,metasenv
857 let len = List.length fl in
858 let liftedfl,subst,metasenv =
860 (fun (name, i, ty, bo) (l,subst,metasenv) ->
861 let ty',subst,metasenv = liftaux subst metasenv k ty in
862 let bo',subst,metasenv = liftaux subst metasenv (k+len) bo in
863 (name,i,ty',bo')::l,subst,metasenv
864 ) fl ([],subst,metasenv)
866 C.Fix (i, liftedfl),subst,metasenv
868 let len = List.length fl in
869 let liftedfl,subst,metasenv =
871 (fun (name, ty, bo) (l,subst,metasenv) ->
872 let ty',subst,metasenv = liftaux subst metasenv k ty in
873 let bo',subst,metasenv = liftaux subst metasenv (k+len) bo in
874 (name,ty',bo')::l,subst,metasenv
875 ) fl ([],subst,metasenv)
877 C.CoFix (i, liftedfl),subst,metasenv
879 liftaux subst metasenv k
881 let delift_rels subst metasenv n t =
882 delift_rels_from subst metasenv 1 n t
885 (**** END OF DELIFT ****)
888 (** {2 Format-like pretty printers} *)
891 Format.pp_print_string ppf s;
892 Format.pp_print_newline ppf ();
893 Format.pp_print_flush ppf ()
895 let fppsubst ppf subst = fpp_gen ppf (ppsubst subst)
896 let fppterm ppf term = fpp_gen ppf (CicPp.ppterm term)
897 let fppmetasenv ppf metasenv = fpp_gen ppf (ppmetasenv [] metasenv)