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)
99 let ppcontext = NCicPp.ppcontext;;
100 let ppmetasenv = NCicPp.ppmetasenv;;
102 let ppcontext ~metasenv:_metasenv ~subst:_subst _context = "...";;
103 let ppmetasenv ~subst:_subst _metasenv = "...";;
107 let fix_sorts swap metasenv subst context meta t =
108 let rec aux () = function
109 | NCic.Sort (NCic.Type u) as orig ->
111 match NCicEnvironment.sup u with
112 | None -> prerr_endline "no sup for" ;
113 raise (fail_exc metasenv subst context meta t)
114 | Some u1 -> if u = u1 then orig else NCic.Sort (NCic.Type u1)
116 NCic.Sort (NCic.Type (
117 match NCicEnvironment.sup NCicEnvironment.type0 with
119 | _ -> assert false))
120 | NCic.Meta _ as orig -> orig
121 | t -> NCicUtils.map (fun _ _ -> ()) () aux t
126 let is_locked n subst =
128 match NCicUtils.lookup_subst n subst with
129 | Some tag, _,_,_ when NCicMetaSubst.is_out_scope_tag tag -> true
131 with NCicUtils.Subst_not_found _ -> false
137 | n -> NCic.Rel n :: mk_irl (n-1)
140 let rec lambda_intros rdb metasenv subst context t args =
141 let tty = NCicTypeChecker.typeof ~metasenv ~subst context t in
144 (fun arg -> arg, NCicTypeChecker.typeof ~metasenv ~subst context arg) args in
145 let context_of_args = context in
146 let mk_appl = function [] -> assert false | [t] -> t | l -> NCic.Appl l in
147 let rec mk_lambda metasenv subst context n processed_args = function
149 let metasenv, _, bo, _ =
150 NCicMetaSubst.mk_meta metasenv context
151 (`WithType (NCicSubstitution.lift n tty))
155 let name = "HBeta"^string_of_int n in
156 let metasenv,_,instance,_ =
157 NCicMetaSubst.mk_meta metasenv context_of_args `Term in
158 let meta_applied = mk_appl (instance::List.rev processed_args) in
159 let metasenv,subst,_,_ =
160 !refiner_typeof ((rdb :> NRstatus.status)#set_coerc_db NCicCoercion.empty_db) metasenv subst context_of_args meta_applied None
162 prerr_endline ("########################## LI1: " ^ NCicPp.ppterm ~metasenv ~subst ~context:context_of_args meta_applied ^ " vs " ^ NCicPp.ppterm ~metasenv ~subst ~context:context_of_args ty);
163 prerr_endline (NCicPp.ppcontext ~metasenv ~subst context_of_args);
164 prerr_endline (NCicPp.ppmetasenv metasenv ~subst);
166 unify rdb true metasenv subst context_of_args meta_applied ty in
167 prerr_endline "UNIFY FINITA";
168 let telescopic_ty = NCicSubstitution.lift n instance in
170 mk_appl (telescopic_ty :: mk_irl (List.length processed_args)) in
171 prerr_endline ("########################## LI: " ^ NCicPp.ppterm ~metasenv ~subst ~context telescopic_ty);
172 let metasenv, subst, bo =
173 mk_lambda metasenv subst ((name,NCic.Decl telescopic_ty)::context) (n+1)
174 (arg::processed_args) tail
176 metasenv, subst, NCic.Lambda (name, telescopic_ty, bo)
178 mk_lambda metasenv subst context 0 [] argsty
180 and instantiate rdb test_eq_only metasenv subst context n lc t swap =
181 (*D*) inside 'I'; try let rc =
182 pp (lazy(string_of_int n ^ " :=?= "^
183 NCicPp.ppterm ~metasenv ~subst ~context t));
184 let unify test_eq_only m s c t1 t2 =
185 if swap then unify rdb test_eq_only m s c t2 t1
186 else unify rdb test_eq_only m s c t1 t2
188 let name, ctx, ty = NCicUtils.lookup_meta n metasenv in
189 let metasenv, subst, t =
191 | NCic.Implicit (`Typeof _) ->
193 (* fix_sorts swap metasenv subst context (NCic.Meta(n,lc)) t *)
196 "typeof: " ^ NCicPp.ppterm ~metasenv ~subst ~context t ^ "\nctx:\n"^
197 ppcontext ~metasenv ~subst context ^ "\nmenv:\n"^
198 ppmetasenv ~subst metasenv));
200 try t, NCicTypeChecker.typeof ~subst ~metasenv context t
202 | NCicTypeChecker.AssertFailure msg ->
203 (pp (lazy "fine typeof (fallimento)");
204 let ft=fix_sorts swap metasenv subst context (NCic.Meta (n,lc)) t in
206 (prerr_endline ( ("ILLTYPED: " ^
207 NCicPp.ppterm ~metasenv ~subst ~context t
208 ^ "\nBECAUSE:" ^ Lazy.force msg ^ "\nCONTEXT:\n" ^
209 ppcontext ~metasenv ~subst context ^ "\nMENV:\n" ^
210 ppmetasenv ~subst metasenv
215 pp (lazy ("typeof: " ^
216 NCicPp.ppterm ~metasenv ~subst ~context ft));
217 ft, NCicTypeChecker.typeof ~subst ~metasenv context ft
218 with NCicTypeChecker.AssertFailure _ ->
220 | NCicTypeChecker.TypeCheckerFailure msg ->
221 prerr_endline (Lazy.force msg);
224 let lty = NCicSubstitution.subst_meta lc ty in
227 raise (UnificationFailure
228 (lazy "trying to unify a term with a type"))
230 pp (lazy ("On the types: " ^
231 NCicPp.ppterm ~metasenv ~subst ~context:ctx ty ^ " ~~~ " ^
232 NCicPp.ppterm ~metasenv ~subst ~context lty ^ " === "
233 ^ NCicPp.ppterm ~metasenv ~subst ~context ty_t));
235 unify test_eq_only metasenv subst context lty ty_t in
238 pp (lazy(string_of_int n ^ " := 111 = "^
239 NCicPp.ppterm ~metasenv ~subst ~context t));
240 let (metasenv, subst), t =
243 ~unify:(fun m s c t1 t2 ->
246 try Some (unify test_eq_only m s c t1 t2 )
247 with UnificationFailure _ | Uncertain _ -> None
250 metasenv subst context n lc t
251 with NCicMetaSubst.Uncertain msg ->
252 pp (lazy ("delift fails: " ^ Lazy.force msg));
253 raise (Uncertain msg)
254 | NCicMetaSubst.MetaSubstFailure msg ->
255 pp (lazy ("delift fails: " ^ Lazy.force msg));
256 raise (UnificationFailure msg)
258 pp (lazy(string_of_int n ^ " := 222 = "^
259 NCicPp.ppterm ~metasenv ~subst ~context:ctx t
260 ^ "\n" ^ ppmetasenv ~subst metasenv));
261 (* Unifying the types may have already instantiated n. *)
263 let _, _,oldt,_ = NCicUtils.lookup_subst n subst in
264 let oldt = NCicSubstitution.subst_meta lc oldt in
265 let t = NCicSubstitution.subst_meta lc t in
266 (* conjecture: always fail --> occur check *)
267 unify test_eq_only metasenv subst context oldt t
268 with NCicUtils.Subst_not_found _ ->
269 (* by cumulativity when unify(?,Type_i)
270 * we could ? := Type_j with j <= i... *)
271 let subst = (n, (name, ctx, t, ty)) :: subst in
272 pp (lazy ("?"^string_of_int n^" := "^NCicPp.ppterm
273 ~metasenv ~subst ~context (NCicSubstitution.subst_meta lc t)));
275 List.filter (fun (m,_) -> not (n = m)) metasenv
278 (*D*) in outside(); rc with exn -> outside (); raise exn
280 and unify rdb test_eq_only metasenv subst context t1 t2 =
281 (*D*) inside 'U'; try let rc =
282 let fo_unif test_eq_only metasenv subst t1 t2 =
283 (*D*) inside 'F'; try let rc =
284 pp (lazy(" " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " ==?== " ^
285 NCicPp.ppterm ~metasenv ~subst ~context t2 ^ "\n" ^ ppmetasenv
291 | C.Appl [_], _ | _, C.Appl [_] | C.Appl [], _ | _, C.Appl []
292 | C.Appl (C.Appl _::_), _ | _, C.Appl (C.Appl _::_) ->
293 prerr_endline "Appl [Appl _;_] or Appl [] or Appl [_] invariant";
295 | (C.Sort (C.Type a), C.Sort (C.Type b)) when not test_eq_only ->
296 if NCicEnvironment.universe_leq a b then metasenv, subst
297 else raise (fail_exc metasenv subst context t1 t2)
298 | (C.Sort (C.Type a), C.Sort (C.Type b)) ->
299 if NCicEnvironment.universe_eq a b then metasenv, subst
300 else raise (fail_exc metasenv subst context t1 t2)
301 | (C.Sort C.Prop,C.Sort (C.Type _)) ->
302 if (not test_eq_only) then metasenv, subst
303 else raise (fail_exc metasenv subst context t1 t2)
305 | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2))
306 | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
307 let metasenv, subst = unify rdb true metasenv subst context s1 s2 in
308 unify rdb test_eq_only metasenv subst ((name1, C.Decl s1)::context) t1 t2
309 | (C.LetIn (name1,ty1,s1,t1), C.LetIn(_,ty2,s2,t2)) ->
310 let metasenv,subst=unify rdb test_eq_only metasenv subst context ty1 ty2 in
311 let metasenv,subst=unify rdb test_eq_only metasenv subst context s1 s2 in
312 let context = (name1, C.Def (s1,ty1))::context in
313 unify rdb test_eq_only metasenv subst context t1 t2
315 | (C.Meta (n1,(s1,l1 as lc1)),C.Meta (n2,(s2,l2 as lc2))) when n1 = n2 ->
317 let l1 = NCicUtils.expand_local_context l1 in
318 let l2 = NCicUtils.expand_local_context l2 in
319 let metasenv, subst, to_restrict, _ =
321 (fun t1 t2 (metasenv, subst, to_restrict, i) ->
323 let metasenv, subst =
324 unify rdb test_eq_only metasenv subst context
325 (NCicSubstitution.lift s1 t1) (NCicSubstitution.lift s2 t2)
327 metasenv, subst, to_restrict, i-1
328 with UnificationFailure _ | Uncertain _ ->
329 metasenv, subst, i::to_restrict, i-1)
330 l1 l2 (metasenv, subst, [], List.length l1)
332 if to_restrict <> [] then
333 let metasenv, subst, _ =
334 NCicMetaSubst.restrict metasenv subst n1 to_restrict
339 | Invalid_argument _ -> assert false
340 | NCicMetaSubst.MetaSubstFailure msg ->
342 let _,_,term,_ = NCicUtils.lookup_subst n1 subst in
343 let term1 = NCicSubstitution.subst_meta lc1 term in
344 let term2 = NCicSubstitution.subst_meta lc2 term in
345 unify rdb test_eq_only metasenv subst context term1 term2
346 with NCicUtils.Subst_not_found _-> raise (UnificationFailure msg))
348 | _, NCic.Meta (n, _) when is_locked n subst ->
349 (let (metasenv, subst), i =
350 match NCicReduction.whd ~subst context t1 with
351 | NCic.Appl (NCic.Meta (i,l)::args) when
352 not (NCicMetaSubst.flexible subst args)
354 let metasenv, subst, lambda_Mj =
355 lambda_intros rdb metasenv subst context t1 args
357 prerr_endline ("XXXXXXXXX " ^ NCicPp.ppterm ~metasenv ~subst ~context lambda_Mj);
358 unify rdb test_eq_only metasenv subst context
359 (C.Meta (i,l)) lambda_Mj,
361 | NCic.Meta (i,_) -> (metasenv, subst), i
364 let t1 = NCicReduction.whd ~subst context t1 in
366 match t1 with NCic.Meta (j,l) -> j, l | _ -> assert false
368 let metasenv, subst =
369 instantiate rdb test_eq_only metasenv subst context j lj t2 true
371 (* We need to remove the out_scope_tags to avoid propagation of
372 them that triggers again the ad-hoc case *)
374 List.map (fun (i,(tag,ctx,bo,ty)) ->
378 tag = NCicMetaSubst.in_scope_tag
379 || NCicMetaSubst.is_out_scope_tag tag -> None
386 let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
387 let term = eta_reduce subst term in
388 let subst = List.filter (fun (j,_) -> j <> i) subst in
389 metasenv, ((i, (name, ctx, term, ty)) :: subst)
390 with Not_found -> assert false))
392 | C.Meta (n,lc), t ->
394 let _,_,term,_ = NCicUtils.lookup_subst n subst in
395 let term = NCicSubstitution.subst_meta lc term in
396 unify rdb test_eq_only metasenv subst context term t
397 with NCicUtils.Subst_not_found _->
398 instantiate rdb test_eq_only metasenv subst context n lc
399 (NCicReduction.head_beta_reduce ~subst t) false)
401 | t, C.Meta (n,lc) ->
403 let _,_,term,_ = NCicUtils.lookup_subst n subst in
404 let term = NCicSubstitution.subst_meta lc term in
405 unify rdb test_eq_only metasenv subst context t term
406 with NCicUtils.Subst_not_found _->
407 instantiate rdb test_eq_only metasenv subst context n lc
408 (NCicReduction.head_beta_reduce ~subst t) true)
410 | NCic.Appl (NCic.Meta (i,l)::args), _ when List.mem_assoc i subst ->
411 let _,_,term,_ = NCicUtils.lookup_subst i subst in
412 let term = NCicSubstitution.subst_meta l term in
413 unify rdb test_eq_only metasenv subst context
414 (mk_appl ~upto:(List.length args) term args) t2
416 | _, NCic.Appl (NCic.Meta (i,l)::args) when List.mem_assoc i subst ->
417 let _,_,term,_ = NCicUtils.lookup_subst i subst in
418 let term = NCicSubstitution.subst_meta l term in
419 unify rdb test_eq_only metasenv subst context t1
420 (mk_appl ~upto:(List.length args) term args)
422 | NCic.Appl (NCic.Meta (i,_)::_ as l1),
423 NCic.Appl (NCic.Meta (j,_)::_ as l2) when i=j ->
426 (fun (metasenv, subst) t1 t2 ->
427 unify rdb test_eq_only metasenv subst context t1 t2)
428 (metasenv,subst) l1 l2
429 with Invalid_argument _ ->
430 raise (fail_exc metasenv subst context t1 t2))
432 | NCic.Appl (NCic.Meta (i,l)::args), _ when
433 not (NCicMetaSubst.flexible subst args) ->
434 (* we verify that none of the args is a Meta,
435 since beta expanding w.r.t a metavariable makes no sense *)
436 let metasenv, subst, lambda_Mj =
437 lambda_intros rdb metasenv subst context t1 args
439 let metasenv, subst =
440 unify rdb test_eq_only metasenv subst context
441 (C.Meta (i,l)) lambda_Mj
443 let metasenv, subst =
444 unify rdb test_eq_only metasenv subst context t1 t2
447 let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
448 let term = eta_reduce subst term in
449 let subst = List.filter (fun (j,_) -> j <> i) subst in
450 metasenv, ((i, (name, ctx, term, ty)) :: subst)
451 with Not_found -> assert false)
453 | _, NCic.Appl (NCic.Meta (i,l)::args) when
454 not(NCicMetaSubst.flexible subst args) ->
455 let metasenv, subst, lambda_Mj =
456 lambda_intros rdb metasenv subst context t2 args
458 let metasenv, subst =
459 unify rdb test_eq_only metasenv subst context
460 lambda_Mj (C.Meta (i,l))
462 let metasenv, subst =
463 unify rdb test_eq_only metasenv subst context t1 t2
466 let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
467 let term = eta_reduce subst term in
468 let subst = List.filter (fun (j,_) -> j <> i) subst in
469 metasenv, ((i, (name, ctx, term, ty)) :: subst)
470 with Not_found -> assert false)
472 (* processing this case here we avoid a useless small delta step *)
473 | (C.Appl ((C.Const r1) as _hd1::tl1), C.Appl (C.Const r2::tl2))
475 let relevance = NCicEnvironment.get_relevance r1 in
476 let relevance = match r1 with
477 | Ref.Ref (_,Ref.Con (_,_,lno)) ->
479 try snd (HExtlib.split_nth lno relevance)
482 HExtlib.mk_list false lno @ relevance
485 let metasenv, subst, _ =
488 (fun (metasenv, subst, relevance) t1 t2 ->
490 match relevance with b::tl -> b,tl | _ -> true, [] in
491 let metasenv, subst =
492 try unify rdb test_eq_only metasenv subst context t1 t2
493 with UnificationFailure _ | Uncertain _ when not b ->
496 metasenv, subst, relevance)
497 (metasenv, subst, relevance) tl1 tl2
498 with Invalid_argument _ ->
499 raise (uncert_exc metasenv subst context t1 t2)
503 | (C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as ref1,outtype1,term1,pl1),
504 C.Match (ref2,outtype2,term2,pl2)) ->
505 let _,_,itl,_,_ = NCicEnvironment.get_checked_indtys ref1 in
506 let _,_,ty,_ = List.nth itl tyno in
507 let rec remove_prods ~subst context ty =
508 let ty = NCicReduction.whd ~subst context ty in
511 | C.Prod (name,so,ta) ->
512 remove_prods ~subst ((name,(C.Decl so))::context) ta
516 match remove_prods ~subst [] ty with
517 | C.Sort C.Prop -> true
520 if not (Ref.eq ref1 ref2) then
521 raise (uncert_exc metasenv subst context t1 t2)
523 let metasenv, subst =
524 unify rdb test_eq_only metasenv subst context outtype1 outtype2 in
525 let metasenv, subst =
526 try unify rdb test_eq_only metasenv subst context term1 term2
527 with UnificationFailure _ | Uncertain _ when is_prop ->
532 (fun (metasenv,subst) ->
533 unify rdb test_eq_only metasenv subst context)
534 (metasenv, subst) pl1 pl2
535 with Invalid_argument _ ->
536 raise (uncert_exc metasenv subst context t1 t2))
537 | (C.Implicit _, _) | (_, C.Implicit _) -> assert false
538 | _ when NCicUntrusted.metas_of_term subst context t1 = [] &&
539 NCicUntrusted.metas_of_term subst context t2 = [] ->
540 raise (fail_exc metasenv subst context t1 t2)
541 | _ -> raise (uncert_exc metasenv subst context t1 t2)
542 (*D*) in outside(); rc with exn -> outside (); raise exn
544 let try_hints metasenv subst t1 t2 (* exc*) =
546 prerr_endline ("\nProblema:\n" ^
547 NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " =?= " ^
548 NCicPp.ppterm ~metasenv ~subst ~context t2);
551 NCicUnifHint.look_for_hint rdb metasenv subst context t1 t2
553 let rec cand_iter = function
554 | [] -> None (* raise exc *)
555 | (metasenv,(c1,c2),premises)::tl ->
557 prerr_endline ("\nProvo il candidato:\n" ^
561 NCicPp.ppterm ~metasenv ~subst ~context a ^ " =?= " ^
562 NCicPp.ppterm ~metasenv ~subst ~context b) premises) ^
563 "\n-------------------------------------------\n"^
564 NCicPp.ppterm ~metasenv ~subst ~context c1 ^ " = " ^
565 NCicPp.ppterm ~metasenv ~subst ~context c2);
569 fo_unif test_eq_only metasenv subst t1 c1 in
571 fo_unif test_eq_only metasenv subst c2 t2 in
574 (fun (metasenv, subst) (x,y) ->
575 unify rdb test_eq_only metasenv subst context x y)
576 (metasenv, subst) premises
578 Some (metasenv, subst)
580 UnificationFailure _ | Uncertain _ ->
585 let height_of = function
586 | NCic.Const (Ref.Ref (_,Ref.Def h))
587 | NCic.Const (Ref.Ref (_,Ref.Fix (_,_,h)))
588 | NCic.Appl(NCic.Const(Ref.Ref(_,Ref.Def h))::_)
589 | NCic.Appl(NCic.Const(Ref.Ref(_,Ref.Fix (_,_,h)))::_) -> h
592 let put_in_whd m1 m2 =
593 NCicReduction.reduce_machine ~delta:max_int ~subst context m1,
594 NCicReduction.reduce_machine ~delta:max_int ~subst context m2
596 let small_delta_step ~subst
597 ((_,_,t1,_ as m1, norm1) as x1) ((_,_,t2,_ as m2, norm2) as x2)
599 assert (not (norm1 && norm2));
601 x1,NCicReduction.reduce_machine ~delta:(height_of t2 -1) ~subst context m2
603 NCicReduction.reduce_machine ~delta:(height_of t1 -1) ~subst context m1,x2
605 let h1 = height_of t1 in
606 let h2 = height_of t2 in
607 let delta = if h1 = h2 then max 0 (h1 -1) else min h1 h2 in
608 NCicReduction.reduce_machine ~delta ~subst context m1,
609 NCicReduction.reduce_machine ~delta ~subst context m2
611 let rec unif_machines metasenv subst =
613 | ((k1,e1,t1,s1),norm1 as m1),((k2,e2,t2,s2),norm2 as m2) ->
614 (*D*) inside 'M'; try let rc =
616 NCicPp.ppterm ~metasenv ~subst ~context
617 (NCicReduction.unwind (k1,e1,t1,s1)) ^
619 NCicPp.ppterm ~metasenv ~subst ~context
620 (NCicReduction.unwind (k2,e2,t2,s2))));
621 pp (lazy (string_of_bool norm1 ^ " ?? " ^ string_of_bool norm2));
622 let relevance = [] (* TO BE UNDERSTOOD
624 | C.Const r -> NCicEnvironment.get_relevance r
626 let unif_from_stack t1 t2 b metasenv subst =
628 let t1 = NCicReduction.from_stack t1 in
629 let t2 = NCicReduction.from_stack t2 in
630 unif_machines metasenv subst (put_in_whd t1 t2)
631 with UnificationFailure _ | Uncertain _ when not b ->
634 let rec check_stack l1 l2 r todo =
636 | x1::tl1, x2::tl2, r::tr-> check_stack tl1 tl2 tr ((x1,x2,r)::todo)
637 | x1::tl1, x2::tl2, []-> check_stack tl1 tl2 [] ((x1,x2,true)::todo)
639 NCicReduction.unwind (k1,e1,t1,List.rev l1),
640 NCicReduction.unwind (k2,e2,t2,List.rev l2),
643 let hh1,hh2,todo=check_stack (List.rev s1) (List.rev s2) relevance [] in
645 let metasenv,subst = fo_unif test_eq_only metasenv subst hh1 hh2 in
647 (fun (metasenv,subst) (x1,x2,r) ->
648 unif_from_stack x1 x2 r metasenv subst
649 ) (metasenv,subst) todo
650 with UnificationFailure _ | Uncertain _ when not (norm1 && norm2) ->
651 unif_machines metasenv subst (small_delta_step ~subst m1 m2)
652 (*D*) in outside(); rc with exn -> outside (); raise exn
654 try fo_unif test_eq_only metasenv subst t1 t2
656 | UnificationFailure msg as exn ->
658 unif_machines metasenv subst
659 (put_in_whd (0,[],t1,[]) (0,[],t2,[]))
661 | UnificationFailure _ -> raise (UnificationFailure msg)
662 | Uncertain _ -> raise exn)
663 | Uncertain msg as exn ->
664 match try_hints metasenv subst t1 t2 with
668 unif_machines metasenv subst
669 (put_in_whd (0,[],t1,[]) (0,[],t2,[]))
671 | UnificationFailure _ -> raise (UnificationFailure msg)
672 | Uncertain _ -> raise exn
673 (*D*) in outside(); rc with exn -> outside (); raise exn
676 let unify rdb ?(test_eq_only=false) =
678 unify rdb test_eq_only;;