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 ->
435 let rec aux_ordering ?(recursion=true) t1 t2 =
436 let module C = Cic in
437 let compare_uris u1 u2 =
439 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
441 else if res = 0 then Eq
446 | _, C.Meta _ -> Incomparable
448 | t1, t2 when t1 = t2 -> Eq
450 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
454 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
458 | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
459 | C.MutInd _, _ -> Lt
460 | _, C.MutInd _ -> Gt
462 | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
464 | C.MutConstruct _, _ -> Lt
465 | _, C.MutConstruct _ -> Gt
467 | C.Appl l1, C.Appl l2 when recursion ->
473 | hd1::tl1, hd2::tl2 ->
474 let o = aux_ordering hd1 hd2 in
475 if o = Eq then cmp tl1 tl2
479 | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
485 (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
486 (CicPp.ppterm t1) (CicPp.ppterm t2)));
491 (* w1, w2 are the weights, they should already be normalized... *)
492 let nonrec_kbo_w (t1, w1) (t2, w2) =
493 match compare_weights w1 w2 with
494 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
495 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
496 | Eq -> aux_ordering t1 t2
501 let nonrec_kbo t1 t2 =
502 let w1 = weight_of_term t1 in
503 let w2 = weight_of_term t2 in
505 prerr_endline ("weight1 :"^(string_of_weight w1));
506 prerr_endline ("weight2 :"^(string_of_weight w2));
508 match compare_weights ~normalize:true w1 w2 with
509 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
510 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
511 | Eq -> aux_ordering t1 t2
517 let aux = aux_ordering ~recursion:false in
518 let w1 = weight_of_term t1
519 and w2 = weight_of_term t2 in
525 | hd1::tl1, hd2::tl2 ->
529 if o = Eq then cmp tl1 tl2
532 let comparison = compare_weights ~normalize:true w1 w2 in
533 match comparison with
537 else if r = Eq then (
539 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
540 if cmp tl1 tl2 = Lt then Lt else Incomparable
541 | _, _ -> Incomparable
546 else if r = Eq then (
548 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
549 if cmp tl1 tl2 = Gt then Gt else Incomparable
550 | _, _ -> Incomparable
556 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
558 | _, _ -> Incomparable
566 Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
567 | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) ->
570 let aux = aux_ordering ~recursion:false in
571 let w1 = weight_of_term t1
572 and w2 = weight_of_term t2 in
578 | hd1::tl1, hd2::tl2 ->
582 if o = Eq then cmp tl1 tl2
585 match get_hd t1, get_hd t2 with
589 let comparison = compare_weights ~normalize:true w1 w2 in
590 match comparison with
594 else if r = Eq then (
596 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
597 if cmp tl1 tl2 = Lt then Lt else Incomparable
598 | _, _ -> Incomparable
603 else if r = Eq then (
605 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
606 if cmp tl1 tl2 = Gt then Gt else Incomparable
607 | _, _ -> Incomparable
613 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
615 | _, _ -> Incomparable
620 let names_of_context context =
624 | Some (n, e) -> Some n)
630 let module C = Cic in
632 | t1, t2 when t1 = t2 -> Eq
633 | t1, (C.Meta _ as m) ->
634 if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
635 | (C.Meta _ as m), t2 ->
636 if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
637 | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
645 let res1 = List.fold_left (f t2) false tl1 in
647 else let res2 = List.fold_left (f t1) false tl2 in
651 if res <> Incomparable then
655 if not r then false else
660 match aux_ordering hd1 hd2 with
662 let res = List.fold_left (f t1) false tl2 in
666 let res = List.fold_left (f t2) false tl1 in
673 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
675 with Invalid_argument _ ->
680 if List.fold_left (f t1) false tl2 then Gt
683 if List.fold_left (f t2) false tl1 then Lt
689 | t1, t2 -> aux_ordering t1 t2
693 (* settable by the user... *)
694 let compare_terms = ref nonrec_kbo;;
695 (* let compare_terms = ref ao;; *)
696 (* let compare_terms = ref rpo;; *)
698 let guarded_simpl ?(debug=false) context t =
699 if !compare_terms == nonrec_kbo then t
701 let t' = ProofEngineReduction.simpl context t in
702 if t = t' then t else
704 let simpl_order = !compare_terms t t' in
706 prerr_endline ("comparing "^(CicPp.ppterm t)^(CicPp.ppterm t'));
707 if simpl_order = Gt then (if debug then prerr_endline "GT";t')
708 else (if debug then prerr_endline "NO_GT";t)
712 type equality_sign = Negative | Positive;;
714 let string_of_sign = function
715 | Negative -> "Negative"
716 | Positive -> "Positive"
720 type pos = Left | Right
722 let string_of_pos = function
728 let eq_ind_URI () = LibraryObjects.eq_ind_URI ~eq:(LibraryObjects.eq_URI ())
729 let eq_ind_r_URI () = LibraryObjects.eq_ind_r_URI ~eq:(LibraryObjects.eq_URI ())
730 let sym_eq_URI () = LibraryObjects.sym_eq_URI ~eq:(LibraryObjects.eq_URI ())
732 let s = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
733 UriManager.uri_of_string (s ^ "#xpointer(1/1/1)")
734 let trans_eq_URI () = LibraryObjects.trans_eq_URI ~eq:(LibraryObjects.eq_URI ())
736 let metas_of_term t =
737 List.map fst (CicUtil.metas_of_term t)