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