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
120 | Cic.MutConstruct _ -> true
123 let sig_order t1 t2 =
125 let u1 = CicUtil.uri_of_term t1 in
126 let u2 = CicUtil.uri_of_term t2 in
127 let n1 = List.assoc u1 number in
128 let n2 = List.assoc u2 number in
130 else if n1 > n2 then Gt
133 prerr_endline ("t1 = "^(CicPp.ppterm t1));
134 prerr_endline ("t2 = "^(CicPp.ppterm t2));
139 | Not_found -> Incomparable
142 let module C = Cic in
144 C.Meta (_, _), C.Meta (_,_) -> Incomparable
145 | C.Meta (_,_) as t1,t2 when TermSet.mem t1 (metas_of_term t2)
147 | t1, (C.Meta (_,_) as t2) when TermSet.mem t2 (metas_of_term t1)
149 | C.Appl (h1::arg1),C.Appl (h2::arg2) when h1=h2 ->
150 (match lex arg1 arg2 with
151 | Lt when (check Gt t2 arg1) -> Lt
152 | Gt when (check Gt t1 arg2) -> Gt
153 | _ -> Incomparable )
154 | C.Appl (h1::arg1),C.Appl (h2::arg2) ->
155 (match sig_order h1 h2 with
156 | Lt when (check Gt t2 arg1) -> Lt
157 | Gt when (check Gt t1 arg2) -> Gt
158 | _ -> Incomparable )
159 | C.Appl (h1::arg1), t2 when atomic t2 ->
160 (match sig_order h1 t2 with
161 | Lt when (check Gt t2 arg1) -> Lt
163 | _ -> Incomparable )
164 | t1 , C.Appl (h2::arg2) when atomic t1 ->
165 (match sig_order t1 h2 with
167 | Gt when (check Gt t1 arg2) -> Gt
168 | _ -> Incomparable )
169 | C.Appl [] , _ -> assert false
170 | _ , C.Appl [] -> assert false
171 | _,_ -> Incomparable
175 [],[] -> Incomparable
176 | [],_ -> assert false
177 | _, [] -> assert false
178 | a1::l1, a2::l2 when a1 = a2 -> lex l1 l2
179 | a1::_, a2::_ -> rpo a1 a2
183 (fun b a -> b && (rpo t a = o))
188 (*********************** fine rpo *****************************)
190 (* (weight of constants, [(meta, weight_of_meta)]) *)
191 type weight = int * (int * int) list;;
193 let string_of_weight (cw, mw) =
196 (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
198 Printf.sprintf "[%d; %s]" cw s
201 let weight_of_term ?(consider_metas=true) term =
202 let module C = Cic in
203 let vars_dict = Hashtbl.create 5 in
204 let rec aux = function
205 | C.Meta (metano, _) when consider_metas ->
207 let oldw = Hashtbl.find vars_dict metano in
208 Hashtbl.replace vars_dict metano (oldw+1)
210 Hashtbl.add vars_dict metano 1);
212 | C.Meta _ -> 0 (* "variables" are lighter than constants and functions...*)
216 | C.MutInd (_, _, ens)
217 | C.MutConstruct (_, _, _, ens) ->
218 List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
221 | C.Lambda (_, t1, t2)
223 | C.LetIn (_, t1, t2) ->
228 | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
230 | C.MutCase (_, _, outt, t, pl) ->
233 let w3 = List.fold_left (+) 0 (List.map aux pl) in
237 List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
240 List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
246 Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
249 | (m1, _), (m2, _) -> m2 - m1
251 (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
255 module OrderedInt = struct
258 let compare = Pervasives.compare
261 module IntSet = Set.Make(OrderedInt)
263 let compute_equality_weight ty left right =
264 let metasw = ref 0 in
266 let w, m = (weight_of_term ~consider_metas:true t) in
267 metasw := !metasw + (2 * (List.length m));
270 (* Warning: the following let cannot be expanded since it forces the
271 right evaluation order!!!! *)
272 let w = (weight_of ty) + (weight_of left) + (weight_of right) in
277 (* returns a "normalized" version of the polynomial weight wl (with type
278 * weight list), i.e. a list sorted ascending by meta number,
279 * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
280 * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
281 * (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
282 let normalize_weight maxmeta (cw, wl) =
283 let rec aux = function
285 | m -> (m, 0)::(aux (m-1))
287 let tmpl = aux maxmeta in
290 (fun (m, _) (n, _) -> Pervasives.compare m n)
292 (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
298 let normalize_weights (cw1, wl1) (cw2, wl2) =
299 let rec aux wl1 wl2 =
302 | (m, w)::tl1, (n, w')::tl2 when m = n ->
303 let res1, res2 = aux tl1 tl2 in
304 (m, w)::res1, (n, w')::res2
305 | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
306 let res1, res2 = aux tl1 wl2 in
307 (m, w)::res1, (m, 0)::res2
308 | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
309 let res1, res2 = aux wl1 tl2 in
310 (n, 0)::res1, (n, w')::res2
312 let res1, res2 = aux [] tl2 in
313 (n, 0)::res1, (n, w)::res2
315 let res1, res2 = aux tl1 [] in
316 (m, w)::res1, (m, 0)::res2
317 | _, _ -> assert false
319 let cmp (m, _) (n, _) = compare m n in
320 let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
321 (cw1, wl1), (cw2, wl2)
325 let compare_weights ?(normalize=false)
326 ((h1, w1) as weight1) ((h2, w2) as weight2)=
327 let (h1, w1), (h2, w2) =
329 normalize_weights weight1 weight2
336 (fun ((lt, eq, gt), diffs) w1 w2 ->
338 | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
339 let diffs = (w1 - w2) + diffs in
340 let r = compare w1 w2 in
341 if r < 0 then (lt+1, eq, gt), diffs
342 else if r = 0 then (lt, eq+1, gt), diffs
343 else (lt, eq, gt+1), diffs
344 | (meta1, w1), (meta2, w2) ->
347 (Printf.sprintf "HMMM!!!! %s, %s\n"
348 (string_of_weight weight1) (string_of_weight weight2)));
351 with Invalid_argument _ ->
354 (Printf.sprintf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
355 (string_of_weight (h1, w1)) (string_of_weight weight1)
356 (string_of_weight (h2, w2)) (string_of_weight weight2)
357 (string_of_bool normalize)));
360 let hdiff = h1 - h2 in
364 else if hdiff > 0 then Gt
365 else Eq (* Incomparable *)
367 if hdiff <= 0 then Lt
368 else if (- diffs) >= hdiff then Le else Incomparable
370 if hdiff >= 0 then Gt
371 else if diffs >= (- hdiff) then Ge else Incomparable
372 | (m, _, n) when m > 0 && n > 0 ->
379 let rec aux_ordering ?(recursion=true) t1 t2 =
380 let module C = Cic in
381 let compare_uris u1 u2 =
383 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
385 else if res = 0 then Eq
390 | _, C.Meta _ -> Incomparable
392 | t1, t2 when t1 = t2 -> Eq
394 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
398 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
402 | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
403 | C.MutInd _, _ -> Lt
404 | _, C.MutInd _ -> Gt
406 | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
408 | C.MutConstruct _, _ -> Lt
409 | _, C.MutConstruct _ -> Gt
411 | C.Appl l1, C.Appl l2 when recursion ->
417 | hd1::tl1, hd2::tl2 ->
418 let o = aux_ordering hd1 hd2 in
419 if o = Eq then cmp tl1 tl2
423 | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
429 (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
430 (CicPp.ppterm t1) (CicPp.ppterm t2)));
435 (* w1, w2 are the weights, they should already be normalized... *)
436 let nonrec_kbo_w (t1, w1) (t2, w2) =
437 match compare_weights w1 w2 with
438 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
439 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
440 | Eq -> aux_ordering t1 t2
445 let nonrec_kbo t1 t2 =
446 let w1 = weight_of_term t1 in
447 let w2 = weight_of_term t2 in
449 prerr_endline ("weight1 :"^(string_of_weight w1));
450 prerr_endline ("weight2 :"^(string_of_weight w2));
452 match compare_weights ~normalize:true w1 w2 with
453 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
454 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
455 | Eq -> aux_ordering t1 t2
461 let aux = aux_ordering ~recursion:false in
462 let w1 = weight_of_term t1
463 and w2 = weight_of_term t2 in
469 | hd1::tl1, hd2::tl2 ->
473 if o = Eq then cmp tl1 tl2
476 let comparison = compare_weights ~normalize:true w1 w2 in
477 match comparison with
481 else if r = Eq then (
483 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
484 if cmp tl1 tl2 = Lt then Lt else Incomparable
485 | _, _ -> Incomparable
490 else if r = Eq then (
492 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
493 if cmp tl1 tl2 = Gt then Gt else Incomparable
494 | _, _ -> Incomparable
500 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
502 | _, _ -> Incomparable
510 Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
511 | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) ->
514 let aux = aux_ordering ~recursion:false in
515 let w1 = weight_of_term t1
516 and w2 = weight_of_term t2 in
522 | hd1::tl1, hd2::tl2 ->
526 if o = Eq then cmp tl1 tl2
529 match get_hd t1, get_hd t2 with
533 let comparison = compare_weights ~normalize:true w1 w2 in
534 match comparison with
538 else if r = Eq then (
540 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
541 if cmp tl1 tl2 = Lt then Lt else Incomparable
542 | _, _ -> Incomparable
547 else if r = Eq then (
549 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
550 if cmp tl1 tl2 = Gt then Gt else Incomparable
551 | _, _ -> Incomparable
557 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
559 | _, _ -> Incomparable
564 let names_of_context context =
568 | Some (n, e) -> Some n)
574 let module C = Cic in
576 | t1, t2 when t1 = t2 -> Eq
577 | t1, (C.Meta _ as m) ->
578 if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
579 | (C.Meta _ as m), t2 ->
580 if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
581 | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
589 let res1 = List.fold_left (f t2) false tl1 in
591 else let res2 = List.fold_left (f t1) false tl2 in
595 if res <> Incomparable then
599 if not r then false else
604 match aux_ordering hd1 hd2 with
606 let res = List.fold_left (f t1) false tl2 in
610 let res = List.fold_left (f t2) false tl1 in
617 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
619 with Invalid_argument _ ->
624 if List.fold_left (f t1) false tl2 then Gt
627 if List.fold_left (f t2) false tl1 then Lt
633 | t1, t2 -> aux_ordering t1 t2
637 (* settable by the user... *)
638 let compare_terms = ref nonrec_kbo;;
639 (* let compare_terms = ref ao;; *)
640 (* let compare_terms = ref rpo;; *)
642 let guarded_simpl context t = t
644 let t' = ProofEngineReduction.simpl context t in
645 let simpl_order = !compare_terms t t' in
646 if simpl_order = Gt then
647 (* prerr_endline ("reduce: "^(CicPp.ppterm t)^(CicPp.ppterm t')); *)
652 type equality_sign = Negative | Positive;;
654 let string_of_sign = function
655 | Negative -> "Negative"
656 | Positive -> "Positive"
660 type pos = Left | Right
662 let string_of_pos = function
668 let eq_ind_URI () = LibraryObjects.eq_ind_URI ~eq:(LibraryObjects.eq_URI ())
669 let eq_ind_r_URI () = LibraryObjects.eq_ind_r_URI ~eq:(LibraryObjects.eq_URI ())
670 let sym_eq_URI () = LibraryObjects.sym_eq_URI ~eq:(LibraryObjects.eq_URI ())
672 let s = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
673 UriManager.uri_of_string (s ^ "#xpointer(1/1/1)")
674 let trans_eq_URI () = LibraryObjects.trans_eq_URI ~eq:(LibraryObjects.eq_URI ())