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 exception Success of B.t Terms.bag * int * B.t Terms.unit_clause
25 let rec list_first f = function
27 | x::tl -> match f x with Some _ as x -> x | _ -> list_first f tl
30 let first_position pos ctx t f =
31 let rec aux pos ctx = function
32 | Terms.Leaf _ as t -> f t pos ctx
35 match f t pos ctx with
38 let rec first pre post = function
41 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
42 match aux (List.length pre :: pos) newctx t with
45 if post = [] then None (* tl is also empty *)
46 else first (pre @ [t]) (List.tl post) tl
48 first [] (List.tl l) l
53 let all_positions pos ctx t f =
54 let rec aux pos ctx = function
55 | Terms.Leaf _ as t -> f t pos ctx
60 (fun (acc,pre,post) t -> (* Invariant: pre @ [t] @ post = l *)
61 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
62 let acc = aux (List.length pre :: pos) newctx t @ acc in
63 if post = [] then acc, l, []
64 else acc, pre @ [t], List.tl post)
65 (f t pos ctx, [], List.tl l) l
72 let build_clause bag filter rule t subst vl id id2 pos dir =
73 let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
74 let t = Subst.apply_subst subst t in
78 | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
79 let o = Order.compare_terms l r in
80 Terms.Equation (l, r, ty, o)
81 | t -> Terms.Predicate t
84 Utils.add_to_bag bag (0, literal, vl, proof)
88 ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
92 (* ============ simplification ================= *)
94 let demod table varlist subterm pos context =
95 let cands = IDX.DT.retrieve_generalizations table subterm in
97 (fun (dir, (id,lit,vl,_)) ->
99 | Terms.Predicate _ -> assert false
100 | Terms.Equation (l,r,_,o) ->
101 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
104 Unif.unification (varlist@vl) varlist subterm side
106 if o = Terms.Incomparable then
107 let side = Subst.apply_subst subst side in
108 let newside = Subst.apply_subst subst newside in
109 let o = Order.compare_terms side newside in
110 (* Riazanov, pp. 45 (ii) *)
112 Some (context newside, subst, varlist, id, pos, dir)
114 ((*prerr_endline ("Filtering: " ^
115 Pp.pp_foterm side ^ " =(< || =)" ^
116 Pp.pp_foterm newside ^ " coming from " ^
117 Pp.pp_unit_clause uc );*)None)
119 Some (context newside, subst, varlist, id, pos, dir)
120 with FoUnif.UnificationFailure _ -> None)
121 (IDX.ClauseSet.elements cands)
124 (* XXX: possible optimization, if the literal has a "side" already
125 * in normal form we should not traverse it again *)
126 let demodulate_once bag (id, literal, vl, _) table =
129 | Terms.Predicate t -> t
130 | Terms.Equation (l,r,ty,_) -> Terms.Node [ Terms.Leaf B.eqP; ty; l; r ]
132 match first_position [] (fun x -> x) t (demod table vl) with
134 | Some (newt, subst, varlist, id2, pos, dir) ->
135 build_clause bag (fun _ -> true) Terms.Demodulation
136 newt subst varlist id id2 pos dir
139 let rec demodulate bag clause table =
140 match demodulate_once bag clause table with
141 | None -> bag, clause
142 | Some (bag, clause) -> demodulate bag clause table
146 let is_identity_clause = function
147 | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
148 | _, Terms.Predicate _, _, _ -> assert false
152 let is_subsumed (id, lit, vl, _) table =
154 | Terms.Predicate _ -> assert false
155 | Terms.Equation (l,r,ty,_) ->
156 let lcands = IDX.DT.retrieve_generalizations table l in
157 let rcands = IDX.DT.retrieve_generalizations table l in
161 | (d, (_,Terms.Equation (l,r,ty,_),vl,_))-> d, l, r, vl
164 let l, r = if (dir = Terms.Left2Right) = b then l,r else r,l in
165 Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl
167 let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
168 let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
169 let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
172 try ignore(Unif.unification (vl@vl1) vl c t); true
173 with FoUnif.UnificationFailure _ -> false)
177 (* demodulate and check for subsumption *)
178 let forward_simplify table bag clause =
179 let bag, clause = demodulate bag clause table in
180 if is_identity_clause clause then None
182 if is_subsumed clause table then None
183 else Some (bag, clause)
186 (* this is like forward_simplify but raises Success *)
187 let backward_simplify maxvar table bag clause =
188 let bag, clause = demodulate bag clause table in
189 if is_identity_clause clause then raise (Success (bag, maxvar, clause))
193 (* =================== inference ===================== *)
195 (* this is OK for both the sup_left and sup_right inference steps *)
196 let superposition table varlist subterm pos context =
197 let cands = IDX.DT.retrieve_unifiables table subterm in
199 (fun (dir, (id,lit,vl,_ (*as uc*))) ->
201 | Terms.Predicate _ -> assert false
202 | Terms.Equation (l,r,_,o) ->
203 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
206 Unif.unification (varlist@vl) [] subterm side
208 if o = Terms.Incomparable then
209 let side = Subst.apply_subst subst side in
210 let newside = Subst.apply_subst subst newside in
211 let o = Order.compare_terms side newside in
212 (* XXX: check Riazanov p. 33 (iii) *)
213 if o <> Terms.Lt && o <> Terms.Eq then
214 Some (context newside, subst, varlist, id, pos, dir)
216 ((*prerr_endline ("Filtering: " ^
217 Pp.pp_foterm side ^ " =(< || =)" ^
218 Pp.pp_foterm newside ^ " coming from " ^
219 Pp.pp_unit_clause uc );*)None)
221 Some (context newside, subst, varlist, id, pos, dir)
222 with FoUnif.UnificationFailure _ -> None)
223 (IDX.ClauseSet.elements cands)
226 let build_new_clause bag maxvar filter rule t subst vl id id2 pos dir =
227 let maxvar, vl, relocsubst = Utils.relocate maxvar vl in
228 let subst = Subst.concat relocsubst subst in
229 match build_clause bag filter rule t subst vl id id2 pos dir with
230 | Some (bag, c) -> Some ((bag, maxvar), c)
235 let fold_build_new_clause bag maxvar id rule filter res =
236 let (bag, maxvar), res =
237 HExtlib.filter_map_acc
238 (fun (bag, maxvar) (t,subst,vl,id2,pos,dir) ->
239 build_new_clause bag maxvar filter rule t subst vl id id2 pos dir)
245 let superposition_with_table bag maxvar (id,selected,vl,_) table =
247 | Terms.Predicate _ -> assert false
248 | Terms.Equation (l,r,ty,Terms.Lt) ->
249 fold_build_new_clause bag maxvar id Terms.Superposition
252 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
253 r (superposition table vl))
254 | Terms.Equation (l,r,ty,Terms.Gt) ->
255 fold_build_new_clause bag maxvar id Terms.Superposition
258 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
259 l (superposition table vl))
260 | Terms.Equation (l,r,ty,Terms.Incomparable) ->
261 fold_build_new_clause bag maxvar id Terms.Superposition
262 (function (* Riazanov: p.33 condition (iv) *)
263 | Terms.Node [Terms.Leaf eq; ty; l; r ] when B.eq B.eqP eq ->
264 Order.compare_terms l r <> Terms.Eq
267 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
268 r (superposition table vl)) @
270 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
271 l (superposition table vl)))
275 (* the current equation is normal w.r.t. demodulation with atable
276 * (and is not the identity) *)
277 let infer_right bag maxvar current (alist,atable) =
278 let ctable = IDX.index_unit_clause IDX.DT.empty current in
279 let bag, (alist, atable) =
281 HExtlib.filter_map_acc (forward_simplify ctable) bag alist
283 bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
285 let bag, maxvar, new_clauses =
287 (fun (bag, maxvar, acc) active ->
288 let bag, maxvar, newc =
289 superposition_with_table bag maxvar active ctable
291 bag, maxvar, newc @ acc)
292 (bag, maxvar, []) alist
295 current :: alist, IDX.index_unit_clause atable current
297 let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
298 let bag, maxvar, additional_new_clauses =
299 superposition_with_table bag maxvar fresh_current atable
301 let new_clauses = new_clauses @ additional_new_clauses in
302 let bag, new_clauses =
303 HExtlib.filter_map_acc (forward_simplify atable) bag new_clauses
305 bag, maxvar, (alist, atable), new_clauses
308 let infer_left bag maxvar goal (_alist, atable) =
309 let bag, maxvar, new_goals =
310 superposition_with_table bag maxvar goal atable
315 let bag, g = demodulate bag g atable in
319 bag, maxvar, List.rev new_goals