]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/paramod.ml
ac73df2d0b90fb1fd0fc9110536c13bee8885922
[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     let bag, maxvar, actives, new_clauses = 
189       Sup.infer_right bag maxvar current actives 
190     in
191       debug "Demodulating goals with actives...";
192       (* keep goals demodulated w.r.t. actives and check if solved *)
193       let bag, g_actives = 
194         List.fold_left 
195           (fun (bag,acc) c -> 
196              match 
197                Sup.simplify_goal ~no_demod:false maxvar (snd actives) bag acc c
198              with
199                | None -> bag, acc
200                | Some (bag,c1) -> bag,if c==c1 then c::acc else c::c1::acc)
201           (bag,[]) g_actives 
202       in
203       let ctable = IDX.index_unit_clause maxvar IDX.DT.empty current in
204       let bag, maxvar, new_goals = 
205         List.fold_left 
206           (fun (bag,m,acc) g -> 
207              let bag, m, ng = Sup.infer_left bag m g ([current],ctable) in
208                bag,m,ng@acc) 
209           (bag,maxvar,[]) g_actives 
210       in
211       let bag = Terms.replace_in_bag (current,false,iterno) bag in
212     bag, maxvar, actives,
213     add_passive_clauses passives new_clauses, g_actives,
214     add_passive_goals g_passives new_goals
215   ;;
216  
217   let rec given_clause ~useage ~noinfer 
218     bag maxvar iterno weight_picks max_steps timeout 
219     actives passives g_actives g_passives 
220   =
221     let iterno = iterno + 1 in
222     if iterno = max_steps then raise (Stop (Timeout (maxvar,bag)));
223     (* timeout check: gettimeofday called only if timeout set *)
224     if timeout <> None &&
225       (match timeout with
226       | None -> assert false
227       | Some timeout -> Unix.gettimeofday () > timeout) then
228         if noinfer then
229           begin
230             debug 
231               ("Last chance: all is indexed " ^ string_of_float
232                 (Unix.gettimeofday()));
233             let maxgoals = 100 in
234             ignore(List.fold_left 
235               (fun (acc,i) x -> 
236                  if i < maxgoals then
237                  ignore(Sup.simplify_goal ~no_demod:true 
238                           maxvar (snd actives) bag acc x)
239                  else
240                    ();
241                  x::acc,i+1)
242               ([],0) g_actives);
243             raise (Stop (Timeout (maxvar,bag)))
244           end
245         else if false then (* activates last chance strategy *)
246           begin
247            debug("Last chance: "^string_of_float (Unix.gettimeofday()));
248            given_clause ~useage ~noinfer:true bag maxvar iterno weight_picks max_steps 
249              (Some (Unix.gettimeofday () +. 20.))
250              actives passives g_actives g_passives;
251            raise (Stop (Timeout (maxvar,bag)));
252           end
253         else raise (Stop (Timeout (maxvar,bag)));
254
255     let use_age = useage && (weight_picks = (iterno / 6 + 1)) in
256     let weight_picks = if use_age then 0 else weight_picks+1
257     in
258
259     let rec aux_select bag passives g_passives =
260       let backward,(weight,current),passives,g_passives =
261         select ~use_age passives g_passives
262       in
263         if use_age && weight > monster then
264           let bag,cl = Terms.add_to_bag current bag in
265             if backward then
266               aux_select bag passives (add_passive_clause g_passives cl)
267             else
268               aux_select bag (add_passive_clause passives cl) g_passives
269         else
270           let bag = Terms.replace_in_bag (current,false,iterno) bag in
271         if backward then
272           let _ = debug ("Selected goal : " ^ Pp.pp_unit_clause current) in
273          match 
274            if noinfer then 
275              if weight > monster then None else Some (bag,current)
276            else 
277              Sup.simplify_goal 
278                ~no_demod:false maxvar (snd actives) bag g_actives current 
279          with
280             | None -> aux_select bag passives g_passives
281             | Some (bag,g_current) ->
282                if noinfer then 
283                  let g_actives = g_current :: g_actives in 
284                  bag,maxvar,actives,passives,g_actives,g_passives
285                else
286                  backward_infer_step bag maxvar actives passives
287                    g_actives g_passives g_current iterno
288         else
289           let _ = debug ("Selected fact : " ^ Pp.pp_unit_clause current) in
290           (*let is_orphan = Sup.orphan_murder bag (fst actives) current in*)
291           match 
292             if noinfer then 
293               if weight > monster then bag,None 
294               else  bag, Some (current,actives)
295             else if Sup.orphan_murder bag (fst actives) current then
296               let _ = debug "Orphan murdered" in
297               let bag = Terms.replace_in_bag (current,true,iterno) bag in
298                 bag, None
299             else Sup.keep_simplified current actives bag maxvar
300           with
301         (*match Sup.one_pass_simplification current actives bag maxvar with*)
302               | bag,None -> aux_select bag passives g_passives
303               | bag,Some (current,actives) ->
304 (*                    if is_orphan then prerr_endline
305                       ("WRONG discarded: " ^ (Pp.pp_unit_clause current));
306                   List.iter (fun x ->
307                                prerr_endline (Pp.pp_unit_clause x))
308                     (fst actives);*)
309
310 (*                  List.iter (fun (id,_,_,_) -> let (cl,d) =
311                              Terms.M.find id bag in 
312                              if d then prerr_endline
313                                ("WRONG discarded: " ^ (Pp.pp_unit_clause cl)))
314                     (current::fst actives);*)
315                   if noinfer then
316                     let actives = 
317                       current::fst actives,
318                       IDX.index_unit_clause maxvar (snd actives) current
319                     in
320                     bag,maxvar,actives,passives,g_actives,g_passives
321                   else
322                     forward_infer_step bag maxvar actives passives
323                       g_actives g_passives current iterno
324     in
325     
326
327       (*prerr_endline "Active table :"; 
328        (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
329           (fst actives)); *)
330
331     let bag,maxvar,actives,passives,g_actives,g_passives =      
332       aux_select bag passives g_passives
333     in
334       debug
335         (Printf.sprintf "Number of active goals : %d"
336            (List.length g_actives));
337       debug
338         (Printf.sprintf "Number of passive goals : %d"
339            (passive_set_cardinal g_passives));
340       debug
341         (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
342       debug
343         (Printf.sprintf "Number of passives : %d"
344            (passive_set_cardinal passives));
345       given_clause ~useage ~noinfer
346         bag maxvar iterno weight_picks max_steps timeout 
347         actives passives g_actives g_passives
348   ;;
349
350   let paramod ~useage ~max_steps ?timeout (bag,maxvar) ~g_passives ~passives =
351     let initial_timestamp = Unix.gettimeofday () in
352     let passives =
353       add_passive_clauses ~no_weight:true passive_empty_set passives
354     in
355     let g_passives =
356       add_passive_goals ~no_weight:true passive_empty_set g_passives
357     in
358     let g_actives = [] in
359     let actives = [], IDX.DT.empty in
360     try 
361      given_clause ~useage ~noinfer:false
362       bag maxvar 0  0 max_steps timeout actives passives g_actives g_passives
363     with 
364     | Sup.Success (bag, _, (i,_,_,_)) ->
365         let l =
366           let rec traverse ongoal (accg,acce) i =
367             match Terms.get_from_bag i bag with
368               | (id,_,_,Terms.Exact _),_,_ ->
369                   if ongoal then [i],acce else
370                     if (List.mem i acce) then accg,acce else accg,acce@[i]
371               | (_,_,_,Terms.Step (_,i1,i2,_,_,_)),_,_ ->
372                   if (not ongoal) && (List.mem i acce) then accg,acce
373                   else
374                     let accg,acce = 
375                       traverse false (traverse ongoal (accg,acce) i1) i2
376                     in
377                       if ongoal then i::accg,acce else accg,i::acce
378           in
379           let gsteps,esteps = traverse true ([],[]) i in
380             (List.rev esteps)@gsteps
381         in
382         let max_w = List.fold_left (fun acc i ->
383                     let (cl,_,_) = Terms.get_from_bag i bag in
384                     max acc (Order.compute_unit_clause_weight cl)) 0 l in
385           prerr_endline "Statistics :";
386           prerr_endline ("Max weight : " ^ (string_of_int max_w));
387 (*        List.iter (fun id -> let ((_,lit,_,proof as cl),d,it) =
388             Terms.get_from_bag id bag in
389               if d then
390                 prerr_endline
391                 (Printf.sprintf "Id : %d, selected at %d, weight %d,disc, by %s"
392                    id it (Order.compute_unit_clause_weight cl) 
393                    (Pp.pp_proof_step proof))
394               else
395                prerr_endline
396                 (Printf.sprintf "Id : %d, selected at %d, weight %d by %s"
397                    id it (Order.compute_unit_clause_weight cl) 
398                    (Pp.pp_proof_step proof))) l;*)
399         prerr_endline 
400           (Printf.sprintf "Found proof, %fs" 
401             (Unix.gettimeofday() -. initial_timestamp));
402         (* 
403         prerr_endline "Proof:"; 
404         List.iter (fun x ->
405           prerr_endline (Pp.pp_unit_clause (fst(Terms.M.find x bag)))) l;
406         *)
407         Unsatisfiable [ bag, i, l ]
408     | Stop (Unsatisfiable _) -> Error "stop bug solution found!"
409     | Stop o -> o
410   ;;
411
412 end