]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/paramodulation/inference.ml
some fixes
[helm.git] / helm / ocaml / paramodulation / inference.ml
1 open Utils;;
2
3
4 type equality =
5     int  *               (* weight *)
6     proof * 
7     (Cic.term *          (* type *)
8      Cic.term *          (* left side *)
9      Cic.term *          (* right side *)
10      Utils.comparison) * (* ordering *)  
11     Cic.metasenv *       (* environment for metas *)
12     Cic.term list        (* arguments *)
13
14 and proof =
15   | NoProof
16   | BasicProof of Cic.term
17   | ProofBlock of
18       Cic.substitution * UriManager.uri *
19         (* name, ty, eq_ty, left, right *)
20         (Cic.name * Cic.term * Cic.term * Cic.term * Cic.term) * 
21         (Utils.pos * equality) * proof
22   | ProofGoalBlock of proof * equality
23   | ProofSymBlock of Cic.term Cic.explicit_named_substitution * proof
24 ;;
25
26
27 let string_of_equality ?env =
28   match env with
29   | None -> (
30       function
31         | w, _, (ty, left, right, o), _, _ ->
32             Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.ppterm ty)
33               (CicPp.ppterm left) (string_of_comparison o) (CicPp.ppterm right)
34     )
35   | Some (_, context, _) -> (
36       let names = names_of_context context in
37       function
38         | w, _, (ty, left, right, o), _, _ ->
39             Printf.sprintf "Weight: %d, {%s}: %s =(%s) %s" w (CicPp.pp ty names)
40               (CicPp.pp left names) (string_of_comparison o)
41               (CicPp.pp right names)
42     )
43 ;;
44
45
46 let build_proof_term equality =
47 (*   Printf.printf "build_term_proof %s" (string_of_equality equality); *)
48 (*   print_newline (); *)
49
50   let indent = ref 0 in
51   
52   let rec do_build_proof proof = 
53     match proof with
54     | NoProof ->
55         Printf.fprintf stderr "WARNING: no proof!\n";
56 (*           (string_of_equality equality); *)
57         Cic.Implicit None
58     | BasicProof term -> term
59     | ProofGoalBlock (proofbit, equality) ->
60         print_endline "found ProofGoalBlock, going up...";
61         let _, proof, _, _, _ = equality in
62         do_build_goal_proof proofbit proof
63     | ProofSymBlock (ens, proof) ->
64         let proof = do_build_proof proof in
65         Cic.Appl [
66           Cic.Const (HelmLibraryObjects.Logic.sym_eq_URI, ens); (* symmetry *)
67           proof
68         ]
69     | ProofBlock (subst, eq_URI, t', (pos, eq), eqproof) ->
70 (*         Printf.printf "\nsubst:\n%s\n" (print_subst subst); *)
71 (*         print_newline (); *)
72
73         let name, ty, eq_ty, left, right = t' in
74         let bo =
75           Cic.Appl [Cic.MutInd (HelmLibraryObjects.Logic.eq_URI, 0, []);
76                     eq_ty; left; right]
77         in
78         let t' = Cic.Lambda (name, ty, (* CicSubstitution.lift 1 *) bo) in
79         (*       Printf.printf "   ProofBlock: eq = %s, eq' = %s" *)
80         (*         (string_of_equality eq) (string_of_equality eq'); *)
81         (*       print_newline (); *)
82
83 (*         let s = String.make !indent ' ' in *)
84 (*         incr indent; *)
85         
86 (*         print_endline (s ^ "build proof'------------"); *)
87         
88         let proof' =
89           let _, proof', _, _, _ = eq in
90           do_build_proof proof'
91         in
92 (*         print_endline (s ^ "END proof'"); *)
93
94 (*         print_endline (s ^ "build eqproof-----------"); *)
95
96         let eqproof = do_build_proof eqproof in
97
98 (*         print_endline (s ^ "END eqproof"); *)
99 (*         decr indent; *)
100         
101         
102         let _, _, (ty, what, other, _), menv', args' = eq in
103         let what, other =
104           if pos = Utils.Left then what, other else other, what
105         in
106         CicMetaSubst.apply_subst subst
107           (Cic.Appl [Cic.Const (eq_URI, []); ty;
108                      what; t'; eqproof; other; proof'])
109
110   and do_build_goal_proof proofbit proof =
111 (*     match proofbit with *)
112 (*     | BasicProof _ -> do_build_proof proof *)
113 (*     | proofbit -> *)
114         match proof with
115         | ProofGoalBlock (pb, eq) ->
116             do_build_proof (ProofGoalBlock (replace_proof proofbit pb, eq))
117 (*             let _, proof, _, _, _  = eq in *)
118 (*             let newproof = replace_proof proofbit proof in *)
119 (*             do_build_proof newproof *)
120
121 (*         | ProofBlock (subst, eq_URI, t', poseq, eqproof) -> *)
122 (*             let eqproof' = replace_proof proofbit eqproof in *)
123 (*             do_build_proof (ProofBlock (subst, eq_URI, t', poseq, eqproof')) *)
124         | _ -> do_build_proof (replace_proof proofbit proof) (* assert false *)
125
126   and replace_proof newproof = function
127     | ProofBlock (subst, eq_URI, t', poseq, eqproof) ->
128         let uri = eq_URI in
129 (*           if eq_URI = HelmLibraryObjects.Logic.eq_ind_URI then *)
130 (*             HelmLibraryObjects.Logic.eq_ind_r_URI *)
131 (*           else *)
132 (*             HelmLibraryObjects.Logic.eq_ind_URI *)
133 (*         in *)
134         let eqproof' = replace_proof newproof eqproof in
135         ProofBlock (subst, uri(* eq_URI *), t', poseq, eqproof')
136 (*         ProofBlock (subst, eq_URI, t', poseq, newproof) *)
137     | ProofGoalBlock (pb, equality) ->
138         let pb' = replace_proof newproof pb in
139         ProofGoalBlock (pb', equality)
140 (*         let w, proof, t, menv, args = equality in *)
141 (*         let proof' = replace_proof newproof proof in *)
142 (*         ProofGoalBlock (pb, (w, proof', t, menv, args)) *)
143     | BasicProof _ -> newproof
144     | p -> p
145   in
146   let _, proof, _, _, _ = equality in
147   do_build_proof proof
148 ;;
149
150
151 let rec metas_of_term = function
152   | Cic.Meta (i, c) -> [i]
153   | Cic.Var (_, ens) 
154   | Cic.Const (_, ens) 
155   | Cic.MutInd (_, _, ens) 
156   | Cic.MutConstruct (_, _, _, ens) ->
157       List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
158   | Cic.Cast (s, t)
159   | Cic.Prod (_, s, t)
160   | Cic.Lambda (_, s, t)
161   | Cic.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
162   | Cic.Appl l -> List.flatten (List.map metas_of_term l)
163   | Cic.MutCase (uri, i, s, t, l) ->
164       (metas_of_term s) @ (metas_of_term t) @
165         (List.flatten (List.map metas_of_term l))
166   | Cic.Fix (i, il) ->
167       List.flatten
168         (List.map (fun (s, i, t1, t2) ->
169                      (metas_of_term t1) @ (metas_of_term t2)) il)
170   | Cic.CoFix (i, il) ->
171       List.flatten
172         (List.map (fun (s, t1, t2) ->
173                      (metas_of_term t1) @ (metas_of_term t2)) il)
174   | _ -> []
175 ;;      
176
177
178 exception NotMetaConvertible;;
179
180 let meta_convertibility_aux table t1 t2 =
181   let module C = Cic in
182   let print_table t =
183     String.concat ", "
184       (List.map
185          (fun (k, v) -> Printf.sprintf "(%d, %d)" k v) t)
186   in
187   let rec aux ((table_l, table_r) as table) t1 t2 =
188 (*     Printf.printf "aux %s, %s\ntable_l: %s, table_r: %s\n" *)
189 (*       (CicPp.ppterm t1) (CicPp.ppterm t2) *)
190 (*       (print_table table_l) (print_table table_r); *)
191     match t1, t2 with
192     | C.Meta (m1, tl1), C.Meta (m2, tl2) ->
193         let m1_binding, table_l =
194           try List.assoc m1 table_l, table_l
195           with Not_found -> m2, (m1, m2)::table_l
196         and m2_binding, table_r =
197           try List.assoc m2 table_r, table_r
198           with Not_found -> m1, (m2, m1)::table_r
199         in
200 (*         let m1_binding, m2_binding, table = *)
201 (*           let m1b, table =  *)
202 (*             try List.assoc m1 table, table *)
203 (*             with Not_found -> m2, (m1, m2)::table *)
204 (*           in *)
205 (*           let m2b, table =  *)
206 (*             try List.assoc m2 table, table *)
207 (*             with Not_found -> m1, (m2, m1)::table *)
208 (*           in *)
209 (*           m1b, m2b, table *)
210 (*         in *)
211 (*         Printf.printf "table_l: %s\ntable_r: %s\n\n" *)
212 (*           (print_table table_l) (print_table table_r); *)
213         if (m1_binding <> m2) || (m2_binding <> m1) then
214           raise NotMetaConvertible
215         else (
216           try
217             List.fold_left2
218               (fun res t1 t2 ->
219                  match t1, t2 with
220                  | None, Some _ | Some _, None -> raise NotMetaConvertible
221                  | None, None -> res
222                  | Some t1, Some t2 -> (aux res t1 t2))
223               (table_l, table_r) tl1 tl2
224           with Invalid_argument _ ->
225             raise NotMetaConvertible
226         )
227     | C.Var (u1, ens1), C.Var (u2, ens2)
228     | C.Const (u1, ens1), C.Const (u2, ens2) when (UriManager.eq u1 u2) ->
229         aux_ens table ens1 ens2
230     | C.Cast (s1, t1), C.Cast (s2, t2)
231     | C.Prod (_, s1, t1), C.Prod (_, s2, t2)
232     | C.Lambda (_, s1, t1), C.Lambda (_, s2, t2)
233     | C.LetIn (_, s1, t1), C.LetIn (_, s2, t2) ->
234         let table = aux table s1 s2 in
235         aux table t1 t2
236     | C.Appl l1, C.Appl l2 -> (
237         try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
238         with Invalid_argument _ -> raise NotMetaConvertible
239       )
240     | C.MutInd (u1, i1, ens1), C.MutInd (u2, i2, ens2)
241         when (UriManager.eq u1 u2) && i1 = i2 -> aux_ens table ens1 ens2
242     | C.MutConstruct (u1, i1, j1, ens1), C.MutConstruct (u2, i2, j2, ens2)
243         when (UriManager.eq u1 u2) && i1 = i2 && j1 = j2 ->
244         aux_ens table ens1 ens2
245     | C.MutCase (u1, i1, s1, t1, l1), C.MutCase (u2, i2, s2, t2, l2)
246         when (UriManager.eq u1 u2) && i1 = i2 ->
247         let table = aux table s1 s2 in
248         let table = aux table t1 t2 in (
249           try List.fold_left2 (fun res t1 t2 -> (aux res t1 t2)) table l1 l2
250           with Invalid_argument _ -> raise NotMetaConvertible
251         )
252     | C.Fix (i1, il1), C.Fix (i2, il2) when i1 = i2 -> (
253         try
254           List.fold_left2
255             (fun res (n1, i1, s1, t1) (n2, i2, s2, t2) ->
256                if i1 <> i2 then raise NotMetaConvertible
257                else
258                  let res = (aux res s1 s2) in aux res t1 t2)
259             table il1 il2
260         with Invalid_argument _ -> raise NotMetaConvertible
261       )
262     | C.CoFix (i1, il1), C.CoFix (i2, il2) when i1 = i2 -> (
263         try
264           List.fold_left2
265             (fun res (n1, s1, t1) (n2, s2, t2) ->
266                let res = aux res s1 s2 in aux res t1 t2)
267             table il1 il2
268         with Invalid_argument _ -> raise NotMetaConvertible
269       )
270     | t1, t2 when t1 = t2 -> table
271     | _, _ -> raise NotMetaConvertible
272         
273   and aux_ens table ens1 ens2 =
274     let cmp (u1, t1) (u2, t2) =
275       compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2)
276     in
277     let ens1 = List.sort cmp ens1
278     and ens2 = List.sort cmp ens2 in
279     try
280       List.fold_left2
281         (fun res (u1, t1) (u2, t2) ->
282            if not (UriManager.eq u1 u2) then raise NotMetaConvertible
283            else aux res t1 t2)
284         table ens1 ens2
285     with Invalid_argument _ -> raise NotMetaConvertible
286   in
287   aux table t1 t2
288 ;;
289
290
291 let meta_convertibility_eq eq1 eq2 =
292   let _, _, (ty, left, right, _), _, _ = eq1
293   and _, _, (ty', left', right', _), _, _ = eq2 in
294   if ty <> ty' then
295     false
296   else if (left = left') && (right = right') then
297     true
298   else if (left = right') && (right = left') then
299     true
300   else
301     try
302       let table = meta_convertibility_aux ([], []) left left' in
303       let _ = meta_convertibility_aux table right right' in
304       true
305     with NotMetaConvertible ->
306       try
307         let table = meta_convertibility_aux ([], []) left right' in
308         let _ = meta_convertibility_aux table right left' in
309         true
310       with NotMetaConvertible ->
311         false
312 ;;
313
314
315 let meta_convertibility t1 t2 =
316   let f t =
317     String.concat ", "
318       (List.map
319          (fun (k, v) -> Printf.sprintf "(%d, %d)" k v) t)
320   in
321   if t1 = t2 then
322     true
323   else
324     try
325       let l, r = meta_convertibility_aux ([], []) t1 t2 in
326       (*     Printf.printf "meta_convertibility:\n%s\n%s\n\n" (f l) (f r); *)
327       true
328     with NotMetaConvertible ->
329       false
330 ;;
331
332
333 (*
334 let replace_metas (* context *) term =
335   let module C = Cic in
336   let rec aux = function
337     | C.Meta (i, c) ->
338 (*         let irl = *)
339 (*           CicMkImplicit.identity_relocation_list_for_metavariable context *)
340 (*         in *)
341 (*         if c = irl then *)
342 (*           C.Implicit (Some (`MetaIndex i)) *)
343 (*         else ( *)
344 (*           Printf.printf "WARNING: c non e` un identity_relocation_list!\n%s\n" *)
345 (*             (String.concat "\n" *)
346 (*                (List.map *)
347 (*                   (function None -> "" | Some t -> CicPp.ppterm t) c)); *)
348 (*           C.Meta (i, c) *)
349 (*         ) *)
350         C.Implicit (Some (`MetaInfo (i, c)))
351     | C.Var (u, ens) -> C.Var (u, aux_ens ens)
352     | C.Const (u, ens) -> C.Const (u, aux_ens ens)
353     | C.Cast (s, t) -> C.Cast (aux s, aux t)
354     | C.Prod (name, s, t) -> C.Prod (name, aux s, aux t)
355     | C.Lambda (name, s, t) -> C.Lambda (name, aux s, aux t)
356     | C.LetIn (name, s, t) -> C.LetIn (name, aux s, aux t)
357     | C.Appl l -> C.Appl (List.map aux l)
358     | C.MutInd (uri, i, ens) -> C.MutInd (uri, i, aux_ens ens)
359     | C.MutConstruct (uri, i, j, ens) -> C.MutConstruct (uri, i, j, aux_ens ens)
360     | C.MutCase (uri, i, s, t, l) ->
361         C.MutCase (uri, i, aux s, aux t, List.map aux l)
362     | C.Fix (i, il) ->
363         let il' =
364           List.map (fun (s, i, t1, t2) -> (s, i, aux t1, aux t2)) il in
365         C.Fix (i, il')
366     | C.CoFix (i, il) ->
367         let il' =
368           List.map (fun (s, t1, t2) -> (s, aux t1, aux t2)) il in
369         C.CoFix (i, il')
370     | t -> t
371   and aux_ens ens =
372     List.map (fun (u, t) -> (u, aux t)) ens
373   in
374   aux term
375 ;;
376
377
378 let restore_metas (* context *) term =
379   let module C = Cic in
380   let rec aux = function
381     | C.Implicit (Some (`MetaInfo (i, c))) ->
382 (*         let c = *)
383 (*           CicMkImplicit.identity_relocation_list_for_metavariable context *)
384 (*         in *)
385 (*         C.Meta (i, c) *)
386 (*         let local_context:(C.term option) list = *)
387 (*           Marshal.from_string mc 0 *)
388 (*         in *)
389 (*         C.Meta (i, local_context) *)
390         C.Meta (i, c)
391     | C.Var (u, ens) -> C.Var (u, aux_ens ens)
392     | C.Const (u, ens) -> C.Const (u, aux_ens ens)
393     | C.Cast (s, t) -> C.Cast (aux s, aux t)
394     | C.Prod (name, s, t) -> C.Prod (name, aux s, aux t)
395     | C.Lambda (name, s, t) -> C.Lambda (name, aux s, aux t)
396     | C.LetIn (name, s, t) -> C.LetIn (name, aux s, aux t)
397     | C.Appl l -> C.Appl (List.map aux l)
398     | C.MutInd (uri, i, ens) -> C.MutInd (uri, i, aux_ens ens)
399     | C.MutConstruct (uri, i, j, ens) -> C.MutConstruct (uri, i, j, aux_ens ens)
400     | C.MutCase (uri, i, s, t, l) ->
401         C.MutCase (uri, i, aux s, aux t, List.map aux l)
402     | C.Fix (i, il) ->
403         let il' =
404           List.map (fun (s, i, t1, t2) -> (s, i, aux t1, aux t2)) il in
405         C.Fix (i, il')
406     | C.CoFix (i, il) ->
407         let il' =
408           List.map (fun (s, t1, t2) -> (s, aux t1, aux t2)) il in
409         C.CoFix (i, il')
410     | t -> t
411   and aux_ens ens =
412     List.map (fun (u, t) -> (u, aux t)) ens
413   in
414   aux term
415 ;;
416
417
418 let rec restore_subst (* context *) subst =
419   List.map
420     (fun (i, (c, t, ty)) ->
421        i, (c, restore_metas (* context *) t, ty))
422     subst
423 ;;
424 *)
425
426
427 let rec check_irl start = function
428   | [] -> true
429   | None::tl -> check_irl (start+1) tl
430   | (Some (Cic.Rel x))::tl ->
431       if x = start then check_irl (start+1) tl else false
432   | _ -> false
433 ;;
434
435 let rec is_simple_term = function
436   | Cic.Appl ((Cic.Meta _)::_) -> false
437   | Cic.Appl l -> List.for_all is_simple_term l
438   | Cic.Meta (i, l) -> check_irl 1 l
439   | Cic.Rel _ -> true
440   | Cic.Const _ -> true
441   | Cic.MutInd (_, _, []) -> true
442   | Cic.MutConstruct (_, _, _, []) -> true
443   | _ -> false
444 ;;
445
446
447 let lookup_subst meta subst =
448   match meta with
449   | Cic.Meta (i, _) -> (
450       try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
451       with Not_found -> meta
452     )
453   | _ -> assert false
454 ;;
455
456
457 let unification_simple metasenv context t1 t2 ugraph =
458   let module C = Cic in
459   let module M = CicMetaSubst in
460   let module U = CicUnification in
461   let lookup = lookup_subst in
462   let rec occurs_check subst what where =
463     match where with
464     | t when what = t -> true
465     | C.Appl l -> List.exists (occurs_check subst what) l
466     | C.Meta _ ->
467         let t = lookup where subst in
468         if t <> where then occurs_check subst what t else false
469     | _ -> false
470   in
471   let rec unif subst menv s t =
472     let s = match s with C.Meta _ -> lookup s subst | _ -> s
473     and t = match t with C.Meta _ -> lookup t subst | _ -> t
474     in
475     match s, t with
476     | s, t when s = t -> subst, menv
477     | C.Meta (i, _), C.Meta (j, _) when i > j ->
478         unif subst menv t s
479     | C.Meta _, t when occurs_check subst s t ->
480         raise (U.UnificationFailure "Inference.unification.unif")
481     | C.Meta (i, l), t -> (
482         try
483           let _, _, ty = CicUtil.lookup_meta i menv in
484           let subst =
485             if not (List.mem_assoc i subst) then (i, (context, t, ty))::subst
486             else subst
487           in
488           let menv = menv in (* List.filter (fun (m, _, _) -> i <> m) menv in *)
489           subst, menv
490         with CicUtil.Meta_not_found m ->
491           let names = names_of_context context in
492           debug_print (
493             Printf.sprintf "Meta_not_found %d!: %s %s\n%s\n\n%s" m
494               (CicPp.pp t1 names) (CicPp.pp t2 names)
495               (print_metasenv menv) (print_metasenv metasenv));
496           assert false
497       )
498     | _, C.Meta _ -> unif subst menv t s
499     | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt ->
500         raise (U.UnificationFailure "Inference.unification.unif")
501     | C.Appl (hds::tls), C.Appl (hdt::tlt) -> (
502         try
503           List.fold_left2
504             (fun (subst', menv) s t -> unif subst' menv s t)
505             (subst, menv) tls tlt
506         with Invalid_argument _ ->
507           raise (U.UnificationFailure "Inference.unification.unif")
508       )
509     | _, _ -> raise (U.UnificationFailure "Inference.unification.unif")
510   in
511   let subst, menv = unif [] metasenv t1 t2 in
512   let menv =
513     List.filter
514       (fun (m, _, _) ->
515          try let _ = List.find (fun (i, _) -> m = i) subst in false
516          with Not_found -> true)
517       menv
518   in
519   List.rev subst, menv, ugraph
520 ;;
521
522
523 let unification metasenv context t1 t2 ugraph =
524 (*   Printf.printf "| unification %s %s\n" (CicPp.ppterm t1) (CicPp.ppterm t2); *)
525   let subst, menv, ug =
526     if not (is_simple_term t1) || not (is_simple_term t2) then (
527       debug_print (
528         Printf.sprintf "NOT SIMPLE TERMS: %s %s"
529           (CicPp.ppterm t1) (CicPp.ppterm t2));
530       CicUnification.fo_unif metasenv context t1 t2 ugraph
531     ) else
532       unification_simple metasenv context t1 t2 ugraph
533   in
534   let rec fix_term = function
535     | (Cic.Meta (i, l) as t) ->
536         let t' = lookup_subst t subst in
537         if t <> t' then fix_term t' else t
538     | Cic.Appl l -> Cic.Appl (List.map fix_term l)
539     | t -> t
540   in
541   let rec fix_subst = function
542     | [] -> []
543     | (i, (c, t, ty))::tl -> (i, (c, fix_term t, fix_term ty))::(fix_subst tl)
544   in
545 (*   Printf.printf "| subst: %s\n" (print_subst ~prefix:" ; " subst); *)
546 (*   print_endline "|"; *)
547   fix_subst subst, menv, ug
548 ;;
549
550
551 (* let unification = CicUnification.fo_unif;; *)
552
553 exception MatchingFailure;;
554
555
556 let matching_simple metasenv context t1 t2 ugraph =
557   let module C = Cic in
558   let module M = CicMetaSubst in
559   let module U = CicUnification in
560   let lookup meta subst =
561     match meta with
562     | C.Meta (i, _) -> (
563         try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst in t
564         with Not_found -> meta
565       )
566     | _ -> assert false
567   in
568   let rec do_match subst menv s t =
569 (*     Printf.printf "do_match %s %s\n%s\n" (CicPp.ppterm s) (CicPp.ppterm t) *)
570 (*       (print_subst subst); *)
571 (*     print_newline (); *)
572 (*     let s = match s with C.Meta _ -> lookup s subst | _ -> s *)
573 (*     let t = match t with C.Meta _ -> lookup t subst | _ -> t in  *)
574     (*       Printf.printf "after apply_subst: %s %s\n%s" *)
575     (*         (CicPp.ppterm s) (CicPp.ppterm t) (print_subst subst); *)
576     (*       print_newline (); *)
577     match s, t with
578     | s, t when s = t -> subst, menv
579 (*     | C.Meta (i, _), C.Meta (j, _) when i > j -> *)
580 (*         do_match subst menv t s *)
581 (*     | C.Meta _, t when occurs_check subst s t -> *)
582 (*         raise MatchingFailure *)
583 (*     | s, C.Meta _ when occurs_check subst t s -> *)
584 (*         raise MatchingFailure *)
585     | s, C.Meta (i, l) ->
586         let filter_menv i menv =
587           List.filter (fun (m, _, _) -> i <> m) menv
588         in
589         let subst, menv =
590           let value = lookup t subst in
591           match value with
592 (*           | C.Meta (i', l') when Hashtbl.mem table i' -> *)
593 (*               (i', (context, s, ty))::subst, menv (\* filter_menv i' menv *\) *)
594           | value when value = t ->
595               let _, _, ty = CicUtil.lookup_meta i menv in
596               (i, (context, s, ty))::subst, filter_menv i menv
597           | value when value <> s ->
598               raise MatchingFailure
599           | value -> do_match subst menv s value
600         in
601         subst, menv
602 (*           else if value <> s then *)
603 (*             raise MatchingFailure *)
604 (*           else subst *)
605 (*           if not (List.mem_assoc i subst) then (i, (context, t, ty))::subst *)
606 (*           else subst *)
607 (*         in *)
608 (*         let menv = List.filter (fun (m, _, _) -> i <> m) menv in *)
609 (*         subst, menv *)
610 (*     | _, C.Meta _ -> do_match subst menv t s *)
611 (*     | C.Appl (hds::_), C.Appl (hdt::_) when hds <> hdt -> *)
612 (*         raise MatchingFailure *)
613     | C.Appl ls, C.Appl lt -> (
614         try
615           List.fold_left2
616             (fun (subst, menv) s t -> do_match subst menv s t)
617             (subst, menv) ls lt
618         with Invalid_argument _ ->
619 (*           print_endline (Printexc.to_string e); *)
620 (*           Printf.printf "NO MATCH: %s %s\n" (CicPp.ppterm s) (CicPp.ppterm t); *)
621 (*           print_newline ();           *)
622           raise MatchingFailure
623       )
624     | _, _ ->
625 (*         Printf.printf "NO MATCH: %s %s\n" (CicPp.ppterm s) (CicPp.ppterm t); *)
626 (*         print_newline (); *)
627         raise MatchingFailure
628   in
629   let subst, menv = do_match [] metasenv t1 t2 in
630   (*     Printf.printf "DONE!: subst = \n%s\n" (print_subst subst); *)
631   (*     print_newline (); *)
632   subst, menv, ugraph
633 ;;
634
635
636 let matching metasenv context t1 t2 ugraph =
637 (*   if (is_simple_term t1) && (is_simple_term t2) then *)
638 (*     let subst, menv, ug = *)
639 (*       matching_simple metasenv context t1 t2 ugraph in *)
640 (* (\*     Printf.printf "matching %s %s:\n%s\n" *\) *)
641 (* (\*       (CicPp.ppterm t1) (CicPp.ppterm t2) (print_subst subst); *\) *)
642 (* (\*     print_newline (); *\) *)
643 (*     subst, menv, ug *)
644 (*   else *)
645 (*   Printf.printf "matching %s %s" (CicPp.ppterm t1) (CicPp.ppterm t2); *)
646 (*   print_newline (); *)
647     try
648       let subst, metasenv, ugraph =
649         (*       CicUnification.fo_unif metasenv context t1 t2 ugraph *)
650         unification metasenv context t1 t2 ugraph
651       in
652       let t' = CicMetaSubst.apply_subst subst t1 in
653       if not (meta_convertibility t1 t') then
654         raise MatchingFailure
655       else
656         let metas = metas_of_term t1 in
657         let fix_subst = function
658           | (i, (c, Cic.Meta (j, lc), ty)) when List.mem i metas ->
659               (j, (c, Cic.Meta (i, lc), ty))
660           | s -> s
661         in
662         let subst = List.map fix_subst subst in
663
664 (*         Printf.printf "matching %s %s:\n%s\n" *)
665 (*           (CicPp.ppterm t1) (CicPp.ppterm t2) (print_subst subst); *)
666 (*         print_newline (); *)
667
668         subst, metasenv, ugraph
669     with
670     | CicUnification.UnificationFailure _
671     | CicUnification.Uncertain _ ->
672 (*       Printf.printf "failed to match %s %s\n" *)
673 (*         (CicPp.ppterm t1) (CicPp.ppterm t2); *)
674 (*       print_endline (Printexc.to_string e); *)
675       raise MatchingFailure
676 ;;
677
678 (* let matching = *)
679 (*   let profile = CicUtil.profile "Inference.matching" in *)
680 (*   (fun metasenv context t1 t2 ugraph -> *)
681 (*      profile (matching metasenv context t1 t2) ugraph) *)
682 (* ;; *)
683
684
685 let beta_expand ?(metas_ok=true) ?(match_only=false)
686     what type_of_what where context metasenv ugraph = 
687   let module S = CicSubstitution in
688   let module C = Cic in
689
690 (*   let _ = *)
691 (*     let names = names_of_context context in *)
692 (*     Printf.printf "beta_expand:\nwhat: %s, %s\nwhere: %s, %s\n" *)
693 (*       (CicPp.pp what names) (CicPp.ppterm what) *)
694 (*       (CicPp.pp where names) (CicPp.ppterm where); *)
695 (*     print_newline (); *)
696 (*   in *)
697   (*
698     return value:
699     ((list of all possible beta expansions, subst, metasenv, ugraph),
700      lifted term)
701   *)
702   let rec aux lift_amount term context metasenv subst ugraph =
703 (*     Printf.printf "enter aux %s\n" (CicPp.ppterm term); *)
704     let res, lifted_term = 
705       match term with
706       | C.Rel m  ->
707           [], if m <= lift_amount then C.Rel m else C.Rel (m+1)
708             
709       | C.Var (uri, exp_named_subst) ->
710           let ens', lifted_ens =
711             aux_ens lift_amount exp_named_subst context metasenv subst ugraph
712           in
713           let expansions = 
714             List.map
715               (fun (e, s, m, ug) ->
716                  (C.Var (uri, e), s, m, ug)) ens'
717           in
718           expansions, C.Var (uri, lifted_ens)
719             
720       | C.Meta (i, l) ->
721           let l', lifted_l = 
722             List.fold_right
723               (fun arg (res, lifted_tl) ->
724                  match arg with
725                  | Some arg ->
726                      let arg_res, lifted_arg =
727                        aux lift_amount arg context metasenv subst ugraph in
728                      let l1 =
729                        List.map
730                          (fun (a, s, m, ug) -> (Some a)::lifted_tl, s, m, ug)
731                          arg_res
732                      in
733                      (l1 @
734                         (List.map
735                            (fun (r, s, m, ug) -> (Some lifted_arg)::r, s, m, ug)
736                            res),
737                       (Some lifted_arg)::lifted_tl)
738                  | None ->
739                      (List.map
740                         (fun (r, s, m, ug) -> None::r, s, m, ug)
741                         res, 
742                       None::lifted_tl)
743               ) l ([], [])
744           in
745           let e = 
746             List.map
747               (fun (l, s, m, ug) ->
748                  (C.Meta (i, l), s, m, ug)) l'
749           in
750           e, C.Meta (i, lifted_l)
751             
752       | C.Sort _
753       | C.Implicit _ as t -> [], t
754           
755       | C.Cast (s, t) ->
756           let l1, lifted_s =
757             aux lift_amount s context metasenv subst ugraph in
758           let l2, lifted_t =
759             aux lift_amount t context metasenv subst ugraph
760           in
761           let l1' =
762             List.map
763               (fun (t, s, m, ug) ->
764                  C.Cast (t, lifted_t), s, m, ug) l1 in
765           let l2' =
766             List.map
767               (fun (t, s, m, ug) ->
768                  C.Cast (lifted_s, t), s, m, ug) l2 in
769           l1'@l2', C.Cast (lifted_s, lifted_t)
770             
771       | C.Prod (nn, s, t) ->
772           let l1, lifted_s =
773             aux lift_amount s context metasenv subst ugraph in
774           let l2, lifted_t =
775             aux (lift_amount+1) t ((Some (nn, C.Decl s))::context)
776               metasenv subst ugraph
777           in
778           let l1' =
779             List.map
780               (fun (t, s, m, ug) ->
781                  C.Prod (nn, t, lifted_t), s, m, ug) l1 in
782           let l2' =
783             List.map
784               (fun (t, s, m, ug) ->
785                  C.Prod (nn, lifted_s, t), s, m, ug) l2 in
786           l1'@l2', C.Prod (nn, lifted_s, lifted_t)
787
788       | C.Lambda (nn, s, t) ->
789           let l1, lifted_s =
790             aux lift_amount s context metasenv subst ugraph in
791           let l2, lifted_t =
792             aux (lift_amount+1) t ((Some (nn, C.Decl s))::context)
793               metasenv subst ugraph
794           in
795           let l1' =
796             List.map
797               (fun (t, s, m, ug) ->
798                  C.Lambda (nn, t, lifted_t), s, m, ug) l1 in
799           let l2' =
800             List.map
801               (fun (t, s, m, ug) ->
802                  C.Lambda (nn, lifted_s, t), s, m, ug) l2 in
803           l1'@l2', C.Lambda (nn, lifted_s, lifted_t)
804
805       | C.LetIn (nn, s, t) ->
806           let l1, lifted_s =
807             aux lift_amount s context metasenv subst ugraph in
808           let l2, lifted_t =
809             aux (lift_amount+1) t ((Some (nn, C.Def (s, None)))::context)
810               metasenv subst ugraph
811           in
812           let l1' =
813             List.map
814               (fun (t, s, m, ug) ->
815                  C.LetIn (nn, t, lifted_t), s, m, ug) l1 in
816           let l2' =
817             List.map
818               (fun (t, s, m, ug) ->
819                  C.LetIn (nn, lifted_s, t), s, m, ug) l2 in
820           l1'@l2', C.LetIn (nn, lifted_s, lifted_t)
821
822       | C.Appl l ->
823           let l', lifted_l =
824             aux_list lift_amount l context metasenv subst ugraph
825           in
826           (List.map (fun (l, s, m, ug) -> (C.Appl l, s, m, ug)) l',
827            C.Appl lifted_l)
828             
829       | C.Const (uri, exp_named_subst) ->
830           let ens', lifted_ens =
831             aux_ens lift_amount exp_named_subst context metasenv subst ugraph
832           in
833           let expansions = 
834             List.map
835               (fun (e, s, m, ug) ->
836                  (C.Const (uri, e), s, m, ug)) ens'
837           in
838           (expansions, C.Const (uri, lifted_ens))
839
840       | C.MutInd (uri, i ,exp_named_subst) ->
841           let ens', lifted_ens =
842             aux_ens lift_amount exp_named_subst context metasenv subst ugraph
843           in
844           let expansions = 
845             List.map
846               (fun (e, s, m, ug) ->
847                  (C.MutInd (uri, i, e), s, m, ug)) ens'
848           in
849           (expansions, C.MutInd (uri, i, lifted_ens))
850
851       | C.MutConstruct (uri, i, j, exp_named_subst) ->
852           let ens', lifted_ens =
853             aux_ens lift_amount exp_named_subst context metasenv subst ugraph
854           in
855           let expansions = 
856             List.map
857               (fun (e, s, m, ug) ->
858                  (C.MutConstruct (uri, i, j, e), s, m, ug)) ens'
859           in
860           (expansions, C.MutConstruct (uri, i, j, lifted_ens))
861
862       | C.MutCase (sp, i, outt, t, pl) ->
863           let pl_res, lifted_pl =
864             aux_list lift_amount pl context metasenv subst ugraph
865           in
866           let l1, lifted_outt =
867             aux lift_amount outt context metasenv subst ugraph in
868           let l2, lifted_t =
869             aux lift_amount t context metasenv subst ugraph in
870
871           let l1' =
872             List.map
873               (fun (outt, s, m, ug) ->
874                  C.MutCase (sp, i, outt, lifted_t, lifted_pl), s, m, ug) l1 in
875           let l2' =
876             List.map
877               (fun (t, s, m, ug) ->
878                  C.MutCase (sp, i, lifted_outt, t, lifted_pl), s, m, ug) l2 in
879           let l3' =
880             List.map
881               (fun (pl, s, m, ug) ->
882                  C.MutCase (sp, i, lifted_outt, lifted_t, pl), s, m, ug) pl_res
883           in
884           (l1'@l2'@l3', C.MutCase (sp, i, lifted_outt, lifted_t, lifted_pl))
885
886       | C.Fix (i, fl) ->
887           let len = List.length fl in
888           let fl', lifted_fl =
889             List.fold_right
890               (fun (nm, idx, ty, bo) (res, lifted_tl) ->
891                  let lifted_ty = S.lift lift_amount ty in
892                  let bo_res, lifted_bo =
893                    aux (lift_amount+len) bo context metasenv subst ugraph in
894                  let l1 =
895                    List.map
896                      (fun (a, s, m, ug) ->
897                         (nm, idx, lifted_ty, a)::lifted_tl, s, m, ug)
898                      bo_res
899                  in
900                  (l1 @
901                     (List.map
902                        (fun (r, s, m, ug) ->
903                           (nm, idx, lifted_ty, lifted_bo)::r, s, m, ug) res),
904                   (nm, idx, lifted_ty, lifted_bo)::lifted_tl)
905               ) fl ([], [])
906           in
907           (List.map
908              (fun (fl, s, m, ug) -> C.Fix (i, fl), s, m, ug) fl',
909            C.Fix (i, lifted_fl))
910             
911       | C.CoFix (i, fl) ->
912           let len = List.length fl in
913           let fl', lifted_fl =
914             List.fold_right
915               (fun (nm, ty, bo) (res, lifted_tl) ->
916                  let lifted_ty = S.lift lift_amount ty in
917                  let bo_res, lifted_bo =
918                    aux (lift_amount+len) bo context metasenv subst ugraph in
919                  let l1 =
920                    List.map
921                      (fun (a, s, m, ug) ->
922                         (nm, lifted_ty, a)::lifted_tl, s, m, ug)
923                      bo_res
924                  in
925                  (l1 @
926                     (List.map
927                        (fun (r, s, m, ug) ->
928                           (nm, lifted_ty, lifted_bo)::r, s, m, ug) res),
929                   (nm, lifted_ty, lifted_bo)::lifted_tl)
930               ) fl ([], [])
931           in
932           (List.map
933              (fun (fl, s, m, ug) -> C.CoFix (i, fl), s, m, ug) fl',
934            C.CoFix (i, lifted_fl))
935     in
936     let retval = 
937       match term with
938       | C.Meta _ when (not metas_ok) ->
939           res, lifted_term
940       | _ ->
941 (*           let term' = *)
942 (*             if match_only then replace_metas context term *)
943 (*             else term *)
944 (*           in *)
945           try
946             let subst', metasenv', ugraph' =
947 (*               Printf.printf "provo a unificare %s e %s\n" *)
948 (*                 (CicPp.ppterm (S.lift lift_amount what)) (CicPp.ppterm term); *)
949               if match_only then
950                 matching metasenv context term (S.lift lift_amount what) ugraph
951               else
952                 CicUnification.fo_unif metasenv context
953                   (S.lift lift_amount what) term ugraph
954             in
955 (*           Printf.printf "Ok, trovato: %s\n\nwhat: %s" (CicPp.ppterm term) *)
956 (*             (CicPp.ppterm (S.lift lift_amount what)); *)
957 (*           Printf.printf "substitution:\n%s\n\n" (print_subst subst'); *)
958 (*           Printf.printf "metasenv': %s\n" (print_metasenv metasenv'); *)
959             (* Printf.printf "metasenv: %s\n\n" (print_metasenv metasenv); *)
960 (*             if match_only then *)
961 (*               let t' = CicMetaSubst.apply_subst subst' term in *)
962 (*               if not (meta_convertibility term t') then ( *)
963 (*                 res, lifted_term *)
964 (*               ) else ( *)
965 (*                 let metas = metas_of_term term in *)
966 (*                 let fix_subst = function *)
967 (*                   | (i, (c, C.Meta (j, lc), ty)) when List.mem i metas -> *)
968 (*                       (j, (c, C.Meta (i, lc), ty)) *)
969 (*                   | s -> s *)
970 (*                 in *)
971 (*                 let subst' = List.map fix_subst subst' in *)
972 (*                 ((C.Rel (1 + lift_amount), subst', metasenv', ugraph')::res, *)
973 (*                  lifted_term) *)
974 (*               ) *)
975 (*             else *)
976               ((C.Rel (1 + lift_amount), subst', metasenv', ugraph')::res,
977                lifted_term)
978           with
979           | MatchingFailure
980           | CicUnification.UnificationFailure _
981           | CicUnification.Uncertain _ ->
982               res, lifted_term
983     in
984 (*     Printf.printf "exit aux\n"; *)
985     retval
986
987   and aux_list lift_amount l context metasenv subst ugraph =
988     List.fold_right
989       (fun arg (res, lifted_tl) ->
990          let arg_res, lifted_arg =
991            aux lift_amount arg context metasenv subst ugraph in
992          let l1 = List.map
993            (fun (a, s, m, ug) -> a::lifted_tl, s, m, ug) arg_res
994          in
995          (l1 @ (List.map
996                   (fun (r, s, m, ug) -> lifted_arg::r, s, m, ug) res),
997           lifted_arg::lifted_tl)
998       ) l ([], [])
999
1000   and aux_ens lift_amount exp_named_subst context metasenv subst ugraph =
1001     List.fold_right
1002       (fun (u, arg) (res, lifted_tl) ->
1003          let arg_res, lifted_arg =
1004            aux lift_amount arg context metasenv subst ugraph in
1005          let l1 =
1006            List.map
1007              (fun (a, s, m, ug) -> (u, a)::lifted_tl, s, m, ug) arg_res
1008          in
1009          (l1 @ (List.map (fun (r, s, m, ug) ->
1010                             (u, lifted_arg)::r, s, m, ug) res),
1011           (u, lifted_arg)::lifted_tl)
1012       ) exp_named_subst ([], [])
1013
1014   in
1015   let expansions, _ =
1016 (*     let where = *)
1017 (*       if match_only then replace_metas (\* context *\) where *)
1018 (*       else where *)
1019 (*     in *)
1020     aux 0 where context metasenv [] ugraph
1021   in
1022   let mapfun =
1023 (*     if match_only then *)
1024 (*       (fun (term, subst, metasenv, ugraph) -> *)
1025 (*          let term' = *)
1026 (*            C.Lambda (C.Anonymous, type_of_what, restore_metas term) *)
1027 (*          and subst = restore_subst subst in *)
1028 (*          (term', subst, metasenv, ugraph)) *)
1029 (*     else *)
1030       (fun (term, subst, metasenv, ugraph) ->
1031          let term' = C.Lambda (C.Anonymous, type_of_what, term) in
1032          (term', subst, metasenv, ugraph))
1033   in
1034   List.map mapfun expansions
1035 ;;
1036
1037
1038 let find_equalities ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) context proof =
1039   let module C = Cic in
1040   let module S = CicSubstitution in
1041   let module T = CicTypeChecker in
1042   let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in
1043   let ok_types ty menv =
1044     List.for_all (fun (_, _, mt) -> mt = ty) menv
1045   in
1046   let rec aux index newmeta = function
1047     | [] -> [], newmeta
1048     | (Some (_, C.Decl (term)))::tl ->
1049         let do_find context term =
1050           match term with
1051           | C.Prod (name, s, t) ->
1052 (*               let newmeta = ProofEngineHelpers.new_meta_of_proof ~proof in *)
1053               let (head, newmetas, args, newmeta) =
1054                 ProofEngineHelpers.saturate_term newmeta []
1055                   context (S.lift index term)
1056               in
1057               let p =
1058                 if List.length args = 0 then
1059                   C.Rel index
1060                 else
1061                   C.Appl ((C.Rel index)::args)
1062               in (
1063                 match head with
1064                 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
1065                     when (UriManager.eq uri eq_uri) && (ok_types ty newmetas) ->
1066                     debug_print (
1067                       Printf.sprintf "OK: %s" (CicPp.ppterm term));
1068 (*                     debug_print ( *)
1069 (*                       Printf.sprintf "args: %s\n" *)
1070 (*                         (String.concat ", " (List.map CicPp.ppterm args))); *)
1071 (*                     debug_print ( *)
1072 (*                       Printf.sprintf "newmetas:\n%s\n" *)
1073 (*                         (print_metasenv newmetas)); *)
1074                     let o = !Utils.compare_terms t1 t2 in
1075                     let w = compute_equality_weight ty t1 t2 in
1076                     let proof = BasicProof p in
1077                     let e = (w, proof, (ty, t1, t2, o), newmetas, args) in
1078                     Some e, (newmeta+1)
1079                 | _ -> None, newmeta
1080               )
1081           | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
1082               when UriManager.eq uri eq_uri ->
1083               let t1 = S.lift index t1
1084               and t2 = S.lift index t2 in
1085               let o = !Utils.compare_terms t1 t2 in
1086               let w = compute_equality_weight ty t1 t2 in
1087               let e = (w, BasicProof (C.Rel index), (ty, t1, t2, o), [], []) in
1088               Some e, (newmeta+1)
1089           | _ -> None, newmeta
1090         in (
1091           match do_find context term with
1092           | Some p, newmeta ->
1093               let tl, newmeta' = (aux (index+1) newmeta tl) in
1094               p::tl, max newmeta newmeta'
1095           | None, _ ->
1096               aux (index+1) newmeta tl
1097         )
1098     | _::tl ->
1099         aux (index+1) newmeta tl
1100   in
1101   aux 1 newmeta context
1102 ;;
1103
1104
1105 let equations_blacklist =
1106   List.fold_left
1107     (fun s u -> UriManager.UriSet.add (UriManager.uri_of_string u) s)
1108     UriManager.UriSet.empty [
1109       "cic:/Coq/Init/Logic/eq.ind#xpointer(1/1/1)";
1110       "cic:/Coq/Init/Logic/trans_eq.con";
1111       "cic:/Coq/Init/Logic/f_equal.con";
1112       "cic:/Coq/Init/Logic/f_equal2.con";
1113       "cic:/Coq/Init/Logic/f_equal3.con";
1114       "cic:/Coq/Init/Logic/sym_eq.con";
1115 (*       "cic:/Coq/Logic/Eqdep/UIP_refl.con"; *)
1116 (*       "cic:/Coq/Init/Peano/mult_n_Sm.con"; *)
1117     ]
1118 ;;
1119
1120 let find_library_equalities ~(dbd:Mysql.dbd) context status maxmeta = 
1121   let module C = Cic in
1122   let module S = CicSubstitution in
1123   let module T = CicTypeChecker in
1124   let candidates =
1125     List.fold_left
1126       (fun l uri ->
1127          if UriManager.UriSet.mem uri equations_blacklist then
1128            l
1129          else
1130            let t = CicUtil.term_of_uri uri in
1131            let ty, _ =
1132              CicTypeChecker.type_of_aux' [] context t CicUniv.empty_ugraph
1133            in
1134            (t, ty)::l)
1135       []
1136       (MetadataQuery.equations_for_goal ~dbd status)
1137   in
1138   let eq_uri1 = UriManager.uri_of_string HelmLibraryObjects.Logic.eq_XURI
1139   and eq_uri2 = HelmLibraryObjects.Logic.eq_URI in
1140   let iseq uri =
1141     (UriManager.eq uri eq_uri1) || (UriManager.eq uri eq_uri2)
1142   in
1143   let ok_types ty menv =
1144     List.for_all (fun (_, _, mt) -> mt = ty) menv
1145   in
1146   let rec aux newmeta = function
1147     | [] -> [], newmeta
1148     | (term, termty)::tl ->
1149         debug_print (
1150           Printf.sprintf "Examining: %s (%s)"
1151             (CicPp.ppterm term) (CicPp.ppterm termty));
1152         let res, newmeta = 
1153           match termty with
1154           | C.Prod (name, s, t) ->
1155               let head, newmetas, args, newmeta =
1156                 ProofEngineHelpers.saturate_term newmeta [] context termty
1157               in
1158               let p =
1159                 if List.length args = 0 then
1160                   term
1161                 else
1162                   C.Appl (term::args)
1163               in (
1164                 match head with
1165                 | C.Appl [C.MutInd (uri, _, _); ty; t1; t2]
1166                     when (iseq uri) && (ok_types ty newmetas) ->
1167                     debug_print (
1168                       Printf.sprintf "OK: %s" (CicPp.ppterm term));
1169                     let o = !Utils.compare_terms t1 t2 in
1170                     let w = compute_equality_weight ty t1 t2 in
1171                     let proof = BasicProof p in
1172                     let e = (w, proof, (ty, t1, t2, o), newmetas, args) in
1173                     Some e, (newmeta+1)
1174                 | _ -> None, newmeta
1175               )
1176           | C.Appl [C.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1177               let o = !Utils.compare_terms t1 t2 in
1178               let w = compute_equality_weight ty t1 t2 in
1179               let e = (w, BasicProof term, (ty, t1, t2, o), [], []) in
1180               Some e, (newmeta+1)
1181           | _ -> None, newmeta
1182         in
1183         match res with
1184         | Some e ->
1185             let tl, newmeta' = aux newmeta tl in
1186             e::tl, max newmeta newmeta'
1187         | None ->
1188             aux newmeta tl
1189   in
1190   let found, maxm = aux maxmeta candidates in
1191   (List.fold_left
1192      (fun l e ->
1193         if List.exists (meta_convertibility_eq e) l then (
1194           debug_print (
1195             Printf.sprintf "NO!! %s already there!" (string_of_equality e));
1196           l
1197         )
1198         else e::l)
1199      [] found), maxm
1200 ;;
1201
1202
1203 let fix_metas newmeta ((w, p, (ty, left, right, o), menv, args) as equality) =
1204 (*   print_endline ("fix_metas " ^ (string_of_int newmeta)); *)
1205   let table = Hashtbl.create (List.length args) in
1206   let is_this_case = ref false in
1207   let newargs, newmeta =
1208     List.fold_right
1209       (fun t (newargs, index) ->
1210          match t with
1211          | Cic.Meta (i, l) ->
1212              Hashtbl.add table i index;
1213 (*              if index = 5469 then ( *)
1214 (*                Printf.printf "?5469 COMES FROM (%d): %s\n" *)
1215 (*                  i (string_of_equality equality); *)
1216 (*                print_newline (); *)
1217 (*                is_this_case := true *)
1218 (*              ); *)
1219              ((Cic.Meta (index, l))::newargs, index+1)
1220          | _ -> assert false)
1221       args ([], newmeta+1)
1222   in
1223   let repl where =
1224     ProofEngineReduction.replace ~equality:(=) ~what:args ~with_what:newargs
1225       ~where
1226   in
1227   let menv' =
1228     List.fold_right
1229       (fun (i, context, term) menv ->
1230          try
1231            let index = Hashtbl.find table i in
1232            (index, context, term)::menv
1233          with Not_found ->
1234            (i, context, term)::menv)
1235       menv []
1236   in
1237   let ty = repl ty
1238   and left = repl left
1239   and right = repl right in
1240   let metas = (metas_of_term left) @ (metas_of_term right) in
1241   let menv' = List.filter (fun (i, _, _) -> List.mem i metas) menv'
1242   and newargs =
1243     List.filter
1244       (function Cic.Meta (i, _) -> List.mem i metas | _ -> assert false) newargs
1245   in
1246   let rec fix_proof = function
1247     | NoProof -> NoProof
1248     | BasicProof term -> BasicProof (repl term)
1249     | ProofBlock (subst, eq_URI, t', (pos, eq), p) ->
1250
1251 (*         Printf.printf "fix_proof of equality %s, subst is:\n%s\n" *)
1252 (*           (string_of_equality equality) (print_subst subst); *)
1253         
1254         let subst' =
1255           List.fold_left
1256             (fun s arg ->
1257                match arg with
1258                | Cic.Meta (i, l) -> (
1259                    try
1260                      let j = Hashtbl.find table i in
1261                      if List.mem_assoc i subst then
1262                        s
1263                      else
1264 (*                        let _, context, ty = CicUtil.lookup_meta j menv' in *)
1265 (*                        (i, (context, Cic.Meta (j, l), ty))::s *)
1266                        let _, context, ty = CicUtil.lookup_meta i menv in
1267                        (i, (context, Cic.Meta (j, l), ty))::s
1268                    with Not_found -> s
1269                  )
1270                | _ -> assert false)
1271             [] args
1272         in
1273 (*         let subst'' = *)
1274 (*           List.map *)
1275 (*             (fun (i, e) -> *)
1276 (*                try let j = Hashtbl.find table i in (j, e) *)
1277 (*                with _ -> (i, e)) subst *)
1278 (*         in *)
1279
1280 (*         Printf.printf "subst' is:\n%s\n" (print_subst subst'); *)
1281 (*         print_newline (); *)
1282         
1283         ProofBlock (subst' @ subst, eq_URI, t', (pos, eq), p)
1284 (*     | ProofSymBlock (ens, p) -> *)
1285 (*         let ens' = List.map (fun (u, t) -> (u, repl t)) ens in *)
1286 (*         ProofSymBlock (ens', fix_proof p) *)
1287     | p -> assert false
1288   in
1289 (*   (newmeta + (List.length newargs) + 2, *)
1290   let neweq = (w, fix_proof p, (ty, left, right, o), menv', newargs) in
1291 (*   if !is_this_case then ( *)
1292 (*     print_endline "\nTHIS IS THE TROUBLE!!!"; *)
1293 (*     let pt = build_proof_term neweq in *)
1294 (*     Printf.printf "equality: %s\nproof: %s\n" *)
1295 (*       (string_of_equality neweq) (CicPp.ppterm pt); *)
1296 (*     print_endline (String.make 79 '-'); *)
1297 (*   ); *)
1298   (newmeta + 1, neweq)
1299 (*    (w, fix_proof p, (ty, left, right, o), menv', newargs)) *)
1300 ;;
1301
1302
1303 let term_is_equality ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) term =
1304   let iseq uri = UriManager.eq uri eq_uri in
1305   match term with
1306   | Cic.Appl [Cic.MutInd (uri, _, _); _; _; _] when iseq uri -> true
1307   | _ -> false
1308 ;;
1309
1310
1311 exception TermIsNotAnEquality;;
1312
1313 let equality_of_term ?(eq_uri=HelmLibraryObjects.Logic.eq_URI) proof term =
1314   let iseq uri = UriManager.eq uri eq_uri in
1315   match term with
1316   | Cic.Appl [Cic.MutInd (uri, _, _); ty; t1; t2] when iseq uri ->
1317       let o = !Utils.compare_terms t1 t2 in
1318       let w = compute_equality_weight ty t1 t2 in
1319       let e = (w, BasicProof proof, (ty, t1, t2, o), [], []) in
1320       e
1321 (*       (proof, (ty, t1, t2, o), [], []) *)
1322   | _ ->
1323       raise TermIsNotAnEquality
1324 ;;
1325
1326
1327 type environment = Cic.metasenv * Cic.context * CicUniv.universe_graph;;
1328
1329
1330 (*
1331 let superposition_left (metasenv, context, ugraph) target source =
1332   let module C = Cic in
1333   let module S = CicSubstitution in
1334   let module M = CicMetaSubst in
1335   let module HL = HelmLibraryObjects in
1336   let module CR = CicReduction in
1337   (* we assume that target is ground (does not contain metavariables): this
1338    * should always be the case (I hope, at least) *)
1339   let proof, (eq_ty, left, right, t_order), _, _ = target in
1340   let eqproof, (ty, t1, t2, s_order), newmetas, args = source in
1341
1342   let compare_terms = !Utils.compare_terms in
1343
1344   if eq_ty <> ty then
1345     []
1346   else    
1347     let where, is_left =
1348       match t_order (* compare_terms left right *) with
1349       | Lt -> right, false
1350       | Gt -> left, true
1351       | _ -> (
1352           Printf.printf "????????? %s = %s" (CicPp.ppterm left)
1353             (CicPp.ppterm right);
1354           print_newline ();
1355           assert false (* again, for ground terms this shouldn't happen... *)
1356         )
1357     in
1358     let metasenv' = newmetas @ metasenv in
1359     let result = s_order (* compare_terms t1 t2 *) in
1360     let res1, res2 = 
1361       match result with
1362       | Gt -> (beta_expand t1 ty where context metasenv' ugraph), []
1363       | Lt -> [], (beta_expand t2 ty where context metasenv' ugraph)
1364       | _ ->
1365           let res1 =
1366             List.filter
1367               (fun (t, s, m, ug) ->
1368                  compare_terms (M.apply_subst s t1) (M.apply_subst s t2) = Gt)
1369               (beta_expand t1 ty where context metasenv' ugraph)
1370           and res2 =
1371             List.filter
1372               (fun (t, s, m, ug) ->
1373                  compare_terms (M.apply_subst s t2) (M.apply_subst s t1) = Gt)
1374               (beta_expand t2 ty where context metasenv' ugraph)
1375           in
1376           res1, res2
1377     in
1378     (*   let what, other = *)
1379     (*     if is_left then left, right *)
1380     (*     else right, left *)
1381     (*   in *)
1382     let build_new what other eq_URI (t, s, m, ug) =
1383       let newgoal, newgoalproof =
1384         match t with
1385         | C.Lambda (nn, ty, bo) ->
1386             let bo' = S.subst (M.apply_subst s other) bo in
1387             let bo'' =
1388               C.Appl (
1389                 [C.MutInd (HL.Logic.eq_URI, 0, []);
1390                  S.lift 1 eq_ty] @
1391                   if is_left then [bo'; S.lift 1 right]
1392                   else [S.lift 1 left; bo'])
1393             in
1394             let t' = C.Lambda (nn, ty, bo'') in
1395             S.subst (M.apply_subst s other) bo,
1396             M.apply_subst s
1397               (C.Appl [C.Const (eq_URI, []); ty; what; t';
1398                        proof; other; eqproof])
1399         | _ -> assert false
1400       in
1401       let equation =
1402         if is_left then (eq_ty, newgoal, right, compare_terms newgoal right)
1403         else (eq_ty, left, newgoal, compare_terms left newgoal)
1404       in
1405       (newgoalproof (* eqproof *), equation, [], [])
1406     in
1407     let new1 = List.map (build_new t1 t2 HL.Logic.eq_ind_URI) res1
1408     and new2 = List.map (build_new t2 t1 HL.Logic.eq_ind_r_URI) res2 in
1409     new1 @ new2
1410 ;;
1411
1412
1413 let superposition_right newmeta (metasenv, context, ugraph) target source =
1414   let module C = Cic in
1415   let module S = CicSubstitution in
1416   let module M = CicMetaSubst in
1417   let module HL = HelmLibraryObjects in
1418   let module CR = CicReduction in
1419   let eqproof, (eq_ty, left, right, t_order), newmetas, args = target in
1420   let eqp', (ty', t1, t2, s_order), newm', args' = source in
1421   let maxmeta = ref newmeta in
1422
1423   let compare_terms = !Utils.compare_terms in
1424
1425   if eq_ty <> ty' then
1426     newmeta, []
1427   else
1428     (*   let ok term subst other other_eq_side ugraph = *)
1429     (*     match term with *)
1430     (*     | C.Lambda (nn, ty, bo) -> *)
1431     (*         let bo' = S.subst (M.apply_subst subst other) bo in *)
1432     (*         let res, _ = CR.are_convertible context bo' other_eq_side ugraph in *)
1433     (*         not res *)
1434     (*     |  _ -> assert false *)
1435     (*   in *)
1436     let condition left right what other (t, s, m, ug) =
1437       let subst = M.apply_subst s in
1438       let cmp1 = compare_terms (subst what) (subst other) in
1439       let cmp2 = compare_terms (subst left) (subst right) in
1440       (*     cmp1 = Gt && cmp2 = Gt *)
1441       cmp1 <> Lt && cmp1 <> Le && cmp2 <> Lt && cmp2 <> Le
1442         (*     && (ok t s other right ug) *)
1443     in
1444     let metasenv' = metasenv @ newmetas @ newm' in
1445     let beta_expand = beta_expand ~metas_ok:false in
1446     let cmp1 = t_order (* compare_terms left right *)
1447     and cmp2 = s_order (* compare_terms t1 t2 *) in
1448     let res1, res2, res3, res4 =
1449       let res l r s t =
1450         List.filter
1451           (condition l r s t)
1452           (beta_expand s eq_ty l context metasenv' ugraph)
1453       in
1454       match cmp1, cmp2 with
1455       | Gt, Gt ->
1456           (beta_expand t1 eq_ty left context metasenv' ugraph), [], [], []
1457       | Gt, Lt ->
1458           [], (beta_expand t2 eq_ty left context metasenv' ugraph), [], []
1459       | Lt, Gt ->
1460           [], [], (beta_expand t1 eq_ty right context metasenv' ugraph), []
1461       | Lt, Lt ->
1462           [], [], [], (beta_expand t2 eq_ty right context metasenv' ugraph)
1463       | Gt, _ ->
1464           let res1 = res left right t1 t2
1465           and res2 = res left right t2 t1 in
1466           res1, res2, [], []
1467       | Lt, _ ->
1468           let res3 = res right left t1 t2
1469           and res4 = res right left t2 t1 in
1470           [], [], res3, res4
1471       | _, Gt ->
1472           let res1 = res left right t1 t2
1473           and res3 = res right left t1 t2 in
1474           res1, [], res3, []
1475       | _, Lt ->
1476           let res2 = res left right t2 t1
1477           and res4 = res right left t2 t1 in
1478           [], res2, [], res4
1479       | _, _ ->
1480           let res1 = res left right t1 t2
1481           and res2 = res left right t2 t1
1482           and res3 = res right left t1 t2
1483           and res4 = res right left t2 t1 in
1484           res1, res2, res3, res4
1485     in
1486     let newmetas = newmetas @ newm' in
1487     let newargs = args @ args' in
1488     let build_new what other is_left eq_URI (t, s, m, ug) =
1489       (*     let what, other = *)
1490       (*       if is_left then left, right *)
1491       (*       else right, left *)
1492       (*     in *)
1493       let newterm, neweqproof =
1494         match t with
1495         | C.Lambda (nn, ty, bo) ->
1496             let bo' = M.apply_subst s (S.subst other bo) in
1497             let bo'' =
1498               C.Appl (
1499                 [C.MutInd (HL.Logic.eq_URI, 0, []); S.lift 1 eq_ty] @
1500                   if is_left then [bo'; S.lift 1 right]
1501                   else [S.lift 1 left; bo'])
1502             in
1503             let t' = C.Lambda (nn, ty, bo'') in
1504             bo',
1505             M.apply_subst s
1506               (C.Appl [C.Const (eq_URI, []); ty; what; t';
1507                        eqproof; other; eqp'])
1508         | _ -> assert false
1509       in
1510       let newmeta, newequality =
1511         let left, right =
1512           if is_left then (newterm, M.apply_subst s right)
1513           else (M.apply_subst s left, newterm) in
1514         let neworder = compare_terms left right in
1515         fix_metas !maxmeta
1516           (neweqproof, (eq_ty, left, right, neworder), newmetas, newargs)
1517       in
1518       maxmeta := newmeta;
1519       newequality
1520     in
1521     let new1 = List.map (build_new t1 t2 true HL.Logic.eq_ind_URI) res1
1522     and new2 = List.map (build_new t2 t1 true HL.Logic.eq_ind_r_URI) res2
1523     and new3 = List.map (build_new t1 t2 false HL.Logic.eq_ind_URI) res3
1524     and new4 = List.map (build_new t2 t1 false HL.Logic.eq_ind_r_URI) res4 in
1525     let ok = function
1526       | _, (_, left, right, _), _, _ ->
1527           not (fst (CR.are_convertible context left right ugraph))
1528     in
1529     (!maxmeta,
1530      (List.filter ok (new1 @ new2 @ new3 @ new4)))
1531 ;;
1532 *)
1533
1534
1535 let is_identity ((_, context, ugraph) as env) = function
1536   | ((_, _, (ty, left, right, _), _, _) as equality) ->
1537       (left = right ||
1538           (fst (CicReduction.are_convertible context left right ugraph)))
1539 ;;
1540
1541
1542 (*
1543 let demodulation newmeta (metasenv, context, ugraph) target source =
1544   let module C = Cic in
1545   let module S = CicSubstitution in
1546   let module M = CicMetaSubst in
1547   let module HL = HelmLibraryObjects in
1548   let module CR = CicReduction in
1549
1550   let proof, (eq_ty, left, right, t_order), metas, args = target
1551   and proof', (ty, t1, t2, s_order), metas', args' = source in
1552
1553   let compare_terms = !Utils.compare_terms in
1554   
1555   if eq_ty <> ty then
1556     newmeta, target
1557   else
1558     let first_step, get_params = 
1559       match s_order (* compare_terms t1 t2 *) with
1560       | Gt -> 1, (function
1561                     | 1 -> true, t1, t2, HL.Logic.eq_ind_URI
1562                     | 0 -> false, t1, t2, HL.Logic.eq_ind_URI
1563                     | _ -> assert false)
1564       | Lt -> 1, (function
1565                     | 1 -> true, t2, t1, HL.Logic.eq_ind_r_URI
1566                     | 0 -> false, t2, t1, HL.Logic.eq_ind_r_URI
1567                     | _ -> assert false)
1568       | _ ->
1569           let first_step = 3 in
1570           let get_params step =
1571             match step with
1572             | 3 -> true, t1, t2, HL.Logic.eq_ind_URI
1573             | 2 -> false, t1, t2, HL.Logic.eq_ind_URI
1574             | 1 -> true, t2, t1, HL.Logic.eq_ind_r_URI
1575             | 0 -> false, t2, t1, HL.Logic.eq_ind_r_URI
1576             | _ -> assert false
1577           in
1578           first_step, get_params
1579     in
1580     let rec demodulate newmeta step metasenv target =
1581       let proof, (eq_ty, left, right, t_order), metas, args = target in
1582       let is_left, what, other, eq_URI = get_params step in
1583
1584       let env = metasenv, context, ugraph in
1585       let names = names_of_context context in
1586 (*       Printf.printf *)
1587 (*         "demodulate\ntarget: %s\nwhat: %s\nother: %s\nis_left: %s\n" *)
1588 (*         (string_of_equality ~env target) (CicPp.pp what names) *)
1589 (*         (CicPp.pp other names) (string_of_bool is_left); *)
1590 (*       Printf.printf "step: %d" step; *)
1591 (*       print_newline (); *)
1592
1593       let ok (t, s, m, ug) =
1594         compare_terms (M.apply_subst s what) (M.apply_subst s other) = Gt
1595       in
1596       let res =
1597         let r = (beta_expand ~metas_ok:false ~match_only:true
1598                    what ty (if is_left then left else right)
1599                    context (metasenv @ metas) ugraph) 
1600         in
1601 (*         let m' = metas_of_term what *)
1602 (*         and m'' = metas_of_term (if is_left then left else right) in *)
1603 (*         if (List.mem 527 m'') && (List.mem 6 m') then ( *)
1604 (*           Printf.printf *)
1605 (*             "demodulate\ntarget: %s\nwhat: %s\nother: %s\nis_left: %s\n" *)
1606 (*             (string_of_equality ~env target) (CicPp.pp what names) *)
1607 (*             (CicPp.pp other names) (string_of_bool is_left); *)
1608 (*           Printf.printf "step: %d" step; *)
1609 (*           print_newline (); *)
1610 (*           print_endline "res:"; *)
1611 (*           List.iter (fun (t, s, m, ug) -> print_endline (CicPp.pp t names)) r; *)
1612 (*           print_newline (); *)
1613 (*           Printf.printf "metasenv:\n%s\n" (print_metasenv (metasenv @ metas)); *)
1614 (*           print_newline (); *)
1615 (*         ); *)
1616         List.filter ok r
1617       in
1618       match res with
1619       | [] ->
1620           if step = 0 then newmeta, target
1621           else demodulate newmeta (step-1) metasenv target
1622       | (t, s, m, ug)::_ -> 
1623           let newterm, newproof =
1624             match t with
1625             | C.Lambda (nn, ty, bo) ->
1626 (*                 let bo' = M.apply_subst s (S.subst other bo) in *)
1627                 let bo' = S.subst (M.apply_subst s other) bo in
1628                 let bo'' =
1629                   C.Appl (
1630                     [C.MutInd (HL.Logic.eq_URI, 0, []);
1631                      S.lift 1 eq_ty] @
1632                       if is_left then [bo'; S.lift 1 right]
1633                       else [S.lift 1 left; bo'])
1634                 in
1635                 let t' = C.Lambda (nn, ty, bo'') in
1636 (*                 M.apply_subst s (S.subst other bo), *)
1637                 bo', 
1638                 M.apply_subst s
1639                   (C.Appl [C.Const (eq_URI, []); ty; what; t';
1640                            proof; other; proof'])
1641             | _ -> assert false
1642           in
1643           let newmeta, newtarget =
1644             let left, right =
1645 (*               if is_left then (newterm, M.apply_subst s right) *)
1646 (*               else (M.apply_subst s left, newterm) in *)
1647               if is_left then newterm, right
1648               else left, newterm
1649             in
1650             let neworder = compare_terms left right in
1651 (*             let newmetasenv = metasenv @ metas in *)
1652 (*             let newargs = args @ args' in *)
1653 (*             fix_metas newmeta *)
1654 (*               (newproof, (eq_ty, left, right), newmetasenv, newargs) *)
1655             let m = (metas_of_term left) @ (metas_of_term right) in
1656             let newmetasenv = List.filter (fun (i, _, _) -> List.mem i m) metas
1657             and newargs =
1658               List.filter
1659                 (function C.Meta (i, _) -> List.mem i m | _ -> assert false)
1660                 args
1661             in
1662             newmeta,
1663             (newproof, (eq_ty, left, right, neworder), newmetasenv, newargs)
1664           in
1665 (*           Printf.printf *)
1666 (*             "demodulate, newtarget: %s\ntarget was: %s\n" *)
1667 (*             (string_of_equality ~env newtarget) *)
1668 (*             (string_of_equality ~env target); *)
1669 (* (\*           let _, _, newm, newa = newtarget in *\) *)
1670 (* (\*           Printf.printf "newmetasenv:\n%s\nnewargs:\n%s\n" *\) *)
1671 (* (\*             (print_metasenv newm) *\) *)
1672 (* (\*             (String.concat "\n" (List.map CicPp.ppterm newa)); *\) *)
1673 (*           print_newline (); *)
1674           if is_identity env newtarget then
1675             newmeta, newtarget
1676           else
1677             demodulate newmeta first_step metasenv newtarget
1678     in
1679     demodulate newmeta first_step (metasenv @ metas') target
1680 ;;
1681
1682
1683 (*
1684 let demodulation newmeta env target source =
1685   newmeta, target
1686 ;;
1687 *)
1688
1689
1690 let subsumption env target source =
1691   let _, (ty, tl, tr, _), tmetas, _ = target
1692   and _, (ty', sl, sr, _), smetas, _ = source in
1693   if ty <> ty' then
1694     false
1695   else
1696     let metasenv, context, ugraph = env in
1697     let metasenv = metasenv @ tmetas @ smetas in
1698     let names = names_of_context context in
1699     let samesubst subst subst' =
1700 (*       Printf.printf "samesubst:\nsubst: %s\nsubst': %s\n" *)
1701 (*         (print_subst subst) (print_subst subst'); *)
1702 (*       print_newline (); *)
1703       let tbl = Hashtbl.create (List.length subst) in
1704       List.iter (fun (m, (c, t1, t2)) -> Hashtbl.add tbl m (c, t1, t2)) subst;
1705       List.for_all
1706         (fun (m, (c, t1, t2)) ->
1707            try
1708              let c', t1', t2' = Hashtbl.find tbl m in
1709              if (c = c') && (t1 = t1') && (t2 = t2') then true
1710              else false
1711            with Not_found ->
1712              true)
1713         subst'
1714     in
1715     let subsaux left right left' right' =
1716       try
1717         let subst, menv, ug = matching metasenv context left left' ugraph
1718         and subst', menv', ug' = matching metasenv context right right' ugraph
1719         in
1720 (*         Printf.printf "left = right: %s = %s\n" *)
1721 (*           (CicPp.pp left names) (CicPp.pp right names); *)
1722 (*         Printf.printf "left' = right': %s = %s\n" *)
1723 (*           (CicPp.pp left' names) (CicPp.pp right' names);         *)
1724         samesubst subst subst'
1725       with e ->
1726 (*         print_endline (Printexc.to_string e); *)
1727         false
1728     in
1729     let res = 
1730       if subsaux tl tr sl sr then true
1731       else subsaux tl tr sr sl
1732     in
1733     if res then (
1734       Printf.printf "subsumption!:\ntarget: %s\nsource: %s\n"
1735         (string_of_equality ~env target) (string_of_equality ~env source);
1736       print_newline ();
1737     );
1738     res
1739 ;;
1740 *)
1741
1742
1743 let extract_differing_subterms t1 t2 =
1744   let module C = Cic in
1745   let rec aux t1 t2 =
1746     match t1, t2 with
1747     | C.Appl l1, C.Appl l2 when (List.length l1) <> (List.length l2) ->
1748         [(t1, t2)]
1749     | C.Appl (h1::tl1), C.Appl (h2::tl2) ->
1750         let res = List.concat (List.map2 aux tl1 tl2) in
1751         if h1 <> h2 then
1752           if res = [] then [(h1, h2)] else [(t1, t2)]
1753         else
1754           if List.length res > 1 then [(t1, t2)] else res
1755     | t1, t2 ->
1756         if t1 <> t2 then [(t1, t2)] else []
1757   in
1758   let res = aux t1 t2 in
1759   match res with
1760   | hd::[] -> Some hd
1761   | _ -> None
1762 ;;
1763
1764