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 debug s = prerr_endline (Lazy.force s);; *)
33 let rec list_first f = function
35 | x::tl -> match f x with Some _ as x -> x | _ -> list_first f tl
38 let first_position pos ctx t f =
39 let inject_pos pos ctx = function
41 | Some (a,b,c,d) -> Some(ctx a,b,c,d,pos)
43 let rec aux pos ctx = function
44 | Terms.Leaf _ as t -> inject_pos pos ctx (f t)
48 | Some _ as x -> inject_pos pos ctx x
50 let rec first pre post = function
53 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
54 match aux (List.length pre :: pos) newctx t with
57 if post = [] then None (* tl is also empty *)
58 else first (pre @ [t]) (List.tl post) tl
60 first [] (List.tl l) l
65 let all_positions pos ctx t f =
66 let rec aux pos ctx = function
67 | Terms.Leaf _ as t -> f t pos ctx
72 (fun (acc,pre,post) t -> (* Invariant: pre @ [t] @ post = l *)
73 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
74 let acc = aux (List.length pre :: pos) newctx t @ acc in
75 if post = [] then acc, l, []
76 else acc, pre @ [t], List.tl post)
77 (f t pos ctx, [], List.tl l) l
84 let parallel_positions bag pos ctx id t f =
85 let rec aux bag pos ctx id = function
86 | Terms.Leaf _ as t -> f bag t pos ctx id
87 | Terms.Var _ as t -> bag,t,id
88 | Terms.Node (hd::l) as t->
89 let bag,t,id1 = f bag t pos ctx id in
93 (fun (bag,pre,post,id) t ->
94 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
95 let newpos = (List.length pre)::pos in
96 let bag,newt,id = aux bag newpos newctx id t in
97 if post = [] then bag, pre@[newt], [], id
98 else bag, pre @ [newt], List.tl post, id)
99 (bag, [hd], List.tl l, id) l
101 bag, Terms.Node l, id
103 (* else aux bag pos ctx id1 t *)
109 let visit bag pos ctx id t f =
110 let rec aux bag pos ctx id subst = function
111 | Terms.Leaf _ as t ->
112 let bag,subst,t,id = f bag t pos ctx id
113 in assert (subst=[]); bag,t,id
114 | Terms.Var i as t ->
115 let t= Subst.apply_subst subst t in
117 | Terms.Node (hd::l) ->
120 (fun (bag,pre,post,id) t ->
121 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
122 let newpos = (List.length pre)::pos in
123 let bag,newt,id = aux bag newpos newctx id subst t in
124 if post = [] then bag, pre@[newt], [], id
125 else bag, pre @ [newt], List.tl post, id)
126 (bag, [hd], List.map (Subst.apply_subst subst) (List.tl l), id) l
128 let bag,subst,t,id1 = f bag (Terms.Node l) pos ctx id
130 if id1 = id then (assert (subst=[]); bag,t,id)
131 else aux bag pos ctx id1 subst t
134 aux bag pos ctx id [] t
137 let build_clause bag filter rule t subst id id2 pos dir =
138 let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
139 let t = Subst.apply_subst subst t in
143 | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
144 let o = Order.compare_terms l r in
145 Terms.Equation (l, r, ty, o)
146 | t -> Terms.Predicate t
149 Terms.add_to_bag (0, literal, Terms.vars_of_term t, proof) bag
153 ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
155 let prof_build_clause = HExtlib.profile ~enable "build_clause";;
156 let build_clause bag filter rule t subst id id2 pos x =
157 prof_build_clause.HExtlib.profile (build_clause bag filter rule t subst id id2 pos) x
161 (* ============ simplification ================= *)
162 let prof_demod_u = HExtlib.profile ~enable "demod.unify";;
163 let prof_demod_r = HExtlib.profile ~enable "demod.retrieve_generalizations";;
164 let prof_demod_o = HExtlib.profile ~enable "demod.compare_terms";;
165 let prof_demod_s = HExtlib.profile ~enable "demod.apply_subst";;
167 let demod table varlist subterm =
169 prof_demod_r.HExtlib.profile
170 (IDX.DT.retrieve_generalizations table) subterm
173 (fun (dir, (id,lit,vl,_)) ->
175 | Terms.Predicate _ -> assert false
176 | Terms.Equation (l,r,_,o) ->
177 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
180 prof_demod_u.HExtlib.profile
181 (Unif.unification (* (varlist@vl) *) varlist subterm) side
184 prof_demod_s.HExtlib.profile
185 (Subst.apply_subst subst) side
188 prof_demod_s.HExtlib.profile
189 (Subst.apply_subst subst) newside
191 if o = Terms.Incomparable || o = Terms.Invertible then
193 prof_demod_o.HExtlib.profile
194 (Order.compare_terms newside) side in
195 (* Riazanov, pp. 45 (ii) *)
197 Some (newside, subst, id, dir)
199 ((*prerr_endline ("Filtering: " ^
200 Pp.pp_foterm side ^ " =(< || =)" ^
201 Pp.pp_foterm newside ^ " coming from " ^
202 Pp.pp_unit_clause uc );*)None)
204 Some (newside, subst, id, dir)
205 with FoUnif.UnificationFailure _ -> None)
206 (IDX.ClauseSet.elements cands)
208 let prof_demod = HExtlib.profile ~enable "demod";;
209 let demod table varlist x =
210 prof_demod.HExtlib.profile (demod table varlist) x
213 let mydemod table varlist subterm =
215 prof_demod_r.HExtlib.profile
216 (IDX.DT.retrieve_generalizations table) subterm
219 (fun (dir, ((id,lit,vl,_) as c)) ->
220 debug (lazy("candidate: "
221 ^ Pp.pp_unit_clause c));
223 | Terms.Predicate _ -> assert false
224 | Terms.Equation (l,r,_,o) ->
225 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
228 prof_demod_u.HExtlib.profile
229 (Unif.unification (* (varlist@vl) *) varlist subterm) side
232 prof_demod_s.HExtlib.profile
233 (Subst.apply_subst subst) side
236 prof_demod_s.HExtlib.profile
237 (Subst.apply_subst subst) newside
239 if o = Terms.Incomparable || o = Terms.Invertible then
241 prof_demod_o.HExtlib.profile
242 (Order.compare_terms inewside) iside in
243 (* Riazanov, pp. 45 (ii) *)
245 Some (newside, subst, id, dir)
247 ((*prerr_endline ("Filtering: " ^
248 Pp.pp_foterm side ^ " =(< || =)" ^
249 Pp.pp_foterm newside ^ " coming from " ^
250 Pp.pp_unit_clause uc );*)None)
252 Some (newside, subst, id, dir)
253 with FoUnif.UnificationFailure _ -> None)
254 (IDX.ClauseSet.elements cands)
257 let ctx_demod table vl bag t pos ctx id =
258 match mydemod table vl t with
259 | None -> (bag,[],t,id)
260 | Some (newside, subst, id2, dir) ->
261 let inewside = Subst.apply_subst subst newside in
262 match build_clause bag (fun _ -> true)
263 Terms.Demodulation (ctx inewside) subst id id2 pos dir
265 | None -> assert false
266 | Some (bag,(id,_,_,_)) ->
267 (bag,subst,newside,id)
270 let rec demodulate bag (id, literal, vl, pr) table =
271 debug (lazy "demodulate...");
273 | Terms.Predicate t -> assert false
274 | Terms.Equation (l,r,ty,_) ->
277 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id l
282 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 r
285 let cl,_,_ = Terms.get_from_bag id2 bag in
289 let parallel_demod table vl bag t pos ctx id =
290 match demod table vl t with
292 | Some (newside, subst, id2, dir) ->
293 match build_clause bag (fun _ -> true)
294 Terms.Demodulation (ctx newside) subst id id2 pos dir
296 | None -> assert false
297 | Some (bag,(id,_,_,_)) ->
301 let are_alpha_eq cl1 cl2 =
302 let get_term (_,lit,_,_) =
304 | Terms.Predicate _ -> assert false
305 | Terms.Equation (l,r,ty,_) ->
306 Terms.Node [Terms.Leaf B.eqP; ty; l ; r]
308 try ignore(Unif.alpha_eq (get_term cl1) (get_term cl2)) ; true
309 with FoUnif.UnificationFailure _ -> false
312 let prof_demodulate = HExtlib.profile ~enable "demodulate";;
313 let demodulate bag clause x =
314 prof_demodulate.HExtlib.profile (demodulate bag clause) x
318 let is_identity_clause = function
319 | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
320 | _, Terms.Equation (_,_,_,_), _, _ -> false
321 | _, Terms.Predicate _, _, _ -> assert false
324 let is_identity_goal = function
325 | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> Some []
326 | _, Terms.Equation (l,r,_,_), vl, proof ->
327 (try Some (Unif.unification (* vl *) [] l r)
328 with FoUnif.UnificationFailure _ -> None)
329 | _, Terms.Equation (_,_,_,_), _, _ -> None
330 | _, Terms.Predicate _, _, _ -> assert false
333 let build_new_clause bag maxvar filter rule t subst id id2 pos dir =
334 let maxvar, _vl, subst = Utils.relocate maxvar (Terms.vars_of_term
335 (Subst.apply_subst subst t)) subst in
336 match build_clause bag filter rule t subst id id2 pos dir with
337 | Some (bag, c) -> Some ((bag, maxvar), c)
340 let prof_build_new_clause = HExtlib.profile ~enable "build_new_clause";;
341 let build_new_clause bag maxvar filter rule t subst id id2 pos x =
342 prof_build_new_clause.HExtlib.profile (build_new_clause bag maxvar filter
343 rule t subst id id2 pos) x
346 let fold_build_new_clause bag maxvar id rule filter res =
347 let (bag, maxvar), res =
348 HExtlib.filter_map_acc
349 (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
350 build_new_clause bag maxvar filter rule t subst id id2 pos dir)
356 let rewrite_eq ~unify l r ty vl table =
357 let retrieve = if unify then IDX.DT.retrieve_unifiables
358 else IDX.DT.retrieve_generalizations in
359 let lcands = retrieve table l in
360 let rcands = retrieve table r in
362 let id, dir, l, r, vl =
364 | (d, (id,Terms.Equation (l,r,ty,_),vl,_))-> id, d, l, r, vl
367 let reverse = (dir = Terms.Left2Right) = b in
368 let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
369 else r,l, Terms.Right2Left in
370 (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
372 let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
373 let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
374 let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
375 let locked_vars = if unify then [] else vl in
376 let rec aux = function
378 | (id2,dir,c,vl1)::tl ->
380 let subst = Unif.unification (* (vl@vl1) *) locked_vars c t in
381 Some (id2, dir, subst)
382 with FoUnif.UnificationFailure _ -> aux tl
384 aux (cands1 @ cands2)
387 let is_subsumed ~unify bag maxvar (id, lit, vl, _) table =
389 | Terms.Predicate _ -> assert false
390 | Terms.Equation (l,r,ty,_) ->
391 match rewrite_eq ~unify l r ty vl table with
393 | Some (id2, dir, subst) ->
394 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
395 build_new_clause bag maxvar (fun _ -> true)
396 Terms.Superposition id_t subst id id2 [2] dir
398 let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
399 let is_subsumed ~unify bag maxvar c x =
400 prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
402 (* id refers to a clause proving contextl l = contextr r *)
404 let rec deep_eq ~unify l r ty pos contextl contextr table acc =
407 | Some(bag,maxvar,(id,lit,vl,p),subst) ->
408 let l = Subst.apply_subst subst l in
409 let r = Subst.apply_subst subst r in
411 let subst1 = Unif.unification (* vl *) [] l r in
413 match lit with Terms.Predicate _ -> assert false
414 | Terms.Equation (l,r,ty,o) ->
415 Terms.Equation (FoSubst.apply_subst subst1 l,
416 FoSubst.apply_subst subst1 r, ty, o)
418 Some(bag,maxvar,(id,lit,vl,p),Subst.concat subst1 subst)
419 with FoUnif.UnificationFailure _ ->
420 match rewrite_eq ~unify l r ty vl table with
421 | Some (id2, dir, subst1) ->
422 let newsubst = Subst.concat subst1 subst in
424 FoSubst.apply_subst newsubst
425 (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r])
428 build_new_clause bag maxvar (fun _ -> true)
429 Terms.Superposition id_t
430 subst1 id id2 (pos@[2]) dir
432 | Some ((bag, maxvar), c) ->
433 Some(bag,maxvar,c,newsubst)
434 | None -> assert false)
437 | Terms.Node (a::la), Terms.Node (b::lb) when
438 a = b && List.length la = List.length lb ->
441 (fun (acc,pre,postl,postr) a b ->
443 fun x -> contextl(Terms.Node (pre@(x::postl))) in
445 fun x -> contextr(Terms.Node (pre@(x::postr))) in
446 let newpos = List.length pre::pos in
448 if l = [] then [] else List.tl l in
449 (deep_eq ~unify a b ty
450 newpos newcl newcr table acc,pre@[b],
451 footail postl, footail postr))
452 (acc,[a],List.tl la,List.tl lb) la lb
456 let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
457 let deep_eq ~unify l r ty pos contextl contextr table x =
458 prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
461 let rec orphan_murder bag acc i =
462 match Terms.get_from_bag i bag with
463 | (_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
464 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
465 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
466 if (List.mem i acc) then (false,acc)
467 else match orphan_murder bag acc i1 with
468 | (true,acc) -> (true,acc)
470 let (res,acc) = orphan_murder bag acc i2 in
471 if res then res,acc else res,i::acc
474 let orphan_murder bag actives cl =
475 let (id,_,_,_) = cl in
476 let actives = List.map (fun (i,_,_,_) -> i) actives in
477 let (res,_) = orphan_murder bag actives id in
478 if res then debug (lazy "Orphan murdered"); res
480 let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
481 let orphan_murder bag actives x =
482 prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
485 (* demodulate and check for subsumption *)
486 let simplify table maxvar bag clause =
487 debug (lazy "simplify...");
488 if is_identity_clause clause then bag,None
489 (* else if orphan_murder bag actives clause then bag,None *)
490 else let bag, clause = demodulate bag clause table in
491 if is_identity_clause clause then bag,None
493 match is_subsumed ~unify:false bag maxvar clause table with
494 | None -> bag, Some clause
495 | Some _ -> bag, None
498 let simplify table maxvar bag clause =
499 match simplify table maxvar bag clause with
501 let (id,_,_,_) = clause in
502 let (_,_,iter) = Terms.get_from_bag id bag in
503 Terms.replace_in_bag (clause,true,iter) bag, None
504 | bag, Some clause -> bag, Some clause
505 (*let (id,_,_,_) = clause in
506 if orphan_murder bag clause then
507 Terms.M.add id (clause,true) bag, Some clause
508 else bag, Some clause*)
510 let prof_simplify = HExtlib.profile ~enable "simplify";;
511 let simplify table maxvar bag x =
512 prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
515 let one_pass_simplification new_clause (alist,atable) bag maxvar =
516 match simplify atable maxvar bag new_clause with
517 | bag,None -> bag,None (* new_clause has been discarded *)
518 | bag,(Some clause) ->
519 let ctable = IDX.index_unit_clause IDX.DT.empty clause in
520 let bag, alist, atable =
522 (fun (bag, alist, atable) c ->
523 match simplify ctable maxvar bag c with
524 |bag,None -> (bag,alist,atable)
525 (* an active clause as been discarded *)
527 bag, c :: alist, IDX.index_unit_clause atable c)
528 (bag,[],IDX.DT.empty) alist
530 bag, Some (clause, (alist,atable))
532 let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
533 let one_pass_simplification new_clause t bag x =
534 prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
537 let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
539 if new_cl then atable else
540 IDX.index_unit_clause atable cl
542 (* Simplification of new_clause with : *
543 * - actives and cl if new_clause is not cl *
544 * - only actives otherwise *)
546 simplify atable1 maxvar bag new_clause with
547 | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
549 (* Simplification of each active clause with clause *
550 * which is the simplified form of new_clause *)
551 let ctable = IDX.index_unit_clause IDX.DT.empty clause in
552 let bag, newa, alist, atable =
554 (fun (bag, newa, alist, atable) c ->
555 match simplify ctable maxvar bag c with
556 |bag,None -> (bag, newa, alist, atable)
557 (* an active clause as been discarded *)
560 bag, newa, c :: alist,
561 IDX.index_unit_clause atable c
563 bag, c1 :: newa, alist, atable)
564 (bag,[],[],IDX.DT.empty) alist
567 bag, (Some cl, Some (clause, (alist,atable), newa))
569 (* if new_clause is not cl, we simplify cl with clause *)
570 match simplify ctable maxvar bag cl with
572 (* cl has been discarded *)
573 bag,(None, Some (clause, (alist,atable), newa))
575 bag,(Some cl1, Some (clause, (alist,atable), newa))
577 let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
578 let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
579 prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
582 let keep_simplified cl (alist,atable) bag maxvar =
583 let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
585 match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
586 | _,(None, _) -> assert false
587 | bag,(Some _, None) -> bag,None
588 | bag,(Some _, Some (clause, (alist,atable), newa)) ->
589 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
593 | [] -> bag, Some (cl, (alist,atable))
595 match simplification_step ~new_cl cl
596 (alist,atable) bag maxvar hd with
597 | _,(None,None) -> assert false
598 | bag,(Some _,None) ->
599 keep_simplified_aux ~new_cl cl (alist,atable) bag tl
600 | bag,(None, Some _) -> bag,None
601 | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
603 (clause::alist, IDX.index_unit_clause atable clause)
605 keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
608 keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
610 let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
611 let keep_simplified cl t bag x =
612 prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
615 (* this is like simplify but raises Success *)
616 let simplify_goal ~no_demod maxvar table bag g_actives clause =
618 if no_demod then bag, clause else demodulate bag clause table
620 if List.exists (are_alpha_eq clause) g_actives then None
621 else match (is_identity_goal clause) with
622 | Some subst -> raise (Success (bag,maxvar,clause,subst))
624 let (id,lit,vl,_) = clause in
625 (* this optimization makes sense only if we demodulated, since in
626 that case the clause should have been turned into an identity *)
627 if (vl = [] && not(no_demod))
628 then Some (bag,clause)
632 | Terms.Equation(l,r,ty,_) -> l,r,ty
635 match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x)
636 table (Some(bag,maxvar,clause,Subst.id_subst)) with
637 | None -> Some (bag,clause)
638 | Some (bag,maxvar,cl,subst) ->
639 debug (lazy "Goal subsumed");
640 raise (Success (bag,maxvar,cl,subst))
642 match is_subsumed ~unify:true bag maxvar clause table with
643 | None -> Some (bag, clause)
644 | Some ((bag,maxvar),c) ->
645 prerr_endline "Goal subsumed";
646 raise (Success (bag,maxvar,c))
650 let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
651 let simplify_goal ~no_demod maxvar table bag g_actives x =
652 prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
655 (* =================== inference ===================== *)
657 (* this is OK for both the sup_left and sup_right inference steps *)
658 let superposition table varlist subterm pos context =
659 let cands = IDX.DT.retrieve_unifiables table subterm in
661 (fun (dir, (id,lit,vl,_ (*as uc*))) ->
663 | Terms.Predicate _ -> assert false
664 | Terms.Equation (l,r,_,o) ->
665 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
668 Unif.unification (* (varlist@vl)*) [] subterm side
670 if o = Terms.Incomparable || o = Terms.Invertible then
671 let side = Subst.apply_subst subst side in
672 let newside = Subst.apply_subst subst newside in
673 let o = Order.compare_terms side newside in
674 (* XXX: check Riazanov p. 33 (iii) *)
675 if o <> Terms.Lt && o <> Terms.Eq then
676 Some (context newside, subst, id, pos, dir)
678 ((*prerr_endline ("Filtering: " ^
679 Pp.pp_foterm side ^ " =(< || =)" ^
680 Pp.pp_foterm newside);*)None)
682 Some (context newside, subst, id, pos, dir)
683 with FoUnif.UnificationFailure _ -> None)
684 (IDX.ClauseSet.elements cands)
687 (* Superposes selected equation with equalities in table *)
688 let superposition_with_table bag maxvar (id,selected,vl,_) table =
690 | Terms.Predicate _ -> assert false
691 | Terms.Equation (l,r,ty,Terms.Lt) ->
692 fold_build_new_clause bag maxvar id Terms.Superposition
695 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
696 r (superposition table vl))
697 | Terms.Equation (l,r,ty,Terms.Invertible)
698 | Terms.Equation (l,r,ty,Terms.Gt) ->
699 fold_build_new_clause bag maxvar id Terms.Superposition
702 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
703 l (superposition table vl))
704 | Terms.Equation (l,r,ty,Terms.Incomparable) ->
705 let filtering avoid subst = (* Riazanov: p.33 condition (iv) *)
706 let l = Subst.apply_subst subst l in
707 let r = Subst.apply_subst subst r in
708 let o = Order.compare_terms l r in
709 o <> avoid && o <> Terms.Eq
711 let bag, maxvar,r_terms =
712 fold_build_new_clause bag maxvar id Terms.Superposition
715 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
716 r (superposition table vl))
718 let bag, maxvar, l_terms =
719 fold_build_new_clause bag maxvar id Terms.Superposition
722 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
723 l (superposition table vl))
725 bag, maxvar, r_terms @ l_terms
729 (* the current equation is normal w.r.t. demodulation with atable
730 * (and is not the identity) *)
731 let infer_right bag maxvar current (alist,atable) =
732 (* We demodulate actives clause with current until all *
733 * active clauses are reduced w.r.t each other *)
734 (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
735 let ctable = IDX.index_unit_clause IDX.DT.empty current in
736 (* let bag, (alist, atable) =
738 HExtlib.filter_map_acc (simplify ctable) bag alist
740 bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
742 debug (lazy "Simplified active clauses with fact");
743 (* We superpose active clauses with current *)
744 let bag, maxvar, new_clauses =
746 (fun (bag, maxvar, acc) active ->
747 let bag, maxvar, newc =
748 superposition_with_table bag maxvar active ctable
750 bag, maxvar, newc @ acc)
751 (bag, maxvar, []) alist
753 debug (lazy "First superpositions");
754 (* We add current to active clauses so that it can be *
755 * superposed with itself *)
757 current :: alist, IDX.index_unit_clause atable current
759 debug (lazy "Indexed");
760 let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
761 (* We need to put fresh_current into the bag so that all *
762 * variables clauses refer to are known. *)
763 let bag, fresh_current = Terms.add_to_bag fresh_current bag in
764 (* We superpose current with active clauses *)
765 let bag, maxvar, additional_new_clauses =
766 superposition_with_table bag maxvar fresh_current atable
768 debug (lazy "Another superposition");
769 let new_clauses = new_clauses @ additional_new_clauses in
770 (* debug (lazy (Printf.sprintf "Demodulating %d clauses"
771 (List.length new_clauses))); *)
772 let bag, new_clauses =
773 HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
775 debug (lazy "Demodulated new clauses");
776 bag, maxvar, (alist, atable), new_clauses
779 let prof_ir = HExtlib.profile ~enable "infer_right";;
780 let infer_right bag maxvar current t =
781 prof_ir.HExtlib.profile (infer_right bag maxvar current) t
784 let infer_left bag maxvar goal (_alist, atable) =
785 (* We superpose the goal with active clauses *)
786 if (match goal with (_,_,[],_) -> true | _ -> false) then bag, maxvar, []
788 let bag, maxvar, new_goals =
789 superposition_with_table bag maxvar goal atable
791 debug(lazy "Superposed goal with active clauses");
792 (* We simplify the new goals with active clauses *)
796 match simplify_goal ~no_demod:false maxvar atable bag [] g with
797 | None -> assert false
798 | Some (bag,g) -> bag,g::acc)
801 debug (lazy "Simplified new goals with active clauses");
802 bag, maxvar, List.rev new_goals
805 let prof_il = HExtlib.profile ~enable "infer_left";;
806 let infer_left bag maxvar goal t =
807 prof_il.HExtlib.profile (infer_left bag maxvar goal) t