src/HOL/Tools/Sledgehammer/sledgehammer_reconstruct.ML
author blanchet
Wed Dec 12 21:48:29 2012 +0100 (2012-12-12 ago)
changeset 50510 7e4f2f8d9b50
parent 50450 358b6020f8b6
child 50557 31313171deb5
permissions -rw-r--r--
export a pair of ML functions
blanchet@49883
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_reconstruct.ML
blanchet@49883
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@49883
     3
    Author:     Steffen Juilf Smolka, TU Muenchen
blanchet@49883
     4
blanchet@49914
     5
Isar proof reconstruction from ATP proofs.
blanchet@49883
     6
*)
blanchet@49883
     7
blanchet@49883
     8
signature SLEDGEHAMMER_PROOF_RECONSTRUCT =
blanchet@49883
     9
sig
blanchet@49914
    10
  type 'a proof = 'a ATP_Proof.proof
blanchet@49914
    11
  type stature = ATP_Problem_Generate.stature
blanchet@49914
    12
blanchet@49914
    13
  datatype reconstructor =
blanchet@49914
    14
    Metis of string * string |
blanchet@49914
    15
    SMT
blanchet@49914
    16
blanchet@49914
    17
  datatype play =
blanchet@49914
    18
    Played of reconstructor * Time.time |
blanchet@49914
    19
    Trust_Playable of reconstructor * Time.time option |
blanchet@49914
    20
    Failed_to_Play of reconstructor
blanchet@49914
    21
blanchet@49914
    22
  type minimize_command = string list -> string
blanchet@49914
    23
  type one_line_params =
blanchet@49914
    24
    play * string * (string * stature) list * minimize_command * int * int
blanchet@49914
    25
  type isar_params =
blanchet@50004
    26
    bool * bool * Time.time * real * string Symtab.table
blanchet@50004
    27
    * (string * stature) list vector * int Symtab.table * string proof * thm
blanchet@49914
    28
blanchet@49914
    29
  val smtN : string
blanchet@49914
    30
  val string_for_reconstructor : reconstructor -> string
blanchet@49914
    31
  val lam_trans_from_atp_proof : string proof -> string -> string
blanchet@49914
    32
  val is_typed_helper_used_in_atp_proof : string proof -> bool
blanchet@49914
    33
  val used_facts_in_atp_proof :
blanchet@49914
    34
    Proof.context -> (string * stature) list vector -> string proof ->
blanchet@49914
    35
    (string * stature) list
blanchet@49914
    36
  val used_facts_in_unsound_atp_proof :
blanchet@49914
    37
    Proof.context -> (string * stature) list vector -> 'a proof ->
blanchet@49914
    38
    string list option
blanchet@49914
    39
  val one_line_proof_text : int -> one_line_params -> string
blanchet@49883
    40
  val isar_proof_text :
blanchet@49913
    41
    Proof.context -> bool -> isar_params -> one_line_params -> string
blanchet@49883
    42
  val proof_text :
blanchet@49913
    43
    Proof.context -> bool -> isar_params -> int -> one_line_params -> string
blanchet@49883
    44
end;
blanchet@49883
    45
blanchet@49883
    46
structure Sledgehammer_Reconstruct : SLEDGEHAMMER_PROOF_RECONSTRUCT =
blanchet@49883
    47
struct
blanchet@49883
    48
blanchet@49883
    49
open ATP_Util
blanchet@49914
    50
open ATP_Problem
blanchet@49883
    51
open ATP_Proof
blanchet@49883
    52
open ATP_Problem_Generate
blanchet@49883
    53
open ATP_Proof_Reconstruct
blanchet@49918
    54
open Sledgehammer_Util
smolkas@50264
    55
open Sledgehammer_Proof
smolkas@50258
    56
open Sledgehammer_Annotate
smolkas@50259
    57
open Sledgehammer_Shrink
blanchet@49914
    58
blanchet@49914
    59
structure String_Redirect = ATP_Proof_Redirect(
blanchet@49914
    60
  type key = step_name
blanchet@49914
    61
  val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s')
blanchet@49914
    62
  val string_of = fst)
blanchet@49914
    63
blanchet@49883
    64
open String_Redirect
blanchet@49883
    65
blanchet@49916
    66
blanchet@49914
    67
(** reconstructors **)
blanchet@49914
    68
blanchet@49914
    69
datatype reconstructor =
blanchet@49914
    70
  Metis of string * string |
blanchet@49914
    71
  SMT
blanchet@49914
    72
blanchet@49914
    73
datatype play =
blanchet@49914
    74
  Played of reconstructor * Time.time |
blanchet@49914
    75
  Trust_Playable of reconstructor * Time.time option |
blanchet@49914
    76
  Failed_to_Play of reconstructor
blanchet@49914
    77
blanchet@49914
    78
val smtN = "smt"
blanchet@49914
    79
blanchet@49914
    80
fun string_for_reconstructor (Metis (type_enc, lam_trans)) =
blanchet@49914
    81
    metis_call type_enc lam_trans
blanchet@49914
    82
  | string_for_reconstructor SMT = smtN
blanchet@49914
    83
blanchet@49914
    84
blanchet@49914
    85
(** fact extraction from ATP proofs **)
blanchet@49914
    86
blanchet@49914
    87
fun find_first_in_list_vector vec key =
blanchet@49914
    88
  Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key
blanchet@49914
    89
                 | (_, value) => value) NONE vec
blanchet@49914
    90
blanchet@49914
    91
val unprefix_fact_number = space_implode "_" o tl o space_explode "_"
blanchet@49914
    92
blanchet@49914
    93
fun resolve_one_named_fact fact_names s =
blanchet@49914
    94
  case try (unprefix fact_prefix) s of
blanchet@49914
    95
    SOME s' =>
blanchet@49914
    96
    let val s' = s' |> unprefix_fact_number |> unascii_of in
blanchet@49914
    97
      s' |> find_first_in_list_vector fact_names |> Option.map (pair s')
blanchet@49914
    98
    end
blanchet@49914
    99
  | NONE => NONE
blanchet@49914
   100
fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names)
blanchet@49914
   101
fun is_fact fact_names = not o null o resolve_fact fact_names
blanchet@49914
   102
blanchet@49914
   103
