]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/paramodulation/inference.ml
New unification and new matching.
[helm.git] / components / tactics / paramodulation / inference.ml
1 (* Copyright (C) 2005, 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 (* $Id$ *)
27
28 open Utils;;
29
30 let metas_of_proof_time = ref 0.;;
31 let metas_of_term_time = ref 0.;;
32
33 type equality =
34     int  *               (* weight *)
35     proof * 
36     (Cic.term *          (* type *)
37      Cic.term *          (* left side *)
38      Cic.term *          (* right side *)
39      Utils.comparison) * (* ordering *)  
40     Cic.metasenv *       (* environment for metas *)
41     Cic.term list        (* arguments *)
42
43 and proof =
44   | NoProof (* term is the goal missing a proof *)
45   | BasicProof of Cic.term
46   | ProofBlock of
47       Cic.substitution * UriManager.uri *
48         (Cic.name * Cic.term) * Cic.term * (Utils.pos * equality) * proof
49   | ProofGoalBlock of proof * proof 
50   | ProofSymBlock of Cic.term list * proof
51   | SubProof of Cic.term * int * proof
52 ;;
53
54 let string_of_equality ?env =
55   match env with
56   | None -> (
57       function
58         | w, _, (ty, left, right, o), _, _ ->
59             Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.ppterm ty)
60               (CicPp.ppterm left) (string_of_comparison o) (CicPp.ppterm right)
61     )
62   | Some (_, context, _) -> (
63       let names = names_of_context context in
64       function
65         | w, _, (ty, left, right, o), _, _ ->
66             Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.pp ty names)
67               (CicPp.pp left names) (string_of_comparison o)
68               (CicPp.pp right names)
69     )
70 ;;
71
72
73 let rec string_of_proof = function
74   | NoProof -> "NoProof " 
75   | BasicProof t -> "BasicProof " ^ (CicPp.ppterm t)
76   | SubProof (t, i, p) ->
77       Printf.sprintf "SubProof(%s, %s, %s)"
78         (CicPp.ppterm t) (string_of_int i) (string_of_proof p)
79   | ProofSymBlock _ -> "ProofSymBlock"
80   | ProofBlock (subst, _, _, _ ,_,_) -> 
81       "ProofBlock" ^ (CicMetaSubst.ppsubst subst)
82   | ProofGoalBlock (p1, p2) ->
83       Printf.sprintf "ProofGoalBlock(%s, %s)"
84         (string_of_proof p1) (string_of_proof p2)
85 ;;
86
87
88 let check_disjoint_invariant subst metasenv msg =
89   if (List.exists 
90         (fun (i,_,_) -> (List.exists (fun (j,_) -> i=j) subst)) metasenv)
91   then 
92     begin 
93       prerr_endline ("not disjoint: " ^ msg);
94       assert false
95     end
96 ;;
97
98 (* filter out from metasenv the variables in substs *)
99 let filter subst metasenv =
100   List.filter
101     (fun (m, _, _) ->
102          try let _ = List.find (fun (i, _) -> m = i) subst in false
103          with Not_found -> true)
104     metasenv
105 ;;
106
107 (* returns an explicit named subst and a list of arguments for sym_eq_URI *)
108 let build_ens_for_sym_eq sym_eq_URI termlist =
109   let obj, _ = CicEnvironment.get_obj CicUniv.empty_ugraph sym_eq_URI in
110   match obj with
111   | Cic.Constant (_, _, _, uris, _) ->
112       assert (List.length uris <= List.length termlist);
113       let rec aux = function
114         | [], tl -> [], tl
115         | (uri::uris), (term::tl) ->
116             let ens, args = aux (uris, tl) in
117             (uri, term)::ens, args
118         | _, _ -> assert false
119       in
120       aux (uris, termlist)
121   | _ -> assert false
122 ;;
123
124
125 let build_proof_term ?(noproof=Cic.Implicit None) proof =
126   let rec do_build_proof proof = 
127     match proof with
128     | NoProof ->
129         Printf.fprintf stderr "WARNING: no proof!\n";
130         noproof
131     | BasicProof term -> term
132     | ProofGoalBlock (proofbit, proof) ->
133         print_endline "found ProofGoalBlock, going up...";
134         do_build_goal_proof proofbit proof
135     | ProofSymBlock (termlist, proof) ->
136         let proof = do_build_proof proof in
137         let ens, args = build_ens_for_sym_eq (Utils.sym_eq_URI ()) termlist in
138         Cic.Appl ([Cic.Const (Utils.sym_eq_URI (), ens)] @ args @ [proof])
139     | ProofBlock (subst, eq_URI, (name, ty), bo, (pos, eq), eqproof) ->
140         let t' = Cic.Lambda (name, ty, bo) in
141         let proof' =
142           let _, proof', _, _, _ = eq in
143           do_build_proof proof'
144         in
145         let eqproof = do_build_proof eqproof in
146         let _, _, (ty, what, other, _), menv', args' = eq in
147         let what, other =
148           if pos = Utils.Left then what, other else other, what
149         in
150         CicMetaSubst.apply_subst subst
151           (Cic.Appl [Cic.Const (eq_URI, []); ty;
152                      what; t'; eqproof; other; proof'])
153     | SubProof (term, meta_index, proof) ->
154         let proof = do_build_proof proof in
155         let eq i = function
156           | Cic.Meta (j, _) -> i = j
157           | _ -> false
158         in
159         ProofEngineReduction.replace
160           ~equality:eq ~what:[meta_index] ~with_what:[proof] ~where:term
161
162   and do_build_goal_proof proofbit proof =
163     match proof with
164     | ProofGoalBlock (pb, p) ->
165         do_build_proof (ProofGoalBlock (replace_proof proofbit pb, p))
166     | _ -> do_build_proof (replace_proof proofbit proof)
167
168   and replace_proof newproof = function
169     | ProofBlock (subst, eq_URI, namety, bo, poseq, eqproof) ->
170         let eqproof' = replace_proof newproof eqproof in
171         ProofBlock (subst, eq_URI, namety, bo, poseq, eqproof')
172     | ProofGoalBlock (pb, p) ->
173         let pb' = replace_proof newproof pb in
174         ProofGoalBlock (pb', p)
175     | BasicProof _ -> newproof
176     | SubProof (term, meta_index, p) ->
177         SubProof (term, meta_index, replace_proof newproof p)
178     | p -> p
179   in
180   do_build_proof proof
181 ;;
182
183
184 let rec metas_of_term = function
185   | Cic.Meta (i, c) -> [i]
186   | Cic.Var (_, ens) 
187   | Cic.Const (_, ens) 
188   | Cic.MutInd (_, _, ens) 
189   | Cic.MutConstruct (_, _, _, ens) ->
190       List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
191   | Cic.Cast (s, t)
192   | Cic.Prod (_, s, t)
193   | Cic.Lambda (_, s, t)
194   | Cic.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
195   | Cic.Appl l -> List.flatten (List.map metas_of_term l)
196   | Cic.MutCase (uri, i, s, t, l) ->
197       (metas_of_term s) @ (metas_of_term t) @
198         (List.flatten (List.map metas_of_term l))
199   | Cic.Fix (i, il) ->
200       List.flatten
201         (List.map (fun (s, i, t1, t2) ->
202                      (metas_of_term t1) @ (metas_of_term t2)) il)
203   | Cic.CoFix (i, il) ->
204       List.flatten
205         (List.map (fun (s, t1, t2) ->
206                      (metas_of_term t1) @ (metas_of_term t2)) il)
207   | _ -> []
208 ;;      
209
210 let rec metas_of_proof p = 
211   if Utils.debug then
212     let t1 = Unix.gettimeofday () in
213     let res = metas_of_term (build_proof_term p) in
214     let t2 = Unix.gettimeofday () in
215     metas_of_proof_time := !metas_of_proof_time  +. (t2 -. t1);
216     res
217   else 
218     metas_of_term (build_proof_term p)
219 ;;
220
221 exception NotMetaConvertible;;
222
223 let meta_convertibility_aux table t1 t2 =
224   let module C = Cic in
225   let rec aux ((table_l, table_r) as table) t1 t2 =
226     match t1, t2 with
227     | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
228         let m1_binding, table_l =
229           try List.assoc m1 table_l, table_l
230           with Not_found -> m2, (m1, m2)::table_l
231         and m2_binding, table_r =
232           try List.assoc m2 table_r, table_r
233           with Not_found -> m1, (m2, m1)::table_r
234         in
235         if (m1_binding <> m2) || (m2_binding <> m1) then
236           raise NotMetaConvertible
237         else (
238           try
239             List.fold_left2
240               (fun res t1 t2 ->
241                  match t1, t2 with
242                  | None, Some _ | Some _, None -> raise NotMetaConvertible
243                  | None, None -> res
244                  | Some t1, Some t2 -> (aux res t1 t2))
245               (table_l, table_r) tl1 tl2
246           with Invalid_argument _ ->
247             raise NotMetaConvertible
248         )
249     | C.Var (u1, ens1), C.Var (u2, ens2)
250     | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
251         aux_ens table ens1 ens2
252     | C.Cast (s1, t1), C.Cast (s2, t2)
253     | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
254     | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
255     | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
256         let table = aux table s1 s2 in
257         aux table t1 t2
258     | C.Appl l1, C.Appl l2 -> (
259         try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
260         with Invalid_argument _ -> raise NotMetaConvertible
261       )
262     | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
263         when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
264     | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
265         when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
266         aux_ens table ens1 ens2
267     | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
268         when (UriManager.eq u1 u2) && i1 = i2 ->
269         let table = aux table s1 s2 in
270         let table = aux table t1 t2 in (
271           try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
272           with Invalid_argument _ -> raise NotMetaConvertible
273         )
274     | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
275         try
276           List.fold_left2
277             (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
278                if i1 <> i2 then raise NotMetaConvertible
279                else
280                  let res = (aux res s1 s2) in aux res t1 t2)
281             table il1 il2
282         with Invalid_argument _ -> raise NotMetaConvertible
283       )
284     | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
285         try
286           List.fold_left2
287             (fun res (n1, s1, t1) (n2, s2, t2) ->
288                let res = aux res s1 s2 in aux res t1 t2)
289             table il1 il2
290         with Invalid_argument _ -> raise NotMetaConvertible
291       )
292     | t1, t2 when t1 = t2 -> table
293     | _, _ -> raise NotMetaConvertible
294         
295   and aux_ens table ens1 ens2 =
296     let cmp (u1, t1) (u2, t2) =
297       compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
298     in
299     let ens1 = List.sort cmp ens1
300     and ens2 = List.sort cmp ens2 in
301     try
302       List.fold_left2
303         (fun res (u1, t1) (u2, t2) ->
304            if not (UriManager.eq u1 u2) then raise NotMetaConvertible
305            else aux res t1 t2)
306         table ens1 ens2
307     with Invalid_argument _ -> raise NotMetaConvertible
308   in
309   aux table t1 t2
310 ;;
311
312
313 let meta_convertibility_eq eq1 eq2 =
314   let _, _, (ty, left, right, _), _, _ = eq1
315   and _, _, (ty', left', right', _), _, _ = eq2 in
316   if ty <> ty' then
317     false
318   else if (left = left') && (right = right') then
319     true
320   else if (left = right') && (right = left') then
321     true
322   else
323     try
324       let table = meta_convertibility_aux ([], []) left left' in
325       let _ = meta_convertibility_aux table right right' in
326       true
327     with NotMetaConvertible ->
328       try
329         let table = meta_convertibility_aux ([], []) left right' in
330         let _ = meta_convertibility_aux table right left' in
331         true
332       with NotMetaConvertible ->
333         false
334 ;;
335
336
337 let meta_convertibility t1 t2 =
338   if t1 = t2 then
339     true
340   else
341     try
342       ignore(meta_convertibility_aux ([], []) t1 t2);
343       true
344     with NotMetaConvertible ->
345       false
346 ;;
347
348
349 let rec check_irl start = function
350   | [] -> true
351   | None::tl -> check_irl (start+1) tl
352   | (Some (Cic.Rel x))::tl ->
353       if x = start then check_irl (start+1) tl else false
354   | _ -> false
355 ;;
356
357
358 let rec is_simple_term = function
359   | Cic.Appl ((Cic.Meta _)::_) -> false
360   | Cic.Appl l -> List.for_all is_simple_term l
361   | Cic.Meta (i, l) -> check_irl 1 l
362   | Cic.Rel _ -> true
363   | Cic.Const _ -> true
364   | Cic.MutInd (_, _, []) -> true
365   | Cic.MutConstruct (_, _, _, []) -> true
366   | _ -> false
367 ;;
368
369
370 let rec lookup_subst meta subst =
371   match meta with
372   | Cic.Meta (i, _) -> (
373       try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst 
374       in lookup_subst t subst 
375       with Not_found -> meta
376     )
377   | _ -> meta
378 ;;
379
380 let locked menv i =
381   List.exists (fun (j,_,_) -> i = j) menv
382 ;;
383
384 let unification_simple locked_menv metasenv context t1 t2 ugraph =
385   let module C = Cic in
386   let module M = CicMetaSubst in
387   let module U = CicUnification in
388   let lookup = lookup_subst in
389   let rec occurs_check subst what where =
390     match where with
391     | t when what = t -> true
392     | C.Appl l -> List.exists (occurs_check subst what) l
393     | C.Meta _ ->
394         let t = lookup where subst in
395         if t <> where then occurs_check subst what t else false
396     | _ -> false
397   in
398   let rec unif subst menv s t =
399     let s = match s with C.Meta _ -> lookup s subst | _ -> s
400     and t = match t with C.Meta _ -> lookup t subst | _ -> t
401     
402     in
403     match s, t with
404     | s, t when s = t -> subst, menv
405     | C.Meta (i, _), C.Meta (j, _) 
406         when (locked locked_menv i) &&(locked locked_menv j) ->
407         raise
408           (U.UnificationFailure (lazy "Inference.unification.unif"))
409     | C.Meta (i, _), C.Meta (j, _) when (locked locked_menv i) ->         
410         unif subst menv t s
411     | C.Meta (i, _), C.Meta (j, _) when (i > j) && not (locked locked_menv j) ->
412         unif subst menv t s
413     | C.Meta _, t when occurs_check subst s t ->
414         raise
415           (U.UnificationFailure (lazy "Inference.unification.unif"))
416     | C.Meta (i, l), t when (locked locked_menv i) -> 
417         raise
418           (U.UnificationFailure (lazy "Inference.unification.unif"))
419     | C.Meta (i, l), t -> (
420         try
421           let _, _, ty = CicUtil.lookup_meta i menv in
422           assert (not (List.mem_assoc i subst));
423           let subst = (i, (context, t, ty))::subst in
424           let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
425           subst, menv
426         with CicUtil.Meta_not_found m ->
427           let names = names_of_context context in
428           debug_print
429             (lazy
430                (Printf.sprintf "Meta_not_found %d!: %s %s\n%s\n\n%s" m
431                   (CicPp.pp t1 names) (CicPp.pp t2 names)
432                   (print_metasenv menv) (print_metasenv metasenv)));
433           assert false
434       )
435     | _, C.Meta _ -> unif subst menv t s
436     | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt ->
437         raise (U.UnificationFailure (lazy "Inference.unification.unif"))
438     | C.Appl (hds::tls), C.Appl (hdt::tlt) -> (
439         try
440           List.fold_left2
441             (fun (subst', menv) s t -> unif subst' menv s t)
442             (subst, menv) tls tlt
443         with Invalid_argument _ ->
444           raise (U.UnificationFailure (lazy "Inference.unification.unif"))
445       )
446     | _, _ ->
447         raise (U.UnificationFailure (lazy "Inference.unification.unif"))
448   in
449   let subst, menv = unif [] metasenv t1 t2 in
450   let menv = filter subst menv in
451   List.rev subst, menv, ugraph
452 ;;
453
454 let profiler = HExtlib.profile "flatten"
455
456 let unification_aux b metasenv1 metasenv2 context t1 t2 ugraph =
457   let metasenv = metasenv1 @ metasenv2 in
458   let subst, menv, ug =
459     if not (is_simple_term t1) || not (is_simple_term t2) then (
460       debug_print
461         (lazy
462            (Printf.sprintf "NOT SIMPLE TERMS: %s %s"
463               (CicPp.ppterm t1) (CicPp.ppterm t2)));
464       raise (CicUnification .UnificationFailure (lazy "Inference.unification.unif"))
465     ) else
466       if b then
467         (* full unification *)
468         unification_simple [] metasenv context t1 t2 ugraph
469       else
470         (* matching: metasenv1 is locked *)
471         unification_simple metasenv1 metasenv context t1 t2 ugraph
472   in
473   if Utils.debug_res then
474             ignore(check_disjoint_invariant subst menv "unif");
475   let flatten subst = 
476     List.map
477       (fun (i, (context, term, ty)) ->
478          let context = CicMetaSubst.apply_subst_context subst context in
479          let term = CicMetaSubst.apply_subst subst term in
480          let ty = CicMetaSubst.apply_subst subst ty in  
481            (i, (context, term, ty))) subst 
482   in
483   let flatten subst = profiler.HExtlib.profile flatten subst in
484   let subst = flatten subst in
485     subst, menv, ug
486 ;;
487
488 exception MatchingFailure;;
489
490 let matching1 metasenv1 metasenv2 context t1 t2 ugraph = 
491   try 
492     unification_aux false metasenv1 metasenv2 context t1 t2 ugraph
493   with
494     CicUnification .UnificationFailure _ ->
495       raise MatchingFailure
496 ;;
497
498 let unification = unification_aux true 
499 ;;
500
501
502
503 (*
504 let unification metasenv1 metasenv2 context t1 t2 ugraph = 
505   let (subst, metasenv, ugraph) = 
506     CicUnification.fo_unif (metasenv1@metasenv2) context t1 t2 ugraph in
507   if Utils.debug_res then
508             ignore(check_disjoint_invariant subst metasenv "fo_unif");
509   (subst, metasenv, ugraph)
510     
511 ;;
512 *)
513
514
515 (*
516 let matching_simple metasenv context t1 t2 ugraph =
517   let module C = Cic in
518   let module M = CicMetaSubst in
519   let module U = CicUnification in
520   let lookup meta subst =
521     match meta with
522     | C.Meta (i, _) -> (
523         try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
524         with Not_found -> meta
525       )
526     | _ -> assert false
527   in
528   let rec do_match subst menv s t =
529     match s, t with
530     | s, t when s = t -> subst, menv
531     | s, C.Meta (i, l) ->
532         let filter_menv i menv =
533           List.filter (fun (m, _, _) -> i <> m) menv
534         in
535         let subst, menv =
536           let value = lookup t subst in
537           match value with
538           | value when value = t ->
539               let _, _, ty = CicUtil.lookup_meta i menv in
540               (i, (context, s, ty))::subst, filter_menv i menv
541           | value when value <> s ->
542               raise MatchingFailure
543           | value -> do_match subst menv s value
544         in
545         subst, menv
546     | C.Appl ls, C.Appl lt -> (
547         try
548           List.fold_left2
549             (fun (subst, menv) s t -> do_match subst menv s t)
550             (subst, menv) ls lt
551         with Invalid_argument _ ->
552           raise MatchingFailure
553       )
554     | _, _ ->
555         raise MatchingFailure
556   in
557   let subst, menv = do_match [] metasenv t1 t2 in
558   subst, menv, ugraph
559 ;;
560 *)
561
562 (*
563 let matching metasenv context t1 t2 ugraph =
564     try
565       let subst, metasenv, ugraph =
566         try
567           unification metasenv context t1 t2 ugraph
568         with CicUtil.Meta_not_found _ as exn ->
569           Printf.eprintf "t1 == %s\nt2 = %s\nmetasenv == %s\n%!"
570             (CicPp.ppterm t1) (CicPp.ppterm t2) 
571             (CicMetaSubst.ppmetasenv [] metasenv);
572           raise exn
573       in
574       if Utils.debug_res then
575             ignore(check_disjoint_invariant subst metasenv "qua-2");
576       let t' = CicMetaSubst.apply_subst subst t1 in
577       if not (meta_convertibility t1 t') then
578         raise MatchingFailure
579       else
580         if Utils.debug_res then
581             ignore(check_disjoint_invariant subst metasenv "qua-1");
582         let metas = metas_of_term t1 in
583         let subst =
584           List.map
585             (fun (i, (context, term, ty)) ->
586                let context = CicMetaSubst.apply_subst_context subst context in
587                let term = CicMetaSubst.apply_subst subst term in
588                let ty = CicMetaSubst.apply_subst subst ty in  
589                  (i, (context, term, ty))) subst in
590           if Utils.debug_res then
591             ignore(check_disjoint_invariant subst metasenv "qua0");
592           
593           let subst, metasenv =
594           List.fold_left
595             (fun 
596                (subst,metasenv) s ->
597                  match s with
598                    | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
599                        let metasenv' =
600                          List.filter (fun (x, _, _) -> x<>j) metasenv 
601                        in
602                          ((j, (c, Cic.Meta (i, lc), ty))::subst,
603                           (i,c,ty)::metasenv')
604                    |_ -> s::subst,metasenv) ([],metasenv) subst
605         in
606         if Utils.debug_res then
607           ignore(check_disjoint_invariant subst metasenv "qua1");
608 (*
609         let fix_subst = function
610           | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
611               (j, (c, Cic.Meta (i, lc), ty))
612           | s -> s
613         in
614         let subst = List.map fix_subst subst in *)
615         if CicMetaSubst.apply_subst subst t1 = t1 then
616           subst, metasenv, ugraph
617         else
618           (prerr_endline "mah"; raise MatchingFailure)
619     with
620     | CicUnification.UnificationFailure _
621     | CicUnification.Uncertain _ ->
622       raise MatchingFailure
623 ;;
624 *)
625
626 (** matching takes in input the _disjoint_ metasenv of t1 and  t2;
627 it perform unification in the union metasenv, then check that
628 the first metasenv has not changed *)
629
630
631 let matching2 metasenv1 metasenv2 context t1 t2 ugraph =
632       let subst, metasenv, ugraph =
633         try
634           unification metasenv1 metasenv2 context t1 t2 ugraph
635         with 
636             CicUtil.Meta_not_found _ as exn ->
637               Printf.eprintf "t1 == %s\nt2 = %s\nmetasenv == %s\n%!"
638                 (CicPp.ppterm t1) (CicPp.ppterm t2) 
639                 (CicMetaSubst.ppmetasenv [] (metasenv1@metasenv2));
640               raise exn
641           | CicUnification.UnificationFailure _
642           | CicUnification.Uncertain _ ->
643               raise MatchingFailure    
644       in
645       if Utils.debug_res then
646             ignore(check_disjoint_invariant subst metasenv "qua-2");
647       (* let us unfold subst *)
648       if metasenv = metasenv1 then 
649         let subst =
650           List.map
651             (fun (i, (context, term, ty)) ->
652                let context = CicMetaSubst.apply_subst_context subst context in
653                let term = CicMetaSubst.apply_subst subst term in
654                let ty = CicMetaSubst.apply_subst subst ty in  
655                  (i, (context, term, ty))) subst in
656           subst, metasenv, ugraph (* everything is fine *)
657       else
658         (* let us unfold subst *)
659         (* 
660         let subst =
661           List.map
662             (fun (i, (context, term, ty)) ->
663                let context = CicMetaSubst.apply_subst_context subst context in
664                let term = CicMetaSubst.apply_subst subst term in
665                let ty = CicMetaSubst.apply_subst subst ty in  
666                  (i, (context, term, ty))) subst in
667         *)
668           (* let us revert Meta-Meta in subst privileging metasenv1 *)
669         let subst, metasenv =
670           List.fold_left
671             (fun 
672                (subst,metasenv) s ->
673                  match s with
674                    | (i, (c, Cic.Meta (j, lc), ty)) 
675                        when (List.exists (fun (x, _, _)  -> x=i) metasenv1) &&
676                          not (List.exists (fun (x, _)  -> x=j) subst) ->
677                        let metasenv' =
678                          List.filter (fun (x, _, _) -> x<>j) metasenv 
679                        in
680                          ((j, (c, Cic.Meta (i, lc), ty))::subst,
681                           (i,c,ty)::metasenv')
682                    |_ -> s::subst,metasenv) ([],metasenv) subst
683         in      
684         (* finally, let us chek again that metasenv = metasenv1 *)
685         if metasenv = metasenv1 then 
686           subst, metasenv, ugraph
687         else raise MatchingFailure  
688 ;;
689
690 (* debug 
691 let matching metasenv1 metasenv2 context t1 t2 ugraph =
692   let rc1 = 
693     try Some (matching1 metasenv1 metasenv2 context t1 t2 ugraph)
694     with MatchingFailure -> None
695   in
696   let rc2 = 
697     try 
698       Some (matching2 metasenv1 metasenv2 context t1 t2 ugraph)
699     with MatchingFailure -> None
700   in
701   match rc1,rc2 with
702   | Some (s,m,g) , None -> 
703       prerr_endline (CicPp.ppterm t1);
704       prerr_endline (CicPp.ppterm t2);
705       prerr_endline "SOLO NOI";
706       prerr_endline (CicMetaSubst.ppsubst s);
707       s,m,g
708   | None , Some _ -> 
709       prerr_endline (CicPp.ppterm t1);
710       prerr_endline (CicPp.ppterm t2);
711       prerr_endline "SOLO LUI";
712       assert false
713   | None, None -> raise MatchingFailure 
714   | Some (s,m,g), Some (s',m',g') ->
715       let s = List.sort (fun (i,_) (j,_) -> i - j) s in
716       let s' = List.sort (fun (i,_) (j,_) -> i - j) s' in
717       if s <> s' then 
718         begin
719           prerr_endline (CicMetaSubst.ppsubst s);
720           prerr_endline (CicMetaSubst.ppsubst s');
721           prerr_endline (CicPp.ppterm t1);
722           prerr_endline (CicPp.ppterm t2);
723                 end;
724       s,m,g
725 *)  
726 let matching = matching1;;
727
728 let check_eq context msg eq =
729   let w, proof, (eq_ty, left, right, order), metas, args = eq in
730   if not (fst (CicReduction.are_convertible ~metasenv:metas context eq_ty
731    (fst (CicTypeChecker.type_of_aux' metas context  left CicUniv.empty_ugraph))
732    CicUniv.empty_ugraph))
733   then
734     begin
735       prerr_endline msg;
736       assert false;
737     end
738   else ()
739 ;;
740
741 let find_equalities context proof =
742   let module C = Cic in
743   let module S = CicSubstitution in
744   let module T = CicTypeChecker in
745   let eq_uri = LibraryObjects.eq_URI () in
746   let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
747   let ok_types ty menv =
748     List.for_all (fun (_, _, mt) -> mt = ty) menv
749   in
750   let rec aux index newmeta = function
751     | [] -> [], newmeta
752     | (Some (_, C.Decl (term)))::tl ->
753         let do_find context term =
754           match term with
755           | C.Prod (name, s, t) ->
756               let (head, newmetas, args, newmeta) =
757                 ProofEngineHelpers.saturate_term newmeta []
758                   context (S.lift index term) 0
759               in
760               let p =
761                 if List.length args = 0 then
762                   C.Rel index
763                 else
764                   C.Appl ((C.Rel index)::args)
765               in (
766                 match head with
767                 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
768                     when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
769                     debug_print
770                       (lazy
771                          (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
772                     let o = !Utils.compare_terms t1 t2 in
773                     let stat = (ty,t1,t2,o) in
774                     let w = compute_equality_weight stat in
775                     let proof = BasicProof p in
776                     let e = (w, proof, stat, newmetas, args) in
777                     Some e, (newmeta+1)
778                 | _ -> None, newmeta
779               )
780           | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
781               when UriManager.eq uri eq_uri ->
782               let ty = S.lift index ty in
783               let t1 = S.lift index t1 in
784               let t2 = S.lift index t2 in
785               let o = !Utils.compare_terms t1 t2 in
786               let stat = (ty,t1,t2,o) in
787               let w = compute_equality_weight stat in
788               let e = (w, BasicProof (C.Rel index), stat, [], []) in
789               Some e, (newmeta+1)
790           | _ -> None, newmeta
791         in (
792           match do_find context term with
793           | Some p, newmeta ->
794               let tl, newmeta' = (aux (index+1) newmeta tl) in
795               if newmeta' < newmeta then 
796                 prerr_endline "big trouble";
797               (index, p)::tl, newmeta' (* max???? *)
798           | None, _ ->
799               aux (index+1) newmeta tl
800         )
801     | _::tl ->
802         aux (index+1) newmeta tl
803   in
804   let il, maxm = aux 1 newmeta context in
805   let indexes, equalities = List.split il in
806   ignore (List.iter (check_eq context "find") equalities);
807   indexes, equalities, maxm
808 ;;
809
810
811 (*
812 let equations_blacklist =
813   List.fold_left
814     (fun s u -> UriManager.UriSet.add (UriManager.uri_of_string u) s)
815     UriManager.UriSet.empty [
816       "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
817       "cic:/Coq/Init/Logic/trans_eq.con";
818       "cic:/Coq/Init/Logic/f_equal.con";
819       "cic:/Coq/Init/Logic/f_equal2.con";
820       "cic:/Coq/Init/Logic/f_equal3.con";
821       "cic:/Coq/Init/Logic/f_equal4.con";
822       "cic:/Coq/Init/Logic/f_equal5.con";
823       "cic:/Coq/Init/Logic/sym_eq.con";
824       "cic:/Coq/Init/Logic/eq_ind.con";
825       "cic:/Coq/Init/Logic/eq_ind_r.con";
826       "cic:/Coq/Init/Logic/eq_rec.con";
827       "cic:/Coq/Init/Logic/eq_rec_r.con";
828       "cic:/Coq/Init/Logic/eq_rect.con";
829       "cic:/Coq/Init/Logic/eq_rect_r.con";
830       "cic:/Coq/Logic/Eqdep/UIP.con";
831       "cic:/Coq/Logic/Eqdep/UIP_refl.con";
832       "cic:/Coq/Logic/Eqdep_dec/eq2eqT.con";
833       "cic:/Coq/ZArith/Zcompare/rename.con";
834       (* ALB !!!! questo e` imbrogliare, ma x ora lo lasciamo cosi`...
835          perche' questo cacchio di teorema rompe le scatole :'( *)
836       "cic:/Rocq/SUBST/comparith/mult_n_2.con";
837
838       "cic:/matita/logic/equality/eq_f.con";
839       "cic:/matita/logic/equality/eq_f2.con";
840       "cic:/matita/logic/equality/eq_rec.con";
841       "cic:/matita/logic/equality/eq_rect.con";
842     ]
843 ;;
844 *)
845 let equations_blacklist = UriManager.UriSet.empty;;
846
847
848 let find_library_equalities dbd context status maxmeta = 
849   let module C = Cic in
850   let module S = CicSubstitution in
851   let module T = CicTypeChecker in
852   let blacklist =
853     List.fold_left
854       (fun s u -> UriManager.UriSet.add u s)
855       equations_blacklist
856       [eq_XURI (); sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
857        eq_ind_r_URI ()]
858   in
859   let candidates =
860     List.fold_left
861       (fun l uri ->
862          if UriManager.UriSet.mem uri blacklist then
863            l
864          else
865            let t = CicUtil.term_of_uri uri in
866            let ty, _ =
867              CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
868            in
869            (uri, t, ty)::l)
870       []
871       (let t1 = Unix.gettimeofday () in
872        let eqs = (MetadataQuery.equations_for_goal ~dbd status) in
873        let t2 = Unix.gettimeofday () in
874        (debug_print
875           (lazy
876              (Printf.sprintf "Tempo di MetadataQuery.equations_for_goal: %.9f\n"
877                 (t2 -. t1))));
878        eqs)
879   in
880   let eq_uri1 = eq_XURI ()
881   and eq_uri2 = LibraryObjects.eq_URI () in
882   let iseq uri =
883     (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
884   in
885   let ok_types ty menv =
886     List.for_all (fun (_, _, mt) -> mt = ty) menv
887   in
888   let rec has_vars = function
889     | C.Meta _ | C.Rel _ | C.Const _ -> false
890     | C.Var _ -> true
891     | C.Appl l -> List.exists has_vars l
892     | C.Prod (_, s, t) | C.Lambda (_, s, t)
893     | C.LetIn (_, s, t) | C.Cast (s, t) ->
894         (has_vars s) || (has_vars t)
895     | _ -> false
896   in
897   let rec aux newmeta = function
898     | [] -> [], newmeta
899     | (uri, term, termty)::tl ->
900         debug_print
901           (lazy
902              (Printf.sprintf "Examining: %s (%s)"
903                 (CicPp.ppterm term) (CicPp.ppterm termty)));
904         let res, newmeta = 
905           match termty with
906           | C.Prod (name, s, t) when not (has_vars termty) ->
907               let head, newmetas, args, newmeta =
908                 ProofEngineHelpers.saturate_term newmeta [] context termty 0
909               in
910               let p =
911                 if List.length args = 0 then
912                   term
913                 else
914                   C.Appl (term::args)
915               in (
916                 match head with
917                 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
918                     when (iseq uri) && (ok_types ty newmetas) ->
919                     debug_print
920                       (lazy
921                          (Printf.sprintf "OK: %s" (CicPp.ppterm term)));
922                     let o = !Utils.compare_terms t1 t2 in
923                     let stat = (ty,t1,t2,o) in
924                     let w = compute_equality_weight stat in
925                     let proof = BasicProof p in
926                     let e = (w, proof, stat, newmetas, args) in
927                     Some e, (newmeta+1)
928                 | _ -> None, newmeta
929               )
930           | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
931               when iseq uri && not (has_vars termty) ->
932               let o = !Utils.compare_terms t1 t2 in
933               let stat = (ty,t1,t2,o) in
934               let w = compute_equality_weight stat in
935               let e = (w, BasicProof term, stat, [], []) in
936               Some e, (newmeta+1)
937           | _ -> None, newmeta
938         in
939         match res with
940         | Some e ->
941             let tl, newmeta' = aux newmeta tl in
942               if newmeta' < newmeta then 
943                 prerr_endline "big trouble";
944               (uri, e)::tl, newmeta' (* max???? *)
945         | None ->
946             aux newmeta tl
947   in
948   let found, maxm = aux maxmeta candidates in
949   let uriset, eqlist = 
950     (List.fold_left
951        (fun (s, l) (u, e) ->
952           if List.exists (meta_convertibility_eq e) (List.map snd l) then (
953             debug_print
954               (lazy
955                  (Printf.sprintf "NO!! %s already there!"
956                     (string_of_equality e)));
957             (UriManager.UriSet.add u s, l)
958           ) else (UriManager.UriSet.add u s, (u, e)::l))
959        (UriManager.UriSet.empty, []) found)
960   in
961   uriset, eqlist, maxm
962 ;;
963
964
965 let find_library_theorems dbd env status equalities_uris =
966   let module C = Cic in
967   let module S = CicSubstitution in
968   let module T = CicTypeChecker in
969   let blacklist =
970     let refl_equal =
971       UriManager.uri_of_string "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)" in
972     let s =
973       UriManager.UriSet.remove refl_equal
974         (UriManager.UriSet.union equalities_uris equations_blacklist)
975     in
976     List.fold_left
977       (fun s u -> UriManager.UriSet.add u s)
978       s [eq_XURI () ;sym_eq_URI (); trans_eq_URI (); eq_ind_URI ();
979          eq_ind_r_URI ()]
980   in
981   let metasenv, context, ugraph = env in
982   let candidates =
983     List.fold_left
984       (fun l uri ->
985          if UriManager.UriSet.mem uri blacklist then l
986          else
987            let t = CicUtil.term_of_uri uri in
988            let ty, _ = CicTypeChecker.type_of_aux' metasenv context t ugraph in
989            (t, ty, [])::l)
990       [] (MetadataQuery.signature_of_goal ~dbd status)
991   in
992   let refl_equal =
993     let u = eq_XURI () in
994     let t = CicUtil.term_of_uri u in
995     let ty, _ = CicTypeChecker.type_of_aux' [] [] t CicUniv.empty_ugraph in
996     (t, ty, [])
997   in
998   refl_equal::candidates
999 ;;
1000
1001
1002 let find_context_hypotheses env equalities_indexes =
1003   let metasenv, context, ugraph = env in
1004   let _, res = 
1005     List.fold_left
1006       (fun (n, l) entry ->
1007          match entry with
1008          | None -> (n+1, l)
1009          | Some _ ->
1010              if List.mem n equalities_indexes then
1011                (n+1, l)
1012              else
1013                let t = Cic.Rel n in
1014                let ty, _ =
1015                  CicTypeChecker.type_of_aux' metasenv context t ugraph in 
1016                (n+1, (t, ty, [])::l))
1017       (1, []) context
1018   in
1019   res
1020 ;;
1021
1022
1023 let fix_metas_old newmeta (w, p, (ty, left, right, o), menv, args) =
1024   let table = Hashtbl.create (List.length args) in
1025
1026   let newargs, newmeta =
1027     List.fold_right
1028       (fun t (newargs, index) ->
1029          match t with
1030          | Cic.Meta (i, l) ->
1031              if Hashtbl.mem table i then
1032                let idx = Hashtbl.find table i in
1033                ((Cic.Meta (idx, l))::newargs, index+1)
1034              else
1035                let _ = Hashtbl.add table i index in
1036                ((Cic.Meta (index, l))::newargs, index+1)
1037          | _ -> assert false)
1038       args ([], newmeta+1)
1039   in
1040
1041   let repl where =
1042     ProofEngineReduction.replace ~equality:(=) ~what:args ~with_what:newargs
1043       ~where
1044   in
1045   let menv' =
1046     List.fold_right
1047       (fun (i, context, term) menv ->
1048          try
1049            let index = Hashtbl.find table i in
1050            (index, context, term)::menv
1051          with Not_found ->
1052            (i, context, term)::menv)
1053       menv []
1054   in
1055   let ty = repl ty
1056   and left = repl left
1057   and right = repl right in
1058   let metas = 
1059     (metas_of_term left) @ 
1060       (metas_of_term right) @ 
1061       (metas_of_term ty) @ (metas_of_proof p) in
1062   let menv' = List.filter (fun (i, _, _) -> List.mem i metas) menv' in
1063   let newargs =
1064     List.filter
1065       (function Cic.Meta (i, _) -> List.mem i metas | _ -> assert false) newargs
1066   in
1067   let _ =
1068     if List.length metas > 0 then 
1069       let first = List.hd metas in
1070       (* this new equality might have less variables than its parents: here
1071          we fill the gap with a dummy arg. Example:
1072          with (f X Y) = X we can simplify
1073          (g X) = (f X Y) in
1074          (g X) = X. 
1075          So the new equation has only one variable, but it still has type like
1076          \lambda X,Y:..., so we need to pass a dummy arg for Y
1077          (I hope this makes some sense...)
1078       *)
1079       Hashtbl.iter
1080         (fun k v ->
1081            if not (List.exists
1082                      (function Cic.Meta (i, _) -> i = v | _ -> assert false)
1083                      newargs) then
1084              Hashtbl.replace table k first)
1085         (Hashtbl.copy table)
1086   in
1087   let rec fix_proof = function
1088     | NoProof -> NoProof 
1089     | BasicProof term -> BasicProof (repl term)
1090     | ProofBlock (subst, eq_URI, namety, bo, (pos, eq), p) ->
1091         let subst' =
1092           List.fold_left
1093             (fun s arg ->
1094                match arg with
1095                | Cic.Meta (i, l) -> (
1096                    try
1097                      let j = Hashtbl.find table i in
1098                      if List.mem_assoc i subst then
1099                        s
1100                      else
1101                        let _, context, ty = CicUtil.lookup_meta i menv in
1102                        (i, (context, Cic.Meta (j, l), ty))::s
1103                    with Not_found | CicUtil.Meta_not_found _ ->
1104                      s
1105                  )
1106                | _ -> assert false)
1107             [] args
1108         in
1109         ProofBlock (subst' @ subst, eq_URI, namety, bo(* t' *), (pos, eq), p)
1110     | p -> assert false
1111   in
1112   let neweq = (w, fix_proof p, (ty, left, right, o), menv', newargs) in
1113   (newmeta +1, neweq)
1114 ;;
1115
1116
1117 let relocate newmeta menv =
1118   let subst, metasenv, newmeta = 
1119     List.fold_right 
1120       (fun (i, context, ty) (subst, menv, maxmeta) -> 
1121         let irl=CicMkImplicit.identity_relocation_list_for_metavariable context in
1122         let newsubst = (i, (context, (Cic.Meta (maxmeta, irl)), ty)) in
1123         let newmeta = maxmeta, context, ty in
1124         newsubst::subst, newmeta::menv, maxmeta+1) 
1125       menv ([], [], newmeta+1)
1126   in
1127   let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
1128   let subst =
1129     List.map
1130       (fun (i, (context, term, ty)) ->
1131          let context = CicMetaSubst.apply_subst_context subst context in
1132          let term = CicMetaSubst.apply_subst subst term in
1133          let ty = CicMetaSubst.apply_subst subst ty in  
1134          (i, (context, term, ty))) subst in
1135   subst, metasenv, newmeta
1136
1137
1138 let fix_metas newmeta (w, p, (ty, left, right, o), menv, args) =
1139   (*
1140   let metas = (metas_of_term left)@(metas_of_term right)
1141     @(metas_of_term ty)@(metas_of_proof p) in
1142   let menv = List.filter (fun (i, _, _) -> List.mem i metas) menv in
1143   *)
1144   (* debug 
1145   let _ , eq = 
1146     fix_metas_old newmeta (w, p, (ty, left, right, o), menv, args) in
1147   prerr_endline (string_of_equality eq); *)
1148   let subst, metasenv, newmeta = relocate newmeta menv in
1149   let ty = CicMetaSubst.apply_subst subst ty in
1150   let left = CicMetaSubst.apply_subst subst left in
1151   let right = CicMetaSubst.apply_subst subst right in
1152   let args = List.map (CicMetaSubst.apply_subst subst) args in
1153   let rec fix_proof = function
1154     | NoProof -> NoProof 
1155     | BasicProof term -> BasicProof (CicMetaSubst.apply_subst subst term)
1156     | ProofBlock (subst', eq_URI, namety, bo, (pos, eq), p) ->
1157         (*
1158         let newsubst = 
1159           List.map
1160             (fun (i, (context, term, ty)) ->
1161                let context = CicMetaSubst.apply_subst_context subst context in
1162                let term = CicMetaSubst.apply_subst subst term in
1163                let ty = CicMetaSubst.apply_subst subst ty in  
1164                  (i, (context, term, ty))) subst' in *)
1165           ProofBlock (subst@subst', eq_URI, namety, bo, (pos, eq), p)
1166     | p -> assert false
1167   in
1168   let p = fix_proof p in
1169   (*
1170   let metas = (metas_of_term left)@(metas_of_term right)
1171     @(metas_of_term ty)@(metas_of_proof p) in
1172   let metasenv = List.filter (fun (i, _, _) -> List.mem i metas) metasenv in
1173   *)
1174   let eq = (w, p, (ty, left, right, o), metasenv, args) in
1175   (* debug prerr_endline (string_of_equality eq); *)
1176   newmeta+1, eq  
1177
1178 let term_is_equality term =
1179   let iseq uri = UriManager.eq uri (LibraryObjects.eq_URI ()) in
1180   match term with
1181   | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] when iseq uri -> true
1182   | _ -> false
1183 ;;
1184
1185
1186 exception TermIsNotAnEquality;;
1187
1188 let equality_of_term proof term =
1189   let eq_uri = LibraryObjects.eq_URI () in
1190   let iseq uri = UriManager.eq uri eq_uri in
1191   match term with
1192   | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1193       let o = !Utils.compare_terms t1 t2 in
1194       let stat = (ty,t1,t2,o) in
1195       let w = compute_equality_weight stat in
1196       let e = (w, BasicProof proof, stat, [], []) in
1197       e
1198   | _ ->
1199       raise TermIsNotAnEquality
1200 ;;
1201
1202
1203 type environment = Cic.metasenv * Cic.context * CicUniv.universe_graph;;
1204
1205 let is_weak_identity (metasenv, context, ugraph) = function
1206   | (_, _, (ty, left, right, _), menv, _) -> 
1207        (left = right ||
1208           (meta_convertibility left right)) 
1209            (* the test below is not a good idea since it stops
1210               demodulation too early *)
1211            (* (fst (CicReduction.are_convertible 
1212                   ~metasenv:(metasenv @ menv) context left right ugraph)))*)
1213 ;;
1214
1215 let is_identity (metasenv, context, ugraph) = function
1216   | (_, _, (ty, left, right, _), menv, _) ->
1217        (left = right ||
1218           (* (meta_convertibility left right)) *)
1219            (fst (CicReduction.are_convertible 
1220                   ~metasenv:(metasenv @ menv) context left right ugraph)))
1221 ;;
1222
1223
1224 let term_of_equality equality =
1225   let _, _, (ty, left, right, _), menv, _ = equality in
1226   let eq i = function Cic.Meta (j, _) -> i = j | _ -> false in
1227   let argsno = List.length menv in
1228   let t =
1229     CicSubstitution.lift argsno
1230       (Cic.Appl [Cic.MutInd (LibraryObjects.eq_URI (), 0, []); ty; left; right])
1231   in
1232   snd (
1233     List.fold_right
1234       (fun (i,_,ty) (n, t) ->
1235          let name = Cic.Name ("X" ^ (string_of_int n)) in
1236          let ty = CicSubstitution.lift (n-1) ty in
1237          let t = 
1238            ProofEngineReduction.replace
1239              ~equality:eq ~what:[i]
1240              ~with_what:[Cic.Rel (argsno - (n - 1))] ~where:t
1241          in
1242            (n-1, Cic.Prod (name, ty, t)))
1243       menv (argsno, t))
1244 ;;