]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/orderings.ml
f2151528f698df8472f17c2d608bb2e03a5729ed
[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_clause_weight : 't Terms.clause -> int
29
30     val name : string
31
32   end
33   
34 type weight = int * (int * int) list;;
35   
36 let rec eq_foterm f x y =
37     x == y ||
38     match x, y with
39     | Terms.Leaf t1, Terms.Leaf t2 -> f t1 t2
40     | Terms.Var i, Terms.Var j -> i = j
41     | Terms.Node l1, Terms.Node l2 -> List.for_all2 (eq_foterm f) l1 l2
42     | _ -> false
43 ;;
44   
45 let string_of_weight (cw, mw) =
46   let s =
47     String.concat ", "
48       (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
49   in
50   Printf.sprintf "[%d; %s]" cw s
51 ;;
52   
53 let weight_of_term term =
54     let vars_dict = Hashtbl.create 5 in
55     let rec aux = function
56       | Terms.Var i -> 
57           (try
58              let oldw = Hashtbl.find vars_dict i in
59              Hashtbl.replace vars_dict i (oldw+1)
60            with Not_found ->
61              Hashtbl.add vars_dict i 1);
62           0
63       | Terms.Leaf _ -> 1
64       | Terms.Node l -> List.fold_left (+) 0 (List.map aux l)
65     in
66     let w = aux term in
67     let l =
68       Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] 
69     in
70     let compare w1 w2 = 
71       match w1, w2 with
72       | (m1, _), (m2, _) -> m1 - m2
73     in 
74     (w, List.sort compare l) (* from the smallest meta to the bigest *)
75 ;;
76   
77 let compute_literal_weight l =
78     let weight_of_polynomial w m =
79       let factor = 2 in      
80       w + factor * List.fold_left (fun acc (_,occ) -> acc+occ) 0 m
81     in
82     match l with
83     | Terms.Predicate t -> 
84         let w, m = weight_of_term t in 
85         weight_of_polynomial w m
86     | Terms.Equation (_,x,_,Terms.Lt) 
87     | Terms.Equation (x,_,_,Terms.Gt) ->
88         let w, m = weight_of_term x in 
89         weight_of_polynomial w m
90     | Terms.Equation (l,r,_,Terms.Eq) 
91     | Terms.Equation (l,r,_,Terms.Incomparable) ->
92         let wl, ml = weight_of_term l in 
93         let wr, mr = weight_of_term r in 
94         weight_of_polynomial (wl+wr) (ml@mr)
95 ;;
96
97 let compute_clause_weight (_,nl,pl,_,_) =
98   List.fold_left (fun acc lit -> compute_literal_weight lit + acc) 0 (nl@pl)
99
100 let compute_goal_weight = compute_clause_weight;;
101   
102 (* Riazanov: 3.1.5 pag 38 *)
103 (* Compare weights normalized in a new way :
104  * Variables should be sorted from the lowest index to the highest
105  * Variables which do not occur in the term should not be present
106  * in the normalized polynomial
107  *)
108 let compare_weights (h1, w1) (h2, w2) =
109   let rec aux hdiff (lt, gt) diffs w1 w2 =
110     match w1, w2 with
111       | ((var1, w1)::tl1) as l1, (((var2, w2)::tl2) as l2) ->
112           if var1 = var2 then
113             let diffs = (w1 - w2) + diffs in
114             let r = Pervasives.compare w1 w2 in
115             let lt = lt or (r < 0) in
116             let gt = gt or (r > 0) in
117               if lt && gt then XINCOMPARABLE else
118                 aux hdiff (lt, gt) diffs tl1 tl2
119           else if var1 < var2 then
120             if lt then XINCOMPARABLE else
121               aux hdiff (false,true) (diffs+w1) tl1 l2        
122           else
123             if gt then XINCOMPARABLE else
124               aux hdiff (true,false) (diffs-w2) l1 tl2
125       | [], (_,w2)::tl2 ->
126           if gt then XINCOMPARABLE else
127             aux hdiff (true,false) (diffs-w2) [] tl2
128       | (_,w1)::tl1, [] ->
129           if lt then XINCOMPARABLE else
130             aux hdiff (false,true) (diffs+w1) tl1 []
131       | [], [] ->
132           if lt then
133             if hdiff <= 0 then XLT
134             else if (- diffs) >= hdiff then XLE else XINCOMPARABLE
135           else if gt then
136             if hdiff >= 0 then XGT
137             else if diffs >= (- hdiff) then XGE else XINCOMPARABLE
138           else
139             if hdiff < 0 then XLT
140             else if hdiff > 0 then XGT
141             else XEQ
142   in
143     aux (h1-h2) (false,false) 0 w1 w2
144 ;;
145
146 (* Riazanov: p. 40, relation >>> 
147  * if head_only=true then it is not >>> but helps case 2 of 3.14 p 39 *)
148 let rec aux_ordering b_compare ?(head_only=false) t1 t2 =
149   match t1, t2 with
150   (* We want to discard any identity equality. *
151    * If we give back XEQ, no inference rule    *
152    * will be applied on this equality          *)
153   | Terms.Var i, Terms.Var j when i = j ->
154       XEQ
155   (* 1. *)
156   | Terms.Var _, _
157   | _, Terms.Var _ -> XINCOMPARABLE
158   (* 2.a *)
159   | Terms.Leaf a1, Terms.Leaf a2 -> 
160       let cmp = b_compare a1 a2 in
161       if cmp = 0 then XEQ else if cmp < 0 then XLT else XGT
162   | Terms.Leaf _, Terms.Node _ -> XLT
163   | Terms.Node _, Terms.Leaf _ -> XGT
164   (* 2.b *)
165   | Terms.Node l1, Terms.Node l2 ->
166       let rec cmp t1 t2 =
167         match t1, t2 with
168         | [], [] -> XEQ
169         | _, [] -> (* XGT *) assert false (* hd symbols were eq *)
170         | [], _ -> (* XLT *) assert false (* hd symbols were eq *)
171         | hd1::tl1, hd2::tl2 ->
172             let o = aux_ordering b_compare ~head_only hd1 hd2 in
173             if o = XEQ && not head_only then cmp tl1 tl2 else o
174       in
175       cmp l1 l2
176 ;;
177   
178 let compare_terms o x y = 
179     match o x y with
180       | XINCOMPARABLE -> Terms.Incomparable
181       | XGT -> Terms.Gt
182       | XLT -> Terms.Lt
183       | XEQ -> Terms.Eq
184       | _ -> assert false
185 ;;
186
187 module NRKBO (B : Terms.Blob) = struct
188   let name = "nrkbo"
189   include B 
190
191   module Pp = Pp.Pp(B)
192
193   let eq_foterm = eq_foterm B.eq;;
194
195   let compute_clause_weight = compute_clause_weight;;
196   let compute_goal_weight = compute_goal_weight;;
197   
198   (* Riazanov: p. 40, relation >_n *)
199   let nonrec_kbo t1 t2 =
200     let w1 = weight_of_term t1 in
201     let w2 = weight_of_term t2 in
202     match compare_weights w1 w2 with
203     | XLE ->  (* this is .> *)
204         if aux_ordering B.compare t1 t2 = XLT then XLT else XINCOMPARABLE
205     | XGE -> 
206         if aux_ordering B.compare t1 t2 = XGT then XGT else XINCOMPARABLE
207     | XEQ -> aux_ordering B.compare t1 t2
208     | res -> res
209   ;;
210
211   let compare_terms = compare_terms nonrec_kbo;;
212
213   let profiler = HExtlib.profile ~enable:true "compare_terms(nrkbo)";;
214   let compare_terms x y =
215     profiler.HExtlib.profile (compare_terms x) y
216   ;;
217
218 end
219   
220 module KBO (B : Terms.Blob) = struct
221   let name = "kbo"
222   include B 
223
224   module Pp = Pp.Pp(B)
225
226   let eq_foterm = eq_foterm B.eq;;
227
228   let compute_clause_weight = compute_clause_weight;;
229   let compute_goal_weight = compute_goal_weight;;
230
231   (* Riazanov: p. 38, relation > *)
232   let rec kbo t1 t2 =
233     let aux = aux_ordering B.compare ~head_only:true in
234     let rec cmp t1 t2 =
235       match t1, t2 with
236       | [], [] -> XEQ
237       | _, [] -> XGT
238       | [], _ -> XLT
239       | hd1::tl1, hd2::tl2 ->
240           let o = kbo hd1 hd2 in
241           if o = XEQ then cmp tl1 tl2
242           else o
243     in
244     let w1 = weight_of_term t1 in
245     let w2 = weight_of_term t2 in
246     let comparison = compare_weights w1 w2 in
247     match comparison with
248     | XLE ->
249         let r = aux t1 t2 in
250         if r = XLT then XLT
251         else if r = XEQ then (
252           match t1, t2 with
253           | Terms.Node (_::tl1), Terms.Node (_::tl2) ->
254               if cmp tl1 tl2 = XLT then XLT else XINCOMPARABLE
255           | _, _ -> assert false
256         ) else XINCOMPARABLE
257     | XGE ->
258         let r = aux t1 t2 in
259         if r = XGT then XGT
260         else if r = XEQ then (
261           match t1, t2 with
262           | Terms.Node (_::tl1), Terms.Node (_::tl2) ->
263               if cmp tl1 tl2 = XGT then XGT else XINCOMPARABLE
264           | _, _ ->  assert false
265         ) else XINCOMPARABLE
266     | XEQ ->
267         let r = aux t1 t2 in
268         if r = XEQ then (
269           match t1, t2 with
270           | Terms.Node (_::tl1), Terms.Node (_::tl2) -> cmp tl1 tl2
271           | _, _ ->  XINCOMPARABLE
272         ) else r 
273     | res -> res
274   ;;
275
276   let compare_terms = compare_terms kbo;;
277
278   let profiler = HExtlib.profile ~enable:true "compare_terms(kbo)";;
279   let compare_terms x y =
280     profiler.HExtlib.profile (compare_terms x) y
281   ;;
282
283 end
284
285 module LPO (B : Terms.Blob) = struct
286   let name = "lpo"
287   include B 
288
289   module Pp = Pp.Pp(B)
290
291   let eq_foterm = eq_foterm B.eq;;
292
293   let compute_clause_weight = compute_clause_weight;;
294   let compute_goal_weight = compute_goal_weight;;
295
296   let rec lpo s t =
297     match s,t with
298       | s, t when eq_foterm s t ->
299           XEQ
300       | Terms.Var _, Terms.Var _ ->
301           XINCOMPARABLE
302       | _, Terms.Var i ->
303           if (List.mem i (Terms.vars_of_term s)) then XGT
304           else XINCOMPARABLE
305       | Terms.Var i,_ ->
306           if (List.mem i (Terms.vars_of_term t)) then XLT
307           else XINCOMPARABLE
308       | Terms.Node (hd1::tl1), Terms.Node (hd2::tl2) ->
309           let rec ge_subterm t ol = function
310             | [] -> (false, ol)
311             | x::tl ->
312                 let res = lpo x t in
313                 match res with
314                   | XGT | XEQ -> (true,res::ol)
315                   | o -> ge_subterm t (o::ol) tl
316           in
317           let (res, l_ol) = ge_subterm t [] tl1 in
318             if res then XGT
319             else let (res, r_ol) = ge_subterm s [] tl2 in
320               if res then XLT
321               else begin
322                 let rec check_subterms t = function
323                   | _,[] -> true
324                   | o::ol,_::tl ->
325                       if o = XLT then check_subterms t (ol,tl)
326                       else false
327                   | [], x::tl ->
328                       if lpo x t = XLT then check_subterms t ([],tl)
329                       else false
330                 in
331                 match aux_ordering B.compare hd1 hd2 with
332                   | XGT -> if check_subterms s (r_ol,tl2) then XGT
333                     else XINCOMPARABLE
334                   | XLT -> if check_subterms t (l_ol,tl1) then XLT
335                     else XINCOMPARABLE
336                   | XEQ -> 
337                       let lex = List.fold_left2
338                         (fun acc si ti -> if acc = XEQ then lpo si ti else acc)
339                         XEQ tl1 tl2
340                       in
341                  (match lex with
342                     | XGT ->
343                         if List.for_all (fun x -> lpo s x = XGT) tl2 then XGT
344                       else XINCOMPARABLE
345                     | XLT ->
346                         if List.for_all (fun x -> lpo x t = XLT) tl1 then XLT
347                       else XINCOMPARABLE
348                     | o -> o)   
349               | XINCOMPARABLE -> XINCOMPARABLE
350               | _ -> assert false
351           end
352       | _,_ -> aux_ordering B.compare s t
353             
354   ;;
355
356   let compare_terms = compare_terms lpo;;
357
358   let profiler = HExtlib.profile ~enable:true "compare_terms(lpo)";;
359   let compare_terms x y =
360     profiler.HExtlib.profile (compare_terms x) y
361   ;;
362
363 end
364