src/HOL/Tools/Sledgehammer/sledgehammer_atp_reconstruct.ML
author blanchet
Fri, 27 May 2011 10:30:08 +0200
changeset 43874 c4b9b4be90c4
parent 43847 ff631c45797e
child 43875 18259246abb5
permissions -rw-r--r--
show time taken for reconstruction
blanchet@40358
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_atp_reconstruct.ML
blanchet@38261
     2
    Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
blanchet@38261
     3
    Author:     Claire Quigley, Cambridge University Computer Laboratory
blanchet@36392
     4
    Author:     Jasmin Blanchette, TU Muenchen
paulson@21978
     5
blanchet@39735
     6
Proof reconstruction for Sledgehammer.
wenzelm@33318
     7
*)
wenzelm@33318
     8
blanchet@40249
     9
signature SLEDGEHAMMER_ATP_RECONSTRUCT =
paulson@24425
    10
sig
blanchet@43320
    11
  type 'a proof = 'a ATP_Proof.proof
blanchet@39232
    12
  type locality = Sledgehammer_Filter.locality
blanchet@41384
    13
  type type_system = Sledgehammer_ATP_Translate.type_system
blanchet@43874
    14
blanchet@43874
    15
  datatype reconstructor =
blanchet@43874
    16
    Metis |
blanchet@43874
    17
    MetisFT |
blanchet@43874
    18
    SMT of string
blanchet@43874
    19
blanchet@43874
    20
  datatype preplay =
blanchet@43874
    21
    Preplayed of reconstructor * Time.time |
blanchet@43874
    22
    Trust_Playable of reconstructor * Time.time option|
blanchet@43874
    23
    Preplay_Failed
blanchet@43874
    24
blanchet@36281
    25
  type minimize_command = string list -> string
blanchet@43874
    26
  type one_line_params =
blanchet@43874
    27
    preplay * string * (string * locality) list * minimize_command * thm * int
blanchet@39053
    28
  type isar_params =
blanchet@43874
    29
    bool * bool * int * type_system * string Symtab.table * int list list
blanchet@43874
    30
    * int * (string * locality) list vector * int Symtab.table * string proof
blanchet@40445
    31
  val repair_conjecture_shape_and_fact_names :
blanchet@43518
    32
    type_system -> string -> int list list -> int
blanchet@43750
    33
    -> (string * locality) list vector -> int list
blanchet@43750
    34
    -> int list list * int * (string * locality) list vector * int list
blanchet@43322
    35
  val used_facts_in_atp_proof :
blanchet@43809
    36
    Proof.context -> type_system -> int -> (string * locality) list vector
blanchet@43809
    37
    -> string proof -> (string * locality) list
blanchet@43745
    38
  val used_facts_in_unsound_atp_proof :
blanchet@43809
    39
    Proof.context -> type_system -> int list list -> int
blanchet@43874
    40
    -> (string * locality) list vector -> 'a proof -> string list option
blanchet@43874
    41
  val uses_typed_helpers : int list -> 'a proof -> bool
blanchet@43874
    42
  val reconstructor_name : reconstructor -> string
blanchet@43874
    43
  val reconstructor_settings : reconstructor -> string
blanchet@41399
    44
  val apply_on_subgoal : string -> int -> int -> string
blanchet@40241
    45
  val command_call : string -> string list -> string
blanchet@43874
    46
  val try_command_line : string -> (bool * Time.time) option -> string -> string
blanchet@40245
    47
  val minimize_line : ('a list -> string) -> 'a list -> string
blanchet@40966
    48
  val split_used_facts :
blanchet@40966
    49
    (string * locality) list
blanchet@40966
    50
    -> (string * locality) list * (string * locality) list
blanchet@43874
    51
  val one_line_proof_text : one_line_params -> string
blanchet@43809
    52
  val isar_proof_text :
blanchet@43874
    53
    Proof.context -> isar_params -> one_line_params -> string
blanchet@43809
    54
  val proof_text :
blanchet@43874
    55
    Proof.context -> bool -> isar_params -> one_line_params -> string
paulson@24425
    56
end;
paulson@21978
    57
blanchet@40249
    58
structure Sledgehammer_ATP_Reconstruct : SLEDGEHAMMER_ATP_RECONSTRUCT =
paulson@21978
    59
struct
paulson@21978
    60
blanchet@38262
    61
open ATP_Problem
blanchet@39692
    62
open ATP_Proof
blanchet@39734
    63
open Metis_Translate
blanchet@36474
    64
open Sledgehammer_Util
blanchet@39232
    65
open Sledgehammer_Filter
blanchet@40255
    66
open Sledgehammer_ATP_Translate
blanchet@35826
    67
blanchet@43874
    68
datatype reconstructor =
blanchet@43874
    69
  Metis |
blanchet@43874
    70
  MetisFT |
blanchet@43874
    71
  SMT of string
blanchet@43874
    72
blanchet@43874
    73
datatype preplay =
blanchet@43874
    74
  Preplayed of reconstructor * Time.time |
blanchet@43874
    75
  Trust_Playable of reconstructor * Time.time option |
blanchet@43874
    76
  Preplay_Failed
blanchet@43874
    77
blanchet@36281
    78
type minimize_command = string list -> string
blanchet@43874
    79
type one_line_params =
blanchet@43874
    80
  preplay * string * (string * locality) list * minimize_command * thm * int
blanchet@39053
    81
type isar_params =
blanchet@43874
    82
  bool * bool * int * type_system * string Symtab.table * int list list * int
blanchet@43874
    83
  * (string * locality) list vector * int Symtab.table * string proof
blanchet@36281
    84
blanchet@39740
    85
fun is_head_digit s = Char.isDigit (String.sub (s, 0))
blanchet@39740
    86
val scan_integer = Scan.many1 is_head_digit >> (the o Int.fromString o implode)
blanchet@39740
    87
blanchet@43750
    88
val is_typed_helper_name =
blanchet@43750
    89
  String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix
blanchet@43750
    90
blanchet@39740
    91
fun find_first_in_list_vector vec key =
blanchet@39740
    92
  Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key
blanchet@39740
    93
                 | (_, value) => value) NONE vec
blanchet@39740
    94
blanchet@39693
    95
blanchet@39733
    96
