1 (* Copyright (C) 2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
30 let debug_metas = false;;
31 let debug_res = false;;
33 let debug_print s = if debug then prerr_endline (Lazy.force s);;
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))
44 let print_subst ?(prefix="\n") subst =
47 (fun (i, (c, t, ty)) ->
48 Printf.sprintf "?%d -> %s : %s" i
49 (CicPp.ppterm t) (CicPp.ppterm ty))
53 type comparison = Lt | Le | Eq | Ge | Gt | Incomparable;;
55 let string_of_comparison = function
63 type environment = Cic.metasenv * Cic.context * CicUniv.universe_graph
69 let compare = Pervasives.compare
72 module TermSet = Set.Make(OrderedTerm);;
73 module TermMap = Map.Make(OrderedTerm);;
75 let symbols_of_term term =
77 let rec aux map = function
80 List.fold_left (fun res t -> (aux res t)) map l
84 let c = TermMap.find t map in
85 TermMap.add t (c+1) map
91 aux TermMap.empty term
95 let metas_of_term term =
97 let rec aux = function
98 | C.Meta _ as t -> TermSet.singleton t
100 List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
101 | t -> TermSet.empty (* TODO: maybe add other cases? *)
107 (************************* rpo ********************************)
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;
134 let sig_order_const t1 t2 =
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
141 else if n1 > n2 then Gt
144 prerr_endline ("t1 = "^(CicPp.ppterm t1));
145 prerr_endline ("t2 = "^(CicPp.ppterm t2));
150 | Not_found -> Incomparable
152 let sig_order t1 t2 =
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
159 | _,_ -> sig_order_const t1 t2
161 let rec rpo_lt t1 t2 =
162 let module C = Cic in
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 ->
170 | C.Appl (h1::arg1),C.Appl (h2::arg2) when h1=h2 ->
171 if lex_lt arg1 arg2 then
174 | C.Appl (h1::arg1),C.Appl (h2::arg2) ->
175 (match sig_order h1 h2 with
176 | Lt -> check_lt arg1 t2
178 | C.Appl (h1::arg1), t2 when atomic t2 ->
179 (match sig_order h1 t2 with
180 | Lt -> check_lt arg1 t2
182 | t1 , C.Appl (h2::arg2) when atomic t1 ->
183 (match sig_order t1 h2 with
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
194 if first_trie then true else
197 List.exists (fun a -> t1 = a || rpo_lt t1 a) args
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
210 (fun b a -> b && (rpo_lt a t))
215 if rpo_lt t2 t1 then Gt
216 else if rpo_lt t1 t2 then Lt
220 (*********************** fine rpo *****************************)
222 (* (weight of constants, [(meta, weight_of_meta)]) *)
223 type weight = int * (int * int) list;;
225 let string_of_weight (cw, mw) =
228 (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
230 Printf.sprintf "[%d; %s]" cw s
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 ->
239 let oldw = Hashtbl.find vars_dict metano in
240 Hashtbl.replace vars_dict metano (oldw+1)
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
248 | C.MutInd (_, _, ens)
249 | C.MutConstruct (_, _, _, ens) ->
250 List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
253 | C.Lambda (_, t1, t2)
255 | C.LetIn (_, t1, t2) ->
260 | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
262 | C.MutCase (_, _, outt, t, pl) ->
265 let w3 = List.fold_left (+) 0 (List.map aux pl) in
269 List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
272 List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
278 Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
281 | (m1, _), (m2, _) -> m2 - m1
283 (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
287 module OrderedInt = struct
290 let compare = Pervasives.compare
293 module IntSet = Set.Make(OrderedInt)
295 let compute_equality_weight (ty,left,right,o) =
299 let w, m = (weight_of_term
300 ~consider_metas:true ~count_metas_occurrences:false right) in
301 w + (factor * (List.length m)) ;
304 let w, m = (weight_of_term
305 ~consider_metas:true ~count_metas_occurrences:false left) in
306 w + (factor * (List.length m)) ;
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))
318 let compute_equality_weight (ty,left,right,o) =
319 let metasw = ref 0 in
321 let w, m = (weight_of_term
322 ~consider_metas:true ~count_metas_occurrences:false t) in
323 metasw := !metasw + (1 * (List.length m)) ;
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 *)
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
342 | m -> (m, 0)::(aux (m-1))
344 let tmpl = aux maxmeta in
347 (fun (m, _) (n, _) -> Pervasives.compare m n)
349 (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
355 let normalize_weights (cw1, wl1) (cw2, wl2) =
356 let rec aux wl1 wl2 =
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
369 let res1, res2 = aux [] tl2 in
370 (n, 0)::res1, (n, w)::res2
372 let res1, res2 = aux tl1 [] in
373 (m, w)::res1, (m, 0)::res2
374 | _, _ -> assert false
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)
382 let compare_weights ?(normalize=false)
383 ((h1, w1) as weight1) ((h2, w2) as weight2)=
384 let (h1, w1), (h2, w2) =
386 normalize_weights weight1 weight2
393 (fun ((lt, eq, gt), diffs) w1 w2 ->
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) ->
404 (Printf.sprintf "HMMM!!!! %s, %s\n"
405 (string_of_weight weight1) (string_of_weight weight2)));
408 with Invalid_argument _ ->
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)));
417 let hdiff = h1 - h2 in
421 else if hdiff > 0 then Gt
422 else Eq (* Incomparable *)
424 if hdiff <= 0 then Lt
425 else if (- diffs) >= hdiff then Le else Incomparable
427 if hdiff >= 0 then Gt
428 else if diffs >= (- hdiff) then Ge else Incomparable
429 | (m, _, n) when m > 0 && n > 0 ->
436 let rec aux_ordering ?(recursion=true) t1 t2 =
437 let module C = Cic in
438 let compare_uris u1 u2 =
440 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
442 else if res = 0 then Eq
447 | _, C.Meta _ -> Incomparable
449 | t1, t2 when t1 = t2 -> Eq
451 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
455 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
459 | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
460 | C.MutInd _, _ -> Lt
461 | _, C.MutInd _ -> Gt
463 | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
465 | C.MutConstruct _, _ -> Lt
466 | _, C.MutConstruct _ -> Gt
468 | C.Appl l1, C.Appl l2 when recursion ->
474 | hd1::tl1, hd2::tl2 ->
475 let o = aux_ordering hd1 hd2 in
476 if o = Eq then cmp tl1 tl2
480 | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
486 (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
487 (CicPp.ppterm t1) (CicPp.ppterm t2)));
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
502 let nonrec_kbo t1 t2 =
503 let w1 = weight_of_term t1 in
504 let w2 = weight_of_term t2 in
506 prerr_endline ("weight1 :"^(string_of_weight w1));
507 prerr_endline ("weight2 :"^(string_of_weight w2));
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
518 let aux = aux_ordering ~recursion:false in
519 let w1 = weight_of_term t1
520 and w2 = weight_of_term t2 in
526 | hd1::tl1, hd2::tl2 ->
530 if o = Eq then cmp tl1 tl2
533 let comparison = compare_weights ~normalize:true w1 w2 in
534 match comparison with
538 else if r = Eq then (
540 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
541 if cmp tl1 tl2 = Lt then Lt else Incomparable
542 | _, _ -> Incomparable
547 else if r = Eq then (
549 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
550 if cmp tl1 tl2 = Gt then Gt else Incomparable
551 | _, _ -> Incomparable
557 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
559 | _, _ -> Incomparable
567 Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
568 | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) ->
571 let aux = aux_ordering ~recursion:false in
572 let w1 = weight_of_term t1
573 and w2 = weight_of_term t2 in
579 | hd1::tl1, hd2::tl2 ->
583 if o = Eq then cmp tl1 tl2
586 match get_hd t1, get_hd t2 with
590 let comparison = compare_weights ~normalize:true w1 w2 in
591 match comparison with
595 else if r = Eq then (
597 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
598 if cmp tl1 tl2 = Lt then Lt else Incomparable
599 | _, _ -> Incomparable
604 else if r = Eq then (
606 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
607 if cmp tl1 tl2 = Gt then Gt else Incomparable
608 | _, _ -> Incomparable
614 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
616 | _, _ -> Incomparable
621 let names_of_context context =
625 | Some (n, e) -> Some n)
631 let module C = Cic in
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) -> (
646 let res1 = List.fold_left (f t2) false tl1 in
648 else let res2 = List.fold_left (f t1) false tl2 in
652 if res <> Incomparable then
656 if not r then false else
661 match aux_ordering hd1 hd2 with
663 let res = List.fold_left (f t1) false tl2 in
667 let res = List.fold_left (f t2) false tl1 in
674 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
676 with Invalid_argument _ ->
681 if List.fold_left (f t1) false tl2 then Gt
684 if List.fold_left (f t2) false tl1 then Lt
690 | t1, t2 -> aux_ordering t1 t2
694 (* settable by the user... *)
695 let compare_terms = ref nonrec_kbo;;
696 (* let compare_terms = ref ao;; *)
697 (* let compare_terms = ref rpo;; *)
699 let guarded_simpl ?(debug=false) context t =
700 if !compare_terms == nonrec_kbo then t
702 let t' = ProofEngineReduction.simpl context t in
703 if t = t' then t else
705 let simpl_order = !compare_terms t t' in
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)
713 type equality_sign = Negative | Positive;;
715 let string_of_sign = function
716 | Negative -> "Negative"
717 | Positive -> "Positive"
721 type pos = Left | Right
723 let string_of_pos = function
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 ())
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 ())
737 let rec metas_of_term = function
738 | Cic.Meta (i, c) -> [i]
741 | Cic.MutInd (_, _, ens)
742 | Cic.MutConstruct (_, _, _, ens) ->
743 List.flatten (List.map (fun (u, t) -> metas_of_term t) ens)
746 | Cic.Lambda (_, s, t)
747 | Cic.LetIn (_, s, t) -> (metas_of_term s) @ (metas_of_term t)
748 | Cic.Appl l -> List.flatten (List.map metas_of_term l)
749 | Cic.MutCase (uri, i, s, t, l) ->
750 (metas_of_term s) @ (metas_of_term t) @
751 (List.flatten (List.map metas_of_term l))
754 (List.map (fun (s, i, t1, t2) ->
755 (metas_of_term t1) @ (metas_of_term t2)) il)
756 | Cic.CoFix (i, il) ->
758 (List.map (fun (s, t1, t2) ->
759 (metas_of_term t1) @ (metas_of_term t2)) il)