]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/paramod.ml
proof patching!
[helm.git] / helm / software / components / ng_paramodulation / paramod.ml
1 let debug s =
2   () (* prerr_endline s *)
3 ;;
4
5 let nparamod rdb metasenv subst context t table =
6   let nb_iter = ref 200 in
7   let amount_of_time = 20.0 in
8   let module C = struct
9     let metasenv = metasenv
10     let subst = subst
11     let context = context
12   end
13   in
14   let module B = NCicBlob.NCicBlob(C) in
15   let module Pp = Pp.Pp (B) in
16   let module FU = FoUnif.Founif(B) in
17   let module IDX = Index.Index(B) in
18   let module Sup = Superposition.Superposition(B) in
19   let module Utils = FoUtils.Utils(B) in
20     
21   let module OrderedPassives =
22       struct
23         type t = B.t Terms.passive_clause
24             
25         let compare = Utils.compare_passive_clauses
26       end
27   in
28   let module PassiveSet = Set.Make(OrderedPassives)
29   in
30   let add_passive_clause passives cl =
31     PassiveSet.add (Utils.mk_passive_clause cl) passives
32   in
33   let timeout = Unix.gettimeofday () +. amount_of_time in 
34     (* TODO : fairness condition *)
35   let select passives =
36     if PassiveSet.is_empty passives then None
37     else let cl = PassiveSet.min_elt passives in
38       Some (snd cl,PassiveSet.remove cl passives)
39   in
40   let rec given_clause bag maxvar actives      
41       passives g_actives g_passives =
42     
43     decr nb_iter; if !nb_iter = 0 then 
44       (*(prerr_endline "Bag :"; prerr_endline (Pp.pp_bag bag);
45       prerr_endline "Active table :"; 
46        (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
47           (fst actives));*)
48       raise (Failure "No iterations left !");
49     if Unix.gettimeofday () > timeout then
50       raise (Failure "Timeout !");
51
52
53       
54     (* superposition left, simplifications on goals *)
55       debug "infer_left step...";
56       let bag, maxvar, g_actives, g_passives =
57         match select g_passives with
58       | None -> bag, maxvar, g_actives, g_passives
59       | Some (g_current, g_passives) -> 
60           debug ("Selected goal : " ^ Pp.pp_unit_clause g_current);
61           let bag, g_current = 
62             Sup.simplify_goal maxvar (snd actives) bag g_current
63           in
64           let bag, maxvar, new_goals = 
65             Sup.infer_left bag maxvar g_current actives 
66           in
67           let new_goals = List.fold_left add_passive_clause
68             PassiveSet.empty new_goals
69           in
70           bag, maxvar, g_current::g_actives,
71           (PassiveSet.union new_goals g_passives)
72     in
73       debug
74         (Printf.sprintf "Number of active goals : %d"
75            (List.length g_actives));
76       debug
77         (Printf.sprintf "Number of passive goals : %d"
78            (PassiveSet.cardinal g_passives));
79   
80       (* forward step *)
81  
82       (* e = select P           *
83        * e' = demod A e         *
84        * A' = demod [e'] A      *
85        * A'' = A' + e'          *
86        * e'' = fresh e'         *
87        * new = supright e'' A'' *
88        * new'= demod A'' new    *
89        * P' = P + new'          *)
90       debug "Forward infer step...";
91     let bag, maxvar, actives, passives, g_passives =
92       let rec aux_simplify passives =
93         match select passives with
94           | None -> assert false
95           | Some (current, passives) -> 
96               debug ("Selected fact : " ^ Pp.pp_unit_clause current);
97 (*               match Sup.keep_simplified current actives bag maxvar with *)
98                match Sup.one_pass_simplification current actives bag maxvar with 
99                 | None -> aux_simplify passives
100                 | Some x -> x,passives
101       in
102       let (current, bag, actives),passives = aux_simplify passives
103       in                    
104         debug ("Fact after simplification :"
105                ^ Pp.pp_unit_clause current);
106         let bag, maxvar, actives, new_clauses = 
107           Sup.infer_right bag maxvar current actives 
108         in
109   debug "Demodulating goals with actives...";
110           (* keep goals demodulated w.r.t. actives and check if solved *)
111         let bag, g_actives = 
112           List.fold_left 
113             (fun (bag,acc) c -> 
114                let bag, c = Sup.simplify_goal maxvar (snd actives) bag c in
115                  bag, c::acc) 
116             (bag,[]) g_actives 
117         in
118         let ctable = IDX.index_unit_clause IDX.DT.empty current in
119         let bag, maxvar, new_goals = 
120           List.fold_left 
121             (fun (bag,m,acc) g -> 
122                let bag, m, ng = Sup.infer_left bag maxvar g
123                  ([current],ctable) in
124                  bag,m,ng@acc) 
125             (bag,maxvar,[]) g_actives 
126         in
127         let new_clauses = List.fold_left add_passive_clause
128           PassiveSet.empty new_clauses in
129         let new_goals = List.fold_left add_passive_clause
130           PassiveSet.empty new_goals in
131           bag, maxvar, actives,
132       PassiveSet.union new_clauses passives,
133       PassiveSet.union new_goals g_passives
134     in
135       debug
136         (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
137       debug
138         (Printf.sprintf "Number of passives : %d"
139            (PassiveSet.cardinal passives));
140       given_clause bag maxvar actives passives g_actives g_passives
141   in
142
143   let mk_clause bag maxvar (t,ty) =
144     let (proof,ty) = B.saturate t ty in
145     let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
146     let bag, c = Utils.add_to_bag bag c in
147     bag, maxvar, c
148   in
149   let bag, maxvar, goal = mk_clause Terms.M.empty 0 t in
150   let g_actives = [] in
151   let g_passives = PassiveSet.singleton (Utils.mk_passive_clause goal) in
152   let passives, bag, maxvar = 
153     List.fold_left
154       (fun (cl, bag, maxvar) t -> 
155          let bag, maxvar, c = mk_clause bag maxvar t in
156          (add_passive_clause cl c), bag, maxvar)
157       (PassiveSet.empty, bag, maxvar) table 
158   in
159   let actives = [], IDX.DT.empty in
160   try given_clause bag maxvar actives passives g_actives g_passives
161   with 
162   | Sup.Success (bag, _, (i,_,_,_)) ->
163       let l =
164         let module S  = 
165           HTopoSort.Make(struct type t=int let compare=Pervasives.compare end)
166         in
167         let module C : Set.S with type elt = int = 
168           Set.Make(struct type t=int let compare=Pervasives.compare end)
169         in
170         let all id = 
171           let rec traverse ongoal (accg,acce) i =
172             match Terms.M.find i bag with
173             | (_,_,_,Terms.Exact _) -> accg, acce
174             | (_,_,_,Terms.Step (_,i1,i2,_,_,_)) -> 
175                let accg, acce = 
176                  if ongoal then C.add i1 accg, acce
177                  else accg, C.add i1 acce
178                in
179                let acce = C.add i2 acce in
180                  traverse false (traverse ongoal (accg,acce) i1) i2    
181           in
182             traverse true (C.empty,C.empty) id
183         in
184         let esteps = 
185           S.topological_sort (C.elements (snd (all i))) 
186            (fun i -> C.elements (C.union (snd (all i)) (fst (all i)) ))
187         in
188         let gsteps = 
189           S.topological_sort (C.elements (fst (all i))) 
190            (fun i -> C.elements (fst (all i)))
191         in
192         let gsteps = List.rev gsteps in
193         esteps@(i::gsteps)
194       in
195       prerr_endline "YES!";
196       prerr_endline ("Meeting point : " ^ (string_of_int i));
197       prerr_endline "Proof:"; 
198       List.iter (fun x -> prerr_endline (string_of_int x);
199               prerr_endline (Pp.pp_unit_clause (Terms.M.find x bag))) l;
200       let proofterm = B.mk_proof bag i l in
201       let metasenv, proofterm = 
202         let rec aux k metasenv = function
203           | NCic.Meta _ as t -> metasenv, t
204           | NCic.Implicit _ -> 
205               let metasenv,i,_,_=NCicMetaSubst.mk_meta metasenv context `Term in
206               metasenv, NCic.Meta (i,(k,NCic.Irl (List.length context)))
207           | t -> NCicUntrusted.map_term_fold_a 
208                   (fun _ k -> k+1) k aux metasenv t                 
209         in
210          aux 0 metasenv proofterm
211       in
212       prerr_endline
213         ("prova generata: " ^ NCicPp.ppterm 
214            ~metasenv ~subst ~context proofterm);
215       let metasenv, subst, proofterm, _prooftype = 
216         NCicRefiner.typeof 
217           (rdb#set_coerc_db NCicCoercion.empty_db) 
218           metasenv subst context proofterm None
219       in
220       prerr_endline "prova raffinata";
221       proofterm, metasenv, subst
222   | Failure _ -> prerr_endline "FAILURE"; assert false
223 ;;