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 *)
236 if diffs < (- hdiff) then Lt
237 else if diffs = (- hdiff) then Le else Incomparable
240 if m > 0 || hdiff < 0 then Lt
241 else if diffs >= (- hdiff) then Le else Incomparable
243 if diffs >= (- hdiff) then Le else Incomparable *)
245 if (- hdiff) < diffs then Gt
246 else if (- hdiff) = diffs then Ge else Incomparable
249 if m > 0 || hdiff > 0 then Gt
250 else if (- diffs) >= hdiff then Ge else Incomparable
252 if (- diffs) >= hdiff then Ge else Incomparable *)
253 | (m, _, n) when m > 0 && n > 0 ->
259 let rec aux_ordering ?(recursion=true) t1 t2 =
260 let module C = Cic in
261 let compare_uris u1 u2 =
263 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
265 else if res = 0 then Eq
270 | _, C.Meta _ -> Incomparable
272 | t1, t2 when t1 = t2 -> Eq
274 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
278 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
282 | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
283 | C.MutInd _, _ -> Lt
284 | _, C.MutInd _ -> Gt
286 | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
288 | C.MutConstruct _, _ -> Lt
289 | _, C.MutConstruct _ -> Gt
291 | C.Appl l1, C.Appl l2 when recursion ->
297 | hd1::tl1, hd2::tl2 ->
298 let o = aux_ordering hd1 hd2 in
299 if o = Eq then cmp tl1 tl2
303 | C.Appl (h1::t1), C.Appl (h2::t2) when not recursion ->
309 (Printf.sprintf "These two terms are not comparable:\n%s\n%s\n\n"
310 (CicPp.ppterm t1) (CicPp.ppterm t2)));
315 (* w1, w2 are the weights, they should already be normalized... *)
316 let nonrec_kbo_w (t1, w1) (t2, w2) =
317 match compare_weights w1 w2 with
318 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
319 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
320 | Eq -> aux_ordering t1 t2
325 let nonrec_kbo t1 t2 =
326 let w1 = weight_of_term t1 in
327 let w2 = weight_of_term t2 in
329 prerr_endline ("weight1 :"^(string_of_weight w1));
330 prerr_endline ("weight2 :"^(string_of_weight w2));
332 match compare_weights ~normalize:true w1 w2 with
333 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
334 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
335 | Eq -> aux_ordering t1 t2
341 let aux = aux_ordering ~recursion:false in
342 let w1 = weight_of_term t1
343 and w2 = weight_of_term t2 in
349 | hd1::tl1, hd2::tl2 ->
353 if o = Eq then cmp tl1 tl2
356 let comparison = compare_weights ~normalize:true w1 w2 in
357 match comparison with
361 else if r = Eq then (
363 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
364 if cmp tl1 tl2 = Lt then Lt else Incomparable
365 | _, _ -> Incomparable
370 else if r = Eq then (
372 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
373 if cmp tl1 tl2 = Gt then Gt else Incomparable
374 | _, _ -> Incomparable
380 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
382 | _, _ -> Incomparable
390 Cic.MutConstruct(uri,tyno,cno,_) -> Some(uri,tyno,cno)
391 | Cic.Appl(Cic.MutConstruct(uri,tyno,cno,_)::_) ->
394 let aux = aux_ordering ~recursion:false in
395 let w1 = weight_of_term t1
396 and w2 = weight_of_term t2 in
402 | hd1::tl1, hd2::tl2 ->
406 if o = Eq then cmp tl1 tl2
409 match get_hd t1, get_hd t2 with
413 let comparison = compare_weights ~normalize:true w1 w2 in
414 match comparison with
418 else if r = Eq then (
420 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
421 if cmp tl1 tl2 = Lt then Lt else Incomparable
422 | _, _ -> Incomparable
427 else if r = Eq then (
429 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
430 if cmp tl1 tl2 = Gt then Gt else Incomparable
431 | _, _ -> Incomparable
437 | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
439 | _, _ -> Incomparable
444 let names_of_context context =
448 | Some (n, e) -> Some n)
457 let compare = Pervasives.compare
460 module TermSet = Set.Make(OrderedTerm);;
461 module TermMap = Map.Make(OrderedTerm);;
463 let symbols_of_term term =
464 let module C = Cic in
465 let rec aux map = function
468 List.fold_left (fun res t -> (aux res t)) map l
472 let c = TermMap.find t map in
473 TermMap.add t (c+1) map
479 aux TermMap.empty term
483 let metas_of_term term =
484 let module C = Cic in
485 let rec aux = function
486 | C.Meta _ as t -> TermSet.singleton t
488 List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
489 | t -> TermSet.empty (* TODO: maybe add other cases? *)
496 let module C = Cic in
498 | t1, t2 when t1 = t2 -> Eq
499 | t1, (C.Meta _ as m) ->
500 if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
501 | (C.Meta _ as m), t2 ->
502 if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
503 | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
511 let res1 = List.fold_left (f t2) false tl1 in
513 else let res2 = List.fold_left (f t1) false tl2 in
517 if res <> Incomparable then
521 if not r then false else
526 match aux_ordering hd1 hd2 with
528 let res = List.fold_left (f t1) false tl2 in
532 let res = List.fold_left (f t2) false tl1 in
539 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
541 with Invalid_argument _ ->
546 if List.fold_left (f t1) false tl2 then Gt
549 if List.fold_left (f t2) false tl1 then Lt
555 | t1, t2 -> aux_ordering t1 t2
559 (* settable by the user... *)
560 let compare_terms = ref nonrec_kbo;;
561 (* let compare_terms = ref ao;; *)
563 let guarded_simpl context t =
564 let t' = ProofEngineReduction.simpl context t in
565 let simpl_order = !compare_terms t t' in
566 if simpl_order = Gt then
567 (prerr_endline ("reduce: "^(CicPp.ppterm t)^(CicPp.ppterm t'));
572 type equality_sign = Negative | Positive;;
574 let string_of_sign = function
575 | Negative -> "Negative"
576 | Positive -> "Positive"
580 type pos = Left | Right
582 let string_of_pos = function
588 let eq_ind_URI () = LibraryObjects.eq_ind_URI ~eq:(LibraryObjects.eq_URI ())
589 let eq_ind_r_URI () = LibraryObjects.eq_ind_r_URI ~eq:(LibraryObjects.eq_URI ())
590 let sym_eq_URI () = LibraryObjects.sym_eq_URI ~eq:(LibraryObjects.eq_URI ())
592 let s = UriManager.string_of_uri (LibraryObjects.eq_URI ()) in
593 UriManager.uri_of_string (s ^ "#xpointer(1/1/1)")
594 let trans_eq_URI () = LibraryObjects.trans_eq_URI ~eq:(LibraryObjects.eq_URI ())