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 visit bag pos ctx id lit t f =
106 let rec aux bag pos ctx id subst lit = function
107 | Terms.Leaf _ as t ->
108 let bag,subst,t,id,lit = f bag t pos ctx id lit
109 in assert (subst=[]); bag,t,id,lit
110 | Terms.Var i as t ->
111 let t= Subst.apply_subst subst t in
113 | Terms.Node (hd::l) ->
114 let bag, l, _, id,lit =
116 (fun (bag,pre,post,id,lit) t ->
117 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
118 let newpos = (List.length pre)::pos in
119 let bag,newt,id,lit = aux bag newpos newctx id subst lit t in
120 if post = [] then bag, pre@[newt], [], id, lit
121 else bag, pre @ [newt], List.tl post, id, lit)
122 (bag, [hd], List.map (Subst.apply_subst subst) (List.tl l), id, lit) l
124 let bag,subst,t,id1,lit = f bag (Terms.Node l) pos ctx id lit
126 if id1 = id then (assert (subst=[]); bag,t,id,lit)
127 else aux bag pos ctx id1 subst lit t
130 aux bag pos ctx id [] lit t
133 let build_clause ~fresh bag maxvar filter rule t subst id id2 pos dir clause_ctx =
134 let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
135 let t = Subst.apply_subst subst t in
139 | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
140 let o = Order.compare_terms l r in
141 Terms.Equation (l, r, ty, o)
142 | t -> Terms.Predicate t
144 let nlit,plit = clause_ctx literal in
145 let cl = (0, nlit, plit, [], proof) in
146 let vl = Clauses.vars_of_clause cl in
148 if fresh then Clauses.fresh_clause ~subst maxvar (0, nlit, plit, vl, proof)
149 else (0,nlit,plit,vl,proof),maxvar
152 Terms.add_to_bag cl bag
154 debug (lazy (Pp.pp_clause cl));
155 Some (bag, maxvar, cl, literal)
157 ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
159 let prof_build_clause = HExtlib.profile ~enable "build_clause";;
160 let build_clause ~fresh bag maxvar filter rule t subst id id2 pos x =
161 prof_build_clause.HExtlib.profile
162 (build_clause ~fresh bag maxvar filter rule t subst id id2 pos) x
166 (* ============ simplification ================= *)
167 let prof_demod_u = HExtlib.profile ~enable "demod.unify";;
168 let prof_demod_r = HExtlib.profile ~enable "demod.retrieve_generalizations";;
169 let prof_demod_o = HExtlib.profile ~enable "demod.compare_terms";;
170 let prof_demod_s = HExtlib.profile ~enable "demod.apply_subst";;
172 let demod table varlist subterm =
174 prof_demod_r.HExtlib.profile
175 (IDX.DT.retrieve_generalizations table) subterm
178 (fun (dir, is_pos, pos, (id,nlit,plit,vl,_)) ->
182 | Terms.Predicate _ -> assert false
183 | Terms.Equation (l,r,_,o) ->
184 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
187 prof_demod_u.HExtlib.profile
188 (Unif.unification (* (varlist@vl) *) varlist subterm) side
191 prof_demod_s.HExtlib.profile
192 (Subst.apply_subst subst) side
195 prof_demod_s.HExtlib.profile
196 (Subst.apply_subst subst) newside
198 if o = Terms.Incomparable || o = Terms.Invertible then
200 prof_demod_o.HExtlib.profile
201 (Order.compare_terms newside) side in
202 (* Riazanov, pp. 45 (ii) *)
204 Some (newside, subst, id, dir)
206 ((*prerr_endline ("Filtering: " ^
207 Pp.pp_foterm side ^ " =(< || =)" ^
208 Pp.pp_foterm newside ^ " coming from " ^
209 Pp.pp_clause uc );*)None)
211 Some (newside, subst, id, dir)
212 with FoUnif.UnificationFailure _ -> None)
214 (IDX.ClauseSet.elements cands)
216 let prof_demod = HExtlib.profile ~enable "demod";;
217 let demod table varlist x =
218 prof_demod.HExtlib.profile (demod table varlist) x
221 let mydemod table varlist subterm =
223 prof_demod_r.HExtlib.profile
224 (IDX.DT.retrieve_generalizations table) subterm
227 (fun (dir, is_pos, pos, (id,nlit,plit,vl,_)) ->
228 match (nlit,plit) with
231 | Terms.Predicate _ -> assert false
232 | Terms.Equation (l,r,_,o) ->
233 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
236 prof_demod_u.HExtlib.profile
237 (Unif.unification (* (varlist@vl) *) varlist subterm) side
240 prof_demod_s.HExtlib.profile
241 (Subst.apply_subst subst) side
244 prof_demod_s.HExtlib.profile
245 (Subst.apply_subst subst) newside
247 if o = Terms.Incomparable || o = Terms.Invertible then
249 prof_demod_o.HExtlib.profile
250 (Order.compare_terms inewside) iside in
251 (* Riazanov, pp. 45 (ii) *)
253 Some (newside, subst, id, dir)
255 ((*prerr_endline ("Filtering: " ^
256 Pp.pp_foterm side ^ " =(< || =)" ^
257 Pp.pp_foterm newside ^ " coming from " ^
258 Pp.pp_unit_clause uc );*)None)
260 Some (newside, subst, id, dir)
261 with FoUnif.UnificationFailure _ -> None)
263 (IDX.ClauseSet.elements cands)
266 let ctx_demod table vl clause_ctx bag t pos ctx id lit =
267 match mydemod table vl t with
268 | None -> (bag,[],t,id,lit)
269 | Some (newside, subst, id2, dir) ->
270 let inewside = Subst.apply_subst subst newside in
271 match build_clause ~fresh:false bag 0 (fun _ -> true)
272 Terms.Demodulation (ctx inewside) subst id id2 pos dir clause_ctx
274 | None -> assert false
275 | Some (bag,_,(id,_,_,_,_),lit) ->
276 (bag,subst,newside,id,lit)
279 let rec demodulate bag (id,nlit,plit,vl,proof) table =
280 let rec demod_lit ~jump_to_right bag id lit clause_ctx =
282 | Terms.Predicate t -> assert false
283 | Terms.Equation (l,r,ty,_) -> assert false
284 (*let bag,l,id1,lit =
286 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id lit l
287 (ctx_demod table vl clause_ctx)
291 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 lit r
292 (ctx_demod table vl clause_ctx)
294 let cl,_,_ = Terms.get_from_bag id2 bag in
297 let nlit,_,bag,id = if nlit = [] then nlit,[],bag,id
299 (fun (pre,post,bag,id) (lit,sel) ->
301 demod_lit ~jump_to_right:false bag id lit (fun l -> pre@[l,sel]@post,plit)
303 if post=[] then pre@[(lit,sel)],[],bag,id
304 else pre@[(lit,sel)],List.tl post,bag,id)
305 ([],List.tl nlit, bag, id) nlit
307 let _,_,bag,id = if plit = [] then plit,[],bag,id
309 (fun (pre,post,bag,id) (lit,sel) ->
311 demod_lit ~jump_to_right:false bag id lit (fun l -> nlit,pre@[l,sel]@post)
313 if post=[] then pre@[(lit,sel)],[],bag,id
314 else pre@[(lit,sel)],List.tl post,bag,id)
315 ([],List.tl plit, bag, id) plit
317 let cl,_,_ = Terms.get_from_bag id bag in
321 let parallel_demod table vl clause_ctx bag t pos ctx id lit =
322 match demod table vl t with
323 | None -> (bag,t,id,lit)
324 | Some (newside, subst, id2, dir) ->
325 match build_clause ~fresh:false bag 0 (fun _ -> true)
326 Terms.Demodulation (ctx newside) subst id id2 pos dir clause_ctx
328 | None -> assert false
329 | Some (bag,_,(id,_,_,_,_),lit) ->
333 let demodulate_once ~jump_to_right bag id literal vl table clause_ctx =
335 | Terms.Predicate t -> assert false
336 | Terms.Equation (l,r,ty,_) as lit ->
337 let bag,l,id1,lit = if jump_to_right then (bag,l,id,lit) else
338 parallel_positions bag [2]
339 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id lit l
340 (parallel_demod table vl clause_ctx)
342 let jump_to_right = id1 = id in
344 parallel_positions bag [3]
345 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 lit r
346 (parallel_demod table vl clause_ctx)
348 if id = id2 then None
350 Some ((bag,id2,lit),jump_to_right)
353 let rec demodulate_old bag (id,nlit,plit,vl,proof) table =
354 let rec demod_lit ~jump_to_right bag id lit clause_ctx =
355 match demodulate_once ~jump_to_right bag id lit vl table clause_ctx with
356 | None -> bag, id, lit
357 | Some ((bag, id, lit),jump) ->
358 demod_lit ~jump_to_right:jump bag id lit clause_ctx
360 (*match nlit,plit with
362 let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> nlit, [l,true])
364 let cl,_,_ = Terms.get_from_bag id bag in
367 let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> [l,true],[])
369 let cl,_,_ = Terms.get_from_bag id bag in
372 let nlit,_,bag,id = if nlit = [] then nlit,[],bag,id
374 (fun (pre,post,bag,id) (lit,sel) ->
376 demod_lit ~jump_to_right:false bag id lit (fun l -> pre@[l,sel]@post,plit)
378 if post=[] then pre@[(lit,sel)],[],bag,id
379 else pre@[(lit,sel)],List.tl post,bag,id)
380 ([],List.tl nlit, bag, id) nlit
382 let _,_,bag,id = if plit = [] then plit,[],bag,id
384 (fun (pre,post,bag,id) (lit,sel) ->
386 demod_lit ~jump_to_right:false bag id lit (fun l -> nlit,pre@[l,sel]@post)
388 if post=[] then pre@[(lit,sel)],[],bag,id
389 else pre@[(lit,sel)],List.tl post,bag,id)
390 ([],List.tl plit, bag, id) plit
392 let cl,_,_ = Terms.get_from_bag id bag in
396 let prof_demodulate = HExtlib.profile ~enable "demodulate";;
397 let demodulate bag clause x =
398 prof_demodulate.HExtlib.profile (demodulate bag clause) x
402 let is_identity_clause ~unify = function
403 | _, [], [Terms.Equation (_,_,_,Terms.Eq),_], _, _ -> true
404 | _, [], [Terms.Equation (l,r,_,_),_], vl, _ when unify ->
405 (try ignore(Unif.unification (* vl *) [] l r); true
406 with FoUnif.UnificationFailure _ -> false)
410 let is_goal_trivial = function
411 | _, [Terms.Equation (_,_,_,Terms.Eq),_], [], _, _ -> true
412 | _, [Terms.Equation (l,r,_,_),_], [], vl, _ ->
413 (try ignore(Unif.unification (* vl *) [] l r); true
414 with FoUnif.UnificationFailure _ -> false)
417 let fold_build_new_clause bag maxvar id rule filter res clause_ctx =
418 debug (lazy (string_of_int (List.length res)));
419 let (bag, maxvar), res =
420 HExtlib.filter_map_acc
421 (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
422 match build_clause ~fresh:true bag maxvar filter
423 rule t subst id id2 pos dir clause_ctx with
425 | Some (bag,maxvar,res,_) -> Some ((bag,maxvar),res))
431 (* Tries to rewrite an equality to identity, using unit equalities in table *)
432 let rewrite_eq ~unify l r ty vl table =
433 let retrieve = if unify then IDX.DT.retrieve_unifiables
434 else IDX.DT.retrieve_generalizations in
435 let lcands = retrieve table l in
436 let rcands = retrieve table r in
438 let id, dir, l, r, vl =
440 | (d,_,_, (id,[],[Terms.Equation (l,r,ty,_),_],vl,_))-> id, d, l, r, vl
441 | (d,_,_, (id,[Terms.Equation (l,r,ty,_),_],[],vl,_))-> id, d, l, r, vl
444 let reverse = (dir = Terms.Left2Right) = b in
445 let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
446 else r,l, Terms.Right2Left in
447 (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
449 let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
450 let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
451 let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
452 let locked_vars = if unify then [] else vl in
453 let rec aux = function
455 | (id2,dir,c,vl1)::tl ->
457 let subst = Unif.unification (* (vl@vl1) *) locked_vars c t in
458 Some (id2, dir, subst)
459 with FoUnif.UnificationFailure _ -> aux tl
461 aux (cands1 @ cands2)
464 let is_subsumed ~unify bag maxvar (id, nlit, plit, vl, _) table =
466 | [Terms.Equation (l,r,ty,_) ,_],[]
467 | [],[Terms.Equation (l,r,ty,_) ,_]->
468 (match rewrite_eq ~unify l r ty vl table with
470 | Some (id2, dir, subst) ->
471 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
472 build_clause ~fresh:true bag maxvar (fun _ -> true)
473 Terms.Superposition id_t subst id id2 [2] dir (fun l -> [],[l,true]))
474 | _ -> None (* TODO : implement subsumption for clauses *)
476 let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
477 let is_subsumed ~unify bag maxvar c x =
478 prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
480 (* id refers to a clause proving contextl l = contextr r *)
482 let rec deep_eq ~unify l r ty pos contextl contextr table acc =
485 | Some(bag,maxvar,(id,nlit,plit,vl,p as cl),subst) ->
486 let l = Subst.apply_subst subst l in
487 let r = Subst.apply_subst subst r in
489 let subst1 = Unif.unification (* vl *) [] l r in
492 | [Terms.Equation (l,r,ty,o),_],[] ->
493 Terms.Equation (FoSubst.apply_subst subst1 l,
494 FoSubst.apply_subst subst1 r, ty, o)
495 | _ -> debug (lazy (Pp.pp_clause cl));assert false
497 Some(bag,maxvar,(id,[lit,true],[],vl,p),Subst.concat subst1 subst)
498 with FoUnif.UnificationFailure _ ->
499 match rewrite_eq ~unify l r ty vl table with
500 | Some (id2, dir, subst1) ->
501 let newsubst = Subst.concat subst1 subst in
503 FoSubst.apply_subst newsubst
504 (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r])
507 build_clause ~fresh:true bag maxvar (fun _ -> true)
508 Terms.Superposition id_t
509 subst1 id id2 (pos@[2]) dir (fun l -> [l,true],[])
511 | Some (bag, maxvar, c, _) ->
512 Some(bag,maxvar,c,newsubst)
513 | None -> assert false)
516 | Terms.Node (a::la), Terms.Node (b::lb) when
517 a = b && List.length la = List.length lb ->
520 (fun (acc,pre,postl,postr) a b ->
522 fun x -> contextl(Terms.Node (pre@(x::postl))) in
524 fun x -> contextr(Terms.Node (pre@(x::postr))) in
525 let newpos = List.length pre::pos in
527 if l = [] then [] else List.tl l in
528 (deep_eq ~unify a b ty
529 newpos newcl newcr table acc,pre@[b],
530 footail postl, footail postr))
531 (acc,[a],List.tl la,List.tl lb) la lb
535 let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
536 let deep_eq ~unify l r ty pos contextl contextr table x =
537 prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
540 let rec orphan_murder bag acc i =
541 match Terms.get_from_bag i bag with
542 | (_,_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
543 | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
544 | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
545 if (List.mem i acc) then (false,acc)
546 else match orphan_murder bag acc i1 with
547 | (true,acc) -> (true,acc)
549 let (res,acc) = orphan_murder bag acc i2 in
550 if res then res,acc else res,i::acc
553 let orphan_murder bag actives cl =
554 let (id,_,_,_,_) = cl in
555 let actives = List.map (fun (i,_,_,_,_) -> i) actives in
556 let (res,_) = orphan_murder bag actives id in
557 if res then debug (lazy "Orphan murdered"); res
559 let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
560 let orphan_murder bag actives x =
561 prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
564 (* demodulate and check for subsumption *)
565 let simplify table maxvar bag clause =
566 if is_identity_clause ~unify:false clause then bag,None
567 (* else if orphan_murder bag actives clause then bag,None *)
568 else let bag, clause = demodulate bag clause table in
569 if is_identity_clause ~unify:false clause then bag,None
571 match is_subsumed ~unify:false bag maxvar clause table with
572 | None -> bag, Some clause
573 | Some _ -> bag, None
576 let simplify table maxvar bag clause =
577 match simplify table maxvar bag clause with
579 let (id,_,_,_,_) = clause in
580 let (_,_,iter) = Terms.get_from_bag id bag in
581 Terms.replace_in_bag (clause,true,iter) bag, None
582 | bag, Some clause -> bag, Some clause
583 (*let (id,_,_,_) = clause in
584 if orphan_murder bag clause then
585 Terms.M.add id (clause,true) bag, Some clause
586 else bag, Some clause*)
588 let prof_simplify = HExtlib.profile ~enable "simplify";;
589 let simplify table maxvar bag x =
590 prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
593 let one_pass_simplification new_clause (alist,atable) bag maxvar =
594 match simplify atable maxvar bag new_clause with
595 | bag,None -> bag,None (* new_clause has been discarded *)
596 | bag,(Some clause) ->
597 let ctable = IDX.index_clause IDX.DT.empty clause in
598 let bag, alist, atable =
600 (fun (bag, alist, atable) c ->
601 match simplify ctable maxvar bag c with
602 |bag,None -> (bag,alist,atable)
603 (* an active clause as been discarded *)
605 bag, c :: alist, IDX.index_clause atable c)
606 (bag,[],IDX.DT.empty) alist
608 bag, Some (clause, (alist,atable))
610 let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
611 let one_pass_simplification new_clause t bag x =
612 prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
615 let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
617 if new_cl then atable else
618 IDX.index_clause atable cl
620 (* Simplification of new_clause with : *
621 * - actives and cl if new_clause is not cl *
622 * - only actives otherwise *)
624 simplify atable1 maxvar bag new_clause with
625 | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
627 (* Simplification of each active clause with clause *
628 * which is the simplified form of new_clause *)
629 let ctable = IDX.index_clause IDX.DT.empty clause in
630 let bag, newa, alist, atable =
632 (fun (bag, newa, alist, atable) c ->
633 match simplify ctable maxvar bag c with
634 |bag,None -> (bag, newa, alist, atable)
635 (* an active clause as been discarded *)
638 bag, newa, c :: alist,
639 IDX.index_clause atable c
641 bag, c1 :: newa, alist, atable)
642 (bag,[],[],IDX.DT.empty) alist
645 bag, (Some cl, Some (clause, (alist,atable), newa))
647 (* if new_clause is not cl, we simplify cl with clause *)
648 match simplify ctable maxvar bag cl with
650 (* cl has been discarded *)
651 bag,(None, Some (clause, (alist,atable), newa))
653 bag,(Some cl1, Some (clause, (alist,atable), newa))
655 let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
656 let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
657 prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
660 let keep_simplified cl (alist,atable) bag maxvar =
661 let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
663 match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
664 | _,(None, _) -> assert false
665 | bag,(Some _, None) -> bag,None
666 | bag,(Some _, Some (clause, (alist,atable), newa)) ->
667 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
671 | [] -> bag, Some (cl, (alist,atable))
673 match simplification_step ~new_cl cl
674 (alist,atable) bag maxvar hd with
675 | _,(None,None) -> assert false
676 | bag,(Some _,None) ->
677 keep_simplified_aux ~new_cl cl (alist,atable) bag tl
678 | bag,(None, Some _) -> bag,None
679 | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
681 (clause::alist, IDX.index_clause atable clause)
683 keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
686 keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
688 let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
689 let keep_simplified cl t bag x =
690 prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
693 (* this is like simplify but raises Success *)
694 let simplify_goal ~no_demod maxvar table bag g_actives clause =
696 if no_demod then bag, clause else demodulate bag clause table
698 if List.exists (Clauses.are_alpha_eq_cl clause) g_actives then None else
699 if (is_goal_trivial clause)
700 then raise (Success (bag, maxvar, clause))
702 let (id,nlit,plit,vl,_) = clause in
703 if vl = [] then Some (bag,clause)
707 | [Terms.Equation(l,r,ty,_),_],[] -> l,r,ty
710 match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x)
711 table (Some(bag,maxvar,clause,Subst.id_subst)) with
712 | None -> Some (bag,clause)
713 | Some (bag,maxvar,cl,subst) ->
714 prerr_endline "Goal subsumed";
715 raise (Success (bag,maxvar,cl))
717 else match is_subsumed ~unify:true bag maxvar clause table with
718 | None -> Some (bag, clause)
719 | Some ((bag,maxvar),c) ->
720 prerr_endline "Goal subsumed";
721 raise (Success (bag,maxvar,c))
725 let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
726 let simplify_goal ~no_demod maxvar table bag g_actives x =
727 prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
730 (* =================== inference ===================== *)
732 (* this is OK for both the sup_left and sup_right inference steps *)
733 let superposition table varlist is_pos subterm pos context =
734 let cands = IDX.DT.retrieve_unifiables table subterm in
736 (fun (dir, is_cand_pos, _, (id,nlit,plit,vl,_ (*as uc*))) ->
738 | [],[Terms.Equation (l,r,_,o),_] ->
739 (let side, newside = if dir=Terms.Left2Right then l,r else r,l in
740 debug (lazy (Pp.pp_foterm subterm));
741 debug (lazy (Pp.pp_foterm side));
744 Unif.unification (* (varlist@vl)*) [] subterm side
746 if o = Terms.Incomparable || o = Terms.Invertible then
747 let side = Subst.apply_subst subst side in
748 let newside = Subst.apply_subst subst newside in
749 let o = Order.compare_terms side newside in
750 (* XXX: check Riazanov p. 33 (iii) *)
751 if o <> Terms.Lt && o <> Terms.Eq then
752 Some (context newside, subst, id, pos, dir)
754 (debug (lazy "Filtering out..."); None)
756 Some (context newside, subst, id, pos, dir)
757 with FoUnif.UnificationFailure _ -> None)
759 (IDX.ClauseSet.elements cands)
762 (* Superposes selected equation with equalities in table *)
763 let superposition_with_table bag maxvar id vl lit is_pos clause_ctx table =
765 | Terms.Equation (l,r,ty,Terms.Lt) ->
766 fold_build_new_clause bag maxvar id Terms.Superposition
769 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
770 r (superposition table vl is_pos)) clause_ctx
771 | Terms.Equation (l,r,ty,Terms.Invertible)
772 | Terms.Equation (l,r,ty,Terms.Gt) ->
773 fold_build_new_clause bag maxvar id Terms.Superposition
776 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
777 l (superposition table vl is_pos)) clause_ctx
778 | Terms.Equation (l,r,ty,Terms.Incomparable) ->
779 let filtering avoid subst = (* Riazanov: p.33 condition (iv) *)
780 let l = Subst.apply_subst subst l in
781 let r = Subst.apply_subst subst r in
782 let o = Order.compare_terms l r in
783 o <> avoid && o <> Terms.Eq
785 let bag, maxvar,r_terms =
786 fold_build_new_clause bag maxvar id Terms.Superposition
789 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
790 r (superposition table vl is_pos)) clause_ctx
792 let bag, maxvar, l_terms =
793 fold_build_new_clause bag maxvar id Terms.Superposition
796 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
797 l (superposition table vl is_pos)) clause_ctx
799 bag, maxvar, r_terms @ l_terms
803 let superpose_literal id vl table is_pos (bag,maxvar,pre,post,acc) (lit,sel) =
805 if is_pos then fun l -> [],pre@[l,true]@post
806 else fun l -> pre@[l,true]@post,[]
808 let bag, maxvar, newc =
809 superposition_with_table bag maxvar id vl lit is_pos clause_ctx table
811 if post = [] then bag,maxvar,pre@[lit,sel],[],newc@acc
812 else bag,maxvar,pre@[lit,sel],List.tl post,newc@acc
816 (* the current equation is normal w.r.t. demodulation with atable
817 * (and is not the identity) *)
818 let infer_right bag maxvar current (alist,atable) =
819 (* We demodulate actives clause with current until all *
820 * active clauses are reduced w.r.t each other *)
821 (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
822 let ctable = IDX.index_clause IDX.DT.empty current in
823 (* let bag, (alist, atable) =
825 HExtlib.filter_map_acc (simplify ctable) bag alist
827 bag, (alist, List.fold_left IDX.index_clause IDX.DT.empty alist)
829 debug (lazy "Simplified active clauses with fact");
830 (* We superpose active clauses with current *)
831 let bag, maxvar, new_clauses =
833 (fun (bag, maxvar, acc) (id,nlit,plit,vl,_) ->
834 let bag, maxvar, _, _, acc =
835 if nlit = [] then bag,maxvar,[],[],acc
837 (superpose_literal id vl ctable false) (bag,maxvar,[],List.tl nlit,acc) nlit
839 let bag, maxvar, _, _, acc =
840 if plit = [] then bag,maxvar,[],[],acc
842 (superpose_literal id vl ctable true) (bag,maxvar,[],List.tl plit,acc) plit
845 (bag, maxvar, []) alist
847 debug (lazy "First superpositions");
848 (* We add current to active clauses so that it can be *
849 * superposed with itself *)
851 current :: alist, IDX.index_clause atable current
853 debug (lazy "Indexed");
854 let fresh_current, maxvar = Clauses.fresh_clause maxvar current in
855 (* We need to put fresh_current into the bag so that all *
856 * variables clauses refer to are known. *)
857 let bag, (id,nlit,plit,vl,_) = Terms.add_to_bag fresh_current bag in
858 (* We superpose current with active clauses *)
859 let bag, maxvar, _, _, additional_new_clauses =
860 if nlit = [] then bag,maxvar,[],[],[]
862 (superpose_literal id vl atable false) (bag,maxvar,[],List.tl nlit,[]) nlit
864 let bag, maxvar, _, _, additional_new_clauses =
865 if plit = [] then bag,maxvar,[],[],[]
867 (superpose_literal id vl atable true) (bag,maxvar,[],List.tl plit,additional_new_clauses) plit
869 debug (lazy "Another superposition");
870 let new_clauses = new_clauses@additional_new_clauses in
871 debug (lazy (Printf.sprintf "Demodulating %d clauses"
872 (List.length new_clauses)));
873 let bag, new_clauses =
874 HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
876 debug (lazy "Demodulated new clauses");
877 bag, maxvar, (alist, atable), new_clauses
880 let prof_ir = HExtlib.profile ~enable "infer_right";;
881 let infer_right bag maxvar current t =
882 prof_ir.HExtlib.profile (infer_right bag maxvar current) t
885 let infer_left bag maxvar goal (_alist, atable) =
886 (* We superpose the goal with active clauses *)
887 if (match goal with (_,_,_,[],_) -> true | _ -> false) then bag, maxvar, []
891 | (id,nlit,[],vl,_) -> id,nlit,vl
894 let bag, maxvar, _, _, new_goals =
895 List.fold_left (superpose_literal id vl atable false)
896 (bag,maxvar,[],List.tl nlit,[]) nlit
898 debug (lazy (string_of_int (List.length new_goals) ^ " new goals"));
899 (* We simplify the new goals with active clauses *)
903 match simplify_goal ~no_demod:false maxvar atable bag [] g with
904 | None -> assert false
905 | Some (bag,g) -> bag,g::acc)
908 debug (lazy "Simplified new goals with active clauses");
909 bag, maxvar, List.rev new_goals
912 let prof_il = HExtlib.profile ~enable "infer_left";;
913 let infer_left bag maxvar goal t =
914 prof_il.HExtlib.profile (infer_left bag maxvar goal) t