1 (* Title: HOL/Tools/atp_minimal.ML
2 Author: Philipp Meyer, TU Muenchen
4 Minimalization of theorem list for metis by using an external automated theorem prover
11 fun debug str = Output.debug (fn () => str)
12 fun debug_fn f = if !Output.debugging then f() else ()
13 fun answer str = Output.writeln str
14 fun println str = Output.priority str
16 fun order_unique name_list = OrdList.make (String.collate Char.compare) name_list
17 fun length_string namelist = Int.toString (length namelist)
19 fun print_names name_thms_pairs =
21 val names = (map fst name_thms_pairs)
22 val ordered = order_unique names
24 app (fn name => (debug (" " ^ name))) ordered
27 (* minimalization algorithm *)
29 fun isplit (l,r) [] = (l,r)
30 | isplit (l,r) (h::[]) = (h::l, r)
31 | isplit (l,r) (h1::h2::t) = isplit (h1::l, h2::r) t
33 fun split lst = isplit ([],[]) lst
37 fun min p sup [] = raise Empty
38 | min p sup [s0] = [s0]
41 val (l0, r0) = split s
50 val l = min p (sup @ r0) l0
51 val r = min p (sup @ l) r0
57 (* return a minimal subset v of s that satisfies p
58 @pre p(s) & ~p([]) & monotone(p)
59 @post v subset s & p(v) &
60 forall e in v. ~p(v \ e)
62 fun minimal p s = min p [] s
65 (* failure check and producing answer*)
66 datatype 'a prove_result = Success of 'a | Failure | Timeout | Error
68 val string_of_result = fn
69 Success _ => "Success"
70 | Failure => "Failure"
71 | Timeout => "Timeout"
75 [("SPASS beiseite: Ran out of time.", Timeout),
77 ("time limit exceeded", Timeout),
78 ("# Cannot determine problem status within resource limit", Timeout),
81 fun produce_answer (success, message, result_string, thm_name_vec) =
83 (Success (Vector.foldr op:: [] thm_name_vec), result_string)
86 val failure = get_first (fn (s, t) => if String.isSubstring s result_string then SOME (t, result_string) else NONE) failure_strings
88 if is_some failure then
91 (Failure, result_string)
94 (* wrapper for calling external prover *)
95 fun sh_test_thms prover prover_name time_limit subgoalno state name_thms_pairs =
97 val _ = println ("Testing " ^ (length_string name_thms_pairs) ^ " theorems... ")
98 val name_thm_pairs = flat (map (fn (n, ths) => map_index (fn (i, th) => (n, th)) ths) name_thms_pairs)
99 val _ = debug_fn (fn () => print_names name_thm_pairs)
100 val axclauses = ResAxioms.cnf_rules_pairs (Proof.theory_of state) name_thm_pairs
101 val (result, proof) =
102 (produce_answer (prover time_limit (SOME axclauses) prover_name subgoalno (Proof.get_goal state)))
103 val _ = println (string_of_result result)
109 (* minimalization of thms *)
110 fun minimalize prover prover_name time_limit state name_thms_pairs =
112 val _ = println ("Minimize called with " ^ (length_string name_thms_pairs) ^ " theorems, prover: "
113 ^ prover_name ^ ", time limit: " ^ (Int.toString time_limit) ^ " seconds")
114 val _ = debug_fn (fn () => app (fn (n, tl) => (debug n; app (fn t => debug (" " ^ Display.string_of_thm t)) tl)) name_thms_pairs)
115 val test_thms_fun = sh_test_thms prover prover_name time_limit 1 state
116 fun test_thms thms = case test_thms_fun thms of (Success _, _) => true | _ => false
118 (* try proove first to check result and get used theorems *)
119 (case test_thms_fun name_thms_pairs of
122 val ordered_used = order_unique used
124 if length ordered_used < length name_thms_pairs then
125 filter (fn (name1, _) => List.exists (equal name1) ordered_used) name_thms_pairs
128 val min_thms = (minimal test_thms to_use)
129 val min_names = order_unique (map fst min_thms)
130 val _ = println ("Minimal " ^ (length_string min_thms) ^ " theorems")
131 val _ = debug_fn (fn () => print_names min_thms)
133 answer ("Try this command: " ^ Markup.markup Markup.sendback ("apply (metis " ^ (space_implode " " min_names) ^ ")"))
136 answer ("Timeout: You may need to increase the time limit of " ^ (Int.toString time_limit) ^ " seconds. Call atp_minimize [time=...] ")
138 answer ("Error in prover: " ^ msg)
140 answer "Failure: No proof with the theorems supplied")
141 handle ResHolClause.TOO_TRIVIAL =>
142 answer ("Trivial: Try this command: " ^ Markup.markup Markup.sendback "apply metis")
144 answer ("Error: " ^ msg)
147 (* isar command and parsing input *)
149 local structure K = OuterKeyword and P = OuterParse and T = OuterLex in
151 fun get_thms context =
152 map (fn (name, interval) =>
154 val thmref = Facts.Named ((name, Position.none), interval)
155 val ths = ProofContext.get_fact context thmref
156 val name' = Facts.string_of_ref thmref
161 val default_prover = "remote_vampire"
162 val default_time_limit = 5
164 fun get_time_limit_arg time_string =
165 (case Int.fromString time_string of
167 | NONE => error ("Invalid time limit: " ^ quote time_string))
171 val def = (default_prover, default_time_limit)
173 foldl (fn ((name, a), (p, t)) => (case name of
174 "time" => (p, (get_time_limit_arg a))
176 | n => error ("Invalid argument: " ^ n))) def
179 fun sh_min_command args thm_names state =
181 val (prover_name, time_limit) = get_options args
183 case AtpManager.get_prover prover_name (Proof.theory_of state) of
184 SOME prover => prover
185 | NONE => error ("Unknown prover: " ^ quote prover_name)
186 val name_thms_pairs = get_thms (Proof.context_of state) thm_names
188 minimalize prover prover_name time_limit state name_thms_pairs
191 val parse_args = Scan.optional (Args.bracks (P.list (P.xname --| P.$$$ "=" -- P.xname) )) []
192 val parse_thm_names = Scan.repeat (P.xname -- Scan.option Attrib.thm_sel)
195 OuterSyntax.command "atp_minimize" "minimize theorem list with external prover" K.diag
196 (parse_args -- parse_thm_names >> (fn (args, thm_names) =>
197 Toplevel.no_timing o Toplevel.unknown_proof o Toplevel.keep ((sh_min_command args thm_names) o Toplevel.proof_of)))