]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/paramod.ml
435c95024416b3f232ce280855605de6e0cc1fb1
[helm.git] / helm / software / components / ng_paramodulation / paramod.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 (* $Id: orderings.ml 9869 2009-06-11 22:52:38Z denes $ *)
13
14 let debug s = prerr_endline s ;;
15 let debug _ = ();;
16
17 let monster = 500;;
18     
19 module Paramod (B : Terms.Blob) = struct
20   exception Failure of string * B.t Terms.bag * int * int
21   type bag = B.t Terms.bag * int
22   module Pp = Pp.Pp (B) 
23   module FU = FoUnif.Founif(B) 
24   module IDX = Index.Index(B) 
25   module Sup = Superposition.Superposition(B) 
26   module Utils = FoUtils.Utils(B) 
27   module WeightOrderedPassives =
28       struct
29         type t = B.t Terms.passive_clause
30         let compare = Utils.compare_passive_clauses_weight
31       end
32
33   module AgeOrderedPassives =
34       struct
35         type t = B.t Terms.passive_clause
36         let compare = Utils.compare_passive_clauses_age
37       end
38   
39   module WeightPassiveSet = Set.Make(WeightOrderedPassives)
40   module AgePassiveSet = Set.Make(AgeOrderedPassives)
41
42   let add_passive_clause ?(no_weight=false) (passives_w,passives_a) cl =
43     let cl = if no_weight then (0,cl)
44     else Utils.mk_passive_clause cl in
45     WeightPassiveSet.add cl passives_w, AgePassiveSet.add cl passives_a
46   ;;
47
48   let add_passive_goal ?(no_weight=false) (passives_w,passives_a) g =
49     let g = if no_weight then (0,g)
50     else Utils.mk_passive_goal g in
51     WeightPassiveSet.add g passives_w, AgePassiveSet.add g passives_a
52   ;;
53
54   let remove_passive_clause (passives_w,passives_a) cl =
55     let passives_w = WeightPassiveSet.remove cl passives_w in
56     let passives_a = AgePassiveSet.remove cl passives_a in
57       passives_w,passives_a
58   ;;
59
60   let add_passive_clauses ?(no_weight=false)
61       (passives_w,passives_a) new_clauses =
62     let new_clauses_w,new_clauses_a =
63       List.fold_left (add_passive_clause ~no_weight)
64       (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
65     in
66       (WeightPassiveSet.union new_clauses_w passives_w,
67        AgePassiveSet.union new_clauses_a passives_a)
68   ;;
69
70   let add_passive_goals ?(no_weight=false)
71       (passives_w,passives_a) new_clauses =
72     let new_clauses_w,new_clauses_a =
73       List.fold_left (add_passive_goal ~no_weight)
74       (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
75     in
76       (WeightPassiveSet.union new_clauses_w passives_w,
77        AgePassiveSet.union new_clauses_a passives_a)
78   ;;
79
80   let is_passive_set_empty (passives_w,passives_a) =
81     if (WeightPassiveSet.is_empty passives_w) then begin
82       assert (AgePassiveSet.is_empty passives_a); true
83     end else begin
84       assert (not (AgePassiveSet.is_empty passives_a)); false
85     end
86   ;;
87
88   let passive_set_cardinal (passives_w,_) = WeightPassiveSet.cardinal passives_w
89   
90   let passive_empty_set =
91     (WeightPassiveSet.empty,AgePassiveSet.empty)
92   ;;
93
94   let pick_min_passive ~use_age (passives_w,passives_a) =
95     if use_age then AgePassiveSet.min_elt passives_a
96     else WeightPassiveSet.min_elt passives_w
97   ;;
98
99   let mk_clause bag maxvar (t,ty) =
100     let (proof,ty) = B.saturate t ty in
101     let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
102     let bag, c = Utils.add_to_bag bag c in
103     (bag, maxvar), c
104   ;;
105   
106   let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
107   let mk_goal (bag,maxvar) = mk_clause bag maxvar;;
108
109   (* TODO : global age over facts and goals (without comparing weights) *)
110   let select ~use_age passives g_passives =
111     if is_passive_set_empty passives then begin
112       assert (not (is_passive_set_empty g_passives));
113       let g_cl = pick_min_passive ~use_age:use_age g_passives in
114         (true,g_cl,passives,remove_passive_clause g_passives g_cl)
115     end
116     else let cl = pick_min_passive ~use_age:use_age passives in
117       if is_passive_set_empty g_passives then
118         (false,cl,remove_passive_clause passives cl,g_passives)
119       else
120         let g_cl = pick_min_passive ~use_age:use_age g_passives in
121           if (fst cl <= fst g_cl) then
122             (false,cl,remove_passive_clause passives cl,g_passives)
123           else
124             (true,g_cl,passives,remove_passive_clause g_passives g_cl)
125   ;;
126
127   let backward_infer_step bag maxvar actives passives
128                           g_actives g_passives g_current =
129     (* superposition left, simplifications on goals *)
130       debug "infer_left step...";
131       let bag, maxvar, new_goals = 
132         Sup.infer_left bag maxvar g_current actives 
133       in
134         debug "Performed infer_left step";
135         bag, maxvar, actives, passives, g_current::g_actives,
136     (add_passive_goals g_passives new_goals)
137   ;;
138
139   let forward_infer_step bag maxvar actives passives g_actives
140                          g_passives current =
141     (* forward step *)
142     
143     (* e = select P           *
144      * e' = demod A e         *
145      * A' = demod [e'] A      *
146      * A'' = A' + e'          *
147      * e'' = fresh e'         *
148      * new = supright e'' A'' *
149      * new'= demod A'' new    *
150      * P' = P + new'          *)
151     debug "Forward infer step...";
152     let bag, maxvar, actives, new_clauses = 
153       Sup.infer_right bag maxvar current actives 
154     in
155       debug "Demodulating goals with actives...";
156       (* keep goals demodulated w.r.t. actives and check if solved *)
157       let bag, g_actives = 
158         List.fold_left 
159           (fun (bag,acc) c -> 
160              match 
161                Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
162              with
163                | None -> bag, acc
164                | Some (bag,c) -> bag,c::acc)
165           (bag,[]) g_actives 
166       in
167       let ctable = IDX.index_unit_clause IDX.DT.empty current in
168       let bag, maxvar, new_goals = 
169         List.fold_left 
170           (fun (bag,m,acc) g -> 
171              let bag, m, ng = Sup.infer_left bag m g
172                ([current],ctable) in
173                bag,m,ng@acc) 
174           (bag,maxvar,[]) g_actives 
175       in
176     bag, maxvar, actives,
177     add_passive_clauses passives new_clauses, g_actives,
178     add_passive_goals g_passives new_goals
179   ;;
180  
181   let rec given_clause ~noinfer 
182     bag maxvar iterno max_steps timeout 
183     actives passives g_actives g_passives 
184   =
185     let iterno = iterno + 1 in
186     if iterno = max_steps then       
187       raise (Failure ("No iterations left !",bag,maxvar,iterno));
188     (* timeout check: gettimeofday called only if timeout set *)
189     if timeout <> None &&
190       (match timeout with
191       | None -> assert false
192       | Some timeout -> Unix.gettimeofday () > timeout) then
193         if noinfer then
194           begin
195             debug 
196               ("Last chance: all is indexed " ^ string_of_float
197                 (Unix.gettimeofday()));
198             let maxgoals = 100 in
199             ignore(List.fold_left 
200               (fun (acc,i) x -> 
201                  if i < maxgoals then
202                  ignore(Sup.simplify_goal ~no_demod:true 
203                           maxvar (snd actives) bag acc x)
204                  else
205                    ();
206                  x::acc,i+1)
207               ([],0) g_actives);
208             raise (Failure (("Last chance: failed over " ^ 
209               string_of_int maxgoals^ " goal " ^ 
210               string_of_float (Unix.gettimeofday())),bag,maxvar,0));
211           end
212         else if false then (* activates last chance strategy *)
213           begin
214            debug("Last chance: "^string_of_float (Unix.gettimeofday()));
215            given_clause ~noinfer:true bag maxvar iterno max_steps 
216              (Some (Unix.gettimeofday () +. 20.))
217              actives passives g_actives g_passives;
218            raise (Failure ("Timeout !",bag,maxvar,iterno))
219           end
220         else raise (Failure ("Timeout !",bag,maxvar,iterno));
221
222     (* let use_age = iterno mod 10 = 0 in *)
223
224     let rec aux_select bag passives g_passives =
225       let backward,(weight,current),passives,g_passives =
226         select ~use_age:false passives g_passives
227       in
228         if backward then
229          match 
230            if noinfer then 
231              if weight > monster then None else Some (bag,current)
232            else 
233              Sup.simplify_goal 
234                ~no_demod:false maxvar (snd actives) bag g_actives current 
235          with
236             | None -> aux_select bag passives g_passives
237             | Some (bag,g_current) ->
238                if noinfer then 
239                  let g_actives = g_current :: g_actives in 
240                  bag,maxvar,actives,passives,g_actives,g_passives
241                else
242                  backward_infer_step bag maxvar actives passives
243                    g_actives g_passives g_current
244         else
245           let _ = debug ("Selected fact : " ^ Pp.pp_unit_clause current) in
246           (*let is_orphan = Sup.orphan_murder bag (fst actives) current in*)
247           match 
248             if noinfer then 
249               if weight > monster then bag,None 
250               else  bag, Some (current,actives)
251             else if Sup.orphan_murder bag (fst actives) current then
252               let (id,_,_,_) = current in
253               let bag = Terms.M.add id (current,true) bag in
254                 bag, None
255             else Sup.keep_simplified current actives bag maxvar
256           with
257         (*match Sup.one_pass_simplification current actives bag maxvar with*)
258               | bag,None -> aux_select bag passives g_passives
259               | bag,Some (current,actives) ->
260 (*                    if is_orphan then prerr_endline
261                       ("WRONG discarded: " ^ (Pp.pp_unit_clause current));
262                   List.iter (fun x ->
263                                prerr_endline (Pp.pp_unit_clause x))
264                     (fst actives);*)
265
266 (*                  List.iter (fun (id,_,_,_) -> let (cl,d) =
267                              Terms.M.find id bag in 
268                              if d then prerr_endline
269                                ("WRONG discarded: " ^ (Pp.pp_unit_clause cl)))
270                     (current::fst actives);*)
271                   if noinfer then
272                     let actives = 
273                       current::fst actives,
274                       IDX.index_unit_clause (snd actives) current
275                     in
276                     bag,maxvar,actives,passives,g_actives,g_passives
277                   else
278                     forward_infer_step bag maxvar actives passives
279                       g_actives g_passives current
280     in
281     
282
283       (*prerr_endline "Active table :"; 
284        (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
285           (fst actives)); *)
286
287     let bag,maxvar,actives,passives,g_actives,g_passives =      
288       aux_select bag passives g_passives
289     in
290       debug
291         (Printf.sprintf "Number of active goals : %d"
292            (List.length g_actives));
293       debug
294         (Printf.sprintf "Number of passive goals : %d"
295            (passive_set_cardinal g_passives));
296       debug
297         (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
298       debug
299         (Printf.sprintf "Number of passives : %d"
300            (passive_set_cardinal passives));
301       given_clause ~noinfer
302         bag maxvar iterno max_steps timeout 
303         actives passives g_actives g_passives
304   ;;
305
306   let paramod ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
307     let initial_timestamp = Unix.gettimeofday () in
308     let passives =
309       add_passive_clauses ~no_weight:true passive_empty_set passives
310     in
311     let g_passives =
312       add_passive_goals ~no_weight:true passive_empty_set g_passives
313     in
314     let g_actives = [] in
315     let actives = [], IDX.DT.empty in
316     try 
317      given_clause ~noinfer:false
318       bag maxvar 0 max_steps timeout actives passives g_actives g_passives
319     with 
320     | Sup.Success (bag, _, (i,_,_,_)) ->
321         let l =
322           let rec traverse ongoal (accg,acce) i =
323             match Terms.M.find i bag with
324               | (id,_,_,Terms.Exact _),_ ->
325                   if ongoal then [i],acce else
326                     if (List.mem i acce) then accg,acce else accg,acce@[i]
327               | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),_ ->
328                   if (not ongoal) && (List.mem i acce) then accg,acce
329                   else
330                     let accg,acce = 
331                       traverse false (traverse ongoal (accg,acce) i1) i2
332                     in
333                       if ongoal then i::accg,acce else accg,i::acce
334           in
335           let gsteps,esteps = traverse true ([],[]) i in
336             (List.rev esteps)@gsteps
337         in
338 (*        List.iter (fun id -> let (cl,d) =
339                        Terms.M.find id bag in 
340                     if d then prerr_endline (Pp.pp_unit_clause cl)) l;*)
341         prerr_endline 
342           (Printf.sprintf "Found proof, %fs" 
343             (Unix.gettimeofday() -. initial_timestamp));
344         (* 
345         prerr_endline "Proof:"; 
346         List.iter (fun x ->
347           prerr_endline (Pp.pp_unit_clause (fst(Terms.M.find x bag)))) l;
348         *)
349         [ bag, i, l ]
350     | Failure (msg,_bag,_maxvar,iterno) -> 
351         prerr_endline msg;
352         prerr_endline (Printf.sprintf "FAILURE in %d iterations" iterno); 
353         []
354   ;;
355
356 end