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/.
30 let deref_counter = ref 0
31 let apply_subst_context_counter = ref 0
32 let apply_subst_metasenv_counter = ref 0
33 let lift_counter = ref 0
34 let subst_counter = ref 0
35 let whd_counter = ref 0
36 let are_convertible_counter = ref 0
37 let metasenv_length = ref 0
38 let context_length = ref 0
39 let reset_counters () =
40 apply_subst_counter := 0;
41 apply_subst_context_counter := 0;
42 apply_subst_metasenv_counter := 0;
46 are_convertible_counter := 0;
49 let print_counters () =
50 prerr_endline (Printf.sprintf
52 apply_subst_context: %d
53 apply_subst_metasenv: %d
58 metasenv length: %d (avg = %.2f)
59 context length: %d (avg = %.2f)
61 !apply_subst_counter !apply_subst_context_counter
62 !apply_subst_metasenv_counter !lift_counter !subst_counter !whd_counter
63 !are_convertible_counter !metasenv_length
64 ((float !metasenv_length) /. (float !apply_subst_metasenv_counter))
66 ((float !context_length) /. (float !apply_subst_context_counter))
71 exception MetaSubstFailure of string
72 exception Uncertain of string
73 exception AssertFailure of string
75 let debug_print = prerr_endline
77 type substitution = (int * (Cic.context * Cic.term)) list
81 let third _,_,a = a in
86 (CicSubstitution.subst_meta
87 l (third (CicUtil.lookup_subst n subst)))
89 CicUtil.Subst_not_found _ -> t)
94 let lookup_subst = CicUtil.lookup_subst
98 (* clean_up_meta take a metasenv and a term and make every local context
99 of each occurrence of a metavariable consistent with its canonical context,
100 with respect to the hidden hipothesis *)
103 let clean_up_meta subst metasenv t =
104 let module C = Cic in
109 | C.Implicit _ -> assert false
110 | C.Meta (n,l) as t ->
113 let (cc,_) = lookup_subst n subst in cc
114 with CicUtil.Subst_not_found _ ->
116 let (_,cc,_) = CicUtil.lookup_meta n metasenv in cc
117 with CicUtil.Meta_not_found _ -> assert false) in
126 Invalid_argument _ -> assert false) in
128 | C.Cast (te,ty) -> C.Cast (aux te, aux ty)
129 | C.Prod (name,so,dest) -> C.Prod (name, aux so, aux dest)
130 | C.Lambda (name,so,dest) -> C.Lambda (name, aux so, aux dest)
131 | C.LetIn (name,so,dest) -> C.LetIn (name, aux so, aux dest)
132 | C.Appl l -> C.Appl (List.map aux l)
133 | C.Var (uri,exp_named_subst) ->
134 let exp_named_subst' =
135 List.map (fun (uri,t) -> (uri, aux t)) exp_named_subst
137 C.Var (uri, exp_named_subst')
138 | C.Const (uri, exp_named_subst) ->
139 let exp_named_subst' =
140 List.map (fun (uri,t) -> (uri, aux t)) exp_named_subst
142 C.Const (uri, exp_named_subst')
143 | C.MutInd (uri,tyno,exp_named_subst) ->
144 let exp_named_subst' =
145 List.map (fun (uri,t) -> (uri, aux t)) exp_named_subst
147 C.MutInd (uri, tyno, exp_named_subst')
148 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
149 let exp_named_subst' =
150 List.map (fun (uri,t) -> (uri, aux t)) exp_named_subst
152 C.MutConstruct (uri, tyno, consno, exp_named_subst')
153 | C.MutCase (uri,tyno,out,te,pl) ->
154 C.MutCase (uri, tyno, aux out, aux te, List.map aux pl)
158 (fun (name,j,ty,bo) -> (name, j, aux ty, aux bo)) fl
164 (fun (name,ty,bo) -> (name, aux ty, aux bo)) fl
170 (*** Functions to apply a substitution ***)
172 let apply_subst_gen ~appl_fun subst term =
174 let module C = Cic in
175 let module S = CicSubstitution in
178 | C.Var (uri,exp_named_subst) ->
179 let exp_named_subst' =
180 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
182 C.Var (uri, exp_named_subst')
185 let (_, t,_) = lookup_subst i subst in
186 um_aux (S.subst_meta l t)
187 with CicUtil.Subst_not_found _ ->
188 (* unconstrained variable, i.e. free in subst*)
190 List.map (function None -> None | Some t -> Some (um_aux t)) l
194 | C.Implicit _ -> assert false
195 | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
196 | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
197 | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
198 | C.LetIn (n,s,t) -> C.LetIn (n, um_aux s, um_aux t)
199 | C.Appl (hd :: tl) -> appl_fun um_aux hd tl
200 | C.Appl _ -> assert false
201 | C.Const (uri,exp_named_subst) ->
202 let exp_named_subst' =
203 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
205 C.Const (uri, exp_named_subst')
206 | C.MutInd (uri,typeno,exp_named_subst) ->
207 let exp_named_subst' =
208 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
210 C.MutInd (uri,typeno,exp_named_subst')
211 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
212 let exp_named_subst' =
213 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
215 C.MutConstruct (uri,typeno,consno,exp_named_subst')
216 | C.MutCase (sp,i,outty,t,pl) ->
217 let pl' = List.map um_aux pl in
218 C.MutCase (sp, i, um_aux outty, um_aux t, pl')
221 List.map (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo)) fl
226 List.map (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo)) fl
234 (* CSC: old code that never performs beta reduction
235 let appl_fun um_aux he tl =
236 let tl' = List.map um_aux tl in
239 Cic.Appl l -> Cic.Appl (l@tl')
240 | he' -> Cic.Appl (he'::tl')
243 apply_subst_gen ~appl_fun
245 let appl_fun um_aux he tl =
246 let tl' = List.map um_aux tl in
249 Cic.Appl l -> Cic.Appl (l@tl')
250 | he' -> Cic.Appl (he'::tl')
255 let rec beta_reduce =
257 (Cic.Appl (Cic.Lambda (_,_,t)::he'::tl')) ->
258 let he'' = CicSubstitution.subst he' t in
262 beta_reduce (Cic.Appl(he''::tl'))
270 (* incr apply_subst_counter; *)
271 apply_subst_gen ~appl_fun s t
274 let rec apply_subst_context subst context =
276 incr apply_subst_context_counter;
277 context_length := !context_length + List.length context;
282 | Some (n, Cic.Decl t) ->
283 let t' = apply_subst subst t in
284 Some (n, Cic.Decl t') :: context
285 | Some (n, Cic.Def (t, ty)) ->
289 | Some ty -> Some (apply_subst subst ty)
291 let t' = apply_subst subst t in
292 Some (n, Cic.Def (t', ty')) :: context
293 | None -> None :: context)
296 let apply_subst_metasenv subst metasenv =
298 incr apply_subst_metasenv_counter;
299 metasenv_length := !metasenv_length + List.length metasenv;
302 (fun (n, context, ty) ->
303 (n, apply_subst_context subst context, apply_subst subst ty))
305 (fun (i, _, _) -> not (List.mem_assoc i subst))
308 (***** Pretty printing functions ******)
310 let ppterm subst term = CicPp.ppterm (apply_subst subst term)
312 let ppterm_in_context subst term name_context =
313 CicPp.pp (apply_subst subst term) name_context
315 let ppcontext' ?(sep = "\n") subst context =
316 let separate s = if s = "" then "" else s ^ sep in
318 (fun context_entry (i,name_context) ->
319 match context_entry with
320 Some (n,Cic.Decl t) ->
321 sprintf "%s%s : %s" (separate i) (CicPp.ppname n)
322 (ppterm_in_context subst t name_context), (Some n)::name_context
323 | Some (n,Cic.Def (bo,ty)) ->
324 sprintf "%s%s : %s := %s" (separate i) (CicPp.ppname n)
327 | Some ty -> ppterm_in_context subst ty name_context)
328 (ppterm_in_context subst bo name_context), (Some n)::name_context
330 sprintf "%s_ :? _" (separate i), None::name_context
333 let ppsubst_unfolded subst =
336 (fun (idx, (c, t,_)) ->
337 let context,name_context = ppcontext' ~sep:"; " subst c in
338 sprintf "%s |- ?%d:= %s" context idx
339 (ppterm_in_context subst t name_context))
342 Printf.sprintf "?%d := %s" idx (CicPp.ppterm term))
349 (fun (idx, (c, t, _)) ->
350 let context,name_context = ppcontext' ~sep:"; " [] c in
351 sprintf "%s |- ?%d:= %s" context idx
352 (ppterm_in_context [] t name_context))
356 let ppcontext ?sep subst context = fst (ppcontext' ?sep subst context)
358 let ppmetasenv ?(sep = "\n") metasenv subst =
362 let context,name_context = ppcontext' ~sep:"; " subst c in
363 sprintf "%s |- ?%d: %s" context i
364 (ppterm_in_context subst t name_context))
366 (fun (i, _, _) -> not (List.mem_assoc i subst))
369 let tempi_type_of_aux_subst = ref 0.0;;
370 let tempi_subst = ref 0.0;;
371 let tempi_type_of_aux = ref 0.0;;
374 (* the delift function takes in input a metavariable index, an ordered list of
375 * optional terms [t1,...,tn] and a term t, and substitutes every tk = Some
376 * (rel(nk)) with rel(k). Typically, the list of optional terms is the explicit
377 * substitution that is applied to a metavariable occurrence and the result of
378 * the delift function is a term the implicit variable can be substituted with
379 * to make the term [t] unifiable with the metavariable occurrence. In general,
380 * the problem is undecidable if we consider equivalence in place of alpha
381 * convertibility. Our implementation, though, is even weaker than alpha
382 * convertibility, since it replace the term [tk] if and only if [tk] is a Rel
383 * (missing all the other cases). Does this matter in practice?
384 * The metavariable index is the index of the metavariable that must not occur
385 * in the term (for occur check).
388 exception NotInTheList;;
393 [] -> raise NotInTheList
394 | (Some (Cic.Rel m))::_ when m=n -> k
395 | _::tl -> aux (k+1) tl in
401 let rec force_does_not_occur subst to_be_restricted t =
402 let module C = Cic in
403 let more_to_be_restricted = ref [] in
404 let rec aux k = function
405 C.Rel r when List.mem (r - k) to_be_restricted -> raise Occur
408 | C.Implicit _ -> assert false
410 (* we do not retrieve the term associated to ?n in subst since *)
411 (* in this way we can restrict if something goes wrong *)
423 more_to_be_restricted := (n,!i) :: !more_to_be_restricted;
428 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty)
429 | C.Prod (name,so,dest) -> C.Prod (name, aux k so, aux (k+1) dest)
430 | C.Lambda (name,so,dest) -> C.Lambda (name, aux k so, aux (k+1) dest)
431 | C.LetIn (name,so,dest) -> C.LetIn (name, aux k so, aux (k+1) dest)
432 | C.Appl l -> C.Appl (List.map (aux k) l)
433 | C.Var (uri,exp_named_subst) ->
434 let exp_named_subst' =
435 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
437 C.Var (uri, exp_named_subst')
438 | C.Const (uri, exp_named_subst) ->
439 let exp_named_subst' =
440 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
442 C.Const (uri, exp_named_subst')
443 | C.MutInd (uri,tyno,exp_named_subst) ->
444 let exp_named_subst' =
445 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
447 C.MutInd (uri, tyno, exp_named_subst')
448 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
449 let exp_named_subst' =
450 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
452 C.MutConstruct (uri, tyno, consno, exp_named_subst')
453 | C.MutCase (uri,tyno,out,te,pl) ->
454 C.MutCase (uri, tyno, aux k out, aux k te, List.map (aux k) pl)
456 let len = List.length fl in
457 let k_plus_len = k + len in
460 (fun (name,j,ty,bo) -> (name, j, aux k ty, aux k_plus_len bo)) fl
464 let len = List.length fl in
465 let k_plus_len = k + len in
468 (fun (name,ty,bo) -> (name, aux k ty, aux k_plus_len bo)) fl
473 (!more_to_be_restricted, res)
475 let rec restrict subst to_be_restricted metasenv =
476 let names_of_context_indexes context indexes =
481 match List.nth context (i-1) with
482 | None -> assert false
483 | Some (n, _) -> CicPp.ppname n
485 Failure _ -> assert false
488 let force_does_not_occur_in_context to_be_restricted = function
490 | Some (name, Cic.Decl t) ->
491 let (more_to_be_restricted, t') =
492 force_does_not_occur subst to_be_restricted t
494 more_to_be_restricted, Some (name, Cic.Decl t')
495 | Some (name, Cic.Def (bo, ty)) ->
496 let (more_to_be_restricted, bo') =
497 force_does_not_occur subst to_be_restricted bo
499 let more_to_be_restricted, ty' =
501 | None -> more_to_be_restricted, None
503 let more_to_be_restricted', ty' =
504 force_does_not_occur subst to_be_restricted ty
506 more_to_be_restricted @ more_to_be_restricted',
509 more_to_be_restricted, Some (name, Cic.Def (bo', ty'))
511 let rec erase i to_be_restricted n = function
512 | [] -> [], to_be_restricted, []
514 let more_to_be_restricted,restricted,tl' =
515 erase (i+1) to_be_restricted n tl
517 let restrict_me = List.mem i restricted in
519 more_to_be_restricted, restricted, None:: tl'
522 let more_to_be_restricted', hd' =
523 let delifted_restricted =
527 | j::tl when j > i -> (j - i)::aux tl
532 force_does_not_occur_in_context delifted_restricted hd
534 more_to_be_restricted @ more_to_be_restricted',
535 restricted, hd' :: tl'
537 more_to_be_restricted, (i :: restricted), None :: tl')
539 let (more_to_be_restricted, metasenv) = (* restrict metasenv *)
541 (fun (n, context, t) (more, metasenv) ->
542 let to_be_restricted =
543 List.map snd (List.filter (fun (m, _) -> m = n) to_be_restricted)
545 let (more_to_be_restricted, restricted, context') =
546 (* just an optimization *)
547 if to_be_restricted = [] then
550 erase 1 to_be_restricted n context
553 let more_to_be_restricted', t' =
554 force_does_not_occur subst restricted t
556 let metasenv' = (n, context', t') :: metasenv in
557 (more @ more_to_be_restricted @ more_to_be_restricted',
560 raise (MetaSubstFailure (sprintf
561 "Cannot restrict the context of the metavariable ?%d over the hypotheses %s since metavariable's type depends on at least one of them"
562 n (names_of_context_indexes context to_be_restricted))))
565 let (more_to_be_restricted', subst) = (* restrict subst *)
567 (* TODO: cambiare dopo l'aggiunta del ty *)
568 (fun (n, (context, term,ty)) (more, subst') ->
569 let to_be_restricted =
570 List.map snd (List.filter (fun (m, _) -> m = n) to_be_restricted)
573 let (more_to_be_restricted, restricted, context') =
574 (* just an optimization *)
575 if to_be_restricted = [] then
578 erase 1 to_be_restricted n context
580 let more_to_be_restricted', term' =
581 force_does_not_occur subst restricted term
583 let more_to_be_restricted'', ty' =
584 force_does_not_occur subst restricted ty in
585 let subst' = (n, (context', term',ty')) :: subst' in
587 more @ more_to_be_restricted
588 @ more_to_be_restricted'@more_to_be_restricted'' in
591 let error_msg = sprintf
592 "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"
593 n (names_of_context_indexes context to_be_restricted) n
597 prerr_endline error_msg;
598 prerr_endline ("metasenv = \n" ^ (ppmetasenv metasenv subst));
599 prerr_endline ("subst = \n" ^ (ppsubst subst));
600 prerr_endline ("context = \n" ^ (ppcontext subst context)); *)
601 raise (MetaSubstFailure error_msg)))
604 match more_to_be_restricted @ more_to_be_restricted' with
605 | [] -> (metasenv, subst)
606 | l -> restrict subst l metasenv
609 (*CSC: maybe we should rename delift in abstract, as I did in my dissertation *)(*Andrea: maybe not*)
611 let delift n subst context metasenv l t =
612 (* INVARIANT: we suppose that t is not another occurrence of Meta(n,_),
613 otherwise the occur check does not make sense *)
616 prerr_endline ("sto deliftando il termine " ^ (CicPp.ppterm t) ^ " rispetto
617 al contesto locale " ^ (CicPp.ppterm (Cic.Meta(0,l))));
620 let module S = CicSubstitution in
622 let (_, canonical_context, _) = CicUtil.lookup_meta n metasenv in
623 List.map2 (fun ct lt ->
629 let to_be_restricted = ref [] in
630 let rec deliftaux k =
631 let module C = Cic in
635 C.Rel m (*CSC: che succede se c'e' un Def? Dovrebbe averlo gia' *)
636 (*CSC: deliftato la regola per il LetIn *)
637 (*CSC: FALSO! La regola per il LetIn non lo fa *)
640 match List.nth context (m-k-1) with
641 Some (_,C.Def (t,_)) ->
642 (*CSC: Hmmm. This bit of reduction is not in the spirit of *)
643 (*CSC: first order unification. Does it help or does it harm? *)
644 deliftaux k (S.lift m t)
645 | Some (_,C.Decl t) ->
646 C.Rel ((position (m-k) l) + k)
647 | None -> raise (MetaSubstFailure "RelToHiddenHypothesis")
650 raise (MetaSubstFailure "Unbound variable found in deliftaux")
652 | C.Var (uri,exp_named_subst) ->
653 let exp_named_subst' =
654 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
656 C.Var (uri,exp_named_subst')
657 | C.Meta (i, l1) as t ->
659 let (_,t,_) = CicUtil.lookup_subst i subst in
660 deliftaux k (CicSubstitution.subst_meta l1 t)
661 with CicUtil.Subst_not_found _ ->
662 (* see the top level invariant *)
664 raise (MetaSubstFailure (sprintf
665 "Cannot unify the metavariable ?%d with a term that has as subterm %s in which the same metavariable occurs (occur check)"
669 (* I do not consider the term associated to ?i in subst since *)
670 (* in this way I can restrict if something goes wrong. *)
674 | None::tl -> None::(deliftl (j+1) tl)
676 let l1' = (deliftl (j+1) tl) in
678 Some (deliftaux k t)::l1'
681 | MetaSubstFailure _ ->
683 (i,j)::!to_be_restricted ; None::l1'
685 let l' = deliftl 1 l1 in
689 | C.Implicit _ as t -> t
690 | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
691 | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
692 | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
693 | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
694 | C.Appl l -> C.Appl (List.map (deliftaux k) l)
695 | C.Const (uri,exp_named_subst) ->
696 let exp_named_subst' =
697 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
699 C.Const (uri,exp_named_subst')
700 | C.MutInd (uri,typeno,exp_named_subst) ->
701 let exp_named_subst' =
702 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
704 C.MutInd (uri,typeno,exp_named_subst')
705 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
706 let exp_named_subst' =
707 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
709 C.MutConstruct (uri,typeno,consno,exp_named_subst')
710 | C.MutCase (sp,i,outty,t,pl) ->
711 C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
712 List.map (deliftaux k) pl)
714 let len = List.length fl in
717 (fun (name, i, ty, bo) ->
718 (name, i, deliftaux k ty, deliftaux (k+len) bo))
723 let len = List.length fl in
726 (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
729 C.CoFix (i, liftedfl)
736 (* This is the case where we fail even first order unification. *)
737 (* The reason is that our delift function is weaker than first *)
738 (* order (in the sense of alpha-conversion). See comment above *)
739 (* related to the delift function. *)
740 (* debug_print "First Order UnificationFailure during delift" ;
741 prerr_endline(sprintf
742 "Error trying to abstract %s over [%s]: the algorithm only tried to abstract over bound variables"
746 (function Some t -> ppterm subst t | None -> "_") l
748 raise (Uncertain (sprintf
749 "Error trying to abstract %s over [%s]: the algorithm only tried to abstract over bound variables"
753 (function Some t -> ppterm subst t | None -> "_")
756 let (metasenv, subst) = restrict subst !to_be_restricted metasenv in
760 (**** END OF DELIFT ****)
763 (** {2 Format-like pretty printers} *)
766 Format.pp_print_string ppf s;
767 Format.pp_print_newline ppf ();
768 Format.pp_print_flush ppf ()
770 let fppsubst ppf subst = fpp_gen ppf (ppsubst subst)
771 let fppterm ppf term = fpp_gen ppf (CicPp.ppterm term)
772 let fppmetasenv ppf metasenv = fpp_gen ppf (ppmetasenv metasenv [])