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 print s = prerr_endline (Lazy.force s) ;;
26 (t Terms.bag * int * t Terms.substitution * int list) list
29 | Timeout of int * t Terms.bag
30 type bag = t Terms.bag * int
32 val empty_state : state
33 val bag_of_state : state -> bag
34 val replace_bag: state -> bag -> state
35 val mk_passive : bag -> input * input -> bag * t Terms.unit_clause
36 val mk_goal : bag -> input * input -> bag * t Terms.unit_clause
37 val forward_infer_step :
39 t Terms.unit_clause ->
53 g_passives:t Terms.unit_clause list ->
54 passives:t Terms.unit_clause list -> szsontology
56 state -> input* input -> szsontology
58 state -> input* input -> szsontology
63 state -> input* input -> szsontology
66 module Paramod (B : Orderings.Blob) = struct
68 module FU = FoUnif.Founif(B)
69 module IDX = Index.Index(B)
70 module Sup = Superposition.Superposition(B)
71 module Utils = FoUtils.Utils(B)
73 module WeightOrderedPassives =
75 type t = B.t Terms.passive_clause
76 let compare = Utils.compare_passive_clauses_weight
79 module AgeOrderedPassives =
81 type t = B.t Terms.passive_clause
82 let compare = Utils.compare_passive_clauses_age
85 module WeightPassiveSet = Set.Make(WeightOrderedPassives)
86 module AgePassiveSet = Set.Make(AgeOrderedPassives)
90 type bag = B.t Terms.bag * int
93 (B.t Terms.bag * int * B.t Terms.substitution * int list) list
96 | Timeout of int * B.t Terms.bag
97 exception Stop of szsontology
101 * Index.Index(B).active_set
102 * (IDX.DT.t * WeightPassiveSet.t * AgePassiveSet.t)
103 * B.t Terms.unit_clause list
104 * (WeightPassiveSet.t * AgePassiveSet.t)
110 (IDX.DT.empty,WeightPassiveSet.empty,AgePassiveSet.empty),
112 (WeightPassiveSet.empty,AgePassiveSet.empty)
115 let bag_of_state (bag,n,_,_,_,_) = bag,n
118 let replace_bag (_,_,a,b,c,d) (bag,n) = bag,n,a,b,c,d
121 let add_passive_clause ?(no_weight=false)
122 (passive_t,passives_w,passives_a) cl =
123 let pcl = if no_weight then (0,cl)
124 else Utils.mk_passive_clause cl in
125 IDX.index_unit_clause passive_t cl,
126 WeightPassiveSet.add pcl passives_w,
127 AgePassiveSet.add pcl passives_a
130 let add_passive_goal ?(no_weight=false) (passives_w,passives_a) g =
131 let g = if no_weight then (0,g)
132 else Utils.mk_passive_goal g in
133 WeightPassiveSet.add g passives_w, AgePassiveSet.add g passives_a
136 let remove_passive_clause (passive_t,passives_w,passives_a) cl =
137 let passive_t = IDX.remove_unit_clause passive_t (snd cl) in
138 let passives_w = WeightPassiveSet.remove cl passives_w in
139 let passives_a = AgePassiveSet.remove cl passives_a in
140 passive_t,passives_w,passives_a
143 let add_passive_clauses ?(no_weight=false) =
144 List.fold_left (add_passive_clause ~no_weight)
147 let add_passive_goals ?(no_weight=false)
148 (passives_w,passives_a) new_clauses =
149 let new_clauses_w,new_clauses_a =
150 List.fold_left (add_passive_goal ~no_weight)
151 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
153 (WeightPassiveSet.union new_clauses_w passives_w,
154 AgePassiveSet.union new_clauses_a passives_a)
157 let remove_passive_goal (passives_w,passives_a) cl =
158 let passives_w = WeightPassiveSet.remove cl passives_w in
159 let passives_a = AgePassiveSet.remove cl passives_a in
160 passives_w,passives_a
163 let is_passive_set_empty (_,passives_w,passives_a) =
164 if (WeightPassiveSet.is_empty passives_w) then begin
165 assert (AgePassiveSet.is_empty passives_a); true
167 assert (not (AgePassiveSet.is_empty passives_a)); false
171 let is_passive_g_set_empty (passives_w,passives_a) =
172 if (WeightPassiveSet.is_empty passives_w) then begin
173 assert (AgePassiveSet.is_empty passives_a); true
175 assert (not (AgePassiveSet.is_empty passives_a)); false
179 let passive_set_cardinal (_,passives_w,_)
180 = WeightPassiveSet.cardinal passives_w
183 let g_passive_set_cardinal (passives_w,_)
184 = WeightPassiveSet.cardinal passives_w
187 let passive_empty_set =
188 (IDX.DT.empty,WeightPassiveSet.empty,AgePassiveSet.empty)
191 let g_passive_empty_set =
192 (WeightPassiveSet.empty,AgePassiveSet.empty)
195 let pick_min_passive ~use_age (_,passives_w,passives_a) =
196 if use_age then AgePassiveSet.min_elt passives_a
197 else WeightPassiveSet.min_elt passives_w
200 let pick_min_g_passive ~use_age (passives_w,passives_a) =
201 if use_age then AgePassiveSet.min_elt passives_a
202 else WeightPassiveSet.min_elt passives_w
205 let mk_clause bag maxvar (t,ty) =
206 let (proof,ty) = B.saturate t ty in
207 let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
208 let bag, c = Terms.add_to_bag c bag in
212 let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
213 let mk_goal (bag,maxvar) = mk_clause bag maxvar;;
214 let initialize_goal (bag,maxvar,actives,passives,_,_) t =
215 let (bag,maxvar), g = mk_goal (bag,maxvar) t in
216 let g_passives = g_passive_empty_set in
217 (* if the goal is not an equation we returns an empty
220 if Terms.is_eq_clause g then add_passive_goal g_passives g
223 (bag,maxvar,actives,passives,[],g_passives)
226 (* TODO : global age over facts and goals (without comparing weights) *)
227 let select ~use_age passives g_passives =
228 if is_passive_set_empty passives then begin
229 if (is_passive_g_set_empty g_passives) then
230 raise (Stop GaveUp) (* we say we are incomplete *)
232 let g_cl = pick_min_g_passive ~use_age:use_age g_passives in
233 (true,g_cl,passives,remove_passive_goal g_passives g_cl)
235 else let cl = pick_min_passive ~use_age:use_age passives in
236 if is_passive_g_set_empty g_passives then
237 (false,cl,remove_passive_clause passives cl,g_passives)
239 let g_cl = pick_min_g_passive ~use_age:use_age g_passives in
240 let (id1,_,_,_),(id2,_,_,_) = snd cl, snd g_cl in
241 let cmp = if use_age then id1 <= id2
242 else fst cl <= fst g_cl
245 (false,cl,remove_passive_clause passives cl,g_passives)
247 (true,g_cl,passives,remove_passive_goal g_passives g_cl)
250 let backward_infer_step bag maxvar actives passives
251 g_actives g_passives g_current iterno =
252 (* superposition left, simplifications on goals *)
253 debug (lazy "infer_left step...");
254 let bag, maxvar, new_goals =
255 Sup.infer_left bag maxvar g_current actives
257 debug (lazy "Performed infer_left step");
258 let bag = Terms.replace_in_bag (g_current,false,iterno) bag in
259 bag, maxvar, actives, passives, g_current::g_actives,
260 (add_passive_goals g_passives new_goals)
263 let pp_clauses actives passives =
264 let actives_l, _ = actives in
265 let passive_t,_,_ = passives in
266 let wset = IDX.elems passive_t in
267 ("Actives :" ^ (String.concat ";\n"
268 (List.map Pp.pp_unit_clause actives_l)))
270 ("Passives:" ^(String.concat ";\n"
271 (List.map (fun _,cl -> Pp.pp_unit_clause cl)
272 (IDX.ClauseSet.elements wset))))
275 let forward_infer_step
276 ((bag,maxvar,actives,passives,g_actives,g_passives) as s)
282 * A' = demod [e'] A *
285 * new = supright e'' A'' *
286 * new'= demod A'' new *
288 debug (lazy "Forward infer step...");
289 debug (lazy("Number of actives : " ^ (string_of_int (List.length (fst actives)))));
290 debug (lazy (pp_clauses actives passives));
291 match Sup.keep_simplified current actives bag maxvar
294 | bag,Some (current,actives) ->
295 debug (lazy ("simplified to " ^ (Pp.pp_unit_clause current)));
296 let bag, maxvar, actives, new_clauses =
297 Sup.infer_right bag maxvar current actives
301 ("New clauses :" ^ (String.concat ";\n"
302 (List.map Pp.pp_unit_clause new_clauses))));
303 debug (lazy "Demodulating goals with actives...");
304 (* keep goals demodulated w.r.t. actives and check if solved *)
309 Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
312 | Some (bag,c1) -> bag,if c==c1 then c::acc else c::c1::acc)
315 let ctable = IDX.index_unit_clause IDX.DT.empty current in
316 let bag, maxvar, new_goals =
318 (fun (bag,m,acc) g ->
319 let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
321 (bag,maxvar,[]) g_actives
323 let bag = Terms.replace_in_bag (current,false,iterno) bag in
324 (* prerr_endline (Pp.pp_bag bag); *)
325 bag, maxvar, actives,
326 add_passive_clauses passives new_clauses, g_actives,
327 add_passive_goals g_passives new_goals
330 let debug_status (_,_,actives,passives,g_actives,g_passives) =
332 ((Printf.sprintf "Number of active goals : %d\n"
333 (List.length g_actives)) ^
334 (Printf.sprintf "Number of passive goals : %d\n"
335 (g_passive_set_cardinal g_passives)) ^
336 (Printf.sprintf "Number of actives : %d\n"
337 (List.length (fst actives))) ^
338 (Printf.sprintf "Number of passives : %d\n"
339 (passive_set_cardinal passives)))
343 (* we just check if any of the active goals is subsumed by a
344 passive clause, or if any of the passive goal is subsumed
345 by an active or passive clause *)
346 let last_chance (bag,maxvar,actives,passives,g_actives,g_passives) =
347 debug (lazy("Last chance " ^ string_of_float
348 (Unix.gettimeofday())));
349 let actives_l, active_t = actives in
350 let passive_t,wset,_ = passives in
353 ("Actives :" ^ (String.concat ";\n"
354 (List.map Pp.pp_unit_clause actives_l)))) in
355 let wset = IDX.elems passive_t in
358 ("Passives:" ^(String.concat ";\n"
359 (List.map (fun _,cl -> Pp.pp_unit_clause cl)
360 (IDX.ClauseSet.elements wset))))) in
362 WeightPassiveSet.fold
364 if List.exists (Sup.are_alpha_eq x) g_actives then acc
372 (debug (lazy("ckecking goal vs a: " ^ Pp.pp_unit_clause x));
373 Sup.simplify_goal ~no_demod:true maxvar active_t bag [] x))
379 (debug (lazy("ckecking goal vs p: " ^ Pp.pp_unit_clause x));
380 Sup.simplify_goal ~no_demod:true maxvar passive_t bag [] x))
381 (g_actives@g_passives));
382 raise (Stop (Timeout (maxvar,bag)))
384 let check_timeout = function
386 | Some timeout -> Unix.gettimeofday () > timeout
388 let rec given_clause ~useage
389 bag maxvar iterno weight_picks max_steps timeout
390 actives passives g_actives g_passives
392 let iterno = iterno + 1 in
393 if iterno = max_steps || check_timeout timeout then
394 last_chance (bag,maxvar,actives,passives,g_actives,g_passives)
396 let use_age = useage && (weight_picks = (iterno / 6 + 1)) in
397 let weight_picks = if use_age then 0 else weight_picks+1
400 let rec aux_select bag
401 (passives:IDX.DT.t * WeightPassiveSet.t * AgePassiveSet.t)
403 let backward,(weight,current),passives,g_passives =
404 select ~use_age passives g_passives
406 if use_age && weight > monster then
407 let bag,cl = Terms.add_to_bag current bag in
409 aux_select bag passives (add_passive_goal g_passives cl)
411 aux_select bag (add_passive_clause passives cl) g_passives
413 let bag = Terms.replace_in_bag (current,false,iterno) bag in
415 let _ = debug (lazy("Selected goal : " ^ Pp.pp_unit_clause current)) in
418 ~no_demod:false maxvar (snd actives) bag g_actives current
420 | None -> aux_select bag passives g_passives
421 | Some (bag,g_current) ->
422 backward_infer_step bag maxvar actives passives
423 g_actives g_passives g_current iterno
425 let _ = debug (lazy("Selected fact : " ^ Pp.pp_unit_clause current))
427 if Sup.orphan_murder bag (fst actives) current then
428 let _ = debug (lazy "Orphan murdered") in
429 let bag = Terms.replace_in_bag (current,true,iterno) bag in
430 aux_select bag passives g_passives
432 let s = bag,maxvar,actives,passives,g_actives,g_passives in
433 let s1 = forward_infer_step s current iterno
435 if s == s1 then aux_select bag passives g_passives
438 (*prerr_endline "Active table :";
439 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
442 let (bag,maxvar,actives,passives,g_actives,g_passives) as status =
443 aux_select bag passives g_passives
445 debug (debug_status status);
447 bag maxvar iterno weight_picks max_steps timeout
448 actives passives g_actives g_passives
451 let check_and_infer ~no_demod iterno status current =
452 let bag,maxvar,actives,passives,g_actives,g_passives = status in
455 ~no_demod maxvar (snd actives) bag g_actives current
457 | None -> debug (lazy "None"); status
458 | Some (bag,g_current) ->
460 debug (lazy("Infer on goal : "
461 ^ Pp.pp_unit_clause g_current))
463 backward_infer_step bag maxvar actives passives
464 g_actives g_passives g_current iterno
466 (* similar to given_clause, but it merely works on goals,
467 in parallel, at each iteration *)
468 let rec goal_narrowing iterno max_steps timeout status
470 debug (debug_status status);
471 let iterno = iterno + 1 in
472 if iterno = max_steps || check_timeout timeout then
475 let _,_,_,_,_,g_passives = status in
476 let passive_goals = WeightPassiveSet.elements (fst g_passives) in
480 let bag,maxvar,actives,passives,g_actives,g_passives = acc in
482 remove_passive_goal g_passives g in
483 let current = snd g in
485 debug (lazy("Selected goal : " ^ Pp.pp_unit_clause current))
487 (* we work both on the original goal and the demodulated one*)
488 let acc = check_and_infer ~no_demod:false iterno acc current
489 in check_and_infer ~no_demod:true iterno acc current)
492 goal_narrowing iterno max_steps timeout newstatus
494 let compute_result bag i subst =
496 let rec traverse ongoal (accg,acce) i =
497 match Terms.get_from_bag i bag with
498 | (id,_,_,Terms.Exact _),_,_ ->
499 if ongoal then [i],acce else
500 if (List.mem i acce) then accg,acce else accg,acce@[i]
501 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),_,_ ->
502 if (not ongoal) && (List.mem i acce) then accg,acce
505 traverse false (traverse ongoal (accg,acce) i1) i2
507 if ongoal then i::accg,acce else accg,i::acce
509 let gsteps,esteps = traverse true ([],[]) i in
510 (List.rev esteps)@gsteps
512 debug (lazy ("steps: " ^ (string_of_int (List.length l))));
516 let (cl,_,_) = Terms.get_from_bag i bag in
517 max acc (Order.compute_unit_clause_weight cl)) 0 l in
518 debug (lazy ("Max weight : " ^ (string_of_int max_w)));
519 (* List.iter (fun id -> let ((_,lit,_,proof as cl),d,it) =
520 Terms.get_from_bag id bag in
523 (Printf.sprintf "Id : %d, selected at %d, weight %d,disc, by %s"
524 id it (Order.compute_unit_clause_weight cl)
525 (Pp.pp_proof_step proof))
528 (Printf.sprintf "Id : %d, selected at %d, weight %d by %s"
529 id it (Order.compute_unit_clause_weight cl)
530 (Pp.pp_proof_step proof))) l;*)
531 debug (lazy ("Proof:" ^
535 let cl,_,_ = Terms.get_from_bag x bag in
536 Pp.pp_unit_clause cl) l))));
537 Unsatisfiable [ bag, i, subst, l ]
539 let paramod ~useage ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
540 let _initial_timestamp = Unix.gettimeofday () in
542 add_passive_clauses ~no_weight:true passive_empty_set passives
545 add_passive_goals ~no_weight:true g_passive_empty_set g_passives
547 let g_actives = [] in
548 let actives = [], IDX.DT.empty in
550 given_clause ~useage ~noinfer:false
551 bag maxvar 0 0 max_steps timeout actives passives g_actives g_passives
553 | Sup.Success (bag, _, (i,_,_,_),subst) ->
554 compute_result bag i subst
555 | Stop (Unsatisfiable _) -> Error "solution found!"
560 let bag,maxvar,actives,passives,g_actives,g_passives = s in
561 let (bag,maxvar), g = mk_goal (bag,maxvar) goal in
562 if Terms.is_eq_clause g then
563 let bag, ((i,_,_,_) as g1) = Sup.demodulate bag g (snd actives) in
564 if g1 = g then GaveUp else compute_result bag i []
567 let fast_eq_check s goal =
568 let (_,_,_,_,_,g_passives) as s = initialize_goal s goal in
569 if is_passive_g_set_empty g_passives then Error "not an equation"
572 goal_narrowing 0 2 None s
574 | Sup.Success (bag, _, (i,_,_,_),subst) ->
575 compute_result bag i subst
576 | Stop (Unsatisfiable _) -> Error "solution found!"
580 let nparamod ~useage ~max_steps ?timeout s goal =
581 let bag,maxvar,actives,passives,g_actives,g_passives
582 = initialize_goal s goal in
583 if is_passive_g_set_empty g_passives then Error "not an equation"
585 try given_clause ~useage ~noinfer:false
586 bag maxvar 0 0 max_steps timeout actives passives g_actives g_passives
588 | Sup.Success (bag, _, (i,_,_,_),subst) ->
589 compute_result bag i subst
590 | Stop (Unsatisfiable _) -> Error "solution found!"