fun resolve_one_named_conjecture s =
blanchet@49914
   104
  case try (unprefix conjecture_prefix) s of
blanchet@49914
   105
    SOME s' => Int.fromString s'
blanchet@49914
   106
  | NONE => NONE
blanchet@49914
   107
blanchet@49914
   108
val resolve_conjecture = map_filter resolve_one_named_conjecture
blanchet@49914
   109
val is_conjecture = not o null o resolve_conjecture
blanchet@49914
   110
blanchet@49914
   111
val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix
blanchet@49914
   112
blanchet@49914
   113
(* overapproximation (good enough) *)
blanchet@49914
   114
fun is_lam_lifted s =
blanchet@49914
   115
  String.isPrefix fact_prefix s andalso
blanchet@49914
   116
  String.isSubstring ascii_of_lam_fact_prefix s
blanchet@49914
   117
blanchet@49914
   118
val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix)
blanchet@49914
   119
blanchet@49914
   120
fun is_axiom_used_in_proof pred =
blanchet@50012
   121
  exists (fn Inference_Step ((_, ss), _, _, _, []) => exists pred ss
blanchet@50012
   122
           | _ => false)
blanchet@49914
   123
blanchet@49914
   124
fun lam_trans_from_atp_proof atp_proof default =
blanchet@49914
   125
  case (is_axiom_used_in_proof is_combinator_def atp_proof,
blanchet@49914
   126
        is_axiom_used_in_proof is_lam_lifted atp_proof) of
blanchet@49914
   127
    (false, false) => default
blanchet@49914
   128
  | (false, true) => liftingN
blanchet@49914
   129
(*  | (true, true) => combs_and_liftingN -- not supported by "metis" *)
blanchet@49914
   130
  | (true, _) => combsN
blanchet@49914
   131
blanchet@49914
   132
val is_typed_helper_name =
blanchet@49914
   133
  String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix
blanchet@49914
   134
fun is_typed_helper_used_in_atp_proof atp_proof =
blanchet@49914
   135
  is_axiom_used_in_proof is_typed_helper_name atp_proof
blanchet@49914
   136
blanchet@49914
   137
