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
67 let compare = Pervasives.compare
70 module TermSet = Set.Make(OrderedTerm);;
71 module TermMap = Map.Make(OrderedTerm);;
73 let symbols_of_term term =
75 let rec aux map = function
78 List.fold_left (fun res t -> (aux res t)) map l
82 let c = TermMap.find t map in
83 TermMap.add t (c+1) map
89 aux TermMap.empty term
93 let metas_of_term term =
95 let rec aux = function
96 | C.Meta _ as t -> TermSet.singleton t
98 List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
99 | t -> TermSet.empty (* TODO: maybe add other cases? *)
105 (************************* rpo ********************************)
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;
132 let sig_order_const t1 t2 =
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
139 else if n1 > n2 then Gt
142 prerr_endline ("t1 = "^(CicPp.ppterm t1));
143 prerr_endline ("t2 = "^(CicPp.ppterm t2));
148 | Not_found -> Incomparable
150 let sig_order t1 t2 =
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
157 | _,_ -> sig_order_const t1 t2
159 let rec rpo_lt t1 t2 =
160 let module C = Cic in
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 ->
168 | C.Appl (h1::arg1),C.Appl (h2::arg2) when h1=h2 ->
169 if lex_lt arg1 arg2 then
172 | C.Appl (h1::arg1),C.Appl (h2::arg2) ->
173 (match sig_order h1 h2 with
174 | Lt -> check_lt arg1 t2
176 | C.Appl (h1::arg1), t2 when atomic t2 ->
177 (match sig_order h1 t2 with
178 | Lt -> check_lt arg1 t2
180 | t1 , C.Appl (h2::arg2) when atomic t1 ->
181 (match sig_order t1 h2 with
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
192 if first_trie then true else
195 List.exists (fun a -> t1 = a || rpo_lt t1 a) args
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
208 (fun b a -> b && (rpo_lt a t))
213 if rpo_lt t2 t1 then Gt
214 else if rpo_lt t1 t2 then Lt
218 (*********************** fine rpo *****************************)
220 (* (weight of constants, [(meta, weight_of_meta)]) *)
221 type weight = int * (int * int) list;;
223 let string_of_weight (cw, mw) =
226 (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
228 Printf.sprintf "[%d; %s]" cw s
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 ->
237 let oldw = Hashtbl.find vars_dict metano in
238 Hashtbl.replace vars_dict metano (oldw+1)
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
246 | C.MutInd (_, _, ens)
247 | C.MutConstruct (_, _, _, ens) ->
248 List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
251 | C.Lambda (_, t1, t2)
253 | C.LetIn (_, t1, t2) ->
258 | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
260 | C.MutCase (_, _, outt, t, pl) ->
263 let w3 = List.fold_left (+) 0 (List.map aux pl) in
267 List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
270 List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
276 Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
279 | (m1, _), (m2, _) -> m2 - m1
281 (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
285 module OrderedInt = struct
288 let compare = Pervasives.compare
291 module IntSet = Set.Make(OrderedInt)
293 let compute_equality_weight (ty,left,right,o) =
297 let w, m = (weight_of_term
298 ~consider_metas:true ~count_metas_occurrences:false right) in
299 w + (factor * (List.length m)) ;
302 let w, m = (weight_of_term
303 ~consider_metas:true ~count_metas_occurrences:false left) in
304 w + (factor * (List.length m)) ;
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))
316 let compute_equality_weight (ty,left,right,o) =
317 let metasw = ref 0 in
319 let w, m = (weight_of_term
320 ~consider_metas:true ~count_metas_occurrences:false t) in
321 metasw := !metasw + (1 * (List.length m)) ;
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 *)
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
340 | m -> (m, 0)::(aux (m-1))
342 let tmpl = aux maxmeta in
345 (fun (m, _) (n, _) -> Pervasives.compare m n)
347 (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
353 let normalize_weights (cw1, wl1) (cw2, wl2) =
354 let rec aux wl1 wl2 =
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
367 let res1, res2 = aux [] tl2 in
368 (n, 0)::res1, (n, w)::res2
370 let res1, res2 = aux tl1 [] in
371 (m, w)::res1, (m, 0)::res2
372 | _, _ -> assert false
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)
380 let compare_weights ?(normalize=false)
381 ((h1, w1) as weight1) ((h2, w2) as weight2)=
382 let (h1, w1), (h2, w2) =
384 normalize_weights weight1 weight2
391 (fun ((lt, eq, gt), diffs) w1 w2 ->
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) ->
402 (Printf.sprintf "HMMM!!!! %s, %s\n"
403 (string_of_weight weight1) (string_of_weight weight2)));
406 with Invalid_argument _ ->
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)));
415 let hdiff = h1 - h2 in
419 else if hdiff > 0 then Gt
420 else Eq (* Incomparable *)
422 if hdiff <= 0 then Lt
423 else if (- diffs) >= hdiff then Le else Incomparable
425 if hdiff >= 0 then Gt
426 else if diffs >= (- hdiff) then Ge else Incomparable
427 | (m, _, n) when m > 0 && n > 0 ->
434 let rec aux_ordering ?(recursion=true) t1 t2 =
435 let module C = Cic in
436 let compare_uris u1 u2 =
438 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
440 else if res = 0 then Eq
445 | _, C.Meta _ -> Incomparable
447 | t1, t2 when t1 = t2 -> Eq
449 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
453 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
457 | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
458 | C.MutInd _, _ -> Lt
459 | _, C.MutInd _ -> Gt
461 | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
463 | C.MutConstruct _, _ -> Lt
464 | _, C.MutConstruct _ -> Gt
466 | C.Appl l1, C.Appl l2 when recursion ->
472 | hd1::tl1, hd2::tl2 ->
473 let o = aux_ordering hd1 hd2 in
474 if o = Eq then cmp tl1 tl2
478 | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
484 (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
485 (CicPp.ppterm t1) (CicPp.ppterm t2)));
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
500 let nonrec_kbo t1 t2 =
501 let w1 = weight_of_term t1 in
502 let w2 = weight_of_term t2 in
504 prerr_endline ("weight1 :"^(string_of_weight w1));
505 prerr_endline ("weight2 :"^(string_of_weight w2));
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
516 let aux = aux_ordering ~recursion:false in
517 let w1 = weight_of_term t1
518 and w2 = weight_of_term t2 in
524 | hd1::tl1, hd2::tl2 ->
528 if o = Eq then cmp tl1 tl2
531 let comparison = compare_weights ~normalize:true w1 w2 in
532 match comparison with
536 else if r = Eq then (
538 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
539 if cmp tl1 tl2 = Lt then Lt else Incomparable
540 | _, _ -> Incomparable
545 else if r = Eq then (
547 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
548 if cmp tl1 tl2 = Gt then Gt else Incomparable
549 | _, _ -> Incomparable
555 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
557 | _, _ -> Incomparable
565 Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
566 | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) ->
569 let aux = aux_ordering ~recursion:false in
570 let w1 = weight_of_term t1
571 and w2 = weight_of_term t2 in
577 | hd1::tl1, hd2::tl2 ->
581 if o = Eq then cmp tl1 tl2
584 match get_hd t1, get_hd t2 with
588 let comparison = compare_weights ~normalize:true w1 w2 in
589 match comparison with
593 else if r = Eq then (
595 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
596 if cmp tl1 tl2 = Lt then Lt else Incomparable
597 | _, _ -> Incomparable
602 else if r = Eq then (
604 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
605 if cmp tl1 tl2 = Gt then Gt else Incomparable
606 | _, _ -> Incomparable
612 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
614 | _, _ -> Incomparable
619 let names_of_context context =
623 | Some (n, e) -> Some n)
629 let module C = Cic in
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) -> (
644 let res1 = List.fold_left (f t2) false tl1 in
646 else let res2 = List.fold_left (f t1) false tl2 in
650 if res <> Incomparable then
654 if not r then false else
659 match aux_ordering hd1 hd2 with
661 let res = List.fold_left (f t1) false tl2 in
665 let res = List.fold_left (f t2) false tl1 in
672 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
674 with Invalid_argument _ ->
679 if List.fold_left (f t1) false tl2 then Gt
682 if List.fold_left (f t2) false tl1 then Lt
688 | t1, t2 -> aux_ordering t1 t2
692 (* settable by the user... *)
693 let compare_terms = ref nonrec_kbo;;
694 (* let compare_terms = ref ao;; *)
695 (* let compare_terms = ref rpo;; *)
697 let guarded_simpl ?(debug=false) context t =
698 if !compare_terms == nonrec_kbo then t
700 let t' = ProofEngineReduction.simpl context t in
701 if t = t' then t else
703 let simpl_order = !compare_terms t t' in
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)
711 type equality_sign = Negative | Positive;;
713 let string_of_sign = function
714 | Negative -> "Negative"
715 | Positive -> "Positive"
719 type pos = Left | Right
721 let string_of_pos = function
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 ())
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 ())