]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/orderings.ml
Reorganized foUtils, added Clauses module to avoid duplicate code around are_invertib...
[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 | XINVERTIBLE
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     | Terms.Equation (l,r,_,Terms.Invertible) ->
93         let wl, ml = weight_of_term l in 
94         let wr, mr = weight_of_term r in 
95         weight_of_polynomial (wl+wr) (ml@mr)
96 ;;
97
98 let compute_clause_weight (_,nl,pl,_,_) =
99   List.fold_left (fun acc (lit,_) -> compute_literal_weight lit + acc) 0 (nl@pl)
100
101 let compute_goal_weight = compute_clause_weight;;
102   
103 (* Riazanov: 3.1.5 pag 38 *)
104 (* Compare weights normalized in a new way :
105  * Variables should be sorted from the lowest index to the highest
106  * Variables which do not occur in the term should not be present
107  * in the normalized polynomial
108  *)
109 let compare_weights (h1, w1) (h2, w2) =
110   let rec aux hdiff (lt, gt) diffs w1 w2 =
111     match w1, w2 with
112       | ((var1, w1)::tl1) as l1, (((var2, w2)::tl2) as l2) ->
113           if var1 = var2 then
114             let diffs = (w1 - w2) + diffs in
115             let r = Pervasives.compare w1 w2 in
116             let lt = lt or (r < 0) in
117             let gt = gt or (r > 0) in
118               if lt && gt then XINCOMPARABLE else
119                 aux hdiff (lt, gt) diffs tl1 tl2
120           else if var1 < var2 then
121             if lt then XINCOMPARABLE else
122               aux hdiff (false,true) (diffs+w1) tl1 l2        
123           else
124             if gt then XINCOMPARABLE else
125               aux hdiff (true,false) (diffs-w2) l1 tl2
126       | [], (_,w2)::tl2 ->
127           if gt then XINCOMPARABLE else
128             aux hdiff (true,false) (diffs-w2) [] tl2
129       | (_,w1)::tl1, [] ->
130           if lt then XINCOMPARABLE else
131             aux hdiff (false,true) (diffs+w1) tl1 []
132       | [], [] ->
133           if lt then
134             if hdiff <= 0 then XLT
135             else if (- diffs) >= hdiff then XLE else XINCOMPARABLE
136           else if gt then
137             if hdiff >= 0 then XGT
138             else if diffs >= (- hdiff) then XGE else XINCOMPARABLE
139           else
140             if hdiff < 0 then XLT
141             else if hdiff > 0 then XGT
142             else XEQ
143   in
144     aux (h1-h2) (false,false) 0 w1 w2
145 ;;
146
147 (* Riazanov: p. 40, relation >>> 
148  * if head_only=true then it is not >>> but helps case 2 of 3.14 p 39 *)
149 let rec aux_ordering b_compare ?(head_only=false) t1 t2 =
150   match t1, t2 with
151   (* We want to discard any identity equality. *
152    * If we give back XEQ, no inference rule    *
153    * will be applied on this equality          *)
154   | Terms.Var i, Terms.Var j when i = j ->
155       XEQ
156   (* 1. *)
157   | Terms.Var _, _
158   | _, Terms.Var _ -> XINCOMPARABLE
159   (* 2.a *)
160   | Terms.Leaf a1, Terms.Leaf a2 -> 
161       let cmp = b_compare a1 a2 in
162       if cmp = 0 then XEQ else if cmp < 0 then XLT else XGT
163   | Terms.Leaf _, Terms.Node _ -> XLT
164   | Terms.Node _, Terms.Leaf _ -> XGT
165   (* 2.b *)
166   | Terms.Node l1, Terms.Node l2 ->
167       let rec cmp t1 t2 =
168         match t1, t2 with
169         | [], [] -> XEQ
170         | _, [] -> (* XGT *) assert false (* hd symbols were eq *)
171         | [], _ -> (* XLT *) assert false (* hd symbols were eq *)
172         | hd1::tl1, hd2::tl2 ->
173             let o = aux_ordering b_compare ~head_only hd1 hd2 in
174             if o = XEQ && not head_only then cmp tl1 tl2 else o
175       in
176       cmp l1 l2
177 ;;
178   
179 let compare_terms o x y = 
180     match o x y with
181       | XINCOMPARABLE -> Terms.Incomparable
182       | XGT -> Terms.Gt
183       | XLT -> Terms.Lt
184       | XEQ -> Terms.Eq
185       | XINVERTIBLE -> Terms.Invertible
186       | _ -> assert false
187 ;;
188
189 let are_invertible relocate alpha_eq l r =
190     let varlist = Terms.vars_of_term l in
191     let maxvar = List.fold_left max 0 varlist in
192     let _,_,subst = relocate maxvar varlist FoSubst.id_subst in
193     let l = FoSubst.apply_subst subst l in
194       try (ignore(alpha_eq l r);true) with
195           FoUnif.UnificationFailure _ -> false;;
196
197 module NRKBO (B : Terms.Blob) = struct
198   let name = "nrkbo"
199   include B 
200
201   module Pp = Pp.Pp(B)
202   module Unif = FoUnif.FoUnif(B)
203   module Utils = FoUtils.Utils(B)
204
205   let eq_foterm = eq_foterm B.eq;;
206
207   let are_invertible = are_invertible Utils.relocate Unif.alpha_eq;;
208
209   let compute_clause_weight = compute_clause_weight;;
210   
211   (* Riazanov: p. 40, relation >_n *)
212   let nonrec_kbo t1 t2 =
213     let w1 = weight_of_term t1 in
214     let w2 = weight_of_term t2 in
215     match compare_weights w1 w2 with
216     | XLE ->  (* this is .> *)
217         if aux_ordering B.compare t1 t2 = XLT then XLT else XINCOMPARABLE
218     | XGE -> 
219         if aux_ordering B.compare t1 t2 = XGT then XGT else XINCOMPARABLE
220     | XEQ -> let res = aux_ordering B.compare t1 t2 in
221         if res = XINCOMPARABLE && are_invertible t1 t2 then XINVERTIBLE
222         else res
223     | res -> res
224   ;;
225
226   let compare_terms = compare_terms nonrec_kbo;;
227
228   let profiler = HExtlib.profile ~enable:true "compare_terms(nrkbo)";;
229   let compare_terms x y =
230     profiler.HExtlib.profile (compare_terms x) y
231   ;;
232
233 end
234   
235 module KBO (B : Terms.Blob) = struct
236   let name = "kbo"
237   include B 
238
239   module Pp = Pp.Pp(B)
240   module Unif = FoUnif.FoUnif(B)
241   module Utils = FoUtils.Utils(B)
242
243   let eq_foterm = eq_foterm B.eq;;
244
245   let are_invertible = are_invertible Utils.relocate Unif.alpha_eq;;
246
247   let compute_clause_weight = compute_clause_weight;;
248   let compute_goal_weight = compute_goal_weight;;
249
250   (* Riazanov: p. 38, relation > *)
251   let rec kbo t1 t2 =
252     let aux = aux_ordering B.compare ~head_only:true in
253     let rec cmp t1 t2 =
254       match t1, t2 with
255       | [], [] -> XEQ
256       | _, [] -> XGT
257       | [], _ -> XLT
258       | hd1::tl1, hd2::tl2 ->
259           let o = kbo hd1 hd2 in
260           if o = XEQ then cmp tl1 tl2
261           else o
262     in
263     let w1 = weight_of_term t1 in
264     let w2 = weight_of_term t2 in
265     let comparison = compare_weights w1 w2 in
266     match comparison with
267     | XLE ->
268         let r = aux t1 t2 in
269         if r = XLT then XLT
270         else if r = XEQ then (
271           match t1, t2 with
272           | Terms.Node (_::tl1), Terms.Node (_::tl2) ->
273               if cmp tl1 tl2 = XLT then XLT else XINCOMPARABLE
274           | _, _ -> assert false
275         ) else XINCOMPARABLE
276     | XGE ->
277         let r = aux t1 t2 in
278         if r = XGT then XGT
279         else if r = XEQ then (
280           match t1, t2 with
281           | Terms.Node (_::tl1), Terms.Node (_::tl2) ->
282               if cmp tl1 tl2 = XGT then XGT else XINCOMPARABLE
283           | _, _ ->  assert false
284         ) else XINCOMPARABLE
285     | XEQ ->
286         let r = aux t1 t2 in
287         if r = XEQ then (
288           match t1, t2 with
289           | Terms.Var i, Terms.Var j when i=j -> XEQ
290           | Terms.Node (_::tl1), Terms.Node (_::tl2) -> cmp tl1 tl2
291           | _, _ ->  XINCOMPARABLE
292         ) else r 
293     | res -> res
294   ;;
295
296   let compare_terms = compare_terms kbo;;
297
298   let profiler = HExtlib.profile ~enable:true "compare_terms(kbo)";;
299   let compare_terms x y =
300     profiler.HExtlib.profile (compare_terms x) y
301   ;;
302
303 end
304
305 module LPO (B : Terms.Blob) = struct
306   let name = "lpo"
307   include B 
308
309   module Pp = Pp.Pp(B)
310   module Unif = FoUnif.FoUnif(B)
311   module Utils = FoUtils.Utils(B)
312
313   let eq_foterm = eq_foterm B.eq;;
314
315   let are_invertible = are_invertible Utils.relocate Unif.alpha_eq;;
316
317   let compute_clause_weight = compute_clause_weight;;
318   let compute_goal_weight = compute_goal_weight;;
319
320   let rec lpo s t =
321     match s,t with
322       | s, t when eq_foterm s t ->
323           XEQ
324       | Terms.Var _, Terms.Var _ ->
325           XINCOMPARABLE
326       | _, Terms.Var i ->
327           if (List.mem i (Terms.vars_of_term s)) then XGT
328           else XINCOMPARABLE
329       | Terms.Var i,_ ->
330           if (List.mem i (Terms.vars_of_term t)) then XLT
331           else XINCOMPARABLE
332       | Terms.Node (hd1::tl1), Terms.Node (hd2::tl2) ->
333           let rec ge_subterm t ol = function
334             | [] -> (false, ol)
335             | x::tl ->
336                 let res = lpo x t in
337                 match res with
338                   | XGT | XEQ -> (true,res::ol)
339                   | o -> ge_subterm t (o::ol) tl
340           in
341           let (res, l_ol) = ge_subterm t [] tl1 in
342             if res then XGT
343             else let (res, r_ol) = ge_subterm s [] tl2 in
344               if res then XLT
345               else begin
346                 let rec check_subterms t = function
347                   | _,[] -> true
348                   | o::ol,_::tl ->
349                       if o = XLT then check_subterms t (ol,tl)
350                       else false
351                   | [], x::tl ->
352                       if lpo x t = XLT then check_subterms t ([],tl)
353                       else false
354                 in
355                 match aux_ordering B.compare hd1 hd2 with
356                   | XGT -> if check_subterms s (r_ol,tl2) then XGT
357                     else XINCOMPARABLE
358                   | XLT -> if check_subterms t (l_ol,tl1) then XLT
359                     else XINCOMPARABLE
360                   | XEQ -> 
361                       let lex = List.fold_left2
362                         (fun acc si ti -> if acc = XEQ then lpo si ti else acc)
363                         XEQ tl1 tl2
364                       in
365                  (match lex with
366                     | XGT ->
367                         if List.for_all (fun x -> lpo s x = XGT) tl2 then XGT
368                       else XINCOMPARABLE
369                     | XLT ->
370                         if List.for_all (fun x -> lpo x t = XLT) tl1 then XLT
371                       else XINCOMPARABLE
372                     | o -> o)   
373               | XINCOMPARABLE -> XINCOMPARABLE
374               | _ -> assert false
375           end
376       | _,_ -> aux_ordering B.compare s t
377             
378   ;;
379
380   let compare_terms = compare_terms lpo;;
381
382   let profiler = HExtlib.profile ~enable:true "compare_terms(lpo)";;
383   let compare_terms x y =
384     profiler.HExtlib.profile (compare_terms x) y
385   ;;
386
387 end
388