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
77 let rec aux acc = function
79 | Terms.Var i -> if (List.mem i acc) then acc else i::acc
80 | Terms.Node l -> List.fold_left aux acc l
84 let build_clause bag filter rule t subst vl id id2 pos dir =
85 let proof = Terms.Step(rule,id,id2,dir,pos,subst) in
86 let t = Subst.apply_subst subst t in
90 | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
91 let o = Order.compare_terms l r in
92 Terms.Equation (l, r, ty, o)
93 | t -> Terms.Predicate t
96 Utils.add_to_bag bag (0, literal, vars_of_term t, proof)
100 ((*prerr_endline ("Filtering: " ^ Pp.pp_foterm t);*)None)
104 (* ============ simplification ================= *)
106 let demod table varlist subterm pos context =
107 let cands = IDX.DT.retrieve_generalizations table subterm in
109 (fun (dir, (id,lit,vl,_)) ->
111 | Terms.Predicate _ -> assert false
112 | Terms.Equation (l,r,_,o) ->
113 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
116 Unif.unification (varlist@vl) varlist subterm side
118 if o = Terms.Incomparable then
119 let side = Subst.apply_subst subst side in
120 let newside = Subst.apply_subst subst newside in
121 let o = Order.compare_terms newside side in
122 (* Riazanov, pp. 45 (ii) *)
124 Some (context newside, subst, varlist, id, pos, dir)
126 ((*prerr_endline ("Filtering: " ^
127 Pp.pp_foterm side ^ " =(< || =)" ^
128 Pp.pp_foterm newside ^ " coming from " ^
129 Pp.pp_unit_clause uc );*)None)
131 Some (context newside, subst, varlist, id, pos, dir)
132 with FoUnif.UnificationFailure _ -> None)
133 (IDX.ClauseSet.elements cands)
136 (* XXX: possible optimization, if the literal has a "side" already
137 * in normal form we should not traverse it again *)
138 let demodulate_once bag (id, literal, vl, pr) table =
139 (* debug ("Demodulating : " ^ (Pp.pp_unit_clause (id, literal, vl, pr)));*)
142 | Terms.Predicate t -> t
143 | Terms.Equation (l,r,ty,_) -> Terms.Node [ Terms.Leaf B.eqP; ty; l; r ]
145 match first_position [] (fun x -> x) t (demod table vl) with
147 | Some (newt, subst, varlist, id2, pos, dir) ->
148 build_clause bag (fun _ -> true) Terms.Demodulation
149 newt subst varlist id id2 pos dir
152 let rec demodulate bag clause table =
153 match demodulate_once bag clause table with
154 | None -> bag, clause
155 | Some (bag, clause) -> demodulate bag clause table
159 let is_identity_clause = function
160 | _, Terms.Equation (_,_,_,Terms.Eq), _, _ -> true
161 | _, Terms.Predicate _, _, _ -> assert false
165 let build_new_clause bag maxvar filter rule t subst vl id id2 pos dir =
166 let maxvar, vl, relocsubst = Utils.relocate maxvar vl in
167 let subst = Subst.concat relocsubst subst in
168 match build_clause bag filter rule t subst vl id id2 pos dir with
169 | Some (bag, c) -> Some ((bag, maxvar), c)
174 let fold_build_new_clause bag maxvar id rule filter res =
175 let (bag, maxvar), res =
176 HExtlib.filter_map_acc
177 (fun (bag, maxvar) (t,subst,vl,id2,pos,dir) ->
178 build_new_clause bag maxvar filter rule t subst vl id id2 pos dir)
184 let is_subsumed ~unify bag maxvar (id, lit, vl, _) table =
186 | Terms.Predicate _ -> assert false
187 | Terms.Equation (l,r,ty,_) ->
188 let retrieve = if unify then IDX.DT.retrieve_unifiables
189 else IDX.DT.retrieve_generalizations in
190 let lcands = retrieve table l in
191 let rcands = retrieve table r in
193 let id, dir, l, r, vl =
195 | (d, (id,Terms.Equation (l,r,ty,_),vl,_))-> id, d, l, r, vl
198 let reverse = (dir = Terms.Left2Right) = b in
199 let l, r, proof_rewrite_dir = if reverse then l,r,Terms.Left2Right
200 else r,l, Terms.Right2Left in
201 (id,proof_rewrite_dir,Terms.Node [ Terms.Leaf B.eqP; ty; l; r ], vl)
203 let cands1 = List.map (f true) (IDX.ClauseSet.elements lcands) in
204 let cands2 = List.map (f false) (IDX.ClauseSet.elements rcands) in
205 let t = Terms.Node [ Terms.Leaf B.eqP; ty; l; r ] in
206 let locked_vars = if unify then [] else vl in
207 let rec aux = function
209 | (id2,dir,c,vl1)::tl ->
211 let subst,vl1 = Unif.unification (vl@vl1) locked_vars c t in
212 let id_t = Terms.Node [ Terms.Leaf B.eqP; ty; r; r ] in
213 build_new_clause bag maxvar (fun _ -> true)
214 Terms.Superposition id_t subst [] id id2 [2] dir
215 with FoUnif.UnificationFailure _ -> aux tl
217 aux (cands1 @ cands2)
220 (* demodulate and check for subsumption *)
221 let simplify table maxvar bag clause =
222 let bag, clause = demodulate bag clause table in
223 if is_identity_clause clause then None
225 match is_subsumed ~unify:false bag maxvar clause table with
226 | None -> Some (bag, clause)
230 let one_pass_simplification new_clause (alist,atable) bag maxvar =
231 match simplify atable maxvar bag new_clause with
232 | None -> None (* new_clause has been discarded *)
233 | Some (bag, clause) ->
234 let ctable = IDX.index_unit_clause IDX.DT.empty clause in
235 let bag, alist, atable =
237 (fun (bag, alist, atable as acc) c ->
238 match simplify ctable maxvar bag c with
239 |None -> acc (* an active clause as been discarded *)
241 bag, c :: alist, IDX.index_unit_clause atable c)
242 (bag,[],IDX.DT.empty) alist
244 Some (clause, bag, (alist,atable))
247 let simplification_step ~new_cl cl (alist,atable) bag maxvar new_clause =
249 if new_cl then atable else
250 IDX.index_unit_clause atable cl
252 (* Simplification of new_clause with : *
253 * - actives and cl if new_clause is not cl *
254 * - only actives otherwise *)
255 match simplify atable1 maxvar bag new_clause with
256 | None -> (Some cl, None) (* new_clause has been discarded *)
257 | Some (bag, clause) ->
258 (* Simplification of each active clause with clause *
259 * which is the simplified form of new_clause *)
260 let ctable = IDX.index_unit_clause IDX.DT.empty clause in
261 let bag, newa, alist, atable =
263 (fun (bag, newa, alist, atable as acc) c ->
264 match simplify ctable maxvar bag c with
265 |None -> acc (* an active clause as been discarded *)
268 bag, newa, c :: alist,
269 IDX.index_unit_clause atable c
271 bag, c1 :: newa, alist, atable)
272 (bag,[],[],IDX.DT.empty) alist
275 (Some cl, Some (clause, (alist,atable), newa, bag))
277 (* if new_clause is not cl, we simplify cl with clause *)
278 match simplify ctable maxvar bag cl with
280 (* cl has been discarded *)
281 (None, Some (clause, (alist,atable), newa, bag))
283 (Some cl1, Some (clause, (alist,atable), newa, bag))
286 let keep_simplified cl (alist,atable) bag maxvar =
287 let rec keep_simplified_aux ~new_cl cl (alist,atable) bag newc =
289 match simplification_step ~new_cl cl (alist,atable) bag maxvar cl with
290 | (None, _) -> assert false
291 | (Some _, None) -> None
292 | (Some _, Some (clause, (alist,atable), newa, bag)) ->
293 keep_simplified_aux ~new_cl:(cl!=clause) clause (alist,atable)
297 | [] -> Some (cl, bag, (alist,atable))
299 match simplification_step ~new_cl cl
300 (alist,atable) bag maxvar hd with
301 | (None,None) -> assert false
303 keep_simplified_aux ~new_cl cl (alist,atable) bag tl
304 | (None, Some _) -> None
305 | (Some cl1, Some (clause, (alist,atable), newa, bag)) ->
307 (clause::alist, IDX.index_unit_clause atable clause)
309 keep_simplified_aux ~new_cl:(cl!=cl1) cl1 (alist,atable)
312 keep_simplified_aux ~new_cl:true cl (alist,atable) bag []
315 (* this is like simplify but raises Success *)
316 let simplify_goal maxvar table bag clause =
317 let bag, clause = demodulate bag clause table in
318 if (is_identity_clause clause)
319 then raise (Success (bag, maxvar, clause))
320 else match is_subsumed ~unify:true bag maxvar clause table with
321 | None -> bag, clause
322 | Some ((bag,maxvar),c) ->
323 debug "Goal subsumed";
324 raise (Success (bag,maxvar,c))
327 (* =================== inference ===================== *)
329 (* this is OK for both the sup_left and sup_right inference steps *)
330 let superposition table varlist subterm pos context =
331 let cands = IDX.DT.retrieve_unifiables table subterm in
333 (fun (dir, (id,lit,vl,_ (*as uc*))) ->
335 | Terms.Predicate _ -> assert false
336 | Terms.Equation (l,r,_,o) ->
337 let side, newside = if dir=Terms.Left2Right then l,r else r,l in
340 Unif.unification (varlist@vl) [] subterm side
342 if o = Terms.Incomparable then
343 let side = Subst.apply_subst subst side in
344 let newside = Subst.apply_subst subst newside in
345 let o = Order.compare_terms side newside in
346 (* XXX: check Riazanov p. 33 (iii) *)
347 if o <> Terms.Lt && o <> Terms.Eq then
348 Some (context newside, subst, varlist, id, pos, dir)
350 ((*prerr_endline ("Filtering: " ^
351 Pp.pp_foterm side ^ " =(< || =)" ^
352 Pp.pp_foterm newside ^ " coming from " ^
353 Pp.pp_unit_clause uc );*)None)
355 Some (context newside, subst, varlist, id, pos, dir)
356 with FoUnif.UnificationFailure _ -> None)
357 (IDX.ClauseSet.elements cands)
360 (* Superposes selected equation with equalities in table *)
361 let superposition_with_table bag maxvar (id,selected,vl,_) table =
363 | Terms.Predicate _ -> assert false
364 | Terms.Equation (l,r,ty,Terms.Lt) ->
365 fold_build_new_clause bag maxvar id Terms.Superposition
368 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
369 r (superposition table vl))
370 | Terms.Equation (l,r,ty,Terms.Gt) ->
371 fold_build_new_clause bag maxvar id Terms.Superposition
374 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
375 l (superposition table vl))
376 | Terms.Equation (l,r,ty,Terms.Incomparable) ->
377 fold_build_new_clause bag maxvar id Terms.Superposition
378 (function (* Riazanov: p.33 condition (iv) *)
379 | Terms.Node [Terms.Leaf eq; ty; l; r ] when B.eq B.eqP eq ->
380 Order.compare_terms l r <> Terms.Eq
383 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; l; x ])
384 r (superposition table vl)) @
386 (fun x -> Terms.Node [ Terms.Leaf B.eqP; ty; x; r ])
387 l (superposition table vl)))
391 (* the current equation is normal w.r.t. demodulation with atable
392 * (and is not the identity) *)
393 let infer_right bag maxvar current (alist,atable) =
394 (* We demodulate actives clause with current until all *
395 * active clauses are reduced w.r.t each other *)
396 (* let bag, (alist,atable) = keep_simplified (alist,atable) bag [current] in *)
397 let ctable = IDX.index_unit_clause IDX.DT.empty current in
398 (* let bag, (alist, atable) =
400 HExtlib.filter_map_acc (simplify ctable) bag alist
402 bag, (alist, List.fold_left IDX.index_unit_clause IDX.DT.empty alist)
404 debug "Simplified active clauses with fact";
405 (* We superpose active clauses with current *)
406 let bag, maxvar, new_clauses =
408 (fun (bag, maxvar, acc) active ->
409 let bag, maxvar, newc =
410 superposition_with_table bag maxvar active ctable
412 bag, maxvar, newc @ acc)
413 (bag, maxvar, []) alist
415 debug "First superpositions";
416 (* We add current to active clauses so that it can be *
417 * superposed with itself *)
419 current :: alist, IDX.index_unit_clause atable current
422 let fresh_current, maxvar = Utils.fresh_unit_clause maxvar current in
423 (* We need to put fresh_current into the bag so that all *
424 * variables clauses refer to are known. *)
425 let bag, fresh_current = Utils.add_to_bag bag fresh_current in
426 (* We superpose current with active clauses *)
427 let bag, maxvar, additional_new_clauses =
428 superposition_with_table bag maxvar fresh_current atable
430 debug "Another superposition";
431 let new_clauses = new_clauses @ additional_new_clauses in
432 let bag, new_clauses =
433 HExtlib.filter_map_acc (simplify atable maxvar) bag new_clauses
435 debug "Demodulated new clauses";
436 bag, maxvar, (alist, atable), new_clauses
439 let infer_left bag maxvar goal (_alist, atable) =
440 (* We superpose the goal with active clauses *)
441 let bag, maxvar, new_goals =
442 superposition_with_table bag maxvar goal atable
444 (* We demodulate the goal with active clauses *)
448 let bag, g = demodulate bag g atable in
452 bag, maxvar, List.rev new_goals