]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/paramod.ml
Fixed nasty bug in maxvar updating
[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     (* TODO : fairness condition *)
38   let select passives =
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)
42   in
43   let rec given_clause bag maxvar actives      
44       passives g_actives g_passives =
45     (* prerr_endline "Bag :"; prerr_endline (Pp.pp_bag bag);
46       prerr_endline "Active table :"; 
47        (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
48           (fst actives)); *)
49     incr nb_iter; if !nb_iter = max_nb_iter then       
50       raise (Failure "No iterations left !");
51     if Unix.gettimeofday () > timeout then
52       raise (Failure "Timeout !");
53
54
55       
56     (* superposition left, simplifications on goals *)
57       debug "infer_left step...";
58       let bag, maxvar, g_actives, g_passives =
59         match select g_passives with
60       | None -> bag, maxvar, g_actives, g_passives
61       | Some (g_current, g_passives) -> 
62           debug ("Selected goal : " ^ Pp.pp_unit_clause g_current);
63           let bag, g_current = 
64             Sup.simplify_goal maxvar (snd actives) bag g_current
65           in
66             debug "Simplified goal";
67           let bag, maxvar, new_goals = 
68             Sup.infer_left bag maxvar g_current actives 
69           in
70             debug "Performed infer_left step";
71           let new_goals = List.fold_left add_passive_clause
72             PassiveSet.empty new_goals
73           in
74           bag, maxvar, g_current::g_actives,
75           (PassiveSet.union new_goals g_passives)
76     in
77       debug
78         (Printf.sprintf "Number of active goals : %d"
79            (List.length g_actives));
80       debug
81         (Printf.sprintf "Number of passive goals : %d"
82            (PassiveSet.cardinal g_passives));
83   
84       (* forward step *)
85  
86       (* e = select P           *
87        * e' = demod A e         *
88        * A' = demod [e'] A      *
89        * A'' = A' + e'          *
90        * e'' = fresh e'         *
91        * new = supright e'' A'' *
92        * new'= demod A'' new    *
93        * P' = P + new'          *)
94       debug "Forward infer step...";
95     let bag, maxvar, actives, passives, g_passives =
96       let rec aux_simplify passives =
97         match select passives with
98           | None -> assert false
99           | Some (current, passives) -> 
100               debug ("Selected fact : " ^ Pp.pp_unit_clause current);
101                 match Sup.keep_simplified current actives bag maxvar with
102 (*  match Sup.one_pass_simplification current actives bag maxvar with*)
103                 | None -> aux_simplify passives
104                 | Some x -> x,passives
105       in
106       let (current, bag, actives),passives = aux_simplify passives
107       in                    
108         debug ("Fact after simplification :"
109                ^ Pp.pp_unit_clause current);
110         let bag, maxvar, actives, new_clauses = 
111           Sup.infer_right bag maxvar current actives 
112         in
113   debug "Demodulating goals with actives...";
114           (* keep goals demodulated w.r.t. actives and check if solved *)
115         let bag, g_actives = 
116           List.fold_left 
117             (fun (bag,acc) c -> 
118                let bag, c = Sup.simplify_goal maxvar (snd actives) bag c in
119                  bag, c::acc) 
120             (bag,[]) g_actives 
121         in
122         let ctable = IDX.index_unit_clause IDX.DT.empty current in
123         let bag, maxvar, new_goals = 
124           List.fold_left 
125             (fun (bag,m,acc) g -> 
126                let bag, m, ng = Sup.infer_left bag m g
127                  ([current],ctable) in
128                  bag,m,ng@acc) 
129             (bag,maxvar,[]) g_actives 
130         in
131         let new_clauses = List.fold_left add_passive_clause
132           PassiveSet.empty new_clauses in
133         let new_goals = List.fold_left add_passive_clause
134           PassiveSet.empty new_goals in
135           bag, maxvar, actives,
136       PassiveSet.union new_clauses passives,
137       PassiveSet.union new_goals g_passives
138     in
139       debug
140         (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
141       debug
142         (Printf.sprintf "Number of passives : %d"
143            (PassiveSet.cardinal passives));
144       given_clause bag maxvar actives passives g_actives g_passives
145   in
146
147   let mk_clause bag maxvar (t,ty) =
148     let (proof,ty) = B.saturate t ty in
149     let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
150     let bag, c = Utils.add_to_bag bag c in
151     bag, maxvar, c
152   in
153   let bag, maxvar, goal = mk_clause Terms.M.empty 0 t in
154   let g_actives = [] in
155   let g_passives = PassiveSet.singleton (Utils.mk_passive_clause goal) in
156   let passives, bag, maxvar = 
157     List.fold_left
158       (fun (cl, bag, maxvar) t -> 
159          let bag, maxvar, c = mk_clause bag maxvar t in
160          (add_passive_clause cl c), bag, maxvar)
161       (PassiveSet.empty, bag, maxvar) table 
162   in
163   let actives = [], IDX.DT.empty in
164   try given_clause bag maxvar actives passives g_actives g_passives
165   with 
166   | Sup.Success (bag, _, (i,_,_,_)) ->
167       let l =
168         let rec traverse ongoal (accg,acce) i =
169           match Terms.M.find i bag with
170             | (id,_,_,Terms.Exact _) ->
171                 if ongoal then [i],acce else
172                   if (List.mem i acce) then accg,acce else accg,acce@[i]
173             | (_,_,_,Terms.Step (_,i1,i2,_,_,_)) ->
174                 if (not ongoal) && (List.mem i acce) then accg,acce
175                 else
176                   let accg,acce = 
177                     traverse false (traverse ongoal (accg,acce) i1) i2
178                   in
179                     if ongoal then i::accg,acce else accg,i::acce
180         in
181         let gsteps,esteps = traverse true ([],[]) i in
182           (List.rev esteps)@gsteps
183       in
184         prerr_endline (Printf.sprintf "Found proof in %d iterations, %fs"
185                          !nb_iter
186                          (Unix.gettimeofday() -. timeout +. amount_of_time));
187       (* prerr_endline "Proof:"; 
188       List.iter (fun x -> prerr_endline (string_of_int x);
189               prerr_endline (Pp.pp_unit_clause (Terms.M.find x bag))) l;*)
190       let proofterm = B.mk_proof bag i l in
191         prerr_endline (Printf.sprintf "Got proof term, %fs"
192                          (Unix.gettimeofday() -. timeout +. amount_of_time));
193       let metasenv, proofterm = 
194         let rec aux k metasenv = function
195           | NCic.Meta _ as t -> metasenv, t
196           | NCic.Implicit _ -> 
197               let metasenv,i,_,_=NCicMetaSubst.mk_meta metasenv context `Term in
198               metasenv, NCic.Meta (i,(k,NCic.Irl (List.length context)))
199           | t -> NCicUntrusted.map_term_fold_a 
200                   (fun _ k -> k+1) k aux metasenv t                 
201         in
202          aux 0 metasenv proofterm
203       in
204       let metasenv, subst, proofterm, _prooftype = 
205         NCicRefiner.typeof 
206           (rdb#set_coerc_db NCicCoercion.empty_db) 
207           metasenv subst context proofterm None
208       in
209       proofterm, metasenv, subst
210   | Failure _ -> prerr_endline
211       (Printf.sprintf "FAILURE in %d iterations" !nb_iter); assert false
212 ;;