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 (* keep goals demodulated w.r.t. actives and check if solved *)
99 let bag, c = Sup.simplify_goal maxvar (snd actives) bag c in
103 let ctable = IDX.index_unit_clause IDX.DT.empty current in
104 let bag, maxvar, new_goals =
106 (fun (bag,m,acc) g ->
107 let bag, m, ng = Sup.infer_left bag m g
108 ([current],ctable) in
110 (bag,maxvar,[]) g_actives
112 let new_clauses = List.fold_left add_passive_clause
113 PassiveSet.empty new_clauses in
114 let new_goals = List.fold_left add_passive_clause
115 PassiveSet.empty new_goals in
116 bag, maxvar, actives,
117 PassiveSet.union new_clauses passives, g_actives,
118 PassiveSet.union new_goals g_passives
121 let rec given_clause bag maxvar actives passives g_actives g_passives =
122 (* prerr_endline "Bag :"; prerr_endline (Pp.pp_bag bag);
123 prerr_endline "Active table :";
124 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
126 incr nb_iter; if !nb_iter = max_nb_iter then
127 raise (Failure "No iterations left !");
128 if Unix.gettimeofday () > timeout then
129 raise (Failure "Timeout !");
132 let rec aux_select passives g_passives =
133 let backward,current,passives,g_passives = select passives g_passives in
135 backward_infer_step bag maxvar actives passives
136 g_actives g_passives current
138 (* debug ("Selected fact : " ^ Pp.pp_unit_clause current); *)
139 match Sup.keep_simplified current actives bag maxvar with
140 (* match Sup.one_pass_simplification current actives bag maxvar with*)
141 | None -> aux_select passives g_passives
142 | Some x -> let (current, bag, actives) = x in
143 forward_infer_step bag maxvar actives passives
144 g_actives g_passives current
147 let bag,maxvar,actives,passives,g_actives,g_passives =
148 aux_select passives g_passives
151 (Printf.sprintf "Number of active goals : %d"
152 (List.length g_actives));
154 (Printf.sprintf "Number of passive goals : %d"
155 (PassiveSet.cardinal g_passives));
157 (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
159 (Printf.sprintf "Number of passives : %d"
160 (PassiveSet.cardinal passives));
161 given_clause bag maxvar actives passives g_actives g_passives
164 let mk_clause bag maxvar (t,ty) =
165 let (proof,ty) = B.saturate t ty in
166 let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
167 let bag, c = Utils.add_to_bag bag c in
170 let bag, maxvar, goal = mk_clause Terms.M.empty 0 t in
171 let g_actives = [] in
172 let g_passives = PassiveSet.singleton (Utils.mk_passive_clause goal) in
173 let passives, bag, maxvar =
175 (fun (cl, bag, maxvar) t ->
176 let bag, maxvar, c = mk_clause bag maxvar t in
177 (add_passive_clause cl c), bag, maxvar)
178 (PassiveSet.empty, bag, maxvar) table
180 let actives = [], IDX.DT.empty in
181 try given_clause bag maxvar actives passives g_actives g_passives
183 | Sup.Success (bag, _, (i,_,_,_)) ->
185 let rec traverse ongoal (accg,acce) i =
186 match Terms.M.find i bag with
187 | (id,_,_,Terms.Exact _) ->
188 if ongoal then [i],acce else
189 if (List.mem i acce) then accg,acce else accg,acce@[i]
190 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)) ->
191 if (not ongoal) && (List.mem i acce) then accg,acce
194 traverse false (traverse ongoal (accg,acce) i1) i2
196 if ongoal then i::accg,acce else accg,i::acce
198 let gsteps,esteps = traverse true ([],[]) i in
199 (List.rev esteps)@gsteps
201 prerr_endline (Printf.sprintf "Found proof in %d iterations, %fs"
203 (Unix.gettimeofday() -. timeout +. amount_of_time));
204 (* prerr_endline "Proof:";
205 List.iter (fun x -> prerr_endline (string_of_int x);
206 prerr_endline (Pp.pp_unit_clause (Terms.M.find x bag))) l;*)
207 let proofterm = B.mk_proof bag i l in
208 prerr_endline (Printf.sprintf "Got proof term, %fs"
209 (Unix.gettimeofday() -. timeout +. amount_of_time));
210 let metasenv, proofterm =
211 let rec aux k metasenv = function
212 | NCic.Meta _ as t -> metasenv, t
214 let metasenv,i,_,_=NCicMetaSubst.mk_meta metasenv context `Term in
215 metasenv, NCic.Meta (i,(k,NCic.Irl (List.length context)))
216 | t -> NCicUntrusted.map_term_fold_a
217 (fun _ k -> k+1) k aux metasenv t
219 aux 0 metasenv proofterm
221 let metasenv, subst, proofterm, _prooftype =
223 (rdb#set_coerc_db NCicCoercion.empty_db)
224 metasenv subst context proofterm None
226 proofterm, metasenv, subst
230 (Printf.sprintf "FAILURE in %d iterations" !nb_iter); assert false