]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/orderings.ml
ba6969aeb143ddaa1df97d72ea3ff61b4c2bcaab
[helm.git] / helm / software / components / ng_paramodulation / orderings.ml
1 (*
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.                     
5     ||I||                                                                
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_______________________________________________________________ *)
11
12 (* $Id$ *)
13
14 type aux_comparison = XEQ | XLE | XGE | XLT | XGT | XINCOMPARABLE
15
16 module type Blob =
17   sig 
18     include Terms.Blob 
19
20     (* This order relation should be:
21      * - stable for instantiation
22      * - total on ground terms
23      *
24      *)
25     val compare_terms : 
26           t Terms.foterm -> t Terms.foterm -> Terms.comparison
27
28     val compute_unit_clause_weight : 't Terms.unit_clause -> int
29
30     val compute_goal_weight : 't Terms.unit_clause -> int
31
32     val name : string
33
34   end
35   
36 type weight = int * (int * int) list;;
37   
38 let rec eq_foterm f x y =
39     x == y ||
40     match x, y with
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
44     | _ -> false
45 ;;
46   
47 let string_of_weight (cw, mw) =
48   let s =
49     String.concat ", "
50       (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
51   in
52   Printf.sprintf "[%d; %s]" cw s
53 ;;
54   
55 let weight_of_term term =
56     let vars_dict = Hashtbl.create 5 in
57     let rec aux = function
58       | Terms.Var i -> 
59           (try
60              let oldw = Hashtbl.find vars_dict i in
61              Hashtbl.replace vars_dict i (oldw+1)
62            with Not_found ->
63              Hashtbl.add vars_dict i 1);
64           0
65       | Terms.Leaf _ -> 1
66       | Terms.Node l -> List.fold_left (+) 0 (List.map aux l)
67     in
68     let w = aux term in
69     let l =
70       Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] 
71     in
72     let compare w1 w2 = 
73       match w1, w2 with
74       | (m1, _), (m2, _) -> m1 - m2
75     in 
76     (w, List.sort compare l) (* from the smallest meta to the bigest *)
77 ;;
78   
79 let compute_unit_clause_weight (_,l, _, _) = 
80     let weight_of_polynomial w m =
81       let factor = 2 in      
82       w + factor * List.fold_left (fun acc (_,occ) -> acc+occ) 0 m
83     in
84     match l with
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)
97 ;;
98
99 let compute_goal_weight (_,l, _, _) = 
100     let weight_of_polynomial w m =
101       let factor = 2 in      
102       w + factor * List.fold_left (fun acc (_,occ) -> acc+occ) 0 m
103     in
104     match l with
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
113           - (abs (wl-wr))
114   ;;
115   
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
121  *)
122 let compare_weights (h1, w1) (h2, w2) =
123   let rec aux hdiff (lt, gt) diffs w1 w2 =
124     match w1, w2 with
125       | ((var1, w1)::tl1) as l1, (((var2, w2)::tl2) as l2) ->
126           if var1 = var2 then
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        
136           else
137             if gt then XINCOMPARABLE else
138               aux hdiff (true,false) (diffs-w2) l1 tl2
139       | [], (_,w2)::tl2 ->
140           if gt then XINCOMPARABLE else
141             aux hdiff (true,false) (diffs-w2) [] tl2
142       | (_,w1)::tl1, [] ->
143           if lt then XINCOMPARABLE else
144             aux hdiff (false,true) (diffs+w1) tl1 []
145       | [], [] ->
146           if lt then
147             if hdiff <= 0 then XLT
148             else if (- diffs) >= hdiff then XLE else XINCOMPARABLE
149           else if gt then
150             if hdiff >= 0 then XGT
151             else if diffs >= (- hdiff) then XGE else XINCOMPARABLE
152           else
153             if hdiff < 0 then XLT
154             else if hdiff > 0 then XGT
155             else XEQ
156   in
157     aux (h1-h2) (false,false) 0 w1 w2
158 ;;
159
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 =
163   match t1, t2 with
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 ->
168       XEQ
169   (* 1. *)
170   | Terms.Var _, _
171   | _, Terms.Var _ -> XINCOMPARABLE
172   (* 2.a *)
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
178   (* 2.b *)
179   | Terms.Node l1, Terms.Node l2 ->
180       let rec cmp t1 t2 =
181         match t1, t2 with
182         | [], [] -> XEQ
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
188       in
189       cmp l1 l2
190 ;;
191   
192 let compare_terms o x y = 
193     match o x y with
194       | XINCOMPARABLE -> Terms.Incomparable
195       | XGT -> Terms.Gt
196       | XLT -> Terms.Lt
197       | XEQ -> Terms.Eq
198       | _ -> assert false
199 ;;
200
201 module NRKBO (B : Terms.Blob) = struct
202   let name = "nrkbo"
203   include B 
204
205   module Pp = Pp.Pp(B)
206
207   let eq_foterm = eq_foterm B.eq;;
208
209   let compute_unit_clause_weight = compute_unit_clause_weight;;
210   let compute_goal_weight = compute_goal_weight;;
211   
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
219     | XGE -> 
220         if aux_ordering B.compare t1 t2 = XGT then XGT else XINCOMPARABLE
221     | XEQ -> aux_ordering B.compare t1 t2
222     | res -> res
223   ;;
224
225   let compare_terms = compare_terms nonrec_kbo;;
226
227   let profiler = HExtlib.profile ~enable:true "compare_terms(nrkbo)";;
228   let compare_terms x y =
229     profiler.HExtlib.profile (compare_terms x) y
230   ;;
231
232 end
233   
234 module KBO (B : Terms.Blob) = struct
235   let name = "kbo"
236   include B 
237
238   module Pp = Pp.Pp(B)
239
240   let eq_foterm = eq_foterm B.eq;;
241
242   let compute_unit_clause_weight = compute_unit_clause_weight;;
243   let compute_goal_weight = compute_goal_weight;;
244
245   (* Riazanov: p. 38, relation > *)
246   let rec kbo t1 t2 =
247     let aux = aux_ordering B.compare ~head_only:true in
248     let rec cmp t1 t2 =
249       match t1, t2 with
250       | [], [] -> XEQ
251       | _, [] -> XGT
252       | [], _ -> XLT
253       | hd1::tl1, hd2::tl2 ->
254           let o = kbo hd1 hd2 in
255           if o = XEQ then cmp tl1 tl2
256           else o
257     in
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
262     | XLE ->
263         let r = aux t1 t2 in
264         if r = XLT then XLT
265         else if r = XEQ then (
266           match t1, t2 with
267           | Terms.Node (_::tl1), Terms.Node (_::tl2) ->
268               if cmp tl1 tl2 = XLT then XLT else XINCOMPARABLE
269           | _, _ -> assert false
270         ) else XINCOMPARABLE
271     | XGE ->
272         let r = aux t1 t2 in
273         if r = XGT then XGT
274         else if r = XEQ then (
275           match t1, t2 with
276           | Terms.Node (_::tl1), Terms.Node (_::tl2) ->
277               if cmp tl1 tl2 = XGT then XGT else XINCOMPARABLE
278           | _, _ ->  assert false
279         ) else XINCOMPARABLE
280     | XEQ ->
281         let r = aux t1 t2 in
282         if r = XEQ then (
283           match t1, t2 with
284           | Terms.Node (_::tl1), Terms.Node (_::tl2) -> cmp tl1 tl2
285           | _, _ ->  XINCOMPARABLE
286         ) else r 
287     | res -> res
288   ;;
289
290   let compare_terms = compare_terms kbo;;
291
292   let profiler = HExtlib.profile ~enable:true "compare_terms(kbo)";;
293   let compare_terms x y =
294     profiler.HExtlib.profile (compare_terms x) y
295   ;;
296
297 end
298
299 module LPO (B : Terms.Blob) = struct
300   let name = "lpo"
301   include B 
302
303   module Pp = Pp.Pp(B)
304
305   let eq_foterm = eq_foterm B.eq;;
306
307   let compute_unit_clause_weight = compute_unit_clause_weight;;
308   let compute_goal_weight = compute_goal_weight;;
309
310   let rec lpo s t =
311     match s,t with
312       | s, t when eq_foterm s t ->
313           XEQ
314       | Terms.Var _, Terms.Var _ ->
315           XINCOMPARABLE
316       | _, Terms.Var i ->
317           if (List.mem i (Terms.vars_of_term s)) then XGT
318           else XINCOMPARABLE
319       | Terms.Var i,_ ->
320           if (List.mem i (Terms.vars_of_term t)) then XLT
321           else XINCOMPARABLE
322       | Terms.Node (hd1::tl1), Terms.Node (hd2::tl2) ->
323           let rec ge_subterm t ol = function
324             | [] -> (false, ol)
325             | x::tl ->
326                 let res = lpo x t in
327                 match res with
328                   | XGT | XEQ -> (true,res::ol)
329                   | o -> ge_subterm t (o::ol) tl
330           in
331           let (res, l_ol) = ge_subterm t [] tl1 in
332             if res then XGT
333             else let (res, r_ol) = ge_subterm s [] tl2 in
334               if res then XLT
335               else begin
336                 let rec check_subterms t = function
337                   | _,[] -> true
338                   | o::ol,_::tl ->
339                       if o = XLT then check_subterms t (ol,tl)
340                       else false
341                   | [], x::tl ->
342                       if lpo x t = XLT then check_subterms t ([],tl)
343                       else false
344                 in
345                 match aux_ordering B.compare hd1 hd2 with
346                   | XGT -> if check_subterms s (r_ol,tl2) then XGT
347                     else XINCOMPARABLE
348                   | XLT -> if check_subterms t (l_ol,tl1) then XLT
349                     else XINCOMPARABLE
350                   | XEQ -> 
351                       let lex = List.fold_left2
352                         (fun acc si ti -> if acc = XEQ then lpo si ti else acc)
353                         XEQ tl1 tl2
354                       in
355                  (match lex with
356                     | XGT ->
357                         if List.for_all (fun x -> lpo s x = XGT) tl2 then XGT
358                       else XINCOMPARABLE
359                     | XLT ->
360                         if List.for_all (fun x -> lpo x t = XLT) tl1 then XLT
361                       else XINCOMPARABLE
362                     | o -> o)   
363               | XINCOMPARABLE -> XINCOMPARABLE
364               | _ -> assert false
365           end
366       | _,_ -> aux_ordering B.compare s t
367             
368   ;;
369
370   let compare_terms = compare_terms lpo;;
371
372   let profiler = HExtlib.profile ~enable:true "compare_terms(lpo)";;
373   let compare_terms x y =
374     profiler.HExtlib.profile (compare_terms x) y
375   ;;
376
377 end
378