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