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 : Terms.Blob) =
16 module IDX = Index.Index(B)
17 module Unif = FoUnif.Founif(B)
18 module Subst = FoSubst
19 module Order = Orderings.Orderings(B)
20 module Utils = FoUtils.Utils(B)
23 exception Success of B.t Terms.bag * int * B.t Terms.unit_clause
25 let debug s = prerr_endline s;;
28 let rec list_first f = function
30 | x::tl -> match f x with Some _ as x -> x | _ -> list_first f tl
33 let first_position pos ctx t f =
34 let inject_pos pos ctx = function
36 | Some (a,b,c,d,e) -> Some(ctx a,b,c,d,e,pos)
38 let rec aux pos ctx = function
39 | Terms.Leaf _ as t -> inject_pos pos ctx (f t)
43 | Some _ as x -> inject_pos pos ctx x
45 let rec first pre post = function
48 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
49 match aux (List.length pre :: pos) newctx t with
52 if post = [] then None (* tl is also empty *)
53 else first (pre @ [t]) (List.tl post) tl
55 first [] (List.tl l) l
60 let all_positions pos ctx t f =
61 let rec aux pos ctx = function
62 | Terms.Leaf _ as t -> f t pos ctx
67 (fun (acc,pre,post) t -> (* Invariant: pre @ [t] @ post = l *)
68 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
69 let acc = aux (List.length pre :: pos) newctx t @ acc in
70 if post = [] then acc, l, []
71 else acc, pre @ [t], List.tl post)
72 (f t pos ctx, [], List.tl l) l
79 let parallel_positions bag pos ctx id t f =
80 let rec aux bag pos ctx id = function
81 | Terms.Leaf _ as t -> f bag t pos ctx id
82 | Terms.Var _ as t -> bag,t,id
84 let bag,t,id1 = f bag t pos ctx id in
88 (fun (bag,pre,post,id) t ->
89 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
90 let newpos = (List.length pre)::pos in
91 let bag,newt,id = aux bag newpos newctx id t in
92 if post = [] then bag, pre@[newt], [], id
93 else bag, pre @ [newt], List.tl post, id)
94 (bag, [], List.tl l, id) l
102 let build_clause bag filter rule t subst vl id id2 pos dir =
103 let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
104 let t = Subst.apply_subst subst t in
108 | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
109 let o = Order.compare_terms l r in
110 Terms.Equation (l, r, ty, o)
111 | t -> Terms.Predicate t
114 Terms.add_to_bag (0, literal, Terms.vars_of_term t, proof) bag
118 ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
122 (* ============ simplification ================= *)
124 let demod table varlist subterm =
125 let cands = IDX.DT.retrieve_generalizations table subterm in
127 (fun (dir, (id,lit,vl,_)) ->
129 | Terms.Predicate _ -> assert false
130 | Terms.Equation (l,r,_,o) ->
131 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
134 Unif.unification (varlist@vl) varlist subterm side
136 let side = Subst.apply_subst subst side in
137 let newside = Subst.apply_subst subst newside in
138 if o = Terms.Incomparable then
139 let o = Order.compare_terms newside side in
140 (* Riazanov, pp. 45 (ii) *)
142 Some (newside, subst, varlist, id, dir)
144 ((*prerr_endline ("Filtering: " ^
145 Pp.pp_foterm side ^ " =(< || =)" ^
146 Pp.pp_foterm newside ^ " coming from " ^
147 Pp.pp_unit_clause uc );*)None)
149 Some (newside, subst, varlist, id, dir)
150 with FoUnif.UnificationFailure _ -> None)
151 (IDX.ClauseSet.elements cands)
154 let demodulate_once_old ~jump_to_right bag (id, literal, vl, pr) table =
156 | Terms.Predicate t -> assert false
157 | Terms.Equation (l,r,ty,_) ->
158 let left_position = if jump_to_right then None else
160 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) l
163 match left_position with
164 | Some (newt, subst, varlist, id2, dir, pos) ->
166 match build_clause bag (fun _ -> true) Terms.Demodulation
167 newt subst varlist id id2 pos dir
169 | None -> assert false
170 | Some x -> Some (x,false)
174 [3] (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) r
178 | Some (newt, subst, varlist, id2, dir, pos) ->
179 match build_clause bag (fun _ -> true)
180 Terms.Demodulation newt subst varlist id id2 pos dir
182 | None -> assert false
183 | Some x -> Some (x,true)
186 let parallel_demod table vl bag t pos ctx id =
187 match demod table vl t with
189 | Some (newside, subst, vl, id2, dir) ->
190 match build_clause bag (fun _ -> true)
191 Terms.Demodulation (ctx newside) subst vl id id2 pos dir
193 | None -> assert false
194 | Some (bag,(id,_,_,_)) ->
198 let demodulate_once ~jump_to_right bag (id, literal, vl, pr) table =
200 | Terms.Predicate t -> assert false
201 | Terms.Equation (l,r,ty,_) ->
202 let bag,l,id1 = if jump_to_right then (bag,l,id) else
203 parallel_positions bag [2]
204 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ]) id l
205 (parallel_demod table vl)
207 let jump_to_right = id1 = id in
209 parallel_positions bag [3]
210 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ]) id1 r
211 (parallel_demod table vl)
213 if id = id2 then None
215 let cl,_,_ = Terms.get_from_bag id2 bag in
216 Some ((bag,cl),jump_to_right)
219 let rec demodulate ~jump_to_right bag clause table =
220 match demodulate_once ~jump_to_right bag clause table with
221 | None -> bag, clause
222 | Some ((bag, clause),r) -> demodulate ~jump_to_right:r
226 let rec demodulate_old ~jump_to_right bag clause table =
227 match demodulate_once_old ~jump_to_right bag clause table with
228 | None -> bag, clause
229 | Some ((bag, clause),r) -> demodulate_old ~jump_to_right:r
233 let are_alpha_eq cl1 cl2 =
234 let get_term (_,lit,_,_) =
236 | Terms.Predicate _ -> assert false
237 | Terms.Equation (l,r,ty,_) ->
238 Terms.Node [Terms.Leaf B.eqP; ty; l ; r]
240 try ignore(Unif.alpha_eq (get_term cl1) (get_term cl2)) ; true
241 with FoUnif.UnificationFailure _ -> false
244 let demodulate bag clause table =
245 (* let (bag1,c1), (_,c2) =*)
246 demodulate ~jump_to_right:false bag clause table
247 (* demodulate_old ~jump_to_right:false bag clause table*)
249 if are_alpha_eq c1 c2 then bag1,c1
251 prerr_endline (Pp.pp_unit_clause c1);
252 prerr_endline (Pp.pp_unit_clause c2);
253 prerr_endline "Bag :";
254 prerr_endline (Pp.pp_bag bag1);
260 let is_identity_clause ~unify = function
261 | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
262 | _, Terms.Equation (l,r,_,_), vl, proof when unify ->
263 (try ignore(Unif.unification vl [] l r); true
264 with FoUnif.UnificationFailure _ -> false)
265 | _, Terms.Equation (_,_,_,_), _, _ -> false
266 | _, Terms.Predicate _, _, _ -> assert false
269 let build_new_clause bag maxvar filter rule t subst vl id id2 pos dir =
270 let maxvar, vl, relocsubst = Utils.relocate maxvar vl in
271 let subst = Subst.concat relocsubst subst in
272 match build_clause bag filter rule t subst vl id id2 pos dir with
273 | Some (bag, c) -> Some ((bag, maxvar), c)
277 let fold_build_new_clause bag maxvar id rule filter res =
278 let (bag, maxvar), res =
279 HExtlib.filter_map_acc
280 (fun (bag, maxvar) (t,subst,vl,id2,pos,dir) ->
281 build_new_clause bag maxvar filter rule t subst vl id id2 pos dir)
288 let rewrite_eq ~unify l r ty vl table =
289 let retrieve = if unify then IDX.DT.retrieve_unifiables
290 else IDX.DT.retrieve_generalizations in
291 let lcands = retrieve table l in
292 let rcands = retrieve table r in
294 let id, dir, l, r, vl =
296 | (d, (id,Terms.Equation (l,r,ty,_),vl,_))-> id, d, l, r, vl
299 let reverse = (dir = Terms.Left2Right) = b in
300 let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
301 else r,l, Terms.Right2Left in
302 (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
304 let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
305 let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
306 let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
307 let locked_vars = if unify then [] else vl in
308 let rec aux = function
310 | (id2,dir,c,vl1)::tl ->
312 let subst,vl1 = Unif.unification (vl@vl1) locked_vars c t in
313 Some (id2, dir, subst)
314 with FoUnif.UnificationFailure _ -> aux tl
316 aux (cands1 @ cands2)
319 let is_subsumed ~unify bag maxvar (id, lit, vl, _) table =
321 | Terms.Predicate _ -> assert false
322 | Terms.Equation (l,r,ty,_) ->
323 match rewrite_eq ~unify l r ty vl table with
325 | Some (id2, dir, subst) ->
326 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
327 build_new_clause bag maxvar (fun _ -> true)
328 Terms.Superposition id_t subst [] id id2 [2] dir
330 (* id refers to a clause proving contextl l = contextr r *)
332 let rec deep_eq ~unify l r ty pos contextl contextr table acc =
335 | Some(bag,maxvar,(id,lit,vl,p),subst) ->
336 let l = Subst.apply_subst subst l in
337 let r = Subst.apply_subst subst r in
339 let subst1,vl1 = Unif.unification vl [] l r in
341 match lit with Terms.Predicate _ -> assert false
342 | Terms.Equation (l,r,ty,o) ->
343 Terms.Equation (FoSubst.apply_subst subst1 l,
344 FoSubst.apply_subst subst1 r, ty, o)
346 Some(bag,maxvar,(id,lit,vl1,p),Subst.concat subst1 subst)
347 with FoUnif.UnificationFailure _ ->
348 match rewrite_eq ~unify l r ty vl table with
349 | Some (id2, dir, subst1) ->
350 let newsubst = Subst.concat subst1 subst in
352 FoSubst.apply_subst newsubst
353 (Terms.Node[Terms.Leaf B.eqP;ty;contextl r;contextr r])
356 build_new_clause bag maxvar (fun _ -> true)
357 Terms.Superposition id_t
358 subst1 [] id id2 (pos@[2]) dir
360 | Some ((bag, maxvar), c) ->
361 Some(bag,maxvar,c,newsubst)
362 | None -> assert false)
365 | Terms.Node (a::la), Terms.Node (b::lb) when
366 a = b && List.length la = List.length lb ->
369 (fun (acc,pre,postl,postr) a b ->
371 fun x -> contextl(Terms.Node (pre@(x::postl))) in
373 fun x -> contextr(Terms.Node (pre@(x::postr))) in
374 let newpos = List.length pre::pos in
376 if l = [] then [] else List.tl l in
377 (deep_eq ~unify a b ty
378 newpos newcl newcr table acc,pre@[b],
379 footail postl, footail postr))
380 (acc,[a],List.tl la,List.tl lb) la lb
385 let rec orphan_murder bag acc i =
386 match Terms.get_from_bag i bag with
387 | (_,_,_,Terms.Exact _),discarded,_ -> (discarded,acc)
388 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),true,_ -> (true,acc)
389 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),false,_ ->
390 if (List.mem i acc) then (false,acc)
391 else match orphan_murder bag acc i1 with
392 | (true,acc) -> (true,acc)
394 let (res,acc) = orphan_murder bag acc i2 in
395 if res then res,acc else res,i::acc
398 let orphan_murder bag actives cl =
399 let (id,_,_,_) = cl in
400 let actives = List.map (fun (i,_,_,_) -> i) actives in
401 let (res,_) = orphan_murder bag actives id in
402 if res then debug "Orphan murdered"; res
405 (* demodulate and check for subsumption *)
406 let simplify table maxvar bag clause =
407 if is_identity_clause ~unify:false clause then bag,None
408 (* else if orphan_murder bag actives clause then bag,None *)
409 else let bag, clause = demodulate bag clause table in
410 if is_identity_clause ~unify:false clause then bag,None
412 match is_subsumed ~unify:false bag maxvar clause table with
413 | None -> bag, Some clause
414 | Some _ -> bag, None
417 let simplify table maxvar bag clause =
418 match simplify table maxvar bag clause with
420 let (id,_,_,_) = clause in
421 let (_,_,iter) = Terms.get_from_bag id bag in
422 Terms.replace_in_bag (clause,true,iter) bag, None
423 | bag, Some clause -> bag, Some clause
424 (*let (id,_,_,_) = clause in
425 if orphan_murder bag clause then
426 Terms.M.add id (clause,true) bag, Some clause
427 else bag, Some clause*)
430 let one_pass_simplification new_clause (alist,atable) bag maxvar =
431 match simplify atable maxvar bag new_clause with
432 | bag,None -> bag,None (* new_clause has been discarded *)
433 | bag,(Some clause) ->
434 let ctable = IDX.index_unit_clause IDX.DT.empty clause in
435 let bag, alist, atable =
437 (fun (bag, alist, atable) c ->
438 match simplify ctable maxvar bag c with
439 |bag,None -> (bag,alist,atable)
440 (* an active clause as been discarded *)
442 bag, c :: alist, IDX.index_unit_clause atable c)
443 (bag,[],IDX.DT.empty) alist
445 bag, Some (clause, (alist,atable))
448 let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
450 if new_cl then atable else
451 IDX.index_unit_clause atable cl
453 (* Simplification of new_clause with : *
454 * - actives and cl if new_clause is not cl *
455 * - only actives otherwise *)
457 simplify atable1 maxvar bag new_clause with
458 | bag,None -> bag,(Some cl, None) (* new_clause has been discarded *)
460 (* Simplification of each active clause with clause *
461 * which is the simplified form of new_clause *)
462 let ctable = IDX.index_unit_clause IDX.DT.empty clause in
463 let bag, newa, alist, atable =
465 (fun (bag, newa, alist, atable) c ->
466 match simplify ctable maxvar bag c with
467 |bag,None -> (bag, newa, alist, atable)
468 (* an active clause as been discarded *)
471 bag, newa, c :: alist,
472 IDX.index_unit_clause atable c
474 bag, c1 :: newa, alist, atable)
475 (bag,[],[],IDX.DT.empty) alist
478 bag, (Some cl, Some (clause, (alist,atable), newa))
480 (* if new_clause is not cl, we simplify cl with clause *)
481 match simplify ctable maxvar bag cl with
483 (* cl has been discarded *)
484 bag,(None, Some (clause, (alist,atable), newa))
486 bag,(Some cl1, Some (clause, (alist,atable), newa))
489 let keep_simplified cl (alist,atable) bag maxvar =
490 let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
492 match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
493 | _,(None, _) -> assert false
494 | bag,(Some _, None) -> bag,None
495 | bag,(Some _, Some (clause, (alist,atable), newa)) ->
496 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
500 | [] -> bag, Some (cl, (alist,atable))
502 match simplification_step ~new_cl cl
503 (alist,atable) bag maxvar hd with
504 | _,(None,None) -> assert false
505 | bag,(Some _,None) ->
506 keep_simplified_aux ~new_cl cl (alist,atable) bag tl
507 | bag,(None, Some _) -> bag,None
508 | bag,(Some cl1, Some (clause, (alist,atable), newa)) ->
510 (clause::alist, IDX.index_unit_clause atable clause)
512 keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
515 keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
518 (* this is like simplify but raises Success *)
519 let simplify_goal ~no_demod maxvar table bag g_actives clause =
521 if no_demod then bag, clause else demodulate bag clause table
523 if List.exists (are_alpha_eq clause) g_actives then None else
524 if (is_identity_clause ~unify:true clause)
525 then raise (Success (bag, maxvar, clause))
527 let (id,lit,vl,_) = clause in
528 if vl = [] then Some (bag,clause)
532 | Terms.Equation(l,r,ty,_) -> l,r,ty
535 match deep_eq ~unify:true l r ty [] (fun x -> x) (fun x -> x)
536 table (Some(bag,maxvar,clause,Subst.id_subst)) with
537 | None -> Some (bag,clause)
538 | Some (bag,maxvar,cl,subst) ->
539 prerr_endline "Goal subsumed";
540 raise (Success (bag,maxvar,cl))
542 else match is_subsumed ~unify:true bag maxvar clause table with
543 | None -> Some (bag, clause)
544 | Some ((bag,maxvar),c) ->
545 prerr_endline "Goal subsumed";
546 raise (Success (bag,maxvar,c))
550 (* =================== inference ===================== *)
552 (* this is OK for both the sup_left and sup_right inference steps *)
553 let superposition table varlist subterm pos context =
554 let cands = IDX.DT.retrieve_unifiables table subterm in
556 (fun (dir, (id,lit,vl,_ (*as uc*))) ->
558 | Terms.Predicate _ -> assert false
559 | Terms.Equation (l,r,_,o) ->
560 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
563 Unif.unification (varlist@vl) [] subterm side
565 if o = Terms.Incomparable then
566 let side = Subst.apply_subst subst side in
567 let newside = Subst.apply_subst subst newside in
568 let o = Order.compare_terms side newside in
569 (* XXX: check Riazanov p. 33 (iii) *)
570 if o <> Terms.Lt && o <> Terms.Eq then
571 Some (context newside, subst, varlist, id, pos, dir)
573 ((*prerr_endline ("Filtering: " ^
574 Pp.pp_foterm side ^ " =(< || =)" ^
575 Pp.pp_foterm newside);*)None)
577 Some (context newside, subst, varlist, id, pos, dir)
578 with FoUnif.UnificationFailure _ -> None)
579 (IDX.ClauseSet.elements cands)
582 (* Superposes selected equation with equalities in table *)
583 let superposition_with_table bag maxvar (id,selected,vl,_) table =
585 | Terms.Predicate _ -> assert false
586 | Terms.Equation (l,r,ty,Terms.Lt) ->
587 fold_build_new_clause bag maxvar id Terms.Superposition
590 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
591 r (superposition table vl))
592 | Terms.Equation (l,r,ty,Terms.Gt) ->
593 fold_build_new_clause bag maxvar id Terms.Superposition
596 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
597 l (superposition table vl))
598 | Terms.Equation (l,r,ty,Terms.Incomparable) ->
599 fold_build_new_clause bag maxvar id Terms.Superposition
600 (function (* Riazanov: p.33 condition (iv) *)
601 | Terms.Node [Terms.Leaf eq; ty; l; r ] when B.eq B.eqP eq ->
602 Order.compare_terms l r <> Terms.Eq
605 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
606 r (superposition table vl)) @
608 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
609 l (superposition table vl)))
613 (* the current equation is normal w.r.t. demodulation with atable
614 * (and is not the identity) *)
615 let infer_right bag maxvar current (alist,atable) =
616 (* We demodulate actives clause with current until all *
617 * active clauses are reduced w.r.t each other *)
618 (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
619 let ctable = IDX.index_unit_clause IDX.DT.empty current in
620 (* let bag, (alist, atable) =
622 HExtlib.filter_map_acc (simplify ctable) bag alist
624 bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
626 debug "Simplified active clauses with fact";
627 (* We superpose active clauses with current *)
628 let bag, maxvar, new_clauses =
630 (fun (bag, maxvar, acc) active ->
631 let bag, maxvar, newc =
632 superposition_with_table bag maxvar active ctable
634 bag, maxvar, newc @ acc)
635 (bag, maxvar, []) alist
637 debug "First superpositions";
638 (* We add current to active clauses so that it can be *
639 * superposed with itself *)
641 current :: alist, IDX.index_unit_clause atable current
644 let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
645 (* We need to put fresh_current into the bag so that all *
646 * variables clauses refer to are known. *)
647 let bag, fresh_current = Terms.add_to_bag fresh_current bag in
648 (* We superpose current with active clauses *)
649 let bag, maxvar, additional_new_clauses =
650 superposition_with_table bag maxvar fresh_current atable
652 debug "Another superposition";
653 let new_clauses = new_clauses @ additional_new_clauses in
654 debug (Printf.sprintf "Demodulating %d clauses"
655 (List.length new_clauses));
656 let bag, new_clauses =
657 HExtlib.filter_map_monad (simplify atable maxvar) bag new_clauses
659 debug "Demodulated new clauses";
660 bag, maxvar, (alist, atable), new_clauses
663 let infer_left bag maxvar goal (_alist, atable) =
664 (* We superpose the goal with active clauses *)
665 if (match goal with (_,_,[],_) -> true | _ -> false) then bag, maxvar, []
667 let bag, maxvar, new_goals =
668 superposition_with_table bag maxvar goal atable
670 debug "Superposed goal with active clauses";
671 (* We simplify the new goals with active clauses *)
675 match simplify_goal ~no_demod:false maxvar atable bag [] g with
676 | None -> assert false
677 | Some (bag,g) -> bag,g::acc)
680 debug "Simplified new goals with active clauses";
681 bag, maxvar, List.rev new_goals