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/.
28 let debug_print s = if debug then prerr_endline (Lazy.force s);;
31 let print_metasenv metasenv =
32 String.concat "\n--------------------------\n"
33 (List.map (fun (i, context, term) ->
34 (string_of_int i) ^ " [\n" ^ (CicPp.ppcontext context) ^
35 "\n] " ^ (CicPp.ppterm term))
40 let print_subst ?(prefix="\n") subst =
43 (fun (i, (c, t, ty)) ->
44 Printf.sprintf "?%d -> %s : %s" i
45 (CicPp.ppterm t) (CicPp.ppterm ty))
49 (* (weight of constants, [(meta, weight_of_meta)]) *)
50 type weight = int * (int * int) list;;
52 let string_of_weight (cw, mw) =
55 (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
57 Printf.sprintf "[%d; %s]" cw s
60 let weight_of_term ?(consider_metas=true) term =
62 let vars_dict = Hashtbl.create 5 in
63 let rec aux = function
64 | C.Meta (metano, _) when consider_metas ->
66 let oldw = Hashtbl.find vars_dict metano in
67 Hashtbl.replace vars_dict metano (oldw+1)
69 Hashtbl.add vars_dict metano 1);
71 | C.Meta _ -> 0 (* "variables" are lighter than constants and functions...*)
75 | C.MutInd (_, _, ens)
76 | C.MutConstruct (_, _, _, ens) ->
77 List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
80 | C.Lambda (_, t1, t2)
82 | C.LetIn (_, t1, t2) ->
87 | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
89 | C.MutCase (_, _, outt, t, pl) ->
92 let w3 = List.fold_left (+) 0 (List.map aux pl) in
96 List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
99 List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
105 Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
108 | (m1, _), (m2, _) -> m2 - m1
110 (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
114 module OrderedInt = struct
117 let compare = Pervasives.compare
120 module IntSet = Set.Make(OrderedInt)
122 let compute_equality_weight ty left right =
123 let metasw = ref 0 in
125 let w, m = (weight_of_term ~consider_metas:true t) in
126 metasw := !metasw + (2 * (List.length m));
129 (* Warning: the following let cannot be expanded since it forces the
130 right evaluation order!!!! *)
131 let w = (weight_of ty) + (weight_of left) + (weight_of right) in
136 (* returns a "normalized" version of the polynomial weight wl (with type
137 * weight list), i.e. a list sorted ascending by meta number,
138 * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
139 * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
140 * (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
141 let normalize_weight maxmeta (cw, wl) =
142 let rec aux = function
144 | m -> (m, 0)::(aux (m-1))
146 let tmpl = aux maxmeta in
149 (fun (m, _) (n, _) -> Pervasives.compare m n)
151 (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
157 let normalize_weights (cw1, wl1) (cw2, wl2) =
158 let rec aux wl1 wl2 =
161 | (m, w)::tl1, (n, w')::tl2 when m = n ->
162 let res1, res2 = aux tl1 tl2 in
163 (m, w)::res1, (n, w')::res2
164 | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
165 let res1, res2 = aux tl1 wl2 in
166 (m, w)::res1, (m, 0)::res2
167 | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
168 let res1, res2 = aux wl1 tl2 in
169 (n, 0)::res1, (n, w')::res2
171 let res1, res2 = aux [] tl2 in
172 (n, 0)::res1, (n, w)::res2
174 let res1, res2 = aux tl1 [] in
175 (m, w)::res1, (m, 0)::res2
176 | _, _ -> assert false
178 let cmp (m, _) (n, _) = compare m n in
179 let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
180 (cw1, wl1), (cw2, wl2)
184 type comparison = Lt | Le | Eq | Ge | Gt | Incomparable;;
186 let string_of_comparison = function
192 | Incomparable -> "I"
195 let compare_weights ?(normalize=false)
196 ((h1, w1) as weight1) ((h2, w2) as weight2)=
197 let (h1, w1), (h2, w2) =
199 normalize_weights weight1 weight2
206 (fun ((lt, eq, gt), diffs) w1 w2 ->
208 | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
209 let diffs = (w1 - w2) + diffs in
210 let r = compare w1 w2 in
211 if r < 0 then (lt+1, eq, gt), diffs
212 else if r = 0 then (lt, eq+1, gt), diffs
213 else (lt, eq, gt+1), diffs
214 | (meta1, w1), (meta2, w2) ->
217 (Printf.sprintf "HMMM!!!! %s, %s\n"
218 (string_of_weight weight1) (string_of_weight weight2)));
221 with Invalid_argument _ ->
224 (Printf.sprintf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
225 (string_of_weight (h1, w1)) (string_of_weight weight1)
226 (string_of_weight (h2, w2)) (string_of_weight weight2)
227 (string_of_bool normalize)));
230 let hdiff = h1 - h2 in
234 else if hdiff > 0 then Gt
235 else Eq (* Incomparable *)
238 if m > 0 || hdiff < 0 then Lt
239 else if diffs >= (- hdiff) then Le else Incomparable
241 if diffs >= (- hdiff) then Le else Incomparable
244 if m > 0 || hdiff > 0 then Gt
245 else if (- diffs) >= hdiff then Ge else Incomparable
247 if (- diffs) >= hdiff then Ge else Incomparable
248 | (m, _, n) when m > 0 && n > 0 ->
254 let rec aux_ordering ?(recursion=true) t1 t2 =
255 let module C = Cic in
256 let compare_uris u1 u2 =
258 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
260 else if res = 0 then Eq
265 | _, C.Meta _ -> Incomparable
267 | t1, t2 when t1 = t2 -> Eq
269 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
273 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
277 | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
278 | C.MutInd _, _ -> Lt
279 | _, C.MutInd _ -> Gt
281 | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
283 | C.MutConstruct _, _ -> Lt
284 | _, C.MutConstruct _ -> Gt
286 | C.Appl l1, C.Appl l2 when recursion ->
292 | hd1::tl1, hd2::tl2 ->
293 let o = aux_ordering hd1 hd2 in
294 if o = Eq then cmp tl1 tl2
298 | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
304 (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
305 (CicPp.ppterm t1) (CicPp.ppterm t2)));
310 (* w1, w2 are the weights, they should already be normalized... *)
311 let nonrec_kbo_w (t1, w1) (t2, w2) =
312 match compare_weights w1 w2 with
313 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
314 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
315 | Eq -> aux_ordering t1 t2
320 let nonrec_kbo t1 t2 =
321 let w1 = weight_of_term t1 in
322 let w2 = weight_of_term t2 in
323 match compare_weights ~normalize:true w1 w2 with
324 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
325 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
326 | Eq -> aux_ordering t1 t2
332 let aux = aux_ordering ~recursion:false in
333 let w1 = weight_of_term t1
334 and w2 = weight_of_term t2 in
340 | hd1::tl1, hd2::tl2 ->
344 if o = Eq then cmp tl1 tl2
347 let comparison = compare_weights ~normalize:true w1 w2 in
348 match comparison with
352 else if r = Eq then (
354 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
355 if cmp tl1 tl2 = Lt then Lt else Incomparable
356 | _, _ -> Incomparable
361 else if r = Eq then (
363 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
364 if cmp tl1 tl2 = Gt then Gt else Incomparable
365 | _, _ -> Incomparable
371 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
373 | _, _ -> Incomparable
379 let names_of_context context =
383 | Some (n, e) -> Some n)
392 let compare = Pervasives.compare
395 module TermSet = Set.Make(OrderedTerm);;
396 module TermMap = Map.Make(OrderedTerm);;
398 let symbols_of_term term =
399 let module C = Cic in
400 let rec aux map = function
403 List.fold_left (fun res t -> (aux res t)) map l
407 let c = TermMap.find t map in
408 TermMap.add t (c+1) map
414 aux TermMap.empty term
418 let metas_of_term term =
419 let module C = Cic in
420 let rec aux = function
421 | C.Meta _ as t -> TermSet.singleton t
423 List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
424 | t -> TermSet.empty (* TODO: maybe add other cases? *)
431 let module C = Cic in
433 | t1, t2 when t1 = t2 -> Eq
434 | t1, (C.Meta _ as m) ->
435 if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
436 | (C.Meta _ as m), t2 ->
437 if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
438 | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
446 let res1 = List.fold_left (f t2) false tl1 in
448 else let res2 = List.fold_left (f t1) false tl2 in
452 if res <> Incomparable then
456 if not r then false else
461 match aux_ordering hd1 hd2 with
463 let res = List.fold_left (f t1) false tl2 in
467 let res = List.fold_left (f t2) false tl1 in
474 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
476 with Invalid_argument _ ->
481 if List.fold_left (f t1) false tl2 then Gt
484 if List.fold_left (f t2) false tl1 then Lt
490 | t1, t2 -> aux_ordering t1 t2
494 (* settable by the user... *)
495 let compare_terms = ref nonrec_kbo;;
498 type equality_sign = Negative | Positive;;
500 let string_of_sign = function
501 | Negative -> "Negative"
502 | Positive -> "Positive"
506 type pos = Left | Right
508 let string_of_pos = function
514 let eq_ind_URI () = LibraryObjects.eq_ind_URI ~eq:(LibraryObjects.eq_URI ())
515 let eq_ind_r_URI () = LibraryObjects.eq_ind_r_URI ~eq:(LibraryObjects.eq_URI ())
516 let sym_eq_URI () = LibraryObjects.sym_eq_URI ~eq:(LibraryObjects.eq_URI ())
518 let s = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
519 UriManager.uri_of_string (s ^ "#xpointer(1/1/1)")
520 let trans_eq_URI () = LibraryObjects.trans_eq_URI ~eq:(LibraryObjects.eq_URI ())