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 = 10.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
37 (* TODO : fairness condition *)
39 if PassiveSet.is_empty passives then None
40 else let cl = PassiveSet.min_elt passives in
41 Some (snd cl,PassiveSet.remove cl passives)
43 let rec given_clause bag maxvar actives
44 passives g_actives g_passives =
46 incr nb_iter; if !nb_iter = max_nb_iter then
47 (*(prerr_endline "Bag :"; prerr_endline (Pp.pp_bag bag);
48 prerr_endline "Active table :";
49 (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
51 raise (Failure "No iterations left !");
52 if Unix.gettimeofday () > timeout then
53 raise (Failure "Timeout !");
57 (* superposition left, simplifications on goals *)
58 debug "infer_left step...";
59 let bag, maxvar, g_actives, g_passives =
60 match select g_passives with
61 | None -> bag, maxvar, g_actives, g_passives
62 | Some (g_current, g_passives) ->
63 debug ("Selected goal : " ^ Pp.pp_unit_clause g_current);
65 Sup.simplify_goal maxvar (snd actives) bag g_current
67 debug "Simplified goal";
68 let bag, maxvar, new_goals =
69 Sup.infer_left bag maxvar g_current actives
71 debug "Performed infer_left step";
72 let new_goals = List.fold_left add_passive_clause
73 PassiveSet.empty new_goals
75 bag, maxvar, g_current::g_actives,
76 (PassiveSet.union new_goals g_passives)
79 (Printf.sprintf "Number of active goals : %d"
80 (List.length g_actives));
82 (Printf.sprintf "Number of passive goals : %d"
83 (PassiveSet.cardinal g_passives));
92 * new = supright e'' A'' *
93 * new'= demod A'' new *
95 debug "Forward infer step...";
96 let bag, maxvar, actives, passives, g_passives =
97 let rec aux_simplify passives =
98 match select passives with
99 | None -> assert false
100 | Some (current, passives) ->
101 debug ("Selected fact : " ^ Pp.pp_unit_clause current);
102 match Sup.keep_simplified current actives bag maxvar with
103 (* match Sup.one_pass_simplification current actives bag maxvar with*)
104 | None -> aux_simplify passives
105 | Some x -> x,passives
107 let (current, bag, actives),passives = aux_simplify passives
109 debug ("Fact after simplification :"
110 ^ Pp.pp_unit_clause current);
111 let bag, maxvar, actives, new_clauses =
112 Sup.infer_right bag maxvar current actives
114 debug "Demodulating goals with actives...";
115 (* keep goals demodulated w.r.t. actives and check if solved *)
119 let bag, c = Sup.simplify_goal maxvar (snd actives) bag c in
123 let ctable = IDX.index_unit_clause IDX.DT.empty current in
124 let bag, maxvar, new_goals =
126 (fun (bag,m,acc) g ->
127 let bag, m, ng = Sup.infer_left bag maxvar g
128 ([current],ctable) in
130 (bag,maxvar,[]) g_actives
132 let new_clauses = List.fold_left add_passive_clause
133 PassiveSet.empty new_clauses in
134 let new_goals = List.fold_left add_passive_clause
135 PassiveSet.empty new_goals in
136 bag, maxvar, actives,
137 PassiveSet.union new_clauses passives,
138 PassiveSet.union new_goals g_passives
141 (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
143 (Printf.sprintf "Number of passives : %d"
144 (PassiveSet.cardinal passives));
145 given_clause bag maxvar actives passives g_actives g_passives
148 let mk_clause bag maxvar (t,ty) =
149 let (proof,ty) = B.saturate t ty in
150 let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
151 let bag, c = Utils.add_to_bag bag c in
154 let bag, maxvar, goal = mk_clause Terms.M.empty 0 t in
155 let g_actives = [] in
156 let g_passives = PassiveSet.singleton (Utils.mk_passive_clause goal) in
157 let passives, bag, maxvar =
159 (fun (cl, bag, maxvar) t ->
160 let bag, maxvar, c = mk_clause bag maxvar t in
161 (add_passive_clause cl c), bag, maxvar)
162 (PassiveSet.empty, bag, maxvar) table
164 let actives = [], IDX.DT.empty in
165 try given_clause bag maxvar actives passives g_actives g_passives
167 | Sup.Success (bag, _, (i,_,_,_)) ->
169 let rec traverse ongoal (accg,acce) i =
170 match Terms.M.find i bag with
171 | (id,_,_,Terms.Exact _) ->
172 if ongoal then [i],acce else
173 if (List.mem i acce) then accg,acce else accg,acce@[i]
174 | (_,_,_,Terms.Step (_,i1,i2,_,_,_)) ->
175 if (not ongoal) && (List.mem i acce) then accg,acce
178 traverse false (traverse ongoal (accg,acce) i1) i2
180 if ongoal then i::accg,acce else accg,i::acce
182 let gsteps,esteps = traverse true ([],[]) i in
183 (List.rev esteps)@gsteps
185 prerr_endline (Printf.sprintf "Found proof in %d iterations, %fs"
187 (Unix.gettimeofday() -. timeout +. amount_of_time));
188 (* prerr_endline "Proof:";
189 List.iter (fun x -> prerr_endline (string_of_int x);
190 prerr_endline (Pp.pp_unit_clause (Terms.M.find x bag))) l;*)
191 let proofterm = B.mk_proof bag i l in
192 prerr_endline (Printf.sprintf "Got proof term, %fs"
193 (Unix.gettimeofday() -. timeout +. amount_of_time));
194 let metasenv, proofterm =
195 let rec aux k metasenv = function
196 | NCic.Meta _ as t -> metasenv, t
198 let metasenv,i,_,_=NCicMetaSubst.mk_meta metasenv context `Term in
199 metasenv, NCic.Meta (i,(k,NCic.Irl (List.length context)))
200 | t -> NCicUntrusted.map_term_fold_a
201 (fun _ k -> k+1) k aux metasenv t
203 aux 0 metasenv proofterm
205 let metasenv, subst, proofterm, _prooftype =
207 (rdb#set_coerc_db NCicCoercion.empty_db)
208 metasenv subst context proofterm None
210 proofterm, metasenv, subst
211 | Failure _ -> prerr_endline
212 (Printf.sprintf "FAILURE in %d iterations" !nb_iter); assert false