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)
17 type leaf = int * string
19 let cache = ref HC.empty
25 cache := HC.add s (!num,s) !cache;
33 let problem_file = ref "no-file-given";;
34 let tptppath = ref "/";;
35 let seconds = ref 300;;
38 print_endline ("% SZS status Timeout for " ^
39 Filename.basename !problem_file)
42 module type LeafComparer =
44 val cmp : leaf -> leaf -> int
48 module MakeBlob(C:LeafComparer) : Terms.Blob
49 with type t = leaf and type input = Ast.term = struct
52 let compare a b = C.cmp a b
56 let rec embed m = function
57 | Ast.Variable name ->
58 (try m, List.assoc name m
60 let t = Terms.Var ~-(List.length m) in
62 | Ast.Constant name -> m, Terms.Leaf (hash name)
63 | Ast.Function (name,args) ->
66 (fun x _ m -> embed m x) m args
68 m, Terms.Node (Terms.Leaf (hash name):: args)
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
76 let embed t = snd(embed [] t);;
81 let success_msg bag l (pp : ?margin:int -> leaf Terms.unit_clause -> string) ord =
82 (* TODO: do some sort of poor man lock (open + OEXCL) so that
83 * just one thread at a time prints the proof *)
84 print_endline ("% SZS status Unsatisfiable for " ^
85 Filename.basename !problem_file);
86 print_endline ("% SZS output start CNFRefutation for " ^
87 Filename.basename !problem_file);
90 let (cl,_,_) = Terms.get_from_bag x bag in
91 print_endline (pp ~margin:max_int
93 print_endline ("% SZS output end CNFRefutation for " ^
94 Filename.basename !problem_file);
95 let prefix = string_of_int (Unix.getpid ()) in
96 let prerr_endline s = prerr_endline (prefix ^ ": " ^ s) in
97 let times = Unix.times () in
98 prerr_endline ("solved " ^ !problem_file ^ " in " ^ string_of_float
99 (times.Unix.tms_utime +. times.Unix.tms_stime) ^ " using " ^ ord);
102 let start_msg stats passives g_passives (pp : ?margin:int -> leaf Terms.unit_clause -> string) oname =
103 let prefix = string_of_int (Unix.getpid ()) in
104 let prerr_endline s = prerr_endline (prefix ^ ": " ^ s) in
105 prerr_endline "Facts:";
106 List.iter (fun x -> prerr_endline (" " ^ pp x)) passives;
107 prerr_endline "Goal:";
108 prerr_endline (" " ^ pp g_passives);
109 prerr_endline "Order:";
110 prerr_endline (" " ^ oname);
111 prerr_endline "Leaf order:";
112 List.iter (fun ((_,name), (a,b,c,gp,l)) ->
113 prerr_endline (" " ^name ^ " " ^ string_of_int a ^ " " ^
114 string_of_int b ^ " " ^
115 string_of_int c ^ " " ^
116 String.concat "," (List.map string_of_int gp) ^
117 String.concat "," (List.map snd l))) stats;
120 let report_error s = prerr_endline (string_of_int (Unix.getpid())^": "^s);;
122 module Main(P : Paramod.Paramod with type t = leaf) = struct
124 let run stats goal hypotheses pp_unit_clause name =
125 let bag = Terms.empty_bag, 0 in
126 let bag, g_passives = P.mk_goal bag goal in
128 HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses
130 start_msg stats passives g_passives pp_unit_clause name;
133 ~max_steps:max_int bag ~g_passives:[g_passives] ~passives
135 | P.Error s -> report_error s; 3
136 | P.Unsatisfiable ((bag,_,l)::_) ->
137 success_msg bag l pp_unit_clause name; 0
138 | P.Unsatisfiable ([]) ->
139 report_error "Unsatisfiable but no solution output"; 3
145 let compute_stats goal hypotheses =
147 struct type t = leaf let cmp (a,_) (b,_) = Pervasives.compare a b end
149 let module B = MakeBlob(C) in
150 let module Pp = Pp.Pp(B) in
151 let module O = Orderings.NRKBO(B) in
152 let module P = Paramod.Paramod(O) in
153 let module Stats = Stats.Stats(O) in
154 let bag = Terms.empty_bag, 0 in
155 let bag, g_passives = P.mk_goal bag goal in
157 HExtlib.list_mapi_acc (fun x _ b -> P.mk_passive b x) bag hypotheses
159 let data = Stats.parse_symbols passives g_passives in
162 (fun (name, n_occ, arity, n_gocc, g_pos) ->
163 name, (n_occ, arity, n_gocc, g_pos, Stats.dependencies name passives))
167 (fun (n1,(o1,a1,go1,p1,d1)) (n2,(o2,a2,go2,p2,d2)) ->
169 (a1,o1,go1,p1,d1) (a2,o2,go2,p2,d2))
173 let worker order goal hypotheses =
174 let stats = compute_stats goal hypotheses in
178 let raw = List.map snd stats in
179 let rec pos x = function
180 | ((y,_)::tl) when y = x -> 0
181 | _::tl -> 1 + pos x tl
185 List.length (HExtlib.list_uniq raw)
187 (prerr_endline "NO CLASH, using fixed ground order";
193 (prerr_endline "CLASH, statistics insufficient";
194 fun (a,_) (b,_) -> Pervasives.compare a b)
198 let module B = MakeBlob(C) in
199 let module Pp = Pp.Pp(B) in
202 let module O = Orderings.NRKBO(B) in
203 let module Main = Main(Paramod.Paramod(O)) in
204 Main.run stats goal hypotheses Pp.pp_unit_clause O.name
206 let module O = Orderings.KBO(B) in
207 let module Main = Main(Paramod.Paramod(O)) in
208 Main.run stats goal hypotheses Pp.pp_unit_clause O.name
210 let module O = Orderings.LPO(B) in
211 let module Main = Main(Paramod.Paramod(O)) in
212 Main.run stats goal hypotheses Pp.pp_unit_clause O.name
216 let print_endline s = prerr_endline (string_of_int p ^ ": " ^ s) in
219 print_endline ("status Unsatisfiable for " ^
220 Filename.basename !problem_file);
222 print_endline ("status Timeout for " ^
223 Filename.basename !problem_file);
225 print_endline ("status GaveUp for " ^
226 Filename.basename !problem_file);
228 print_endline ("status Error for " ^
229 Filename.basename !problem_file);
230 | Unix.WEXITED _ -> assert false
231 | Unix.WSIGNALED s -> print_endline ("killed by signal " ^ string_of_int s)
232 | Unix.WSTOPPED _ -> print_endline "stopped"
236 List.iter (fun pid -> try Unix.kill pid 9 with _ -> ()) l
240 let childs = ref [] in
242 Sys.signal 24 (Sys.Signal_handle
243 (fun _ -> fail_msg (); killall !childs; exit 1))
246 Sys.signal Sys.sigalrm
247 (Sys.Signal_handle (fun _ -> fail_msg (); killall !childs; exit 1))
250 "--tptppath", Arg.String (fun p -> tptppath := p),
251 ("[path] TPTP lib root, default " ^ !tptppath);
252 "--timeout", Arg.Int (fun p -> seconds := p),
253 ("[seconds] timeout, default " ^ string_of_int !seconds);
254 ] (fun x -> problem_file := x) "
255 Matitaprover is the first order automatic prover that equips the
256 Matita interactive theorem prover (http://matita.cs.unibo.it).
258 Developed by A.Asperti, M.Denes and E.Tassi, released under GPL version 2
259 or at your option any later version.
261 If --tptppath is given, instead of the problem file you can just give the
262 problem name with the .p suffix (e.g. BOO001-1.p)
264 If --tptppath is not given, included files (i.e. axiom sets) are searched
265 in the current directory only.
267 usage: matitaprover [options] problemfile";
268 let hypotheses, goals = Tptp_cnf.parse ~tptppath:!tptppath !problem_file in
269 let goal = match goals with [x] -> x | _ -> assert false in
270 let _ = Unix.alarm !seconds in
274 let pid = Unix.fork () in
275 if pid = 0 then (exit (f ())) else pid)
277 (fun () -> worker `NRKBO goal hypotheses)
279 (fun () -> worker `KBO goal hypotheses)
281 (fun () -> worker `LPO goal hypotheses)
284 if List.length !childs = 0 then
285 (fail_msg (); exit 1)
287 match Unix.wait () with
288 | p, (Unix.WEXITED 0 as s) ->
290 killall !childs; exit 0;
293 childs := List.filter ((<>)p) !childs; aux ()