2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
14 type aux_comparison = XEQ | XLE | XGE | XLT | XGT | XINCOMPARABLE
20 (* This order relation should be:
21 * - stable for instantiation
22 * - total on ground terms
26 t Terms.foterm -> t Terms.foterm -> Terms.comparison
28 val compute_unit_clause_weight : 't Terms.unit_clause -> int
30 val compute_goal_weight : 't Terms.unit_clause -> int
36 type weight = int * (int * int) list;;
38 let rec eq_foterm f x y =
41 | Terms.Leaf t1, Terms.Leaf t2 -> f t1 t2
42 | Terms.Var i, Terms.Var j -> i = j
43 | Terms.Node l1, Terms.Node l2 -> List.for_all2 (eq_foterm f) l1 l2
47 let string_of_weight (cw, mw) =
50 (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
52 Printf.sprintf "[%d; %s]" cw s
55 let weight_of_term term =
56 let vars_dict = Hashtbl.create 5 in
57 let rec aux = function
60 let oldw = Hashtbl.find vars_dict i in
61 Hashtbl.replace vars_dict i (oldw+1)
63 Hashtbl.add vars_dict i 1);
66 | Terms.Node l -> List.fold_left (+) 0 (List.map aux l)
70 Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict []
74 | (m1, _), (m2, _) -> m1 - m2
76 (w, List.sort compare l) (* from the smallest meta to the bigest *)
79 let compute_unit_clause_weight (_,l, _, _) =
80 let weight_of_polynomial w m =
82 w + factor * List.fold_left (fun acc (_,occ) -> acc+occ) 0 m
85 | Terms.Predicate t ->
86 let w, m = weight_of_term t in
87 weight_of_polynomial w m
88 | Terms.Equation (_,x,_,Terms.Lt)
89 | Terms.Equation (x,_,_,Terms.Gt) ->
90 let w, m = weight_of_term x in
91 weight_of_polynomial w m
92 | Terms.Equation (l,r,_,Terms.Eq)
93 | Terms.Equation (l,r,_,Terms.Incomparable) ->
94 let wl, ml = weight_of_term l in
95 let wr, mr = weight_of_term r in
96 weight_of_polynomial (wl+wr) (ml@mr)
100 let compute_goal_weight (_,l, _, _) =
101 let weight_of_polynomial w m =
103 w + factor * List.fold_left (fun acc (_,occ) -> acc+occ) 0 m
106 | Terms.Predicate t ->
107 let w, m = weight_of_term t in
108 weight_of_polynomial w m
109 | Terms.Equation (l,r,_,_) ->
110 let wl, ml = weight_of_term l in
111 let wr, mr = weight_of_term r in
112 let wl = weight_of_polynomial wl ml in
113 let wr = weight_of_polynomial wr mr in
117 let compute_goal_weight = compute_unit_clause_weight;;
119 (* Riazanov: 3.1.5 pag 38 *)
120 (* Compare weights normalized in a new way :
121 * Variables should be sorted from the lowest index to the highest
122 * Variables which do not occur in the term should not be present
123 * in the normalized polynomial
125 let compare_weights (h1, w1) (h2, w2) =
126 let rec aux hdiff (lt, gt) diffs w1 w2 =
128 | ((var1, w1)::tl1) as l1, (((var2, w2)::tl2) as l2) ->
130 let diffs = (w1 - w2) + diffs in
131 let r = Pervasives.compare w1 w2 in
132 let lt = lt or (r < 0) in
133 let gt = gt or (r > 0) in
134 if lt && gt then XINCOMPARABLE else
135 aux hdiff (lt, gt) diffs tl1 tl2
136 else if var1 < var2 then
137 if lt then XINCOMPARABLE else
138 aux hdiff (false,true) (diffs+w1) tl1 l2
140 if gt then XINCOMPARABLE else
141 aux hdiff (true,false) (diffs-w2) l1 tl2
143 if gt then XINCOMPARABLE else
144 aux hdiff (true,false) (diffs-w2) [] tl2
146 if lt then XINCOMPARABLE else
147 aux hdiff (false,true) (diffs+w1) tl1 []
150 if hdiff <= 0 then XLT
151 else if (- diffs) >= hdiff then XLE else XINCOMPARABLE
153 if hdiff >= 0 then XGT
154 else if diffs >= (- hdiff) then XGE else XINCOMPARABLE
156 if hdiff < 0 then XLT
157 else if hdiff > 0 then XGT
160 aux (h1-h2) (false,false) 0 w1 w2
163 (* Riazanov: p. 40, relation >>>
164 * if head_only=true then it is not >>> but helps case 2 of 3.14 p 39 *)
165 let rec aux_ordering b_compare ?(head_only=false) t1 t2 =
167 (* We want to discard any identity equality. *
168 * If we give back XEQ, no inference rule *
169 * will be applied on this equality *)
170 | Terms.Var i, Terms.Var j when i = j ->
174 | _, Terms.Var _ -> XINCOMPARABLE
176 | Terms.Leaf a1, Terms.Leaf a2 ->
177 let cmp = b_compare a1 a2 in
178 if cmp = 0 then XEQ else if cmp < 0 then XLT else XGT
179 | Terms.Leaf _, Terms.Node _ -> XLT
180 | Terms.Node _, Terms.Leaf _ -> XGT
182 | Terms.Node l1, Terms.Node l2 ->
186 | _, [] -> (* XGT *) assert false (* hd symbols were eq *)
187 | [], _ -> (* XLT *) assert false (* hd symbols were eq *)
188 | hd1::tl1, hd2::tl2 ->
189 let o = aux_ordering b_compare ~head_only hd1 hd2 in
190 if o = XEQ && not head_only then cmp tl1 tl2 else o
195 let compare_terms o x y =
197 | XINCOMPARABLE -> Terms.Incomparable
204 module NRKBO (B : Terms.Blob) = struct
210 let eq_foterm = eq_foterm B.eq;;
212 let compute_unit_clause_weight = compute_unit_clause_weight;;
213 let compute_goal_weight = compute_goal_weight;;
215 (* Riazanov: p. 40, relation >_n *)
216 let nonrec_kbo t1 t2 =
217 let w1 = weight_of_term t1 in
218 let w2 = weight_of_term t2 in
219 match compare_weights w1 w2 with
220 | XLE -> (* this is .> *)
221 if aux_ordering B.compare t1 t2 = XLT then XLT else XINCOMPARABLE
223 if aux_ordering B.compare t1 t2 = XGT then XGT else XINCOMPARABLE
224 | XEQ -> aux_ordering B.compare t1 t2
228 let compare_terms = compare_terms nonrec_kbo;;
230 let profiler = HExtlib.profile ~enable:true "compare_terms(nrkbo)";;
231 let compare_terms x y =
232 profiler.HExtlib.profile (compare_terms x) y
237 module KBO (B : Terms.Blob) = struct
243 let eq_foterm = eq_foterm B.eq;;
245 let compute_unit_clause_weight = compute_unit_clause_weight;;
246 let compute_goal_weight = compute_goal_weight;;
248 (* Riazanov: p. 38, relation > *)
250 let aux = aux_ordering B.compare ~head_only:true in
256 | hd1::tl1, hd2::tl2 ->
257 let o = kbo hd1 hd2 in
258 if o = XEQ then cmp tl1 tl2
261 let w1 = weight_of_term t1 in
262 let w2 = weight_of_term t2 in
263 let comparison = compare_weights w1 w2 in
264 match comparison with
268 else if r = XEQ then (
270 | Terms.Node (_::tl1), Terms.Node (_::tl2) ->
271 if cmp tl1 tl2 = XLT then XLT else XINCOMPARABLE
272 | _, _ -> assert false
277 else if r = XEQ then (
279 | Terms.Node (_::tl1), Terms.Node (_::tl2) ->
280 if cmp tl1 tl2 = XGT then XGT else XINCOMPARABLE
281 | _, _ -> assert false
287 | Terms.Node (_::tl1), Terms.Node (_::tl2) -> cmp tl1 tl2
288 | _, _ -> XINCOMPARABLE
293 let compare_terms = compare_terms kbo;;
295 let profiler = HExtlib.profile ~enable:true "compare_terms(kbo)";;
296 let compare_terms x y =
297 profiler.HExtlib.profile (compare_terms x) y
302 module LPO (B : Terms.Blob) = struct
308 let eq_foterm = eq_foterm B.eq;;
310 let compute_unit_clause_weight = compute_unit_clause_weight;;
311 let compute_goal_weight = compute_goal_weight;;
315 | s, t when eq_foterm s t ->
317 | Terms.Var _, Terms.Var _ ->
320 if (List.mem i (Terms.vars_of_term s)) then XGT
323 if (List.mem i (Terms.vars_of_term t)) then XLT
325 | Terms.Node (hd1::tl1), Terms.Node (hd2::tl2) ->
326 let rec ge_subterm t ol = function
331 | XGT | XEQ -> (true,res::ol)
332 | o -> ge_subterm t (o::ol) tl
334 let (res, l_ol) = ge_subterm t [] tl1 in
336 else let (res, r_ol) = ge_subterm s [] tl2 in
339 let rec check_subterms t = function
342 if o = XLT then check_subterms t (ol,tl)
345 if lpo x t = XLT then check_subterms t ([],tl)
348 match aux_ordering B.compare hd1 hd2 with
349 | XGT -> if check_subterms s (r_ol,tl2) then XGT
351 | XLT -> if check_subterms t (l_ol,tl1) then XLT
354 let lex = List.fold_left2
355 (fun acc si ti -> if acc = XEQ then lpo si ti else acc)
360 if List.for_all (fun x -> lpo s x = XGT) tl2 then XGT
363 if List.for_all (fun x -> lpo x t = XLT) tl1 then XLT
366 | XINCOMPARABLE -> XINCOMPARABLE
369 | _,_ -> aux_ordering B.compare s t
373 let compare_terms = compare_terms lpo;;
375 let profiler = HExtlib.profile ~enable:true "compare_terms(lpo)";;
376 let compare_terms x y =
377 profiler.HExtlib.profile (compare_terms x) y