4 exception AssertFailure of string
5 exception MetaSubstFailure of string
7 let debug_print = prerr_endline
9 type substitution = (int * Cic.term) list
14 (fun (idx, term) -> Printf.sprintf "?%d := %s" idx (CicPp.ppterm term))
18 (* the delift function takes in input a metavariable index, an ordered list of
19 * optional terms [t1,...,tn] and a term t, and substitutes every tk = Some
20 * (rel(nk)) with rel(k). Typically, the list of optional terms is the explicit
21 * substitution that is applied to a metavariable occurrence and the result of
22 * the delift function is a term the implicit variable can be substituted with
23 * to make the term [t] unifiable with the metavariable occurrence. In general,
24 * the problem is undecidable if we consider equivalence in place of alpha
25 * convertibility. Our implementation, though, is even weaker than alpha
26 * convertibility, since it replace the term [tk] if and only if [tk] is a Rel
27 * (missing all the other cases). Does this matter in practice?
28 * The metavariable index is the index of the metavariable that must not occur
29 * in the term (for occur check).
32 exception NotInTheList;;
37 [] -> raise NotInTheList
38 | (Some (Cic.Rel m))::_ when m=n -> k
39 | _::tl -> aux (k+1) tl in
45 let rec force_does_not_occur subst to_be_restricted t =
47 let more_to_be_restricted = ref [] in
48 let rec aux k = function
49 C.Rel r when List.mem (r - k) to_be_restricted -> raise Occur
52 | C.Implicit -> assert false
54 (* we do not retrieve the term associated to ?n in subst since *)
55 (* in this way we can restrict if something goes wrong *)
67 more_to_be_restricted := (n,!i) :: !more_to_be_restricted;
72 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty)
73 | C.Prod (name,so,dest) -> C.Prod (name, aux k so, aux (k+1) dest)
74 | C.Lambda (name,so,dest) -> C.Lambda (name, aux k so, aux (k+1) dest)
75 | C.LetIn (name,so,dest) -> C.LetIn (name, aux k so, aux (k+1) dest)
76 | C.Appl l -> C.Appl (List.map (aux k) l)
77 | C.Var (uri,exp_named_subst) ->
78 let exp_named_subst' =
79 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
81 C.Var (uri, exp_named_subst')
82 | C.Const (uri, exp_named_subst) ->
83 let exp_named_subst' =
84 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
86 C.Const (uri, exp_named_subst')
87 | C.MutInd (uri,tyno,exp_named_subst) ->
88 let exp_named_subst' =
89 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
91 C.MutInd (uri, tyno, exp_named_subst')
92 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
93 let exp_named_subst' =
94 List.map (fun (uri,t) -> (uri, aux k t)) exp_named_subst
96 C.MutConstruct (uri, tyno, consno, exp_named_subst')
97 | C.MutCase (uri,tyno,out,te,pl) ->
98 C.MutCase (uri, tyno, aux k out, aux k te, List.map (aux k) pl)
100 let len = List.length fl in
101 let k_plus_len = k + len in
104 (fun (name,j,ty,bo) -> (name, j, aux k ty, aux k_plus_len bo)) fl
108 let len = List.length fl in
109 let k_plus_len = k + len in
112 (fun (name,ty,bo) -> (name, aux k ty, aux k_plus_len bo)) fl
117 (!more_to_be_restricted, res)
119 let rec restrict subst to_be_restricted metasenv =
120 let names_of_context_indexes context indexes =
125 match List.nth context i with
126 | None -> assert false
127 | Some (n, _) -> CicPp.ppname n
129 Failure _ -> assert false
132 let force_does_not_occur_in_context to_be_restricted = function
134 | Some (name, Cic.Decl t) ->
135 let (more_to_be_restricted, t') =
136 force_does_not_occur subst to_be_restricted t
138 more_to_be_restricted, Some (name, Cic.Decl t')
139 | Some (name, Cic.Def (bo, ty)) ->
140 let (more_to_be_restricted, bo') =
141 force_does_not_occur subst to_be_restricted bo
143 let more_to_be_restricted, ty' =
145 | None -> more_to_be_restricted, None
147 let more_to_be_restricted', ty' =
148 force_does_not_occur subst to_be_restricted ty
150 more_to_be_restricted @ more_to_be_restricted',
153 more_to_be_restricted, Some (name, Cic.Def (bo', ty'))
155 let rec erase i to_be_restricted n = function
156 | [] -> [], to_be_restricted, []
158 let more_to_be_restricted,restricted,tl' =
159 erase (i+1) to_be_restricted n tl
161 let restrict_me = List.mem i restricted in
163 more_to_be_restricted, restricted, None:: tl'
166 let more_to_be_restricted', hd' =
167 let delifted_restricted =
171 | j::tl when j > i -> (j - i)::aux tl
176 force_does_not_occur_in_context delifted_restricted hd
178 more_to_be_restricted @ more_to_be_restricted',
179 restricted, hd' :: tl'
181 more_to_be_restricted, (i :: restricted), None :: tl')
183 let (more_to_be_restricted, metasenv, subst) =
185 (fun (n, context, t) (more, metasenv, subst) ->
186 let to_be_restricted =
187 List.map snd (List.filter (fun (m, _) -> m = n) to_be_restricted)
189 let (more_to_be_restricted, restricted, context') =
190 (* just an optimization *)
191 if to_be_restricted = [] then
194 erase 1 to_be_restricted n context
197 let more_to_be_restricted', t' =
198 force_does_not_occur subst restricted t
200 let metasenv' = (n, context', t') :: metasenv in
202 let s = List.assoc n subst in
204 let more_to_be_restricted'', s' =
205 force_does_not_occur subst restricted s
207 let subst' = (n, s') :: (List.remove_assoc n subst) in
209 more @ more_to_be_restricted @ more_to_be_restricted' @
210 more_to_be_restricted''
212 (more, metasenv', subst')
214 raise (MetaSubstFailure (sprintf
215 "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"
216 n (names_of_context_indexes context to_be_restricted) n
218 with Not_found -> (more @ more_to_be_restricted @ more_to_be_restricted', metasenv', subst))
220 raise (MetaSubstFailure (sprintf
221 "Cannot restrict the context of the metavariable ?%d over the hypotheses %s since metavariable's type depends on at least one of them"
222 n (names_of_context_indexes context to_be_restricted))))
223 metasenv ([], [], subst)
225 match more_to_be_restricted with
226 | [] -> (metasenv, subst)
227 | _ -> restrict subst more_to_be_restricted metasenv
230 (*CSC: maybe we should rename delift in abstract, as I did in my dissertation *)
231 let delift n subst context metasenv l t =
232 let module S = CicSubstitution in
234 let (_, canonical_context, _) = CicUtil.lookup_meta n metasenv in
235 List.map2 (fun ct lt ->
241 let to_be_restricted = ref [] in
242 let rec deliftaux k =
243 let module C = Cic in
247 C.Rel m (*CSC: che succede se c'e' un Def? Dovrebbe averlo gia' *)
248 (*CSC: deliftato la regola per il LetIn *)
249 (*CSC: FALSO! La regola per il LetIn non lo fa *)
251 (match List.nth context (m-k-1) with
252 Some (_,C.Def (t,_)) ->
253 (*CSC: Hmmm. This bit of reduction is not in the spirit of *)
254 (*CSC: first order unification. Does it help or does it harm? *)
255 deliftaux k (S.lift m t)
256 | Some (_,C.Decl t) ->
257 (*CSC: The following check seems to be wrong! *)
258 (*CSC: B:Set |- ?2 : Set *)
259 (*CSC: A:Set ; x:?2[A/B] |- ?1[x/A] =?= x *)
260 (*CSC: Why should I restrict ?2 over B? The instantiation *)
261 (*CSC: ?1 := A is perfectly reasonable and well-typed. *)
262 (*CSC: Thus I comment out the following two lines that *)
263 (*CSC: are the incriminated ones. *)
264 (*(* It may augment to_be_restricted *)
265 ignore (deliftaux k (S.lift m t)) ;*)
266 (*CSC: end of bug commented out *)
267 C.Rel ((position (m-k) l) + k)
268 | None -> raise (MetaSubstFailure "RelToHiddenHypothesis"))
269 | C.Var (uri,exp_named_subst) ->
270 let exp_named_subst' =
271 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
273 C.Var (uri,exp_named_subst')
274 | C.Meta (i, l1) as t ->
276 raise (MetaSubstFailure (sprintf
277 "Cannot unify the metavariable ?%d with a term that has as subterm %s in which the same metavariable occurs (occur check)"
280 (* I do not consider the term associated to ?i in subst since *)
281 (* in this way I can restrict if something goes wrong. *)
285 | None::tl -> None::(deliftl (j+1) tl)
287 let l1' = (deliftl (j+1) tl) in
289 Some (deliftaux k t)::l1'
292 | MetaSubstFailure _ ->
293 to_be_restricted := (i,j)::!to_be_restricted ; None::l1'
295 let l' = deliftl 1 l1 in
298 | C.Implicit as t -> t
299 | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
300 | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
301 | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
302 | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
303 | C.Appl l -> C.Appl (List.map (deliftaux k) l)
304 | C.Const (uri,exp_named_subst) ->
305 let exp_named_subst' =
306 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
308 C.Const (uri,exp_named_subst')
309 | C.MutInd (uri,typeno,exp_named_subst) ->
310 let exp_named_subst' =
311 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
313 C.MutInd (uri,typeno,exp_named_subst')
314 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
315 let exp_named_subst' =
316 List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
318 C.MutConstruct (uri,typeno,consno,exp_named_subst')
319 | C.MutCase (sp,i,outty,t,pl) ->
320 C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
321 List.map (deliftaux k) pl)
323 let len = List.length fl in
326 (fun (name, i, ty, bo) ->
327 (name, i, deliftaux k ty, deliftaux (k+len) bo))
332 let len = List.length fl in
335 (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
338 C.CoFix (i, liftedfl)
345 (* This is the case where we fail even first order unification. *)
346 (* The reason is that our delift function is weaker than first *)
347 (* order (in the sense of alpha-conversion). See comment above *)
348 (* related to the delift function. *)
349 debug_print "!!!!!!!!!!! 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 !!!!!!!!!!!!!!!!" ;
350 raise (MetaSubstFailure (sprintf
351 "Error trying to abstract %s over [%s]: the algorithm only tried to abstract over bound variables"
355 (function Some t -> CicPp.ppterm t | None -> "_")
358 let (metasenv, subst) = restrict subst !to_be_restricted metasenv in
362 (**** END OF DELIFT ****)
364 let apply_subst_gen ~appl_fun subst term =
366 let module C = Cic in
367 let module S = CicSubstitution in
373 let t = List.assoc i subst in
374 um_aux (S.lift_meta l t)
375 with Not_found -> (* not constrained variable, i.e. free in subst*)
377 List.map (function None -> None | Some t -> Some (um_aux t)) l
381 | C.Implicit -> assert false
382 | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
383 | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
384 | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
385 | C.LetIn (n,s,t) -> C.LetIn (n, um_aux s, um_aux t)
386 | C.Appl (hd :: tl) -> appl_fun um_aux hd tl
387 | C.Appl _ -> assert false
388 | C.Const (uri,exp_named_subst) ->
389 let exp_named_subst' =
390 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
392 C.Const (uri, exp_named_subst')
393 | C.MutInd (uri,typeno,exp_named_subst) ->
394 let exp_named_subst' =
395 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
397 C.MutInd (uri,typeno,exp_named_subst')
398 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
399 let exp_named_subst' =
400 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
402 C.MutConstruct (uri,typeno,consno,exp_named_subst')
403 | C.MutCase (sp,i,outty,t,pl) ->
404 let pl' = List.map um_aux pl in
405 C.MutCase (sp, i, um_aux outty, um_aux t, pl')
408 List.map (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo)) fl
413 List.map (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo)) fl
420 let appl_fun um_aux he tl =
421 let tl' = List.map um_aux tl in
424 Cic.Appl l -> Cic.Appl (l@tl')
425 | he' -> Cic.Appl (he'::tl')
428 apply_subst_gen ~appl_fun
430 let ppterm subst term = CicPp.ppterm (apply_subst subst term)
432 (* apply_subst_reducing subst (Some (mtr,reductions_no)) t *)
433 (* performs as (apply_subst subst t) until it finds an application of *)
434 (* (META [meta_to_reduce]) that, once unwinding is performed, creates *)
435 (* a new beta-redex; in this case up to [reductions_no] consecutive *)
436 (* beta-reductions are performed. *)
437 (* Hint: this function is usually called when [reductions_no] *)
438 (* eta-expansions have been performed and the head of the new *)
439 (* application has been unified with (META [meta_to_reduce]): *)
440 (* during the unwinding the eta-expansions are undone. *)
442 let apply_subst_reducing meta_to_reduce =
443 let appl_fun um_aux he tl =
444 let tl' = List.map um_aux tl in
447 Cic.Appl l -> Cic.Appl (l@tl')
448 | he' -> Cic.Appl (he'::tl')
451 match meta_to_reduce, he with
452 Some (mtr,reductions_no), Cic.Meta (m,_) when m = mtr ->
453 let rec beta_reduce =
455 (n,(Cic.Appl (Cic.Lambda (_,_,t)::he'::tl'))) when n > 0 ->
456 let he'' = CicSubstitution.subst he' t in
460 beta_reduce (n-1,Cic.Appl(he''::tl'))
463 beta_reduce (reductions_no,t')
467 apply_subst_gen ~appl_fun
469 let rec apply_subst_context subst context =
473 | Some (n, Cic.Decl t) ->
474 let t' = apply_subst subst t in
475 Some (n, Cic.Decl t') :: context
476 | Some (n, Cic.Def (t, ty)) ->
480 | Some ty -> Some (apply_subst subst ty)
482 let t' = apply_subst subst t in
483 Some (n, Cic.Def (t', ty')) :: context
484 | None -> None :: context)
487 let apply_subst_metasenv subst metasenv =
489 (fun (n, context, ty) ->
490 (n, apply_subst_context subst context, apply_subst subst ty))
492 (fun (i, _, _) -> not (List.exists (fun (j, _) -> (j = i)) subst))
495 let ppterm subst term = CicPp.ppterm (apply_subst subst term)
497 let ppterm_in_context subst term name_context =
498 CicPp.pp (apply_subst subst term) name_context
500 let ppcontext' ?(sep = "\n") subst context =
501 let separate s = if s = "" then "" else s ^ sep in
503 (fun context_entry (i,name_context) ->
504 match context_entry with
505 Some (n,Cic.Decl t) ->
506 sprintf "%s%s : %s" (separate i) (CicPp.ppname n)
507 (ppterm_in_context subst t name_context), (Some n)::name_context
508 | Some (n,Cic.Def (bo,ty)) ->
509 sprintf "%s%s : %s := %s" (separate i) (CicPp.ppname n)
512 | Some ty -> ppterm_in_context subst ty name_context)
513 (ppterm_in_context subst bo name_context), (Some n)::name_context
515 sprintf "%s_ :? _" (separate i), None::name_context
518 let ppcontext ?sep subst context = fst (ppcontext' ?sep subst context)
520 let ppmetasenv ?(sep = "\n") metasenv subst =
524 let context,name_context = ppcontext' ~sep:"; " subst c in
525 sprintf "%s |- ?%d: %s" context i
526 (ppterm_in_context subst t name_context))
528 (fun (i, _, _) -> not (List.exists (fun (j, _) -> (j = i)) subst))
531 (* UNWIND THE MGU INSIDE THE MGU *)
533 let unwind_subst metasenv subst =
535 (fun (unwinded,metasenv) (i,_) ->
536 let (_,canonical_context,_) = CicUtil.lookup_meta i metasenv in
537 let identity_relocation_list =
538 CicMkImplicit.identity_relocation_list_for_metavariable canonical_context
540 let (_,metasenv',subst') =
541 unwind metasenv subst unwinded (Cic.Meta (i,identity_relocation_list))
544 ) ([],metasenv) subst
547 (* From now on we recreate a kernel abstraction where substitutions are part of
550 let lift subst n term =
551 let term = apply_subst subst term in
553 CicSubstitution.lift n term
555 raise (MetaSubstFailure ("Lift failure: " ^ Printexc.to_string e))
557 let subst subst t1 t2 =
558 let t1 = apply_subst subst t1 in
559 let t2 = apply_subst subst t2 in
561 CicSubstitution.subst t1 t2
563 raise (MetaSubstFailure ("Subst failure: " ^ Printexc.to_string e))
565 let whd subst context term =
566 let term = apply_subst subst term in
567 let context = apply_subst_context subst context in
569 CicReduction.whd context term
571 raise (MetaSubstFailure ("Weak head reduction failure: " ^
572 Printexc.to_string e))
574 let are_convertible subst context t1 t2 =
575 let context = apply_subst_context subst context in
576 let t1 = apply_subst subst t1 in
577 let t2 = apply_subst subst t2 in
578 CicReduction.are_convertible context t1 t2
580 let tempi_type_of_aux_subst = ref 0.0;;
581 let tempi_type_of_aux = ref 0.0;;
583 let type_of_aux' metasenv subst context term =
584 let time1 = Unix.gettimeofday () in
585 let term = apply_subst subst term in
586 let context = apply_subst_context subst context in
589 (fun (i, c, t) -> (i, apply_subst_context subst c, apply_subst subst t))
591 (fun (i, _, _) -> not (List.exists (fun (j, _) -> (j = i)) subst))
594 let time2 = Unix.gettimeofday () in
597 CicTypeChecker.type_of_aux' metasenv context term
598 with CicTypeChecker.TypeCheckerFailure msg ->
599 raise (MetaSubstFailure ("Type checker failure: " ^ msg))
601 let time3 = Unix.gettimeofday () in
602 tempi_type_of_aux_subst := !tempi_type_of_aux_subst +. time3 -. time1 ;
603 tempi_type_of_aux := !tempi_type_of_aux +. time2 -. time1 ;
606 (** {2 Format-like pretty printers} *)
609 Format.pp_print_string ppf s;
610 Format.pp_print_newline ppf ();
611 Format.pp_print_flush ppf ()
613 let fppsubst ppf subst = fpp_gen ppf (ppsubst subst)
614 let fppterm ppf term = fpp_gen ppf (CicPp.ppterm term)
615 let fppmetasenv ppf metasenv = fpp_gen ppf (ppmetasenv metasenv [])