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