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_print s = if debug then prerr_endline (Lazy.force s);;
32 let print_metasenv metasenv =
33 String.concat "\n--------------------------\n"
34 (List.map (fun (i, context, term) ->
35 (string_of_int i) ^ " [\n" ^ (CicPp.ppcontext context) ^
36 "\n] " ^ (CicPp.ppterm term))
43 let print_subst ?(prefix="\n") subst =
46 (fun (i, (c, t, ty)) ->
47 Printf.sprintf "?%d -> %s : %s" i
48 (CicPp.ppterm t) (CicPp.ppterm ty))
52 type comparison = Lt | Le | Eq | Ge | Gt | Incomparable;;
54 let string_of_comparison = function
66 let compare = Pervasives.compare
69 module TermSet = Set.Make(OrderedTerm);;
70 module TermMap = Map.Make(OrderedTerm);;
72 let symbols_of_term term =
74 let rec aux map = function
77 List.fold_left (fun res t -> (aux res t)) map l
81 let c = TermMap.find t map in
82 TermMap.add t (c+1) map
88 aux TermMap.empty term
92 let metas_of_term term =
94 let rec aux = function
95 | C.Meta _ as t -> TermSet.singleton t
97 List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
98 | t -> TermSet.empty (* TODO: maybe add other cases? *)
104 (************************* rpo ********************************)
106 UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1)",3;
107 UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1/1)",6;
108 UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1/2)",9;
109 HelmLibraryObjects.Peano.pred_URI, 12;
110 HelmLibraryObjects.Peano.plus_URI, 15;
111 HelmLibraryObjects.Peano.minus_URI, 18;
112 HelmLibraryObjects.Peano.mult_URI, 21;
113 UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind#xpointer(1/1)",103;
114 UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind#xpointer(1/1/1)",106;
115 UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind#xpointer(1/1/2)",109;
116 UriManager.uri_of_string "cic:/matita/nat/nat/pred.con",112;
117 UriManager.uri_of_string "cic:/matita/nat/plus/plus.con",115;
118 UriManager.uri_of_string "cic:/matita/nat/minus/minus.con",118;
119 UriManager.uri_of_string "cic:/matita/nat/times/times.con",121;
131 let sig_order_const t1 t2 =
133 let u1 = CicUtil.uri_of_term t1 in
134 let u2 = CicUtil.uri_of_term t2 in
135 let n1 = List.assoc u1 number in
136 let n2 = List.assoc u2 number in
138 else if n1 > n2 then Gt
141 prerr_endline ("t1 = "^(CicPp.ppterm t1));
142 prerr_endline ("t2 = "^(CicPp.ppterm t2));
147 | Not_found -> Incomparable
149 let sig_order t1 t2 =
151 Cic.Rel n, Cic.Rel m when n < m -> Gt (* inverted order *)
152 | Cic.Rel n, Cic.Rel m when n = m -> Incomparable
153 | Cic.Rel n, Cic.Rel m when n > m -> Lt
156 | _,_ -> sig_order_const t1 t2
158 let rec rpo_lt t1 t2 =
159 let module C = Cic in
162 C.Meta (_, _), C.Meta (_,_) -> false
163 | C.Meta (_,_) , t2 -> TermSet.mem t1 (metas_of_term t2)
164 | t1, C.Meta (_,_) -> false
165 | C.Appl [h1;a1],C.Appl [h2;a2] when h1=h2 ->
167 | C.Appl (h1::arg1),C.Appl (h2::arg2) when h1=h2 ->
168 if lex_lt arg1 arg2 then
171 | C.Appl (h1::arg1),C.Appl (h2::arg2) ->
172 (match sig_order h1 h2 with
173 | Lt -> check_lt arg1 t2
175 | C.Appl (h1::arg1), t2 when atomic t2 ->
176 (match sig_order h1 t2 with
177 | Lt -> check_lt arg1 t2
179 | t1 , C.Appl (h2::arg2) when atomic t1 ->
180 (match sig_order t1 h2 with
183 | C.Appl [] , _ -> assert false
184 | _ , C.Appl [] -> assert false
185 | t1, t2 when (atomic t1 && atomic t2 && t1<>t2) ->
186 (match sig_order t1 t2 with
191 if first_trie then true else
194 List.exists (fun a -> t1 = a || rpo_lt t1 a) args
200 | [],_ -> assert false
201 | _, [] -> assert false
202 | a1::l1, a2::l2 when a1 = a2 -> lex_lt l1 l2
203 | a1::_, a2::_ -> rpo_lt a1 a2
207 (fun b a -> b && (rpo_lt a t))
212 if rpo_lt t2 t1 then Gt
213 else if rpo_lt t1 t2 then Lt
217 (*********************** fine rpo *****************************)
219 (* (weight of constants, [(meta, weight_of_meta)]) *)
220 type weight = int * (int * int) list;;
222 let string_of_weight (cw, mw) =
225 (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
227 Printf.sprintf "[%d; %s]" cw s
230 let weight_of_term ?(consider_metas=true) term =
231 let module C = Cic in
232 let vars_dict = Hashtbl.create 5 in
233 let rec aux = function
234 | C.Meta (metano, _) when consider_metas ->
236 let oldw = Hashtbl.find vars_dict metano in
237 Hashtbl.replace vars_dict metano (oldw+1)
239 Hashtbl.add vars_dict metano 1);
241 | C.Meta _ -> 0 (* "variables" are lighter than constants and functions...*)
245 | C.MutInd (_, _, ens)
246 | C.MutConstruct (_, _, _, ens) ->
247 List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
250 | C.Lambda (_, t1, t2)
252 | C.LetIn (_, t1, t2) ->
257 | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
259 | C.MutCase (_, _, outt, t, pl) ->
262 let w3 = List.fold_left (+) 0 (List.map aux pl) in
266 List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
269 List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
275 Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
278 | (m1, _), (m2, _) -> m2 - m1
280 (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
284 module OrderedInt = struct
287 let compare = Pervasives.compare
290 module IntSet = Set.Make(OrderedInt)
292 let compute_equality_weight ty left right =
293 let metasw = ref 0 in
295 let w, m = (weight_of_term ~consider_metas:true t) in
296 metasw := !metasw + (2 * (List.length m));
299 (* Warning: the following let cannot be expanded since it forces the
300 right evaluation order!!!! *)
301 let w = (weight_of ty) + (weight_of left) + (weight_of right) in
306 (* returns a "normalized" version of the polynomial weight wl (with type
307 * weight list), i.e. a list sorted ascending by meta number,
308 * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
309 * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
310 * (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
311 let normalize_weight maxmeta (cw, wl) =
312 let rec aux = function
314 | m -> (m, 0)::(aux (m-1))
316 let tmpl = aux maxmeta in
319 (fun (m, _) (n, _) -> Pervasives.compare m n)
321 (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
327 let normalize_weights (cw1, wl1) (cw2, wl2) =
328 let rec aux wl1 wl2 =
331 | (m, w)::tl1, (n, w')::tl2 when m = n ->
332 let res1, res2 = aux tl1 tl2 in
333 (m, w)::res1, (n, w')::res2
334 | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
335 let res1, res2 = aux tl1 wl2 in
336 (m, w)::res1, (m, 0)::res2
337 | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
338 let res1, res2 = aux wl1 tl2 in
339 (n, 0)::res1, (n, w')::res2
341 let res1, res2 = aux [] tl2 in
342 (n, 0)::res1, (n, w)::res2
344 let res1, res2 = aux tl1 [] in
345 (m, w)::res1, (m, 0)::res2
346 | _, _ -> assert false
348 let cmp (m, _) (n, _) = compare m n in
349 let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
350 (cw1, wl1), (cw2, wl2)
354 let compare_weights ?(normalize=false)
355 ((h1, w1) as weight1) ((h2, w2) as weight2)=
356 let (h1, w1), (h2, w2) =
358 normalize_weights weight1 weight2
365 (fun ((lt, eq, gt), diffs) w1 w2 ->
367 | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
368 let diffs = (w1 - w2) + diffs in
369 let r = compare w1 w2 in
370 if r < 0 then (lt+1, eq, gt), diffs
371 else if r = 0 then (lt, eq+1, gt), diffs
372 else (lt, eq, gt+1), diffs
373 | (meta1, w1), (meta2, w2) ->
376 (Printf.sprintf "HMMM!!!! %s, %s\n"
377 (string_of_weight weight1) (string_of_weight weight2)));
380 with Invalid_argument _ ->
383 (Printf.sprintf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
384 (string_of_weight (h1, w1)) (string_of_weight weight1)
385 (string_of_weight (h2, w2)) (string_of_weight weight2)
386 (string_of_bool normalize)));
389 let hdiff = h1 - h2 in
393 else if hdiff > 0 then Gt
394 else Eq (* Incomparable *)
396 if hdiff <= 0 then Lt
397 else if (- diffs) >= hdiff then Le else Incomparable
399 if hdiff >= 0 then Gt
400 else if diffs >= (- hdiff) then Ge else Incomparable
401 | (m, _, n) when m > 0 && n > 0 ->
408 let rec aux_ordering ?(recursion=true) t1 t2 =
409 let module C = Cic in
410 let compare_uris u1 u2 =
412 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
414 else if res = 0 then Eq
419 | _, C.Meta _ -> Incomparable
421 | t1, t2 when t1 = t2 -> Eq
423 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
427 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
431 | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
432 | C.MutInd _, _ -> Lt
433 | _, C.MutInd _ -> Gt
435 | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
437 | C.MutConstruct _, _ -> Lt
438 | _, C.MutConstruct _ -> Gt
440 | C.Appl l1, C.Appl l2 when recursion ->
446 | hd1::tl1, hd2::tl2 ->
447 let o = aux_ordering hd1 hd2 in
448 if o = Eq then cmp tl1 tl2
452 | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
458 (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
459 (CicPp.ppterm t1) (CicPp.ppterm t2)));
464 (* w1, w2 are the weights, they should already be normalized... *)
465 let nonrec_kbo_w (t1, w1) (t2, w2) =
466 match compare_weights w1 w2 with
467 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
468 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
469 | Eq -> aux_ordering t1 t2
474 let nonrec_kbo t1 t2 =
475 let w1 = weight_of_term t1 in
476 let w2 = weight_of_term t2 in
478 prerr_endline ("weight1 :"^(string_of_weight w1));
479 prerr_endline ("weight2 :"^(string_of_weight w2));
481 match compare_weights ~normalize:true w1 w2 with
482 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
483 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
484 | Eq -> aux_ordering t1 t2
490 let aux = aux_ordering ~recursion:false in
491 let w1 = weight_of_term t1
492 and w2 = weight_of_term t2 in
498 | hd1::tl1, hd2::tl2 ->
502 if o = Eq then cmp tl1 tl2
505 let comparison = compare_weights ~normalize:true w1 w2 in
506 match comparison with
510 else if r = Eq then (
512 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
513 if cmp tl1 tl2 = Lt then Lt else Incomparable
514 | _, _ -> Incomparable
519 else if r = Eq then (
521 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
522 if cmp tl1 tl2 = Gt then Gt else Incomparable
523 | _, _ -> Incomparable
529 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
531 | _, _ -> Incomparable
539 Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
540 | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) ->
543 let aux = aux_ordering ~recursion:false in
544 let w1 = weight_of_term t1
545 and w2 = weight_of_term t2 in
551 | hd1::tl1, hd2::tl2 ->
555 if o = Eq then cmp tl1 tl2
558 match get_hd t1, get_hd t2 with
562 let comparison = compare_weights ~normalize:true w1 w2 in
563 match comparison with
567 else if r = Eq then (
569 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
570 if cmp tl1 tl2 = Lt then Lt else Incomparable
571 | _, _ -> Incomparable
576 else if r = Eq then (
578 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
579 if cmp tl1 tl2 = Gt then Gt else Incomparable
580 | _, _ -> Incomparable
586 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
588 | _, _ -> Incomparable
593 let names_of_context context =
597 | Some (n, e) -> Some n)
603 let module C = Cic in
605 | t1, t2 when t1 = t2 -> Eq
606 | t1, (C.Meta _ as m) ->
607 if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
608 | (C.Meta _ as m), t2 ->
609 if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
610 | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
618 let res1 = List.fold_left (f t2) false tl1 in
620 else let res2 = List.fold_left (f t1) false tl2 in
624 if res <> Incomparable then
628 if not r then false else
633 match aux_ordering hd1 hd2 with
635 let res = List.fold_left (f t1) false tl2 in
639 let res = List.fold_left (f t2) false tl1 in
646 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
648 with Invalid_argument _ ->
653 if List.fold_left (f t1) false tl2 then Gt
656 if List.fold_left (f t2) false tl1 then Lt
662 | t1, t2 -> aux_ordering t1 t2
666 (* settable by the user... *)
667 (* let compare_terms = ref nonrec_kbo;; *)
668 (* let compare_terms = ref ao;; *)
669 let compare_terms = ref rpo;;
671 let guarded_simpl ?(debug=false) context t =
672 let t' = ProofEngineReduction.simpl context t in
673 if t = t' then t else
675 let simpl_order = !compare_terms t t' in
677 prerr_endline ("comparing "^(CicPp.ppterm t)^(CicPp.ppterm t'));
678 if simpl_order = Gt then (if debug then prerr_endline "GT";t')
679 else (if debug then prerr_endline "NO_GT";t)
683 type equality_sign = Negative | Positive;;
685 let string_of_sign = function
686 | Negative -> "Negative"
687 | Positive -> "Positive"
691 type pos = Left | Right
693 let string_of_pos = function
699 let eq_ind_URI () = LibraryObjects.eq_ind_URI ~eq:(LibraryObjects.eq_URI ())
700 let eq_ind_r_URI () = LibraryObjects.eq_ind_r_URI ~eq:(LibraryObjects.eq_URI ())
701 let sym_eq_URI () = LibraryObjects.sym_eq_URI ~eq:(LibraryObjects.eq_URI ())
703 let s = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
704 UriManager.uri_of_string (s ^ "#xpointer(1/1/1)")
705 let trans_eq_URI () = LibraryObjects.trans_eq_URI ~eq:(LibraryObjects.eq_URI ())