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? *)
106 let rec remove_local_context =
108 | Cic.Meta (i,_) -> Cic.Meta (i,[])
110 Cic.Appl(List.map remove_local_context l)
114 (************************* rpo ********************************)
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;
141 let sig_order_const t1 t2 =
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
148 else if n1 > n2 then Gt
151 prerr_endline ("t1 = "^(CicPp.ppterm t1));
152 prerr_endline ("t2 = "^(CicPp.ppterm t2));
157 | Not_found -> Incomparable
159 let sig_order t1 t2 =
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
166 | _,_ -> sig_order_const t1 t2
168 let rec rpo_lt t1 t2 =
169 let module C = Cic in
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 ->
177 | C.Appl (h1::arg1),C.Appl (h2::arg2) when h1=h2 ->
178 if lex_lt arg1 arg2 then
181 | C.Appl (h1::arg1),C.Appl (h2::arg2) ->
182 (match sig_order h1 h2 with
183 | Lt -> check_lt arg1 t2
185 | C.Appl (h1::arg1), t2 when atomic t2 ->
186 (match sig_order h1 t2 with
187 | Lt -> check_lt arg1 t2
189 | t1 , C.Appl (h2::arg2) when atomic t1 ->
190 (match sig_order t1 h2 with
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
201 if first_trie then true else
204 List.exists (fun a -> t1 = a || rpo_lt t1 a) args
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
217 (fun b a -> b && (rpo_lt a t))
222 if rpo_lt t2 t1 then Gt
223 else if rpo_lt t1 t2 then Lt
227 (*********************** fine rpo *****************************)
229 (* (weight of constants, [(meta, weight_of_meta)]) *)
230 type weight = int * (int * int) list;;
232 let string_of_weight (cw, mw) =
235 (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
237 Printf.sprintf "[%d; %s]" cw s
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 ->
246 let oldw = Hashtbl.find vars_dict metano in
247 Hashtbl.replace vars_dict metano (oldw+1)
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
255 | C.MutInd (_, _, ens)
256 | C.MutConstruct (_, _, _, ens) ->
257 List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
260 | C.Lambda (_, t1, t2)
262 | C.LetIn (_, t1, t2) ->
267 | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
269 | C.MutCase (_, _, outt, t, pl) ->
272 let w3 = List.fold_left (+) 0 (List.map aux pl) in
276 List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
279 List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
285 Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
288 | (m1, _), (m2, _) -> m2 - m1
290 (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
294 module OrderedInt = struct
297 let compare = Pervasives.compare
300 module IntSet = Set.Make(OrderedInt)
302 let compute_equality_weight (ty,left,right,o) =
306 let w, m = (weight_of_term
307 ~consider_metas:true ~count_metas_occurrences:false right) in
308 w + (factor * (List.length m)) ;
311 let w, m = (weight_of_term
312 ~consider_metas:true ~count_metas_occurrences:false left) in
313 w + (factor * (List.length m)) ;
317 let w1, m1 = (weight_of_term
318 ~consider_metas:true ~count_metas_occurrences:false right) in
319 let w2, m2 = (weight_of_term
320 ~consider_metas:true ~count_metas_occurrences:false left) in
321 (max w1 w2)+(max (factor * (List.length m1)) (factor * (List.length m2)))
325 let compute_equality_weight (ty,left,right,o) =
326 let metasw = ref 0 in
328 let w, m = (weight_of_term
329 ~consider_metas:true ~count_metas_occurrences:false t) in
330 metasw := !metasw + (1 * (List.length m)) ;
333 (* Warning: the following let cannot be expanded since it forces the
334 right evaluation order!!!! *)
335 let w = (weight_of ty) + (weight_of left) + (weight_of right) in
336 (* let w = weight_of (Cic.Appl [ty;left;right]) in *)
341 (* returns a "normalized" version of the polynomial weight wl (with type
342 * weight list), i.e. a list sorted ascending by meta number,
343 * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
344 * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
345 * (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
346 let normalize_weight maxmeta (cw, wl) =
347 let rec aux = function
349 | m -> (m, 0)::(aux (m-1))
351 let tmpl = aux maxmeta in
354 (fun (m, _) (n, _) -> Pervasives.compare m n)
356 (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
362 let normalize_weights (cw1, wl1) (cw2, wl2) =
363 let rec aux wl1 wl2 =
366 | (m, w)::tl1, (n, w')::tl2 when m = n ->
367 let res1, res2 = aux tl1 tl2 in
368 (m, w)::res1, (n, w')::res2
369 | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
370 let res1, res2 = aux tl1 wl2 in
371 (m, w)::res1, (m, 0)::res2
372 | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
373 let res1, res2 = aux wl1 tl2 in
374 (n, 0)::res1, (n, w')::res2
376 let res1, res2 = aux [] tl2 in
377 (n, 0)::res1, (n, w)::res2
379 let res1, res2 = aux tl1 [] in
380 (m, w)::res1, (m, 0)::res2
381 | _, _ -> assert false
383 let cmp (m, _) (n, _) = compare m n in
384 let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
385 (cw1, wl1), (cw2, wl2)
389 let compare_weights ?(normalize=false)
390 ((h1, w1) as weight1) ((h2, w2) as weight2)=
391 let (h1, w1), (h2, w2) =
393 normalize_weights weight1 weight2
400 (fun ((lt, eq, gt), diffs) w1 w2 ->
402 | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
403 let diffs = (w1 - w2) + diffs in
404 let r = compare w1 w2 in
405 if r < 0 then (lt+1, eq, gt), diffs
406 else if r = 0 then (lt, eq+1, gt), diffs
407 else (lt, eq, gt+1), diffs
408 | (meta1, w1), (meta2, w2) ->
411 (Printf.sprintf "HMMM!!!! %s, %s\n"
412 (string_of_weight weight1) (string_of_weight weight2)));
415 with Invalid_argument _ ->
418 (Printf.sprintf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
419 (string_of_weight (h1, w1)) (string_of_weight weight1)
420 (string_of_weight (h2, w2)) (string_of_weight weight2)
421 (string_of_bool normalize)));
424 let hdiff = h1 - h2 in
428 else if hdiff > 0 then Gt
429 else Eq (* Incomparable *)
431 if hdiff <= 0 then Lt
432 else if (- diffs) >= hdiff then Le else Incomparable
434 if hdiff >= 0 then Gt
435 else if diffs >= (- hdiff) then Ge else Incomparable
436 | (m, _, n) when m > 0 && n > 0 ->
442 let rec aux_ordering ?(recursion=true) t1 t2 =
443 let module C = Cic in
444 let compare_uris u1 u2 =
446 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
448 else if res = 0 then Eq
453 | _, C.Meta _ -> Incomparable
455 | t1, t2 when t1 = t2 -> Eq
457 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
461 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
465 | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
466 | C.MutInd _, _ -> Lt
467 | _, C.MutInd _ -> Gt
469 | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
471 | C.MutConstruct _, _ -> Lt
472 | _, C.MutConstruct _ -> Gt
474 | C.Appl l1, C.Appl l2 when recursion ->
480 | hd1::tl1, hd2::tl2 ->
481 let o = aux_ordering hd1 hd2 in
482 if o = Eq then cmp tl1 tl2
486 | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
492 (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
493 (CicPp.ppterm t1) (CicPp.ppterm t2)));
498 (* w1, w2 are the weights, they should already be normalized... *)
499 let nonrec_kbo_w (t1, w1) (t2, w2) =
500 match compare_weights w1 w2 with
501 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
502 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
503 | Eq -> aux_ordering t1 t2
508 let nonrec_kbo t1 t2 =
509 let w1 = weight_of_term t1 in
510 let w2 = weight_of_term t2 in
512 prerr_endline ("weight1 :"^(string_of_weight w1));
513 prerr_endline ("weight2 :"^(string_of_weight w2));
515 match compare_weights ~normalize:true w1 w2 with
516 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
517 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
518 | Eq -> aux_ordering t1 t2
524 let aux = aux_ordering ~recursion:false in
525 let w1 = weight_of_term t1
526 and w2 = weight_of_term t2 in
532 | hd1::tl1, hd2::tl2 ->
536 if o = Eq then cmp tl1 tl2
539 let comparison = compare_weights ~normalize:true w1 w2 in
540 match comparison with
544 else if r = Eq then (
546 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
547 if cmp tl1 tl2 = Lt then Lt else Incomparable
548 | _, _ -> Incomparable
553 else if r = Eq then (
555 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
556 if cmp tl1 tl2 = Gt then Gt else Incomparable
557 | _, _ -> Incomparable
563 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
565 | _, _ -> Incomparable
573 Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
574 | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) ->
577 let aux = aux_ordering ~recursion:false in
578 let w1 = weight_of_term t1
579 and w2 = weight_of_term t2 in
585 | hd1::tl1, hd2::tl2 ->
589 if o = Eq then cmp tl1 tl2
592 match get_hd t1, get_hd t2 with
596 let comparison = compare_weights ~normalize:true w1 w2 in
597 match comparison with
601 else if r = Eq then (
603 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
604 if cmp tl1 tl2 = Lt then Lt else Incomparable
605 | _, _ -> Incomparable
610 else if r = Eq then (
612 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
613 if cmp tl1 tl2 = Gt then Gt else Incomparable
614 | _, _ -> Incomparable
620 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
622 | _, _ -> Incomparable
627 let names_of_context context =
631 | Some (n, e) -> Some n)
637 let module C = Cic in
639 | t1, t2 when t1 = t2 -> Eq
640 | t1, (C.Meta _ as m) ->
641 if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
642 | (C.Meta _ as m), t2 ->
643 if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
644 | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
652 let res1 = List.fold_left (f t2) false tl1 in
654 else let res2 = List.fold_left (f t1) false tl2 in
658 if res <> Incomparable then
662 if not r then false else
667 match aux_ordering hd1 hd2 with
669 let res = List.fold_left (f t1) false tl2 in
673 let res = List.fold_left (f t2) false tl1 in
680 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
682 with Invalid_argument _ ->
687 if List.fold_left (f t1) false tl2 then Gt
690 if List.fold_left (f t2) false tl1 then Lt
696 | t1, t2 -> aux_ordering t1 t2
700 (* settable by the user... *)
701 let compare_terms = ref nonrec_kbo;;
702 (* let compare_terms = ref ao;; *)
703 (* let compare_terms = ref rpo;; *)
705 let guarded_simpl ?(debug=false) context t =
706 if !compare_terms == nonrec_kbo then t
708 let t' = ProofEngineReduction.simpl context t in
709 if t = t' then t else
711 let simpl_order = !compare_terms t t' in
713 prerr_endline ("comparing "^(CicPp.ppterm t)^(CicPp.ppterm t'));
714 if simpl_order = Gt then (if debug then prerr_endline "GT";t')
715 else (if debug then prerr_endline "NO_GT";t)
719 type equality_sign = Negative | Positive;;
721 let string_of_sign = function
722 | Negative -> "Negative"
723 | Positive -> "Positive"
727 type pos = Left | Right
729 let string_of_pos = function
734 let metas_of_term t =
735 List.map fst (CicUtil.metas_of_term t)