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,ty,t) -> C.LetIn (n, um_aux s, um_aux ty, 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; *)
254 | _ -> apply_subst_gen ~appl_fun subst t
257 let profiler = HExtlib.profile "U/CicMetaSubst.apply_subst"
258 let apply_subst s t =
259 profiler.HExtlib.profile (apply_subst s) t
262 let apply_subst_context subst context =
267 incr apply_subst_context_counter;
268 context_length := !context_length + List.length context;
273 | Some (n, Cic.Decl t) ->
274 let t' = apply_subst subst t in
275 Some (n, Cic.Decl t') :: context
276 | Some (n, Cic.Def (t, ty)) ->
277 let ty' = apply_subst subst ty in
278 let t' = apply_subst subst t in
279 Some (n, Cic.Def (t', ty')) :: context
280 | None -> None :: context)
283 let apply_subst_metasenv subst metasenv =
285 incr apply_subst_metasenv_counter;
286 metasenv_length := !metasenv_length + List.length metasenv;
292 (fun (n, context, ty) ->
293 (n, apply_subst_context subst context, apply_subst subst ty))
295 (fun (i, _, _) -> not (List.mem_assoc i subst))
298 (***** Pretty printing functions ******)
300 let ppterm ~metasenv subst term =
301 CicPp.ppterm ~metasenv (apply_subst subst term)
303 let ppterm_in_context ~metasenv subst term context =
305 List.map (function None -> None | Some (n,_) -> Some n) context
307 CicPp.pp ~metasenv (apply_subst subst term) name_context
309 let ppterm_in_context_ref = ref ppterm_in_context
310 let set_ppterm_in_context f =
311 ppterm_in_context_ref := f
312 let use_low_level_ppterm_in_context = ref false
314 let ppterm_in_context ~metasenv subst term context =
315 if !use_low_level_ppterm_in_context then
316 ppterm_in_context ~metasenv subst term context
318 !ppterm_in_context_ref ~metasenv subst term context
320 let ppcontext' ~metasenv ?(sep = "\n") subst context =
321 let separate s = if s = "" then "" else s ^ sep in
323 (fun context_entry (i,context) ->
324 match context_entry with
325 Some (n,Cic.Decl t) ->
326 sprintf "%s%s : %s" (separate i) (CicPp.ppname n)
327 (ppterm_in_context ~metasenv subst t context),
328 context_entry::context
329 | Some (n,Cic.Def (bo,ty)) ->
330 sprintf "%s%s : %s := %s" (separate i) (CicPp.ppname n)
331 (ppterm_in_context ~metasenv subst ty context)
332 (ppterm_in_context ~metasenv subst bo context),
333 context_entry::context
335 sprintf "%s_ :? _" (separate i), context_entry::context
338 let ppsubst_unfolded ~metasenv subst =
341 (fun (idx, (c, t,ty)) ->
342 let scontext,context = ppcontext' ~metasenv ~sep:"; " subst c in
343 sprintf "%s |- ?%d : %s := %s" scontext idx
344 (ppterm_in_context ~metasenv [] ty context)
345 (ppterm_in_context ~metasenv subst t context))
348 Printf.sprintf "?%d := %s" idx (CicPp.ppterm term))
352 let ppsubst ~metasenv subst =
355 (fun (idx, (c, t, ty)) ->
356 let scontext,context = ppcontext' ~metasenv ~sep:"; " [] c in
357 sprintf "%s |- ?%d : %s := %s" scontext idx (ppterm_in_context ~metasenv [] ty context)
358 (ppterm_in_context ~metasenv [] t context))
362 let ppcontext ~metasenv ?sep subst context =
363 fst (ppcontext' ~metasenv ?sep subst context)
365 let ppmetasenv ?(sep = "\n") subst metasenv =
369 let scontext,context = ppcontext' ~metasenv ~sep:"; " subst c in
370 sprintf "%s |- ?%d: %s" scontext i
371 (ppterm_in_context ~metasenv subst t context))
373 (fun (i, _, _) -> not (List.mem_assoc i subst))
376 let tempi_type_of_aux_subst = ref 0.0;;
377 let tempi_subst = ref 0.0;;
378 let tempi_type_of_aux = ref 0.0;;
381 (* the delift function takes in input a metavariable index, an ordered list of
382 * optional terms [t1,...,tn] and a term t, and substitutes every tk = Some
383 * (rel(nk)) with rel(k). Typically, the list of optional terms is the explicit
384 * substitution that is applied to a metavariable occurrence and the result of
385 * the delift function is a term the implicit variable can be substituted with
386 * to make the term [t] unifiable with the metavariable occurrence. In general,
387 * the problem is undecidable if we consider equivalence in place of alpha
388 * convertibility. Our implementation, though, is even weaker than alpha
389 * convertibility, since it replace the term [tk] if and only if [tk] is a Rel
390 * (missing all the other cases). Does this matter in practice?
391 * The metavariable index is the index of the metavariable that must not occur
392 * in the term (for occur check).
395 exception NotInTheList;;
400 [] -> raise NotInTheList
401 | (Some (Cic.Rel m))::_ when m=n -> k
402 | _::tl -> aux (k+1) tl in
408 let rec force_does_not_occur subst to_be_restricted t =
409 let module C = Cic in
410 let more_to_be_restricted = ref [] in
411 let rec aux k = function
412 C.Rel r when List.mem (r - k) to_be_restricted -> raise Occur
415 | C.Implicit _ -> assert false
417 (* we do not retrieve the term associated to ?n in subst since *)
418 (* in this way we can restrict if something goes wrong *)
430 more_to_be_restricted := (n,!i) :: !more_to_be_restricted;
435 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty)
436 | C.Prod (name,so,dest) -> C.Prod (name, aux k so, aux (k+1) dest)
437 | C.Lambda (name,so,dest) -> C.Lambda (name, aux k so, aux (k+1) dest)
438 | C.LetIn (name,so,ty,dest) ->
439 C.LetIn (name, aux k so, aux k ty, aux (k+1) dest)
440 | C.Appl l -> C.Appl (List.map (aux k) l)
441 | C.Var (uri,exp_named_subst) ->
442 let exp_named_subst' =
443 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
445 C.Var (uri, exp_named_subst')
446 | C.Const (uri, exp_named_subst) ->
447 let exp_named_subst' =
448 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
450 C.Const (uri, exp_named_subst')
451 | C.MutInd (uri,tyno,exp_named_subst) ->
452 let exp_named_subst' =
453 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
455 C.MutInd (uri, tyno, exp_named_subst')
456 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
457 let exp_named_subst' =
458 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
460 C.MutConstruct (uri, tyno, consno, exp_named_subst')
461 | C.MutCase (uri,tyno,out,te,pl) ->
462 C.MutCase (uri, tyno, aux k out, aux k te, List.map (aux k) pl)
464 let len = List.length fl in
465 let k_plus_len = k + len in
468 (fun (name,j,ty,bo) -> (name, j, aux k ty, aux k_plus_len bo)) fl
472 let len = List.length fl in
473 let k_plus_len = k + len in
476 (fun (name,ty,bo) -> (name, aux k ty, aux k_plus_len bo)) fl
481 (!more_to_be_restricted, res)
483 let rec restrict subst to_be_restricted metasenv =
484 match to_be_restricted with
485 | [] -> metasenv, subst
487 let names_of_context_indexes context indexes =
492 match List.nth context (i-1) with
493 | None -> assert false
494 | Some (n, _) -> CicPp.ppname n
496 Failure _ -> assert false
499 let force_does_not_occur_in_context to_be_restricted = function
501 | Some (name, Cic.Decl t) ->
502 let (more_to_be_restricted, t') =
503 force_does_not_occur subst to_be_restricted t
505 more_to_be_restricted, Some (name, Cic.Decl t')
506 | Some (name, Cic.Def (bo, ty)) ->
507 let (more_to_be_restricted, bo') =
508 force_does_not_occur subst to_be_restricted bo
510 let more_to_be_restricted, ty' =
511 let more_to_be_restricted', ty' =
512 force_does_not_occur subst to_be_restricted ty
514 more_to_be_restricted @ more_to_be_restricted',
517 more_to_be_restricted, Some (name, Cic.Def (bo', ty'))
519 let rec erase i to_be_restricted n = function
520 | [] -> [], to_be_restricted, []
522 let more_to_be_restricted,restricted,tl' =
523 erase (i+1) to_be_restricted n tl
525 let restrict_me = List.mem i restricted in
527 more_to_be_restricted, restricted, None:: tl'
530 let more_to_be_restricted', hd' =
531 let delifted_restricted =
535 | j::tl when j > i -> (j - i)::aux tl
540 force_does_not_occur_in_context delifted_restricted hd
542 more_to_be_restricted @ more_to_be_restricted',
543 restricted, hd' :: tl'
545 more_to_be_restricted, (i :: restricted), None :: tl')
547 let (more_to_be_restricted, metasenv) = (* restrict metasenv *)
549 (fun (n, context, t) (more, metasenv) ->
550 let to_be_restricted =
551 List.map snd (List.filter (fun (m, _) -> m = n) to_be_restricted)
553 let (more_to_be_restricted, restricted, context') =
554 (* just an optimization *)
555 if to_be_restricted = [] then
558 erase 1 to_be_restricted n context
561 let more_to_be_restricted', t' =
562 force_does_not_occur subst restricted t
564 let metasenv' = (n, context', t') :: metasenv in
565 (more @ more_to_be_restricted @ more_to_be_restricted',
568 raise (MetaSubstFailure (lazy (sprintf
569 "Cannot restrict the context of the metavariable ?%d over the hypotheses %s since metavariable's type depends on at least one of them"
570 n (names_of_context_indexes context to_be_restricted)))))
573 let (more_to_be_restricted', subst) = (* restrict subst *)
575 (* TODO: cambiare dopo l'aggiunta del ty *)
576 (fun (n, (context, term,ty)) (more, subst') ->
577 let to_be_restricted =
578 List.map snd (List.filter (fun (m, _) -> m = n) to_be_restricted)
581 let (more_to_be_restricted, restricted, context') =
582 (* just an optimization *)
583 if to_be_restricted = [] then
586 erase 1 to_be_restricted n context
588 let more_to_be_restricted', term' =
589 force_does_not_occur subst restricted term
591 let more_to_be_restricted'', ty' =
592 force_does_not_occur subst restricted ty in
593 let subst' = (n, (context', term',ty')) :: subst' in
595 more @ more_to_be_restricted
596 @ more_to_be_restricted'@more_to_be_restricted'' in
599 let error_msg = lazy (sprintf
600 "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"
601 n (names_of_context_indexes context to_be_restricted) n
602 (ppterm ~metasenv subst term))
605 debug_print (lazy error_msg);
606 debug_print (lazy ("metasenv = \n" ^ (ppmetasenv metasenv subst)));
607 debug_print (lazy ("subst = \n" ^ (ppsubst subst)));
608 debug_print (lazy ("context = \n" ^ (ppcontext subst context))); *)
609 raise (MetaSubstFailure error_msg)))
612 restrict subst (more_to_be_restricted @ more_to_be_restricted') metasenv
615 (*CSC: maybe we should rename delift in abstract, as I did in my dissertation *)(*Andrea: maybe not*)
617 let delift n subst context metasenv l t =
618 (* INVARIANT: we suppose that t is not another occurrence of Meta(n,_),
619 otherwise the occur check does not make sense *)
622 debug_print (lazy ("sto deliftando il termine " ^ (CicPp.ppterm t) ^ " rispetto
623 al contesto locale " ^ (CicPp.ppterm (Cic.Meta(0,l)))));
626 let module S = CicSubstitution in
628 let (_, canonical_context, _) =
630 CicUtil.lookup_meta n metasenv
631 with CicUtil.Meta_not_found _ ->
632 raise (MetaSubstFailure (lazy
633 ("delifting error: the metavariable " ^ string_of_int n ^ " is not " ^
634 "declared in the metasenv")))
636 List.map2 (fun ct lt ->
642 let to_be_restricted = ref [] in
643 let rec deliftaux k =
644 let module C = Cic in
651 match List.nth context (m-k-1) with
652 Some (_,C.Def (t,_)) ->
654 C.Rel ((position (m-k) l) + k)
657 (*CSC: Hmmm. This bit of reduction is not in the spirit of *)
658 (*CSC: first order unification. Does it help or does it harm? *)
659 (*CSC: ANSWER: it hurts performances since it is possible to *)
660 (*CSC: have an exponential explosion of the size of the proof.*)
661 (*CSC: However, without this bit of reduction some "apply" in *)
662 (*CSC: the library fail (e.g. nat/nth_prime.ma). *)
663 deliftaux k (S.lift m t))
664 | Some (_,C.Decl t) ->
665 C.Rel ((position (m-k) l) + k)
666 | None -> raise (MetaSubstFailure (lazy "RelToHiddenHypothesis"))
669 raise (MetaSubstFailure (lazy "Unbound variable found in deliftaux"))
671 | C.Var (uri,exp_named_subst) ->
672 let exp_named_subst' =
673 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
675 C.Var (uri,exp_named_subst')
676 | C.Meta (i, l1) as t ->
678 let (_,t,_) = CicUtil.lookup_subst i subst in
679 deliftaux k (CicSubstitution.subst_meta l1 t)
680 with CicUtil.Subst_not_found _ ->
681 (* see the top level invariant *)
683 raise (MetaSubstFailure (lazy (sprintf
684 "Cannot unify the metavariable ?%d with a term that has as subterm %s in which the same metavariable occurs (occur check)"
685 i (ppterm ~metasenv subst t))))
688 (* I do not consider the term associated to ?i in subst since *)
689 (* in this way I can restrict if something goes wrong. *)
693 | None::tl -> None::(deliftl (j+1) tl)
695 let l1' = (deliftl (j+1) tl) in
697 Some (deliftaux k t)::l1'
700 | MetaSubstFailure _ ->
702 (i,j)::!to_be_restricted ; None::l1'
704 let l' = deliftl 1 l1 in
708 | C.Implicit _ as t -> t
709 | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
710 | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
711 | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
712 | C.LetIn (n,s,ty,t) ->
713 C.LetIn (n, deliftaux k s, deliftaux k ty, deliftaux (k+1) t)
714 | C.Appl l -> C.Appl (List.map (deliftaux k) l)
715 | C.Const (uri,exp_named_subst) ->
716 let exp_named_subst' =
717 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
719 C.Const (uri,exp_named_subst')
720 | C.MutInd (uri,typeno,exp_named_subst) ->
721 let exp_named_subst' =
722 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
724 C.MutInd (uri,typeno,exp_named_subst')
725 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
726 let exp_named_subst' =
727 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
729 C.MutConstruct (uri,typeno,consno,exp_named_subst')
730 | C.MutCase (sp,i,outty,t,pl) ->
731 C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
732 List.map (deliftaux k) pl)
734 let len = List.length fl in
737 (fun (name, i, ty, bo) ->
738 (name, i, deliftaux k ty, deliftaux (k+len) bo))
743 let len = List.length fl in
746 (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
749 C.CoFix (i, liftedfl)
756 (* This is the case where we fail even first order unification. *)
757 (* The reason is that our delift function is weaker than first *)
758 (* order (in the sense of alpha-conversion). See comment above *)
759 (* related to the delift function. *)
760 (* debug_print (lazy "First Order UnificationFailure during delift") ;
761 debug_print(lazy (sprintf
762 "Error trying to abstract %s over [%s]: the algorithm only tried to abstract over bound variables"
766 (function Some t -> ppterm subst t | None -> "_") l
768 let msg = (lazy (sprintf
769 "Error trying to abstract %s over [%s]: the algorithm only tried to abstract over bound variables"
770 (ppterm ~metasenv subst t)
773 (function Some t -> ppterm ~metasenv subst t | None -> "_")
779 Some t -> CicUtil.is_meta_closed (apply_subst subst t)
782 raise (Uncertain msg)
784 raise (MetaSubstFailure msg)
786 let (metasenv, subst) = restrict subst !to_be_restricted metasenv in
790 (* delifts a term t of n levels strating from k, that is changes (Rel m)
791 * to (Rel (m - n)) when m > (k + n). if k <= m < k + n delift fails
793 let delift_rels_from subst metasenv k n =
794 let rec liftaux subst metasenv k =
795 let module C = Cic in
800 else if m < k + n then
801 raise DeliftingARelWouldCaptureAFreeVariable
803 C.Rel (m - n), subst, metasenv
804 | C.Var (uri,exp_named_subst) ->
805 let exp_named_subst',subst,metasenv =
807 (fun (uri,t) (l,subst,metasenv) ->
808 let t',subst,metasenv = liftaux subst metasenv k t in
809 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
811 C.Var (uri,exp_named_subst'),subst,metasenv
814 let (_, t,_) = lookup_subst i subst in
815 liftaux subst metasenv k (CicSubstitution.subst_meta l t)
816 with CicUtil.Subst_not_found _ ->
817 let l',to_be_restricted,subst,metasenv =
818 let rec aux con l subst metasenv =
820 [] -> [],[],subst,metasenv
822 let tl',to_be_restricted,subst,metasenv =
823 aux (con + 1) tl subst metasenv in
824 let he',more_to_be_restricted,subst,metasenv =
826 None -> None,[],subst,metasenv
829 let t',subst,metasenv = liftaux subst metasenv k t in
830 Some t',[],subst,metasenv
832 DeliftingARelWouldCaptureAFreeVariable ->
833 None,[i,con],subst,metasenv
835 he'::tl',more_to_be_restricted@to_be_restricted,subst,metasenv
837 aux 1 l subst metasenv in
838 let metasenv,subst = restrict subst to_be_restricted metasenv in
839 C.Meta(i,l'),subst,metasenv)
840 | C.Sort _ as t -> t,subst,metasenv
841 | C.Implicit _ as t -> t,subst,metasenv
843 let te',subst,metasenv = liftaux subst metasenv k te in
844 let ty',subst,metasenv = liftaux subst metasenv k ty in
845 C.Cast (te',ty'),subst,metasenv
847 let s',subst,metasenv = liftaux subst metasenv k s in
848 let t',subst,metasenv = liftaux subst metasenv (k+1) t in
849 C.Prod (n,s',t'),subst,metasenv
850 | C.Lambda (n,s,t) ->
851 let s',subst,metasenv = liftaux subst metasenv k s in
852 let t',subst,metasenv = liftaux subst metasenv (k+1) t in
853 C.Lambda (n,s',t'),subst,metasenv
854 | C.LetIn (n,s,ty,t) ->
855 let s',subst,metasenv = liftaux subst metasenv k s in
856 let ty',subst,metasenv = liftaux subst metasenv k ty in
857 let t',subst,metasenv = liftaux subst metasenv (k+1) t in
858 C.LetIn (n,s',ty',t'),subst,metasenv
860 let l',subst,metasenv =
862 (fun t (l,subst,metasenv) ->
863 let t',subst,metasenv = liftaux subst metasenv k t in
864 t'::l,subst,metasenv) l ([],subst,metasenv) in
865 C.Appl l',subst,metasenv
866 | C.Const (uri,exp_named_subst) ->
867 let exp_named_subst',subst,metasenv =
869 (fun (uri,t) (l,subst,metasenv) ->
870 let t',subst,metasenv = liftaux subst metasenv k t in
871 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
873 C.Const (uri,exp_named_subst'),subst,metasenv
874 | C.MutInd (uri,tyno,exp_named_subst) ->
875 let exp_named_subst',subst,metasenv =
877 (fun (uri,t) (l,subst,metasenv) ->
878 let t',subst,metasenv = liftaux subst metasenv k t in
879 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
881 C.MutInd (uri,tyno,exp_named_subst'),subst,metasenv
882 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
883 let exp_named_subst',subst,metasenv =
885 (fun (uri,t) (l,subst,metasenv) ->
886 let t',subst,metasenv = liftaux subst metasenv k t in
887 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
889 C.MutConstruct (uri,tyno,consno,exp_named_subst'),subst,metasenv
890 | C.MutCase (sp,i,outty,t,pl) ->
891 let outty',subst,metasenv = liftaux subst metasenv k outty in
892 let t',subst,metasenv = liftaux subst metasenv k t in
893 let pl',subst,metasenv =
895 (fun t (l,subst,metasenv) ->
896 let t',subst,metasenv = liftaux subst metasenv k t in
897 t'::l,subst,metasenv) pl ([],subst,metasenv)
899 C.MutCase (sp,i,outty',t',pl'),subst,metasenv
901 let len = List.length fl in
902 let liftedfl,subst,metasenv =
904 (fun (name, i, ty, bo) (l,subst,metasenv) ->
905 let ty',subst,metasenv = liftaux subst metasenv k ty in
906 let bo',subst,metasenv = liftaux subst metasenv (k+len) bo in
907 (name,i,ty',bo')::l,subst,metasenv
908 ) fl ([],subst,metasenv)
910 C.Fix (i, liftedfl),subst,metasenv
912 let len = List.length fl in
913 let liftedfl,subst,metasenv =
915 (fun (name, ty, bo) (l,subst,metasenv) ->
916 let ty',subst,metasenv = liftaux subst metasenv k ty in
917 let bo',subst,metasenv = liftaux subst metasenv (k+len) bo in
918 (name,ty',bo')::l,subst,metasenv
919 ) fl ([],subst,metasenv)
921 C.CoFix (i, liftedfl),subst,metasenv
923 liftaux subst metasenv k
925 let delift_rels subst metasenv n t =
926 delift_rels_from subst metasenv 1 n t
929 (**** END OF DELIFT ****)
932 (** {2 Format-like pretty printers} *)
935 Format.pp_print_string ppf s;
936 Format.pp_print_newline ppf ();
937 Format.pp_print_flush ppf ()
939 let fppsubst ppf subst = fpp_gen ppf (ppsubst ~metasenv:[] subst)
940 let fppterm ppf term = fpp_gen ppf (CicPp.ppterm term)
941 let fppmetasenv ppf metasenv = fpp_gen ppf (ppmetasenv [] metasenv)