]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/superposition.ml
1) added simplification of actives w.r.t. selected
[helm.git] / helm / software / components / ng_paramodulation / superposition.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: index.mli 9822 2009-06-03 15:37:06Z tassi $ *)
13
14 module Superposition (B : Terms.Blob) = 
15   struct
16     module IDX = Index.Index(B)
17     module Unif = FoUnif.Founif(B)
18     module Subst = FoSubst.Subst(B)
19     module Order = Orderings.Orderings(B)
20     module Utils = FoUtils.Utils(B)
21     module Pp = Pp.Pp(B)
22     
23     exception Success of B.t Terms.bag * int * B.t Terms.unit_clause
24
25     let rec list_first f = function
26       | [] -> None
27       | x::tl -> match f x with Some _ as x -> x | _ -> list_first f tl
28     ;;
29
30     let first_position pos ctx t f =
31       let rec aux pos ctx = function
32       | Terms.Leaf _ as t -> f t pos ctx 
33       | Terms.Var _ -> None
34       | Terms.Node l as t-> 
35           match f t pos ctx with
36           | Some _ as x -> x
37           | None ->
38               let rec first pre post = function
39                 | [] -> None
40                 | t :: tl -> 
41                      let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
42                      match aux (List.length pre :: pos) newctx t with
43                      | Some _ as x -> x
44                      | None -> 
45                          if post = [] then None (* tl is also empty *)
46                          else first (pre @ [t]) (List.tl post) tl
47               in
48                 first [] (List.tl l) l 
49       in
50         aux pos ctx t
51     ;;
52                                      
53     let all_positions pos ctx t f =
54       let rec aux pos ctx = function
55       | Terms.Leaf _ as t -> f t pos ctx 
56       | Terms.Var _ -> []
57       | Terms.Node l as t-> 
58           let acc, _, _ = 
59             List.fold_left
60             (fun (acc,pre,post) t -> (* Invariant: pre @ [t] @ post = l *)
61                 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
62                 let acc = aux (List.length pre :: pos) newctx t @ acc in
63                 if post = [] then acc, l, []
64                 else acc, pre @ [t], List.tl post)
65              (f t pos ctx, [], List.tl l) l
66           in
67            acc
68       in
69         aux pos ctx t
70     ;;
71     
72     let build_clause bag filter rule t subst vl id id2 pos dir =
73       let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
74       let t = Subst.apply_subst subst t in
75       if filter t then
76         let literal = 
77           match t with
78           | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
79                let o = Order.compare_terms l r in
80                Terms.Equation (l, r, ty, o)
81           | t -> Terms.Predicate t
82         in
83         let bag, uc = 
84           Utils.add_to_bag bag (0, literal, vl, proof)
85         in
86         Some (bag, uc)
87       else
88         ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
89     ;;
90       
91     
92     (* ============ simplification ================= *)
93
94     let demod table varlist subterm pos context =
95       let cands = IDX.DT.retrieve_generalizations table subterm in
96       list_first
97         (fun (dir, (id,lit,vl,_)) ->
98            match lit with
99            | Terms.Predicate _ -> assert false
100            | Terms.Equation (l,r,_,o) ->
101                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
102                try 
103                  let subst, varlist = 
104                    Unif.unification (varlist@vl) varlist subterm side 
105                  in
106                  if o = Terms.Incomparable then
107                    let side = Subst.apply_subst subst side in
108                    let newside = Subst.apply_subst subst newside in
109                    let o = Order.compare_terms side newside in
110                    (* Riazanov, pp. 45 (ii) *)
111                    if o = Terms.Lt then  
112                      Some (context newside, subst, varlist, id, pos, dir)
113                    else 
114                      ((*prerr_endline ("Filtering: " ^ 
115                         Pp.pp_foterm side ^ " =(< || =)" ^ 
116                         Pp.pp_foterm newside ^ " coming from " ^ 
117                         Pp.pp_unit_clause uc );*)None)
118                  else
119                    Some (context newside, subst, varlist, id, pos, dir)
120                with FoUnif.UnificationFailure _ -> None)
121         (IDX.ClauseSet.elements cands)
122     ;;
123
124     (* XXX: possible optimization, if the literal has a "side" already
125      * in normal form we should not traverse it again *)
126     let demodulate_once bag (id, literal, vl, _) table =
127       let t = 
128         match literal with
129         | Terms.Predicate t -> t
130         | Terms.Equation (l,r,ty,_) -> Terms.Node [ Terms.Leaf B.eqP; ty; l; r ]
131       in
132       match first_position [] (fun x -> x) t (demod table vl) with
133       | None -> None
134       | Some (newt, subst, varlist, id2, pos, dir) ->
135           build_clause bag (fun _ -> true) Terms.Demodulation 
136             newt subst varlist id id2 pos dir
137     ;;
138
139     let rec demodulate bag clause table =
140       match demodulate_once bag clause table with
141       | None -> bag, clause
142       | Some (bag, clause) -> demodulate bag clause table
143     ;;
144
145     (* move away *)
146     let is_identity_clause = function
147       | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
148       | _, Terms.Predicate _, _, _ -> assert false
149       | _ -> false
150     ;;
151
152     let is_subsumed (id, lit, vl, _) table =
153       match lit with
154       | Terms.Predicate _ -> assert false
155       | Terms.Equation (l,r,ty,_) -> 
156           let lcands = IDX.DT.retrieve_generalizations table l in
157           let rcands = IDX.DT.retrieve_generalizations table l in
158           let f b c = 
159             let dir, l, r, vl = 
160               match c with
161               | (d, (_,Terms.Equation (l,r,ty,_),vl,_))-> d, l, r, vl
162               |_ -> assert false 
163             in 
164             let l, r = if (dir = Terms.Left2Right) = b then l,r else r,l in
165             Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl
166           in
167           let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
168           let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
169           let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
170           List.exists 
171             (fun (c, vl1) ->
172                try ignore(Unif.unification (vl@vl1) vl c t); true
173                with FoUnif.UnificationFailure _ -> false)
174             (cands1 @ cands2)
175     ;;
176
177     (* demodulate and check for subsumption *)
178     let forward_simplify table bag clause = 
179       let bag, clause = demodulate bag clause table in
180       if is_identity_clause clause then None
181       else
182         if is_subsumed clause table then None
183         else Some (bag, clause)
184     ;;
185
186     (* this is like forward_simplify but raises Success *)
187     let backward_simplify maxvar table bag clause = 
188       let bag, clause = demodulate bag clause table in
189       if is_identity_clause clause then raise (Success (bag, maxvar, clause))
190       else bag, clause
191     ;;
192
193     (* =================== inference ===================== *)
194
195     (* this is OK for both the sup_left and sup_right inference steps *)
196     let superposition table varlist subterm pos context =
197       let cands = IDX.DT.retrieve_unifiables table subterm in
198       HExtlib.filter_map
199         (fun (dir, (id,lit,vl,_ (*as uc*))) ->
200            match lit with
201            | Terms.Predicate _ -> assert false
202            | Terms.Equation (l,r,_,o) ->
203                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
204                try 
205                  let subst, varlist = 
206                    Unif.unification (varlist@vl) [] subterm side 
207                  in
208                  if o = Terms.Incomparable then
209                    let side = Subst.apply_subst subst side in
210                    let newside = Subst.apply_subst subst newside in
211                    let o = Order.compare_terms side newside in
212                    (* XXX: check Riazanov p. 33 (iii) *)
213                    if o <> Terms.Lt && o <> Terms.Eq then  
214                      Some (context newside, subst, varlist, id, pos, dir)
215                    else 
216                      ((*prerr_endline ("Filtering: " ^ 
217                         Pp.pp_foterm side ^ " =(< || =)" ^ 
218                         Pp.pp_foterm newside ^ " coming from " ^ 
219                         Pp.pp_unit_clause uc );*)None)
220                  else
221                    Some (context newside, subst, varlist, id, pos, dir)
222                with FoUnif.UnificationFailure _ -> None)
223         (IDX.ClauseSet.elements cands)
224     ;;
225
226     let build_new_clause bag maxvar filter rule t subst vl id id2 pos dir =
227       let maxvar, vl, relocsubst = Utils.relocate maxvar vl in
228       let subst = Subst.concat relocsubst subst in
229       match build_clause bag filter rule t subst vl id id2 pos dir with
230       | Some (bag, c) -> Some ((bag, maxvar), c)
231       | None -> None
232     ;;
233
234
235     let fold_build_new_clause bag maxvar id rule filter res =
236       let (bag, maxvar), res =
237        HExtlib.filter_map_acc 
238          (fun (bag, maxvar) (t,subst,vl,id2,pos,dir) ->
239             build_new_clause bag maxvar filter rule t subst vl id id2 pos dir)
240          (bag, maxvar) res
241       in
242        bag, maxvar, res
243     ;;
244
245     let superposition_with_table bag maxvar (id,selected,vl,_) table =
246       match selected with 
247       | Terms.Predicate _ -> assert false
248       | Terms.Equation (l,r,ty,Terms.Lt) ->
249           fold_build_new_clause bag maxvar id Terms.Superposition
250             (fun _ -> true)
251             (all_positions [3] 
252               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
253               r (superposition table vl))          
254       | Terms.Equation (l,r,ty,Terms.Gt) ->
255           fold_build_new_clause bag maxvar id Terms.Superposition
256             (fun _ -> true)
257             (all_positions [2] 
258               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
259               l (superposition table vl))
260       | Terms.Equation (l,r,ty,Terms.Incomparable) -> 
261           fold_build_new_clause bag maxvar id Terms.Superposition
262             (function (* Riazanov: p.33 condition (iv) *)
263               | Terms.Node [Terms.Leaf eq; ty; l; r ] when B.eq B.eqP eq -> 
264                   Order.compare_terms l r <> Terms.Eq
265               | _ -> assert false)
266             ((all_positions [3] 
267                (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
268                r (superposition table vl)) @         
269              (all_positions [2] 
270                (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
271                l (superposition table vl)))
272       | _ -> assert false
273     ;;
274
275     (* the current equation is normal w.r.t. demodulation with atable
276      * (and is not the identity) *)
277     let infer_right bag maxvar current (alist,atable) = 
278       let ctable = IDX.index_unit_clause IDX.DT.empty current in
279       let bag, (alist, atable) = 
280         let bag, alist = 
281           HExtlib.filter_map_acc (forward_simplify ctable) bag alist
282         in
283         bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
284       in
285       let bag, maxvar, new_clauses =
286         List.fold_left 
287           (fun (bag, maxvar, acc) active ->
288              let bag, maxvar, newc = 
289                superposition_with_table bag maxvar active ctable 
290              in
291              bag, maxvar, newc @ acc)
292           (bag, maxvar, []) alist
293       in
294       let alist, atable = 
295         current :: alist, IDX.index_unit_clause atable current
296       in
297       let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
298       let bag, maxvar, additional_new_clauses =
299         superposition_with_table bag maxvar fresh_current atable 
300       in
301       let new_clauses = new_clauses @ additional_new_clauses in
302       let bag, new_clauses = 
303         HExtlib.filter_map_acc (forward_simplify atable) bag new_clauses
304       in
305       bag, maxvar, (alist, atable), new_clauses
306     ;;
307
308     let infer_left bag maxvar goal (_alist, atable) =
309       let bag, maxvar, new_goals =
310         superposition_with_table bag maxvar goal atable 
311       in
312       let bag, new_goals = 
313         List.fold_left
314          (fun (bag, acc) g -> 
315             let bag, g = demodulate bag g atable in
316             bag, g :: acc) 
317          (bag, []) new_goals
318       in
319       bag, maxvar, List.rev new_goals
320     ;;
321
322   end
323
324
325