]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/superposition.ml
96b16bb07b6a66dd674495d750e76075185faae2
[helm.git] / helm / software / components / ng_paramodulation / superposition.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 (* $Id: index.mli 9822 2009-06-03 15:37:06Z tassi $ *)
13
14 module Superposition (B : Terms.Blob) = 
15   struct
16     module IDX = Index.Index(B)
17     module Unif = FoUnif.Founif(B)
18     module Subst = FoSubst 
19     module Order = Orderings.Orderings(B)
20     module Utils = FoUtils.Utils(B)
21     module Pp = Pp.Pp(B)
22     
23     exception Success of B.t Terms.bag * int * B.t Terms.unit_clause
24
25     let debug s = prerr_endline s;;
26     let debug _ = ();;
27
28     let rec list_first f = function
29       | [] -> None
30       | x::tl -> match f x with Some _ as x -> x | _ -> list_first f tl
31     ;;
32
33     let first_position pos ctx t f =
34       let rec aux pos ctx = function
35       | Terms.Leaf _ as t -> f t pos ctx 
36       | Terms.Var _ -> None
37       | Terms.Node l as t->
38           match f t pos ctx with
39           | Some _ as x -> x
40           | None ->
41               let rec first pre post = function
42                 | [] -> None
43                 | t :: tl -> 
44                      let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
45                      match aux (List.length pre :: pos) newctx t with
46                      | Some _ as x -> x
47                      | None -> 
48                          if post = [] then None (* tl is also empty *)
49                          else first (pre @ [t]) (List.tl post) tl
50               in
51                 first [] (List.tl l) l 
52       in
53         aux pos ctx t
54     ;;
55                                      
56     let all_positions pos ctx t f =
57       let rec aux pos ctx = function
58       | Terms.Leaf _ as t -> f t pos ctx 
59       | Terms.Var _ -> []
60       | Terms.Node l as t-> 
61           let acc, _, _ = 
62             List.fold_left
63             (fun (acc,pre,post) t -> (* Invariant: pre @ [t] @ post = l *)
64                 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
65                 let acc = aux (List.length pre :: pos) newctx t @ acc in
66                 if post = [] then acc, l, []
67                 else acc, pre @ [t], List.tl post)
68              (f t pos ctx, [], List.tl l) l
69           in
70            acc
71       in
72         aux pos ctx t
73     ;;
74
75     let vars_of_term t =
76       let rec aux acc = function
77         | Terms.Leaf _ -> acc
78         | Terms.Var i -> if (List.mem i acc) then acc else i::acc
79         | Terms.Node l -> List.fold_left aux acc l
80       in aux [] t
81     ;;
82     
83     let build_clause bag filter rule t subst vl id id2 pos dir =
84       let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
85       let t = Subst.apply_subst subst t in
86       if filter t then
87         let literal = 
88           match t with
89           | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
90                let o = Order.compare_terms l r in
91                Terms.Equation (l, r, ty, o)
92           | t -> Terms.Predicate t
93         in
94         let bag, uc = 
95           Terms.add_to_bag (0, literal, vars_of_term t, proof) bag
96         in
97         Some (bag, uc)
98       else
99         ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
100     ;;
101       
102     
103     (* ============ simplification ================= *)
104
105     let demod table varlist subterm pos context =
106       let cands = IDX.DT.retrieve_generalizations table subterm in
107       list_first
108         (fun (dir, (id,lit,vl,_)) ->
109            match lit with
110            | Terms.Predicate _ -> assert false
111            | Terms.Equation (l,r,_,o) ->
112                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
113                try 
114                  let subst, varlist = 
115                    Unif.unification (varlist@vl) varlist subterm side 
116                  in
117                  if o = Terms.Incomparable then
118                    let side = Subst.apply_subst subst side in
119                    let newside = Subst.apply_subst subst newside in
120                    let o = Order.compare_terms newside side in
121                    (* Riazanov, pp. 45 (ii) *)
122                    if o = Terms.Lt then
123                      Some (context newside, subst, varlist, id, pos, dir)
124                    else 
125                      ((*prerr_endline ("Filtering: " ^ 
126                         Pp.pp_foterm side ^ " =(< || =)" ^ 
127                         Pp.pp_foterm newside ^ " coming from " ^ 
128                         Pp.pp_unit_clause uc );*)None)
129                  else
130                    Some (context newside, subst, varlist, id, pos, dir)
131                with FoUnif.UnificationFailure _ -> None)
132         (IDX.ClauseSet.elements cands)
133     ;;
134
135     let demodulate_once ~jump_to_right bag (id, literal, vl, pr) table =
136       match literal with
137       | Terms.Predicate t -> assert false
138       | Terms.Equation (l,r,ty,_) ->
139         let left_position = if jump_to_right then None else
140           first_position [2]
141             (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) l
142             (demod table vl)
143         in
144         match left_position with
145           | Some (newt, subst, varlist, id2, pos, dir) ->
146               begin
147                 match build_clause bag (fun _ -> true) Terms.Demodulation 
148                   newt subst varlist id id2 pos dir
149                 with
150                   | None -> assert false
151                   | Some x -> Some (x,false)
152               end
153           | None ->
154               match first_position
155                 [3] (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) r
156                 (demod table vl)
157               with
158                 | None -> None
159                 | Some (newt, subst, varlist, id2, pos, dir) ->
160                     match build_clause bag (fun _ -> true)
161                       Terms.Demodulation newt subst varlist id id2 pos dir
162                     with
163                         | None -> assert false
164                         | Some x -> Some (x,true)
165     ;;
166
167     let rec demodulate ~jump_to_right bag clause table =
168       match demodulate_once ~jump_to_right bag clause table with
169       | None -> bag, clause
170       | Some ((bag, clause),r) -> demodulate ~jump_to_right:r
171           bag clause table
172     ;;
173
174     let demodulate bag clause table = demodulate ~jump_to_right:false
175       bag clause table
176     ;;
177
178     (* move away *)
179     let is_identity_clause ~unify = function
180       | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
181       | _, Terms.Equation (l,r,_,_), vl, proof when unify ->
182           (try ignore(Unif.unification vl [] l r); true
183           with FoUnif.UnificationFailure _ -> false)
184       | _, Terms.Equation (_,_,_,_), _, _ -> false
185       | _, Terms.Predicate _, _, _ -> assert false        
186     ;;
187
188     let build_new_clause bag maxvar filter rule t subst vl id id2 pos dir =
189       let maxvar, vl, relocsubst = Utils.relocate maxvar vl in
190       let subst = Subst.concat relocsubst subst in
191       match build_clause bag filter rule t subst vl id id2 pos dir with
192       | Some (bag, c) -> Some ((bag, maxvar), c)
193       | None -> None
194     ;;
195
196     let fold_build_new_clause bag maxvar id rule filter res =
197       let (bag, maxvar), res =
198        HExtlib.filter_map_acc 
199          (fun (bag, maxvar) (t,subst,vl,id2,pos,dir) ->
200             build_new_clause bag maxvar filter rule t subst vl id id2 pos dir)
201          (bag, maxvar) res
202       in
203        bag, maxvar, res
204     ;;
205
206     
207     let rewrite_eq ~unify l r ty vl table =
208       let retrieve = if unify then IDX.DT.retrieve_unifiables
209       else IDX.DT.retrieve_generalizations in
210       let lcands = retrieve table l in
211       let rcands = retrieve table r in
212       let f b c = 
213         let id, dir, l, r, vl = 
214           match c with
215             | (d, (id,Terms.Equation (l,r,ty,_),vl,_))-> id, d, l, r, vl
216             |_ -> assert false 
217         in 
218         let reverse = (dir = Terms.Left2Right) = b in
219         let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
220         else r,l, Terms.Right2Left in
221           (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
222       in
223       let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
224       let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
225       let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
226       let locked_vars = if unify then [] else vl in
227       let rec aux = function
228         | [] -> None
229         | (id2,dir,c,vl1)::tl ->
230             try
231               let subst,vl1 = Unif.unification (vl@vl1) locked_vars c t in
232               Some (id2, dir, subst)
233             with FoUnif.UnificationFailure _ -> aux tl
234       in
235         aux (cands1 @ cands2)
236     ;;
237
238     let is_subsumed ~unify bag maxvar (id, lit, vl, _) table =
239       match lit with
240       | Terms.Predicate _ -> assert false
241       | Terms.Equation (l,r,ty,_) -> 
242           match rewrite_eq ~unify l r ty vl table with
243             | None -> None
244             | Some (id2, dir, subst) ->
245                 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
246                   build_new_clause bag maxvar (fun _ -> true)
247                     Terms.Superposition id_t subst [] id id2 [2] dir 
248     ;;
249     (* id refers to a clause proving contextl l = contextr r *)
250
251     let rec deep_eq ~unify l r ty pos contextl contextr table acc =
252       match acc with 
253       | None -> None
254       | Some(bag,maxvar,(id,lit,vl,p),subst) -> 
255           let l = Subst.apply_subst subst l in 
256           let r = Subst.apply_subst subst r in 
257             try 
258               let subst1,vl1 = Unif.unification vl [] l r in
259               let lit = 
260                 match lit with Terms.Predicate _ -> assert false
261                   | Terms.Equation (l,r,ty,o) -> 
262                      Terms.Equation (FoSubst.apply_subst subst1 l,
263                        FoSubst.apply_subst subst1 r, ty, o)
264               in
265                 Some(bag,maxvar,(id,lit,vl1,p),Subst.concat subst1 subst)
266             with FoUnif.UnificationFailure _ -> 
267               match rewrite_eq ~unify l r ty vl table with
268               | Some (id2, dir, subst1) ->
269                   let newsubst = Subst.concat subst1 subst in
270                   let id_t = 
271                     FoSubst.apply_subst newsubst
272                       (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r]) 
273                   in
274                     (match 
275                       build_new_clause bag maxvar (fun _ -> true)
276                         Terms.Superposition id_t 
277                           subst1 [] id id2 (pos@[2]) dir 
278                     with
279                     | Some ((bag, maxvar), c) -> 
280                         Some(bag,maxvar,c,newsubst)
281                     | None -> assert false)
282               | None ->
283                   match l,r with 
284                   | Terms.Node (a::la), Terms.Node (b::lb) when 
285                       a = b && List.length la = List.length lb ->
286                       let acc,_,_,_ =
287                         List.fold_left2 
288                           (fun (acc,pre,postl,postr) a b -> 
289                              let newcl = 
290                               fun x -> contextl(Terms.Node (pre@(x::postl))) in
291                              let newcr = 
292                               fun x -> contextr(Terms.Node (pre@(x::postr))) in
293                              let newpos = List.length pre::pos in
294                              let footail l =
295                                if l = [] then [] else List.tl l in
296                                (deep_eq ~unify a b ty 
297                                  newpos newcl newcr table acc,pre@[b],
298                                  footail postl, footail postr))
299                           (acc,[a],List.tl la,List.tl lb) la lb
300                       in acc
301                   | _,_ -> None
302     ;;
303
304     let rec orphan_murder bag acc i =
305       match Terms.get_from_bag i bag with
306         | (_,_,_,Terms.Exact _),discarded -> (discarded,acc)
307         | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),true -> (true,acc)
308         | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),false ->
309             if (List.mem i acc) then (false,acc)
310             else match orphan_murder bag acc i1 with
311               | (true,acc) -> (true,acc)
312               | (false,acc) ->
313                   let (res,acc) = orphan_murder bag acc i2 in
314                   if res then res,acc else res,i::acc
315     ;;
316
317     let orphan_murder bag actives cl =
318       let (id,_,_,_) = cl in
319       let actives = List.map (fun (i,_,_,_) -> i) actives in
320       let (res,_) = orphan_murder bag actives id in
321         if res then debug "Orphan murdered"; res
322     ;;
323
324     (* demodulate and check for subsumption *)
325     let simplify table maxvar bag clause = 
326       if is_identity_clause ~unify:false clause then bag,None
327       (* else if orphan_murder bag actives clause then bag,None *)
328       else let bag, clause = demodulate bag clause table in
329       if is_identity_clause ~unify:false clause then bag,None
330       else
331         match is_subsumed ~unify:false bag maxvar clause table with
332           | None -> bag, Some clause
333           | Some _ -> bag, None
334     ;;
335
336     let simplify table maxvar bag clause =
337       match simplify table maxvar bag clause with
338         | bag, None ->
339             Terms.replace_in_bag (clause,true) bag, None
340         | bag, Some clause -> bag, Some clause
341     (*let (id,_,_,_) = clause in
342             if orphan_murder bag clause then
343               Terms.M.add id (clause,true) bag, Some clause
344             else bag, Some clause*)
345     ;;
346
347     let one_pass_simplification new_clause (alist,atable) bag maxvar =
348       match simplify atable maxvar bag new_clause with
349         | bag,None -> bag,None (* new_clause has been discarded *)
350         | bag,(Some clause) ->
351             let ctable = IDX.index_unit_clause IDX.DT.empty clause in
352             let bag, alist, atable = 
353               List.fold_left 
354                 (fun (bag, alist, atable) c ->
355                    match simplify ctable maxvar bag c with
356                      |bag,None -> (bag,alist,atable)
357                         (* an active clause as been discarded *)
358                      |bag,Some c1 ->
359                         bag, c :: alist, IDX.index_unit_clause atable c)
360                 (bag,[],IDX.DT.empty) alist
361             in
362               bag, Some (clause, (alist,atable))
363     ;;
364
365     let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
366       let atable1 =
367         if new_cl then atable else
368         IDX.index_unit_clause atable cl
369       in
370         (* Simplification of new_clause with :      *
371          * - actives and cl if new_clause is not cl *
372          * - only actives otherwise                 *)
373         match
374           simplify atable1 maxvar bag new_clause with
375           | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
376           | bag,Some clause ->
377               (* Simplification of each active clause with clause *
378                * which is the simplified form of new_clause       *)
379               let ctable = IDX.index_unit_clause IDX.DT.empty clause in
380               let bag, newa, alist, atable = 
381                 List.fold_left 
382                   (fun (bag, newa, alist, atable) c ->
383                      match simplify ctable maxvar bag c with
384                        |bag,None -> (bag, newa, alist, atable)
385                           (* an active clause as been discarded *)
386                        |bag,Some c1 ->
387                             if (c1 == c) then 
388                               bag, newa, c :: alist,
389                             IDX.index_unit_clause atable c
390                             else
391                               bag, c1 :: newa, alist, atable)             
392                   (bag,[],[],IDX.DT.empty) alist
393               in
394                 if new_cl then
395                   bag, (Some cl, Some (clause, (alist,atable), newa))
396                 else
397                   (* if new_clause is not cl, we simplify cl with clause *)
398                   match simplify ctable maxvar bag cl with
399                     | bag,None ->
400                         (* cl has been discarded *)
401                         bag,(None, Some (clause, (alist,atable), newa))
402                     | bag,Some cl1 ->
403                         bag,(Some cl1, Some (clause, (alist,atable), newa))
404     ;;
405
406     let keep_simplified cl (alist,atable) bag maxvar =
407       let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
408         if new_cl then
409           match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
410             | _,(None, _) -> assert false
411             | bag,(Some _, None) -> bag,None
412             | bag,(Some _, Some (clause, (alist,atable), newa)) ->
413                 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
414                   bag (newa@newc)
415         else
416           match newc with
417             | [] -> bag, Some (cl, (alist,atable))
418             | hd::tl ->
419                 match simplification_step ~new_cl cl
420                   (alist,atable) bag maxvar hd with
421                   | _,(None,None) -> assert false
422                   | bag,(Some _,None) ->
423                       keep_simplified_aux ~new_cl cl (alist,atable) bag tl
424                   | bag,(None, Some _) -> bag,None
425                   | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
426                       let alist,atable =
427                         (clause::alist, IDX.index_unit_clause atable clause)
428                       in
429                         keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
430                           bag (newa@tl)
431       in
432         keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
433     ;;
434
435     let are_alpha_eq cl1 cl2 =
436       let get_term (_,lit,_,_) =
437         match lit with
438           | Terms.Predicate _ -> assert false
439           | Terms.Equation (l,r,ty,_) ->
440               Terms.Node [Terms.Leaf B.eqP; ty; l ; r]
441       in
442         try ignore(Unif.alpha_eq (get_term cl1) (get_term cl2)) ; true
443         with FoUnif.UnificationFailure _ -> false
444 ;;
445
446     (* this is like simplify but raises Success *)
447     let simplify_goal ~no_demod maxvar table bag g_actives clause = 
448       let bag, clause = 
449         if no_demod then bag, clause else demodulate bag clause table 
450       in
451       if List.exists (are_alpha_eq clause) g_actives then None else
452       if (is_identity_clause ~unify:true clause)
453       then raise (Success (bag, maxvar, clause))
454       else   
455         let (id,lit,vl,_) = clause in 
456         if vl = [] then Some (bag,clause)
457         else
458          let l,r,ty = 
459            match lit with
460              | Terms.Equation(l,r,ty,_) -> l,r,ty
461              | _ -> assert false 
462          in
463          match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x) 
464            table (Some(bag,maxvar,clause,Subst.id_subst)) with
465          | None -> Some (bag,clause)
466          | Some (bag,maxvar,cl,subst) -> 
467              prerr_endline "Goal subsumed";
468              raise (Success (bag,maxvar,cl))
469 (*
470       else match is_subsumed ~unify:true bag maxvar clause table with
471         | None -> Some (bag, clause)
472         | Some ((bag,maxvar),c) -> 
473             prerr_endline "Goal subsumed";
474             raise (Success (bag,maxvar,c))
475 *) 
476     ;;
477
478     (* =================== inference ===================== *)
479
480     (* this is OK for both the sup_left and sup_right inference steps *)
481     let superposition table varlist subterm pos context =
482       let cands = IDX.DT.retrieve_unifiables table subterm in
483       HExtlib.filter_map
484         (fun (dir, (id,lit,vl,_ (*as uc*))) ->
485            match lit with
486            | Terms.Predicate _ -> assert false
487            | Terms.Equation (l,r,_,o) ->
488                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
489                try 
490                  let subst, varlist = 
491                    Unif.unification (varlist@vl) [] subterm side 
492                  in
493                  if o = Terms.Incomparable then
494                    let side = Subst.apply_subst subst side in
495                    let newside = Subst.apply_subst subst newside in
496                    let o = Order.compare_terms side newside in
497                    (* XXX: check Riazanov p. 33 (iii) *)
498                    if o <> Terms.Lt && o <> Terms.Eq then  
499                      Some (context newside, subst, varlist, id, pos, dir)
500                    else 
501                      ((*prerr_endline ("Filtering: " ^ 
502                         Pp.pp_foterm side ^ " =(< || =)" ^ 
503                         Pp.pp_foterm newside ^ " coming from " ^ 
504                         Pp.pp_unit_clause uc );*)None)
505                  else
506                    Some (context newside, subst, varlist, id, pos, dir)
507                with FoUnif.UnificationFailure _ -> None)
508         (IDX.ClauseSet.elements cands)
509     ;;
510
511     (* Superposes selected equation with equalities in table *)
512     let superposition_with_table bag maxvar (id,selected,vl,_) table =
513       match selected with 
514       | Terms.Predicate _ -> assert false
515       | Terms.Equation (l,r,ty,Terms.Lt) ->
516           fold_build_new_clause bag maxvar id Terms.Superposition
517             (fun _ -> true)
518             (all_positions [3] 
519               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
520               r (superposition table vl))
521       | Terms.Equation (l,r,ty,Terms.Gt) ->
522           fold_build_new_clause bag maxvar id Terms.Superposition
523             (fun _ -> true)
524             (all_positions [2] 
525               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
526               l (superposition table vl))
527       | Terms.Equation (l,r,ty,Terms.Incomparable) -> 
528           fold_build_new_clause bag maxvar id Terms.Superposition
529             (function (* Riazanov: p.33 condition (iv) *)
530               | Terms.Node [Terms.Leaf eq; ty; l; r ] when B.eq B.eqP eq -> 
531                   Order.compare_terms l r <> Terms.Eq
532               | _ -> assert false)
533             ((all_positions [3] 
534                (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
535                r (superposition table vl)) @         
536              (all_positions [2] 
537                (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
538                l (superposition table vl)))
539       | _ -> assert false
540     ;;
541
542     (* the current equation is normal w.r.t. demodulation with atable
543      * (and is not the identity) *)
544     let infer_right bag maxvar current (alist,atable) = 
545       (* We demodulate actives clause with current until all *
546        * active clauses are reduced w.r.t each other         *)
547       (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
548       let ctable = IDX.index_unit_clause IDX.DT.empty current in
549       (* let bag, (alist, atable) = 
550         let bag, alist = 
551           HExtlib.filter_map_acc (simplify ctable) bag alist
552         in
553         bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
554       in*)
555         debug "Simplified active clauses with fact";
556       (* We superpose active clauses with current *)
557       let bag, maxvar, new_clauses =
558         List.fold_left 
559           (fun (bag, maxvar, acc) active ->
560              let bag, maxvar, newc = 
561                superposition_with_table bag maxvar active ctable 
562              in
563              bag, maxvar, newc @ acc)
564           (bag, maxvar, []) alist
565       in
566         debug "First superpositions";
567         (* We add current to active clauses so that it can be *
568          * superposed with itself                             *)
569       let alist, atable = 
570         current :: alist, IDX.index_unit_clause atable current
571       in
572         debug "Indexed";
573       let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
574         (* We need to put fresh_current into the bag so that all *
575          * variables clauses refer to are known.                 *)
576       let bag, fresh_current = Terms.add_to_bag fresh_current bag in
577         (* We superpose current with active clauses *)
578       let bag, maxvar, additional_new_clauses =
579         superposition_with_table bag maxvar fresh_current atable 
580       in
581         debug "Another superposition";
582       let new_clauses = new_clauses @ additional_new_clauses in
583         debug (Printf.sprintf "Demodulating %d clauses"
584                  (List.length new_clauses));
585       let bag, new_clauses = 
586         HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
587       in
588         debug "Demodulated new clauses";
589       bag, maxvar, (alist, atable), new_clauses
590     ;;
591
592     let infer_left bag maxvar goal (_alist, atable) =
593         (* We superpose the goal with active clauses *)
594      if (match goal with (_,_,[],_) -> true | _ -> false) then bag, maxvar, []
595      else
596       let bag, maxvar, new_goals =      
597         superposition_with_table bag maxvar goal atable 
598       in
599         debug "Superposed goal with active clauses";
600         (* We simplify the new goals with active clauses *)
601       let bag, new_goals = 
602         List.fold_left
603          (fun (bag, acc) g -> 
604             match simplify_goal ~no_demod:false maxvar atable bag [] g with
605               | None -> assert false
606               | Some (bag,g) -> bag,g::acc)
607          (bag, []) new_goals
608       in
609         debug "Simplified new goals with active clauses";
610       bag, maxvar, List.rev new_goals
611     ;;
612
613   end