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) ;;
15 (* let debug _ = ();; *)
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 Clauses = Clauses.Clauses(B)
52 module WeightOrderedPassives =
54 type t = B.t Terms.passive_clause
55 let compare = Clauses.compare_passive_clauses_weight
58 module AgeOrderedPassives =
60 type t = B.t Terms.passive_clause
61 let compare = Clauses.compare_passive_clauses_age
64 module WeightPassiveSet = Set.Make(WeightOrderedPassives)
65 module AgePassiveSet = Set.Make(AgeOrderedPassives)
67 let add_passive_clause ?(bonus_weight=0) (passives_w,passives_a) cl =
68 let (w,cl) = Clauses.mk_passive_clause cl in
69 let cl = (w+bonus_weight,cl) in
70 WeightPassiveSet.add cl passives_w, AgePassiveSet.add cl passives_a
73 let add_passive_goal ?(bonus_weight=0) (passives_w,passives_a) g =
74 let (w,g) = Clauses.mk_passive_goal g in
75 let g = (w+bonus_weight,g) in
76 WeightPassiveSet.add g passives_w, AgePassiveSet.add g passives_a
79 let remove_passive_clause (passives_w,passives_a) cl =
80 let passives_w = WeightPassiveSet.remove cl passives_w in
81 let passives_a = AgePassiveSet.remove cl passives_a in
85 let add_passive_clauses ?(bonus_weight=0)
86 (passives_w,passives_a) new_clauses =
87 let new_clauses_w,new_clauses_a =
88 List.fold_left (add_passive_clause ~bonus_weight)
89 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
91 (WeightPassiveSet.union new_clauses_w passives_w,
92 AgePassiveSet.union new_clauses_a passives_a)
95 let add_passive_goals ?(bonus_weight=0)
96 (passives_w,passives_a) new_clauses =
97 let new_clauses_w,new_clauses_a =
98 List.fold_left (add_passive_goal ~bonus_weight)
99 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
101 (WeightPassiveSet.union new_clauses_w passives_w,
102 AgePassiveSet.union new_clauses_a passives_a)
105 let is_passive_set_empty (passives_w,passives_a) =
106 if (WeightPassiveSet.is_empty passives_w) then begin
107 assert (AgePassiveSet.is_empty passives_a); true
109 assert (not (AgePassiveSet.is_empty passives_a)); false
113 let passive_set_cardinal (passives_w,_) = WeightPassiveSet.cardinal passives_w
115 let passive_empty_set =
116 (WeightPassiveSet.empty,AgePassiveSet.empty)
119 let pick_min_passive ~use_age (passives_w,passives_a) =
120 if use_age then AgePassiveSet.min_elt passives_a
121 else WeightPassiveSet.min_elt passives_w
124 (* TODO : global age over facts and goals (without comparing weights) *)
125 let select ~use_age passives g_passives =
126 if is_passive_set_empty passives then begin
127 if (is_passive_set_empty g_passives) then
128 raise (Stop GaveUp) (* we say we are incomplete *)
130 let g_cl = pick_min_passive ~use_age:use_age g_passives in
131 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
133 else let cl = pick_min_passive ~use_age:use_age passives in
134 if is_passive_set_empty g_passives then
135 (false,cl,remove_passive_clause passives cl,g_passives)
137 let g_cl = pick_min_passive ~use_age:use_age g_passives in
138 let (id1,_,_,_,_),(id2,_,_,_,_) = snd cl, snd g_cl in
139 let cmp = if use_age then id1 <= id2
140 else fst cl <= fst g_cl
143 (false,cl,remove_passive_clause passives cl,g_passives)
145 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
148 let backward_infer_step bag maxvar actives passives
149 g_actives g_passives g_current iterno =
150 (* superposition left, simplifications on goals *)
151 debug (lazy "infer_left step...");
152 let bag, maxvar, new_goals =
153 Sup.infer_left bag maxvar g_current actives
155 debug (lazy "Performed infer_left step");
156 let bag = Terms.replace_in_bag (g_current,false,iterno) bag in
157 bag, maxvar, actives, passives, g_current::g_actives,
158 (add_passive_goals g_passives new_goals)
161 let forward_infer_step bag maxvar actives passives g_actives
162 g_passives current iterno =
167 * A' = demod [e'] A *
170 * new = supright e'' A'' *
171 * new'= demod A'' new *
173 debug (lazy "Forward infer step...");
174 debug (lazy("Number of actives : " ^ (string_of_int (List.length (fst actives)))));
175 let bag, maxvar, actives, new_clauses =
176 Sup.infer_right bag maxvar current actives
178 debug (lazy "Demodulating goals with actives...");
179 (* keep goals demodulated w.r.t. actives and check if solved *)
184 Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
187 | Some (bag,c1) -> bag,if c==c1 then c::acc else c::c1::acc)
190 let ctable = IDX.index_clause IDX.DT.empty current in
191 let bag, maxvar, new_goals =
193 (fun (bag,m,acc) g ->
194 let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
196 (bag,maxvar,[]) g_actives
198 let bag = Terms.replace_in_bag (current,false,iterno) bag in
199 bag, maxvar, actives,
200 add_passive_clauses passives new_clauses, g_actives,
201 add_passive_goals g_passives new_goals
204 let rec given_clause ~useage ~noinfer
205 bag maxvar iterno weight_picks max_steps timeout
206 actives passives g_actives g_passives
208 let iterno = iterno + 1 in
209 if iterno = max_steps then raise (Stop (Timeout (maxvar,bag)));
210 (* timeout check: gettimeofday called only if timeout set *)
211 if timeout <> None &&
213 | None -> assert false
214 | Some timeout -> Unix.gettimeofday () > timeout) then
218 (lazy("Last chance: all is indexed " ^ string_of_float
219 (Unix.gettimeofday())));
220 let maxgoals = 100 in
221 ignore(List.fold_left
224 ignore(Sup.simplify_goal ~no_demod:true
225 maxvar (snd actives) bag acc x)
230 raise (Stop (Timeout (maxvar,bag)))
232 else if false then (* activates last chance strategy *)
234 debug (lazy("Last chance: "^string_of_float (Unix.gettimeofday())));
235 given_clause ~useage ~noinfer:true bag maxvar iterno weight_picks max_steps
236 (Some (Unix.gettimeofday () +. 20.))
237 actives passives g_actives g_passives;
238 raise (Stop (Timeout (maxvar,bag)));
240 else raise (Stop (Timeout (maxvar,bag)));
242 let use_age = useage && (weight_picks = (iterno / 6 + 1)) in
243 let weight_picks = if use_age then 0 else weight_picks+1
246 let rec aux_select bag passives g_passives =
247 let backward,(weight,current),passives,g_passives =
248 select ~use_age passives g_passives
250 if use_age && weight > monster then
251 let bag,cl = Terms.add_to_bag current bag in
253 aux_select bag passives (add_passive_clause g_passives cl)
255 aux_select bag (add_passive_clause passives cl) g_passives
257 let bag = Terms.replace_in_bag (current,false,iterno) bag in
259 let _ = debug (lazy("Selected goal : " ^ Pp.pp_clause current)) in
262 if weight > monster then None else Some (bag,current)
265 ~no_demod:false maxvar (snd actives) bag g_actives current
267 | None -> aux_select bag passives g_passives
268 | Some (bag,g_current) ->
270 let g_actives = g_current :: g_actives in
271 bag,maxvar,actives,passives,g_actives,g_passives
273 backward_infer_step bag maxvar actives passives
274 g_actives g_passives g_current iterno
276 let _ = debug (lazy("Selected fact : " ^ Pp.pp_clause current)) in
277 (*let is_orphan = Sup.orphan_murder bag (fst actives) current in*)
280 if weight > monster then bag,None
281 else bag, Some (current,actives)
282 else if Sup.orphan_murder bag (fst actives) current then
283 let _ = debug (lazy "Orphan murdered") in
284 let bag = Terms.replace_in_bag (current,true,iterno) bag in
286 else Sup.keep_simplified current actives bag maxvar
288 (*match Sup.one_pass_simplification current actives bag maxvar with*)
289 | bag,None -> aux_select bag passives g_passives
290 | bag,Some (current,actives) ->
291 (* if is_orphan then prerr_endline
292 ("WRONG discarded: " ^ (Pp.pp_unit_clause current));
294 prerr_endline (Pp.pp_unit_clause x))
297 (* List.iter (fun (id,_,_,_) -> let (cl,d) =
298 Terms.M.find id bag in
299 if d then prerr_endline
300 ("WRONG discarded: " ^ (Pp.pp_unit_clause cl)))
301 (current::fst actives);*)
304 current::fst actives,
305 IDX.index_clause (snd actives) current
307 bag,maxvar,actives,passives,g_actives,g_passives
309 forward_infer_step bag maxvar actives passives
310 g_actives g_passives current iterno
314 (*prerr_endline "Active table :";
315 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
317 let bag,maxvar,actives,passives,g_actives,g_passives =
318 aux_select bag passives g_passives
321 (lazy(Printf.sprintf "Number of active goals : %d"
322 (List.length g_actives)));
324 (lazy(Printf.sprintf "Number of passive goals : %d"
325 (passive_set_cardinal g_passives)));
327 (lazy(Printf.sprintf "Number of actives : %d" (List.length (fst actives))));
329 (lazy(Printf.sprintf "Number of passives : %d"
330 (passive_set_cardinal passives)));
331 given_clause ~useage ~noinfer
332 bag maxvar iterno weight_picks max_steps timeout
333 actives passives g_actives g_passives
336 let paramod ~useage ~max_steps ~print_problem ?timeout goal hypotheses =
337 let initial_timestamp = Unix.gettimeofday () in
338 let bag = Terms.empty_bag in
340 let build_clause (bag,maxvar,l) (t,(nlit,plit)) =
341 let c,maxvar = Clauses.mk_clause maxvar nlit plit t in
342 let bag,c = Terms.add_to_bag c bag in
345 let bag,maxvar,goals = build_clause (bag,maxvar,[]) goal in
346 let bag,maxvar,hypotheses = List.fold_left build_clause (bag,maxvar,[]) hypotheses in
347 let goal = match goals with | [g] -> g | _ -> assert false in
349 add_passive_clauses ~bonus_weight:(-1000) passive_empty_set hypotheses
352 add_passive_goal ~bonus_weight:(-1000) passive_empty_set goal
354 let g_actives = [] in
355 let actives = [], IDX.DT.empty in
356 if print_problem then begin
357 prerr_endline "Facts:";
358 List.iter (fun x -> prerr_endline (" " ^ Pp.pp_clause x)) hypotheses;
359 prerr_endline "Goal:";
360 prerr_endline (" " ^ Pp.pp_clause goal);
363 given_clause ~useage ~noinfer:false
364 bag maxvar 0 0 max_steps timeout actives passives g_actives g_passives
366 | Sup.Success (bag, _, (i,_,_,_,_)) ->
368 let rec traverse ongoal (accg,acce) i =
369 match Terms.get_from_bag i bag with
370 | (id,_,_,_,Terms.Exact _),_,_ ->
371 if ongoal then [i],acce else
372 if (List.mem i acce) then accg,acce else accg,acce@[i]
373 | (_,_,_,_,Terms.Step (_,i1,i2,_,_,_)),_,_ ->
374 if (not ongoal) && (List.mem i acce) then accg,acce
377 traverse false (traverse ongoal (accg,acce) i1) i2
379 if ongoal then i::accg,acce else accg,i::acce
381 let gsteps,esteps = traverse true ([],[]) i in
382 (List.rev esteps)@gsteps
384 let max_w = List.fold_left (fun acc i ->
385 let (cl,_,_) = Terms.get_from_bag i bag in
386 max acc (Order.compute_clause_weight cl)) 0 l in
387 prerr_endline "Statistics :";
388 prerr_endline ("Max weight : " ^ (string_of_int max_w));
389 (* List.iter (fun id -> let ((_,lit,_,proof as cl),d,it) =
390 Terms.get_from_bag id bag in
393 (Printf.sprintf "Id : %d, selected at %d, weight %d,disc, by %s"
394 id it (Order.compute_unit_clause_weight cl)
395 (Pp.pp_proof_step proof))
398 (Printf.sprintf "Id : %d, selected at %d, weight %d by %s"
399 id it (Order.compute_unit_clause_weight cl)
400 (Pp.pp_proof_step proof))) l;*)
402 (Printf.sprintf "Found proof, %fs"
403 (Unix.gettimeofday() -. initial_timestamp));
405 prerr_endline "Proof:";
407 prerr_endline (Pp.pp_unit_clause (fst(Terms.M.find x bag)))) l;
409 Unsatisfiable [ bag, i, l ]
410 | Stop (Unsatisfiable _) -> Error "stop bug solution found!"