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