3 let debug_print = if debug then prerr_endline else ignore;;
6 let print_metasenv metasenv =
7 String.concat "\n--------------------------\n"
8 (List.map (fun (i, context, term) ->
9 (string_of_int i) ^ " [\n" ^ (CicPp.ppcontext context) ^
10 "\n] " ^ (CicPp.ppterm term))
15 let print_subst ?(prefix="\n") subst =
18 (fun (i, (c, t, ty)) ->
19 Printf.sprintf "?%d -> %s : %s" i
20 (CicPp.ppterm t) (CicPp.ppterm ty))
24 (* (weight of constants, [(meta, weight_of_meta)]) *)
25 type weight = int * (int * int) list;;
27 let string_of_weight (cw, mw) =
30 (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
32 Printf.sprintf "[%d; %s]" cw s
35 let weight_of_term ?(consider_metas=true) term =
36 (* ALB: what to consider as a variable? I think "variables" in our case are
37 Metas and maybe Rels... *)
39 let vars_dict = Hashtbl.create 5 in
40 let rec aux = function
41 | C.Meta (metano, _) when consider_metas ->
43 let oldw = Hashtbl.find vars_dict metano in
44 Hashtbl.replace vars_dict metano (oldw+1)
46 Hashtbl.add vars_dict metano 1);
48 | C.Meta _ -> 0 (* "variables" are lighter than constants and functions...*)
52 | C.MutInd (_, _, ens)
53 | C.MutConstruct (_, _, _, ens) ->
54 List.fold_left (fun w (u, t) -> (aux t) + w) 1 ens
57 | C.Lambda (_, t1, t2)
59 | C.LetIn (_, t1, t2) ->
64 | C.Appl l -> List.fold_left (+) 0 (List.map aux l)
66 | C.MutCase (_, _, outt, t, pl) ->
69 let w3 = List.fold_left (+) 0 (List.map aux pl) in
73 List.fold_left (fun w (n, i, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
76 List.fold_left (fun w (n, t1, t2) -> (aux t1) + (aux t2) + w) 1 fl
82 Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] in
85 | (m1, _), (m2, _) -> m2 - m1
87 (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
91 let compute_equality_weight ty left right =
94 let w, m = (weight_of_term ~consider_metas:true(* false *) t) in
95 (* let mw = List.fold_left (fun mw (_, c) -> mw + 2 * c) 0 m in *)
96 (* metasw := !metasw + mw; *)
97 metasw := !metasw + (2 * (List.length m));
100 (weight_of ty) + (weight_of left) + (weight_of right) + !metasw
104 (* returns a "normalized" version of the polynomial weight wl (with type
105 * weight list), i.e. a list sorted ascending by meta number,
106 * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
107 * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
108 * (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
109 let normalize_weight maxmeta (cw, wl) =
110 (* Printf.printf "normalize_weight: %d, %s\n" maxmeta *)
111 (* (string_of_weight (cw, wl)); *)
112 let rec aux = function
114 | m -> (m, 0)::(aux (m-1))
116 let tmpl = aux maxmeta in
119 (fun (m, _) (n, _) -> Pervasives.compare m n)
121 (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
127 let normalize_weights (cw1, wl1) (cw2, wl2) =
128 let rec aux wl1 wl2 =
131 | (m, w)::tl1, (n, w')::tl2 when m = n ->
132 let res1, res2 = aux tl1 tl2 in
133 (m, w)::res1, (n, w')::res2
134 | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
135 let res1, res2 = aux tl1 wl2 in
136 (m, w)::res1, (m, 0)::res2
137 | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
138 let res1, res2 = aux wl1 tl2 in
139 (n, 0)::res1, (n, w')::res2
141 let res1, res2 = aux [] tl2 in
142 (n, 0)::res1, (n, w)::res2
144 let res1, res2 = aux tl1 [] in
145 (m, w)::res1, (m, 0)::res2
147 let cmp (m, _) (n, _) = compare m n in
148 let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
149 (cw1, wl1), (cw2, wl2)
153 type comparison = Lt | Le | Eq | Ge | Gt | Incomparable;;
155 let string_of_comparison = function
161 | Incomparable -> "I"
164 let compare_weights ?(normalize=false)
165 ((h1, w1) as weight1) ((h2, w2) as weight2)=
166 let (h1, w1), (h2, w2) =
169 (* let maxmeta l = *)
171 (* match List.hd l with *)
173 (* with Failure _ -> 0 *)
175 (* max (maxmeta w1) (maxmeta w2) *)
177 (* (normalize_weight maxmeta (h1, w1)), (normalize_weight maxmeta (h2, w2)) *)
178 normalize_weights weight1 weight2
185 (fun ((lt, eq, gt), diffs) w1 w2 ->
187 | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
188 let diffs = (w1 - w2) + diffs in
189 let r = compare w1 w2 in
190 if r < 0 then (lt+1, eq, gt), diffs
191 else if r = 0 then (lt, eq+1, gt), diffs
192 else (lt, eq, gt+1), diffs
193 | (meta1, w1), (meta2, w2) ->
194 Printf.printf "HMMM!!!! %s, %s\n"
195 (string_of_weight weight1) (string_of_weight weight2);
198 with Invalid_argument _ ->
199 Printf.printf "Invalid_argument: %s{%s}, %s{%s}, normalize = %s\n"
200 (string_of_weight (h1, w1)) (string_of_weight weight1)
201 (string_of_weight (h2, w2)) (string_of_weight weight2)
202 (string_of_bool normalize);
205 let hdiff = h1 - h2 in
209 else if hdiff > 0 then Gt
210 else Eq (* Incomparable *)
213 if m > 0 || hdiff < 0 then Lt
214 else if diffs >= (- hdiff) then Le else Incomparable
216 if diffs >= (- hdiff) then Le else Incomparable
219 if m > 0 || hdiff > 0 then Gt
220 else if (- diffs) >= hdiff then Ge else Incomparable
222 if (- diffs) >= hdiff then Ge else Incomparable
223 | (m, _, n) when m > 0 && n > 0 ->
228 let rec aux_ordering t1 t2 =
229 let module C = Cic in
230 let compare_uris u1 u2 =
232 compare (UriManager.string_of_uri u1) (UriManager.string_of_uri u2) in
234 else if res = 0 then Eq
239 | _, C.Meta _ -> Incomparable
241 | t1, t2 when t1 = t2 -> Eq
243 | C.Rel n, C.Rel m -> if n > m then Lt else Gt
247 | C.Const (u1, _), C.Const (u2, _) -> compare_uris u1 u2
251 | C.MutInd (u1, _, _), C.MutInd (u2, _, _) -> compare_uris u1 u2
252 | C.MutInd _, _ -> Lt
253 | _, C.MutInd _ -> Gt
255 | C.MutConstruct (u1, _, _, _), C.MutConstruct (u2, _, _, _) ->
257 | C.MutConstruct _, _ -> Lt
258 | _, C.MutConstruct _ -> Gt
260 | C.Appl l1, C.Appl l2 ->
266 | hd1::tl1, hd2::tl2 ->
267 let o = aux_ordering hd1 hd2 in
268 if o = Eq then cmp tl1 tl2
274 Printf.printf "These two terms are not comparable:\n%s\n%s\n\n"
275 (CicPp.ppterm t1) (CicPp.ppterm t2);
280 (* w1, w2 are the weights, they should already be normalized... *)
281 let nonrec_kbo_w (t1, w1) (t2, w2) =
282 match compare_weights w1 w2 with
283 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
284 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
285 | Eq -> aux_ordering t1 t2
290 let nonrec_kbo t1 t2 =
291 let w1 = weight_of_term t1 in
292 let w2 = weight_of_term t2 in
293 match compare_weights ~normalize:true w1 w2 with
294 | Le -> if aux_ordering t1 t2 = Lt then Lt else Incomparable
295 | Ge -> if aux_ordering t1 t2 = Gt then Gt else Incomparable
296 | Eq -> aux_ordering t1 t2
301 let names_of_context context =
305 | Some (n, e) -> Some n)
314 let compare = Pervasives.compare
317 module TermSet = Set.Make(OrderedTerm);;
318 module TermMap = Map.Make(OrderedTerm);;
320 let symbols_of_term term =
321 let module C = Cic in
322 let rec aux map = function
325 List.fold_left (fun res t -> (aux res t)) map l
329 let c = TermMap.find t map in
330 TermMap.add t (c+1) map
336 aux TermMap.empty term
340 let metas_of_term term =
341 let module C = Cic in
342 let rec aux = function
343 | C.Meta _ as t -> TermSet.singleton t
345 List.fold_left (fun res t -> TermSet.union res (aux t)) TermSet.empty l
346 | t -> TermSet.empty (* TODO: maybe add other cases? *)
353 let module C = Cic in
355 | t1, t2 when t1 = t2 -> Eq
356 | t1, (C.Meta _ as m) ->
357 if TermSet.mem m (metas_of_term t1) then Gt else Incomparable
358 | (C.Meta _ as m), t2 ->
359 if TermSet.mem m (metas_of_term t2) then Lt else Incomparable
360 | C.Appl (hd1::tl1), C.Appl (hd2::tl2) -> (
368 let res1 = List.fold_left (f t2) false tl1 in
370 else let res2 = List.fold_left (f t1) false tl2 in
374 if res <> Incomparable then
378 if not r then false else
383 match aux_ordering hd1 hd2 with
385 let res = List.fold_left (f t1) false tl2 in
389 let res = List.fold_left (f t2) false tl1 in
396 (fun r t1 t2 -> if r <> Eq then r else lpo t1 t2)
398 with Invalid_argument _ ->
403 if List.fold_left (f t1) false tl2 then Gt
406 if List.fold_left (f t2) false tl1 then Lt
412 | t1, t2 -> aux_ordering t1 t2
416 (* settable by the user... *)
417 let compare_terms = ref nonrec_kbo;;
420 type equality_sign = Negative | Positive;;
422 let string_of_sign = function
423 | Negative -> "Negative"
424 | Positive -> "Positive"
428 type pos = Left | Right
430 let string_of_pos = function