]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/superposition.ml
53d2958d1c2d1788fc2ff36ac843a9d2b864aafe
[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 ~jump_to_right 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   
283     let demodulate_once_old ~jump_to_right bag (id, literal, vl, pr) table =
284       match literal with
285       | Terms.Predicate t -> assert false
286       | Terms.Equation (l,r,ty,_) ->
287         let left_position = if jump_to_right then None else
288           first_position [2]
289             (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) l
290             (demod table vl)
291         in
292         match left_position with
293           | Some (newt, subst, id2, dir, pos) ->
294               begin
295                 match build_clause bag (fun _ -> true) Terms.Demodulation 
296                   newt subst id id2 pos dir
297                 with
298                   | None -> assert false
299                   | Some x -> Some (x,false)
300               end
301           | None ->
302               match first_position
303                 [3] (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) r
304                 (demod table vl)
305               with
306                 | None -> None
307                 | Some (newt, subst, id2, dir, pos) ->
308                     match build_clause bag (fun _ -> true)
309                       Terms.Demodulation newt subst id id2 pos dir
310                     with
311                         | None -> assert false
312                         | Some x -> Some (x,true)
313     ;;
314
315     let parallel_demod table vl bag t pos ctx id =
316       match demod table vl t with
317         | None -> (bag,t,id)
318         | Some (newside, subst, id2, dir) ->
319             match build_clause bag (fun _ -> true)
320               Terms.Demodulation (ctx newside) subst id id2 pos dir
321             with
322               | None -> assert false
323               | Some (bag,(id,_,_,_)) ->
324                     (bag,newside,id)
325     ;;
326
327     let demodulate_once ~jump_to_right bag (id, literal, vl, pr) table =
328       match literal with
329       | Terms.Predicate t -> assert false
330       | Terms.Equation (l,r,ty,_) ->
331           let bag,l,id1 = if jump_to_right then (bag,l,id) else
332             parallel_positions bag [2]
333               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id l
334               (parallel_demod table vl)
335           in
336           let jump_to_right = id1 = id in
337           let bag,r,id2 =
338             parallel_positions bag [3]
339               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 r
340               (parallel_demod table vl)
341           in
342             if id = id2 then None
343             else
344               let cl,_,_ = Terms.get_from_bag id2 bag in
345                 Some ((bag,cl),jump_to_right)
346     ;;
347
348     let rec demodulate_old ~jump_to_right bag clause table =
349       match demodulate_once ~jump_to_right bag clause table with
350       | None -> bag, clause
351       | Some ((bag, clause),r) -> demodulate ~jump_to_right:r
352           bag clause table
353     ;;
354 (*
355     let rec demodulate_old ~jump_to_right bag clause table =
356       match demodulate_once_old ~jump_to_right bag clause table with
357         | None -> bag, clause
358         | Some ((bag, clause),r) -> demodulate_old ~jump_to_right:r
359           bag clause table
360     ;;
361 *)
362
363     let are_alpha_eq cl1 cl2 =
364       let get_term (_,lit,_,_) =
365         match lit with
366           | Terms.Predicate _ -> assert false
367           | Terms.Equation (l,r,ty,_) ->
368               Terms.Node [Terms.Leaf B.eqP; ty; l ; r]
369       in
370         try ignore(Unif.alpha_eq (get_term cl1) (get_term cl2)) ; true
371         with FoUnif.UnificationFailure _ -> false
372     ;;
373
374     let demodulate bag clause table =
375       demodulate ~jump_to_right:false bag clause table
376 ;;  
377 (*
378     let (bag1,c1), (bag2,c2) =
379         demodulate ~jump_to_right:false bag clause table,
380         demodulate_old ~jump_to_right:false bag clause table 
381       in
382         if are_alpha_eq c1 c2 then bag1,c1
383         else 
384           begin
385           prerr_endline (Pp.pp_unit_clause clause);
386           prerr_endline (Pp.pp_unit_clause c1);
387           prerr_endline (Pp.pp_unit_clause c2);
388           prerr_endline "Bag1 :";
389           prerr_endline (Pp.pp_bag bag1);
390           prerr_endline "Bag2 :";
391           prerr_endline (Pp.pp_bag bag2);
392           assert false
393           end
394     ;; *)
395
396     let prof_demodulate = HExtlib.profile ~enable "demodulate";;
397     let demodulate bag clause x =
398       prof_demodulate.HExtlib.profile (demodulate bag clause) x
399     ;;
400
401     (* move away *)
402     let is_identity_clause ~unify = function
403       | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
404       | _, Terms.Equation (l,r,_,_), vl, proof when unify ->
405           (try ignore(Unif.unification (* vl *) [] l r); true
406           with FoUnif.UnificationFailure _ -> false)
407       | _, Terms.Equation (_,_,_,_), _, _ -> false
408       | _, Terms.Predicate _, _, _ -> assert false          
409     ;;
410
411     let build_new_clause bag maxvar filter rule t subst id id2 pos dir =
412       let maxvar, _vl, subst = Utils.relocate maxvar (Terms.vars_of_term
413       (Subst.apply_subst subst t)) subst in
414       match build_clause bag filter rule t subst id id2 pos dir with
415       | Some (bag, c) -> Some ((bag, maxvar), c)
416       | None -> None
417     ;;
418     let prof_build_new_clause = HExtlib.profile ~enable "build_new_clause";;
419     let build_new_clause bag maxvar filter rule t subst id id2 pos x =
420       prof_build_new_clause.HExtlib.profile (build_new_clause bag maxvar filter
421       rule t subst id id2 pos) x
422     ;;
423
424     let fold_build_new_clause bag maxvar id rule filter res =
425       let (bag, maxvar), res =
426        HExtlib.filter_map_acc 
427          (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
428             build_new_clause bag maxvar filter rule t subst id id2 pos dir)
429          (bag, maxvar) res
430       in
431        bag, maxvar, res
432     ;;
433
434     
435     let rewrite_eq ~unify l r ty vl table =
436       let retrieve = if unify then IDX.DT.retrieve_unifiables
437       else IDX.DT.retrieve_generalizations in
438       let lcands = retrieve table l in
439       let rcands = retrieve table r in
440       let f b c = 
441         let id, dir, l, r, vl = 
442           match c with
443             | (d, (id,Terms.Equation (l,r,ty,_),vl,_))-> id, d, l, r, vl
444             |_ -> assert false 
445         in 
446         let reverse = (dir = Terms.Left2Right) = b in
447         let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
448         else r,l, Terms.Right2Left in
449           (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
450       in
451       let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
452       let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
453       let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
454       let locked_vars = if unify then [] else vl in
455       let rec aux = function
456         | [] -> None
457         | (id2,dir,c,vl1)::tl ->
458             try
459               let subst = Unif.unification (* (vl@vl1) *) locked_vars c t in
460               Some (id2, dir, subst)
461             with FoUnif.UnificationFailure _ -> aux tl
462       in
463         aux (cands1 @ cands2)
464     ;;
465
466     let is_subsumed ~unify bag maxvar (id, lit, vl, _) table =
467       match lit with
468       | Terms.Predicate _ -> assert false
469       | Terms.Equation (l,r,ty,_) -> 
470           match rewrite_eq ~unify l r ty vl table with
471             | None -> None
472             | Some (id2, dir, subst) ->
473                 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
474                   build_new_clause bag maxvar (fun _ -> true)
475                   Terms.Superposition id_t subst id id2 [2] dir 
476     ;;
477     let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
478     let is_subsumed ~unify bag maxvar c x =
479       prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
480     ;;
481     (* id refers to a clause proving contextl l = contextr r *)
482
483     let rec deep_eq ~unify l r ty pos contextl contextr table acc =
484       match acc with 
485       | None -> None
486       | Some(bag,maxvar,(id,lit,vl,p),subst) -> 
487           let l = Subst.apply_subst subst l in 
488           let r = Subst.apply_subst subst r in 
489             try 
490               let subst1 = Unif.unification (* vl *) [] l r in
491               let lit = 
492                 match lit with Terms.Predicate _ -> assert false
493                   | Terms.Equation (l,r,ty,o) -> 
494                      Terms.Equation (FoSubst.apply_subst subst1 l,
495                        FoSubst.apply_subst subst1 r, ty, o)
496               in
497                 Some(bag,maxvar,(id,lit,vl,p),Subst.concat subst1 subst)
498             with FoUnif.UnificationFailure _ -> 
499               match rewrite_eq ~unify l r ty vl table with
500               | Some (id2, dir, subst1) ->
501                   let newsubst = Subst.concat subst1 subst in
502                   let id_t = 
503                     FoSubst.apply_subst newsubst
504                       (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r]) 
505                   in
506                     (match 
507                       build_new_clause bag maxvar (fun _ -> true)
508                         Terms.Superposition id_t 
509                         subst1 id id2 (pos@[2]) dir 
510                     with
511                     | Some ((bag, maxvar), c) -> 
512                         Some(bag,maxvar,c,newsubst)
513                     | None -> assert false)
514               | None ->
515                   match l,r with 
516                   | Terms.Node (a::la), Terms.Node (b::lb) when 
517                       a = b && List.length la = List.length lb ->
518                       let acc,_,_,_ =
519                         List.fold_left2 
520                           (fun (acc,pre,postl,postr) a b -> 
521                              let newcl = 
522                               fun x -> contextl(Terms.Node (pre@(x::postl))) in
523                              let newcr = 
524                               fun x -> contextr(Terms.Node (pre@(x::postr))) in
525                              let newpos = List.length pre::pos in
526                              let footail l =
527                                if l = [] then [] else List.tl l in
528                                (deep_eq ~unify a b ty 
529                                  newpos newcl newcr table acc,pre@[b],
530                                  footail postl, footail postr))
531                           (acc,[a],List.tl la,List.tl lb) la lb
532                       in acc
533                   | _,_ -> None
534     ;;
535     let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
536     let deep_eq ~unify l r ty pos contextl contextr table x =
537       prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
538     ;;
539
540     let rec orphan_murder bag acc i =
541       match Terms.get_from_bag i bag with
542         | (_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
543         | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
544         | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
545             if (List.mem i acc) then (false,acc)
546             else match orphan_murder bag acc i1 with
547               | (true,acc) -> (true,acc)
548               | (false,acc) ->
549                   let (res,acc) = orphan_murder bag acc i2 in
550                   if res then res,acc else res,i::acc
551     ;;
552
553     let orphan_murder bag actives cl =
554       let (id,_,_,_) = cl in
555       let actives = List.map (fun (i,_,_,_) -> i) actives in
556       let (res,_) = orphan_murder bag actives id in
557         if res then debug "Orphan murdered"; res
558     ;;
559     let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
560     let orphan_murder bag actives x =
561       prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
562     ;;
563
564     (* demodulate and check for subsumption *)
565     let simplify table maxvar bag clause = 
566       if is_identity_clause ~unify:false clause then bag,None
567       (* else if orphan_murder bag actives clause then bag,None *)
568       else let bag, clause = demodulate bag clause table in
569       if is_identity_clause ~unify:false clause then bag,None
570       else
571         match is_subsumed ~unify:false bag maxvar clause table with
572           | None -> bag, Some clause
573           | Some _ -> bag, None
574     ;;
575
576     let simplify table maxvar bag clause =
577       match simplify table maxvar bag clause with
578         | bag, None ->
579             let (id,_,_,_) = clause in
580             let (_,_,iter) = Terms.get_from_bag id bag in
581             Terms.replace_in_bag (clause,true,iter) bag, None
582         | bag, Some clause -> bag, Some clause
583     (*let (id,_,_,_) = clause in
584             if orphan_murder bag clause then
585               Terms.M.add id (clause,true) bag, Some clause
586             else bag, Some clause*)
587     ;;
588     let prof_simplify = HExtlib.profile ~enable "simplify";;
589     let simplify table maxvar bag x =
590       prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
591     ;;
592
593     let one_pass_simplification new_clause (alist,atable) bag maxvar =
594       match simplify atable maxvar bag new_clause with
595         | bag,None -> bag,None (* new_clause has been discarded *)
596         | bag,(Some clause) ->
597             let ctable = IDX.index_unit_clause maxvar IDX.DT.empty clause in
598             let bag, alist, atable = 
599               List.fold_left 
600                 (fun (bag, alist, atable) c ->
601                    match simplify ctable maxvar bag c with
602                      |bag,None -> (bag,alist,atable)
603                         (* an active clause as been discarded *)
604                      |bag,Some c1 ->
605                         bag, c :: alist, IDX.index_unit_clause maxvar atable c)
606                 (bag,[],IDX.DT.empty) alist
607             in
608               bag, Some (clause, (alist,atable))
609     ;;
610     let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
611     let one_pass_simplification new_clause t bag x =
612       prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
613     ;;
614
615     let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
616       let atable1 =
617         if new_cl then atable else
618         IDX.index_unit_clause maxvar atable cl
619       in
620         (* Simplification of new_clause with :      *
621          * - actives and cl if new_clause is not cl *
622          * - only actives otherwise                 *)
623         match
624           simplify atable1 maxvar bag new_clause with
625           | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
626           | bag,Some clause ->
627               (* Simplification of each active clause with clause *
628                * which is the simplified form of new_clause       *)
629               let ctable = IDX.index_unit_clause maxvar IDX.DT.empty clause in
630               let bag, newa, alist, atable = 
631                 List.fold_left 
632                   (fun (bag, newa, alist, atable) c ->
633                      match simplify ctable maxvar bag c with
634                        |bag,None -> (bag, newa, alist, atable)
635                           (* an active clause as been discarded *)
636                        |bag,Some c1 ->
637                             if (c1 == c) then 
638                               bag, newa, c :: alist,
639                             IDX.index_unit_clause maxvar atable c
640                             else
641                               bag, c1 :: newa, alist, atable)                  
642                   (bag,[],[],IDX.DT.empty) alist
643               in
644                 if new_cl then
645                   bag, (Some cl, Some (clause, (alist,atable), newa))
646                 else
647                   (* if new_clause is not cl, we simplify cl with clause *)
648                   match simplify ctable maxvar bag cl with
649                     | bag,None ->
650                         (* cl has been discarded *)
651                         bag,(None, Some (clause, (alist,atable), newa))
652                     | bag,Some cl1 ->
653                         bag,(Some cl1, Some (clause, (alist,atable), newa))
654     ;;
655     let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
656     let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
657       prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
658     ;;
659
660     let keep_simplified cl (alist,atable) bag maxvar =
661       let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
662         if new_cl then
663           match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
664             | _,(None, _) -> assert false
665             | bag,(Some _, None) -> bag,None
666             | bag,(Some _, Some (clause, (alist,atable), newa)) ->
667                 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
668                   bag (newa@newc)
669         else
670           match newc with
671             | [] -> bag, Some (cl, (alist,atable))
672             | hd::tl ->
673                 match simplification_step ~new_cl cl
674                   (alist,atable) bag maxvar hd with
675                   | _,(None,None) -> assert false
676                   | bag,(Some _,None) ->
677                       keep_simplified_aux ~new_cl cl (alist,atable) bag tl
678                   | bag,(None, Some _) -> bag,None
679                   | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
680                       let alist,atable =
681                      (clause::alist, IDX.index_unit_clause maxvar atable clause)
682                       in
683                         keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
684                           bag (newa@tl)
685       in
686         keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
687     ;;
688     let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
689     let keep_simplified cl t bag x =
690       prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
691     ;;
692
693     (* this is like simplify but raises Success *)
694     let simplify_goal ~no_demod maxvar table bag g_actives clause = 
695       let bag, clause = 
696         if no_demod then bag, clause else demodulate bag clause table 
697       in
698       if List.exists (are_alpha_eq clause) g_actives then None else
699       if (is_identity_clause ~unify:true clause)
700       then raise (Success (bag, maxvar, clause))
701       else   
702         let (id,lit,vl,_) = clause in 
703         if vl = [] then Some (bag,clause)
704         else
705          let l,r,ty = 
706            match lit with
707              | Terms.Equation(l,r,ty,_) -> l,r,ty
708              | _ -> assert false 
709          in
710          match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x) 
711            table (Some(bag,maxvar,clause,Subst.id_subst)) with
712          | None -> Some (bag,clause)
713          | Some (bag,maxvar,cl,subst) -> 
714              prerr_endline "Goal subsumed";
715              raise (Success (bag,maxvar,cl))
716 (*
717       else match is_subsumed ~unify:true bag maxvar clause table with
718         | None -> Some (bag, clause)
719         | Some ((bag,maxvar),c) -> 
720             prerr_endline "Goal subsumed";
721             raise (Success (bag,maxvar,c))
722 *) 
723     ;;
724
725     let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
726     let  simplify_goal ~no_demod maxvar table bag g_actives x =
727       prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
728     ;;
729
730     (* =================== inference ===================== *)
731
732     (* this is OK for both the sup_left and sup_right inference steps *)
733     let superposition table varlist subterm pos context =
734       let cands = IDX.DT.retrieve_unifiables table subterm in
735       HExtlib.filter_map
736         (fun (dir, (id,lit,vl,_ (*as uc*))) ->
737            match lit with
738            | Terms.Predicate _ -> assert false
739            | Terms.Equation (l,r,_,o) ->
740                let side, newside = if dir=Terms.Left2Right then l,r else r,l in
741                try 
742                  let subst = 
743                    Unif.unification (* (varlist@vl)*)  [] subterm side 
744                  in 
745                  if o = Terms.Incomparable || o = Terms.Invertible then
746                    let side = Subst.apply_subst subst side in
747                    let newside = Subst.apply_subst subst newside in
748                    let o = Order.compare_terms side newside in
749                    (* XXX: check Riazanov p. 33 (iii) *)
750                    if o <> Terms.Lt && o <> Terms.Eq then  
751                      Some (context newside, subst, id, pos, dir)
752                    else 
753                      ((*prerr_endline ("Filtering: " ^ 
754                         Pp.pp_foterm side ^ " =(< || =)" ^ 
755                         Pp.pp_foterm newside);*)None)
756                  else
757                    Some (context newside, subst, id, pos, dir)
758                with FoUnif.UnificationFailure _ -> None)
759         (IDX.ClauseSet.elements cands)
760     ;;
761
762     (* Superposes selected equation with equalities in table *)
763     let superposition_with_table bag maxvar (id,selected,vl,_) table =
764       match selected with 
765       | Terms.Predicate _ -> assert false
766       | Terms.Equation (l,r,ty,Terms.Lt) ->
767           fold_build_new_clause bag maxvar id Terms.Superposition
768             (fun _ -> true)
769             (all_positions [3] 
770               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
771               r (superposition table vl))
772       | Terms.Equation (l,r,ty,Terms.Invertible)
773       | Terms.Equation (l,r,ty,Terms.Gt) ->
774           fold_build_new_clause bag maxvar id Terms.Superposition
775             (fun _ -> true)
776             (all_positions [2] 
777               (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
778               l (superposition table vl))
779       | Terms.Equation (l,r,ty,Terms.Incomparable) ->
780           let filtering avoid subst = (* Riazanov: p.33 condition (iv) *)
781             let l = Subst.apply_subst subst l in
782             let r = Subst.apply_subst subst r in
783             let o = Order.compare_terms l r in
784             o <> avoid && o <> Terms.Eq
785           in
786           let bag, maxvar,r_terms =
787             fold_build_new_clause bag maxvar id Terms.Superposition
788               (filtering Terms.Gt)
789               (all_positions [3] 
790                  (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
791                  r (superposition table vl))
792           in
793           let bag, maxvar, l_terms =
794             fold_build_new_clause bag maxvar id Terms.Superposition
795               (filtering Terms.Lt)
796               (all_positions [2] 
797                  (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
798                  l (superposition table vl))
799           in
800             bag, maxvar, r_terms @ l_terms
801       | _ -> assert false
802     ;;
803
804     (* the current equation is normal w.r.t. demodulation with atable
805      * (and is not the identity) *)
806     let infer_right bag maxvar current (alist,atable) = 
807       (* We demodulate actives clause with current until all *
808        * active clauses are reduced w.r.t each other         *)
809       (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
810       let ctable = IDX.index_unit_clause maxvar IDX.DT.empty current in
811       (* let bag, (alist, atable) = 
812         let bag, alist = 
813           HExtlib.filter_map_acc (simplify ctable) bag alist
814         in
815         bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
816       in*)
817         debug "Simplified active clauses with fact";
818       (* We superpose active clauses with current *)
819       let bag, maxvar, new_clauses =
820         List.fold_left 
821           (fun (bag, maxvar, acc) active ->
822              let bag, maxvar, newc = 
823                superposition_with_table bag maxvar active ctable 
824              in
825              bag, maxvar, newc @ acc)
826           (bag, maxvar, []) alist
827       in
828         debug "First superpositions";
829         (* We add current to active clauses so that it can be *
830          * superposed with itself                             *)
831       let alist, atable = 
832         current :: alist, IDX.index_unit_clause maxvar atable current
833       in
834         debug "Indexed";
835       let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
836         (* We need to put fresh_current into the bag so that all *
837          * variables clauses refer to are known.                 *)
838       let bag, fresh_current = Terms.add_to_bag fresh_current bag in
839         (* We superpose current with active clauses *)
840       let bag, maxvar, additional_new_clauses =
841         superposition_with_table bag maxvar fresh_current atable 
842       in
843         debug "Another superposition";
844       let new_clauses = new_clauses @ additional_new_clauses in
845         debug (lazy (Printf.sprintf "Demodulating %d clauses"
846                  (List.length new_clauses)));
847       let bag, new_clauses = 
848         HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
849       in
850         debug "Demodulated new clauses";
851       bag, maxvar, (alist, atable), new_clauses
852     ;;
853
854     let prof_ir = HExtlib.profile ~enable "infer_right";;
855     let infer_right bag maxvar current t = 
856       prof_ir.HExtlib.profile (infer_right bag maxvar current) t
857     ;;
858
859     let infer_left bag maxvar goal (_alist, atable) =
860         (* We superpose the goal with active clauses *)
861      if (match goal with (_,_,[],_) -> true | _ -> false) then bag, maxvar, []
862      else
863       let bag, maxvar, new_goals =        
864         superposition_with_table bag maxvar goal atable 
865       in
866         debug "Superposed goal with active clauses";
867         (* We simplify the new goals with active clauses *)
868       let bag, new_goals = 
869         List.fold_left
870          (fun (bag, acc) g -> 
871             match simplify_goal ~no_demod:false maxvar atable bag [] g with
872               | None -> assert false
873               | Some (bag,g) -> bag,g::acc)
874          (bag, []) new_goals
875       in
876         debug "Simplified new goals with active clauses";
877       bag, maxvar, List.rev new_goals
878     ;;
879
880     let prof_il = HExtlib.profile ~enable "infer_left";;
881     let infer_left bag maxvar goal t = 
882       prof_il.HExtlib.profile (infer_left bag maxvar goal) t
883     ;;
884
885   end