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