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