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 "/";;
39 print_endline ("% SZS status Timeout for " ^
40 Filename.basename !problem_file)
43 module type LeafComparer =
45 val cmp : leaf -> leaf -> int
49 module MakeBlob(C:LeafComparer) : Terms.Blob
50 with type t = leaf and type input = Ast.term = struct
53 let compare a b = C.cmp a b
57 let rec embed m = function
58 | Ast.Variable name ->
59 (try m, List.assoc name m
61 let t = Terms.Var ~-(List.length m) in
63 | Ast.Constant name -> m, Terms.Leaf (hash name)
64 | Ast.Function (name,args) ->
67 (fun x _ m -> embed m x) m args
69 m, Terms.Node (Terms.Leaf (hash name):: args)
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
77 let embed t = snd(embed [] t);;
82 let success_msg bag l (pp : ?margin:int -> leaf Terms.unit_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);
91 let (cl,_,_) = Terms.get_from_bag x bag in
92 print_endline (pp ~margin:max_int
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) ^ "(Process Time) using " ^ ord);
103 let start_msg stats passives g_passives (pp : ?margin:int -> leaf Terms.unit_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;*)
121 let report_error s = prerr_endline (string_of_int (Unix.getpid())^": "^s);;
124 module Main(P : Paramod.Paramod with type t = leaf) = struct
126 (*let mk_clause bag maxvar (t,ty) =
127 let (proof,ty) = B.saturate t ty in
128 let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
129 let bag, c = Terms.add_to_bag c bag in
133 let mk_passive (bag,maxvar) = mk_clause bag maxvar;;
134 let mk_goal (bag,maxvar) = mk_clause bag maxvar;;*)
136 let run ~useage ~printmsg stats goal hypotheses pp_unit_clause name =
137 let bag = Terms.empty_bag, 0 in
138 let bag, g_passives = P.mk_goal bag goal in
140 HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses
142 if printmsg then start_msg stats passives g_passives pp_unit_clause name;
145 ~max_steps:max_int bag ~g_passives:[g_passives] ~passives
147 | P.Error s -> report_error s; 3
148 | P.Unsatisfiable ((bag,_,l)::_) ->
149 success_msg bag l pp_unit_clause name; 0
150 | P.Unsatisfiable ([]) ->
151 report_error "Unsatisfiable but no solution output"; 3
157 let compute_stats goal hypotheses =
159 struct type t = leaf let cmp (a,_) (b,_) = Pervasives.compare a b end
161 let module B = MakeBlob(C) in
162 let module Pp = Pp.Pp(B) in
163 let module O = Orderings.NRKBO(B) in
164 let module P = Paramod.Paramod(O) in
165 let module Stats = Stats.Stats(O) in
166 let bag = Terms.empty_bag, 0 in
167 let bag, g_passives = P.mk_goal bag goal in
169 HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses
171 let data = Stats.parse_symbols passives g_passives in
174 (fun (name, n_occ, arity, n_gocc, g_pos) ->
175 name, (n_occ, arity, n_gocc, g_pos, Stats.dependencies name passives))
178 let oplist = List.map (fun ((_,x),_) -> x) data in
180 (let _,(_,_,_,_,d) = List.find (fun ((_,op'),_) -> op = op') data
183 let oplist = TS.topological_sort oplist deps in
185 (fun ((_,n1),(o1,a1,go1,p1,_)) ((_,n2),(o2,a2,go2,p2,_)) ->
186 if a1 = 0 && a2 = 0 then 0
187 else if a1 = 0 then -1
188 else if a2 = 0 then 1
189 else let res = Pervasives.compare (a1,o1,-go1,p1) (a2,o2,-go2,p2)
190 in if res = 0 then Pervasives.compare (HExtlib.list_index ((=) n1) oplist) (HExtlib.list_index ((=) n2) oplist)
195 let worker order ~useage ~printmsg goal hypotheses =
196 let stats = compute_stats goal hypotheses in
200 let raw = List.map snd stats in
201 let rec pos x = function
202 | ((y,_)::tl) when y = x -> 0
203 | _::tl -> 1 + pos x tl
207 List.length (HExtlib.list_uniq raw)
209 ((*prerr_endline "NO CLASH, using fixed ground order";*)
215 ((*prerr_endline "CLASH, statistics insufficient";*)
216 fun (a,_) (b,_) -> Pervasives.compare a b)
220 let module B = MakeBlob(C) in
221 let module Pp = Pp.Pp(B) in
224 let module O = Orderings.NRKBO(B) in
225 let module Main = Main(Paramod.Paramod(O)) in
226 Main.run ~useage ~printmsg stats goal hypotheses Pp.pp_unit_clause O.name
228 let module O = Orderings.KBO(B) in
229 let module Main = Main(Paramod.Paramod(O)) in
230 Main.run ~useage ~printmsg stats goal hypotheses Pp.pp_unit_clause O.name
232 let module O = Orderings.LPO(B) in
233 let module Main = Main(Paramod.Paramod(O)) in
234 Main.run ~useage ~printmsg stats goal hypotheses Pp.pp_unit_clause O.name
238 let print_endline s = () in (* prerr_endline (string_of_int p ^ ": " ^ s) in*)
241 print_endline ("status Unsatisfiable for " ^
242 Filename.basename !problem_file);
244 print_endline ("status Timeout for " ^
245 Filename.basename !problem_file);
247 print_endline ("status GaveUp for " ^
248 Filename.basename !problem_file);
250 print_endline ("status Error for " ^
251 Filename.basename !problem_file);
252 | Unix.WEXITED _ -> assert false
253 | Unix.WSIGNALED s -> print_endline ("killed by signal " ^ string_of_int s)
254 | Unix.WSTOPPED _ -> print_endline "stopped"
258 List.iter (fun pid -> try Unix.kill pid 9 with _ -> ()) l
262 let childs = ref [] in
264 Sys.signal 24 (Sys.Signal_handle
265 (fun _ -> fail_msg (); killall !childs; exit 1))
268 Sys.signal Sys.sigalrm
269 (Sys.Signal_handle (fun _ -> prerr_endline "Alarm!"; fail_msg (); killall !childs; exit 1))
272 "--tptppath", Arg.String (fun p -> tptppath := p),
273 ("[path] TPTP lib root, default " ^ !tptppath);
274 "--timeout", Arg.Int (fun p -> seconds := p),
275 ("[seconds] timeout, default none");
276 ] (fun x -> problem_file := x) "
277 Matitaprover is the first order automatic prover that equips the
278 Matita interactive theorem prover (http://matita.cs.unibo.it).
280 Developed by A.Asperti, M.Denes and E.Tassi, released under GPL version 2
281 or at your option any later version.
283 If --tptppath is given, instead of the problem file you can just give the
284 problem name with the .p suffix (e.g. BOO001-1.p)
286 If --tptppath is not given, included files (i.e. axiom sets) are searched
287 in the current directory only.
289 usage: matitaprover [options] problemfile";
290 let hypotheses, goals = Tptp_cnf.parse ~tptppath:!tptppath !problem_file in
291 let goal = match goals with [x] -> x | _ -> assert false in
292 let _ = if !seconds > 0 then Unix.alarm !seconds else 0 in
296 let pid = Unix.fork () in
297 if pid = 0 then (exit (f ())) else pid)
299 (fun () -> worker `NRKBO ~useage:true ~printmsg:true goal hypotheses)
301 (fun () -> worker `KBO ~useage:true ~printmsg:false goal hypotheses)
303 (fun () -> worker `LPO ~useage:true ~printmsg:false goal hypotheses)
305 (fun () -> worker `NRKBO ~useage:false ~printmsg:false goal hypotheses)
308 if List.length !childs = 0 then
309 (fail_msg (); exit 1)
311 match Unix.wait () with
312 | p, (Unix.WEXITED 0 as s) ->
314 killall !childs; exit 0;
317 childs := List.filter ((<>)p) !childs; aux ()