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