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