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 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) ^ " 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);;
123 module Main(P : Paramod.Paramod with type t = leaf) = struct
125 let run ~useage ~printmsg stats goal hypotheses pp_unit_clause name =
126 let bag = Terms.empty_bag, 0 in
127 let bag, g_passives = P.mk_goal bag goal in
129 HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses
131 if printmsg then start_msg stats passives g_passives pp_unit_clause name;
134 ~max_steps:max_int bag ~g_passives:[g_passives] ~passives
136 | P.Error s -> report_error s; 3
137 | P.Unsatisfiable ((bag,_,l)::_) ->
138 success_msg bag l pp_unit_clause name; 0
139 | P.Unsatisfiable ([]) ->
140 report_error "Unsatisfiable but no solution output"; 3
146 let compute_stats goal hypotheses =
148 struct type t = leaf let cmp (a,_) (b,_) = Pervasives.compare a b end
150 let module B = MakeBlob(C) in
151 let module Pp = Pp.Pp(B) in
152 let module O = Orderings.NRKBO(B) in
153 let module P = Paramod.Paramod(O) in
154 let module Stats = Stats.Stats(O) in
155 let bag = Terms.empty_bag, 0 in
156 let bag, g_passives = P.mk_goal bag goal in
158 HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses
160 let data = Stats.parse_symbols passives g_passives in
163 (fun (name, n_occ, arity, n_gocc, g_pos) ->
164 name, (n_occ, arity, n_gocc, g_pos, Stats.dependencies name passives))
167 let oplist = List.map (fun ((_,x),_) -> x) data in
169 (let _,(_,_,_,_,d) = List.find (fun ((_,op'),_) -> op = op') data
172 let oplist = TS.topological_sort oplist deps in
174 (fun ((_,n1),(o1,a1,go1,p1,_)) ((_,n2),(o2,a2,go2,p2,_)) ->
175 if a1 = 0 && a2 = 0 then 0
176 else if a1 = 0 then -1
177 else if a2 = 0 then 1
178 else let res = Pervasives.compare (a1,o1,-go1,p1) (a2,o2,-go2,p2)
179 in if res = 0 then Pervasives.compare (HExtlib.list_index ((=) n1) oplist) (HExtlib.list_index ((=) n2) oplist)
184 let worker order ~useage ~printmsg goal hypotheses =
185 let stats = compute_stats goal hypotheses in
189 let raw = List.map snd stats in
190 let rec pos x = function
191 | ((y,_)::tl) when y = x -> 0
192 | _::tl -> 1 + pos x tl
196 List.length (HExtlib.list_uniq raw)
198 ((*prerr_endline "NO CLASH, using fixed ground order";*)
204 ((*prerr_endline "CLASH, statistics insufficient";*)
205 fun (a,_) (b,_) -> Pervasives.compare a b)
209 let module B = MakeBlob(C) in
210 let module Pp = Pp.Pp(B) in
213 let module O = Orderings.NRKBO(B) in
214 let module Main = Main(Paramod.Paramod(O)) in
215 Main.run ~useage ~printmsg stats goal hypotheses Pp.pp_unit_clause O.name
217 let module O = Orderings.KBO(B) in
218 let module Main = Main(Paramod.Paramod(O)) in
219 Main.run ~useage ~printmsg stats goal hypotheses Pp.pp_unit_clause O.name
221 let module O = Orderings.LPO(B) in
222 let module Main = Main(Paramod.Paramod(O)) in
223 Main.run ~useage ~printmsg stats goal hypotheses Pp.pp_unit_clause O.name
227 let print_endline s = () in (* prerr_endline (string_of_int p ^ ": " ^ s) in*)
230 print_endline ("status Unsatisfiable for " ^
231 Filename.basename !problem_file);
233 print_endline ("status Timeout for " ^
234 Filename.basename !problem_file);
236 print_endline ("status GaveUp for " ^
237 Filename.basename !problem_file);
239 print_endline ("status Error for " ^
240 Filename.basename !problem_file);
241 | Unix.WEXITED _ -> assert false
242 | Unix.WSIGNALED s -> print_endline ("killed by signal " ^ string_of_int s)
243 | Unix.WSTOPPED _ -> print_endline "stopped"
247 List.iter (fun pid -> try Unix.kill pid 9 with _ -> ()) l
251 let childs = ref [] in
253 Sys.signal 24 (Sys.Signal_handle
254 (fun _ -> fail_msg (); killall !childs; exit 1))
257 Sys.signal Sys.sigalrm
258 (Sys.Signal_handle (fun _ -> fail_msg (); killall !childs; exit 1))
261 "--tptppath", Arg.String (fun p -> tptppath := p),
262 ("[path] TPTP lib root, default " ^ !tptppath);
263 "--timeout", Arg.Int (fun p -> seconds := p),
264 ("[seconds] timeout, default " ^ string_of_int !seconds);
265 ] (fun x -> problem_file := x) "
266 Matitaprover is the first order automatic prover that equips the
267 Matita interactive theorem prover (http://matita.cs.unibo.it).
269 Developed by A.Asperti, M.Denes and E.Tassi, released under GPL version 2
270 or at your option any later version.
272 If --tptppath is given, instead of the problem file you can just give the
273 problem name with the .p suffix (e.g. BOO001-1.p)
275 If --tptppath is not given, included files (i.e. axiom sets) are searched
276 in the current directory only.
278 usage: matitaprover [options] problemfile";
279 let hypotheses, goals = Tptp_cnf.parse ~tptppath:!tptppath !problem_file in
280 let goal = match goals with [x] -> x | _ -> assert false in
281 let _ = Unix.alarm !seconds in
285 let pid = Unix.fork () in
286 if pid = 0 then (exit (f ())) else pid)
288 (fun () -> worker `NRKBO ~useage:true ~printmsg:true goal hypotheses)
290 (fun () -> worker `KBO ~useage:true ~printmsg:false goal hypotheses)
292 (fun () -> worker `LPO ~useage:true ~printmsg:false goal hypotheses)
294 (fun () -> worker `NRKBO ~useage:false ~printmsg:false goal hypotheses)
297 if List.length !childs = 0 then
298 (fail_msg (); exit 1)
300 match Unix.wait () with
301 | p, (Unix.WEXITED 0 as s) ->
303 killall !childs; exit 0;
306 childs := List.filter ((<>)p) !childs; aux ()