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 bag 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
118 Terms.add_to_bag (0, nlit, plit, Terms.vars_of_term t, proof) bag
120 Some (bag, uc, literal)
122 ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
124 let prof_build_clause = HExtlib.profile ~enable "build_clause";;
125 let build_clause bag filter rule t subst id id2 pos x =
126 prof_build_clause.HExtlib.profile (build_clause bag filter rule t subst id id2 pos) x
130 (* ============ simplification ================= *)
131 let prof_demod_u = HExtlib.profile ~enable "demod.unify";;
132 let prof_demod_r = HExtlib.profile ~enable "demod.retrieve_generalizations";;
133 let prof_demod_o = HExtlib.profile ~enable "demod.compare_terms";;
134 let prof_demod_s = HExtlib.profile ~enable "demod.apply_subst";;
136 let demod table varlist subterm =
138 prof_demod_r.HExtlib.profile
139 (IDX.DT.retrieve_generalizations table) subterm
142 (fun (dir, is_pos, pos, (id,nlit,plit,vl,_)) ->
146 | Terms.Predicate _ -> assert false
147 | Terms.Equation (l,r,_,o) ->
148 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
151 prof_demod_u.HExtlib.profile
152 (Unif.unification (* (varlist@vl) *) varlist subterm) side
155 prof_demod_s.HExtlib.profile
156 (Subst.apply_subst subst) side
159 prof_demod_s.HExtlib.profile
160 (Subst.apply_subst subst) newside
162 if o = Terms.Incomparable || o = Terms.Invertible then
164 prof_demod_o.HExtlib.profile
165 (Order.compare_terms newside) side in
166 (* Riazanov, pp. 45 (ii) *)
168 Some (newside, subst, id, dir)
170 ((*prerr_endline ("Filtering: " ^
171 Pp.pp_foterm side ^ " =(< || =)" ^
172 Pp.pp_foterm newside ^ " coming from " ^
173 Pp.pp_clause uc );*)None)
175 Some (newside, subst, id, dir)
176 with FoUnif.UnificationFailure _ -> None)
178 (IDX.ClauseSet.elements cands)
180 let prof_demod = HExtlib.profile ~enable "demod";;
181 let demod table varlist x =
182 prof_demod.HExtlib.profile (demod table varlist) x
185 let parallel_demod table vl clause_ctx bag t pos ctx id lit =
186 match demod table vl t with
187 | None -> (bag,t,id,lit)
188 | Some (newside, subst, id2, dir) ->
189 match build_clause bag (fun _ -> true)
190 Terms.Demodulation (ctx newside) subst id id2 pos dir clause_ctx
192 | None -> assert false
193 | Some (bag,(id,_,_,_,_),lit) ->
197 let demodulate_once ~jump_to_right bag id literal vl table clause_ctx =
199 | Terms.Predicate t -> assert false
200 | Terms.Equation (l,r,ty,_) as lit ->
201 let bag,l,id1,lit = if jump_to_right then (bag,l,id,lit) else
202 parallel_positions bag [2]
203 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id lit l
204 (parallel_demod table vl clause_ctx)
206 let jump_to_right = id1 = id in
208 parallel_positions bag [3]
209 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 lit r
210 (parallel_demod table vl clause_ctx)
212 if id = id2 then None
214 Some ((bag,id2,lit),jump_to_right)
217 let rec demodulate bag (id,nlit,plit,vl,proof) table =
218 let rec demod_lit ~jump_to_right bag id lit clause_ctx =
219 match demodulate_once ~jump_to_right bag id lit vl table clause_ctx with
220 | None -> bag, id, lit
221 | Some ((bag, id, lit),jump) ->
222 demod_lit ~jump_to_right:jump bag id lit clause_ctx
224 (*let cmp_bag,cmp_cl = match nlit,plit with
226 let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> nlit, [l,true])
228 let cl,_,_ = Terms.get_from_bag id bag in
231 let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> [l,true],[])
233 let cl,_,_ = Terms.get_from_bag id bag in
237 let nlit,_,bag,id = if nlit = [] then nlit,[],bag,id
239 (fun (pre,post,bag,id) (lit,sel) ->
241 demod_lit ~jump_to_right:false bag id lit (fun l -> pre@[l,sel]@post,plit)
243 if post=[] then pre@[(lit,sel)],[],bag,id
244 else pre@[(lit,sel)],List.tl post,bag,id)
245 ([],List.tl nlit, bag, id) nlit
247 let _,_,bag,id = if plit = [] then plit,[],bag,id
249 (fun (pre,post,bag,id) (lit,sel) ->
251 demod_lit ~jump_to_right:false bag id lit (fun l -> nlit,pre@[l,sel]@post)
253 if post=[] then pre@[(lit,sel)],[],bag,id
254 else pre@[(lit,sel)],List.tl post,bag,id)
255 ([],List.tl plit, bag, id) plit
257 let cl,_,_ = Terms.get_from_bag id bag in
261 let prof_demodulate = HExtlib.profile ~enable "demodulate";;
262 let demodulate bag clause x =
263 prof_demodulate.HExtlib.profile (demodulate bag clause) x
267 let is_identity_clause ~unify = function
268 | _, [], [Terms.Equation (_,_,_,Terms.Eq),_], _, _ -> true
269 | _, [], [Terms.Equation (l,r,_,_),_], vl, _ when unify ->
270 (try ignore(Unif.unification (* vl *) [] l r); true
271 with FoUnif.UnificationFailure _ -> false)
275 let is_goal_trivial = function
276 | _, [Terms.Equation (_,_,_,Terms.Eq),_], [], _, _ -> true
277 | _, [Terms.Equation (l,r,_,_),_], [], vl, _ ->
278 (try ignore(Unif.unification (* vl *) [] l r); true
279 with FoUnif.UnificationFailure _ -> false)
282 let build_new_clause bag maxvar filter rule t subst id id2 pos dir =
283 let maxvar, _vl, subst = Utils.relocate maxvar (Terms.vars_of_term
284 (Subst.apply_subst subst t)) subst in
285 match build_clause bag filter rule t subst id id2 pos dir (fun x -> [],[(x,true)]) with
286 | Some (bag, c, _) -> Some ((bag, maxvar), c)
289 let prof_build_new_clause = HExtlib.profile ~enable "build_new_clause";;
290 let build_new_clause bag maxvar filter rule t subst id id2 pos x =
291 prof_build_new_clause.HExtlib.profile (build_new_clause bag maxvar filter
292 rule t subst id id2 pos) x
295 let fold_build_new_clause bag maxvar id rule filter res =
296 let (bag, maxvar), res =
297 HExtlib.filter_map_acc
298 (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
299 build_new_clause bag maxvar filter rule t subst id id2 pos dir)
305 (* Tries to rewrite an equality to identity, using unit equalities in table *)
306 let rewrite_eq ~unify l r ty vl table =
307 let retrieve = if unify then IDX.DT.retrieve_unifiables
308 else IDX.DT.retrieve_generalizations in
309 let lcands = retrieve table l in
310 let rcands = retrieve table r in
312 let id, dir, l, r, vl =
314 | (d,_,_, (id,[],[Terms.Equation (l,r,ty,_),_],vl,_))-> id, d, l, r, vl
315 | (d,_,_, (id,[Terms.Equation (l,r,ty,_),_],[],vl,_))-> id, d, l, r, vl
318 let reverse = (dir = Terms.Left2Right) = b in
319 let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
320 else r,l, Terms.Right2Left in
321 (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
323 let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
324 let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
325 let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
326 let locked_vars = if unify then [] else vl in
327 let rec aux = function
329 | (id2,dir,c,vl1)::tl ->
331 let subst = Unif.unification (* (vl@vl1) *) locked_vars c t in
332 Some (id2, dir, subst)
333 with FoUnif.UnificationFailure _ -> aux tl
335 aux (cands1 @ cands2)
338 let is_subsumed ~unify bag maxvar (id, nlit, plit, vl, _) table =
340 | [],[Terms.Equation (l,r,ty,_) ,_]->
341 (match rewrite_eq ~unify l r ty vl table with
343 | Some (id2, dir, subst) ->
344 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
345 build_new_clause bag maxvar (fun _ -> true)
346 Terms.Superposition id_t subst id id2 [2] dir)
347 | _ -> None (* TODO : implement subsumption for clauses *)
349 let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
350 let is_subsumed ~unify bag maxvar c x =
351 prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
353 (* id refers to a clause proving contextl l = contextr r *)
355 let rec deep_eq ~unify l r ty pos contextl contextr table acc =
358 | Some(bag,maxvar,(id,nlit,plit,vl,p),subst) ->
359 let l = Subst.apply_subst subst l in
360 let r = Subst.apply_subst subst r in
362 let subst1 = Unif.unification (* vl *) [] l r in
365 | [],[Terms.Equation (l,r,ty,o),_] ->
366 Terms.Equation (FoSubst.apply_subst subst1 l,
367 FoSubst.apply_subst subst1 r, ty, o)
370 Some(bag,maxvar,(id,[],[lit,true],vl,p),Subst.concat subst1 subst)
371 with FoUnif.UnificationFailure _ ->
372 match rewrite_eq ~unify l r ty vl table with
373 | Some (id2, dir, subst1) ->
374 let newsubst = Subst.concat subst1 subst in
376 FoSubst.apply_subst newsubst
377 (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r])
380 build_new_clause bag maxvar (fun _ -> true)
381 Terms.Superposition id_t
382 subst1 id id2 (pos@[2]) dir
384 | Some ((bag, maxvar), c) ->
385 Some(bag,maxvar,c,newsubst)
386 | None -> assert false)
389 | Terms.Node (a::la), Terms.Node (b::lb) when
390 a = b && List.length la = List.length lb ->
393 (fun (acc,pre,postl,postr) a b ->
395 fun x -> contextl(Terms.Node (pre@(x::postl))) in
397 fun x -> contextr(Terms.Node (pre@(x::postr))) in
398 let newpos = List.length pre::pos in
400 if l = [] then [] else List.tl l in
401 (deep_eq ~unify a b ty
402 newpos newcl newcr table acc,pre@[b],
403 footail postl, footail postr))
404 (acc,[a],List.tl la,List.tl lb) la lb
408 let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
409 let deep_eq ~unify l r ty pos contextl contextr table x =
410 prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
413 let rec orphan_murder bag acc i =
414 match Terms.get_from_bag i bag with
415 | (_,_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
416 | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
417 | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
418 if (List.mem i acc) then (false,acc)
419 else match orphan_murder bag acc i1 with
420 | (true,acc) -> (true,acc)
422 let (res,acc) = orphan_murder bag acc i2 in
423 if res then res,acc else res,i::acc
426 let orphan_murder bag actives cl =
427 let (id,_,_,_,_) = cl in
428 let actives = List.map (fun (i,_,_,_,_) -> i) actives in
429 let (res,_) = orphan_murder bag actives id in
430 if res then debug (lazy "Orphan murdered"); res
432 let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
433 let orphan_murder bag actives x =
434 prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
437 (* demodulate and check for subsumption *)
438 let simplify table maxvar bag clause =
439 if is_identity_clause ~unify:false clause then bag,None
440 (* else if orphan_murder bag actives clause then bag,None *)
441 else let bag, clause = demodulate bag clause table in
442 if is_identity_clause ~unify:false clause then bag,None
444 match is_subsumed ~unify:false bag maxvar clause table with
445 | None -> bag, Some clause
446 | Some _ -> bag, None
449 let simplify table maxvar bag clause =
450 match simplify table maxvar bag clause with
452 let (id,_,_,_,_) = clause in
453 let (_,_,iter) = Terms.get_from_bag id bag in
454 Terms.replace_in_bag (clause,true,iter) bag, None
455 | bag, Some clause -> bag, Some clause
456 (*let (id,_,_,_) = clause in
457 if orphan_murder bag clause then
458 Terms.M.add id (clause,true) bag, Some clause
459 else bag, Some clause*)
461 let prof_simplify = HExtlib.profile ~enable "simplify";;
462 let simplify table maxvar bag x =
463 prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
466 let one_pass_simplification new_clause (alist,atable) bag maxvar =
467 match simplify atable maxvar bag new_clause with
468 | bag,None -> bag,None (* new_clause has been discarded *)
469 | bag,(Some clause) ->
470 let ctable = IDX.index_clause IDX.DT.empty clause in
471 let bag, alist, atable =
473 (fun (bag, alist, atable) c ->
474 match simplify ctable maxvar bag c with
475 |bag,None -> (bag,alist,atable)
476 (* an active clause as been discarded *)
478 bag, c :: alist, IDX.index_clause atable c)
479 (bag,[],IDX.DT.empty) alist
481 bag, Some (clause, (alist,atable))
483 let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
484 let one_pass_simplification new_clause t bag x =
485 prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
488 let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
490 if new_cl then atable else
491 IDX.index_clause atable cl
493 (* Simplification of new_clause with : *
494 * - actives and cl if new_clause is not cl *
495 * - only actives otherwise *)
497 simplify atable1 maxvar bag new_clause with
498 | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
500 (* Simplification of each active clause with clause *
501 * which is the simplified form of new_clause *)
502 let ctable = IDX.index_clause IDX.DT.empty clause in
503 let bag, newa, alist, atable =
505 (fun (bag, newa, alist, atable) c ->
506 match simplify ctable maxvar bag c with
507 |bag,None -> (bag, newa, alist, atable)
508 (* an active clause as been discarded *)
511 bag, newa, c :: alist,
512 IDX.index_clause atable c
514 bag, c1 :: newa, alist, atable)
515 (bag,[],[],IDX.DT.empty) alist
518 bag, (Some cl, Some (clause, (alist,atable), newa))
520 (* if new_clause is not cl, we simplify cl with clause *)
521 match simplify ctable maxvar bag cl with
523 (* cl has been discarded *)
524 bag,(None, Some (clause, (alist,atable), newa))
526 bag,(Some cl1, Some (clause, (alist,atable), newa))
528 let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
529 let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
530 prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
533 let keep_simplified cl (alist,atable) bag maxvar =
534 let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
536 match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
537 | _,(None, _) -> assert false
538 | bag,(Some _, None) -> bag,None
539 | bag,(Some _, Some (clause, (alist,atable), newa)) ->
540 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
544 | [] -> bag, Some (cl, (alist,atable))
546 match simplification_step ~new_cl cl
547 (alist,atable) bag maxvar hd with
548 | _,(None,None) -> assert false
549 | bag,(Some _,None) ->
550 keep_simplified_aux ~new_cl cl (alist,atable) bag tl
551 | bag,(None, Some _) -> bag,None
552 | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
554 (clause::alist, IDX.index_clause atable clause)
556 keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
559 keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
561 let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
562 let keep_simplified cl t bag x =
563 prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
566 (* this is like simplify but raises Success *)
567 let simplify_goal ~no_demod maxvar table bag g_actives clause =
569 if no_demod then bag, clause else demodulate bag clause table
571 if List.exists (Clauses.are_alpha_eq_cl clause) g_actives then None else
572 (debug (lazy (Pp.pp_clause clause));
573 if (is_goal_trivial clause)
574 then raise (Success (bag, maxvar, clause))
576 let (id,nlit,plit,vl,_) = clause in
577 if vl = [] then Some (bag,clause)
581 | [Terms.Equation(l,r,ty,_),_],[] -> l,r,ty
584 match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x)
585 table (Some(bag,maxvar,clause,Subst.id_subst)) with
586 | None -> Some (bag,clause)
587 | Some (bag,maxvar,cl,subst) ->
588 prerr_endline "Goal subsumed";
589 raise (Success (bag,maxvar,cl)))
591 else match is_subsumed ~unify:true bag maxvar clause table with
592 | None -> Some (bag, clause)
593 | Some ((bag,maxvar),c) ->
594 prerr_endline "Goal subsumed";
595 raise (Success (bag,maxvar,c))
599 let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
600 let simplify_goal ~no_demod maxvar table bag g_actives x =
601 prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
604 (* =================== inference ===================== *)
606 (* this is OK for both the sup_left and sup_right inference steps *)
607 let superposition table varlist subterm pos context =
608 let cands = IDX.DT.retrieve_unifiables table subterm in
610 (fun (dir, _, _, (id,nlit,plit,vl,_ (*as uc*))) ->
612 | [],[Terms.Equation (l,r,_,o),_] ->
613 (let side, newside = if dir=Terms.Left2Right then l,r else r,l in
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 ((*prerr_endline ("Filtering: " ^
627 Pp.pp_foterm side ^ " =(< || =)" ^
628 Pp.pp_foterm newside);*)None)
630 Some (context newside, subst, id, pos, dir)
631 with FoUnif.UnificationFailure _ -> None)
633 (IDX.ClauseSet.elements cands)
636 (* Superposes selected equation with equalities in table *)
637 let superposition_with_table bag maxvar (id,nlit,plit,vl,_) table =
639 | [],[Terms.Equation (l,r,ty,Terms.Lt),_] ->
640 fold_build_new_clause bag maxvar id Terms.Superposition
643 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
644 r (superposition table vl))
645 | [],[Terms.Equation (l,r,ty,Terms.Invertible),_]
646 | [],[Terms.Equation (l,r,ty,Terms.Gt),_] ->
647 fold_build_new_clause bag maxvar id Terms.Superposition
650 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
651 l (superposition table vl))
652 | [],[Terms.Equation (l,r,ty,Terms.Incomparable),_] ->
653 let filtering avoid subst = (* Riazanov: p.33 condition (iv) *)
654 let l = Subst.apply_subst subst l in
655 let r = Subst.apply_subst subst r in
656 let o = Order.compare_terms l r in
657 o <> avoid && o <> Terms.Eq
659 let bag, maxvar,r_terms =
660 fold_build_new_clause bag maxvar id Terms.Superposition
663 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
664 r (superposition table vl))
666 let bag, maxvar, l_terms =
667 fold_build_new_clause bag maxvar id Terms.Superposition
670 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
671 l (superposition table vl))
673 bag, maxvar, r_terms @ l_terms
677 (* the current equation is normal w.r.t. demodulation with atable
678 * (and is not the identity) *)
679 let infer_right bag maxvar current (alist,atable) =
680 (* We demodulate actives clause with current until all *
681 * active clauses are reduced w.r.t each other *)
682 (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
683 let ctable = IDX.index_clause IDX.DT.empty current in
684 (* let bag, (alist, atable) =
686 HExtlib.filter_map_acc (simplify ctable) bag alist
688 bag, (alist, List.fold_left IDX.index_clause IDX.DT.empty alist)
690 debug (lazy "Simplified active clauses with fact");
691 (* We superpose active clauses with current *)
692 let bag, maxvar, new_clauses =
694 (fun (bag, maxvar, acc) active ->
695 let bag, maxvar, newc =
696 superposition_with_table bag maxvar active ctable
698 bag, maxvar, newc @ acc)
699 (bag, maxvar, []) alist
701 debug (lazy "First superpositions");
702 (* We add current to active clauses so that it can be *
703 * superposed with itself *)
705 current :: alist, IDX.index_clause atable current
707 debug (lazy "Indexed");
708 let fresh_current, maxvar = Clauses.fresh_clause maxvar current in
709 (* We need to put fresh_current into the bag so that all *
710 * variables clauses refer to are known. *)
711 let bag, fresh_current = Terms.add_to_bag fresh_current bag in
712 (* We superpose current with active clauses *)
713 let bag, maxvar, additional_new_clauses =
714 superposition_with_table bag maxvar fresh_current atable
716 debug (lazy "Another superposition");
717 let new_clauses = new_clauses @ additional_new_clauses in
718 debug (lazy (Printf.sprintf "Demodulating %d clauses"
719 (List.length new_clauses)));
720 let bag, new_clauses =
721 HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
723 debug (lazy "Demodulated new clauses");
724 bag, maxvar, (alist, atable), new_clauses
727 let prof_ir = HExtlib.profile ~enable "infer_right";;
728 let infer_right bag maxvar current t =
729 prof_ir.HExtlib.profile (infer_right bag maxvar current) t
732 let infer_left bag maxvar goal (_alist, atable) =
733 (* We superpose the goal with active clauses *)
734 if (match goal with (_,_,_,[],_) -> true | _ -> false) then bag, maxvar, []
736 let bag, maxvar, new_goals =
737 superposition_with_table bag maxvar goal atable
739 debug (lazy "Superposed goal with active clauses");
740 (* We simplify the new goals with active clauses *)
744 match simplify_goal ~no_demod:false maxvar atable bag [] g with
745 | None -> assert false
746 | Some (bag,g) -> bag,g::acc)
749 debug (lazy "Simplified new goals with active clauses");
750 bag, maxvar, List.rev new_goals
753 let prof_il = HExtlib.profile ~enable "infer_left";;
754 let infer_left bag maxvar goal t =
755 prof_il.HExtlib.profile (infer_left bag maxvar goal) t