]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_unification/cicUnification.ml
* comments improved an possible code weakness (i.e. less than first order
[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 let restrict to_be_restricted =
56   let rec erase i n = 
57     function
58         [] -> []
59       |        _::tl when List.mem (n,i) to_be_restricted ->
60           None::(erase (i+1) n tl) 
61       | he::tl -> he::(erase (i+1) n tl) in
62   let rec aux =
63     function 
64         [] -> []
65       |        (n,context,t)::tl -> (n,erase 1 n context,t)::(aux tl) in
66   aux
67 ;;
68
69
70 let delift context metasenv l t =
71  let module S = CicSubstitution in
72   let to_be_restricted = ref [] in
73   let rec deliftaux k =
74    let module C = Cic in
75     function
76        C.Rel m -> 
77          if m <=k then
78           C.Rel m   (*CSC: che succede se c'e' un Def? Dovrebbe averlo gia' *)
79                     (*CSC: deliftato la regola per il LetIn                 *)
80          else
81           (match List.nth context (m-k-1) with
82             Some (_,C.Def (t,_)) -> deliftaux k (S.lift m t)
83           | Some (_,C.Decl t) ->
84              (* It may augment to_be_restricted *)
85              ignore (deliftaux k (S.lift m t)) ;
86              C.Rel ((position (m-k) l) + k)
87           | None -> raise RelToHiddenHypothesis)
88      | C.Var (uri,exp_named_subst) ->
89         let exp_named_subst' =
90          List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
91         in
92          C.Var (uri,exp_named_subst')
93      | C.Meta (i, l1) as t -> 
94         let rec deliftl j =
95          function
96             [] -> []
97           | None::tl -> None::(deliftl (j+1) tl)
98           | (Some t)::tl ->
99              let l1' = (deliftl (j+1) tl) in
100               try
101                Some (deliftaux k t)::l1'
102               with
103                  RelToHiddenHypothesis
104                | NotInTheList ->
105                   to_be_restricted := (i,j)::!to_be_restricted ; None::l1'
106         in
107          let l' = deliftl 1 l1 in
108           C.Meta(i,l')
109      | C.Sort _ as t -> t
110      | C.Implicit as t -> t
111      | C.Cast (te,ty) -> C.Cast (deliftaux k te, deliftaux k ty)
112      | C.Prod (n,s,t) -> C.Prod (n, deliftaux k s, deliftaux (k+1) t)
113      | C.Lambda (n,s,t) -> C.Lambda (n, deliftaux k s, deliftaux (k+1) t)
114      | C.LetIn (n,s,t) -> C.LetIn (n, deliftaux k s, deliftaux (k+1) t)
115      | C.Appl l -> C.Appl (List.map (deliftaux k) l)
116      | C.Const (uri,exp_named_subst) ->
117         let exp_named_subst' =
118          List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
119         in
120          C.Const (uri,exp_named_subst')
121      | C.MutInd (uri,typeno,exp_named_subst) ->
122         let exp_named_subst' =
123          List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
124         in
125          C.MutInd (uri,typeno,exp_named_subst')
126      | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
127         let exp_named_subst' =
128          List.map (function (uri,t) -> uri,deliftaux k t) exp_named_subst
129         in
130          C.MutConstruct (uri,typeno,consno,exp_named_subst')
131      | C.MutCase (sp,i,outty,t,pl) ->
132         C.MutCase (sp, i, deliftaux k outty, deliftaux k t,
133          List.map (deliftaux k) pl)
134      | C.Fix (i, fl) ->
135         let len = List.length fl in
136         let liftedfl =
137          List.map
138           (fun (name, i, ty, bo) ->
139            (name, i, deliftaux k ty, deliftaux (k+len) bo))
140            fl
141         in
142          C.Fix (i, liftedfl)
143      | C.CoFix (i, fl) ->
144         let len = List.length fl in
145         let liftedfl =
146          List.map
147           (fun (name, ty, bo) -> (name, deliftaux k ty, deliftaux (k+len) bo))
148            fl
149         in
150          C.CoFix (i, liftedfl)
151   in
152    let res =
153     try
154      deliftaux 0 t
155     with
156      NotInTheList ->
157       (* This is the case where we fail even first order unification. *)
158       (* The reason is that our delift function is weaker than first  *)
159       (* order (in the sense of alpha-conversion). See comment above  *)
160       (* related to the delift function.                              *)
161 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 !!!!!!!!!!!!!!!!" ;
162       raise UnificationFailed
163    in
164     res, restrict !to_be_restricted metasenv
165 ;;
166
167 (**** END OF DELIFT ****)
168
169 type substitution = (int * Cic.term) list
170
171 (* NUOVA UNIFICAZIONE *)
172 (* A substitution is a (int * Cic.term) list that associates a
173    metavariable i with its body.
174    A metaenv is a (int * Cic.term) list that associate a metavariable
175    i with is type. 
176    fo_unif_new takes a metasenv, a context, two terms t1 and t2 and gives back
177    a new substitution which is _NOT_ unwinded. It must be unwinded before
178    applying it. *)
179  
180 let rec fo_unif_subst subst context metasenv t1 t2 =  
181  let module C = Cic in
182  let module R = CicReduction in
183  let module S = CicSubstitution in
184   match (t1, t2) with
185      (C.Meta (n,ln), C.Meta (m,lm)) when n=m ->
186        let ok =
187         List.fold_left2
188          (fun b t1 t2 ->
189            b &&
190             match t1,t2 with
191                None,_
192              | _,None -> true
193              | Some t1', Some t2' ->
194                 (* First possibility:  restriction    *)
195                 (* Second possibility: unification    *)
196                 (* Third possibility:  convertibility *)
197                 R.are_convertible context t1' t2'
198          ) true ln lm
199        in
200         if ok then subst,metasenv else raise UnificationFailed
201    | (C.Meta (n,l), C.Meta (m,_)) when n>m ->
202        fo_unif_subst subst context metasenv t2 t1
203    | (C.Meta (n,l), t)   
204    | (t, C.Meta (n,l)) ->
205        let subst',metasenv' =
206         try
207          let oldt = (List.assoc n subst) in
208          let lifted_oldt = S.lift_meta l oldt in
209           fo_unif_subst subst context metasenv lifted_oldt t
210         with Not_found ->
211          let t',metasenv' = delift context metasenv l t in
212           (n, t')::subst, metasenv'
213        in
214         let (_,_,meta_type) = 
215          List.find (function (m,_,_) -> m=n) metasenv' in
216         let tyt = CicTypeChecker.type_of_aux' metasenv' context t in
217          fo_unif_subst subst' context metasenv' (S.lift_meta l meta_type) tyt
218    | (C.Var (uri1,exp_named_subst1),C.Var (uri2,exp_named_subst2))
219    | (C.Const (uri1,exp_named_subst1),C.Const (uri2,exp_named_subst2)) ->
220       if UriManager.eq uri1 uri2 then
221        fo_unif_subst_exp_named_subst subst context metasenv
222         exp_named_subst1 exp_named_subst2
223       else
224        raise UnificationFailed
225    | C.MutInd (uri1,i1,exp_named_subst1),C.MutInd (uri2,i2,exp_named_subst2) ->
226       if UriManager.eq uri1 uri2 && i1 = i2 then
227        fo_unif_subst_exp_named_subst subst context metasenv
228         exp_named_subst1 exp_named_subst2
229       else
230        raise UnificationFailed
231    | C.MutConstruct (uri1,i1,j1,exp_named_subst1),
232      C.MutConstruct (uri2,i2,j2,exp_named_subst2) ->
233       if UriManager.eq uri1 uri2 && i1 = i2 && j1 = j2 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.Rel _, _)
239    | (_,  C.Rel _) 
240    | (C.Sort _ ,_)
241    | (_, C.Sort _)
242    | (C.Implicit, _)
243    | (_, C.Implicit) -> 
244        if R.are_convertible context t1 t2 then
245         subst, metasenv
246        else
247         raise UnificationFailed
248    | (C.Cast (te,ty), t2) -> fo_unif_subst subst context metasenv te t2
249    | (t1, C.Cast (te,ty)) -> fo_unif_subst subst context metasenv t1 te
250    | (C.Prod (n1,s1,t1), C.Prod (_,s2,t2)) -> 
251        let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
252         fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
253    | (C.Lambda (n1,s1,t1), C.Lambda (_,s2,t2)) -> 
254        let subst',metasenv' = fo_unif_subst subst context metasenv s1 s2 in
255         fo_unif_subst subst' ((Some (n1,(C.Decl s1)))::context) metasenv' t1 t2
256    | (C.LetIn (_,s1,t1), t2)  
257    | (t2, C.LetIn (_,s1,t1)) -> 
258        fo_unif_subst subst context metasenv t2 (S.subst s1 t1)
259    | (C.Appl l1, C.Appl l2) -> 
260        let lr1 = List.rev l1 in
261        let lr2 = List.rev l2 in
262        let rec fo_unif_l subst metasenv =
263         function
264            [],_
265          | _,[] -> assert false
266          | ([h1],[h2]) ->
267              fo_unif_subst subst context metasenv h1 h2
268          | ([h],l) 
269          | (l,[h]) ->
270              fo_unif_subst subst context metasenv h (C.Appl (List.rev l))
271          | ((h1::l1),(h2::l2)) -> 
272             let subst', metasenv' = 
273              fo_unif_subst subst context metasenv h1 h2
274             in 
275              fo_unif_l subst' metasenv' (l1,l2)
276        in
277         fo_unif_l subst metasenv (lr1, lr2) 
278    | (C.Const _, _) 
279    | (_, C.Const _)
280    | (C.MutInd  _, _) 
281    | (_, C.MutInd _)
282    | (C.MutConstruct _, _)
283    | (_, C.MutConstruct _) -> 
284       if R.are_convertible context t1 t2 then
285        subst, metasenv
286       else
287        raise UnificationFailed
288    | (C.MutCase (_,_,outt1,t1,pl1), C.MutCase (_,_,outt2,t2,pl2))->
289        let subst', metasenv' = 
290         fo_unif_subst subst context metasenv outt1 outt2 in
291        let subst'',metasenv'' = 
292         fo_unif_subst subst' context metasenv' t1 t2 in
293        List.fold_left2 
294         (function (subst,metasenv) ->
295           fo_unif_subst subst context metasenv
296         ) (subst'',metasenv'') pl1 pl2 
297    | (C.Fix _, _)
298    | (_, C.Fix _) 
299    | (C.CoFix _, _)
300    | (_, C.CoFix _) -> 
301        if R.are_convertible context t1 t2 then
302         subst, metasenv
303        else
304         raise UnificationFailed
305    | (_,_) ->
306        if R.are_convertible context t1 t2 then
307         subst, metasenv
308        else
309         raise UnificationFailed
310
311 and fo_unif_subst_exp_named_subst subst context metasenv
312  exp_named_subst1 exp_named_subst2
313 =
314 try
315  List.fold_left2
316   (fun (subst,metasenv) (uri1,t1) (uri2,t2) ->
317     assert (uri1=uri2) ;
318     fo_unif_subst subst context metasenv t1 t2
319   ) (subst,metasenv) exp_named_subst1 exp_named_subst2
320 with
321 e ->
322 let uri = UriManager.uri_of_string "cic:/dummy.var" in
323 prerr_endline ("@@@: " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst1)) ^
324 " <==> " ^ CicPp.ppterm (Cic.Var (uri,exp_named_subst2))) ; raise e
325 ;;
326
327 let unwind metasenv subst unwinded t =
328  let unwinded = ref unwinded in
329  let frozen = ref [] in
330  let rec um_aux metasenv =
331   let module C = Cic in
332   let module S = CicSubstitution in 
333    function
334       C.Rel _ as t -> t,metasenv
335     | C.Var _  as t -> t,metasenv
336     | C.Meta (i,l) -> 
337         (try
338           S.lift_meta l (List.assoc i !unwinded), metasenv
339          with Not_found ->
340            if List.mem i !frozen then raise OccurCheck
341            else
342             let saved_frozen = !frozen in 
343             frozen := i::!frozen ;
344             let res =
345              try
346               let t = List.assoc i subst in
347               let t',metasenv' = um_aux metasenv t in
348               let _,metasenv'' =
349                let (_,canonical_context,_) = 
350                 List.find (function (m,_,_) -> m=i) metasenv
351                in
352                 delift canonical_context metasenv' l t'
353               in
354                unwinded := (i,t')::!unwinded ;
355                S.lift_meta l t', metasenv'
356              with
357               Not_found ->
358                (* not constrained variable, i.e. free in subst*)
359                let l',metasenv' =
360                 List.fold_right
361                  (fun t (tl,metasenv) ->
362                    match t with
363                       None -> None::tl,metasenv
364                     | Some t -> 
365                        let t',metasenv' = um_aux metasenv t in
366                         (Some t')::tl, metasenv'
367                  ) l ([],metasenv)
368                in
369                 C.Meta (i,l'), metasenv'
370             in
371             frozen := saved_frozen ;
372             res
373         ) 
374     | C.Sort _
375     | C.Implicit as t -> t,metasenv
376     | C.Cast (te,ty) ->
377        let te',metasenv' = um_aux metasenv te in
378        let ty',metasenv'' = um_aux metasenv' ty in
379        C.Cast (te',ty'),metasenv''
380     | C.Prod (n,s,t) ->
381        let s',metasenv' = um_aux metasenv s in
382        let t',metasenv'' = um_aux metasenv' t in
383        C.Prod (n, s', t'), metasenv''
384     | C.Lambda (n,s,t) ->
385        let s',metasenv' = um_aux metasenv s in
386        let t',metasenv'' = um_aux metasenv' t in
387        C.Lambda (n, s', t'), metasenv''
388     | C.LetIn (n,s,t) ->
389        let s',metasenv' = um_aux metasenv s in
390        let t',metasenv'' = um_aux metasenv' t in
391        C.LetIn (n, s', t'), metasenv''
392     | C.Appl (he::tl) ->
393        let tl',metasenv' =
394         List.fold_right
395          (fun t (tl,metasenv) ->
396            let t',metasenv' = um_aux metasenv t in
397             t'::tl, metasenv'
398          ) tl ([],metasenv)
399        in
400         begin
401          match um_aux metasenv' he with
402             (C.Appl l, metasenv'') -> C.Appl (l@tl'),metasenv''
403           | (he', metasenv'') -> C.Appl (he'::tl'),metasenv''
404         end
405     | C.Appl _ -> assert false
406     | C.Const (uri,exp_named_subst) ->
407        let exp_named_subst', metasenv' =
408         List.fold_right
409          (fun (uri,t) (tl,metasenv) ->
410            let t',metasenv' = um_aux metasenv t in
411             (uri,t')::tl, metasenv'
412          ) exp_named_subst ([],metasenv)
413        in
414         C.Const (uri,exp_named_subst'),metasenv'
415     | C.MutInd (uri,typeno,exp_named_subst) ->
416        let exp_named_subst', metasenv' =
417         List.fold_right
418          (fun (uri,t) (tl,metasenv) ->
419            let t',metasenv' = um_aux metasenv t in
420             (uri,t')::tl, metasenv'
421          ) exp_named_subst ([],metasenv)
422        in
423         C.MutInd (uri,typeno,exp_named_subst'),metasenv'
424     | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
425        let exp_named_subst', metasenv' =
426         List.fold_right
427          (fun (uri,t) (tl,metasenv) ->
428            let t',metasenv' = um_aux metasenv t in
429             (uri,t')::tl, metasenv'
430          ) exp_named_subst ([],metasenv)
431        in
432         C.MutConstruct (uri,typeno,consno,exp_named_subst'),metasenv'
433     | C.MutCase (sp,i,outty,t,pl) ->
434        let outty',metasenv' = um_aux metasenv outty in
435        let t',metasenv'' = um_aux metasenv' t in
436        let pl',metasenv''' =
437         List.fold_right
438          (fun p (pl,metasenv) ->
439            let p',metasenv' = um_aux metasenv p in
440             p'::pl, metasenv'
441          ) pl ([],metasenv'')
442        in
443         C.MutCase (sp, i, outty', t', pl'),metasenv'''
444     | C.Fix (i, fl) ->
445        let len = List.length fl in
446        let liftedfl,metasenv' =
447         List.fold_right
448          (fun (name, i, ty, bo) (fl,metasenv) ->
449            let ty',metasenv' = um_aux metasenv ty in
450            let bo',metasenv'' = um_aux metasenv' bo in
451             (name, i, ty', bo')::fl,metasenv''
452          ) fl ([],metasenv)
453        in
454         C.Fix (i, liftedfl),metasenv'
455     | C.CoFix (i, fl) ->
456        let len = List.length fl in
457        let liftedfl,metasenv' =
458         List.fold_right
459          (fun (name, ty, bo) (fl,metasenv) ->
460            let ty',metasenv' = um_aux metasenv ty in
461            let bo',metasenv'' = um_aux metasenv' bo in
462             (name, ty', bo')::fl,metasenv''
463          ) fl ([],metasenv)
464        in
465         C.CoFix (i, liftedfl),metasenv'
466  in
467   let t',metasenv' = um_aux metasenv t in
468    t',metasenv',!unwinded 
469 ;;
470
471 (* apply_subst_reducing subst (Some (mtr,reductions_no)) t              *)
472 (* performs as (apply_subst subst t) until it finds an application of   *)
473 (* (META [meta_to_reduce]) that, once unwinding is performed, creates   *)
474 (* a new beta-redex; in this case up to [reductions_no] consecutive     *)
475 (* beta-reductions are performed.                                       *)
476 (* Hint: this function is usually called when [reductions_no]           *)
477 (*  eta-expansions have been performed and the head of the new          *)
478 (*  application has been unified with (META [meta_to_reduce]):          *)
479 (*  during the unwinding the eta-expansions are undone.                 *)
480
481 let apply_subst_reducing subst meta_to_reduce t =
482  let unwinded = ref subst in
483  let rec um_aux =
484   let module C = Cic in
485   let module S = CicSubstitution in 
486    function
487       C.Rel _
488     | C.Var _  as t -> t
489     | C.Meta (i,l) as t ->
490        (try
491          S.lift_meta l (List.assoc i !unwinded)
492         with Not_found ->
493           C.Meta (i,l))
494     | C.Sort _ as t -> t
495     | C.Implicit as t -> t
496     | C.Cast (te,ty) -> C.Cast (um_aux te, um_aux ty)
497     | C.Prod (n,s,t) -> C.Prod (n, um_aux s, um_aux t)
498     | C.Lambda (n,s,t) -> C.Lambda (n, um_aux s, um_aux t)
499     | C.LetIn (n,s,t) -> C.LetIn (n, um_aux s, um_aux t)
500     | C.Appl (he::tl) ->
501        let tl' = List.map um_aux tl in
502         let t' =
503          match um_aux he with
504             C.Appl l -> C.Appl (l@tl')
505           | _ as he' -> C.Appl (he'::tl')
506         in
507          begin
508           match meta_to_reduce,he with
509              Some (mtr,reductions_no), C.Meta (m,_) when m = mtr ->
510               let rec beta_reduce =
511                function
512                   (n,(C.Appl (C.Lambda (_,_,t)::he'::tl'))) when n > 0 ->
513                     let he'' = CicSubstitution.subst he' t in
514                      if tl' = [] then
515                       he''
516                      else
517                       beta_reduce (n-1,C.Appl(he''::tl'))
518                 | (_,t) -> t
519               in
520                beta_reduce (reductions_no,t')
521            | _,_ -> t'
522          end
523     | C.Appl _ -> assert false
524     | C.Const (uri,exp_named_subst) ->
525        let exp_named_subst' =
526         List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
527        in
528         C.Const (uri,exp_named_subst')
529     | C.MutInd (uri,typeno,exp_named_subst) ->
530        let exp_named_subst' =
531         List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
532        in
533         C.MutInd (uri,typeno,exp_named_subst')
534     | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
535        let exp_named_subst' =
536         List.map (function (uri,t) -> (uri,um_aux t)) exp_named_subst
537        in
538         C.MutConstruct (uri,typeno,consno,exp_named_subst')
539     | C.MutCase (sp,i,outty,t,pl) ->
540        C.MutCase (sp, i, um_aux outty, um_aux t,
541         List.map um_aux pl)
542     | C.Fix (i, fl) ->
543        let len = List.length fl in
544        let liftedfl =
545         List.map
546          (fun (name, i, ty, bo) -> (name, i, um_aux ty, um_aux bo))
547           fl
548        in
549         C.Fix (i, liftedfl)
550     | C.CoFix (i, fl) ->
551        let len = List.length fl in
552        let liftedfl =
553         List.map
554          (fun (name, ty, bo) -> (name, um_aux ty, um_aux bo))
555           fl
556        in
557         C.CoFix (i, liftedfl)
558  in
559    um_aux t
560 ;;
561
562 (* UNWIND THE MGU INSIDE THE MGU *)
563 let unwind_subst metasenv subst =
564  let identity_relocation_list_for_metavariable i =
565   let (_,canonical_context,_) =
566    List.find (function (m,_,_) -> m=i) metasenv
567   in
568    let canonical_context_length = List.length canonical_context in
569     let rec aux =
570      function
571         n when n > canonical_context_length -> []
572       | n -> (Some (Cic.Rel n))::(aux (n+1))
573     in
574      aux 1
575  in
576   List.fold_left
577    (fun (unwinded,metasenv) (i,_) ->
578      let identity_relocation_list =
579       identity_relocation_list_for_metavariable i
580      in
581       let (_,metasenv',subst') =
582        unwind metasenv subst unwinded (Cic.Meta (i,identity_relocation_list))
583       in
584        subst',metasenv'
585    ) ([],metasenv) subst
586 ;;
587
588 let apply_subst subst t = 
589  (* metasenv will not be used nor modified. So, let's use a dummy empty one *)
590  let metasenv = [] in
591   let (t',_,_) = unwind metasenv [] subst t in
592    t'
593 ;;
594
595 (* A substitution is a (int * Cic.term) list that associates a               *)
596 (* metavariable i with its body.                                             *)
597 (* metasenv is of type Cic.metasenv                                          *)
598 (* fo_unif takes a metasenv, a context, two terms t1 and t2 and gives back   *)
599 (* a new substitution which is already unwinded and ready to be applied and  *)
600 (* a new metasenv in which some hypothesis in the contexts of the            *)
601 (* metavariables may have been restricted.                                   *)
602 let fo_unif metasenv context t1 t2 =
603  let subst_to_unwind,metasenv' = fo_unif_subst [] context metasenv t1 t2 in
604   unwind_subst metasenv' subst_to_unwind
605 ;;