]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/paramod.ml
Removed debug printing
[helm.git] / helm / software / components / ng_paramodulation / paramod.ml
1 (* LOOPING : COL057-1.ma *)
2
3 let debug s =
4    () (* prerr_endline s *)
5 ;;
6
7 let nparamod rdb metasenv subst context t table =
8   let max_nb_iter = 999999999 in
9   let amount_of_time = 300.0 in
10   let module C = struct
11     let metasenv = metasenv
12     let subst = subst
13     let context = context
14   end
15   in
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
23     
24   let module OrderedPassives =
25       struct
26         type t = B.t Terms.passive_clause
27             
28         let compare = Utils.compare_passive_clauses
29       end
30   in
31   let module PassiveSet = Set.Make(OrderedPassives)
32   in
33   let add_passive_clause passives cl =
34     PassiveSet.add (Utils.mk_passive_clause cl) passives
35   in
36   let timeout = Unix.gettimeofday () +. amount_of_time in 
37
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)
44     end
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)
48       else
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)
52           else
53             (true,snd g_cl,passives,PassiveSet.remove g_cl g_passives)
54   in
55
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);
60     let bag, g_current = 
61       Sup.simplify_goal maxvar (snd actives) bag g_current
62     in
63       debug "Simplified goal";
64       let bag, maxvar, new_goals = 
65         Sup.infer_left bag maxvar g_current actives 
66       in
67         debug "Performed infer_left step";
68         let new_goals = List.fold_left add_passive_clause
69           PassiveSet.empty new_goals
70         in
71           bag, maxvar, actives, passives, g_current::g_actives,
72     (PassiveSet.union new_goals g_passives)
73   in
74
75   let forward_infer_step bag maxvar actives passives g_actives
76                          g_passives current =
77     (* forward step *)
78     
79     (* e = select P           *
80      * e' = demod A e         *
81      * A' = demod [e'] A      *
82      * A'' = A' + e'          *
83      * e'' = fresh e'         *
84      * new = supright e'' A'' *
85      * new'= demod A'' new    *
86      * P' = P + 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 
93     in
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 *)
98       let bag, g_actives = 
99         List.fold_left 
100           (fun (bag,acc) c -> 
101              let bag, c = Sup.simplify_goal maxvar (snd actives) bag c in
102                bag, c::acc) 
103           (bag,[]) g_actives 
104       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 = 
109         List.fold_left 
110           (fun (bag,m,acc) g -> 
111              let bag, m, ng = Sup.infer_left bag m g
112                ([current],ctable) in
113                bag,m,ng@acc) 
114           (bag,maxvar,[]) g_actives 
115       in
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
125   in
126
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))
131           (fst actives)); *)
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 !");
136
137
138     let rec aux_select passives g_passives =
139       let backward,current,passives,g_passives = select passives g_passives in
140         if backward then
141           backward_infer_step bag maxvar actives passives
142                               g_actives g_passives current
143         else
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
151     in
152
153     let bag,maxvar,actives,passives,g_actives,g_passives =      
154       aux_select passives g_passives
155     in
156       assert (PassiveSet.cardinal g_passives < 2);
157       debug
158         (Printf.sprintf "Number of active goals : %d"
159            (List.length g_actives));
160       debug
161         (Printf.sprintf "Number of passive goals : %d"
162            (PassiveSet.cardinal g_passives));
163       debug
164         (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
165       debug
166         (Printf.sprintf "Number of passives : %d"
167            (PassiveSet.cardinal passives));
168       given_clause bag maxvar actives passives g_actives g_passives
169   in
170
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
175     bag, maxvar, c
176   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 = 
181     List.fold_left
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 
186   in
187   let actives = [], IDX.DT.empty in
188   try given_clause bag maxvar actives passives g_actives g_passives
189   with 
190   | Sup.Success (bag, _, (i,_,_,_)) ->
191       let l =
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
199                 else
200                   let accg,acce = 
201                     traverse false (traverse ongoal (accg,acce) i1) i2
202                   in
203                     if ongoal then i::accg,acce else accg,i::acce
204         in
205         let gsteps,esteps = traverse true ([],[]) i in
206           (List.rev esteps)@gsteps
207       in
208         prerr_endline (Printf.sprintf "Found proof in %d iterations, %fs"
209                          !nb_iter
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
220           | NCic.Implicit _ -> 
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                 
225         in
226          aux 0 metasenv proofterm
227       in
228       let metasenv, subst, proofterm, _prooftype = 
229         NCicRefiner.typeof 
230           (rdb#set_coerc_db NCicCoercion.empty_db) 
231           metasenv subst context proofterm None
232       in
233       proofterm, metasenv, subst
234   | Failure _ -> prerr_endline
235       (Printf.sprintf "FAILURE in %d iterations" !nb_iter); assert false
236 ;;