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
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 (* if the goal is not an equation we returns an empty
210 if Terms.is_eq_clause g then add_passive_goal g_passives g
213 (bag,maxvar,actives,passives,[],g_passives)
216 (* TODO : global age over facts and goals (without comparing weights) *)
217 let select ~use_age passives g_passives =
218 if is_passive_set_empty passives then begin
219 if (is_passive_g_set_empty g_passives) then
220 raise (Stop GaveUp) (* we say we are incomplete *)
222 let g_cl = pick_min_g_passive ~use_age:use_age g_passives in
223 (true,g_cl,passives,remove_passive_goal g_passives g_cl)
225 else let cl = pick_min_passive ~use_age:use_age passives in
226 if is_passive_g_set_empty g_passives then
227 (false,cl,remove_passive_clause passives cl,g_passives)
229 let g_cl = pick_min_g_passive ~use_age:use_age g_passives in
230 let (id1,_,_,_),(id2,_,_,_) = snd cl, snd g_cl in
231 let cmp = if use_age then id1 <= id2
232 else fst cl <= fst g_cl
235 (false,cl,remove_passive_clause passives cl,g_passives)
237 (true,g_cl,passives,remove_passive_goal g_passives g_cl)
240 let backward_infer_step bag maxvar actives passives
241 g_actives g_passives g_current iterno =
242 (* superposition left, simplifications on goals *)
243 debug (lazy "infer_left step...");
244 let bag, maxvar, new_goals =
245 Sup.infer_left bag maxvar g_current actives
247 debug (lazy "Performed infer_left step");
248 let bag = Terms.replace_in_bag (g_current,false,iterno) bag in
249 bag, maxvar, actives, passives, g_current::g_actives,
250 (add_passive_goals g_passives new_goals)
253 let forward_infer_step
254 ((bag,maxvar,actives,passives,g_actives,g_passives) as s)
260 * A' = demod [e'] A *
263 * new = supright e'' A'' *
264 * new'= demod A'' new *
266 debug (lazy "Forward infer step...");
267 debug (lazy("Number of actives : " ^ (string_of_int (List.length (fst actives)))));
268 let id,_,_,_ = current in
269 let _ = Terms.get_from_bag id bag in
271 match Sup.keep_simplified current actives bag maxvar
274 | bag,Some (current,actives) ->
275 debug (lazy "simplified...");
276 let bag, maxvar, actives, new_clauses =
277 Sup.infer_right bag maxvar current actives
279 debug (lazy "Demodulating goals with actives...");
280 (* keep goals demodulated w.r.t. actives and check if solved *)
285 Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
288 | Some (bag,c1) -> bag,if c==c1 then c::acc else c::c1::acc)
291 let ctable = IDX.index_unit_clause IDX.DT.empty current in
292 let bag, maxvar, new_goals =
294 (fun (bag,m,acc) g ->
295 let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
297 (bag,maxvar,[]) g_actives
299 let bag = Terms.replace_in_bag (current,false,iterno) bag in
300 (* prerr_endline (Pp.pp_bag bag); *)
301 bag, maxvar, actives,
302 add_passive_clauses passives new_clauses, g_actives,
303 add_passive_goals g_passives new_goals
306 let debug_status (_,_,actives,passives,g_actives,g_passives) =
308 ((Printf.sprintf "Number of active goals : %d\n"
309 (List.length g_actives)) ^
310 (Printf.sprintf "Number of passive goals : %d\n"
311 (g_passive_set_cardinal g_passives)) ^
312 (Printf.sprintf "Number of actives : %d\n"
313 (List.length (fst actives))) ^
314 (Printf.sprintf "Number of passives : %d\n"
315 (passive_set_cardinal passives)))
319 (* we just check if any of the active goals is subsumed by a
320 passive clause, or if any of the passive goal is subsumed
321 by an active or passive clause *)
322 let last_chance (bag,maxvar,actives,passives,g_actives,g_passives) =
323 debug (lazy("Last chance " ^ string_of_float
324 (Unix.gettimeofday())));
325 let active_t = snd actives in
326 let passive_t,wset,_ = passives in
329 ("Passive set :" ^ (String.concat ";\n"
330 (List.map (fun _,cl -> Pp.pp_unit_clause cl)
331 (WeightPassiveSet.elements wset))))) in
332 let wset = IDX.elems passive_t in
335 ("Passive table :" ^(String.concat ";\n"
336 (List.map (fun _,cl -> Pp.pp_unit_clause cl)
337 (IDX.ClauseSet.elements wset))))) in
339 WeightPassiveSet.fold
341 if List.exists (Sup.are_alpha_eq x) g_actives then acc
349 (Sup.simplify_goal ~no_demod:true maxvar active_t bag [] x))
355 (Sup.simplify_goal ~no_demod:true maxvar passive_t bag [] x))
356 (g_actives@g_passives));
357 raise (Stop (Timeout (maxvar,bag)))
359 let check_timeout = function
361 | Some timeout -> Unix.gettimeofday () > timeout
363 let rec given_clause ~useage
364 bag maxvar iterno weight_picks max_steps timeout
365 actives passives g_actives g_passives
367 let iterno = iterno + 1 in
368 if iterno = max_steps || check_timeout timeout then
369 last_chance (bag,maxvar,actives,passives,g_actives,g_passives)
371 let use_age = useage && (weight_picks = (iterno / 6 + 1)) in
372 let weight_picks = if use_age then 0 else weight_picks+1
375 let rec aux_select bag
376 (passives:IDX.DT.t * WeightPassiveSet.t * AgePassiveSet.t)
378 let backward,(weight,current),passives,g_passives =
379 select ~use_age passives g_passives
381 if use_age && weight > monster then
382 let bag,cl = Terms.add_to_bag current bag in
384 aux_select bag passives (add_passive_goal g_passives cl)
386 aux_select bag (add_passive_clause passives cl) g_passives
388 let bag = Terms.replace_in_bag (current,false,iterno) bag in
390 let _ = debug (lazy("Selected goal : " ^ Pp.pp_unit_clause current)) in
393 ~no_demod:false maxvar (snd actives) bag g_actives current
395 | None -> aux_select bag passives g_passives
396 | Some (bag,g_current) ->
397 backward_infer_step bag maxvar actives passives
398 g_actives g_passives g_current iterno
400 let _ = debug (lazy("Selected fact : " ^ Pp.pp_unit_clause current))
402 if Sup.orphan_murder bag (fst actives) current then
403 let _ = debug (lazy "Orphan murdered") in
404 let bag = Terms.replace_in_bag (current,true,iterno) bag in
405 aux_select bag passives g_passives
407 let s = bag,maxvar,actives,passives,g_actives,g_passives in
408 let s1 = forward_infer_step s current iterno
410 if s == s1 then aux_select bag passives g_passives
413 (*prerr_endline "Active table :";
414 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
417 let (bag,maxvar,actives,passives,g_actives,g_passives) as status =
418 aux_select bag passives g_passives
420 debug (debug_status status);
422 bag maxvar iterno weight_picks max_steps timeout
423 actives passives g_actives g_passives
426 (* similar to given_clause, but it merely works on goals,
427 in parallel, at each iteration *)
428 let rec goal_narrowing iterno max_steps timeout status
430 debug (debug_status status);
431 let iterno = iterno + 1 in
432 if iterno = max_steps || check_timeout timeout then
435 let _,_,_,_,_,g_passives = status in
436 let passive_goals = WeightPassiveSet.elements (fst g_passives) in
440 let bag,maxvar,actives,passives,g_actives,g_passives = acc in
442 remove_passive_goal g_passives g in
443 let current = snd g in
445 debug (lazy("Selected goal : " ^ Pp.pp_unit_clause current))
449 ~no_demod:false maxvar (snd actives) bag g_actives current
452 | Some (bag,g_current) ->
454 debug (lazy("Demodulated goal : "
455 ^ Pp.pp_unit_clause g_current))
457 backward_infer_step bag maxvar actives passives
458 g_actives g_passives g_current iterno)
461 goal_narrowing iterno max_steps timeout newstatus
463 let compute_result bag i =
465 let rec traverse ongoal (accg,acce) i =
466 match Terms.get_from_bag i bag with
467 | (id,_,_,Terms.Exact _),_,_ ->
468 if ongoal then [i],acce else
469 if (List.mem i acce) then accg,acce else accg,acce@[i]
470 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),_,_ ->
471 if (not ongoal) && (List.mem i acce) then accg,acce
474 traverse false (traverse ongoal (accg,acce) i1) i2
476 if ongoal then i::accg,acce else accg,i::acce
478 let gsteps,esteps = traverse true ([],[]) i in
479 (List.rev esteps)@gsteps
481 debug (lazy ("steps: " ^ (string_of_int (List.length l))));
485 let (cl,_,_) = Terms.get_from_bag i bag in
486 max acc (Order.compute_unit_clause_weight cl)) 0 l in
487 debug (lazy ("Max weight : " ^ (string_of_int max_w)));
488 (* List.iter (fun id -> let ((_,lit,_,proof as cl),d,it) =
489 Terms.get_from_bag id bag in
492 (Printf.sprintf "Id : %d, selected at %d, weight %d,disc, by %s"
493 id it (Order.compute_unit_clause_weight cl)
494 (Pp.pp_proof_step proof))
497 (Printf.sprintf "Id : %d, selected at %d, weight %d by %s"
498 id it (Order.compute_unit_clause_weight cl)
499 (Pp.pp_proof_step proof))) l;*)
500 debug (lazy ("Proof:" ^
504 let cl,_,_ = Terms.get_from_bag x bag in
505 Pp.pp_unit_clause cl) l))));
506 Unsatisfiable [ bag, i, l ]
508 let paramod ~useage ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
509 let _initial_timestamp = Unix.gettimeofday () in
511 add_passive_clauses ~no_weight:true passive_empty_set passives
514 add_passive_goals ~no_weight:true g_passive_empty_set g_passives
516 let g_actives = [] in
517 let actives = [], IDX.DT.empty in
519 given_clause ~useage ~noinfer:false
520 bag maxvar 0 0 max_steps timeout actives passives g_actives g_passives
522 | Sup.Success (bag, _, (i,_,_,_)) ->
524 | Stop (Unsatisfiable _) -> Error "solution found!"
528 let fast_eq_check s goal =
529 let (_,_,_,_,_,g_passives) as s = initialize_goal s goal in
530 if is_passive_g_set_empty g_passives then Error "not an equation"
533 goal_narrowing 0 2 None s
535 | Sup.Success (bag, _, (i,_,_,_)) ->
537 | Stop (Unsatisfiable _) -> Error "solution found!"