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
26 ()(* prerr_endline s *)
29 let rec list_first f = function
31 | x::tl -> match f x with Some _ as x -> x | _ -> list_first f tl
34 let first_position pos ctx t f =
35 let rec aux pos ctx = function
36 | Terms.Leaf _ as t -> f t pos ctx
39 match f t pos ctx with
42 let rec first pre post = function
45 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
46 match aux (List.length pre :: pos) newctx t with
49 if post = [] then None (* tl is also empty *)
50 else first (pre @ [t]) (List.tl post) tl
52 first [] (List.tl l) l
57 let all_positions pos ctx t f =
58 let rec aux pos ctx = function
59 | Terms.Leaf _ as t -> f t pos ctx
64 (fun (acc,pre,post) t -> (* Invariant: pre @ [t] @ post = l *)
65 let newctx = fun x -> ctx (Terms.Node (pre@[x]@post)) in
66 let acc = aux (List.length pre :: pos) newctx t @ acc in
67 if post = [] then acc, l, []
68 else acc, pre @ [t], List.tl post)
69 (f t pos ctx, [], List.tl l) l
76 let build_clause bag filter rule t subst vl id id2 pos dir =
77 let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
78 let t = Subst.apply_subst subst t in
82 | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
83 let o = Order.compare_terms l r in
84 Terms.Equation (l, r, ty, o)
85 | t -> Terms.Predicate t
88 Utils.add_to_bag bag (0, literal, vl, proof)
92 ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
96 (* ============ simplification ================= *)
98 let demod table varlist subterm pos context =
99 let cands = IDX.DT.retrieve_generalizations table subterm in
101 (fun (dir, (id,lit,vl,_)) ->
103 | Terms.Predicate _ -> assert false
104 | Terms.Equation (l,r,_,o) ->
105 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
108 Unif.unification (varlist@vl) varlist subterm side
110 if o = Terms.Incomparable then
111 let side = Subst.apply_subst subst side in
112 let newside = Subst.apply_subst subst newside in
113 let o = Order.compare_terms newside side in
114 (* Riazanov, pp. 45 (ii) *)
116 Some (context newside, subst, varlist, id, pos, dir)
118 ((*prerr_endline ("Filtering: " ^
119 Pp.pp_foterm side ^ " =(< || =)" ^
120 Pp.pp_foterm newside ^ " coming from " ^
121 Pp.pp_unit_clause uc );*)None)
123 Some (context newside, subst, varlist, id, pos, dir)
124 with FoUnif.UnificationFailure _ -> None)
125 (IDX.ClauseSet.elements cands)
128 (* XXX: possible optimization, if the literal has a "side" already
129 * in normal form we should not traverse it again *)
130 let demodulate_once bag (id, literal, vl, pr) table =
131 debug ("Demodulating : " ^ (Pp.pp_unit_clause (id, literal, vl, pr)));
134 | Terms.Predicate t -> t
135 | Terms.Equation (l,r,ty,_) -> Terms.Node [ Terms.Leaf B.eqP; ty; l; r ]
137 match first_position [] (fun x -> x) t (demod table vl) with
139 | Some (newt, subst, varlist, id2, pos, dir) ->
140 build_clause bag (fun _ -> true) Terms.Demodulation
141 newt subst varlist id id2 pos dir
144 let rec demodulate bag clause table =
145 match demodulate_once bag clause table with
146 | None -> bag, clause
147 | Some (bag, clause) -> demodulate bag clause table
151 let is_identity_clause = function
152 | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
153 | _, Terms.Predicate _, _, _ -> assert false
157 let is_subsumed ~unify (id, lit, vl, _) table =
159 | Terms.Predicate _ -> assert false
160 | Terms.Equation (l,r,ty,_) ->
161 let retrieve = if unify then IDX.DT.retrieve_unifiables
162 else IDX.DT.retrieve_generalizations in
163 let lcands = retrieve table l in
164 let rcands = retrieve table r in
168 | (d, (_,Terms.Equation (l,r,ty,_),vl,_))-> d, l, r, vl
171 let l, r = if (dir = Terms.Left2Right) = b then l,r else r,l in
172 Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl
174 let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
175 let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
176 let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
177 let locked_vars = if unify then [] else vl in
180 try ignore(Unif.unification (vl@vl1) locked_vars c t); true
181 with FoUnif.UnificationFailure _ -> false)
185 (* demodulate and check for subsumption *)
186 let forward_simplify table bag clause =
187 let bag, clause = demodulate bag clause table in
188 if is_identity_clause clause then None
190 if is_subsumed ~unify:false clause table then None
191 else Some (bag, clause)
194 (* this is like forward_simplify but raises Success *)
195 let backward_simplify maxvar table bag clause =
196 let bag, clause = demodulate bag clause table in
197 if (is_identity_clause clause) || (is_subsumed ~unify:true clause table)
198 then raise (Success (bag, maxvar, clause))
202 (* =================== inference ===================== *)
204 (* this is OK for both the sup_left and sup_right inference steps *)
205 let superposition table varlist subterm pos context =
206 let cands = IDX.DT.retrieve_unifiables table subterm in
208 (fun (dir, (id,lit,vl,_ (*as uc*))) ->
210 | Terms.Predicate _ -> assert false
211 | Terms.Equation (l,r,_,o) ->
212 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
215 Unif.unification (varlist@vl) [] subterm side
217 if o = Terms.Incomparable then
218 let side = Subst.apply_subst subst side in
219 let newside = Subst.apply_subst subst newside in
220 let o = Order.compare_terms side newside in
221 (* XXX: check Riazanov p. 33 (iii) *)
222 if o <> Terms.Lt && o <> Terms.Eq then
223 Some (context newside, subst, varlist, id, pos, dir)
225 ((*prerr_endline ("Filtering: " ^
226 Pp.pp_foterm side ^ " =(< || =)" ^
227 Pp.pp_foterm newside ^ " coming from " ^
228 Pp.pp_unit_clause uc );*)None)
230 Some (context newside, subst, varlist, id, pos, dir)
231 with FoUnif.UnificationFailure _ -> None)
232 (IDX.ClauseSet.elements cands)
235 let build_new_clause bag maxvar filter rule t subst vl id id2 pos dir =
236 let maxvar, vl, relocsubst = Utils.relocate maxvar vl in
237 let subst = Subst.concat relocsubst subst in
238 match build_clause bag filter rule t subst vl id id2 pos dir with
239 | Some (bag, c) -> Some ((bag, maxvar), c)
244 let fold_build_new_clause bag maxvar id rule filter res =
245 let (bag, maxvar), res =
246 HExtlib.filter_map_acc
247 (fun (bag, maxvar) (t,subst,vl,id2,pos,dir) ->
248 build_new_clause bag maxvar filter rule t subst vl id id2 pos dir)
254 (* Superposes selected equation with equalities in table *)
255 let superposition_with_table bag maxvar (id,selected,vl,_) table =
257 | Terms.Predicate _ -> assert false
258 | Terms.Equation (l,r,ty,Terms.Lt) ->
259 fold_build_new_clause bag maxvar id Terms.Superposition
262 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
263 r (superposition table vl))
264 | Terms.Equation (l,r,ty,Terms.Gt) ->
265 fold_build_new_clause bag maxvar id Terms.Superposition
268 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
269 l (superposition table vl))
270 | Terms.Equation (l,r,ty,Terms.Incomparable) ->
271 fold_build_new_clause bag maxvar id Terms.Superposition
272 (function (* Riazanov: p.33 condition (iv) *)
273 | Terms.Node [Terms.Leaf eq; ty; l; r ] when B.eq B.eqP eq ->
274 Order.compare_terms l r <> Terms.Eq
277 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
278 r (superposition table vl)) @
280 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
281 l (superposition table vl)))
285 (* the current equation is normal w.r.t. demodulation with atable
286 * (and is not the identity) *)
287 let infer_right bag maxvar current (alist,atable) =
288 (* We demodulate actives clause with current *)
289 let ctable = IDX.index_unit_clause IDX.DT.empty current in
290 let bag, (alist, atable) =
292 HExtlib.filter_map_acc (forward_simplify ctable) bag alist
294 bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
296 debug "Simplified active clauses with fact";
297 (* We superpose active clauses with current *)
298 let bag, maxvar, new_clauses =
300 (fun (bag, maxvar, acc) active ->
301 let bag, maxvar, newc =
302 superposition_with_table bag maxvar active ctable
304 bag, maxvar, newc @ acc)
305 (bag, maxvar, []) alist
307 debug "First superpositions";
308 (* We add current to active clauses so that it can be *
309 * superposed with itself *)
311 current :: alist, IDX.index_unit_clause atable current
314 let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
315 (* We need to put fresh_current into the bag so that all *
316 * variables clauses refer to are known. *)
317 let bag, fresh_current = Utils.add_to_bag bag fresh_current in
318 (* We superpose current with active clauses *)
319 let bag, maxvar, additional_new_clauses =
320 superposition_with_table bag maxvar fresh_current atable
322 debug "Another superposition";
323 let new_clauses = new_clauses @ additional_new_clauses in
324 let bag, new_clauses =
325 HExtlib.filter_map_acc (forward_simplify atable) bag new_clauses
327 debug "Demodulated new clauses";
328 bag, maxvar, (alist, atable), new_clauses
331 let infer_left bag maxvar goal (_alist, atable) =
332 (* We superpose the goal with active clauses *)
333 let bag, maxvar, new_goals =
334 superposition_with_table bag maxvar goal atable
336 (* We demodulate the goal with active clauses *)
340 let bag, g = demodulate bag g atable in
344 bag, maxvar, List.rev new_goals