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