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