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
102 TermSet.union (aux s) (aux t)
104 TermSet.union (aux s) (aux t)
105 | C.LetIn(n,s,ty,t) ->
106 TermSet.union (aux s) (TermSet.union (aux ty) (aux t))
107 | t -> TermSet.empty (* TODO: maybe add other cases? *)
112 let rec remove_local_context =
114 | Cic.Meta (i,_) -> Cic.Meta (i,[])
116 Cic.Appl(List.map remove_local_context l)
120 (************************* rpo ********************************)
122 UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1)",3;
123 UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1/1)",6;
124 UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1/2)",9;
125 HelmLibraryObjects.Peano.pred_URI, 12;
126 HelmLibraryObjects.Peano.plus_URI, 15;
127 HelmLibraryObjects.Peano.minus_URI, 18;
128 HelmLibraryObjects.Peano.mult_URI, 21;
129 UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind#xpointer(1/1)",103;
130 UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind#xpointer(1/1/1)",106;
131 UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind#xpointer(1/1/2)",109;
132 UriManager.uri_of_string "cic:/matita/nat/nat/pred.con",112;
133 UriManager.uri_of_string "cic:/matita/nat/plus/plus.con",115;
134 UriManager.uri_of_string "cic:/matita/nat/minus/minus.con",118;
135 UriManager.uri_of_string "cic:/matita/nat/times/times.con",121;
147 let sig_order_const t1 t2 =
149 let u1 = CicUtil.uri_of_term t1 in
150 let u2 = CicUtil.uri_of_term t2 in
151 let n1 = List.assoc u1 number in
152 let n2 = List.assoc u2 number in
154 else if n1 > n2 then Gt
157 prerr_endline ("t1 = "^(CicPp.ppterm t1));
158 prerr_endline ("t2 = "^(CicPp.ppterm t2));
163 | Not_found -> Incomparable
165 let sig_order t1 t2 =
167 Cic.Rel n, Cic.Rel m when n < m -> Gt (* inverted order *)
168 | Cic.Rel n, Cic.Rel m when n = m -> Incomparable
169 | Cic.Rel n, Cic.Rel m when n > m -> Lt
172 | _,_ -> sig_order_const t1 t2
174 let rec rpo_lt t1 t2 =
175 let module C = Cic in
178 C.Meta (_, _), C.Meta (_,_) -> false
179 | C.Meta (_,_) , t2 -> TermSet.mem t1 (metas_of_term t2)
180 | t1, C.Meta (_,_) -> false
181 | C.Appl [h1;a1],C.Appl [h2;a2] when h1=h2 ->
183 | C.Appl (h1::arg1),C.Appl (h2::arg2) when h1=h2 ->
184 if lex_lt arg1 arg2 then
187 | C.Appl (h1::arg1),C.Appl (h2::arg2) ->
188 (match sig_order h1 h2 with
189 | Lt -> check_lt arg1 t2
191 | C.Appl (h1::arg1), t2 when atomic t2 ->
192 (match sig_order h1 t2 with
193 | Lt -> check_lt arg1 t2
195 | t1 , C.Appl (h2::arg2) when atomic t1 ->
196 (match sig_order t1 h2 with
199 | C.Appl [] , _ -> assert false
200 | _ , C.Appl [] -> assert false
201 | t1, t2 when (atomic t1 && atomic t2 && t1<>t2) ->
202 (match sig_order t1 t2 with
207 if first_trie then true else
210 List.exists (fun a -> t1 = a || rpo_lt t1 a) args
216 | [],_ -> assert false
217 | _, [] -> assert false
218 | a1::l1, a2::l2 when a1 = a2 -> lex_lt l1 l2
219 | a1::_, a2::_ -> rpo_lt a1 a2
223 (fun b a -> b && (rpo_lt a t))
228 if rpo_lt t2 t1 then Gt
229 else if rpo_lt t1 t2 then Lt
233 (*********************** fine rpo *****************************)
235 (* (weight of constants, [(meta, weight_of_meta)]) *)
236 type weight = int * (int * int) list;;
238 let string_of_weight (cw, mw) =
241 (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
243 Printf.sprintf "[%d; %s]" cw s
246 let weight_of_term ?(consider_metas=true) ?(count_metas_occurrences=false) term =
247 let module C = Cic in
248 let vars_dict = Hashtbl.create 5 in
249 let rec aux = function
250 | C.Meta (metano, _) when consider_metas ->
252 let oldw = Hashtbl.find vars_dict metano in
253 Hashtbl.replace vars_dict metano (oldw+1)
255 Hashtbl.add vars_dict metano 1);
256 if count_metas_occurrences then 1 else 0
257 | C.Meta _ -> (* "variables" are lighter than constants and functions...*)
258 if count_metas_occurrences then 1 else 0
261 | C.MutInd (_, _, ens)
262 | C.MutConstruct (_, _, _, ens) ->
263 List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
266 | C.Lambda (_, t1, t2)
268 | C.LetIn (_, t1, _, t2) ->
273 | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
275 | C.MutCase (_, _, outt, t, pl) ->
278 let w3 = List.fold_left (+) 0 (List.map aux pl) in
282 List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
285 List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
291 Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
294 | (m1, _), (m2, _) -> m2 - m1
296 (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
300 module OrderedInt = struct
303 let compare = Pervasives.compare
306 module IntSet = Set.Make(OrderedInt)
308 let goal_symbols = ref TermSet.empty
311 TermMap.fold (fun k _ s -> TermSet.add k s) m TermSet.empty
314 let set_goal_symbols term =
315 let m = symbols_of_term term in
316 goal_symbols := (set_of_map m)
319 let symbols_of_eq (ty,left,right,_) =
320 let sty = set_of_map (symbols_of_term ty) in
321 let sl = set_of_map (symbols_of_term left) in
322 let sr = set_of_map (symbols_of_term right) in
323 TermSet.union sty (TermSet.union sl sr)
326 let distance sgoal seq =
327 let s = TermSet.diff seq sgoal in
331 let compute_equality_weight (ty,left,right,o) =
335 let w, m = (weight_of_term
336 ~consider_metas:true ~count_metas_occurrences:false right) in
337 w + (factor * (List.length m)) ;
340 let w, m = (weight_of_term
341 ~consider_metas:true ~count_metas_occurrences:false left) in
342 w + (factor * (List.length m)) ;
346 let w1, m1 = (weight_of_term
347 ~consider_metas:true ~count_metas_occurrences:false right) in
348 let w2, m2 = (weight_of_term
349 ~consider_metas:true ~count_metas_occurrences:false left) in
350 w1 + w2 + (factor * (List.length m1)) + (factor * (List.length m2))
353 let compute_equality_weight e =
354 let w = compute_equality_weight e in
355 let d = 0 in (* distance !goal_symbols (symbols_of_eq e) in *)
357 prerr_endline (Printf.sprintf "dist %s --- %s === %d"
358 (String.concat ", " (List.map (CicPp.ppterm) (TermSet.elements
360 (String.concat ", " (List.map (CicPp.ppterm) (TermSet.elements
369 let compute_equality_weight (ty,left,right,o) =
370 let metasw = ref 0 in
372 let w, m = (weight_of_term
373 ~consider_metas:true ~count_metas_occurrences:false t) in
374 metasw := !metasw + (1 * (List.length m)) ;
377 (* Warning: the following let cannot be expanded since it forces the
378 right evaluation order!!!! *)
379 let w = (weight_of ty) + (weight_of left) + (weight_of right) in
380 (* let w = weight_of (Cic.Appl [ty;left;right]) in *)
385 (* returns a "normalized" version of the polynomial weight wl (with type
386 * weight list), i.e. a list sorted ascending by meta number,
387 * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
388 * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
389 * (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
390 let normalize_weight maxmeta (cw, wl) =
391 let rec aux = function
393 | m -> (m, 0)::(aux (m-1))
395 let tmpl = aux maxmeta in
398 (fun (m, _) (n, _) -> Pervasives.compare m n)
400 (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
406 let normalize_weights (cw1, wl1) (cw2, wl2) =
407 let rec aux wl1 wl2 =
410 | (m, w)::tl1, (n, w')::tl2 when m = n ->
411 let res1, res2 = aux tl1 tl2 in
412 (m, w)::res1, (n, w')::res2
413 | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
414 let res1, res2 = aux tl1 wl2 in
415 (m, w)::res1, (m, 0)::res2
416 | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
417 let res1, res2 = aux wl1 tl2 in
418 (n, 0)::res1, (n, w')::res2
420 let res1, res2 = aux [] tl2 in
421 (n, 0)::res1, (n, w)::res2
423 let res1, res2 = aux tl1 [] in
424 (m, w)::res1, (m, 0)::res2
425 | _, _ -> assert false
427 let cmp (m, _) (n, _) = compare m n in
428 let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
429 (cw1, wl1), (cw2, wl2)
433 let compare_weights ?(normalize=false)
434 ((h1, w1) as weight1) ((h2, w2) as weight2)=
435 let (h1, w1), (h2, w2) =
437 normalize_weights weight1 weight2
444 (fun ((lt, eq, gt), diffs) w1 w2 ->
446 | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
447 let diffs = (w1 - w2) + diffs in
448 let r = compare w1 w2 in
449 if r < 0 then (lt+1, eq, gt), diffs
450 else if r = 0 then (lt, eq+1, gt), diffs
451 else (lt, eq, gt+1), diffs
452 | (meta1, w1), (meta2, w2) ->
455 (Printf.sprintf "HMMM!!!! %s, %s\n"
456 (string_of_weight weight1) (string_of_weight weight2)));
459 with Invalid_argument _ ->
462 (Printf.sprintf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
463 (string_of_weight (h1, w1)) (string_of_weight weight1)
464 (string_of_weight (h2, w2)) (string_of_weight weight2)
465 (string_of_bool normalize)));
468 let hdiff = h1 - h2 in
472 else if hdiff > 0 then Gt
473 else Eq (* Incomparable *)
475 if hdiff <= 0 then Lt
476 else if (- diffs) >= hdiff then Le else Incomparable
478 if hdiff >= 0 then Gt
479 else if diffs >= (- hdiff) then Ge else Incomparable
480 | (m, _, n) when m > 0 && n > 0 ->
486 let rec aux_ordering ?(recursion=true) t1 t2 =
487 let module C = Cic in
488 let compare_uris u1 u2 =
490 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
492 else if res = 0 then Eq
497 | _, C.Meta _ -> Incomparable
499 | t1, t2 when t1 = t2 -> Eq
501 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
505 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
509 | C.MutInd (u1, tno1, _), C.MutInd (u2, tno2, _) ->
510 let res = compare_uris u1 u2 in
511 if res <> Eq then res
513 let res = compare tno1 tno2 in
514 if res = 0 then Eq else if res < 0 then Lt else Gt
515 | C.MutInd _, _ -> Lt
516 | _, C.MutInd _ -> Gt
518 | C.MutConstruct (u1, tno1, cno1, _), C.MutConstruct (u2, tno2, cno2, _) ->
519 let res = compare_uris u1 u2 in
520 if res <> Eq then res
522 let res = compare (tno1,cno1) (tno2,cno2) in
523 if res = 0 then Eq else if res < 0 then Lt else Gt
524 | C.MutConstruct _, _ -> Lt
525 | _, C.MutConstruct _ -> Gt
527 | C.Appl l1, C.Appl l2 when recursion ->
533 | hd1::tl1, hd2::tl2 ->
534 let o = aux_ordering hd1 hd2 in
535 if o = Eq then cmp tl1 tl2
539 | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
545 (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
546 (CicPp.ppterm t1) (CicPp.ppterm t2)));
551 (* w1, w2 are the weights, they should already be normalized... *)
552 let nonrec_kbo_w (t1, w1) (t2, w2) =
553 match compare_weights w1 w2 with
554 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
555 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
556 | Eq -> aux_ordering t1 t2
561 let nonrec_kbo t1 t2 =
562 let w1 = weight_of_term t1 in
563 let w2 = weight_of_term t2 in
565 prerr_endline ("weight1 :"^(string_of_weight w1));
566 prerr_endline ("weight2 :"^(string_of_weight w2));
568 match compare_weights ~normalize:true w1 w2 with
569 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
570 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
571 | Eq -> aux_ordering t1 t2
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 let comparison = compare_weights ~normalize:true w1 w2 in
593 match comparison with
597 else if r = Eq then (
599 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
600 if cmp tl1 tl2 = Lt then Lt else Incomparable
601 | _, _ -> Incomparable
606 else if r = Eq then (
608 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
609 if cmp tl1 tl2 = Gt then Gt else Incomparable
610 | _, _ -> Incomparable
616 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
618 | _, _ -> Incomparable
626 Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
627 | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) ->
630 let aux = aux_ordering ~recursion:false in
631 let w1 = weight_of_term t1
632 and w2 = weight_of_term t2 in
638 | hd1::tl1, hd2::tl2 ->
642 if o = Eq then cmp tl1 tl2
645 match get_hd t1, get_hd t2 with
649 let comparison = compare_weights ~normalize:true w1 w2 in
650 match comparison with
654 else if r = Eq then (
656 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
657 if cmp tl1 tl2 = Lt then Lt else Incomparable
658 | _, _ -> Incomparable
663 else if r = Eq then (
665 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
666 if cmp tl1 tl2 = Gt then Gt else Incomparable
667 | _, _ -> Incomparable
673 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
675 | _, _ -> Incomparable
680 let names_of_context context =
684 | Some (n, e) -> Some n)
690 let module C = Cic in
692 | t1, t2 when t1 = t2 -> Eq
693 | t1, (C.Meta _ as m) ->
694 if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
695 | (C.Meta _ as m), t2 ->
696 if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
697 | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
705 let res1 = List.fold_left (f t2) false tl1 in
707 else let res2 = List.fold_left (f t1) false tl2 in
711 if res <> Incomparable then
715 if not r then false else
720 match aux_ordering hd1 hd2 with
722 let res = List.fold_left (f t1) false tl2 in
726 let res = List.fold_left (f t2) false tl1 in
733 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
735 with Invalid_argument _ ->
740 if List.fold_left (f t1) false tl2 then Gt
743 if List.fold_left (f t2) false tl1 then Lt
749 | t1, t2 -> aux_ordering t1 t2
753 (* settable by the user... *)
754 let compare_terms = ref nonrec_kbo;;
755 (* let compare_terms = ref ao;; *)
756 (* let compare_terms = ref rpo;; *)
758 let guarded_simpl ?(debug=false) context t =
759 if !compare_terms == nonrec_kbo then t
761 let t' = ProofEngineReduction.simpl context t in
762 if t = t' then t else
764 let simpl_order = !compare_terms t t' in
765 debug_print (lazy ("comparing "^(CicPp.ppterm t)^(CicPp.ppterm t')));
766 if simpl_order = Gt then (if debug then prerr_endline "GT";t')
767 else (if debug then prerr_endline "NO_GT";t)
771 type pos = Left | Right
773 let string_of_pos = function
778 let metas_of_term t =
779 List.map fst (CicUtil.metas_of_term t)