]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/paramod.ml
Corrected proof visiting (topological sort)
[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 = 10.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     
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))
50           (fst actives));*)
51       raise (Failure "No iterations left !");
52     if Unix.gettimeofday () > timeout then
53       raise (Failure "Timeout !");
54
55
56       
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);
64           let bag, g_current = 
65             Sup.simplify_goal maxvar (snd actives) bag g_current
66           in
67             debug "Simplified goal";
68           let bag, maxvar, new_goals = 
69             Sup.infer_left bag maxvar g_current actives 
70           in
71             debug "Performed infer_left step";
72           let new_goals = List.fold_left add_passive_clause
73             PassiveSet.empty new_goals
74           in
75           bag, maxvar, g_current::g_actives,
76           (PassiveSet.union new_goals g_passives)
77     in
78       debug
79         (Printf.sprintf "Number of active goals : %d"
80            (List.length g_actives));
81       debug
82         (Printf.sprintf "Number of passive goals : %d"
83            (PassiveSet.cardinal g_passives));
84   
85       (* forward step *)
86  
87       (* e = select P           *
88        * e' = demod A e         *
89        * A' = demod [e'] A      *
90        * A'' = A' + e'          *
91        * e'' = fresh e'         *
92        * new = supright e'' A'' *
93        * new'= demod A'' new    *
94        * P' = P + 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
106       in
107       let (current, bag, actives),passives = aux_simplify passives
108       in                    
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 
113         in
114   debug "Demodulating goals with actives...";
115           (* keep goals demodulated w.r.t. actives and check if solved *)
116         let bag, g_actives = 
117           List.fold_left 
118             (fun (bag,acc) c -> 
119                let bag, c = Sup.simplify_goal maxvar (snd actives) bag c in
120                  bag, c::acc) 
121             (bag,[]) g_actives 
122         in
123         let ctable = IDX.index_unit_clause IDX.DT.empty current in
124         let bag, maxvar, new_goals = 
125           List.fold_left 
126             (fun (bag,m,acc) g -> 
127                let bag, m, ng = Sup.infer_left bag maxvar g
128                  ([current],ctable) in
129                  bag,m,ng@acc) 
130             (bag,maxvar,[]) g_actives 
131         in
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
139     in
140       debug
141         (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
142       debug
143         (Printf.sprintf "Number of passives : %d"
144            (PassiveSet.cardinal passives));
145       given_clause bag maxvar actives passives g_actives g_passives
146   in
147
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
152     bag, maxvar, c
153   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 = 
158     List.fold_left
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 
163   in
164   let actives = [], IDX.DT.empty in
165   try given_clause bag maxvar actives passives g_actives g_passives
166   with 
167   | Sup.Success (bag, _, (i,_,_,_)) ->
168       let l =
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
176                 else
177                   let accg,acce = 
178                     traverse false (traverse ongoal (accg,acce) i1) i2
179                   in
180                     if ongoal then i::accg,acce else accg,i::acce
181         in
182         let gsteps,esteps = traverse true ([],[]) i in
183           (List.rev esteps)@gsteps
184       in
185         prerr_endline (Printf.sprintf "Found proof in %d iterations" !nb_iter);
186       (* prerr_endline "Proof:"; 
187       List.iter (fun x -> prerr_endline (string_of_int x);
188               prerr_endline (Pp.pp_unit_clause (Terms.M.find x bag))) l;*)
189       let proofterm = B.mk_proof bag i l in
190         prerr_endline "Got proof term";
191       let metasenv, proofterm = 
192         let rec aux k metasenv = function
193           | NCic.Meta _ as t -> metasenv, t
194           | NCic.Implicit _ -> 
195               let metasenv,i,_,_=NCicMetaSubst.mk_meta metasenv context `Term in
196               metasenv, NCic.Meta (i,(k,NCic.Irl (List.length context)))
197           | t -> NCicUntrusted.map_term_fold_a 
198                   (fun _ k -> k+1) k aux metasenv t                 
199         in
200          aux 0 metasenv proofterm
201       in
202       let metasenv, subst, proofterm, _prooftype = 
203         NCicRefiner.typeof 
204           (rdb#set_coerc_db NCicCoercion.empty_db) 
205           metasenv subst context proofterm None
206       in
207       proofterm, metasenv, subst
208   | Failure _ -> prerr_endline
209       (Printf.sprintf "FAILURE in %d iterations" !nb_iter); assert false
210 ;;