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