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 ;;
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
35 g_passives:t Terms.unit_clause list ->
36 passives:t Terms.unit_clause list -> szsontology
39 module Paramod (B : Orderings.Blob) = struct
43 | Unsatisfiable of (B.t Terms.bag * int * int list) list
46 | Timeout of int * B.t Terms.bag
47 exception Stop of szsontology
48 type bag = B.t Terms.bag * int
50 module FU = FoUnif.Founif(B)
51 module IDX = Index.Index(B)
52 module Sup = Superposition.Superposition(B)
53 module Utils = FoUtils.Utils(B)
55 module WeightOrderedPassives =
57 type t = B.t Terms.passive_clause
58 let compare = Utils.compare_passive_clauses_weight
61 module AgeOrderedPassives =
63 type t = B.t Terms.passive_clause
64 let compare = Utils.compare_passive_clauses_age
67 module WeightPassiveSet = Set.Make(WeightOrderedPassives)
68 module AgePassiveSet = Set.Make(AgeOrderedPassives)
70 let add_passive_clause ?(no_weight=false) (passives_w,passives_a) cl =
71 let cl = if no_weight then (0,cl)
72 else Utils.mk_passive_clause cl in
73 WeightPassiveSet.add cl passives_w, AgePassiveSet.add cl passives_a
76 let add_passive_goal ?(no_weight=false) (passives_w,passives_a) g =
77 let g = if no_weight then (0,g)
78 else Utils.mk_passive_goal g in
79 WeightPassiveSet.add g passives_w, AgePassiveSet.add g passives_a
82 let remove_passive_clause (passives_w,passives_a) cl =
83 let passives_w = WeightPassiveSet.remove cl passives_w in
84 let passives_a = AgePassiveSet.remove cl passives_a in
88 let add_passive_clauses ?(no_weight=false)
89 (passives_w,passives_a) new_clauses =
90 let new_clauses_w,new_clauses_a =
91 List.fold_left (add_passive_clause ~no_weight)
92 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
94 (WeightPassiveSet.union new_clauses_w passives_w,
95 AgePassiveSet.union new_clauses_a passives_a)
98 let add_passive_goals ?(no_weight=false)
99 (passives_w,passives_a) new_clauses =
100 let new_clauses_w,new_clauses_a =
101 List.fold_left (add_passive_goal ~no_weight)
102 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
104 (WeightPassiveSet.union new_clauses_w passives_w,
105 AgePassiveSet.union new_clauses_a passives_a)
108 let is_passive_set_empty (passives_w,passives_a) =
109 if (WeightPassiveSet.is_empty passives_w) then begin
110 assert (AgePassiveSet.is_empty passives_a); true
112 assert (not (AgePassiveSet.is_empty passives_a)); false
116 let passive_set_cardinal (passives_w,_) = WeightPassiveSet.cardinal passives_w
118 let passive_empty_set =
119 (WeightPassiveSet.empty,AgePassiveSet.empty)
122 let pick_min_passive ~use_age (passives_w,passives_a) =
123 if use_age then AgePassiveSet.min_elt passives_a
124 else WeightPassiveSet.min_elt passives_w
127 let mk_clause bag maxvar (t,ty) =
128 let (proof,ty) = B.saturate t ty in
129 let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
130 let bag, c = Terms.add_to_bag c bag in
134 let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
135 let mk_goal (bag,maxvar) = mk_clause bag maxvar;;
137 (* TODO : global age over facts and goals (without comparing weights) *)
138 let select ~use_age passives g_passives =
139 if is_passive_set_empty passives then begin
140 if (is_passive_set_empty g_passives) then
141 raise (Stop GaveUp) (* we say we are incomplete *)
143 let g_cl = pick_min_passive ~use_age:use_age g_passives in
144 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
146 else let cl = pick_min_passive ~use_age:use_age passives in
147 if is_passive_set_empty g_passives then
148 (false,cl,remove_passive_clause passives cl,g_passives)
150 let g_cl = pick_min_passive ~use_age:use_age g_passives in
151 let (id1,_,_,_),(id2,_,_,_) = snd cl, snd g_cl in
152 let cmp = if use_age then id1 <= id2
153 else fst cl <= fst g_cl
156 (false,cl,remove_passive_clause passives cl,g_passives)
158 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
161 let backward_infer_step bag maxvar actives passives
162 g_actives g_passives g_current iterno =
163 (* superposition left, simplifications on goals *)
164 debug "infer_left step...";
165 let bag, maxvar, new_goals =
166 Sup.infer_left bag maxvar g_current actives
168 debug "Performed infer_left step";
169 let bag = Terms.replace_in_bag (g_current,false,iterno) bag in
170 bag, maxvar, actives, passives, g_current::g_actives,
171 (add_passive_goals g_passives new_goals)
174 let forward_infer_step bag maxvar actives passives g_actives
175 g_passives current iterno =
180 * A' = demod [e'] A *
183 * new = supright e'' A'' *
184 * new'= demod A'' new *
186 debug "Forward infer step...";
187 let bag, maxvar, actives, new_clauses =
188 Sup.infer_right bag maxvar current actives
190 debug "Demodulating goals with actives...";
191 (* keep goals demodulated w.r.t. actives and check if solved *)
196 Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
199 | Some (bag,c1) -> bag,if c==c1 then c::acc else c::c1::acc)
202 let ctable = IDX.index_unit_clause IDX.DT.empty current in
203 let bag, maxvar, new_goals =
205 (fun (bag,m,acc) g ->
206 let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
208 (bag,maxvar,[]) g_actives
210 let bag = Terms.replace_in_bag (current,false,iterno) bag in
211 bag, maxvar, actives,
212 add_passive_clauses passives new_clauses, g_actives,
213 add_passive_goals g_passives new_goals
216 let rec given_clause ~noinfer
217 bag maxvar iterno weight_picks max_steps timeout
218 actives passives g_actives g_passives
220 let iterno = iterno + 1 in
221 if iterno = max_steps then raise (Stop (Timeout (maxvar,bag)));
222 (* timeout check: gettimeofday called only if timeout set *)
223 if timeout <> None &&
225 | None -> assert false
226 | Some timeout -> Unix.gettimeofday () > timeout) then
230 ("Last chance: all is indexed " ^ string_of_float
231 (Unix.gettimeofday()));
232 let maxgoals = 100 in
233 ignore(List.fold_left
236 ignore(Sup.simplify_goal ~no_demod:true
237 maxvar (snd actives) bag acc x)
242 raise (Stop (Timeout (maxvar,bag)))
244 else if false then (* activates last chance strategy *)
246 debug("Last chance: "^string_of_float (Unix.gettimeofday()));
247 given_clause ~noinfer:true bag maxvar iterno weight_picks max_steps
248 (Some (Unix.gettimeofday () +. 20.))
249 actives passives g_actives g_passives;
250 raise (Stop (Timeout (maxvar,bag)));
252 else raise (Stop (Timeout (maxvar,bag)));
254 let use_age = false && (weight_picks = (iterno / 6 + 1)) in
255 let weight_picks = if use_age then 0 else weight_picks+1
258 let rec aux_select bag passives g_passives =
259 let backward,(weight,current),passives,g_passives =
260 select ~use_age passives g_passives
262 if use_age && weight > monster then
263 let bag,cl = Terms.add_to_bag current bag in
265 aux_select bag passives (add_passive_clause g_passives cl)
267 aux_select bag (add_passive_clause passives cl) g_passives
269 let bag = Terms.replace_in_bag (current,false,iterno) bag in
271 let _ = debug ("Selected goal : " ^ Pp.pp_unit_clause current) in
274 if weight > monster then None else Some (bag,current)
277 ~no_demod:false maxvar (snd actives) bag g_actives current
279 | None -> aux_select bag passives g_passives
280 | Some (bag,g_current) ->
282 let g_actives = g_current :: g_actives in
283 bag,maxvar,actives,passives,g_actives,g_passives
285 backward_infer_step bag maxvar actives passives
286 g_actives g_passives g_current iterno
288 let _ = debug ("Selected fact : " ^ Pp.pp_unit_clause current) in
289 (*let is_orphan = Sup.orphan_murder bag (fst actives) current in*)
292 if weight > monster then bag,None
293 else bag, Some (current,actives)
294 else if Sup.orphan_murder bag (fst actives) current then
295 let _ = debug "Orphan murdered" in
296 let bag = Terms.replace_in_bag (current,true,iterno) bag in
298 else Sup.keep_simplified current actives bag maxvar
300 (*match Sup.one_pass_simplification current actives bag maxvar with*)
301 | bag,None -> aux_select bag passives g_passives
302 | bag,Some (current,actives) ->
303 (* if is_orphan then prerr_endline
304 ("WRONG discarded: " ^ (Pp.pp_unit_clause current));
306 prerr_endline (Pp.pp_unit_clause x))
309 (* List.iter (fun (id,_,_,_) -> let (cl,d) =
310 Terms.M.find id bag in
311 if d then prerr_endline
312 ("WRONG discarded: " ^ (Pp.pp_unit_clause cl)))
313 (current::fst actives);*)
316 current::fst actives,
317 IDX.index_unit_clause (snd actives) current
319 bag,maxvar,actives,passives,g_actives,g_passives
321 forward_infer_step bag maxvar actives passives
322 g_actives g_passives current iterno
326 (*prerr_endline "Active table :";
327 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
330 let bag,maxvar,actives,passives,g_actives,g_passives =
331 aux_select bag passives g_passives
334 (Printf.sprintf "Number of active goals : %d"
335 (List.length g_actives));
337 (Printf.sprintf "Number of passive goals : %d"
338 (passive_set_cardinal g_passives));
340 (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
342 (Printf.sprintf "Number of passives : %d"
343 (passive_set_cardinal passives));
344 given_clause ~noinfer
345 bag maxvar iterno weight_picks max_steps timeout
346 actives passives g_actives g_passives
349 let paramod ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
350 let initial_timestamp = Unix.gettimeofday () in
352 add_passive_clauses ~no_weight:true passive_empty_set passives
355 add_passive_goals ~no_weight:true passive_empty_set g_passives
357 let g_actives = [] in
358 let actives = [], IDX.DT.empty in
360 given_clause ~noinfer:false
361 bag maxvar 0 0 max_steps timeout actives passives g_actives g_passives
363 | Sup.Success (bag, _, (i,_,_,_)) ->
365 let rec traverse ongoal (accg,acce) i =
366 match Terms.get_from_bag i bag with
367 | (id,_,_,Terms.Exact _),_,_ ->
368 if ongoal then [i],acce else
369 if (List.mem i acce) then accg,acce else accg,acce@[i]
370 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),_,_ ->
371 if (not ongoal) && (List.mem i acce) then accg,acce
374 traverse false (traverse ongoal (accg,acce) i1) i2
376 if ongoal then i::accg,acce else accg,i::acce
378 let gsteps,esteps = traverse true ([],[]) i in
379 (List.rev esteps)@gsteps
381 let max_w = List.fold_left (fun acc i ->
382 let (cl,_,_) = Terms.get_from_bag i bag in
383 max acc (Order.compute_unit_clause_weight cl)) 0 l in
384 prerr_endline "Statistics :";
385 prerr_endline ("Max weight : " ^ (string_of_int max_w));
386 (* List.iter (fun id -> let ((_,lit,_,proof as cl),d,it) =
387 Terms.get_from_bag id bag in
390 (Printf.sprintf "Id : %d, selected at %d, weight %d,disc, by %s"
391 id it (Order.compute_unit_clause_weight cl)
392 (Pp.pp_proof_step proof))
395 (Printf.sprintf "Id : %d, selected at %d, weight %d by %s"
396 id it (Order.compute_unit_clause_weight cl)
397 (Pp.pp_proof_step proof))) l;*)
399 (Printf.sprintf "Found proof, %fs"
400 (Unix.gettimeofday() -. initial_timestamp));
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!"