]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/paramod.ml
added (but not active) last chance idea
[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 remove_passive_clause (passives_w,passives_a) cl =
49     let passives_w = WeightPassiveSet.remove cl passives_w in
50     let passives_a = AgePassiveSet.remove cl passives_a in
51       passives_w,passives_a
52   ;;
53
54   let add_passive_clauses ?(no_weight=false)
55       (passives_w,passives_a) new_clauses =
56     let new_clauses_w,new_clauses_a =
57       List.fold_left (add_passive_clause ~no_weight)
58       (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
59     in
60       (WeightPassiveSet.union new_clauses_w passives_w,
61        AgePassiveSet.union new_clauses_a passives_a)
62   ;;
63
64   let is_passive_set_empty (passives_w,passives_a) =
65     if (WeightPassiveSet.is_empty passives_w) then begin
66       assert (AgePassiveSet.is_empty passives_a); true
67     end else begin
68       assert (not (AgePassiveSet.is_empty passives_a)); false
69     end
70   ;;
71
72   let passive_set_cardinal (passives_w,_) = WeightPassiveSet.cardinal passives_w
73   
74   let passive_empty_set =
75     (WeightPassiveSet.empty,AgePassiveSet.empty)
76   ;;
77
78   let pick_min_passive ~use_age (passives_w,passives_a) =
79     if use_age then AgePassiveSet.min_elt passives_a
80     else WeightPassiveSet.min_elt passives_w
81   ;;
82
83   let mk_clause bag maxvar (t,ty) =
84     let (proof,ty) = B.saturate t ty in
85     let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
86     let bag, c = Utils.add_to_bag bag c in
87     (bag, maxvar), c
88   ;;
89   
90   let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
91   let mk_goal (bag,maxvar) = mk_clause bag maxvar;;
92
93   (* TODO : global age over facts and goals (without comparing weights) *)
94   let select ~use_age passives g_passives =
95     if is_passive_set_empty passives then begin
96       assert (not (is_passive_set_empty g_passives));
97       let g_cl = pick_min_passive ~use_age:use_age g_passives in
98         (true,g_cl,passives,remove_passive_clause g_passives g_cl)
99     end
100     else let cl = pick_min_passive ~use_age:use_age passives in
101       if is_passive_set_empty g_passives then
102         (false,cl,remove_passive_clause passives cl,g_passives)
103       else
104         let g_cl = pick_min_passive ~use_age:use_age g_passives in
105           if (fst cl <= fst g_cl) then
106             (false,cl,remove_passive_clause passives cl,g_passives)
107           else
108             (true,g_cl,passives,remove_passive_clause g_passives g_cl)
109   ;;
110
111   let backward_infer_step bag maxvar actives passives
112                           g_actives g_passives g_current =
113     (* superposition left, simplifications on goals *)
114       debug "infer_left step...";
115       let bag, maxvar, new_goals = 
116         Sup.infer_left bag maxvar g_current actives 
117       in
118         debug "Performed infer_left step";
119         bag, maxvar, actives, passives, g_current::g_actives,
120     (add_passive_clauses g_passives new_goals)
121   ;;
122
123   let forward_infer_step bag maxvar actives passives g_actives
124                          g_passives current =
125     (* forward step *)
126     
127     (* e = select P           *
128      * e' = demod A e         *
129      * A' = demod [e'] A      *
130      * A'' = A' + e'          *
131      * e'' = fresh e'         *
132      * new = supright e'' A'' *
133      * new'= demod A'' new    *
134      * P' = P + new'          *)
135     debug "Forward infer step...";
136     let bag, maxvar, actives, new_clauses = 
137       Sup.infer_right bag maxvar current actives 
138     in
139       debug "Demodulating goals with actives...";
140       (* keep goals demodulated w.r.t. actives and check if solved *)
141       let bag, g_actives = 
142         List.fold_left 
143           (fun (bag,acc) c -> 
144              match 
145                Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
146              with
147                | None -> bag, acc
148                | Some (bag,c) -> bag,c::acc)
149           (bag,[]) g_actives 
150       in
151       let ctable = IDX.index_unit_clause IDX.DT.empty current in
152       let bag, maxvar, new_goals = 
153         List.fold_left 
154           (fun (bag,m,acc) g -> 
155              let bag, m, ng = Sup.infer_left bag m g
156                ([current],ctable) in
157                bag,m,ng@acc) 
158           (bag,maxvar,[]) g_actives 
159       in
160     bag, maxvar, actives,
161     add_passive_clauses passives new_clauses, g_actives,
162     add_passive_clauses g_passives new_goals
163   ;;
164  
165   let rec given_clause ~noinfer 
166     bag maxvar iterno max_steps timeout 
167     actives passives g_actives g_passives 
168   =
169     let iterno = iterno + 1 in
170     if iterno = max_steps then       
171       raise (Failure ("No iterations left !",bag,maxvar,iterno));
172     (* timeout check: gettimeofday called only if timeout set *)
173     if timeout <> None &&
174       (match timeout with
175       | None -> assert false
176       | Some timeout -> Unix.gettimeofday () > timeout) then
177         if noinfer then
178           begin
179             debug 
180               ("Last chance: all is indexed " ^ string_of_float
181                 (Unix.gettimeofday()));
182             let maxgoals = 100 in
183             ignore(List.fold_left 
184               (fun (acc,i) x -> 
185                  if i < maxgoals then
186                  ignore(Sup.simplify_goal ~no_demod:true 
187                           maxvar (snd actives) bag acc x)
188                  else
189                    ();
190                  x::acc,i+1)
191               ([],0) g_actives);
192             raise (Failure (("Last chance: failed over " ^ 
193               string_of_int maxgoals^ " goal " ^ 
194               string_of_float (Unix.gettimeofday())),bag,maxvar,0));
195           end
196         else if false then (* activates last chance strategy *)
197           begin
198            debug("Last chance: "^string_of_float (Unix.gettimeofday()));
199            given_clause ~noinfer:true bag maxvar iterno max_steps 
200              (Some (Unix.gettimeofday () +. 20.))
201              actives passives g_actives g_passives;
202            raise (Failure ("Timeout !",bag,maxvar,iterno))
203           end
204         else raise (Failure ("Timeout !",bag,maxvar,iterno));
205
206     (* let use_age = iterno mod 10 = 0 in *)
207
208     let rec aux_select bag passives g_passives =
209       let backward,(weight,current),passives,g_passives =
210         select ~use_age:false passives g_passives
211       in
212         if backward then
213          match 
214            if noinfer then 
215              if weight > monster then None else Some (bag,current)
216            else 
217              Sup.simplify_goal 
218                ~no_demod:false maxvar (snd actives) bag g_actives current 
219          with
220             | None -> aux_select bag passives g_passives
221             | Some (bag,g_current) ->
222                if noinfer then 
223                  let g_actives = g_current :: g_actives in 
224                  bag,maxvar,actives,passives,g_actives,g_passives
225                else
226                  backward_infer_step bag maxvar actives passives
227                    g_actives g_passives g_current
228         else
229           let _ = debug ("Selected fact : " ^ Pp.pp_unit_clause current) in
230           (*let is_orphan = Sup.orphan_murder bag (fst actives) current in*)
231           match 
232             if noinfer then 
233               if weight > monster then bag,None 
234               else  bag, Some (current,actives)
235             else if Sup.orphan_murder bag (fst actives) current then
236               let (id,_,_,_) = current in
237               let bag = Terms.M.add id (current,true) bag in
238                 bag, None
239             else Sup.keep_simplified current actives bag maxvar
240           with
241         (*match Sup.one_pass_simplification current actives bag maxvar with*)
242               | bag,None -> aux_select bag passives g_passives
243               | bag,Some (current,actives) ->
244 (*                    if is_orphan then prerr_endline
245                       ("WRONG discarded: " ^ (Pp.pp_unit_clause current));
246                   List.iter (fun x ->
247                                prerr_endline (Pp.pp_unit_clause x))
248                     (fst actives);*)
249
250 (*                  List.iter (fun (id,_,_,_) -> let (cl,d) =
251                              Terms.M.find id bag in 
252                              if d then prerr_endline
253                                ("WRONG discarded: " ^ (Pp.pp_unit_clause cl)))
254                     (current::fst actives);*)
255                   if noinfer then
256                     let actives = 
257                       current::fst actives,
258                       IDX.index_unit_clause (snd actives) current
259                     in
260                     bag,maxvar,actives,passives,g_actives,g_passives
261                   else
262                     forward_infer_step bag maxvar actives passives
263                       g_actives g_passives current
264     in
265     
266
267       (*prerr_endline "Active table :"; 
268        (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
269           (fst actives)); *)
270
271     let bag,maxvar,actives,passives,g_actives,g_passives =      
272       aux_select bag passives g_passives
273     in
274       debug
275         (Printf.sprintf "Number of active goals : %d"
276            (List.length g_actives));
277       debug
278         (Printf.sprintf "Number of passive goals : %d"
279            (passive_set_cardinal g_passives));
280       debug
281         (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
282       debug
283         (Printf.sprintf "Number of passives : %d"
284            (passive_set_cardinal passives));
285       given_clause ~noinfer
286         bag maxvar iterno max_steps timeout 
287         actives passives g_actives g_passives
288   ;;
289
290   let paramod ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
291     let initial_timestamp = Unix.gettimeofday () in
292     let passives =
293       add_passive_clauses ~no_weight:true passive_empty_set passives
294     in
295     let g_passives =
296       add_passive_clauses ~no_weight:true passive_empty_set g_passives
297     in
298     let g_actives = [] in
299     let actives = [], IDX.DT.empty in
300     try 
301      given_clause ~noinfer:false
302       bag maxvar 0 max_steps timeout actives passives g_actives g_passives
303     with 
304     | Sup.Success (bag, _, (i,_,_,_)) ->
305         let l =
306           let rec traverse ongoal (accg,acce) i =
307             match Terms.M.find i bag with
308               | (id,_,_,Terms.Exact _),_ ->
309                   if ongoal then [i],acce else
310                     if (List.mem i acce) then accg,acce else accg,acce@[i]
311               | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),_ ->
312                   if (not ongoal) && (List.mem i acce) then accg,acce
313                   else
314                     let accg,acce = 
315                       traverse false (traverse ongoal (accg,acce) i1) i2
316                     in
317                       if ongoal then i::accg,acce else accg,i::acce
318           in
319           let gsteps,esteps = traverse true ([],[]) i in
320             (List.rev esteps)@gsteps
321         in
322 (*        List.iter (fun id -> let (cl,d) =
323                        Terms.M.find id bag in 
324                     if d then prerr_endline (Pp.pp_unit_clause cl)) l;*)
325         prerr_endline 
326           (Printf.sprintf "Found proof, %fs" 
327             (Unix.gettimeofday() -. initial_timestamp));
328         (* 
329         prerr_endline "Proof:"; 
330         List.iter (fun x ->
331           prerr_endline (Pp.pp_unit_clause (fst(Terms.M.find x bag)))) l;
332         *)
333         [ bag, i, l ]
334     | Failure (msg,_bag,_maxvar,iterno) -> 
335         prerr_endline msg;
336         prerr_endline (Printf.sprintf "FAILURE in %d iterations" iterno); 
337         []
338   ;;
339
340 end