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