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.
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_______________________________________________________________ *)
12 (* $Id: orderings.ml 9869 2009-06-11 22:52:38Z denes $ *)
14 module OT = struct type t = string let compare = Pervasives.compare end
15 module HC = Map.Make(OT)
16 module TS = HTopoSort.Make(OT)
18 type leaf = int * string
20 let cache = ref HC.empty
26 cache := HC.add s (!num,s) !cache;
34 let problem_file = ref "no-file-given";;
35 let tptppath = ref "/";;
36 let seconds = ref 300;;
39 print_endline ("% SZS status Timeout for " ^
40 Filename.basename !problem_file)
43 module type LeafComparer =
45 val cmp : leaf -> leaf -> int
49 let rec embed m = function
50 | Ast.Variable name ->
51 (try m, List.assoc name m
53 let t = Terms.Var ~-(List.length m) in
55 | Ast.Constant name -> m, Terms.Leaf (hash name)
56 | Ast.Function (name,args) ->
59 (fun x _ m -> embed m x) m args
61 m, Terms.Node (Terms.Leaf (hash name):: args)
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
72 let embed t = snd(embed [] t);;
75 module MakeBlob(C:LeafComparer) : Terms.Blob
76 with type t = leaf = struct
79 let compare a b = C.cmp a b
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);
95 let (cl,_,_) = Terms.get_from_bag x bag in
96 print_endline (pp ~margin:max_int
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);
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;
126 let report_error s = prerr_endline (string_of_int (Unix.getpid())^": "^s);;
128 module Main(O : Orderings.Blob with type t = leaf) = struct
129 module P = Paramod.Paramod(O)
130 module Utils = FoUtils.Utils(O)
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;
138 ~max_steps:max_int (g_t,g_ty) ~print_problem:printmsg hypotheses
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
150 (* let compute_stats goal hypotheses =
152 struct type t = leaf let cmp (a,_) (b,_) = Pervasives.compare a b end
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
162 HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses
164 let data = Stats.parse_symbols passives g_passives in
167 (fun (name, n_occ, arity, n_gocc, g_pos) ->
168 name, (n_occ, arity, n_gocc, g_pos, Stats.dependencies name passives))
171 let oplist = List.map (fun ((_,x),_) -> x) data in
173 (let _,(_,_,_,_,d) = List.find (fun ((_,op'),_) -> op = op') data
176 let oplist = TS.topological_sort oplist deps in
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)
188 let worker order ~useage ~printmsg goal hypotheses =
189 (*let stats = compute_stats goal hypotheses in*)
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
200 List.length (HExtlib.list_uniq raw)
202 (prerr_endline "NO CLASH, using fixed ground order";
208 (prerr_endline "CLASH, statistics insufficient";*)
209 fun (a,_) (b,_) -> Pervasives.compare a b(* ) *)
213 let module B = MakeBlob(C) in
214 let module Pp = Pp.Pp(B) in
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
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
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
232 let print_endline s = prerr_endline (string_of_int p ^ ": " ^ s) in
235 print_endline ("status Unsatisfiable for " ^
236 Filename.basename !problem_file);
238 print_endline ("status Timeout for " ^
239 Filename.basename !problem_file);
241 print_endline ("status GaveUp for " ^
242 Filename.basename !problem_file);
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"
252 List.iter (fun pid -> try Unix.kill pid 9 with _ -> ()) l
256 let childs = ref [] in
258 Sys.signal 24 (Sys.Signal_handle
259 (fun _ -> fail_msg (); killall !childs; exit 1))
262 Sys.signal Sys.sigalrm
263 (Sys.Signal_handle (fun _ -> fail_msg (); killall !childs; exit 1))
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).
274 Developed by A.Asperti, M.Denes and E.Tassi, released under GPL version 2
275 or at your option any later version.
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)
280 If --tptppath is not given, included files (i.e. axiom sets) are searched
281 in the current directory only.
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
290 let pid = Unix.fork () in
291 if pid = 0 then (exit (f ())) else pid)
293 (fun () -> worker `NRKBO ~useage:true ~printmsg:true goal hypotheses)
295 (fun () -> worker `KBO ~useage:true ~printmsg:false goal hypotheses)
297 (fun () -> worker `LPO ~useage:true ~printmsg:false goal hypotheses)
299 (fun () -> worker `NRKBO ~useage:false ~printmsg:false goal hypotheses)
302 if List.length !childs = 0 then
303 (fail_msg (); exit 1)
305 match Unix.wait () with
306 | p, (Unix.WEXITED 0 as s) ->
308 killall !childs; exit 0;
311 childs := List.filter ((<>)p) !childs; aux ()