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