]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/paramod.ml
137cb458672901fb8d3603cc7d79537dfe053105
[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 = 100;;
18     
19 module type Paramod =
20   sig
21     type t
22     type input
23     type szsontology = 
24       | Unsatisfiable of (t Terms.bag * int * int list) list
25       | GaveUp 
26       | Error of string 
27       | Timeout of int * t Terms.bag
28     type bag = t Terms.bag * int
29     val mk_passive : bag -> input * input -> bag * t Terms.unit_clause
30     val mk_goal : bag -> input * input -> bag * t Terms.unit_clause
31     val paramod :
32       useage:bool ->
33       max_steps:int ->
34       ?timeout:float ->
35       bag -> 
36       g_passives:t Terms.unit_clause list -> 
37       passives:t Terms.unit_clause list -> szsontology
38   end
39
40 module Paramod (B : Orderings.Blob) = struct
41   type t = B.t
42   type input = B.input
43   type szsontology = 
44     | Unsatisfiable of (B.t Terms.bag * int * int list) list
45     | GaveUp 
46     | Error of string 
47     | Timeout of int * B.t Terms.bag
48   exception Stop of szsontology
49   type bag = B.t Terms.bag * int
50   module Pp = Pp.Pp (B) 
51   module FU = FoUnif.Founif(B) 
52   module IDX = Index.Index(B) 
53   module Sup = Superposition.Superposition(B) 
54   module Utils = FoUtils.Utils(B) 
55   module Order = B
56   module WeightOrderedPassives =
57       struct
58         type t = B.t Terms.passive_clause
59         let compare = Utils.compare_passive_clauses_weight
60       end
61
62   module AgeOrderedPassives =
63       struct
64         type t = B.t Terms.passive_clause
65         let compare = Utils.compare_passive_clauses_age
66       end
67   
68   module WeightPassiveSet = Set.Make(WeightOrderedPassives)
69   module AgePassiveSet = Set.Make(AgeOrderedPassives)
70
71   let add_passive_clause ?(no_weight=false) (passives_w,passives_a) cl =
72     let cl = if no_weight then (0,cl)
73     else Utils.mk_passive_clause cl in
74     WeightPassiveSet.add cl passives_w, AgePassiveSet.add cl passives_a
75   ;;
76
77   let add_passive_goal ?(no_weight=false) (passives_w,passives_a) g =
78     let g = if no_weight then (0,g)
79     else Utils.mk_passive_goal g in
80     WeightPassiveSet.add g passives_w, AgePassiveSet.add g passives_a
81   ;;
82
83   let remove_passive_clause (passives_w,passives_a) cl =
84     let passives_w = WeightPassiveSet.remove cl passives_w in
85     let passives_a = AgePassiveSet.remove cl passives_a in
86       passives_w,passives_a
87   ;;
88
89   let add_passive_clauses ?(no_weight=false)
90       (passives_w,passives_a) new_clauses =
91     let new_clauses_w,new_clauses_a =
92       List.fold_left (add_passive_clause ~no_weight)
93       (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
94     in
95       (WeightPassiveSet.union new_clauses_w passives_w,
96        AgePassiveSet.union new_clauses_a passives_a)
97   ;;
98
99   let add_passive_goals ?(no_weight=false)
100       (passives_w,passives_a) new_clauses =
101     let new_clauses_w,new_clauses_a =
102       List.fold_left (add_passive_goal ~no_weight)
103       (WeightPassiveSet.empty,AgePassiveSet.empty) new_clauses
104     in
105       (WeightPassiveSet.union new_clauses_w passives_w,
106        AgePassiveSet.union new_clauses_a passives_a)
107   ;;
108
109   let is_passive_set_empty (passives_w,passives_a) =
110     if (WeightPassiveSet.is_empty passives_w) then begin
111       assert (AgePassiveSet.is_empty passives_a); true
112     end else begin
113       assert (not (AgePassiveSet.is_empty passives_a)); false
114     end
115   ;;
116
117   let passive_set_cardinal (passives_w,_) = WeightPassiveSet.cardinal passives_w
118   
119   let passive_empty_set =
120     (WeightPassiveSet.empty,AgePassiveSet.empty)
121   ;;
122
123   let pick_min_passive ~use_age (passives_w,passives_a) =
124     if use_age then AgePassiveSet.min_elt passives_a
125     else WeightPassiveSet.min_elt passives_w
126   ;;
127
128   let mk_clause bag maxvar (t,ty) =
129     let (proof,ty) = B.saturate t ty in
130     let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
131     let bag, c = Terms.add_to_bag c bag in
132     (bag, maxvar), c
133   ;;
134   
135   let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
136   let mk_goal (bag,maxvar) = mk_clause bag maxvar;;
137
138   (* TODO : global age over facts and goals (without comparing weights) *)
139   let select ~use_age passives g_passives =
140     if is_passive_set_empty passives then begin
141       if (is_passive_set_empty g_passives) then
142         raise (Stop GaveUp) (* we say we are incomplete *)
143       else
144        let g_cl = pick_min_passive ~use_age:use_age g_passives in
145         (true,g_cl,passives,remove_passive_clause g_passives g_cl)
146     end
147     else let cl = pick_min_passive ~use_age:use_age passives in
148       if is_passive_set_empty g_passives then
149         (false,cl,remove_passive_clause passives cl,g_passives)
150       else
151         let g_cl = pick_min_passive ~use_age:use_age g_passives in
152         let (id1,_,_,_),(id2,_,_,_) = snd cl, snd g_cl in
153         let cmp = if use_age then id1 <= id2
154         else fst cl <= fst g_cl
155         in
156           if cmp then
157             (false,cl,remove_passive_clause passives cl,g_passives)
158           else
159             (true,g_cl,passives,remove_passive_clause g_passives g_cl)
160   ;;
161
162   let backward_infer_step bag maxvar actives passives
163                           g_actives g_passives g_current iterno =
164     (* superposition left, simplifications on goals *)
165       debug "infer_left step...";
166       let bag, maxvar, new_goals = 
167         Sup.infer_left bag maxvar g_current actives 
168       in
169         debug "Performed infer_left step";
170         let bag = Terms.replace_in_bag (g_current,false,iterno) bag in
171         bag, maxvar, actives, passives, g_current::g_actives,
172     (add_passive_goals g_passives new_goals)
173   ;;
174
175   let forward_infer_step bag maxvar actives passives g_actives
176                          g_passives current iterno =
177     (* forward step *)
178     
179     (* e = select P           *
180      * e' = demod A e         *
181      * A' = demod [e'] A      *
182      * A'' = A' + e'          *
183      * e'' = fresh e'         *
184      * new = supright e'' A'' *
185      * new'= demod A'' new    *
186      * P' = P + new'          *)
187     debug "Forward infer step...";
188     debug ("Number of actives : " ^ (string_of_int (List.length (fst actives))));
189     let bag, maxvar, actives, new_clauses = 
190       Sup.infer_right bag maxvar current actives 
191     in
192       debug "Demodulating goals with actives...";
193       (* keep goals demodulated w.r.t. actives and check if solved *)
194       let bag, g_actives = 
195         List.fold_left 
196           (fun (bag,acc) c -> 
197              match 
198                Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
199              with
200                | None -> bag, acc
201                | Some (bag,c1) -> bag,if c==c1 then c::acc else c::c1::acc)
202           (bag,[]) g_actives 
203       in
204       let ctable = IDX.index_unit_clause IDX.DT.empty current in
205       let bag, maxvar, new_goals = 
206         List.fold_left 
207           (fun (bag,m,acc) g -> 
208              let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
209                bag,m,ng@acc) 
210           (bag,maxvar,[]) g_actives 
211       in
212       let bag = Terms.replace_in_bag (current,false,iterno) bag in
213     bag, maxvar, actives,
214     add_passive_clauses passives new_clauses, g_actives,
215     add_passive_goals g_passives new_goals
216   ;;
217  
218   let rec given_clause ~noinfer 
219     bag maxvar iterno weight_picks max_steps timeout 
220     actives passives g_actives g_passives 
221   =
222     let iterno = iterno + 1 in
223     if iterno = max_steps then raise (Stop (Timeout (maxvar,bag)));
224     (* timeout check: gettimeofday called only if timeout set *)
225     if timeout <> None &&
226       (match timeout with
227       | None -> assert false
228       | Some timeout -> Unix.gettimeofday () > timeout) then
229         if noinfer then
230           begin
231             debug 
232               ("Last chance: all is indexed " ^ string_of_float
233                 (Unix.gettimeofday()));
234             let maxgoals = 100 in
235             ignore(List.fold_left 
236               (fun (acc,i) x -> 
237                  if i < maxgoals then
238                  ignore(Sup.simplify_goal ~no_demod:true 
239                           maxvar (snd actives) bag acc x)
240                  else
241                    ();
242                  x::acc,i+1)
243               ([],0) g_actives);
244             raise (Stop (Timeout (maxvar,bag)))
245           end
246         else if false then (* activates last chance strategy *)
247           begin
248            debug("Last chance: "^string_of_float (Unix.gettimeofday()));
249            given_clause ~noinfer:true bag maxvar iterno weight_picks max_steps 
250              (Some (Unix.gettimeofday () +. 20.))
251              actives passives g_actives g_passives;
252            raise (Stop (Timeout (maxvar,bag)));
253           end
254         else raise (Stop (Timeout (maxvar,bag)));
255
256     let use_age = weight_picks = (iterno / 6 + 1) in
257     let weight_picks = if use_age then 0 else weight_picks+1
258     in
259
260     let rec aux_select bag passives g_passives =
261       let backward,(weight,current),passives,g_passives =
262         select ~use_age passives g_passives
263       in
264         if use_age && weight > monster then
265           let bag,cl = Terms.add_to_bag current bag in
266             if backward then
267               aux_select bag passives (add_passive_clause g_passives cl)
268             else
269               aux_select bag (add_passive_clause passives cl) g_passives
270         else
271           let bag = Terms.replace_in_bag (current,false,iterno) bag in
272         if backward then
273           let _ = debug ("Selected goal : " ^ Pp.pp_unit_clause current) in
274          match 
275            if noinfer then 
276              if weight > monster then None else Some (bag,current)
277            else 
278              Sup.simplify_goal 
279                ~no_demod:false maxvar (snd actives) bag g_actives current 
280          with
281             | None -> aux_select bag passives g_passives
282             | Some (bag,g_current) ->
283                if noinfer then 
284                  let g_actives = g_current :: g_actives in 
285                  bag,maxvar,actives,passives,g_actives,g_passives
286                else
287                  backward_infer_step bag maxvar actives passives
288                    g_actives g_passives g_current iterno
289         else
290           let _ = debug ("Selected fact : " ^ Pp.pp_unit_clause current) in
291           (*let is_orphan = Sup.orphan_murder bag (fst actives) current in*)
292           match 
293             if noinfer then 
294               if weight > monster then bag,None 
295               else  bag, Some (current,actives)
296             else if Sup.orphan_murder bag (fst actives) current then
297               let _ = debug "Orphan murdered" in
298               let bag = Terms.replace_in_bag (current,true,iterno) bag in
299                 bag, None
300             else Sup.keep_simplified current actives bag maxvar
301           with
302         (*match Sup.one_pass_simplification current actives bag maxvar with*)
303               | bag,None -> aux_select bag passives g_passives
304               | bag,Some (current,actives) ->
305 (*                    if is_orphan then prerr_endline
306                       ("WRONG discarded: " ^ (Pp.pp_unit_clause current));
307                   List.iter (fun x ->
308                                prerr_endline (Pp.pp_unit_clause x))
309                     (fst actives);*)
310
311 (*                  List.iter (fun (id,_,_,_) -> let (cl,d) =
312                              Terms.M.find id bag in 
313                              if d then prerr_endline
314                                ("WRONG discarded: " ^ (Pp.pp_unit_clause cl)))
315                     (current::fst actives);*)
316                   if noinfer then
317                     let actives = 
318                       current::fst actives,
319                       IDX.index_unit_clause (snd actives) current
320                     in
321                     bag,maxvar,actives,passives,g_actives,g_passives
322                   else
323                     forward_infer_step bag maxvar actives passives
324                       g_actives g_passives current iterno
325     in
326     
327
328       (*prerr_endline "Active table :"; 
329        (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
330           (fst actives)); *)
331
332     let bag,maxvar,actives,passives,g_actives,g_passives =      
333       aux_select bag passives g_passives
334     in
335       debug
336         (Printf.sprintf "Number of active goals : %d"
337            (List.length g_actives));
338       debug
339         (Printf.sprintf "Number of passive goals : %d"
340            (passive_set_cardinal g_passives));
341       debug
342         (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
343       debug
344         (Printf.sprintf "Number of passives : %d"
345            (passive_set_cardinal passives));
346       given_clause ~useage ~noinfer
347         bag maxvar iterno weight_picks max_steps timeout 
348         actives passives g_actives g_passives
349   ;;
350
351   let paramod ~useage ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
352     let initial_timestamp = Unix.gettimeofday () in
353     let passives =
354       add_passive_clauses ~no_weight:true passive_empty_set passives
355     in
356     let g_passives =
357       add_passive_goals ~no_weight:true passive_empty_set g_passives
358     in
359     let g_actives = [] in
360     let actives = [], IDX.DT.empty in
361     try 
362      given_clause ~useage ~noinfer:false
363       bag maxvar 0  0 max_steps timeout actives passives g_actives g_passives
364     with 
365     | Sup.Success (bag, _, (i,_,_,_)) ->
366         let l =
367           let rec traverse ongoal (accg,acce) i =
368             match Terms.get_from_bag i bag with
369               | (id,_,_,Terms.Exact _),_,_ ->
370                   if ongoal then [i],acce else
371                     if (List.mem i acce) then accg,acce else accg,acce@[i]
372               | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),_,_ ->
373                   if (not ongoal) && (List.mem i acce) then accg,acce
374                   else
375                     let accg,acce = 
376                       traverse false (traverse ongoal (accg,acce) i1) i2
377                     in
378                       if ongoal then i::accg,acce else accg,i::acce
379           in
380           let gsteps,esteps = traverse true ([],[]) i in
381             (List.rev esteps)@gsteps
382         in
383         let max_w = List.fold_left (fun acc i ->
384                     let (cl,_,_) = Terms.get_from_bag i bag in
385                     max acc (Order.compute_unit_clause_weight cl)) 0 l in
386           prerr_endline "Statistics :";
387           prerr_endline ("Max weight : " ^ (string_of_int max_w));
388 (*        List.iter (fun id -> let ((_,lit,_,proof as cl),d,it) =
389             Terms.get_from_bag id bag in
390               if d then
391                 prerr_endline
392                 (Printf.sprintf "Id : %d, selected at %d, weight %d,disc, by %s"
393                    id it (Order.compute_unit_clause_weight cl) 
394                    (Pp.pp_proof_step proof))
395               else
396                prerr_endline
397                 (Printf.sprintf "Id : %d, selected at %d, weight %d by %s"
398                    id it (Order.compute_unit_clause_weight cl) 
399                    (Pp.pp_proof_step proof))) l;*)
400         prerr_endline 
401           (Printf.sprintf "Found proof, %fs" 
402             (Unix.gettimeofday() -. initial_timestamp));
403         (* 
404         prerr_endline "Proof:"; 
405         List.iter (fun x ->
406           prerr_endline (Pp.pp_unit_clause (fst(Terms.M.find x bag)))) l;
407         *)
408         Unsatisfiable [ bag, i, l ]
409     | Stop (Unsatisfiable _) -> Error "stop bug solution found!"
410     | Stop o -> o
411   ;;
412
413 end