]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/superposition.ml
e87a03f52db6e29f1d8152ce23b99a72d921a508
[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 vars_of_term t =
77       let rec aux acc = function
78         | Terms.Leaf _ -> acc
79         | Terms.Var i -> if (List.mem i acc) then acc else i::acc
80         | Terms.Node l -> List.fold_left aux acc l
81       in aux [] t
82     ;;
83     
84     let build_clause bag filter rule t subst vl id id2 pos dir =
85       let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
86       let t = Subst.apply_subst subst t in
87       if filter t then
88         let literal = 
89           match t with
90           | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
91                let o = Order.compare_terms l r in
92                Terms.Equation (l, r, ty, o)
93           | t -> Terms.Predicate t
94         in
95         let bag, uc = 
96           Utils.add_to_bag bag (0, literal, vars_of_term t, proof)
97         in
98         Some (bag, uc)
99       else
100         ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
101     ;;
102       
103     
104     (* ============ simplification ================= *)
105
106     let demod table varlist subterm pos context =
107       let cands = IDX.DT.retrieve_generalizations table subterm in
108       list_first
109         (fun (dir, (id,lit,vl,_)) ->
110            match lit with
111            | Terms.Predicate _ -> assert false
112            | Terms.Equation (l,r,_,o) ->
113                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
114                try 
115                  let subst, varlist = 
116                    Unif.unification (varlist@vl) varlist subterm side 
117                  in
118                  if o = Terms.Incomparable then
119                    let side = Subst.apply_subst subst side in
120                    let newside = Subst.apply_subst subst newside in
121                    let o = Order.compare_terms newside side in
122                    (* Riazanov, pp. 45 (ii) *)
123                    if o = Terms.Lt then  
124                      Some (context newside, subst, varlist, id, pos, dir)
125                    else 
126                      ((*prerr_endline ("Filtering: " ^ 
127                         Pp.pp_foterm side ^ " =(< || =)" ^ 
128                         Pp.pp_foterm newside ^ " coming from " ^ 
129                         Pp.pp_unit_clause uc );*)None)
130                  else
131                    Some (context newside, subst, varlist, id, pos, dir)
132                with FoUnif.UnificationFailure _ -> None)
133         (IDX.ClauseSet.elements cands)
134     ;;
135
136     (* XXX: possible optimization, if the literal has a "side" already
137      * in normal form we should not traverse it again *)
138     let demodulate_once bag (id, literal, vl, pr) table =
139       (* debug ("Demodulating : " ^ (Pp.pp_unit_clause (id, literal, vl, pr)));*)
140       let t = 
141         match literal with
142         | Terms.Predicate t -> t
143         | Terms.Equation (l,r,ty,_) -> Terms.Node [ Terms.Leaf B.eqP; ty; l; r ]
144       in
145       match first_position [] (fun x -> x) t (demod table vl) with
146       | None -> None
147       | Some (newt, subst, varlist, id2, pos, dir) ->
148           build_clause bag (fun _ -> true) Terms.Demodulation 
149             newt subst varlist id id2 pos dir
150     ;;
151
152     let rec demodulate bag clause table =
153       match demodulate_once bag clause table with
154       | None -> bag, clause
155       | Some (bag, clause) -> demodulate bag clause table
156     ;;
157
158     (* move away *)
159     let is_identity_clause = function
160       | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
161       | _, Terms.Predicate _, _, _ -> assert false
162       | _ -> false
163     ;;
164
165     let is_subsumed ~unify (id, lit, vl, _) table =
166       match lit with
167       | Terms.Predicate _ -> assert false
168       | Terms.Equation (l,r,ty,_) -> 
169           let retrieve = if unify then IDX.DT.retrieve_unifiables
170           else IDX.DT.retrieve_generalizations in
171           let lcands = retrieve table l in
172           let rcands = retrieve table r in
173           let f b c = 
174             let dir, l, r, vl = 
175               match c with
176               | (d, (_,Terms.Equation (l,r,ty,_),vl,_))-> d, l, r, vl
177               |_ -> assert false 
178             in 
179             let l, r = if (dir = Terms.Left2Right) = b then l,r else r,l in
180             Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl
181           in
182           let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
183           let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
184           let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
185           let locked_vars = if unify then [] else vl in
186             List.exists 
187               (fun (c, vl1) ->
188                  try ignore(Unif.unification (vl@vl1) locked_vars c t); true
189                  with FoUnif.UnificationFailure _ -> false)
190               (cands1 @ cands2)
191     ;;
192
193     (* demodulate and check for subsumption *)
194     let simplify table bag clause = 
195       let bag, clause = demodulate bag clause table in
196       if is_identity_clause clause then None
197       else
198         if is_subsumed ~unify:false clause table then None
199         else Some (bag, clause)
200     ;;
201
202     let one_pass_simplification new_clause (alist,atable) bag =
203       match simplify atable bag new_clause with
204         | None -> None (* new_clause has been discarded *)
205         | Some (bag, clause) ->
206             let ctable = IDX.index_unit_clause IDX.DT.empty clause in
207             let bag, alist, atable = 
208               List.fold_left 
209                 (fun (bag, alist, atable as acc) c ->
210                    match simplify ctable bag c with
211                      |None -> acc (* an active clause as been discarded *)
212                      |Some (bag, c1) ->
213                         bag, c :: alist, IDX.index_unit_clause atable c)
214                 (bag,[],IDX.DT.empty) alist
215             in
216               Some (clause, bag, (alist,atable))
217     ;;
218
219     let simplification_step ~new_cl cl (alist,atable) bag new_clause =
220       let atable1 =
221         if new_cl then atable else
222         IDX.index_unit_clause atable cl
223       in
224         (* Simplification of new_clause with :      *
225          * - actives and cl if new_clause is not cl *
226          * - only actives otherwise                 *)
227         match simplify atable1 bag new_clause with
228           | None -> (Some cl, None) (* new_clause has been discarded *)
229           | Some (bag, clause) ->
230               (* Simplification of each active clause with clause *
231                * which is the simplified form of new_clause       *)
232               let ctable = IDX.index_unit_clause IDX.DT.empty clause in
233               let bag, newa, alist, atable = 
234                 List.fold_left 
235                   (fun (bag, newa, alist, atable as acc) c ->
236                      match simplify ctable bag c with
237                        |None -> acc (* an active clause as been discarded *)
238                        |Some (bag, c1) ->
239                             if (c1 == c) then 
240                               bag, newa, c :: alist,
241                             IDX.index_unit_clause atable c
242                             else
243                               bag, c1 :: newa, alist, atable)             
244                   (bag,[],[],IDX.DT.empty) alist
245               in
246                 if new_cl then
247                   (Some cl, Some (clause, (alist,atable), newa, bag))
248                 else
249                   (* if new_clause is not cl, we simplify cl with clause *)
250                   match simplify ctable bag cl with
251                     | None ->
252                         (* cl has been discarded *)
253                         (None, Some (clause, (alist,atable), newa, bag))
254                     | Some (bag,cl1) ->
255                         (Some cl1, Some (clause, (alist,atable), newa, bag))
256     ;;
257
258     let keep_simplified cl (alist,atable) bag =
259       let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
260         if new_cl then
261           match simplification_step ~new_cl cl (alist,atable) bag cl with
262             | (None, _) -> assert false
263             | (Some _, None) -> None
264             | (Some _, Some (clause, (alist,atable), newa, bag)) ->
265                 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
266                   bag (newa@newc)
267         else
268           match newc with
269             | [] -> Some (cl, bag, (alist,atable))
270             | hd::tl ->
271                 match simplification_step ~new_cl cl
272                   (alist,atable) bag hd with
273                   | (None,None) -> assert false
274                   | (Some _,None) ->
275                       keep_simplified_aux ~new_cl cl (alist,atable) bag tl
276                   | (None, Some _) -> None
277                   | (Some cl1, Some (clause, (alist,atable), newa, bag)) ->
278                       let alist,atable =
279                         (clause::alist, IDX.index_unit_clause atable clause)
280                       in
281                         keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
282                           bag (newa@tl)
283       in
284         keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
285     ;;                  
286           
287     (* this is like simplify but raises Success *)
288     let simplify_goal maxvar table bag clause = 
289       let bag, clause = demodulate bag clause table in
290       if (is_identity_clause clause) || (is_subsumed ~unify:true clause table)
291       then raise (Success (bag, maxvar, clause))
292       else bag, clause
293     ;;
294
295     (* =================== inference ===================== *)
296
297     (* this is OK for both the sup_left and sup_right inference steps *)
298     let superposition table varlist subterm pos context =
299       let cands = IDX.DT.retrieve_unifiables table subterm in
300       HExtlib.filter_map
301         (fun (dir, (id,lit,vl,_ (*as uc*))) ->
302            match lit with
303            | Terms.Predicate _ -> assert false
304            | Terms.Equation (l,r,_,o) ->
305                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
306                try 
307                  let subst, varlist = 
308                    Unif.unification (varlist@vl) [] subterm side 
309                  in
310                  if o = Terms.Incomparable then
311                    let side = Subst.apply_subst subst side in
312                    let newside = Subst.apply_subst subst newside in
313                    let o = Order.compare_terms side newside in
314                    (* XXX: check Riazanov p. 33 (iii) *)
315                    if o <> Terms.Lt && o <> Terms.Eq then  
316                      Some (context newside, subst, varlist, id, pos, dir)
317                    else 
318                      ((*prerr_endline ("Filtering: " ^ 
319                         Pp.pp_foterm side ^ " =(< || =)" ^ 
320                         Pp.pp_foterm newside ^ " coming from " ^ 
321                         Pp.pp_unit_clause uc );*)None)
322                  else
323                    Some (context newside, subst, varlist, id, pos, dir)
324                with FoUnif.UnificationFailure _ -> None)
325         (IDX.ClauseSet.elements cands)
326     ;;
327
328     let build_new_clause bag maxvar filter rule t subst vl id id2 pos dir =
329       let maxvar, vl, relocsubst = Utils.relocate maxvar vl in
330       let subst = Subst.concat relocsubst subst in
331       match build_clause bag filter rule t subst vl id id2 pos dir with
332       | Some (bag, c) -> Some ((bag, maxvar), c)
333       | None -> None
334     ;;
335
336
337     let fold_build_new_clause bag maxvar id rule filter res =
338       let (bag, maxvar), res =
339        HExtlib.filter_map_acc 
340          (fun (bag, maxvar) (t,subst,vl,id2,pos,dir) ->
341             build_new_clause bag maxvar filter rule t subst vl id id2 pos dir)
342          (bag, maxvar) res
343       in
344        bag, maxvar, res
345     ;;
346
347     (* Superposes selected equation with equalities in table *)
348     let superposition_with_table bag maxvar (id,selected,vl,_) table =
349       match selected with 
350       | Terms.Predicate _ -> assert false
351       | Terms.Equation (l,r,ty,Terms.Lt) ->
352           fold_build_new_clause bag maxvar id Terms.Superposition
353             (fun _ -> true)
354             (all_positions [3] 
355               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
356               r (superposition table vl))          
357       | Terms.Equation (l,r,ty,Terms.Gt) ->
358           fold_build_new_clause bag maxvar id Terms.Superposition
359             (fun _ -> true)
360             (all_positions [2] 
361               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
362               l (superposition table vl))
363       | Terms.Equation (l,r,ty,Terms.Incomparable) -> 
364           fold_build_new_clause bag maxvar id Terms.Superposition
365             (function (* Riazanov: p.33 condition (iv) *)
366               | Terms.Node [Terms.Leaf eq; ty; l; r ] when B.eq B.eqP eq -> 
367                   Order.compare_terms l r <> Terms.Eq
368               | _ -> assert false)
369             ((all_positions [3] 
370                (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
371                r (superposition table vl)) @         
372              (all_positions [2] 
373                (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
374                l (superposition table vl)))
375       | _ -> assert false
376     ;;
377
378     (* the current equation is normal w.r.t. demodulation with atable
379      * (and is not the identity) *)
380     let infer_right bag maxvar current (alist,atable) = 
381       (* We demodulate actives clause with current until all *
382        * active clauses are reduced w.r.t each other         *)
383       (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
384       let ctable = IDX.index_unit_clause IDX.DT.empty current in
385       (* let bag, (alist, atable) = 
386         let bag, alist = 
387           HExtlib.filter_map_acc (simplify ctable) bag alist
388         in
389         bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
390       in*)
391         debug "Simplified active clauses with fact";
392       (* We superpose active clauses with current *)
393       let bag, maxvar, new_clauses =
394         List.fold_left 
395           (fun (bag, maxvar, acc) active ->
396              let bag, maxvar, newc = 
397                superposition_with_table bag maxvar active ctable 
398              in
399              bag, maxvar, newc @ acc)
400           (bag, maxvar, []) alist
401       in
402         debug "First superpositions";
403         (* We add current to active clauses so that it can be *
404          * superposed with itself                             *)
405       let alist, atable = 
406         current :: alist, IDX.index_unit_clause atable current
407       in
408         debug "Indexed";
409       let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
410         (* We need to put fresh_current into the bag so that all *
411          * variables clauses refer to are known.                 *)
412       let bag, fresh_current = Utils.add_to_bag bag fresh_current in
413         (* We superpose current with active clauses *)
414       let bag, maxvar, additional_new_clauses =
415         superposition_with_table bag maxvar fresh_current atable 
416       in
417         debug "Another superposition";
418       let new_clauses = new_clauses @ additional_new_clauses in
419       let bag, new_clauses = 
420         HExtlib.filter_map_acc (simplify atable) bag new_clauses
421       in
422         debug "Demodulated new clauses";
423       bag, maxvar, (alist, atable), new_clauses
424     ;;
425
426     let infer_left bag maxvar goal (_alist, atable) =
427         (* We superpose the goal with active clauses *)
428       let bag, maxvar, new_goals =
429         superposition_with_table bag maxvar goal atable 
430       in
431         (* We demodulate the goal with active clauses *)
432       let bag, new_goals = 
433         List.fold_left
434          (fun (bag, acc) g -> 
435             let bag, g = demodulate bag g atable in
436             bag, g :: acc) 
437          (bag, []) new_goals
438       in
439       bag, maxvar, List.rev new_goals
440     ;;
441
442   end
443
444
445