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);;
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)
121 else (0,nlit,plit,vl,proof),maxvar
124 Terms.add_to_bag cl bag
126 debug (lazy (Pp.pp_clause cl));
127 Some (bag, maxvar, cl, literal)
129 ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
131 let prof_build_clause = HExtlib.profile ~enable "build_clause";;
132 let build_clause ~fresh bag maxvar filter rule t subst id id2 pos x =
133 prof_build_clause.HExtlib.profile
134 (build_clause ~fresh bag maxvar filter rule t subst id id2 pos) x
138 (* ============ simplification ================= *)
139 let prof_demod_u = HExtlib.profile ~enable "demod.unify";;
140 let prof_demod_r = HExtlib.profile ~enable "demod.retrieve_generalizations";;
141 let prof_demod_o = HExtlib.profile ~enable "demod.compare_terms";;
142 let prof_demod_s = HExtlib.profile ~enable "demod.apply_subst";;
144 let demod table varlist subterm =
146 prof_demod_r.HExtlib.profile
147 (IDX.DT.retrieve_generalizations table) subterm
150 (fun (dir, is_pos, pos, (id,nlit,plit,vl,_)) ->
154 | Terms.Predicate _ -> assert false
155 | Terms.Equation (l,r,_,o) ->
156 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
159 prof_demod_u.HExtlib.profile
160 (Unif.unification (* (varlist@vl) *) varlist subterm) side
163 prof_demod_s.HExtlib.profile
164 (Subst.apply_subst subst) side
167 prof_demod_s.HExtlib.profile
168 (Subst.apply_subst subst) newside
170 if o = Terms.Incomparable || o = Terms.Invertible then
172 prof_demod_o.HExtlib.profile
173 (Order.compare_terms newside) side in
174 (* Riazanov, pp. 45 (ii) *)
176 Some (newside, subst, id, dir)
178 ((*prerr_endline ("Filtering: " ^
179 Pp.pp_foterm side ^ " =(< || =)" ^
180 Pp.pp_foterm newside ^ " coming from " ^
181 Pp.pp_clause uc );*)None)
183 Some (newside, subst, id, dir)
184 with FoUnif.UnificationFailure _ -> None)
186 (IDX.ClauseSet.elements cands)
188 let prof_demod = HExtlib.profile ~enable "demod";;
189 let demod table varlist x =
190 prof_demod.HExtlib.profile (demod table varlist) x
193 let parallel_demod table vl clause_ctx bag t pos ctx id lit =
194 match demod table vl t with
195 | None -> (bag,t,id,lit)
196 | Some (newside, subst, id2, dir) ->
197 match build_clause ~fresh:false bag 0 (fun _ -> true)
198 Terms.Demodulation (ctx newside) subst id id2 pos dir clause_ctx
200 | None -> assert false
201 | Some (bag,_,(id,_,_,_,_),lit) ->
205 let demodulate_once ~jump_to_right bag id literal vl table clause_ctx =
207 | Terms.Predicate t -> assert false
208 | Terms.Equation (l,r,ty,_) as lit ->
209 let bag,l,id1,lit = if jump_to_right then (bag,l,id,lit) else
210 parallel_positions bag [2]
211 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id lit l
212 (parallel_demod table vl clause_ctx)
214 let jump_to_right = id1 = id in
216 parallel_positions bag [3]
217 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 lit r
218 (parallel_demod table vl clause_ctx)
220 if id = id2 then None
222 Some ((bag,id2,lit),jump_to_right)
225 let rec demodulate bag (id,nlit,plit,vl,proof) table =
226 let rec demod_lit ~jump_to_right bag id lit clause_ctx =
227 match demodulate_once ~jump_to_right bag id lit vl table clause_ctx with
228 | None -> bag, id, lit
229 | Some ((bag, id, lit),jump) ->
230 demod_lit ~jump_to_right:jump bag id lit clause_ctx
232 (*match nlit,plit with
234 let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> nlit, [l,true])
236 let cl,_,_ = Terms.get_from_bag id bag in
239 let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> [l,true],[])
241 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 debug (lazy (string_of_int (List.length res)));
291 let (bag, maxvar), res =
292 HExtlib.filter_map_acc
293 (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
294 match build_clause ~fresh:true bag maxvar filter
295 rule t subst id id2 pos dir clause_ctx with
297 | Some (bag,maxvar,res,_) -> Some ((bag,maxvar),res))
303 (* Tries to rewrite an equality to identity, using unit equalities in table *)
304 let rewrite_eq ~unify l r ty vl table =
305 let retrieve = if unify then IDX.DT.retrieve_unifiables
306 else IDX.DT.retrieve_generalizations in
307 let lcands = retrieve table l in
308 let rcands = retrieve table r in
310 let id, dir, l, r, vl =
312 | (d,_,_, (id,[],[Terms.Equation (l,r,ty,_),_],vl,_))-> id, d, l, r, vl
313 | (d,_,_, (id,[Terms.Equation (l,r,ty,_),_],[],vl,_))-> id, d, l, r, vl
316 let reverse = (dir = Terms.Left2Right) = b in
317 let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
318 else r,l, Terms.Right2Left in
319 (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
321 let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
322 let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
323 let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
324 let locked_vars = if unify then [] else vl in
325 let rec aux = function
327 | (id2,dir,c,vl1)::tl ->
329 let subst = Unif.unification (* (vl@vl1) *) locked_vars c t in
330 Some (id2, dir, subst)
331 with FoUnif.UnificationFailure _ -> aux tl
333 aux (cands1 @ cands2)
336 let is_subsumed ~unify bag maxvar (id, nlit, plit, vl, _) table =
338 | [Terms.Equation (l,r,ty,_) ,_],[]
339 | [],[Terms.Equation (l,r,ty,_) ,_]->
340 (match rewrite_eq ~unify l r ty vl table with
342 | Some (id2, dir, subst) ->
343 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
344 build_clause ~fresh:true bag maxvar (fun _ -> true)
345 Terms.Superposition id_t subst id id2 [2] dir (fun l -> [],[l,true]))
346 | _ -> None (* TODO : implement subsumption for clauses *)
348 let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
349 let is_subsumed ~unify bag maxvar c x =
350 prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
352 (* id refers to a clause proving contextl l = contextr r *)
354 let rec deep_eq ~unify l r ty pos contextl contextr table acc =
357 | Some(bag,maxvar,(id,nlit,plit,vl,p as cl),subst) ->
358 let l = Subst.apply_subst subst l in
359 let r = Subst.apply_subst subst r in
361 let subst1 = Unif.unification (* vl *) [] l r in
364 | [Terms.Equation (l,r,ty,o),_],[] ->
365 Terms.Equation (FoSubst.apply_subst subst1 l,
366 FoSubst.apply_subst subst1 r, ty, o)
367 | _ -> debug (lazy (Pp.pp_clause cl));assert false
369 Some(bag,maxvar,(id,[lit,true],[],vl,p),Subst.concat subst1 subst)
370 with FoUnif.UnificationFailure _ ->
371 match rewrite_eq ~unify l r ty vl table with
372 | Some (id2, dir, subst1) ->
373 let newsubst = Subst.concat subst1 subst in
375 FoSubst.apply_subst newsubst
376 (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r])
379 build_clause ~fresh:true bag maxvar (fun _ -> true)
380 Terms.Superposition id_t
381 subst1 id id2 (pos@[2]) dir (fun l -> [l,true],[])
383 | Some (bag, maxvar, c, _) ->
384 Some(bag,maxvar,c,newsubst)
385 | None -> assert false)
388 | Terms.Node (a::la), Terms.Node (b::lb) when
389 a = b && List.length la = List.length lb ->
392 (fun (acc,pre,postl,postr) a b ->
394 fun x -> contextl(Terms.Node (pre@(x::postl))) in
396 fun x -> contextr(Terms.Node (pre@(x::postr))) in
397 let newpos = List.length pre::pos in
399 if l = [] then [] else List.tl l in
400 (deep_eq ~unify a b ty
401 newpos newcl newcr table acc,pre@[b],
402 footail postl, footail postr))
403 (acc,[a],List.tl la,List.tl lb) la lb
407 let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
408 let deep_eq ~unify l r ty pos contextl contextr table x =
409 prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
412 let rec orphan_murder bag acc i =
413 match Terms.get_from_bag i bag with
414 | (_,_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
415 | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
416 | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
417 if (List.mem i acc) then (false,acc)
418 else match orphan_murder bag acc i1 with
419 | (true,acc) -> (true,acc)
421 let (res,acc) = orphan_murder bag acc i2 in
422 if res then res,acc else res,i::acc
425 let orphan_murder bag actives cl =
426 let (id,_,_,_,_) = cl in
427 let actives = List.map (fun (i,_,_,_,_) -> i) actives in
428 let (res,_) = orphan_murder bag actives id in
429 if res then debug (lazy "Orphan murdered"); res
431 let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
432 let orphan_murder bag actives x =
433 prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
436 (* demodulate and check for subsumption *)
437 let simplify table maxvar bag clause =
438 if is_identity_clause ~unify:false clause then bag,None
439 (* else if orphan_murder bag actives clause then bag,None *)
440 else let bag, clause = demodulate bag clause table in
441 if is_identity_clause ~unify:false clause then bag,None
443 match is_subsumed ~unify:false bag maxvar clause table with
444 | None -> bag, Some clause
445 | Some _ -> bag, None
448 let simplify table maxvar bag clause =
449 match simplify table maxvar bag clause with
451 let (id,_,_,_,_) = clause in
452 let (_,_,iter) = Terms.get_from_bag id bag in
453 Terms.replace_in_bag (clause,true,iter) bag, None
454 | bag, Some clause -> bag, Some clause
455 (*let (id,_,_,_) = clause in
456 if orphan_murder bag clause then
457 Terms.M.add id (clause,true) bag, Some clause
458 else bag, Some clause*)
460 let prof_simplify = HExtlib.profile ~enable "simplify";;
461 let simplify table maxvar bag x =
462 prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
465 let one_pass_simplification new_clause (alist,atable) bag maxvar =
466 match simplify atable maxvar bag new_clause with
467 | bag,None -> bag,None (* new_clause has been discarded *)
468 | bag,(Some clause) ->
469 let ctable = IDX.index_clause IDX.DT.empty clause in
470 let bag, alist, atable =
472 (fun (bag, alist, atable) c ->
473 match simplify ctable maxvar bag c with
474 |bag,None -> (bag,alist,atable)
475 (* an active clause as been discarded *)
477 bag, c :: alist, IDX.index_clause atable c)
478 (bag,[],IDX.DT.empty) alist
480 bag, Some (clause, (alist,atable))
482 let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
483 let one_pass_simplification new_clause t bag x =
484 prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
487 let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
489 if new_cl then atable else
490 IDX.index_clause atable cl
492 (* Simplification of new_clause with : *
493 * - actives and cl if new_clause is not cl *
494 * - only actives otherwise *)
496 simplify atable1 maxvar bag new_clause with
497 | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
499 (* Simplification of each active clause with clause *
500 * which is the simplified form of new_clause *)
501 let ctable = IDX.index_clause IDX.DT.empty clause in
502 let bag, newa, alist, atable =
504 (fun (bag, newa, alist, atable) c ->
505 match simplify ctable maxvar bag c with
506 |bag,None -> (bag, newa, alist, atable)
507 (* an active clause as been discarded *)
510 bag, newa, c :: alist,
511 IDX.index_clause atable c
513 bag, c1 :: newa, alist, atable)
514 (bag,[],[],IDX.DT.empty) alist
517 bag, (Some cl, Some (clause, (alist,atable), newa))
519 (* if new_clause is not cl, we simplify cl with clause *)
520 match simplify ctable maxvar bag cl with
522 (* cl has been discarded *)
523 bag,(None, Some (clause, (alist,atable), newa))
525 bag,(Some cl1, Some (clause, (alist,atable), newa))
527 let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
528 let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
529 prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
532 let keep_simplified cl (alist,atable) bag maxvar =
533 let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
535 match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
536 | _,(None, _) -> assert false
537 | bag,(Some _, None) -> bag,None
538 | bag,(Some _, Some (clause, (alist,atable), newa)) ->
539 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
543 | [] -> bag, Some (cl, (alist,atable))
545 match simplification_step ~new_cl cl
546 (alist,atable) bag maxvar hd with
547 | _,(None,None) -> assert false
548 | bag,(Some _,None) ->
549 keep_simplified_aux ~new_cl cl (alist,atable) bag tl
550 | bag,(None, Some _) -> bag,None
551 | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
553 (clause::alist, IDX.index_clause atable clause)
555 keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
558 keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
560 let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
561 let keep_simplified cl t bag x =
562 prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
565 (* this is like simplify but raises Success *)
566 let simplify_goal ~no_demod maxvar table bag g_actives clause =
568 if no_demod then bag, clause else demodulate bag clause table
570 if List.exists (Clauses.are_alpha_eq_cl clause) g_actives then None else
571 if (is_goal_trivial clause)
572 then raise (Success (bag, maxvar, clause))
574 let (id,nlit,plit,vl,_) = clause in
575 if vl = [] then Some (bag,clause)
579 | [Terms.Equation(l,r,ty,_),_],[] -> l,r,ty
582 match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x)
583 table (Some(bag,maxvar,clause,Subst.id_subst)) with
584 | None -> Some (bag,clause)
585 | Some (bag,maxvar,cl,subst) ->
586 prerr_endline "Goal subsumed";
587 raise (Success (bag,maxvar,cl))
589 else match is_subsumed ~unify:true bag maxvar clause table with
590 | None -> Some (bag, clause)
591 | Some ((bag,maxvar),c) ->
592 prerr_endline "Goal subsumed";
593 raise (Success (bag,maxvar,c))
597 let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
598 let simplify_goal ~no_demod maxvar table bag g_actives x =
599 prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
602 (* =================== inference ===================== *)
604 (* this is OK for both the sup_left and sup_right inference steps *)
605 let superposition table varlist is_pos subterm pos context =
606 let cands = IDX.DT.retrieve_unifiables table subterm in
608 (fun (dir, is_cand_pos, _, (id,nlit,plit,vl,_ (*as uc*))) ->
610 | [],[Terms.Equation (l,r,_,o),_] ->
611 (let side, newside = if dir=Terms.Left2Right then l,r else r,l in
612 debug (lazy (Pp.pp_foterm subterm));
613 debug (lazy (Pp.pp_foterm side));
616 Unif.unification (* (varlist@vl)*) [] subterm side
618 if o = Terms.Incomparable || o = Terms.Invertible then
619 let side = Subst.apply_subst subst side in
620 let newside = Subst.apply_subst subst newside in
621 let o = Order.compare_terms side newside in
622 (* XXX: check Riazanov p. 33 (iii) *)
623 if o <> Terms.Lt && o <> Terms.Eq then
624 Some (context newside, subst, id, pos, dir)
626 (debug (lazy "Filtering out..."); None)
628 Some (context newside, subst, id, pos, dir)
629 with FoUnif.UnificationFailure _ -> None)
631 (IDX.ClauseSet.elements cands)
634 (* Superposes selected equation with equalities in table *)
635 let superposition_with_table bag maxvar id vl lit is_pos clause_ctx table =
637 | Terms.Equation (l,r,ty,Terms.Lt) ->
638 fold_build_new_clause bag maxvar id Terms.Superposition
641 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
642 r (superposition table vl is_pos)) clause_ctx
643 | Terms.Equation (l,r,ty,Terms.Invertible)
644 | Terms.Equation (l,r,ty,Terms.Gt) ->
645 fold_build_new_clause bag maxvar id Terms.Superposition
648 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
649 l (superposition table vl is_pos)) clause_ctx
650 | Terms.Equation (l,r,ty,Terms.Incomparable) ->
651 let filtering avoid subst = (* Riazanov: p.33 condition (iv) *)
652 let l = Subst.apply_subst subst l in
653 let r = Subst.apply_subst subst r in
654 let o = Order.compare_terms l r in
655 o <> avoid && o <> Terms.Eq
657 let bag, maxvar,r_terms =
658 fold_build_new_clause bag maxvar id Terms.Superposition
661 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
662 r (superposition table vl is_pos)) clause_ctx
664 let bag, maxvar, l_terms =
665 fold_build_new_clause bag maxvar id Terms.Superposition
668 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
669 l (superposition table vl is_pos)) clause_ctx
671 bag, maxvar, r_terms @ l_terms
675 let superpose_literal id vl table is_pos (bag,maxvar,pre,post,acc) (lit,sel) =
677 if is_pos then fun l -> [],pre@[l,true]@post
678 else fun l -> pre@[l,true]@post,[]
680 let bag, maxvar, newc =
681 superposition_with_table bag maxvar id vl lit is_pos clause_ctx table
683 if post = [] then bag,maxvar,pre@[lit,sel],[],newc@acc
684 else bag,maxvar,pre@[lit,sel],List.tl post,newc@acc
688 (* the current equation is normal w.r.t. demodulation with atable
689 * (and is not the identity) *)
690 let infer_right bag maxvar current (alist,atable) =
691 (* We demodulate actives clause with current until all *
692 * active clauses are reduced w.r.t each other *)
693 (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
694 let ctable = IDX.index_clause IDX.DT.empty current in
695 (* let bag, (alist, atable) =
697 HExtlib.filter_map_acc (simplify ctable) bag alist
699 bag, (alist, List.fold_left IDX.index_clause IDX.DT.empty alist)
701 debug (lazy "Simplified active clauses with fact");
702 (* We superpose active clauses with current *)
703 let bag, maxvar, new_clauses =
705 (fun (bag, maxvar, acc) (id,nlit,plit,vl,_) ->
706 let bag, maxvar, _, _, acc =
707 if nlit = [] then bag,maxvar,[],[],acc
709 (superpose_literal id vl ctable false) (bag,maxvar,[],List.tl nlit,acc) nlit
711 let bag, maxvar, _, _, acc =
712 if plit = [] then bag,maxvar,[],[],acc
714 (superpose_literal id vl ctable true) (bag,maxvar,[],List.tl plit,acc) plit
717 (bag, maxvar, []) alist
719 debug (lazy "First superpositions");
720 (* We add current to active clauses so that it can be *
721 * superposed with itself *)
723 current :: alist, IDX.index_clause atable current
725 debug (lazy "Indexed");
726 let fresh_current, maxvar = Clauses.fresh_clause maxvar current in
727 (* We need to put fresh_current into the bag so that all *
728 * variables clauses refer to are known. *)
729 let bag, (id,nlit,plit,vl,_) = Terms.add_to_bag fresh_current bag in
730 (* We superpose current with active clauses *)
731 let bag, maxvar, _, _, additional_new_clauses =
732 if nlit = [] then bag,maxvar,[],[],[]
734 (superpose_literal id vl atable false) (bag,maxvar,[],List.tl nlit,[]) nlit
736 let bag, maxvar, _, _, additional_new_clauses =
737 if plit = [] then bag,maxvar,[],[],[]
739 (superpose_literal id vl atable true) (bag,maxvar,[],List.tl plit,additional_new_clauses) plit
741 debug (lazy "Another superposition");
742 let new_clauses = new_clauses@additional_new_clauses in
743 debug (lazy (Printf.sprintf "Demodulating %d clauses"
744 (List.length new_clauses)));
745 let bag, new_clauses =
746 HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
748 debug (lazy "Demodulated new clauses");
749 bag, maxvar, (alist, atable), new_clauses
752 let prof_ir = HExtlib.profile ~enable "infer_right";;
753 let infer_right bag maxvar current t =
754 prof_ir.HExtlib.profile (infer_right bag maxvar current) t
757 let infer_left bag maxvar goal (_alist, atable) =
758 (* We superpose the goal with active clauses *)
759 if (match goal with (_,_,_,[],_) -> true | _ -> false) then bag, maxvar, []
763 | (id,nlit,[],vl,_) -> id,nlit,vl
766 let bag, maxvar, _, _, new_goals =
767 List.fold_left (superpose_literal id vl atable false)
768 (bag,maxvar,[],List.tl nlit,[]) nlit
770 debug (lazy (string_of_int (List.length new_goals) ^ " new goals"));
771 (* We simplify the new goals with active clauses *)
775 match simplify_goal ~no_demod:false maxvar atable bag [] g with
776 | None -> assert false
777 | Some (bag,g) -> bag,g::acc)
780 debug (lazy "Simplified new goals with active clauses");
781 bag, maxvar, List.rev new_goals
784 let prof_il = HExtlib.profile ~enable "infer_left";;
785 let infer_left bag maxvar goal t =
786 prof_il.HExtlib.profile (infer_left bag maxvar goal) t