]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/superposition.ml
the prover is almost OK, types in fuctors a bit extended to
[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     let demodulate_once ~jump_to_right bag (id, literal, vl, pr) table =
137       (* debug ("Demodulating : " ^ (Pp.pp_unit_clause (id, literal, vl, pr)));*)
138       match literal with
139       | Terms.Predicate t -> assert false
140       | Terms.Equation (l,r,ty,_) ->
141         let left_position = if jump_to_right then None else
142           first_position [2]
143             (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) l
144             (demod table vl)
145         in
146         match left_position with
147           | Some (newt, subst, varlist, id2, pos, dir) ->
148               begin
149                 match build_clause bag (fun _ -> true) Terms.Demodulation 
150                   newt subst varlist id id2 pos dir
151                 with
152                   | None -> assert false
153                   | Some x -> Some (x,false)
154               end
155           | None ->
156               match first_position
157                 [3] (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) r
158                 (demod table vl)
159               with
160                 | None -> None
161                 | Some (newt, subst, varlist, id2, pos, dir) ->
162                     match build_clause bag (fun _ -> true)
163                       Terms.Demodulation newt subst varlist id id2 pos dir
164                     with
165                         | None -> assert false
166                         | Some x -> Some (x,true)
167     ;;
168
169     let rec demodulate ~jump_to_right bag clause table =
170       match demodulate_once ~jump_to_right bag clause table with
171       | None -> bag, clause
172       | Some ((bag, clause),r) -> demodulate ~jump_to_right:r
173           bag clause table
174     ;;
175
176     let demodulate bag clause table = demodulate ~jump_to_right:false
177       bag clause table
178     ;;
179
180     (* move away *)
181     let is_identity_clause ~unify = function
182       | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
183       | _, Terms.Equation (l,r,_,_), vl, proof when unify ->
184           (try ignore(Unif.unification vl [] l r); true
185           with FoUnif.UnificationFailure _ -> false)
186       | _, Terms.Equation (_,_,_,_), _, _ -> false
187       | _, Terms.Predicate _, _, _ -> assert false        
188     ;;
189
190     let build_new_clause bag maxvar filter rule t subst vl id id2 pos dir =
191       let maxvar, vl, relocsubst = Utils.relocate maxvar vl in
192       let subst = Subst.concat relocsubst subst in
193       match build_clause bag filter rule t subst vl id id2 pos dir with
194       | Some (bag, c) -> Some ((bag, maxvar), c)
195       | None -> None
196     ;;
197
198     let fold_build_new_clause bag maxvar id rule filter res =
199       let (bag, maxvar), res =
200        HExtlib.filter_map_acc 
201          (fun (bag, maxvar) (t,subst,vl,id2,pos,dir) ->
202             build_new_clause bag maxvar filter rule t subst vl id id2 pos dir)
203          (bag, maxvar) res
204       in
205        bag, maxvar, res
206     ;;
207
208     
209     let rewrite_eq ~unify l r ty vl table =
210       let retrieve = if unify then IDX.DT.retrieve_unifiables
211       else IDX.DT.retrieve_generalizations in
212       let lcands = retrieve table l in
213       let rcands = retrieve table r in
214       let f b c = 
215         let id, dir, l, r, vl = 
216           match c with
217             | (d, (id,Terms.Equation (l,r,ty,_),vl,_))-> id, d, l, r, vl
218             |_ -> assert false 
219         in 
220         let reverse = (dir = Terms.Left2Right) = b in
221         let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
222         else r,l, Terms.Right2Left in
223           (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
224       in
225       let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
226       let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
227       let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
228       let locked_vars = if unify then [] else vl in
229       let rec aux = function
230         | [] -> None
231         | (id2,dir,c,vl1)::tl ->
232             try
233               let subst,vl1 = Unif.unification (vl@vl1) locked_vars c t in
234               Some (id2, dir, subst)
235             with FoUnif.UnificationFailure _ -> aux tl
236       in
237         aux (cands1 @ cands2)
238     ;;
239
240     let is_subsumed ~unify bag maxvar (id, lit, vl, _) table =
241       match lit with
242       | Terms.Predicate _ -> assert false
243       | Terms.Equation (l,r,ty,_) -> 
244           match rewrite_eq ~unify l r ty vl table with
245             | None -> None
246             | Some (id2, dir, subst) ->
247                 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
248                   build_new_clause bag maxvar (fun _ -> true)
249                     Terms.Superposition id_t subst [] id id2 [2] dir 
250     ;;
251     (* id refers to a clause proving contextl l = contextr r *)
252
253     let rec deep_eq ~unify l r ty pos contextl contextr table acc =
254       match acc with 
255       | None -> None
256       | Some(bag,maxvar,[],subst) -> assert false
257       | Some(bag,maxvar,(id,_,vl,_)::clauses,subst) -> 
258           let l = Subst.apply_subst subst l in 
259           let r = Subst.apply_subst subst r in 
260             try 
261               let subst1,vl1 = Unif.unification vl [] l r in
262                 Some(bag,maxvar,clauses,Subst.concat subst1 subst)
263             with FoUnif.UnificationFailure _ -> 
264               match rewrite_eq ~unify l r ty vl table with
265               | Some (id2, dir, subst1) ->
266                   let id_t = 
267                     Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r] in
268                     (match 
269                       build_new_clause bag maxvar (fun _ -> true)
270                         Terms.Superposition id_t subst1 [] id id2 (2::pos) dir 
271                     with
272                     | Some ((bag, maxvar), c) -> 
273                         Some(bag,maxvar,c::clauses,Subst.concat subst1 subst)
274                     | None -> assert false)
275               | None ->
276                   match l,r with 
277                   | Terms.Node (a::la), Terms.Node (b::lb) when 
278                       a = b && List.length la = List.length lb ->
279                       let acc,_,_,_ =
280                         List.fold_left2 
281                           (fun (acc,pre,postl,postr) a b -> 
282                              let newcl = 
283                               fun x -> contextl(Terms.Node (pre@(x::postl))) in
284                              let newcr = 
285                               fun x -> contextr(Terms.Node (pre@(x::postr))) in
286                              let newpos = List.length pre::pos in
287                              let footail l =
288                                if l = [] then [] else List.tl l in
289                                (deep_eq ~unify a b ty 
290                                  newpos newcl newcr table acc,pre@[b],
291                                  footail postl, footail postr))
292                           (acc,[a],List.tl la,List.tl lb) la lb
293                       in acc
294                   | Terms.Var _, _
295                   | _, Terms.Var _ -> assert false
296                   | _,_ -> None
297     ;;
298
299     (* demodulate and check for subsumption *)
300     let simplify table maxvar bag clause = 
301       let bag, clause = demodulate bag clause table in
302       if is_identity_clause ~unify:false clause then bag,None
303       else
304         match is_subsumed ~unify:false bag maxvar clause table with
305           | None -> bag, Some clause
306           | Some _ -> bag, None
307     ;;
308
309     let one_pass_simplification new_clause (alist,atable) bag maxvar =
310       match simplify atable maxvar bag new_clause with
311         | bag,None -> None (* new_clause has been discarded *)
312         | bag,(Some clause) ->
313             let ctable = IDX.index_unit_clause IDX.DT.empty clause in
314             let bag, alist, atable = 
315               List.fold_left 
316                 (fun (bag, alist, atable as acc) c ->
317                    match simplify ctable maxvar bag c with
318                      |bag,None -> acc (* an active clause as been discarded *)
319                      |bag,Some c1 ->
320                         bag, c :: alist, IDX.index_unit_clause atable c)
321                 (bag,[],IDX.DT.empty) alist
322             in
323               Some (clause, bag, (alist,atable))
324     ;;
325
326     let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
327       let atable1 =
328         if new_cl then atable else
329         IDX.index_unit_clause atable cl
330       in
331         (* Simplification of new_clause with :      *
332          * - actives and cl if new_clause is not cl *
333          * - only actives otherwise                 *)
334         match simplify atable1 maxvar bag new_clause with
335           | bag,None -> (Some cl, None) (* new_clause has been discarded *)
336           | bag,Some clause ->
337               (* Simplification of each active clause with clause *
338                * which is the simplified form of new_clause       *)
339               let ctable = IDX.index_unit_clause IDX.DT.empty clause in
340               let bag, newa, alist, atable = 
341                 List.fold_left 
342                   (fun (bag, newa, alist, atable as acc) c ->
343                      match simplify ctable maxvar bag c with
344                        |bag,None -> acc (* an active clause as been discarded *)
345                        |bag,Some c1 ->
346                             if (c1 == c) then 
347                               bag, newa, c :: alist,
348                             IDX.index_unit_clause atable c
349                             else
350                               bag, c1 :: newa, alist, atable)             
351                   (bag,[],[],IDX.DT.empty) alist
352               in
353                 if new_cl then
354                   (Some cl, Some (clause, (alist,atable), newa, bag))
355                 else
356                   (* if new_clause is not cl, we simplify cl with clause *)
357                   match simplify ctable maxvar bag cl with
358                     | bag,None ->
359                         (* cl has been discarded *)
360                         (None, Some (clause, (alist,atable), newa, bag))
361                     | bag,Some cl1 ->
362                         (Some cl1, Some (clause, (alist,atable), newa, bag))
363     ;;
364
365     let keep_simplified cl (alist,atable) bag maxvar =
366       let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
367         if new_cl then
368           match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
369             | (None, _) -> assert false
370             | (Some _, None) -> None
371             | (Some _, Some (clause, (alist,atable), newa, bag)) ->
372                 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
373                   bag (newa@newc)
374         else
375           match newc with
376             | [] -> Some (cl, bag, (alist,atable))
377             | hd::tl ->
378                 match simplification_step ~new_cl cl
379                   (alist,atable) bag maxvar hd with
380                   | (None,None) -> assert false
381                   | (Some _,None) ->
382                       keep_simplified_aux ~new_cl cl (alist,atable) bag tl
383                   | (None, Some _) -> None
384                   | (Some cl1, Some (clause, (alist,atable), newa, bag)) ->
385                       let alist,atable =
386                         (clause::alist, IDX.index_unit_clause atable clause)
387                       in
388                         keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
389                           bag (newa@tl)
390       in
391         keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
392     ;;
393
394     let are_alpha_eq cl1 cl2 =
395       let get_term (_,lit,_,_) =
396         match lit with
397           | Terms.Predicate _ -> assert false
398           | Terms.Equation (l,r,ty,_) ->
399               Terms.Node [Terms.Leaf B.eqP; ty; l ; r]
400       in
401         try ignore(Unif.alpha_eq (get_term cl1) (get_term cl2)) ; true
402         with FoUnif.UnificationFailure _ -> false
403 ;;
404
405     (* this is like simplify but raises Success *)
406     let simplify_goal maxvar table bag g_actives clause = 
407       let bag, clause = demodulate bag clause table in
408       if (is_identity_clause ~unify:true clause)
409       then raise (Success (bag, maxvar, clause))
410 (*
411       else   
412         let (id,lit,vl,_) = clause in 
413         let l,r,ty = 
414           match lit with
415             | Terms.Equation(l,r,ty,_) -> l,r,ty
416             | _ -> assert false 
417         in
418         match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x) 
419           table (Some(bag,maxvar,[clause],Subst.id_subst)) with
420         | None -> 
421             if List.exists (are_alpha_eq clause) g_actives then None
422             else Some (bag, clause)
423         | Some (bag,maxvar,cl,subst) -> 
424             debug "Goal subsumed";
425             raise (Success (bag,maxvar,List.hd cl))
426 *)
427       else match is_subsumed ~unify:true bag maxvar clause table with
428         | None -> 
429             if List.exists (are_alpha_eq clause) g_actives then None
430             else Some (bag, clause)
431         | Some ((bag,maxvar),c) -> 
432             debug "Goal subsumed";
433             raise (Success (bag,maxvar,c)) 
434     ;;
435
436     (* =================== inference ===================== *)
437
438     (* this is OK for both the sup_left and sup_right inference steps *)
439     let superposition table varlist subterm pos context =
440       let cands = IDX.DT.retrieve_unifiables table subterm in
441       HExtlib.filter_map
442         (fun (dir, (id,lit,vl,_ (*as uc*))) ->
443            match lit with
444            | Terms.Predicate _ -> assert false
445            | Terms.Equation (l,r,_,o) ->
446                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
447                try 
448                  let subst, varlist = 
449                    Unif.unification (varlist@vl) [] subterm side 
450                  in
451                  if o = Terms.Incomparable then
452                    let side = Subst.apply_subst subst side in
453                    let newside = Subst.apply_subst subst newside in
454                    let o = Order.compare_terms side newside in
455                    (* XXX: check Riazanov p. 33 (iii) *)
456                    if o <> Terms.Lt && o <> Terms.Eq then  
457                      Some (context newside, subst, varlist, id, pos, dir)
458                    else 
459                      ((*prerr_endline ("Filtering: " ^ 
460                         Pp.pp_foterm side ^ " =(< || =)" ^ 
461                         Pp.pp_foterm newside ^ " coming from " ^ 
462                         Pp.pp_unit_clause uc );*)None)
463                  else
464                    Some (context newside, subst, varlist, id, pos, dir)
465                with FoUnif.UnificationFailure _ -> None)
466         (IDX.ClauseSet.elements cands)
467     ;;
468
469     (* Superposes selected equation with equalities in table *)
470     let superposition_with_table bag maxvar (id,selected,vl,_) table =
471       match selected with 
472       | Terms.Predicate _ -> assert false
473       | Terms.Equation (l,r,ty,Terms.Lt) ->
474           fold_build_new_clause bag maxvar id Terms.Superposition
475             (fun _ -> true)
476             (all_positions [3] 
477               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
478               r (superposition table vl))
479       | Terms.Equation (l,r,ty,Terms.Gt) ->
480           fold_build_new_clause bag maxvar id Terms.Superposition
481             (fun _ -> true)
482             (all_positions [2] 
483               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
484               l (superposition table vl))
485       | Terms.Equation (l,r,ty,Terms.Incomparable) -> 
486           fold_build_new_clause bag maxvar id Terms.Superposition
487             (function (* Riazanov: p.33 condition (iv) *)
488               | Terms.Node [Terms.Leaf eq; ty; l; r ] when B.eq B.eqP eq -> 
489                   Order.compare_terms l r <> Terms.Eq
490               | _ -> assert false)
491             ((all_positions [3] 
492                (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
493                r (superposition table vl)) @         
494              (all_positions [2] 
495                (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
496                l (superposition table vl)))
497       | _ -> assert false
498     ;;
499
500     (* the current equation is normal w.r.t. demodulation with atable
501      * (and is not the identity) *)
502     let infer_right bag maxvar current (alist,atable) = 
503       (* We demodulate actives clause with current until all *
504        * active clauses are reduced w.r.t each other         *)
505       (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
506       let ctable = IDX.index_unit_clause IDX.DT.empty current in
507       (* let bag, (alist, atable) = 
508         let bag, alist = 
509           HExtlib.filter_map_acc (simplify ctable) bag alist
510         in
511         bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
512       in*)
513         debug "Simplified active clauses with fact";
514       (* We superpose active clauses with current *)
515       let bag, maxvar, new_clauses =
516         List.fold_left 
517           (fun (bag, maxvar, acc) active ->
518              let bag, maxvar, newc = 
519                superposition_with_table bag maxvar active ctable 
520              in
521              bag, maxvar, newc @ acc)
522           (bag, maxvar, []) alist
523       in
524         debug "First superpositions";
525         (* We add current to active clauses so that it can be *
526          * superposed with itself                             *)
527       let alist, atable = 
528         current :: alist, IDX.index_unit_clause atable current
529       in
530         debug "Indexed";
531       let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
532         (* We need to put fresh_current into the bag so that all *
533          * variables clauses refer to are known.                 *)
534       let bag, fresh_current = Utils.add_to_bag bag fresh_current in
535         (* We superpose current with active clauses *)
536       let bag, maxvar, additional_new_clauses =
537         superposition_with_table bag maxvar fresh_current atable 
538       in
539         debug "Another superposition";
540       let new_clauses = new_clauses @ additional_new_clauses in
541         debug (Printf.sprintf "Demodulating %d clauses"
542                  (List.length new_clauses));
543       let bag, new_clauses = 
544         HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
545       in
546         debug "Demodulated new clauses";
547       bag, maxvar, (alist, atable), new_clauses
548     ;;
549
550     let infer_left bag maxvar goal (_alist, atable) =
551         (* We superpose the goal with active clauses *)
552       let bag, maxvar, new_goals =      
553         superposition_with_table bag maxvar goal atable 
554       in
555         debug "Superposed goal with active clauses";
556         (* We simplify the new goals with active clauses *)
557       let bag, new_goals = 
558         List.fold_left
559          (fun (bag, acc) g -> 
560             match simplify_goal maxvar atable bag [] g with
561               | None -> assert false
562               | Some (bag,g) -> bag,g::acc)
563          (bag, []) new_goals
564       in
565         debug "Simplified new goals with active clauses";
566       bag, maxvar, List.rev new_goals
567     ;;
568
569   end