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 UnificationFailure of string Lazy.t;;
15 exception Uncertain of string Lazy.t;;
16 exception AssertFailure of string Lazy.t;;
19 ref (fun _ ?localise _ _ _ _ _ -> ignore localise; assert false);;
20 let set_refiner_typeof f = refiner_typeof := f
23 let (===) x y = Pervasives.compare x y = 0 ;;
25 let uncert_exc metasenv subst context t1 t2 =
27 "Can't unify " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^
28 " with " ^ NCicPp.ppterm ~metasenv ~subst ~context t2))
31 let fail_exc metasenv subst context t1 t2 =
32 UnificationFailure (lazy (
33 "Can't unify " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^
34 " with " ^ NCicPp.ppterm ~metasenv ~subst ~context t2));
37 let mk_appl ~upto hd tl =
38 NCicReduction.head_beta_reduce ~upto
40 | NCic.Appl l -> NCic.Appl (l@tl)
41 | _ -> NCic.Appl (hd :: tl))
44 exception WrongShape;;
46 let eta_reduce subst t =
47 let delift_if_not_occur body =
49 Some (NCicSubstitution.psubst ~avoid_beta_redexes:true
50 (fun () -> raise WrongShape) [()] body)
51 with WrongShape -> None
53 let rec eat_lambdas ctx = function
54 | NCic.Lambda (name, src, tgt) ->
55 eat_lambdas ((name, src) :: ctx) tgt
56 | NCic.Meta (i,lc) as t->
58 let _,_,t,_ = NCicUtils.lookup_subst i subst in
59 let t = NCicSubstitution.subst_meta lc t in
61 with Not_found -> ctx, t)
64 let context_body = eat_lambdas [] t in
65 let rec aux = function
67 | (name, src)::ctx, (NCic.Appl (hd::[NCic.Rel 1]) as bo) ->
68 (match delift_if_not_occur hd with
69 | None -> aux (ctx,NCic.Lambda(name,src, bo))
70 | Some bo -> aux (ctx,bo))
71 | (name, src)::ctx, (NCic.Appl args as bo)
72 when HExtlib.list_last args = NCic.Rel 1 ->
73 let args, _ = HExtlib.split_nth (List.length args - 1) args in
74 (match delift_if_not_occur (NCic.Appl args) with
75 | None -> aux (ctx,NCic.Lambda(name,src, bo))
76 | Some bo -> aux (ctx,bo))
77 | (name, src) :: ctx, t ->
78 aux (ctx,NCic.Lambda(name,src, t))
84 module Ref = NReference;;
87 let inside c = indent := !indent ^ String.make 1 c;;
90 indent := String.sub !indent 0 (String.length !indent -1)
92 Invalid_argument _ -> indent := "??"; ()
96 prerr_endline (Printf.sprintf "%-20s" !indent ^ " " ^ Lazy.force s)
101 let ppcontext ~metasenv ~subst c =
102 "\nctx:\n"^ NCicPp.ppcontext ~metasenv ~subst c
104 let ppmetasenv ~subst m = "\nmenv:\n" ^ NCicPp.ppmetasenv ~subst m;;
106 let ppcontext ~metasenv:_metasenv ~subst:_subst _context = "";;
107 let ppmetasenv ~subst:_subst _metasenv = "";;
109 let fix_sorts swap exc t =
110 let rec aux () = function
111 | NCic.Sort (NCic.Type u) as orig ->
113 match NCicEnvironment.sup u with
115 prerr_endline ("no sup for " ^ NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] orig) ;
117 | Some u1 -> if u = u1 then orig else NCic.Sort (NCic.Type u1)
119 NCic.Sort (NCic.Type (
120 match NCicEnvironment.sup NCicEnvironment.type0 with
122 | _ -> assert false))
123 | NCic.Meta _ as orig -> orig
124 | t -> NCicUtils.map (fun _ _ -> ()) () aux t
129 let is_locked n subst =
131 match NCicUtils.lookup_subst n subst with
132 | Some tag, _,_,_ when NCicMetaSubst.is_out_scope_tag tag -> true
134 with NCicUtils.Subst_not_found _ -> false
140 | n -> NCic.Rel n :: mk_irl (n-1)
143 let rec lambda_intros rdb metasenv subst context t args =
144 let tty = NCicTypeChecker.typeof ~metasenv ~subst context t in
147 (fun arg -> arg, NCicTypeChecker.typeof ~metasenv ~subst context arg) args in
148 let context_of_args = context in
149 let rec mk_lambda metasenv subst context n processed_args = function
151 let metasenv, _, bo, _ =
152 NCicMetaSubst.mk_meta metasenv context
153 (`WithType (NCicSubstitution.lift n tty))
158 NCicPp.ppterm ~metasenv ~subst ~context:context_of_args arg ^ ":" ^
159 NCicPp.ppterm ~metasenv ~subst ~context:context_of_args ty));
160 let metasenv, subst, telescopic_ty =
161 if processed_args = [] then metasenv, subst, ty else
162 let _ = pp(lazy("delift")) in
163 delift_type_wrt_terms rdb metasenv subst context_of_args ty
164 (List.rev processed_args)
167 let name = "HBeta"^string_of_int n in
168 let metasenv, subst, bo =
169 mk_lambda metasenv subst ((name,NCic.Decl telescopic_ty)::context) (n+1)
170 (arg::processed_args) tail
172 metasenv, subst, NCic.Lambda (name, telescopic_ty, bo)
174 pp(lazy("LAMBDA_INTROS{ " ^
175 NCicPp.ppterm ~metasenv ~subst ~context t ^ ":" ^
176 NCicPp.ppterm ~metasenv ~subst ~context tty ^ " over " ^
177 String.concat "," (List.map (NCicPp.ppterm ~metasenv ~subst ~context)args)));
178 let rc = mk_lambda metasenv subst context 0 [] argsty in
179 pp(lazy("LAMBDA_INTROS}"));
182 and instantiate rdb test_eq_only metasenv subst context n lc t swap =
183 (*D*) inside 'I'; try let rc =
184 pp (lazy(string_of_int n ^ " :=?= "^
185 NCicPp.ppterm ~metasenv ~subst ~context t));
186 let unify test_eq_only m s c t1 t2 =
187 if swap then unify rdb test_eq_only m s c t2 t1
188 else unify rdb test_eq_only m s c t1 t2
190 let name, ctx, ty = NCicUtils.lookup_meta n metasenv in
191 let metasenv, subst, t =
193 | NCic.Implicit (`Typeof _) ->
195 (* fix_sorts swap metasenv subst context (NCic.Meta(n,lc)) t *)
198 "typeof: " ^ NCicPp.ppterm ~metasenv ~subst ~context t ^
199 ppcontext ~metasenv ~subst context ^
200 ppmetasenv ~subst metasenv));
201 let exc_to_be = fail_exc metasenv subst context (NCic.Meta (n,lc)) t in
203 try t, NCicTypeChecker.typeof ~subst ~metasenv context t
205 | NCicTypeChecker.AssertFailure msg ->
206 (pp (lazy "fine typeof (fallimento)");
207 let ft = fix_sorts swap exc_to_be t in
209 (prerr_endline ( ("ILLTYPED: " ^
210 NCicPp.ppterm ~metasenv ~subst ~context t
211 ^ "\nBECAUSE:" ^ Lazy.force msg ^
212 ppcontext ~metasenv ~subst context ^
213 ppmetasenv ~subst metasenv
218 pp (lazy ("typeof: " ^
219 NCicPp.ppterm ~metasenv ~subst ~context ft));
220 ft, NCicTypeChecker.typeof ~subst ~metasenv context ft
221 with NCicTypeChecker.AssertFailure _ ->
223 | NCicTypeChecker.TypeCheckerFailure msg ->
224 prerr_endline (Lazy.force msg);
227 let lty = NCicSubstitution.subst_meta lc ty in
230 raise (UnificationFailure
231 (lazy "trying to unify a term with a type"))
233 pp (lazy ("On the types: " ^
234 NCicPp.ppterm ~metasenv ~subst ~context:ctx ty ^ " ~~~ " ^
235 NCicPp.ppterm ~metasenv ~subst ~context lty ^ " === "
236 ^ NCicPp.ppterm ~metasenv ~subst ~context ty_t));
239 unify test_eq_only metasenv subst context lty ty_t
240 with NCicEnvironment.BadConstraint _ as exc ->
241 let ty_t = fix_sorts swap exc_to_be ty_t in
242 try unify test_eq_only metasenv subst context lty ty_t
243 with _ -> raise exc in
246 pp (lazy(string_of_int n ^ " := 111 = "^
247 NCicPp.ppterm ~metasenv ~subst ~context t));
248 let (metasenv, subst), t =
251 ~unify:(fun m s c t1 t2 ->
254 try Some (unify test_eq_only m s c t1 t2 )
255 with UnificationFailure _ | Uncertain _ -> None
258 metasenv subst context n lc t
259 with NCicMetaSubst.Uncertain msg ->
260 pp (lazy ("delift fails: " ^ Lazy.force msg));
261 raise (Uncertain msg)
262 | NCicMetaSubst.MetaSubstFailure msg ->
263 pp (lazy ("delift fails: " ^ Lazy.force msg));
264 raise (UnificationFailure msg)
266 pp (lazy(string_of_int n ^ " := 222 = "^
267 NCicPp.ppterm ~metasenv ~subst ~context:ctx t
268 ^ ppmetasenv ~subst metasenv));
269 (* Unifying the types may have already instantiated n. *)
271 let _, _,oldt,_ = NCicUtils.lookup_subst n subst in
272 let oldt = NCicSubstitution.subst_meta lc oldt in
273 let t = NCicSubstitution.subst_meta lc t in
274 (* conjecture: always fail --> occur check *)
275 unify test_eq_only metasenv subst context oldt t
276 with NCicUtils.Subst_not_found _ ->
277 (* by cumulativity when unify(?,Type_i)
278 * we could ? := Type_j with j <= i... *)
279 let subst = (n, (name, ctx, t, ty)) :: subst in
280 pp (lazy ("?"^string_of_int n^" := "^NCicPp.ppterm
281 ~metasenv ~subst ~context (NCicSubstitution.subst_meta lc t)));
283 List.filter (fun (m,_) -> not (n = m)) metasenv
286 (*D*) in outside(); rc with exn -> outside (); raise exn
288 and unify rdb test_eq_only metasenv subst context t1 t2 =
289 (*D*) inside 'U'; try let rc =
290 let fo_unif test_eq_only metasenv subst t1 t2 =
291 (*D*) inside 'F'; try let rc =
292 pp (lazy(" " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " ==?== " ^
293 NCicPp.ppterm ~metasenv ~subst ~context t2 ^ ppmetasenv
299 | C.Appl [_], _ | _, C.Appl [_] | C.Appl [], _ | _, C.Appl []
300 | C.Appl (C.Appl _::_), _ | _, C.Appl (C.Appl _::_) ->
301 prerr_endline "Appl [Appl _;_] or Appl [] or Appl [_] invariant";
303 | (C.Sort (C.Type a), C.Sort (C.Type b)) when not test_eq_only ->
304 if NCicEnvironment.universe_leq a b then metasenv, subst
305 else raise (fail_exc metasenv subst context t1 t2)
306 | (C.Sort (C.Type a), C.Sort (C.Type b)) ->
307 if NCicEnvironment.universe_eq a b then metasenv, subst
308 else raise (fail_exc metasenv subst context t1 t2)
309 | (C.Sort C.Prop,C.Sort (C.Type _)) ->
310 if (not test_eq_only) then metasenv, subst
311 else raise (fail_exc metasenv subst context t1 t2)
313 | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2))
314 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
315 let metasenv, subst = unify rdb true metasenv subst context s1 s2 in
316 unify rdb test_eq_only metasenv subst ((name1, C.Decl s1)::context) t1 t2
317 | (C.LetIn (name1,ty1,s1,t1), C.LetIn(_,ty2,s2,t2)) ->
318 let metasenv,subst=unify rdb test_eq_only metasenv subst context ty1 ty2 in
319 let metasenv,subst=unify rdb test_eq_only metasenv subst context s1 s2 in
320 let context = (name1, C.Def (s1,ty1))::context in
321 unify rdb test_eq_only metasenv subst context t1 t2
323 | (C.Meta (n1,(s1,l1 as lc1)),C.Meta (n2,(s2,l2 as lc2))) when n1 = n2 ->
325 let l1 = NCicUtils.expand_local_context l1 in
326 let l2 = NCicUtils.expand_local_context l2 in
327 let metasenv, subst, to_restrict, _ =
329 (fun t1 t2 (metasenv, subst, to_restrict, i) ->
331 let metasenv, subst =
332 unify rdb test_eq_only metasenv subst context
333 (NCicSubstitution.lift s1 t1) (NCicSubstitution.lift s2 t2)
335 metasenv, subst, to_restrict, i-1
336 with UnificationFailure _ | Uncertain _ ->
337 metasenv, subst, i::to_restrict, i-1)
338 l1 l2 (metasenv, subst, [], List.length l1)
340 if to_restrict <> [] then
341 let metasenv, subst, _ =
342 NCicMetaSubst.restrict metasenv subst n1 to_restrict
347 | Invalid_argument _ -> assert false
348 | NCicMetaSubst.MetaSubstFailure msg ->
350 let _,_,term,_ = NCicUtils.lookup_subst n1 subst in
351 let term1 = NCicSubstitution.subst_meta lc1 term in
352 let term2 = NCicSubstitution.subst_meta lc2 term in
353 unify rdb test_eq_only metasenv subst context term1 term2
354 with NCicUtils.Subst_not_found _-> raise (UnificationFailure msg))
356 | _, NCic.Meta (n, _) when is_locked n subst ->
357 (let (metasenv, subst), i =
358 match NCicReduction.whd ~subst context t1 with
359 | NCic.Appl (NCic.Meta (i,l)::args) ->
360 let metasenv, subst, lambda_Mj =
361 lambda_intros rdb metasenv subst context t1 args
363 unify rdb test_eq_only metasenv subst context
364 (C.Meta (i,l)) lambda_Mj,
366 | NCic.Meta (i,_) -> (metasenv, subst), i
368 raise (UnificationFailure (lazy "Locked term vs non
369 flexible term; probably not saturated enough yet!"))
371 let t1 = NCicReduction.whd ~subst context t1 in
373 match t1 with NCic.Meta (j,l) -> j, l | _ -> assert false
375 let metasenv, subst =
376 instantiate rdb test_eq_only metasenv subst context j lj t2 true
378 (* We need to remove the out_scope_tags to avoid propagation of
379 them that triggers again the ad-hoc case *)
381 List.map (fun (i,(tag,ctx,bo,ty)) ->
385 tag = NCicMetaSubst.in_scope_tag
386 || NCicMetaSubst.is_out_scope_tag tag -> None
393 let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
394 let term = eta_reduce subst term in
395 let subst = List.filter (fun (j,_) -> j <> i) subst in
396 metasenv, ((i, (name, ctx, term, ty)) :: subst)
397 with Not_found -> assert false))
399 | C.Meta (n,lc), t ->
401 let _,_,term,_ = NCicUtils.lookup_subst n subst in
402 let term = NCicSubstitution.subst_meta lc term in
403 unify rdb test_eq_only metasenv subst context term t
404 with NCicUtils.Subst_not_found _->
405 instantiate rdb test_eq_only metasenv subst context n lc
406 (NCicReduction.head_beta_reduce ~subst t) false)
408 | t, C.Meta (n,lc) ->
410 let _,_,term,_ = NCicUtils.lookup_subst n subst in
411 let term = NCicSubstitution.subst_meta lc term in
412 unify rdb test_eq_only metasenv subst context t term
413 with NCicUtils.Subst_not_found _->
414 instantiate rdb test_eq_only metasenv subst context n lc
415 (NCicReduction.head_beta_reduce ~subst t) true)
417 | NCic.Appl (NCic.Meta (i,l)::args), _ when List.mem_assoc i subst ->
418 let _,_,term,_ = NCicUtils.lookup_subst i subst in
419 let term = NCicSubstitution.subst_meta l term in
420 unify rdb test_eq_only metasenv subst context
421 (mk_appl ~upto:(List.length args) term args) t2
423 | _, NCic.Appl (NCic.Meta (i,l)::args) when List.mem_assoc i subst ->
424 let _,_,term,_ = NCicUtils.lookup_subst i subst in
425 let term = NCicSubstitution.subst_meta l term in
426 unify rdb test_eq_only metasenv subst context t1
427 (mk_appl ~upto:(List.length args) term args)
429 | NCic.Appl (NCic.Meta (i,_)::_ as l1),
430 NCic.Appl (NCic.Meta (j,_)::_ as l2) when i=j ->
433 (fun (metasenv, subst) t1 t2 ->
434 unify rdb test_eq_only metasenv subst context t1 t2)
435 (metasenv,subst) l1 l2
436 with Invalid_argument _ ->
437 raise (fail_exc metasenv subst context t1 t2))
439 | NCic.Appl (NCic.Meta (i,l)::args), _ ->
440 let metasenv, subst, lambda_Mj =
441 lambda_intros rdb metasenv subst context t1 args
443 let metasenv, subst =
444 unify rdb test_eq_only metasenv subst context
445 (C.Meta (i,l)) lambda_Mj
447 let metasenv, subst =
448 unify rdb test_eq_only metasenv subst context t1 t2
451 let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
452 let term = eta_reduce subst term in
453 let subst = List.filter (fun (j,_) -> j <> i) subst in
454 metasenv, ((i, (name, ctx, term, ty)) :: subst)
455 with Not_found -> assert false)
457 | _, NCic.Appl (NCic.Meta (i,l)::args) ->
458 let metasenv, subst, lambda_Mj =
459 lambda_intros rdb metasenv subst context t2 args
461 let metasenv, subst =
462 unify rdb test_eq_only metasenv subst context
463 lambda_Mj (C.Meta (i,l))
465 let metasenv, subst =
466 unify rdb test_eq_only metasenv subst context t1 t2
469 let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
470 let term = eta_reduce subst term in
471 let subst = List.filter (fun (j,_) -> j <> i) subst in
472 metasenv, ((i, (name, ctx, term, ty)) :: subst)
473 with Not_found -> assert false)
475 (* processing this case here we avoid a useless small delta step *)
476 | (C.Appl ((C.Const r1) as _hd1::tl1), C.Appl (C.Const r2::tl2))
478 let relevance = NCicEnvironment.get_relevance r1 in
479 let relevance = match r1 with
480 | Ref.Ref (_,Ref.Con (_,_,lno)) ->
482 try snd (HExtlib.split_nth lno relevance)
485 HExtlib.mk_list false lno @ relevance
488 let metasenv, subst, _ =
491 (fun (metasenv, subst, relevance) t1 t2 ->
493 match relevance with b::tl -> b,tl | _ -> true, [] in
494 let metasenv, subst =
495 try unify rdb test_eq_only metasenv subst context t1 t2
496 with UnificationFailure _ | Uncertain _ when not b ->
499 metasenv, subst, relevance)
500 (metasenv, subst, relevance) tl1 tl2
501 with Invalid_argument _ ->
502 raise (uncert_exc metasenv subst context t1 t2)
506 | (C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as ref1,outtype1,term1,pl1),
507 C.Match (ref2,outtype2,term2,pl2)) ->
508 let _,_,itl,_,_ = NCicEnvironment.get_checked_indtys ref1 in
509 let _,_,ty,_ = List.nth itl tyno in
510 let rec remove_prods ~subst context ty =
511 let ty = NCicReduction.whd ~subst context ty in
514 | C.Prod (name,so,ta) ->
515 remove_prods ~subst ((name,(C.Decl so))::context) ta
519 match remove_prods ~subst [] ty with
520 | C.Sort C.Prop -> true
523 if not (Ref.eq ref1 ref2) then
524 raise (uncert_exc metasenv subst context t1 t2)
526 let metasenv, subst =
527 unify rdb test_eq_only metasenv subst context outtype1 outtype2 in
528 let metasenv, subst =
529 try unify rdb test_eq_only metasenv subst context term1 term2
530 with UnificationFailure _ | Uncertain _ when is_prop ->
535 (fun (metasenv,subst) ->
536 unify rdb test_eq_only metasenv subst context)
537 (metasenv, subst) pl1 pl2
538 with Invalid_argument _ ->
539 raise (uncert_exc metasenv subst context t1 t2))
540 | (C.Implicit _, _) | (_, C.Implicit _) -> assert false
541 | _ when NCicUntrusted.metas_of_term subst context t1 = [] &&
542 NCicUntrusted.metas_of_term subst context t2 = [] ->
543 raise (fail_exc metasenv subst context t1 t2)
544 | _ -> raise (uncert_exc metasenv subst context t1 t2)
545 (*D*) in outside(); rc with exn -> outside (); raise exn
547 let try_hints metasenv subst t1 t2 (* exc*) =
549 prerr_endline ("\nProblema:\n" ^
550 NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " =?= " ^
551 NCicPp.ppterm ~metasenv ~subst ~context t2);
554 NCicUnifHint.look_for_hint rdb metasenv subst context t1 t2
556 let rec cand_iter = function
557 | [] -> None (* raise exc *)
558 | (metasenv,(c1,c2),premises)::tl ->
560 prerr_endline ("\nProvo il candidato:\n" ^
564 NCicPp.ppterm ~metasenv ~subst ~context a ^ " =?= " ^
565 NCicPp.ppterm ~metasenv ~subst ~context b) premises) ^
566 "\n-------------------------------------------\n"^
567 NCicPp.ppterm ~metasenv ~subst ~context c1 ^ " = " ^
568 NCicPp.ppterm ~metasenv ~subst ~context c2);
572 fo_unif test_eq_only metasenv subst t1 c1 in
574 fo_unif test_eq_only metasenv subst c2 t2 in
577 (fun (metasenv, subst) (x,y) ->
578 unify rdb test_eq_only metasenv subst context x y)
579 (metasenv, subst) premises
581 Some (metasenv, subst)
583 UnificationFailure _ | Uncertain _ ->
588 let height_of = function
589 | NCic.Const (Ref.Ref (_,Ref.Def h))
590 | NCic.Const (Ref.Ref (_,Ref.Fix (_,_,h)))
591 | NCic.Appl(NCic.Const(Ref.Ref(_,Ref.Def h))::_)
592 | NCic.Appl(NCic.Const(Ref.Ref(_,Ref.Fix (_,_,h)))::_) -> h
595 let put_in_whd m1 m2 =
596 NCicReduction.reduce_machine ~delta:max_int ~subst context m1,
597 NCicReduction.reduce_machine ~delta:max_int ~subst context m2
599 let small_delta_step ~subst
600 ((_,_,t1,_ as m1, norm1) as x1) ((_,_,t2,_ as m2, norm2) as x2)
602 assert (not (norm1 && norm2));
604 x1,NCicReduction.reduce_machine ~delta:(height_of t2 -1) ~subst context m2
606 NCicReduction.reduce_machine ~delta:(height_of t1 -1) ~subst context m1,x2
608 let h1 = height_of t1 in
609 let h2 = height_of t2 in
610 let delta = if h1 = h2 then max 0 (h1 -1) else min h1 h2 in
611 NCicReduction.reduce_machine ~delta ~subst context m1,
612 NCicReduction.reduce_machine ~delta ~subst context m2
614 let rec unif_machines metasenv subst =
616 | ((k1,e1,t1,s1),norm1 as m1),((k2,e2,t2,s2),norm2 as m2) ->
617 (*D*) inside 'M'; try let rc =
619 NCicPp.ppterm ~metasenv ~subst ~context
620 (NCicReduction.unwind (k1,e1,t1,s1)) ^
622 NCicPp.ppterm ~metasenv ~subst ~context
623 (NCicReduction.unwind (k2,e2,t2,s2))));
624 pp (lazy (string_of_bool norm1 ^ " ?? " ^ string_of_bool norm2));
625 let relevance = [] (* TO BE UNDERSTOOD
627 | C.Const r -> NCicEnvironment.get_relevance r
629 let unif_from_stack t1 t2 b metasenv subst =
631 let t1 = NCicReduction.from_stack ~delta:max_int t1 in
632 let t2 = NCicReduction.from_stack ~delta:max_int t2 in
633 unif_machines metasenv subst (put_in_whd t1 t2)
634 with UnificationFailure _ | Uncertain _ when not b ->
637 let rec check_stack l1 l2 r todo =
639 | x1::tl1, x2::tl2, r::tr-> check_stack tl1 tl2 tr ((x1,x2,r)::todo)
640 | x1::tl1, x2::tl2, []-> check_stack tl1 tl2 [] ((x1,x2,true)::todo)
642 NCicReduction.unwind (k1,e1,t1,List.rev l1),
643 NCicReduction.unwind (k2,e2,t2,List.rev l2),
646 let hh1,hh2,todo=check_stack (List.rev s1) (List.rev s2) relevance [] in
648 let metasenv,subst = fo_unif test_eq_only metasenv subst hh1 hh2 in
650 (fun (metasenv,subst) (x1,x2,r) ->
651 unif_from_stack x1 x2 r metasenv subst
652 ) (metasenv,subst) todo
653 with UnificationFailure _ | Uncertain _ when not (norm1 && norm2) ->
654 unif_machines metasenv subst (small_delta_step ~subst m1 m2)
655 (*D*) in outside(); rc with exn -> outside (); raise exn
657 try fo_unif test_eq_only metasenv subst t1 t2
659 | UnificationFailure msg as exn ->
661 unif_machines metasenv subst
662 (put_in_whd (0,[],t1,[]) (0,[],t2,[]))
664 | UnificationFailure _ -> raise (UnificationFailure msg)
665 | Uncertain _ -> raise exn)
666 | Uncertain msg as exn ->
667 match try_hints metasenv subst t1 t2 with
671 unif_machines metasenv subst
672 (put_in_whd (0,[],t1,[]) (0,[],t2,[]))
674 | UnificationFailure _ -> raise (UnificationFailure msg)
675 | Uncertain _ -> raise exn
676 (*D*) in outside(); rc with exn -> outside (); raise exn
678 and delift_type_wrt_terms rdb metasenv subst context t args =
679 let (metasenv, subst), t =
682 ~unify:(fun m s c t1 t2 ->
685 try Some (unify rdb false m s c t1 t2 )
686 with UnificationFailure _ | Uncertain _ -> None
689 metasenv subst context 0 (0,NCic.Ctx (args @
690 List.rev (mk_irl (List.length context)))) t
691 with NCicMetaSubst.MetaSubstFailure _ -> (metasenv, subst), t
697 let unify rdb ?(test_eq_only=false) =
699 unify rdb test_eq_only;;
701 let fix_sorts = fix_sorts true (UnificationFailure (lazy "no sup"));;