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) ;;
24 | Unsatisfiable of (t Terms.bag * int * int list) list
27 | Timeout of int * t Terms.bag
28 type bag = t Terms.bag * int
29 val mk_passive : bag -> input * input -> bag * t Terms.unit_clause
30 val mk_goal : bag -> input * input -> bag * t Terms.unit_clause
36 g_passives:t Terms.unit_clause list ->
37 passives:t Terms.unit_clause list -> szsontology
40 module Paramod (B : Orderings.Blob) = struct
44 | Unsatisfiable of (B.t Terms.bag * int * int list) list
47 | Timeout of int * B.t Terms.bag
48 exception Stop of szsontology
49 type bag = B.t Terms.bag * int
51 module FU = FoUnif.Founif(B)
52 module IDX = Index.Index(B)
53 module Sup = Superposition.Superposition(B)
54 module Utils = FoUtils.Utils(B)
56 module WeightOrderedPassives =
58 type t = B.t Terms.passive_clause
59 let compare = Utils.compare_passive_clauses_weight
62 module AgeOrderedPassives =
64 type t = B.t Terms.passive_clause
65 let compare = Utils.compare_passive_clauses_age
68 module WeightPassiveSet = Set.Make(WeightOrderedPassives)
69 module AgePassiveSet = Set.Make(AgeOrderedPassives)
71 let add_passive_clause ?(no_weight=false) (passives_w,passives_a) cl =
72 let cl = if no_weight then (0,cl)
73 else Utils.mk_passive_clause cl in
74 WeightPassiveSet.add cl passives_w, AgePassiveSet.add cl passives_a
77 let add_passive_goal ?(no_weight=false) (passives_w,passives_a) g =
78 let g = if no_weight then (0,g)
79 else Utils.mk_passive_goal g in
80 WeightPassiveSet.add g passives_w, AgePassiveSet.add g passives_a
83 let remove_passive_clause (passives_w,passives_a) cl =
84 let passives_w = WeightPassiveSet.remove cl passives_w in
85 let passives_a = AgePassiveSet.remove cl passives_a in
89 let add_passive_clauses ?(no_weight=false)
90 (passives_w,passives_a) new_clauses =
91 let new_clauses_w,new_clauses_a =
92 List.fold_left (add_passive_clause ~no_weight)
93 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
95 (WeightPassiveSet.union new_clauses_w passives_w,
96 AgePassiveSet.union new_clauses_a passives_a)
99 let add_passive_goals ?(no_weight=false)
100 (passives_w,passives_a) new_clauses =
101 let new_clauses_w,new_clauses_a =
102 List.fold_left (add_passive_goal ~no_weight)
103 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
105 (WeightPassiveSet.union new_clauses_w passives_w,
106 AgePassiveSet.union new_clauses_a passives_a)
109 let is_passive_set_empty (passives_w,passives_a) =
110 if (WeightPassiveSet.is_empty passives_w) then begin
111 assert (AgePassiveSet.is_empty passives_a); true
113 assert (not (AgePassiveSet.is_empty passives_a)); false
117 let passive_set_cardinal (passives_w,_) = WeightPassiveSet.cardinal passives_w
119 let passive_empty_set =
120 (WeightPassiveSet.empty,AgePassiveSet.empty)
123 let pick_min_passive ~use_age (passives_w,passives_a) =
124 if use_age then AgePassiveSet.min_elt passives_a
125 else WeightPassiveSet.min_elt passives_w
128 let mk_clause bag maxvar (t,ty) =
129 let (proof,ty) = B.saturate t ty in
130 let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
131 let bag, c = Terms.add_to_bag c bag in
135 let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
136 let mk_goal (bag,maxvar) = mk_clause bag maxvar;;
138 (* TODO : global age over facts and goals (without comparing weights) *)
139 let select ~use_age passives g_passives =
140 if is_passive_set_empty passives then begin
141 if (is_passive_set_empty g_passives) then
142 raise (Stop GaveUp) (* we say we are incomplete *)
144 let g_cl = pick_min_passive ~use_age:use_age g_passives in
145 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
147 else let cl = pick_min_passive ~use_age:use_age passives in
148 if is_passive_set_empty g_passives then
149 (false,cl,remove_passive_clause passives cl,g_passives)
151 let g_cl = pick_min_passive ~use_age:use_age g_passives in
152 let (id1,_,_,_),(id2,_,_,_) = snd cl, snd g_cl in
153 let cmp = if use_age then id1 <= id2
154 else fst cl <= fst g_cl
157 (false,cl,remove_passive_clause passives cl,g_passives)
159 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
162 let backward_infer_step bag maxvar actives passives
163 g_actives g_passives g_current iterno =
164 (* superposition left, simplifications on goals *)
165 debug (lazy "infer_left step...");
166 let bag, maxvar, new_goals =
167 Sup.infer_left bag maxvar g_current actives
169 debug (lazy "Performed infer_left step");
170 let bag = Terms.replace_in_bag (g_current,false,iterno) bag in
171 bag, maxvar, actives, passives, g_current::g_actives,
172 (add_passive_goals g_passives new_goals)
175 let forward_infer_step bag maxvar actives passives g_actives
176 g_passives current iterno =
181 * A' = demod [e'] A *
184 * new = supright e'' A'' *
185 * new'= demod A'' new *
187 debug (lazy "Forward infer step...");
188 debug (lazy("Number of actives : " ^ (string_of_int (List.length (fst actives)))));
189 let bag, maxvar, actives, new_clauses =
190 Sup.infer_right bag maxvar current actives
192 debug (lazy "Demodulating goals with actives...");
193 (* keep goals demodulated w.r.t. actives and check if solved *)
198 Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
201 | Some (bag,c1) -> bag,if c==c1 then c::acc else c::c1::acc)
204 let ctable = IDX.index_unit_clause maxvar IDX.DT.empty current in
205 let bag, maxvar, new_goals =
207 (fun (bag,m,acc) g ->
208 let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
210 (bag,maxvar,[]) g_actives
212 let bag = Terms.replace_in_bag (current,false,iterno) bag in
213 (* prerr_endline (Pp.pp_bag bag); *)
214 bag, maxvar, actives,
215 add_passive_clauses passives new_clauses, g_actives,
216 add_passive_goals g_passives new_goals
219 let rec given_clause ~useage ~noinfer
220 bag maxvar iterno weight_picks max_steps timeout
221 actives passives g_actives g_passives
223 let iterno = iterno + 1 in
224 if iterno = max_steps then raise (Stop (Timeout (maxvar,bag)));
225 (* timeout check: gettimeofday called only if timeout set *)
226 if timeout <> None &&
228 | None -> assert false
229 | Some timeout -> Unix.gettimeofday () > timeout) then
233 (lazy("Last chance: all is indexed " ^ string_of_float
234 (Unix.gettimeofday())));
235 let maxgoals = 100 in
236 ignore(List.fold_left
239 ignore(Sup.simplify_goal ~no_demod:true
240 maxvar (snd actives) bag acc x)
245 raise (Stop (Timeout (maxvar,bag)))
247 else if false then (* activates last chance strategy *)
249 debug (lazy("Last chance: "^string_of_float (Unix.gettimeofday())));
250 given_clause ~useage ~noinfer:true bag maxvar iterno weight_picks max_steps
251 (Some (Unix.gettimeofday () +. 20.))
252 actives passives g_actives g_passives;
253 raise (Stop (Timeout (maxvar,bag)));
255 else raise (Stop (Timeout (maxvar,bag)));
257 let use_age = useage && (weight_picks = (iterno / 6 + 1)) in
258 let weight_picks = if use_age then 0 else weight_picks+1
261 let rec aux_select bag passives g_passives =
262 let backward,(weight,current),passives,g_passives =
263 select ~use_age passives g_passives
265 if use_age && weight > monster then
266 let bag,cl = Terms.add_to_bag current bag in
268 aux_select bag passives (add_passive_clause g_passives cl)
270 aux_select bag (add_passive_clause passives cl) g_passives
272 let bag = Terms.replace_in_bag (current,false,iterno) bag in
274 let _ = debug (lazy("Selected goal : " ^ Pp.pp_unit_clause current)) in
277 if weight > monster then None else Some (bag,current)
280 ~no_demod:false maxvar (snd actives) bag g_actives current
282 | None -> aux_select bag passives g_passives
283 | Some (bag,g_current) ->
285 let g_actives = g_current :: g_actives in
286 bag,maxvar,actives,passives,g_actives,g_passives
288 backward_infer_step bag maxvar actives passives
289 g_actives g_passives g_current iterno
291 let _ = debug (lazy("Selected fact : " ^ Pp.pp_unit_clause current)) in
292 (*let is_orphan = Sup.orphan_murder bag (fst actives) current in*)
295 if weight > monster then bag,None
296 else bag, Some (current,actives)
297 else if Sup.orphan_murder bag (fst actives) current then
298 let _ = debug (lazy "Orphan murdered") in
299 let bag = Terms.replace_in_bag (current,true,iterno) bag in
301 else Sup.keep_simplified current actives bag maxvar
303 (*match Sup.one_pass_simplification current actives bag maxvar with*)
304 | bag,None -> aux_select bag passives g_passives
305 | bag,Some (current,actives) ->
306 (* if is_orphan then prerr_endline
307 ("WRONG discarded: " ^ (Pp.pp_unit_clause current));
309 prerr_endline (Pp.pp_unit_clause x))
312 (* List.iter (fun (id,_,_,_) -> let (cl,d) =
313 Terms.M.find id bag in
314 if d then prerr_endline
315 ("WRONG discarded: " ^ (Pp.pp_unit_clause cl)))
316 (current::fst actives);*)
319 current::fst actives,
320 IDX.index_unit_clause maxvar (snd actives) current
322 bag,maxvar,actives,passives,g_actives,g_passives
324 forward_infer_step bag maxvar actives passives
325 g_actives g_passives current iterno
329 (*prerr_endline "Active table :";
330 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
333 let bag,maxvar,actives,passives,g_actives,g_passives =
334 aux_select bag passives g_passives
337 (lazy(Printf.sprintf "Number of active goals : %d"
338 (List.length g_actives)));
340 (lazy(Printf.sprintf "Number of passive goals : %d"
341 (passive_set_cardinal g_passives)));
343 (lazy(Printf.sprintf "Number of actives : %d" (List.length (fst actives))));
345 (lazy(Printf.sprintf "Number of passives : %d"
346 (passive_set_cardinal passives)));
347 given_clause ~useage ~noinfer
348 bag maxvar iterno weight_picks max_steps timeout
349 actives passives g_actives g_passives
352 let paramod ~useage ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
353 let _initial_timestamp = Unix.gettimeofday () in
355 add_passive_clauses ~no_weight:true passive_empty_set passives
358 add_passive_goals ~no_weight:true passive_empty_set g_passives
360 let g_actives = [] in
361 let actives = [], IDX.DT.empty in
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_unit_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 prerr_endline "Proof:";
404 prerr_endline (Pp.pp_unit_clause (fst(Terms.M.find x bag)))) l;
406 Unsatisfiable [ bag, i, l ]
407 | Stop (Unsatisfiable _) -> Error "stop bug solution found!"