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 _ = ();; *)
24 | Unsatisfiable of (t Terms.bag * int * int list) list
27 | Timeout of int * t Terms.bag
28 type bag = t Terms.bag * int
30 val empty_state : state
31 val bag_of_state : state -> bag
32 val replace_bag: state -> bag -> state
33 val mk_passive : bag -> input * input -> bag * t Terms.unit_clause
34 val mk_goal : bag -> input * input -> bag * t Terms.unit_clause
35 val forward_infer_step :
37 t Terms.unit_clause ->
51 g_passives:t Terms.unit_clause list ->
52 passives:t Terms.unit_clause list -> szsontology
54 state -> input* input -> szsontology
57 module Paramod (B : Orderings.Blob) = struct
59 module FU = FoUnif.Founif(B)
60 module IDX = Index.Index(B)
61 module Sup = Superposition.Superposition(B)
62 module Utils = FoUtils.Utils(B)
64 module WeightOrderedPassives =
66 type t = B.t Terms.passive_clause
67 let compare = Utils.compare_passive_clauses_weight
70 module AgeOrderedPassives =
72 type t = B.t Terms.passive_clause
73 let compare = Utils.compare_passive_clauses_age
76 module WeightPassiveSet = Set.Make(WeightOrderedPassives)
77 module AgePassiveSet = Set.Make(AgeOrderedPassives)
81 type bag = B.t Terms.bag * int
83 | Unsatisfiable of (B.t Terms.bag * int * int list) list
86 | Timeout of int * B.t Terms.bag
87 exception Stop of szsontology
91 * Index.Index(B).active_set
92 * (IDX.DT.t * WeightPassiveSet.t * AgePassiveSet.t)
93 * B.t Terms.unit_clause list
94 * (WeightPassiveSet.t * AgePassiveSet.t)
100 (IDX.DT.empty,WeightPassiveSet.empty,AgePassiveSet.empty),
102 (WeightPassiveSet.empty,AgePassiveSet.empty)
105 let bag_of_state (bag,n,_,_,_,_) = bag,n
108 let replace_bag (_,_,a,b,c,d) (bag,n) = bag,n,a,b,c,d
111 let add_passive_clause ?(no_weight=false)
112 (passive_t,passives_w,passives_a) cl =
113 let pcl = if no_weight then (0,cl)
114 else Utils.mk_passive_clause cl in
115 IDX.index_unit_clause passive_t cl,
116 WeightPassiveSet.add pcl passives_w,
117 AgePassiveSet.add pcl passives_a
120 let add_passive_goal ?(no_weight=false) (passives_w,passives_a) g =
121 let g = if no_weight then (0,g)
122 else Utils.mk_passive_goal g in
123 WeightPassiveSet.add g passives_w, AgePassiveSet.add g passives_a
126 let remove_passive_clause (passive_t,passives_w,passives_a) cl =
127 let passive_t = IDX.remove_unit_clause passive_t (snd cl) in
128 let passives_w = WeightPassiveSet.remove cl passives_w in
129 let passives_a = AgePassiveSet.remove cl passives_a in
130 passive_t,passives_w,passives_a
133 let add_passive_clauses ?(no_weight=false) =
134 List.fold_left (add_passive_clause ~no_weight)
137 let add_passive_goals ?(no_weight=false)
138 (passives_w,passives_a) new_clauses =
139 let new_clauses_w,new_clauses_a =
140 List.fold_left (add_passive_goal ~no_weight)
141 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
143 (WeightPassiveSet.union new_clauses_w passives_w,
144 AgePassiveSet.union new_clauses_a passives_a)
147 let remove_passive_goal (passives_w,passives_a) cl =
148 let passives_w = WeightPassiveSet.remove cl passives_w in
149 let passives_a = AgePassiveSet.remove cl passives_a in
150 passives_w,passives_a
153 let is_passive_set_empty (_,passives_w,passives_a) =
154 if (WeightPassiveSet.is_empty passives_w) then begin
155 assert (AgePassiveSet.is_empty passives_a); true
157 assert (not (AgePassiveSet.is_empty passives_a)); false
161 let is_passive_g_set_empty (passives_w,passives_a) =
162 if (WeightPassiveSet.is_empty passives_w) then begin
163 assert (AgePassiveSet.is_empty passives_a); true
165 assert (not (AgePassiveSet.is_empty passives_a)); false
169 let passive_set_cardinal (_,passives_w,_)
170 = WeightPassiveSet.cardinal passives_w
173 let g_passive_set_cardinal (passives_w,_)
174 = WeightPassiveSet.cardinal passives_w
177 let passive_empty_set =
178 (IDX.DT.empty,WeightPassiveSet.empty,AgePassiveSet.empty)
181 let g_passive_empty_set =
182 (WeightPassiveSet.empty,AgePassiveSet.empty)
185 let pick_min_passive ~use_age (_,passives_w,passives_a) =
186 if use_age then AgePassiveSet.min_elt passives_a
187 else WeightPassiveSet.min_elt passives_w
190 let pick_min_g_passive ~use_age (passives_w,passives_a) =
191 if use_age then AgePassiveSet.min_elt passives_a
192 else WeightPassiveSet.min_elt passives_w
195 let mk_clause bag maxvar (t,ty) =
196 let (proof,ty) = B.saturate t ty in
197 let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
198 let bag, c = Terms.add_to_bag c bag in
202 let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
203 let mk_goal (bag,maxvar) = mk_clause bag maxvar;;
204 let initialize_goal (bag,maxvar,actives,passives,_,_) t =
205 let (bag,maxvar), g = mk_goal (bag,maxvar) t in
206 let g_passives = g_passive_empty_set in
207 let g_passives = add_passive_goal g_passives g in
208 (bag,maxvar,actives,passives,[],g_passives)
211 (* TODO : global age over facts and goals (without comparing weights) *)
212 let select ~use_age passives g_passives =
213 if is_passive_set_empty passives then begin
214 if (is_passive_g_set_empty g_passives) then
215 raise (Stop GaveUp) (* we say we are incomplete *)
217 let g_cl = pick_min_g_passive ~use_age:use_age g_passives in
218 (true,g_cl,passives,remove_passive_goal g_passives g_cl)
220 else let cl = pick_min_passive ~use_age:use_age passives in
221 if is_passive_g_set_empty g_passives then
222 (false,cl,remove_passive_clause passives cl,g_passives)
224 let g_cl = pick_min_g_passive ~use_age:use_age g_passives in
225 let (id1,_,_,_),(id2,_,_,_) = snd cl, snd g_cl in
226 let cmp = if use_age then id1 <= id2
227 else fst cl <= fst g_cl
230 (false,cl,remove_passive_clause passives cl,g_passives)
232 (true,g_cl,passives,remove_passive_goal g_passives g_cl)
235 let backward_infer_step bag maxvar actives passives
236 g_actives g_passives g_current iterno =
237 (* superposition left, simplifications on goals *)
238 debug (lazy "infer_left step...");
239 let bag, maxvar, new_goals =
240 Sup.infer_left bag maxvar g_current actives
242 debug (lazy "Performed infer_left step");
243 let bag = Terms.replace_in_bag (g_current,false,iterno) bag in
244 bag, maxvar, actives, passives, g_current::g_actives,
245 (add_passive_goals g_passives new_goals)
248 let forward_infer_step
249 ((bag,maxvar,actives,passives,g_actives,g_passives) as s)
255 * A' = demod [e'] A *
258 * new = supright e'' A'' *
259 * new'= demod A'' new *
261 debug (lazy "Forward infer step...");
262 debug (lazy("Number of actives : " ^ (string_of_int (List.length (fst actives)))));
263 let id,_,_,_ = current in
264 let _ = Terms.get_from_bag id bag in
266 match Sup.keep_simplified current actives bag maxvar
269 | bag,Some (current,actives) ->
270 debug (lazy "simplified...");
271 let bag, maxvar, actives, new_clauses =
272 Sup.infer_right bag maxvar current actives
274 debug (lazy "Demodulating goals with actives...");
275 (* keep goals demodulated w.r.t. actives and check if solved *)
280 Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
283 | Some (bag,c1) -> bag,if c==c1 then c::acc else c::c1::acc)
286 let ctable = IDX.index_unit_clause IDX.DT.empty current in
287 let bag, maxvar, new_goals =
289 (fun (bag,m,acc) g ->
290 let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
292 (bag,maxvar,[]) g_actives
294 let bag = Terms.replace_in_bag (current,false,iterno) bag in
295 (* prerr_endline (Pp.pp_bag bag); *)
296 bag, maxvar, actives,
297 add_passive_clauses passives new_clauses, g_actives,
298 add_passive_goals g_passives new_goals
301 let debug_status (_,_,actives,passives,g_actives,g_passives) =
303 ((Printf.sprintf "Number of active goals : %d\n"
304 (List.length g_actives)) ^
305 (Printf.sprintf "Number of passive goals : %d\n"
306 (g_passive_set_cardinal g_passives)) ^
307 (Printf.sprintf "Number of actives : %d\n"
308 (List.length (fst actives))) ^
309 (Printf.sprintf "Number of passives : %d\n"
310 (passive_set_cardinal passives)))
314 (* we just check if any of the active goals is subsumed by a
315 passive clause, or if any of the passive goal is subsumed
316 by an active or passive clause *)
317 let last_chance (bag,maxvar,actives,passives,g_actives,g_passives) =
318 debug (lazy("Last chance " ^ string_of_float
319 (Unix.gettimeofday())));
320 let active_t = snd actives in
321 let passive_t,_,_ = passives in
323 WeightPassiveSet.fold
325 if List.exists (Sup.are_alpha_eq x) g_actives then acc
333 (Sup.simplify_goal ~no_demod:true maxvar active_t bag [] x))
339 (Sup.simplify_goal ~no_demod:true maxvar passive_t bag [] x))
340 (g_actives@g_passives));
341 raise (Stop (Timeout (maxvar,bag)))
343 let check_timeout = function
345 | Some timeout -> Unix.gettimeofday () > timeout
347 let rec given_clause ~useage
348 bag maxvar iterno weight_picks max_steps timeout
349 actives passives g_actives g_passives
351 let iterno = iterno + 1 in
352 if iterno = max_steps || check_timeout timeout then
353 last_chance (bag,maxvar,actives,passives,g_actives,g_passives)
355 let use_age = useage && (weight_picks = (iterno / 6 + 1)) in
356 let weight_picks = if use_age then 0 else weight_picks+1
359 let rec aux_select bag
360 (passives:IDX.DT.t * WeightPassiveSet.t * AgePassiveSet.t)
362 let backward,(weight,current),passives,g_passives =
363 select ~use_age passives g_passives
365 if use_age && weight > monster then
366 let bag,cl = Terms.add_to_bag current bag in
368 aux_select bag passives (add_passive_goal g_passives cl)
370 aux_select bag (add_passive_clause passives cl) g_passives
372 let bag = Terms.replace_in_bag (current,false,iterno) bag in
374 let _ = debug (lazy("Selected goal : " ^ Pp.pp_unit_clause current)) in
377 ~no_demod:false maxvar (snd actives) bag g_actives current
379 | None -> aux_select bag passives g_passives
380 | Some (bag,g_current) ->
381 backward_infer_step bag maxvar actives passives
382 g_actives g_passives g_current iterno
384 let _ = debug (lazy("Selected fact : " ^ Pp.pp_unit_clause current))
386 if Sup.orphan_murder bag (fst actives) current then
387 let _ = debug (lazy "Orphan murdered") in
388 let bag = Terms.replace_in_bag (current,true,iterno) bag in
389 aux_select bag passives g_passives
391 let s = bag,maxvar,actives,passives,g_actives,g_passives in
392 let s1 = forward_infer_step s current iterno
394 if s == s1 then aux_select bag passives g_passives
397 (*prerr_endline "Active table :";
398 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
401 let (bag,maxvar,actives,passives,g_actives,g_passives) as status =
402 aux_select bag passives g_passives
404 debug (debug_status status);
406 bag maxvar iterno weight_picks max_steps timeout
407 actives passives g_actives g_passives
410 (* similar to given_clause, but it merely works on goals,
411 in parallel, at each iteration *)
412 let rec goal_narrowing iterno max_steps timeout status
414 debug (debug_status status);
415 let iterno = iterno + 1 in
416 if iterno = max_steps || check_timeout timeout then
419 let _,_,_,_,_,g_passives = status in
420 let passive_goals = WeightPassiveSet.elements (fst g_passives) in
424 let bag,maxvar,actives,passives,g_actives,g_passives = acc in
426 remove_passive_goal g_passives g in
427 let current = snd g in
429 debug (lazy("Selected goal : " ^ Pp.pp_unit_clause current))
433 ~no_demod:false maxvar (snd actives) bag g_actives current
436 | Some (bag,g_current) ->
438 debug (lazy("Demodulated goal : "
439 ^ Pp.pp_unit_clause g_current))
441 backward_infer_step bag maxvar actives passives
442 g_actives g_passives g_current iterno)
445 goal_narrowing iterno max_steps timeout newstatus
447 let compute_result bag i =
449 let rec traverse ongoal (accg,acce) i =
450 match Terms.get_from_bag i bag with
451 | (id,_,_,Terms.Exact _),_,_ ->
452 if ongoal then [i],acce else
453 if (List.mem i acce) then accg,acce else accg,acce@[i]
454 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),_,_ ->
455 if (not ongoal) && (List.mem i acce) then accg,acce
458 traverse false (traverse ongoal (accg,acce) i1) i2
460 if ongoal then i::accg,acce else accg,i::acce
462 let gsteps,esteps = traverse true ([],[]) i in
463 (List.rev esteps)@gsteps
465 prerr_endline ("steps: " ^ (string_of_int (List.length l)));
469 let (cl,_,_) = Terms.get_from_bag i bag in
470 max acc (Order.compute_unit_clause_weight cl)) 0 l in
471 prerr_endline "Statistics :";
472 prerr_endline ("Max weight : " ^ (string_of_int max_w));
473 (* List.iter (fun id -> let ((_,lit,_,proof as cl),d,it) =
474 Terms.get_from_bag id bag in
477 (Printf.sprintf "Id : %d, selected at %d, weight %d,disc, by %s"
478 id it (Order.compute_unit_clause_weight cl)
479 (Pp.pp_proof_step proof))
482 (Printf.sprintf "Id : %d, selected at %d, weight %d by %s"
483 id it (Order.compute_unit_clause_weight cl)
484 (Pp.pp_proof_step proof))) l;*)
485 prerr_endline "Proof:";
488 let cl,_,_ = Terms.get_from_bag x bag in
489 prerr_endline (Pp.pp_unit_clause cl)) l;
490 Unsatisfiable [ bag, i, l ]
492 let paramod ~useage ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
493 let _initial_timestamp = Unix.gettimeofday () in
495 add_passive_clauses ~no_weight:true passive_empty_set passives
498 add_passive_goals ~no_weight:true g_passive_empty_set g_passives
500 let g_actives = [] in
501 let actives = [], IDX.DT.empty in
503 given_clause ~useage ~noinfer:false
504 bag maxvar 0 0 max_steps timeout actives passives g_actives g_passives
506 | Sup.Success (bag, _, (i,_,_,_)) ->
508 | Stop (Unsatisfiable _) -> Error "stop bug solution found!"
512 let fast_eq_check s goal =
513 let s = initialize_goal s goal in
515 goal_narrowing 0 2 None s
517 | Sup.Success (bag, _, (i,_,_,_)) ->
519 | Stop (Unsatisfiable _) -> Error "stop bug solution found!"