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