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