fun add_non_rec_defs fact_names accum =
blanchet@49914
   138
  Vector.foldl (fn (facts, facts') =>
blanchet@49914
   139
      union (op =) (filter (fn (_, (_, status)) => status = Non_Rec_Def) facts)
blanchet@49914
   140
            facts')
blanchet@49914
   141
    accum fact_names
blanchet@49914
   142
blanchet@49914
   143
val isa_ext = Thm.get_name_hint @{thm ext}
blanchet@49914
   144
val isa_short_ext = Long_Name.base_name isa_ext
blanchet@49914
   145
blanchet@49914
   146
fun ext_name ctxt =
blanchet@49914
   147
  if Thm.eq_thm_prop (@{thm ext},
blanchet@49914
   148
         singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then
blanchet@49914
   149
    isa_short_ext
blanchet@49914
   150
  else
blanchet@49914
   151
    isa_ext
blanchet@49914
   152
blanchet@49914
   153
val leo2_ext = "extcnf_equal_neg"
blanchet@49914
   154
val leo2_unfold_def = "unfold_def"
blanchet@49914
   155
blanchet@50012
   156
fun add_fact ctxt fact_names (Inference_Step ((_, ss), _, _, rule, deps)) =
blanchet@49914
   157
    (if rule = leo2_ext then
blanchet@49914
   158
       insert (op =) (ext_name ctxt, (Global, General))
blanchet@49914
   159
     else if rule = leo2_unfold_def then
blanchet@49914
   160
       (* LEO 1.3.3 does not record definitions properly, leading to missing
blanchet@49914
   161
         dependencies in the TSTP proof. Remove the next line once this is
blanchet@49914
   162
         fixed. *)
blanchet@49914
   163
       add_non_rec_defs fact_names
blanchet@49914
   164
     else if rule = satallax_coreN then
blanchet@49914
   165
       (fn [] =>
blanchet@49914
   166
           (* Satallax doesn't include definitions in its unsatisfiable cores,
blanchet@49914
   167
              so we assume the worst and include them all here. *)
blanchet@49914
   168
           [(ext_name ctxt, (Global, General))] |> add_non_rec_defs fact_names
blanchet@49914
   169
         | facts => facts)
blanchet@49914
   170
     else
blanchet@49914
   171
       I)
blanchet@49914
   172
    #> (if null deps then union (op =) (resolve_fact fact_names ss)
blanchet@49914
   173
        else I)
blanchet@49914
   174
  | add_fact _ _ _ = I
blanchet@49914
   175
blanchet@49914
   176
fun used_facts_in_atp_proof ctxt fact_names atp_proof =
blanchet@49914
   177
  if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names
blanchet@49914
   178
  else fold (add_fact ctxt fact_names) atp_proof []
blanchet@49914
   179
blanchet@49914
   180
fun used_facts_in_unsound_atp_proof _ _ [] = NONE
blanchet@49914
   181
  | used_facts_in_unsound_atp_proof ctxt fact_names atp_proof =
blanchet@49914
   182
    let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in
blanchet@49914
   183
      if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso
blanchet@49914
   184
         not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then
blanchet@49914
   185
        SOME (map fst used_facts)
blanchet@49914
   186
      else
blanchet@49914
   187
        NONE
blanchet@49914
   188
    end
blanchet@49914
   189
blanchet@49914
   190
blanchet@49914
   191
(** one-liner reconstructor proofs **)
blanchet@49914
   192
blanchet@49914
   193
fun show_time NONE = ""
blanchet@49914
   194
  | show_time (SOME ext_time) = " (" ^ string_from_ext_time ext_time ^ ")"
blanchet@49914
   195
blanchet@49986
   196
(* FIXME: Various bugs, esp. with "unfolding"
blanchet@49914
   197
fun unusing_chained_facts _ 0 = ""
blanchet@49914
   198
  | unusing_chained_facts used_chaineds num_chained =
blanchet@49914
   199
    if length used_chaineds = num_chained then ""
blanchet@49914
   200
    else if null used_chaineds then "(* using no facts *) "
blanchet@49914
   201
    else "(* using only " ^ space_implode " " used_chaineds ^ " *) "
blanchet@49986
   202
*)
blanchet@49914
   203
blanchet@49914
   204
fun apply_on_subgoal _ 1 = "by "
blanchet@49914
   205
  | apply_on_subgoal 1 _ = "apply "
blanchet@49914
   206
  | apply_on_subgoal i n =
blanchet@49914
   207
    "prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal 1 n
blanchet@49914
   208
blanchet@49914
   209
fun using_labels [] = ""
blanchet@49914
   210
  | using_labels ls =
blanchet@49914
   211
    "using " ^ space_implode " " (map string_for_label ls) ^ " "
blanchet@49914
   212
blanchet@49914
   213
fun command_call name [] =
wenzelm@50239
   214
    name |> not (Symbol_Pos.is_identifier name) ? enclose "(" ")"
blanchet@49914
   215
  | command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")"
blanchet@49914
   216
blanchet@49914
   217
fun reconstructor_command reconstr i n used_chaineds num_chained (ls, ss) =
blanchet@49986
   218
  (* unusing_chained_facts used_chaineds num_chained ^ *)
blanchet@49914
   219
  using_labels ls ^ apply_on_subgoal i n ^
blanchet@49914
   220
  command_call (string_for_reconstructor reconstr) ss
blanchet@49914
   221
blanchet@49914
   222
fun try_command_line banner time command =
wenzelm@50450
   223
  banner ^ ": " ^ Active.sendback_markup command ^ show_time time ^ "."
blanchet@49914
   224
blanchet@49914
   225
fun minimize_line _ [] = ""
blanchet@49914
   226
  | minimize_line minimize_command ss =
blanchet@49914
   227
    case minimize_command ss of
blanchet@49914
   228
      "" => ""
blanchet@49914
   229
    | command =>
wenzelm@50450
   230
      "\nTo minimize: " ^ Active.sendback_markup command ^ "."
blanchet@49914
   231
blanchet@49914
   232
fun split_used_facts facts =
blanchet@49914
   233
  facts |> List.partition (fn (_, (sc, _)) => sc = Chained)
blanchet@49914
   234
        |> pairself (sort_distinct (string_ord o pairself fst))
blanchet@49914
   235
blanchet@49914
   236
type minimize_command = string list -> string
blanchet@49914
   237
type one_line_params =
blanchet@49914
   238
  play * string * (string * stature) list * minimize_command * int * int
blanchet@49914
   239
blanchet@49914
   240
fun one_line_proof_text num_chained
blanchet@49914
   241
        (preplay, banner, used_facts, minimize_command, subgoal,
blanchet@49914
   242
         subgoal_count) =
blanchet@49914
   243
  let
blanchet@49914
   244
    val (chained, extra) = split_used_facts used_facts
blanchet@49914
   245
    val (failed, reconstr, ext_time) =
blanchet@49914
   246
      case preplay of
blanchet@49914
   247
        Played (reconstr, time) => (false, reconstr, (SOME (false, time)))
blanchet@49914
   248
      | Trust_Playable (reconstr, time) =>
blanchet@49914
   249
        (false, reconstr,
blanchet@49914
   250
         case time of
blanchet@49914
   251
           NONE => NONE
blanchet@49914
   252
         | SOME time =>
blanchet@49914
   253
           if time = Time.zeroTime then NONE else SOME (true, time))
blanchet@49914
   254
      | Failed_to_Play reconstr => (true, reconstr, NONE)
blanchet@49914
   255
    val try_line =
blanchet@49914
   256
      ([], map fst extra)
blanchet@49914
   257
      |> reconstructor_command reconstr subgoal subgoal_count (map fst chained)
blanchet@49914
   258
                               num_chained
blanchet@49914
   259
      |> (if failed then
blanchet@49914
   260
            enclose "One-line proof reconstruction failed: "
blanchet@49914
   261
                     ".\n(Invoking \"sledgehammer\" with \"[strict]\" might \
blanchet@49914
   262
                     \solve this.)"
blanchet@49914
   263
          else
blanchet@49914
   264
            try_command_line banner ext_time)
blanchet@49914
   265
  in try_line ^ minimize_line minimize_command (map fst (extra @ chained)) end
blanchet@49914
   266
blanchet@49914
   267
blanchet@49914
   268
(** Isar proof construction and manipulation **)
blanchet@49914
   269
blanchet@50017
   270
val assume_prefix = "a"
blanchet@49914
   271
val have_prefix = "f"
blanchet@49914
   272
val raw_prefix = "x"
blanchet@49914
   273
blanchet@49914
   274
fun raw_label_for_name (num, ss) =
blanchet@49914
   275
  case resolve_conjecture ss of
blanchet@49914
   276
    [j] => (conjecture_prefix, j)
blanchet@49914
   277
  | _ => (raw_prefix ^ ascii_of num, 0)
blanchet@49914
   278
blanchet@50005
   279
fun label_of_clause [name] = raw_label_for_name name
smolkas@50262
   280
  | label_of_clause c = (space_implode "___" (map (fst o raw_label_for_name) c), 0)
blanchet@50005
   281
blanchet@50005
   282
fun add_fact_from_dependencies fact_names (names as [(_, ss)]) =
blanchet@50005
   283
    if is_fact fact_names ss then
blanchet@50005
   284
      apsnd (union (op =) (map fst (resolve_fact fact_names ss)))
blanchet@50005
   285
    else
blanchet@50005
   286
      apfst (insert (op =) (label_of_clause names))
blanchet@50005
   287
  | add_fact_from_dependencies fact_names names =
blanchet@50005
   288
    apfst (insert (op =) (label_of_clause names))
blanchet@49914
   289
blanchet@49914
   290
fun repair_name "$true" = "c_True"
blanchet@49914
   291
  | repair_name "$false" = "c_False"
blanchet@49914
   292
  | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
blanchet@49914
   293
  | repair_name s =
blanchet@49914
   294
    if is_tptp_equal s orelse
blanchet@49914
   295
       (* seen in Vampire proofs *)
blanchet@49914
   296
       (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then
blanchet@49914
   297
      tptp_equal
blanchet@49914
   298
    else
blanchet@49914
   299
      s
blanchet@49914
   300
blanchet@49914
   301
fun unvarify_term (Var ((s, 0), T)) = Free (s, T)
blanchet@49914
   302
  | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t])
blanchet@49914
   303
