]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/paramodulation/utils.ml
Changed the type of compute-equality_weight that now takes also
[helm.git] / components / tactics / paramodulation / utils.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 let time = true;;
29 let debug = false;;
30 let debug_metas = false;; 
31 let debug_res = false;;
32
33 let debug_print s = if debug then prerr_endline (Lazy.force s);;
34
35 let print_metasenv metasenv =
36   String.concat "\n--------------------------\n"
37     (List.map (fun (i, context, term) ->
38                  (string_of_int i) ^ " [\n" ^ (CicPp.ppcontext context) ^
39                    "\n] " ^  (CicPp.ppterm term))
40        metasenv)
41 ;;
42
43
44
45
46 let print_subst ?(prefix="\n") subst =
47     String.concat prefix
48      (List.map
49        (fun (i, (c, t, ty)) ->
50           Printf.sprintf "?%d -> %s : %s" i
51             (CicPp.ppterm t) (CicPp.ppterm ty))
52        subst)
53 ;;  
54
55 type comparison = Lt | Le | Eq | Ge | Gt | Incomparable;;
56     
57 let string_of_comparison = function
58   | Lt -> "<"
59   | Le -> "<="
60   | Gt -> ">"
61   | Ge -> ">="
62   | Eq -> "="
63   | Incomparable -> "I"
64
65 module OrderedTerm =
66 struct
67   type t = Cic.term
68       
69   let compare = Pervasives.compare
70 end
71
72 module TermSet = Set.Make(OrderedTerm);;
73 module TermMap = Map.Make(OrderedTerm);;
74
75 let symbols_of_term term =
76   let module C = Cic in
77   let rec aux map = function
78     | C.Meta _ -> map
79     | C.Appl l ->
80         List.fold_left (fun res t -> (aux res t)) map l
81     | t ->
82         let map = 
83           try
84             let c = TermMap.find t map in
85             TermMap.add t (c+1) map
86           with Not_found ->
87             TermMap.add t 1 map
88         in
89         map
90   in
91   aux TermMap.empty term
92 ;;
93
94
95 let metas_of_term term =
96   let module C = Cic in
97   let rec aux = function
98     | C.Meta _ as t -> TermSet.singleton t
99     | C.Appl l ->
100         List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
101     | t -> TermSet.empty (* TODO: maybe add other cases? *)
102   in
103   aux term
104 ;;
105
106
107 (************************* rpo ********************************)
108 let number = [
109   UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1)",3;
110   UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1/1)",6;
111   UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1/2)",9;
112   HelmLibraryObjects.Peano.pred_URI, 12;
113   HelmLibraryObjects.Peano.plus_URI, 15;
114   HelmLibraryObjects.Peano.minus_URI, 18;
115   HelmLibraryObjects.Peano.mult_URI, 21;
116   UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind#xpointer(1/1)",103;
117   UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind#xpointer(1/1/1)",106;
118   UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind#xpointer(1/1/2)",109;
119   UriManager.uri_of_string "cic:/matita/nat/nat/pred.con",112;
120   UriManager.uri_of_string "cic:/matita/nat/plus/plus.con",115;
121   UriManager.uri_of_string "cic:/matita/nat/minus/minus.con",118;
122   UriManager.uri_of_string "cic:/matita/nat/times/times.con",121;
123   ]
124 ;;
125
126 let atomic t =
127   match t with
128       Cic.Const _ 
129     | Cic.MutInd _ 
130     | Cic.MutConstruct _ 
131     | Cic.Rel _ -> true
132     | _ -> false
133
134 let sig_order_const t1 t2 =
135   try
136     let u1 = CicUtil.uri_of_term t1 in
137     let u2 = CicUtil.uri_of_term t2 in  
138     let n1 = List.assoc u1 number in
139     let n2 = List.assoc u2 number in
140     if n1 < n2 then Lt
141     else if n1 > n2 then Gt
142     else 
143       begin
144         prerr_endline ("t1 = "^(CicPp.ppterm t1));
145         prerr_endline ("t2 = "^(CicPp.ppterm t2)); 
146         assert false
147       end
148   with 
149       Invalid_argument _ 
150     | Not_found -> Incomparable
151
152 let sig_order t1 t2 =
153   match t1, t2 with
154       Cic.Rel n, Cic.Rel m when n < m -> Gt (* inverted order *)
155     | Cic.Rel n, Cic.Rel m when n = m -> Incomparable
156     | Cic.Rel n, Cic.Rel m when n > m -> Lt
157     | Cic.Rel _, _ -> Gt
158     | _, Cic.Rel _ -> Lt
159     | _,_ -> sig_order_const t1 t2
160
161 let rec rpo_lt t1 t2 =
162   let module C = Cic in 
163   let first_trie =
164     match t1,t2 with 
165         C.Meta (_, _), C.Meta (_,_) -> false
166       | C.Meta (_,_) , t2 -> TermSet.mem t1 (metas_of_term t2)
167       | t1, C.Meta (_,_) -> false
168       | C.Appl [h1;a1],C.Appl [h2;a2] when h1=h2 -> 
169           rpo_lt a1 a2
170       | C.Appl (h1::arg1),C.Appl (h2::arg2) when h1=h2 ->
171           if lex_lt arg1 arg2 then
172             check_lt arg1 t2 
173           else false
174       | C.Appl (h1::arg1),C.Appl (h2::arg2) -> 
175           (match sig_order h1 h2 with
176              | Lt -> check_lt arg1 t2
177              | _ -> false)
178       | C.Appl (h1::arg1), t2 when atomic t2 ->
179           (match sig_order h1 t2 with
180              | Lt -> check_lt arg1 t2
181              | _ -> false)
182       | t1 , C.Appl (h2::arg2) when atomic t1 ->
183           (match sig_order t1 h2 with
184              | Lt -> true
185              | _ -> false )
186       | C.Appl [] , _ -> assert false 
187       | _ , C.Appl [] -> assert false
188       | t1, t2 when (atomic t1 && atomic t2 && t1<>t2) ->
189           (match sig_order t1 t2 with
190              | Lt -> true
191              | _ -> false)
192       | _,_ -> false
193   in
194   if first_trie then true else
195   match t2 with
196       C.Appl (_::args) ->
197         List.exists (fun a -> t1 = a || rpo_lt t1 a) args
198     | _ -> false
199  
200 and lex_lt l1 l2 = 
201   match l1,l2 with
202       [],[] -> false
203     | [],_ -> assert false
204     | _, [] -> assert false
205     | a1::l1, a2::l2 when a1 = a2 -> lex_lt l1 l2
206     | a1::_, a2::_ -> rpo_lt a1 a2
207  
208 and check_lt l t =
209   List.fold_left 
210     (fun b a -> b && (rpo_lt a t))
211     true l
212 ;;
213
214 let rpo t1 t2 =
215   if rpo_lt t2 t1 then Gt
216   else if rpo_lt t1 t2 then Lt
217   else Incomparable
218
219
220 (*********************** fine rpo *****************************)
221
222 (* (weight of constants, [(meta, weight_of_meta)]) *)
223 type weight = int * (int * int) list;;
224
225 let string_of_weight (cw, mw) =
226   let s =
227     String.concat ", "
228       (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
229   in
230   Printf.sprintf "[%d; %s]" cw s
231
232
233 let weight_of_term ?(consider_metas=true) ?(count_metas_occurrences=false) term =
234   let module C = Cic in
235   let vars_dict = Hashtbl.create 5 in
236   let rec aux = function
237     | C.Meta (metano, _) when consider_metas ->
238         (try
239            let oldw = Hashtbl.find vars_dict metano in
240            Hashtbl.replace vars_dict metano (oldw+1)
241          with Not_found ->
242            Hashtbl.add vars_dict metano 1);
243         if count_metas_occurrences then 1 else 0
244     | C.Meta _ ->  (* "variables" are lighter than constants and functions...*)
245         if count_metas_occurrences then 1 else 0         
246     | C.Var (_, ens)
247     | C.Const (_, ens)
248     | C.MutInd (_, _, ens)
249     | C.MutConstruct (_, _, _, ens) ->
250         List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
251          
252     | C.Cast (t1, t2)
253     | C.Lambda (_, t1, t2)
254     | C.Prod (_, t1, t2)
255     | C.LetIn (_, t1, t2) ->
256         let w1 = aux t1 in
257         let w2 = aux t2 in
258         w1 + w2 + 1
259           
260     | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
261         
262     | C.MutCase (_, _, outt, t, pl) ->
263         let w1 = aux outt in
264         let w2 = aux t in
265         let w3 = List.fold_left (+) 0 (List.map aux pl) in
266         w1 + w2 + w3 + 1
267           
268     | C.Fix (_, fl) ->
269         List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
270           
271     | C.CoFix (_, fl) ->
272         List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
273           
274     | _ -> 1
275   in
276   let w = aux term in
277   let l =
278     Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
279   let compare w1 w2 = 
280     match w1, w2 with
281     | (m1, _), (m2, _) -> m2 - m1 
282   in 
283   (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
284 ;;
285
286
287 module OrderedInt = struct
288   type t = int
289
290   let compare = Pervasives.compare
291 end
292
293 module IntSet = Set.Make(OrderedInt)
294
295 let compute_equality_weight (ty,left,right,o) =
296   let metasw = ref 0 in
297   let weight_of t =
298     let w, m = (weight_of_term 
299                   ~consider_metas:true ~count_metas_occurrences:false t) in
300     metasw := !metasw + (1 * (List.length m)) ;
301     w
302   in
303   (* Warning: the following let cannot be expanded since it forces the
304      right evaluation order!!!! *)
305   let w = (weight_of ty) + (weight_of left) + (weight_of right) in 
306   (* let w = weight_of (Cic.Appl [ty;left;right]) in *)
307   w + !metasw
308 ;;
309
310
311 (* returns a "normalized" version of the polynomial weight wl (with type
312  * weight list), i.e. a list sorted ascending by meta number,
313  * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
314  * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
315  *      (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
316 let normalize_weight maxmeta (cw, wl) =
317   let rec aux = function
318     | 0 -> []
319     | m -> (m, 0)::(aux (m-1))
320   in
321   let tmpl = aux maxmeta in
322   let wl =
323     List.sort
324       (fun (m, _) (n, _) -> Pervasives.compare m n)
325       (List.fold_left
326          (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
327   in
328   (cw, wl)
329 ;;
330
331
332 let normalize_weights (cw1, wl1) (cw2, wl2) =
333   let rec aux wl1 wl2 =
334     match wl1, wl2 with
335     | [], [] -> [], []
336     | (m, w)::tl1, (n, w')::tl2 when m = n ->
337         let res1, res2 = aux tl1 tl2 in
338         (m, w)::res1, (n, w')::res2
339     | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
340         let res1, res2 = aux tl1 wl2 in
341         (m, w)::res1, (m, 0)::res2
342     | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
343         let res1, res2 = aux wl1 tl2 in
344         (n, 0)::res1, (n, w')::res2
345     | [], (n, w)::tl2 ->
346         let res1, res2 = aux [] tl2 in
347         (n, 0)::res1, (n, w)::res2
348     | (m, w)::tl1, [] ->
349         let res1, res2 = aux tl1 [] in
350         (m, w)::res1, (m, 0)::res2
351     | _, _ -> assert false
352   in
353   let cmp (m, _) (n, _) = compare m n in
354   let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
355   (cw1, wl1), (cw2, wl2)
356 ;;
357
358         
359 let compare_weights ?(normalize=false)
360     ((h1, w1) as weight1) ((h2, w2) as weight2)=
361   let (h1, w1), (h2, w2) =
362     if normalize then
363       normalize_weights weight1 weight2
364     else
365       (h1, w1), (h2, w2)
366   in
367   let res, diffs =
368     try
369       List.fold_left2
370         (fun ((lt, eq, gt), diffs) w1 w2 ->
371            match w1, w2 with
372            | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
373                let diffs = (w1 - w2) + diffs in 
374                let r = compare w1 w2 in
375                if r < 0 then (lt+1, eq, gt), diffs
376                else if r = 0 then (lt, eq+1, gt), diffs
377                else (lt, eq, gt+1), diffs
378            | (meta1, w1), (meta2, w2) ->
379                debug_print
380                  (lazy
381                     (Printf.sprintf "HMMM!!!! %s, %s\n"
382                        (string_of_weight weight1) (string_of_weight weight2)));
383                assert false)
384         ((0, 0, 0), 0) w1 w2
385     with Invalid_argument _ ->
386       debug_print
387         (lazy
388            (Printf.sprintf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
389               (string_of_weight (h1, w1)) (string_of_weight weight1)
390               (string_of_weight (h2, w2)) (string_of_weight weight2)
391               (string_of_bool normalize)));
392       assert false
393   in
394   let hdiff = h1 - h2 in 
395   match res with
396   | (0, _, 0) ->
397       if hdiff < 0 then Lt
398       else if hdiff > 0 then Gt
399       else Eq (* Incomparable *)
400   | (m, _, 0) ->
401       if hdiff <= 0 then Lt
402       else if (- diffs) >= hdiff then Le else Incomparable
403   | (0, _, m) ->
404       if hdiff >= 0 then Gt
405       else if diffs >= (- hdiff) then Ge else Incomparable
406   | (m, _, n) when m > 0 && n > 0 ->
407       Incomparable
408   | _ -> assert false 
409
410 ;;
411
412
413 let rec aux_ordering ?(recursion=true) t1 t2 =
414   let module C = Cic in
415   let compare_uris u1 u2 =
416     let res =
417       compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
418     if res < 0 then Lt
419     else if res = 0 then Eq
420     else Gt
421   in
422   match t1, t2 with
423   | C.Meta _, _
424   | _, C.Meta _ -> Incomparable
425
426   | t1, t2 when t1 = t2 -> Eq
427
428   | C.Rel n, C.Rel m -> if n > m then Lt else Gt
429   | C.Rel _, _ -> Lt
430   | _, C.Rel _ -> Gt
431
432   | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
433   | C.Const _, _ -> Lt
434   | _, C.Const _ -> Gt
435
436   | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
437   | C.MutInd _, _ -> Lt
438   | _, C.MutInd _ -> Gt
439
440   | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
441       compare_uris u1 u2
442   | C.MutConstruct _, _ -> Lt
443   | _, C.MutConstruct _ -> Gt
444
445   | C.Appl l1, C.Appl l2 when recursion ->
446       let rec cmp t1 t2 =
447         match t1, t2 with
448         | [], [] -> Eq
449         | _, [] -> Gt
450         | [], _ -> Lt
451         | hd1::tl1, hd2::tl2 ->
452             let o = aux_ordering hd1 hd2 in
453             if o = Eq then cmp tl1 tl2
454             else o
455       in
456       cmp l1 l2
457   | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
458       aux_ordering h1 h2
459         
460   | t1, t2 ->
461       debug_print
462         (lazy
463            (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
464               (CicPp.ppterm t1) (CicPp.ppterm t2)));
465       Incomparable
466 ;;
467
468
469 (* w1, w2 are the weights, they should already be normalized... *)
470 let nonrec_kbo_w (t1, w1) (t2, w2) =
471   match compare_weights w1 w2 with
472   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
473   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
474   | Eq -> aux_ordering t1 t2
475   | res -> res
476 ;;
477
478     
479 let nonrec_kbo t1 t2 =
480   let w1 = weight_of_term t1 in
481   let w2 = weight_of_term t2 in
482   (* 
483   prerr_endline ("weight1 :"^(string_of_weight w1));
484   prerr_endline ("weight2 :"^(string_of_weight w2)); 
485   *)
486   match compare_weights ~normalize:true w1 w2 with
487   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
488   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
489   | Eq -> aux_ordering t1 t2
490   | res -> res
491 ;;
492
493
494 let rec kbo t1 t2 =
495   let aux = aux_ordering ~recursion:false in
496   let w1 = weight_of_term t1
497   and w2 = weight_of_term t2 in
498   let rec cmp t1 t2 =
499     match t1, t2 with
500     | [], [] -> Eq
501     | _, [] -> Gt
502     | [], _ -> Lt
503     | hd1::tl1, hd2::tl2 ->
504         let o =
505           kbo hd1 hd2
506         in
507         if o = Eq then cmp tl1 tl2
508         else o
509   in
510   let comparison = compare_weights ~normalize:true w1 w2 in
511   match comparison with
512   | Le ->
513       let r = aux t1 t2 in
514       if r = Lt then Lt
515       else if r = Eq then (
516         match t1, t2 with
517         | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
518             if cmp tl1 tl2 = Lt then Lt else Incomparable
519         | _, _ ->  Incomparable
520       ) else Incomparable
521   | Ge ->
522       let r = aux t1 t2 in
523       if r = Gt then Gt
524       else if r = Eq then (
525         match t1, t2 with
526         | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
527             if cmp tl1 tl2 = Gt then Gt else Incomparable
528         | _, _ ->  Incomparable
529       ) else Incomparable
530   | Eq ->
531       let r = aux t1 t2 in
532       if r = Eq then (
533         match t1, t2 with
534         | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
535             cmp tl1 tl2
536         | _, _ ->  Incomparable
537       ) else r 
538   | res -> res
539 ;;
540           
541 let rec ao t1 t2 =
542   let get_hd t =
543     match t with
544         Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
545       | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) -> 
546           Some(uri,tyno,cno)
547       | _ -> None in
548   let aux = aux_ordering ~recursion:false in
549   let w1 = weight_of_term t1
550   and w2 = weight_of_term t2 in
551   let rec cmp t1 t2 =
552     match t1, t2 with
553     | [], [] -> Eq
554     | _, [] -> Gt
555     | [], _ -> Lt
556     | hd1::tl1, hd2::tl2 ->
557         let o =
558           ao hd1 hd2
559         in
560         if o = Eq then cmp tl1 tl2
561         else o
562   in
563   match get_hd t1, get_hd t2 with
564       Some(_),None -> Lt
565     | None,Some(_) -> Gt
566     | _ ->
567         let comparison = compare_weights ~normalize:true w1 w2 in
568           match comparison with
569             | Le ->
570                 let r = aux t1 t2 in
571                   if r = Lt then Lt
572                   else if r = Eq then (
573                     match t1, t2 with
574                       | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
575                           if cmp tl1 tl2 = Lt then Lt else Incomparable
576                       | _, _ ->  Incomparable
577                   ) else Incomparable
578             | Ge ->
579                 let r = aux t1 t2 in
580                   if r = Gt then Gt
581                   else if r = Eq then (
582                     match t1, t2 with
583                       | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
584                           if cmp tl1 tl2 = Gt then Gt else Incomparable
585                       | _, _ ->  Incomparable
586                   ) else Incomparable
587             | Eq ->
588                 let r = aux t1 t2 in
589                   if r = Eq then (
590                     match t1, t2 with
591                       | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
592                           cmp tl1 tl2
593                       | _, _ ->  Incomparable
594                   ) else r 
595             | res -> res
596 ;;
597
598 let names_of_context context = 
599   List.map
600     (function
601        | None -> None
602        | Some (n, e) -> Some n)
603     context
604 ;;
605
606
607 let rec lpo t1 t2 =
608   let module C = Cic in
609   match t1, t2 with
610   | t1, t2 when t1 = t2 -> Eq
611   | t1, (C.Meta _ as m) ->
612       if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
613   | (C.Meta _ as m), t2 ->
614       if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
615   | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
616       let res =
617         let f o r t =
618           if r then true else
619             match lpo t o with
620             | Gt | Eq -> true
621             | _ -> false
622         in
623         let res1 = List.fold_left (f t2) false tl1 in
624         if res1 then Gt
625         else let res2 = List.fold_left (f t1) false tl2 in
626         if res2 then Lt
627         else Incomparable
628       in
629       if res <> Incomparable then
630         res
631       else
632         let f o r t =
633           if not r then false else
634             match lpo o t with
635             | Gt -> true
636             | _ -> false
637         in
638         match aux_ordering hd1 hd2 with
639         | Gt ->
640             let res = List.fold_left (f t1) false tl2 in
641             if res then Gt
642             else Incomparable
643         | Lt ->
644             let res = List.fold_left (f t2) false tl1 in
645             if res then Lt
646             else Incomparable
647         | Eq -> (
648             let lex_res =
649               try
650                 List.fold_left2
651                   (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
652                   Eq tl1 tl2
653               with Invalid_argument _ ->
654                 Incomparable
655             in
656             match lex_res with
657             | Gt ->
658                 if List.fold_left (f t1) false tl2 then Gt
659                 else Incomparable
660             | Lt ->
661                 if List.fold_left (f t2) false tl1 then Lt
662                 else Incomparable
663             | _ -> Incomparable
664           )
665         | _ -> Incomparable
666     )
667   | t1, t2 -> aux_ordering t1 t2
668 ;;
669
670
671 (* settable by the user... *)
672 let compare_terms = ref nonrec_kbo;; 
673 (* let compare_terms = ref ao;; *)
674 (* let compare_terms = ref rpo;; *)
675
676 let guarded_simpl ?(debug=false) context t =
677   if !compare_terms == nonrec_kbo then t
678   else
679     let t' = ProofEngineReduction.simpl context t in
680     if t = t' then t else
681       begin
682         let simpl_order = !compare_terms t t' in
683         if debug then
684           prerr_endline ("comparing "^(CicPp.ppterm t)^(CicPp.ppterm t'));
685         if simpl_order = Gt then (if debug then prerr_endline "GT";t')
686         else (if debug then prerr_endline "NO_GT";t)
687       end
688 ;;
689
690 type equality_sign = Negative | Positive;;
691
692 let string_of_sign = function
693   | Negative -> "Negative"
694   | Positive -> "Positive"
695 ;;
696
697
698 type pos = Left | Right 
699
700 let string_of_pos = function
701   | Left -> "Left"
702   | Right -> "Right"
703 ;;
704
705
706 let eq_ind_URI () = LibraryObjects.eq_ind_URI ~eq:(LibraryObjects.eq_URI ())
707 let eq_ind_r_URI () = LibraryObjects.eq_ind_r_URI ~eq:(LibraryObjects.eq_URI ())
708 let sym_eq_URI () = LibraryObjects.sym_eq_URI ~eq:(LibraryObjects.eq_URI ())
709 let eq_XURI () =
710   let s = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
711   UriManager.uri_of_string (s ^ "#xpointer(1/1/1)")
712 let trans_eq_URI () = LibraryObjects.trans_eq_URI ~eq:(LibraryObjects.eq_URI ())