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