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