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 ;;*)
17 let max_nb_iter = 999999999 ;;
18 let amount_of_time = 300.0 ;;
20 module Paramod (B : Terms.Blob) = struct
21 exception Failure of string * B.t Terms.bag * int * int
22 type bag = B.t Terms.bag * int
24 module FU = FoUnif.Founif(B)
25 module IDX = Index.Index(B)
26 module Sup = Superposition.Superposition(B)
27 module Utils = FoUtils.Utils(B)
28 module WeightOrderedPassives =
30 type t = B.t Terms.passive_clause
31 let compare = Utils.compare_passive_clauses_weight
34 module AgeOrderedPassives =
36 type t = B.t Terms.passive_clause
37 let compare = Utils.compare_passive_clauses_age
40 module WeightPassiveSet = Set.Make(WeightOrderedPassives)
41 module AgePassiveSet = Set.Make(AgeOrderedPassives)
43 let add_passive_clause ?(no_weight=false) (passives_w,passives_a) cl =
44 let cl = if no_weight then (0,cl)
45 else Utils.mk_passive_clause cl in
46 WeightPassiveSet.add cl passives_w, AgePassiveSet.add cl passives_a
49 let remove_passive_clause (passives_w,passives_a) cl =
50 let passives_w = WeightPassiveSet.remove cl passives_w in
51 let passives_a = AgePassiveSet.remove cl passives_a in
55 let add_passive_clauses (passives_w,passives_a) new_clauses =
56 let new_clauses_w,new_clauses_a = List.fold_left add_passive_clause
57 (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
59 (WeightPassiveSet.union new_clauses_w passives_w,
60 AgePassiveSet.union new_clauses_a passives_a)
63 let is_passive_set_empty (passives_w,passives_a) =
64 if (WeightPassiveSet.is_empty passives_w) then begin
65 assert (AgePassiveSet.is_empty passives_a); true
67 assert (not (AgePassiveSet.is_empty passives_a)); false
71 let passive_set_cardinal (passives_w,_) = WeightPassiveSet.cardinal passives_w
73 let passive_empty_set =
74 (WeightPassiveSet.empty,AgePassiveSet.empty)
77 let pick_min_passive ~use_age (passives_w,passives_a) =
78 if use_age then AgePassiveSet.min_elt passives_a
79 else WeightPassiveSet.min_elt passives_w
82 let mk_clause bag maxvar (t,ty) =
83 let (proof,ty) = B.saturate t ty in
84 let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
85 let bag, c = Utils.add_to_bag bag c in
89 let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
90 let mk_goal (bag,maxvar) = mk_clause bag maxvar;;
92 (* TODO : global age over facts and goals (without comparing weights) *)
93 let select ~use_age passives g_passives =
94 if is_passive_set_empty passives then begin
95 assert (not (is_passive_set_empty g_passives));
96 let g_cl = pick_min_passive ~use_age:use_age g_passives in
97 (true,snd g_cl,passives,remove_passive_clause g_passives g_cl)
99 else let cl = pick_min_passive ~use_age:use_age passives in
100 if is_passive_set_empty g_passives then
101 (false,snd cl,remove_passive_clause passives cl,g_passives)
103 let g_cl = pick_min_passive ~use_age:use_age g_passives in
104 if (fst cl <= fst g_cl) then
105 (false,snd cl,remove_passive_clause passives cl,g_passives)
107 (true,snd g_cl,passives,remove_passive_clause g_passives g_cl)
110 let backward_infer_step bag maxvar actives passives
111 g_actives g_passives g_current =
112 (* superposition left, simplifications on goals *)
113 debug "infer_left step...";
114 let bag, maxvar, new_goals =
115 Sup.infer_left bag maxvar g_current actives
117 debug "Performed infer_left step";
118 bag, maxvar, actives, passives, g_current::g_actives,
119 (add_passive_clauses g_passives new_goals)
122 let forward_infer_step bag maxvar actives passives g_actives
128 * A' = demod [e'] A *
131 * new = supright e'' A'' *
132 * new'= demod A'' new *
134 debug "Forward infer step...";
135 let bag, maxvar, actives, new_clauses =
136 Sup.infer_right bag maxvar current actives
138 debug "Demodulating goals with actives...";
139 (* keep goals demodulated w.r.t. actives and check if solved *)
143 match Sup.simplify_goal maxvar (snd actives) bag acc c with
145 | Some (bag,c) -> bag,c::acc)
148 let ctable = IDX.index_unit_clause IDX.DT.empty current in
149 let bag, maxvar, new_goals =
151 (fun (bag,m,acc) g ->
152 let bag, m, ng = Sup.infer_left bag m g
153 ([current],ctable) in
155 (bag,maxvar,[]) g_actives
157 bag, maxvar, actives,
158 add_passive_clauses passives new_clauses, g_actives,
159 add_passive_clauses g_passives new_goals
162 let rec given_clause bag maxvar nb_iter timeout actives passives g_actives g_passives =
163 (* prerr_endline "Bag :"; prerr_endline (Pp.pp_bag bag);
164 prerr_endline "Active table :";
165 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
167 let nb_iter = nb_iter + 1 in
168 if nb_iter = max_nb_iter then
169 raise (Failure ("No iterations left !",bag,maxvar,nb_iter));
170 if Unix.gettimeofday () > timeout then
171 raise (Failure ("Timeout !",bag,maxvar,nb_iter));
173 let use_age = nb_iter mod 10 = 0 in
175 let rec aux_select passives g_passives =
176 let backward,current,passives,g_passives =
177 select ~use_age passives g_passives
180 match Sup.simplify_goal maxvar (snd actives) bag g_actives current with
181 | None -> aux_select passives g_passives
182 | Some x -> let bag,g_current = x in
183 backward_infer_step bag maxvar actives passives
184 g_actives g_passives g_current
186 (* debug ("Selected fact : " ^ Pp.pp_unit_clause current); *)
187 match Sup.keep_simplified current actives bag maxvar with
188 (* match Sup.one_pass_simplification current actives bag maxvar with*)
189 | None -> aux_select passives g_passives
190 | Some x -> let (current, bag, actives) = x in
191 forward_infer_step bag maxvar actives passives
192 g_actives g_passives current
195 let bag,maxvar,actives,passives,g_actives,g_passives =
196 aux_select passives g_passives
199 (Printf.sprintf "Number of active goals : %d"
200 (List.length g_actives));
202 (Printf.sprintf "Number of passive goals : %d"
203 (passive_set_cardinal g_passives));
205 (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
207 (Printf.sprintf "Number of passives : %d"
208 (passive_set_cardinal passives));
210 bag maxvar nb_iter timeout actives passives g_actives g_passives
213 let paramod (bag,maxvar) ~g_passives ~passives =
214 let timeout = Unix.gettimeofday () +. amount_of_time in
215 let passives = add_passive_clauses passive_empty_set passives in
216 let g_passives = add_passive_clauses passive_empty_set g_passives in
217 let g_actives = [] in
218 let actives = [], IDX.DT.empty in
221 bag maxvar 0 timeout actives passives g_actives g_passives
223 | Sup.Success (bag, _, (i,_,_,_)) ->
225 let rec traverse ongoal (accg,acce) i =
226 match Terms.M.find i bag with
227 | (id,_,_,Terms.Exact _) ->
228 if ongoal then [i],acce else
229 if (List.mem i acce) then accg,acce else accg,acce@[i]
230 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)) ->
231 if (not ongoal) && (List.mem i acce) then accg,acce
234 traverse false (traverse ongoal (accg,acce) i1) i2
236 if ongoal then i::accg,acce else accg,i::acce
238 let gsteps,esteps = traverse true ([],[]) i in
239 (List.rev esteps)@gsteps
242 (Printf.sprintf "Found proof, %fs"
243 (Unix.gettimeofday() -. timeout +. amount_of_time));
244 prerr_endline "Proof:";
245 List.iter (fun x -> prerr_endline (string_of_int x);
246 prerr_endline (Pp.pp_unit_clause (Terms.M.find x bag))) l;
248 | Failure (msg,_bag,_maxvar,nb_iter) ->
250 prerr_endline (Printf.sprintf "FAILURE in %d iterations" nb_iter);