]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/binaries/matitaprover/matitaprover.ml
cfa82da98b6fa826c86113f631e9d6f07ee2bdee
[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
17 type leaf = int * string
18
19 let cache = ref HC.empty 
20 let num = ref 100
21
22 let hash s = 
23   try HC.find s !cache
24   with Not_found ->
25           cache := HC.add s (!num,s) !cache;
26           decr num;
27           HC.find s !cache
28 ;;
29
30 hash "==";;
31 hash "_";;
32
33 let problem_file = ref "no-file-given";;
34 let tptppath = ref "/";;
35 let seconds = ref 300;;
36
37 let fail_msg () =
38       print_endline ("% SZS status Timeout for " ^ 
39         Filename.basename !problem_file)
40 ;;
41   
42 module type LeafComparer =
43   sig
44     val cmp : leaf -> leaf -> int
45   end
46 ;;
47
48 module MakeBlob(C:LeafComparer) : Terms.Blob 
49   with type t = leaf and type input = Ast.term = struct
50         type t = leaf
51         let eq a b = a == b
52         let compare a b = C.cmp a b
53         let eqP = hash "=="
54         let pp (_,a) =  a 
55         type input = Ast.term
56         let rec embed m = function
57             | Ast.Variable name ->
58                 (try m, List.assoc name m
59                  with Not_found -> 
60                     let t = Terms.Var ~-(List.length m) in
61                     (name,t)::m, t)
62             | Ast.Constant name -> m, Terms.Leaf (hash name)
63             | Ast.Function (name,args) -> 
64                 let m, args = 
65                   HExtlib.list_mapi_acc 
66                     (fun x _ m -> embed m x) m args
67                 in
68                 m, Terms.Node (Terms.Leaf (hash name):: args) 
69         ;;
70         let saturate bo ty = 
71           let vars, ty = embed [] ty in
72           let _, bo = embed vars bo in
73           let bo = Terms.Node (bo :: List.map snd (List.rev vars)) in
74           bo, ty
75         ;;
76         let embed t = snd(embed [] t);;
77
78   end
79 ;;
80
81 let success_msg bag l (pp : ?margin:int -> leaf Terms.unit_clause -> string) ord =
82   (* TODO: do some sort of poor man lock (open + OEXCL) so that
83    *       just one thread at a time prints the proof *)
84   print_endline ("% SZS status Unsatisfiable for " ^ 
85     Filename.basename !problem_file);
86   print_endline ("% SZS output start CNFRefutation for " ^ 
87     Filename.basename !problem_file);
88   flush stdout;
89   List.iter (fun x ->
90     let (cl,_,_) = Terms.get_from_bag x bag in
91     print_endline (pp ~margin:max_int 
92       cl)) l;
93   print_endline ("% SZS output end CNFRefutation for " ^ 
94     Filename.basename !problem_file);
95   let prefix = string_of_int (Unix.getpid ()) in
96   let prerr_endline s = prerr_endline (prefix ^ ": " ^ s) in
97   let times = Unix.times () in
98   prerr_endline ("solved " ^ !problem_file ^ " in " ^ string_of_float
99     (times.Unix.tms_utime +. times.Unix.tms_stime) ^ " using " ^ ord);
100 ;;
101
102 let start_msg passives g_passives (pp : leaf Terms.unit_clause -> string) oname =
103   let prefix = string_of_int (Unix.getpid ()) in
104   let prerr_endline s = prerr_endline (prefix ^ ": " ^ s) in
105   prerr_endline "Facts:";
106   List.iter (fun x -> prerr_endline (" " ^ pp x)) passives;
107   prerr_endline "Goal:";
108   prerr_endline (" " ^ pp g_passives);
109   prerr_endline "Order:";
110   prerr_endline ("  " ^ oname);
111 ;;
112
113 let report_error s =  prerr_endline (string_of_int (Unix.getpid())^": "^s);;
114
115 module Main(P : Paramod.Paramod with type t = leaf) = struct
116
117  let run bag g_passives passives pp_unit_clause name = 
118    match
119      P.paramod 
120       ~max_steps:max_int bag ~g_passives:[g_passives] ~passives 
121    with
122    | P.Error s -> report_error s; 3
123    | P.Unsatisfiable ((bag,_,l)::_) -> 
124          success_msg bag l pp_unit_clause name; 0
125    | P.Unsatisfiable ([]) -> 
126          report_error "Unsatisfiable but no solution output"; 3
127    | P.GaveUp -> 2
128    | P.Timeout _ -> 1
129  ;;
130 end
131
132 let worker order goal hypotheses =
133    let module C = 
134      struct type t = leaf let cmp (a,_) (b,_) = Pervasives.compare a b end 
135    in
136    let module B = MakeBlob(C) in
137    let module Pp = Pp.Pp(B) in
138    let module O = Orderings.NRKBO(B) in (* just for processing the clauses *)
139    let module P = Paramod.Paramod(O) in
140    let module Stats = Stats.Stats(O) in
141    let bag = Terms.empty_bag, 0 in
142    let bag, g_passives = P.mk_goal bag goal in
143    let bag, passives = 
144      HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses 
145    in
146    (* TODO: do stats analysys there and generate a new
147     *       C and then B 
148     * TODO: rebuild clauses, since the ordering has to
149     *       change after the stats are computed *)
150    let symb_list = Stats.parse_symbols passives g_passives in
151      prerr_endline "Hypotheses statistics :";
152      List.iter (fun (t,occ,ar,g_occ) -> prerr_endline
153                   (Printf.sprintf "%s %d %d %d %s"
154                      (B.pp t) ar occ g_occ
155                      (String.concat ","
156                         (List.map B.pp (Stats.dependencies t passives))));
157                   if List.exists
158                     (fun (u,occ2,ar2,g_occ2) -> not (B.eq t u) && occ = occ2
159                         && ar = ar2 && g_occ = g_occ2) symb_list
160                     then prerr_endline ((B.pp t) ^ " clashes")
161                  ) symb_list;
162    let module C = C in
163    let module B = MakeBlob(C) in
164    match order with
165    | `NRKBO ->
166        let module O = Orderings.NRKBO(B) in
167        let module Main = Main(Paramod.Paramod(O)) in
168        start_msg passives g_passives Pp.pp_unit_clause O.name;
169        Main.run bag g_passives passives Pp.pp_unit_clause O.name
170    | `KBO ->
171        let module O = Orderings.KBO(B) in
172        let module Main = Main(Paramod.Paramod(O)) in
173        start_msg passives g_passives Pp.pp_unit_clause O.name;
174        Main.run bag g_passives passives Pp.pp_unit_clause O.name
175    | `LPO ->
176        let module O = Orderings.LPO(B) in
177        let module Main = Main(Paramod.Paramod(O)) in
178        start_msg passives g_passives Pp.pp_unit_clause O.name;
179        Main.run bag g_passives passives Pp.pp_unit_clause O.name
180 ;;
181
182 let print_status p = 
183   let print_endline s = prerr_endline (string_of_int p ^ ": " ^ s) in
184     function
185     | Unix.WEXITED 0 -> 
186         print_endline ("status Unsatisfiable for " ^ 
187           Filename.basename !problem_file);
188     | Unix.WEXITED 1 -> 
189         print_endline ("status Timeout for " ^ 
190           Filename.basename !problem_file);
191     | Unix.WEXITED 2 -> 
192         print_endline ("status GaveUp for " ^ 
193           Filename.basename !problem_file);
194     | Unix.WEXITED 3 ->
195         print_endline ("status Error for " ^ 
196           Filename.basename !problem_file);
197     | Unix.WEXITED _ -> assert false
198     | Unix.WSIGNALED s -> print_endline ("killed by signal " ^ string_of_int s)
199     | Unix.WSTOPPED _ -> print_endline "stopped"
200  ;;
201
202 let killall l = 
203   List.iter (fun pid -> try Unix.kill pid 9 with _ -> ()) l
204 ;;
205
206 let main () =
207   let childs = ref [] in
208   let _ =
209     Sys.signal 24 (Sys.Signal_handle 
210       (fun _ -> fail_msg (); killall !childs; exit 1)) 
211   in
212   let _ =
213     Sys.signal Sys.sigalrm 
214       (Sys.Signal_handle (fun _ -> fail_msg (); killall !childs; exit 1)) 
215   in
216   Arg.parse [
217    "--tptppath", Arg.String (fun p -> tptppath := p), 
218      ("[path]  TPTP lib root, default " ^ !tptppath);
219    "--timeout", Arg.Int (fun p -> seconds := p), 
220      ("[seconds]  timeout, default " ^ string_of_int !seconds);
221    ] (fun x -> problem_file := x) "
222 Matitaprover is the first order automatic prover that equips the 
223 Matita interactive theorem prover (http://matita.cs.unibo.it).
224
225 Developed by A.Asperti, M.Denes and E.Tassi, released under GPL version 2
226 or at your option any later version.
227
228 If --tptppath is given, instead of the problem file you can just give the 
229 problem name with the .p suffix (e.g. BOO001-1.p)
230
231 If --tptppath is not given, included files (i.e. axiom sets) are searched 
232 in the current directory only.
233
234 usage: matitaprover [options] problemfile";
235   let hypotheses, goals = Tptp_cnf.parse ~tptppath:!tptppath !problem_file in
236   let goal = match goals with [x] -> x | _ -> assert false in
237   let _ = Unix.alarm !seconds in
238   childs := 
239     List.map 
240       (fun f -> 
241          let pid = Unix.fork () in 
242          if pid = 0 then (exit (f ())) else pid)
243     [ 
244       (fun () -> worker `NRKBO goal hypotheses)
245     ;
246       (fun () -> worker `KBO goal hypotheses)
247     ;
248       (fun () -> worker `LPO goal hypotheses)
249     ];
250   let rec aux () =
251     if List.length !childs = 0 then
252       (fail_msg (); exit 1)
253     else 
254      match Unix.wait () with
255      | p, (Unix.WEXITED 0 as s) -> 
256          print_status p s;            
257          killall !childs; exit 0;
258      | p, s -> 
259          print_status p s;            
260          childs := List.filter ((<>)p) !childs; aux ()
261   in
262    aux ()
263 ;;
264
265 main ()