]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/superposition.ml
Preparing for 0.5.9 release.
[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 : Orderings.Blob) = 
15   struct
16     module IDX = Index.Index(B)
17     module Unif = FoUnif.Founif(B)
18     module Subst = FoSubst 
19     module Order = B
20     module Utils = FoUtils.Utils(B)
21     module Pp = Pp.Pp(B)
22     
23     exception Success of 
24       B.t Terms.bag 
25       * int 
26       * B.t Terms.unit_clause
27       * B.t Terms.substitution
28
29     let print s = prerr_endline (Lazy.force s);; 
30     let debug _ = ();; 
31     let enable = true;;
32
33     let rec list_first f = function
34       | [] -> None
35       | x::tl -> match f x with Some _ as x -> x | _ -> list_first f tl
36     ;;
37
38     let first_position pos ctx t f =
39       let inject_pos pos ctx = function
40         | None -> None
41         | Some (a,b,c,d) -> Some(ctx a,b,c,d,pos)
42       in
43       let rec aux pos ctx = function
44       | Terms.Leaf _ as t -> inject_pos pos ctx (f t)
45       | Terms.Var _ -> None
46       | Terms.Node l as t->
47           match f t with
48           | Some _ as x -> inject_pos pos ctx x
49           | None ->
50               let rec first pre post = function
51                 | [] -> None
52                 | t :: tl -> 
53                      let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
54                      match aux (List.length pre :: pos) newctx t with
55                      | Some _ as x -> x
56                      | None -> 
57                          if post = [] then None (* tl is also empty *)
58                          else first (pre @ [t]) (List.tl post) tl
59               in
60                 first [] (List.tl l) l 
61       in
62         aux pos ctx t
63     ;;
64                                      
65     let all_positions pos ctx t f =
66       let rec aux pos ctx = function
67       | Terms.Leaf _ as t -> f t pos ctx 
68       | Terms.Var _ -> []
69       | Terms.Node l as t-> 
70           let acc, _, _ = 
71             List.fold_left
72             (fun (acc,pre,post) t -> (* Invariant: pre @ [t] @ post = l *)
73                 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
74                 let acc = aux (List.length pre :: pos) newctx t @ acc in
75                 if post = [] then acc, l, []
76                 else acc, pre @ [t], List.tl post)
77              (f t pos ctx, [], List.tl l) l
78           in
79            acc
80       in
81         aux pos ctx t
82     ;;
83
84     let parallel_positions bag pos ctx id t f =
85       let rec aux bag pos ctx id = function
86       | Terms.Leaf _ as t -> f bag t pos ctx id
87       | Terms.Var _ as t -> bag,t,id
88       | Terms.Node (hd::l) as t->
89           let bag,t,id1 = f bag t pos ctx id in
90             if id = id1 then
91               let bag, l, _, id = 
92                 List.fold_left
93                   (fun (bag,pre,post,id) t ->
94                      let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
95                      let newpos = (List.length pre)::pos in
96                      let bag,newt,id = aux bag newpos newctx id t in
97                        if post = [] then bag, pre@[newt], [], id
98                        else bag, pre @ [newt], List.tl post, id)
99                   (bag, [hd], List.tl l, id) l
100               in
101                 bag, Terms.Node l, id
102             else bag,t,id1 
103             (* else aux bag pos ctx id1 t *) 
104       | _ -> assert false
105       in
106         aux bag pos ctx id t
107     ;;
108
109     let visit bag pos ctx id t f =
110       let rec aux bag pos ctx id subst = function
111       | Terms.Leaf _ as t -> 
112           let  bag,subst,t,id = f bag t pos ctx id
113           in assert (subst=[]); bag,t,id
114       | Terms.Var i as t ->  
115           let t= Subst.apply_subst subst t in
116             bag,t,id
117       | Terms.Node (hd::l) ->
118           let bag, l, _, id = 
119             List.fold_left
120               (fun (bag,pre,post,id) t ->
121                  let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
122                  let newpos = (List.length pre)::pos in
123                  let bag,newt,id = aux bag newpos newctx id subst t in
124                    if post = [] then bag, pre@[newt], [], id
125                    else bag, pre @ [newt], List.tl post, id)
126               (bag, [hd], List.map (Subst.apply_subst subst) (List.tl l), id) l
127           in
128           let bag,subst,t,id1 = f bag (Terms.Node l) pos ctx id
129           in
130             if id1 = id then (assert (subst=[]); bag,t,id)
131             else aux bag pos ctx id1 subst t
132       | _ -> assert false
133       in
134         aux bag pos ctx id [] t
135     ;;
136     
137     let build_clause bag filter rule t subst id id2 pos dir =
138       let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
139       let t = Subst.apply_subst subst t in
140       if filter subst then
141         let literal = 
142           match t with
143           | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
144                let o = Order.compare_terms l r in
145                Terms.Equation (l, r, ty, o)
146           | t -> Terms.Predicate t
147         in
148         let bag, uc = 
149           Terms.add_to_bag (0, literal, Terms.vars_of_term t, proof) bag
150         in
151         Some (bag, uc)
152       else
153         ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
154     ;;
155     let prof_build_clause = HExtlib.profile ~enable "build_clause";;
156     let build_clause bag filter rule t subst id id2 pos x =
157       prof_build_clause.HExtlib.profile (build_clause bag filter rule t subst id id2 pos) x
158     ;;
159       
160     
161     (* ============ simplification ================= *)
162     let prof_demod_u = HExtlib.profile ~enable "demod.unify";;
163     let prof_demod_r = HExtlib.profile ~enable "demod.retrieve_generalizations";;
164     let prof_demod_o = HExtlib.profile ~enable "demod.compare_terms";;
165     let prof_demod_s = HExtlib.profile ~enable "demod.apply_subst";;
166
167     let demod table varlist subterm =
168       let cands = 
169         prof_demod_r.HExtlib.profile 
170          (IDX.DT.retrieve_generalizations table) subterm 
171       in
172       list_first
173         (fun (dir, (id,lit,vl,_)) ->
174            match lit with
175            | Terms.Predicate _ -> assert false
176            | Terms.Equation (l,r,_,o) ->
177                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
178                try 
179                  let subst =
180                    prof_demod_u.HExtlib.profile 
181                      (Unif.unification (* (varlist@vl) *) varlist subterm) side 
182                  in 
183                  let side = 
184                    prof_demod_s.HExtlib.profile 
185                      (Subst.apply_subst subst) side 
186                  in
187                  let newside = 
188                    prof_demod_s.HExtlib.profile 
189                      (Subst.apply_subst subst) newside 
190                  in
191                  if o = Terms.Incomparable || o = Terms.Invertible then
192                    let o = 
193                      prof_demod_o.HExtlib.profile 
194                       (Order.compare_terms newside) side in
195                    (* Riazanov, pp. 45 (ii) *)
196                    if o = Terms.Lt then
197                      Some (newside, subst, id, dir)
198                    else 
199                      ((*prerr_endline ("Filtering: " ^ 
200                         Pp.pp_foterm side ^ " =(< || =)" ^ 
201                         Pp.pp_foterm newside ^ " coming from " ^ 
202                         Pp.pp_unit_clause uc );*)None)
203                  else
204                    Some (newside, subst, id, dir)
205                with FoUnif.UnificationFailure _ -> None)
206         (IDX.ClauseSet.elements cands)
207     ;;
208     let prof_demod = HExtlib.profile ~enable "demod";;
209     let demod table varlist x =
210       prof_demod.HExtlib.profile (demod table varlist) x
211     ;;
212
213     let mydemod table varlist subterm = 
214       let cands = 
215         prof_demod_r.HExtlib.profile 
216          (IDX.DT.retrieve_generalizations table) subterm 
217       in
218       list_first
219         (fun (dir, ((id,lit,vl,_) as c)) ->
220            debug (lazy("candidate: " 
221                        ^ Pp.pp_unit_clause c)); 
222            match lit with
223            | Terms.Predicate _ -> assert false
224            | Terms.Equation (l,r,_,o) ->
225                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
226                try 
227                  let subst =
228                    prof_demod_u.HExtlib.profile 
229                      (Unif.unification (* (varlist@vl) *) varlist subterm) side 
230                  in 
231                  let iside = 
232                    prof_demod_s.HExtlib.profile 
233                      (Subst.apply_subst subst) side 
234                  in
235                  let inewside = 
236                    prof_demod_s.HExtlib.profile 
237                      (Subst.apply_subst subst) newside 
238                  in
239                  if o = Terms.Incomparable || o = Terms.Invertible then
240                    let o = 
241                      prof_demod_o.HExtlib.profile 
242                       (Order.compare_terms inewside) iside in
243                    (* Riazanov, pp. 45 (ii) *)
244                    if o = Terms.Lt then
245                      Some (newside, subst, id, dir)
246                    else 
247                      ((*prerr_endline ("Filtering: " ^ 
248                         Pp.pp_foterm side ^ " =(< || =)" ^ 
249                         Pp.pp_foterm newside ^ " coming from " ^ 
250                         Pp.pp_unit_clause uc );*)
251                        debug (lazy "not applied");None)
252                  else
253                    Some (newside, subst, id, dir)
254                with FoUnif.UnificationFailure _ -> 
255                  debug (lazy "not applied"); None)
256         (IDX.ClauseSet.elements cands)
257     ;;
258
259     let ctx_demod table vl bag t pos ctx id =
260       match mydemod table vl t with
261         | None -> (bag,[],t,id)
262         | Some (newside, subst, id2, dir) ->
263             let inewside = Subst.apply_subst subst newside in
264             match build_clause bag (fun _ -> true)
265               Terms.Demodulation (ctx inewside) subst id id2 pos dir
266             with
267               | None -> assert false
268               | Some (bag,(id,_,_,_)) ->
269                     (bag,subst,newside,id)
270     ;;
271       
272     let rec demodulate bag (id, literal, vl, pr) table =
273       debug (lazy ("demodulate " ^ (string_of_int id)));
274        match literal with
275       | Terms.Predicate t -> (* assert false *)
276           let bag,_,id1 =
277             visit bag [] (fun x -> x) id t (ctx_demod table vl)
278           in          
279           let cl,_,_ = Terms.get_from_bag id1 bag in
280             bag,cl
281       | Terms.Equation (l,r,ty,_) ->
282           let bag,l,id1 = 
283             visit bag [2]
284             (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id l
285             (ctx_demod table vl)
286           in 
287           let bag,_,id2 = 
288             visit bag [3]
289               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 r
290               (ctx_demod table vl)
291           in 
292           let cl,_,_ = Terms.get_from_bag id2 bag in
293             bag,cl
294     ;;
295       
296     let parallel_demod table vl bag t pos ctx id =
297       match demod table vl t with
298         | None -> (bag,t,id)
299         | Some (newside, subst, id2, dir) ->
300             match build_clause bag (fun _ -> true)
301               Terms.Demodulation (ctx newside) subst id id2 pos dir
302             with
303               | None -> assert false
304               | Some (bag,(id,_,_,_)) ->
305                     (bag,newside,id)
306     ;;
307
308     let are_alpha_eq cl1 cl2 =
309       let get_term (_,lit,_,_) =
310         match lit with
311           | Terms.Predicate _ -> assert false
312           | Terms.Equation (l,r,ty,_) ->
313               Terms.Node [Terms.Leaf B.eqP; ty; l ; r]
314       in
315         try ignore(Unif.alpha_eq (get_term cl1) (get_term cl2)) ; true
316         with FoUnif.UnificationFailure _ -> false
317     ;;
318
319     let prof_demodulate = HExtlib.profile ~enable "demodulate";;
320     let demodulate bag clause x =
321       prof_demodulate.HExtlib.profile (demodulate bag clause) x
322     ;;
323
324     (* move away *)
325     let is_identity_clause = function
326       | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
327       | _, Terms.Equation (_,_,_,_), _, _ -> false
328       | _, Terms.Predicate _, _, _ -> assert false          
329     ;;
330
331     let is_identity_goal = function
332       | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> Some []
333       | _, Terms.Equation (l,r,_,_), vl, proof ->
334           (try Some (Unif.unification (* vl *) [] l r)
335            with FoUnif.UnificationFailure _ -> None)
336       | _, Terms.Predicate _, _, _ -> assert false          
337     ;;
338
339     let build_new_clause_reloc bag maxvar filter rule t subst id id2 pos dir =
340       let maxvar, _vl, subst = Utils.relocate maxvar (Terms.vars_of_term
341       (Subst.apply_subst subst t)) subst in
342       match build_clause bag filter rule t subst id id2 pos dir with
343       | Some (bag, c) -> Some ((bag, maxvar), c), subst
344       | None -> None,subst
345     ;;
346
347     let build_new_clause bag maxvar filter rule t subst id id2 pos dir =
348       fst (build_new_clause_reloc bag maxvar filter rule t 
349              subst id id2 pos dir)
350     ;;
351
352     let prof_build_new_clause = HExtlib.profile ~enable "build_new_clause";;
353     let build_new_clause bag maxvar filter rule t subst id id2 pos x =
354       prof_build_new_clause.HExtlib.profile (build_new_clause bag maxvar filter
355       rule t subst id id2 pos) x
356     ;;
357
358     let fold_build_new_clause bag maxvar id rule filter res =
359       let (bag, maxvar), res =
360        HExtlib.filter_map_acc 
361          (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
362             build_new_clause bag maxvar filter rule t subst id id2 pos dir)
363          (bag, maxvar) res
364       in
365        bag, maxvar, res
366     ;;
367     
368     let rewrite_eq ~unify l r ty vl table =
369       let retrieve = if unify then IDX.DT.retrieve_unifiables
370       else IDX.DT.retrieve_generalizations in
371       let lcands = retrieve table l in
372       let rcands = retrieve table r in
373       let f b c = 
374         let id, dir, l, r, vl = 
375           match c with
376             | (d, (id,Terms.Equation (l,r,ty,_),vl,_))-> id, d, l, r, vl
377             |_ -> assert false 
378         in 
379         let reverse = (dir = Terms.Left2Right) = b in
380         let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
381         else r,l, Terms.Right2Left in
382           (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
383       in
384       let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
385       let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
386       let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
387       let locked_vars = if unify then [] else vl in
388       let rec aux = function
389         | [] -> None
390         | (id2,dir,c,vl1)::tl ->
391             try
392               let subst = Unif.unification (* (vl@vl1) *) locked_vars c t in
393               Some (id2, dir, subst)
394             with FoUnif.UnificationFailure _ -> aux tl
395       in
396         aux (cands1 @ cands2)
397     ;;
398
399     let is_subsumed ~unify bag maxvar (id, lit, vl, _) table =
400       match lit with
401       | Terms.Predicate _ -> assert false
402       | Terms.Equation (l,r,ty,_) -> 
403           match rewrite_eq ~unify l r ty vl table with
404             | None -> None
405             | Some (id2, dir, subst) ->
406                 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
407                   build_new_clause bag maxvar (fun _ -> true)
408                   Terms.Superposition id_t subst id id2 [2] dir 
409     ;;
410     let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
411     let is_subsumed ~unify bag maxvar c x =
412       prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
413     ;;
414     (* id refers to a clause proving contextl l = contextr r *)
415
416     let rec deep_eq ~unify l r ty pos contextl contextr table acc =
417       match acc with 
418       | None -> None
419       | Some(bag,maxvar,(id,lit,vl,p),subst) -> 
420           (* prerr_endline ("input subst = "^Pp.pp_substitution subst); *)
421           let l = Subst.apply_subst subst l in 
422           let r = Subst.apply_subst subst r in 
423             try 
424               let subst1 = Unif.unification (* vl *) [] l r in
425               let lit = 
426                 match lit with Terms.Predicate _ -> assert false
427                   | Terms.Equation (l,r,ty,o) -> 
428                      Terms.Equation (FoSubst.apply_subst subst1 l,
429                        FoSubst.apply_subst subst1 r, ty, o)
430               in
431                 Some(bag,maxvar,(id,lit,vl,p),Subst.concat subst1 subst)
432             with FoUnif.UnificationFailure _ -> 
433               match rewrite_eq ~unify l r ty vl table with
434               | Some (id2, dir, subst1) ->
435                   (* prerr_endline ("subst1 = "^Pp.pp_substitution subst1);
436                      prerr_endline ("old subst = "^Pp.pp_substitution subst);*)
437                   let newsubst = Subst.concat subst1 subst in
438                   let id_t = 
439                     FoSubst.apply_subst newsubst
440                       (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r]) 
441                   in
442                     (match 
443                       build_new_clause_reloc bag maxvar (fun _ -> true)
444                         Terms.Superposition id_t 
445                         subst1 id id2 (pos@[2]) dir 
446                     with
447                     | Some ((bag, maxvar), c), r ->
448                         (* prerr_endline ("r = "^Pp.pp_substitution r); *)
449                         let newsubst = Subst.flat 
450                           (Subst.concat r subst) in
451                         Some(bag,maxvar,c,newsubst)
452                     | None, _ -> assert false)
453               | None ->
454                   match l,r with 
455                   | Terms.Node (a::la), Terms.Node (b::lb) when 
456                       a = b && List.length la = List.length lb ->
457                       let acc,_,_,_ =
458                         List.fold_left2 
459                           (fun (acc,pre,postl,postr) a b -> 
460                              let newcl = 
461                               fun x -> contextl(Terms.Node (pre@(x::postl))) in
462                              let newcr = 
463                               fun x -> contextr(Terms.Node (pre@(x::postr))) in
464                              let newpos = List.length pre::pos in
465                              let footail l =
466                                if l = [] then [] else List.tl l in
467                                (deep_eq ~unify a b ty 
468                                  newpos newcl newcr table acc,pre@[b],
469                                  footail postl, footail postr))
470                           (acc,[a],List.tl la,List.tl lb) la lb
471                       in acc
472                   | _,_ -> None
473     ;;
474
475     let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
476     let deep_eq ~unify l r ty pos contextl contextr table x =
477       prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
478     ;;
479
480     let rec orphan_murder bag acc i =
481       match Terms.get_from_bag i bag with
482         | (_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
483         | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
484         | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
485             if (List.mem i acc) then (false,acc)
486             else match orphan_murder bag acc i1 with
487               | (true,acc) -> (true,acc)
488               | (false,acc) ->
489                   let (res,acc) = orphan_murder bag acc i2 in
490                   if res then res,acc else res,i::acc
491     ;;
492
493     let orphan_murder bag actives cl =
494       let (id,_,_,_) = cl in
495       let actives = List.map (fun (i,_,_,_) -> i) actives in
496       let (res,_) = orphan_murder bag actives id in
497         if res then debug (lazy "Orphan murdered"); res
498     ;;
499     let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
500     let orphan_murder bag actives x =
501       prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
502     ;;
503
504     (* demodulate and check for subsumption *)
505     let simplify table maxvar bag clause =
506       debug (lazy "simplify...");
507       if is_identity_clause clause then bag,None
508       (* else if orphan_murder bag actives clause then bag,None *)
509       else let bag, clause = demodulate bag clause table in
510       if is_identity_clause clause then bag,None
511       else
512         match is_subsumed ~unify:false bag maxvar clause table with
513           | None -> bag, Some clause
514           | Some _ -> bag, None
515     ;;
516
517     let simplify table maxvar bag clause =
518       match simplify table maxvar bag clause with
519         | bag, None ->
520             let (id,_,_,_) = clause in
521             let (_,_,iter) = Terms.get_from_bag id bag in
522             Terms.replace_in_bag (clause,true,iter) bag, None
523         | bag, Some clause -> bag, Some clause
524     (*let (id,_,_,_) = clause in
525             if orphan_murder bag clause then
526               Terms.M.add id (clause,true) bag, Some clause
527             else bag, Some clause*)
528     ;;
529     let prof_simplify = HExtlib.profile ~enable "simplify";;
530     let simplify table maxvar bag x =
531       prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
532     ;;
533
534     let one_pass_simplification new_clause (alist,atable) bag maxvar =
535       match simplify atable maxvar bag new_clause with
536         | bag,None -> bag,None (* new_clause has been discarded *)
537         | bag,(Some clause) ->
538             let ctable = IDX.index_unit_clause IDX.DT.empty clause in
539             let bag, alist, atable = 
540               List.fold_left 
541                 (fun (bag, alist, atable) c ->
542                    match simplify ctable maxvar bag c with
543                      |bag,None -> (bag,alist,atable)
544                         (* an active clause as been discarded *)
545                      |bag,Some c1 ->
546                         bag, c :: alist, IDX.index_unit_clause atable c)
547                 (bag,[],IDX.DT.empty) alist
548             in
549               bag, Some (clause, (alist,atable))
550     ;;
551     let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
552     let one_pass_simplification new_clause t bag x =
553       prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
554     ;;
555
556     let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
557       let atable1 =
558         if new_cl then atable else
559         IDX.index_unit_clause atable cl
560       in
561         (* Simplification of new_clause with :      *
562          * - actives and cl if new_clause is not cl *
563          * - only actives otherwise                 *)
564         match
565           simplify atable1 maxvar bag new_clause with
566           | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
567           | bag,Some clause ->
568               (* Simplification of each active clause with clause *
569                * which is the simplified form of new_clause       *)
570               let ctable = IDX.index_unit_clause IDX.DT.empty clause in
571               let bag, newa, alist, atable = 
572                 List.fold_left 
573                   (fun (bag, newa, alist, atable) c ->
574                      match simplify ctable maxvar bag c with
575                        |bag,None -> (bag, newa, alist, atable)
576                           (* an active clause as been discarded *)
577                        |bag,Some c1 ->
578                             if (c1 == c) then 
579                               bag, newa, c :: alist,
580                             IDX.index_unit_clause atable c
581                             else
582                               bag, c1 :: newa, alist, atable)                  
583                   (bag,[],[],IDX.DT.empty) alist
584               in
585                 if new_cl then
586                   bag, (Some cl, Some (clause, (alist,atable), newa))
587                 else
588                   (* if new_clause is not cl, we simplify cl with clause *)
589                   match simplify ctable maxvar bag cl with
590                     | bag,None ->
591                         (* cl has been discarded *)
592                         bag,(None, Some (clause, (alist,atable), newa))
593                     | bag,Some cl1 ->
594                         bag,(Some cl1, Some (clause, (alist,atable), newa))
595     ;;
596     let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
597     let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
598       prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
599     ;;
600
601     let keep_simplified cl (alist,atable) bag maxvar =
602       let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
603         if new_cl then
604           match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
605             | _,(None, _) -> assert false
606             | bag,(Some _, None) -> bag,None
607             | bag,(Some _, Some (clause, (alist,atable), newa)) ->
608                 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
609                   bag (newa@newc)
610         else
611           match newc with
612             | [] -> bag, Some (cl, (alist,atable))
613             | hd::tl ->
614                 match simplification_step ~new_cl cl
615                   (alist,atable) bag maxvar hd with
616                   | _,(None,None) -> assert false
617                   | bag,(Some _,None) ->
618                       keep_simplified_aux ~new_cl cl (alist,atable) bag tl
619                   | bag,(None, Some _) -> bag,None
620                   | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
621                       let alist,atable =
622                      (clause::alist, IDX.index_unit_clause atable clause)
623                       in
624                         keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
625                           bag (newa@tl)
626       in
627         keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
628     ;;
629     let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
630     let keep_simplified cl t bag x =
631       prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
632     ;;
633
634     (* this is like simplify but raises Success *)
635     let simplify_goal ~no_demod maxvar table bag g_actives clause = 
636       let bag, clause = 
637         if no_demod then bag, clause else demodulate bag clause table 
638       in
639       let _ = debug (lazy ("demodulated goal  : " 
640                              ^ Pp.pp_unit_clause clause))
641       in
642       if List.exists (are_alpha_eq clause) g_actives then None
643       else match (is_identity_goal clause) with
644         | Some subst -> raise (Success (bag,maxvar,clause,subst))
645         | None ->
646         let (id,lit,vl,_) = clause in 
647         (* this optimization makes sense only if we demodulated, since in 
648            that case the clause should have been turned into an identity *)
649         if (vl = [] && not(no_demod)) 
650         then Some (bag,clause)
651         else
652          let l,r,ty = 
653            match lit with
654              | Terms.Equation(l,r,ty,_) -> l,r,ty
655              | _ -> assert false 
656          in
657          match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x) 
658            table (Some(bag,maxvar,clause,Subst.id_subst)) with
659          | None -> Some (bag,clause)
660          | Some (bag,maxvar,cl,subst) -> 
661              debug (lazy "Goal subsumed");
662              raise (Success (bag,maxvar,cl,subst))
663 (*
664         match is_subsumed ~unify:true bag maxvar clause table with
665         | None -> Some (bag, clause)
666         | Some ((bag,maxvar),c) -> 
667             prerr_endline "Goal subsumed";
668             raise (Success (bag,maxvar,c))
669 *)
670     ;;
671
672     let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
673     let  simplify_goal ~no_demod maxvar table bag g_actives x =
674       prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
675     ;;
676
677     (* =================== inference ===================== *)
678
679     (* this is OK for both the sup_left and sup_right inference steps *)
680     let superposition table varlist subterm pos context =
681       let cands = IDX.DT.retrieve_unifiables table subterm in
682       HExtlib.filter_map
683         (fun (dir, (id,lit,vl,_ (*as uc*))) ->
684            match lit with
685            | Terms.Predicate _ -> assert false
686            | Terms.Equation (l,r,_,o) ->
687                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
688                try 
689                  let subst = 
690                    Unif.unification (* (varlist@vl)*)  [] subterm side 
691                  in 
692                  if o = Terms.Incomparable || o = Terms.Invertible then
693                    let side = Subst.apply_subst subst side in
694                    let newside = Subst.apply_subst subst newside in
695                    let o = Order.compare_terms side newside in
696                    (* XXX: check Riazanov p. 33 (iii) *)
697                    if o <> Terms.Lt && o <> Terms.Eq then  
698                      Some (context newside, subst, id, pos, dir)
699                    else 
700                      ((*prerr_endline ("Filtering: " ^ 
701                         Pp.pp_foterm side ^ " =(< || =)" ^ 
702                         Pp.pp_foterm newside);*)None)
703                  else
704                    Some (context newside, subst, id, pos, dir)
705                with FoUnif.UnificationFailure _ -> None)
706         (IDX.ClauseSet.elements cands)
707     ;;
708
709     (* Superposes selected equation with equalities in table *)
710     let superposition_with_table bag maxvar (id,selected,vl,_) table =
711       match selected with 
712       | Terms.Predicate _ -> assert false
713       | Terms.Equation (l,r,ty,Terms.Lt) ->
714           fold_build_new_clause bag maxvar id Terms.Superposition
715             (fun _ -> true)
716             (all_positions [3] 
717               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
718               r (superposition table vl))
719       | Terms.Equation (l,r,ty,Terms.Invertible)
720       | Terms.Equation (l,r,ty,Terms.Gt) ->
721           fold_build_new_clause bag maxvar id Terms.Superposition
722             (fun _ -> true)
723             (all_positions [2] 
724               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
725               l (superposition table vl))
726       | Terms.Equation (l,r,ty,Terms.Incomparable) ->
727           let filtering avoid subst = (* Riazanov: p.33 condition (iv) *)
728             let l = Subst.apply_subst subst l in
729             let r = Subst.apply_subst subst r in
730             let o = Order.compare_terms l r in
731             o <> avoid && o <> Terms.Eq
732           in
733           let bag, maxvar,r_terms =
734             fold_build_new_clause bag maxvar id Terms.Superposition
735               (filtering Terms.Gt)
736               (all_positions [3] 
737                  (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
738                  r (superposition table vl))
739           in
740           let bag, maxvar, l_terms =
741             fold_build_new_clause bag maxvar id Terms.Superposition
742               (filtering Terms.Lt)
743               (all_positions [2] 
744                  (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
745                  l (superposition table vl))
746           in
747             bag, maxvar, r_terms @ l_terms
748       | _ -> assert false
749     ;;
750
751     (* the current equation is normal w.r.t. demodulation with atable
752      * (and is not the identity) *)
753     let infer_right bag maxvar current (alist,atable) = 
754       (* We demodulate actives clause with current until all *
755        * active clauses are reduced w.r.t each other         *)
756       (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
757       let ctable = IDX.index_unit_clause IDX.DT.empty current in
758       (* let bag, (alist, atable) = 
759         let bag, alist = 
760           HExtlib.filter_map_acc (simplify ctable) bag alist
761         in
762         bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
763       in*)
764         debug (lazy "Simplified active clauses with fact");
765       (* We superpose active clauses with current *)
766       let bag, maxvar, new_clauses =
767         List.fold_left 
768           (fun (bag, maxvar, acc) active ->
769              let bag, maxvar, newc = 
770                superposition_with_table bag maxvar active ctable 
771              in
772              bag, maxvar, newc @ acc)
773           (bag, maxvar, []) alist
774       in
775         debug
776         (lazy 
777          ("New clauses :" ^ (String.concat ";\n" 
778             (List.map Pp.pp_unit_clause new_clauses)))); 
779         debug (lazy "First superpositions");
780         (* We add current to active clauses so that it can be *
781          * superposed with itself                             *)
782       let alist, atable = 
783         current :: alist, IDX.index_unit_clause atable current
784       in
785         debug (lazy "Indexed");
786       let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
787         (* We need to put fresh_current into the bag so that all *
788          * variables clauses refer to are known.                 *)
789       let bag, fresh_current = Terms.add_to_bag fresh_current bag in
790         (* We superpose current with active clauses *)
791       let bag, maxvar, additional_new_clauses =
792         superposition_with_table bag maxvar fresh_current atable 
793       in
794         debug (lazy "Another superposition");
795       let new_clauses = new_clauses @ additional_new_clauses in
796         (* debug (lazy (Printf.sprintf "Demodulating %d clauses"
797                  (List.length new_clauses))); *)
798       let bag, new_clauses = 
799         HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
800       in
801         debug (lazy "Demodulated new clauses");
802       bag, maxvar, (alist, atable), new_clauses
803     ;;
804
805     let prof_ir = HExtlib.profile ~enable "infer_right";;
806     let infer_right bag maxvar current t = 
807       prof_ir.HExtlib.profile (infer_right bag maxvar current) t
808     ;;
809
810     let infer_left bag maxvar goal (_alist, atable) =
811         (* We superpose the goal with active clauses *)
812      if (match goal with (_,_,[],_) -> true | _ -> false) then bag, maxvar, []
813      else
814       let bag, maxvar, new_goals =        
815         superposition_with_table bag maxvar goal atable 
816       in
817         debug(lazy  "Superposed goal with active clauses");
818         (* We simplify the new goals with active clauses *)
819       let bag, new_goals = 
820         List.fold_left
821          (fun (bag, acc) g -> 
822             match simplify_goal ~no_demod:false maxvar atable bag [] g with
823               | None -> assert false
824               | Some (bag,g) -> bag,g::acc)
825          (bag, []) new_goals
826       in
827         debug (lazy "Simplified new goals with active clauses");
828       bag, maxvar, List.rev new_goals
829     ;;
830
831     let prof_il = HExtlib.profile ~enable "infer_left";;
832     let infer_left bag maxvar goal t = 
833       prof_il.HExtlib.profile (infer_left bag maxvar goal) t
834     ;;
835
836   end