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