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: orderings.ml 9869 2009-06-11 22:52:38Z denes $ *)
14 let debug s = prerr_endline s ;;
19 module Paramod (B : Orderings.Blob) = struct
21 | Unsatisfiable of (B.t Terms.bag * int * int list) list
24 | Timeout of int * B.t Terms.bag
25 exception Stop of szsontology
26 type bag = B.t Terms.bag * int
28 module FU = FoUnif.Founif(B)
29 module IDX = Index.Index(B)
30 module Sup = Superposition.Superposition(B)
31 module Utils = FoUtils.Utils(B)
33 module WeightOrderedPassives =
35 type t = B.t Terms.passive_clause
36 let compare = Utils.compare_passive_clauses_weight
39 module AgeOrderedPassives =
41 type t = B.t Terms.passive_clause
42 let compare = Utils.compare_passive_clauses_age
45 module WeightPassiveSet = Set.Make(WeightOrderedPassives)
46 module AgePassiveSet = Set.Make(AgeOrderedPassives)
48 let add_passive_clause ?(no_weight=false) (passives_w,passives_a) cl =
49 let cl = if no_weight then (0,cl)
50 else Utils.mk_passive_clause cl in
51 WeightPassiveSet.add cl passives_w, AgePassiveSet.add cl passives_a
54 let add_passive_goal ?(no_weight=false) (passives_w,passives_a) g =
55 let g = if no_weight then (0,g)
56 else Utils.mk_passive_goal g in
57 WeightPassiveSet.add g passives_w, AgePassiveSet.add g passives_a
60 let remove_passive_clause (passives_w,passives_a) cl =
61 let passives_w = WeightPassiveSet.remove cl passives_w in
62 let passives_a = AgePassiveSet.remove cl passives_a in
66 let add_passive_clauses ?(no_weight=false)
67 (passives_w,passives_a) new_clauses =
68 let new_clauses_w,new_clauses_a =
69 List.fold_left (add_passive_clause ~no_weight)
70 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
72 (WeightPassiveSet.union new_clauses_w passives_w,
73 AgePassiveSet.union new_clauses_a passives_a)
76 let add_passive_goals ?(no_weight=false)
77 (passives_w,passives_a) new_clauses =
78 let new_clauses_w,new_clauses_a =
79 List.fold_left (add_passive_goal ~no_weight)
80 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
82 (WeightPassiveSet.union new_clauses_w passives_w,
83 AgePassiveSet.union new_clauses_a passives_a)
86 let is_passive_set_empty (passives_w,passives_a) =
87 if (WeightPassiveSet.is_empty passives_w) then begin
88 assert (AgePassiveSet.is_empty passives_a); true
90 assert (not (AgePassiveSet.is_empty passives_a)); false
94 let passive_set_cardinal (passives_w,_) = WeightPassiveSet.cardinal passives_w
96 let passive_empty_set =
97 (WeightPassiveSet.empty,AgePassiveSet.empty)
100 let pick_min_passive ~use_age (passives_w,passives_a) =
101 if use_age then AgePassiveSet.min_elt passives_a
102 else WeightPassiveSet.min_elt passives_w
105 let mk_clause bag maxvar (t,ty) =
106 let (proof,ty) = B.saturate t ty in
107 let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
108 let bag, c = Terms.add_to_bag c bag in
112 let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
113 let mk_goal (bag,maxvar) = mk_clause bag maxvar;;
115 (* TODO : global age over facts and goals (without comparing weights) *)
116 let select ~use_age passives g_passives =
117 if is_passive_set_empty passives then begin
118 if (is_passive_set_empty g_passives) then
119 raise (Stop GaveUp) (* we say we are incomplete *)
121 let g_cl = pick_min_passive ~use_age:use_age g_passives in
122 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
124 else let cl = pick_min_passive ~use_age:use_age passives in
125 if is_passive_set_empty g_passives then
126 (false,cl,remove_passive_clause passives cl,g_passives)
128 let g_cl = pick_min_passive ~use_age:use_age g_passives in
129 let (id1,_,_,_),(id2,_,_,_) = snd cl, snd g_cl in
130 let cmp = if use_age then id1 <= id2
131 else fst cl <= fst g_cl
134 (false,cl,remove_passive_clause passives cl,g_passives)
136 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
139 let backward_infer_step bag maxvar actives passives
140 g_actives g_passives g_current iterno =
141 (* superposition left, simplifications on goals *)
142 debug "infer_left step...";
143 let bag, maxvar, new_goals =
144 Sup.infer_left bag maxvar g_current actives
146 debug "Performed infer_left step";
147 let bag = Terms.replace_in_bag (g_current,false,iterno) bag in
148 bag, maxvar, actives, passives, g_current::g_actives,
149 (add_passive_goals g_passives new_goals)
152 let forward_infer_step bag maxvar actives passives g_actives
153 g_passives current iterno =
158 * A' = demod [e'] A *
161 * new = supright e'' A'' *
162 * new'= demod A'' new *
164 debug "Forward infer step...";
165 let bag, maxvar, actives, new_clauses =
166 Sup.infer_right bag maxvar current actives
168 debug "Demodulating goals with actives...";
169 (* keep goals demodulated w.r.t. actives and check if solved *)
174 Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
177 | Some (bag,c1) -> bag,if c==c1 then c::acc else c::c1::acc)
180 let ctable = IDX.index_unit_clause IDX.DT.empty current in
181 let bag, maxvar, new_goals =
183 (fun (bag,m,acc) g ->
184 let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
186 (bag,maxvar,[]) g_actives
188 let bag = Terms.replace_in_bag (current,false,iterno) bag in
189 bag, maxvar, actives,
190 add_passive_clauses passives new_clauses, g_actives,
191 add_passive_goals g_passives new_goals
194 let rec given_clause ~noinfer
195 bag maxvar iterno weight_picks max_steps timeout
196 actives passives g_actives g_passives
198 let iterno = iterno + 1 in
199 if iterno = max_steps then raise (Stop (Timeout (maxvar,bag)));
200 (* timeout check: gettimeofday called only if timeout set *)
201 if timeout <> None &&
203 | None -> assert false
204 | Some timeout -> Unix.gettimeofday () > timeout) then
208 ("Last chance: all is indexed " ^ string_of_float
209 (Unix.gettimeofday()));
210 let maxgoals = 100 in
211 ignore(List.fold_left
214 ignore(Sup.simplify_goal ~no_demod:true
215 maxvar (snd actives) bag acc x)
220 raise (Stop (Timeout (maxvar,bag)))
222 else if false then (* activates last chance strategy *)
224 debug("Last chance: "^string_of_float (Unix.gettimeofday()));
225 given_clause ~noinfer:true bag maxvar iterno weight_picks max_steps
226 (Some (Unix.gettimeofday () +. 20.))
227 actives passives g_actives g_passives;
228 raise (Stop (Timeout (maxvar,bag)));
230 else raise (Stop (Timeout (maxvar,bag)));
232 let use_age = weight_picks = (iterno / 6 + 1) in
233 let weight_picks = if use_age then 0 else weight_picks+1
236 let rec aux_select bag passives g_passives =
237 let backward,(weight,current),passives,g_passives =
238 select ~use_age passives g_passives
240 if use_age && weight > monster then
241 let bag,cl = Terms.add_to_bag current bag in
243 aux_select bag passives (add_passive_clause g_passives cl)
245 aux_select bag (add_passive_clause passives cl) g_passives
247 let bag = Terms.replace_in_bag (current,false,iterno) bag in
249 let _ = debug ("Selected goal : " ^ Pp.pp_unit_clause current) in
252 if weight > monster then None else Some (bag,current)
255 ~no_demod:false maxvar (snd actives) bag g_actives current
257 | None -> aux_select bag passives g_passives
258 | Some (bag,g_current) ->
260 let g_actives = g_current :: g_actives in
261 bag,maxvar,actives,passives,g_actives,g_passives
263 backward_infer_step bag maxvar actives passives
264 g_actives g_passives g_current iterno
266 let _ = debug ("Selected fact : " ^ Pp.pp_unit_clause current) in
267 (*let is_orphan = Sup.orphan_murder bag (fst actives) current in*)
270 if weight > monster then bag,None
271 else bag, Some (current,actives)
272 else if Sup.orphan_murder bag (fst actives) current then
273 let _ = debug "Orphan murdered" in
274 let bag = Terms.replace_in_bag (current,true,iterno) bag in
276 else Sup.keep_simplified current actives bag maxvar
278 (*match Sup.one_pass_simplification current actives bag maxvar with*)
279 | bag,None -> aux_select bag passives g_passives
280 | bag,Some (current,actives) ->
281 (* if is_orphan then prerr_endline
282 ("WRONG discarded: " ^ (Pp.pp_unit_clause current));
284 prerr_endline (Pp.pp_unit_clause x))
287 (* List.iter (fun (id,_,_,_) -> let (cl,d) =
288 Terms.M.find id bag in
289 if d then prerr_endline
290 ("WRONG discarded: " ^ (Pp.pp_unit_clause cl)))
291 (current::fst actives);*)
294 current::fst actives,
295 IDX.index_unit_clause (snd actives) current
297 bag,maxvar,actives,passives,g_actives,g_passives
299 forward_infer_step bag maxvar actives passives
300 g_actives g_passives current iterno
304 (*prerr_endline "Active table :";
305 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
308 let bag,maxvar,actives,passives,g_actives,g_passives =
309 aux_select bag passives g_passives
312 (Printf.sprintf "Number of active goals : %d"
313 (List.length g_actives));
315 (Printf.sprintf "Number of passive goals : %d"
316 (passive_set_cardinal g_passives));
318 (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
320 (Printf.sprintf "Number of passives : %d"
321 (passive_set_cardinal passives));
322 given_clause ~noinfer
323 bag maxvar iterno weight_picks max_steps timeout
324 actives passives g_actives g_passives
327 let paramod ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
328 let initial_timestamp = Unix.gettimeofday () in
330 add_passive_clauses ~no_weight:true passive_empty_set passives
333 add_passive_goals ~no_weight:true passive_empty_set g_passives
335 let g_actives = [] in
336 let actives = [], IDX.DT.empty in
338 given_clause ~noinfer:false
339 bag maxvar 0 0 max_steps timeout actives passives g_actives g_passives
341 | Sup.Success (bag, _, (i,_,_,_)) ->
343 let rec traverse ongoal (accg,acce) i =
344 match Terms.get_from_bag i bag with
345 | (id,_,_,Terms.Exact _),_,_ ->
346 if ongoal then [i],acce else
347 if (List.mem i acce) then accg,acce else accg,acce@[i]
348 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),_,_ ->
349 if (not ongoal) && (List.mem i acce) then accg,acce
352 traverse false (traverse ongoal (accg,acce) i1) i2
354 if ongoal then i::accg,acce else accg,i::acce
356 let gsteps,esteps = traverse true ([],[]) i in
357 (List.rev esteps)@gsteps
359 let max_w = List.fold_left (fun acc i ->
360 let (cl,_,_) = Terms.get_from_bag i bag in
361 max acc (Order.compute_unit_clause_weight cl)) 0 l in
362 prerr_endline "Statistics :";
363 prerr_endline ("Max weight : " ^ (string_of_int max_w));
364 (* List.iter (fun id -> let ((_,lit,_,proof as cl),d,it) =
365 Terms.get_from_bag id bag in
368 (Printf.sprintf "Id : %d, selected at %d, weight %d,disc, by %s"
369 id it (Order.compute_unit_clause_weight cl)
370 (Pp.pp_proof_step proof))
373 (Printf.sprintf "Id : %d, selected at %d, weight %d by %s"
374 id it (Order.compute_unit_clause_weight cl)
375 (Pp.pp_proof_step proof))) l;*)
377 (Printf.sprintf "Found proof, %fs"
378 (Unix.gettimeofday() -. initial_timestamp));
380 prerr_endline "Proof:";
382 prerr_endline (Pp.pp_unit_clause (fst(Terms.M.find x bag)))) l;
384 Unsatisfiable [ bag, i, l ]
385 | Stop (Unsatisfiable _) -> Error "stop bug solution found!"