]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/binaries/matitaprover/matitaprover.ml
New functorialization: paramod is abstracted over a Orderings.Blob, that is like...
[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   print_endline ("% SZS status Unsatisfiable for " ^ 
83     Filename.basename !problem_file);
84   print_endline ("% SZS output start CNFRefutation for " ^ 
85     Filename.basename !problem_file);
86   flush stdout;
87   List.iter (fun x ->
88     let (cl,_,_) = Terms.get_from_bag x bag in
89     print_endline (pp ~margin:max_int 
90       cl)) l;
91   print_endline ("% SZS output end CNFRefutation for " ^ 
92     Filename.basename !problem_file);
93   let prefix = string_of_int (Unix.getpid ()) in
94   let prerr_endline s = prerr_endline (prefix ^ ": " ^ s) in
95   let times = Unix.times () in
96   prerr_endline ("solved " ^ !problem_file ^ " in " ^ string_of_float
97     (times.Unix.tms_utime +. times.Unix.tms_stime) ^ " using " ^ ord);
98 ;;
99
100 let start_msg passives g_passives (pp : leaf Terms.unit_clause -> string) oname =
101   let prefix = string_of_int (Unix.getpid ()) in
102   let prerr_endline s = prerr_endline (prefix ^ ": " ^ s) in
103   prerr_endline "Facts:";
104   List.iter (fun x -> prerr_endline (" " ^ pp x)) passives;
105   prerr_endline "Goal:";
106   prerr_endline (" " ^ pp g_passives);
107   prerr_endline "Order:";
108   prerr_endline ("  " ^ oname);
109 ;;
110
111 let report_error s =  prerr_endline (string_of_int (Unix.getpid())^": "^s);;
112
113 module Main(C:Orderings.Blob with type t = leaf) = struct
114  let main goal hypotheses =
115    let module B = C in
116    let module Pp = Pp.Pp(B) in
117    let module P = Paramod.Paramod(B) in
118    let bag = Terms.empty_bag, 0 in
119    let bag, g_passives = P.mk_goal bag goal in
120    let bag, passives = 
121      HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses 
122    in
123    start_msg passives g_passives Pp.pp_unit_clause C.name;
124    match 
125      P.paramod 
126       ~max_steps:max_int bag ~g_passives:[g_passives] ~passives 
127    with
128    | P.Error s -> report_error s; 3
129    | P.Unsatisfiable ((bag,_,l)::_) -> 
130          success_msg bag l Pp.pp_unit_clause C.name; 0
131    | P.Unsatisfiable ([]) -> 
132          report_error "Unsatisfiable but no solution output"; 3
133    | P.GaveUp -> 2
134    | P.Timeout _ -> 1
135  ;;
136 end
137
138 let print_status p = 
139   let print_endline s = prerr_endline (string_of_int p ^ ": " ^ s) in
140     function
141     | Unix.WEXITED 0 -> 
142         print_endline ("status Unsatisfiable for " ^ 
143           Filename.basename !problem_file);
144     | Unix.WEXITED 1 -> 
145         print_endline ("status Timeout for " ^ 
146           Filename.basename !problem_file);
147     | Unix.WEXITED 2 -> 
148         print_endline ("status GaveUp for " ^ 
149           Filename.basename !problem_file);
150     | Unix.WEXITED 3 ->
151         print_endline ("status Error for " ^ 
152           Filename.basename !problem_file);
153     | Unix.WEXITED _ -> assert false
154     | Unix.WSIGNALED s -> print_endline ("killed by signal " ^ string_of_int s)
155     | Unix.WSTOPPED _ -> print_endline "stopped"
156  ;;
157
158 let killall l = 
159   List.iter (fun pid -> try Unix.kill pid 9 with _ -> ()) l
160 ;;
161
162 let main () =
163   let childs = ref [] in
164   let _ =
165     Sys.signal 24 (Sys.Signal_handle 
166       (fun _ -> fail_msg (); killall !childs; exit 1)) 
167   in
168   let _ =
169     Sys.signal Sys.sigalrm 
170       (Sys.Signal_handle (fun _ -> fail_msg (); killall !childs; exit 1)) 
171   in
172   Arg.parse [
173    "--tptppath", Arg.String (fun p -> tptppath := p), 
174      ("[path]  TPTP lib root, default " ^ !tptppath);
175    "--timeout", Arg.Int (fun p -> seconds := p), 
176      ("[seconds]  timeout, default " ^ string_of_int !seconds);
177    ] (fun x -> problem_file := x) "
178 Matitaprover is the first order automatic prover that equips the 
179 Matita interactive theorem prover (http://matita.cs.unibo.it).
180
181 Developed by A.Asperti, M.Denes and E.Tassi, released under GPL version 2
182 or at your option any later version.
183
184 If --tptppath is given, instead of the problem file you can just give the 
185 problem name with the .p suffix (e.g. BOO001-1.p)
186
187 If --tptppath is not given, included files (i.e. axiom sets) are searched 
188 in the current directory only.
189
190 usage: matitaprover [options] problemfile";
191   let hypotheses, goals = Tptp_cnf.parse ~tptppath:!tptppath !problem_file in
192   let goal = match goals with [x] -> x | _ -> assert false in
193   let _ = Unix.alarm !seconds in
194   childs := 
195     List.map 
196       (fun f -> 
197          let pid = Unix.fork () in 
198          if pid = 0 then (exit (f ())) else pid)
199     [ 
200       (fun () ->     
201         let module M = Main(Orderings.NRKBO(MakeBlob(struct let cmp (a,_) (b,_) = compare a b end))) in
202         M.main goal hypotheses)
203     ;
204       (fun () ->     
205         let module M = Main(Orderings.KBO(MakeBlob(struct let cmp (a,_) (b,_) = compare a b end))) in
206         M.main goal hypotheses)
207     ;
208       (fun () ->     
209         let module M = Main(Orderings.LPO(MakeBlob(struct let cmp (a,_) (b,_) = compare a b end))) in
210         M.main goal hypotheses)
211     ];
212   let rec aux () =
213     if List.length !childs = 0 then
214       (fail_msg (); exit 1)
215     else 
216      match Unix.wait () with
217      | p, (Unix.WEXITED 0 as s) -> 
218          print_status p s;            
219          killall !childs; exit 0;
220      | p, s -> 
221          print_status p s;            
222          childs := List.filter ((<>)p) !childs; aux ()
223   in
224    aux ()
225 ;;
226
227 main ()