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 let maxmeta = ref 0 in
19 (fun () -> incr maxmeta; !maxmeta),
23 exception NotInTheList;;
25 let position to_skip n (shift, lc) =
27 | NCic.Irl _ when to_skip > 0 -> assert false (* unclear to me *)
28 | NCic.Irl len when n <= shift || n > shift + len -> raise NotInTheList
29 | NCic.Irl _ -> n - shift
31 let rec aux to_skip k = function
32 | [] -> raise NotInTheList
33 | _ :: tl when to_skip > 0 -> aux (to_skip - 1) (k+1) tl
34 | (NCic.Rel m)::_ when m + shift = n -> k
35 | _::tl -> aux to_skip (k+1) tl
41 let rec are_contiguous k = function
43 | (NCic.Rel j) :: tl when j = k+1 -> are_contiguous j tl
47 | _, NCic.Ctx [] -> 0, NCic.Irl 0
48 | shift, NCic.Ctx (NCic.Rel k::tl as l) when are_contiguous k tl ->
49 shift+k-1, NCic.Irl (List.length l)
55 let mk_perforated_irl shift len restrictions =
58 if List.mem (n+shift) restrictions then aux (n-1)
59 else (NCic.Rel n) :: aux (n-1)
61 pack_lc (shift, NCic.Ctx (List.rev (aux len)))
66 let rec force_does_not_occur metasenv subst restrictions t =
67 let rec aux k ms = function
68 | NCic.Rel r when List.mem (r - k) restrictions -> raise Occur
69 | NCic.Rel r as orig ->
71 List.length (List.filter (fun x -> x < r - k) restrictions)
73 if amount > 0 then ms, NCic.Rel (r - amount) else ms, orig
74 | NCic.Meta (n, (shift,lc as l)) as orig ->
77 Some (NCicUtils.lookup_subst n subst)
79 NCicUtils.Subst_not_found _ -> None
81 (match meta_chain with
83 aux k ms (NCicSubstitution.subst_meta l bo)
85 (* we ignore the subst since restrict will take care of already
86 * instantiated/restricted metavariabels *)
87 let (metasenv,subst as ms), restrictions_for_n, l' =
88 let l = NCicUtils.expand_local_context lc in
90 let ms, _, restrictions_for_n, l =
92 (fun t (ms, i, restrictions_for_n, l) ->
94 let ms, t = aux (k-shift) ms t in
95 ms, i-1, restrictions_for_n, t::l
97 ms, i-1, i::restrictions_for_n, l)
98 l (ms, List.length l, [], [])
101 ms, restrictions_for_n, pack_lc (shift, NCic.Ctx l)
103 if restrictions_for_n = [] then
104 ms, if l = l' then orig else NCic.Meta (n, l')
106 let metasenv, subst, newmeta =
107 restrict metasenv subst n restrictions_for_n
109 (metasenv, subst), NCic.Meta (newmeta, l'))
110 | t -> NCicUntrusted.map_term_fold_a (fun _ k -> k+1) k aux ms t
112 aux 0 (metasenv,subst) t
114 and force_does_not_occur_in_context metasenv subst restrictions = function
115 | name, NCic.Decl t as orig ->
116 let (metasenv, subst), t' =
117 force_does_not_occur metasenv subst restrictions t in
118 metasenv, subst, (if t == t' then orig else (name,NCic.Decl t'))
119 | name, NCic.Def (bo, ty) as orig ->
120 let (metasenv, subst), bo' =
121 force_does_not_occur metasenv subst restrictions bo in
122 let (metasenv, subst), ty' =
123 force_does_not_occur metasenv subst restrictions ty in
125 (if bo == bo' && ty == ty' then orig else (name, NCic.Def (bo', ty')))
127 and erase_in_context metasenv subst pos restrictions = function
128 | [] -> metasenv, subst, restrictions, []
130 let metasenv, subst, restricted, tl' =
131 erase_in_context metasenv subst (pos+1) restrictions tl in
132 if List.mem pos restricted then
133 metasenv, subst, restricted, tl'
136 let metasenv, subst, hd' =
137 let delifted_restricted =
138 List.map ((+) ~-pos) (List.filter ((<=) pos) restricted) in
139 force_does_not_occur_in_context
140 metasenv subst delifted_restricted hd
142 metasenv, subst, restricted,
143 (if hd' == hd && tl' == tl then orig else (hd' :: tl'))
145 metasenv, subst, (pos :: restricted), tl'
147 and restrict metasenv subst i restrictions =
148 assert (restrictions <> []);
150 let name, ctx, bo, ty = NCicUtils.lookup_subst i subst in
152 let metasenv, subst, restrictions, newctx =
153 erase_in_context metasenv subst 1 restrictions ctx in
154 let (metasenv, subst), newty =
155 force_does_not_occur metasenv subst restrictions ty in
156 let (metasenv, subst), newbo =
157 force_does_not_occur metasenv subst restrictions bo in
158 let j = newmeta () in
159 let subst_entry_j = j, (name, newctx, newbo, newty) in
160 let reloc_irl = mk_perforated_irl 0 (List.length ctx) restrictions in
161 let subst_entry_i = i, (name, ctx, NCic.Meta (j, reloc_irl), ty) in
163 subst_entry_j :: List.map
164 (fun (n,_) as orig -> if i = n then subst_entry_i else orig) subst
167 prerr_endline ("restringo nella subst: " ^string_of_int i ^ " -> " ^
168 string_of_int j ^ "\n" ^
169 NCicPp.ppsubst ~metasenv [subst_entry_j] ^ "\n\n" ^
170 NCicPp.ppsubst ~metasenv [subst_entry_i] ^ "\n" ^
171 NCicPp.ppterm ~metasenv ~subst ~context:ctx bo ^ " ---- " ^
172 NCicPp.ppterm ~metasenv ~subst ~context:newctx newbo
175 metasenv, new_subst, j
176 with Occur -> raise (MetaSubstFailure (lazy (Printf.sprintf
177 ("Cannot restrict the context of the metavariable ?%d over "^^
178 "the hypotheses %s since ?%d is already instantiated "^^
179 "with %s and at least one of the hypotheses occurs in "^^
180 "the substituted term") i (String.concat ", "
181 (List.map (fun x -> fst (List.nth ctx (x-1))) restrictions)) i
182 (NCicPp.ppterm ~metasenv ~subst ~context:ctx bo))))
183 with NCicUtils.Subst_not_found _ ->
185 let name, ctx, ty = NCicUtils.lookup_meta i metasenv in
187 let metasenv, subst, restrictions, newctx =
188 erase_in_context metasenv subst 1 restrictions ctx in
189 let (metasenv, subst), newty =
190 force_does_not_occur metasenv subst restrictions ty in
191 let j = newmeta () in
192 let metasenv_entry = j, (name, newctx, newty) in
194 mk_perforated_irl 0 (List.length ctx) restrictions in
195 let subst_entry = i, (name, ctx, NCic.Meta (j, reloc_irl), ty) in
197 (fun (n,_) as orig -> if i = n then metasenv_entry else orig)
199 subst_entry :: subst, j
200 with Occur -> raise (MetaSubstFailure (lazy (Printf.sprintf
201 ("Cannot restrict the context of the metavariable ?%d "^^
202 "over the hypotheses %s since metavariable's type depends "^^
203 "on at least one of them") i (String.concat ", "
204 (List.map (fun x -> fst (List.nth ctx (x-1))) restrictions)))))
206 | NCicUtils.Meta_not_found _ -> assert false
209 let rec flexible_arg context subst = function
212 let _,_,t,_ = List.assoc i subst in
213 flexible_arg context subst t
214 with Not_found -> true)
215 | NCic.Appl (NCic.Meta (i,_) :: args)->
217 let _,_,t,_ = List.assoc i subst in
218 flexible_arg context subst
219 (NCicReduction.head_beta_reduce ~delta:max_int
220 (NCic.Appl (t :: args)))
221 with Not_found -> true)
222 (* this is a cheap whd, it only performs zeta-reduction.
224 * it works when the **omissis** disambiguation algorithm
225 * is run on `let x := K a b in t`, K is substituted for a
226 * ? and thus in t metavariables have a flexible Rel
230 match List.nth context (i-1)
232 | _,NCic.Def (bo,_) ->
233 flexible_arg context subst
234 (NCicSubstitution.lift i bo)
238 prerr_endline (Printf.sprintf "Rel %d inside context:\n%s" i
239 (NCicPp.ppcontext ~subst ~metasenv:[] context));
241 | Invalid_argument _ -> assert false)
245 let is_out_scope = function `OutScope _ -> true | _ -> false;;
246 let is_out_scope_tag = List.exists is_out_scope;;
247 let int_of_out_scope_tag tag =
248 match List.filter is_out_scope tag with [`OutScope n] -> n | _ -> assert false
254 (* INVARIANT: we suppose that t is not another occurrence of Meta(n,_),
255 otherwise the occur check does not make sense in case of unification
257 let delift ~unify metasenv subst context n l t =
258 let is_in_scope_meta subst = function
261 let tag, _, _, _ = NCicUtils.lookup_subst i subst in
262 List.mem `InScope tag
263 with NCicUtils.Subst_not_found _ -> false)
266 let contains_in_scope subst t =
267 let rec aux _ () = function
268 | NCic.Meta _ as t ->
269 if is_in_scope_meta subst t then raise Found
272 if is_in_scope_meta subst t then raise Found
273 else NCicUtils.fold (fun _ () -> ()) () aux () t
275 try aux () () t; false with Found -> true
277 let unify_list in_scope =
279 | _, NCic.Irl _ -> fun _ _ _ _ _ -> None
280 | shift, NCic.Ctx l -> fun metasenv subst context k t ->
281 if flexible_arg context subst t || contains_in_scope subst t then None else
284 let t = NCicSubstitution.lift (k+shift) t in
285 t, flexible_arg context subst t)
289 (fun (li,flexible) i ->
290 if flexible || i < in_scope then None else
291 match unify metasenv subst context li t with
292 | Some (metasenv,subst) ->
293 Some ((metasenv, subst), NCic.Rel (i+1+k))
297 let rec aux (context,k,in_scope) (metasenv, subst as ms) t =
298 match unify_list in_scope metasenv subst context k t with
302 | NCic.Rel n as t when n <= k -> ms, t
305 match List.nth context (n-1) with
306 | _,NCic.Def (bo,_) ->
307 (try ms, NCic.Rel ((position in_scope (n-k) l) + k)
309 (* CSC: This bit of reduction hurts performances since it is
310 * possible to have an exponential explosion of the size of the
311 * proof. required for nat/nth_prime.ma *)
312 aux (context,k,in_scope) ms (NCicSubstitution.lift n bo))
313 | _,NCic.Decl _ -> ms, NCic.Rel ((position in_scope (n-k) l) + k)
314 with Failure _ -> assert false) (*Unbound variable found in delift*)
315 | NCic.Meta (i,_) when i=n ->
316 raise (MetaSubstFailure (lazy (Printf.sprintf (
317 "Cannot unify the metavariable ?%d with a term that has "^^
318 "as subterm %s in which the same metavariable "^^
319 "occurs (occur check)") i
320 (NCicPp.ppterm ~context ~metasenv ~subst t))))
321 | NCic.Meta (i,l1) as orig ->
323 let tag,c,t,ty = NCicUtils.lookup_subst i subst in
324 let in_scope, clear =
325 if List.mem `InScope tag then 0, true
326 else if is_out_scope_tag tag then int_of_out_scope_tag tag,true
333 (i,([],c,t,ty)) :: List.filter (fun j,_ -> i <> j) subst
335 aux (context,k,in_scope) ms (NCicSubstitution.subst_meta l1 t)
336 with NCicUtils.Subst_not_found _ ->
337 if snd l1 = NCic.Irl 0 || snd l1 = NCic.Ctx [] then ms, orig
339 let shift1,lc1 = l1 in
341 let shift = shift + k in
343 | NCic.Irl len, NCic.Irl len1
344 when shift1 + len1 < shift || shift1 > shift + len ->
345 let restrictions = HExtlib.list_seq 1 (len1 + 1) in
346 let metasenv, subst, newmeta =
347 restrict metasenv subst i restrictions
350 NCic.Meta (newmeta, (0,NCic.Irl (max 0 (k-shift1))))
351 | NCic.Irl len, NCic.Irl len1 ->
352 let low_restrictions, new_shift =
353 if k <= shift1 && shift1 < shift then
354 HExtlib.list_seq 1 (shift - shift1 + 1), k
355 else if shift1 < k (* <= shift *) then
356 let save_below = k - shift1 in
357 HExtlib.list_seq (save_below + 1) (shift - shift1 + 1),
359 else [], shift1 - shift + k
361 let high_restrictions =
362 let last = shift + len in
363 let last1 = shift1 + len1 in
365 let high_gap = last1 - last in
366 HExtlib.list_seq (len1 - high_gap + 1) (len1 + 1)
369 let restrictions = low_restrictions @ high_restrictions in
370 if restrictions = [] then
371 if shift = k then ms, orig
372 else ms, NCic.Meta (i, (new_shift, lc1))
374 let metasenv, subst, newmeta =
375 restrict metasenv subst i restrictions
378 prerr_endline ("RESTRICTIONS FOR: " ^
379 NCicPp.ppterm ~metasenv ~subst ~context:[]
380 (NCic.Meta (i,l1))^" that was part of a term unified with "
381 ^ NCicPp.ppterm ~metasenv ~subst ~context:[] (NCic.Meta
382 (n,l)) ^ " ====> " ^ String.concat "," (List.map
383 string_of_int restrictions) ^ "\nMENV:\n" ^
384 NCicPp.ppmetasenv ~subst metasenv ^ "\nSUBST:\n" ^
385 NCicPp.ppsubst subst ~metasenv);
387 let newlc_len = len1 - List.length restrictions in
389 NCic.Meta(newmeta,(new_shift, NCic.Irl newlc_len))
392 let _, cctx, _ = NCicUtils.lookup_meta newmeta metasenv in
393 List.length cctx = newlc_len);
394 (metasenv, subst), meta
397 let lc1 = NCicUtils.expand_local_context lc1 in
398 let lc1 = List.map (NCicSubstitution.lift shift1) lc1 in
399 let rec deliftl tbr j ms = function
402 let ms, tbr, tl = deliftl tbr (j+1) ms tl in
404 let ms, t = aux (context,k,in_scope) ms t in
407 | NotInTheList | MetaSubstFailure _ -> ms, j::tbr, tl
409 let (metasenv, subst), to_be_r, lc1' = deliftl [] 1 ms lc1 in
411 prerr_endline ("TO BE RESTRICTED: " ^
412 (String.concat "," (List.map string_of_int to_be_r)));
414 let l1 = pack_lc (0, NCic.Ctx lc1') in
416 prerr_endline ("newmeta:" ^ NCicPp.ppterm
417 ~metasenv ~subst ~context (NCic.Meta (999,l1)));
421 (if lc1' = lc1 then orig else NCic.Meta (i,l1))
423 let metasenv, subst, newmeta =
424 restrict metasenv subst i to_be_r in
425 (metasenv, subst), NCic.Meta(newmeta,l1))
428 NCicUntrusted.map_term_fold_a
429 (fun e (c,k,s) -> (e::c,k+1,s)) (context,k,in_scope) aux ms t
433 "DELIFTO " ^ NCicPp.ppterm ~metasenv ~subst ~context t ^ " w.r.t. " ^
434 String.concat ", " (List.map (NCicPp.ppterm ~metasenv ~subst ~context) (
436 (List.map (NCicSubstitution.lift shift)
437 (NCicUtils.expand_local_context lc))
440 try aux (context,0,0) (metasenv,subst) t
442 (* This is the case where we fail even first order unification. *)
443 (* The reason is that our delift function is weaker than first *)
444 (* order (in the sense of alpha-conversion). See comment above *)
445 (* related to the delift function. *)
446 let msg = (lazy (Printf.sprintf
447 ("Error trying to abstract %s over [%s]: the algorithm only tried to "^^
448 "abstract over bound variables") (NCicPp.ppterm ~metasenv ~subst
449 ~context t) (String.concat "; " (List.map (NCicPp.ppterm ~metasenv
450 ~subst ~context) (let shift, lc = l in List.map (NCicSubstitution.lift
451 shift) (NCicUtils.expand_local_context lc))))))
454 let lc = NCicUtils.expand_local_context lc in
455 let l = List.map (NCicSubstitution.lift shift) lc in
457 List.exists (fun t-> NCicUntrusted.metas_of_term subst context t <> [])l
459 raise (Uncertain msg)
461 raise (MetaSubstFailure msg)
464 let mk_meta ?(attrs=[]) metasenv context ty =
465 let tyof = List.map (function `Name s -> `Name ("typeof_"^s) | x -> x) in
466 let rec mk_meta attrs n metasenv context = function
468 let len = List.length context in
469 let menv_entry = (n, (attrs, context, ty)) in
470 menv_entry :: metasenv, n, NCic.Meta (n, (0,NCic.Irl len)), ty
472 let ty = NCic.Implicit (`Typeof n) in
473 mk_meta (`IsSort::tyof attrs) n metasenv [] (`WithType ty)
475 let metasenv, _, ty, _ =
476 mk_meta (tyof attrs) (newmeta ()) metasenv context `Sort in
477 mk_meta attrs n metasenv context (`WithType ty)
479 let metasenv, _, ty, _ =
480 mk_meta (tyof attrs) (newmeta ()) metasenv context `Type in
481 mk_meta attrs n metasenv context (`WithType ty)
483 mk_meta attrs (newmeta ()) metasenv context ty
486 let saturate ?(delta=0) metasenv subst context ty goal_arity =
487 assert (goal_arity >= 0);
488 let rec aux metasenv = function
489 | NCic.Prod (name,s,t) as ty ->
490 let metasenv1, _, arg,_ =
491 mk_meta ~attrs:[`Name name] metasenv context (`WithType s) in
492 let t, metasenv1, args, pno =
493 aux metasenv1 (NCicSubstitution.subst arg t)
495 if pno + 1 = goal_arity then
496 ty, metasenv, [], goal_arity+1
498 t, metasenv1, arg::args, pno+1
500 match NCicReduction.whd ~subst context ty ~delta with
501 | NCic.Prod _ as ty -> aux metasenv ty
502 | ty -> ty, metasenv, [], 0
504 let res, newmetasenv, arguments, _ = aux metasenv ty in
505 res, newmetasenv, arguments