blanchet@49914
   304
fun infer_formula_types ctxt =
blanchet@49914
   305
  Type.constraint HOLogic.boolT
blanchet@49914
   306
  #> Syntax.check_term
blanchet@49914
   307
         (Proof_Context.set_mode Proof_Context.mode_schematic ctxt)
blanchet@49914
   308
blanchet@49914
   309
val combinator_table =
blanchet@49914
   310
  [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def [abs_def]}),
blanchet@49914
   311
   (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def [abs_def]}),
blanchet@49914
   312
   (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def [abs_def]}),
blanchet@49914
   313
   (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def [abs_def]}),
blanchet@49914
   314
   (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def [abs_def]})]
blanchet@49914
   315
blanchet@49914
   316
fun uncombine_term thy =
blanchet@49914
   317
  let
blanchet@49914
   318
    fun aux (t1 $ t2) = betapply (pairself aux (t1, t2))
blanchet@49914
   319
      | aux (Abs (s, T, t')) = Abs (s, T, aux t')
blanchet@49914
   320
      | aux (t as Const (x as (s, _))) =
blanchet@49914
   321
        (case AList.lookup (op =) combinator_table s of
blanchet@49914
   322
           SOME thm => thm |> prop_of |> specialize_type thy x
blanchet@49914
   323
                           |> Logic.dest_equals |> snd
blanchet@49914
   324
         | NONE => t)
blanchet@49914
   325
      | aux t = t
blanchet@49914
   326
  in aux end
blanchet@49914
   327
blanchet@49914
   328
fun decode_line sym_tab (Definition_Step (name, phi1, phi2)) ctxt =
blanchet@49914
   329
    let
blanchet@49914
   330
      val thy = Proof_Context.theory_of ctxt
blanchet@49914
   331
      val t1 = prop_from_atp ctxt true sym_tab phi1
blanchet@49914
   332
      val vars = snd (strip_comb t1)
blanchet@49914
   333
      val frees = map unvarify_term vars
blanchet@49914
   334
      val unvarify_args = subst_atomic (vars ~~ frees)
blanchet@49914
   335
      val t2 = prop_from_atp ctxt true sym_tab phi2
blanchet@49914
   336
      val (t1, t2) =
blanchet@49914
   337
        HOLogic.eq_const HOLogic.typeT $ t1 $ t2
blanchet@49914
   338
        |> unvarify_args |> uncombine_term thy |> infer_formula_types ctxt
blanchet@49914
   339
        |> HOLogic.dest_eq
blanchet@49914
   340
    in
blanchet@49914
   341
      (Definition_Step (name, t1, t2),
blanchet@49914
   342
       fold Variable.declare_term (maps Misc_Legacy.term_frees [t1, t2]) ctxt)
blanchet@49914
   343
    end
blanchet@50012
   344
  | decode_line sym_tab (Inference_Step (name, role, u, rule, deps)) ctxt =
blanchet@49914
   345
    let
blanchet@49914
   346
      val thy = Proof_Context.theory_of ctxt
blanchet@49914
   347
      val t = u |> prop_from_atp ctxt true sym_tab
blanchet@49914
   348
                |> uncombine_term thy |> infer_formula_types ctxt
blanchet@49914
   349
    in
blanchet@50012
   350
      (Inference_Step (name, role, t, rule, deps),
blanchet@49914
   351
       fold Variable.declare_term (Misc_Legacy.term_frees t) ctxt)
blanchet@49914
   352
    end
blanchet@49914
   353
fun decode_lines ctxt sym_tab lines =
blanchet@49914
   354
  fst (fold_map (decode_line sym_tab) lines ctxt)
blanchet@49914
   355
blanchet@49914
   356
fun replace_one_dependency (old, new) dep =
blanchet@49914
   357
  if is_same_atp_step dep old then new else [dep]
blanchet@49914
   358
fun replace_dependencies_in_line _ (line as Definition_Step _) = line
blanchet@50012
   359
  | replace_dependencies_in_line p
blanchet@50012
   360
        (Inference_Step (name, role, t, rule, deps)) =
blanchet@50012
   361
    Inference_Step (name, role, t, rule,
blanchet@49914
   362
                    fold (union (op =) o replace_one_dependency p) deps [])
blanchet@49914
   363
blanchet@49914
   364
(* No "real" literals means only type information (tfree_tcs, clsrel, or
blanchet@49914
   365
   clsarity). *)
blanchet@49914
   366
fun is_only_type_information t = t aconv @{term True}
blanchet@49914
   367
blanchet@49914
   368
fun is_same_inference _ (Definition_Step _) = false
blanchet@50012
   369
  | is_same_inference t (Inference_Step (_, _, t', _, _)) = t aconv t'
blanchet@49914
   370
blanchet@49914
   371
(* Discard facts; consolidate adjacent lines that prove the same formula, since
blanchet@49914
   372
   they differ only in type information.*)
blanchet@49914
   373
fun add_line _ (line as Definition_Step _) lines = line :: lines
blanchet@50012
   374
  | add_line fact_names (Inference_Step (name as (_, ss), role, t, rule, []))
blanchet@50012
   375
             lines =
blanchet@49914
   376
    (* No dependencies: fact, conjecture, or (for Vampire) internal facts or
blanchet@49914
   377
       definitions. *)
blanchet@49914
   378
    if is_fact fact_names ss then
blanchet@49914
   379
      (* Facts are not proof lines. *)
blanchet@49914
   380
      if is_only_type_information t then
blanchet@49914
   381
        map (replace_dependencies_in_line (name, [])) lines
blanchet@49914
   382
      (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@49914
   383
      else case take_prefix (not o is_same_inference t) lines of
blanchet@49914
   384
        (_, []) => lines (* no repetition of proof line *)
blanchet@50012
   385
      | (pre, Inference_Step (name', _, _, _, _) :: post) =>
blanchet@49914
   386
        pre @ map (replace_dependencies_in_line (name', [name])) post
blanchet@49914
   387
      | _ => raise Fail "unexpected inference"
blanchet@49914
   388
    else if is_conjecture ss then
blanchet@50012
   389
      Inference_Step (name, role, t, rule, []) :: lines
blanchet@49914
   390
    else
blanchet@49914
   391
      map (replace_dependencies_in_line (name, [])) lines
blanchet@50012
   392
  | add_line _ (Inference_Step (name, role, t, rule, deps)) lines =
blanchet@49914
   393
    (* Type information will be deleted later; skip repetition test. *)
blanchet@49914
   394
    if is_only_type_information t then
blanchet@50012
   395
      Inference_Step (name, role, t, rule, deps) :: lines
blanchet@49914
   396
    (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@49914
   397
    else case take_prefix (not o is_same_inference t) lines of
blanchet@49914
   398
      (* FIXME: Doesn't this code risk conflating proofs involving different
blanchet@49914
   399
         types? *)
blanchet@50012
   400
       (_, []) => Inference_Step (name, role, t, rule, deps) :: lines
blanchet@50012
   401
     | (pre, Inference_Step (name', role, t', rule, _) :: post) =>
blanchet@50012
   402
       Inference_Step (name, role, t', rule, deps) ::
blanchet@49914
   403
       pre @ map (replace_dependencies_in_line (name', [name])) post
blanchet@49914
   404
     | _ => raise Fail "unexpected inference"
blanchet@49914
   405
blanchet@49914
   406
val waldmeister_conjecture_num = "1.0.0.0"
blanchet@49914
   407
blanchet@49914
   408
val repair_waldmeister_endgame =
blanchet@49914
   409
  let
blanchet@50012
   410
    fun do_tail (Inference_Step (name, role, t, rule, deps)) =
blanchet@50012
   411
        Inference_Step (name, role, s_not t, rule, deps)
blanchet@49914
   412
      | do_tail line = line
blanchet@49914
   413
    fun do_body [] = []
blanchet@50012
   414
      | do_body ((line as Inference_Step ((num, _), _, _, _, _)) :: lines) =
blanchet@49914
   415
        if num = waldmeister_conjecture_num then map do_tail (line :: lines)
blanchet@49914
   416
        else line :: do_body lines
blanchet@49914
   417
      | do_body (line :: lines) = line :: do_body lines
blanchet@49914
   418
  in do_body end
blanchet@49914
   419
blanchet@49914
   420
(* Recursively delete empty lines (type information) from the proof. *)
blanchet@50012
   421
fun add_nontrivial_line (line as Inference_Step (name, _, t, _, [])) lines =
blanchet@49914
   422
    if is_only_type_information t then delete_dependency name lines
blanchet@49914
   423
    else line :: lines
blanchet@49914
   424
  | add_nontrivial_line line lines = line :: lines
blanchet@49914
   425
and delete_dependency name lines =
blanchet@49914
   426
  fold_rev add_nontrivial_line
blanchet@49914
   427
           (map (replace_dependencies_in_line (name, [])) lines) []
blanchet@49914
   428
blanchet@49914
   429
(* ATPs sometimes reuse free variable names in the strangest ways. Removing
blanchet@49914
   430
   offending lines often does the trick. *)
blanchet@49914
   431
fun is_bad_free frees (Free x) = not (member (op =) frees x)
blanchet@49914
   432
  | is_bad_free _ _ = false
blanchet@49914
   433
blanchet@49918
   434
fun add_desired_line _ _ (line as Definition_Step (name, _, _)) (j, lines) =
blanchet@49914
   435
    (j, line :: map (replace_dependencies_in_line (name, [])) lines)
blanchet@49918
   436
  | add_desired_line fact_names frees
blanchet@50012
   437
        (Inference_Step (name as (_, ss), role, t, rule, deps)) (j, lines) =
blanchet@49914
   438
    (j + 1,
blanchet@49914
   439
     if is_fact fact_names ss orelse
blanchet@49914
   440
        is_conjecture ss orelse
blanchet@49914
   441
        (* the last line must be kept *)
blanchet@49914
   442
        j = 0 orelse
blanchet@49914
   443
        (not (is_only_type_information t) andalso
blanchet@49914
   444
         null (Term.add_tvars t []) andalso
blanchet@49914
   445
         not (exists_subterm (is_bad_free frees) t) andalso
blanchet@49918
   446
         length deps >= 2 andalso
blanchet@49914
   447
         (* kill next to last line, which usually results in a trivial step *)
blanchet@49914
   448
         j <> 1) then
blanchet@50012
   449
       Inference_Step (name, role, t, rule, deps) :: lines  (* keep line *)
blanchet@49914
   450
     else
blanchet@49914
   451
       map (replace_dependencies_in_line (name, deps)) lines)  (* drop line *)
blanchet@49914
   452
blanchet@49914
   453
val indent_size = 2
blanchet@49914
   454
val no_label = ("", ~1)
blanchet@49914
   455
blanchet@49883
   456
fun string_for_proof ctxt type_enc lam_trans i n =
blanchet@49883
   457
  let
blanchet@49883
   458
    fun do_indent ind = replicate_string (ind * indent_size) " "
blanchet@49883
   459
    fun do_free (s, T) =
blanchet@49883
   460
      maybe_quote s ^ " :: " ^
blanchet@50049
   461
      maybe_quote (simplify_spaces (with_vanilla_print_mode
blanchet@50048
   462
        (Syntax.string_of_typ ctxt) T))
blanchet@49883
   463
    fun do_label l = if l = no_label then "" else string_for_label l ^ ": "
blanchet@49883
   464
    fun do_have qs =
blanchet@49883
   465
      (if member (op =) qs Ultimately then "ultimately " else "") ^
blanchet@49883
   466
      (if member (op =) qs Then then
blanchet@49883
   467
         if member (op =) qs Show then "thus" else "hence"
blanchet@49883
   468
       else
blanchet@49883
   469
         if member (op =) qs Show then "show" else "have")
blanchet@49883
   470
    val do_term =
blanchet@50048
   471
      annotate_types ctxt
blanchet@50048
   472
      #> with_vanilla_print_mode (Syntax.string_of_term ctxt)
blanchet@50049
   473
      #> simplify_spaces
blanchet@50048
   474
      #> maybe_quote
blanchet@49883
   475
    val reconstr = Metis (type_enc, lam_trans)
blanchet@49921
   476
    fun do_facts ind (ls, ss) =
blanchet@49921
   477
      "\n" ^ do_indent (ind + 1) ^
blanchet@49883
   478
      reconstructor_command reconstr 1 1 [] 0
blanchet@49883
   479
          (ls |> sort_distinct (prod_ord string_ord int_ord),
blanchet@49883
   480
           ss |> sort_distinct string_ord)
blanchet@49883
   481
    and do_step ind (Fix xs) =
blanchet@49883
   482
        do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n"
blanchet@49883
   483
      | do_step ind (Let (t1, t2)) =
blanchet@49883
   484
        do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n"
blanchet@49883
   485
      | do_step ind (Assume (l, t)) =
blanchet@49883
   486
        do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n"
blanchet@49883
   487
      | do_step ind (Prove (qs, l, t, By_Metis facts)) =
blanchet@49883
   488
        do_indent ind ^ do_have qs ^ " " ^
blanchet@49921
   489
        do_label l ^ do_term t ^ do_facts ind facts ^ "\n"
blanchet@49883
   490
      | do_step ind (Prove (qs, l, t, Case_Split (proofs, facts))) =
blanchet@49883
   491
        implode (map (prefix (do_indent ind ^ "moreover\n") o do_block ind)
blanchet@49883
   492
                     proofs) ^
blanchet@49921
   493
        do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^
blanchet@49921
   494
        do_facts ind facts ^ "\n"
blanchet@49883
   495
    and do_steps prefix suffix ind steps =
blanchet@49883
   496
      let val s = implode (map (do_step ind) steps) in
blanchet@49883
   497
        replicate_string (ind * indent_size - size prefix) " " ^ prefix ^
blanchet@49883
   498
        String.extract (s, ind * indent_size,
blanchet@49883
   499
                        SOME (size s - ind * indent_size - 1)) ^
blanchet@49883
   500
        suffix ^ "\n"
blanchet@49883
   501
      end
blanchet@49883
   502
    and do_block ind proof = do_steps "{ " " }" (ind + 1) proof
blanchet@49883
   503
    (* One-step proofs are pointless; better use the Metis one-liner
blanchet@49883
   504
       directly. *)
blanchet@49883
   505
    and do_proof [Prove (_, _, _, By_Metis _)] = ""
blanchet@49883
   506
      | do_proof proof =
blanchet@49883
   507
        (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
blanchet@49883
   508
        do_indent 0 ^ "proof -\n" ^ do_steps "" "" 1 proof ^ do_indent 0 ^
blanchet@49883
   509
        (if n <> 1 then "next" else "qed")
blanchet@49883
   510
  in do_proof end
blanchet@49883
   511
blanchet@49914
   512
fun used_labels_of_step (Prove (_, _, _, by)) =
blanchet@49914
   513
    (case by of
blanchet@49914
   514
       By_Metis (ls, _) => ls
blanchet@49914
   515
     | Case_Split (proofs, (ls, _)) =>
blanchet@49914
   516
       fold (union (op =) o used_labels_of) proofs ls)
blanchet@49914
   517
  | used_labels_of_step _ = []
blanchet@49914
   518
and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof []
blanchet@49914
   519
blanchet@49914
   520
fun kill_useless_labels_in_proof proof =
blanchet@49914
   521
  let
blanchet@49914
   522
    val used_ls = used_labels_of proof
blanchet@49914
   523
    fun do_label l = if member (op =) used_ls l then l else no_label
blanchet@49914
   524
    fun do_step (Assume (l, t)) = Assume (do_label l, t)
blanchet@49914
   525
      | do_step (Prove (qs, l, t, by)) =
blanchet@49914
   526
        Prove (qs, do_label l, t,
blanchet@49914
   527
               case by of
blanchet@49914
   528
                 Case_Split (proofs, facts) =>
blanchet@49914
   529
                 Case_Split (map (map do_step) proofs, facts)
blanchet@49914
   530
               | _ => by)
blanchet@49914
   531
      | do_step step = step
blanchet@49914
   532
  in map do_step proof end
blanchet@49914
   533
blanchet@49914
   534
fun prefix_for_depth n = replicate_string (n + 1)
blanchet@49914
   535
blanchet@49914
   536
val relabel_proof =
blanchet@49914
   537
  let
blanchet@49914
   538
    fun aux _ _ _ [] = []
blanchet@50017
   539
      | aux subst depth (next_assum, next_have) (Assume (l, t) :: proof) =
blanchet@49914
   540
        if l = no_label then
blanchet@50017
   541
          Assume (l, t) :: aux subst depth (next_assum, next_have) proof
blanchet@49914
   542
        else
blanchet@50017
   543
          let val l' = (prefix_for_depth depth assume_prefix, next_assum) in
blanchet@49914
   544
            Assume (l', t) ::
blanchet@50017
   545
            aux ((l, l') :: subst) depth (next_assum + 1, next_have) proof
blanchet@49914
   546
          end
blanchet@50017
   547
      | aux subst depth (next_assum, next_have)
blanchet@49914
   548
            (Prove (qs, l, t, by) :: proof) =
blanchet@49914
   549
        let
blanchet@50017
   550
          val (l', subst, next_have) =
blanchet@49914
   551
            if l = no_label then
blanchet@50017
   552
              (l, subst, next_have)
blanchet@49914
   553
            else
blanchet@50017
   554
              let val l' = (prefix_for_depth depth have_prefix, next_have) in
blanchet@50017
   555
                (l', (l, l') :: subst, next_have + 1)
blanchet@50017
   556
              end
blanchet@49914
   557
          val relabel_facts =
blanchet@49914
   558
            apfst (maps (the_list o AList.lookup (op =) subst))
blanchet@49914
   559
          val by =
blanchet@49914
   560
            case by of
blanchet@49914
   561
              By_Metis facts => By_Metis (relabel_facts facts)
blanchet@49914
   562
            | Case_Split (proofs, facts) =>
blanchet@49914
   563
              Case_Split (map (aux subst (depth + 1) (1, 1)) proofs,
blanchet@49914
   564
                          relabel_facts facts)
blanchet@49914
   565
        in
blanchet@50017
   566
          Prove (qs, l', t, by) :: aux subst depth (next_assum, next_have) proof
blanchet@49914
   567
        end
blanchet@49914
   568
      | aux subst depth nextp (step :: proof) =
blanchet@49914
   569
        step :: aux subst depth nextp proof
blanchet@49914
   570
  in aux [] 0 (1, 1) end
blanchet@49914
   571
blanchet@50004
   572
val chain_direct_proof =
blanchet@50004
   573
  let
blanchet@50004
   574
    fun succedent_of_step (Prove (_, label, _, _)) = SOME label
blanchet@50004
   575
      | succedent_of_step (Assume (label, _)) = SOME label
blanchet@50004
   576
      | succedent_of_step _ = NONE
blanchet@50004
   577
    fun chain_inf (SOME label0)
blanchet@50004
   578
                  (step as Prove (qs, label, t, By_Metis (lfs, gfs))) =
blanchet@50004
   579
        if member (op =) lfs label0 then
blanchet@50004
   580
          Prove (Then :: qs, label, t,
blanchet@50004
   581
                 By_Metis (filter_out (curry (op =) label0) lfs, gfs))
blanchet@50004
   582
        else
blanchet@50004
   583
          step
blanchet@50004
   584
      | chain_inf _ (Prove (qs, label, t, Case_Split (proofs, facts))) =
blanchet@50004
   585
        Prove (qs, label, t, Case_Split ((map (chain_proof NONE) proofs), facts))
blanchet@50004
   586
      | chain_inf _ step = step
blanchet@50004
   587
    and chain_proof _ [] = []
blanchet@50004
   588
      | chain_proof (prev as SOME _) (i :: is) =
blanchet@50004
   589
        chain_inf prev i :: chain_proof (succedent_of_step i) is
blanchet@50004
   590
      | chain_proof _ (i :: is) =
blanchet@50004
   591
        i :: chain_proof (succedent_of_step i) is
blanchet@50004
   592
  in chain_proof NONE end
blanchet@49883
   593
blanchet@49914
   594
type isar_params =
blanchet@50004
   595
  bool * bool * Time.time * real * string Symtab.table
blanchet@50004
   596
  * (string * stature) list vector * int Symtab.table * string proof * thm
blanchet@49914
   597
blanchet@49918
   598
fun isar_proof_text ctxt isar_proofs
blanchet@50020
   599
    (debug, verbose, preplay_timeout, isar_shrink, pool, fact_names, sym_tab,
blanchet@50020
   600
     atp_proof, goal)
blanchet@49918
   601
    (one_line_params as (_, _, _, _, subgoal, subgoal_count)) =
blanchet@49883
   602
  let
blanchet@49883
   603
    val (params, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal
blanchet@49883
   604
    val frees = fold Term.add_frees (concl_t :: hyp_ts) []
blanchet@49883
   605
    val one_line_proof = one_line_proof_text 0 one_line_params
blanchet@49883
   606
    val type_enc =
blanchet@49883
   607
      if is_typed_helper_used_in_atp_proof atp_proof then full_typesN
blanchet@49883
   608
      else partial_typesN
blanchet@49883
   609
    val lam_trans = lam_trans_from_atp_proof atp_proof metis_default_lam_trans
blanchet@50004
   610
    val preplay = preplay_timeout <> seconds 0.0
blanchet@49883
   611
blanchet@49883
   612
    fun isar_proof_of () =
blanchet@49883
   613
      let
blanchet@49883
   614
        val atp_proof =
blanchet@49883
   615
          atp_proof
blanchet@49883
   616
          |> clean_up_atp_proof_dependencies
blanchet@49883
   617
          |> nasty_atp_proof pool
blanchet@49883
   618
          |> map_term_names_in_atp_proof repair_name
blanchet@49883
   619
          |> decode_lines ctxt sym_tab
blanchet@49883
   620
          |> rpair [] |-> fold_rev (add_line fact_names)
blanchet@49883
   621
          |> repair_waldmeister_endgame
blanchet@49883
   622
          |> rpair [] |-> fold_rev add_nontrivial_line
blanchet@49883
   623
          |> rpair (0, [])
blanchet@49918
   624
          |-> fold_rev (add_desired_line fact_names frees)
blanchet@49883
   625
          |> snd
blanchet@49883
   626
        val conj_name = conjecture_prefix ^ string_of_int (length hyp_ts)
blanchet@49883
   627
        val conjs =
blanchet@50010
   628
          atp_proof |> map_filter
blanchet@50012
   629
            (fn Inference_Step (name as (_, ss), _, _, _, []) =>
blanchet@50010
   630
                if member (op =) ss conj_name then SOME name else NONE
blanchet@50010
   631
              | _ => NONE)
blanchet@50010
   632
        val assms =
blanchet@50010
   633
          atp_proof |> map_filter
blanchet@50013
   634
            (fn Inference_Step (name as (_, ss), _, _, _, []) =>
blanchet@50013
   635
                (case resolve_conjecture ss of
blanchet@50013
   636
                   [j] =>
blanchet@50013
   637
                   if j = length hyp_ts then NONE
blanchet@50013
   638
                   else SOME (Assume (raw_label_for_name name, nth hyp_ts j))
blanchet@50013
   639
                 | _ => NONE)
blanchet@50010
   640
              | _ => NONE)
blanchet@49883
   641
        fun dep_of_step (Definition_Step _) = NONE
blanchet@50012
   642
          | dep_of_step (Inference_Step (name, _, _, _, from)) =
blanchet@50012
   643
            SOME (from, name)
blanchet@49883
   644
        val ref_graph = atp_proof |> map_filter dep_of_step |> make_ref_graph
blanchet@49883
   645
        val axioms = axioms_of_ref_graph ref_graph conjs
blanchet@49883
   646
        val tainted = tainted_atoms_of_ref_graph ref_graph conjs
blanchet@49883
   647
        val props =
blanchet@49883
   648
          Symtab.empty
blanchet@49883
   649
          |> fold (fn Definition_Step _ => I (* FIXME *)
blanchet@50013
   650
                    | Inference_Step (name as (s, ss), role, t, _, _) =>
blanchet@49883
   651
                      Symtab.update_new (s,
blanchet@50016
   652
                        if member (op = o apsnd fst) tainted s then
blanchet@50016
   653
                          t |> role <> Conjecture ? s_not
blanchet@50016
   654
                            |> fold exists_of (map Var (Term.add_vars t []))
blanchet@50016
   655
                        else
blanchet@50016
   656
                          t))
blanchet@49883
   657
                  atp_proof
blanchet@50013
   658
        (* The assumptions and conjecture are props; the rest are bools. *)
blanchet@50016
   659
        fun prop_of_clause [name as (s, ss)] =
blanchet@50016
   660
            (case resolve_conjecture ss of
blanchet@50016
   661
               [j] => if j = length hyp_ts then concl_t else nth hyp_ts j
blanchet@50016
   662
             | _ => the_default @{term False} (Symtab.lookup props s)
blanchet@50016
   663
                    |> HOLogic.mk_Trueprop |> close_form)
blanchet@50016
   664
          | prop_of_clause names =
blanchet@50018
   665
            let val lits = map_filter (Symtab.lookup props o fst) names in
blanchet@50018
   666
              case List.partition (can HOLogic.dest_not) lits of
blanchet@50018
   667
                (negs as _ :: _, pos as _ :: _) =>
blanchet@50018
   668
                HOLogic.mk_imp
blanchet@50018
   669
                  (Library.foldr1 s_conj (map HOLogic.dest_not negs),
blanchet@50018
   670
                   Library.foldr1 s_disj pos)
blanchet@50018
   671
              | _ => fold (curry s_disj) lits @{term False}
blanchet@50018
   672
            end
blanchet@50016
   673
            |> HOLogic.mk_Trueprop |> close_form
blanchet@49883
   674
        fun maybe_show outer c =
blanchet@49883
   675
          (outer andalso length c = 1 andalso subset (op =) (c, conjs))
blanchet@49883
   676
          ? cons Show
blanchet@49883
   677
        fun do_have outer qs (gamma, c) =
blanchet@49883
   678
          Prove (maybe_show outer c qs, label_of_clause c, prop_of_clause c,
blanchet@50005
   679
                 By_Metis (fold (add_fact_from_dependencies fact_names) gamma
blanchet@50005
   680
                                ([], [])))
blanchet@49883
   681
        fun do_inf outer (Have z) = do_have outer [] z
blanchet@49883
   682
          | do_inf outer (Cases cases) =
blanchet@49883
   683
            let val c = succedent_of_cases cases in
blanchet@49883
   684
              Prove (maybe_show outer c [Ultimately], label_of_clause c,
blanchet@49883
   685
                     prop_of_clause c,
blanchet@49883
   686
                     Case_Split (map (do_case false) cases, ([], [])))
blanchet@49883
   687
            end
blanchet@49883
   688
        and do_case outer (c, infs) =
blanchet@49883
   689
          Assume (label_of_clause c, prop_of_clause c) ::
blanchet@49883
   690
          map (do_inf outer) infs
smolkas@50271
   691
        val (isar_proof, (preplay_fail, ext_time)) =
blanchet@50004
   692
          ref_graph
blanchet@50004
   693
          |> redirect_graph axioms tainted
blanchet@50004
   694
          |> map (do_inf true)
blanchet@50010
   695
          |> append assms
smolkas@50257
   696
          |> (if not preplay andalso isar_shrink <= 1.0
smolkas@50271
   697
              then pair (false, (true, seconds 0.0)) #> swap
smolkas@50257
   698
              else shrink_proof debug ctxt type_enc lam_trans preplay
smolkas@50257
   699
                preplay_timeout (if isar_proofs then isar_shrink else 1000.0))
smolkas@50257
   700
       (* |>> reorder_proof_to_minimize_jumps (* ? *) *)
smolkas@50257
   701
          |>> chain_direct_proof
smolkas@50257
   702
          |>> kill_useless_labels_in_proof
smolkas@50257
   703
          |>> relabel_proof
smolkas@50257
   704
          |>> not (null params) ? cons (Fix params)
blanchet@49918
   705
        val isar_text =
blanchet@49918
   706
          string_for_proof ctxt type_enc lam_trans subgoal subgoal_count
blanchet@49918
   707
                           isar_proof
blanchet@49883
   708
      in
blanchet@49918
   709
        case isar_text of
blanchet@49883
   710
          "" =>
blanchet@49918
   711
          if isar_proofs then
blanchet@49883
   712
            "\nNo structured proof available (proof too short)."
blanchet@49883
   713
          else
blanchet@49883
   714
            ""
blanchet@49883
   715
        | _ =>
smolkas@50271
   716
          let 
smolkas@50277
   717
            val msg = 
smolkas@50277
   718
              (if preplay then 
smolkas@50277
   719
                [if preplay_fail 
smolkas@50277
   720
                 then "may fail" 
smolkas@50277
   721
                 else string_from_ext_time ext_time]
smolkas@50277
   722
               else [])
smolkas@50277
   723
              @ 
smolkas@50277
   724
               (if verbose then
smolkas@50277
   725
                  [let val num_steps = metis_steps_total isar_proof
smolkas@50277
   726
                   in string_of_int num_steps ^ plural_s num_steps end]
smolkas@50277
   727
                else [])
smolkas@50277
   728
          in
smolkas@50277
   729
            "\n\nStructured proof "
smolkas@50277
   730
              ^ (commas msg |> not (null msg) ? enclose "(" ")")
wenzelm@50450
   731
              ^ ":\n" ^ Active.sendback_markup isar_text
smolkas@50277
   732
          end
blanchet@49883
   733
      end
blanchet@49883
   734
    val isar_proof =
blanchet@49883
   735
      if debug then
blanchet@49883
   736
        isar_proof_of ()
blanchet@49883
   737
      else case try isar_proof_of () of
blanchet@49883
   738
        SOME s => s
blanchet@49918
   739
      | NONE => if isar_proofs then
blanchet@49883
   740
                  "\nWarning: The Isar proof construction failed."
blanchet@49883
   741
                else
blanchet@49883
   742
                  ""
blanchet@49883
   743
  in one_line_proof ^ isar_proof end
blanchet@49883
   744
blanchet@49918
   745
fun proof_text ctxt isar_proofs isar_params num_chained
blanchet@49883
   746
               (one_line_params as (preplay, _, _, _, _, _)) =
blanchet@49918
   747
  (if case preplay of Failed_to_Play _ => true | _ => isar_proofs then
blanchet@49918
   748
     isar_proof_text ctxt isar_proofs isar_params
blanchet@49883
   749
   else
blanchet@49883
   750
     one_line_proof_text num_chained) one_line_params
blanchet@49883
   751
blanchet@49883
   752
end;