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