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,_) ->
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)
296 let nlit,_,bag,id = if nlit = [] then nlit,[],bag,id
298 (fun (pre,post,bag,id) (lit,sel) ->
300 demod_lit ~jump_to_right:false bag id lit (fun l -> pre@[l,sel]@post,plit)
302 if post=[] then pre@[(lit,sel)],[],bag,id
303 else pre@[(lit,sel)],List.tl post,bag,id)
304 ([],List.tl nlit, bag, id) nlit
306 let _,_,bag,id = if plit = [] then plit,[],bag,id
308 (fun (pre,post,bag,id) (lit,sel) ->
310 demod_lit ~jump_to_right:false bag id lit (fun l -> nlit,pre@[l,sel]@post)
312 if post=[] then pre@[(lit,sel)],[],bag,id
313 else pre@[(lit,sel)],List.tl post,bag,id)
314 ([],List.tl plit, bag, id) plit
316 let cl,_,_ = Terms.get_from_bag id bag in
320 let parallel_demod table vl clause_ctx bag t pos ctx id lit =
321 match demod table vl t with
322 | None -> (bag,t,id,lit)
323 | Some (newside, subst, id2, dir) ->
324 match build_clause ~fresh:false bag 0 (fun _ -> true)
325 Terms.Demodulation (ctx newside) subst id id2 pos dir clause_ctx
327 | None -> assert false
328 | Some (bag,_,(id,_,_,_,_),lit) ->
332 let demodulate_once ~jump_to_right bag id literal vl table clause_ctx =
334 | Terms.Predicate t -> assert false
335 | Terms.Equation (l,r,ty,_) as lit ->
336 let bag,l,id1,lit = if jump_to_right then (bag,l,id,lit) else
337 parallel_positions bag [2]
338 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id lit l
339 (parallel_demod table vl clause_ctx)
341 let jump_to_right = id1 = id in
343 parallel_positions bag [3]
344 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 lit r
345 (parallel_demod table vl clause_ctx)
347 if id = id2 then None
349 Some ((bag,id2,lit),jump_to_right)
352 let rec demodulate_old bag (id,nlit,plit,vl,proof) table =
353 let rec demod_lit ~jump_to_right bag id lit clause_ctx =
354 match demodulate_once ~jump_to_right bag id lit vl table clause_ctx with
355 | None -> bag, id, lit
356 | Some ((bag, id, lit),jump) ->
357 demod_lit ~jump_to_right:jump bag id lit clause_ctx
359 (*match nlit,plit with
361 let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> nlit, [l,true])
363 let cl,_,_ = Terms.get_from_bag id bag in
366 let bag, id, lit = demod_lit ~jump_to_right:false bag id lit (fun l -> [l,true],[])
368 let cl,_,_ = Terms.get_from_bag id bag in
371 let nlit,_,bag,id = if nlit = [] then nlit,[],bag,id
373 (fun (pre,post,bag,id) (lit,sel) ->
375 demod_lit ~jump_to_right:false bag id lit (fun l -> pre@[l,sel]@post,plit)
377 if post=[] then pre@[(lit,sel)],[],bag,id
378 else pre@[(lit,sel)],List.tl post,bag,id)
379 ([],List.tl nlit, bag, id) nlit
381 let _,_,bag,id = if plit = [] then plit,[],bag,id
383 (fun (pre,post,bag,id) (lit,sel) ->
385 demod_lit ~jump_to_right:false bag id lit (fun l -> nlit,pre@[l,sel]@post)
387 if post=[] then pre@[(lit,sel)],[],bag,id
388 else pre@[(lit,sel)],List.tl post,bag,id)
389 ([],List.tl plit, bag, id) plit
391 let cl,_,_ = Terms.get_from_bag id bag in
395 let prof_demodulate = HExtlib.profile ~enable "demodulate";;
396 let demodulate bag clause x =
397 prof_demodulate.HExtlib.profile (demodulate bag clause) x
401 let is_identity_clause ~unify = function
402 | _, [], [Terms.Equation (_,_,_,Terms.Eq),_], _, _ -> true
403 | _, [], [Terms.Equation (l,r,_,_),_], vl, _ when unify ->
404 (try ignore(Unif.unification (* vl *) [] l r); true
405 with FoUnif.UnificationFailure _ -> false)
409 let is_goal_trivial = function
410 | _, [Terms.Equation (_,_,_,Terms.Eq),_], [], _, _ -> true
411 | _, [Terms.Equation (l,r,_,_),_], [], vl, _ ->
412 (try ignore(Unif.unification (* vl *) [] l r); true
413 with FoUnif.UnificationFailure _ -> false)
416 let fold_build_new_clause bag maxvar id rule filter res clause_ctx =
417 debug (lazy (string_of_int (List.length res)));
418 let (bag, maxvar), res =
419 HExtlib.filter_map_acc
420 (fun (bag, maxvar) (t,subst,id2,pos,dir) ->
421 match build_clause ~fresh:true bag maxvar filter
422 rule t subst id id2 pos dir clause_ctx with
424 | Some (bag,maxvar,res,_) -> Some ((bag,maxvar),res))
430 (* Tries to rewrite an equality to identity, using unit equalities in table *)
431 let rewrite_eq ~unify l r ty vl table =
432 let retrieve = if unify then IDX.DT.retrieve_unifiables
433 else IDX.DT.retrieve_generalizations in
434 let lcands = retrieve table l in
435 let rcands = retrieve table r in
437 let id, dir, l, r, vl =
439 | (d,_,_, (id,[],[Terms.Equation (l,r,ty,_),_],vl,_))-> id, d, l, r, vl
440 | (d,_,_, (id,[Terms.Equation (l,r,ty,_),_],[],vl,_))-> id, d, l, r, vl
443 let reverse = (dir = Terms.Left2Right) = b in
444 let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
445 else r,l, Terms.Right2Left in
446 (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
448 let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
449 let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
450 let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
451 let locked_vars = if unify then [] else vl in
452 let rec aux = function
454 | (id2,dir,c,vl1)::tl ->
456 let subst = Unif.unification (* (vl@vl1) *) locked_vars c t in
457 Some (id2, dir, subst)
458 with FoUnif.UnificationFailure _ -> aux tl
460 aux (cands1 @ cands2)
463 let is_subsumed ~unify bag maxvar (id, nlit, plit, vl, _) table =
465 | [Terms.Equation (l,r,ty,_) ,_],[]
466 | [],[Terms.Equation (l,r,ty,_) ,_]->
467 (match rewrite_eq ~unify l r ty vl table with
469 | Some (id2, dir, subst) ->
470 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
471 build_clause ~fresh:true bag maxvar (fun _ -> true)
472 Terms.Superposition id_t subst id id2 [2] dir (fun l -> [],[l,true]))
473 | _ -> None (* TODO : implement subsumption for clauses *)
475 let prof_is_subsumed = HExtlib.profile ~enable "is_subsumed";;
476 let is_subsumed ~unify bag maxvar c x =
477 prof_is_subsumed.HExtlib.profile (is_subsumed ~unify bag maxvar c) x
479 (* id refers to a clause proving contextl l = contextr r *)
481 let rec deep_eq ~unify l r ty pos contextl contextr table acc =
484 | Some(bag,maxvar,(id,nlit,plit,vl,p as cl),subst) ->
485 let l = Subst.apply_subst subst l in
486 let r = Subst.apply_subst subst r in
488 let subst1 = Unif.unification (* vl *) [] l r in
491 | [Terms.Equation (l,r,ty,o),_],[] ->
492 Terms.Equation (FoSubst.apply_subst subst1 l,
493 FoSubst.apply_subst subst1 r, ty, o)
494 | _ -> debug (lazy (Pp.pp_clause cl));assert false
496 Some(bag,maxvar,(id,[lit,true],[],vl,p),Subst.concat subst1 subst)
497 with FoUnif.UnificationFailure _ ->
498 match rewrite_eq ~unify l r ty vl table with
499 | Some (id2, dir, subst1) ->
500 let newsubst = Subst.concat subst1 subst in
502 FoSubst.apply_subst newsubst
503 (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r])
506 build_clause ~fresh:true bag maxvar (fun _ -> true)
507 Terms.Superposition id_t
508 subst1 id id2 (pos@[2]) dir (fun l -> [l,true],[])
510 | Some (bag, maxvar, c, _) ->
511 Some(bag,maxvar,c,newsubst)
512 | None -> assert false)
515 | Terms.Node (a::la), Terms.Node (b::lb) when
516 a = b && List.length la = List.length lb ->
519 (fun (acc,pre,postl,postr) a b ->
521 fun x -> contextl(Terms.Node (pre@(x::postl))) in
523 fun x -> contextr(Terms.Node (pre@(x::postr))) in
524 let newpos = List.length pre::pos in
526 if l = [] then [] else List.tl l in
527 (deep_eq ~unify a b ty
528 newpos newcl newcr table acc,pre@[b],
529 footail postl, footail postr))
530 (acc,[a],List.tl la,List.tl lb) la lb
534 let prof_deep_eq = HExtlib.profile ~enable "deep_eq";;
535 let deep_eq ~unify l r ty pos contextl contextr table x =
536 prof_deep_eq.HExtlib.profile (deep_eq ~unify l r ty pos contextl contextr table) x
539 let rec orphan_murder bag acc i =
540 match Terms.get_from_bag i bag with
541 | (_,_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
542 | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
543 | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
544 if (List.mem i acc) then (false,acc)
545 else match orphan_murder bag acc i1 with
546 | (true,acc) -> (true,acc)
548 let (res,acc) = orphan_murder bag acc i2 in
549 if res then res,acc else res,i::acc
552 let orphan_murder bag actives cl =
553 let (id,_,_,_,_) = cl in
554 let actives = List.map (fun (i,_,_,_,_) -> i) actives in
555 let (res,_) = orphan_murder bag actives id in
556 if res then debug (lazy "Orphan murdered"); res
558 let prof_orphan_murder = HExtlib.profile ~enable "orphan_murder";;
559 let orphan_murder bag actives x =
560 prof_orphan_murder.HExtlib.profile (orphan_murder bag actives) x
563 (* demodulate and check for subsumption *)
564 let simplify table maxvar bag clause =
565 if is_identity_clause ~unify:false clause then bag,None
566 (* else if orphan_murder bag actives clause then bag,None *)
567 else let bag, clause = demodulate bag clause table in
568 if is_identity_clause ~unify:false clause then bag,None
570 match is_subsumed ~unify:false bag maxvar clause table with
571 | None -> bag, Some clause
572 | Some _ -> bag, None
575 let simplify table maxvar bag clause =
576 match simplify table maxvar bag clause with
578 let (id,_,_,_,_) = clause in
579 let (_,_,iter) = Terms.get_from_bag id bag in
580 Terms.replace_in_bag (clause,true,iter) bag, None
581 | bag, Some clause -> bag, Some clause
582 (*let (id,_,_,_) = clause in
583 if orphan_murder bag clause then
584 Terms.M.add id (clause,true) bag, Some clause
585 else bag, Some clause*)
587 let prof_simplify = HExtlib.profile ~enable "simplify";;
588 let simplify table maxvar bag x =
589 prof_simplify.HExtlib.profile (simplify table maxvar bag ) x
592 let one_pass_simplification new_clause (alist,atable) bag maxvar =
593 match simplify atable maxvar bag new_clause with
594 | bag,None -> bag,None (* new_clause has been discarded *)
595 | bag,(Some clause) ->
596 let ctable = IDX.index_clause IDX.DT.empty clause in
597 let bag, alist, atable =
599 (fun (bag, alist, atable) c ->
600 match simplify ctable maxvar bag c with
601 |bag,None -> (bag,alist,atable)
602 (* an active clause as been discarded *)
604 bag, c :: alist, IDX.index_clause atable c)
605 (bag,[],IDX.DT.empty) alist
607 bag, Some (clause, (alist,atable))
609 let prof_one_pass_simplification = HExtlib.profile ~enable "one_pass_simplification";;
610 let one_pass_simplification new_clause t bag x =
611 prof_one_pass_simplification.HExtlib.profile (one_pass_simplification new_clause t bag ) x
614 let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
616 if new_cl then atable else
617 IDX.index_clause atable cl
619 (* Simplification of new_clause with : *
620 * - actives and cl if new_clause is not cl *
621 * - only actives otherwise *)
623 simplify atable1 maxvar bag new_clause with
624 | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
626 (* Simplification of each active clause with clause *
627 * which is the simplified form of new_clause *)
628 let ctable = IDX.index_clause IDX.DT.empty clause in
629 let bag, newa, alist, atable =
631 (fun (bag, newa, alist, atable) c ->
632 match simplify ctable maxvar bag c with
633 |bag,None -> (bag, newa, alist, atable)
634 (* an active clause as been discarded *)
637 bag, newa, c :: alist,
638 IDX.index_clause atable c
640 bag, c1 :: newa, alist, atable)
641 (bag,[],[],IDX.DT.empty) alist
644 bag, (Some cl, Some (clause, (alist,atable), newa))
646 (* if new_clause is not cl, we simplify cl with clause *)
647 match simplify ctable maxvar bag cl with
649 (* cl has been discarded *)
650 bag,(None, Some (clause, (alist,atable), newa))
652 bag,(Some cl1, Some (clause, (alist,atable), newa))
654 let prof_simplification_step = HExtlib.profile ~enable "simplification_step";;
655 let simplification_step ~new_cl cl (alist,atable) bag maxvar x =
656 prof_simplification_step.HExtlib.profile (simplification_step ~new_cl cl (alist,atable) bag maxvar) x
659 let keep_simplified cl (alist,atable) bag maxvar =
660 let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
662 match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
663 | _,(None, _) -> assert false
664 | bag,(Some _, None) -> bag,None
665 | bag,(Some _, Some (clause, (alist,atable), newa)) ->
666 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
670 | [] -> bag, Some (cl, (alist,atable))
672 match simplification_step ~new_cl cl
673 (alist,atable) bag maxvar hd with
674 | _,(None,None) -> assert false
675 | bag,(Some _,None) ->
676 keep_simplified_aux ~new_cl cl (alist,atable) bag tl
677 | bag,(None, Some _) -> bag,None
678 | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
680 (clause::alist, IDX.index_clause atable clause)
682 keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
685 keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
687 let prof_keep_simplified = HExtlib.profile ~enable "keep_simplified";;
688 let keep_simplified cl t bag x =
689 prof_keep_simplified.HExtlib.profile (keep_simplified cl t bag) x
692 (* this is like simplify but raises Success *)
693 let simplify_goal ~no_demod maxvar table bag g_actives clause =
695 if no_demod then bag, clause else demodulate bag clause table
697 if List.exists (Clauses.are_alpha_eq_cl clause) g_actives then None else
698 if (is_goal_trivial clause)
699 then raise (Success (bag, maxvar, clause))
701 let (id,nlit,plit,vl,_) = clause in
702 if vl = [] then Some (bag,clause)
706 | [Terms.Equation(l,r,ty,_),_],[] -> l,r,ty
709 match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x)
710 table (Some(bag,maxvar,clause,Subst.id_subst)) with
711 | None -> Some (bag,clause)
712 | Some (bag,maxvar,cl,subst) ->
713 prerr_endline "Goal subsumed";
714 raise (Success (bag,maxvar,cl))
716 else match is_subsumed ~unify:true bag maxvar clause table with
717 | None -> Some (bag, clause)
718 | Some ((bag,maxvar),c) ->
719 prerr_endline "Goal subsumed";
720 raise (Success (bag,maxvar,c))
724 let prof_simplify_goal = HExtlib.profile ~enable "simplify_goal";;
725 let simplify_goal ~no_demod maxvar table bag g_actives x =
726 prof_simplify_goal.HExtlib.profile ( simplify_goal ~no_demod maxvar table bag g_actives) x
729 (* =================== inference ===================== *)
731 (* this is OK for both the sup_left and sup_right inference steps *)
732 let superposition table varlist is_pos subterm pos context =
733 let cands = IDX.DT.retrieve_unifiables table subterm in
735 (fun (dir, is_cand_pos, _, (id,nlit,plit,vl,_ (*as uc*))) ->
737 | [],[Terms.Equation (l,r,_,o),_] ->
738 (let side, newside = if dir=Terms.Left2Right then l,r else r,l in
739 debug (lazy (Pp.pp_foterm subterm));
740 debug (lazy (Pp.pp_foterm side));
743 Unif.unification (* (varlist@vl)*) [] subterm side
745 if o = Terms.Incomparable || o = Terms.Invertible then
746 let side = Subst.apply_subst subst side in
747 let newside = Subst.apply_subst subst newside in
748 let o = Order.compare_terms side newside in
749 (* XXX: check Riazanov p. 33 (iii) *)
750 if o <> Terms.Lt && o <> Terms.Eq then
751 Some (context newside, subst, id, pos, dir)
753 (debug (lazy "Filtering out..."); None)
755 Some (context newside, subst, id, pos, dir)
756 with FoUnif.UnificationFailure _ -> None)
758 (IDX.ClauseSet.elements cands)
761 (* Superposes selected equation with equalities in table *)
762 let superposition_with_table bag maxvar id vl lit is_pos clause_ctx table =
764 | Terms.Equation (l,r,ty,Terms.Lt) ->
765 fold_build_new_clause bag maxvar id Terms.Superposition
768 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
769 r (superposition table vl is_pos)) clause_ctx
770 | Terms.Equation (l,r,ty,Terms.Invertible)
771 | Terms.Equation (l,r,ty,Terms.Gt) ->
772 fold_build_new_clause bag maxvar id Terms.Superposition
775 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
776 l (superposition table vl is_pos)) clause_ctx
777 | Terms.Equation (l,r,ty,Terms.Incomparable) ->
778 let filtering avoid subst = (* Riazanov: p.33 condition (iv) *)
779 let l = Subst.apply_subst subst l in
780 let r = Subst.apply_subst subst r in
781 let o = Order.compare_terms l r in
782 o <> avoid && o <> Terms.Eq
784 let bag, maxvar,r_terms =
785 fold_build_new_clause bag maxvar id Terms.Superposition
788 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
789 r (superposition table vl is_pos)) clause_ctx
791 let bag, maxvar, l_terms =
792 fold_build_new_clause bag maxvar id Terms.Superposition
795 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
796 l (superposition table vl is_pos)) clause_ctx
798 bag, maxvar, r_terms @ l_terms
802 let superpose_literal id vl table is_pos (bag,maxvar,pre,post,acc) (lit,sel) =
804 if is_pos then fun l -> [],pre@[l,true]@post
805 else fun l -> pre@[l,true]@post,[]
807 let bag, maxvar, newc =
808 superposition_with_table bag maxvar id vl lit is_pos clause_ctx table
810 if post = [] then bag,maxvar,pre@[lit,sel],[],newc@acc
811 else bag,maxvar,pre@[lit,sel],List.tl post,newc@acc
815 (* the current equation is normal w.r.t. demodulation with atable
816 * (and is not the identity) *)
817 let infer_right bag maxvar current (alist,atable) =
818 (* We demodulate actives clause with current until all *
819 * active clauses are reduced w.r.t each other *)
820 (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
821 let ctable = IDX.index_clause IDX.DT.empty current in
822 (* let bag, (alist, atable) =
824 HExtlib.filter_map_acc (simplify ctable) bag alist
826 bag, (alist, List.fold_left IDX.index_clause IDX.DT.empty alist)
828 debug (lazy "Simplified active clauses with fact");
829 (* We superpose active clauses with current *)
830 let bag, maxvar, new_clauses =
832 (fun (bag, maxvar, acc) (id,nlit,plit,vl,_) ->
833 let bag, maxvar, _, _, acc =
834 if nlit = [] then bag,maxvar,[],[],acc
836 (superpose_literal id vl ctable false) (bag,maxvar,[],List.tl nlit,acc) nlit
838 let bag, maxvar, _, _, acc =
839 if plit = [] then bag,maxvar,[],[],acc
841 (superpose_literal id vl ctable true) (bag,maxvar,[],List.tl plit,acc) plit
844 (bag, maxvar, []) alist
846 debug (lazy "First superpositions");
847 (* We add current to active clauses so that it can be *
848 * superposed with itself *)
850 current :: alist, IDX.index_clause atable current
852 debug (lazy "Indexed");
853 let fresh_current, maxvar = Clauses.fresh_clause maxvar current in
854 (* We need to put fresh_current into the bag so that all *
855 * variables clauses refer to are known. *)
856 let bag, (id,nlit,plit,vl,_) = Terms.add_to_bag fresh_current bag in
857 (* We superpose current with active clauses *)
858 let bag, maxvar, _, _, additional_new_clauses =
859 if nlit = [] then bag,maxvar,[],[],[]
861 (superpose_literal id vl atable false) (bag,maxvar,[],List.tl nlit,[]) nlit
863 let bag, maxvar, _, _, additional_new_clauses =
864 if plit = [] then bag,maxvar,[],[],[]
866 (superpose_literal id vl atable true) (bag,maxvar,[],List.tl plit,additional_new_clauses) plit
868 debug (lazy "Another superposition");
869 let new_clauses = new_clauses@additional_new_clauses in
870 debug (lazy (Printf.sprintf "Demodulating %d clauses"
871 (List.length new_clauses)));
872 let bag, new_clauses =
873 HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
875 debug (lazy "Demodulated new clauses");
876 bag, maxvar, (alist, atable), new_clauses
879 let prof_ir = HExtlib.profile ~enable "infer_right";;
880 let infer_right bag maxvar current t =
881 prof_ir.HExtlib.profile (infer_right bag maxvar current) t
884 let infer_left bag maxvar goal (_alist, atable) =
885 (* We superpose the goal with active clauses *)
886 if (match goal with (_,_,_,[],_) -> true | _ -> false) then bag, maxvar, []
890 | (id,nlit,[],vl,_) -> id,nlit,vl
893 let bag, maxvar, _, _, new_goals =
894 List.fold_left (superpose_literal id vl atable false)
895 (bag,maxvar,[],List.tl nlit,[]) nlit
897 debug (lazy (string_of_int (List.length new_goals) ^ " new goals"));
898 (* We simplify the new goals with active clauses *)
902 match simplify_goal ~no_demod:false maxvar atable bag [] g with
903 | None -> assert false
904 | Some (bag,g) -> bag,g::acc)
907 debug (lazy "Simplified new goals with active clauses");
908 bag, maxvar, List.rev new_goals
911 let prof_il = HExtlib.profile ~enable "infer_left";;
912 let infer_left bag maxvar goal t =
913 prof_il.HExtlib.profile (infer_left bag maxvar goal) t