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