2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
14 exception MetaSubstFailure of string Lazy.t
15 exception Uncertain of string Lazy.t
18 (*** Functions to apply a substitution ***)
20 let apply_subst_gen ~appl_fun subst term =
23 let module S = CicSubstitution in
26 | C.Var (uri,exp_named_subst) ->
27 let exp_named_subst' =
28 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
30 C.Var (uri, exp_named_subst')
33 let (_, t,_) = lookup_subst i subst in
34 um_aux (S.subst_meta l t)
35 with CicUtil.Subst_not_found _ ->
36 (* unconstrained variable, i.e. free in subst*)
38 List.map (function None -> None | Some t -> Some (um_aux t)) l
42 | C.Implicit _ as t -> t
43 | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
44 | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
45 | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
46 | C.LetIn (n,s,ty,t) -> C.LetIn (n, um_aux s, um_aux ty, um_aux t)
47 | C.Appl (hd :: tl) -> appl_fun um_aux hd tl
48 | C.Appl _ -> assert false
49 | C.Const (uri,exp_named_subst) ->
50 let exp_named_subst' =
51 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
53 C.Const (uri, exp_named_subst')
54 | C.MutInd (uri,typeno,exp_named_subst) ->
55 let exp_named_subst' =
56 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
58 C.MutInd (uri,typeno,exp_named_subst')
59 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
60 let exp_named_subst' =
61 List.map (fun (uri, t) -> (uri, um_aux t)) exp_named_subst
63 C.MutConstruct (uri,typeno,consno,exp_named_subst')
64 | C.MutCase (sp,i,outty,t,pl) ->
65 let pl' = List.map um_aux pl in
66 C.MutCase (sp, i, um_aux outty, um_aux t, pl')
69 List.map (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo)) fl
74 List.map (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo)) fl
82 let appl_fun um_aux he tl =
83 let tl' = List.map um_aux tl in
86 Cic.Appl l -> Cic.Appl (l@tl')
87 | he' -> Cic.Appl (he'::tl')
91 Cic.Meta (m,_) -> CicReduction.head_beta_reduce t'
96 (* incr apply_subst_counter; *)
99 | _ -> apply_subst_gen ~appl_fun subst t
102 let profiler = HExtlib.profile "U/CicMetaSubst.apply_subst"
103 let apply_subst s t =
104 profiler.HExtlib.profile (apply_subst s) t
107 let apply_subst_context subst context =
112 incr apply_subst_context_counter;
113 context_length := !context_length + List.length context;
118 | Some (n, Cic.Decl t) ->
119 let t' = apply_subst subst t in
120 Some (n, Cic.Decl t') :: context
121 | Some (n, Cic.Def (t, ty)) ->
122 let ty' = apply_subst subst ty in
123 let t' = apply_subst subst t in
124 Some (n, Cic.Def (t', ty')) :: context
125 | None -> None :: context)
128 let apply_subst_metasenv subst metasenv =
130 incr apply_subst_metasenv_counter;
131 metasenv_length := !metasenv_length + List.length metasenv;
137 (fun (n, context, ty) ->
138 (n, apply_subst_context subst context, apply_subst subst ty))
140 (fun (i, _, _) -> not (List.mem_assoc i subst))
143 let tempi_type_of_aux_subst = ref 0.0;;
144 let tempi_subst = ref 0.0;;
145 let tempi_type_of_aux = ref 0.0;;
149 let maxmeta = ref 0 in
150 fun () -> incr maxmeta; !maxmeta
153 exception NotInTheList;;
155 let position n (shift, lc) =
157 | NCic.Irl len when n <= shift || n > shift + len -> raise NotInTheList
158 | NCic.Irl _ -> n - shift
160 let rec aux k = function
161 | [] -> raise NotInTheList
162 | (NCic.Rel m)::_ when m + shift = n -> k
163 | _::tl -> aux (k+1) tl
169 let rec are_contiguous k = function
171 | (NCic.Rel j) :: tl when j = k+1 -> are_contiguous j tl
175 | _, NCic.Ctx [] -> 0, NCic.Irl 0
176 | shift, NCic.Ctx (NCic.Rel k::tl as l) when are_contiguous k tl ->
177 shift+k-1, NCic.Irl (List.length l)
182 let mk_restricted_irl shift len restrictions =
185 if List.mem (n+shift) restrictions then aux (n-1)
188 pack_lc (shift, NCic.Irl (aux len))
192 let mk_perforated_irl shift len restrictions =
194 if n = 0 then [] else
195 if List.mem (n+shift) restrictions then aux (n-1)
196 else (NCic.Rel n) :: aux (n-1)
198 pack_lc (shift, NCic.Ctx (List.rev (aux len)))
203 let rec force_does_not_occur metasenv subst restrictions t =
204 let rec aux k ms = function
205 | NCic.Rel r when List.mem (r - k) restrictions -> raise Occur
206 | NCic.Rel r as orig ->
208 List.length (List.filter (fun x -> x < r - k) restrictions)
210 if amount > 0 then ms, NCic.Rel (r - amount) else ms, orig
211 | NCic.Meta (n, l) as orig ->
212 (* we ignore the subst since restrict will take care of already
213 * instantiated/restricted metavariabels *)
214 let (metasenv,subst as ms), restrictions_for_n, l' =
216 | shift, NCic.Irl len ->
219 (fun i -> i > shift && i <= shift + len) restrictions in
220 ms, restrictions, mk_restricted_irl shift len restrictions
221 | shift, NCic.Ctx l ->
222 let ms, _, restrictions_for_n, l =
224 (fun t (ms, i, restrictions_for_n, l) ->
226 let ms, t = aux (k-shift) ms t in
227 ms, i-1, restrictions_for_n, t::l
229 ms, i-1, i::restrictions_for_n, l)
230 l (ms, List.length l, [], [])
232 ms, restrictions_for_n, pack_lc (shift, NCic.Ctx l)
234 if restrictions_for_n = [] then
235 ms, if l = l' then orig else NCic.Meta (n, l')
237 let metasenv, subst, newmeta =
238 restrict metasenv subst n restrictions_for_n
240 (metasenv, subst), NCic.Meta (newmeta, l')
241 | t -> NCicUntrusted.map_term_fold_a (fun _ k -> k+1) k aux ms t
243 aux 0 (metasenv,subst) t
245 and force_does_not_occur_in_context metasenv subst restrictions = function
246 | name, NCic.Decl t as orig ->
247 let (metasenv, subst), t' =
248 force_does_not_occur metasenv subst restrictions t in
249 metasenv, subst, (if t == t' then orig else (name,NCic.Decl t'))
250 | name, NCic.Def (bo, ty) as orig ->
251 let (metasenv, subst), bo' =
252 force_does_not_occur metasenv subst restrictions bo in
253 let (metasenv, subst), ty' =
254 force_does_not_occur metasenv subst restrictions ty in
256 (if bo == bo' && ty == ty' then orig else (name, NCic.Def (bo', ty')))
258 and erase_in_context metasenv subst pos restrictions = function
259 | [] -> metasenv, subst, restrictions, []
261 let metasenv, subst, restricted, tl' =
262 erase_in_context metasenv subst (pos+1) restrictions tl in
263 if List.mem pos restricted then
264 metasenv, subst, restricted, tl'
267 let metasenv, subst, hd' =
268 let delifted_restricted =
269 List.map ((+) ~-pos) (List.filter ((<=) pos) restricted) in
270 force_does_not_occur_in_context
271 metasenv subst delifted_restricted hd
273 metasenv, subst, restricted,
274 (if hd' == hd && tl' == tl then orig else (hd' :: tl'))
276 metasenv, subst, (pos :: restricted), tl'
278 and restrict metasenv subst i restrictions =
279 assert (restrictions <> []);
281 let name, ctx, bo, ty = NCicUtils.lookup_subst i subst in
283 let metasenv, subst, restrictions, newctx =
284 erase_in_context metasenv subst 1 restrictions ctx in
285 let (metasenv, subst), newty =
286 force_does_not_occur metasenv subst restrictions ty in
287 let (metasenv, subst), newbo =
288 force_does_not_occur metasenv subst restrictions bo in
289 let j = newmeta () in
290 let subst_entry_j = j, (name, newctx, newty, newbo) in
291 let reloc_irl = mk_perforated_irl 0 (List.length ctx) restrictions in
292 let subst_entry_i = i, (name, ctx, NCic.Meta (j, reloc_irl), ty) in
294 subst_entry_j :: List.map
295 (fun (n,_) as orig -> if i = n then subst_entry_i else orig) subst,
297 with Occur -> raise (MetaSubstFailure (lazy (Printf.sprintf
298 ("Cannot restrict the context of the metavariable ?%d over "^^
299 "the hypotheses %s since ?%d is already instantiated "^^
300 "with %s and at least one of the hypotheses occurs in "^^
301 "the substituted term") i (String.concat ", "
302 (List.map (fun x -> fst (List.nth ctx (x-1))) restrictions)) i
303 (NCicPp.ppterm ~metasenv ~subst ~context:ctx bo))))
304 with NCicUtils.Subst_not_found _ ->
306 let name, ctx, ty = NCicUtils.lookup_meta i metasenv in
308 let metasenv, subst, restrictions, newctx =
309 erase_in_context metasenv subst 1 restrictions ctx in
310 let (metasenv, subst), newty =
311 force_does_not_occur metasenv subst restrictions ty in
312 let j = newmeta () in
313 let metasenv_entry = j, (name, newctx, newty) in
314 prerr_endline ("restricting ?" ^ string_of_int i ^ " to ?" ^
315 string_of_int j ^ " : " ^ NCicPp.ppterm ~metasenv ~context:newctx
316 ~subst newty ^" in a shorter context:\n" ^
317 NCicPp.ppcontext ~metasenv ~subst newctx);
319 mk_perforated_irl 0 (List.length ctx) restrictions in
320 let subst_entry = i, (name, ctx, NCic.Meta (j, reloc_irl), ty) in
322 (fun (n,_) as orig -> if i = n then metasenv_entry else orig)
324 subst_entry :: subst, j
325 with Occur -> raise (MetaSubstFailure (lazy (Printf.sprintf
326 ("Cannot restrict the context of the metavariable ?%d "^^
327 "over the hypotheses %s since metavariable's type depends "^^
328 "on at least one of them") i (String.concat ", "
329 (List.map (fun x -> fst (List.nth ctx (x-1))) restrictions)))))
331 | NCicUtils.Meta_not_found _ -> assert false
334 (* INVARIANT: we suppose that t is not another occurrence of Meta(n,_),
335 otherwise the occur check does not make sense in case of unification
337 let delift metasenv subst context n l t =
338 let rec aux k (metasenv, subst as ms) = function
339 | NCic.Rel n as t when n <= k -> ms, t
342 match List.nth context (n-k-1) with
343 | _,NCic.Def (bo,_) ->
344 (try ms, NCic.Rel ((position (n-k) l) + k)
346 (* CSC: This bit of reduction hurts performances since it is
347 * possible to have an exponential explosion of the size of the
348 * proof. required for nat/nth_prime.ma *)
349 aux k ms (NCicSubstitution.lift n bo))
350 | _,NCic.Decl _ -> ms, NCic.Rel ((position (n-k) l) + k)
351 with Failure _ -> assert false) (*Unbound variable found in delift*)
352 | NCic.Meta (i,l1) as orig ->
354 let _,_,t,_ = NCicUtils.lookup_subst i subst in
355 aux k ms (NCicSubstitution.subst_meta l1 t)
356 with NCicUtils.Subst_not_found _ ->
357 (* see the top level invariant *)
359 raise (MetaSubstFailure (lazy (Printf.sprintf (
360 "Cannot unify the metavariable ?%d with a term that has "^^
361 "as subterm %s in which the same metavariable "^^
362 "occurs (occur check)") i
363 (NCicPp.ppterm ~context ~metasenv ~subst t))))
365 let shift1,lc1 = l1 in
368 | NCic.Irl len, NCic.Irl len1
369 when shift1 < shift || len1 + shift1 > len + shift ->
371 HExtlib.list_seq 1 (shift - shift1) @
372 HExtlib.list_seq (shift+len+1) (shift1+len1)
374 let metasenv, subst, newmeta =
375 restrict metasenv subst i restrictions
378 NCic.Meta(newmeta, mk_perforated_irl shift1 len1 restrictions)
379 | NCic.Irl _, NCic.Irl _ when shift = 0 -> ms, orig
380 | NCic.Irl _, NCic.Irl _ ->
381 ms, NCic.Meta (i, (shift1 - shift, lc1))
383 let lc1 = NCicUtils.expand_local_context lc1 in
384 let rec deliftl tbr j ms = function
387 let ms, tbr, tl = deliftl tbr (j+1) ms tl in
389 let ms, t = aux (k-shift1) ms t in
392 | NotInTheList | MetaSubstFailure _ -> ms, j::tbr, tl
394 let (metasenv, subst), to_be_r, lc1' = deliftl [] 1 ms lc1 in
395 prerr_endline ("TO BE RESTRICTED: " ^
396 (String.concat "," (List.map string_of_int to_be_r)));
397 let l1 = pack_lc (shift, NCic.Ctx lc1') in
400 (if lc1' = lc1 then orig else NCic.Meta (i,l1))
402 let metasenv, subst, newmeta =
403 restrict metasenv subst i to_be_r in
404 (metasenv, subst), NCic.Meta(newmeta,l1))
405 | t -> NCicUntrusted.map_term_fold_a (fun _ k -> k+1) k aux ms t
407 try aux 0 (metasenv,subst) t
409 (* This is the case where we fail even first order unification. *)
410 (* The reason is that our delift function is weaker than first *)
411 (* order (in the sense of alpha-conversion). See comment above *)
412 (* related to the delift function. *)
413 let msg = (lazy (Printf.sprintf
414 ("Error trying to abstract %s over [%s]: the algorithm only tried to "^^
415 "abstract over bound variables") (NCicPp.ppterm ~metasenv ~subst
416 ~context t) (String.concat "; " (List.map (NCicPp.ppterm ~metasenv
417 ~subst ~context) (let shift, lc = l in List.map (NCicSubstitution.lift
418 shift) (NCicUtils.expand_local_context lc))))))
421 let lc = NCicUtils.expand_local_context lc in
422 let l = List.map (NCicSubstitution.lift shift) lc in
425 (fun t -> NCicUntrusted.metas_of_term subst context t = [])
428 raise (Uncertain msg)
430 raise (MetaSubstFailure msg)
434 (* delifts a term t of n levels strating from k, that is changes (Rel m)
435 * to (Rel (m - n)) when m > (k + n). if k <= m < k + n delift fails
437 let delift_rels_from subst metasenv k n =
438 let rec liftaux subst metasenv k =
439 let module C = Cic in
444 else if m < k + n then
445 raise DeliftingARelWouldCaptureAFreeVariable
447 C.Rel (m - n), subst, metasenv
448 | C.Var (uri,exp_named_subst) ->
449 let exp_named_subst',subst,metasenv =
451 (fun (uri,t) (l,subst,metasenv) ->
452 let t',subst,metasenv = liftaux subst metasenv k t in
453 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
455 C.Var (uri,exp_named_subst'),subst,metasenv
458 let (_, t,_) = lookup_subst i subst in
459 liftaux subst metasenv k (CicSubstitution.subst_meta l t)
460 with CicUtil.Subst_not_found _ ->
461 let l',to_be_restricted,subst,metasenv =
462 let rec aux con l subst metasenv =
464 [] -> [],[],subst,metasenv
466 let tl',to_be_restricted,subst,metasenv =
467 aux (con + 1) tl subst metasenv in
468 let he',more_to_be_restricted,subst,metasenv =
470 None -> None,[],subst,metasenv
473 let t',subst,metasenv = liftaux subst metasenv k t in
474 Some t',[],subst,metasenv
476 DeliftingARelWouldCaptureAFreeVariable ->
477 None,[i,con],subst,metasenv
479 he'::tl',more_to_be_restricted@to_be_restricted,subst,metasenv
481 aux 1 l subst metasenv in
482 let metasenv,subst = restrict subst to_be_restricted metasenv in
483 C.Meta(i,l'),subst,metasenv)
484 | C.Sort _ as t -> t,subst,metasenv
485 | C.Implicit _ as t -> t,subst,metasenv
487 let te',subst,metasenv = liftaux subst metasenv k te in
488 let ty',subst,metasenv = liftaux subst metasenv k ty in
489 C.Cast (te',ty'),subst,metasenv
491 let s',subst,metasenv = liftaux subst metasenv k s in
492 let t',subst,metasenv = liftaux subst metasenv (k+1) t in
493 C.Prod (n,s',t'),subst,metasenv
494 | C.Lambda (n,s,t) ->
495 let s',subst,metasenv = liftaux subst metasenv k s in
496 let t',subst,metasenv = liftaux subst metasenv (k+1) t in
497 C.Lambda (n,s',t'),subst,metasenv
498 | C.LetIn (n,s,ty,t) ->
499 let s',subst,metasenv = liftaux subst metasenv k s in
500 let ty',subst,metasenv = liftaux subst metasenv k ty in
501 let t',subst,metasenv = liftaux subst metasenv (k+1) t in
502 C.LetIn (n,s',ty',t'),subst,metasenv
504 let l',subst,metasenv =
506 (fun t (l,subst,metasenv) ->
507 let t',subst,metasenv = liftaux subst metasenv k t in
508 t'::l,subst,metasenv) l ([],subst,metasenv) in
509 C.Appl l',subst,metasenv
510 | C.Const (uri,exp_named_subst) ->
511 let exp_named_subst',subst,metasenv =
513 (fun (uri,t) (l,subst,metasenv) ->
514 let t',subst,metasenv = liftaux subst metasenv k t in
515 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
517 C.Const (uri,exp_named_subst'),subst,metasenv
518 | C.MutInd (uri,tyno,exp_named_subst) ->
519 let exp_named_subst',subst,metasenv =
521 (fun (uri,t) (l,subst,metasenv) ->
522 let t',subst,metasenv = liftaux subst metasenv k t in
523 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
525 C.MutInd (uri,tyno,exp_named_subst'),subst,metasenv
526 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
527 let exp_named_subst',subst,metasenv =
529 (fun (uri,t) (l,subst,metasenv) ->
530 let t',subst,metasenv = liftaux subst metasenv k t in
531 (uri,t')::l,subst,metasenv) exp_named_subst ([],subst,metasenv)
533 C.MutConstruct (uri,tyno,consno,exp_named_subst'),subst,metasenv
534 | C.MutCase (sp,i,outty,t,pl) ->
535 let outty',subst,metasenv = liftaux subst metasenv k outty in
536 let t',subst,metasenv = liftaux subst metasenv k t in
537 let pl',subst,metasenv =
539 (fun t (l,subst,metasenv) ->
540 let t',subst,metasenv = liftaux subst metasenv k t in
541 t'::l,subst,metasenv) pl ([],subst,metasenv)
543 C.MutCase (sp,i,outty',t',pl'),subst,metasenv
545 let len = List.length fl in
546 let liftedfl,subst,metasenv =
548 (fun (name, i, ty, bo) (l,subst,metasenv) ->
549 let ty',subst,metasenv = liftaux subst metasenv k ty in
550 let bo',subst,metasenv = liftaux subst metasenv (k+len) bo in
551 (name,i,ty',bo')::l,subst,metasenv
552 ) fl ([],subst,metasenv)
554 C.Fix (i, liftedfl),subst,metasenv
556 let len = List.length fl in
557 let liftedfl,subst,metasenv =
559 (fun (name, ty, bo) (l,subst,metasenv) ->
560 let ty',subst,metasenv = liftaux subst metasenv k ty in
561 let bo',subst,metasenv = liftaux subst metasenv (k+len) bo in
562 (name,ty',bo')::l,subst,metasenv
563 ) fl ([],subst,metasenv)
565 C.CoFix (i, liftedfl),subst,metasenv
567 liftaux subst metasenv k
569 let delift_rels subst metasenv n t =
570 delift_rels_from subst metasenv 1 n t
573 let mk_meta ?name metasenv context ty =
576 let len = List.length context in
577 let n = newmeta () in
578 let ty_menv_entry = (n, (name, context, NCic.Implicit (`Typeof n))) in
579 let m = newmeta () in
580 let ty = NCic.Meta (n, (0,NCic.Irl len)) in
581 let menv_entry = (m, (name, context, ty)) in
582 menv_entry :: ty_menv_entry :: metasenv, NCic.Meta (m, (0,NCic.Irl len)), ty
584 let n = newmeta () in
585 let len = List.length context in
586 let menv_entry = (n, (name, context, ty)) in
587 menv_entry :: metasenv, NCic.Meta (n, (0,NCic.Irl len)), ty
590 let saturate ?(delta=0) metasenv context ty goal_arity =
591 assert (goal_arity >= 0);
592 let rec aux metasenv = function
593 | NCic.Prod (name,s,t) ->
594 let metasenv1, arg,_ = mk_meta ~name:name metasenv context (Some s) in
595 let t, metasenv1, args, pno =
596 aux metasenv1 (NCicSubstitution.subst arg t)
598 if pno + 1 = goal_arity then
599 ty, metasenv, [], goal_arity+1
601 t, metasenv1, arg::args, pno+1
603 match NCicReduction.whd context ty ~delta with
604 | NCic.Prod _ as ty -> aux metasenv ty
605 | ty -> ty, metasenv, [], 0
607 let res, newmetasenv, arguments, _ = aux metasenv ty in
608 res, newmetasenv, arguments