]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/binaries/matitaprover/matitaprover.ml
Release 0.5.9.
[helm.git] / helm / software / components / binaries / matitaprover / matitaprover.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 module OT = struct type t = string  let compare = Pervasives.compare end 
15 module HC = Map.Make(OT)
16 module TS = HTopoSort.Make(OT)
17
18 type leaf = int * string
19
20 let cache = ref HC.empty 
21 let num = ref 100
22
23 let hash s = 
24   try HC.find s !cache
25   with Not_found ->
26           cache := HC.add s (!num,s) !cache;
27           decr num;
28           HC.find s !cache
29 ;;
30
31 hash "==";;
32 hash "_";;
33
34 let problem_file = ref "no-file-given";;
35 let tptppath = ref "/";;
36 let seconds = ref 0;;
37
38 let fail_msg () =
39       print_endline ("% SZS status Timeout for " ^ 
40         Filename.basename !problem_file)
41 ;;
42   
43 module type LeafComparer =
44   sig
45     val cmp : leaf -> leaf -> int
46   end
47 ;;
48
49 module MakeBlob(C:LeafComparer) : Terms.Blob 
50   with type t = leaf and type input = Ast.term = struct
51         type t = leaf
52         let eq a b = a == b
53         let compare a b = C.cmp a b
54         let eqP = hash "=="
55         let pp (_,a) =  a 
56         type input = Ast.term
57         let rec embed m = function
58             | Ast.Variable name ->
59                 (try m, List.assoc name m
60                  with Not_found -> 
61                     let t = Terms.Var ~-(List.length m) in
62                     (name,t)::m, t)
63             | Ast.Constant name -> m, Terms.Leaf (hash name)
64             | Ast.Function (name,args) -> 
65                 let m, args = 
66                   HExtlib.list_mapi_acc 
67                     (fun x _ m -> embed m x) m args
68                 in
69                 m, Terms.Node (Terms.Leaf (hash name):: args) 
70         ;;
71         let saturate bo ty = 
72           let vars, ty = embed [] ty in
73           let _, bo = embed vars bo in
74           let bo = Terms.Node (bo :: List.map snd (List.rev vars)) in
75           bo, ty
76         ;;
77         let embed t = snd(embed [] t);;
78
79   end
80 ;;
81
82 let success_msg bag l (pp : ?margin:int -> leaf Terms.unit_clause -> string) ord =
83   (* TODO: do some sort of poor man lock (open + OEXCL) so that
84    *       just one thread at a time prints the proof *)
85   print_endline ("% SZS status Unsatisfiable for " ^ 
86     Filename.basename !problem_file);
87   print_endline ("% SZS output start CNFRefutation for " ^ 
88     Filename.basename !problem_file);
89   flush stdout;
90   List.iter (fun x ->
91     let (cl,_,_) = Terms.get_from_bag x bag in
92     print_endline (pp ~margin:max_int 
93       cl)) l;
94   print_endline ("% SZS output end CNFRefutation for " ^ 
95     Filename.basename !problem_file);
96   let prefix = string_of_int (Unix.getpid ()) in
97   let prerr_endline s = prerr_endline (prefix ^ ": " ^ s) in
98   let times = Unix.times () in
99   prerr_endline ("solved " ^ !problem_file ^ " in " ^ string_of_float
100     (times.Unix.tms_utime +. times.Unix.tms_stime) ^ "(Process Time) using " ^ ord);
101 ;;
102
103 let start_msg stats passives g_passives (pp : ?margin:int -> leaf Terms.unit_clause -> string) oname =
104   let prefix = string_of_int (Unix.getpid ()) in
105   let prerr_endline s = prerr_endline (prefix ^ ": " ^ s) in
106   prerr_endline "Facts:";
107   List.iter (fun x -> prerr_endline (" " ^ pp x)) passives;
108   prerr_endline "Goal:";
109   prerr_endline (" " ^ pp g_passives);
110 (*  prerr_endline "Order:";
111   prerr_endline ("  " ^ oname);
112   prerr_endline "Leaf order:";
113   List.iter (fun ((_,name), (a,b,c,gp,l)) ->
114      prerr_endline (" " ^name ^ " " ^ string_of_int a ^ " " ^
115        string_of_int b ^ " " ^
116        string_of_int c ^ " " ^
117        String.concat "," (List.map string_of_int gp) ^ 
118        String.concat "," (List.map snd l))) stats;*)
119 ;;
120
121 let report_error s =  prerr_endline (string_of_int (Unix.getpid())^": "^s);;
122
123
124 module Main(P : Paramod.Paramod with type t = leaf) = struct
125
126   (*let mk_clause bag maxvar (t,ty) =
127     let (proof,ty) = B.saturate t ty in
128     let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
129     let bag, c = Terms.add_to_bag c bag in
130       (bag, maxvar), c
131   ;;
132   
133   let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
134   let mk_goal (bag,maxvar) = mk_clause bag maxvar;;*)
135
136  let run ~useage ~printmsg stats goal hypotheses pp_unit_clause name = 
137    let bag = Terms.empty_bag, 0 in
138    let bag, g_passives = P.mk_goal bag goal in
139    let bag, passives = 
140      HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses 
141    in
142    if printmsg then start_msg stats passives g_passives pp_unit_clause name;
143    match
144      P.paramod ~useage
145       ~max_steps:max_int bag ~g_passives:[g_passives] ~passives 
146    with
147    | P.Error s -> report_error s; 3
148    | P.Unsatisfiable ((bag,_,l)::_) -> 
149          success_msg bag l pp_unit_clause name; 0
150    | P.Unsatisfiable ([]) -> 
151          report_error "Unsatisfiable but no solution output"; 3
152    | P.GaveUp -> 2
153    | P.Timeout _ -> 1
154  ;;
155 end
156
157  let compute_stats goal hypotheses =
158    let module C = 
159      struct type t = leaf let cmp (a,_) (b,_) = Pervasives.compare a b end 
160    in
161    let module B = MakeBlob(C) in
162    let module Pp = Pp.Pp(B) in
163    let module O = Orderings.NRKBO(B) in
164    let module P = Paramod.Paramod(O) in
165    let module Stats = Stats.Stats(O) in
166    let bag = Terms.empty_bag, 0 in
167    let bag, g_passives = P.mk_goal bag goal in
168    let bag, passives = 
169      HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses 
170    in
171    let data = Stats.parse_symbols passives g_passives in
172    let data = 
173      List.map
174        (fun (name, n_occ, arity, n_gocc, g_pos) ->
175           name, (n_occ, arity, n_gocc, g_pos, Stats.dependencies name passives))
176        data
177    in
178    let oplist = List.map (fun ((_,x),_) -> x) data in
179    let deps op = 
180      (let _,(_,_,_,_,d) = List.find (fun ((_,op'),_) -> op = op') data 
181       in List.map snd d)
182    in 
183    let oplist = TS.topological_sort oplist deps in
184    List.sort 
185      (fun ((_,n1),(o1,a1,go1,p1,_)) ((_,n2),(o2,a2,go2,p2,_)) ->
186         if a1 = 0 && a2 = 0 then 0
187         else if a1 = 0 then -1
188         else if a2 = 0 then 1
189         else let res = Pervasives.compare (a1,o1,-go1,p1) (a2,o2,-go2,p2)
190              in if res = 0 then Pervasives.compare (HExtlib.list_index ((=) n1) oplist) (HExtlib.list_index ((=) n2) oplist)
191                 else res)
192      data
193  ;;
194
195 let worker order ~useage ~printmsg goal hypotheses =
196    let stats = compute_stats goal hypotheses in
197    let module C = 
198      struct
199       let cmp =
200         let raw = List.map snd stats in
201         let rec pos x = function
202           | ((y,_)::tl) when y = x -> 0
203           | _::tl -> 1 + pos x tl
204           | [] -> assert false
205         in
206         if List.length raw = 
207            List.length (HExtlib.list_uniq raw)
208         then
209           ((*prerr_endline "NO CLASH, using fixed ground order";*)
210            fun a b ->
211             Pervasives.compare 
212               (pos a stats)
213               (pos b stats))
214         else
215           ((*prerr_endline "CLASH, statistics insufficient";*)
216             fun (a,_) (b,_) -> Pervasives.compare a b)
217       ;;
218      end
219    in
220    let module B = MakeBlob(C) in
221    let module Pp = Pp.Pp(B) in
222    match order with
223    | `NRKBO ->
224        let module O = Orderings.NRKBO(B) in
225        let module Main = Main(Paramod.Paramod(O)) in
226        Main.run ~useage ~printmsg stats goal hypotheses Pp.pp_unit_clause O.name
227    | `KBO ->
228        let module O = Orderings.KBO(B) in
229        let module Main = Main(Paramod.Paramod(O)) in
230        Main.run ~useage ~printmsg stats goal hypotheses Pp.pp_unit_clause O.name
231    | `LPO ->
232        let module O = Orderings.LPO(B) in
233        let module Main = Main(Paramod.Paramod(O)) in
234        Main.run ~useage ~printmsg stats goal hypotheses Pp.pp_unit_clause O.name
235 ;;
236
237 let print_status p = 
238   let print_endline s = () in (* prerr_endline (string_of_int p ^ ": " ^ s) in*)
239     function
240     | Unix.WEXITED 0 -> 
241         print_endline ("status Unsatisfiable for " ^ 
242           Filename.basename !problem_file);
243     | Unix.WEXITED 1 -> 
244         print_endline ("status Timeout for " ^ 
245           Filename.basename !problem_file);
246     | Unix.WEXITED 2 -> 
247         print_endline ("status GaveUp for " ^ 
248           Filename.basename !problem_file);
249     | Unix.WEXITED 3 ->
250         print_endline ("status Error for " ^ 
251           Filename.basename !problem_file);
252     | Unix.WEXITED _ -> assert false
253     | Unix.WSIGNALED s -> print_endline ("killed by signal " ^ string_of_int s)
254     | Unix.WSTOPPED _ -> print_endline "stopped"
255  ;;
256
257 let killall l = 
258   List.iter (fun pid -> try Unix.kill pid 9 with _ -> ()) l
259 ;;
260
261 let main () =
262   let childs = ref [] in
263   let _ =
264     Sys.signal 24 (Sys.Signal_handle 
265       (fun _ -> fail_msg (); killall !childs; exit 1)) 
266   in
267   let _ =
268     Sys.signal Sys.sigalrm 
269       (Sys.Signal_handle (fun _ -> prerr_endline "Alarm!"; fail_msg (); killall !childs; exit 1)) 
270   in
271   Arg.parse [
272    "--tptppath", Arg.String (fun p -> tptppath := p), 
273      ("[path]  TPTP lib root, default " ^ !tptppath);
274    "--timeout", Arg.Int (fun p -> seconds := p), 
275      ("[seconds]  timeout, default none");
276    ] (fun x -> problem_file := x) "
277 Matitaprover is the first order automatic prover that equips the 
278 Matita interactive theorem prover (http://matita.cs.unibo.it).
279
280 Developed by A.Asperti, M.Denes and E.Tassi, released under GPL version 2
281 or at your option any later version.
282
283 If --tptppath is given, instead of the problem file you can just give the 
284 problem name with the .p suffix (e.g. BOO001-1.p)
285
286 If --tptppath is not given, included files (i.e. axiom sets) are searched 
287 in the current directory only.
288
289 usage: matitaprover [options] problemfile";
290   let hypotheses, goals = Tptp_cnf.parse ~tptppath:!tptppath !problem_file in
291   let goal = match goals with [x] -> x | _ -> assert false in
292   let _ = if !seconds > 0 then Unix.alarm !seconds else 0 in
293   childs := 
294     List.map 
295       (fun f -> 
296          let pid = Unix.fork () in 
297          if pid = 0 then (exit (f ())) else pid)
298     [ 
299       (fun () -> worker `NRKBO ~useage:true ~printmsg:true goal hypotheses)
300     ;
301       (fun () -> worker `KBO ~useage:true ~printmsg:false goal hypotheses)
302     ;
303       (fun () -> worker `LPO ~useage:true ~printmsg:false goal hypotheses)
304     ;
305       (fun () -> worker `NRKBO ~useage:false ~printmsg:false goal hypotheses)
306     ];
307   let rec aux () =
308     if List.length !childs = 0 then
309       (fail_msg (); exit 1)
310     else 
311      match Unix.wait () with
312      | p, (Unix.WEXITED 0 as s) -> 
313          print_status p s;            
314          killall !childs; exit 0;
315      | p, s -> 
316          print_status p s;            
317          childs := List.filter ((<>)p) !childs; aux ()
318   in
319    aux ()
320 ;;
321
322 main ()