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