1 (* Title: HOL/Tools/ATP/atp_reconstruct.ML |
|
2 Author: Lawrence C. Paulson, Cambridge University Computer Laboratory |
|
3 Author: Claire Quigley, Cambridge University Computer Laboratory |
|
4 Author: Jasmin Blanchette, TU Muenchen |
|
5 |
|
6 Proof reconstruction from ATP proofs. |
|
7 *) |
|
8 |
|
9 signature ATP_RECONSTRUCT = |
|
10 sig |
|
11 type ('a, 'b) ho_term = ('a, 'b) ATP_Problem.ho_term |
|
12 type ('a, 'b, 'c) formula = ('a, 'b, 'c) ATP_Problem.formula |
|
13 type 'a proof = 'a ATP_Proof.proof |
|
14 type locality = ATP_Translate.locality |
|
15 |
|
16 datatype reconstructor = |
|
17 Metis of string * string | |
|
18 SMT |
|
19 |
|
20 datatype play = |
|
21 Played of reconstructor * Time.time | |
|
22 Trust_Playable of reconstructor * Time.time option | |
|
23 Failed_to_Play of reconstructor |
|
24 |
|
25 type minimize_command = string list -> string |
|
26 type one_line_params = |
|
27 play * string * (string * locality) list * minimize_command * int * int |
|
28 type isar_params = |
|
29 bool * int * string Symtab.table * (string * locality) list vector |
|
30 * int Symtab.table * string proof * thm |
|
31 |
|
32 val metisN : string |
|
33 val smtN : string |
|
34 val full_typesN : string |
|
35 val partial_typesN : string |
|
36 val no_typesN : string |
|
37 val really_full_type_enc : string |
|
38 val full_type_enc : string |
|
39 val partial_type_enc : string |
|
40 val no_type_enc : string |
|
41 val full_type_encs : string list |
|
42 val partial_type_encs : string list |
|
43 val metis_default_lam_trans : string |
|
44 val metis_call : string -> string -> string |
|
45 val string_for_reconstructor : reconstructor -> string |
|
46 val used_facts_in_atp_proof : |
|
47 Proof.context -> (string * locality) list vector -> string proof |
|
48 -> (string * locality) list |
|
49 val lam_trans_from_atp_proof : string proof -> string -> string |
|
50 val is_typed_helper_used_in_atp_proof : string proof -> bool |
|
51 val used_facts_in_unsound_atp_proof : |
|
52 Proof.context -> (string * locality) list vector -> 'a proof |
|
53 -> string list option |
|
54 val unalias_type_enc : string -> string list |
|
55 val one_line_proof_text : one_line_params -> string |
|
56 val make_tvar : string -> typ |
|
57 val make_tfree : Proof.context -> string -> typ |
|
58 val term_from_atp : |
|
59 Proof.context -> bool -> int Symtab.table -> typ option |
|
60 -> (string, string) ho_term -> term |
|
61 val prop_from_atp : |
|
62 Proof.context -> bool -> int Symtab.table |
|
63 -> (string, string, (string, string) ho_term) formula -> term |
|
64 val isar_proof_text : |
|
65 Proof.context -> bool -> isar_params -> one_line_params -> string |
|
66 val proof_text : |
|
67 Proof.context -> bool -> isar_params -> one_line_params -> string |
|
68 end; |
|
69 |
|
70 structure ATP_Reconstruct : ATP_RECONSTRUCT = |
|
71 struct |
|
72 |
|
73 open ATP_Util |
|
74 open ATP_Problem |
|
75 open ATP_Proof |
|
76 open ATP_Translate |
|
77 |
|
78 structure String_Redirect = ATP_Redirect( |
|
79 type key = step_name |
|
80 val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s') |
|
81 val string_of = fst) |
|
82 |
|
83 open String_Redirect |
|
84 |
|
85 datatype reconstructor = |
|
86 Metis of string * string | |
|
87 SMT |
|
88 |
|
89 datatype play = |
|
90 Played of reconstructor * Time.time | |
|
91 Trust_Playable of reconstructor * Time.time option | |
|
92 Failed_to_Play of reconstructor |
|
93 |
|
94 type minimize_command = string list -> string |
|
95 type one_line_params = |
|
96 play * string * (string * locality) list * minimize_command * int * int |
|
97 type isar_params = |
|
98 bool * int * string Symtab.table * (string * locality) list vector |
|
99 * int Symtab.table * string proof * thm |
|
100 |
|
101 val metisN = "metis" |
|
102 val smtN = "smt" |
|
103 |
|
104 val full_typesN = "full_types" |
|
105 val partial_typesN = "partial_types" |
|
106 val no_typesN = "no_types" |
|
107 |
|
108 val really_full_type_enc = "mono_tags" |
|
109 val full_type_enc = "poly_guards_query" |
|
110 val partial_type_enc = "poly_args" |
|
111 val no_type_enc = "erased" |
|
112 |
|
113 val full_type_encs = [full_type_enc, really_full_type_enc] |
|
114 val partial_type_encs = partial_type_enc :: full_type_encs |
|
115 |
|
116 val type_enc_aliases = |
|
117 [(full_typesN, full_type_encs), |
|
118 (partial_typesN, partial_type_encs), |
|
119 (no_typesN, [no_type_enc])] |
|
120 |
|
121 fun unalias_type_enc s = |
|
122 AList.lookup (op =) type_enc_aliases s |> the_default [s] |
|
123 |
|
124 val metis_default_lam_trans = combinatorsN |
|
125 |
|
126 fun metis_call type_enc lam_trans = |
|
127 let |
|
128 val type_enc = |
|
129 case AList.find (fn (enc, encs) => enc = hd encs) type_enc_aliases |
|
130 type_enc of |
|
131 [alias] => alias |
|
132 | _ => type_enc |
|
133 val opts = [] |> type_enc <> partial_typesN ? cons type_enc |
|
134 |> lam_trans <> metis_default_lam_trans ? cons lam_trans |
|
135 in metisN ^ (if null opts then "" else " (" ^ commas opts ^ ")") end |
|
136 |
|
137 fun string_for_reconstructor (Metis (type_enc, lam_trans)) = |
|
138 metis_call type_enc lam_trans |
|
139 | string_for_reconstructor SMT = smtN |
|
140 |
|
141 fun find_first_in_list_vector vec key = |
|
142 Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key |
|
143 | (_, value) => value) NONE vec |
|
144 |
|
145 val unprefix_fact_number = space_implode "_" o tl o space_explode "_" |
|
146 |
|
147 fun resolve_one_named_fact fact_names s = |
|
148 case try (unprefix fact_prefix) s of |
|
149 SOME s' => |
|
150 let val s' = s' |> unprefix_fact_number |> unascii_of in |
|
151 s' |> find_first_in_list_vector fact_names |> Option.map (pair s') |
|
152 end |
|
153 | NONE => NONE |
|
154 fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names) |
|
155 fun is_fact fact_names = not o null o resolve_fact fact_names |
|
156 |
|
157 fun resolve_one_named_conjecture s = |
|
158 case try (unprefix conjecture_prefix) s of |
|
159 SOME s' => Int.fromString s' |
|
160 | NONE => NONE |
|
161 |
|
162 val resolve_conjecture = map_filter resolve_one_named_conjecture |
|
163 val is_conjecture = not o null o resolve_conjecture |
|
164 |
|
165 fun is_axiom_used_in_proof pred = |
|
166 exists (fn Inference ((_, ss), _, _, []) => exists pred ss | _ => false) |
|
167 |
|
168 val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix) |
|
169 |
|
170 val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix |
|
171 |
|
172 (* overapproximation (good enough) *) |
|
173 fun is_lam_lifted s = |
|
174 String.isPrefix fact_prefix s andalso |
|
175 String.isSubstring ascii_of_lam_fact_prefix s |
|
176 |
|
177 fun lam_trans_from_atp_proof atp_proof default = |
|
178 if is_axiom_used_in_proof is_combinator_def atp_proof then combinatorsN |
|
179 else if is_axiom_used_in_proof is_lam_lifted atp_proof then lam_liftingN |
|
180 else default |
|
181 |
|
182 val is_typed_helper_name = |
|
183 String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix |
|
184 fun is_typed_helper_used_in_atp_proof atp_proof = |
|
185 is_axiom_used_in_proof is_typed_helper_name atp_proof |
|
186 |
|
187 val leo2_ext = "extcnf_equal_neg" |
|
188 val isa_ext = Thm.get_name_hint @{thm ext} |
|
189 val isa_short_ext = Long_Name.base_name isa_ext |
|
190 |
|
191 fun ext_name ctxt = |
|
192 if Thm.eq_thm_prop (@{thm ext}, |
|
193 singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then |
|
194 isa_short_ext |
|
195 else |
|
196 isa_ext |
|
197 |
|
198 fun add_fact _ fact_names (Inference ((_, ss), _, _, [])) = |
|
199 union (op =) (resolve_fact fact_names ss) |
|
200 | add_fact ctxt _ (Inference (_, _, rule, _)) = |
|
201 if rule = leo2_ext then insert (op =) (ext_name ctxt, General) else I |
|
202 | add_fact _ _ _ = I |
|
203 |
|
204 fun used_facts_in_atp_proof ctxt fact_names atp_proof = |
|
205 if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names |
|
206 else fold (add_fact ctxt fact_names) atp_proof [] |
|
207 |
|
208 (* (quasi-)underapproximation of the truth *) |
|
209 fun is_locality_global Local = false |
|
210 | is_locality_global Assum = false |
|
211 | is_locality_global Chained = false |
|
212 | is_locality_global _ = true |
|
213 |
|
214 fun used_facts_in_unsound_atp_proof _ _ [] = NONE |
|
215 | used_facts_in_unsound_atp_proof ctxt fact_names atp_proof = |
|
216 let |
|
217 val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof |
|
218 in |
|
219 if forall (is_locality_global o snd) used_facts andalso |
|
220 not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then |
|
221 SOME (map fst used_facts) |
|
222 else |
|
223 NONE |
|
224 end |
|
225 |
|
226 |
|
227 (** Soft-core proof reconstruction: one-liners **) |
|
228 |
|
229 fun string_for_label (s, num) = s ^ string_of_int num |
|
230 |
|
231 fun show_time NONE = "" |
|
232 | show_time (SOME ext_time) = " (" ^ string_from_ext_time ext_time ^ ")" |
|
233 |
|
234 fun apply_on_subgoal _ 1 = "by " |
|
235 | apply_on_subgoal 1 _ = "apply " |
|
236 | apply_on_subgoal i n = |
|
237 "prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal 1 n |
|
238 fun command_call name [] = |
|
239 name |> not (Lexicon.is_identifier name) ? enclose "(" ")" |
|
240 | command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")" |
|
241 fun try_command_line banner time command = |
|
242 banner ^ ": " ^ Markup.markup Isabelle_Markup.sendback command ^ show_time time ^ "." |
|
243 fun using_labels [] = "" |
|
244 | using_labels ls = |
|
245 "using " ^ space_implode " " (map string_for_label ls) ^ " " |
|
246 fun reconstructor_command reconstr i n (ls, ss) = |
|
247 using_labels ls ^ apply_on_subgoal i n ^ |
|
248 command_call (string_for_reconstructor reconstr) ss |
|
249 fun minimize_line _ [] = "" |
|
250 | minimize_line minimize_command ss = |
|
251 case minimize_command ss of |
|
252 "" => "" |
|
253 | command => "\nTo minimize: " ^ Markup.markup Isabelle_Markup.sendback command ^ "." |
|
254 |
|
255 val split_used_facts = |
|
256 List.partition (curry (op =) Chained o snd) |
|
257 #> pairself (sort_distinct (string_ord o pairself fst)) |
|
258 |
|
259 fun one_line_proof_text (preplay, banner, used_facts, minimize_command, |
|
260 subgoal, subgoal_count) = |
|
261 let |
|
262 val (chained, extra) = split_used_facts used_facts |
|
263 val (failed, reconstr, ext_time) = |
|
264 case preplay of |
|
265 Played (reconstr, time) => (false, reconstr, (SOME (false, time))) |
|
266 | Trust_Playable (reconstr, time) => |
|
267 (false, reconstr, |
|
268 case time of |
|
269 NONE => NONE |
|
270 | SOME time => |
|
271 if time = Time.zeroTime then NONE else SOME (true, time)) |
|
272 | Failed_to_Play reconstr => (true, reconstr, NONE) |
|
273 val try_line = |
|
274 ([], map fst extra) |
|
275 |> reconstructor_command reconstr subgoal subgoal_count |
|
276 |> (if failed then enclose "One-line proof reconstruction failed: " "." |
|
277 else try_command_line banner ext_time) |
|
278 in try_line ^ minimize_line minimize_command (map fst (extra @ chained)) end |
|
279 |
|
280 (** Hard-core proof reconstruction: structured Isar proofs **) |
|
281 |
|
282 fun forall_of v t = HOLogic.all_const (fastype_of v) $ lambda v t |
|
283 fun exists_of v t = HOLogic.exists_const (fastype_of v) $ lambda v t |
|
284 |
|
285 fun make_tvar s = TVar (("'" ^ s, 0), HOLogic.typeS) |
|
286 fun make_tfree ctxt w = |
|
287 let val ww = "'" ^ w in |
|
288 TFree (ww, the_default HOLogic.typeS (Variable.def_sort ctxt (ww, ~1))) |
|
289 end |
|
290 |
|
291 val indent_size = 2 |
|
292 val no_label = ("", ~1) |
|
293 |
|
294 val raw_prefix = "x" |
|
295 val assum_prefix = "a" |
|
296 val have_prefix = "f" |
|
297 |
|
298 fun raw_label_for_name (num, ss) = |
|
299 case resolve_conjecture ss of |
|
300 [j] => (conjecture_prefix, j) |
|
301 | _ => case Int.fromString num of |
|
302 SOME j => (raw_prefix, j) |
|
303 | NONE => (raw_prefix ^ num, 0) |
|
304 |
|
305 (**** INTERPRETATION OF TSTP SYNTAX TREES ****) |
|
306 |
|
307 exception HO_TERM of (string, string) ho_term list |
|
308 exception FORMULA of (string, string, (string, string) ho_term) formula list |
|
309 exception SAME of unit |
|
310 |
|
311 (* Type variables are given the basic sort "HOL.type". Some will later be |
|
312 constrained by information from type literals, or by type inference. *) |
|
313 fun typ_from_atp ctxt (u as ATerm (a, us)) = |
|
314 let val Ts = map (typ_from_atp ctxt) us in |
|
315 case unprefix_and_unascii type_const_prefix a of |
|
316 SOME b => Type (invert_const b, Ts) |
|
317 | NONE => |
|
318 if not (null us) then |
|
319 raise HO_TERM [u] (* only "tconst"s have type arguments *) |
|
320 else case unprefix_and_unascii tfree_prefix a of |
|
321 SOME b => make_tfree ctxt b |
|
322 | NONE => |
|
323 (* Could be an Isabelle variable or a variable from the ATP, say "X1" |
|
324 or "_5018". Sometimes variables from the ATP are indistinguishable |
|
325 from Isabelle variables, which forces us to use a type parameter in |
|
326 all cases. *) |
|
327 (a |> perhaps (unprefix_and_unascii tvar_prefix), HOLogic.typeS) |
|
328 |> Type_Infer.param 0 |
|
329 end |
|
330 |
|
331 (* Type class literal applied to a type. Returns triple of polarity, class, |
|
332 type. *) |
|
333 fun type_constraint_from_term ctxt (u as ATerm (a, us)) = |
|
334 case (unprefix_and_unascii class_prefix a, map (typ_from_atp ctxt) us) of |
|
335 (SOME b, [T]) => (b, T) |
|
336 | _ => raise HO_TERM [u] |
|
337 |
|
338 (* Accumulate type constraints in a formula: negative type literals. *) |
|
339 fun add_var (key, z) = Vartab.map_default (key, []) (cons z) |
|
340 fun add_type_constraint false (cl, TFree (a ,_)) = add_var ((a, ~1), cl) |
|
341 | add_type_constraint false (cl, TVar (ix, _)) = add_var (ix, cl) |
|
342 | add_type_constraint _ _ = I |
|
343 |
|
344 fun repair_variable_name f s = |
|
345 let |
|
346 fun subscript_name s n = s ^ nat_subscript n |
|
347 val s = String.map f s |
|
348 in |
|
349 case space_explode "_" s of |
|
350 [_] => (case take_suffix Char.isDigit (String.explode s) of |
|
351 (cs1 as _ :: _, cs2 as _ :: _) => |
|
352 subscript_name (String.implode cs1) |
|
353 (the (Int.fromString (String.implode cs2))) |
|
354 | (_, _) => s) |
|
355 | [s1, s2] => (case Int.fromString s2 of |
|
356 SOME n => subscript_name s1 n |
|
357 | NONE => s) |
|
358 | _ => s |
|
359 end |
|
360 |
|
361 (* The number of type arguments of a constant, zero if it's monomorphic. For |
|
362 (instances of) Skolem pseudoconstants, this information is encoded in the |
|
363 constant name. *) |
|
364 fun num_type_args thy s = |
|
365 if String.isPrefix skolem_const_prefix s then |
|
366 s |> space_explode Long_Name.separator |> List.last |> Int.fromString |> the |
|
367 else if String.isPrefix lam_lifted_prefix s then |
|
368 if String.isPrefix lam_lifted_poly_prefix s then 2 else 0 |
|
369 else |
|
370 (s, Sign.the_const_type thy s) |> Sign.const_typargs thy |> length |
|
371 |
|
372 fun slack_fastype_of t = fastype_of t handle TERM _ => HOLogic.typeT |
|
373 |
|
374 (* First-order translation. No types are known for variables. "HOLogic.typeT" |
|
375 should allow them to be inferred. *) |
|
376 fun term_from_atp ctxt textual sym_tab = |
|
377 let |
|
378 val thy = Proof_Context.theory_of ctxt |
|
379 (* For Metis, we use 1 rather than 0 because variable references in clauses |
|
380 may otherwise conflict with variable constraints in the goal. At least, |
|
381 type inference often fails otherwise. See also "axiom_inference" in |
|
382 "Metis_Reconstruct". *) |
|
383 val var_index = if textual then 0 else 1 |
|
384 fun do_term extra_ts opt_T u = |
|
385 case u of |
|
386 ATerm (s, us) => |
|
387 if String.isPrefix simple_type_prefix s then |
|
388 @{const True} (* ignore TPTP type information *) |
|
389 else if s = tptp_equal then |
|
390 let val ts = map (do_term [] NONE) us in |
|
391 if textual andalso length ts = 2 andalso |
|
392 hd ts aconv List.last ts then |
|
393 (* Vampire is keen on producing these. *) |
|
394 @{const True} |
|
395 else |
|
396 list_comb (Const (@{const_name HOL.eq}, HOLogic.typeT), ts) |
|
397 end |
|
398 else case unprefix_and_unascii const_prefix s of |
|
399 SOME s' => |
|
400 let |
|
401 val ((s', s''), mangled_us) = |
|
402 s' |> unmangled_const |>> `invert_const |
|
403 in |
|
404 if s' = type_tag_name then |
|
405 case mangled_us @ us of |
|
406 [typ_u, term_u] => |
|
407 do_term extra_ts (SOME (typ_from_atp ctxt typ_u)) term_u |
|
408 | _ => raise HO_TERM us |
|
409 else if s' = predicator_name then |
|
410 do_term [] (SOME @{typ bool}) (hd us) |
|
411 else if s' = app_op_name then |
|
412 let val extra_t = do_term [] NONE (List.last us) in |
|
413 do_term (extra_t :: extra_ts) |
|
414 (case opt_T of |
|
415 SOME T => SOME (slack_fastype_of extra_t --> T) |
|
416 | NONE => NONE) |
|
417 (nth us (length us - 2)) |
|
418 end |
|
419 else if s' = type_guard_name then |
|
420 @{const True} (* ignore type predicates *) |
|
421 else |
|
422 let |
|
423 val new_skolem = String.isPrefix new_skolem_const_prefix s'' |
|
424 val num_ty_args = |
|
425 length us - the_default 0 (Symtab.lookup sym_tab s) |
|
426 val (type_us, term_us) = |
|
427 chop num_ty_args us |>> append mangled_us |
|
428 val term_ts = map (do_term [] NONE) term_us |
|
429 val T = |
|
430 (if not (null type_us) andalso |
|
431 num_type_args thy s' = length type_us then |
|
432 let val Ts = type_us |> map (typ_from_atp ctxt) in |
|
433 if new_skolem then |
|
434 SOME (Type_Infer.paramify_vars (tl Ts ---> hd Ts)) |
|
435 else if textual then |
|
436 try (Sign.const_instance thy) (s', Ts) |
|
437 else |
|
438 NONE |
|
439 end |
|
440 else |
|
441 NONE) |
|
442 |> (fn SOME T => T |
|
443 | NONE => map slack_fastype_of term_ts ---> |
|
444 (case opt_T of |
|
445 SOME T => T |
|
446 | NONE => HOLogic.typeT)) |
|
447 val t = |
|
448 if new_skolem then |
|
449 Var ((new_skolem_var_name_from_const s'', var_index), T) |
|
450 else |
|
451 Const (unproxify_const s', T) |
|
452 in list_comb (t, term_ts @ extra_ts) end |
|
453 end |
|
454 | NONE => (* a free or schematic variable *) |
|
455 let |
|
456 val term_ts = map (do_term [] NONE) us |
|
457 val ts = term_ts @ extra_ts |
|
458 val T = |
|
459 case opt_T of |
|
460 SOME T => map slack_fastype_of term_ts ---> T |
|
461 | NONE => map slack_fastype_of ts ---> HOLogic.typeT |
|
462 val t = |
|
463 case unprefix_and_unascii fixed_var_prefix s of |
|
464 SOME s => Free (s, T) |
|
465 | NONE => |
|
466 case unprefix_and_unascii schematic_var_prefix s of |
|
467 SOME s => Var ((s, var_index), T) |
|
468 | NONE => |
|
469 Var ((s |> textual ? repair_variable_name Char.toLower, |
|
470 var_index), T) |
|
471 in list_comb (t, ts) end |
|
472 in do_term [] end |
|
473 |
|
474 fun term_from_atom ctxt textual sym_tab pos (u as ATerm (s, _)) = |
|
475 if String.isPrefix class_prefix s then |
|
476 add_type_constraint pos (type_constraint_from_term ctxt u) |
|
477 #> pair @{const True} |
|
478 else |
|
479 pair (term_from_atp ctxt textual sym_tab (SOME @{typ bool}) u) |
|
480 |
|
481 val combinator_table = |
|
482 [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def_raw}), |
|
483 (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def_raw}), |
|
484 (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def_raw}), |
|
485 (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def_raw}), |
|
486 (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def_raw})] |
|
487 |
|
488 fun uncombine_term thy = |
|
489 let |
|
490 fun aux (t1 $ t2) = betapply (pairself aux (t1, t2)) |
|
491 | aux (Abs (s, T, t')) = Abs (s, T, aux t') |
|
492 | aux (t as Const (x as (s, _))) = |
|
493 (case AList.lookup (op =) combinator_table s of |
|
494 SOME thm => thm |> prop_of |> specialize_type thy x |
|
495 |> Logic.dest_equals |> snd |
|
496 | NONE => t) |
|
497 | aux t = t |
|
498 in aux end |
|
499 |
|
500 (* Update schematic type variables with detected sort constraints. It's not |
|
501 totally clear whether this code is necessary. *) |
|
502 fun repair_tvar_sorts (t, tvar_tab) = |
|
503 let |
|
504 fun do_type (Type (a, Ts)) = Type (a, map do_type Ts) |
|
505 | do_type (TVar (xi, s)) = |
|
506 TVar (xi, the_default s (Vartab.lookup tvar_tab xi)) |
|
507 | do_type (TFree z) = TFree z |
|
508 fun do_term (Const (a, T)) = Const (a, do_type T) |
|
509 | do_term (Free (a, T)) = Free (a, do_type T) |
|
510 | do_term (Var (xi, T)) = Var (xi, do_type T) |
|
511 | do_term (t as Bound _) = t |
|
512 | do_term (Abs (a, T, t)) = Abs (a, do_type T, do_term t) |
|
513 | do_term (t1 $ t2) = do_term t1 $ do_term t2 |
|
514 in t |> not (Vartab.is_empty tvar_tab) ? do_term end |
|
515 |
|
516 fun quantify_over_var quant_of var_s t = |
|
517 let |
|
518 val vars = [] |> Term.add_vars t |> filter (fn ((s, _), _) => s = var_s) |
|
519 |> map Var |
|
520 in fold_rev quant_of vars t end |
|
521 |
|
522 (* Interpret an ATP formula as a HOL term, extracting sort constraints as they |
|
523 appear in the formula. *) |
|
524 fun prop_from_atp ctxt textual sym_tab phi = |
|
525 let |
|
526 fun do_formula pos phi = |
|
527 case phi of |
|
528 AQuant (_, [], phi) => do_formula pos phi |
|
529 | AQuant (q, (s, _) :: xs, phi') => |
|
530 do_formula pos (AQuant (q, xs, phi')) |
|
531 (* FIXME: TFF *) |
|
532 #>> quantify_over_var (case q of |
|
533 AForall => forall_of |
|
534 | AExists => exists_of) |
|
535 (s |> textual ? repair_variable_name Char.toLower) |
|
536 | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not |
|
537 | AConn (c, [phi1, phi2]) => |
|
538 do_formula (pos |> c = AImplies ? not) phi1 |
|
539 ##>> do_formula pos phi2 |
|
540 #>> (case c of |
|
541 AAnd => s_conj |
|
542 | AOr => s_disj |
|
543 | AImplies => s_imp |
|
544 | AIff => s_iff |
|
545 | ANot => raise Fail "impossible connective") |
|
546 | AAtom tm => term_from_atom ctxt textual sym_tab pos tm |
|
547 | _ => raise FORMULA [phi] |
|
548 in repair_tvar_sorts (do_formula true phi Vartab.empty) end |
|
549 |
|
550 fun infer_formula_types ctxt = |
|
551 Type.constraint HOLogic.boolT |
|
552 #> Syntax.check_term |
|
553 (Proof_Context.set_mode Proof_Context.mode_schematic ctxt) |
|
554 |
|
555 fun uncombined_etc_prop_from_atp ctxt textual sym_tab = |
|
556 let val thy = Proof_Context.theory_of ctxt in |
|
557 prop_from_atp ctxt textual sym_tab |
|
558 #> textual ? uncombine_term thy #> infer_formula_types ctxt |
|
559 end |
|
560 |
|
561 (**** Translation of TSTP files to Isar proofs ****) |
|
562 |
|
563 fun unvarify_term (Var ((s, 0), T)) = Free (s, T) |
|
564 | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t]) |
|
565 |
|
566 fun decode_line sym_tab (Definition (name, phi1, phi2)) ctxt = |
|
567 let |
|
568 val thy = Proof_Context.theory_of ctxt |
|
569 val t1 = prop_from_atp ctxt true sym_tab phi1 |
|
570 val vars = snd (strip_comb t1) |
|
571 val frees = map unvarify_term vars |
|
572 val unvarify_args = subst_atomic (vars ~~ frees) |
|
573 val t2 = prop_from_atp ctxt true sym_tab phi2 |
|
574 val (t1, t2) = |
|
575 HOLogic.eq_const HOLogic.typeT $ t1 $ t2 |
|
576 |> unvarify_args |> uncombine_term thy |> infer_formula_types ctxt |
|
577 |> HOLogic.dest_eq |
|
578 in |
|
579 (Definition (name, t1, t2), |
|
580 fold Variable.declare_term (maps Misc_Legacy.term_frees [t1, t2]) ctxt) |
|
581 end |
|
582 | decode_line sym_tab (Inference (name, u, rule, deps)) ctxt = |
|
583 let val t = u |> uncombined_etc_prop_from_atp ctxt true sym_tab in |
|
584 (Inference (name, t, rule, deps), |
|
585 fold Variable.declare_term (Misc_Legacy.term_frees t) ctxt) |
|
586 end |
|
587 fun decode_lines ctxt sym_tab lines = |
|
588 fst (fold_map (decode_line sym_tab) lines ctxt) |
|
589 |
|
590 fun is_same_inference _ (Definition _) = false |
|
591 | is_same_inference t (Inference (_, t', _, _)) = t aconv t' |
|
592 |
|
593 (* No "real" literals means only type information (tfree_tcs, clsrel, or |
|
594 clsarity). *) |
|
595 val is_only_type_information = curry (op aconv) @{term True} |
|
596 |
|
597 fun replace_one_dependency (old, new) dep = |
|
598 if is_same_atp_step dep old then new else [dep] |
|
599 fun replace_dependencies_in_line _ (line as Definition _) = line |
|
600 | replace_dependencies_in_line p (Inference (name, t, rule, deps)) = |
|
601 Inference (name, t, rule, |
|
602 fold (union (op =) o replace_one_dependency p) deps []) |
|
603 |
|
604 (* Discard facts; consolidate adjacent lines that prove the same formula, since |
|
605 they differ only in type information.*) |
|
606 fun add_line _ (line as Definition _) lines = line :: lines |
|
607 | add_line fact_names (Inference (name as (_, ss), t, rule, [])) lines = |
|
608 (* No dependencies: fact, conjecture, or (for Vampire) internal facts or |
|
609 definitions. *) |
|
610 if is_fact fact_names ss then |
|
611 (* Facts are not proof lines. *) |
|
612 if is_only_type_information t then |
|
613 map (replace_dependencies_in_line (name, [])) lines |
|
614 (* Is there a repetition? If so, replace later line by earlier one. *) |
|
615 else case take_prefix (not o is_same_inference t) lines of |
|
616 (_, []) => lines (* no repetition of proof line *) |
|
617 | (pre, Inference (name', _, _, _) :: post) => |
|
618 pre @ map (replace_dependencies_in_line (name', [name])) post |
|
619 | _ => raise Fail "unexpected inference" |
|
620 else if is_conjecture ss then |
|
621 Inference (name, s_not t, rule, []) :: lines |
|
622 else |
|
623 map (replace_dependencies_in_line (name, [])) lines |
|
624 | add_line _ (Inference (name, t, rule, deps)) lines = |
|
625 (* Type information will be deleted later; skip repetition test. *) |
|
626 if is_only_type_information t then |
|
627 Inference (name, t, rule, deps) :: lines |
|
628 (* Is there a repetition? If so, replace later line by earlier one. *) |
|
629 else case take_prefix (not o is_same_inference t) lines of |
|
630 (* FIXME: Doesn't this code risk conflating proofs involving different |
|
631 types? *) |
|
632 (_, []) => Inference (name, t, rule, deps) :: lines |
|
633 | (pre, Inference (name', t', rule, _) :: post) => |
|
634 Inference (name, t', rule, deps) :: |
|
635 pre @ map (replace_dependencies_in_line (name', [name])) post |
|
636 | _ => raise Fail "unexpected inference" |
|
637 |
|
638 (* Recursively delete empty lines (type information) from the proof. *) |
|
639 fun add_nontrivial_line (line as Inference (name, t, _, [])) lines = |
|
640 if is_only_type_information t then delete_dependency name lines |
|
641 else line :: lines |
|
642 | add_nontrivial_line line lines = line :: lines |
|
643 and delete_dependency name lines = |
|
644 fold_rev add_nontrivial_line |
|
645 (map (replace_dependencies_in_line (name, [])) lines) [] |
|
646 |
|
647 (* ATPs sometimes reuse free variable names in the strangest ways. Removing |
|
648 offending lines often does the trick. *) |
|
649 fun is_bad_free frees (Free x) = not (member (op =) frees x) |
|
650 | is_bad_free _ _ = false |
|
651 |
|
652 fun add_desired_line _ _ _ (line as Definition (name, _, _)) (j, lines) = |
|
653 (j, line :: map (replace_dependencies_in_line (name, [])) lines) |
|
654 | add_desired_line isar_shrink_factor fact_names frees |
|
655 (Inference (name as (_, ss), t, rule, deps)) (j, lines) = |
|
656 (j + 1, |
|
657 if is_fact fact_names ss orelse |
|
658 is_conjecture ss orelse |
|
659 (* the last line must be kept *) |
|
660 j = 0 orelse |
|
661 (not (is_only_type_information t) andalso |
|
662 null (Term.add_tvars t []) andalso |
|
663 not (exists_subterm (is_bad_free frees) t) andalso |
|
664 length deps >= 2 andalso j mod isar_shrink_factor = 0 andalso |
|
665 (* kill next to last line, which usually results in a trivial step *) |
|
666 j <> 1) then |
|
667 Inference (name, t, rule, deps) :: lines (* keep line *) |
|
668 else |
|
669 map (replace_dependencies_in_line (name, deps)) lines) (* drop line *) |
|
670 |
|
671 (** Isar proof construction and manipulation **) |
|
672 |
|
673 type label = string * int |
|
674 type facts = label list * string list |
|
675 |
|
676 datatype isar_qualifier = Show | Then | Moreover | Ultimately |
|
677 |
|
678 datatype isar_step = |
|
679 Fix of (string * typ) list | |
|
680 Let of term * term | |
|
681 Assume of label * term | |
|
682 Prove of isar_qualifier list * label * term * byline |
|
683 and byline = |
|
684 By_Metis of facts | |
|
685 Case_Split of isar_step list list * facts |
|
686 |
|
687 fun add_fact_from_dependency fact_names (name as (_, ss)) = |
|
688 if is_fact fact_names ss then |
|
689 apsnd (union (op =) (map fst (resolve_fact fact_names ss))) |
|
690 else |
|
691 apfst (insert (op =) (raw_label_for_name name)) |
|
692 |
|
693 fun repair_name "$true" = "c_True" |
|
694 | repair_name "$false" = "c_False" |
|
695 | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *) |
|
696 | repair_name s = |
|
697 if is_tptp_equal s orelse |
|
698 (* seen in Vampire proofs *) |
|
699 (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then |
|
700 tptp_equal |
|
701 else |
|
702 s |
|
703 |
|
704 (* FIXME: Still needed? Try with SPASS proofs perhaps. *) |
|
705 val kill_duplicate_assumptions_in_proof = |
|
706 let |
|
707 fun relabel_facts subst = |
|
708 apfst (map (fn l => AList.lookup (op =) subst l |> the_default l)) |
|
709 fun do_step (step as Assume (l, t)) (proof, subst, assums) = |
|
710 (case AList.lookup (op aconv) assums t of |
|
711 SOME l' => (proof, (l, l') :: subst, assums) |
|
712 | NONE => (step :: proof, subst, (t, l) :: assums)) |
|
713 | do_step (Prove (qs, l, t, by)) (proof, subst, assums) = |
|
714 (Prove (qs, l, t, |
|
715 case by of |
|
716 By_Metis facts => By_Metis (relabel_facts subst facts) |
|
717 | Case_Split (proofs, facts) => |
|
718 Case_Split (map do_proof proofs, |
|
719 relabel_facts subst facts)) :: |
|
720 proof, subst, assums) |
|
721 | do_step step (proof, subst, assums) = (step :: proof, subst, assums) |
|
722 and do_proof proof = fold do_step proof ([], [], []) |> #1 |> rev |
|
723 in do_proof end |
|
724 |
|
725 fun used_labels_of_step (Prove (_, _, _, by)) = |
|
726 (case by of |
|
727 By_Metis (ls, _) => ls |
|
728 | Case_Split (proofs, (ls, _)) => |
|
729 fold (union (op =) o used_labels_of) proofs ls) |
|
730 | used_labels_of_step _ = [] |
|
731 and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof [] |
|
732 |
|
733 fun kill_useless_labels_in_proof proof = |
|
734 let |
|
735 val used_ls = used_labels_of proof |
|
736 fun do_label l = if member (op =) used_ls l then l else no_label |
|
737 fun do_step (Assume (l, t)) = Assume (do_label l, t) |
|
738 | do_step (Prove (qs, l, t, by)) = |
|
739 Prove (qs, do_label l, t, |
|
740 case by of |
|
741 Case_Split (proofs, facts) => |
|
742 Case_Split (map (map do_step) proofs, facts) |
|
743 | _ => by) |
|
744 | do_step step = step |
|
745 in map do_step proof end |
|
746 |
|
747 fun prefix_for_depth n = replicate_string (n + 1) |
|
748 |
|
749 val relabel_proof = |
|
750 let |
|
751 fun aux _ _ _ [] = [] |
|
752 | aux subst depth (next_assum, next_fact) (Assume (l, t) :: proof) = |
|
753 if l = no_label then |
|
754 Assume (l, t) :: aux subst depth (next_assum, next_fact) proof |
|
755 else |
|
756 let val l' = (prefix_for_depth depth assum_prefix, next_assum) in |
|
757 Assume (l', t) :: |
|
758 aux ((l, l') :: subst) depth (next_assum + 1, next_fact) proof |
|
759 end |
|
760 | aux subst depth (next_assum, next_fact) |
|
761 (Prove (qs, l, t, by) :: proof) = |
|
762 let |
|
763 val (l', subst, next_fact) = |
|
764 if l = no_label then |
|
765 (l, subst, next_fact) |
|
766 else |
|
767 let |
|
768 val l' = (prefix_for_depth depth have_prefix, next_fact) |
|
769 in (l', (l, l') :: subst, next_fact + 1) end |
|
770 val relabel_facts = |
|
771 apfst (maps (the_list o AList.lookup (op =) subst)) |
|
772 val by = |
|
773 case by of |
|
774 By_Metis facts => By_Metis (relabel_facts facts) |
|
775 | Case_Split (proofs, facts) => |
|
776 Case_Split (map (aux subst (depth + 1) (1, 1)) proofs, |
|
777 relabel_facts facts) |
|
778 in |
|
779 Prove (qs, l', t, by) :: aux subst depth (next_assum, next_fact) proof |
|
780 end |
|
781 | aux subst depth nextp (step :: proof) = |
|
782 step :: aux subst depth nextp proof |
|
783 in aux [] 0 (1, 1) end |
|
784 |
|
785 fun string_for_proof ctxt0 type_enc lam_trans i n = |
|
786 let |
|
787 val ctxt = |
|
788 ctxt0 |> Config.put show_free_types false |
|
789 |> Config.put show_types true |
|
790 |> Config.put show_sorts true |
|
791 fun fix_print_mode f x = |
|
792 Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN) |
|
793 (print_mode_value ())) f x |
|
794 fun do_indent ind = replicate_string (ind * indent_size) " " |
|
795 fun do_free (s, T) = |
|
796 maybe_quote s ^ " :: " ^ |
|
797 maybe_quote (fix_print_mode (Syntax.string_of_typ ctxt) T) |
|
798 fun do_label l = if l = no_label then "" else string_for_label l ^ ": " |
|
799 fun do_have qs = |
|
800 (if member (op =) qs Moreover then "moreover " else "") ^ |
|
801 (if member (op =) qs Ultimately then "ultimately " else "") ^ |
|
802 (if member (op =) qs Then then |
|
803 if member (op =) qs Show then "thus" else "hence" |
|
804 else |
|
805 if member (op =) qs Show then "show" else "have") |
|
806 val do_term = maybe_quote o fix_print_mode (Syntax.string_of_term ctxt) |
|
807 val reconstr = Metis (type_enc, lam_trans) |
|
808 fun do_facts (ls, ss) = |
|
809 reconstructor_command reconstr 1 1 |
|
810 (ls |> sort_distinct (prod_ord string_ord int_ord), |
|
811 ss |> sort_distinct string_ord) |
|
812 and do_step ind (Fix xs) = |
|
813 do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n" |
|
814 | do_step ind (Let (t1, t2)) = |
|
815 do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n" |
|
816 | do_step ind (Assume (l, t)) = |
|
817 do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n" |
|
818 | do_step ind (Prove (qs, l, t, By_Metis facts)) = |
|
819 do_indent ind ^ do_have qs ^ " " ^ |
|
820 do_label l ^ do_term t ^ " " ^ do_facts facts ^ "\n" |
|
821 | do_step ind (Prove (qs, l, t, Case_Split (proofs, facts))) = |
|
822 implode (map (prefix (do_indent ind ^ "moreover\n") o do_block ind) |
|
823 proofs) ^ |
|
824 do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ " " ^ |
|
825 do_facts facts ^ "\n" |
|
826 and do_steps prefix suffix ind steps = |
|
827 let val s = implode (map (do_step ind) steps) in |
|
828 replicate_string (ind * indent_size - size prefix) " " ^ prefix ^ |
|
829 String.extract (s, ind * indent_size, |
|
830 SOME (size s - ind * indent_size - 1)) ^ |
|
831 suffix ^ "\n" |
|
832 end |
|
833 and do_block ind proof = do_steps "{ " " }" (ind + 1) proof |
|
834 (* One-step proofs are pointless; better use the Metis one-liner |
|
835 directly. *) |
|
836 and do_proof [Prove (_, _, _, By_Metis _)] = "" |
|
837 | do_proof proof = |
|
838 (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^ |
|
839 do_indent 0 ^ "proof -\n" ^ do_steps "" "" 1 proof ^ do_indent 0 ^ |
|
840 (if n <> 1 then "next" else "qed") |
|
841 in do_proof end |
|
842 |
|
843 fun isar_proof_text ctxt isar_proof_requested |
|
844 (debug, isar_shrink_factor, pool, fact_names, sym_tab, atp_proof, goal) |
|
845 (one_line_params as (_, _, _, _, subgoal, subgoal_count)) = |
|
846 let |
|
847 val isar_shrink_factor = |
|
848 (if isar_proof_requested then 1 else 2) * isar_shrink_factor |
|
849 val (params, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal |
|
850 val frees = fold Term.add_frees (concl_t :: hyp_ts) [] |
|
851 val one_line_proof = one_line_proof_text one_line_params |
|
852 val type_enc = |
|
853 if is_typed_helper_used_in_atp_proof atp_proof then full_typesN |
|
854 else partial_typesN |
|
855 val lam_trans = lam_trans_from_atp_proof atp_proof metis_default_lam_trans |
|
856 |
|
857 fun isar_proof_of () = |
|
858 let |
|
859 val atp_proof = |
|
860 atp_proof |
|
861 |> clean_up_atp_proof_dependencies |
|
862 |> nasty_atp_proof pool |
|
863 |> map_term_names_in_atp_proof repair_name |
|
864 |> decode_lines ctxt sym_tab |
|
865 |> rpair [] |-> fold_rev (add_line fact_names) |
|
866 |> rpair [] |-> fold_rev add_nontrivial_line |
|
867 |> rpair (0, []) |
|
868 |-> fold_rev (add_desired_line isar_shrink_factor fact_names frees) |
|
869 |> snd |
|
870 val conj_name = conjecture_prefix ^ string_of_int (length hyp_ts) |
|
871 val conjs = |
|
872 atp_proof |
|
873 |> map_filter (fn Inference (name as (_, ss), _, _, []) => |
|
874 if member (op =) ss conj_name then SOME name else NONE |
|
875 | _ => NONE) |
|
876 fun dep_of_step (Definition _) = NONE |
|
877 | dep_of_step (Inference (name, _, _, from)) = SOME (from, name) |
|
878 val ref_graph = atp_proof |> map_filter dep_of_step |> make_ref_graph |
|
879 val axioms = axioms_of_ref_graph ref_graph conjs |
|
880 val tainted = tainted_atoms_of_ref_graph ref_graph conjs |
|
881 val props = |
|
882 Symtab.empty |
|
883 |> fold (fn Definition _ => I (* FIXME *) |
|
884 | Inference ((s, _), t, _, _) => |
|
885 Symtab.update_new (s, |
|
886 t |> member (op = o apsnd fst) tainted s ? s_not)) |
|
887 atp_proof |
|
888 (* FIXME: add "fold_rev forall_of (map Var (Term.add_vars t []))"? *) |
|
889 fun prop_of_clause c = |
|
890 fold (curry s_disj) (map_filter (Symtab.lookup props o fst) c) |
|
891 @{term False} |
|
892 fun label_of_clause c = (space_implode "___" (map fst c), 0) |
|
893 fun maybe_show outer c = |
|
894 (outer andalso length c = 1 andalso subset (op =) (c, conjs)) |
|
895 ? cons Show |
|
896 fun do_have outer qs (gamma, c) = |
|
897 Prove (maybe_show outer c qs, label_of_clause c, prop_of_clause c, |
|
898 By_Metis (fold (add_fact_from_dependency fact_names |
|
899 o the_single) gamma ([], []))) |
|
900 fun do_inf outer (Have z) = do_have outer [] z |
|
901 | do_inf outer (Hence z) = do_have outer [Then] z |
|
902 | do_inf outer (Cases cases) = |
|
903 let val c = succedent_of_cases cases in |
|
904 Prove (maybe_show outer c [Ultimately], label_of_clause c, |
|
905 prop_of_clause c, |
|
906 Case_Split (map (do_case false) cases, ([], []))) |
|
907 end |
|
908 and do_case outer (c, infs) = |
|
909 Assume (label_of_clause c, prop_of_clause c) :: |
|
910 map (do_inf outer) infs |
|
911 val isar_proof = |
|
912 (if null params then [] else [Fix params]) @ |
|
913 (ref_graph |
|
914 |> redirect_graph axioms tainted |
|
915 |> chain_direct_proof |
|
916 |> map (do_inf true) |
|
917 |> kill_duplicate_assumptions_in_proof |
|
918 |> kill_useless_labels_in_proof |
|
919 |> relabel_proof) |
|
920 |> string_for_proof ctxt type_enc lam_trans subgoal subgoal_count |
|
921 in |
|
922 case isar_proof of |
|
923 "" => |
|
924 if isar_proof_requested then |
|
925 "\nNo structured proof available (proof too short)." |
|
926 else |
|
927 "" |
|
928 | _ => |
|
929 "\n\n" ^ (if isar_proof_requested then "Structured proof" |
|
930 else "Perhaps this will work") ^ |
|
931 ":\n" ^ Markup.markup Isabelle_Markup.sendback isar_proof |
|
932 end |
|
933 val isar_proof = |
|
934 if debug then |
|
935 isar_proof_of () |
|
936 else case try isar_proof_of () of |
|
937 SOME s => s |
|
938 | NONE => if isar_proof_requested then |
|
939 "\nWarning: The Isar proof construction failed." |
|
940 else |
|
941 "" |
|
942 in one_line_proof ^ isar_proof end |
|
943 |
|
944 fun proof_text ctxt isar_proof isar_params |
|
945 (one_line_params as (preplay, _, _, _, _, _)) = |
|
946 (if case preplay of Failed_to_Play _ => true | _ => isar_proof then |
|
947 isar_proof_text ctxt isar_proof isar_params |
|
948 else |
|
949 one_line_proof_text) one_line_params |
|
950 |
|
951 end; |
|