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