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