]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/paramodulation/utils.ml
Snapshot.
[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 =
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 + (2 * (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   w + !metasw
307 ;;
308
309
310 (* returns a "normalized" version of the polynomial weight wl (with type
311  * weight list), i.e. a list sorted ascending by meta number,
312  * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
313  * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
314  *      (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
315 let normalize_weight maxmeta (cw, wl) =
316   let rec aux = function
317     | 0 -> []
318     | m -> (m, 0)::(aux (m-1))
319   in
320   let tmpl = aux maxmeta in
321   let wl =
322     List.sort
323       (fun (m, _) (n, _) -> Pervasives.compare m n)
324       (List.fold_left
325          (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
326   in
327   (cw, wl)
328 ;;
329
330
331 let normalize_weights (cw1, wl1) (cw2, wl2) =
332   let rec aux wl1 wl2 =
333     match wl1, wl2 with
334     | [], [] -> [], []
335     | (m, w)::tl1, (n, w')::tl2 when m = n ->
336         let res1, res2 = aux tl1 tl2 in
337         (m, w)::res1, (n, w')::res2
338     | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
339         let res1, res2 = aux tl1 wl2 in
340         (m, w)::res1, (m, 0)::res2
341     | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
342         let res1, res2 = aux wl1 tl2 in
343         (n, 0)::res1, (n, w')::res2
344     | [], (n, w)::tl2 ->
345         let res1, res2 = aux [] tl2 in
346         (n, 0)::res1, (n, w)::res2
347     | (m, w)::tl1, [] ->
348         let res1, res2 = aux tl1 [] in
349         (m, w)::res1, (m, 0)::res2
350     | _, _ -> assert false
351   in
352   let cmp (m, _) (n, _) = compare m n in
353   let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
354   (cw1, wl1), (cw2, wl2)
355 ;;
356
357         
358 let compare_weights ?(normalize=false)
359     ((h1, w1) as weight1) ((h2, w2) as weight2)=
360   let (h1, w1), (h2, w2) =
361     if normalize then
362       normalize_weights weight1 weight2
363     else
364       (h1, w1), (h2, w2)
365   in
366   let res, diffs =
367     try
368       List.fold_left2
369         (fun ((lt, eq, gt), diffs) w1 w2 ->
370            match w1, w2 with
371            | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
372                let diffs = (w1 - w2) + diffs in 
373                let r = compare w1 w2 in
374                if r < 0 then (lt+1, eq, gt), diffs
375                else if r = 0 then (lt, eq+1, gt), diffs
376                else (lt, eq, gt+1), diffs
377            | (meta1, w1), (meta2, w2) ->
378                debug_print
379                  (lazy
380                     (Printf.sprintf "HMMM!!!! %s, %s\n"
381                        (string_of_weight weight1) (string_of_weight weight2)));
382                assert false)
383         ((0, 0, 0), 0) w1 w2
384     with Invalid_argument _ ->
385       debug_print
386         (lazy
387            (Printf.sprintf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
388               (string_of_weight (h1, w1)) (string_of_weight weight1)
389               (string_of_weight (h2, w2)) (string_of_weight weight2)
390               (string_of_bool normalize)));
391       assert false
392   in
393   let hdiff = h1 - h2 in 
394   match res with
395   | (0, _, 0) ->
396       if hdiff < 0 then Lt
397       else if hdiff > 0 then Gt
398       else Eq (* Incomparable *)
399   | (m, _, 0) ->
400       if hdiff <= 0 then Lt
401       else if (- diffs) >= hdiff then Le else Incomparable
402   | (0, _, m) ->
403       if hdiff >= 0 then Gt
404       else if diffs >= (- hdiff) then Ge else Incomparable
405   | (m, _, n) when m > 0 && n > 0 ->
406       Incomparable
407   | _ -> assert false 
408
409 ;;
410
411
412 let rec aux_ordering ?(recursion=true) t1 t2 =
413   let module C = Cic in
414   let compare_uris u1 u2 =
415     let res =
416       compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
417     if res < 0 then Lt
418     else if res = 0 then Eq
419     else Gt
420   in
421   match t1, t2 with
422   | C.Meta _, _
423   | _, C.Meta _ -> Incomparable
424
425   | t1, t2 when t1 = t2 -> Eq
426
427   | C.Rel n, C.Rel m -> if n > m then Lt else Gt
428   | C.Rel _, _ -> Lt
429   | _, C.Rel _ -> Gt
430
431   | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
432   | C.Const _, _ -> Lt
433   | _, C.Const _ -> Gt
434
435   | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
436   | C.MutInd _, _ -> Lt
437   | _, C.MutInd _ -> Gt
438
439   | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
440       compare_uris u1 u2
441   | C.MutConstruct _, _ -> Lt
442   | _, C.MutConstruct _ -> Gt
443
444   | C.Appl l1, C.Appl l2 when recursion ->
445       let rec cmp t1 t2 =
446         match t1, t2 with
447         | [], [] -> Eq
448         | _, [] -> Gt
449         | [], _ -> Lt
450         | hd1::tl1, hd2::tl2 ->
451             let o = aux_ordering hd1 hd2 in
452             if o = Eq then cmp tl1 tl2
453             else o
454       in
455       cmp l1 l2
456   | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
457       aux_ordering h1 h2
458         
459   | t1, t2 ->
460       debug_print
461         (lazy
462            (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
463               (CicPp.ppterm t1) (CicPp.ppterm t2)));
464       Incomparable
465 ;;
466
467
468 (* w1, w2 are the weights, they should already be normalized... *)
469 let nonrec_kbo_w (t1, w1) (t2, w2) =
470   match compare_weights w1 w2 with
471   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
472   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
473   | Eq -> aux_ordering t1 t2
474   | res -> res
475 ;;
476
477     
478 let nonrec_kbo t1 t2 =
479   let w1 = weight_of_term t1 in
480   let w2 = weight_of_term t2 in
481   (* 
482   prerr_endline ("weight1 :"^(string_of_weight w1));
483   prerr_endline ("weight2 :"^(string_of_weight w2)); 
484   *)
485   match compare_weights ~normalize:true w1 w2 with
486   | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
487   | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
488   | Eq -> aux_ordering t1 t2
489   | res -> res
490 ;;
491
492
493 let rec kbo t1 t2 =
494   let aux = aux_ordering ~recursion:false in
495   let w1 = weight_of_term t1
496   and w2 = weight_of_term t2 in
497   let rec cmp t1 t2 =
498     match t1, t2 with
499     | [], [] -> Eq
500     | _, [] -> Gt
501     | [], _ -> Lt
502     | hd1::tl1, hd2::tl2 ->
503         let o =
504           kbo hd1 hd2
505         in
506         if o = Eq then cmp tl1 tl2
507         else o
508   in
509   let comparison = compare_weights ~normalize:true w1 w2 in
510   match comparison with
511   | Le ->
512       let r = aux t1 t2 in
513       if r = Lt then Lt
514       else if r = Eq then (
515         match t1, t2 with
516         | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
517             if cmp tl1 tl2 = Lt then Lt else Incomparable
518         | _, _ ->  Incomparable
519       ) else Incomparable
520   | Ge ->
521       let r = aux t1 t2 in
522       if r = Gt then Gt
523       else if r = Eq then (
524         match t1, t2 with
525         | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
526             if cmp tl1 tl2 = Gt then Gt else Incomparable
527         | _, _ ->  Incomparable
528       ) else Incomparable
529   | Eq ->
530       let r = aux t1 t2 in
531       if r = Eq then (
532         match t1, t2 with
533         | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
534             cmp tl1 tl2
535         | _, _ ->  Incomparable
536       ) else r 
537   | res -> res
538 ;;
539           
540 let rec ao t1 t2 =
541   let get_hd t =
542     match t with
543         Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
544       | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) -> 
545           Some(uri,tyno,cno)
546       | _ -> None in
547   let aux = aux_ordering ~recursion:false in
548   let w1 = weight_of_term t1
549   and w2 = weight_of_term t2 in
550   let rec cmp t1 t2 =
551     match t1, t2 with
552     | [], [] -> Eq
553     | _, [] -> Gt
554     | [], _ -> Lt
555     | hd1::tl1, hd2::tl2 ->
556         let o =
557           ao hd1 hd2
558         in
559         if o = Eq then cmp tl1 tl2
560         else o
561   in
562   match get_hd t1, get_hd t2 with
563       Some(_),None -> Lt
564     | None,Some(_) -> Gt
565     | _ ->
566         let comparison = compare_weights ~normalize:true w1 w2 in
567           match comparison with
568             | Le ->
569                 let r = aux t1 t2 in
570                   if r = Lt then Lt
571                   else if r = Eq then (
572                     match t1, t2 with
573                       | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
574                           if cmp tl1 tl2 = Lt then Lt else Incomparable
575                       | _, _ ->  Incomparable
576                   ) else Incomparable
577             | Ge ->
578                 let r = aux t1 t2 in
579                   if r = Gt then Gt
580                   else if r = Eq then (
581                     match t1, t2 with
582                       | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
583                           if cmp tl1 tl2 = Gt then Gt else Incomparable
584                       | _, _ ->  Incomparable
585                   ) else Incomparable
586             | Eq ->
587                 let r = aux t1 t2 in
588                   if r = Eq then (
589                     match t1, t2 with
590                       | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
591                           cmp tl1 tl2
592                       | _, _ ->  Incomparable
593                   ) else r 
594             | res -> res
595 ;;
596
597 let names_of_context context = 
598   List.map
599     (function
600        | None -> None
601        | Some (n, e) -> Some n)
602     context
603 ;;
604
605
606 let rec lpo t1 t2 =
607   let module C = Cic in
608   match t1, t2 with
609   | t1, t2 when t1 = t2 -> Eq
610   | t1, (C.Meta _ as m) ->
611       if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
612   | (C.Meta _ as m), t2 ->
613       if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
614   | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
615       let res =
616         let f o r t =
617           if r then true else
618             match lpo t o with
619             | Gt | Eq -> true
620             | _ -> false
621         in
622         let res1 = List.fold_left (f t2) false tl1 in
623         if res1 then Gt
624         else let res2 = List.fold_left (f t1) false tl2 in
625         if res2 then Lt
626         else Incomparable
627       in
628       if res <> Incomparable then
629         res
630       else
631         let f o r t =
632           if not r then false else
633             match lpo o t with
634             | Gt -> true
635             | _ -> false
636         in
637         match aux_ordering hd1 hd2 with
638         | Gt ->
639             let res = List.fold_left (f t1) false tl2 in
640             if res then Gt
641             else Incomparable
642         | Lt ->
643             let res = List.fold_left (f t2) false tl1 in
644             if res then Lt
645             else Incomparable
646         | Eq -> (
647             let lex_res =
648               try
649                 List.fold_left2
650                   (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
651                   Eq tl1 tl2
652               with Invalid_argument _ ->
653                 Incomparable
654             in
655             match lex_res with
656             | Gt ->
657                 if List.fold_left (f t1) false tl2 then Gt
658                 else Incomparable
659             | Lt ->
660                 if List.fold_left (f t2) false tl1 then Lt
661                 else Incomparable
662             | _ -> Incomparable
663           )
664         | _ -> Incomparable
665     )
666   | t1, t2 -> aux_ordering t1 t2
667 ;;
668
669
670 (* settable by the user... *)
671 let compare_terms = ref nonrec_kbo;; 
672 (* let compare_terms = ref ao;; *)
673 (* let compare_terms = ref rpo;; *)
674
675 let guarded_simpl ?(debug=false) context t =
676   if !compare_terms == nonrec_kbo then t
677   else
678     let t' = ProofEngineReduction.simpl context t in
679     if t = t' then t else
680       begin
681         let simpl_order = !compare_terms t t' in
682         if debug then
683           prerr_endline ("comparing "^(CicPp.ppterm t)^(CicPp.ppterm t'));
684         if simpl_order = Gt then (if debug then prerr_endline "GT";t')
685         else (if debug then prerr_endline "NO_GT";t)
686       end
687 ;;
688
689 type equality_sign = Negative | Positive;;
690
691 let string_of_sign = function
692   | Negative -> "Negative"
693   | Positive -> "Positive"
694 ;;
695
696
697 type pos = Left | Right 
698
699 let string_of_pos = function
700   | Left -> "Left"
701   | Right -> "Right"
702 ;;
703
704
705 let eq_ind_URI () = LibraryObjects.eq_ind_URI ~eq:(LibraryObjects.eq_URI ())
706 let eq_ind_r_URI () = LibraryObjects.eq_ind_r_URI ~eq:(LibraryObjects.eq_URI ())
707 let sym_eq_URI () = LibraryObjects.sym_eq_URI ~eq:(LibraryObjects.eq_URI ())
708 let eq_XURI () =
709   let s = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
710   UriManager.uri_of_string (s ^ "#xpointer(1/1/1)")
711 let trans_eq_URI () = LibraryObjects.trans_eq_URI ~eq:(LibraryObjects.eq_URI ())