(** SPASS's Flotter hack **)
blanchet@39733
    97
blanchet@40445
    98
(* This is a hack required for keeping track of facts after they have been
blanchet@39733
    99
   clausified by SPASS's Flotter tool. The "ATP/scripts/spass" script is also
blanchet@39733
   100
   part of this hack. *)
blanchet@39733
   101
blanchet@39733
   102
val set_ClauseFormulaRelationN = "set_ClauseFormulaRelation"
blanchet@39733
   103
blanchet@39733
   104
fun extract_clause_sequence output =
blanchet@39733
   105
  let
blanchet@39733
   106
    val tokens_of = String.tokens (not o Char.isAlphaNum)
blanchet@43458
   107
    fun extract_num ("clause" :: (ss as _ :: _)) = Int.fromString (List.last ss)
blanchet@39733
   108
      | extract_num _ = NONE
blanchet@39733
   109
  in output |> split_lines |> map_filter (extract_num o tokens_of) end
blanchet@39733
   110
blanchet@39733
   111
val parse_clause_formula_pair =
blanchet@39733
   112
  $$ "(" |-- scan_integer --| $$ ","
blanchet@39733
   113
  -- (Symbol.scan_id ::: Scan.repeat ($$ "," |-- Symbol.scan_id)) --| $$ ")"
blanchet@39733
   114
  --| Scan.option ($$ ",")
blanchet@39733
   115
val parse_clause_formula_relation =
blanchet@39733
   116
  Scan.this_string set_ClauseFormulaRelationN |-- $$ "("
blanchet@39733
   117
  |-- Scan.repeat parse_clause_formula_pair
blanchet@39733
   118
val extract_clause_formula_relation =
blanchet@39733
   119
  Substring.full #> Substring.position set_ClauseFormulaRelationN
blanchet@39733
   120
  #> snd #> Substring.position "." #> fst #> Substring.string
wenzelm@40875
   121
  #> raw_explode #> filter_out Symbol.is_blank #> parse_clause_formula_relation
blanchet@39733
   122
  #> fst
blanchet@39733
   123
blanchet@43518
   124
fun maybe_unprefix_fact_number type_sys =
blanchet@43518
   125
  polymorphism_of_type_sys type_sys <> Polymorphic
blanchet@43518
   126
  ? (space_implode "_" o tl o space_explode "_")
blanchet@43051
   127
blanchet@43518
   128
fun repair_conjecture_shape_and_fact_names type_sys output conjecture_shape
blanchet@43750
   129
        fact_offset fact_names typed_helpers =
blanchet@39733
   130
  if String.isSubstring set_ClauseFormulaRelationN output then
blanchet@39733
   131
    let
blanchet@39733
   132
      val j0 = hd (hd conjecture_shape)
blanchet@39733
   133
      val seq = extract_clause_sequence output
blanchet@39733
   134
      val name_map = extract_clause_formula_relation output
blanchet@39733
   135
      fun renumber_conjecture j =
blanchet@39733
   136
        conjecture_prefix ^ string_of_int (j - j0)
blanchet@39733
   137
        |> AList.find (fn (s, ss) => member (op =) ss s) name_map
blanchet@39733
   138
        |> map (fn s => find_index (curry (op =) s) seq + 1)
blanchet@39733
   139
      fun names_for_number j =
blanchet@39733
   140
        j |> AList.lookup (op =) name_map |> these
blanchet@43518
   141
          |> map_filter (try (unascii_of o maybe_unprefix_fact_number type_sys
blanchet@43051
   142
                              o unprefix fact_prefix))
blanchet@39733
   143
          |> map (fn name =>
blanchet@40445
   144
                     (name, name |> find_first_in_list_vector fact_names |> the)
blanchet@39733
   145
                     handle Option.Option =>
blanchet@39733
   146
                            error ("No such fact: " ^ quote name ^ "."))
blanchet@39733
   147
    in
blanchet@43458
   148
      (conjecture_shape |> map (maps renumber_conjecture), 0,
blanchet@43750
   149
       seq |> map names_for_number |> Vector.fromList,
blanchet@43750
   150
       name_map |> filter (forall is_typed_helper_name o snd) |> map fst)
blanchet@39733
   151
    end
blanchet@39733
   152
  else
blanchet@43750
   153
    (conjecture_shape, fact_offset, fact_names, typed_helpers)
blanchet@39733
   154
blanchet@43051
   155
val vampire_step_prefix = "f" (* grrr... *)
blanchet@41451
   156
blanchet@43750
   157
val extract_step_number =
blanchet@43750
   158
  Int.fromString o perhaps (try (unprefix vampire_step_prefix))
blanchet@43750
   159
blanchet@43616
   160
fun resolve_fact type_sys _ fact_names (_, SOME s) =
blanchet@43051
   161
    (case try (unprefix fact_prefix) s of
blanchet@43051
   162
       SOME s' =>
blanchet@43518
   163
       let val s' = s' |> maybe_unprefix_fact_number type_sys |> unascii_of in
blanchet@43051
   164
         case find_first_in_list_vector fact_names s' of
blanchet@43051
   165
           SOME x => [(s', x)]
blanchet@43051
   166
         | NONE => []
blanchet@43051
   167
       end
blanchet@39693
   168
     | NONE => [])
blanchet@43518
   169
  | resolve_fact _ facts_offset fact_names (num, NONE) =
blanchet@43750
   170
    (case extract_step_number num of
blanchet@43750
   171
       SOME j =>
blanchet@43750
   172
       let val j = j - facts_offset in
blanchet@43750
   173
         if j > 0 andalso j <= Vector.length fact_names then
blanchet@43750
   174
           Vector.sub (fact_names, j - 1)
blanchet@43750
   175
         else
blanchet@43750
   176
           []
blanchet@43750
   177
       end
blanchet@43750
   178
     | NONE => [])
blanchet@43750
   179
blanchet@43750
   180
fun is_fact type_sys conjecture_shape =
blanchet@43750
   181
  not o null o resolve_fact type_sys 0 conjecture_shape
blanchet@39693
   182
blanchet@43616
   183
fun resolve_conjecture _ (_, SOME s) =
blanchet@43616
   184
    (case try (unprefix conjecture_prefix) s of
blanchet@43616
   185
       SOME s' =>
blanchet@43616
   186
       (case Int.fromString s' of
blanchet@43616
   187
          SOME j => [j]
blanchet@43616
   188
        | NONE => [])
blanchet@43616
   189
     | NONE => [])
blanchet@43616
   190
  | resolve_conjecture conjecture_shape (num, NONE) =
blanchet@43750
   191
    case extract_step_number num of
blanchet@43750
   192
      SOME i => (case find_index (exists (curry (op =) i)) conjecture_shape of
blanchet@43750
   193
                   ~1 => []
blanchet@43750
   194
                 | j => [j])
blanchet@43616
   195
    | NONE => []
blanchet@43320
   196
blanchet@43320
   197
fun is_conjecture conjecture_shape =
blanchet@43320
   198
  not o null o resolve_conjecture conjecture_shape
blanchet@43320
   199
blanchet@43750
   200
fun is_typed_helper _ (_, SOME s) = is_typed_helper_name s
blanchet@43750
   201
  | is_typed_helper typed_helpers (num, NONE) =
blanchet@43750
   202
    (case extract_step_number num of
blanchet@43750
   203
       SOME i => member (op =) typed_helpers i
blanchet@43750
   204
     | NONE => false)
blanchet@43750
   205
blanchet@43809
   206
val leo2_ext = "extcnf_equal_neg"
blanchet@43809
   207
val isa_ext = Thm.get_name_hint @{thm ext}
blanchet@43809
   208
val isa_short_ext = Long_Name.base_name isa_ext
blanchet@43809
   209
blanchet@43809
   210
fun ext_name ctxt =
blanchet@43809
   211
  if Thm.eq_thm_prop (@{thm ext},
blanchet@43809
   212
         singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then
blanchet@43809
   213
    isa_short_ext
blanchet@43809
   214
  else
blanchet@43809
   215
    isa_ext
blanchet@43809
   216
blanchet@43809
   217
fun add_fact _ type_sys facts_offset fact_names (Inference (name, _, [])) =
blanchet@43784
   218
    union (op =) (resolve_fact type_sys facts_offset fact_names name)
blanchet@43809
   219
  | add_fact ctxt _ _ _ (Inference (_, _, deps)) =
blanchet@43809
   220
    if AList.defined (op =) deps leo2_ext then
blanchet@43809
   221
      insert (op =) (ext_name ctxt, General (* or Chained... *))
blanchet@43809
   222
    else
blanchet@43809
   223
      I
blanchet@43809
   224
  | add_fact _ _ _ _ _ = I
blanchet@39693
   225
blanchet@43809
   226
fun used_facts_in_atp_proof ctxt type_sys facts_offset fact_names atp_proof =
blanchet@43320
   227
  if null atp_proof then Vector.foldl (op @) [] fact_names
blanchet@43809
   228
  else fold (add_fact ctxt type_sys facts_offset fact_names) atp_proof []
blanchet@43320
   229
blanchet@43320
   230
fun is_conjecture_referred_to_in_proof conjecture_shape =
blanchet@43320
   231
  exists (fn Inference (name, _, []) => is_conjecture conjecture_shape name
blanchet@43320
   232
           | _ => false)
blanchet@43320
   233
blanchet@43809
   234
fun used_facts_in_unsound_atp_proof ctxt type_sys conjecture_shape facts_offset
blanchet@43745
   235
                                    fact_names atp_proof =
blanchet@43745
   236
  let
blanchet@43809
   237
    val used_facts =
blanchet@43809
   238
      used_facts_in_atp_proof ctxt type_sys facts_offset fact_names atp_proof
blanchet@43745
   239
  in
blanchet@43745
   240
    if forall (is_locality_global o snd) used_facts andalso
blanchet@43745
   241
       not (is_conjecture_referred_to_in_proof conjecture_shape atp_proof) then
blanchet@43745
   242
      SOME (map fst used_facts)
blanchet@43745
   243
    else
blanchet@43745
   244
      NONE
blanchet@43745
   245
  end
blanchet@43320
   246
blanchet@43874
   247
fun uses_typed_helpers typed_helpers =
blanchet@43874
   248
  exists (fn Inference (name, _, []) => is_typed_helper typed_helpers name
blanchet@43874
   249
           | _ => false)
blanchet@43874
   250
blanchet@43874
   251
blanchet@43320
   252
(** Soft-core proof reconstruction: Metis one-liner **)
blanchet@43320
   253
blanchet@43874
   254
fun reconstructor_name Metis = "metis"
blanchet@43874
   255
  | reconstructor_name MetisFT = "metisFT"
blanchet@43874
   256
  | reconstructor_name (SMT _) = "smt"
blanchet@43874
   257
blanchet@43874
   258
fun reconstructor_settings (SMT settings) = settings
blanchet@43874
   259
  | reconstructor_settings _ = ""
blanchet@43874
   260
blanchet@43320
   261
fun string_for_label (s, num) = s ^ string_of_int num
blanchet@43320
   262
blanchet@43874
   263
fun show_time NONE = ""
blanchet@43874
   264
  | show_time (SOME ext_time) = " ~ " ^ string_from_ext_time ext_time
blanchet@43874
   265
blanchet@43320
   266
fun set_settings "" = ""
blanchet@43320
   267
  | set_settings settings = "using [[" ^ settings ^ "]] "
blanchet@43320
   268
fun apply_on_subgoal settings _ 1 = set_settings settings ^ "by "
blanchet@43320
   269
  | apply_on_subgoal settings 1 _ = set_settings settings ^ "apply "
blanchet@43320
   270
  | apply_on_subgoal settings i n =
blanchet@43320
   271
    "prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal settings 1 n
blanchet@43320
   272
fun command_call name [] = name
blanchet@43320
   273
  | command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")"
blanchet@43874
   274
fun try_command_line banner time command =
blanchet@43874
   275
  banner ^ ": " ^ Markup.markup Markup.sendback command ^ show_time time ^ "."
blanchet@43320
   276
fun using_labels [] = ""
blanchet@43320
   277
  | using_labels ls =
blanchet@43320
   278
    "using " ^ space_implode " " (map string_for_label ls) ^ " "
blanchet@43874
   279
fun reconstructor_command reconstructor i n (ls, ss) =
blanchet@43874
   280
  using_labels ls ^
blanchet@43874
   281
  apply_on_subgoal (reconstructor_settings reconstructor) i n ^
blanchet@43874
   282
  command_call (reconstructor_name reconstructor) ss
blanchet@43320
   283
fun minimize_line _ [] = ""
blanchet@43320
   284
  | minimize_line minimize_command ss =
blanchet@43320
   285
    case minimize_command ss of
blanchet@43320
   286
      "" => ""
blanchet@43847
   287
    | command => "\nTo minimize: " ^ Markup.markup Markup.sendback command ^ "."
blanchet@39693
   288
blanchet@40966
   289
val split_used_facts =
blanchet@40966
   290
  List.partition (curry (op =) Chained o snd)
blanchet@39693
   291
  #> pairself (sort_distinct (string_ord o pairself fst))
blanchet@39693
   292
blanchet@43874
   293
fun one_line_proof_text (preplay, banner, used_facts, minimize_command,
blanchet@43874
   294
                         goal, i) =
blanchet@39693
   295
  let
blanchet@43874
   296
    val (chained, extra) = split_used_facts used_facts
blanchet@43874
   297
    val (reconstructor, ext_time) =
blanchet@43874
   298
      case preplay of
blanchet@43874
   299
        Preplayed (reconstructor, time) =>
blanchet@43874
   300
        (SOME reconstructor, (SOME (false, time)))
blanchet@43874
   301
      | Trust_Playable (reconstructor, time) =>
blanchet@43874
   302
        (SOME reconstructor,
blanchet@43874
   303
         case time of
blanchet@43874
   304
           NONE => NONE
blanchet@43874
   305
         | SOME time =>
blanchet@43874
   306
           if time = Time.zeroTime then NONE else SOME (true, time))
blanchet@43874
   307
      | Preplay_Failed => (NONE, NONE)
blanchet@39693
   308
    val n = Logic.count_prems (prop_of goal)
blanchet@43874
   309
    val try_line =
blanchet@43874
   310
      case reconstructor of
blanchet@43874
   311
        SOME r => reconstructor_command r i n ([], map fst extra)
blanchet@43874
   312
                  |> try_command_line banner ext_time
blanchet@43874
   313
      | NONE => "Proof reconstruction failed."
blanchet@43874
   314
  in try_line ^ minimize_line minimize_command (map fst (extra @ chained)) end
blanchet@39693
   315
blanchet@39693
   316
(** Hard-core proof reconstruction: structured Isar proofs **)
blanchet@39693
   317
blanchet@38248
   318
(* Simple simplifications to ensure that sort annotations don't leave a trail of
blanchet@38248
   319
   spurious "True"s. *)
blanchet@43477
   320
fun s_not (Const (@{const_name All}, T) $ Abs (s, T', t')) =
blanchet@43477
   321
    Const (@{const_name Ex}, T) $ Abs (s, T', s_not t')
blanchet@43477
   322
  | s_not (Const (@{const_name Ex}, T) $ Abs (s, T', t')) =
blanchet@43477
   323
    Const (@{const_name All}, T) $ Abs (s, T', s_not t')
blanchet@43477
   324
  | s_not (@{const HOL.implies} $ t1 $ t2) = @{const HOL.conj} $ t1 $ s_not t2
blanchet@43477
   325
  | s_not (@{const HOL.conj} $ t1 $ t2) =
blanchet@43477
   326
    @{const HOL.disj} $ s_not t1 $ s_not t2
blanchet@43477
   327
  | s_not (@{const HOL.disj} $ t1 $ t2) =
blanchet@43477
   328
    @{const HOL.conj} $ s_not t1 $ s_not t2
blanchet@43477
   329
  | s_not (@{const False}) = @{const True}
blanchet@43477
   330
  | s_not (@{const True}) = @{const False}
blanchet@38248
   331
  | s_not (@{const Not} $ t) = t
blanchet@38248
   332
  | s_not t = @{const Not} $ t
blanchet@38248
   333
fun s_conj (@{const True}, t2) = t2
blanchet@38248
   334
  | s_conj (t1, @{const True}) = t1
blanchet@38248
   335
  | s_conj p = HOLogic.mk_conj p
blanchet@38248
   336
fun s_disj (@{const False}, t2) = t2
blanchet@38248
   337
  | s_disj (t1, @{const False}) = t1
blanchet@38248
   338
  | s_disj p = HOLogic.mk_disj p
blanchet@38248
   339
fun s_imp (@{const True}, t2) = t2
blanchet@38248
   340
  | s_imp (t1, @{const False}) = s_not t1
blanchet@38248
   341
  | s_imp p = HOLogic.mk_imp p
blanchet@38248
   342
fun s_iff (@{const True}, t2) = t2
blanchet@38248
   343
  | s_iff (t1, @{const True}) = t1
blanchet@38248
   344
  | s_iff (t1, t2) = HOLogic.eq_const HOLogic.boolT $ t1 $ t2
blanchet@38248
   345
blanchet@39671
   346
fun forall_of v t = HOLogic.all_const (fastype_of v) $ lambda v t
blanchet@39671
   347
fun exists_of v t = HOLogic.exists_const (fastype_of v) $ lambda v t
blanchet@39671
   348
blanchet@39693
   349
val indent_size = 2
blanchet@39693
   350
val no_label = ("", ~1)
blanchet@39693
   351
blanchet@39693
   352
val raw_prefix = "X"
blanchet@39693
   353
val assum_prefix = "A"
blanchet@43051
   354
val have_prefix = "F"
blanchet@39616
   355
blanchet@39693
   356
fun raw_label_for_name conjecture_shape name =
blanchet@39693
   357
  case resolve_conjecture conjecture_shape name of
blanchet@39693
   358
    [j] => (conjecture_prefix, j)
blanchet@39695
   359
  | _ => case Int.fromString (fst name) of
blanchet@39693
   360
           SOME j => (raw_prefix, j)
blanchet@39695
   361
         | NONE => (raw_prefix ^ fst name, 0)
blanchet@39693
   362
paulson@21978
   363
(**** INTERPRETATION OF TSTP SYNTAX TREES ****)
paulson@21978
   364
blanchet@38225
   365
exception FO_TERM of string fo_term list
blanchet@43402
   366
exception FORMULA of (string, string, string fo_term) formula list
blanchet@38225
   367
exception SAME of unit
paulson@21978
   368
blanchet@36901
   369
(* Type variables are given the basic sort "HOL.type". Some will later be
blanchet@38225
   370
   constrained by information from type literals, or by type inference. *)
blanchet@43434
   371
fun typ_from_fo_term tfrees (u as ATerm (a, us)) =
blanchet@43434
   372
  let val Ts = map (typ_from_fo_term tfrees) us in
blanchet@38987
   373
    case strip_prefix_and_unascii type_const_prefix a of
blanchet@38225
   374
      SOME b => Type (invert_const b, Ts)
blanchet@38225
   375
    | NONE =>
blanchet@38225
   376
      if not (null us) then
blanchet@38225
   377
        raise FO_TERM [u]  (* only "tconst"s have type arguments *)
blanchet@38987
   378
      else case strip_prefix_and_unascii tfree_prefix a of
blanchet@38225
   379
        SOME b =>
blanchet@38225
   380
        let val s = "'" ^ b in
blanchet@38225
   381
          TFree (s, AList.lookup (op =) tfrees s |> the_default HOLogic.typeS)
blanchet@38225
   382
        end
blanchet@36482
   383
      | NONE =>
blanchet@38987
   384
        case strip_prefix_and_unascii tvar_prefix a of
blanchet@38225
   385
          SOME b => TVar (("'" ^ b, 0), HOLogic.typeS)
blanchet@36482
   386
        | NONE =>
blanchet@38225
   387
          (* Variable from the ATP, say "X1" *)
blanchet@38225
   388
          Type_Infer.param 0 (a, HOLogic.typeS)
blanchet@38225
   389
  end
paulson@21978
   390
blanchet@38248
   391
(* Type class literal applied to a type. Returns triple of polarity, class,
blanchet@38248
   392
   type. *)
blanchet@43477
   393
fun type_constraint_from_term tfrees (u as ATerm (a, us)) =
blanchet@38987
   394
  case (strip_prefix_and_unascii class_prefix a,
blanchet@43434
   395
        map (typ_from_fo_term tfrees) us) of
blanchet@43477
   396
    (SOME b, [T]) => (b, T)
blanchet@38248
   397
  | _ => raise FO_TERM [u]
blanchet@38248
   398
blanchet@38331
   399
(** Accumulate type constraints in a formula: negative type literals **)
blanchet@38248
   400
fun add_var (key, z)  = Vartab.map_default (key, []) (cons z)
blanchet@43477
   401
fun add_type_constraint false (cl, TFree (a ,_)) = add_var ((a, ~1), cl)
blanchet@43477
   402
  | add_type_constraint false (cl, TVar (ix, _)) = add_var (ix, cl)
blanchet@43477
   403
  | add_type_constraint _ _ = I
blanchet@38248
   404
blanchet@43839
   405
fun repair_tptp_variable_name f s =
blanchet@36482
   406
  let
blanchet@36482
   407
    fun subscript_name s n = s ^ nat_subscript n
blanchet@38713
   408
    val s = String.map f s
blanchet@36482
   409
  in
blanchet@36482
   410
    case space_explode "_" s of
blanchet@36482
   411
      [_] => (case take_suffix Char.isDigit (String.explode s) of
blanchet@36482
   412
                (cs1 as _ :: _, cs2 as _ :: _) =>
blanchet@36482
   413
                subscript_name (String.implode cs1)
blanchet@36482
   414
                               (the (Int.fromString (String.implode cs2)))
blanchet@36482
   415
              | (_, _) => s)
blanchet@36482
   416
    | [s1, s2] => (case Int.fromString s2 of
blanchet@36482
   417
                     SOME n => subscript_name s1 n
blanchet@36482
   418
                   | NONE => s)
blanchet@36482
   419
    | _ => s
blanchet@36482
   420
  end
blanchet@36482
   421
blanchet@36901
   422
(* First-order translation. No types are known for variables. "HOLogic.typeT"
blanchet@38248
   423
   should allow them to be inferred. *)
blanchet@43620
   424
fun raw_term_from_pred thy sym_tab tfrees =
blanchet@36901
   425
  let
blanchet@37643
   426
    fun aux opt_T extra_us u =
blanchet@36901
   427
      case u of
blanchet@43410
   428
        ATerm (a, us) =>
blanchet@43803
   429
        if String.isPrefix simple_type_prefix a then
blanchet@43803
   430
          @{const True} (* ignore TPTP type information *)
blanchet@43841
   431
        else if a = tptp_equal then
blanchet@39352
   432
          let val ts = map (aux NONE []) us in
blanchet@39352
   433
            if length ts = 2 andalso hd ts aconv List.last ts then
blanchet@39352
   434
              (* Vampire is keen on producing these. *)
blanchet@39352
   435
              @{const True}
blanchet@39352
   436
            else
blanchet@39352
   437
              list_comb (Const (@{const_name HOL.eq}, HOLogic.typeT), ts)
blanchet@39352
   438
          end
blanchet@43841
   439
        else case strip_prefix_and_unascii const_prefix a of
blanchet@43841
   440
          SOME s =>
blanchet@43626
   441
          let
blanchet@43626
   442
            val ((s', s), mangled_us) = s |> unmangled_const |>> `invert_const
blanchet@43626
   443
          in
blanchet@43620
   444
            if s' = type_tag_name then
blanchet@43460
   445
              case mangled_us @ us of
blanchet@43460
   446
                [typ_u, term_u] =>
blanchet@43460
   447
                aux (SOME (typ_from_fo_term tfrees typ_u)) extra_us term_u
blanchet@43460
   448
              | _ => raise FO_TERM us
blanchet@43807
   449
            else if s' = predicator_name then
blanchet@43420
   450
              aux (SOME @{typ bool}) [] (hd us)
blanchet@43807
   451
            else if s' = app_op_name then
blanchet@43420
   452
              aux opt_T (nth us 1 :: extra_us) (hd us)
blanchet@43807
   453
            else if s' = type_pred_name then
blanchet@43422
   454
              @{const True} (* ignore type predicates *)
blanchet@43420
   455
            else
blanchet@43420
   456
              let
blanchet@43620
   457
                val num_ty_args =
blanchet@43620
   458
                  length us - the_default 0 (Symtab.lookup sym_tab s)
blanchet@43420
   459
                val (type_us, term_us) =
blanchet@43420
   460
                  chop num_ty_args us |>> append mangled_us
blanchet@43420
   461
                (* Extra args from "hAPP" come after any arguments given
blanchet@43420
   462
                   directly to the constant. *)
blanchet@43420
   463
                val term_ts = map (aux NONE []) term_us
blanchet@43420
   464
                val extra_ts = map (aux NONE []) extra_us
blanchet@43420
   465
                val T =
blanchet@43626
   466
                  if num_type_args thy s' = length type_us then
blanchet@43626
   467
                    Sign.const_instance thy
blanchet@43626
   468
                        (s', map (typ_from_fo_term tfrees) type_us)
blanchet@43626
   469
                  else case opt_T of
blanchet@43626
   470
                    SOME T => map fastype_of (term_ts @ extra_ts) ---> T
blanchet@43626
   471
                  | NONE => HOLogic.typeT
blanchet@43620
   472
                val s' = s' |> unproxify_const
blanchet@43620
   473
              in list_comb (Const (s', T), term_ts @ extra_ts) end
blanchet@43420
   474
          end
blanchet@36901
   475
        | NONE => (* a free or schematic variable *)
blanchet@36901
   476
          let
blanchet@37643
   477
            val ts = map (aux NONE []) (us @ extra_us)
blanchet@36901
   478
            val T = map fastype_of ts ---> HOLogic.typeT
blanchet@36901
   479
            val t =
blanchet@38987
   480
              case strip_prefix_and_unascii fixed_var_prefix a of
blanchet@36901
   481
                SOME b => Free (b, T)
blanchet@36901
   482
              | NONE =>
blanchet@38987
   483
                case strip_prefix_and_unascii schematic_var_prefix a of
blanchet@36960
   484
                  SOME b => Var ((b, 0), T)
blanchet@36901
   485
                | NONE =>
blanchet@43839
   486
                  if is_tptp_variable a then
blanchet@43839
   487
                    Var ((repair_tptp_variable_name Char.toLower a, 0), T)
blanchet@38251
   488
                  else
blanchet@38713
   489
                    (* Skolem constants? *)
blanchet@43839
   490
                    Var ((repair_tptp_variable_name Char.toUpper a, 0), T)
blanchet@36901
   491
          in list_comb (t, ts) end
blanchet@38248
   492
  in aux (SOME HOLogic.boolT) [] end
paulson@21978
   493
blanchet@43620
   494
fun term_from_pred thy sym_tab tfrees pos (u as ATerm (s, _)) =
blanchet@38248
   495
  if String.isPrefix class_prefix s then
blanchet@43477
   496
    add_type_constraint pos (type_constraint_from_term tfrees u)
blanchet@38248
   497
    #> pair @{const True}
blanchet@38248
   498
  else
blanchet@43620
   499
    pair (raw_term_from_pred thy sym_tab tfrees u)
blanchet@36402
   500
blanchet@36553
   501
val combinator_table =
blanchet@40134
   502
  [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def_raw}),
blanchet@40134
   503
   (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def_raw}),
blanchet@40134
   504
   (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def_raw}),
blanchet@40134
   505
   (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def_raw}),
blanchet@40134
   506
   (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def_raw})]
blanchet@36553
   507
blanchet@43626
   508
fun uncombine_term thy =
blanchet@43626
   509
  let
blanchet@43626
   510
    fun aux (t1 $ t2) = betapply (pairself aux (t1, t2))
blanchet@43626
   511
      | aux (Abs (s, T, t')) = Abs (s, T, aux t')
blanchet@43626
   512
      | aux (t as Const (x as (s, _))) =
blanchet@43626
   513
        (case AList.lookup (op =) combinator_table s of
blanchet@43626
   514
           SOME thm => thm |> prop_of |> specialize_type thy x
blanchet@43626
   515
                           |> Logic.dest_equals |> snd
blanchet@43626
   516
         | NONE => t)
blanchet@43626
   517
      | aux t = t
blanchet@43626
   518
  in aux end
blanchet@36553
   519
blanchet@38225
   520
(* Update schematic type variables with detected sort constraints. It's not
blanchet@43434
   521
   totally clear whether this code is necessary. *)
blanchet@38248
   522
fun repair_tvar_sorts (t, tvar_tab) =
blanchet@36901
   523
  let
blanchet@38225
   524
    fun do_type (Type (a, Ts)) = Type (a, map do_type Ts)
blanchet@38225
   525
      | do_type (TVar (xi, s)) =
blanchet@38225
   526
        TVar (xi, the_default s (Vartab.lookup tvar_tab xi))
blanchet@38225
   527
      | do_type (TFree z) = TFree z
blanchet@38225
   528
    fun do_term (Const (a, T)) = Const (a, do_type T)
blanchet@38225
   529
      | do_term (Free (a, T)) = Free (a, do_type T)
blanchet@38225
   530
      | do_term (Var (xi, T)) = Var (xi, do_type T)
blanchet@38225
   531
      | do_term (t as Bound _) = t
blanchet@38225
   532
      | do_term (Abs (a, T, t)) = Abs (a, do_type T, do_term t)
blanchet@38225
   533
      | do_term (t1 $ t2) = do_term t1 $ do_term t2
blanchet@38225
   534
  in t |> not (Vartab.is_empty tvar_tab) ? do_term end
blanchet@38225
   535
blanchet@39671
   536
fun quantify_over_var quant_of var_s t =
blanchet@39671
   537
  let
blanchet@39671
   538
    val vars = [] |> Term.add_vars t |> filter (fn ((s, _), _) => s = var_s)
blanchet@39671
   539
                  |> map Var
blanchet@39671
   540
  in fold_rev quant_of vars t end
blanchet@38225
   541
blanchet@38331
   542
(* Interpret an ATP formula as a HOL term, extracting sort constraints as they
blanchet@38331
   543
   appear in the formula. *)
blanchet@43620
   544
fun prop_from_formula thy sym_tab tfrees phi =
blanchet@38248
   545
  let
blanchet@38248
   546
    fun do_formula pos phi =
blanchet@38225
   547
      case phi of
blanchet@38248
   548
        AQuant (_, [], phi) => do_formula pos phi
blanchet@43397
   549
      | AQuant (q, (s, _) :: xs, phi') =>
blanchet@38248
   550
        do_formula pos (AQuant (q, xs, phi'))
blanchet@43397
   551
        (* FIXME: TFF *)
blanchet@39671
   552
        #>> quantify_over_var (case q of
blanchet@39671
   553
                                 AForall => forall_of
blanchet@39671
   554
                               | AExists => exists_of)
blanchet@43839
   555
                              (repair_tptp_variable_name Char.toLower s)
blanchet@38248
   556
      | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not
blanchet@38225
   557
      | AConn (c, [phi1, phi2]) =>
blanchet@38248
   558
        do_formula (pos |> c = AImplies ? not) phi1
blanchet@38248
   559
        ##>> do_formula pos phi2
blanchet@38248
   560
        #>> (case c of
blanchet@38248
   561
               AAnd => s_conj
blanchet@38248
   562
             | AOr => s_disj
blanchet@38248
   563
             | AImplies => s_imp
blanchet@38284
   564
             | AIf => s_imp o swap
blanchet@38284
   565
             | AIff => s_iff
blanchet@40250
   566
             | ANotIff => s_not o s_iff
blanchet@40250
   567
             | _ => raise Fail "unexpected connective")
blanchet@43620
   568
      | AAtom tm => term_from_pred thy sym_tab tfrees pos tm
blanchet@38225
   569
      | _ => raise FORMULA [phi]
blanchet@38248
   570
  in repair_tvar_sorts (do_formula true phi Vartab.empty) end
blanchet@38225
   571
blanchet@36554
   572
fun check_formula ctxt =
wenzelm@39541
   573
  Type.constraint HOLogic.boolT
blanchet@43626
   574
  #> Syntax.check_term
blanchet@43626
   575
         (Proof_Context.set_mode Proof_Context.mode_schematic ctxt)
paulson@21978
   576
paulson@21978
   577
(**** Translation of TSTP files to Isar Proofs ****)
paulson@21978
   578
blanchet@36482
   579
fun unvarify_term (Var ((s, 0), T)) = Free (s, T)
blanchet@36482
   580
  | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t])
paulson@21978
   581
blanchet@43620
   582
fun decode_line sym_tab tfrees (Definition (name, phi1, phi2)) ctxt =
blanchet@36482
   583
    let
wenzelm@43232
   584
      val thy = Proof_Context.theory_of ctxt
blanchet@43620
   585
      val t1 = prop_from_formula thy sym_tab tfrees phi1
blanchet@36549
   586
      val vars = snd (strip_comb t1)
blanchet@36482
   587
      val frees = map unvarify_term vars
blanchet@36482
   588
      val unvarify_args = subst_atomic (vars ~~ frees)
blanchet@43620
   589
      val t2 = prop_from_formula thy sym_tab tfrees phi2
blanchet@36549
   590
      val (t1, t2) =
blanchet@36549
   591
        HOLogic.eq_const HOLogic.typeT $ t1 $ t2
blanchet@43626
   592
        |> unvarify_args |> uncombine_term thy |> check_formula ctxt
blanchet@36553
   593
        |> HOLogic.dest_eq
blanchet@36482
   594
    in
blanchet@39614
   595
      (Definition (name, t1, t2),
blanchet@36549
   596
       fold Variable.declare_term (maps OldTerm.term_frees [t1, t2]) ctxt)
blanchet@36482
   597
    end
blanchet@43620
   598
  | decode_line sym_tab tfrees (Inference (name, u, deps)) ctxt =
blanchet@36549
   599
    let
wenzelm@43232
   600
      val thy = Proof_Context.theory_of ctxt
blanchet@43620
   601
      val t = u |> prop_from_formula thy sym_tab tfrees
blanchet@43626
   602
                |> uncombine_term thy |> check_formula ctxt
blanchet@36549
   603
    in
blanchet@39614
   604
      (Inference (name, t, deps),
blanchet@36549
   605
       fold Variable.declare_term (OldTerm.term_frees t) ctxt)
blanchet@36482
   606
    end
blanchet@43620
   607
fun decode_lines ctxt sym_tab tfrees lines =
blanchet@43620
   608
  fst (fold_map (decode_line sym_tab tfrees) lines ctxt)
paulson@21978
   609
blanchet@38281
   610
fun is_same_inference _ (Definition _) = false
blanchet@38281
   611
  | is_same_inference t (Inference (_, t', _)) = t aconv t'
blanchet@36482
   612
blanchet@36482
   613
(* No "real" literals means only type information (tfree_tcs, clsrel, or
blanchet@36482
   614
   clsarity). *)
blanchet@36482
   615
val is_only_type_information = curry (op aconv) HOLogic.true_const
blanchet@36482
   616
blanchet@39619
   617
fun replace_one_dependency (old, new) dep =
blanchet@43809
   618
  if is_same_atp_step dep old then new else [dep]
blanchet@39619
   619
fun replace_dependencies_in_line _ (line as Definition _) = line
blanchet@39619
   620
  | replace_dependencies_in_line p (Inference (name, t, deps)) =
blanchet@39619
   621
    Inference (name, t, fold (union (op =) o replace_one_dependency p) deps [])
paulson@21978
   622
blanchet@40445
   623
(* Discard facts; consolidate adjacent lines that prove the same formula, since
blanchet@38331
   624
   they differ only in type information.*)
blanchet@43518
   625
fun add_line _ _ _ (line as Definition _) lines = line :: lines
blanchet@43518
   626
  | add_line type_sys conjecture_shape fact_names (Inference (name, t, []))
blanchet@43518
   627
             lines =
blanchet@40445
   628
    (* No dependencies: fact, conjecture, or (for Vampire) internal facts or
blanchet@38331
   629
       definitions. *)
blanchet@43518
   630
    if is_fact type_sys fact_names name then
blanchet@40445
   631
      (* Facts are not proof lines. *)
blanchet@36482
   632
      if is_only_type_information t then
blanchet@39619
   633
        map (replace_dependencies_in_line (name, [])) lines
blanchet@36482
   634
      (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@38281
   635
      else case take_prefix (not o is_same_inference t) lines of
blanchet@39619
   636
        (_, []) => lines (* no repetition of proof line *)
blanchet@39614
   637
      | (pre, Inference (name', _, _) :: post) =>
blanchet@39619
   638
        pre @ map (replace_dependencies_in_line (name', [name])) post
blanchet@40250
   639
      | _ => raise Fail "unexpected inference"
blanchet@39616
   640
    else if is_conjecture conjecture_shape name then
blanchet@43477
   641
      Inference (name, s_not t, []) :: lines
blanchet@36482
   642
    else
blanchet@39619
   643
      map (replace_dependencies_in_line (name, [])) lines
blanchet@43518
   644
  | add_line _ _ _ (Inference (name, t, deps)) lines =
blanchet@36482
   645
    (* Type information will be deleted later; skip repetition test. *)
blanchet@36482
   646
    if is_only_type_information t then
blanchet@39614
   647
      Inference (name, t, deps) :: lines
blanchet@36482
   648
    (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@38281
   649
    else case take_prefix (not o is_same_inference t) lines of
blanchet@36482
   650
      (* FIXME: Doesn't this code risk conflating proofs involving different
blanchet@38281
   651
         types? *)
blanchet@39614
   652
       (_, []) => Inference (name, t, deps) :: lines
blanchet@39614
   653
     | (pre, Inference (name', t', _) :: post) =>
blanchet@39614
   654
       Inference (name, t', deps) ::
blanchet@39619
   655
       pre @ map (replace_dependencies_in_line (name', [name])) post
blanchet@40250
   656
     | _ => raise Fail "unexpected inference"
paulson@22044
   657
blanchet@36482
   658
(* Recursively delete empty lines (type information) from the proof. *)
blanchet@39614
   659
fun add_nontrivial_line (Inference (name, t, [])) lines =
blanchet@39619
   660
    if is_only_type_information t then delete_dependency name lines
blanchet@39614
   661
    else Inference (name, t, []) :: lines
blanchet@36482
   662
  | add_nontrivial_line line lines = line :: lines
blanchet@39619
   663
and delete_dependency name lines =
blanchet@39619
   664
  fold_rev add_nontrivial_line
blanchet@39619
   665
           (map (replace_dependencies_in_line (name, [])) lines) []
paulson@22470
   666
blanchet@37320
   667
(* ATPs sometimes reuse free variable names in the strangest ways. Removing
blanchet@37320
   668
   offending lines often does the trick. *)
blanchet@36558
   669
fun is_bad_free frees (Free x) = not (member (op =) frees x)
blanchet@36558
   670
  | is_bad_free _ _ = false
paulson@22731
   671
blanchet@43518
   672
fun add_desired_line _ _ _ _ _ (line as Definition (name, _, _)) (j, lines) =
blanchet@39619
   673
    (j, line :: map (replace_dependencies_in_line (name, [])) lines)
blanchet@43518
   674
  | add_desired_line type_sys isar_shrink_factor conjecture_shape fact_names
blanchet@43518
   675
                     frees (Inference (name, t, deps)) (j, lines) =
blanchet@36402
   676
    (j + 1,
blanchet@43518
   677
     if is_fact type_sys fact_names name orelse
blanchet@39616
   678
        is_conjecture conjecture_shape name orelse
blanchet@39619
   679
        (* the last line must be kept *)
blanchet@39619
   680
        j = 0 orelse
blanchet@36568
   681
        (not (is_only_type_information t) andalso
blanchet@36568
   682
         null (Term.add_tvars t []) andalso
blanchet@36568
   683
         not (exists_subterm (is_bad_free frees) t) andalso
blanchet@39619
   684
         length deps >= 2 andalso j mod isar_shrink_factor = 0 andalso
blanchet@39619
   685
         (* kill next to last line, which usually results in a trivial step *)
blanchet@39619
   686
         j <> 1) then
blanchet@39614
   687
       Inference (name, t, deps) :: lines  (* keep line *)
blanchet@36402
   688
     else
blanchet@39619
   689
       map (replace_dependencies_in_line (name, deps)) lines)  (* drop line *)
paulson@21978
   690
blanchet@36482
   691
(** Isar proof construction and manipulation **)
blanchet@36482
   692
blanchet@36482
   693
fun merge_fact_sets (ls1, ss1) (ls2, ss2) =
blanchet@36482
   694
  (union (op =) ls1 ls2, union (op =) ss1 ss2)
blanchet@36402
   695
blanchet@36402
   696
type label = string * int
blanchet@36402
   697
type facts = label list * string list
blanchet@36402
   698
blanchet@39692
   699
datatype isar_qualifier = Show | Then | Moreover | Ultimately
blanchet@36402
   700
blanchet@39692
   701
datatype isar_step =
blanchet@36474
   702
  Fix of (string * typ) list |
blanchet@36482
   703
  Let of term * term |
blanchet@36402
   704
  Assume of label * term |
blanchet@39692
   705
  Have of isar_qualifier list * label * term * byline
blanchet@36402
   706
and byline =
blanchet@36562
   707
  ByMetis of facts |
blanchet@39692
   708
  CaseSplit of isar_step list list * facts
blanchet@36402
   709
blanchet@36572
   710
fun smart_case_split [] facts = ByMetis facts
blanchet@36572
   711
  | smart_case_split proofs facts = CaseSplit (proofs, facts)
blanchet@36572
   712
blanchet@43518
   713
fun add_fact_from_dependency type_sys conjecture_shape facts_offset fact_names
blanchet@43518
   714
                             name =
blanchet@43518
   715
  if is_fact type_sys fact_names name then
blanchet@43518
   716
    apsnd (union (op =)
blanchet@43518
   717
          (map fst (resolve_fact type_sys facts_offset fact_names name)))
blanchet@36471
   718
  else
blanchet@39616
   719
    apfst (insert (op =) (raw_label_for_name conjecture_shape name))
blanchet@36402
   720
blanchet@43518
   721
fun step_for_line _ _ _ _ _ (Definition (_, t1, t2)) = Let (t1, t2)
blanchet@43518
   722
  | step_for_line _ conjecture_shape _ _ _ (Inference (name, t, [])) =
blanchet@39616
   723
    Assume (raw_label_for_name conjecture_shape name, t)
blanchet@43518
   724
  | step_for_line type_sys conjecture_shape facts_offset
blanchet@43518
   725
                  fact_names j (Inference (name, t, deps)) =
blanchet@39616
   726
    Have (if j = 1 then [Show] else [],
blanchet@39671
   727
          raw_label_for_name conjecture_shape name,
blanchet@39671
   728
          fold_rev forall_of (map Var (Term.add_vars t [])) t,
blanchet@43518
   729
          ByMetis (fold (add_fact_from_dependency type_sys conjecture_shape
blanchet@43518
   730
                                                  facts_offset fact_names)
blanchet@39619
   731
                        deps ([], [])))
blanchet@36291
   732
blanchet@39694
   733
fun repair_name "$true" = "c_True"
blanchet@39694
   734
  | repair_name "$false" = "c_False"
blanchet@43841
   735
  | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
blanchet@39694
   736
  | repair_name s =
blanchet@43841
   737
    if is_tptp_equal s orelse
blanchet@43841
   738
       (* seen in Vampire proofs *)
blanchet@43841
   739
       (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then
blanchet@43841
   740
      tptp_equal
blanchet@39694
   741
    else
blanchet@39694
   742
      s
blanchet@39694
   743
blanchet@43320
   744
fun isar_proof_from_atp_proof pool ctxt type_sys tfrees isar_shrink_factor
blanchet@43874
   745
        conjecture_shape facts_offset fact_names sym_tab params frees
blanchet@43874
   746
        atp_proof =
wenzelm@33318
   747
  let
blanchet@36482
   748
    val lines =
blanchet@43320
   749
      atp_proof
blanchet@43809
   750
      |> clean_up_atp_proof_dependencies
blanchet@39694
   751
      |> nasty_atp_proof pool
blanchet@39694
   752
      |> map_term_names_in_atp_proof repair_name
blanchet@43620
   753
      |> decode_lines ctxt sym_tab tfrees
blanchet@43518
   754
      |> rpair [] |-> fold_rev (add_line type_sys conjecture_shape fact_names)
blanchet@36482
   755
      |> rpair [] |-> fold_rev add_nontrivial_line
blanchet@43518
   756
      |> rpair (0, [])
blanchet@43518
   757
      |-> fold_rev (add_desired_line type_sys isar_shrink_factor
blanchet@43518
   758
                                     conjecture_shape fact_names frees)
blanchet@36482
   759
      |> snd
blanchet@36402
   760
  in
blanchet@36901
   761
    (if null params then [] else [Fix params]) @
blanchet@43518
   762
    map2 (step_for_line type_sys conjecture_shape facts_offset fact_names)
blanchet@43412
   763
         (length lines downto 1) lines
blanchet@36402
   764
  end
blanchet@36402
   765
blanchet@36402
   766
(* When redirecting proofs, we keep information about the labels seen so far in
blanchet@36402
   767
   the "backpatches" data structure. The first component indicates which facts
blanchet@36402
   768
   should be associated with forthcoming proof steps. The second component is a
blanchet@37319
   769
   pair ("assum_ls", "drop_ls"), where "assum_ls" are the labels that should
blanchet@37319
   770
   become assumptions and "drop_ls" are the labels that should be dropped in a
blanchet@37319
   771
   case split. *)
blanchet@36402
   772
type backpatches = (label * facts) list * (label list * label list)
blanchet@36402
   773
blanchet@36554
   774
fun used_labels_of_step (Have (_, _, _, by)) =
blanchet@36402
   775
    (case by of
blanchet@36562
   776
       ByMetis (ls, _) => ls
blanchet@36554
   777
     | CaseSplit (proofs, (ls, _)) =>
blanchet@36554
   778
       fold (union (op =) o used_labels_of) proofs ls)
blanchet@36554
   779
  | used_labels_of_step _ = []
blanchet@36554
   780
and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof []
blanchet@36402
   781
blanchet@36402
   782
fun new_labels_of_step (Fix _) = []
blanchet@36482
   783
  | new_labels_of_step (Let _) = []
blanchet@36402
   784
  | new_labels_of_step (Assume (l, _)) = [l]
blanchet@36402
   785
  | new_labels_of_step (Have (_, l, _, _)) = [l]
blanchet@36402
   786
val new_labels_of = maps new_labels_of_step
blanchet@36402
   787
blanchet@36402
   788
val join_proofs =
blanchet@36402
   789
  let
blanchet@36402
   790
    fun aux _ [] = NONE
blanchet@36402
   791
      | aux proof_tail (proofs as (proof1 :: _)) =
blanchet@36402
   792
        if exists null proofs then
blanchet@36402
   793
          NONE
blanchet@36402
   794
        else if forall (curry (op =) (hd proof1) o hd) (tl proofs) then
blanchet@36402
   795
          aux (hd proof1 :: proof_tail) (map tl proofs)
blanchet@36402
   796
        else case hd proof1 of
blanchet@37498
   797
          Have ([], l, t, _) => (* FIXME: should we really ignore the "by"? *)
blanchet@36402
   798
          if forall (fn Have ([], l', t', _) :: _ => (l, t) = (l', t')
blanchet@36402
   799
                      | _ => false) (tl proofs) andalso
blanchet@36402
   800
             not (exists (member (op =) (maps new_labels_of proofs))
blanchet@36554
   801
                         (used_labels_of proof_tail)) then
blanchet@36402
   802
            SOME (l, t, map rev proofs, proof_tail)
blanchet@36402
   803
          else
blanchet@36402
   804
            NONE
blanchet@36402
   805
        | _ => NONE
blanchet@36402
   806
  in aux [] o map rev end
blanchet@36402
   807
blanchet@36402
   808
fun case_split_qualifiers proofs =
blanchet@36402
   809
  case length proofs of
blanchet@36402
   810
    0 => []
blanchet@36402
   811
  | 1 => [Then]
blanchet@36402
   812
  | _ => [Ultimately]
blanchet@36402
   813
blanchet@39618
   814
fun redirect_proof hyp_ts concl_t proof =
blanchet@36402
   815
  let
blanchet@37321
   816
    (* The first pass outputs those steps that are independent of the negated
blanchet@37321
   817
       conjecture. The second pass flips the proof by contradiction to obtain a
blanchet@37321
   818
       direct proof, introducing case splits when an inference depends on
blanchet@37321
   819
       several facts that depend on the negated conjecture. *)
blanchet@39618
   820
     val concl_l = (conjecture_prefix, length hyp_ts)
blanchet@38286
   821
     fun first_pass ([], contra) = ([], contra)
blanchet@38286
   822
       | first_pass ((step as Fix _) :: proof, contra) =
blanchet@38286
   823
         first_pass (proof, contra) |>> cons step
blanchet@38286
   824
       | first_pass ((step as Let _) :: proof, contra) =
blanchet@38286
   825
         first_pass (proof, contra) |>> cons step
blanchet@39616
   826
       | first_pass ((step as Assume (l as (_, j), _)) :: proof, contra) =
blanchet@39618
   827
         if l = concl_l then first_pass (proof, contra ||> cons step)
blanchet@39618
   828
         else first_pass (proof, contra) |>> cons (Assume (l, nth hyp_ts j))
blanchet@38286
   829
       | first_pass (Have (qs, l, t, ByMetis (ls, ss)) :: proof, contra) =
blanchet@39618
   830
         let val step = Have (qs, l, t, ByMetis (ls, ss)) in
blanchet@38286
   831
           if exists (member (op =) (fst contra)) ls then
blanchet@38286
   832
             first_pass (proof, contra |>> cons l ||> cons step)
blanchet@38286
   833
           else
blanchet@38286
   834
             first_pass (proof, contra) |>> cons step
blanchet@38286
   835
         end
blanchet@38286
   836
       | first_pass _ = raise Fail "malformed proof"
blanchet@36402
   837
    val (proof_top, (contra_ls, contra_proof)) =
blanchet@39618
   838
      first_pass (proof, ([concl_l], []))
blanchet@36402
   839
    val backpatch_label = the_default ([], []) oo AList.lookup (op =) o fst
blanchet@36402
   840
    fun backpatch_labels patches ls =
blanchet@36402
   841
      fold merge_fact_sets (map (backpatch_label patches) ls) ([], [])
blanchet@36402
   842
    fun second_pass end_qs ([], assums, patches) =
blanchet@37321
   843
        ([Have (end_qs, no_label, concl_t,
blanchet@36562
   844
                ByMetis (backpatch_labels patches (map snd assums)))], patches)
blanchet@36402
   845
      | second_pass end_qs (Assume (l, t) :: proof, assums, patches) =
blanchet@36402
   846
        second_pass end_qs (proof, (t, l) :: assums, patches)
blanchet@36562
   847
      | second_pass end_qs (Have (qs, l, t, ByMetis (ls, ss)) :: proof, assums,
blanchet@36402
   848
                            patches) =
blanchet@39619
   849
        (if member (op =) (snd (snd patches)) l andalso
blanchet@39619
   850
            not (member (op =) (fst (snd patches)) l) andalso
blanchet@39619
   851
            not (AList.defined (op =) (fst patches) l) then
blanchet@39619
   852
           second_pass end_qs (proof, assums, patches ||> apsnd (append ls))
blanchet@39619
   853
         else case List.partition (member (op =) contra_ls) ls of
blanchet@39619
   854
           ([contra_l], co_ls) =>
blanchet@39619
   855
           if member (op =) qs Show then
blanchet@39619
   856
             second_pass end_qs (proof, assums,
blanchet@39619
   857
                                 patches |>> cons (contra_l, (co_ls, ss)))
blanchet@39619
   858
           else
blanchet@39619
   859
             second_pass end_qs
blanchet@39619
   860
                         (proof, assums,
blanchet@39619
   861
                          patches |>> cons (contra_l, (l :: co_ls, ss)))
blanchet@39619
   862
             |>> cons (if member (op =) (fst (snd patches)) l then
blanchet@43477
   863
                         Assume (l, s_not t)
blanchet@39619
   864
                       else
blanchet@43477
   865
                         Have (qs, l, s_not t,
blanchet@39619
   866
                               ByMetis (backpatch_label patches l)))
blanchet@39619
   867
         | (contra_ls as _ :: _, co_ls) =>
blanchet@39619
   868
           let
blanchet@39619
   869
             val proofs =
blanchet@39619
   870
               map_filter
blanchet@39619
   871
                   (fn l =>
blanchet@39619
   872
                       if l = concl_l then
blanchet@39619
   873
                         NONE
blanchet@39619
   874
                       else
blanchet@39619
   875
                         let
blanchet@39619
   876
                           val drop_ls = filter (curry (op <>) l) contra_ls
blanchet@39619
   877
                         in
blanchet@39619
   878
                           second_pass []
blanchet@39619
   879
                               (proof, assums,
blanchet@39619
   880
                                patches ||> apfst (insert (op =) l)
blanchet@39619
   881
                                        ||> apsnd (union (op =) drop_ls))
blanchet@39619
   882
                           |> fst |> SOME
blanchet@39619
   883
                         end) contra_ls
blanchet@39619
   884
             val (assumes, facts) =
blanchet@39619
   885
               if member (op =) (fst (snd patches)) l then
blanchet@43477
   886
                 ([Assume (l, s_not t)], (l :: co_ls, ss))
blanchet@39619
   887
               else
blanchet@39619
   888
                 ([], (co_ls, ss))
blanchet@39619
   889
           in
blanchet@39619
   890
             (case join_proofs proofs of
blanchet@39619
   891
                SOME (l, t, proofs, proof_tail) =>
blanchet@39619
   892
                Have (case_split_qualifiers proofs @
blanchet@39619
   893
                      (if null proof_tail then end_qs else []), l, t,
blanchet@39619
   894
                      smart_case_split proofs facts) :: proof_tail
blanchet@39619
   895
              | NONE =>
blanchet@39619
   896
                [Have (case_split_qualifiers proofs @ end_qs, no_label,
blanchet@39619
   897
                       concl_t, smart_case_split proofs facts)],
blanchet@39619
   898
              patches)
blanchet@39619
   899
             |>> append assumes
blanchet@39619
   900
           end
blanchet@39619
   901
         | _ => raise Fail "malformed proof")
blanchet@36402
   902
       | second_pass _ _ = raise Fail "malformed proof"
blanchet@36482
   903
    val proof_bottom =
blanchet@36482
   904
      second_pass [Show] (contra_proof, [], ([], ([], []))) |> fst
blanchet@36402
   905
  in proof_top @ proof_bottom end
blanchet@36402
   906
blanchet@38715
   907
(* FIXME: Still needed? Probably not. *)
blanchet@36402
   908
val kill_duplicate_assumptions_in_proof =
blanchet@36402
   909
  let
blanchet@36402
   910
    fun relabel_facts subst =
blanchet@36402
   911
      apfst (map (fn l => AList.lookup (op =) subst l |> the_default l))
blanchet@36487
   912
    fun do_step (step as Assume (l, t)) (proof, subst, assums) =
blanchet@36402
   913
        (case AList.lookup (op aconv) assums t of
blanchet@36960
   914
           SOME l' => (proof, (l, l') :: subst, assums)
blanchet@36487
   915
         | NONE => (step :: proof, subst, (t, l) :: assums))
blanchet@36402
   916
      | do_step (Have (qs, l, t, by)) (proof, subst, assums) =
blanchet@36402
   917
        (Have (qs, l, t,
blanchet@36402
   918
               case by of
blanchet@36562
   919
                 ByMetis facts => ByMetis (relabel_facts subst facts)
blanchet@36402
   920
               | CaseSplit (proofs, facts) =>
blanchet@36402
   921
                 CaseSplit (map do_proof proofs, relabel_facts subst facts)) ::
blanchet@36402
   922
         proof, subst, assums)
blanchet@36487
   923
      | do_step step (proof, subst, assums) = (step :: proof, subst, assums)
blanchet@36402
   924
    and do_proof proof = fold do_step proof ([], [], []) |> #1 |> rev
blanchet@36402
   925
  in do_proof end
blanchet@36402
   926
blanchet@36402
   927
val then_chain_proof =
blanchet@36402
   928
  let
blanchet@36402
   929
    fun aux _ [] = []
blanchet@36487
   930
      | aux _ ((step as Assume (l, _)) :: proof) = step :: aux l proof
blanchet@36402
   931
      | aux l' (Have (qs, l, t, by) :: proof) =
blanchet@36402
   932
        (case by of
blanchet@36562
   933
           ByMetis (ls, ss) =>
blanchet@36402
   934
           Have (if member (op =) ls l' then
blanchet@36402
   935
                   (Then :: qs, l, t,
blanchet@36562
   936
                    ByMetis (filter_out (curry (op =) l') ls, ss))
blanchet@36402
   937
                 else
blanchet@36562
   938
                   (qs, l, t, ByMetis (ls, ss)))
blanchet@36402
   939
         | CaseSplit (proofs, facts) =>
blanchet@36402
   940
           Have (qs, l, t, CaseSplit (map (aux no_label) proofs, facts))) ::
blanchet@36402
   941
        aux l proof
blanchet@36487
   942
      | aux _ (step :: proof) = step :: aux no_label proof
blanchet@36402
   943
  in aux no_label end
blanchet@36402
   944
blanchet@36402
   945
fun kill_useless_labels_in_proof proof =
blanchet@36402
   946
  let
blanchet@36554
   947
    val used_ls = used_labels_of proof
blanchet@36402
   948
    fun do_label l = if member (op =) used_ls l then l else no_label
blanchet@36554
   949
    fun do_step (Assume (l, t)) = Assume (do_label l, t)
blanchet@36554
   950
      | do_step (Have (qs, l, t, by)) =
blanchet@36402
   951
        Have (qs, do_label l, t,
blanchet@36402
   952
              case by of
blanchet@36402
   953
                CaseSplit (proofs, facts) =>
blanchet@36554
   954
                CaseSplit (map (map do_step) proofs, facts)
blanchet@36402
   955
              | _ => by)
blanchet@36554
   956
      | do_step step = step
blanchet@36554
   957
  in map do_step proof end
blanchet@36402
   958
blanchet@36402
   959
fun prefix_for_depth n = replicate_string (n + 1)
blanchet@36402
   960
blanchet@36402
   961
val relabel_proof =
blanchet@36402
   962
  let
blanchet@36402
   963
    fun aux _ _ _ [] = []
blanchet@36402
   964
      | aux subst depth (next_assum, next_fact) (Assume (l, t) :: proof) =
blanchet@36402
   965
        if l = no_label then
blanchet@36402
   966
          Assume (l, t) :: aux subst depth (next_assum, next_fact) proof
blanchet@36402
   967
        else
blanchet@36402
   968
          let val l' = (prefix_for_depth depth assum_prefix, next_assum) in
blanchet@36402
   969
            Assume (l', t) ::
blanchet@36402
   970
            aux ((l, l') :: subst) depth (next_assum + 1, next_fact) proof
blanchet@36402
   971
          end
blanchet@36402
   972
      | aux subst depth (next_assum, next_fact) (Have (qs, l, t, by) :: proof) =
blanchet@36402
   973
        let
blanchet@36402
   974
          val (l', subst, next_fact) =
blanchet@36402
   975
            if l = no_label then
blanchet@36402
   976
              (l, subst, next_fact)
blanchet@36402
   977
            else
blanchet@36402
   978
              let
blanchet@43051
   979
                val l' = (prefix_for_depth depth have_prefix, next_fact)
blanchet@36402
   980
              in (l', (l, l') :: subst, next_fact + 1) end
blanchet@36568
   981
          val relabel_facts =
blanchet@39616
   982
            apfst (maps (the_list o AList.lookup (op =) subst))
blanchet@36402
   983
          val by =
blanchet@36402
   984
            case by of
blanchet@36562
   985
              ByMetis facts => ByMetis (relabel_facts facts)
blanchet@36402
   986
            | CaseSplit (proofs, facts) =>
blanchet@36402
   987
              CaseSplit (map (aux subst (depth + 1) (1, 1)) proofs,
blanchet@36402
   988
                         relabel_facts facts)
blanchet@36402
   989
        in
blanchet@36402
   990
          Have (qs, l', t, by) ::
blanchet@36402
   991
          aux subst depth (next_assum, next_fact) proof
blanchet@36402
   992
        end
blanchet@36487
   993
      | aux subst depth nextp (step :: proof) =
blanchet@36487
   994
        step :: aux subst depth nextp proof
blanchet@36402
   995
  in aux [] 0 (1, 1) end
blanchet@36402
   996
blanchet@43750
   997
fun string_for_proof ctxt0 full_types i n =
blanchet@36402
   998
  let
blanchet@43626
   999
    val ctxt =
blanchet@43626
  1000
      ctxt0 |> Config.put show_free_types false
blanchet@43626
  1001
            |> Config.put show_types true
blanchet@43626
  1002
            |> Config.put show_sorts true
blanchet@37316
  1003
    fun fix_print_mode f x =
wenzelm@39393
  1004
      Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN)
wenzelm@39393
  1005
                               (print_mode_value ())) f x
blanchet@36402
  1006
    fun do_indent ind = replicate_string (ind * indent_size) " "
blanchet@36474
  1007
    fun do_free (s, T) =
blanchet@36474
  1008
      maybe_quote s ^ " :: " ^
blanchet@36474
  1009
      maybe_quote (fix_print_mode (Syntax.string_of_typ ctxt) T)
blanchet@36568
  1010
    fun do_label l = if l = no_label then "" else string_for_label l ^ ": "
blanchet@36402
  1011
    fun do_have qs =
blanchet@36402
  1012
      (if member (op =) qs Moreover then "moreover " else "") ^
blanchet@36402
  1013
      (if member (op =) qs Ultimately then "ultimately " else "") ^
blanchet@36402
  1014
      (if member (op =) qs Then then
blanchet@36402
  1015
         if member (op =) qs Show then "thus" else "hence"
blanchet@36402
  1016
       else
blanchet@36402
  1017
         if member (op =) qs Show then "show" else "have")
blanchet@36474
  1018
    val do_term = maybe_quote o fix_print_mode (Syntax.string_of_term ctxt)
blanchet@43874
  1019
    val reconstructor = if full_types then MetisFT else Metis
blanchet@36568
  1020
    fun do_facts (ls, ss) =
blanchet@43874
  1021
      reconstructor_command reconstructor 1 1
blanchet@43874
  1022
          (ls |> sort_distinct (prod_ord string_ord int_ord),
blanchet@43874
  1023
           ss |> sort_distinct string_ord)
blanchet@36474
  1024
    and do_step ind (Fix xs) =
blanchet@36474
  1025
        do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n"
blanchet@36482
  1026
      | do_step ind (Let (t1, t2)) =
blanchet@36482
  1027
        do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n"
blanchet@36402
  1028
      | do_step ind (Assume (l, t)) =
blanchet@36402
  1029
        do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n"
blanchet@36562
  1030
      | do_step ind (Have (qs, l, t, ByMetis facts)) =
blanchet@36402
  1031
        do_indent ind ^ do_have qs ^ " " ^
blanchet@36475
  1032
        do_label l ^ do_term t ^ " " ^ do_facts facts ^ "\n"
blanchet@36402
  1033
      | do_step ind (Have (qs, l, t, CaseSplit (proofs, facts))) =
blanchet@36402
  1034
        space_implode (do_indent ind ^ "moreover\n")
blanchet@36402
  1035
                      (map (do_block ind) proofs) ^
blanchet@36475
  1036
        do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ " " ^
blanchet@36474
  1037
        do_facts facts ^ "\n"
blanchet@36402
  1038
    and do_steps prefix suffix ind steps =
blanchet@36402
  1039
      let val s = implode (map (do_step ind) steps) in
blanchet@36402
  1040
        replicate_string (ind * indent_size - size prefix) " " ^ prefix ^
blanchet@36402
  1041
        String.extract (s, ind * indent_size,
blanchet@36402
  1042
                        SOME (size s - ind * indent_size - 1)) ^
blanchet@36402
  1043
        suffix ^ "\n"
blanchet@36402
  1044
      end
blanchet@36402
  1045
    and do_block ind proof = do_steps "{ " " }" (ind + 1) proof
blanchet@36562
  1046
    (* One-step proofs are pointless; better use the Metis one-liner
blanchet@36562
  1047
       directly. *)
blanchet@36562
  1048
    and do_proof [Have (_, _, _, ByMetis _)] = ""
blanchet@36562
  1049
      | do_proof proof =
blanchet@36476
  1050
        (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
blanchet@39692
  1051
        do_indent 0 ^ "proof -\n" ^ do_steps "" "" 1 proof ^ do_indent 0 ^
blanchet@39692
  1052
        (if n <> 1 then "next" else "qed")
blanchet@36484
  1053
  in do_proof end
blanchet@36402
  1054
blanchet@43809
  1055
fun isar_proof_text ctxt
blanchet@43874
  1056
        (debug, full_types, isar_shrink_factor, type_sys, pool,
blanchet@43874
  1057
         conjecture_shape, facts_offset, fact_names, sym_tab, atp_proof)
blanchet@43874
  1058
        (one_line_params as (_, _, _, _, goal, i)) =
blanchet@36402
  1059
  let
blanchet@43845
  1060
    val (params, hyp_ts, concl_t) = strip_subgoal ctxt goal i
blanchet@36901
  1061
    val frees = fold Term.add_frees (concl_t :: hyp_ts) []
blanchet@36960
  1062
    val tfrees = fold Term.add_tfrees (concl_t :: hyp_ts) []
blanchet@36402
  1063
    val n = Logic.count_prems (prop_of goal)
blanchet@43874
  1064
    val one_line_proof = one_line_proof_text one_line_params
blanchet@36283
  1065
    fun isar_proof_for () =
blanchet@43874
  1066
      case atp_proof
blanchet@43874
  1067
           |> isar_proof_from_atp_proof pool ctxt type_sys tfrees
blanchet@43874
  1068
                  isar_shrink_factor conjecture_shape facts_offset
blanchet@43874
  1069
                  fact_names sym_tab params frees
blanchet@39618
  1070
           |> redirect_proof hyp_ts concl_t
blanchet@36402
  1071
           |> kill_duplicate_assumptions_in_proof
blanchet@36402
  1072
           |> then_chain_proof
blanchet@36402
  1073
           |> kill_useless_labels_in_proof
blanchet@36402
  1074
           |> relabel_proof
blanchet@43750
  1075
           |> string_for_proof ctxt full_types i n of
blanchet@43425
  1076
        "" => "\nNo structured proof available (proof too short)."
blanchet@38822
  1077
      | proof => "\n\nStructured proof:\n" ^ Markup.markup Markup.sendback proof
blanchet@35868
  1078
    val isar_proof =
blanchet@36402
  1079
      if debug then
blanchet@36283
  1080
        isar_proof_for ()
blanchet@36283
  1081
      else
blanchet@36283
  1082
        try isar_proof_for ()
blanchet@38822
  1083
        |> the_default "\nWarning: The Isar proof construction failed."
blanchet@43874
  1084
  in one_line_proof ^ isar_proof end
immler@31038
  1085
blanchet@43874
  1086
fun proof_text ctxt isar_proof isar_params
blanchet@43874
  1087
               (one_line_params as (preplay, _, _, _, _, _)) =
blanchet@43874
  1088
  (if isar_proof orelse preplay = Preplay_Failed then
blanchet@43874
  1089
     isar_proof_text ctxt isar_params
blanchet@43874
  1090
   else
blanchet@43874
  1091
     one_line_proof_text) one_line_params
blanchet@36223
  1092
immler@31038
  1093
end;