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.Subst(B)
19 module Order = Orderings.Orderings(B)
20 module Utils = FoUtils.Utils(B)
23 let rec list_first f = function
25 | x::tl -> match f x with Some _ as x -> x | _ -> list_first f tl
28 let first_position pos ctx t f =
29 let rec aux pos ctx = function
30 | Terms.Leaf _ as t -> f t pos ctx
33 match f t pos ctx with
36 let rec first pre post = function
39 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
40 match aux (List.length pre :: pos) newctx t with
43 if post = [] then None (* tl is also empty *)
44 else first (pre @ [t]) (List.tl post) tl
46 first [] (List.tl l) l
51 let all_positions pos ctx t f =
52 let rec aux pos ctx = function
53 | Terms.Leaf _ as t -> f t pos ctx
58 (fun (acc,pre,post) t -> (* Invariant: pre @ [t] @ post = l *)
59 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
60 let acc = aux (List.length pre :: pos) newctx t @ acc in
61 if post = [] then acc, l, []
62 else acc, pre @ [t], List.tl post)
63 (f t pos ctx, [], List.tl l) l
70 let build_clause bag filter rule t subst vl id id2 pos dir =
71 let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
72 let t = Subst.apply_subst subst t in
76 | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
77 let o = Order.compare_terms l r in
78 Terms.Equation (l, r, ty, o)
79 | t -> Terms.Predicate t
82 Utils.add_to_bag bag (0, literal, vl, proof)
86 ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
90 (* ============ simplification ================= *)
92 let demod table varlist subterm pos context =
93 let cands = IDX.DT.retrieve_generalizations table subterm in
95 (fun (dir, (id,lit,vl,_)) ->
97 | Terms.Predicate _ -> assert false
98 | Terms.Equation (l,r,_,o) ->
99 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
102 Unif.unification (varlist@vl) varlist subterm side
104 if o = Terms.Incomparable then
105 let side = Subst.apply_subst subst side in
106 let newside = Subst.apply_subst subst newside in
107 let o = Order.compare_terms side newside in
108 (* Riazanov, pp. 45 (ii) *)
110 Some (context newside, subst, varlist, id, pos, dir)
112 ((*prerr_endline ("Filtering: " ^
113 Pp.pp_foterm side ^ " =(< || =)" ^
114 Pp.pp_foterm newside ^ " coming from " ^
115 Pp.pp_unit_clause uc );*)None)
117 Some (context newside, subst, varlist, id, pos, dir)
118 with FoUnif.UnificationFailure _ -> None)
119 (IDX.ClauseSet.elements cands)
122 (* XXX: possible optimization, if the literal has a "side" already
123 * in normal form we should not traverse it again *)
124 let demodulate_once bag (id, literal, vl, _) table =
127 | Terms.Predicate t -> t
128 | Terms.Equation (l,r,ty,_) -> Terms.Node [ Terms.Leaf B.eqP; ty; l; r ]
130 match first_position [] (fun x -> x) t (demod table vl) with
132 | Some (newt, subst, varlist, id2, pos, dir) ->
133 build_clause bag (fun _ -> true) Terms.Demodulation
134 newt subst varlist id id2 pos dir
137 let rec demodulate bag clause table =
138 match demodulate_once bag clause table with
139 | None -> bag, clause
140 | Some (bag, clause) -> demodulate bag clause table
144 let is_identity_clause = function
145 | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
146 | _, Terms.Predicate _, _, _ -> assert false
150 let is_subsumed (id, lit, vl, _) table =
152 | Terms.Predicate _ -> assert false
153 | Terms.Equation (l,r,ty,_) ->
154 let lcands = IDX.DT.retrieve_generalizations table l in
155 let rcands = IDX.DT.retrieve_generalizations table l in
159 | (d, (_,Terms.Equation (l,r,ty,_),vl,_))-> d, l, r, vl
162 let l, r = if (dir = Terms.Left2Right) = b then l,r else r,l in
163 Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl
165 let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
166 let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
167 let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
170 try ignore(Unif.unification (vl@vl1) vl c t); true
171 with FoUnif.UnificationFailure _ -> false)
175 (* demodulate and check for subsumption *)
176 let forward_simplify table bag clause =
177 let bag, clause = demodulate bag clause table in
178 if is_identity_clause clause then None
180 if is_subsumed clause table then None
181 else Some (bag, clause)
184 (* =================== inference ===================== *)
186 let superposition_right table varlist subterm pos context =
187 let cands = IDX.DT.retrieve_unifiables table subterm in
189 (fun (dir, (id,lit,vl,_ (*as uc*))) ->
191 | Terms.Predicate _ -> assert false
192 | Terms.Equation (l,r,_,o) ->
193 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
196 Unif.unification (varlist@vl) [] subterm side
198 if o = Terms.Incomparable then
199 let side = Subst.apply_subst subst side in
200 let newside = Subst.apply_subst subst newside in
201 let o = Order.compare_terms side newside in
202 (* XXX: check Riazanov p. 33 (iii) *)
203 if o <> Terms.Lt && o <> Terms.Eq then
204 Some (context newside, subst, varlist, id, pos, dir)
206 ((*prerr_endline ("Filtering: " ^
207 Pp.pp_foterm side ^ " =(< || =)" ^
208 Pp.pp_foterm newside ^ " coming from " ^
209 Pp.pp_unit_clause uc );*)None)
211 Some (context newside, subst, varlist, id, pos, dir)
212 with FoUnif.UnificationFailure _ -> None)
213 (IDX.ClauseSet.elements cands)
216 let build_new_clause bag maxvar filter rule t subst vl id id2 pos dir =
217 let maxvar, vl, relocsubst = Utils.relocate maxvar vl in
218 let subst = Subst.concat relocsubst subst in
219 match build_clause bag filter rule t subst vl id id2 pos dir with
220 | Some (bag, c) -> Some ((bag, maxvar), c)
225 let fold_build_new_clause bag maxvar id rule filter res =
226 let (bag, maxvar), res =
227 HExtlib.filter_map_acc
228 (fun (bag, maxvar) (t,subst,vl,id2,pos,dir) ->
229 build_new_clause bag maxvar filter rule t subst vl id id2 pos dir)
235 let superposition_right_with_table bag maxvar (id,selected,vl,_) table =
237 | Terms.Predicate _ -> assert false
238 | Terms.Equation (l,r,ty,Terms.Lt) ->
239 fold_build_new_clause bag maxvar id Terms.SuperpositionRight
242 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
243 r (superposition_right table vl))
244 | Terms.Equation (l,r,ty,Terms.Gt) ->
245 fold_build_new_clause bag maxvar id Terms.SuperpositionRight
248 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
249 l (superposition_right table vl))
250 | Terms.Equation (l,r,ty,Terms.Incomparable) ->
251 fold_build_new_clause bag maxvar id Terms.SuperpositionRight
252 (function (* Riazanov: p.33 condition (iv) *)
253 | Terms.Node [Terms.Leaf eq; ty; l; r ] when B.eq B.eqP eq ->
254 Order.compare_terms l r <> Terms.Eq
257 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
258 r (superposition_right table vl)) @
260 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
261 l (superposition_right table vl)))
265 (* the current equation is normal w.r.t. demodulation with atable
266 * (and is not the identity) *)
267 let infer_right bag maxvar current (alist,atable) =
268 let ctable = IDX.index_unit_clause IDX.DT.empty current in
269 let bag, maxvar, new_clauses =
271 (fun (bag, maxvar, acc) active ->
272 let bag, maxvar, newc =
273 superposition_right_with_table bag maxvar active ctable
275 bag, maxvar, newc @ acc)
276 (bag, maxvar, []) alist
279 current :: alist, IDX.index_unit_clause atable current
281 let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
282 let bag, maxvar, additional_new_clauses =
283 superposition_right_with_table bag maxvar fresh_current atable
285 let new_clauses = new_clauses @ additional_new_clauses in
286 let bag, new_clauses =
287 HExtlib.filter_map_acc (forward_simplify atable) bag new_clauses
289 bag, maxvar, (alist, atable), new_clauses