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))
41 let print_subst ?(prefix="\n") subst =
44 (fun (i, (c, t, ty)) ->
45 Printf.sprintf "?%d -> %s : %s" i
46 (CicPp.ppterm t) (CicPp.ppterm ty))
50 (* (weight of constants, [(meta, weight_of_meta)]) *)
51 type weight = int * (int * int) list;;
53 let string_of_weight (cw, mw) =
56 (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
58 Printf.sprintf "[%d; %s]" cw s
61 let weight_of_term ?(consider_metas=true) term =
63 let vars_dict = Hashtbl.create 5 in
64 let rec aux = function
65 | C.Meta (metano, _) when consider_metas ->
67 let oldw = Hashtbl.find vars_dict metano in
68 Hashtbl.replace vars_dict metano (oldw+1)
70 Hashtbl.add vars_dict metano 1);
72 | C.Meta _ -> 0 (* "variables" are lighter than constants and functions...*)
76 | C.MutInd (_, _, ens)
77 | C.MutConstruct (_, _, _, ens) ->
78 List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
81 | C.Lambda (_, t1, t2)
83 | C.LetIn (_, t1, t2) ->
88 | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
90 | C.MutCase (_, _, outt, t, pl) ->
93 let w3 = List.fold_left (+) 0 (List.map aux pl) in
97 List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
100 List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
106 Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
109 | (m1, _), (m2, _) -> m2 - m1
111 (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
115 module OrderedInt = struct
118 let compare = Pervasives.compare
121 module IntSet = Set.Make(OrderedInt)
123 let compute_equality_weight ty left right =
124 let metasw = ref 0 in
126 let w, m = (weight_of_term ~consider_metas:true t) in
127 metasw := !metasw + (2 * (List.length m));
130 (* Warning: the following let cannot be expanded since it forces the
131 right evaluation order!!!! *)
132 let w = (weight_of ty) + (weight_of left) + (weight_of right) in
137 (* returns a "normalized" version of the polynomial weight wl (with type
138 * weight list), i.e. a list sorted ascending by meta number,
139 * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
140 * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
141 * (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
142 let normalize_weight maxmeta (cw, wl) =
143 let rec aux = function
145 | m -> (m, 0)::(aux (m-1))
147 let tmpl = aux maxmeta in
150 (fun (m, _) (n, _) -> Pervasives.compare m n)
152 (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
158 let normalize_weights (cw1, wl1) (cw2, wl2) =
159 let rec aux wl1 wl2 =
162 | (m, w)::tl1, (n, w')::tl2 when m = n ->
163 let res1, res2 = aux tl1 tl2 in
164 (m, w)::res1, (n, w')::res2
165 | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
166 let res1, res2 = aux tl1 wl2 in
167 (m, w)::res1, (m, 0)::res2
168 | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
169 let res1, res2 = aux wl1 tl2 in
170 (n, 0)::res1, (n, w')::res2
172 let res1, res2 = aux [] tl2 in
173 (n, 0)::res1, (n, w)::res2
175 let res1, res2 = aux tl1 [] in
176 (m, w)::res1, (m, 0)::res2
177 | _, _ -> assert false
179 let cmp (m, _) (n, _) = compare m n in
180 let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
181 (cw1, wl1), (cw2, wl2)
185 type comparison = Lt | Le | Eq | Ge | Gt | Incomparable;;
187 let string_of_comparison = function
193 | Incomparable -> "I"
196 let compare_weights ?(normalize=false)
197 ((h1, w1) as weight1) ((h2, w2) as weight2)=
198 let (h1, w1), (h2, w2) =
200 normalize_weights weight1 weight2
207 (fun ((lt, eq, gt), diffs) w1 w2 ->
209 | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
210 let diffs = (w1 - w2) + diffs in
211 let r = compare w1 w2 in
212 if r < 0 then (lt+1, eq, gt), diffs
213 else if r = 0 then (lt, eq+1, gt), diffs
214 else (lt, eq, gt+1), diffs
215 | (meta1, w1), (meta2, w2) ->
218 (Printf.sprintf "HMMM!!!! %s, %s\n"
219 (string_of_weight weight1) (string_of_weight weight2)));
222 with Invalid_argument _ ->
225 (Printf.sprintf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
226 (string_of_weight (h1, w1)) (string_of_weight weight1)
227 (string_of_weight (h2, w2)) (string_of_weight weight2)
228 (string_of_bool normalize)));
231 let hdiff = h1 - h2 in
235 else if hdiff > 0 then Gt
236 else Eq (* Incomparable *)
238 if diffs < (- hdiff) then Lt
239 else if diffs = (- hdiff) then Le else Incomparable
242 if m > 0 || hdiff < 0 then Lt
243 else if diffs >= (- hdiff) then Le else Incomparable
245 if diffs >= (- hdiff) then Le else Incomparable *)
247 if (- hdiff) < diffs then Gt
248 else if (- hdiff) = diffs then Ge else Incomparable
251 if m > 0 || hdiff > 0 then Gt
252 else if (- diffs) >= hdiff then Ge else Incomparable
254 if (- diffs) >= hdiff then Ge else Incomparable *)
255 | (m, _, n) when m > 0 && n > 0 ->
261 let rec aux_ordering ?(recursion=true) t1 t2 =
262 let module C = Cic in
263 let compare_uris u1 u2 =
265 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
267 else if res = 0 then Eq
272 | _, C.Meta _ -> Incomparable
274 | t1, t2 when t1 = t2 -> Eq
276 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
280 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
284 | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
285 | C.MutInd _, _ -> Lt
286 | _, C.MutInd _ -> Gt
288 | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
290 | C.MutConstruct _, _ -> Lt
291 | _, C.MutConstruct _ -> Gt
293 | C.Appl l1, C.Appl l2 when recursion ->
299 | hd1::tl1, hd2::tl2 ->
300 let o = aux_ordering hd1 hd2 in
301 if o = Eq then cmp tl1 tl2
305 | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
311 (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
312 (CicPp.ppterm t1) (CicPp.ppterm t2)));
317 (* w1, w2 are the weights, they should already be normalized... *)
318 let nonrec_kbo_w (t1, w1) (t2, w2) =
319 match compare_weights w1 w2 with
320 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
321 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
322 | Eq -> aux_ordering t1 t2
327 let nonrec_kbo t1 t2 =
328 let w1 = weight_of_term t1 in
329 let w2 = weight_of_term t2 in
331 prerr_endline ("weight1 :"^(string_of_weight w1));
332 prerr_endline ("weight2 :"^(string_of_weight w2));
334 match compare_weights ~normalize:true w1 w2 with
335 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
336 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
337 | Eq -> aux_ordering t1 t2
343 let aux = aux_ordering ~recursion:false in
344 let w1 = weight_of_term t1
345 and w2 = weight_of_term t2 in
351 | hd1::tl1, hd2::tl2 ->
355 if o = Eq then cmp tl1 tl2
358 let comparison = compare_weights ~normalize:true w1 w2 in
359 match comparison with
363 else if r = Eq then (
365 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
366 if cmp tl1 tl2 = Lt then Lt else Incomparable
367 | _, _ -> Incomparable
372 else if r = Eq then (
374 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
375 if cmp tl1 tl2 = Gt then Gt else Incomparable
376 | _, _ -> Incomparable
382 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
384 | _, _ -> Incomparable
392 Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
393 | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) ->
396 let aux = aux_ordering ~recursion:false in
397 let w1 = weight_of_term t1
398 and w2 = weight_of_term t2 in
404 | hd1::tl1, hd2::tl2 ->
408 if o = Eq then cmp tl1 tl2
411 match get_hd t1, get_hd t2 with
415 let comparison = compare_weights ~normalize:true w1 w2 in
416 match comparison with
420 else if r = Eq then (
422 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
423 if cmp tl1 tl2 = Lt then Lt else Incomparable
424 | _, _ -> Incomparable
429 else if r = Eq then (
431 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
432 if cmp tl1 tl2 = Gt then Gt else Incomparable
433 | _, _ -> Incomparable
439 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
441 | _, _ -> Incomparable
446 let names_of_context context =
450 | Some (n, e) -> Some n)
459 let compare = Pervasives.compare
462 module TermSet = Set.Make(OrderedTerm);;
463 module TermMap = Map.Make(OrderedTerm);;
465 let symbols_of_term term =
466 let module C = Cic in
467 let rec aux map = function
470 List.fold_left (fun res t -> (aux res t)) map l
474 let c = TermMap.find t map in
475 TermMap.add t (c+1) map
481 aux TermMap.empty term
485 let metas_of_term term =
486 let module C = Cic in
487 let rec aux = function
488 | C.Meta _ as t -> TermSet.singleton t
490 List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
491 | t -> TermSet.empty (* TODO: maybe add other cases? *)
498 let module C = Cic in
500 | t1, t2 when t1 = t2 -> Eq
501 | t1, (C.Meta _ as m) ->
502 if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
503 | (C.Meta _ as m), t2 ->
504 if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
505 | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
513 let res1 = List.fold_left (f t2) false tl1 in
515 else let res2 = List.fold_left (f t1) false tl2 in
519 if res <> Incomparable then
523 if not r then false else
528 match aux_ordering hd1 hd2 with
530 let res = List.fold_left (f t1) false tl2 in
534 let res = List.fold_left (f t2) false tl1 in
541 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
543 with Invalid_argument _ ->
548 if List.fold_left (f t1) false tl2 then Gt
551 if List.fold_left (f t2) false tl1 then Lt
557 | t1, t2 -> aux_ordering t1 t2
561 (* settable by the user... *)
562 let compare_terms = ref nonrec_kbo;;
563 (* let compare_terms = ref ao;; *)
565 let guarded_simpl context t =
566 let t' = ProofEngineReduction.simpl context t in
567 let simpl_order = !compare_terms t t' in
568 if simpl_order = Gt then
569 (* prerr_endline ("reduce: "^(CicPp.ppterm t)^(CicPp.ppterm t')); *)
574 type equality_sign = Negative | Positive;;
576 let string_of_sign = function
577 | Negative -> "Negative"
578 | Positive -> "Positive"
582 type pos = Left | Right
584 let string_of_pos = function
590 let eq_ind_URI () = LibraryObjects.eq_ind_URI ~eq:(LibraryObjects.eq_URI ())
591 let eq_ind_r_URI () = LibraryObjects.eq_ind_r_URI ~eq:(LibraryObjects.eq_URI ())
592 let sym_eq_URI () = LibraryObjects.sym_eq_URI ~eq:(LibraryObjects.eq_URI ())
594 let s = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
595 UriManager.uri_of_string (s ^ "#xpointer(1/1/1)")
596 let trans_eq_URI () = LibraryObjects.trans_eq_URI ~eq:(LibraryObjects.eq_URI ())