author | blanchet |
Tue, 27 Jul 2010 19:41:19 +0200 | |
changeset 38028 | 22dcaec5fa77 |
parent 38023 | 962b0a7f544b |
child 38032 | 54448f5d151f |
permissions | -rw-r--r-- |
38021
e024504943d1
rename "ATP_Manager" ML module to "Sledgehammer";
blanchet
parents:
38020
diff
changeset
|
1 |
(* Title: HOL/Tools/Sledgehammer/sledgehammer.ML |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
2 |
Author: Fabian Immler, TU Muenchen |
32996
d2e48879e65a
removed disjunctive group cancellation -- provers run independently;
wenzelm
parents:
32995
diff
changeset
|
3 |
Author: Makarius |
35969 | 4 |
Author: Jasmin Blanchette, TU Muenchen |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
5 |
|
38021
e024504943d1
rename "ATP_Manager" ML module to "Sledgehammer";
blanchet
parents:
38020
diff
changeset
|
6 |
Sledgehammer's heart. |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
7 |
*) |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
8 |
|
38021
e024504943d1
rename "ATP_Manager" ML module to "Sledgehammer";
blanchet
parents:
38020
diff
changeset
|
9 |
signature SLEDGEHAMMER = |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
10 |
sig |
38023 | 11 |
type failure = ATP_Systems.failure |
35969 | 12 |
type relevance_override = Sledgehammer_Fact_Filter.relevance_override |
36281
dbbf4d5d584d
pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents:
36235
diff
changeset
|
13 |
type minimize_command = Sledgehammer_Proof_Reconstruct.minimize_command |
35969 | 14 |
type params = |
15 |
{debug: bool, |
|
16 |
verbose: bool, |
|
36143
6490319b1703
added "overlord" option (to get easy access to output files for debugging) + systematically use "raw_goal" rather than an inconsistent mixture
blanchet
parents:
36064
diff
changeset
|
17 |
overlord: bool, |
35969 | 18 |
atps: string list, |
19 |
full_types: bool, |
|
36235
61159615a0c5
added "explicit_apply" option to Sledgehammer, to control whether an explicit apply function should be used as much or as little as possible (replaces a previous global variable)
blanchet
parents:
36231
diff
changeset
|
20 |
explicit_apply: bool, |
35969 | 21 |
relevance_threshold: real, |
36922 | 22 |
relevance_convergence: real, |
36220
f3655a3ae1ab
rename Sledgehammer "theory_const" option to "theory_relevant", now that I understand better what it does
blanchet
parents:
36184
diff
changeset
|
23 |
theory_relevant: bool option, |
36922 | 24 |
defs_relevant: bool, |
35969 | 25 |
isar_proof: bool, |
36924 | 26 |
isar_shrink_factor: int, |
35969 | 27 |
timeout: Time.time, |
28 |
minimize_timeout: Time.time} |
|
35867 | 29 |
type problem = |
35969 | 30 |
{subgoal: int, |
31 |
goal: Proof.context * (thm list * thm), |
|
32 |
relevance_override: relevance_override, |
|
38002
31705eccee23
get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents:
37996
diff
changeset
|
33 |
axiom_clauses: (string * thm) list option, |
31705eccee23
get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents:
37996
diff
changeset
|
34 |
filtered_clauses: (string * thm) list option} |
35867 | 35 |
type prover_result = |
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
36 |
{outcome: failure option, |
35969 | 37 |
message: string, |
37926
e6ff246c0cdb
renamings + only need second component of name pool to reconstruct proofs
blanchet
parents:
37627
diff
changeset
|
38 |
pool: string Symtab.table, |
38015 | 39 |
used_thm_names: string list, |
35969 | 40 |
atp_run_time_in_msecs: int, |
36369
d2cd0d04b8e6
handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents:
36289
diff
changeset
|
41 |
output: string, |
35969 | 42 |
proof: string, |
43 |
internal_thm_names: string Vector.vector, |
|
37996
11c076ea92e9
simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents:
37994
diff
changeset
|
44 |
conjecture_shape: int list, |
38002
31705eccee23
get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents:
37996
diff
changeset
|
45 |
filtered_clauses: (string * thm) list} |
36281
dbbf4d5d584d
pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents:
36235
diff
changeset
|
46 |
type prover = |
dbbf4d5d584d
pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents:
36235
diff
changeset
|
47 |
params -> minimize_command -> Time.time -> problem -> prover_result |
35867 | 48 |
|
38023 | 49 |
val dest_dir : string Config.T |
50 |
val problem_prefix : string Config.T |
|
51 |
val measure_runtime : bool Config.T |
|
35969 | 52 |
val kill_atps: unit -> unit |
53 |
val running_atps: unit -> unit |
|
29112
f2b45eea6dac
added 'atp_messages' command, which displays recent messages synchronously;
wenzelm
parents:
28835
diff
changeset
|
54 |
val messages: int option -> unit |
38023 | 55 |
val get_prover_fun : theory -> string -> prover |
37584 | 56 |
val start_prover_thread : |
57 |
params -> int -> int -> relevance_override -> (string -> minimize_command) |
|
58 |
-> Proof.state -> string -> unit |
|
38023 | 59 |
val setup : theory -> theory |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
60 |
end; |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
61 |
|
38021
e024504943d1
rename "ATP_Manager" ML module to "Sledgehammer";
blanchet
parents:
38020
diff
changeset
|
62 |
structure Sledgehammer : SLEDGEHAMMER = |
28477
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
63 |
struct |
9339d4dcec8b
version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff
changeset
|
64 |
|
38028 | 65 |
open ATP_Problem |
66 |
open ATP_Systems |
|
37578
9367cb36b1c4
renamed "Sledgehammer_FOL_Clauses" to "Metis_Clauses", so that Metis doesn't depend on Sledgehammer
blanchet
parents:
37577
diff
changeset
|
67 |
open Metis_Clauses |
38023 | 68 |
open Sledgehammer_Util |
36063
cdc6855a6387
make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents:
36059
diff
changeset
|
69 |
open Sledgehammer_Fact_Filter |
cdc6855a6387
make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents:
36059
diff
changeset
|
70 |
open Sledgehammer_Proof_Reconstruct |
37583
9ce2451647d5
factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
37581
diff
changeset
|
71 |
|
38023 | 72 |
|
37583
9ce2451647d5
factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
37581
diff
changeset
|
73 |
(** The Sledgehammer **) |
9ce2451647d5
factored non-ATP specific code from "ATP_Manager" out, so that it can be reused for the LEO-II integration
blanchet
parents:
37581
diff
changeset
|
74 |
|
37585 | 75 |
val das_Tool = "Sledgehammer" |
76 |
||
77 |
fun kill_atps () = Async_Manager.kill_threads das_Tool "ATPs" |
|
78 |
fun running_atps () = Async_Manager.running_threads das_Tool "ATPs" |
|
79 |
val messages = Async_Manager.thread_messages das_Tool "ATP" |
|
35969 | 80 |
|
36281
dbbf4d5d584d
pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents:
36235
diff
changeset
|
81 |
(** problems, results, provers, etc. **) |
35969 | 82 |
|
83 |
type params = |
|
84 |
{debug: bool, |
|
85 |
verbose: bool, |
|
36143
6490319b1703
added "overlord" option (to get easy access to output files for debugging) + systematically use "raw_goal" rather than an inconsistent mixture
blanchet
parents:
36064
diff
changeset
|
86 |
overlord: bool, |
35969 | 87 |
atps: string list, |
88 |
full_types: bool, |
|
36235
61159615a0c5
added "explicit_apply" option to Sledgehammer, to control whether an explicit apply function should be used as much or as little as possible (replaces a previous global variable)
blanchet
parents:
36231
diff
changeset
|
89 |
explicit_apply: bool, |
35969 | 90 |
relevance_threshold: real, |
36922 | 91 |
relevance_convergence: real, |
36220
f3655a3ae1ab
rename Sledgehammer "theory_const" option to "theory_relevant", now that I understand better what it does
blanchet
parents:
36184
diff
changeset
|
92 |
theory_relevant: bool option, |
36922 | 93 |
defs_relevant: bool, |
35969 | 94 |
isar_proof: bool, |
36924 | 95 |
isar_shrink_factor: int, |
35969 | 96 |
timeout: Time.time, |
97 |
minimize_timeout: Time.time} |
|
35867 | 98 |
|
99 |
type problem = |
|
35969 | 100 |
{subgoal: int, |
101 |
goal: Proof.context * (thm list * thm), |
|
102 |
relevance_override: relevance_override, |
|
38002
31705eccee23
get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents:
37996
diff
changeset
|
103 |
axiom_clauses: (string * thm) list option, |
31705eccee23
get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents:
37996
diff
changeset
|
104 |
filtered_clauses: (string * thm) list option} |
35867 | 105 |
|
106 |
type prover_result = |
|
36370
a4f601daa175
centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents:
36369
diff
changeset
|
107 |
{outcome: failure option, |
35969 | 108 |
message: string, |
37926
e6ff246c0cdb
renamings + only need second component of name pool to reconstruct proofs
blanchet
parents:
37627
diff
changeset
|
109 |
pool: string Symtab.table, |
38015 | 110 |
used_thm_names: string list, |
35969 | 111 |
atp_run_time_in_msecs: int, |
36369
d2cd0d04b8e6
handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents:
36289
diff
changeset
|
112 |
output: string, |
35969 | 113 |
proof: string, |
114 |
internal_thm_names: string Vector.vector, |
|
37996
11c076ea92e9
simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents:
37994
diff
changeset
|
115 |
conjecture_shape: int list, |
38002
31705eccee23
get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents:
37996
diff
changeset
|
116 |
filtered_clauses: (string * thm) list} |
35867 | 117 |
|
36281
dbbf4d5d584d
pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents:
36235
diff
changeset
|
118 |
type prover = |
dbbf4d5d584d
pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents:
36235
diff
changeset
|
119 |
params -> minimize_command -> Time.time -> problem -> prover_result |
35867 | 120 |
|
38023 | 121 |
(* configuration attributes *) |
122 |
||
123 |
val (dest_dir, dest_dir_setup) = Attrib.config_string "atp_dest_dir" (K ""); |
|
124 |
(*Empty string means create files in Isabelle's temporary files directory.*) |
|
125 |
||
126 |
val (problem_prefix, problem_prefix_setup) = |
|
127 |
Attrib.config_string "atp_problem_prefix" (K "prob"); |
|
128 |
||
129 |
val (measure_runtime, measure_runtime_setup) = |
|
130 |
Attrib.config_bool "atp_measure_runtime" (K false); |
|
28484 | 131 |
|
38023 | 132 |
fun with_path cleanup after f path = |
133 |
Exn.capture f path |
|
134 |
|> tap (fn _ => cleanup path) |
|
135 |
|> Exn.release |
|
136 |
|> tap (after path) |
|
137 |
||
138 |
(* Splits by the first possible of a list of delimiters. *) |
|
139 |
fun extract_proof delims output = |
|
140 |
case pairself (find_first (fn s => String.isSubstring s output)) |
|
141 |
(ListPair.unzip delims) of |
|
142 |
(SOME begin_delim, SOME end_delim) => |
|
143 |
(output |> first_field begin_delim |> the |> snd |
|
144 |
|> first_field end_delim |> the |> fst |
|
145 |
|> first_field "\n" |> the |> snd |
|
146 |
handle Option.Option => "") |
|
147 |
| _ => "" |
|
28484 | 148 |
|
38023 | 149 |
fun extract_proof_and_outcome complete res_code proof_delims known_failures |
150 |
output = |
|
151 |
case map_filter (fn (failure, pattern) => |
|
152 |
if String.isSubstring pattern output then SOME failure |
|
153 |
else NONE) known_failures of |
|
154 |
[] => (case extract_proof proof_delims output of |
|
155 |
"" => ("", SOME UnknownError) |
|
156 |
| proof => if res_code = 0 then (proof, NONE) |
|
157 |
else ("", SOME UnknownError)) |
|
158 |
| (failure :: _) => |
|
159 |
("", SOME (if failure = IncompleteUnprovable andalso complete then |
|
160 |
Unprovable |
|
161 |
else |
|
162 |
failure)) |
|
28582 | 163 |
|
38023 | 164 |
fun string_for_failure Unprovable = "The ATP problem is unprovable." |
165 |
| string_for_failure IncompleteUnprovable = |
|
166 |
"The ATP cannot prove the problem." |
|
167 |
| string_for_failure CantConnect = "Can't connect to remote ATP." |
|
168 |
| string_for_failure TimedOut = "Timed out." |
|
169 |
| string_for_failure OutOfResources = "The ATP ran out of resources." |
|
170 |
| string_for_failure OldSpass = |
|
171 |
(* FIXME: Change the error message below to point to the Isabelle download |
|
172 |
page once the package is there. *) |
|
173 |
"Warning: Sledgehammer requires a more recent version of SPASS with \ |
|
174 |
\support for the TPTP syntax. To install it, download and untar the \ |
|
175 |
\package \"http://isabelle.in.tum.de/~blanchet/spass-3.7.tgz\" and add the \ |
|
176 |
\\"spass-3.7\" directory's full path to \"" ^ |
|
177 |
Path.implode (Path.expand (Path.appends |
|
178 |
(Path.variable "ISABELLE_HOME_USER" :: |
|
179 |
map Path.basic ["etc", "components"]))) ^ |
|
180 |
"\" on a line of its own." |
|
181 |
| string_for_failure MalformedInput = |
|
182 |
"Internal Sledgehammer error: The ATP problem is malformed. Please report \ |
|
183 |
\this to the Isabelle developers." |
|
184 |
| string_for_failure MalformedOutput = "Error: The ATP output is malformed." |
|
185 |
| string_for_failure UnknownError = "Error: An unknown ATP error occurred." |
|
28571 | 186 |
|
28586
d238b83ba3fc
renamed kill_all to kill, in conformance with atp_kill command;
wenzelm
parents:
28582
diff
changeset
|
187 |
|
38023 | 188 |
(* Clause preparation *) |
189 |
||
190 |
datatype fol_formula = |
|
191 |
FOLFormula of {formula_name: string, |
|
192 |
kind: kind, |
|
193 |
combformula: (name, combterm) formula, |
|
194 |
ctypes_sorts: typ list} |
|
195 |
||
196 |
fun mk_anot phi = AConn (ANot, [phi]) |
|
197 |
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2]) |
|
198 |
fun mk_ahorn [] phi = phi |
|
199 |
| mk_ahorn (phi :: phis) psi = |
|
200 |
AConn (AImplies, [fold (mk_aconn AAnd) phis phi, psi]) |
|
201 |
||
202 |
(* ### FIXME: reintroduce |
|
203 |
fun make_clause_table xs = |
|
204 |
fold (Termtab.update o `(prop_of o snd)) xs Termtab.empty |
|
205 |
(* Remove existing axiom clauses from the conjecture clauses, as this can |
|
206 |
dramatically boost an ATP's performance (for some reason). *) |
|
207 |
fun subtract_cls ax_clauses = |
|
208 |
filter_out (Termtab.defined (make_clause_table ax_clauses) o prop_of) |
|
209 |
*) |
|
210 |
||
211 |
fun combformula_for_prop thy = |
|
212 |
let |
|
213 |
val do_term = combterm_from_term thy |
|
214 |
fun do_quant bs q s T t' = |
|
215 |
do_formula ((s, T) :: bs) t' |
|
216 |
#>> (fn phi => AQuant (q, [`make_bound_var s], phi)) |
|
217 |
and do_conn bs c t1 t2 = |
|
218 |
do_formula bs t1 ##>> do_formula bs t2 |
|
219 |
#>> (fn (phi1, phi2) => AConn (c, [phi1, phi2])) |
|
220 |
and do_formula bs t = |
|
221 |
case t of |
|
222 |
@{const Not} $ t1 => |
|
223 |
do_formula bs t1 #>> (fn phi => AConn (ANot, [phi])) |
|
224 |
| Const (@{const_name All}, _) $ Abs (s, T, t') => |
|
225 |
do_quant bs AForall s T t' |
|
226 |
| Const (@{const_name Ex}, _) $ Abs (s, T, t') => |
|
227 |
do_quant bs AExists s T t' |
|
228 |
| @{const "op &"} $ t1 $ t2 => do_conn bs AAnd t1 t2 |
|
229 |
| @{const "op |"} $ t1 $ t2 => do_conn bs AOr t1 t2 |
|
230 |
| @{const "op -->"} $ t1 $ t2 => do_conn bs AImplies t1 t2 |
|
231 |
| Const (@{const_name "op ="}, Type (_, [@{typ bool}, _])) $ t1 $ t2 => |
|
232 |
do_conn bs AIff t1 t2 |
|
233 |
| _ => (fn ts => do_term bs (Envir.eta_contract t) |
|
234 |
|>> APred ||> union (op =) ts) |
|
235 |
in do_formula [] end |
|
236 |
||
237 |
(* Converts an elim-rule into an equivalent theorem that does not have the |
|
238 |
predicate variable. Leaves other theorems unchanged. We simply instantiate |
|
239 |
the conclusion variable to False. (Cf. "transform_elim_term" in |
|
240 |
"ATP_Systems".) *) |
|
241 |
(* FIXME: test! *) |
|
242 |
fun transform_elim_term t = |
|
243 |
case Logic.strip_imp_concl t of |
|
244 |
@{const Trueprop} $ Var (z, @{typ bool}) => |
|
245 |
subst_Vars [(z, @{const True})] t |
|
246 |
| Var (z, @{typ prop}) => subst_Vars [(z, @{prop True})] t |
|
247 |
| _ => t |
|
248 |
||
249 |
(* Removes the lambdas from an equation of the form "t = (%x. u)". |
|
250 |
(Cf. "extensionalize_theorem" in "Clausifier".) *) |
|
251 |
fun extensionalize_term t = |
|
252 |
let |
|
253 |
fun aux j (Const (@{const_name "op ="}, Type (_, [Type (_, [_, T']), _])) |
|
254 |
$ t2 $ Abs (s, var_T, t')) = |
|
255 |
let val var_t = Var (("x", j), var_T) in |
|
256 |
Const (@{const_name "op ="}, T' --> T' --> HOLogic.boolT) |
|
257 |
$ betapply (t2, var_t) $ subst_bound (var_t, t') |
|
258 |
|> aux (j + 1) |
|
259 |
end |
|
260 |
| aux _ t = t |
|
261 |
in aux (maxidx_of_term t + 1) t end |
|
262 |
||
263 |
(* FIXME: Guarantee freshness *) |
|
264 |
fun concealed_bound_name j = "Sledgehammer" ^ Int.toString j |
|
265 |
fun conceal_bounds Ts t = |
|
266 |
subst_bounds (map (Free o apfst concealed_bound_name) |
|
267 |
(length Ts - 1 downto 0 ~~ rev Ts), t) |
|
268 |
fun reveal_bounds Ts = |
|
269 |
subst_atomic (map (fn (j, T) => (Free (concealed_bound_name j, T), Bound j)) |
|
270 |
(0 upto length Ts - 1 ~~ Ts)) |
|
271 |
||
272 |
fun introduce_combinators_in_term ctxt kind t = |
|
273 |
let |
|
274 |
val thy = ProofContext.theory_of ctxt |
|
275 |
fun aux Ts t = |
|
276 |
case t of |
|
277 |
@{const Not} $ t1 => @{const Not} $ aux Ts t1 |
|
278 |
| (t0 as Const (@{const_name All}, _)) $ Abs (s, T, t') => |
|
279 |
t0 $ Abs (s, T, aux (T :: Ts) t') |
|
280 |
| (t0 as Const (@{const_name Ex}, _)) $ Abs (s, T, t') => |
|
281 |
t0 $ Abs (s, T, aux (T :: Ts) t') |
|
282 |
| (t0 as @{const "op &"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2 |
|
283 |
| (t0 as @{const "op |"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2 |
|
284 |
| (t0 as @{const "op -->"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2 |
|
285 |
| (t0 as Const (@{const_name "op ="}, Type (_, [@{typ bool}, _]))) |
|
286 |
$ t1 $ t2 => |
|
287 |
t0 $ aux Ts t1 $ aux Ts t2 |
|
288 |
| _ => if not (exists_subterm (fn Abs _ => true | _ => false) t) then |
|
289 |
t |
|
290 |
else |
|
291 |
let |
|
292 |
val t = t |> conceal_bounds Ts |
|
293 |
|> Envir.eta_contract |
|
294 |
val ([t], ctxt') = Variable.import_terms true [t] ctxt |
|
295 |
in |
|
296 |
t |> cterm_of thy |
|
297 |
|> Clausifier.introduce_combinators_in_cterm |
|
298 |
|> singleton (Variable.export ctxt' ctxt) |
|
299 |
|> prop_of |> Logic.dest_equals |> snd |
|
300 |
|> reveal_bounds Ts |
|
301 |
end |
|
302 |
in t |> not (Meson.is_fol_term thy t) ? aux [] end |
|
303 |
handle THM _ => |
|
304 |
(* A type variable of sort "{}" will make abstraction fail. *) |
|
305 |
case kind of |
|
306 |
Axiom => HOLogic.true_const |
|
307 |
| Conjecture => HOLogic.false_const |
|
308 |
||
309 |
(* making axiom and conjecture clauses *) |
|
310 |
fun make_clause ctxt (formula_name, kind, t) = |
|
311 |
let |
|
312 |
val thy = ProofContext.theory_of ctxt |
|
313 |
(* ### FIXME: perform other transformations previously done by |
|
314 |
"Clausifier.to_nnf", e.g. "HOL.If" *) |
|
315 |
val t = t |> transform_elim_term |
|
316 |
|> Object_Logic.atomize_term thy |
|
317 |
|> extensionalize_term |
|
318 |
|> introduce_combinators_in_term ctxt kind |
|
319 |
val (combformula, ctypes_sorts) = combformula_for_prop thy t [] |
|
320 |
in |
|
321 |
FOLFormula {formula_name = formula_name, combformula = combformula, |
|
322 |
kind = kind, ctypes_sorts = ctypes_sorts} |
|
323 |
end |
|
324 |
||
325 |
fun make_axiom_clause ctxt (name, th) = |
|
326 |
(name, make_clause ctxt (name, Axiom, prop_of th)) |
|
327 |
fun make_conjecture_clauses ctxt ts = |
|
328 |
map2 (fn j => fn t => make_clause ctxt (Int.toString j, Conjecture, t)) |
|
329 |
(0 upto length ts - 1) ts |
|
330 |
||
331 |
(** Helper clauses **) |
|
332 |
||
333 |
fun count_combterm (CombConst ((s, _), _, _)) = |
|
334 |
Symtab.map_entry s (Integer.add 1) |
|
335 |
| count_combterm (CombVar _) = I |
|
336 |
| count_combterm (CombApp (t1, t2)) = fold count_combterm [t1, t2] |
|
337 |
fun count_combformula (AQuant (_, _, phi)) = count_combformula phi |
|
338 |
| count_combformula (AConn (_, phis)) = fold count_combformula phis |
|
339 |
| count_combformula (APred tm) = count_combterm tm |
|
340 |
fun count_fol_formula (FOLFormula {combformula, ...}) = |
|
341 |
count_combformula combformula |
|
342 |
||
343 |
val optional_helpers = |
|
344 |
[(["c_COMBI", "c_COMBK"], @{thms COMBI_def COMBK_def}), |
|
345 |
(["c_COMBB", "c_COMBC"], @{thms COMBB_def COMBC_def}), |
|
346 |
(["c_COMBS"], @{thms COMBS_def})] |
|
347 |
val optional_typed_helpers = |
|
348 |
[(["c_True", "c_False"], @{thms True_or_False}), |
|
349 |
(["c_If"], @{thms if_True if_False True_or_False})] |
|
350 |
val mandatory_helpers = @{thms fequal_imp_equal equal_imp_fequal} |
|
351 |
||
352 |
val init_counters = |
|
353 |
Symtab.make (maps (maps (map (rpair 0) o fst)) |
|
354 |
[optional_helpers, optional_typed_helpers]) |
|
355 |
||
356 |
fun get_helper_clauses ctxt is_FO full_types conjectures axclauses = |
|
357 |
let |
|
358 |
val ct = fold (fold count_fol_formula) [conjectures, axclauses] |
|
359 |
init_counters |
|
360 |
fun is_needed c = the (Symtab.lookup ct c) > 0 |
|
361 |
val cnfs = |
|
362 |
(optional_helpers |
|
363 |
|> full_types ? append optional_typed_helpers |
|
364 |
|> maps (fn (ss, ths) => |
|
365 |
if exists is_needed ss then map (`Thm.get_name_hint) ths |
|
366 |
else [])) @ |
|
367 |
(if is_FO then [] else map (`Thm.get_name_hint) mandatory_helpers) |
|
368 |
in map (snd o make_axiom_clause ctxt) cnfs end |
|
369 |
||
370 |
fun s_not (@{const Not} $ t) = t |
|
371 |
| s_not t = @{const Not} $ t |
|
372 |
||
373 |
(* prepare for passing to writer, |
|
374 |
create additional clauses based on the information from extra_cls *) |
|
375 |
fun prepare_clauses ctxt full_types hyp_ts concl_t axcls extra_cls = |
|
376 |
let |
|
377 |
val thy = ProofContext.theory_of ctxt |
|
378 |
val goal_t = Logic.list_implies (hyp_ts, concl_t) |
|
379 |
val is_FO = Meson.is_fol_term thy goal_t |
|
380 |
val axtms = map (prop_of o snd) extra_cls |
|
381 |
val subs = tfree_classes_of_terms [goal_t] |
|
382 |
val supers = tvar_classes_of_terms axtms |
|
383 |
val tycons = type_consts_of_terms thy (goal_t :: axtms) |
|
384 |
(* TFrees in conjecture clauses; TVars in axiom clauses *) |
|
385 |
val conjectures = |
|
386 |
map (s_not o HOLogic.dest_Trueprop) hyp_ts @ |
|
387 |
[HOLogic.dest_Trueprop concl_t] |
|
388 |
|> make_conjecture_clauses ctxt |
|
389 |
val extra_clauses = map (snd o make_axiom_clause ctxt) extra_cls |
|
390 |
val (clnames, axiom_clauses) = |
|
391 |
ListPair.unzip (map (make_axiom_clause ctxt) axcls) |
|
392 |
(* FIXME: Should it read "extra_clauses" or "axiom_clauses" in the |
|
393 |
"get_helper_clauses" call? *) |
|
394 |
val helper_clauses = |
|
395 |
get_helper_clauses ctxt is_FO full_types conjectures extra_clauses |
|
396 |
val (supers', arity_clauses) = make_arity_clauses thy tycons supers |
|
397 |
val class_rel_clauses = make_class_rel_clauses thy subs supers' |
|
398 |
in |
|
399 |
(Vector.fromList clnames, |
|
400 |
(conjectures, axiom_clauses, extra_clauses, helper_clauses, |
|
401 |
class_rel_clauses, arity_clauses)) |
|
402 |
end |
|
403 |
||
404 |
val axiom_prefix = "ax_" |
|
405 |
val conjecture_prefix = "conj_" |
|
406 |
val arity_clause_prefix = "clsarity_" |
|
407 |
val tfrees_name = "tfrees" |
|
408 |
||
409 |
fun wrap_type ty t = ATerm ((type_wrapper_name, type_wrapper_name), [ty, t]) |
|
410 |
||
411 |
fun fo_term_for_combtyp (CombTVar name) = ATerm (name, []) |
|
412 |
| fo_term_for_combtyp (CombTFree name) = ATerm (name, []) |
|
413 |
| fo_term_for_combtyp (CombType (name, tys)) = |
|
414 |
ATerm (name, map fo_term_for_combtyp tys) |
|
415 |
||
416 |
fun fo_literal_for_type_literal (TyLitVar (class, name)) = |
|
417 |
(true, ATerm (class, [ATerm (name, [])])) |
|
418 |
| fo_literal_for_type_literal (TyLitFree (class, name)) = |
|
419 |
(true, ATerm (class, [ATerm (name, [])])) |
|
420 |
||
421 |
fun formula_for_fo_literal (pos, t) = APred t |> not pos ? mk_anot |
|
422 |
||
423 |
fun fo_term_for_combterm full_types = |
|
424 |
let |
|
425 |
fun aux top_level u = |
|
426 |
let |
|
427 |
val (head, args) = strip_combterm_comb u |
|
428 |
val (x, ty_args) = |
|
429 |
case head of |
|
430 |
CombConst (name, _, ty_args) => |
|
431 |
if fst name = "equal" then |
|
432 |
(if top_level andalso length args = 2 then name |
|
433 |
else ("c_fequal", @{const_name fequal}), []) |
|
434 |
else |
|
435 |
(name, if full_types then [] else ty_args) |
|
436 |
| CombVar (name, _) => (name, []) |
|
437 |
| CombApp _ => raise Fail "impossible \"CombApp\"" |
|
438 |
val t = ATerm (x, map fo_term_for_combtyp ty_args @ |
|
439 |
map (aux false) args) |
|
440 |
in |
|
441 |
if full_types then wrap_type (fo_term_for_combtyp (combtyp_of u)) t else t |
|
442 |
end |
|
443 |
in aux true end |
|
444 |
||
445 |
fun formula_for_combformula full_types = |
|
446 |
let |
|
447 |
fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi) |
|
448 |
| aux (AConn (c, phis)) = AConn (c, map aux phis) |
|
449 |
| aux (APred tm) = APred (fo_term_for_combterm full_types tm) |
|
450 |
in aux end |
|
451 |
||
452 |
fun formula_for_axiom full_types (FOLFormula {combformula, ctypes_sorts, ...}) = |
|
453 |
mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal) |
|
454 |
(type_literals_for_types ctypes_sorts)) |
|
455 |
(formula_for_combformula full_types combformula) |
|
456 |
||
457 |
fun problem_line_for_axiom full_types |
|
458 |
(formula as FOLFormula {formula_name, kind, ...}) = |
|
459 |
Fof (axiom_prefix ^ ascii_of formula_name, kind, |
|
460 |
formula_for_axiom full_types formula) |
|
461 |
||
462 |
fun problem_line_for_class_rel_clause |
|
463 |
(ClassRelClause {axiom_name, subclass, superclass, ...}) = |
|
464 |
let val ty_arg = ATerm (("T", "T"), []) in |
|
465 |
Fof (ascii_of axiom_name, Axiom, |
|
466 |
AConn (AImplies, [APred (ATerm (subclass, [ty_arg])), |
|
467 |
APred (ATerm (superclass, [ty_arg]))])) |
|
468 |
end |
|
469 |
||
470 |
fun fo_literal_for_arity_literal (TConsLit (c, t, args)) = |
|
471 |
(true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)])) |
|
472 |
| fo_literal_for_arity_literal (TVarLit (c, sort)) = |
|
473 |
(false, ATerm (c, [ATerm (sort, [])])) |
|
474 |
||
475 |
fun problem_line_for_arity_clause |
|
476 |
(ArityClause {axiom_name, conclLit, premLits, ...}) = |
|
477 |
Fof (arity_clause_prefix ^ ascii_of axiom_name, Axiom, |
|
478 |
mk_ahorn (map (formula_for_fo_literal o apfst not |
|
479 |
o fo_literal_for_arity_literal) premLits) |
|
480 |
(formula_for_fo_literal |
|
481 |
(fo_literal_for_arity_literal conclLit))) |
|
482 |
||
483 |
fun problem_line_for_conjecture full_types |
|
484 |
(FOLFormula {formula_name, kind, combformula, ...}) = |
|
485 |
Fof (conjecture_prefix ^ formula_name, kind, |
|
486 |
formula_for_combformula full_types combformula) |
|
487 |
||
488 |
fun free_type_literals_for_conjecture (FOLFormula {ctypes_sorts, ...}) = |
|
489 |
map fo_literal_for_type_literal (type_literals_for_types ctypes_sorts) |
|
490 |
||
491 |
fun problem_line_for_free_type lit = |
|
492 |
Fof (tfrees_name, Conjecture, mk_anot (formula_for_fo_literal lit)) |
|
493 |
fun problem_lines_for_free_types conjectures = |
|
494 |
let |
|
495 |
val litss = map free_type_literals_for_conjecture conjectures |
|
496 |
val lits = fold (union (op =)) litss [] |
|
497 |
in map problem_line_for_free_type lits end |
|
498 |
||
499 |
(** "hBOOL" and "hAPP" **) |
|
500 |
||
501 |
type const_info = {min_arity: int, max_arity: int, sub_level: bool} |
|
502 |
||
503 |
fun consider_term top_level (ATerm ((s, _), ts)) = |
|
504 |
(if is_tptp_variable s then |
|
505 |
I |
|
506 |
else |
|
507 |
let val n = length ts in |
|
508 |
Symtab.map_default |
|
509 |
(s, {min_arity = n, max_arity = 0, sub_level = false}) |
|
510 |
(fn {min_arity, max_arity, sub_level} => |
|
511 |
{min_arity = Int.min (n, min_arity), |
|
512 |
max_arity = Int.max (n, max_arity), |
|
513 |
sub_level = sub_level orelse not top_level}) |
|
514 |
end) |
|
515 |
#> fold (consider_term (top_level andalso s = type_wrapper_name)) ts |
|
516 |
fun consider_formula (AQuant (_, _, phi)) = consider_formula phi |
|
517 |
| consider_formula (AConn (_, phis)) = fold consider_formula phis |
|
518 |
| consider_formula (APred tm) = consider_term true tm |
|
519 |
||
520 |
fun consider_problem_line (Fof (_, _, phi)) = consider_formula phi |
|
521 |
fun consider_problem problem = fold (fold consider_problem_line o snd) problem |
|
522 |
||
523 |
fun const_table_for_problem explicit_apply problem = |
|
524 |
if explicit_apply then NONE |
|
525 |
else SOME (Symtab.empty |> consider_problem problem) |
|
526 |
||
527 |
val tc_fun = make_fixed_type_const @{type_name fun} |
|
528 |
||
529 |
fun min_arity_of thy full_types NONE s = |
|
530 |
(if s = "equal" orelse s = type_wrapper_name orelse |
|
531 |
String.isPrefix type_const_prefix s orelse |
|
532 |
String.isPrefix class_prefix s then |
|
533 |
16383 (* large number *) |
|
534 |
else if full_types then |
|
535 |
0 |
|
536 |
else case strip_prefix_and_undo_ascii const_prefix s of |
|
537 |
SOME s' => num_type_args thy (invert_const s') |
|
538 |
| NONE => 0) |
|
539 |
| min_arity_of _ _ (SOME the_const_tab) s = |
|
540 |
case Symtab.lookup the_const_tab s of |
|
541 |
SOME ({min_arity, ...} : const_info) => min_arity |
|
542 |
| NONE => 0 |
|
543 |
||
544 |
fun full_type_of (ATerm ((s, _), [ty, _])) = |
|
545 |
if s = type_wrapper_name then ty else raise Fail "expected type wrapper" |
|
546 |
| full_type_of _ = raise Fail "expected type wrapper" |
|
547 |
||
548 |
fun list_hAPP_rev _ t1 [] = t1 |
|
549 |
| list_hAPP_rev NONE t1 (t2 :: ts2) = |
|
550 |
ATerm (`I "hAPP", [list_hAPP_rev NONE t1 ts2, t2]) |
|
551 |
| list_hAPP_rev (SOME ty) t1 (t2 :: ts2) = |
|
552 |
let val ty' = ATerm (`make_fixed_type_const @{type_name fun}, |
|
553 |
[full_type_of t2, ty]) in |
|
554 |
ATerm (`I "hAPP", [wrap_type ty' (list_hAPP_rev (SOME ty') t1 ts2), t2]) |
|
555 |
end |
|
556 |
||
557 |
fun repair_applications_in_term thy full_types const_tab = |
|
558 |
let |
|
559 |
fun aux opt_ty (ATerm (name as (s, _), ts)) = |
|
560 |
if s = type_wrapper_name then |
|
561 |
case ts of |
|
562 |
[t1, t2] => ATerm (name, [aux NONE t1, aux (SOME t1) t2]) |
|
563 |
| _ => raise Fail "malformed type wrapper" |
|
564 |
else |
|
565 |
let |
|
566 |
val ts = map (aux NONE) ts |
|
567 |
val (ts1, ts2) = chop (min_arity_of thy full_types const_tab s) ts |
|
568 |
in list_hAPP_rev opt_ty (ATerm (name, ts1)) (rev ts2) end |
|
569 |
in aux NONE end |
|
570 |
||
571 |
fun boolify t = ATerm (`I "hBOOL", [t]) |
|
572 |
||
573 |
(* True if the constant ever appears outside of the top-level position in |
|
574 |
literals, or if it appears with different arities (e.g., because of different |
|
575 |
type instantiations). If false, the constant always receives all of its |
|
576 |
arguments and is used as a predicate. *) |
|
577 |
fun is_predicate NONE s = |
|
578 |
s = "equal" orelse String.isPrefix type_const_prefix s orelse |
|
579 |
String.isPrefix class_prefix s |
|
580 |
| is_predicate (SOME the_const_tab) s = |
|
581 |
case Symtab.lookup the_const_tab s of |
|
582 |
SOME {min_arity, max_arity, sub_level} => |
|
583 |
not sub_level andalso min_arity = max_arity |
|
584 |
| NONE => false |
|
585 |
||
586 |
fun repair_predicates_in_term const_tab (t as ATerm ((s, _), ts)) = |
|
587 |
if s = type_wrapper_name then |
|
588 |
case ts of |
|
589 |
[_, t' as ATerm ((s', _), _)] => |
|
590 |
if is_predicate const_tab s' then t' else boolify t |
|
591 |
| _ => raise Fail "malformed type wrapper" |
|
592 |
else |
|
593 |
t |> not (is_predicate const_tab s) ? boolify |
|
594 |
||
595 |
fun close_universally phi = |
|
596 |
let |
|
597 |
fun term_vars bounds (ATerm (name as (s, _), tms)) = |
|
598 |
(is_tptp_variable s andalso not (member (op =) bounds name)) |
|
599 |
? insert (op =) name |
|
600 |
#> fold (term_vars bounds) tms |
|
601 |
fun formula_vars bounds (AQuant (q, xs, phi)) = |
|
602 |
formula_vars (xs @ bounds) phi |
|
603 |
| formula_vars bounds (AConn (_, phis)) = fold (formula_vars bounds) phis |
|
604 |
| formula_vars bounds (APred tm) = term_vars bounds tm |
|
605 |
in |
|
606 |
case formula_vars [] phi [] of [] => phi | xs => AQuant (AForall, xs, phi) |
|
607 |
end |
|
608 |
||
609 |
fun repair_formula thy explicit_forall full_types const_tab = |
|
610 |
let |
|
611 |
fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi) |
|
612 |
| aux (AConn (c, phis)) = AConn (c, map aux phis) |
|
613 |
| aux (APred tm) = |
|
614 |
APred (tm |> repair_applications_in_term thy full_types const_tab |
|
615 |
|> repair_predicates_in_term const_tab) |
|
616 |
in aux #> explicit_forall ? close_universally end |
|
617 |
||
618 |
fun repair_problem_line thy explicit_forall full_types const_tab |
|
619 |
(Fof (ident, kind, phi)) = |
|
620 |
Fof (ident, kind, repair_formula thy explicit_forall full_types const_tab phi) |
|
621 |
fun repair_problem_with_const_table thy = |
|
622 |
map o apsnd o map ooo repair_problem_line thy |
|
623 |
||
624 |
fun repair_problem thy explicit_forall full_types explicit_apply problem = |
|
625 |
repair_problem_with_const_table thy explicit_forall full_types |
|
626 |
(const_table_for_problem explicit_apply problem) problem |
|
627 |
||
628 |
fun write_tptp_file thy readable_names explicit_forall full_types explicit_apply |
|
629 |
file (conjectures, axiom_clauses, extra_clauses, |
|
630 |
helper_clauses, class_rel_clauses, arity_clauses) = |
|
631 |
let |
|
632 |
val axiom_lines = map (problem_line_for_axiom full_types) axiom_clauses |
|
633 |
val class_rel_lines = |
|
634 |
map problem_line_for_class_rel_clause class_rel_clauses |
|
635 |
val arity_lines = map problem_line_for_arity_clause arity_clauses |
|
636 |
val helper_lines = map (problem_line_for_axiom full_types) helper_clauses |
|
637 |
val conjecture_lines = |
|
638 |
map (problem_line_for_conjecture full_types) conjectures |
|
639 |
val tfree_lines = problem_lines_for_free_types conjectures |
|
640 |
(* Reordering these might or might not confuse the proof reconstruction |
|
641 |
code or the SPASS Flotter hack. *) |
|
642 |
val problem = |
|
643 |
[("Relevant facts", axiom_lines), |
|
644 |
("Class relationships", class_rel_lines), |
|
645 |
("Arity declarations", arity_lines), |
|
646 |
("Helper facts", helper_lines), |
|
647 |
("Conjectures", conjecture_lines), |
|
648 |
("Type variables", tfree_lines)] |
|
649 |
|> repair_problem thy explicit_forall full_types explicit_apply |
|
650 |
val (problem, pool) = nice_tptp_problem readable_names problem |
|
651 |
val conjecture_offset = |
|
652 |
length axiom_lines + length class_rel_lines + length arity_lines |
|
653 |
+ length helper_lines |
|
654 |
val _ = File.write_list file (strings_for_tptp_problem problem) |
|
655 |
in |
|
656 |
(case pool of SOME the_pool => snd the_pool | NONE => Symtab.empty, |
|
657 |
conjecture_offset) |
|
658 |
end |
|
659 |
||
660 |
fun extract_clause_sequence output = |
|
661 |
let |
|
662 |
val tokens_of = String.tokens (not o Char.isAlphaNum) |
|
663 |
fun extract_num ("clause" :: (ss as _ :: _)) = |
|
664 |
Int.fromString (List.last ss) |
|
665 |
| extract_num _ = NONE |
|
666 |
in output |> split_lines |> map_filter (extract_num o tokens_of) end |
|
667 |
||
668 |
val set_ClauseFormulaRelationN = "set_ClauseFormulaRelation" |
|
669 |
||
670 |
val parse_clause_formula_pair = |
|
671 |
$$ "(" |-- scan_integer --| $$ "," -- Symbol.scan_id --| $$ ")" |
|
672 |
--| Scan.option ($$ ",") |
|
673 |
val parse_clause_formula_relation = |
|
674 |
Scan.this_string set_ClauseFormulaRelationN |-- $$ "(" |
|
675 |
|-- Scan.repeat parse_clause_formula_pair |
|
676 |
val extract_clause_formula_relation = |
|
677 |
Substring.full |
|
678 |
#> Substring.position set_ClauseFormulaRelationN |
|
679 |
#> snd #> Substring.string #> strip_spaces #> explode |
|
680 |
#> parse_clause_formula_relation #> fst |
|
681 |
||
682 |
fun repair_conjecture_shape_and_theorem_names output conjecture_shape |
|
683 |
thm_names = |
|
684 |
if String.isSubstring set_ClauseFormulaRelationN output then |
|
685 |
(* This is a hack required for keeping track of axioms after they have been |
|
686 |
clausified by SPASS's Flotter tool. The "SPASS_TPTP" script is also part |
|
687 |
of this hack. *) |
|
688 |
let |
|
689 |
val j0 = hd conjecture_shape |
|
690 |
val seq = extract_clause_sequence output |
|
691 |
val name_map = extract_clause_formula_relation output |
|
692 |
fun renumber_conjecture j = |
|
693 |
AList.find (op =) name_map (conjecture_prefix ^ Int.toString (j - j0)) |
|
694 |
|> the_single |
|
695 |
|> (fn s => find_index (curry (op =) s) seq + 1) |
|
696 |
in |
|
697 |
(conjecture_shape |> map renumber_conjecture, |
|
698 |
seq |> map (the o AList.lookup (op =) name_map) |
|
699 |
|> map (fn s => case try (unprefix axiom_prefix) s of |
|
700 |
SOME s' => undo_ascii_of s' |
|
701 |
| NONE => "") |
|
702 |
|> Vector.fromList) |
|
703 |
end |
|
704 |
else |
|
705 |
(conjecture_shape, thm_names) |
|
706 |
||
707 |
||
708 |
(* generic TPTP-based provers *) |
|
709 |
||
710 |
fun prover_fun name |
|
711 |
{home_var, executable, arguments, proof_delims, known_failures, |
|
712 |
max_new_relevant_facts_per_iter, prefers_theory_relevant, |
|
713 |
explicit_forall} |
|
714 |
({debug, overlord, full_types, explicit_apply, relevance_threshold, |
|
715 |
relevance_convergence, theory_relevant, defs_relevant, isar_proof, |
|
716 |
isar_shrink_factor, ...} : params) |
|
717 |
minimize_command timeout |
|
718 |
({subgoal, goal, relevance_override, axiom_clauses, filtered_clauses} |
|
719 |
: problem) = |
|
720 |
let |
|
721 |
(* get clauses and prepare them for writing *) |
|
722 |
val (ctxt, (_, th)) = goal; |
|
723 |
val thy = ProofContext.theory_of ctxt |
|
724 |
(* ### FIXME: (1) preprocessing for "if" etc. *) |
|
725 |
val (params, hyp_ts, concl_t) = strip_subgoal th subgoal |
|
726 |
val the_filtered_clauses = |
|
727 |
case filtered_clauses of |
|
728 |
SOME fcls => fcls |
|
729 |
| NONE => relevant_facts full_types relevance_threshold |
|
730 |
relevance_convergence defs_relevant |
|
731 |
max_new_relevant_facts_per_iter |
|
732 |
(the_default prefers_theory_relevant theory_relevant) |
|
733 |
relevance_override goal hyp_ts concl_t |
|
734 |
val the_axiom_clauses = axiom_clauses |> the_default the_filtered_clauses |
|
735 |
val (internal_thm_names, clauses) = |
|
736 |
prepare_clauses ctxt full_types hyp_ts concl_t the_axiom_clauses |
|
737 |
the_filtered_clauses |
|
738 |
||
739 |
(* path to unique problem file *) |
|
740 |
val the_dest_dir = if overlord then getenv "ISABELLE_HOME_USER" |
|
741 |
else Config.get ctxt dest_dir; |
|
742 |
val the_problem_prefix = Config.get ctxt problem_prefix; |
|
743 |
fun prob_pathname nr = |
|
744 |
let |
|
745 |
val probfile = |
|
746 |
Path.basic ((if overlord then "prob_" ^ name |
|
747 |
else the_problem_prefix ^ serial_string ()) |
|
748 |
^ "_" ^ string_of_int nr) |
|
749 |
in |
|
750 |
if the_dest_dir = "" then File.tmp_path probfile |
|
751 |
else if File.exists (Path.explode the_dest_dir) |
|
752 |
then Path.append (Path.explode the_dest_dir) probfile |
|
753 |
else error ("No such directory: " ^ the_dest_dir ^ ".") |
|
754 |
end; |
|
755 |
||
756 |
val home = getenv home_var |
|
757 |
val command = Path.explode (home ^ "/" ^ executable) |
|
758 |
(* write out problem file and call prover *) |
|
759 |
fun command_line complete probfile = |
|
760 |
let |
|
761 |
val core = File.shell_path command ^ " " ^ arguments complete timeout ^ |
|
762 |
" " ^ File.shell_path probfile |
|
763 |
in |
|
764 |
(if Config.get ctxt measure_runtime then |
|
765 |
"TIMEFORMAT='%3U'; { time " ^ core ^ " ; }" |
|
766 |
else |
|
767 |
"exec " ^ core) ^ " 2>&1" |
|
768 |
end |
|
769 |
fun split_time s = |
|
770 |
let |
|
771 |
val split = String.tokens (fn c => str c = "\n"); |
|
772 |
val (output, t) = s |> split |> split_last |> apfst cat_lines; |
|
773 |
fun as_num f = f >> (fst o read_int); |
|
774 |
val num = as_num (Scan.many1 Symbol.is_ascii_digit); |
|
775 |
val digit = Scan.one Symbol.is_ascii_digit; |
|
776 |
val num3 = as_num (digit ::: digit ::: (digit >> single)); |
|
777 |
val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b); |
|
778 |
val as_time = the_default 0 o Scan.read Symbol.stopper time o explode; |
|
779 |
in (output, as_time t) end; |
|
780 |
fun run_on probfile = |
|
781 |
if home = "" then |
|
782 |
error ("The environment variable " ^ quote home_var ^ " is not set.") |
|
783 |
else if File.exists command then |
|
784 |
let |
|
785 |
fun do_run complete = |
|
786 |
let |
|
787 |
val command = command_line complete probfile |
|
788 |
val ((output, msecs), res_code) = |
|
789 |
bash_output command |
|
790 |
|>> (if overlord then |
|
791 |
prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n") |
|
792 |
else |
|
793 |
I) |
|
794 |
|>> (if Config.get ctxt measure_runtime then split_time |
|
795 |
else rpair 0) |
|
796 |
val (proof, outcome) = |
|
797 |
extract_proof_and_outcome complete res_code proof_delims |
|
798 |
known_failures output |
|
799 |
in (output, msecs, proof, outcome) end |
|
800 |
val readable_names = debug andalso overlord |
|
801 |
val (pool, conjecture_offset) = |
|
802 |
write_tptp_file thy readable_names explicit_forall full_types |
|
803 |
explicit_apply probfile clauses |
|
804 |
val conjecture_shape = |
|
805 |
conjecture_offset + 1 upto conjecture_offset + length hyp_ts + 1 |
|
806 |
val result = |
|
807 |
do_run false |
|
808 |
|> (fn (_, msecs0, _, SOME _) => |
|
809 |
do_run true |
|
810 |
|> (fn (output, msecs, proof, outcome) => |
|
811 |
(output, msecs0 + msecs, proof, outcome)) |
|
812 |
| result => result) |
|
813 |
in ((pool, conjecture_shape), result) end |
|
814 |
else |
|
815 |
error ("Bad executable: " ^ Path.implode command ^ "."); |
|
816 |
||
817 |
(* If the problem file has not been exported, remove it; otherwise, export |
|
818 |
the proof file too. *) |
|
819 |
fun cleanup probfile = |
|
820 |
if the_dest_dir = "" then try File.rm probfile else NONE |
|
821 |
fun export probfile (_, (output, _, _, _)) = |
|
822 |
if the_dest_dir = "" then |
|
823 |
() |
|
824 |
else |
|
825 |
File.write (Path.explode (Path.implode probfile ^ "_proof")) output |
|
826 |
||
827 |
val ((pool, conjecture_shape), (output, msecs, proof, outcome)) = |
|
828 |
with_path cleanup export run_on (prob_pathname subgoal) |
|
829 |
val (conjecture_shape, internal_thm_names) = |
|
830 |
repair_conjecture_shape_and_theorem_names output conjecture_shape |
|
831 |
internal_thm_names |
|
832 |
||
833 |
val (message, used_thm_names) = |
|
834 |
case outcome of |
|
835 |
NONE => |
|
836 |
proof_text isar_proof |
|
837 |
(pool, debug, isar_shrink_factor, ctxt, conjecture_shape) |
|
838 |
(full_types, minimize_command, proof, internal_thm_names, th, |
|
839 |
subgoal) |
|
840 |
| SOME failure => (string_for_failure failure ^ "\n", []) |
|
841 |
in |
|
842 |
{outcome = outcome, message = message, pool = pool, |
|
843 |
used_thm_names = used_thm_names, atp_run_time_in_msecs = msecs, |
|
844 |
output = output, proof = proof, internal_thm_names = internal_thm_names, |
|
845 |
conjecture_shape = conjecture_shape, |
|
846 |
filtered_clauses = the_filtered_clauses} |
|
847 |
end |
|
848 |
||
849 |
fun get_prover_fun thy name = prover_fun name (get_prover thy name) |
|
850 |
||
28595
67e3945b53f1
add_prover: plain prover function, without thread;
wenzelm
parents:
28589
diff
changeset
|
851 |
(* start prover thread *) |
37584 | 852 |
fun start_prover_thread (params as {verbose, full_types, timeout, ...}) i n |
853 |
relevance_override minimize_command proof_state name = |
|
36379
20ef039bccff
make "ATP_Manager.get_prover" a total function, since we always want to show the same error text
blanchet
parents:
36373
diff
changeset
|
854 |
let |
38023 | 855 |
val thy = Proof.theory_of proof_state |
37584 | 856 |
val birth_time = Time.now () |
857 |
val death_time = Time.+ (birth_time, timeout) |
|
38023 | 858 |
val prover = get_prover_fun thy name |
36379
20ef039bccff
make "ATP_Manager.get_prover" a total function, since we always want to show the same error text
blanchet
parents:
36373
diff
changeset
|
859 |
val {context = ctxt, facts, goal} = Proof.goal proof_state; |
20ef039bccff
make "ATP_Manager.get_prover" a total function, since we always want to show the same error text
blanchet
parents:
36373
diff
changeset
|
860 |
val desc = |
20ef039bccff
make "ATP_Manager.get_prover" a total function, since we always want to show the same error text
blanchet
parents:
36373
diff
changeset
|
861 |
"ATP " ^ quote name ^ " for subgoal " ^ string_of_int i ^ ":\n" ^ |
36392 | 862 |
Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i)); |
37584 | 863 |
in |
37585 | 864 |
Async_Manager.launch das_Tool verbose birth_time death_time desc |
37584 | 865 |
(fn () => |
866 |
let |
|
867 |
val problem = |
|
868 |
{subgoal = i, goal = (ctxt, (facts, goal)), |
|
869 |
relevance_override = relevance_override, axiom_clauses = NONE, |
|
870 |
filtered_clauses = NONE} |
|
871 |
in |
|
872 |
prover params (minimize_command name) timeout problem |> #message |
|
37994
b04307085a09
make TPTP generator accept full first-order formulas
blanchet
parents:
37926
diff
changeset
|
873 |
handle ERROR message => "Error: " ^ message ^ "\n" |
37584 | 874 |
end) |
875 |
end |
|
28582 | 876 |
|
38023 | 877 |
val setup = |
878 |
dest_dir_setup |
|
879 |
#> problem_prefix_setup |
|
880 |
#> measure_runtime_setup |
|
881 |
||
28582 | 882 |
end; |