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)
99 let compute_goal_weight (_,l, _, _) =
100 let weight_of_polynomial w m =
102 w + factor * List.fold_left (fun acc (_,occ) -> acc+occ) 0 m
105 | Terms.Predicate t ->
106 let w, m = weight_of_term t in
107 weight_of_polynomial w m
108 | Terms.Equation (l,r,_,_) ->
109 let wl, ml = weight_of_term l in
110 let wr, mr = weight_of_term r in
111 let wl = weight_of_polynomial wl ml in
112 let wr = weight_of_polynomial wr mr in
116 (* Riazanov: 3.1.5 pag 38 *)
117 (* Compare weights normalized in a new way :
118 * Variables should be sorted from the lowest index to the highest
119 * Variables which do not occur in the term should not be present
120 * in the normalized polynomial
122 let compare_weights (h1, w1) (h2, w2) =
123 let rec aux hdiff (lt, gt) diffs w1 w2 =
125 | ((var1, w1)::tl1) as l1, (((var2, w2)::tl2) as l2) ->
127 let diffs = (w1 - w2) + diffs in
128 let r = Pervasives.compare w1 w2 in
129 let lt = lt or (r < 0) in
130 let gt = gt or (r > 0) in
131 if lt && gt then XINCOMPARABLE else
132 aux hdiff (lt, gt) diffs tl1 tl2
133 else if var1 < var2 then
134 if lt then XINCOMPARABLE else
135 aux hdiff (false,true) (diffs+w1) tl1 l2
137 if gt then XINCOMPARABLE else
138 aux hdiff (true,false) (diffs-w2) l1 tl2
140 if gt then XINCOMPARABLE else
141 aux hdiff (true,false) (diffs-w2) [] tl2
143 if lt then XINCOMPARABLE else
144 aux hdiff (false,true) (diffs+w1) tl1 []
147 if hdiff <= 0 then XLT
148 else if (- diffs) >= hdiff then XLE else XINCOMPARABLE
150 if hdiff >= 0 then XGT
151 else if diffs >= (- hdiff) then XGE else XINCOMPARABLE
153 if hdiff < 0 then XLT
154 else if hdiff > 0 then XGT
157 aux (h1-h2) (false,false) 0 w1 w2
160 (* Riazanov: p. 40, relation >>>
161 * if head_only=true then it is not >>> but helps case 2 of 3.14 p 39 *)
162 let rec aux_ordering b_compare ?(head_only=false) t1 t2 =
164 (* We want to discard any identity equality. *
165 * If we give back XEQ, no inference rule *
166 * will be applied on this equality *)
167 | Terms.Var i, Terms.Var j when i = j ->
171 | _, Terms.Var _ -> XINCOMPARABLE
173 | Terms.Leaf a1, Terms.Leaf a2 ->
174 let cmp = b_compare a1 a2 in
175 if cmp = 0 then XEQ else if cmp < 0 then XLT else XGT
176 | Terms.Leaf _, Terms.Node _ -> XLT
177 | Terms.Node _, Terms.Leaf _ -> XGT
179 | Terms.Node l1, Terms.Node l2 ->
183 | _, [] -> (* XGT *) assert false (* hd symbols were eq *)
184 | [], _ -> (* XLT *) assert false (* hd symbols were eq *)
185 | hd1::tl1, hd2::tl2 ->
186 let o = aux_ordering b_compare ~head_only hd1 hd2 in
187 if o = XEQ && not head_only then cmp tl1 tl2 else o
192 let compare_terms o x y =
194 | XINCOMPARABLE -> Terms.Incomparable
201 module NRKBO (B : Terms.Blob) = struct
207 let eq_foterm = eq_foterm B.eq;;
209 let compute_unit_clause_weight = compute_unit_clause_weight;;
210 let compute_goal_weight = compute_goal_weight;;
212 (* Riazanov: p. 40, relation >_n *)
213 let nonrec_kbo t1 t2 =
214 let w1 = weight_of_term t1 in
215 let w2 = weight_of_term t2 in
216 match compare_weights w1 w2 with
217 | XLE -> (* this is .> *)
218 if aux_ordering B.compare t1 t2 = XLT then XLT else XINCOMPARABLE
220 if aux_ordering B.compare t1 t2 = XGT then XGT else XINCOMPARABLE
221 | XEQ -> aux_ordering B.compare t1 t2
225 let compare_terms = compare_terms nonrec_kbo;;
227 let profiler = HExtlib.profile ~enable:true "compare_terms(nrkbo)";;
228 let compare_terms x y =
229 profiler.HExtlib.profile (compare_terms x) y
234 module KBO (B : Terms.Blob) = struct
240 let eq_foterm = eq_foterm B.eq;;
242 let compute_unit_clause_weight = compute_unit_clause_weight;;
243 let compute_goal_weight = compute_goal_weight;;
245 (* Riazanov: p. 38, relation > *)
247 let aux = aux_ordering B.compare ~head_only:true in
253 | hd1::tl1, hd2::tl2 ->
254 let o = kbo hd1 hd2 in
255 if o = XEQ then cmp tl1 tl2
258 let w1 = weight_of_term t1 in
259 let w2 = weight_of_term t2 in
260 let comparison = compare_weights w1 w2 in
261 match comparison with
265 else if r = XEQ then (
267 | Terms.Node (_::tl1), Terms.Node (_::tl2) ->
268 if cmp tl1 tl2 = XLT then XLT else XINCOMPARABLE
269 | _, _ -> assert false
274 else if r = XEQ then (
276 | Terms.Node (_::tl1), Terms.Node (_::tl2) ->
277 if cmp tl1 tl2 = XGT then XGT else XINCOMPARABLE
278 | _, _ -> assert false
284 | Terms.Node (_::tl1), Terms.Node (_::tl2) -> cmp tl1 tl2
285 | _, _ -> XINCOMPARABLE
290 let compare_terms = compare_terms kbo;;
292 let profiler = HExtlib.profile ~enable:true "compare_terms(kbo)";;
293 let compare_terms x y =
294 profiler.HExtlib.profile (compare_terms x) y
299 module LPO (B : Terms.Blob) = struct
305 let eq_foterm = eq_foterm B.eq;;
307 let compute_unit_clause_weight = compute_unit_clause_weight;;
308 let compute_goal_weight = compute_goal_weight;;
312 | s, t when eq_foterm s t ->
314 | Terms.Var _, Terms.Var _ ->
317 if (List.mem i (Terms.vars_of_term s)) then XGT
320 if (List.mem i (Terms.vars_of_term t)) then XLT
322 | Terms.Node (hd1::tl1), Terms.Node (hd2::tl2) ->
323 let rec ge_subterm t ol = function
328 | XGT | XEQ -> (true,res::ol)
329 | o -> ge_subterm t (o::ol) tl
331 let (res, l_ol) = ge_subterm t [] tl1 in
333 else let (res, r_ol) = ge_subterm s [] tl2 in
336 let rec check_subterms t = function
339 if o = XLT then check_subterms t (ol,tl)
342 if lpo x t = XLT then check_subterms t ([],tl)
345 match aux_ordering B.compare hd1 hd2 with
346 | XGT -> if check_subterms s (r_ol,tl2) then XGT
348 | XLT -> if check_subterms t (l_ol,tl1) then XLT
351 let lex = List.fold_left2
352 (fun acc si ti -> if acc = XEQ then lpo si ti else acc)
357 if List.for_all (fun x -> lpo s x = XGT) tl2 then XGT
360 if List.for_all (fun x -> lpo x t = XLT) tl1 then XLT
363 | XINCOMPARABLE -> XINCOMPARABLE
366 | _,_ -> aux_ordering B.compare s t
370 let compare_terms = compare_terms lpo;;
372 let profiler = HExtlib.profile ~enable:true "compare_terms(lpo)";;
373 let compare_terms x y =
374 profiler.HExtlib.profile (compare_terms x) y