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
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 "infer_left step...";
166 let bag, maxvar, new_goals =
167 Sup.infer_left bag maxvar g_current actives
169 debug "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 "Forward infer step...";
188 let bag, maxvar, actives, new_clauses =
189 Sup.infer_right bag maxvar current actives
191 debug "Demodulating goals with actives...";
192 (* keep goals demodulated w.r.t. actives and check if solved *)
197 Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
200 | Some (bag,c1) -> bag,if c==c1 then c::acc else c::c1::acc)
203 let ctable = IDX.index_unit_clause maxvar IDX.DT.empty current in
204 let bag, maxvar, new_goals =
206 (fun (bag,m,acc) g ->
207 let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
209 (bag,maxvar,[]) g_actives
211 let bag = Terms.replace_in_bag (current,false,iterno) bag in
212 bag, maxvar, actives,
213 add_passive_clauses passives new_clauses, g_actives,
214 add_passive_goals g_passives new_goals
217 let rec given_clause ~useage ~noinfer
218 bag maxvar iterno weight_picks max_steps timeout
219 actives passives g_actives g_passives
221 let iterno = iterno + 1 in
222 if iterno = max_steps then raise (Stop (Timeout (maxvar,bag)));
223 (* timeout check: gettimeofday called only if timeout set *)
224 if timeout <> None &&
226 | None -> assert false
227 | Some timeout -> Unix.gettimeofday () > timeout) then
231 ("Last chance: all is indexed " ^ string_of_float
232 (Unix.gettimeofday()));
233 let maxgoals = 100 in
234 ignore(List.fold_left
237 ignore(Sup.simplify_goal ~no_demod:true
238 maxvar (snd actives) bag acc x)
243 raise (Stop (Timeout (maxvar,bag)))
245 else if false then (* activates last chance strategy *)
247 debug("Last chance: "^string_of_float (Unix.gettimeofday()));
248 given_clause ~useage ~noinfer:true bag maxvar iterno weight_picks max_steps
249 (Some (Unix.gettimeofday () +. 20.))
250 actives passives g_actives g_passives;
251 raise (Stop (Timeout (maxvar,bag)));
253 else raise (Stop (Timeout (maxvar,bag)));
255 let use_age = useage && (weight_picks = (iterno / 6 + 1)) in
256 let weight_picks = if use_age then 0 else weight_picks+1
259 let rec aux_select bag passives g_passives =
260 let backward,(weight,current),passives,g_passives =
261 select ~use_age passives g_passives
263 if use_age && weight > monster then
264 let bag,cl = Terms.add_to_bag current bag in
266 aux_select bag passives (add_passive_clause g_passives cl)
268 aux_select bag (add_passive_clause passives cl) g_passives
270 let bag = Terms.replace_in_bag (current,false,iterno) bag in
272 let _ = debug ("Selected goal : " ^ Pp.pp_unit_clause current) in
275 if weight > monster then None else Some (bag,current)
278 ~no_demod:false maxvar (snd actives) bag g_actives current
280 | None -> aux_select bag passives g_passives
281 | Some (bag,g_current) ->
283 let g_actives = g_current :: g_actives in
284 bag,maxvar,actives,passives,g_actives,g_passives
286 backward_infer_step bag maxvar actives passives
287 g_actives g_passives g_current iterno
289 let _ = debug ("Selected fact : " ^ Pp.pp_unit_clause current) in
290 (*let is_orphan = Sup.orphan_murder bag (fst actives) current in*)
293 if weight > monster then bag,None
294 else bag, Some (current,actives)
295 else if Sup.orphan_murder bag (fst actives) current then
296 let _ = debug "Orphan murdered" in
297 let bag = Terms.replace_in_bag (current,true,iterno) bag in
299 else Sup.keep_simplified current actives bag maxvar
301 (*match Sup.one_pass_simplification current actives bag maxvar with*)
302 | bag,None -> aux_select bag passives g_passives
303 | bag,Some (current,actives) ->
304 (* if is_orphan then prerr_endline
305 ("WRONG discarded: " ^ (Pp.pp_unit_clause current));
307 prerr_endline (Pp.pp_unit_clause x))
310 (* List.iter (fun (id,_,_,_) -> let (cl,d) =
311 Terms.M.find id bag in
312 if d then prerr_endline
313 ("WRONG discarded: " ^ (Pp.pp_unit_clause cl)))
314 (current::fst actives);*)
317 current::fst actives,
318 IDX.index_unit_clause maxvar (snd actives) current
320 bag,maxvar,actives,passives,g_actives,g_passives
322 forward_infer_step bag maxvar actives passives
323 g_actives g_passives current iterno
327 (*prerr_endline "Active table :";
328 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
331 let bag,maxvar,actives,passives,g_actives,g_passives =
332 aux_select bag passives g_passives
335 (Printf.sprintf "Number of active goals : %d"
336 (List.length g_actives));
338 (Printf.sprintf "Number of passive goals : %d"
339 (passive_set_cardinal g_passives));
341 (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
343 (Printf.sprintf "Number of passives : %d"
344 (passive_set_cardinal passives));
345 given_clause ~useage ~noinfer
346 bag maxvar iterno weight_picks max_steps timeout
347 actives passives g_actives g_passives
350 let paramod ~useage ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
351 let initial_timestamp = Unix.gettimeofday () in
353 add_passive_clauses ~no_weight:true passive_empty_set passives
356 add_passive_goals ~no_weight:true passive_empty_set g_passives
358 let g_actives = [] in
359 let actives = [], IDX.DT.empty in
361 given_clause ~useage ~noinfer:false
362 bag maxvar 0 0 max_steps timeout actives passives g_actives g_passives
364 | Sup.Success (bag, _, (i,_,_,_)) ->
366 let rec traverse ongoal (accg,acce) i =
367 match Terms.get_from_bag i bag with
368 | (id,_,_,Terms.Exact _),_,_ ->
369 if ongoal then [i],acce else
370 if (List.mem i acce) then accg,acce else accg,acce@[i]
371 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),_,_ ->
372 if (not ongoal) && (List.mem i acce) then accg,acce
375 traverse false (traverse ongoal (accg,acce) i1) i2
377 if ongoal then i::accg,acce else accg,i::acce
379 let gsteps,esteps = traverse true ([],[]) i in
380 (List.rev esteps)@gsteps
382 let max_w = List.fold_left (fun acc i ->
383 let (cl,_,_) = Terms.get_from_bag i bag in
384 max acc (Order.compute_unit_clause_weight cl)) 0 l in
385 prerr_endline "Statistics :";
386 prerr_endline ("Max weight : " ^ (string_of_int max_w));
387 (* List.iter (fun id -> let ((_,lit,_,proof as cl),d,it) =
388 Terms.get_from_bag id bag in
391 (Printf.sprintf "Id : %d, selected at %d, weight %d,disc, by %s"
392 id it (Order.compute_unit_clause_weight cl)
393 (Pp.pp_proof_step proof))
396 (Printf.sprintf "Id : %d, selected at %d, weight %d by %s"
397 id it (Order.compute_unit_clause_weight cl)
398 (Pp.pp_proof_step proof))) l;*)
400 (Printf.sprintf "Found proof, %fs"
401 (Unix.gettimeofday() -. initial_timestamp));
403 prerr_endline "Proof:";
405 prerr_endline (Pp.pp_unit_clause (fst(Terms.M.find x bag)))) l;
407 Unsatisfiable [ bag, i, l ]
408 | Stop (Unsatisfiable _) -> Error "stop bug solution found!"