]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/superposition.ml
Now iterating superposition
[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 cl,maxvar
122         in
123         let bag, cl = 
124           Terms.add_to_bag (0, nlit, plit, vl, proof) bag
125         in
126         Some (bag, maxvar, cl, literal)
127       else
128         ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
129     ;;
130     let prof_build_clause = HExtlib.profile ~enable "build_clause";;
131     let build_clause ~fresh bag maxvar filter rule t subst id id2 pos x =
132       prof_build_clause.HExtlib.profile
133           (build_clause ~fresh bag maxvar filter rule t subst id id2 pos) x
134     ;;
135       
136     
137     (* ============ simplification ================= *)
138     let prof_demod_u = HExtlib.profile ~enable "demod.unify";;
139     let prof_demod_r = HExtlib.profile ~enable "demod.retrieve_generalizations";;
140     let prof_demod_o = HExtlib.profile ~enable "demod.compare_terms";;
141     let prof_demod_s = HExtlib.profile ~enable "demod.apply_subst";;
142
143     let demod table varlist subterm =
144       let cands = 
145         prof_demod_r.HExtlib.profile 
146          (IDX.DT.retrieve_generalizations table) subterm 
147       in
148       list_first
149         (fun (dir, is_pos, pos, (id,nlit,plit,vl,_)) ->
150           match nlit,plit with
151            | [], [(lit,_)] ->
152               (match lit with
153               | Terms.Predicate _ -> assert false
154               | Terms.Equation (l,r,_,o) ->
155                   let side, newside = if dir=Terms.Left2Right then l,r else r,l in
156                   try 
157                     let subst =
158                       prof_demod_u.HExtlib.profile 
159                         (Unif.unification (* (varlist@vl) *) varlist subterm) side 
160                     in 
161                     let side = 
162                       prof_demod_s.HExtlib.profile 
163                         (Subst.apply_subst subst) side 
164                     in
165                     let newside = 
166                       prof_demod_s.HExtlib.profile 
167                         (Subst.apply_subst subst) newside 
168                     in
169                     if o = Terms.Incomparable || o = Terms.Invertible then
170                       let o = 
171                         prof_demod_o.HExtlib.profile 
172                          (Order.compare_terms newside) side in
173                       (* Riazanov, pp. 45 (ii) *)
174                       if o = Terms.Lt then
175                         Some (newside, subst, id, dir)
176                       else 
177                         ((*prerr_endline ("Filtering: " ^ 
178                            Pp.pp_foterm side ^ " =(< || =)" ^ 
179                            Pp.pp_foterm newside ^ " coming from " ^ 
180                            Pp.pp_clause uc );*)None)
181                     else
182                       Some (newside, subst, id, dir)
183                   with FoUnif.UnificationFailure _ -> None)
184           |  _ -> None)
185               (IDX.ClauseSet.elements cands)
186     ;;
187     let prof_demod = HExtlib.profile ~enable "demod";;
188     let demod table varlist x =
189       prof_demod.HExtlib.profile (demod table varlist) x
190     ;;
191
192     let parallel_demod table vl clause_ctx bag t pos ctx id lit =
193       match demod table vl t with
194         | None -> (bag,t,id,lit)
195         | Some (newside, subst, id2, dir) ->
196             match build_clause ~fresh:false bag 0 (fun _ -> true)
197               Terms.Demodulation (ctx newside) subst id id2 pos dir clause_ctx
198             with
199               | None -> assert false
200               | Some (bag,_,(id,_,_,_,_),lit) ->
201                     (bag,newside,id,lit)
202     ;;
203
204     let demodulate_once ~jump_to_right bag id literal vl table clause_ctx =
205       match literal with
206       | Terms.Predicate t -> assert false
207       | Terms.Equation (l,r,ty,_) as lit ->
208           let bag,l,id1,lit = if jump_to_right then (bag,l,id,lit) else
209             parallel_positions bag [2]
210               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id lit l
211               (parallel_demod table vl clause_ctx)
212           in
213           let jump_to_right = id1 = id in
214           let bag,r,id2,lit =
215             parallel_positions bag [3]
216               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 lit r
217               (parallel_demod table vl clause_ctx)
218           in
219             if id = id2 then None
220             else
221               Some ((bag,id2,lit),jump_to_right)
222     ;;
223
224     let rec demodulate bag (id,nlit,plit,vl,proof) table =
225       let rec demod_lit ~jump_to_right bag id lit clause_ctx =
226          match demodulate_once ~jump_to_right bag id lit vl table clause_ctx with
227          | None -> bag, id, lit
228          | Some ((bag, id, lit),jump) ->
229              demod_lit ~jump_to_right:jump bag id lit clause_ctx
230       in
231       (*let cmp_bag,cmp_cl = match nlit,plit with
232       |[],[lit,_] ->
233         let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> nlit, [l,true])
234         in
235         let cl,_,_ = Terms.get_from_bag id bag in
236         bag,cl
237       |[lit,_],[] ->
238         let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> [l,true],[])
239         in
240         let cl,_,_ = Terms.get_from_bag id bag in
241         bag,cl
242       |_ -> assert false
243  in*)
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       let (bag, maxvar), res =
291        HExtlib.filter_map_acc 
292          (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
293            match build_clause ~fresh:true bag maxvar filter 
294                   rule t subst id id2 pos dir clause_ctx with
295             | None -> None
296             | Some (bag,maxvar,res,_) -> Some ((bag,maxvar),res))
297          (bag, maxvar) res
298       in
299        bag, maxvar, res
300     ;;
301
302     (* Tries to rewrite an equality to identity, using unit equalities in table *)    
303     let rewrite_eq ~unify l r ty vl table =
304       let retrieve = if unify then IDX.DT.retrieve_unifiables
305       else IDX.DT.retrieve_generalizations in
306       let lcands = retrieve table l in
307       let rcands = retrieve table r in
308       let f b c = 
309         let id, dir, l, r, vl = 
310           match c with
311             | (d,_,_, (id,[],[Terms.Equation (l,r,ty,_),_],vl,_))-> id, d, l, r, vl
312             | (d,_,_, (id,[Terms.Equation (l,r,ty,_),_],[],vl,_))-> id, d, l, r, vl
313             |_ -> assert false 
314         in 
315         let reverse = (dir = Terms.Left2Right) = b in
316         let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
317         else r,l, Terms.Right2Left in
318           (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
319       in
320       let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
321       let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
322       let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
323       let locked_vars = if unify then [] else vl in
324       let rec aux = function
325         | [] -> None
326         | (id2,dir,c,vl1)::tl ->
327             try
328               let subst = Unif.unification (* (vl@vl1) *) locked_vars c t in
329               Some (id2, dir, subst)
330             with FoUnif.UnificationFailure _ -> aux tl
331       in
332         aux (cands1 @ cands2)
333     ;;
334
335     let is_subsumed ~unify bag maxvar (id, nlit, plit, vl, _) table =
336       match nlit,plit with
337       | [],[Terms.Equation (l,r,ty,_) ,_]-> 
338           (match rewrite_eq ~unify l r ty vl table with
339             | None -> None
340             | Some (id2, dir, subst) ->
341                 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
342                   build_clause ~fresh:true bag maxvar (fun _ -> true)
343                   Terms.Superposition id_t subst id id2 [2] dir (fun l -> [],[l,true]))
344      | _ -> None (* TODO : implement subsumption for clauses *)
345     ;;
346     let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
347     let is_subsumed ~unify bag maxvar c x =
348       prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
349     ;;
350     (* id refers to a clause proving contextl l = contextr r *)
351
352     let rec deep_eq ~unify l r ty pos contextl contextr table acc =
353       match acc with 
354       | None -> None
355       | Some(bag,maxvar,(id,nlit,plit,vl,p),subst) -> 
356           let l = Subst.apply_subst subst l in 
357           let r = Subst.apply_subst subst r in 
358             try 
359               let subst1 = Unif.unification (* vl *) [] l r in
360               let lit = 
361                 match nlit,plit with
362                   | [Terms.Equation (l,r,ty,o),_],[] -> 
363                      Terms.Equation (FoSubst.apply_subst subst1 l,
364                        FoSubst.apply_subst subst1 r, ty, o)
365                   | _ -> assert false
366               in
367                 Some(bag,maxvar,(id,[],[lit,true],vl,p),Subst.concat subst1 subst)
368             with FoUnif.UnificationFailure _ -> 
369               match rewrite_eq ~unify l r ty vl table with
370               | Some (id2, dir, subst1) ->
371                   let newsubst = Subst.concat subst1 subst in
372                   let id_t = 
373                     FoSubst.apply_subst newsubst
374                       (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r]) 
375                   in
376                     (match 
377                       build_clause ~fresh:true bag maxvar (fun _ -> true)
378                         Terms.Superposition id_t 
379                         subst1 id id2 (pos@[2]) dir (fun l -> [],[l,true])
380                     with
381                     | Some (bag, maxvar, c, _) -> 
382                         Some(bag,maxvar,c,newsubst)
383                     | None -> assert false)
384               | None ->
385                   match l,r with 
386                   | Terms.Node (a::la), Terms.Node (b::lb) when 
387                       a = b && List.length la = List.length lb ->
388                       let acc,_,_,_ =
389                         List.fold_left2 
390                           (fun (acc,pre,postl,postr) a b -> 
391                              let newcl = 
392                               fun x -> contextl(Terms.Node (pre@(x::postl))) in
393                              let newcr = 
394                               fun x -> contextr(Terms.Node (pre@(x::postr))) in
395                              let newpos = List.length pre::pos in
396                              let footail l =
397                                if l = [] then [] else List.tl l in
398                                (deep_eq ~unify a b ty 
399                                  newpos newcl newcr table acc,pre@[b],
400                                  footail postl, footail postr))
401                           (acc,[a],List.tl la,List.tl lb) la lb
402                       in acc
403                   | _,_ -> None
404     ;;
405     let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
406     let deep_eq ~unify l r ty pos contextl contextr table x =
407       prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
408     ;;
409
410     let rec orphan_murder bag acc i =
411       match Terms.get_from_bag i bag with
412         | (_,_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
413         | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
414         | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
415             if (List.mem i acc) then (false,acc)
416             else match orphan_murder bag acc i1 with
417               | (true,acc) -> (true,acc)
418               | (false,acc) ->
419                   let (res,acc) = orphan_murder bag acc i2 in
420                   if res then res,acc else res,i::acc
421     ;;
422
423     let orphan_murder bag actives cl =
424       let (id,_,_,_,_) = cl in
425       let actives = List.map (fun (i,_,_,_,_) -> i) actives in
426       let (res,_) = orphan_murder bag actives id in
427         if res then debug (lazy "Orphan murdered"); res
428     ;;
429     let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
430     let orphan_murder bag actives x =
431       prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
432     ;;
433
434     (* demodulate and check for subsumption *)
435     let simplify table maxvar bag clause = 
436       if is_identity_clause ~unify:false clause then bag,None
437       (* else if orphan_murder bag actives clause then bag,None *)
438       else let bag, clause = demodulate bag clause table in
439       if is_identity_clause ~unify:false clause then bag,None
440       else
441         match is_subsumed ~unify:false bag maxvar clause table with
442           | None -> bag, Some clause
443           | Some _ -> bag, None
444     ;;
445
446     let simplify table maxvar bag clause =
447       match simplify table maxvar bag clause with
448         | bag, None ->
449             let (id,_,_,_,_) = clause in
450             let (_,_,iter) = Terms.get_from_bag id bag in
451             Terms.replace_in_bag (clause,true,iter) bag, None
452         | bag, Some clause -> bag, Some clause
453     (*let (id,_,_,_) = clause in
454             if orphan_murder bag clause then
455               Terms.M.add id (clause,true) bag, Some clause
456             else bag, Some clause*)
457     ;;
458     let prof_simplify = HExtlib.profile ~enable "simplify";;
459     let simplify table maxvar bag x =
460       prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
461     ;;
462
463     let one_pass_simplification new_clause (alist,atable) bag maxvar =
464       match simplify atable maxvar bag new_clause with
465         | bag,None -> bag,None (* new_clause has been discarded *)
466         | bag,(Some clause) ->
467             let ctable = IDX.index_clause IDX.DT.empty clause in
468             let bag, alist, atable = 
469               List.fold_left 
470                 (fun (bag, alist, atable) c ->
471                    match simplify ctable maxvar bag c with
472                      |bag,None -> (bag,alist,atable)
473                         (* an active clause as been discarded *)
474                      |bag,Some c1 ->
475                         bag, c :: alist, IDX.index_clause atable c)
476                 (bag,[],IDX.DT.empty) alist
477             in
478               bag, Some (clause, (alist,atable))
479     ;;
480     let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
481     let one_pass_simplification new_clause t bag x =
482       prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
483     ;;
484
485     let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
486       let atable1 =
487         if new_cl then atable else
488         IDX.index_clause atable cl
489       in
490         (* Simplification of new_clause with :      *
491          * - actives and cl if new_clause is not cl *
492          * - only actives otherwise                 *)
493         match
494           simplify atable1 maxvar bag new_clause with
495           | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
496           | bag,Some clause ->
497               (* Simplification of each active clause with clause *
498                * which is the simplified form of new_clause       *)
499               let ctable = IDX.index_clause IDX.DT.empty clause in
500               let bag, newa, alist, atable = 
501                 List.fold_left 
502                   (fun (bag, newa, alist, atable) c ->
503                      match simplify ctable maxvar bag c with
504                        |bag,None -> (bag, newa, alist, atable)
505                           (* an active clause as been discarded *)
506                        |bag,Some c1 ->
507                             if (c1 == c) then 
508                               bag, newa, c :: alist,
509                             IDX.index_clause atable c
510                             else
511                               bag, c1 :: newa, alist, atable)                  
512                   (bag,[],[],IDX.DT.empty) alist
513               in
514                 if new_cl then
515                   bag, (Some cl, Some (clause, (alist,atable), newa))
516                 else
517                   (* if new_clause is not cl, we simplify cl with clause *)
518                   match simplify ctable maxvar bag cl with
519                     | bag,None ->
520                         (* cl has been discarded *)
521                         bag,(None, Some (clause, (alist,atable), newa))
522                     | bag,Some cl1 ->
523                         bag,(Some cl1, Some (clause, (alist,atable), newa))
524     ;;
525     let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
526     let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
527       prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
528     ;;
529
530     let keep_simplified cl (alist,atable) bag maxvar =
531       let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
532         if new_cl then
533           match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
534             | _,(None, _) -> assert false
535             | bag,(Some _, None) -> bag,None
536             | bag,(Some _, Some (clause, (alist,atable), newa)) ->
537                 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
538                   bag (newa@newc)
539         else
540           match newc with
541             | [] -> bag, Some (cl, (alist,atable))
542             | hd::tl ->
543                 match simplification_step ~new_cl cl
544                   (alist,atable) bag maxvar hd with
545                   | _,(None,None) -> assert false
546                   | bag,(Some _,None) ->
547                       keep_simplified_aux ~new_cl cl (alist,atable) bag tl
548                   | bag,(None, Some _) -> bag,None
549                   | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
550                       let alist,atable =
551                         (clause::alist, IDX.index_clause atable clause)
552                       in
553                         keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
554                           bag (newa@tl)
555       in
556         keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
557     ;;
558     let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
559     let keep_simplified cl t bag x =
560       prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
561     ;;
562
563     (* this is like simplify but raises Success *)
564     let simplify_goal ~no_demod maxvar table bag g_actives clause = 
565       let bag, clause = 
566         if no_demod then bag, clause else demodulate bag clause table 
567       in
568       if List.exists (Clauses.are_alpha_eq_cl clause) g_actives then None else
569       (debug (lazy (Pp.pp_clause clause));
570       if (is_goal_trivial clause)
571       then raise (Success (bag, maxvar, clause))
572       else   
573         let (id,nlit,plit,vl,_) = clause in 
574         if vl = [] then Some (bag,clause)
575         else
576          let l,r,ty = 
577            match nlit,plit with
578              | [Terms.Equation(l,r,ty,_),_],[] -> l,r,ty
579              | _ -> assert false 
580          in
581          match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x) 
582            table (Some(bag,maxvar,clause,Subst.id_subst)) with
583          | None -> Some (bag,clause)
584          | Some (bag,maxvar,cl,subst) -> 
585              prerr_endline "Goal subsumed";
586              raise (Success (bag,maxvar,cl)))
587 (*
588       else match is_subsumed ~unify:true bag maxvar clause table with
589         | None -> Some (bag, clause)
590         | Some ((bag,maxvar),c) -> 
591             prerr_endline "Goal subsumed";
592             raise (Success (bag,maxvar,c))
593 *) 
594     ;;
595
596     let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
597     let  simplify_goal ~no_demod maxvar table bag g_actives x =
598       prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
599     ;;
600
601     (* =================== inference ===================== *)
602
603     (* this is OK for both the sup_left and sup_right inference steps *)
604     let superposition table varlist is_pos subterm pos context =
605       let cands = IDX.DT.retrieve_unifiables table subterm in
606       HExtlib.filter_map
607         (fun (dir, is_cand_pos, _, (id,nlit,plit,vl,_ (*as uc*))) ->
608            match nlit,plit with
609            | [],[Terms.Equation (l,r,_,o),_] ->
610                (let side, newside = if dir=Terms.Left2Right then l,r else r,l in
611                try 
612                  let subst = 
613                    Unif.unification (* (varlist@vl)*)  [] subterm side 
614                  in 
615                  if o = Terms.Incomparable || o = Terms.Invertible then
616                    let side = Subst.apply_subst subst side in
617                    let newside = Subst.apply_subst subst newside in
618                    let o = Order.compare_terms side newside in
619                    (* XXX: check Riazanov p. 33 (iii) *)
620                    if o <> Terms.Lt && o <> Terms.Eq then  
621                      Some (context newside, subst, id, pos, dir)
622                    else 
623                      ((*prerr_endline ("Filtering: " ^ 
624                         Pp.pp_foterm side ^ " =(< || =)" ^ 
625                         Pp.pp_foterm newside);*)None)
626                  else
627                    Some (context newside, subst, id, pos, dir)
628                with FoUnif.UnificationFailure _ -> None)
629             | _ -> assert false)
630         (IDX.ClauseSet.elements cands)
631     ;;
632
633     (* Superposes selected equation with equalities in table *)
634     let superposition_with_table bag maxvar id vl lit is_pos clause_ctx table =
635       match lit with 
636       | Terms.Equation (l,r,ty,Terms.Lt) ->
637           fold_build_new_clause bag maxvar id Terms.Superposition
638             (fun _ -> true)
639             (all_positions [3] 
640               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
641               r (superposition table vl is_pos)) clause_ctx
642       | Terms.Equation (l,r,ty,Terms.Invertible)
643       | Terms.Equation (l,r,ty,Terms.Gt) ->
644           fold_build_new_clause bag maxvar id Terms.Superposition
645             (fun _ -> true)
646             (all_positions [2] 
647               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
648               l (superposition table vl is_pos)) clause_ctx
649       | Terms.Equation (l,r,ty,Terms.Incomparable) ->
650           let filtering avoid subst = (* Riazanov: p.33 condition (iv) *)
651             let l = Subst.apply_subst subst l in
652             let r = Subst.apply_subst subst r in
653             let o = Order.compare_terms l r in
654             o <> avoid && o <> Terms.Eq
655           in
656           let bag, maxvar,r_terms =
657             fold_build_new_clause bag maxvar id Terms.Superposition
658               (filtering Terms.Gt)
659               (all_positions [3] 
660                  (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
661                  r (superposition table vl is_pos)) clause_ctx
662           in
663           let bag, maxvar, l_terms =
664             fold_build_new_clause bag maxvar id Terms.Superposition
665               (filtering Terms.Lt)
666               (all_positions [2] 
667                  (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
668                  l (superposition table vl is_pos)) clause_ctx
669           in
670             bag, maxvar, r_terms @ l_terms
671       | _ -> assert false
672     ;;
673
674     let superpose_literal id vl table is_pos (bag,maxvar,pre,post,acc) (lit,sel) =
675       let clause_ctx =
676          if is_pos then fun l -> [],pre@[l,true]@post
677          else fun l -> pre@[l,true]@post,[]
678       in
679       let bag, maxvar, newc =
680         superposition_with_table bag maxvar id vl lit is_pos clause_ctx table
681       in
682          if post = [] then bag,maxvar,pre@[lit,sel],[],newc@acc
683          else bag,maxvar,pre@[lit,sel],List.tl post,newc@acc
684     ;;
685
686
687     (* the current equation is normal w.r.t. demodulation with atable
688      * (and is not the identity) *)
689     let infer_right bag maxvar current (alist,atable) = 
690       (* We demodulate actives clause with current until all *
691        * active clauses are reduced w.r.t each other         *)
692       (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
693       let ctable = IDX.index_clause IDX.DT.empty current in
694       (* let bag, (alist, atable) = 
695         let bag, alist = 
696           HExtlib.filter_map_acc (simplify ctable) bag alist
697         in
698         bag, (alist, List.fold_left IDX.index_clause IDX.DT.empty alist)
699       in*)
700         debug (lazy "Simplified active clauses with fact");
701       (* We superpose active clauses with current *)
702       let bag, maxvar, new_clauses =
703         List.fold_left 
704           (fun (bag, maxvar, acc) (id,nlit,plit,vl,_) ->
705              let bag, maxvar, _, _, acc =
706               if nlit = [] then bag,maxvar,[],[],acc
707               else List.fold_left
708                  (superpose_literal id vl ctable false) (bag,maxvar,[],List.tl nlit,acc) nlit
709              in
710               let bag, maxvar, _, _, acc =
711                if plit = [] then bag,maxvar,[],[],acc
712                else List.fold_left
713                  (superpose_literal id vl ctable true) (bag,maxvar,[],List.tl plit,acc) plit
714              in            
715              bag, maxvar, acc)
716           (bag, maxvar, []) alist
717       in
718         debug (lazy "First superpositions");
719         (* We add current to active clauses so that it can be *
720          * superposed with itself                             *)
721       let alist, atable = 
722         current :: alist, IDX.index_clause atable current
723       in
724         debug (lazy "Indexed");
725       let fresh_current, maxvar = Clauses.fresh_clause maxvar current in
726         (* We need to put fresh_current into the bag so that all *
727          * variables clauses refer to are known.                 *)
728       let bag, (id,nlit,plit,vl,_) = Terms.add_to_bag fresh_current bag in
729         (* We superpose current with active clauses *)
730       let bag, maxvar, _, _, new_clauses =
731          if nlit = [] then bag,maxvar,[],[],new_clauses
732          else List.fold_left
733          (superpose_literal id vl atable false) (bag,maxvar,[],List.tl nlit,new_clauses) nlit
734       in
735       let bag, maxvar, _, _, new_clauses =
736          if plit = [] then bag,maxvar,[],[],new_clauses
737          else List.fold_left
738           (superpose_literal id vl atable true) (bag,maxvar,[],List.tl plit,new_clauses) plit
739       in 
740         debug (lazy "Another superposition");
741         debug (lazy (Printf.sprintf "Demodulating %d clauses"
742                  (List.length new_clauses)));
743       let bag, new_clauses = 
744         HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
745       in
746         debug (lazy "Demodulated new clauses");
747       bag, maxvar, (alist, atable), new_clauses
748     ;;
749
750     let prof_ir = HExtlib.profile ~enable "infer_right";;
751     let infer_right bag maxvar current t = 
752       prof_ir.HExtlib.profile (infer_right bag maxvar current) t
753     ;;
754
755     let infer_left bag maxvar goal (_alist, atable) =
756         (* We superpose the goal with active clauses *)
757      if (match goal with (_,_,_,[],_) -> true | _ -> false) then bag, maxvar, []
758      else
759       let (id,nlit,vl) =
760          match goal with
761            | (id,nlit,[],vl,_) -> id,nlit,vl
762            | _ -> assert false
763       in
764       let bag, maxvar, _, _, new_goals = 
765         List.fold_left (superpose_literal id vl atable false)
766             (bag,maxvar,[],List.tl nlit,[]) nlit
767       in
768         debug (lazy "Superposed goal with active clauses");
769         (* We simplify the new goals with active clauses *)
770       let bag, new_goals = 
771         List.fold_left
772          (fun (bag, acc) g -> 
773             match simplify_goal ~no_demod:false maxvar atable bag [] g with
774               | None -> assert false
775               | Some (bag,g) -> bag,g::acc)
776          (bag, []) new_goals
777       in
778         debug (lazy "Simplified new goals with active clauses");
779       bag, maxvar, List.rev new_goals
780     ;;
781
782     let prof_il = HExtlib.profile ~enable "infer_left";;
783     let infer_left bag maxvar goal t = 
784       prof_il.HExtlib.profile (infer_left bag maxvar goal) t
785     ;;
786
787   end