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);;
30 let print_metasenv metasenv =
31 String.concat "\n--------------------------\n"
32 (List.map (fun (i, context, term) ->
33 (string_of_int i) ^ " [\n" ^ (CicPp.ppcontext context) ^
34 "\n] " ^ (CicPp.ppterm term))
39 let print_subst ?(prefix="\n") subst =
42 (fun (i, (c, t, ty)) ->
43 Printf.sprintf "?%d -> %s : %s" i
44 (CicPp.ppterm t) (CicPp.ppterm ty))
48 (* (weight of constants, [(meta, weight_of_meta)]) *)
49 type weight = int * (int * int) list;;
51 let string_of_weight (cw, mw) =
54 (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
56 Printf.sprintf "[%d; %s]" cw s
59 let weight_of_term ?(consider_metas=true) term =
61 let vars_dict = Hashtbl.create 5 in
62 let rec aux = function
63 | C.Meta (metano, _) when consider_metas ->
65 let oldw = Hashtbl.find vars_dict metano in
66 Hashtbl.replace vars_dict metano (oldw+1)
68 Hashtbl.add vars_dict metano 1);
70 | C.Meta _ -> 0 (* "variables" are lighter than constants and functions...*)
74 | C.MutInd (_, _, ens)
75 | C.MutConstruct (_, _, _, ens) ->
76 List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
79 | C.Lambda (_, t1, t2)
81 | C.LetIn (_, t1, t2) ->
86 | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
88 | C.MutCase (_, _, outt, t, pl) ->
91 let w3 = List.fold_left (+) 0 (List.map aux pl) in
95 List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
98 List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
104 Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
107 | (m1, _), (m2, _) -> m2 - m1
109 (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
113 module OrderedInt = struct
116 let compare = Pervasives.compare
119 module IntSet = Set.Make(OrderedInt)
121 let compute_equality_weight ty left right =
122 let metasw = ref 0 in
124 let w, m = (weight_of_term ~consider_metas:true t) in
125 metasw := !metasw + (2 * (List.length m));
128 (* Warning: the following let cannot be expanded since it forces the
129 right evaluation order!!!! *)
130 let w = (weight_of ty) + (weight_of left) + (weight_of right) in
135 (* returns a "normalized" version of the polynomial weight wl (with type
136 * weight list), i.e. a list sorted ascending by meta number,
137 * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
138 * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
139 * (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
140 let normalize_weight maxmeta (cw, wl) =
141 let rec aux = function
143 | m -> (m, 0)::(aux (m-1))
145 let tmpl = aux maxmeta in
148 (fun (m, _) (n, _) -> Pervasives.compare m n)
150 (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
156 let normalize_weights (cw1, wl1) (cw2, wl2) =
157 let rec aux wl1 wl2 =
160 | (m, w)::tl1, (n, w')::tl2 when m = n ->
161 let res1, res2 = aux tl1 tl2 in
162 (m, w)::res1, (n, w')::res2
163 | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
164 let res1, res2 = aux tl1 wl2 in
165 (m, w)::res1, (m, 0)::res2
166 | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
167 let res1, res2 = aux wl1 tl2 in
168 (n, 0)::res1, (n, w')::res2
170 let res1, res2 = aux [] tl2 in
171 (n, 0)::res1, (n, w)::res2
173 let res1, res2 = aux tl1 [] in
174 (m, w)::res1, (m, 0)::res2
175 | _, _ -> assert false
177 let cmp (m, _) (n, _) = compare m n in
178 let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
179 (cw1, wl1), (cw2, wl2)
183 type comparison = Lt | Le | Eq | Ge | Gt | Incomparable;;
185 let string_of_comparison = function
191 | Incomparable -> "I"
194 let compare_weights ?(normalize=false)
195 ((h1, w1) as weight1) ((h2, w2) as weight2)=
196 let (h1, w1), (h2, w2) =
198 normalize_weights weight1 weight2
205 (fun ((lt, eq, gt), diffs) w1 w2 ->
207 | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
208 let diffs = (w1 - w2) + diffs in
209 let r = compare w1 w2 in
210 if r < 0 then (lt+1, eq, gt), diffs
211 else if r = 0 then (lt, eq+1, gt), diffs
212 else (lt, eq, gt+1), diffs
213 | (meta1, w1), (meta2, w2) ->
216 (Printf.sprintf "HMMM!!!! %s, %s\n"
217 (string_of_weight weight1) (string_of_weight weight2)));
220 with Invalid_argument _ ->
223 (Printf.sprintf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
224 (string_of_weight (h1, w1)) (string_of_weight weight1)
225 (string_of_weight (h2, w2)) (string_of_weight weight2)
226 (string_of_bool normalize)));
229 let hdiff = h1 - h2 in
233 else if hdiff > 0 then Gt
234 else Eq (* Incomparable *)
237 if m > 0 || hdiff < 0 then Lt
238 else if diffs >= (- hdiff) then Le else Incomparable
240 if diffs >= (- hdiff) then Le else Incomparable
243 if m > 0 || hdiff > 0 then Gt
244 else if (- diffs) >= hdiff then Ge else Incomparable
246 if (- diffs) >= hdiff then Ge else Incomparable
247 | (m, _, n) when m > 0 && n > 0 ->
253 let rec aux_ordering ?(recursion=true) t1 t2 =
254 let module C = Cic in
255 let compare_uris u1 u2 =
257 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
259 else if res = 0 then Eq
264 | _, C.Meta _ -> Incomparable
266 | t1, t2 when t1 = t2 -> Eq
268 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
272 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
276 | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
277 | C.MutInd _, _ -> Lt
278 | _, C.MutInd _ -> Gt
280 | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
282 | C.MutConstruct _, _ -> Lt
283 | _, C.MutConstruct _ -> Gt
285 | C.Appl l1, C.Appl l2 when recursion ->
291 | hd1::tl1, hd2::tl2 ->
292 let o = aux_ordering hd1 hd2 in
293 if o = Eq then cmp tl1 tl2
297 | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
303 (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
304 (CicPp.ppterm t1) (CicPp.ppterm t2)));
309 (* w1, w2 are the weights, they should already be normalized... *)
310 let nonrec_kbo_w (t1, w1) (t2, w2) =
311 match compare_weights w1 w2 with
312 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
313 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
314 | Eq -> aux_ordering t1 t2
319 let nonrec_kbo t1 t2 =
320 let w1 = weight_of_term t1 in
321 let w2 = weight_of_term t2 in
322 match compare_weights ~normalize:true w1 w2 with
323 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
324 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
325 | Eq -> aux_ordering t1 t2
331 let aux = aux_ordering ~recursion:false in
332 let w1 = weight_of_term t1
333 and w2 = weight_of_term t2 in
339 | hd1::tl1, hd2::tl2 ->
343 if o = Eq then cmp tl1 tl2
346 let comparison = compare_weights ~normalize:true w1 w2 in
347 match comparison with
351 else if r = Eq then (
353 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
354 if cmp tl1 tl2 = Lt then Lt else Incomparable
355 | _, _ -> Incomparable
360 else if r = Eq then (
362 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
363 if cmp tl1 tl2 = Gt then Gt else Incomparable
364 | _, _ -> Incomparable
370 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
372 | _, _ -> Incomparable
380 Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
381 | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) ->
384 let aux = aux_ordering ~recursion:false in
385 let w1 = weight_of_term t1
386 and w2 = weight_of_term t2 in
392 | hd1::tl1, hd2::tl2 ->
396 if o = Eq then cmp tl1 tl2
399 match get_hd t1, get_hd t2 with
403 let comparison = compare_weights ~normalize:true w1 w2 in
404 match comparison with
408 else if r = Eq then (
410 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
411 if cmp tl1 tl2 = Lt then Lt else Incomparable
412 | _, _ -> Incomparable
417 else if r = Eq then (
419 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
420 if cmp tl1 tl2 = Gt then Gt else Incomparable
421 | _, _ -> Incomparable
427 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
429 | _, _ -> Incomparable
434 let names_of_context context =
438 | Some (n, e) -> Some n)
447 let compare = Pervasives.compare
450 module TermSet = Set.Make(OrderedTerm);;
451 module TermMap = Map.Make(OrderedTerm);;
453 let symbols_of_term term =
454 let module C = Cic in
455 let rec aux map = function
458 List.fold_left (fun res t -> (aux res t)) map l
462 let c = TermMap.find t map in
463 TermMap.add t (c+1) map
469 aux TermMap.empty term
473 let metas_of_term term =
474 let module C = Cic in
475 let rec aux = function
476 | C.Meta _ as t -> TermSet.singleton t
478 List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
479 | t -> TermSet.empty (* TODO: maybe add other cases? *)
486 let module C = Cic in
488 | t1, t2 when t1 = t2 -> Eq
489 | t1, (C.Meta _ as m) ->
490 if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
491 | (C.Meta _ as m), t2 ->
492 if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
493 | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
501 let res1 = List.fold_left (f t2) false tl1 in
503 else let res2 = List.fold_left (f t1) false tl2 in
507 if res <> Incomparable then
511 if not r then false else
516 match aux_ordering hd1 hd2 with
518 let res = List.fold_left (f t1) false tl2 in
522 let res = List.fold_left (f t2) false tl1 in
529 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
531 with Invalid_argument _ ->
536 if List.fold_left (f t1) false tl2 then Gt
539 if List.fold_left (f t2) false tl1 then Lt
545 | t1, t2 -> aux_ordering t1 t2
549 (* settable by the user... *)
550 (* let compare_terms = ref nonrec_kbo;; *)
551 let compare_terms = ref ao;;
553 let guarded_simpl context t =
554 let t' = ProofEngineReduction.simpl context t in
555 let simpl_order = !compare_terms t t' in
556 if simpl_order = Gt then
557 (prerr_endline ("reduce: "^(CicPp.ppterm t)^(CicPp.ppterm t'));
562 type equality_sign = Negative | Positive;;
564 let string_of_sign = function
565 | Negative -> "Negative"
566 | Positive -> "Positive"
570 type pos = Left | Right
572 let string_of_pos = function
578 let eq_ind_URI () = LibraryObjects.eq_ind_URI ~eq:(LibraryObjects.eq_URI ())
579 let eq_ind_r_URI () = LibraryObjects.eq_ind_r_URI ~eq:(LibraryObjects.eq_URI ())
580 let sym_eq_URI () = LibraryObjects.sym_eq_URI ~eq:(LibraryObjects.eq_URI ())
582 let s = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
583 UriManager.uri_of_string (s ^ "#xpointer(1/1/1)")
584 let trans_eq_URI () = LibraryObjects.trans_eq_URI ~eq:(LibraryObjects.eq_URI ())