1 (* Title: HOL/Tools/ATP/atp_proof_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
6 Proof reconstruction from ATP proofs.
9 signature ATP_PROOF_RECONSTRUCT =
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 stature = ATP_Problem_Generate.stature
16 datatype reconstructor =
17 Metis of string * string |
21 Played of reconstructor * Time.time |
22 Trust_Playable of reconstructor * Time.time option |
23 Failed_to_Play of reconstructor
25 type minimize_command = string list -> string
26 type one_line_params =
27 play * string * (string * stature) list * minimize_command * int * int
29 bool * int * string Symtab.table * (string * stature) list vector
30 * int Symtab.table * string proof * thm
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 * stature) list vector -> string proof
48 -> (string * stature) 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 * stature) list vector -> 'a proof
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
59 Proof.context -> bool -> int Symtab.table -> typ option
60 -> (string, string) ho_term -> term
62 Proof.context -> bool -> int Symtab.table
63 -> (string, string, (string, string) ho_term) formula -> term
65 Proof.context -> bool -> isar_params -> one_line_params -> string
67 Proof.context -> bool -> isar_params -> one_line_params -> string
70 structure ATP_Proof_Reconstruct : ATP_PROOF_RECONSTRUCT =
76 open ATP_Problem_Generate
78 structure String_Redirect = ATP_Proof_Redirect(
80 val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s')
85 datatype reconstructor =
86 Metis of string * string |
90 Played of reconstructor * Time.time |
91 Trust_Playable of reconstructor * Time.time option |
92 Failed_to_Play of reconstructor
94 type minimize_command = string list -> string
95 type one_line_params =
96 play * string * (string * stature) list * minimize_command * int * int
98 bool * int * string Symtab.table * (string * stature) list vector
99 * int Symtab.table * string proof * thm
104 val full_typesN = "full_types"
105 val partial_typesN = "partial_types"
106 val no_typesN = "no_types"
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"
113 val full_type_encs = [full_type_enc, really_full_type_enc]
114 val partial_type_encs = partial_type_enc :: full_type_encs
116 val type_enc_aliases =
117 [(full_typesN, full_type_encs),
118 (partial_typesN, partial_type_encs),
119 (no_typesN, [no_type_enc])]
121 fun unalias_type_enc s =
122 AList.lookup (op =) type_enc_aliases s |> the_default [s]
124 val metis_default_lam_trans = combsN
126 fun metis_call type_enc lam_trans =
129 case AList.find (fn (enc, encs) => enc = hd encs) type_enc_aliases
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
137 fun string_for_reconstructor (Metis (type_enc, lam_trans)) =
138 metis_call type_enc lam_trans
139 | string_for_reconstructor SMT = smtN
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
145 val unprefix_fact_number = space_implode "_" o tl o space_explode "_"
147 fun resolve_one_named_fact fact_names s =
148 case try (unprefix fact_prefix) s of
150 let val s' = s' |> unprefix_fact_number |> unascii_of in
151 s' |> find_first_in_list_vector fact_names |> Option.map (pair s')
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
157 fun resolve_one_named_conjecture s =
158 case try (unprefix conjecture_prefix) s of
159 SOME s' => Int.fromString s'
162 val resolve_conjecture = map_filter resolve_one_named_conjecture
163 val is_conjecture = not o null o resolve_conjecture
165 fun is_axiom_used_in_proof pred =
166 exists (fn Inference ((_, ss), _, _, []) => exists pred ss | _ => false)
168 val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix)
170 val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix
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
177 fun lam_trans_from_atp_proof atp_proof default =
178 case (is_axiom_used_in_proof is_combinator_def atp_proof,
179 is_axiom_used_in_proof is_lam_lifted atp_proof) of
180 (false, false) => default
181 | (false, true) => liftingN
182 (* | (true, true) => combs_and_liftingN -- not supported by "metis" *)
183 | (true, _) => combsN
185 val is_typed_helper_name =
186 String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix
187 fun is_typed_helper_used_in_atp_proof atp_proof =
188 is_axiom_used_in_proof is_typed_helper_name atp_proof
190 val leo2_ext = "extcnf_equal_neg"
191 val isa_ext = Thm.get_name_hint @{thm ext}
192 val isa_short_ext = Long_Name.base_name isa_ext
195 if Thm.eq_thm_prop (@{thm ext},
196 singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then
201 fun add_fact _ fact_names (Inference ((_, ss), _, _, [])) =
202 union (op =) (resolve_fact fact_names ss)
203 | add_fact ctxt _ (Inference (_, _, rule, _)) =
204 if rule = leo2_ext then insert (op =) (ext_name ctxt, (Global, General))
208 fun used_facts_in_atp_proof ctxt fact_names atp_proof =
209 if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names
210 else fold (add_fact ctxt fact_names) atp_proof []
212 fun used_facts_in_unsound_atp_proof _ _ [] = NONE
213 | used_facts_in_unsound_atp_proof ctxt fact_names atp_proof =
214 let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in
215 if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso
216 not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then
217 SOME (map fst used_facts)
223 (** Soft-core proof reconstruction: one-liners **)
225 fun string_for_label (s, num) = s ^ string_of_int num
227 fun show_time NONE = ""
228 | show_time (SOME ext_time) = " (" ^ string_from_ext_time ext_time ^ ")"
230 fun apply_on_subgoal _ 1 = "by "
231 | apply_on_subgoal 1 _ = "apply "
232 | apply_on_subgoal i n =
233 "prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal 1 n
234 fun command_call name [] =
235 name |> not (Lexicon.is_identifier name) ? enclose "(" ")"
236 | command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")"
237 fun try_command_line banner time command =
238 banner ^ ": " ^ Markup.markup Isabelle_Markup.sendback command ^ show_time time ^ "."
239 fun using_labels [] = ""
241 "using " ^ space_implode " " (map string_for_label ls) ^ " "
242 fun reconstructor_command reconstr i n (ls, ss) =
243 using_labels ls ^ apply_on_subgoal i n ^
244 command_call (string_for_reconstructor reconstr) ss
245 fun minimize_line _ [] = ""
246 | minimize_line minimize_command ss =
247 case minimize_command ss of
250 "\nTo minimize: " ^ Markup.markup Isabelle_Markup.sendback command ^ "."
252 fun split_used_facts facts =
253 facts |> List.partition (fn (_, (sc, _)) => sc = Chained)
254 |> pairself (sort_distinct (string_ord o pairself fst))
256 fun one_line_proof_text (preplay, banner, used_facts, minimize_command,
257 subgoal, subgoal_count) =
259 val (chained, extra) = split_used_facts used_facts
260 val (failed, reconstr, ext_time) =
262 Played (reconstr, time) => (false, reconstr, (SOME (false, time)))
263 | Trust_Playable (reconstr, time) =>
268 if time = Time.zeroTime then NONE else SOME (true, time))
269 | Failed_to_Play reconstr => (true, reconstr, NONE)
272 |> reconstructor_command reconstr subgoal subgoal_count
273 |> (if failed then enclose "One-line proof reconstruction failed: " "."
274 else try_command_line banner ext_time)
275 in try_line ^ minimize_line minimize_command (map fst (extra @ chained)) end
277 (** Hard-core proof reconstruction: structured Isar proofs **)
279 fun forall_of v t = HOLogic.all_const (fastype_of v) $ lambda v t
280 fun exists_of v t = HOLogic.exists_const (fastype_of v) $ lambda v t
282 fun make_tvar s = TVar (("'" ^ s, 0), HOLogic.typeS)
283 fun make_tfree ctxt w =
284 let val ww = "'" ^ w in
285 TFree (ww, the_default HOLogic.typeS (Variable.def_sort ctxt (ww, ~1)))
289 val no_label = ("", ~1)
292 val assum_prefix = "a"
293 val have_prefix = "f"
295 fun raw_label_for_name (num, ss) =
296 case resolve_conjecture ss of
297 [j] => (conjecture_prefix, j)
298 | _ => case Int.fromString num of
299 SOME j => (raw_prefix, j)
300 | NONE => (raw_prefix ^ num, 0)
302 (**** INTERPRETATION OF TSTP SYNTAX TREES ****)
304 exception HO_TERM of (string, string) ho_term list
305 exception FORMULA of (string, string, (string, string) ho_term) formula list
306 exception SAME of unit
308 (* Type variables are given the basic sort "HOL.type". Some will later be
309 constrained by information from type literals, or by type inference. *)
310 fun typ_from_atp ctxt (u as ATerm (a, us)) =
311 let val Ts = map (typ_from_atp ctxt) us in
312 case unprefix_and_unascii type_const_prefix a of
313 SOME b => Type (invert_const b, Ts)
315 if not (null us) then
316 raise HO_TERM [u] (* only "tconst"s have type arguments *)
317 else case unprefix_and_unascii tfree_prefix a of
318 SOME b => make_tfree ctxt b
320 (* Could be an Isabelle variable or a variable from the ATP, say "X1"
321 or "_5018". Sometimes variables from the ATP are indistinguishable
322 from Isabelle variables, which forces us to use a type parameter in
324 (a |> perhaps (unprefix_and_unascii tvar_prefix), HOLogic.typeS)
325 |> Type_Infer.param 0
328 (* Type class literal applied to a type. Returns triple of polarity, class,
330 fun type_constraint_from_term ctxt (u as ATerm (a, us)) =
331 case (unprefix_and_unascii class_prefix a, map (typ_from_atp ctxt) us) of
332 (SOME b, [T]) => (b, T)
333 | _ => raise HO_TERM [u]
335 (* Accumulate type constraints in a formula: negative type literals. *)
336 fun add_var (key, z) = Vartab.map_default (key, []) (cons z)
337 fun add_type_constraint false (cl, TFree (a ,_)) = add_var ((a, ~1), cl)
338 | add_type_constraint false (cl, TVar (ix, _)) = add_var (ix, cl)
339 | add_type_constraint _ _ = I
341 fun repair_variable_name f s =
343 fun subscript_name s n = s ^ nat_subscript n
344 val s = String.map f s
346 case space_explode "_" s of
347 [_] => (case take_suffix Char.isDigit (String.explode s) of
348 (cs1 as _ :: _, cs2 as _ :: _) =>
349 subscript_name (String.implode cs1)
350 (the (Int.fromString (String.implode cs2)))
352 | [s1, s2] => (case Int.fromString s2 of
353 SOME n => subscript_name s1 n
358 (* The number of type arguments of a constant, zero if it's monomorphic. For
359 (instances of) Skolem pseudoconstants, this information is encoded in the
361 fun num_type_args thy s =
362 if String.isPrefix skolem_const_prefix s then
363 s |> space_explode Long_Name.separator |> List.last |> Int.fromString |> the
364 else if String.isPrefix lam_lifted_prefix s then
365 if String.isPrefix lam_lifted_poly_prefix s then 2 else 0
367 (s, Sign.the_const_type thy s) |> Sign.const_typargs thy |> length
369 fun slack_fastype_of t = fastype_of t handle TERM _ => HOLogic.typeT
371 (* First-order translation. No types are known for variables. "HOLogic.typeT"
372 should allow them to be inferred. *)
373 fun term_from_atp ctxt textual sym_tab =
375 val thy = Proof_Context.theory_of ctxt
376 (* For Metis, we use 1 rather than 0 because variable references in clauses
377 may otherwise conflict with variable constraints in the goal. At least,
378 type inference often fails otherwise. See also "axiom_inference" in
379 "Metis_Reconstruct". *)
380 val var_index = if textual then 0 else 1
381 fun do_term extra_ts opt_T u =
384 if String.isPrefix native_type_prefix s then
385 @{const True} (* ignore TPTP type information *)
386 else if s = tptp_equal then
387 let val ts = map (do_term [] NONE) us in
388 if textual andalso length ts = 2 andalso
389 hd ts aconv List.last ts then
390 (* Vampire is keen on producing these. *)
393 list_comb (Const (@{const_name HOL.eq}, HOLogic.typeT), ts)
395 else case unprefix_and_unascii const_prefix s of
398 val ((s', s''), mangled_us) =
399 s' |> unmangled_const |>> `invert_const
401 if s' = type_tag_name then
402 case mangled_us @ us of
404 do_term extra_ts (SOME (typ_from_atp ctxt typ_u)) term_u
405 | _ => raise HO_TERM us
406 else if s' = predicator_name then
407 do_term [] (SOME @{typ bool}) (hd us)
408 else if s' = app_op_name then
409 let val extra_t = do_term [] NONE (List.last us) in
410 do_term (extra_t :: extra_ts)
412 SOME T => SOME (slack_fastype_of extra_t --> T)
414 (nth us (length us - 2))
416 else if s' = type_guard_name then
417 @{const True} (* ignore type predicates *)
420 val new_skolem = String.isPrefix new_skolem_const_prefix s''
422 length us - the_default 0 (Symtab.lookup sym_tab s)
423 val (type_us, term_us) =
424 chop num_ty_args us |>> append mangled_us
425 val term_ts = map (do_term [] NONE) term_us
427 (if not (null type_us) andalso
428 num_type_args thy s' = length type_us then
429 let val Ts = type_us |> map (typ_from_atp ctxt) in
431 SOME (Type_Infer.paramify_vars (tl Ts ---> hd Ts))
433 try (Sign.const_instance thy) (s', Ts)
440 | NONE => map slack_fastype_of term_ts --->
443 | NONE => HOLogic.typeT))
446 Var ((new_skolem_var_name_from_const s'', var_index), T)
448 Const (unproxify_const s', T)
449 in list_comb (t, term_ts @ extra_ts) end
451 | NONE => (* a free or schematic variable *)
453 val term_ts = map (do_term [] NONE) us
454 val ts = term_ts @ extra_ts
457 SOME T => map slack_fastype_of term_ts ---> T
458 | NONE => map slack_fastype_of ts ---> HOLogic.typeT
460 case unprefix_and_unascii fixed_var_prefix s of
461 SOME s => Free (s, T)
463 case unprefix_and_unascii schematic_var_prefix s of
464 SOME s => Var ((s, var_index), T)
466 Var ((s |> textual ? repair_variable_name Char.toLower,
468 in list_comb (t, ts) end
471 fun term_from_atom ctxt textual sym_tab pos (u as ATerm (s, _)) =
472 if String.isPrefix class_prefix s then
473 add_type_constraint pos (type_constraint_from_term ctxt u)
474 #> pair @{const True}
476 pair (term_from_atp ctxt textual sym_tab (SOME @{typ bool}) u)
478 val combinator_table =
479 [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def_raw}),
480 (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def_raw}),
481 (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def_raw}),
482 (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def_raw}),
483 (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def_raw})]
485 fun uncombine_term thy =
487 fun aux (t1 $ t2) = betapply (pairself aux (t1, t2))
488 | aux (Abs (s, T, t')) = Abs (s, T, aux t')
489 | aux (t as Const (x as (s, _))) =
490 (case AList.lookup (op =) combinator_table s of
491 SOME thm => thm |> prop_of |> specialize_type thy x
492 |> Logic.dest_equals |> snd
497 (* Update schematic type variables with detected sort constraints. It's not
498 totally clear whether this code is necessary. *)
499 fun repair_tvar_sorts (t, tvar_tab) =
501 fun do_type (Type (a, Ts)) = Type (a, map do_type Ts)
502 | do_type (TVar (xi, s)) =
503 TVar (xi, the_default s (Vartab.lookup tvar_tab xi))
504 | do_type (TFree z) = TFree z
505 fun do_term (Const (a, T)) = Const (a, do_type T)
506 | do_term (Free (a, T)) = Free (a, do_type T)
507 | do_term (Var (xi, T)) = Var (xi, do_type T)
508 | do_term (t as Bound _) = t
509 | do_term (Abs (a, T, t)) = Abs (a, do_type T, do_term t)
510 | do_term (t1 $ t2) = do_term t1 $ do_term t2
511 in t |> not (Vartab.is_empty tvar_tab) ? do_term end
513 fun quantify_over_var quant_of var_s t =
515 val vars = [] |> Term.add_vars t |> filter (fn ((s, _), _) => s = var_s)
517 in fold_rev quant_of vars t end
519 (* Interpret an ATP formula as a HOL term, extracting sort constraints as they
520 appear in the formula. *)
521 fun prop_from_atp ctxt textual sym_tab phi =
523 fun do_formula pos phi =
525 AQuant (_, [], phi) => do_formula pos phi
526 | AQuant (q, (s, _) :: xs, phi') =>
527 do_formula pos (AQuant (q, xs, phi'))
529 #>> quantify_over_var (case q of
531 | AExists => exists_of)
532 (s |> textual ? repair_variable_name Char.toLower)
533 | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not
534 | AConn (c, [phi1, phi2]) =>
535 do_formula (pos |> c = AImplies ? not) phi1
536 ##>> do_formula pos phi2
542 | ANot => raise Fail "impossible connective")
543 | AAtom tm => term_from_atom ctxt textual sym_tab pos tm
544 | _ => raise FORMULA [phi]
545 in repair_tvar_sorts (do_formula true phi Vartab.empty) end
547 fun infer_formula_types ctxt =
548 Type.constraint HOLogic.boolT
550 (Proof_Context.set_mode Proof_Context.mode_schematic ctxt)
552 fun uncombined_etc_prop_from_atp ctxt textual sym_tab =
553 let val thy = Proof_Context.theory_of ctxt in
554 prop_from_atp ctxt textual sym_tab
555 #> textual ? uncombine_term thy #> infer_formula_types ctxt
558 (**** Translation of TSTP files to Isar proofs ****)
560 fun unvarify_term (Var ((s, 0), T)) = Free (s, T)
561 | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t])
563 fun decode_line sym_tab (Definition (name, phi1, phi2)) ctxt =
565 val thy = Proof_Context.theory_of ctxt
566 val t1 = prop_from_atp ctxt true sym_tab phi1
567 val vars = snd (strip_comb t1)
568 val frees = map unvarify_term vars
569 val unvarify_args = subst_atomic (vars ~~ frees)
570 val t2 = prop_from_atp ctxt true sym_tab phi2
572 HOLogic.eq_const HOLogic.typeT $ t1 $ t2
573 |> unvarify_args |> uncombine_term thy |> infer_formula_types ctxt
576 (Definition (name, t1, t2),
577 fold Variable.declare_term (maps Misc_Legacy.term_frees [t1, t2]) ctxt)
579 | decode_line sym_tab (Inference (name, u, rule, deps)) ctxt =
580 let val t = u |> uncombined_etc_prop_from_atp ctxt true sym_tab in
581 (Inference (name, t, rule, deps),
582 fold Variable.declare_term (Misc_Legacy.term_frees t) ctxt)
584 fun decode_lines ctxt sym_tab lines =
585 fst (fold_map (decode_line sym_tab) lines ctxt)
587 fun is_same_inference _ (Definition _) = false
588 | is_same_inference t (Inference (_, t', _, _)) = t aconv t'
590 (* No "real" literals means only type information (tfree_tcs, clsrel, or
592 val is_only_type_information = curry (op aconv) @{term True}
594 fun replace_one_dependency (old, new) dep =
595 if is_same_atp_step dep old then new else [dep]
596 fun replace_dependencies_in_line _ (line as Definition _) = line
597 | replace_dependencies_in_line p (Inference (name, t, rule, deps)) =
598 Inference (name, t, rule,
599 fold (union (op =) o replace_one_dependency p) deps [])
601 (* Discard facts; consolidate adjacent lines that prove the same formula, since
602 they differ only in type information.*)
603 fun add_line _ (line as Definition _) lines = line :: lines
604 | add_line fact_names (Inference (name as (_, ss), t, rule, [])) lines =
605 (* No dependencies: fact, conjecture, or (for Vampire) internal facts or
607 if is_fact fact_names ss then
608 (* Facts are not proof lines. *)
609 if is_only_type_information t then
610 map (replace_dependencies_in_line (name, [])) lines
611 (* Is there a repetition? If so, replace later line by earlier one. *)
612 else case take_prefix (not o is_same_inference t) lines of
613 (_, []) => lines (* no repetition of proof line *)
614 | (pre, Inference (name', _, _, _) :: post) =>
615 pre @ map (replace_dependencies_in_line (name', [name])) post
616 | _ => raise Fail "unexpected inference"
617 else if is_conjecture ss then
618 Inference (name, s_not t, rule, []) :: lines
620 map (replace_dependencies_in_line (name, [])) lines
621 | add_line _ (Inference (name, t, rule, deps)) lines =
622 (* Type information will be deleted later; skip repetition test. *)
623 if is_only_type_information t then
624 Inference (name, t, rule, deps) :: lines
625 (* Is there a repetition? If so, replace later line by earlier one. *)
626 else case take_prefix (not o is_same_inference t) lines of
627 (* FIXME: Doesn't this code risk conflating proofs involving different
629 (_, []) => Inference (name, t, rule, deps) :: lines
630 | (pre, Inference (name', t', rule, _) :: post) =>
631 Inference (name, t', rule, deps) ::
632 pre @ map (replace_dependencies_in_line (name', [name])) post
633 | _ => raise Fail "unexpected inference"
635 (* Recursively delete empty lines (type information) from the proof. *)
636 fun add_nontrivial_line (line as Inference (name, t, _, [])) lines =
637 if is_only_type_information t then delete_dependency name lines
639 | add_nontrivial_line line lines = line :: lines
640 and delete_dependency name lines =
641 fold_rev add_nontrivial_line
642 (map (replace_dependencies_in_line (name, [])) lines) []
644 (* ATPs sometimes reuse free variable names in the strangest ways. Removing
645 offending lines often does the trick. *)
646 fun is_bad_free frees (Free x) = not (member (op =) frees x)
647 | is_bad_free _ _ = false
649 fun add_desired_line _ _ _ (line as Definition (name, _, _)) (j, lines) =
650 (j, line :: map (replace_dependencies_in_line (name, [])) lines)
651 | add_desired_line isar_shrink_factor fact_names frees
652 (Inference (name as (_, ss), t, rule, deps)) (j, lines) =
654 if is_fact fact_names ss orelse
655 is_conjecture ss orelse
656 (* the last line must be kept *)
658 (not (is_only_type_information t) andalso
659 null (Term.add_tvars t []) andalso
660 not (exists_subterm (is_bad_free frees) t) andalso
661 length deps >= 2 andalso j mod isar_shrink_factor = 0 andalso
662 (* kill next to last line, which usually results in a trivial step *)
664 Inference (name, t, rule, deps) :: lines (* keep line *)
666 map (replace_dependencies_in_line (name, deps)) lines) (* drop line *)
668 (** Isar proof construction and manipulation **)
670 type label = string * int
671 type facts = label list * string list
673 datatype isar_qualifier = Show | Then | Moreover | Ultimately
676 Fix of (string * typ) list |
678 Assume of label * term |
679 Prove of isar_qualifier list * label * term * byline
682 Case_Split of isar_step list list * facts
684 fun add_fact_from_dependency fact_names (name as (_, ss)) =
685 if is_fact fact_names ss then
686 apsnd (union (op =) (map fst (resolve_fact fact_names ss)))
688 apfst (insert (op =) (raw_label_for_name name))
690 fun repair_name "$true" = "c_True"
691 | repair_name "$false" = "c_False"
692 | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
694 if is_tptp_equal s orelse
695 (* seen in Vampire proofs *)
696 (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then
701 (* FIXME: Still needed? Try with SPASS proofs perhaps. *)
702 val kill_duplicate_assumptions_in_proof =
704 fun relabel_facts subst =
705 apfst (map (fn l => AList.lookup (op =) subst l |> the_default l))
706 fun do_step (step as Assume (l, t)) (proof, subst, assums) =
707 (case AList.lookup (op aconv) assums t of
708 SOME l' => (proof, (l, l') :: subst, assums)
709 | NONE => (step :: proof, subst, (t, l) :: assums))
710 | do_step (Prove (qs, l, t, by)) (proof, subst, assums) =
713 By_Metis facts => By_Metis (relabel_facts subst facts)
714 | Case_Split (proofs, facts) =>
715 Case_Split (map do_proof proofs,
716 relabel_facts subst facts)) ::
717 proof, subst, assums)
718 | do_step step (proof, subst, assums) = (step :: proof, subst, assums)
719 and do_proof proof = fold do_step proof ([], [], []) |> #1 |> rev
722 fun used_labels_of_step (Prove (_, _, _, by)) =
724 By_Metis (ls, _) => ls
725 | Case_Split (proofs, (ls, _)) =>
726 fold (union (op =) o used_labels_of) proofs ls)
727 | used_labels_of_step _ = []
728 and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof []
730 fun kill_useless_labels_in_proof proof =
732 val used_ls = used_labels_of proof
733 fun do_label l = if member (op =) used_ls l then l else no_label
734 fun do_step (Assume (l, t)) = Assume (do_label l, t)
735 | do_step (Prove (qs, l, t, by)) =
736 Prove (qs, do_label l, t,
738 Case_Split (proofs, facts) =>
739 Case_Split (map (map do_step) proofs, facts)
741 | do_step step = step
742 in map do_step proof end
744 fun prefix_for_depth n = replicate_string (n + 1)
748 fun aux _ _ _ [] = []
749 | aux subst depth (next_assum, next_fact) (Assume (l, t) :: proof) =
751 Assume (l, t) :: aux subst depth (next_assum, next_fact) proof
753 let val l' = (prefix_for_depth depth assum_prefix, next_assum) in
755 aux ((l, l') :: subst) depth (next_assum + 1, next_fact) proof
757 | aux subst depth (next_assum, next_fact)
758 (Prove (qs, l, t, by) :: proof) =
760 val (l', subst, next_fact) =
762 (l, subst, next_fact)
765 val l' = (prefix_for_depth depth have_prefix, next_fact)
766 in (l', (l, l') :: subst, next_fact + 1) end
768 apfst (maps (the_list o AList.lookup (op =) subst))
771 By_Metis facts => By_Metis (relabel_facts facts)
772 | Case_Split (proofs, facts) =>
773 Case_Split (map (aux subst (depth + 1) (1, 1)) proofs,
776 Prove (qs, l', t, by) :: aux subst depth (next_assum, next_fact) proof
778 | aux subst depth nextp (step :: proof) =
779 step :: aux subst depth nextp proof
780 in aux [] 0 (1, 1) end
782 fun string_for_proof ctxt0 type_enc lam_trans i n =
785 ctxt0 |> Config.put show_free_types false
786 |> Config.put show_types true
787 |> Config.put show_sorts true
788 fun fix_print_mode f x =
789 Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN)
790 (print_mode_value ())) f x
791 fun do_indent ind = replicate_string (ind * indent_size) " "
793 maybe_quote s ^ " :: " ^
794 maybe_quote (fix_print_mode (Syntax.string_of_typ ctxt) T)
795 fun do_label l = if l = no_label then "" else string_for_label l ^ ": "
797 (if member (op =) qs Moreover then "moreover " else "") ^
798 (if member (op =) qs Ultimately then "ultimately " else "") ^
799 (if member (op =) qs Then then
800 if member (op =) qs Show then "thus" else "hence"
802 if member (op =) qs Show then "show" else "have")
803 val do_term = maybe_quote o fix_print_mode (Syntax.string_of_term ctxt)
804 val reconstr = Metis (type_enc, lam_trans)
805 fun do_facts (ls, ss) =
806 reconstructor_command reconstr 1 1
807 (ls |> sort_distinct (prod_ord string_ord int_ord),
808 ss |> sort_distinct string_ord)
809 and do_step ind (Fix xs) =
810 do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n"
811 | do_step ind (Let (t1, t2)) =
812 do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n"
813 | do_step ind (Assume (l, t)) =
814 do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n"
815 | do_step ind (Prove (qs, l, t, By_Metis facts)) =
816 do_indent ind ^ do_have qs ^ " " ^
817 do_label l ^ do_term t ^ " " ^ do_facts facts ^ "\n"
818 | do_step ind (Prove (qs, l, t, Case_Split (proofs, facts))) =
819 implode (map (prefix (do_indent ind ^ "moreover\n") o do_block ind)
821 do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ " " ^
822 do_facts facts ^ "\n"
823 and do_steps prefix suffix ind steps =
824 let val s = implode (map (do_step ind) steps) in
825 replicate_string (ind * indent_size - size prefix) " " ^ prefix ^
826 String.extract (s, ind * indent_size,
827 SOME (size s - ind * indent_size - 1)) ^
830 and do_block ind proof = do_steps "{ " " }" (ind + 1) proof
831 (* One-step proofs are pointless; better use the Metis one-liner
833 and do_proof [Prove (_, _, _, By_Metis _)] = ""
835 (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
836 do_indent 0 ^ "proof -\n" ^ do_steps "" "" 1 proof ^ do_indent 0 ^
837 (if n <> 1 then "next" else "qed")
840 fun isar_proof_text ctxt isar_proof_requested
841 (debug, isar_shrink_factor, pool, fact_names, sym_tab, atp_proof, goal)
842 (one_line_params as (_, _, _, _, subgoal, subgoal_count)) =
844 val isar_shrink_factor =
845 (if isar_proof_requested then 1 else 2) * isar_shrink_factor
846 val (params, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal
847 val frees = fold Term.add_frees (concl_t :: hyp_ts) []
848 val one_line_proof = one_line_proof_text one_line_params
850 if is_typed_helper_used_in_atp_proof atp_proof then full_typesN
852 val lam_trans = lam_trans_from_atp_proof atp_proof metis_default_lam_trans
854 fun isar_proof_of () =
858 |> clean_up_atp_proof_dependencies
859 |> nasty_atp_proof pool
860 |> map_term_names_in_atp_proof repair_name
861 |> decode_lines ctxt sym_tab
862 |> rpair [] |-> fold_rev (add_line fact_names)
863 |> rpair [] |-> fold_rev add_nontrivial_line
865 |-> fold_rev (add_desired_line isar_shrink_factor fact_names frees)
867 val conj_name = conjecture_prefix ^ string_of_int (length hyp_ts)
870 |> map_filter (fn Inference (name as (_, ss), _, _, []) =>
871 if member (op =) ss conj_name then SOME name else NONE
873 fun dep_of_step (Definition _) = NONE
874 | dep_of_step (Inference (name, _, _, from)) = SOME (from, name)
875 val ref_graph = atp_proof |> map_filter dep_of_step |> make_ref_graph
876 val axioms = axioms_of_ref_graph ref_graph conjs
877 val tainted = tainted_atoms_of_ref_graph ref_graph conjs
880 |> fold (fn Definition _ => I (* FIXME *)
881 | Inference ((s, _), t, _, _) =>
882 Symtab.update_new (s,
883 t |> member (op = o apsnd fst) tainted s ? s_not))
885 (* FIXME: add "fold_rev forall_of (map Var (Term.add_vars t []))"? *)
886 fun prop_of_clause c =
887 fold (curry s_disj) (map_filter (Symtab.lookup props o fst) c)
889 fun label_of_clause c = (space_implode "___" (map fst c), 0)
890 fun maybe_show outer c =
891 (outer andalso length c = 1 andalso subset (op =) (c, conjs))
893 fun do_have outer qs (gamma, c) =
894 Prove (maybe_show outer c qs, label_of_clause c, prop_of_clause c,
895 By_Metis (fold (add_fact_from_dependency fact_names
896 o the_single) gamma ([], [])))
897 fun do_inf outer (Have z) = do_have outer [] z
898 | do_inf outer (Hence z) = do_have outer [Then] z
899 | do_inf outer (Cases cases) =
900 let val c = succedent_of_cases cases in
901 Prove (maybe_show outer c [Ultimately], label_of_clause c,
903 Case_Split (map (do_case false) cases, ([], [])))
905 and do_case outer (c, infs) =
906 Assume (label_of_clause c, prop_of_clause c) ::
907 map (do_inf outer) infs
909 (if null params then [] else [Fix params]) @
911 |> redirect_graph axioms tainted
912 |> chain_direct_proof
914 |> kill_duplicate_assumptions_in_proof
915 |> kill_useless_labels_in_proof
917 |> string_for_proof ctxt type_enc lam_trans subgoal subgoal_count
921 if isar_proof_requested then
922 "\nNo structured proof available (proof too short)."
926 "\n\n" ^ (if isar_proof_requested then "Structured proof"
927 else "Perhaps this will work") ^
928 ":\n" ^ Markup.markup Isabelle_Markup.sendback isar_proof
933 else case try isar_proof_of () of
935 | NONE => if isar_proof_requested then
936 "\nWarning: The Isar proof construction failed."
939 in one_line_proof ^ isar_proof end
941 fun proof_text ctxt isar_proof isar_params
942 (one_line_params as (preplay, _, _, _, _, _)) =
943 (if case preplay of Failed_to_Play _ => true | _ => isar_proof then
944 isar_proof_text ctxt isar_proof isar_params
946 one_line_proof_text) one_line_params