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 ;;
15 (* let debug _ = ();;*)
19 module Paramod (B : Terms.Blob) = struct
20 exception Failure of string * B.t Terms.bag * int * int
21 type bag = B.t Terms.bag * int
23 module FU = FoUnif.Founif(B)
24 module IDX = Index.Index(B)
25 module Sup = Superposition.Superposition(B)
26 module Utils = FoUtils.Utils(B)
27 module WeightOrderedPassives =
29 type t = B.t Terms.passive_clause
30 let compare = Utils.compare_passive_clauses_weight
33 module AgeOrderedPassives =
35 type t = B.t Terms.passive_clause
36 let compare = Utils.compare_passive_clauses_age
39 module WeightPassiveSet = Set.Make(WeightOrderedPassives)
40 module AgePassiveSet = Set.Make(AgeOrderedPassives)
42 let add_passive_clause ?(no_weight=false) (passives_w,passives_a) cl =
43 let cl = if no_weight then (0,cl)
44 else Utils.mk_passive_clause cl in
45 WeightPassiveSet.add cl passives_w, AgePassiveSet.add cl passives_a
48 let add_passive_goal ?(no_weight=false) (passives_w,passives_a) g =
49 let g = if no_weight then (0,g)
50 else Utils.mk_passive_goal g in
51 WeightPassiveSet.add g passives_w, AgePassiveSet.add g passives_a
54 let remove_passive_clause (passives_w,passives_a) cl =
55 let passives_w = WeightPassiveSet.remove cl passives_w in
56 let passives_a = AgePassiveSet.remove cl passives_a in
60 let add_passive_clauses ?(no_weight=false)
61 (passives_w,passives_a) new_clauses =
62 let new_clauses_w,new_clauses_a =
63 List.fold_left (add_passive_clause ~no_weight)
64 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
66 (WeightPassiveSet.union new_clauses_w passives_w,
67 AgePassiveSet.union new_clauses_a passives_a)
70 let add_passive_goals ?(no_weight=false)
71 (passives_w,passives_a) new_clauses =
72 let new_clauses_w,new_clauses_a =
73 List.fold_left (add_passive_goal ~no_weight)
74 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
76 (WeightPassiveSet.union new_clauses_w passives_w,
77 AgePassiveSet.union new_clauses_a passives_a)
80 let is_passive_set_empty (passives_w,passives_a) =
81 if (WeightPassiveSet.is_empty passives_w) then begin
82 assert (AgePassiveSet.is_empty passives_a); true
84 assert (not (AgePassiveSet.is_empty passives_a)); false
88 let passive_set_cardinal (passives_w,_) = WeightPassiveSet.cardinal passives_w
90 let passive_empty_set =
91 (WeightPassiveSet.empty,AgePassiveSet.empty)
94 let pick_min_passive ~use_age (passives_w,passives_a) =
95 if use_age then AgePassiveSet.min_elt passives_a
96 else WeightPassiveSet.min_elt passives_w
99 let mk_clause bag maxvar (t,ty) =
100 let (proof,ty) = B.saturate t ty in
101 let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
102 let bag, c = Utils.add_to_bag bag c in
106 let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
107 let mk_goal (bag,maxvar) = mk_clause bag maxvar;;
109 (* TODO : global age over facts and goals (without comparing weights) *)
110 let select ~use_age passives g_passives =
111 if is_passive_set_empty passives then begin
112 assert (not (is_passive_set_empty g_passives));
113 let g_cl = pick_min_passive ~use_age:use_age g_passives in
114 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
116 else let cl = pick_min_passive ~use_age:use_age passives in
117 if is_passive_set_empty g_passives then
118 (false,cl,remove_passive_clause passives cl,g_passives)
120 let g_cl = pick_min_passive ~use_age:use_age g_passives in
121 let (id1,_,_,_),(id2,_,_,_) = snd cl, snd g_cl in
122 let cmp = if use_age then id1 <= id2
123 else fst cl <= fst g_cl
126 (false,cl,remove_passive_clause passives cl,g_passives)
128 (true,g_cl,passives,remove_passive_clause g_passives g_cl)
131 let backward_infer_step bag maxvar actives passives
132 g_actives g_passives g_current =
133 (* superposition left, simplifications on goals *)
134 debug "infer_left step...";
135 let bag, maxvar, new_goals =
136 Sup.infer_left bag maxvar g_current actives
138 debug "Performed infer_left step";
139 bag, maxvar, actives, passives, g_current::g_actives,
140 (add_passive_goals g_passives new_goals)
143 let forward_infer_step bag maxvar actives passives g_actives
149 * A' = demod [e'] A *
152 * new = supright e'' A'' *
153 * new'= demod A'' new *
155 debug "Forward infer step...";
156 let bag, maxvar, actives, new_clauses =
157 Sup.infer_right bag maxvar current actives
159 debug "Demodulating goals with actives...";
160 (* keep goals demodulated w.r.t. actives and check if solved *)
165 Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
168 | Some (bag,c) -> bag,c::acc)
171 let ctable = IDX.index_unit_clause IDX.DT.empty current in
172 let bag, maxvar, new_goals =
174 (fun (bag,m,acc) g ->
175 let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
177 (bag,maxvar,[]) g_actives
179 bag, maxvar, actives,
180 add_passive_clauses passives new_clauses, g_actives,
181 add_passive_goals g_passives new_goals
184 let rec given_clause ~noinfer
185 bag maxvar iterno max_steps timeout
186 actives passives g_actives g_passives
188 let iterno = iterno + 1 in
189 if iterno = max_steps then
190 raise (Failure ("No iterations left !",bag,maxvar,iterno));
191 (* timeout check: gettimeofday called only if timeout set *)
192 if timeout <> None &&
194 | None -> assert false
195 | Some timeout -> Unix.gettimeofday () > timeout) then
199 ("Last chance: all is indexed " ^ string_of_float
200 (Unix.gettimeofday()));
201 let maxgoals = 100 in
202 ignore(List.fold_left
205 ignore(Sup.simplify_goal ~no_demod:true
206 maxvar (snd actives) bag acc x)
211 raise (Failure (("Last chance: failed over " ^
212 string_of_int maxgoals^ " goal " ^
213 string_of_float (Unix.gettimeofday())),bag,maxvar,0));
215 else if false then (* activates last chance strategy *)
217 debug("Last chance: "^string_of_float (Unix.gettimeofday()));
218 given_clause ~noinfer:true bag maxvar iterno max_steps
219 (Some (Unix.gettimeofday () +. 20.))
220 actives passives g_actives g_passives;
221 raise (Failure ("Timeout !",bag,maxvar,iterno))
223 else raise (Failure ("Timeout !",bag,maxvar,iterno));
225 let use_age = iterno mod 5 = 0 in
227 let rec aux_select bag passives g_passives =
228 let backward,(weight,current),passives,g_passives =
229 select ~use_age passives g_passives
231 if use_age && weight > monster then
232 let bag,cl = Utils.add_to_bag bag current in
234 aux_select bag passives (add_passive_clause g_passives cl)
236 aux_select bag (add_passive_clause passives cl) g_passives
239 let _ = debug ("Selected goal : " ^ Pp.pp_unit_clause current) in
242 if weight > monster then None else Some (bag,current)
245 ~no_demod:false maxvar (snd actives) bag g_actives current
247 | None -> aux_select bag passives g_passives
248 | Some (bag,g_current) ->
250 let g_actives = g_current :: g_actives in
251 bag,maxvar,actives,passives,g_actives,g_passives
253 backward_infer_step bag maxvar actives passives
254 g_actives g_passives g_current
256 let _ = debug ("Selected fact : " ^ Pp.pp_unit_clause current) in
257 (*let is_orphan = Sup.orphan_murder bag (fst actives) current in*)
260 if weight > monster then bag,None
261 else bag, Some (current,actives)
262 else if Sup.orphan_murder bag (fst actives) current then
263 let (id,_,_,_) = current in
264 let bag = Terms.M.add id (current,true) bag in
266 else Sup.keep_simplified current actives bag maxvar
268 (*match Sup.one_pass_simplification current actives bag maxvar with*)
269 | bag,None -> aux_select bag passives g_passives
270 | bag,Some (current,actives) ->
271 (* if is_orphan then prerr_endline
272 ("WRONG discarded: " ^ (Pp.pp_unit_clause current));
274 prerr_endline (Pp.pp_unit_clause x))
277 (* List.iter (fun (id,_,_,_) -> let (cl,d) =
278 Terms.M.find id bag in
279 if d then prerr_endline
280 ("WRONG discarded: " ^ (Pp.pp_unit_clause cl)))
281 (current::fst actives);*)
284 current::fst actives,
285 IDX.index_unit_clause (snd actives) current
287 bag,maxvar,actives,passives,g_actives,g_passives
289 forward_infer_step bag maxvar actives passives
290 g_actives g_passives current
294 (*prerr_endline "Active table :";
295 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
298 let bag,maxvar,actives,passives,g_actives,g_passives =
299 aux_select bag passives g_passives
302 (Printf.sprintf "Number of active goals : %d"
303 (List.length g_actives));
305 (Printf.sprintf "Number of passive goals : %d"
306 (passive_set_cardinal g_passives));
308 (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
310 (Printf.sprintf "Number of passives : %d"
311 (passive_set_cardinal passives));
312 given_clause ~noinfer
313 bag maxvar iterno max_steps timeout
314 actives passives g_actives g_passives
317 let paramod ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
318 let initial_timestamp = Unix.gettimeofday () in
320 add_passive_clauses ~no_weight:true passive_empty_set passives
323 add_passive_goals ~no_weight:true passive_empty_set g_passives
325 let g_actives = [] in
326 let actives = [], IDX.DT.empty in
328 given_clause ~noinfer:false
329 bag maxvar 0 max_steps timeout actives passives g_actives g_passives
331 | Sup.Success (bag, _, (i,_,_,_)) ->
333 let rec traverse ongoal (accg,acce) i =
334 match Terms.M.find i bag with
335 | (id,_,_,Terms.Exact _),_ ->
336 if ongoal then [i],acce else
337 if (List.mem i acce) then accg,acce else accg,acce@[i]
338 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),_ ->
339 if (not ongoal) && (List.mem i acce) then accg,acce
342 traverse false (traverse ongoal (accg,acce) i1) i2
344 if ongoal then i::accg,acce else accg,i::acce
346 let gsteps,esteps = traverse true ([],[]) i in
347 (List.rev esteps)@gsteps
349 (* List.iter (fun id -> let (cl,d) =
350 Terms.M.find id bag in
351 if d then prerr_endline (Pp.pp_unit_clause cl)) l;*)
353 (Printf.sprintf "Found proof, %fs"
354 (Unix.gettimeofday() -. initial_timestamp));
356 prerr_endline "Proof:";
358 prerr_endline (Pp.pp_unit_clause (fst(Terms.M.find x bag)))) l;
361 | Failure (msg,_bag,_maxvar,iterno) ->
363 prerr_endline (Printf.sprintf "FAILURE in %d iterations" iterno);