]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/binaries/matitaprover/matitaprover.ml
3736c024dbf02a4b2199f235480bccbae8e4bab4
[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 300;;
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.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) ^ " using " ^ ord);
101 ;;
102
103 let start_msg stats passives g_passives (pp : ?margin:int -> leaf Terms.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 module Main(P : Paramod.Paramod with type t = leaf) = struct
124
125  let run ~useage ~printmsg stats goal hypotheses pp_clause name = 
126    let bag = Terms.empty_bag, 0 in
127    let bag, g_passives = P.mk_goal bag goal in
128    let bag, passives = 
129      HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses 
130    in
131    if printmsg then start_msg stats passives g_passives pp_clause name;
132    match
133      P.paramod ~useage
134       ~max_steps:max_int bag ~g_passives:[g_passives] ~passives 
135    with
136    | P.Error s -> report_error s; 3
137    | P.Unsatisfiable ((bag,_,l)::_) -> 
138          success_msg bag l pp_clause name; 0
139    | P.Unsatisfiable ([]) -> 
140          report_error "Unsatisfiable but no solution output"; 3
141    | P.GaveUp -> 2
142    | P.Timeout _ -> 1
143  ;;
144 end
145
146  let compute_stats goal hypotheses =
147    let module C = 
148      struct type t = leaf let cmp (a,_) (b,_) = Pervasives.compare a b end 
149    in
150    let module B = MakeBlob(C) in
151    let module Pp = Pp.Pp(B) in
152    let module O = Orderings.NRKBO(B) in
153    let module P = Paramod.Paramod(O) in
154    let module Stats = Stats.Stats(O) in
155    let bag = Terms.empty_bag, 0 in
156    let bag, g_passives = P.mk_goal bag goal in
157    let bag, passives = 
158      HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses 
159    in
160    let data = Stats.parse_symbols passives g_passives in
161    let data = 
162      List.map
163        (fun (name, n_occ, arity, n_gocc, g_pos) ->
164           name, (n_occ, arity, n_gocc, g_pos, Stats.dependencies name passives))
165        data
166    in
167    let oplist = List.map (fun ((_,x),_) -> x) data in
168    let deps op = 
169      (let _,(_,_,_,_,d) = List.find (fun ((_,op'),_) -> op = op') data 
170       in List.map snd d)
171    in 
172    let oplist = TS.topological_sort oplist deps in
173    List.sort 
174      (fun ((_,n1),(o1,a1,go1,p1,_)) ((_,n2),(o2,a2,go2,p2,_)) ->
175         if a1 = 0 && a2 = 0 then 0
176         else if a1 = 0 then -1
177         else if a2 = 0 then 1
178         else let res = Pervasives.compare (a1,o1,-go1,p1) (a2,o2,-go2,p2)
179              in if res = 0 then Pervasives.compare (HExtlib.list_index ((=) n1) oplist) (HExtlib.list_index ((=) n2) oplist)
180                 else res)
181      data
182  ;;
183
184 let worker order ~useage ~printmsg goal hypotheses =
185    let stats = compute_stats goal hypotheses in
186    let module C = 
187      struct
188       let cmp =
189         let raw = List.map snd stats in
190         let rec pos x = function
191           | ((y,_)::tl) when y = x -> 0
192           | _::tl -> 1 + pos x tl
193           | [] -> assert false
194         in
195         if List.length raw = 
196            List.length (HExtlib.list_uniq raw)
197         then
198           (prerr_endline "NO CLASH, using fixed ground order";
199            fun a b ->
200             Pervasives.compare 
201               (pos a stats)
202               (pos b stats))
203         else
204           (prerr_endline "CLASH, statistics insufficient";
205             fun (a,_) (b,_) -> Pervasives.compare a b)
206       ;;
207      end
208    in
209    let module B = MakeBlob(C) in
210    let module Pp = Pp.Pp(B) in
211    match order with
212    | `NRKBO ->
213        let module O = Orderings.NRKBO(B) in
214        let module Main = Main(Paramod.Paramod(O)) in
215        Main.run ~useage ~printmsg stats goal hypotheses Pp.pp_clause O.name
216    | `KBO ->
217        let module O = Orderings.KBO(B) in
218        let module Main = Main(Paramod.Paramod(O)) in
219        Main.run ~useage ~printmsg stats goal hypotheses Pp.pp_clause O.name
220    | `LPO ->
221        let module O = Orderings.LPO(B) in
222        let module Main = Main(Paramod.Paramod(O)) in
223        Main.run ~useage ~printmsg stats goal hypotheses Pp.pp_clause O.name
224 ;;
225
226 let print_status p = 
227   let print_endline s = prerr_endline (string_of_int p ^ ": " ^ s) in
228     function
229     | Unix.WEXITED 0 -> 
230         print_endline ("status Unsatisfiable for " ^ 
231           Filename.basename !problem_file);
232     | Unix.WEXITED 1 -> 
233         print_endline ("status Timeout for " ^ 
234           Filename.basename !problem_file);
235     | Unix.WEXITED 2 -> 
236         print_endline ("status GaveUp for " ^ 
237           Filename.basename !problem_file);
238     | Unix.WEXITED 3 ->
239         print_endline ("status Error for " ^ 
240           Filename.basename !problem_file);
241     | Unix.WEXITED _ -> assert false
242     | Unix.WSIGNALED s -> print_endline ("killed by signal " ^ string_of_int s)
243     | Unix.WSTOPPED _ -> print_endline "stopped"
244  ;;
245
246 let killall l = 
247   List.iter (fun pid -> try Unix.kill pid 9 with _ -> ()) l
248 ;;
249
250 let main () =
251   let childs = ref [] in
252   let _ =
253     Sys.signal 24 (Sys.Signal_handle 
254       (fun _ -> fail_msg (); killall !childs; exit 1)) 
255   in
256   let _ =
257     Sys.signal Sys.sigalrm 
258       (Sys.Signal_handle (fun _ -> fail_msg (); killall !childs; exit 1)) 
259   in
260   Arg.parse [
261    "--tptppath", Arg.String (fun p -> tptppath := p), 
262      ("[path]  TPTP lib root, default " ^ !tptppath);
263    "--timeout", Arg.Int (fun p -> seconds := p), 
264      ("[seconds]  timeout, default " ^ string_of_int !seconds);
265    ] (fun x -> problem_file := x) "
266 Matitaprover is the first order automatic prover that equips the 
267 Matita interactive theorem prover (http://matita.cs.unibo.it).
268
269 Developed by A.Asperti, M.Denes and E.Tassi, released under GPL version 2
270 or at your option any later version.
271
272 If --tptppath is given, instead of the problem file you can just give the 
273 problem name with the .p suffix (e.g. BOO001-1.p)
274
275 If --tptppath is not given, included files (i.e. axiom sets) are searched 
276 in the current directory only.
277
278 usage: matitaprover [options] problemfile";
279   let hypotheses, goals = Tptp_cnf.parse ~tptppath:!tptppath !problem_file in
280   let goal = match goals with [x] -> x | _ -> assert false in
281   let _ = Unix.alarm !seconds in
282   childs := 
283     List.map 
284       (fun f -> 
285          let pid = Unix.fork () in 
286          if pid = 0 then (exit (f ())) else pid)
287     [ 
288       (fun () -> worker `NRKBO ~useage:true ~printmsg:true goal hypotheses)
289     ;
290       (fun () -> worker `KBO ~useage:true ~printmsg:false goal hypotheses)
291     ;
292       (fun () -> worker `LPO ~useage:true ~printmsg:false goal hypotheses)
293     ;
294       (fun () -> worker `NRKBO ~useage:false ~printmsg:false goal hypotheses)
295     ];
296   let rec aux () =
297     if List.length !childs = 0 then
298       (fail_msg (); exit 1)
299     else 
300      match Unix.wait () with
301      | p, (Unix.WEXITED 0 as s) -> 
302          print_status p s;            
303          killall !childs; exit 0;
304      | p, s -> 
305          print_status p s;            
306          childs := List.filter ((<>)p) !childs; aux ()
307   in
308    aux ()
309 ;;
310
311 main ()