]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/paramod.ml
Removed old debugging assertion
[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       (* keep goals demodulated w.r.t. actives and check if solved *)
96       let bag, g_actives = 
97         List.fold_left 
98           (fun (bag,acc) c -> 
99              let bag, c = Sup.simplify_goal maxvar (snd actives) bag c in
100                bag, c::acc) 
101           (bag,[]) g_actives 
102       in
103       let ctable = IDX.index_unit_clause IDX.DT.empty current in
104       let bag, maxvar, new_goals = 
105         List.fold_left 
106           (fun (bag,m,acc) g -> 
107              let bag, m, ng = Sup.infer_left bag m g
108                ([current],ctable) in
109                bag,m,ng@acc) 
110           (bag,maxvar,[]) g_actives 
111       in
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
119   in
120
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))
125           (fst actives)); *)
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 !");
130
131
132     let rec aux_select passives g_passives =
133       let backward,current,passives,g_passives = select passives g_passives in
134         if backward then
135           backward_infer_step bag maxvar actives passives
136                               g_actives g_passives current
137         else
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
145     in
146
147     let bag,maxvar,actives,passives,g_actives,g_passives =      
148       aux_select passives g_passives
149     in
150       debug
151         (Printf.sprintf "Number of active goals : %d"
152            (List.length g_actives));
153       debug
154         (Printf.sprintf "Number of passive goals : %d"
155            (PassiveSet.cardinal g_passives));
156       debug
157         (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
158       debug
159         (Printf.sprintf "Number of passives : %d"
160            (PassiveSet.cardinal passives));
161       given_clause bag maxvar actives passives g_actives g_passives
162   in
163
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
168     bag, maxvar, c
169   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 = 
174     List.fold_left
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 
179   in
180   let actives = [], IDX.DT.empty in
181   try given_clause bag maxvar actives passives g_actives g_passives
182   with 
183   | Sup.Success (bag, _, (i,_,_,_)) ->
184       let l =
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
192                 else
193                   let accg,acce = 
194                     traverse false (traverse ongoal (accg,acce) i1) i2
195                   in
196                     if ongoal then i::accg,acce else accg,i::acce
197         in
198         let gsteps,esteps = traverse true ([],[]) i in
199           (List.rev esteps)@gsteps
200       in
201         prerr_endline (Printf.sprintf "Found proof in %d iterations, %fs"
202                          !nb_iter
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
213           | NCic.Implicit _ -> 
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
218         in
219          aux 0 metasenv proofterm
220       in
221       let metasenv, subst, proofterm, _prooftype = 
222         NCicRefiner.typeof 
223           (rdb#set_coerc_db NCicCoercion.empty_db) 
224           metasenv subst context proofterm None
225       in
226       proofterm, metasenv, subst
227   | Failure s -> 
228       prerr_endline s;
229       prerr_endline
230       (Printf.sprintf "FAILURE in %d iterations" !nb_iter); assert false
231 ;;