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