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