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)
22 module Clauses = Clauses.Clauses(B)
24 exception Success of B.t Terms.bag * int * B.t Terms.clause
26 let debug s = prerr_endline (Lazy.force s);;
27 (* let debug _ = ();; *)
30 let rec list_first f = function
32 | x::tl -> match f x with Some _ as x -> x | _ -> list_first f tl
35 let first_position pos ctx t f =
36 let inject_pos pos ctx = function
38 | Some (a,b,c,d) -> Some(ctx a,b,c,d,pos)
40 let rec aux pos ctx = function
41 | Terms.Leaf _ as t -> inject_pos pos ctx (f t)
45 | Some _ as x -> inject_pos pos ctx x
47 let rec first pre post = function
50 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
51 match aux (List.length pre :: pos) newctx t with
54 if post = [] then None (* tl is also empty *)
55 else first (pre @ [t]) (List.tl post) tl
57 first [] (List.tl l) l
62 let all_positions pos ctx t f =
63 let rec aux pos ctx = function
64 | Terms.Leaf _ as t -> f t pos ctx
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
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
88 let bag, l, _, id, lit =
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
98 bag, Terms.Node l, id, lit
102 aux bag pos ctx id lit t
105 let build_clause ~fresh bag maxvar 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
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
116 let nlit,plit = clause_ctx literal in
117 let cl = (0, nlit, plit, [], proof) in
118 let vl = Clauses.vars_of_clause cl in
120 if fresh then Clauses.fresh_clause ~subst maxvar (0, nlit, plit, vl, proof)
124 Terms.add_to_bag (0, nlit, plit, vl, proof) bag
126 Some (bag, maxvar, cl, literal)
128 ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
130 let prof_build_clause = HExtlib.profile ~enable "build_clause";;
131 let build_clause ~fresh bag maxvar filter rule t subst id id2 pos x =
132 prof_build_clause.HExtlib.profile
133 (build_clause ~fresh bag maxvar filter rule t subst id id2 pos) x
137 (* ============ simplification ================= *)
138 let prof_demod_u = HExtlib.profile ~enable "demod.unify";;
139 let prof_demod_r = HExtlib.profile ~enable "demod.retrieve_generalizations";;
140 let prof_demod_o = HExtlib.profile ~enable "demod.compare_terms";;
141 let prof_demod_s = HExtlib.profile ~enable "demod.apply_subst";;
143 let demod table varlist subterm =
145 prof_demod_r.HExtlib.profile
146 (IDX.DT.retrieve_generalizations table) subterm
149 (fun (dir, is_pos, pos, (id,nlit,plit,vl,_)) ->
153 | Terms.Predicate _ -> assert false
154 | Terms.Equation (l,r,_,o) ->
155 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
158 prof_demod_u.HExtlib.profile
159 (Unif.unification (* (varlist@vl) *) varlist subterm) side
162 prof_demod_s.HExtlib.profile
163 (Subst.apply_subst subst) side
166 prof_demod_s.HExtlib.profile
167 (Subst.apply_subst subst) newside
169 if o = Terms.Incomparable || o = Terms.Invertible then
171 prof_demod_o.HExtlib.profile
172 (Order.compare_terms newside) side in
173 (* Riazanov, pp. 45 (ii) *)
175 Some (newside, subst, id, dir)
177 ((*prerr_endline ("Filtering: " ^
178 Pp.pp_foterm side ^ " =(< || =)" ^
179 Pp.pp_foterm newside ^ " coming from " ^
180 Pp.pp_clause uc );*)None)
182 Some (newside, subst, id, dir)
183 with FoUnif.UnificationFailure _ -> None)
185 (IDX.ClauseSet.elements cands)
187 let prof_demod = HExtlib.profile ~enable "demod";;
188 let demod table varlist x =
189 prof_demod.HExtlib.profile (demod table varlist) x
192 let parallel_demod table vl clause_ctx bag t pos ctx id lit =
193 match demod table vl t with
194 | None -> (bag,t,id,lit)
195 | Some (newside, subst, id2, dir) ->
196 match build_clause ~fresh:false bag 0 (fun _ -> true)
197 Terms.Demodulation (ctx newside) subst id id2 pos dir clause_ctx
199 | None -> assert false
200 | Some (bag,_,(id,_,_,_,_),lit) ->
204 let demodulate_once ~jump_to_right bag id literal vl table clause_ctx =
206 | Terms.Predicate t -> assert false
207 | Terms.Equation (l,r,ty,_) as lit ->
208 let bag,l,id1,lit = if jump_to_right then (bag,l,id,lit) else
209 parallel_positions bag [2]
210 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id lit l
211 (parallel_demod table vl clause_ctx)
213 let jump_to_right = id1 = id in
215 parallel_positions bag [3]
216 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 lit r
217 (parallel_demod table vl clause_ctx)
219 if id = id2 then None
221 Some ((bag,id2,lit),jump_to_right)
224 let rec demodulate bag (id,nlit,plit,vl,proof) table =
225 let rec demod_lit ~jump_to_right bag id lit clause_ctx =
226 match demodulate_once ~jump_to_right bag id lit vl table clause_ctx with
227 | None -> bag, id, lit
228 | Some ((bag, id, lit),jump) ->
229 demod_lit ~jump_to_right:jump bag id lit clause_ctx
231 (*let cmp_bag,cmp_cl = match nlit,plit with
233 let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> nlit, [l,true])
235 let cl,_,_ = Terms.get_from_bag id bag in
238 let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> [l,true],[])
240 let cl,_,_ = Terms.get_from_bag id bag in
244 let nlit,_,bag,id = if nlit = [] then nlit,[],bag,id
246 (fun (pre,post,bag,id) (lit,sel) ->
248 demod_lit ~jump_to_right:false bag id lit (fun l -> pre@[l,sel]@post,plit)
250 if post=[] then pre@[(lit,sel)],[],bag,id
251 else pre@[(lit,sel)],List.tl post,bag,id)
252 ([],List.tl nlit, bag, id) nlit
254 let _,_,bag,id = if plit = [] then plit,[],bag,id
256 (fun (pre,post,bag,id) (lit,sel) ->
258 demod_lit ~jump_to_right:false bag id lit (fun l -> nlit,pre@[l,sel]@post)
260 if post=[] then pre@[(lit,sel)],[],bag,id
261 else pre@[(lit,sel)],List.tl post,bag,id)
262 ([],List.tl plit, bag, id) plit
264 let cl,_,_ = Terms.get_from_bag id bag in
268 let prof_demodulate = HExtlib.profile ~enable "demodulate";;
269 let demodulate bag clause x =
270 prof_demodulate.HExtlib.profile (demodulate bag clause) x
274 let is_identity_clause ~unify = function
275 | _, [], [Terms.Equation (_,_,_,Terms.Eq),_], _, _ -> true
276 | _, [], [Terms.Equation (l,r,_,_),_], vl, _ when unify ->
277 (try ignore(Unif.unification (* vl *) [] l r); true
278 with FoUnif.UnificationFailure _ -> false)
282 let is_goal_trivial = function
283 | _, [Terms.Equation (_,_,_,Terms.Eq),_], [], _, _ -> true
284 | _, [Terms.Equation (l,r,_,_),_], [], vl, _ ->
285 (try ignore(Unif.unification (* vl *) [] l r); true
286 with FoUnif.UnificationFailure _ -> false)
289 let fold_build_new_clause bag maxvar id rule filter res clause_ctx =
290 let (bag, maxvar), res =
291 HExtlib.filter_map_acc
292 (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
293 match build_clause ~fresh:true bag maxvar filter
294 rule t subst id id2 pos dir clause_ctx with
296 | Some (bag,maxvar,res,_) -> Some ((bag,maxvar),res))
302 (* Tries to rewrite an equality to identity, using unit equalities in table *)
303 let rewrite_eq ~unify l r ty vl table =
304 let retrieve = if unify then IDX.DT.retrieve_unifiables
305 else IDX.DT.retrieve_generalizations in
306 let lcands = retrieve table l in
307 let rcands = retrieve table r in
309 let id, dir, l, r, vl =
311 | (d,_,_, (id,[],[Terms.Equation (l,r,ty,_),_],vl,_))-> id, d, l, r, vl
312 | (d,_,_, (id,[Terms.Equation (l,r,ty,_),_],[],vl,_))-> id, d, l, r, vl
315 let reverse = (dir = Terms.Left2Right) = b in
316 let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
317 else r,l, Terms.Right2Left in
318 (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
320 let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
321 let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
322 let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
323 let locked_vars = if unify then [] else vl in
324 let rec aux = function
326 | (id2,dir,c,vl1)::tl ->
328 let subst = Unif.unification (* (vl@vl1) *) locked_vars c t in
329 Some (id2, dir, subst)
330 with FoUnif.UnificationFailure _ -> aux tl
332 aux (cands1 @ cands2)
335 let is_subsumed ~unify bag maxvar (id, nlit, plit, vl, _) table =
337 | [],[Terms.Equation (l,r,ty,_) ,_]->
338 (match rewrite_eq ~unify l r ty vl table with
340 | Some (id2, dir, subst) ->
341 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
342 build_clause ~fresh:true bag maxvar (fun _ -> true)
343 Terms.Superposition id_t subst id id2 [2] dir (fun l -> [],[l,true]))
344 | _ -> None (* TODO : implement subsumption for clauses *)
346 let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
347 let is_subsumed ~unify bag maxvar c x =
348 prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
350 (* id refers to a clause proving contextl l = contextr r *)
352 let rec deep_eq ~unify l r ty pos contextl contextr table acc =
355 | Some(bag,maxvar,(id,nlit,plit,vl,p),subst) ->
356 let l = Subst.apply_subst subst l in
357 let r = Subst.apply_subst subst r in
359 let subst1 = Unif.unification (* vl *) [] l r in
362 | [Terms.Equation (l,r,ty,o),_],[] ->
363 Terms.Equation (FoSubst.apply_subst subst1 l,
364 FoSubst.apply_subst subst1 r, ty, o)
367 Some(bag,maxvar,(id,[],[lit,true],vl,p),Subst.concat subst1 subst)
368 with FoUnif.UnificationFailure _ ->
369 match rewrite_eq ~unify l r ty vl table with
370 | Some (id2, dir, subst1) ->
371 let newsubst = Subst.concat subst1 subst in
373 FoSubst.apply_subst newsubst
374 (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r])
377 build_clause ~fresh:true bag maxvar (fun _ -> true)
378 Terms.Superposition id_t
379 subst1 id id2 (pos@[2]) dir (fun l -> [],[l,true])
381 | Some (bag, maxvar, c, _) ->
382 Some(bag,maxvar,c,newsubst)
383 | None -> assert false)
386 | Terms.Node (a::la), Terms.Node (b::lb) when
387 a = b && List.length la = List.length lb ->
390 (fun (acc,pre,postl,postr) a b ->
392 fun x -> contextl(Terms.Node (pre@(x::postl))) in
394 fun x -> contextr(Terms.Node (pre@(x::postr))) in
395 let newpos = List.length pre::pos in
397 if l = [] then [] else List.tl l in
398 (deep_eq ~unify a b ty
399 newpos newcl newcr table acc,pre@[b],
400 footail postl, footail postr))
401 (acc,[a],List.tl la,List.tl lb) la lb
405 let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
406 let deep_eq ~unify l r ty pos contextl contextr table x =
407 prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
410 let rec orphan_murder bag acc i =
411 match Terms.get_from_bag i bag with
412 | (_,_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
413 | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
414 | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
415 if (List.mem i acc) then (false,acc)
416 else match orphan_murder bag acc i1 with
417 | (true,acc) -> (true,acc)
419 let (res,acc) = orphan_murder bag acc i2 in
420 if res then res,acc else res,i::acc
423 let orphan_murder bag actives cl =
424 let (id,_,_,_,_) = cl in
425 let actives = List.map (fun (i,_,_,_,_) -> i) actives in
426 let (res,_) = orphan_murder bag actives id in
427 if res then debug (lazy "Orphan murdered"); res
429 let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
430 let orphan_murder bag actives x =
431 prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
434 (* demodulate and check for subsumption *)
435 let simplify table maxvar bag clause =
436 if is_identity_clause ~unify:false clause then bag,None
437 (* else if orphan_murder bag actives clause then bag,None *)
438 else let bag, clause = demodulate bag clause table in
439 if is_identity_clause ~unify:false clause then bag,None
441 match is_subsumed ~unify:false bag maxvar clause table with
442 | None -> bag, Some clause
443 | Some _ -> bag, None
446 let simplify table maxvar bag clause =
447 match simplify table maxvar bag clause with
449 let (id,_,_,_,_) = clause in
450 let (_,_,iter) = Terms.get_from_bag id bag in
451 Terms.replace_in_bag (clause,true,iter) bag, None
452 | bag, Some clause -> bag, Some clause
453 (*let (id,_,_,_) = clause in
454 if orphan_murder bag clause then
455 Terms.M.add id (clause,true) bag, Some clause
456 else bag, Some clause*)
458 let prof_simplify = HExtlib.profile ~enable "simplify";;
459 let simplify table maxvar bag x =
460 prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
463 let one_pass_simplification new_clause (alist,atable) bag maxvar =
464 match simplify atable maxvar bag new_clause with
465 | bag,None -> bag,None (* new_clause has been discarded *)
466 | bag,(Some clause) ->
467 let ctable = IDX.index_clause IDX.DT.empty clause in
468 let bag, alist, atable =
470 (fun (bag, alist, atable) c ->
471 match simplify ctable maxvar bag c with
472 |bag,None -> (bag,alist,atable)
473 (* an active clause as been discarded *)
475 bag, c :: alist, IDX.index_clause atable c)
476 (bag,[],IDX.DT.empty) alist
478 bag, Some (clause, (alist,atable))
480 let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
481 let one_pass_simplification new_clause t bag x =
482 prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
485 let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
487 if new_cl then atable else
488 IDX.index_clause atable cl
490 (* Simplification of new_clause with : *
491 * - actives and cl if new_clause is not cl *
492 * - only actives otherwise *)
494 simplify atable1 maxvar bag new_clause with
495 | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
497 (* Simplification of each active clause with clause *
498 * which is the simplified form of new_clause *)
499 let ctable = IDX.index_clause IDX.DT.empty clause in
500 let bag, newa, alist, atable =
502 (fun (bag, newa, alist, atable) c ->
503 match simplify ctable maxvar bag c with
504 |bag,None -> (bag, newa, alist, atable)
505 (* an active clause as been discarded *)
508 bag, newa, c :: alist,
509 IDX.index_clause atable c
511 bag, c1 :: newa, alist, atable)
512 (bag,[],[],IDX.DT.empty) alist
515 bag, (Some cl, Some (clause, (alist,atable), newa))
517 (* if new_clause is not cl, we simplify cl with clause *)
518 match simplify ctable maxvar bag cl with
520 (* cl has been discarded *)
521 bag,(None, Some (clause, (alist,atable), newa))
523 bag,(Some cl1, Some (clause, (alist,atable), newa))
525 let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
526 let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
527 prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
530 let keep_simplified cl (alist,atable) bag maxvar =
531 let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
533 match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
534 | _,(None, _) -> assert false
535 | bag,(Some _, None) -> bag,None
536 | bag,(Some _, Some (clause, (alist,atable), newa)) ->
537 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
541 | [] -> bag, Some (cl, (alist,atable))
543 match simplification_step ~new_cl cl
544 (alist,atable) bag maxvar hd with
545 | _,(None,None) -> assert false
546 | bag,(Some _,None) ->
547 keep_simplified_aux ~new_cl cl (alist,atable) bag tl
548 | bag,(None, Some _) -> bag,None
549 | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
551 (clause::alist, IDX.index_clause atable clause)
553 keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
556 keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
558 let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
559 let keep_simplified cl t bag x =
560 prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
563 (* this is like simplify but raises Success *)
564 let simplify_goal ~no_demod maxvar table bag g_actives clause =
566 if no_demod then bag, clause else demodulate bag clause table
568 if List.exists (Clauses.are_alpha_eq_cl clause) g_actives then None else
569 (debug (lazy (Pp.pp_clause clause));
570 if (is_goal_trivial clause)
571 then raise (Success (bag, maxvar, clause))
573 let (id,nlit,plit,vl,_) = clause in
574 if vl = [] then Some (bag,clause)
578 | [Terms.Equation(l,r,ty,_),_],[] -> l,r,ty
581 match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x)
582 table (Some(bag,maxvar,clause,Subst.id_subst)) with
583 | None -> Some (bag,clause)
584 | Some (bag,maxvar,cl,subst) ->
585 prerr_endline "Goal subsumed";
586 raise (Success (bag,maxvar,cl)))
588 else match is_subsumed ~unify:true bag maxvar clause table with
589 | None -> Some (bag, clause)
590 | Some ((bag,maxvar),c) ->
591 prerr_endline "Goal subsumed";
592 raise (Success (bag,maxvar,c))
596 let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
597 let simplify_goal ~no_demod maxvar table bag g_actives x =
598 prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
601 (* =================== inference ===================== *)
603 (* this is OK for both the sup_left and sup_right inference steps *)
604 let superposition table varlist is_pos subterm pos context =
605 let cands = IDX.DT.retrieve_unifiables table subterm in
607 (fun (dir, is_cand_pos, _, (id,nlit,plit,vl,_ (*as uc*))) ->
609 | [],[Terms.Equation (l,r,_,o),_] ->
610 (let side, newside = if dir=Terms.Left2Right then l,r else r,l in
613 Unif.unification (* (varlist@vl)*) [] subterm side
615 if o = Terms.Incomparable || o = Terms.Invertible then
616 let side = Subst.apply_subst subst side in
617 let newside = Subst.apply_subst subst newside in
618 let o = Order.compare_terms side newside in
619 (* XXX: check Riazanov p. 33 (iii) *)
620 if o <> Terms.Lt && o <> Terms.Eq then
621 Some (context newside, subst, id, pos, dir)
623 ((*prerr_endline ("Filtering: " ^
624 Pp.pp_foterm side ^ " =(< || =)" ^
625 Pp.pp_foterm newside);*)None)
627 Some (context newside, subst, id, pos, dir)
628 with FoUnif.UnificationFailure _ -> None)
630 (IDX.ClauseSet.elements cands)
633 (* Superposes selected equation with equalities in table *)
634 let superposition_with_table bag maxvar id vl lit is_pos clause_ctx table =
636 | Terms.Equation (l,r,ty,Terms.Lt) ->
637 fold_build_new_clause bag maxvar id Terms.Superposition
640 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
641 r (superposition table vl is_pos)) clause_ctx
642 | Terms.Equation (l,r,ty,Terms.Invertible)
643 | Terms.Equation (l,r,ty,Terms.Gt) ->
644 fold_build_new_clause bag maxvar id Terms.Superposition
647 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
648 l (superposition table vl is_pos)) clause_ctx
649 | Terms.Equation (l,r,ty,Terms.Incomparable) ->
650 let filtering avoid subst = (* Riazanov: p.33 condition (iv) *)
651 let l = Subst.apply_subst subst l in
652 let r = Subst.apply_subst subst r in
653 let o = Order.compare_terms l r in
654 o <> avoid && o <> Terms.Eq
656 let bag, maxvar,r_terms =
657 fold_build_new_clause bag maxvar id Terms.Superposition
660 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
661 r (superposition table vl is_pos)) clause_ctx
663 let bag, maxvar, l_terms =
664 fold_build_new_clause bag maxvar id Terms.Superposition
667 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
668 l (superposition table vl is_pos)) clause_ctx
670 bag, maxvar, r_terms @ l_terms
674 let superpose_literal id vl table is_pos (bag,maxvar,pre,post,acc) (lit,sel) =
676 if is_pos then fun l -> [],pre@[l,true]@post
677 else fun l -> pre@[l,true]@post,[]
679 let bag, maxvar, newc =
680 superposition_with_table bag maxvar id vl lit is_pos clause_ctx table
682 if post = [] then bag,maxvar,pre@[lit,sel],[],newc@acc
683 else bag,maxvar,pre@[lit,sel],List.tl post,newc@acc
687 (* the current equation is normal w.r.t. demodulation with atable
688 * (and is not the identity) *)
689 let infer_right bag maxvar current (alist,atable) =
690 (* We demodulate actives clause with current until all *
691 * active clauses are reduced w.r.t each other *)
692 (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
693 let ctable = IDX.index_clause IDX.DT.empty current in
694 (* let bag, (alist, atable) =
696 HExtlib.filter_map_acc (simplify ctable) bag alist
698 bag, (alist, List.fold_left IDX.index_clause IDX.DT.empty alist)
700 debug (lazy "Simplified active clauses with fact");
701 (* We superpose active clauses with current *)
702 let bag, maxvar, new_clauses =
704 (fun (bag, maxvar, acc) (id,nlit,plit,vl,_) ->
705 let bag, maxvar, _, _, acc =
706 if nlit = [] then bag,maxvar,[],[],acc
708 (superpose_literal id vl ctable false) (bag,maxvar,[],List.tl nlit,acc) nlit
710 let bag, maxvar, _, _, acc =
711 if plit = [] then bag,maxvar,[],[],acc
713 (superpose_literal id vl ctable true) (bag,maxvar,[],List.tl plit,acc) plit
716 (bag, maxvar, []) alist
718 debug (lazy "First superpositions");
719 (* We add current to active clauses so that it can be *
720 * superposed with itself *)
722 current :: alist, IDX.index_clause atable current
724 debug (lazy "Indexed");
725 let fresh_current, maxvar = Clauses.fresh_clause maxvar current in
726 (* We need to put fresh_current into the bag so that all *
727 * variables clauses refer to are known. *)
728 let bag, (id,nlit,plit,vl,_) = Terms.add_to_bag fresh_current bag in
729 (* We superpose current with active clauses *)
730 let bag, maxvar, _, _, new_clauses =
731 if nlit = [] then bag,maxvar,[],[],new_clauses
733 (superpose_literal id vl atable false) (bag,maxvar,[],List.tl nlit,new_clauses) nlit
735 let bag, maxvar, _, _, new_clauses =
736 if plit = [] then bag,maxvar,[],[],new_clauses
738 (superpose_literal id vl atable true) (bag,maxvar,[],List.tl plit,new_clauses) plit
740 debug (lazy "Another superposition");
741 debug (lazy (Printf.sprintf "Demodulating %d clauses"
742 (List.length new_clauses)));
743 let bag, new_clauses =
744 HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
746 debug (lazy "Demodulated new clauses");
747 bag, maxvar, (alist, atable), new_clauses
750 let prof_ir = HExtlib.profile ~enable "infer_right";;
751 let infer_right bag maxvar current t =
752 prof_ir.HExtlib.profile (infer_right bag maxvar current) t
755 let infer_left bag maxvar goal (_alist, atable) =
756 (* We superpose the goal with active clauses *)
757 if (match goal with (_,_,_,[],_) -> true | _ -> false) then bag, maxvar, []
761 | (id,nlit,[],vl,_) -> id,nlit,vl
764 let bag, maxvar, _, _, new_goals =
765 List.fold_left (superpose_literal id vl atable false)
766 (bag,maxvar,[],List.tl nlit,[]) nlit
768 debug (lazy "Superposed goal with active clauses");
769 (* We simplify the new goals with active clauses *)
773 match simplify_goal ~no_demod:false maxvar atable bag [] g with
774 | None -> assert false
775 | Some (bag,g) -> bag,g::acc)
778 debug (lazy "Simplified new goals with active clauses");
779 bag, maxvar, List.rev new_goals
782 let prof_il = HExtlib.profile ~enable "infer_left";;
783 let infer_left bag maxvar goal t =
784 prof_il.HExtlib.profile (infer_left bag maxvar goal) t