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.
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_______________________________________________________________ *)
12 (* $Id: index.mli 9822 2009-06-03 15:37:06Z tassi $ *)
14 module Superposition (B : Orderings.Blob) =
16 module IDX = Index.Index(B)
17 module Unif = FoUnif.Founif(B)
18 module Subst = FoSubst
20 module Utils = FoUtils.Utils(B)
26 * B.t Terms.unit_clause
27 * B.t Terms.substitution
29 let print s = prerr_endline (Lazy.force s);;
31 (* let debug = print;; *)
34 let rec list_first f = function
36 | x::tl -> match f x with Some _ as x -> x | _ -> list_first f tl
39 let first_position pos ctx t f =
40 let inject_pos pos ctx = function
42 | Some (a,b,c,d) -> Some(ctx a,b,c,d,pos)
44 let rec aux pos ctx = function
45 | Terms.Leaf _ as t -> inject_pos pos ctx (f t)
49 | Some _ as x -> inject_pos pos ctx x
51 let rec first pre post = function
54 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
55 match aux (List.length pre :: pos) newctx t with
58 if post = [] then None (* tl is also empty *)
59 else first (pre @ [t]) (List.tl post) tl
61 first [] (List.tl l) l
66 let all_positions pos ctx t f =
67 let rec aux pos ctx = function
68 | Terms.Leaf _ as t -> f t pos ctx
73 (fun (acc,pre,post) t -> (* Invariant: pre @ [t] @ post = l *)
74 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
75 let acc = aux (List.length pre :: pos) newctx t @ acc in
76 if post = [] then acc, l, []
77 else acc, pre @ [t], List.tl post)
78 (f t pos ctx, [], List.tl l) l
85 let parallel_positions bag pos ctx id t f =
86 let rec aux bag pos ctx id = function
87 | Terms.Leaf _ as t -> f bag t pos ctx id
88 | Terms.Var _ as t -> bag,t,id
89 | Terms.Node (hd::l) as t->
90 let bag,t,id1 = f bag t pos ctx id in
94 (fun (bag,pre,post,id) t ->
95 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
96 let newpos = (List.length pre)::pos in
97 let bag,newt,id = aux bag newpos newctx id t in
98 if post = [] then bag, pre@[newt], [], id
99 else bag, pre @ [newt], List.tl post, id)
100 (bag, [hd], List.tl l, id) l
102 bag, Terms.Node l, id
104 (* else aux bag pos ctx id1 t *)
110 let visit bag pos ctx id t f =
111 let rec aux bag pos ctx id subst = function
112 | Terms.Leaf _ as t ->
113 let bag,subst,t,id = f bag t pos ctx id
114 in assert (subst=[]); bag,t,id
115 | Terms.Var i as t ->
116 let t= Subst.apply_subst subst t in
118 | Terms.Node (hd::l) ->
121 (fun (bag,pre,post,id) t ->
122 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
123 let newpos = (List.length pre)::pos in
124 let bag,newt,id = aux bag newpos newctx id subst t in
125 if post = [] then bag, pre@[newt], [], id
126 else bag, pre @ [newt], List.tl post, id)
127 (bag, [hd], List.map (Subst.apply_subst subst) (List.tl l), id) l
129 let bag,subst,t,id1 = f bag (Terms.Node l) pos ctx id
131 if id1 = id then (assert (subst=[]); bag,t,id)
132 else aux bag pos ctx id1 subst t
135 aux bag pos ctx id [] t
138 let build_clause bag filter rule t subst id id2 pos dir =
139 let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
140 let t = Subst.apply_subst subst t in
144 | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq (B.eqP()) eq ->
145 let o = Order.compare_terms l r in
146 Terms.Equation (l, r, ty, o)
147 | t -> Terms.Predicate t
150 Terms.add_to_bag (0, literal, Terms.vars_of_term t, proof) bag
154 ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
156 let prof_build_clause = HExtlib.profile ~enable "build_clause";;
157 let build_clause bag filter rule t subst id id2 pos x =
158 prof_build_clause.HExtlib.profile (build_clause bag filter rule t subst id id2 pos) x
162 (* ============ simplification ================= *)
163 let prof_demod_u = HExtlib.profile ~enable "demod.unify";;
164 let prof_demod_r = HExtlib.profile ~enable "demod.retrieve_generalizations";;
165 let prof_demod_o = HExtlib.profile ~enable "demod.compare_terms";;
166 let prof_demod_s = HExtlib.profile ~enable "demod.apply_subst";;
168 let demod table varlist subterm =
170 prof_demod_r.HExtlib.profile
171 (IDX.DT.retrieve_generalizations table) subterm
174 (fun (dir, (id,lit,vl,_)) ->
176 | Terms.Predicate _ -> assert false
177 | Terms.Equation (l,r,_,o) ->
178 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
181 prof_demod_u.HExtlib.profile
182 (Unif.unification (* (varlist@vl) *) varlist subterm) side
185 prof_demod_s.HExtlib.profile
186 (Subst.apply_subst subst) side
189 prof_demod_s.HExtlib.profile
190 (Subst.apply_subst subst) newside
192 if o = Terms.Incomparable || o = Terms.Invertible then
194 prof_demod_o.HExtlib.profile
195 (Order.compare_terms newside) side in
196 (* Riazanov, pp. 45 (ii) *)
198 Some (newside, subst, id, dir)
200 ((*prerr_endline ("Filtering: " ^
201 Pp.pp_foterm side ^ " =(< || =)" ^
202 Pp.pp_foterm newside ^ " coming from " ^
203 Pp.pp_unit_clause uc );*)None)
205 Some (newside, subst, id, dir)
206 with FoUnif.UnificationFailure _ -> None)
207 (IDX.ClauseSet.elements cands)
209 let prof_demod = HExtlib.profile ~enable "demod";;
210 let demod table varlist x =
211 prof_demod.HExtlib.profile (demod table varlist) x
214 let mydemod table varlist subterm =
216 prof_demod_r.HExtlib.profile
217 (IDX.DT.retrieve_generalizations table) subterm
220 (fun (dir, ((id,lit,vl,_) as c)) ->
221 debug (lazy("candidate: "
222 ^ Pp.pp_unit_clause c));
224 | Terms.Predicate _ -> assert false
225 | Terms.Equation (l,r,_,o) ->
226 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
229 prof_demod_u.HExtlib.profile
230 (Unif.unification (* (varlist@vl) *) varlist subterm) side
233 prof_demod_s.HExtlib.profile
234 (Subst.apply_subst subst) side
237 prof_demod_s.HExtlib.profile
238 (Subst.apply_subst subst) newside
240 if o = Terms.Incomparable || o = Terms.Invertible then
242 prof_demod_o.HExtlib.profile
243 (Order.compare_terms inewside) iside in
244 (* Riazanov, pp. 45 (ii) *)
246 Some (newside, subst, id, dir)
248 ((*prerr_endline ("Filtering: " ^
249 Pp.pp_foterm side ^ " =(< || =)" ^
250 Pp.pp_foterm newside ^ " coming from " ^
251 Pp.pp_unit_clause uc );*)
252 debug (lazy "not applied");None)
254 Some (newside, subst, id, dir)
255 with FoUnif.UnificationFailure _ ->
256 debug (lazy "not applied"); None)
257 (IDX.ClauseSet.elements cands)
260 let ctx_demod table vl bag t pos ctx id =
261 match mydemod table vl t with
262 | None -> (bag,[],t,id)
263 | Some (newside, subst, id2, dir) ->
264 let inewside = Subst.apply_subst subst newside in
265 match build_clause bag (fun _ -> true)
266 Terms.Demodulation (ctx inewside) subst id id2 pos dir
268 | None -> assert false
269 | Some (bag,(id,_,_,_)) ->
270 (bag,subst,newside,id)
273 let rec demodulate bag (id, literal, vl, pr) table =
274 debug (lazy ("demodulate " ^ (string_of_int id)));
276 | Terms.Predicate t -> (* assert false *)
278 visit bag [] (fun x -> x) id t (ctx_demod table vl)
280 let cl,_,_ = Terms.get_from_bag id1 bag in
282 | Terms.Equation (l,r,ty,_) ->
285 (fun x -> Terms.Node [ Terms.Leaf (B.eqP()); ty; x; r ]) id l
290 (fun x -> Terms.Node [ Terms.Leaf (B.eqP()); ty; l; x ]) id1 r
293 let cl,_,_ = Terms.get_from_bag id2 bag in
297 let parallel_demod table vl bag t pos ctx id =
298 match demod table vl t with
300 | Some (newside, subst, id2, dir) ->
301 match build_clause bag (fun _ -> true)
302 Terms.Demodulation (ctx newside) subst id id2 pos dir
304 | None -> assert false
305 | Some (bag,(id,_,_,_)) ->
309 let are_alpha_eq cl1 cl2 =
310 let get_term (_,lit,_,_) =
312 | Terms.Predicate _ -> assert false
313 | Terms.Equation (l,r,ty,_) ->
314 Terms.Node [Terms.Leaf (B.eqP()); ty; l ; r]
316 try ignore(Unif.alpha_eq (get_term cl1) (get_term cl2)) ; true
317 with FoUnif.UnificationFailure _ -> false
320 let prof_demodulate = HExtlib.profile ~enable "demodulate";;
321 let demodulate bag clause x =
322 prof_demodulate.HExtlib.profile (demodulate bag clause) x
326 let is_identity_clause = function
327 | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
328 | _, Terms.Equation (_,_,_,_), _, _ -> false
329 | _, Terms.Predicate _, _, _ -> assert false
332 let is_identity_goal = function
333 | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> Some []
334 | _, Terms.Equation (l,r,_,_), vl, proof ->
335 (try Some (Unif.unification (* vl *) [] l r)
336 with FoUnif.UnificationFailure _ -> None)
337 | _, Terms.Predicate _, _, _ -> assert false
340 let build_new_clause_reloc bag maxvar filter rule t subst id id2 pos dir =
341 let maxvar, _vl, subst = Utils.relocate maxvar (Terms.vars_of_term
342 (Subst.apply_subst subst t)) subst in
343 match build_clause bag filter rule t subst id id2 pos dir with
344 | Some (bag, c) -> Some ((bag, maxvar), c), subst
348 let build_new_clause bag maxvar filter rule t subst id id2 pos dir =
349 fst (build_new_clause_reloc bag maxvar filter rule t
350 subst id id2 pos dir)
353 let prof_build_new_clause = HExtlib.profile ~enable "build_new_clause";;
354 let build_new_clause bag maxvar filter rule t subst id id2 pos x =
355 prof_build_new_clause.HExtlib.profile (build_new_clause bag maxvar filter
356 rule t subst id id2 pos) x
359 let fold_build_new_clause bag maxvar id rule filter res =
360 let (bag, maxvar), res =
361 HExtlib.filter_map_acc
362 (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
363 build_new_clause bag maxvar filter rule t subst id id2 pos dir)
369 let rewrite_eq ~unify l r ty vl table =
370 let retrieve = if unify then IDX.DT.retrieve_unifiables
371 else IDX.DT.retrieve_generalizations in
372 let lcands = retrieve table l in
373 let rcands = retrieve table r in
375 let id, dir, l, r, vl =
377 | (d, (id,Terms.Equation (l,r,ty,_),vl,_))-> id, d, l, r, vl
380 let reverse = (dir = Terms.Left2Right) = b in
381 let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
382 else r,l, Terms.Right2Left in
383 (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf (B.eqP()); ty; l; r ], vl)
385 let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
386 let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
387 let t = Terms.Node [ Terms.Leaf (B.eqP()); ty; l; r ] in
388 let locked_vars = if unify then [] else vl in
389 let rec aux = function
391 | (id2,dir,c,vl1)::tl ->
393 let subst = Unif.unification (* (vl@vl1) *) locked_vars c t in
394 Some (id2, dir, subst)
395 with FoUnif.UnificationFailure _ -> aux tl
397 aux (cands1 @ cands2)
400 let is_subsumed ~unify bag maxvar (id, lit, vl, _) table =
402 | Terms.Predicate _ -> assert false
403 | Terms.Equation (l,r,ty,_) ->
404 match rewrite_eq ~unify l r ty vl table with
406 | Some (id2, dir, subst) ->
407 let id_t = Terms.Node [ Terms.Leaf (B.eqP()); ty; r; r ] in
408 build_new_clause bag maxvar (fun _ -> true)
409 Terms.Superposition id_t subst id id2 [2] dir
411 let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
412 let is_subsumed ~unify bag maxvar c x =
413 prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
415 (* id refers to a clause proving contextl l = contextr r *)
417 let rec deep_eq ~unify l r ty pos contextl contextr table acc =
420 | Some(bag,maxvar,(id,lit,vl,p),subst) ->
421 (* prerr_endline ("input subst = "^Pp.pp_substitution subst); *)
422 let l = Subst.apply_subst subst l in
423 let r = Subst.apply_subst subst r in
425 let subst1 = Unif.unification (* vl *) [] l r in
427 match lit with Terms.Predicate _ -> assert false
428 | Terms.Equation (l,r,ty,o) ->
429 Terms.Equation (FoSubst.apply_subst subst1 l,
430 FoSubst.apply_subst subst1 r, ty, o)
432 Some(bag,maxvar,(id,lit,vl,p),Subst.concat subst1 subst)
433 with FoUnif.UnificationFailure _ ->
434 match rewrite_eq ~unify l r ty vl table with
435 | Some (id2, dir, subst1) ->
436 (* prerr_endline ("subst1 = "^Pp.pp_substitution subst1);
437 prerr_endline ("old subst = "^Pp.pp_substitution subst);*)
438 let newsubst = Subst.concat subst1 subst in
440 FoSubst.apply_subst newsubst
441 (Terms.Node[Terms.Leaf (B.eqP());ty;contextl r;contextr r])
444 build_new_clause_reloc bag maxvar (fun _ -> true)
445 Terms.Superposition id_t
446 subst1 id id2 (pos@[2]) dir
448 | Some ((bag, maxvar), c), r ->
449 (* prerr_endline ("r = "^Pp.pp_substitution r); *)
450 let newsubst = Subst.flat
451 (Subst.concat r subst) in
452 Some(bag,maxvar,c,newsubst)
453 | None, _ -> assert false)
456 | Terms.Node (a::la), Terms.Node (b::lb) when
457 a = b && List.length la = List.length lb ->
460 (fun (acc,pre,postl,postr) a b ->
462 fun x -> contextl(Terms.Node (pre@(x::postl))) in
464 fun x -> contextr(Terms.Node (pre@(x::postr))) in
465 let newpos = List.length pre::pos in
467 if l = [] then [] else List.tl l in
468 (deep_eq ~unify a b ty
469 newpos newcl newcr table acc,pre@[b],
470 footail postl, footail postr))
471 (acc,[a],List.tl la,List.tl lb) la lb
476 let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
477 let deep_eq ~unify l r ty pos contextl contextr table x =
478 prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
481 let rec orphan_murder bag acc i =
482 match Terms.get_from_bag i bag with
483 | (_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
484 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
485 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
486 if (List.mem i acc) then (false,acc)
487 else match orphan_murder bag acc i1 with
488 | (true,acc) -> (true,acc)
490 let (res,acc) = orphan_murder bag acc i2 in
491 if res then res,acc else res,i::acc
494 let orphan_murder bag actives cl =
495 let (id,_,_,_) = cl in
496 let actives = List.map (fun (i,_,_,_) -> i) actives in
497 let (res,_) = orphan_murder bag actives id in
498 if res then debug (lazy "Orphan murdered"); res
500 let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
501 let orphan_murder bag actives x =
502 prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
505 (* demodulate and check for subsumption *)
506 let simplify table maxvar bag clause =
507 debug (lazy "simplify...");
508 if is_identity_clause clause then bag,None
509 (* else if orphan_murder bag actives clause then bag,None *)
510 else let bag, clause = demodulate bag clause table in
511 if is_identity_clause clause then bag,None
513 match is_subsumed ~unify:false bag maxvar clause table with
514 | None -> bag, Some clause
515 | Some _ -> bag, None
518 let simplify table maxvar bag clause =
519 match simplify table maxvar bag clause with
521 let (id,_,_,_) = clause in
522 let (_,_,iter) = Terms.get_from_bag id bag in
523 Terms.replace_in_bag (clause,true,iter) bag, None
524 | bag, Some clause -> bag, Some clause
525 (*let (id,_,_,_) = clause in
526 if orphan_murder bag clause then
527 Terms.M.add id (clause,true) bag, Some clause
528 else bag, Some clause*)
530 let prof_simplify = HExtlib.profile ~enable "simplify";;
531 let simplify table maxvar bag x =
532 prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
535 let one_pass_simplification new_clause (alist,atable) bag maxvar =
536 match simplify atable maxvar bag new_clause with
537 | bag,None -> bag,None (* new_clause has been discarded *)
538 | bag,(Some clause) ->
539 let ctable = IDX.index_unit_clause IDX.DT.empty clause in
540 let bag, alist, atable =
542 (fun (bag, alist, atable) c ->
543 match simplify ctable maxvar bag c with
544 |bag,None -> (bag,alist,atable)
545 (* an active clause as been discarded *)
547 bag, c :: alist, IDX.index_unit_clause atable c)
548 (bag,[],IDX.DT.empty) alist
550 bag, Some (clause, (alist,atable))
552 let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
553 let one_pass_simplification new_clause t bag x =
554 prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
557 let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
559 if new_cl then atable else
560 IDX.index_unit_clause atable cl
562 (* Simplification of new_clause with : *
563 * - actives and cl if new_clause is not cl *
564 * - only actives otherwise *)
566 simplify atable1 maxvar bag new_clause with
567 | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
569 (* Simplification of each active clause with clause *
570 * which is the simplified form of new_clause *)
571 let ctable = IDX.index_unit_clause IDX.DT.empty clause in
572 let bag, newa, alist, atable =
574 (fun (bag, newa, alist, atable) c ->
575 match simplify ctable maxvar bag c with
576 |bag,None -> (bag, newa, alist, atable)
577 (* an active clause as been discarded *)
580 bag, newa, c :: alist,
581 IDX.index_unit_clause atable c
583 bag, c1 :: newa, alist, atable)
584 (bag,[],[],IDX.DT.empty) alist
587 bag, (Some cl, Some (clause, (alist,atable), newa))
589 (* if new_clause is not cl, we simplify cl with clause *)
590 match simplify ctable maxvar bag cl with
592 (* cl has been discarded *)
593 bag,(None, Some (clause, (alist,atable), newa))
595 bag,(Some cl1, Some (clause, (alist,atable), newa))
597 let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
598 let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
599 prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
602 let keep_simplified cl (alist,atable) bag maxvar =
603 let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
605 match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
606 | _,(None, _) -> assert false
607 | bag,(Some _, None) -> bag,None
608 | bag,(Some _, Some (clause, (alist,atable), newa)) ->
609 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
613 | [] -> bag, Some (cl, (alist,atable))
615 match simplification_step ~new_cl cl
616 (alist,atable) bag maxvar hd with
617 | _,(None,None) -> assert false
618 | bag,(Some _,None) ->
619 keep_simplified_aux ~new_cl cl (alist,atable) bag tl
620 | bag,(None, Some _) -> bag,None
621 | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
623 (clause::alist, IDX.index_unit_clause atable clause)
625 keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
628 keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
630 let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
631 let keep_simplified cl t bag x =
632 prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
635 (* this is like simplify but raises Success *)
636 let simplify_goal ~no_demod maxvar table bag g_actives clause =
638 if no_demod then bag, clause else demodulate bag clause table
640 let _ = debug (lazy ("demodulated goal : "
641 ^ Pp.pp_unit_clause clause))
643 if List.exists (are_alpha_eq clause) g_actives then None
644 else match (is_identity_goal clause) with
645 | Some subst -> raise (Success (bag,maxvar,clause,subst))
647 let (id,lit,vl,_) = clause in
648 (* this optimization makes sense only if we demodulated, since in
649 that case the clause should have been turned into an identity *)
650 if (vl = [] && not(no_demod))
651 then Some (bag,clause)
655 | Terms.Equation(l,r,ty,_) -> l,r,ty
658 match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x)
659 table (Some(bag,maxvar,clause,Subst.id_subst)) with
660 | None -> Some (bag,clause)
661 | Some (bag,maxvar,cl,subst) ->
662 debug (lazy "Goal subsumed");
663 raise (Success (bag,maxvar,cl,subst))
665 match is_subsumed ~unify:true bag maxvar clause table with
666 | None -> Some (bag, clause)
667 | Some ((bag,maxvar),c) ->
668 prerr_endline "Goal subsumed";
669 raise (Success (bag,maxvar,c))
673 let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
674 let simplify_goal ~no_demod maxvar table bag g_actives x =
675 prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
678 (* =================== inference ===================== *)
680 (* this is OK for both the sup_left and sup_right inference steps *)
681 let superposition table varlist subterm pos context =
682 let cands = IDX.DT.retrieve_unifiables table subterm in
684 (fun (dir, (id,lit,vl,_ (*as uc*))) ->
686 | Terms.Predicate _ -> assert false
687 | Terms.Equation (l,r,_,o) ->
688 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
691 Unif.unification (* (varlist@vl)*) [] subterm side
693 if o = Terms.Incomparable || o = Terms.Invertible then
694 let side = Subst.apply_subst subst side in
695 let newside = Subst.apply_subst subst newside in
696 let o = Order.compare_terms side newside in
697 (* XXX: check Riazanov p. 33 (iii) *)
698 if o <> Terms.Lt && o <> Terms.Eq then
699 Some (context newside, subst, id, pos, dir)
701 ((*prerr_endline ("Filtering: " ^
702 Pp.pp_foterm side ^ " =(< || =)" ^
703 Pp.pp_foterm newside);*)None)
705 Some (context newside, subst, id, pos, dir)
706 with FoUnif.UnificationFailure _ -> None)
707 (IDX.ClauseSet.elements cands)
710 (* Superposes selected equation with equalities in table *)
711 let superposition_with_table bag maxvar (id,selected,vl,_) table =
713 | Terms.Predicate _ -> assert false
714 | Terms.Equation (l,r,ty,Terms.Lt) ->
715 fold_build_new_clause bag maxvar id Terms.Superposition
718 (fun x -> Terms.Node [ Terms.Leaf (B.eqP()); ty; l; x ])
719 r (superposition table vl))
720 | Terms.Equation (l,r,ty,Terms.Invertible)
721 | Terms.Equation (l,r,ty,Terms.Gt) ->
722 fold_build_new_clause bag maxvar id Terms.Superposition
725 (fun x -> Terms.Node [ Terms.Leaf (B.eqP()); ty; x; r ])
726 l (superposition table vl))
727 | Terms.Equation (l,r,ty,Terms.Incomparable) ->
728 let filtering avoid subst = (* Riazanov: p.33 condition (iv) *)
729 let l = Subst.apply_subst subst l in
730 let r = Subst.apply_subst subst r in
731 let o = Order.compare_terms l r in
732 o <> avoid && o <> Terms.Eq
734 let bag, maxvar,r_terms =
735 fold_build_new_clause bag maxvar id Terms.Superposition
738 (fun x -> Terms.Node [ Terms.Leaf (B.eqP()); ty; l; x ])
739 r (superposition table vl))
741 let bag, maxvar, l_terms =
742 fold_build_new_clause bag maxvar id Terms.Superposition
745 (fun x -> Terms.Node [ Terms.Leaf (B.eqP()); ty; x; r ])
746 l (superposition table vl))
748 bag, maxvar, r_terms @ l_terms
752 (* the current equation is normal w.r.t. demodulation with atable
753 * (and is not the identity) *)
754 let infer_right bag maxvar current (alist,atable) =
755 (* We demodulate actives clause with current until all *
756 * active clauses are reduced w.r.t each other *)
757 (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
758 let ctable = IDX.index_unit_clause IDX.DT.empty current in
759 (* let bag, (alist, atable) =
761 HExtlib.filter_map_acc (simplify ctable) bag alist
763 bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
765 debug (lazy "Simplified active clauses with fact");
766 (* We superpose active clauses with current *)
767 let bag, maxvar, new_clauses =
769 (fun (bag, maxvar, acc) active ->
770 let bag, maxvar, newc =
771 superposition_with_table bag maxvar active ctable
773 bag, maxvar, newc @ acc)
774 (bag, maxvar, []) alist
778 ("New clauses :" ^ (String.concat ";\n"
779 (List.map Pp.pp_unit_clause new_clauses))));
780 debug (lazy "First superpositions");
781 (* We add current to active clauses so that it can be *
782 * superposed with itself *)
784 current :: alist, IDX.index_unit_clause atable current
786 debug (lazy "Indexed");
787 let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
788 (* We need to put fresh_current into the bag so that all *
789 * variables clauses refer to are known. *)
790 let bag, fresh_current = Terms.add_to_bag fresh_current bag in
791 (* We superpose current with active clauses *)
792 let bag, maxvar, additional_new_clauses =
793 superposition_with_table bag maxvar fresh_current atable
795 debug (lazy "Another superposition");
796 let new_clauses = new_clauses @ additional_new_clauses in
797 (* debug (lazy (Printf.sprintf "Demodulating %d clauses"
798 (List.length new_clauses))); *)
799 let bag, new_clauses =
800 HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
802 debug (lazy "Demodulated new clauses");
803 bag, maxvar, (alist, atable), new_clauses
806 let prof_ir = HExtlib.profile ~enable "infer_right";;
807 let infer_right bag maxvar current t =
808 prof_ir.HExtlib.profile (infer_right bag maxvar current) t
811 let infer_left bag maxvar goal (_alist, atable) =
812 (* We superpose the goal with active clauses *)
813 if (match goal with (_,_,[],_) -> true | _ -> false) then bag, maxvar, []
815 let bag, maxvar, new_goals =
816 superposition_with_table bag maxvar goal atable
818 debug(lazy "Superposed goal with active clauses");
819 (* We simplify the new goals with active clauses *)
823 match simplify_goal ~no_demod:false maxvar atable bag [] g with
824 | None -> assert false
825 | Some (bag,g) -> bag,g::acc)
828 debug (lazy "Simplified new goals with active clauses");
829 bag, maxvar, List.rev new_goals
832 let prof_il = HExtlib.profile ~enable "infer_left";;
833 let infer_left bag maxvar goal t =
834 prof_il.HExtlib.profile (infer_left bag maxvar goal) t