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)
117 | Cic.Prod (n,s,t) ->
118 Cic.Prod (n,remove_local_context s, remove_local_context t)
122 (************************* rpo ********************************)
124 UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1)",3;
125 UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1/1)",6;
126 UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1/2)",9;
127 HelmLibraryObjects.Peano.pred_URI, 12;
128 HelmLibraryObjects.Peano.plus_URI, 15;
129 HelmLibraryObjects.Peano.minus_URI, 18;
130 HelmLibraryObjects.Peano.mult_URI, 21;
131 UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind#xpointer(1/1)",103;
132 UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind#xpointer(1/1/1)",106;
133 UriManager.uri_of_string "cic:/matita/nat/nat/nat.ind#xpointer(1/1/2)",109;
134 UriManager.uri_of_string "cic:/matita/nat/nat/pred.con",112;
135 UriManager.uri_of_string "cic:/matita/nat/plus/plus.con",115;
136 UriManager.uri_of_string "cic:/matita/nat/minus/minus.con",118;
137 UriManager.uri_of_string "cic:/matita/nat/times/times.con",121;
149 let sig_order_const t1 t2 =
151 let u1 = CicUtil.uri_of_term t1 in
152 let u2 = CicUtil.uri_of_term t2 in
153 let n1 = List.assoc u1 number in
154 let n2 = List.assoc u2 number in
156 else if n1 > n2 then Gt
159 prerr_endline ("t1 = "^(CicPp.ppterm t1));
160 prerr_endline ("t2 = "^(CicPp.ppterm t2));
165 | Not_found -> Incomparable
167 let sig_order t1 t2 =
169 Cic.Rel n, Cic.Rel m when n < m -> Gt (* inverted order *)
170 | Cic.Rel n, Cic.Rel m when n = m -> Incomparable
171 | Cic.Rel n, Cic.Rel m when n > m -> Lt
174 | _,_ -> sig_order_const t1 t2
176 let rec rpo_lt t1 t2 =
177 let module C = Cic in
180 C.Meta (_, _), C.Meta (_,_) -> false
181 | C.Meta (_,_) , t2 -> TermSet.mem t1 (metas_of_term t2)
182 | t1, C.Meta (_,_) -> false
183 | C.Appl [h1;a1],C.Appl [h2;a2] when h1=h2 ->
185 | C.Appl (h1::arg1),C.Appl (h2::arg2) when h1=h2 ->
186 if lex_lt arg1 arg2 then
189 | C.Appl (h1::arg1),C.Appl (h2::arg2) ->
190 (match sig_order h1 h2 with
191 | Lt -> check_lt arg1 t2
193 | C.Appl (h1::arg1), t2 when atomic t2 ->
194 (match sig_order h1 t2 with
195 | Lt -> check_lt arg1 t2
197 | t1 , C.Appl (h2::arg2) when atomic t1 ->
198 (match sig_order t1 h2 with
201 | C.Appl [] , _ -> assert false
202 | _ , C.Appl [] -> assert false
203 | t1, t2 when (atomic t1 && atomic t2 && t1<>t2) ->
204 (match sig_order t1 t2 with
209 if first_trie then true else
212 List.exists (fun a -> t1 = a || rpo_lt t1 a) args
218 | [],_ -> assert false
219 | _, [] -> assert false
220 | a1::l1, a2::l2 when a1 = a2 -> lex_lt l1 l2
221 | a1::_, a2::_ -> rpo_lt a1 a2
225 (fun b a -> b && (rpo_lt a t))
230 if rpo_lt t2 t1 then Gt
231 else if rpo_lt t1 t2 then Lt
235 (*********************** fine rpo *****************************)
237 (* (weight of constants, [(meta, weight_of_meta)]) *)
238 type weight = int * (int * int) list;;
240 let string_of_weight (cw, mw) =
243 (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
245 Printf.sprintf "[%d; %s]" cw s
248 let weight_of_term ?(consider_metas=true) ?(count_metas_occurrences=false) term =
249 let module C = Cic in
250 let vars_dict = Hashtbl.create 5 in
251 let rec aux = function
252 | C.Meta (metano, _) when consider_metas ->
254 let oldw = Hashtbl.find vars_dict metano in
255 Hashtbl.replace vars_dict metano (oldw+1)
257 Hashtbl.add vars_dict metano 1);
258 if count_metas_occurrences then 1 else 0
259 | C.Meta _ -> (* "variables" are lighter than constants and functions...*)
260 if count_metas_occurrences then 1 else 0
263 | C.MutInd (_, _, ens)
264 | C.MutConstruct (_, _, _, ens) ->
265 List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
268 | C.Lambda (_, t1, t2)
270 | C.LetIn (_, t1, _, t2) ->
275 | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
277 | C.MutCase (_, _, outt, t, pl) ->
280 let w3 = List.fold_left (+) 0 (List.map aux pl) in
284 List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
287 List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
293 Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
296 | (m1, _), (m2, _) -> m2 - m1
298 (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
302 module OrderedInt = struct
305 let compare = Pervasives.compare
308 module IntSet = Set.Make(OrderedInt)
310 let goal_symbols = ref TermSet.empty
313 TermMap.fold (fun k _ s -> TermSet.add k s) m TermSet.empty
316 let set_goal_symbols term =
317 let m = symbols_of_term term in
318 goal_symbols := (set_of_map m)
321 let symbols_of_eq (ty,left,right,_) =
322 let sty = set_of_map (symbols_of_term ty) in
323 let sl = set_of_map (symbols_of_term left) in
324 let sr = set_of_map (symbols_of_term right) in
325 TermSet.union sty (TermSet.union sl sr)
328 let distance sgoal seq =
329 let s = TermSet.diff seq sgoal in
333 let compute_equality_weight (ty,left,right,o) =
337 let w, m = (weight_of_term
338 ~consider_metas:true ~count_metas_occurrences:false right) in
339 w + (factor * (List.length m)) ;
342 let w, m = (weight_of_term
343 ~consider_metas:true ~count_metas_occurrences:false left) in
344 w + (factor * (List.length m)) ;
348 let w1, m1 = (weight_of_term
349 ~consider_metas:true ~count_metas_occurrences:false right) in
350 let w2, m2 = (weight_of_term
351 ~consider_metas:true ~count_metas_occurrences:false left) in
352 w1 + w2 + (factor * (List.length m1)) + (factor * (List.length m2))
355 let compute_equality_weight e =
356 let w = compute_equality_weight e in
357 let d = 0 in (* distance !goal_symbols (symbols_of_eq e) in *)
359 prerr_endline (Printf.sprintf "dist %s --- %s === %d"
360 (String.concat ", " (List.map (CicPp.ppterm) (TermSet.elements
362 (String.concat ", " (List.map (CicPp.ppterm) (TermSet.elements
371 let compute_equality_weight (ty,left,right,o) =
372 let metasw = ref 0 in
374 let w, m = (weight_of_term
375 ~consider_metas:true ~count_metas_occurrences:false t) in
376 metasw := !metasw + (1 * (List.length m)) ;
379 (* Warning: the following let cannot be expanded since it forces the
380 right evaluation order!!!! *)
381 let w = (weight_of ty) + (weight_of left) + (weight_of right) in
382 (* let w = weight_of (Cic.Appl [ty;left;right]) in *)
387 (* returns a "normalized" version of the polynomial weight wl (with type
388 * weight list), i.e. a list sorted ascending by meta number,
389 * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
390 * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
391 * (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
392 let normalize_weight maxmeta (cw, wl) =
393 let rec aux = function
395 | m -> (m, 0)::(aux (m-1))
397 let tmpl = aux maxmeta in
400 (fun (m, _) (n, _) -> Pervasives.compare m n)
402 (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
408 let normalize_weights (cw1, wl1) (cw2, wl2) =
409 let rec aux wl1 wl2 =
412 | (m, w)::tl1, (n, w')::tl2 when m = n ->
413 let res1, res2 = aux tl1 tl2 in
414 (m, w)::res1, (n, w')::res2
415 | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
416 let res1, res2 = aux tl1 wl2 in
417 (m, w)::res1, (m, 0)::res2
418 | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
419 let res1, res2 = aux wl1 tl2 in
420 (n, 0)::res1, (n, w')::res2
422 let res1, res2 = aux [] tl2 in
423 (n, 0)::res1, (n, w)::res2
425 let res1, res2 = aux tl1 [] in
426 (m, w)::res1, (m, 0)::res2
427 | _, _ -> assert false
429 let cmp (m, _) (n, _) = compare m n in
430 let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
431 (cw1, wl1), (cw2, wl2)
435 let compare_weights ?(normalize=false)
436 ((h1, w1) as weight1) ((h2, w2) as weight2)=
437 let (h1, w1), (h2, w2) =
439 normalize_weights weight1 weight2
446 (fun ((lt, eq, gt), diffs) w1 w2 ->
448 | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
449 let diffs = (w1 - w2) + diffs in
450 let r = compare w1 w2 in
451 if r < 0 then (lt+1, eq, gt), diffs
452 else if r = 0 then (lt, eq+1, gt), diffs
453 else (lt, eq, gt+1), diffs
454 | (meta1, w1), (meta2, w2) ->
457 (Printf.sprintf "HMMM!!!! %s, %s\n"
458 (string_of_weight weight1) (string_of_weight weight2)));
461 with Invalid_argument _ ->
464 (Printf.sprintf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
465 (string_of_weight (h1, w1)) (string_of_weight weight1)
466 (string_of_weight (h2, w2)) (string_of_weight weight2)
467 (string_of_bool normalize)));
470 let hdiff = h1 - h2 in
474 else if hdiff > 0 then Gt
475 else Eq (* Incomparable *)
477 if hdiff <= 0 then Lt
478 else if (- diffs) >= hdiff then Le else Incomparable
480 if hdiff >= 0 then Gt
481 else if diffs >= (- hdiff) then Ge else Incomparable
482 | (m, _, n) when m > 0 && n > 0 ->
488 let rec aux_ordering ?(recursion=true) t1 t2 =
489 let module C = Cic in
490 let compare_uris u1 u2 =
492 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
494 else if res = 0 then Eq
499 | _, C.Meta _ -> Incomparable
501 | t1, t2 when t1 = t2 -> Eq
503 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
507 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
511 | C.MutInd (u1, tno1, _), C.MutInd (u2, tno2, _) ->
512 let res = compare_uris u1 u2 in
513 if res <> Eq then res
515 let res = compare tno1 tno2 in
516 if res = 0 then Eq else if res < 0 then Lt else Gt
517 | C.MutInd _, _ -> Lt
518 | _, C.MutInd _ -> Gt
520 | C.MutConstruct (u1, tno1, cno1, _), C.MutConstruct (u2, tno2, cno2, _) ->
521 let res = compare_uris u1 u2 in
522 if res <> Eq then res
524 let res = compare (tno1,cno1) (tno2,cno2) in
525 if res = 0 then Eq else if res < 0 then Lt else Gt
526 | C.MutConstruct _, _ -> Lt
527 | _, C.MutConstruct _ -> Gt
529 | C.Appl l1, C.Appl l2 when recursion ->
535 | hd1::tl1, hd2::tl2 ->
536 let o = aux_ordering hd1 hd2 in
537 if o = Eq then cmp tl1 tl2
541 | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
547 (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
548 (CicPp.ppterm t1) (CicPp.ppterm t2)));
553 (* w1, w2 are the weights, they should already be normalized... *)
554 let nonrec_kbo_w (t1, w1) (t2, w2) =
555 match compare_weights w1 w2 with
556 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
557 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
558 | Eq -> aux_ordering t1 t2
563 let nonrec_kbo t1 t2 =
564 let w1 = weight_of_term t1 in
565 let w2 = weight_of_term t2 in
567 prerr_endline ("weight1 :"^(string_of_weight w1));
568 prerr_endline ("weight2 :"^(string_of_weight w2));
570 match compare_weights ~normalize:true w1 w2 with
571 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
572 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
573 | Eq -> aux_ordering t1 t2
579 let aux = aux_ordering ~recursion:false in
580 let w1 = weight_of_term t1
581 and w2 = weight_of_term t2 in
587 | hd1::tl1, hd2::tl2 ->
591 if o = Eq then cmp tl1 tl2
594 let comparison = compare_weights ~normalize:true w1 w2 in
595 match comparison with
599 else if r = Eq then (
601 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
602 if cmp tl1 tl2 = Lt then Lt else Incomparable
603 | _, _ -> Incomparable
608 else if r = Eq then (
610 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
611 if cmp tl1 tl2 = Gt then Gt else Incomparable
612 | _, _ -> Incomparable
618 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
620 | _, _ -> Incomparable
628 Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
629 | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) ->
632 let aux = aux_ordering ~recursion:false in
633 let w1 = weight_of_term t1
634 and w2 = weight_of_term t2 in
640 | hd1::tl1, hd2::tl2 ->
644 if o = Eq then cmp tl1 tl2
647 match get_hd t1, get_hd t2 with
651 let comparison = compare_weights ~normalize:true w1 w2 in
652 match comparison with
656 else if r = Eq then (
658 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
659 if cmp tl1 tl2 = Lt then Lt else Incomparable
660 | _, _ -> Incomparable
665 else if r = Eq then (
667 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
668 if cmp tl1 tl2 = Gt then Gt else Incomparable
669 | _, _ -> Incomparable
675 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
677 | _, _ -> Incomparable
682 let names_of_context context =
686 | Some (n, e) -> Some n)
692 let module C = Cic in
694 | t1, t2 when t1 = t2 -> Eq
695 | t1, (C.Meta _ as m) ->
696 if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
697 | (C.Meta _ as m), t2 ->
698 if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
699 | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
707 let res1 = List.fold_left (f t2) false tl1 in
709 else let res2 = List.fold_left (f t1) false tl2 in
713 if res <> Incomparable then
717 if not r then false else
722 match aux_ordering hd1 hd2 with
724 let res = List.fold_left (f t1) false tl2 in
728 let res = List.fold_left (f t2) false tl1 in
735 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
737 with Invalid_argument _ ->
742 if List.fold_left (f t1) false tl2 then Gt
745 if List.fold_left (f t2) false tl1 then Lt
751 | t1, t2 -> aux_ordering t1 t2
755 (* settable by the user... *)
756 let compare_terms = ref nonrec_kbo;;
757 (* let compare_terms = ref ao;; *)
758 (* let compare_terms = ref rpo;; *)
760 let guarded_simpl ?(debug=false) context t =
761 if !compare_terms == nonrec_kbo then t
763 let t' = ProofEngineReduction.simpl context t in
764 if t = t' then t else
766 let simpl_order = !compare_terms t t' in
767 debug_print (lazy ("comparing "^(CicPp.ppterm t)^(CicPp.ppterm t')));
768 if simpl_order = Gt then (if debug then prerr_endline "GT";t')
769 else (if debug then prerr_endline "NO_GT";t)
773 type pos = Left | Right
775 let string_of_pos = function
780 let metas_of_term t =
781 List.map fst (CicUtil.metas_of_term t)