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/.
29 exception MetaSubstFailure of string
30 exception Uncertain of string
31 exception AssertFailure of string
33 let debug_print = prerr_endline
35 type substitution = (int * Cic.term) list
37 (*** Functions to apply a substitution ***)
39 let apply_subst_gen ~appl_fun subst term =
42 let module S = CicSubstitution in
45 | C.Var (uri,exp_named_subst) ->
46 let exp_named_subst' =
47 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
49 C.Var (uri, exp_named_subst')
52 let t = List.assoc i subst in
53 um_aux (S.lift_meta l t)
54 with Not_found -> (* not constrained variable, i.e. free in subst*)
56 List.map (function None -> None | Some t -> Some (um_aux t)) l
60 | C.Implicit _ -> assert false
61 | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
62 | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
63 | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
64 | C.LetIn (n,s,t) -> C.LetIn (n, um_aux s, um_aux t)
65 | C.Appl (hd :: tl) -> appl_fun um_aux hd tl
66 | C.Appl _ -> assert false
67 | C.Const (uri,exp_named_subst) ->
68 let exp_named_subst' =
69 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
71 C.Const (uri, exp_named_subst')
72 | C.MutInd (uri,typeno,exp_named_subst) ->
73 let exp_named_subst' =
74 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
76 C.MutInd (uri,typeno,exp_named_subst')
77 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
78 let exp_named_subst' =
79 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
81 C.MutConstruct (uri,typeno,consno,exp_named_subst')
82 | C.MutCase (sp,i,outty,t,pl) ->
83 let pl' = List.map um_aux pl in
84 C.MutCase (sp, i, um_aux outty, um_aux t, pl')
87 List.map (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo)) fl
92 List.map (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo)) fl
100 (* CSC: old code that never performs beta reduction
101 let appl_fun um_aux he tl =
102 let tl' = List.map um_aux tl in
105 Cic.Appl l -> Cic.Appl (l@tl')
106 | he' -> Cic.Appl (he'::tl')
109 apply_subst_gen ~appl_fun
111 let appl_fun um_aux he tl =
112 let tl' = List.map um_aux tl in
115 Cic.Appl l -> Cic.Appl (l@tl')
116 | he' -> Cic.Appl (he'::tl')
121 let rec beta_reduce =
123 (Cic.Appl (Cic.Lambda (_,_,t)::he'::tl')) ->
124 let he'' = CicSubstitution.subst he' t in
128 beta_reduce (Cic.Appl(he''::tl'))
135 apply_subst_gen ~appl_fun
138 let rec apply_subst_context subst context =
142 | Some (n, Cic.Decl t) ->
143 let t' = apply_subst subst t in
144 Some (n, Cic.Decl t') :: context
145 | Some (n, Cic.Def (t, ty)) ->
149 | Some ty -> Some (apply_subst subst ty)
151 let t' = apply_subst subst t in
152 Some (n, Cic.Def (t', ty')) :: context
153 | None -> None :: context)
156 let apply_subst_metasenv subst metasenv =
158 (fun (n, context, ty) ->
159 (n, apply_subst_context subst context, apply_subst subst ty))
161 (fun (i, _, _) -> not (List.exists (fun (j, _) -> (j = i)) subst))
164 (***** Pretty printing functions ******)
169 (fun (idx, term) -> Printf.sprintf "?%d := %s" idx (CicPp.ppterm term))
173 let ppterm subst term = CicPp.ppterm (apply_subst subst term)
175 let ppterm_in_context subst term name_context =
176 CicPp.pp (apply_subst subst term) name_context
178 let ppcontext' ?(sep = "\n") subst context =
179 let separate s = if s = "" then "" else s ^ sep in
181 (fun context_entry (i,name_context) ->
182 match context_entry with
183 Some (n,Cic.Decl t) ->
184 sprintf "%s%s : %s" (separate i) (CicPp.ppname n)
185 (ppterm_in_context subst t name_context), (Some n)::name_context
186 | Some (n,Cic.Def (bo,ty)) ->
187 sprintf "%s%s : %s := %s" (separate i) (CicPp.ppname n)
190 | Some ty -> ppterm_in_context subst ty name_context)
191 (ppterm_in_context subst bo name_context), (Some n)::name_context
193 sprintf "%s_ :? _" (separate i), None::name_context
196 let ppcontext ?sep subst context = fst (ppcontext' ?sep subst context)
198 let ppmetasenv ?(sep = "\n") metasenv subst =
202 let context,name_context = ppcontext' ~sep:"; " subst c in
203 sprintf "%s |- ?%d: %s" context i
204 (ppterm_in_context subst t name_context))
206 (fun (i, _, _) -> not (List.exists (fun (j, _) -> (j = i)) subst))
209 (* From now on we recreate a kernel abstraction where substitutions are part of
212 let lift subst n term =
213 let term = apply_subst subst term in
215 CicSubstitution.lift n term
217 raise (MetaSubstFailure ("Lift failure: " ^ Printexc.to_string e))
219 let subst subst t1 t2 =
220 let t1 = apply_subst subst t1 in
221 let t2 = apply_subst subst t2 in
223 CicSubstitution.subst t1 t2
225 raise (MetaSubstFailure ("Subst failure: " ^ Printexc.to_string e))
227 let whd subst context term =
228 let term = apply_subst subst term in
229 let context = apply_subst_context subst context in
231 CicReduction.whd context term
233 raise (MetaSubstFailure ("Weak head reduction failure: " ^
234 Printexc.to_string e))
236 let are_convertible subst context t1 t2 =
237 let context = apply_subst_context subst context in
238 let t1 = apply_subst subst t1 in
239 let t2 = apply_subst subst t2 in
240 CicReduction.are_convertible context t1 t2
242 let tempi_type_of_aux_subst = ref 0.0;;
243 let tempi_subst = ref 0.0;;
244 let tempi_type_of_aux = ref 0.0;;
246 let type_of_aux' metasenv subst context term =
247 let time1 = Unix.gettimeofday () in
248 let term = apply_subst subst term in
249 let context = apply_subst_context subst context in
252 (fun (i, c, t) -> (i, apply_subst_context subst c, apply_subst subst t))
254 (fun (i, _, _) -> not (List.exists (fun (j, _) -> (j = i)) subst))
257 let time2 = Unix.gettimeofday () in
260 CicTypeChecker.type_of_aux' metasenv context term
261 with CicTypeChecker.TypeCheckerFailure msg ->
262 raise (MetaSubstFailure ("Type checker failure: " ^ msg))
264 let time3 = Unix.gettimeofday () in
265 tempi_type_of_aux_subst := !tempi_type_of_aux_subst +. time3 -. time1 ;
266 tempi_subst := !tempi_subst +. time2 -. time1 ;
267 tempi_type_of_aux := !tempi_type_of_aux +. time3 -. time2 ;
271 (* the delift function takes in input a metavariable index, an ordered list of
272 * optional terms [t1,...,tn] and a term t, and substitutes every tk = Some
273 * (rel(nk)) with rel(k). Typically, the list of optional terms is the explicit
274 * substitution that is applied to a metavariable occurrence and the result of
275 * the delift function is a term the implicit variable can be substituted with
276 * to make the term [t] unifiable with the metavariable occurrence. In general,
277 * the problem is undecidable if we consider equivalence in place of alpha
278 * convertibility. Our implementation, though, is even weaker than alpha
279 * convertibility, since it replace the term [tk] if and only if [tk] is a Rel
280 * (missing all the other cases). Does this matter in practice?
281 * The metavariable index is the index of the metavariable that must not occur
282 * in the term (for occur check).
285 exception NotInTheList;;
290 [] -> raise NotInTheList
291 | (Some (Cic.Rel m))::_ when m=n -> k
292 | _::tl -> aux (k+1) tl in
298 let rec force_does_not_occur subst to_be_restricted t =
299 let module C = Cic in
300 let more_to_be_restricted = ref [] in
301 let rec aux k = function
302 C.Rel r when List.mem (r - k) to_be_restricted -> raise Occur
305 | C.Implicit _ -> assert false
307 (* we do not retrieve the term associated to ?n in subst since *)
308 (* in this way we can restrict if something goes wrong *)
320 more_to_be_restricted := (n,!i) :: !more_to_be_restricted;
325 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty)
326 | C.Prod (name,so,dest) -> C.Prod (name, aux k so, aux (k+1) dest)
327 | C.Lambda (name,so,dest) -> C.Lambda (name, aux k so, aux (k+1) dest)
328 | C.LetIn (name,so,dest) -> C.LetIn (name, aux k so, aux (k+1) dest)
329 | C.Appl l -> C.Appl (List.map (aux k) l)
330 | C.Var (uri,exp_named_subst) ->
331 let exp_named_subst' =
332 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
334 C.Var (uri, exp_named_subst')
335 | C.Const (uri, exp_named_subst) ->
336 let exp_named_subst' =
337 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
339 C.Const (uri, exp_named_subst')
340 | C.MutInd (uri,tyno,exp_named_subst) ->
341 let exp_named_subst' =
342 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
344 C.MutInd (uri, tyno, exp_named_subst')
345 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
346 let exp_named_subst' =
347 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
349 C.MutConstruct (uri, tyno, consno, exp_named_subst')
350 | C.MutCase (uri,tyno,out,te,pl) ->
351 C.MutCase (uri, tyno, aux k out, aux k te, List.map (aux k) pl)
353 let len = List.length fl in
354 let k_plus_len = k + len in
357 (fun (name,j,ty,bo) -> (name, j, aux k ty, aux k_plus_len bo)) fl
361 let len = List.length fl in
362 let k_plus_len = k + len in
365 (fun (name,ty,bo) -> (name, aux k ty, aux k_plus_len bo)) fl
370 (!more_to_be_restricted, res)
372 let rec restrict subst to_be_restricted metasenv =
373 let names_of_context_indexes context indexes =
378 match List.nth context i with
379 | None -> assert false
380 | Some (n, _) -> CicPp.ppname n
382 Failure _ -> assert false
385 let force_does_not_occur_in_context to_be_restricted = function
387 | Some (name, Cic.Decl t) ->
388 let (more_to_be_restricted, t') =
389 force_does_not_occur subst to_be_restricted t
391 more_to_be_restricted, Some (name, Cic.Decl t')
392 | Some (name, Cic.Def (bo, ty)) ->
393 let (more_to_be_restricted, bo') =
394 force_does_not_occur subst to_be_restricted bo
396 let more_to_be_restricted, ty' =
398 | None -> more_to_be_restricted, None
400 let more_to_be_restricted', ty' =
401 force_does_not_occur subst to_be_restricted ty
403 more_to_be_restricted @ more_to_be_restricted',
406 more_to_be_restricted, Some (name, Cic.Def (bo', ty'))
408 let rec erase i to_be_restricted n = function
409 | [] -> [], to_be_restricted, []
411 let more_to_be_restricted,restricted,tl' =
412 erase (i+1) to_be_restricted n tl
414 let restrict_me = List.mem i restricted in
416 more_to_be_restricted, restricted, None:: tl'
419 let more_to_be_restricted', hd' =
420 let delifted_restricted =
424 | j::tl when j > i -> (j - i)::aux tl
429 force_does_not_occur_in_context delifted_restricted hd
431 more_to_be_restricted @ more_to_be_restricted',
432 restricted, hd' :: tl'
434 more_to_be_restricted, (i :: restricted), None :: tl')
436 let (more_to_be_restricted, metasenv, subst) =
438 (fun (n, context, t) (more, metasenv, subst) ->
439 let to_be_restricted =
440 List.map snd (List.filter (fun (m, _) -> m = n) to_be_restricted)
442 let (more_to_be_restricted, restricted, context') =
443 (* just an optimization *)
444 if to_be_restricted = [] then
447 erase 1 to_be_restricted n context
450 let more_to_be_restricted', t' =
451 force_does_not_occur subst restricted t
453 let metasenv' = (n, context', t') :: metasenv in
455 let s = List.assoc n subst in
457 let more_to_be_restricted'', s' =
458 force_does_not_occur subst restricted s
460 let subst' = (n, s') :: (List.remove_assoc n subst) in
462 more @ more_to_be_restricted @ more_to_be_restricted' @
463 more_to_be_restricted''
465 (more, metasenv', subst')
467 raise (MetaSubstFailure (sprintf
468 "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"
469 n (names_of_context_indexes context to_be_restricted) n
471 with Not_found -> (more @ more_to_be_restricted @ more_to_be_restricted', metasenv', subst))
473 raise (MetaSubstFailure (sprintf
474 "Cannot restrict the context of the metavariable ?%d over the hypotheses %s since metavariable's type depends on at least one of them"
475 n (names_of_context_indexes context to_be_restricted))))
476 metasenv ([], [], subst)
478 match more_to_be_restricted with
479 | [] -> (metasenv, subst)
480 | _ -> restrict subst more_to_be_restricted metasenv
483 (*CSC: maybe we should rename delift in abstract, as I did in my dissertation *)
484 let delift n subst context metasenv l t =
485 let module S = CicSubstitution in
487 let (_, canonical_context, _) = CicUtil.lookup_meta n metasenv in
488 List.map2 (fun ct lt ->
494 let to_be_restricted = ref [] in
495 let rec deliftaux k =
496 let module C = Cic in
500 C.Rel m (*CSC: che succede se c'e' un Def? Dovrebbe averlo gia' *)
501 (*CSC: deliftato la regola per il LetIn *)
502 (*CSC: FALSO! La regola per il LetIn non lo fa *)
505 match List.nth context (m-k-1) with
506 Some (_,C.Def (t,_)) ->
507 (*CSC: Hmmm. This bit of reduction is not in the spirit of *)
508 (*CSC: first order unification. Does it help or does it harm? *)
509 deliftaux k (S.lift m t)
510 | Some (_,C.Decl t) ->
511 C.Rel ((position (m-k) l) + k)
512 | None -> raise (MetaSubstFailure "RelToHiddenHypothesis")
515 raise (MetaSubstFailure "Unbound variable found in deliftaux")
517 | C.Var (uri,exp_named_subst) ->
518 let exp_named_subst' =
519 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
521 C.Var (uri,exp_named_subst')
522 | C.Meta (i, l1) as t ->
524 raise (MetaSubstFailure (sprintf
525 "Cannot unify the metavariable ?%d with a term that has as subterm %s in which the same metavariable occurs (occur check)"
528 (* I do not consider the term associated to ?i in subst since *)
529 (* in this way I can restrict if something goes wrong. *)
533 | None::tl -> None::(deliftl (j+1) tl)
535 let l1' = (deliftl (j+1) tl) in
537 Some (deliftaux k t)::l1'
540 | MetaSubstFailure _ ->
541 to_be_restricted := (i,j)::!to_be_restricted ; None::l1'
543 let l' = deliftl 1 l1 in
546 | C.Implicit _ as t -> t
547 | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
548 | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
549 | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
550 | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
551 | C.Appl l -> C.Appl (List.map (deliftaux k) l)
552 | C.Const (uri,exp_named_subst) ->
553 let exp_named_subst' =
554 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
556 C.Const (uri,exp_named_subst')
557 | C.MutInd (uri,typeno,exp_named_subst) ->
558 let exp_named_subst' =
559 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
561 C.MutInd (uri,typeno,exp_named_subst')
562 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
563 let exp_named_subst' =
564 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
566 C.MutConstruct (uri,typeno,consno,exp_named_subst')
567 | C.MutCase (sp,i,outty,t,pl) ->
568 C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
569 List.map (deliftaux k) pl)
571 let len = List.length fl in
574 (fun (name, i, ty, bo) ->
575 (name, i, deliftaux k ty, deliftaux (k+len) bo))
580 let len = List.length fl in
583 (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
586 C.CoFix (i, liftedfl)
593 (* This is the case where we fail even first order unification. *)
594 (* The reason is that our delift function is weaker than first *)
595 (* order (in the sense of alpha-conversion). See comment above *)
596 (* related to the delift function. *)
597 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 !!!!!!!!!!!!!!!!" ;
598 raise (Uncertain (sprintf
599 "Error trying to abstract %s over [%s]: the algorithm only tried to abstract over bound variables"
603 (function Some t -> ppterm subst t | None -> "_")
606 let (metasenv, subst) = restrict subst !to_be_restricted metasenv in
610 (**** END OF DELIFT ****)
613 (** {2 Format-like pretty printers} *)
616 Format.pp_print_string ppf s;
617 Format.pp_print_newline ppf ();
618 Format.pp_print_flush ppf ()
620 let fppsubst ppf subst = fpp_gen ppf (ppsubst subst)
621 let fppterm ppf term = fpp_gen ppf (CicPp.ppterm term)
622 let fppmetasenv ppf metasenv = fpp_gen ppf (ppmetasenv metasenv [])