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) ;;
25 | Unsatisfiable of (t Terms.bag * int * int list) list
28 | Timeout of int * t Terms.bag
29 type bag = t Terms.bag * int
30 val empty_state : state
31 val mk_passive : bag -> input * input -> bag * t Terms.unit_clause
32 val mk_goal : bag -> input * input -> bag * t Terms.unit_clause
33 val forward_infer_step :
35 t Terms.unit_clause ->
43 g_passives:t Terms.unit_clause list ->
44 passives:t Terms.unit_clause list -> szsontology
47 module Paramod (B : Orderings.Blob) = struct
49 module FU = FoUnif.Founif(B)
50 module IDX = Index.Index(B)
51 module Sup = Superposition.Superposition(B)
52 module Utils = FoUtils.Utils(B)
54 module WeightOrderedPassives =
56 type t = B.t Terms.passive_clause
57 let compare = Utils.compare_passive_clauses_weight
60 module AgeOrderedPassives =
62 type t = B.t Terms.passive_clause
63 let compare = Utils.compare_passive_clauses_age
66 module WeightPassiveSet = Set.Make(WeightOrderedPassives)
67 module AgePassiveSet = Set.Make(AgeOrderedPassives)
74 * Index.Index(B).active_set
75 * (WeightPassiveSet.t * AgePassiveSet.t)
76 * B.t Terms.unit_clause list
77 * (WeightPassiveSet.t * AgePassiveSet.t)
79 | Unsatisfiable of (B.t Terms.bag * int * int list) list
82 | Timeout of int * B.t Terms.bag
83 exception Stop of szsontology
84 type bag = B.t Terms.bag * int
90 (WeightPassiveSet.empty,AgePassiveSet.empty),
92 (WeightPassiveSet.empty,AgePassiveSet.empty)
95 let add_passive_clause ?(no_weight=false) (passives_w,passives_a) cl =
96 let cl = if no_weight then (0,cl)
97 else Utils.mk_passive_clause cl in
98 WeightPassiveSet.add cl passives_w, AgePassiveSet.add cl passives_a
101 let add_passive_goal ?(no_weight=false) (passives_w,passives_a) g =
102 let g = if no_weight then (0,g)
103 else Utils.mk_passive_goal g in
104 WeightPassiveSet.add g passives_w, AgePassiveSet.add g passives_a
107 let remove_passive_clause (passives_w,passives_a) cl =
108 let passives_w = WeightPassiveSet.remove cl passives_w in
109 let passives_a = AgePassiveSet.remove cl passives_a in
110 passives_w,passives_a
113 let add_passive_clauses ?(no_weight=false)
114 (passives_w,passives_a) new_clauses =
115 let new_clauses_w,new_clauses_a =
116 List.fold_left (add_passive_clause ~no_weight)
117 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
119 (WeightPassiveSet.union new_clauses_w passives_w,
120 AgePassiveSet.union new_clauses_a passives_a)
123 let add_passive_goals ?(no_weight=false)
124 (passives_w,passives_a) new_clauses =
125 let new_clauses_w,new_clauses_a =
126 List.fold_left (add_passive_goal ~no_weight)
127 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
129 (WeightPassiveSet.union new_clauses_w passives_w,
130 AgePassiveSet.union new_clauses_a passives_a)
133 let is_passive_set_empty (passives_w,passives_a) =
134 if (WeightPassiveSet.is_empty passives_w) then begin
135 assert (AgePassiveSet.is_empty passives_a); true
137 assert (not (AgePassiveSet.is_empty passives_a)); false
141 let passive_set_cardinal (passives_w,_) = WeightPassiveSet.cardinal passives_w
143 let passive_empty_set =
144 (WeightPassiveSet.empty,AgePassiveSet.empty)
147 let pick_min_passive ~use_age (passives_w,passives_a) =
148 if use_age then AgePassiveSet.min_elt passives_a
149 else WeightPassiveSet.min_elt passives_w
152 let mk_clause bag maxvar (t,ty) =
153 let (proof,ty) = B.saturate t ty in
154 let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
155 let bag, c = Terms.add_to_bag c bag in
159 let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
160 let mk_goal (bag,maxvar) = mk_clause bag maxvar;;
162 (* TODO : global age over facts and goals (without comparing weights) *)
163 let select ~use_age passives g_passives =
164 if is_passive_set_empty passives then begin
165 if (is_passive_set_empty g_passives) then
166 raise (Stop GaveUp) (* we say we are incomplete *)
168 let g_cl = pick_min_passive ~use_age:use_age g_passives in
169 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
171 else let cl = pick_min_passive ~use_age:use_age passives in
172 if is_passive_set_empty g_passives then
173 (false,cl,remove_passive_clause passives cl,g_passives)
175 let g_cl = pick_min_passive ~use_age:use_age g_passives in
176 let (id1,_,_,_),(id2,_,_,_) = snd cl, snd g_cl in
177 let cmp = if use_age then id1 <= id2
178 else fst cl <= fst g_cl
181 (false,cl,remove_passive_clause passives cl,g_passives)
183 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
186 let backward_infer_step bag maxvar actives passives
187 g_actives g_passives g_current iterno =
188 (* superposition left, simplifications on goals *)
189 debug (lazy "infer_left step...");
190 let bag, maxvar, new_goals =
191 Sup.infer_left bag maxvar g_current actives
193 debug (lazy "Performed infer_left step");
194 let bag = Terms.replace_in_bag (g_current,false,iterno) bag in
195 bag, maxvar, actives, passives, g_current::g_actives,
196 (add_passive_goals g_passives new_goals)
199 let forward_infer_step (bag,maxvar,actives,passives,g_actives,g_passives)
205 * A' = demod [e'] A *
208 * new = supright e'' A'' *
209 * new'= demod A'' new *
211 debug (lazy "Forward infer step...");
212 debug (lazy("Number of actives : " ^ (string_of_int (List.length (fst actives)))));
213 let bag, maxvar, actives, new_clauses =
214 Sup.infer_right bag maxvar current actives
216 debug (lazy "Demodulating goals with actives...");
217 (* keep goals demodulated w.r.t. actives and check if solved *)
222 Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
225 | Some (bag,c1) -> bag,if c==c1 then c::acc else c::c1::acc)
228 let ctable = IDX.index_unit_clause maxvar IDX.DT.empty current in
229 let bag, maxvar, new_goals =
231 (fun (bag,m,acc) g ->
232 let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
234 (bag,maxvar,[]) g_actives
236 let bag = Terms.replace_in_bag (current,false,iterno) bag in
237 (* prerr_endline (Pp.pp_bag bag); *)
238 bag, maxvar, actives,
239 add_passive_clauses passives new_clauses, g_actives,
240 add_passive_goals g_passives new_goals
243 let rec given_clause ~useage ~noinfer
244 bag maxvar iterno weight_picks max_steps timeout
245 actives passives g_actives g_passives
247 let iterno = iterno + 1 in
248 if iterno = max_steps then raise (Stop (Timeout (maxvar,bag)));
249 (* timeout check: gettimeofday called only if timeout set *)
250 if timeout <> None &&
252 | None -> assert false
253 | Some timeout -> Unix.gettimeofday () > timeout) then
257 (lazy("Last chance: all is indexed " ^ string_of_float
258 (Unix.gettimeofday())));
259 let maxgoals = 100 in
260 ignore(List.fold_left
263 ignore(Sup.simplify_goal ~no_demod:true
264 maxvar (snd actives) bag acc x)
269 raise (Stop (Timeout (maxvar,bag)))
271 else if false then (* activates last chance strategy *)
273 debug (lazy("Last chance: "^string_of_float (Unix.gettimeofday())));
274 given_clause ~useage ~noinfer:true bag maxvar iterno weight_picks max_steps
275 (Some (Unix.gettimeofday () +. 20.))
276 actives passives g_actives g_passives;
277 raise (Stop (Timeout (maxvar,bag)));
279 else raise (Stop (Timeout (maxvar,bag)));
281 let use_age = useage && (weight_picks = (iterno / 6 + 1)) in
282 let weight_picks = if use_age then 0 else weight_picks+1
285 let rec aux_select bag passives g_passives =
286 let backward,(weight,current),passives,g_passives =
287 select ~use_age passives g_passives
289 if use_age && weight > monster then
290 let bag,cl = Terms.add_to_bag current bag in
292 aux_select bag passives (add_passive_clause g_passives cl)
294 aux_select bag (add_passive_clause passives cl) g_passives
296 let bag = Terms.replace_in_bag (current,false,iterno) bag in
298 let _ = debug (lazy("Selected goal : " ^ Pp.pp_unit_clause current)) in
301 if weight > monster then None else Some (bag,current)
304 ~no_demod:false maxvar (snd actives) bag g_actives current
306 | None -> aux_select bag passives g_passives
307 | Some (bag,g_current) ->
309 let g_actives = g_current :: g_actives in
310 bag,maxvar,actives,passives,g_actives,g_passives
312 backward_infer_step bag maxvar actives passives
313 g_actives g_passives g_current iterno
315 let _ = debug (lazy("Selected fact : " ^ Pp.pp_unit_clause current)) in
316 (*let is_orphan = Sup.orphan_murder bag (fst actives) current in*)
319 if weight > monster then bag,None
320 else bag, Some (current,actives)
321 else if Sup.orphan_murder bag (fst actives) current then
322 let _ = debug (lazy "Orphan murdered") in
323 let bag = Terms.replace_in_bag (current,true,iterno) bag in
325 else Sup.keep_simplified current actives bag maxvar
327 (*match Sup.one_pass_simplification current actives bag maxvar with*)
328 | bag,None -> aux_select bag passives g_passives
329 | bag,Some (current,actives) ->
330 (* if is_orphan then prerr_endline
331 ("WRONG discarded: " ^ (Pp.pp_unit_clause current));
333 prerr_endline (Pp.pp_unit_clause x))
336 (* List.iter (fun (id,_,_,_) -> let (cl,d) =
337 Terms.M.find id bag in
338 if d then prerr_endline
339 ("WRONG discarded: " ^ (Pp.pp_unit_clause cl)))
340 (current::fst actives);*)
343 current::fst actives,
344 IDX.index_unit_clause maxvar (snd actives) current
346 bag,maxvar,actives,passives,g_actives,g_passives
349 (bag,maxvar,actives,passives,g_actives,g_passives)
354 (*prerr_endline "Active table :";
355 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
358 let bag,maxvar,actives,passives,g_actives,g_passives =
359 aux_select bag passives g_passives
362 (lazy(Printf.sprintf "Number of active goals : %d"
363 (List.length g_actives)));
365 (lazy(Printf.sprintf "Number of passive goals : %d"
366 (passive_set_cardinal g_passives)));
368 (lazy(Printf.sprintf "Number of actives : %d" (List.length (fst actives))));
370 (lazy(Printf.sprintf "Number of passives : %d"
371 (passive_set_cardinal passives)));
372 given_clause ~useage ~noinfer
373 bag maxvar iterno weight_picks max_steps timeout
374 actives passives g_actives g_passives
377 let paramod ~useage ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
378 let _initial_timestamp = Unix.gettimeofday () in
380 add_passive_clauses ~no_weight:true passive_empty_set passives
383 add_passive_goals ~no_weight:true passive_empty_set g_passives
385 let g_actives = [] in
386 let actives = [], IDX.DT.empty in
388 given_clause ~useage ~noinfer:false
389 bag maxvar 0 0 max_steps timeout actives passives g_actives g_passives
391 | Sup.Success (bag, _, (i,_,_,_)) ->
393 let rec traverse ongoal (accg,acce) i =
394 match Terms.get_from_bag i bag with
395 | (id,_,_,Terms.Exact _),_,_ ->
396 if ongoal then [i],acce else
397 if (List.mem i acce) then accg,acce else accg,acce@[i]
398 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),_,_ ->
399 if (not ongoal) && (List.mem i acce) then accg,acce
402 traverse false (traverse ongoal (accg,acce) i1) i2
404 if ongoal then i::accg,acce else accg,i::acce
406 let gsteps,esteps = traverse true ([],[]) i in
407 (List.rev esteps)@gsteps
409 let max_w = List.fold_left (fun acc i ->
410 let (cl,_,_) = Terms.get_from_bag i bag in
411 max acc (Order.compute_unit_clause_weight cl)) 0 l in
412 prerr_endline "Statistics :";
413 prerr_endline ("Max weight : " ^ (string_of_int max_w));
414 (* List.iter (fun id -> let ((_,lit,_,proof as cl),d,it) =
415 Terms.get_from_bag id bag in
418 (Printf.sprintf "Id : %d, selected at %d, weight %d,disc, by %s"
419 id it (Order.compute_unit_clause_weight cl)
420 (Pp.pp_proof_step proof))
423 (Printf.sprintf "Id : %d, selected at %d, weight %d by %s"
424 id it (Order.compute_unit_clause_weight cl)
425 (Pp.pp_proof_step proof))) l;*)
427 prerr_endline "Proof:";
429 prerr_endline (Pp.pp_unit_clause (fst(Terms.M.find x bag)))) l;
431 Unsatisfiable [ bag, i, l ]
432 | Stop (Unsatisfiable _) -> Error "stop bug solution found!"