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