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 _ = ();; *)
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 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 false g_passives in
97 (true,snd g_cl,passives,remove_passive_clause g_passives g_cl)
99 else let cl = pick_min_passive false 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 false 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 debug "Selected and simplified";
136 (* debug ("Fact after simplification :"
137 ^ Pp.pp_unit_clause current); *)
138 let bag, maxvar, actives, new_clauses =
139 Sup.infer_right bag maxvar current actives
141 debug "Demodulating goals with actives...";
142 (* keep goals demodulated w.r.t. actives and check if solved *)
146 match Sup.simplify_goal maxvar (snd actives) bag acc c with
148 | Some (bag,c) -> bag,c::acc)
151 let ctable = IDX.index_unit_clause IDX.DT.empty current in
152 let bag, maxvar, new_goals =
154 (fun (bag,m,acc) g ->
155 let bag, m, ng = Sup.infer_left bag m g
156 ([current],ctable) in
158 (bag,maxvar,[]) g_actives
160 bag, maxvar, actives,
161 add_passive_clauses passives new_clauses, g_actives,
162 add_passive_clauses g_passives new_goals
165 let rec given_clause bag maxvar nb_iter timeout actives passives g_actives g_passives =
166 (* prerr_endline "Bag :"; prerr_endline (Pp.pp_bag bag);
167 prerr_endline "Active table :";
168 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
170 let nb_iter = nb_iter + 1 in
171 if nb_iter = max_nb_iter then
172 raise (Failure ("No iterations left !",bag,maxvar,nb_iter));
173 if Unix.gettimeofday () > timeout then
174 raise (Failure ("Timeout !",bag,maxvar,nb_iter));
176 let rec aux_select passives g_passives =
177 let backward,current,passives,g_passives = select passives g_passives in
179 match Sup.simplify_goal maxvar (snd actives) bag g_actives current with
180 | None -> aux_select passives g_passives
181 | Some x -> let bag,g_current = x in
182 backward_infer_step bag maxvar actives passives
183 g_actives g_passives g_current
185 (* debug ("Selected fact : " ^ Pp.pp_unit_clause current); *)
186 match Sup.keep_simplified current actives bag maxvar with
187 (* match Sup.one_pass_simplification current actives bag maxvar with*)
188 | None -> aux_select passives g_passives
189 | Some x -> let (current, bag, actives) = x in
190 forward_infer_step bag maxvar actives passives
191 g_actives g_passives current
194 let bag,maxvar,actives,passives,g_actives,g_passives =
195 aux_select passives g_passives
198 (Printf.sprintf "Number of active goals : %d"
199 (List.length g_actives));
201 (Printf.sprintf "Number of passive goals : %d"
202 (passive_set_cardinal g_passives));
204 (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
206 (Printf.sprintf "Number of passives : %d"
207 (passive_set_cardinal passives));
209 bag maxvar nb_iter timeout actives passives g_actives g_passives
212 let paramod (bag,maxvar) ~g_passives ~passives =
213 let timeout = Unix.gettimeofday () +. amount_of_time in
214 let passives = add_passive_clauses passive_empty_set passives in
215 let g_passives = add_passive_clauses passive_empty_set g_passives in
216 let g_actives = [] in
217 let actives = [], IDX.DT.empty in
220 bag maxvar 0 timeout actives passives g_actives g_passives
222 | Sup.Success (bag, _, (i,_,_,_)) ->
224 let rec traverse ongoal (accg,acce) i =
225 match Terms.M.find i bag with
226 | (id,_,_,Terms.Exact _) ->
227 if ongoal then [i],acce else
228 if (List.mem i acce) then accg,acce else accg,acce@[i]
229 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)) ->
230 if (not ongoal) && (List.mem i acce) then accg,acce
233 traverse false (traverse ongoal (accg,acce) i1) i2
235 if ongoal then i::accg,acce else accg,i::acce
237 let gsteps,esteps = traverse true ([],[]) i in
238 (List.rev esteps)@gsteps
241 (Printf.sprintf "Found proof, %fs"
242 (Unix.gettimeofday() -. timeout +. amount_of_time));
243 prerr_endline "Proof:";
244 List.iter (fun x -> prerr_endline (string_of_int x);
245 prerr_endline (Pp.pp_unit_clause (Terms.M.find x bag))) l;
247 | Failure (msg,_bag,_maxvar,nb_iter) ->
249 prerr_endline (Printf.sprintf "FAILURE in %d iterations" nb_iter);