]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/orderings.ml
snaphost: supright almost done
[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 Orderings (B : Terms.Blob) = struct
17
18   type weight = int * (int * int) list;;
19   
20   let string_of_weight (cw, mw) =
21     let s =
22       String.concat ", "
23         (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
24     in
25     Printf.sprintf "[%d; %s]" cw s
26   ;;
27   
28   let weight_of_term term =
29     let vars_dict = Hashtbl.create 5 in
30     let rec aux = function
31       | Terms.Var i -> 
32           (try
33              let oldw = Hashtbl.find vars_dict i in
34              Hashtbl.replace vars_dict i (oldw+1)
35            with Not_found ->
36              Hashtbl.add vars_dict i 1);
37           0
38       | Terms.Leaf _ -> 1
39       | Terms.Node l -> List.fold_left (+) 0 (List.map aux l)
40     in
41     let w = aux term in
42     let l =
43       Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] 
44     in
45     let compare w1 w2 = 
46       match w1, w2 with
47       | (m1, _), (m2, _) -> m2 - m1 
48     in 
49     (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
50   ;;
51   
52   let compute_unit_clause_weight = 
53     let weight_of_polynomial w m =
54       let factor = 2 in      
55       w + factor * List.fold_left (fun acc (_,occ) -> acc+occ) 0 m
56     in
57     function
58     | Terms.Predicate t -> 
59         let w, m = weight_of_term t in 
60         weight_of_polynomial w m
61     | Terms.Equation (_,x,_,Terms.Lt) 
62     | Terms.Equation (x,_,_,Terms.Gt) ->
63         let w, m = weight_of_term x in 
64         weight_of_polynomial w m
65     | Terms.Equation (l,r,_,Terms.Eq) 
66     | Terms.Equation (l,r,_,Terms.Incomparable) ->
67         let wl, ml = weight_of_term l in 
68         let wr, mr = weight_of_term r in 
69         weight_of_polynomial (wl+wr) (ml@mr)
70   ;;
71   
72   (* returns a "normalized" version of the polynomial weight wl (with type
73    * weight list), i.e. a list sorted ascending by meta number,
74    * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
75    * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
76    *      (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
77   let normalize_weight maxmeta (cw, wl) =
78     let rec aux = function
79       | 0 -> []
80       | m -> (m, 0)::(aux (m-1))
81     in
82     let tmpl = aux maxmeta in
83     let wl =
84       List.sort
85         (fun (m, _) (n, _) -> Pervasives.compare m n)
86         (List.fold_left
87            (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
88     in
89     (cw, wl)
90   ;;
91   
92   
93   let normalize_weights (cw1, wl1) (cw2, wl2) =
94     let rec aux wl1 wl2 =
95       match wl1, wl2 with
96       | [], [] -> [], []
97       | (m, w)::tl1, (n, w')::tl2 when m = n ->
98           let res1, res2 = aux tl1 tl2 in
99           (m, w)::res1, (n, w')::res2
100       | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
101           let res1, res2 = aux tl1 wl2 in
102           (m, w)::res1, (m, 0)::res2
103       | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
104           let res1, res2 = aux wl1 tl2 in
105           (n, 0)::res1, (n, w')::res2
106       | [], (n, w)::tl2 ->
107           let res1, res2 = aux [] tl2 in
108           (n, 0)::res1, (n, w)::res2
109       | (m, w)::tl1, [] ->
110           let res1, res2 = aux tl1 [] in
111           (m, w)::res1, (m, 0)::res2
112       | _, _ -> assert false
113     in
114     let cmp (m, _) (n, _) = compare m n in
115     let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
116     (cw1, wl1), (cw2, wl2)
117   ;;
118   
119   (* Riazanov: 3.1.5 pag 38 *)
120   (* TODO: optimize early detection of XINCOMPARABLE case *)
121   let compare_weights (h1, w1) (h2, w2) =
122     let res, diffs =
123       try
124         List.fold_left2
125           (fun ((lt, eq, gt), diffs) w1 w2 ->
126              match w1, w2 with
127              | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
128                  let diffs = (w1 - w2) + diffs in 
129                  let r = compare w1 w2 in
130                  if r < 0 then (lt+1, eq, gt), diffs
131                  else if r = 0 then (lt, eq+1, gt), diffs
132                  else (lt, eq, gt+1), diffs
133              | _ -> assert false)
134           ((0, 0, 0), 0) w1 w2
135       with Invalid_argument _ -> assert false
136     in
137     let hdiff = h1 - h2 in 
138     match res with
139     | (0, _, 0) ->
140         if hdiff < 0 then XLT
141         else if hdiff > 0 then XGT
142         else XEQ 
143     | (m, _, 0) ->
144         if hdiff <= 0 then XLT
145         else if (- diffs) >= hdiff then XLE else XINCOMPARABLE
146     | (0, _, m) ->
147         if hdiff >= 0 then XGT
148         else if diffs >= (- hdiff) then XGE else XINCOMPARABLE
149     | (m, _, n) when m > 0 && n > 0 -> XINCOMPARABLE
150     | _ -> assert false 
151   ;;
152   
153   (* Riazanov: p. 40, relation >>> 
154    * if head_only=true then it is not >>> but helps case 2 of 3.14 p 39 *)
155   let rec aux_ordering ?(head_only=false) t1 t2 =
156     match t1, t2 with
157     (* 1. *)
158     | Terms.Var _, _
159     | _, Terms.Var _ -> XINCOMPARABLE
160     (* 2.a *)
161     | Terms.Leaf a1, Terms.Leaf a2 -> 
162         let cmp = B.compare a1 a2 in
163         if cmp = 0 then XEQ else if cmp < 0 then XLT else XGT
164     | Terms.Leaf _, Terms.Node _ -> XLT
165     | Terms.Node _, Terms.Leaf _ -> XGT
166     (* 2.b *)
167     | Terms.Node l1, Terms.Node l2 ->
168         let rec cmp t1 t2 =
169           match t1, t2 with
170           | [], [] -> XEQ
171           | _, [] -> XGT
172           | [], _ -> XLT
173           | hd1::tl1, hd2::tl2 ->
174               let o = aux_ordering ~head_only hd1 hd2 in
175               if o = XEQ && not head_only then cmp tl1 tl2 else o
176         in
177         cmp l1 l2
178   ;;
179   
180   (* Riazanov: p. 40, relation >_n *)
181   let nonrec_kbo t1 t2 =
182     let w1 = weight_of_term t1 in
183     let w2 = weight_of_term t2 in
184     let w1, w2 = normalize_weights w1 w2 in
185     match compare_weights w1 w2 with
186     | XLE ->  (* this is .> *)
187         if aux_ordering t1 t2 = XLT then XLT else XINCOMPARABLE
188     | XGE -> 
189         if aux_ordering t1 t2 = XGT then XGT else XINCOMPARABLE
190     | XEQ -> aux_ordering t1 t2
191     | res -> res
192   ;;
193   
194   (* Riazanov: p. 38, relation > *)
195   let rec kbo t1 t2 =
196     let aux = aux_ordering ~head_only:true in
197     let rec cmp t1 t2 =
198       match t1, t2 with
199       | [], [] -> XEQ
200       | _, [] -> XGT
201       | [], _ -> XLT
202       | hd1::tl1, hd2::tl2 ->
203           let o = kbo hd1 hd2 in
204           if o = XEQ then cmp tl1 tl2
205           else o
206     in
207     let w1 = weight_of_term t1 in
208     let w2 = weight_of_term t2 in
209     let w1, w2 = normalize_weights w1 w2 in
210     let comparison = compare_weights w1 w2 in
211     match comparison with
212     | XLE ->
213         let r = aux t1 t2 in
214         if r = XLT then XLT
215         else if r = XEQ then (
216           match t1, t2 with
217           | Terms.Node (_::tl1), Terms.Node (_::tl2) ->
218               if cmp tl1 tl2 = XLT then XLT else XINCOMPARABLE
219           | _, _ -> assert false
220         ) else XINCOMPARABLE
221     | XGE ->
222         let r = aux t1 t2 in
223         if r = XGT then XGT
224         else if r = XEQ then (
225           match t1, t2 with
226           | Terms.Node (_::tl1), Terms.Node (_::tl2) ->
227               if cmp tl1 tl2 = XGT then XGT else XINCOMPARABLE
228           | _, _ ->  assert false
229         ) else XINCOMPARABLE
230     | XEQ ->
231         let r = aux t1 t2 in
232         if r = XEQ then (
233           match t1, t2 with
234           | Terms.Node (_::tl1), Terms.Node (_::tl2) -> cmp tl1 tl2
235           | _, _ ->  XINCOMPARABLE
236         ) else r 
237     | res -> res
238   ;;
239             
240   let compare_terms x y = 
241     match nonrec_kbo x y with
242     | XINCOMPARABLE -> Terms.Incomparable
243     | XGT -> Terms.Gt
244     | XLT -> Terms.Lt
245     | XEQ -> Terms.Eq
246     | _ -> assert false
247   ;; 
248
249 end