]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_refiner/nCicUnification.ml
snapshot
[helm.git] / helm / software / components / ng_refiner / nCicUnification.ml
1 (*
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.                     
5     ||I||                                                                
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_______________________________________________________________ *)
11
12 (* $Id$ *)
13
14 exception UnificationFailure of string Lazy.t;;
15 exception Uncertain of string Lazy.t;;
16 exception AssertFailure of string Lazy.t;;
17
18 let (===) x y = Pervasives.compare x y = 0 ;;
19
20 let uncert_exc metasenv subst context t1 t2 = 
21   Uncertain (lazy (
22   "Can't unify " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^
23   " with " ^ NCicPp.ppterm ~metasenv ~subst ~context t2))
24 ;;
25
26 let fail_exc metasenv subst context t1 t2 = 
27   UnificationFailure (lazy (
28   "Can't unify " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^
29   " with " ^ NCicPp.ppterm ~metasenv ~subst ~context t2));
30 ;;
31
32 let mk_appl ~upto hd tl =
33   NCicReduction.head_beta_reduce ~upto
34     (match hd with
35     | NCic.Appl l -> NCic.Appl (l@tl)
36     | _ -> NCic.Appl (hd :: tl))
37 ;;
38
39 let flexible l = 
40   List.exists 
41     (function 
42        | NCic.Meta _  
43        | NCic.Appl (NCic.Meta _::_) -> true
44        | _ -> false) l
45 ;;
46
47 exception WrongShape;;
48
49 let eta_reduce = 
50   let delift_if_not_occur body orig =
51     try 
52         NCicSubstitution.psubst ~avoid_beta_redexes:true
53           (fun () -> raise WrongShape) [()] body
54     with WrongShape -> orig
55   in
56   function
57   | NCic.Lambda(_, _, NCic.Appl [hd; NCic.Rel 1]) as orig ->
58       delift_if_not_occur hd orig
59   | NCic.Lambda(_, _, NCic.Appl (hd :: l)) as orig
60     when HExtlib.list_last l = NCic.Rel 1 ->
61        let body = 
62          let args, _ = HExtlib.split_nth (List.length l - 1) l in
63          NCic.Appl (hd::args)
64        in
65        delift_if_not_occur body orig
66   | t -> t
67 ;;
68  
69 module C = NCic;;
70 module Ref = NReference;;
71
72 let indent = ref "";;
73 let inside c = indent := !indent ^ String.make 1 c;;
74 let outside () = indent := String.sub !indent 0 (String.length !indent -1);;
75
76 let pp s = 
77   prerr_endline (Printf.sprintf "%-20s" !indent ^ " " ^ Lazy.force s)
78 ;;  
79
80 let pp _ = ();;
81
82 let fix_sorts swap metasenv subst context meta t =
83   let rec aux () = function
84     | NCic.Sort (NCic.Type u) as orig ->
85         if swap then
86          match NCicEnvironment.sup u with
87          | None -> prerr_endline "no sup for" ;
88             raise (fail_exc metasenv subst context meta t)
89          | Some u1 -> if u = u1 then orig else NCic.Sort (NCic.Type u1)
90         else
91          NCic.Sort (NCic.Type (
92            match NCicEnvironment.sup NCicEnvironment.type0 with 
93            | Some x -> x
94            | _ -> assert false))
95     | NCic.Meta _ as orig -> orig
96     | t -> NCicUtils.map (fun _ _ -> ()) () aux t
97   in
98     aux () t
99 ;;
100
101 let rec beta_expand_many metasenv subst context t args =
102  let tty = NCicTypeChecker.typeof ~metasenv ~subst context t in
103  let argsty = List.map (NCicTypeChecker.typeof ~metasenv ~subst context) args in
104  let rec mk_lambda context n = function
105    | [] -> 
106        let metasenv, _, bo, _ = 
107          NCicMetaSubst.mk_meta metasenv context (`WithType tty) in
108        metasenv, bo
109    | ty::tail -> 
110        let name = "HBeta"^string_of_int n in
111        let ty = NCicSubstitution.lift n ty in
112        let metasenv, bo = mk_lambda ((name,NCic.Decl ty)::context) (n+1) tail in
113        metasenv, NCic.Lambda (name, ty, bo)
114  in
115    mk_lambda context 0 argsty
116
117 and instantiate hdb test_eq_only metasenv subst context n lc t swap =
118  (*D*)  inside 'I'; try let rc =  
119          pp (lazy(string_of_int n ^ " :=?= "^
120            NCicPp.ppterm ~metasenv ~subst ~context t));
121   let unify test_eq_only m s c t1 t2 = 
122     if swap then unify hdb test_eq_only m s c t2 t1 
123     else unify hdb test_eq_only m s c t1 t2
124   in
125   let name, ctx, ty = NCicUtils.lookup_meta n metasenv in
126   let metasenv, subst, t = 
127     match ty with 
128     | NCic.Implicit (`Typeof _) -> 
129        metasenv,subst, t
130          (* fix_sorts swap metasenv subst context (NCic.Meta(n,lc)) t *)
131     | _ ->
132        pp (lazy (
133          "typeof: " ^ NCicPp.ppterm ~metasenv ~subst ~context t ^ "\nctx:\n"^
134           NCicPp.ppcontext ~metasenv ~subst context ^ "\nmenv:\n"^
135           NCicPp.ppmetasenv ~subst metasenv));
136        let t, ty_t = 
137          try t, NCicTypeChecker.typeof ~subst ~metasenv context t 
138          with 
139          | NCicTypeChecker.AssertFailure msg -> 
140            (pp (lazy "fine typeof (fallimento)");
141            let ft=fix_sorts swap metasenv subst context (NCic.Meta (n,lc)) t in
142            if ft == t then 
143              (prerr_endline ( ("ILLTYPED: " ^ 
144                 NCicPp.ppterm ~metasenv ~subst ~context t
145             ^ "\nBECAUSE:" ^ Lazy.force msg ^ "\nCONTEXT:\n" ^
146             NCicPp.ppcontext ~metasenv ~subst context ^ "\nMENV:\n" ^
147             NCicPp.ppmetasenv ~subst metasenv
148             ));
149                      assert false)
150            else
151             try 
152               pp (lazy ("typeof: " ^ 
153                 NCicPp.ppterm ~metasenv ~subst ~context ft));
154               ft, NCicTypeChecker.typeof ~subst ~metasenv context ft 
155             with NCicTypeChecker.AssertFailure _ -> 
156               assert false)
157          | NCicTypeChecker.TypeCheckerFailure msg ->
158               prerr_endline (Lazy.force msg);
159               pp msg; assert false
160        in
161        let lty = NCicSubstitution.subst_meta lc ty in
162        pp (lazy ("On the types: " ^
163         NCicPp.ppterm ~metasenv ~subst ~context:ctx ty ^ " ~~~ " ^
164         NCicPp.ppterm ~metasenv ~subst ~context lty ^ " === "
165          ^ NCicPp.ppterm ~metasenv ~subst ~context ty_t)); 
166        let metasenv,subst= unify test_eq_only metasenv subst context lty ty_t in
167        metasenv, subst, t
168   in
169          pp (lazy(string_of_int n ^ " := 111 = "^
170            NCicPp.ppterm ~metasenv ~subst ~context t));
171   let (metasenv, subst), t = 
172     try 
173       NCicMetaSubst.delift 
174        ~unify:(fun m s c t1 t2 -> 
175          let ind = !indent in
176          let res = 
177            try Some (unify test_eq_only m s c t1 t2 )
178            with UnificationFailure _ | Uncertain _ -> None
179          in
180          indent := ind; res) 
181        metasenv subst context n lc t
182     with NCicMetaSubst.Uncertain msg -> 
183          pp (lazy ("delift fails: " ^ Lazy.force msg));
184          raise (Uncertain msg)
185     | NCicMetaSubst.MetaSubstFailure msg -> 
186          pp (lazy ("delift fails: " ^ Lazy.force msg));
187          raise (UnificationFailure msg)
188   in
189          pp (lazy(string_of_int n ^ " := 222 = "^
190            NCicPp.ppterm ~metasenv ~subst ~context:ctx t
191          ^ "\n" ^ NCicPp.ppmetasenv ~subst metasenv));
192   (* Unifying the types may have already instantiated n. *)
193   try
194     let _, _,oldt,_ = NCicUtils.lookup_subst n subst in
195     let oldt = NCicSubstitution.subst_meta lc oldt in
196     let t = NCicSubstitution.subst_meta lc t in
197     (* conjecture: always fail --> occur check *)
198     unify test_eq_only metasenv subst context oldt t
199   with NCicUtils.Subst_not_found _ -> 
200     (* by cumulativity when unify(?,Type_i) 
201      * we could ? := Type_j with j <= i... *)
202     let subst = (n, (name, ctx, t, ty)) :: subst in
203     pp (lazy ("?"^string_of_int n^" := "^NCicPp.ppterm
204       ~metasenv ~subst ~context (NCicSubstitution.subst_meta lc t)));
205     let metasenv = 
206       List.filter (fun (m,_) -> not (n = m)) metasenv 
207     in
208     metasenv, subst
209  (*D*)  in outside(); rc with exn -> outside (); raise exn 
210
211 and unify hdb test_eq_only metasenv subst context t1 t2 =
212  (*D*) inside 'U'; try let rc =
213    let fo_unif test_eq_only metasenv subst t1 t2 =
214     (*D*) inside 'F'; try let rc =  
215      pp (lazy("  " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " ==?== " ^ 
216          NCicPp.ppterm ~metasenv ~subst ~context t2 ^ "\n" ^ NCicPp.ppmetasenv
217          ~subst metasenv));
218      if t1 === t2 then
219        metasenv, subst
220      else
221        match (t1,t2) with
222        | C.Appl [_], _ | _, C.Appl [_] | C.Appl [], _ | _, C.Appl [] 
223        | C.Appl (C.Appl _::_), _ | _, C.Appl (C.Appl _::_) -> 
224            prerr_endline "Appl [Appl _;_] or Appl [] or Appl [_] invariant";
225            assert false 
226        | (C.Sort (C.Type a), C.Sort (C.Type b)) when not test_eq_only -> 
227            if NCicEnvironment.universe_leq a b then metasenv, subst
228            else raise (fail_exc metasenv subst context t1 t2)
229        | (C.Sort (C.Type a), C.Sort (C.Type b)) -> 
230            if NCicEnvironment.universe_eq a b then metasenv, subst
231            else raise (fail_exc metasenv subst context t1 t2)
232        | (C.Sort C.Prop,C.Sort (C.Type _)) -> 
233            if (not test_eq_only) then metasenv, subst
234            else raise (fail_exc metasenv subst context t1 t2)
235
236        | (C.Lambda (name1,s1,t1), C.Lambda(_,s2,t2)) 
237        | (C.Prod (name1,s1,t1), C.Prod(_,s2,t2)) ->
238            let metasenv, subst = unify hdb true metasenv subst context s1 s2 in
239            unify hdb test_eq_only metasenv subst ((name1, C.Decl s1)::context) t1 t2
240        | (C.LetIn (name1,ty1,s1,t1), C.LetIn(_,ty2,s2,t2)) ->
241            let metasenv,subst=unify hdb test_eq_only metasenv subst context ty1 ty2 in
242            let metasenv,subst=unify hdb test_eq_only metasenv subst context s1 s2 in
243            let context = (name1, C.Def (s1,ty1))::context in
244            unify hdb test_eq_only metasenv subst context t1 t2
245
246        | (C.Meta (n1,(s1,l1 as lc1)),C.Meta (n2,(s2,l2 as lc2))) when n1 = n2 ->
247           (try 
248            let l1 = NCicUtils.expand_local_context l1 in
249            let l2 = NCicUtils.expand_local_context l2 in
250            let metasenv, subst, to_restrict, _ =
251             List.fold_right2 
252              (fun t1 t2 (metasenv, subst, to_restrict, i) -> 
253                 try 
254                   let metasenv, subst = 
255                    unify hdb test_eq_only metasenv subst context 
256                     (NCicSubstitution.lift s1 t1) (NCicSubstitution.lift s2 t2)
257                   in
258                   metasenv, subst, to_restrict, i-1  
259                 with UnificationFailure _ | Uncertain _ ->
260                   metasenv, subst, i::to_restrict, i-1)
261              l1 l2 (metasenv, subst, [], List.length l1)
262            in
263            if to_restrict <> [] then
264              let metasenv, subst, _ = 
265                NCicMetaSubst.restrict metasenv subst n1 to_restrict
266              in
267                metasenv, subst
268            else metasenv, subst
269           with 
270            | Invalid_argument _ -> assert false
271            | NCicMetaSubst.MetaSubstFailure msg ->
272               try 
273                 let _,_,term,_ = NCicUtils.lookup_subst n1 subst in
274                 let term1 = NCicSubstitution.subst_meta lc1 term in
275                 let term2 = NCicSubstitution.subst_meta lc2 term in
276                   unify hdb test_eq_only metasenv subst context term1 term2
277               with NCicUtils.Subst_not_found _-> raise (UnificationFailure msg))
278
279        | C.Meta (n,lc), t -> 
280            (try 
281              let _,_,term,_ = NCicUtils.lookup_subst n subst in
282              let term = NCicSubstitution.subst_meta lc term in
283                unify hdb test_eq_only metasenv subst context term t
284            with NCicUtils.Subst_not_found _-> 
285              instantiate hdb test_eq_only metasenv subst context n lc 
286                (NCicReduction.head_beta_reduce ~subst t) false)
287
288        | t, C.Meta (n,lc) -> 
289            (try 
290              let _,_,term,_ = NCicUtils.lookup_subst n subst in
291              let term = NCicSubstitution.subst_meta lc term in
292                unify hdb test_eq_only metasenv subst context t term
293            with NCicUtils.Subst_not_found _-> 
294              instantiate hdb test_eq_only metasenv subst context n lc 
295               (NCicReduction.head_beta_reduce ~subst t) true)
296
297        | NCic.Appl (NCic.Meta (i,l)::args), _ when List.mem_assoc i subst ->
298             let _,_,term,_ = NCicUtils.lookup_subst i subst in
299             let term = NCicSubstitution.subst_meta l term in
300               unify hdb test_eq_only metasenv subst context 
301                 (mk_appl ~upto:(List.length args) term args) t2
302
303        | _, NCic.Appl (NCic.Meta (i,l)::args) when List.mem_assoc i subst ->
304             let _,_,term,_ = NCicUtils.lookup_subst i subst in
305             let term = NCicSubstitution.subst_meta l term in
306               unify hdb test_eq_only metasenv subst context t1 
307                 (mk_appl ~upto:(List.length args) term args)
308
309        |  NCic.Appl (NCic.Meta (i,_)::_ as l1),
310           NCic.Appl (NCic.Meta (j,_)::_ as l2) when i=j ->
311             (try
312               List.fold_left2 
313                 (fun (metasenv, subst) t1 t2 ->
314                   unify hdb test_eq_only metasenv subst context t1 t2)
315                 (metasenv,subst) l1 l2
316             with Invalid_argument _ -> 
317               raise (fail_exc metasenv subst context t1 t2))
318
319        | NCic.Appl (NCic.Meta (i,l)::args), _ when not (flexible args) ->
320            (* we verify that none of the args is a Meta, 
321               since beta expanding w.r.t a metavariable makes no sense  *)
322               let metasenv, beta_expanded =
323                 beta_expand_many metasenv subst context t2 args
324               in
325               let metasenv, subst = 
326                 unify hdb test_eq_only metasenv subst context 
327                   (C.Meta (i,l)) beta_expanded
328               in
329               
330               (* .... eta_reduce ?i ... *)
331                 unify hdb test_eq_only metasenv subst context t1 t2 
332
333        | _, NCic.Appl (NCic.Meta (i,l)::args) when not(flexible args) ->
334               let metasenv, beta_expanded =
335                 beta_expand_many metasenv subst context t1 args
336               in
337               let metasenv, subst =
338                 unify hdb test_eq_only metasenv subst context 
339                  beta_expanded (C.Meta (i,l))
340               in
341                 unify hdb test_eq_only metasenv subst context t1 t2 
342
343        (* processing this case here we avoid a useless small delta step *)
344        | (C.Appl ((C.Const r1) as _hd1::tl1), C.Appl (C.Const r2::tl2)) 
345          when Ref.eq r1 r2 ->
346            let relevance = NCicEnvironment.get_relevance r1 in
347            let relevance = match r1 with
348              | Ref.Ref (_,Ref.Con (_,_,lno)) ->
349                  let _,relevance = HExtlib.split_nth lno relevance in
350                    HExtlib.mk_list false lno @ relevance
351              | _ -> relevance
352            in
353            let metasenv, subst, _ = 
354              try
355                List.fold_left2 
356                  (fun (metasenv, subst, relevance) t1 t2 ->
357                     let b, relevance = 
358                       match relevance with b::tl -> b,tl | _ -> true, [] in
359                     let metasenv, subst = 
360                       try unify hdb test_eq_only metasenv subst context t1 t2
361                       with UnificationFailure _ | Uncertain _ when not b ->
362                         metasenv, subst
363                     in
364                       metasenv, subst, relevance)
365                  (metasenv, subst, relevance) tl1 tl2
366              with Invalid_argument _ -> 
367                raise (uncert_exc metasenv subst context t1 t2)
368            in 
369              metasenv, subst
370
371        | (C.Match (Ref.Ref (_,Ref.Ind (_,tyno,_)) as ref1,outtype1,term1,pl1),
372           C.Match (ref2,outtype2,term2,pl2)) ->
373            let _,_,itl,_,_ = NCicEnvironment.get_checked_indtys ref1 in
374            let _,_,ty,_ = List.nth itl tyno in
375            let rec remove_prods ~subst context ty = 
376              let ty = NCicReduction.whd ~subst context ty in
377              match ty with
378              | C.Sort _ -> ty
379              | C.Prod (name,so,ta) -> 
380                    remove_prods ~subst ((name,(C.Decl so))::context) ta
381              | _ -> assert false
382            in
383            let is_prop = 
384              match remove_prods ~subst [] ty with
385              | C.Sort C.Prop -> true
386              | _ -> false 
387            in
388            if not (Ref.eq ref1 ref2) then 
389              raise (uncert_exc metasenv subst context t1 t2) 
390            else
391              let metasenv, subst = 
392               unify hdb test_eq_only metasenv subst context outtype1 outtype2 in
393              let metasenv, subst = 
394                try unify hdb test_eq_only metasenv subst context term1 term2 
395                with UnificationFailure _ | Uncertain _ when is_prop -> 
396                  metasenv, subst
397              in
398              (try
399               List.fold_left2 
400                (fun (metasenv,subst) -> 
401                   unify hdb test_eq_only metasenv subst context)
402                (metasenv, subst) pl1 pl2
403              with Invalid_argument _ -> 
404                raise (uncert_exc metasenv subst context t1 t2))
405        | (C.Implicit _, _) | (_, C.Implicit _) -> assert false
406        | _ when NCicUntrusted.metas_of_term subst context t1 = [] && 
407                 NCicUntrusted.metas_of_term subst context t2 = [] -> 
408                   raise (fail_exc metasenv subst context t1 t2)
409        | _ -> raise (uncert_exc metasenv subst context t1 t2)
410      (*D*)  in outside(); rc with exn -> outside (); raise exn 
411     in
412     let try_hints metasenv subst t1 t2 (* exc*) =
413 (*
414       prerr_endline ("\nProblema:\n" ^
415         NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " =?= " ^
416         NCicPp.ppterm ~metasenv ~subst ~context t2);
417 *)
418       let candidates = 
419         NCicUnifHint.look_for_hint hdb metasenv subst context t1 t2
420       in
421       let rec cand_iter = function
422         | [] -> None (* raise exc *)
423         | (metasenv,(c1,c2),premises)::tl -> 
424 (*
425             prerr_endline ("\nProvo il candidato:\n" ^ 
426               String.concat "\n"
427                 (List.map 
428                   (fun (a,b) ->
429                    NCicPp.ppterm ~metasenv ~subst ~context a ^  " =?= " ^
430                    NCicPp.ppterm ~metasenv ~subst ~context b) premises) ^
431               "\n-------------------------------------------\n"^
432               NCicPp.ppterm ~metasenv ~subst ~context c1 ^  " = " ^
433               NCicPp.ppterm ~metasenv ~subst ~context c2);
434 *)
435             try 
436               let metasenv,subst = 
437                 fo_unif test_eq_only metasenv subst t1 c1 in
438               let metasenv,subst = 
439                 fo_unif test_eq_only metasenv subst c2 t2 in
440               let metasenv,subst = 
441                 List.fold_left 
442                   (fun (metasenv, subst) (x,y) ->
443                      unify hdb test_eq_only metasenv subst context x y)
444                   (metasenv, subst) premises
445               in
446               Some (metasenv, subst)
447             with
448               UnificationFailure _ | Uncertain _ ->
449                 cand_iter tl
450       in
451         cand_iter candidates
452     in
453     let height_of = function
454      | NCic.Const (Ref.Ref (_,Ref.Def h)) 
455      | NCic.Const (Ref.Ref (_,Ref.Fix (_,_,h))) 
456      | NCic.Appl(NCic.Const(Ref.Ref(_,Ref.Def h))::_) 
457      | NCic.Appl(NCic.Const(Ref.Ref(_,Ref.Fix (_,_,h)))::_) -> h
458      | _ -> 0
459     in
460     let put_in_whd m1 m2 =
461       NCicReduction.reduce_machine ~delta:max_int ~subst context m1,
462       NCicReduction.reduce_machine ~delta:max_int ~subst context m2
463     in
464     let small_delta_step ~subst  
465       ((_,_,t1,_ as m1, norm1) as x1) ((_,_,t2,_ as m2, norm2) as x2)
466     =
467      assert (not (norm1 && norm2));
468      if norm1 then
469       x1,NCicReduction.reduce_machine ~delta:(height_of t2 -1) ~subst context m2
470      else if norm2 then
471       NCicReduction.reduce_machine ~delta:(height_of t1 -1) ~subst context m1,x2
472      else 
473       let h1 = height_of t1 in 
474       let h2 = height_of t2 in
475       let delta = if h1 = h2 then max 0 (h1 -1) else min h1 h2 in
476       NCicReduction.reduce_machine ~delta ~subst context m1,
477       NCicReduction.reduce_machine ~delta ~subst context m2
478     in
479     let rec unif_machines metasenv subst = 
480       function
481       | ((k1,e1,t1,s1),norm1 as m1),((k2,e2,t2,s2),norm2 as m2) ->
482      (*D*) inside 'M'; try let rc = 
483          pp (lazy("UM: " ^
484            NCicPp.ppterm ~metasenv ~subst ~context 
485              (NCicReduction.unwind (k1,e1,t1,s1)) ^
486            " === " ^ 
487            NCicPp.ppterm ~metasenv ~subst ~context 
488              (NCicReduction.unwind (k2,e2,t2,s2))));
489 pp (lazy (string_of_bool norm1 ^ " ?? " ^ string_of_bool norm2));
490           let relevance = [] (* TO BE UNDERSTOOD 
491             match t1 with
492             | C.Const r -> NCicEnvironment.get_relevance r
493             | _ -> [] *) in
494           let unif_from_stack t1 t2 b metasenv subst =
495               try
496                 let t1 = NCicReduction.from_stack t1 in
497                 let t2 = NCicReduction.from_stack t2 in
498                 unif_machines metasenv subst (put_in_whd t1 t2)
499               with UnificationFailure _ | Uncertain _ when not b ->
500                 metasenv, subst
501           in
502           let rec check_stack l1 l2 r todo =
503             match l1,l2,r with
504             | x1::tl1, x2::tl2, r::tr-> check_stack tl1 tl2 tr ((x1,x2,r)::todo)
505             | x1::tl1, x2::tl2, []-> check_stack tl1 tl2 [] ((x1,x2,true)::todo)
506             | l1, l2, _ -> 
507                NCicReduction.unwind (k1,e1,t1,List.rev l1),
508                 NCicReduction.unwind (k2,e2,t2,List.rev l2),
509                 todo
510           in
511         let hh1,hh2,todo=check_stack (List.rev s1) (List.rev s2) relevance [] in
512         try
513          let metasenv,subst = fo_unif test_eq_only metasenv subst hh1 hh2 in
514          List.fold_left
515           (fun (metasenv,subst) (x1,x2,r) ->
516             unif_from_stack x1 x2 r metasenv subst
517           ) (metasenv,subst) todo
518         with UnificationFailure _ | Uncertain _ when not (norm1 && norm2) ->
519             unif_machines metasenv subst (small_delta_step ~subst m1 m2)
520       (*D*)  in outside(); rc with exn -> outside (); raise exn 
521      in
522      try fo_unif test_eq_only metasenv subst t1 t2
523      with 
524      | UnificationFailure msg as exn ->
525           (try 
526             unif_machines metasenv subst 
527              (put_in_whd (0,[],t1,[]) (0,[],t2,[]))
528           with 
529           | UnificationFailure _ -> raise (UnificationFailure msg)
530           | Uncertain _ -> raise exn)
531      | Uncertain msg as exn -> 
532        match try_hints metasenv subst t1 t2 with
533        | Some x -> x
534        | None -> 
535           try 
536             unif_machines metasenv subst 
537              (put_in_whd (0,[],t1,[]) (0,[],t2,[]))
538           with 
539           | UnificationFailure _ -> raise (UnificationFailure msg)
540           | Uncertain _ -> raise exn
541  (*D*)  in outside(); rc with exn -> outside (); raise exn 
542 ;;
543
544 let unify hdb = 
545   indent := "";      
546   unify hdb false;;
547
548