1 (* Copyright (C) 2004, 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/.
28 exception MetaSubstFailure of string
29 exception Uncertain of string
30 exception AssertFailure of string
32 let debug_print = prerr_endline
34 type substitution = (int * Cic.term) list
36 (*** Functions to apply a substitution ***)
38 let apply_subst_gen ~appl_fun subst term =
41 let module S = CicSubstitution in
47 let t = List.assoc i subst in
48 um_aux (S.lift_meta l t)
49 with Not_found -> (* not constrained variable, i.e. free in subst*)
51 List.map (function None -> None | Some t -> Some (um_aux t)) l
55 | C.Implicit _ -> assert false
56 | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
57 | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
58 | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
59 | C.LetIn (n,s,t) -> C.LetIn (n, um_aux s, um_aux t)
60 | C.Appl (hd :: tl) -> appl_fun um_aux hd tl
61 | C.Appl _ -> assert false
62 | C.Const (uri,exp_named_subst) ->
63 let exp_named_subst' =
64 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
66 C.Const (uri, exp_named_subst')
67 | C.MutInd (uri,typeno,exp_named_subst) ->
68 let exp_named_subst' =
69 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
71 C.MutInd (uri,typeno,exp_named_subst')
72 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
73 let exp_named_subst' =
74 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
76 C.MutConstruct (uri,typeno,consno,exp_named_subst')
77 | C.MutCase (sp,i,outty,t,pl) ->
78 let pl' = List.map um_aux pl in
79 C.MutCase (sp, i, um_aux outty, um_aux t, pl')
82 List.map (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo)) fl
87 List.map (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo)) fl
95 let appl_fun um_aux he tl =
96 let tl' = List.map um_aux tl in
99 Cic.Appl l -> Cic.Appl (l@tl')
100 | he' -> Cic.Appl (he'::tl')
103 apply_subst_gen ~appl_fun
106 (* apply_subst_reducing subst (Some (mtr,reductions_no)) t *)
107 (* performs as (apply_subst subst t) until it finds an application of *)
108 (* (META [meta_to_reduce]) that, once unwinding is performed, creates *)
109 (* a new beta-redex; in this case up to [reductions_no] consecutive *)
110 (* beta-reductions are performed. *)
111 (* Hint: this function is usually called when [reductions_no] *)
112 (* eta-expansions have been performed and the head of the new *)
113 (* application has been unified with (META [meta_to_reduce]): *)
114 (* during the unwinding the eta-expansions are undone. *)
116 let apply_subst_reducing meta_to_reduce =
117 let appl_fun um_aux he tl =
118 let tl' = List.map um_aux tl in
121 Cic.Appl l -> Cic.Appl (l@tl')
122 | he' -> Cic.Appl (he'::tl')
125 match meta_to_reduce, he with
126 Some (mtr,reductions_no), Cic.Meta (m,_) when m = mtr ->
127 let rec beta_reduce =
129 (n,(Cic.Appl (Cic.Lambda (_,_,t)::he'::tl'))) when n > 0 ->
130 let he'' = CicSubstitution.subst he' t in
134 beta_reduce (n-1,Cic.Appl(he''::tl'))
137 beta_reduce (reductions_no,t')
141 apply_subst_gen ~appl_fun
143 let rec apply_subst_context subst context =
147 | Some (n, Cic.Decl t) ->
148 let t' = apply_subst subst t in
149 Some (n, Cic.Decl t') :: context
150 | Some (n, Cic.Def (t, ty)) ->
154 | Some ty -> Some (apply_subst subst ty)
156 let t' = apply_subst subst t in
157 Some (n, Cic.Def (t', ty')) :: context
158 | None -> None :: context)
161 let apply_subst_metasenv subst metasenv =
163 (fun (n, context, ty) ->
164 (n, apply_subst_context subst context, apply_subst subst ty))
166 (fun (i, _, _) -> not (List.exists (fun (j, _) -> (j = i)) subst))
169 (***** Pretty printing functions ******)
174 (fun (idx, term) -> Printf.sprintf "?%d := %s" idx (CicPp.ppterm term))
178 let ppterm subst term = CicPp.ppterm (apply_subst subst term)
180 let ppterm_in_context subst term name_context =
181 CicPp.pp (apply_subst subst term) name_context
183 let ppcontext' ?(sep = "\n") subst context =
184 let separate s = if s = "" then "" else s ^ sep in
186 (fun context_entry (i,name_context) ->
187 match context_entry with
188 Some (n,Cic.Decl t) ->
189 sprintf "%s%s : %s" (separate i) (CicPp.ppname n)
190 (ppterm_in_context subst t name_context), (Some n)::name_context
191 | Some (n,Cic.Def (bo,ty)) ->
192 sprintf "%s%s : %s := %s" (separate i) (CicPp.ppname n)
195 | Some ty -> ppterm_in_context subst ty name_context)
196 (ppterm_in_context subst bo name_context), (Some n)::name_context
198 sprintf "%s_ :? _" (separate i), None::name_context
201 let ppcontext ?sep subst context = fst (ppcontext' ?sep subst context)
203 let ppmetasenv ?(sep = "\n") metasenv subst =
207 let context,name_context = ppcontext' ~sep:"; " subst c in
208 sprintf "%s |- ?%d: %s" context i
209 (ppterm_in_context subst t name_context))
211 (fun (i, _, _) -> not (List.exists (fun (j, _) -> (j = i)) subst))
214 (* From now on we recreate a kernel abstraction where substitutions are part of
217 let lift subst n term =
218 let term = apply_subst subst term in
220 CicSubstitution.lift n term
222 raise (MetaSubstFailure ("Lift failure: " ^ Printexc.to_string e))
224 let subst subst t1 t2 =
225 let t1 = apply_subst subst t1 in
226 let t2 = apply_subst subst t2 in
228 CicSubstitution.subst t1 t2
230 raise (MetaSubstFailure ("Subst failure: " ^ Printexc.to_string e))
232 let whd subst context term =
233 let term = apply_subst subst term in
234 let context = apply_subst_context subst context in
236 CicReduction.whd context term
238 raise (MetaSubstFailure ("Weak head reduction failure: " ^
239 Printexc.to_string e))
241 let are_convertible subst context t1 t2 =
242 let context = apply_subst_context subst context in
243 let t1 = apply_subst subst t1 in
244 let t2 = apply_subst subst t2 in
245 CicReduction.are_convertible context t1 t2
247 let tempi_type_of_aux_subst = ref 0.0;;
248 let tempi_type_of_aux = ref 0.0;;
250 let type_of_aux' metasenv subst context term =
251 let time1 = Unix.gettimeofday () in
252 let term = apply_subst subst term in
253 let context = apply_subst_context subst context in
256 (fun (i, c, t) -> (i, apply_subst_context subst c, apply_subst subst t))
258 (fun (i, _, _) -> not (List.exists (fun (j, _) -> (j = i)) subst))
261 let time2 = Unix.gettimeofday () in
264 CicTypeChecker.type_of_aux' metasenv context term
265 with CicTypeChecker.TypeCheckerFailure msg ->
266 raise (MetaSubstFailure ("Type checker failure: " ^ msg))
268 let time3 = Unix.gettimeofday () in
269 tempi_type_of_aux_subst := !tempi_type_of_aux_subst +. time3 -. time1 ;
270 tempi_type_of_aux := !tempi_type_of_aux +. time2 -. time1 ;
274 (* the delift function takes in input a metavariable index, an ordered list of
275 * optional terms [t1,...,tn] and a term t, and substitutes every tk = Some
276 * (rel(nk)) with rel(k). Typically, the list of optional terms is the explicit
277 * substitution that is applied to a metavariable occurrence and the result of
278 * the delift function is a term the implicit variable can be substituted with
279 * to make the term [t] unifiable with the metavariable occurrence. In general,
280 * the problem is undecidable if we consider equivalence in place of alpha
281 * convertibility. Our implementation, though, is even weaker than alpha
282 * convertibility, since it replace the term [tk] if and only if [tk] is a Rel
283 * (missing all the other cases). Does this matter in practice?
284 * The metavariable index is the index of the metavariable that must not occur
285 * in the term (for occur check).
288 exception NotInTheList;;
293 [] -> raise NotInTheList
294 | (Some (Cic.Rel m))::_ when m=n -> k
295 | _::tl -> aux (k+1) tl in
301 let rec force_does_not_occur subst to_be_restricted t =
302 let module C = Cic in
303 let more_to_be_restricted = ref [] in
304 let rec aux k = function
305 C.Rel r when List.mem (r - k) to_be_restricted -> raise Occur
308 | C.Implicit _ -> assert false
310 (* we do not retrieve the term associated to ?n in subst since *)
311 (* in this way we can restrict if something goes wrong *)
323 more_to_be_restricted := (n,!i) :: !more_to_be_restricted;
328 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty)
329 | C.Prod (name,so,dest) -> C.Prod (name, aux k so, aux (k+1) dest)
330 | C.Lambda (name,so,dest) -> C.Lambda (name, aux k so, aux (k+1) dest)
331 | C.LetIn (name,so,dest) -> C.LetIn (name, aux k so, aux (k+1) dest)
332 | C.Appl l -> C.Appl (List.map (aux k) l)
333 | C.Var (uri,exp_named_subst) ->
334 let exp_named_subst' =
335 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
337 C.Var (uri, exp_named_subst')
338 | C.Const (uri, exp_named_subst) ->
339 let exp_named_subst' =
340 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
342 C.Const (uri, exp_named_subst')
343 | C.MutInd (uri,tyno,exp_named_subst) ->
344 let exp_named_subst' =
345 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
347 C.MutInd (uri, tyno, exp_named_subst')
348 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
349 let exp_named_subst' =
350 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
352 C.MutConstruct (uri, tyno, consno, exp_named_subst')
353 | C.MutCase (uri,tyno,out,te,pl) ->
354 C.MutCase (uri, tyno, aux k out, aux k te, List.map (aux k) pl)
356 let len = List.length fl in
357 let k_plus_len = k + len in
360 (fun (name,j,ty,bo) -> (name, j, aux k ty, aux k_plus_len bo)) fl
364 let len = List.length fl in
365 let k_plus_len = k + len in
368 (fun (name,ty,bo) -> (name, aux k ty, aux k_plus_len bo)) fl
373 (!more_to_be_restricted, res)
375 let rec restrict subst to_be_restricted metasenv =
376 let names_of_context_indexes context indexes =
381 match List.nth context i with
382 | None -> assert false
383 | Some (n, _) -> CicPp.ppname n
385 Failure _ -> assert false
388 let force_does_not_occur_in_context to_be_restricted = function
390 | Some (name, Cic.Decl t) ->
391 let (more_to_be_restricted, t') =
392 force_does_not_occur subst to_be_restricted t
394 more_to_be_restricted, Some (name, Cic.Decl t')
395 | Some (name, Cic.Def (bo, ty)) ->
396 let (more_to_be_restricted, bo') =
397 force_does_not_occur subst to_be_restricted bo
399 let more_to_be_restricted, ty' =
401 | None -> more_to_be_restricted, None
403 let more_to_be_restricted', ty' =
404 force_does_not_occur subst to_be_restricted ty
406 more_to_be_restricted @ more_to_be_restricted',
409 more_to_be_restricted, Some (name, Cic.Def (bo', ty'))
411 let rec erase i to_be_restricted n = function
412 | [] -> [], to_be_restricted, []
414 let more_to_be_restricted,restricted,tl' =
415 erase (i+1) to_be_restricted n tl
417 let restrict_me = List.mem i restricted in
419 more_to_be_restricted, restricted, None:: tl'
422 let more_to_be_restricted', hd' =
423 let delifted_restricted =
427 | j::tl when j > i -> (j - i)::aux tl
432 force_does_not_occur_in_context delifted_restricted hd
434 more_to_be_restricted @ more_to_be_restricted',
435 restricted, hd' :: tl'
437 more_to_be_restricted, (i :: restricted), None :: tl')
439 let (more_to_be_restricted, metasenv, subst) =
441 (fun (n, context, t) (more, metasenv, subst) ->
442 let to_be_restricted =
443 List.map snd (List.filter (fun (m, _) -> m = n) to_be_restricted)
445 let (more_to_be_restricted, restricted, context') =
446 (* just an optimization *)
447 if to_be_restricted = [] then
450 erase 1 to_be_restricted n context
453 let more_to_be_restricted', t' =
454 force_does_not_occur subst restricted t
456 let metasenv' = (n, context', t') :: metasenv in
458 let s = List.assoc n subst in
460 let more_to_be_restricted'', s' =
461 force_does_not_occur subst restricted s
463 let subst' = (n, s') :: (List.remove_assoc n subst) in
465 more @ more_to_be_restricted @ more_to_be_restricted' @
466 more_to_be_restricted''
468 (more, metasenv', subst')
470 raise (MetaSubstFailure (sprintf
471 "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"
472 n (names_of_context_indexes context to_be_restricted) n
474 with Not_found -> (more @ more_to_be_restricted @ more_to_be_restricted', metasenv', subst))
476 raise (MetaSubstFailure (sprintf
477 "Cannot restrict the context of the metavariable ?%d over the hypotheses %s since metavariable's type depends on at least one of them"
478 n (names_of_context_indexes context to_be_restricted))))
479 metasenv ([], [], subst)
481 match more_to_be_restricted with
482 | [] -> (metasenv, subst)
483 | _ -> restrict subst more_to_be_restricted metasenv
486 (*CSC: maybe we should rename delift in abstract, as I did in my dissertation *)
487 let delift n subst context metasenv l t =
488 let module S = CicSubstitution in
490 let (_, canonical_context, _) = CicUtil.lookup_meta n metasenv in
491 List.map2 (fun ct lt ->
497 let to_be_restricted = ref [] in
498 let rec deliftaux k =
499 let module C = Cic in
503 C.Rel m (*CSC: che succede se c'e' un Def? Dovrebbe averlo gia' *)
504 (*CSC: deliftato la regola per il LetIn *)
505 (*CSC: FALSO! La regola per il LetIn non lo fa *)
507 (match List.nth context (m-k-1) with
508 Some (_,C.Def (t,_)) ->
509 (*CSC: Hmmm. This bit of reduction is not in the spirit of *)
510 (*CSC: first order unification. Does it help or does it harm? *)
511 deliftaux k (S.lift m t)
512 | Some (_,C.Decl t) ->
513 C.Rel ((position (m-k) l) + k)
514 | None -> raise (MetaSubstFailure "RelToHiddenHypothesis"))
515 | C.Var (uri,exp_named_subst) ->
516 let exp_named_subst' =
517 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
519 C.Var (uri,exp_named_subst')
520 | C.Meta (i, l1) as t ->
522 raise (MetaSubstFailure (sprintf
523 "Cannot unify the metavariable ?%d with a term that has as subterm %s in which the same metavariable occurs (occur check)"
526 (* I do not consider the term associated to ?i in subst since *)
527 (* in this way I can restrict if something goes wrong. *)
531 | None::tl -> None::(deliftl (j+1) tl)
533 let l1' = (deliftl (j+1) tl) in
535 Some (deliftaux k t)::l1'
538 | MetaSubstFailure _ ->
539 to_be_restricted := (i,j)::!to_be_restricted ; None::l1'
541 let l' = deliftl 1 l1 in
544 | C.Implicit _ as t -> t
545 | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
546 | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
547 | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
548 | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
549 | C.Appl l -> C.Appl (List.map (deliftaux k) l)
550 | C.Const (uri,exp_named_subst) ->
551 let exp_named_subst' =
552 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
554 C.Const (uri,exp_named_subst')
555 | C.MutInd (uri,typeno,exp_named_subst) ->
556 let exp_named_subst' =
557 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
559 C.MutInd (uri,typeno,exp_named_subst')
560 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
561 let exp_named_subst' =
562 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
564 C.MutConstruct (uri,typeno,consno,exp_named_subst')
565 | C.MutCase (sp,i,outty,t,pl) ->
566 C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
567 List.map (deliftaux k) pl)
569 let len = List.length fl in
572 (fun (name, i, ty, bo) ->
573 (name, i, deliftaux k ty, deliftaux (k+len) bo))
578 let len = List.length fl in
581 (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
584 C.CoFix (i, liftedfl)
591 (* This is the case where we fail even first order unification. *)
592 (* The reason is that our delift function is weaker than first *)
593 (* order (in the sense of alpha-conversion). See comment above *)
594 (* related to the delift function. *)
595 debug_print "\n!!!!!!!!!!! First Order UnificationFailure, but maybe it could have been successful even in a first order setting (no conversion, only alpha convertibility)! Please, implement a better delift function !!!!!!!!!!!!!!!!" ;
596 debug_print "\nCicMetaSubst: UNCERTAIN" ;
597 raise (Uncertain (sprintf
598 "Error trying to abstract %s over [%s]: the algorithm only tried to abstract over bound variables"
602 (function Some t -> ppterm subst t | None -> "_")
605 let (metasenv, subst) = restrict subst !to_be_restricted metasenv in
609 (**** END OF DELIFT ****)
612 (** {2 Format-like pretty printers} *)
615 Format.pp_print_string ppf s;
616 Format.pp_print_newline ppf ();
617 Format.pp_print_flush ppf ()
619 let fppsubst ppf subst = fpp_gen ppf (ppsubst subst)
620 let fppterm ppf term = fpp_gen ppf (CicPp.ppterm term)
621 let fppmetasenv ppf metasenv = fpp_gen ppf (ppmetasenv metasenv [])