]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_unification/cicUnification.ml
Big bug spotted: restriction can fail and it was implicitly assumed that it
[helm.git] / helm / ocaml / cic_unification / cicUnification.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 exception UnificationFailed;;
27 exception Free;;
28 exception OccurCheck;;
29 exception RelToHiddenHypothesis;;
30 exception OpenTerm;;
31
32 (**** DELIFT ****)
33
34 (* the delift function takes in input an ordered list of optional terms       *)
35 (* [t1,...,tn] and a term t, and substitutes every tk = Some (rel(nk)) with   *)
36 (* rel(k). Typically, the list of optional terms is the explicit substitution *)(* that is applied to a metavariable occurrence and the result of the delift  *)
37 (* function is a term the implicit variable can be substituted with to make   *)
38 (* the term [t] unifiable with the metavariable occurrence.                   *)
39 (* In general, the problem is undecidable if we consider equivalence in place *)
40 (* of alpha convertibility. Our implementation, though, is even weaker than   *)
41 (* alpha convertibility, since it replace the term [tk] if and only if [tk]   *)
42 (* is a Rel (missing all the other cases). Does this matter in practice?      *)
43
44 exception NotInTheList;;
45
46 let position n =
47   let rec aux k =
48    function 
49        [] -> raise NotInTheList
50      | (Some (Cic.Rel m))::_ when m=n -> k
51      | _::tl -> aux (k+1) tl in
52   aux 1
53 ;;
54  
55 (*CSC: this restriction function is utterly wrong, since it does not check  *)
56 (*CSC: that the variable that is going to be restricted does not occur free *)
57 (*CSC: in a part of the sequent that is not going to be restricted.         *)
58 (*CSC: In particular, the whole approach is wrong; if restriction can fail  *)
59 (*CSC: (as indeed it is the case), we can not collect all the restrictions  *)
60 (*CSC: and restrict everything at the end ;-(                               *)
61 let restrict to_be_restricted =
62   let rec erase i n = 
63     function
64         [] -> []
65       |        _::tl when List.mem (n,i) to_be_restricted ->
66           None::(erase (i+1) n tl) 
67       | he::tl -> he::(erase (i+1) n tl) in
68   let rec aux =
69     function 
70         [] -> []
71       |        (n,context,t)::tl -> (n,erase 1 n context,t)::(aux tl) in
72   aux
73 ;;
74
75
76 (*CSC: maybe we should rename delift in abstract, as I did in my dissertation *)
77 let delift context metasenv l t =
78  let module S = CicSubstitution in
79   let to_be_restricted = ref [] in
80   let rec deliftaux k =
81    let module C = Cic in
82     function
83        C.Rel m -> 
84          if m <=k then
85           C.Rel m   (*CSC: che succede se c'e' un Def? Dovrebbe averlo gia' *)
86                     (*CSC: deliftato la regola per il LetIn                 *)
87                     (*CSC: FALSO! La regola per il LetIn non lo fa          *)
88          else
89           (match List.nth context (m-k-1) with
90             Some (_,C.Def (t,_)) ->
91              (*CSC: Hmmm. This bit of reduction is not in the spirit of    *)
92              (*CSC: first order unification. Does it help or does it harm? *)
93              deliftaux k (S.lift m t)
94           | Some (_,C.Decl t) ->
95              (* It may augment to_be_restricted *)
96              (*CSC: Really? Even in the case of well-typed terms?      *)
97              (*CSC: I am no longer sure of the usefulness of the check *)
98              ignore (deliftaux k (S.lift m t)) ;
99              C.Rel ((position (m-k) l) + k)
100           | None -> raise RelToHiddenHypothesis)
101      | C.Var (uri,exp_named_subst) ->
102         let exp_named_subst' =
103          List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
104         in
105          C.Var (uri,exp_named_subst')
106      | C.Meta (i, l1) as t -> 
107         let rec deliftl j =
108          function
109             [] -> []
110           | None::tl -> None::(deliftl (j+1) tl)
111           | (Some t)::tl ->
112              let l1' = (deliftl (j+1) tl) in
113               try
114                Some (deliftaux k t)::l1'
115               with
116                  RelToHiddenHypothesis
117                | NotInTheList ->
118                   to_be_restricted := (i,j)::!to_be_restricted ; None::l1'
119         in
120          let l' = deliftl 1 l1 in
121           C.Meta(i,l')
122      | C.Sort _ as t -> t
123      | C.Implicit as t -> t
124      | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
125      | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
126      | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
127      | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
128      | C.Appl l -> C.Appl (List.map (deliftaux k) l)
129      | C.Const (uri,exp_named_subst) ->
130         let exp_named_subst' =
131          List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
132         in
133          C.Const (uri,exp_named_subst')
134      | C.MutInd (uri,typeno,exp_named_subst) ->
135         let exp_named_subst' =
136          List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
137         in
138          C.MutInd (uri,typeno,exp_named_subst')
139      | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
140         let exp_named_subst' =
141          List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
142         in
143          C.MutConstruct (uri,typeno,consno,exp_named_subst')
144      | C.MutCase (sp,i,outty,t,pl) ->
145         C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
146          List.map (deliftaux k) pl)
147      | C.Fix (i, fl) ->
148         let len = List.length fl in
149         let liftedfl =
150          List.map
151           (fun (name, i, ty, bo) ->
152            (name, i, deliftaux k ty, deliftaux (k+len) bo))
153            fl
154         in
155          C.Fix (i, liftedfl)
156      | C.CoFix (i, fl) ->
157         let len = List.length fl in
158         let liftedfl =
159          List.map
160           (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
161            fl
162         in
163          C.CoFix (i, liftedfl)
164   in
165    let res =
166     try
167      deliftaux 0 t
168     with
169      NotInTheList ->
170       (* This is the case where we fail even first order unification. *)
171       (* The reason is that our delift function is weaker than first  *)
172       (* order (in the sense of alpha-conversion). See comment above  *)
173       (* related to the delift function.                              *)
174 prerr_endline "!!!!!!!!!!! First Order UnificationFailed, but maybe it could have been successful even in a first order setting (no conversion, only alpha convertibility)! Please, implement a better delift function !!!!!!!!!!!!!!!!" ;
175       raise UnificationFailed
176    in
177     res, restrict !to_be_restricted metasenv
178 ;;
179
180 (**** END OF DELIFT ****)
181
182 type substitution = (int * Cic.term) list
183
184 (* NUOVA UNIFICAZIONE *)
185 (* A substitution is a (int * Cic.term) list that associates a
186    metavariable i with its body.
187    A metaenv is a (int * Cic.term) list that associate a metavariable
188    i with is type. 
189    fo_unif_new takes a metasenv, a context, two terms t1 and t2 and gives back
190    a new substitution which is _NOT_ unwinded. It must be unwinded before
191    applying it. *)
192  
193 let rec fo_unif_subst subst context metasenv t1 t2 =  
194  let module C = Cic in
195  let module R = CicReduction in
196  let module S = CicSubstitution in
197   match (t1, t2) with
198      (C.Meta (n,ln), C.Meta (m,lm)) when n=m ->
199        let ok =
200         List.fold_left2
201          (fun b t1 t2 ->
202            b &&
203             match t1,t2 with
204                None,_
205              | _,None -> true
206              | Some t1', Some t2' ->
207                 (* First possibility:  restriction    *)
208                 (* Second possibility: unification    *)
209                 (* Third possibility:  convertibility *)
210                 R.are_convertible context t1' t2'
211          ) true ln lm
212        in
213         if ok then subst,metasenv else raise UnificationFailed
214    | (C.Meta (n,l), C.Meta (m,_)) when n>m ->
215        fo_unif_subst subst context metasenv t2 t1
216    | (C.Meta (n,l), t)   
217    | (t, C.Meta (n,l)) ->
218        let subst',metasenv' =
219         try
220          let oldt = (List.assoc n subst) in
221          let lifted_oldt = S.lift_meta l oldt in
222           fo_unif_subst subst context metasenv lifted_oldt t
223         with Not_found ->
224          let t',metasenv' = delift context metasenv l t in
225           (n, t')::subst, metasenv'
226        in
227         let (_,_,meta_type) = 
228          List.find (function (m,_,_) -> m=n) metasenv' in
229         let tyt = CicTypeChecker.type_of_aux' metasenv' context t in
230          fo_unif_subst subst' context metasenv' (S.lift_meta l meta_type) tyt
231    | (C.Var (uri1,exp_named_subst1),C.Var (uri2,exp_named_subst2))
232    | (C.Const (uri1,exp_named_subst1),C.Const (uri2,exp_named_subst2)) ->
233       if UriManager.eq uri1 uri2 then
234        fo_unif_subst_exp_named_subst subst context metasenv
235         exp_named_subst1 exp_named_subst2
236       else
237        raise UnificationFailed
238    | C.MutInd (uri1,i1,exp_named_subst1),C.MutInd (uri2,i2,exp_named_subst2) ->
239       if UriManager.eq uri1 uri2 && i1 = i2 then
240        fo_unif_subst_exp_named_subst subst context metasenv
241         exp_named_subst1 exp_named_subst2
242       else
243        raise UnificationFailed
244    | C.MutConstruct (uri1,i1,j1,exp_named_subst1),
245      C.MutConstruct (uri2,i2,j2,exp_named_subst2) ->
246       if UriManager.eq uri1 uri2 && i1 = i2 && j1 = j2 then
247        fo_unif_subst_exp_named_subst subst context metasenv
248         exp_named_subst1 exp_named_subst2
249       else
250        raise UnificationFailed
251    | (C.Rel _, _)
252    | (_,  C.Rel _) 
253    | (C.Sort _ ,_)
254    | (_, C.Sort _)
255    | (C.Implicit, _)
256    | (_, C.Implicit) -> 
257        if R.are_convertible context t1 t2 then
258         subst, metasenv
259        else
260         raise UnificationFailed
261    | (C.Cast (te,ty), t2) -> fo_unif_subst subst context metasenv te t2
262    | (t1, C.Cast (te,ty)) -> fo_unif_subst subst context metasenv t1 te
263    | (C.Prod (n1,s1,t1), C.Prod (_,s2,t2)) -> 
264        let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
265         fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
266    | (C.Lambda (n1,s1,t1), C.Lambda (_,s2,t2)) -> 
267        let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
268         fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
269    | (C.LetIn (_,s1,t1), t2)  
270    | (t2, C.LetIn (_,s1,t1)) -> 
271        fo_unif_subst subst context metasenv t2 (S.subst s1 t1)
272    | (C.Appl l1, C.Appl l2) -> 
273        let lr1 = List.rev l1 in
274        let lr2 = List.rev l2 in
275        let rec fo_unif_l subst metasenv =
276         function
277            [],_
278          | _,[] -> assert false
279          | ([h1],[h2]) ->
280              fo_unif_subst subst context metasenv h1 h2
281          | ([h],l) 
282          | (l,[h]) ->
283              fo_unif_subst subst context metasenv h (C.Appl (List.rev l))
284          | ((h1::l1),(h2::l2)) -> 
285             let subst', metasenv' = 
286              fo_unif_subst subst context metasenv h1 h2
287             in 
288              fo_unif_l subst' metasenv' (l1,l2)
289        in
290         fo_unif_l subst metasenv (lr1, lr2) 
291    | (C.Const _, _) 
292    | (_, C.Const _)
293    | (C.MutInd  _, _) 
294    | (_, C.MutInd _)
295    | (C.MutConstruct _, _)
296    | (_, C.MutConstruct _) -> 
297       if R.are_convertible context t1 t2 then
298        subst, metasenv
299       else
300        raise UnificationFailed
301    | (C.MutCase (_,_,outt1,t1,pl1), C.MutCase (_,_,outt2,t2,pl2))->
302        let subst', metasenv' = 
303         fo_unif_subst subst context metasenv outt1 outt2 in
304        let subst'',metasenv'' = 
305         fo_unif_subst subst' context metasenv' t1 t2 in
306        List.fold_left2 
307         (function (subst,metasenv) ->
308           fo_unif_subst subst context metasenv
309         ) (subst'',metasenv'') pl1 pl2 
310    | (C.Fix _, _)
311    | (_, C.Fix _) 
312    | (C.CoFix _, _)
313    | (_, C.CoFix _) -> 
314        if R.are_convertible context t1 t2 then
315         subst, metasenv
316        else
317         raise UnificationFailed
318    | (_,_) ->
319        if R.are_convertible context t1 t2 then
320         subst, metasenv
321        else
322         raise UnificationFailed
323
324 and fo_unif_subst_exp_named_subst subst context metasenv
325  exp_named_subst1 exp_named_subst2
326 =
327 try
328  List.fold_left2
329   (fun (subst,metasenv) (uri1,t1) (uri2,t2) ->
330     assert (uri1=uri2) ;
331     fo_unif_subst subst context metasenv t1 t2
332   ) (subst,metasenv) exp_named_subst1 exp_named_subst2
333 with
334 e ->
335 let uri = UriManager.uri_of_string "cic:/dummy.var" in
336 prerr_endline ("@@@: " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst1)) ^
337 " <==> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst2))) ; raise e
338 ;;
339
340 let unwind metasenv subst unwinded t =
341  let unwinded = ref unwinded in
342  let frozen = ref [] in
343  let rec um_aux metasenv =
344   let module C = Cic in
345   let module S = CicSubstitution in 
346    function
347       C.Rel _ as t -> t,metasenv
348     | C.Var _  as t -> t,metasenv
349     | C.Meta (i,l) -> 
350         (try
351           S.lift_meta l (List.assoc i !unwinded), metasenv
352          with Not_found ->
353            if List.mem i !frozen then raise OccurCheck
354            else
355             let saved_frozen = !frozen in 
356             frozen := i::!frozen ;
357             let res =
358              try
359               let t = List.assoc i subst in
360               let t',metasenv' = um_aux metasenv t in
361               let _,metasenv'' =
362                let (_,canonical_context,_) = 
363                 List.find (function (m,_,_) -> m=i) metasenv
364                in
365                 delift canonical_context metasenv' l t'
366               in
367                unwinded := (i,t')::!unwinded ;
368                S.lift_meta l t', metasenv'
369              with
370               Not_found ->
371                (* not constrained variable, i.e. free in subst*)
372                let l',metasenv' =
373                 List.fold_right
374                  (fun t (tl,metasenv) ->
375                    match t with
376                       None -> None::tl,metasenv
377                     | Some t -> 
378                        let t',metasenv' = um_aux metasenv t in
379                         (Some t')::tl, metasenv'
380                  ) l ([],metasenv)
381                in
382                 C.Meta (i,l'), metasenv'
383             in
384             frozen := saved_frozen ;
385             res
386         ) 
387     | C.Sort _
388     | C.Implicit as t -> t,metasenv
389     | C.Cast (te,ty) ->
390        let te',metasenv' = um_aux metasenv te in
391        let ty',metasenv'' = um_aux metasenv' ty in
392        C.Cast (te',ty'),metasenv''
393     | C.Prod (n,s,t) ->
394        let s',metasenv' = um_aux metasenv s in
395        let t',metasenv'' = um_aux metasenv' t in
396        C.Prod (n, s', t'), metasenv''
397     | C.Lambda (n,s,t) ->
398        let s',metasenv' = um_aux metasenv s in
399        let t',metasenv'' = um_aux metasenv' t in
400        C.Lambda (n, s', t'), metasenv''
401     | C.LetIn (n,s,t) ->
402        let s',metasenv' = um_aux metasenv s in
403        let t',metasenv'' = um_aux metasenv' t in
404        C.LetIn (n, s', t'), metasenv''
405     | C.Appl (he::tl) ->
406        let tl',metasenv' =
407         List.fold_right
408          (fun t (tl,metasenv) ->
409            let t',metasenv' = um_aux metasenv t in
410             t'::tl, metasenv'
411          ) tl ([],metasenv)
412        in
413         begin
414          match um_aux metasenv' he with
415             (C.Appl l, metasenv'') -> C.Appl (l@tl'),metasenv''
416           | (he', metasenv'') -> C.Appl (he'::tl'),metasenv''
417         end
418     | C.Appl _ -> assert false
419     | C.Const (uri,exp_named_subst) ->
420        let exp_named_subst', metasenv' =
421         List.fold_right
422          (fun (uri,t) (tl,metasenv) ->
423            let t',metasenv' = um_aux metasenv t in
424             (uri,t')::tl, metasenv'
425          ) exp_named_subst ([],metasenv)
426        in
427         C.Const (uri,exp_named_subst'),metasenv'
428     | C.MutInd (uri,typeno,exp_named_subst) ->
429        let exp_named_subst', metasenv' =
430         List.fold_right
431          (fun (uri,t) (tl,metasenv) ->
432            let t',metasenv' = um_aux metasenv t in
433             (uri,t')::tl, metasenv'
434          ) exp_named_subst ([],metasenv)
435        in
436         C.MutInd (uri,typeno,exp_named_subst'),metasenv'
437     | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
438        let exp_named_subst', metasenv' =
439         List.fold_right
440          (fun (uri,t) (tl,metasenv) ->
441            let t',metasenv' = um_aux metasenv t in
442             (uri,t')::tl, metasenv'
443          ) exp_named_subst ([],metasenv)
444        in
445         C.MutConstruct (uri,typeno,consno,exp_named_subst'),metasenv'
446     | C.MutCase (sp,i,outty,t,pl) ->
447        let outty',metasenv' = um_aux metasenv outty in
448        let t',metasenv'' = um_aux metasenv' t in
449        let pl',metasenv''' =
450         List.fold_right
451          (fun p (pl,metasenv) ->
452            let p',metasenv' = um_aux metasenv p in
453             p'::pl, metasenv'
454          ) pl ([],metasenv'')
455        in
456         C.MutCase (sp, i, outty', t', pl'),metasenv'''
457     | C.Fix (i, fl) ->
458        let len = List.length fl in
459        let liftedfl,metasenv' =
460         List.fold_right
461          (fun (name, i, ty, bo) (fl,metasenv) ->
462            let ty',metasenv' = um_aux metasenv ty in
463            let bo',metasenv'' = um_aux metasenv' bo in
464             (name, i, ty', bo')::fl,metasenv''
465          ) fl ([],metasenv)
466        in
467         C.Fix (i, liftedfl),metasenv'
468     | C.CoFix (i, fl) ->
469        let len = List.length fl in
470        let liftedfl,metasenv' =
471         List.fold_right
472          (fun (name, ty, bo) (fl,metasenv) ->
473            let ty',metasenv' = um_aux metasenv ty in
474            let bo',metasenv'' = um_aux metasenv' bo in
475             (name, ty', bo')::fl,metasenv''
476          ) fl ([],metasenv)
477        in
478         C.CoFix (i, liftedfl),metasenv'
479  in
480   let t',metasenv' = um_aux metasenv t in
481    t',metasenv',!unwinded 
482 ;;
483
484 (* apply_subst_reducing subst (Some (mtr,reductions_no)) t              *)
485 (* performs as (apply_subst subst t) until it finds an application of   *)
486 (* (META [meta_to_reduce]) that, once unwinding is performed, creates   *)
487 (* a new beta-redex; in this case up to [reductions_no] consecutive     *)
488 (* beta-reductions are performed.                                       *)
489 (* Hint: this function is usually called when [reductions_no]           *)
490 (*  eta-expansions have been performed and the head of the new          *)
491 (*  application has been unified with (META [meta_to_reduce]):          *)
492 (*  during the unwinding the eta-expansions are undone.                 *)
493
494 let apply_subst_reducing subst meta_to_reduce t =
495  let unwinded = ref subst in
496  let rec um_aux =
497   let module C = Cic in
498   let module S = CicSubstitution in 
499    function
500       C.Rel _
501     | C.Var _  as t -> t
502     | C.Meta (i,l) as t ->
503        (try
504          S.lift_meta l (List.assoc i !unwinded)
505         with Not_found ->
506           C.Meta (i,l))
507     | C.Sort _ as t -> t
508     | C.Implicit as t -> t
509     | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
510     | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
511     | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
512     | C.LetIn (n,s,t) -> C.LetIn (n, um_aux s, um_aux t)
513     | C.Appl (he::tl) ->
514        let tl' = List.map um_aux tl in
515         let t' =
516          match um_aux he with
517             C.Appl l -> C.Appl (l@tl')
518           | _ as he' -> C.Appl (he'::tl')
519         in
520          begin
521           match meta_to_reduce,he with
522              Some (mtr,reductions_no), C.Meta (m,_) when m = mtr ->
523               let rec beta_reduce =
524                function
525                   (n,(C.Appl (C.Lambda (_,_,t)::he'::tl'))) when n > 0 ->
526                     let he'' = CicSubstitution.subst he' t in
527                      if tl' = [] then
528                       he''
529                      else
530                       beta_reduce (n-1,C.Appl(he''::tl'))
531                 | (_,t) -> t
532               in
533                beta_reduce (reductions_no,t')
534            | _,_ -> t'
535          end
536     | C.Appl _ -> assert false
537     | C.Const (uri,exp_named_subst) ->
538        let exp_named_subst' =
539         List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
540        in
541         C.Const (uri,exp_named_subst')
542     | C.MutInd (uri,typeno,exp_named_subst) ->
543        let exp_named_subst' =
544         List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
545        in
546         C.MutInd (uri,typeno,exp_named_subst')
547     | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
548        let exp_named_subst' =
549         List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
550        in
551         C.MutConstruct (uri,typeno,consno,exp_named_subst')
552     | C.MutCase (sp,i,outty,t,pl) ->
553        C.MutCase (sp, i, um_aux outty, um_aux t,
554         List.map um_aux pl)
555     | C.Fix (i, fl) ->
556        let len = List.length fl in
557        let liftedfl =
558         List.map
559          (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo))
560           fl
561        in
562         C.Fix (i, liftedfl)
563     | C.CoFix (i, fl) ->
564        let len = List.length fl in
565        let liftedfl =
566         List.map
567          (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo))
568           fl
569        in
570         C.CoFix (i, liftedfl)
571  in
572    um_aux t
573 ;;
574
575 (* UNWIND THE MGU INSIDE THE MGU *)
576 let unwind_subst metasenv subst =
577  let identity_relocation_list_for_metavariable i =
578   let (_,canonical_context,_) =
579    List.find (function (m,_,_) -> m=i) metasenv
580   in
581    let canonical_context_length = List.length canonical_context in
582     let rec aux =
583      function
584         n when n > canonical_context_length -> []
585       | n -> (Some (Cic.Rel n))::(aux (n+1))
586     in
587      aux 1
588  in
589   List.fold_left
590    (fun (unwinded,metasenv) (i,_) ->
591      let identity_relocation_list =
592       identity_relocation_list_for_metavariable i
593      in
594       let (_,metasenv',subst') =
595        unwind metasenv subst unwinded (Cic.Meta (i,identity_relocation_list))
596       in
597        subst',metasenv'
598    ) ([],metasenv) subst
599 ;;
600
601 let apply_subst subst t = 
602  (* metasenv will not be used nor modified. So, let's use a dummy empty one *)
603  let metasenv = [] in
604   let (t',_,_) = unwind metasenv [] subst t in
605    t'
606 ;;
607
608 (* A substitution is a (int * Cic.term) list that associates a               *)
609 (* metavariable i with its body.                                             *)
610 (* metasenv is of type Cic.metasenv                                          *)
611 (* fo_unif takes a metasenv, a context, two terms t1 and t2 and gives back   *)
612 (* a new substitution which is already unwinded and ready to be applied and  *)
613 (* a new metasenv in which some hypothesis in the contexts of the            *)
614 (* metavariables may have been restricted.                                   *)
615 let fo_unif metasenv context t1 t2 =
616  let subst_to_unwind,metasenv' = fo_unif_subst [] context metasenv t1 t2 in
617   unwind_subst metasenv' subst_to_unwind
618 ;;