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 (Lazy.force s) ;;
23 | Unsatisfiable of (t Terms.bag * int * int list) list
26 | Timeout of int * t Terms.bag
32 t Terms.foterm * (t Terms.foterm list * t Terms.foterm list) ->
33 (t Terms.foterm * (t Terms.foterm list * t Terms.foterm list)) list -> szsontology
36 module Paramod (B : Orderings.Blob) = struct
39 | Unsatisfiable of (B.t Terms.bag * int * int list) list
42 | Timeout of int * B.t Terms.bag
43 exception Stop of szsontology
44 type bag = B.t Terms.bag * int
46 module FU = FoUnif.Founif(B)
47 module IDX = Index.Index(B)
48 module Sup = Superposition.Superposition(B)
49 module Utils = FoUtils.Utils(B)
51 module WeightOrderedPassives =
53 type t = B.t Terms.passive_clause
54 let compare = Utils.compare_passive_clauses_weight
57 module AgeOrderedPassives =
59 type t = B.t Terms.passive_clause
60 let compare = Utils.compare_passive_clauses_age
63 module WeightPassiveSet = Set.Make(WeightOrderedPassives)
64 module AgePassiveSet = Set.Make(AgeOrderedPassives)
66 let add_passive_clause ?(no_weight=false) (passives_w,passives_a) cl =
67 let cl = if no_weight then (0,cl)
68 else Utils.mk_passive_clause cl in
69 WeightPassiveSet.add cl passives_w, AgePassiveSet.add cl passives_a
72 let add_passive_goal ?(no_weight=false) (passives_w,passives_a) g =
73 let g = if no_weight then (0,g)
74 else Utils.mk_passive_goal g in
75 WeightPassiveSet.add g passives_w, AgePassiveSet.add g passives_a
78 let remove_passive_clause (passives_w,passives_a) cl =
79 let passives_w = WeightPassiveSet.remove cl passives_w in
80 let passives_a = AgePassiveSet.remove cl passives_a in
84 let add_passive_clauses ?(no_weight=false)
85 (passives_w,passives_a) new_clauses =
86 let new_clauses_w,new_clauses_a =
87 List.fold_left (add_passive_clause ~no_weight)
88 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
90 (WeightPassiveSet.union new_clauses_w passives_w,
91 AgePassiveSet.union new_clauses_a passives_a)
94 let add_passive_goals ?(no_weight=false)
95 (passives_w,passives_a) new_clauses =
96 let new_clauses_w,new_clauses_a =
97 List.fold_left (add_passive_goal ~no_weight)
98 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
100 (WeightPassiveSet.union new_clauses_w passives_w,
101 AgePassiveSet.union new_clauses_a passives_a)
104 let is_passive_set_empty (passives_w,passives_a) =
105 if (WeightPassiveSet.is_empty passives_w) then begin
106 assert (AgePassiveSet.is_empty passives_a); true
108 assert (not (AgePassiveSet.is_empty passives_a)); false
112 let passive_set_cardinal (passives_w,_) = WeightPassiveSet.cardinal passives_w
114 let passive_empty_set =
115 (WeightPassiveSet.empty,AgePassiveSet.empty)
118 let pick_min_passive ~use_age (passives_w,passives_a) =
119 if use_age then AgePassiveSet.min_elt passives_a
120 else WeightPassiveSet.min_elt passives_w
123 (* TODO : global age over facts and goals (without comparing weights) *)
124 let select ~use_age passives g_passives =
125 if is_passive_set_empty passives then begin
126 if (is_passive_set_empty g_passives) then
127 raise (Stop GaveUp) (* we say we are incomplete *)
129 let g_cl = pick_min_passive ~use_age:use_age g_passives in
130 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
132 else let cl = pick_min_passive ~use_age:use_age passives in
133 if is_passive_set_empty g_passives then
134 (false,cl,remove_passive_clause passives cl,g_passives)
136 let g_cl = pick_min_passive ~use_age:use_age g_passives in
137 let (id1,_,_,_,_),(id2,_,_,_,_) = snd cl, snd g_cl in
138 let cmp = if use_age then id1 <= id2
139 else fst cl <= fst g_cl
142 (false,cl,remove_passive_clause passives cl,g_passives)
144 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
147 let backward_infer_step bag maxvar actives passives
148 g_actives g_passives g_current iterno =
149 (* superposition left, simplifications on goals *)
150 debug (lazy "infer_left step...");
151 let bag, maxvar, new_goals =
152 Sup.infer_left bag maxvar g_current actives
154 debug (lazy "Performed infer_left step");
155 let bag = Terms.replace_in_bag (g_current,false,iterno) bag in
156 bag, maxvar, actives, passives, g_current::g_actives,
157 (add_passive_goals g_passives new_goals)
160 let forward_infer_step bag maxvar actives passives g_actives
161 g_passives current iterno =
166 * A' = demod [e'] A *
169 * new = supright e'' A'' *
170 * new'= demod A'' new *
172 debug (lazy "Forward infer step...");
173 debug (lazy("Number of actives : " ^ (string_of_int (List.length (fst actives)))));
174 let bag, maxvar, actives, new_clauses =
175 Sup.infer_right bag maxvar current actives
177 debug (lazy "Demodulating goals with actives...");
178 (* keep goals demodulated w.r.t. actives and check if solved *)
183 Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
186 | Some (bag,c1) -> bag,if c==c1 then c::acc else c::c1::acc)
189 let ctable = IDX.index_clause IDX.DT.empty current in
190 let bag, maxvar, new_goals =
192 (fun (bag,m,acc) g ->
193 let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
195 (bag,maxvar,[]) g_actives
197 let bag = Terms.replace_in_bag (current,false,iterno) bag in
198 bag, maxvar, actives,
199 add_passive_clauses passives new_clauses, g_actives,
200 add_passive_goals g_passives new_goals
203 let rec given_clause ~useage ~noinfer
204 bag maxvar iterno weight_picks max_steps timeout
205 actives passives g_actives g_passives
207 let iterno = iterno + 1 in
208 if iterno = max_steps then raise (Stop (Timeout (maxvar,bag)));
209 (* timeout check: gettimeofday called only if timeout set *)
210 if timeout <> None &&
212 | None -> assert false
213 | Some timeout -> Unix.gettimeofday () > timeout) then
217 (lazy("Last chance: all is indexed " ^ string_of_float
218 (Unix.gettimeofday())));
219 let maxgoals = 100 in
220 ignore(List.fold_left
223 ignore(Sup.simplify_goal ~no_demod:true
224 maxvar (snd actives) bag acc x)
229 raise (Stop (Timeout (maxvar,bag)))
231 else if false then (* activates last chance strategy *)
233 debug (lazy("Last chance: "^string_of_float (Unix.gettimeofday())));
234 given_clause ~useage ~noinfer:true bag maxvar iterno weight_picks max_steps
235 (Some (Unix.gettimeofday () +. 20.))
236 actives passives g_actives g_passives;
237 raise (Stop (Timeout (maxvar,bag)));
239 else raise (Stop (Timeout (maxvar,bag)));
241 let use_age = useage && (weight_picks = (iterno / 6 + 1)) in
242 let weight_picks = if use_age then 0 else weight_picks+1
245 let rec aux_select bag passives g_passives =
246 let backward,(weight,current),passives,g_passives =
247 select ~use_age passives g_passives
249 if use_age && weight > monster then
250 let bag,cl = Terms.add_to_bag current bag in
252 aux_select bag passives (add_passive_clause g_passives cl)
254 aux_select bag (add_passive_clause passives cl) g_passives
256 let bag = Terms.replace_in_bag (current,false,iterno) bag in
258 let _ = debug (lazy("Selected goal : " ^ Pp.pp_clause current)) in
261 if weight > monster then None else Some (bag,current)
264 ~no_demod:false maxvar (snd actives) bag g_actives current
266 | None -> aux_select bag passives g_passives
267 | Some (bag,g_current) ->
269 let g_actives = g_current :: g_actives in
270 bag,maxvar,actives,passives,g_actives,g_passives
272 backward_infer_step bag maxvar actives passives
273 g_actives g_passives g_current iterno
275 let _ = debug (lazy("Selected fact : " ^ Pp.pp_clause current)) in
276 (*let is_orphan = Sup.orphan_murder bag (fst actives) current in*)
279 if weight > monster then bag,None
280 else bag, Some (current,actives)
281 else if Sup.orphan_murder bag (fst actives) current then
282 let _ = debug (lazy "Orphan murdered") in
283 let bag = Terms.replace_in_bag (current,true,iterno) bag in
285 else Sup.keep_simplified current actives bag maxvar
287 (*match Sup.one_pass_simplification current actives bag maxvar with*)
288 | bag,None -> aux_select bag passives g_passives
289 | bag,Some (current,actives) ->
290 (* if is_orphan then prerr_endline
291 ("WRONG discarded: " ^ (Pp.pp_unit_clause current));
293 prerr_endline (Pp.pp_unit_clause x))
296 (* List.iter (fun (id,_,_,_) -> let (cl,d) =
297 Terms.M.find id bag in
298 if d then prerr_endline
299 ("WRONG discarded: " ^ (Pp.pp_unit_clause cl)))
300 (current::fst actives);*)
303 current::fst actives,
304 IDX.index_clause (snd actives) current
306 bag,maxvar,actives,passives,g_actives,g_passives
308 forward_infer_step bag maxvar actives passives
309 g_actives g_passives current iterno
313 (*prerr_endline "Active table :";
314 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
316 let bag,maxvar,actives,passives,g_actives,g_passives =
317 aux_select bag passives g_passives
320 (lazy(Printf.sprintf "Number of active goals : %d"
321 (List.length g_actives)));
323 (lazy(Printf.sprintf "Number of passive goals : %d"
324 (passive_set_cardinal g_passives)));
326 (lazy(Printf.sprintf "Number of actives : %d" (List.length (fst actives))));
328 (lazy(Printf.sprintf "Number of passives : %d"
329 (passive_set_cardinal passives)));
330 given_clause ~useage ~noinfer
331 bag maxvar iterno weight_picks max_steps timeout
332 actives passives g_actives g_passives
335 let paramod ~useage ~max_steps ~print_problem ?timeout goal hypotheses =
336 let initial_timestamp = Unix.gettimeofday () in
337 let bag = Terms.empty_bag in
339 let build_clause (bag,maxvar,l) (t,(nlit,plit)) =
340 let c,maxvar = Utils.mk_clause maxvar nlit plit t in
341 let bag,c = Terms.add_to_bag c bag in
344 let bag,maxvar,hypotheses = List.fold_left build_clause (bag,maxvar,[]) hypotheses in
345 let bag,maxvar,goals = build_clause (bag,maxvar,[]) goal in
346 let goal = match goals with | [g] -> g | _ -> assert false in
348 add_passive_clauses ~no_weight:true passive_empty_set hypotheses
351 add_passive_goal ~no_weight:true passive_empty_set goal
353 let g_actives = [] in
354 let actives = [], IDX.DT.empty in
355 if print_problem then begin
356 prerr_endline "Facts:";
357 List.iter (fun x -> prerr_endline (" " ^ Pp.pp_clause x)) hypotheses;
358 prerr_endline "Goal:";
359 prerr_endline (" " ^ Pp.pp_clause goal);
362 given_clause ~useage ~noinfer:false
363 bag maxvar 0 0 max_steps timeout actives passives g_actives g_passives
365 | Sup.Success (bag, _, (i,_,_,_,_)) ->
367 let rec traverse ongoal (accg,acce) i =
368 match Terms.get_from_bag i bag with
369 | (id,_,_,_,Terms.Exact _),_,_ ->
370 if ongoal then [i],acce else
371 if (List.mem i acce) then accg,acce else accg,acce@[i]
372 | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),_,_ ->
373 if (not ongoal) && (List.mem i acce) then accg,acce
376 traverse false (traverse ongoal (accg,acce) i1) i2
378 if ongoal then i::accg,acce else accg,i::acce
380 let gsteps,esteps = traverse true ([],[]) i in
381 (List.rev esteps)@gsteps
383 let max_w = List.fold_left (fun acc i ->
384 let (cl,_,_) = Terms.get_from_bag i bag in
385 max acc (Order.compute_clause_weight cl)) 0 l in
386 prerr_endline "Statistics :";
387 prerr_endline ("Max weight : " ^ (string_of_int max_w));
388 (* List.iter (fun id -> let ((_,lit,_,proof as cl),d,it) =
389 Terms.get_from_bag id bag in
392 (Printf.sprintf "Id : %d, selected at %d, weight %d,disc, by %s"
393 id it (Order.compute_unit_clause_weight cl)
394 (Pp.pp_proof_step proof))
397 (Printf.sprintf "Id : %d, selected at %d, weight %d by %s"
398 id it (Order.compute_unit_clause_weight cl)
399 (Pp.pp_proof_step proof))) l;*)
401 (Printf.sprintf "Found proof, %fs"
402 (Unix.gettimeofday() -. initial_timestamp));
404 prerr_endline "Proof:";
406 prerr_endline (Pp.pp_unit_clause (fst(Terms.M.find x bag)))) l;
408 Unsatisfiable [ bag, i, l ]
409 | Stop (Unsatisfiable _) -> Error "stop bug solution found!"