1 (* LOOPING : COL057-1.ma *)
4 () (* prerr_endline s *)
7 let nparamod rdb metasenv subst context t table =
8 let max_nb_iter = 999999999 in
9 let amount_of_time = 300.0 in
11 let metasenv = metasenv
16 let nb_iter = ref 0 in
17 let module B = NCicBlob.NCicBlob(C) in
18 let module Pp = Pp.Pp (B) in
19 let module FU = FoUnif.Founif(B) in
20 let module IDX = Index.Index(B) in
21 let module Sup = Superposition.Superposition(B) in
22 let module Utils = FoUtils.Utils(B) in
24 let module OrderedPassives =
26 type t = B.t Terms.passive_clause
28 let compare = Utils.compare_passive_clauses
31 let module PassiveSet = Set.Make(OrderedPassives)
33 let add_passive_clause passives cl =
34 PassiveSet.add (Utils.mk_passive_clause cl) passives
36 let timeout = Unix.gettimeofday () +. amount_of_time in
38 (* TODO : fairness condition *)
39 let select passives g_passives =
40 if PassiveSet.is_empty passives then begin
41 assert (not (PassiveSet.is_empty g_passives));
42 let g_cl = PassiveSet.min_elt g_passives in
43 (true,snd g_cl,passives,PassiveSet.remove g_cl g_passives)
45 else let cl = PassiveSet.min_elt passives in
46 if PassiveSet.is_empty g_passives then
47 (false,snd cl,PassiveSet.remove cl passives,g_passives)
49 let g_cl = PassiveSet.min_elt g_passives in
50 if (fst cl <= fst g_cl) then
51 (false,snd cl,PassiveSet.remove cl passives,g_passives)
53 (true,snd g_cl,passives,PassiveSet.remove g_cl g_passives)
56 let backward_infer_step bag maxvar actives passives g_actives g_passives g_current =
57 (* superposition left, simplifications on goals *)
58 debug "infer_left step...";
59 debug ("Selected goal : " ^ Pp.pp_unit_clause g_current);
61 Sup.simplify_goal maxvar (snd actives) bag g_current
63 debug "Simplified goal";
64 let bag, maxvar, new_goals =
65 Sup.infer_left bag maxvar g_current actives
67 debug "Performed infer_left step";
68 let new_goals = List.fold_left add_passive_clause
69 PassiveSet.empty new_goals
71 bag, maxvar, actives, passives, g_current::g_actives,
72 (PassiveSet.union new_goals g_passives)
75 let forward_infer_step bag maxvar actives passives g_actives
84 * new = supright e'' A'' *
85 * new'= demod A'' new *
87 debug "Forward infer step...";
88 debug "Selected and simplified";
89 (* debug ("Fact after simplification :"
90 ^ Pp.pp_unit_clause current); *)
91 let bag, maxvar, actives, new_clauses =
92 Sup.infer_right bag maxvar current actives
94 debug "Demodulating goals with actives...";
95 debug ("Current : " ^ (Pp.pp_unit_clause current));
96 debug ("Active goal : " ^ (Pp.pp_unit_clause (List.hd g_actives)));
97 (* keep goals demodulated w.r.t. actives and check if solved *)
101 let bag, c = Sup.simplify_goal maxvar (snd actives) bag c in
105 debug (Pp.pp_unit_clause current);
106 debug (Pp.pp_unit_clause (List.hd g_actives));
107 let ctable = IDX.index_unit_clause IDX.DT.empty current in
108 let bag, maxvar, new_goals =
110 (fun (bag,m,acc) g ->
111 let bag, m, ng = Sup.infer_left bag m g
112 ([current],ctable) in
114 (bag,maxvar,[]) g_actives
116 let new_clauses = List.fold_left add_passive_clause
117 PassiveSet.empty new_clauses in
118 let new_goals = List.fold_left add_passive_clause
119 PassiveSet.empty new_goals in
120 debug (string_of_int (PassiveSet.cardinal new_goals));
121 debug (string_of_int (PassiveSet.cardinal g_passives));
122 bag, maxvar, actives,
123 PassiveSet.union new_clauses passives, g_actives,
124 PassiveSet.union new_goals g_passives
127 let rec given_clause bag maxvar actives passives g_actives g_passives =
128 (* prerr_endline "Bag :"; prerr_endline (Pp.pp_bag bag);
129 prerr_endline "Active table :";
130 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
132 incr nb_iter; if !nb_iter = max_nb_iter then
133 raise (Failure "No iterations left !");
134 if Unix.gettimeofday () > timeout then
135 raise (Failure "Timeout !");
138 let rec aux_select passives g_passives =
139 let backward,current,passives,g_passives = select passives g_passives in
141 backward_infer_step bag maxvar actives passives
142 g_actives g_passives current
144 (* debug ("Selected fact : " ^ Pp.pp_unit_clause current); *)
145 match Sup.keep_simplified current actives bag maxvar with
146 (* match Sup.one_pass_simplification current actives bag maxvar with*)
147 | None -> aux_select passives g_passives
148 | Some x -> let (current, bag, actives) = x in
149 forward_infer_step bag maxvar actives passives
150 g_actives g_passives current
153 let bag,maxvar,actives,passives,g_actives,g_passives =
154 aux_select passives g_passives
156 assert (PassiveSet.cardinal g_passives < 2);
158 (Printf.sprintf "Number of active goals : %d"
159 (List.length g_actives));
161 (Printf.sprintf "Number of passive goals : %d"
162 (PassiveSet.cardinal g_passives));
164 (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
166 (Printf.sprintf "Number of passives : %d"
167 (PassiveSet.cardinal passives));
168 given_clause bag maxvar actives passives g_actives g_passives
171 let mk_clause bag maxvar (t,ty) =
172 let (proof,ty) = B.saturate t ty in
173 let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
174 let bag, c = Utils.add_to_bag bag c in
177 let bag, maxvar, goal = mk_clause Terms.M.empty 0 t in
178 let g_actives = [] in
179 let g_passives = PassiveSet.singleton (Utils.mk_passive_clause goal) in
180 let passives, bag, maxvar =
182 (fun (cl, bag, maxvar) t ->
183 let bag, maxvar, c = mk_clause bag maxvar t in
184 (add_passive_clause cl c), bag, maxvar)
185 (PassiveSet.empty, bag, maxvar) table
187 let actives = [], IDX.DT.empty in
188 try given_clause bag maxvar actives passives g_actives g_passives
190 | Sup.Success (bag, _, (i,_,_,_)) ->
192 let rec traverse ongoal (accg,acce) i =
193 match Terms.M.find i bag with
194 | (id,_,_,Terms.Exact _) ->
195 if ongoal then [i],acce else
196 if (List.mem i acce) then accg,acce else accg,acce@[i]
197 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)) ->
198 if (not ongoal) && (List.mem i acce) then accg,acce
201 traverse false (traverse ongoal (accg,acce) i1) i2
203 if ongoal then i::accg,acce else accg,i::acce
205 let gsteps,esteps = traverse true ([],[]) i in
206 (List.rev esteps)@gsteps
208 prerr_endline (Printf.sprintf "Found proof in %d iterations, %fs"
210 (Unix.gettimeofday() -. timeout +. amount_of_time));
211 (* prerr_endline "Proof:";
212 List.iter (fun x -> prerr_endline (string_of_int x);
213 prerr_endline (Pp.pp_unit_clause (Terms.M.find x bag))) l;*)
214 let proofterm = B.mk_proof bag i l in
215 prerr_endline (Printf.sprintf "Got proof term, %fs"
216 (Unix.gettimeofday() -. timeout +. amount_of_time));
217 let metasenv, proofterm =
218 let rec aux k metasenv = function
219 | NCic.Meta _ as t -> metasenv, t
221 let metasenv,i,_,_=NCicMetaSubst.mk_meta metasenv context `Term in
222 metasenv, NCic.Meta (i,(k,NCic.Irl (List.length context)))
223 | t -> NCicUntrusted.map_term_fold_a
224 (fun _ k -> k+1) k aux metasenv t
226 aux 0 metasenv proofterm
228 let metasenv, subst, proofterm, _prooftype =
230 (rdb#set_coerc_db NCicCoercion.empty_db)
231 metasenv subst context proofterm None
233 proofterm, metasenv, subst
234 | Failure _ -> prerr_endline
235 (Printf.sprintf "FAILURE in %d iterations" !nb_iter); assert false