src/HOL/Tools/Sledgehammer/sledgehammer_reconstruct.ML
author blanchet
Tue Sep 14 19:38:44 2010 +0200 (2010-09-14)
changeset 39370 f8292d3020db
parent 39368 f661064b2b80
child 39372 2fd8a9a7080d
permissions -rw-r--r--
use same hack as in "Async_Manager" to work around Proof General bug
blanchet@38988
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_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
wenzelm@33310
     6
Transfer of proofs from external provers.
wenzelm@33310
     7
*)
wenzelm@33310
     8
blanchet@38988
     9
signature SLEDGEHAMMER_RECONSTRUCT =
paulson@24425
    10
sig
blanchet@38988
    11
  type locality = Sledgehammer_Filter.locality
blanchet@36281
    12
  type minimize_command = string list -> string
blanchet@38818
    13
  type metis_params =
blanchet@39327
    14
    string * bool * minimize_command * string * (string * locality) list vector
blanchet@39327
    15
    * thm * int
blanchet@38818
    16
  type isar_params =
blanchet@38040
    17
    string Symtab.table * bool * int * Proof.context * int list list
blanchet@38818
    18
  type text_result = string * (string * locality) list
blanchet@38818
    19
blanchet@38818
    20
  val metis_proof_text : metis_params -> text_result
blanchet@38818
    21
  val isar_proof_text : isar_params -> metis_params -> text_result
blanchet@38818
    22
  val proof_text : bool -> isar_params -> metis_params -> text_result
paulson@24425
    23
end;
paulson@21978
    24
blanchet@38988
    25
structure Sledgehammer_Reconstruct : SLEDGEHAMMER_RECONSTRUCT =
paulson@21978
    26
struct
paulson@21978
    27
blanchet@38028
    28
open ATP_Problem
blanchet@37578
    29
open Metis_Clauses
blanchet@36478
    30
open Sledgehammer_Util
blanchet@38988
    31
open Sledgehammer_Filter
blanchet@38282
    32
open Sledgehammer_Translate
paulson@21978
    33
blanchet@36281
    34
type minimize_command = string list -> string
blanchet@38818
    35
type metis_params =
blanchet@39327
    36
  string * bool * minimize_command * string * (string * locality) list vector
blanchet@39327
    37
  * thm * int
blanchet@38818
    38
type isar_params =
blanchet@38818
    39
  string Symtab.table * bool * int * Proof.context * int list list
blanchet@38818
    40
type text_result = string * (string * locality) list
blanchet@36281
    41
blanchet@38014
    42
(* Simple simplifications to ensure that sort annotations don't leave a trail of
blanchet@38014
    43
   spurious "True"s. *)
blanchet@38014
    44
fun s_not @{const False} = @{const True}
blanchet@38014
    45
  | s_not @{const True} = @{const False}
blanchet@38014
    46
  | s_not (@{const Not} $ t) = t
blanchet@38014
    47
  | s_not t = @{const Not} $ t
blanchet@38014
    48
fun s_conj (@{const True}, t2) = t2
blanchet@38014
    49
  | s_conj (t1, @{const True}) = t1
blanchet@38014
    50
  | s_conj p = HOLogic.mk_conj p
blanchet@38014
    51
fun s_disj (@{const False}, t2) = t2
blanchet@38014
    52
  | s_disj (t1, @{const False}) = t1
blanchet@38014
    53
  | s_disj p = HOLogic.mk_disj p
blanchet@38014
    54
fun s_imp (@{const True}, t2) = t2
blanchet@38014
    55
  | s_imp (t1, @{const False}) = s_not t1
blanchet@38014
    56
  | s_imp p = HOLogic.mk_imp p
blanchet@38014
    57
fun s_iff (@{const True}, t2) = t2
blanchet@38014
    58
  | s_iff (t1, @{const True}) = t1
blanchet@38014
    59
  | s_iff (t1, t2) = HOLogic.eq_const HOLogic.boolT $ t1 $ t2
blanchet@38014
    60
blanchet@38014
    61
fun mk_anot (AConn (ANot, [phi])) = phi
blanchet@38014
    62
  | mk_anot phi = AConn (ANot, [phi])
blanchet@37991
    63
fun mk_aconn c (phi1, phi2) = AConn (c, [phi1, phi2])
blanchet@37991
    64
blanchet@39370
    65
datatype tstp_name = Str of string * string | Num of string
blanchet@39368
    66
blanchet@38066
    67
fun index_in_shape x = find_index (exists (curry (op =) x))
blanchet@39370
    68
fun resolve_axiom axiom_names (Str (_, str)) =
blanchet@39368
    69
    (case find_first_in_list_vector axiom_names str of
blanchet@39368
    70
       SOME x => [(str, x)]
blanchet@39368
    71
     | NONE => [])
blanchet@39368
    72
  | resolve_axiom axiom_names (Num num) =
blanchet@39370
    73
    case Int.fromString num of
blanchet@39370
    74
      SOME j =>
blanchet@39370
    75
      if j > 0 andalso j <= Vector.length axiom_names then
blanchet@39370
    76
        Vector.sub (axiom_names, j - 1)
blanchet@39370
    77
      else
blanchet@39370
    78
        []
blanchet@39370
    79
    | NONE => []
blanchet@39370
    80
val is_axiom = not o null oo resolve_axiom
blanchet@39370
    81
blanchet@39370
    82
fun resolve_conjecture conjecture_shape (Str (num, s)) =
blanchet@39370
    83
    let
blanchet@39370
    84
      val j = try (unprefix conjecture_prefix) s |> the_default num
blanchet@39370
    85
              |> Int.fromString |> the_default ~1
blanchet@39370
    86
      val k = index_in_shape j conjecture_shape
blanchet@39370
    87
    in if k >= 0 then [k] else [] end
blanchet@39370
    88
  | resolve_conjecture conjecture_shape (Num num) =
blanchet@39370
    89
    resolve_conjecture conjecture_shape (Str (num, "")) (* HACK *)
blanchet@39370
    90
val is_conjecture = not o null oo resolve_conjecture
blanchet@36291
    91
blanchet@37991
    92
fun negate_term (Const (@{const_name All}, T) $ Abs (s, T', t')) =
blanchet@37991
    93
    Const (@{const_name Ex}, T) $ Abs (s, T', negate_term t')
blanchet@37991
    94
  | negate_term (Const (@{const_name Ex}, T) $ Abs (s, T', t')) =
blanchet@37991
    95
    Const (@{const_name All}, T) $ Abs (s, T', negate_term t')
haftmann@38786
    96
  | negate_term (@{const HOL.implies} $ t1 $ t2) =
haftmann@38795
    97
    @{const HOL.conj} $ t1 $ negate_term t2
haftmann@38795
    98
  | negate_term (@{const HOL.conj} $ t1 $ t2) =
haftmann@38795
    99
    @{const HOL.disj} $ negate_term t1 $ negate_term t2
haftmann@38795
   100
  | negate_term (@{const HOL.disj} $ t1 $ t2) =
haftmann@38795
   101
    @{const HOL.conj} $ negate_term t1 $ negate_term t2
blanchet@37991
   102
  | negate_term (@{const Not} $ t) = t
blanchet@37991
   103
  | negate_term t = @{const Not} $ t
blanchet@37991
   104
blanchet@39368
   105
datatype ('a, 'b, 'c) raw_step =
blanchet@39368
   106
  Definition of tstp_name * 'a * 'b |
blanchet@39368
   107
  Inference of tstp_name * 'c * tstp_name list
blanchet@36491
   108
blanchet@38035
   109
(**** PARSING OF TSTP FORMAT ****)
paulson@21978
   110
paulson@21978
   111
(*Strings enclosed in single quotes, e.g. filenames*)
blanchet@39370
   112
val scan_general_id =
blanchet@39370
   113
  $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode
blanchet@39370
   114
  || Scan.repeat ($$ "$") -- Scan.many1 Symbol.is_letdig
blanchet@39370
   115
     >> (fn (ss1, ss2) => implode ss1 ^ implode ss2)
blanchet@36548
   116
blanchet@36548
   117
fun repair_name _ "$true" = "c_True"
blanchet@36548
   118
  | repair_name _ "$false" = "c_False"
blanchet@38007
   119
  | repair_name _ "$$e" = "c_equal" (* seen in Vampire proofs *)
blanchet@38035
   120
  | repair_name _ "equal" = "c_equal" (* needed by SPASS? *)
blanchet@38035
   121
  | repair_name pool s =
blanchet@38035
   122
    case Symtab.lookup pool s of
blanchet@38035
   123
      SOME s' => s'
blanchet@38035
   124
    | NONE =>
blanchet@38035
   125
      if String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s then
blanchet@38035
   126
        "c_equal" (* seen in Vampire proofs *)
blanchet@38035
   127
      else
blanchet@38035
   128
        s
blanchet@36392
   129
(* Generalized first-order terms, which include file names, numbers, etc. *)
blanchet@38035
   130
fun parse_annotation x =
blanchet@39370
   131
  ((scan_general_id ::: Scan.repeat ($$ " " |-- scan_general_id))
blanchet@39370
   132
   -- Scan.optional parse_annotation [] >> uncurry (union (op =))
blanchet@38035
   133
   || $$ "(" |-- parse_annotations --| $$ ")"
blanchet@38035
   134
   || $$ "[" |-- parse_annotations --| $$ "]") x
blanchet@38035
   135
and parse_annotations x =
blanchet@38036
   136
  (Scan.optional (parse_annotation
blanchet@38036
   137
                  ::: Scan.repeat ($$ "," |-- parse_annotation)) []
blanchet@38035
   138
   >> (fn numss => fold (union (op =)) numss [])) x
blanchet@38035
   139
blanchet@38035
   140
(* Vampire proof lines sometimes contain needless information such as "(0:3)",
blanchet@38035
   141
   which can be hard to disambiguate from function application in an LL(1)
blanchet@38035
   142
   parser. As a workaround, we extend the TPTP term syntax with such detritus
blanchet@38035
   143
   and ignore it. *)
blanchet@38066
   144
fun parse_vampire_detritus x =
blanchet@39370
   145
  (scan_general_id |-- $$ ":" --| scan_general_id >> K []) x
blanchet@38035
   146
blanchet@36393
   147
fun parse_term pool x =
blanchet@39370
   148
  ((scan_general_id >> repair_name pool)
blanchet@38035
   149
    -- Scan.optional ($$ "(" |-- (parse_vampire_detritus || parse_terms pool)
blanchet@38035
   150
                      --| $$ ")") []
blanchet@38035
   151
    --| Scan.optional ($$ "(" |-- parse_vampire_detritus --| $$ ")") []
blanchet@38035
   152
   >> ATerm) x
blanchet@36393
   153
and parse_terms pool x =
blanchet@36393
   154
  (parse_term pool ::: Scan.repeat ($$ "," |-- parse_term pool)) x
paulson@21978
   155
blanchet@38034
   156
fun parse_atom pool =
blanchet@36393
   157
  parse_term pool -- Scan.option (Scan.option ($$ "!") --| $$ "="
blanchet@36393
   158
                                  -- parse_term pool)
blanchet@38035
   159
  >> (fn (u1, NONE) => AAtom u1
blanchet@38034
   160
       | (u1, SOME (NONE, u2)) => AAtom (ATerm ("c_equal", [u1, u2]))
blanchet@37991
   161
       | (u1, SOME (SOME _, u2)) =>
blanchet@38034
   162
         mk_anot (AAtom (ATerm ("c_equal", [u1, u2]))))
blanchet@37991
   163
blanchet@37991
   164
fun fo_term_head (ATerm (s, _)) = s
blanchet@36291
   165
blanchet@37991
   166
(* TPTP formulas are fully parenthesized, so we don't need to worry about
blanchet@37991
   167
   operator precedence. *)
blanchet@37991
   168
fun parse_formula pool x =
blanchet@37991
   169
  (($$ "(" |-- parse_formula pool --| $$ ")"
blanchet@37991
   170
    || ($$ "!" >> K AForall || $$ "?" >> K AExists)
blanchet@37991
   171
       --| $$ "[" -- parse_terms pool --| $$ "]" --| $$ ":"
blanchet@37991
   172
       -- parse_formula pool
blanchet@37991
   173
       >> (fn ((q, ts), phi) => AQuant (q, map fo_term_head ts, phi))
blanchet@37991
   174
    || $$ "~" |-- parse_formula pool >> mk_anot
blanchet@38034
   175
    || parse_atom pool)
blanchet@37991
   176
   -- Scan.option ((Scan.this_string "=>" >> K AImplies
blanchet@37991
   177
                    || Scan.this_string "<=>" >> K AIff
blanchet@37991
   178
                    || Scan.this_string "<~>" >> K ANotIff
blanchet@37991
   179
                    || Scan.this_string "<=" >> K AIf
blanchet@37991
   180
                    || $$ "|" >> K AOr || $$ "&" >> K AAnd)
blanchet@37991
   181
                   -- parse_formula pool)
blanchet@37991
   182
   >> (fn (phi1, NONE) => phi1
blanchet@37991
   183
        | (phi1, SOME (c, phi2)) => mk_aconn c (phi1, phi2))) x
blanchet@37991
   184
blanchet@38035
   185
val parse_tstp_extra_arguments =
blanchet@38035
   186
  Scan.optional ($$ "," |-- parse_annotation
blanchet@38035
   187
                 --| Scan.option ($$ "," |-- parse_annotations)) []
blanchet@36486
   188
blanchet@38035
   189
(* Syntax: (fof|cnf)\(<num>, <formula_role>, <formula> <extra_arguments>\).
blanchet@36486
   190
   The <num> could be an identifier, but we assume integers. *)
blanchet@37991
   191
 fun parse_tstp_line pool =
blanchet@37991
   192
   ((Scan.this_string "fof" || Scan.this_string "cnf") -- $$ "(")
blanchet@39370
   193
     |-- scan_general_id --| $$ "," -- Symbol.scan_id --| $$ ","
blanchet@38035
   194
     -- parse_formula pool -- parse_tstp_extra_arguments --| $$ ")" --| $$ "."
blanchet@37991
   195
    >> (fn (((num, role), phi), deps) =>
blanchet@37991
   196
           case role of
blanchet@37991
   197
             "definition" =>
blanchet@37991
   198
             (case phi of
blanchet@38034
   199
                AConn (AIff, [phi1 as AAtom _, phi2]) =>
blanchet@39368
   200
                Definition (Num num, phi1, phi2)
blanchet@38036
   201
              | AAtom (ATerm ("c_equal", _)) =>
blanchet@39368
   202
                (* Vampire's equality proxy axiom *)
blanchet@39368
   203
                Inference (Num num, phi, map Num deps)
blanchet@37991
   204
              | _ => raise Fail "malformed definition")
blanchet@39368
   205
           | _ => Inference (Num num, phi, map Num deps))
blanchet@36291
   206
blanchet@38035
   207
(**** PARSING OF VAMPIRE OUTPUT ****)
blanchet@38035
   208
blanchet@38035
   209
(* Syntax: <num>. <formula> <annotation> *)
blanchet@38035
   210
fun parse_vampire_line pool =
blanchet@39370
   211
  scan_general_id --| $$ "." -- parse_formula pool -- parse_annotation
blanchet@39368
   212
  >> (fn ((num, phi), deps) => Inference (Num num, phi, map Num deps))
blanchet@38035
   213
blanchet@36291
   214
(**** PARSING OF SPASS OUTPUT ****)
blanchet@36291
   215
blanchet@36392
   216
(* SPASS returns clause references of the form "x.y". We ignore "y", whose role
blanchet@36392
   217
   is not clear anyway. *)
blanchet@39370
   218
val parse_dot_name = scan_general_id --| $$ "." --| scan_general_id
paulson@21978
   219
blanchet@36392
   220
val parse_spass_annotations =
blanchet@36392
   221
  Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
blanchet@36392
   222
                                         --| Scan.option ($$ ","))) []
blanchet@36291
   223
blanchet@36574
   224
(* It is not clear why some literals are followed by sequences of stars and/or
blanchet@36574
   225
   pluses. We ignore them. *)
blanchet@38034
   226
fun parse_decorated_atom pool =
blanchet@38034
   227
  parse_atom pool --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")
blanchet@36291
   228
blanchet@38034
   229
fun mk_horn ([], []) = AAtom (ATerm ("c_False", []))
blanchet@37991
   230
  | mk_horn ([], pos_lits) = foldr1 (mk_aconn AOr) pos_lits
blanchet@37991
   231
  | mk_horn (neg_lits, []) = mk_anot (foldr1 (mk_aconn AAnd) neg_lits)
blanchet@37991
   232
  | mk_horn (neg_lits, pos_lits) =
blanchet@37991
   233
    mk_aconn AImplies (foldr1 (mk_aconn AAnd) neg_lits,
blanchet@37991
   234
                       foldr1 (mk_aconn AOr) pos_lits)
blanchet@37991
   235
blanchet@36393
   236
fun parse_horn_clause pool =
blanchet@38034
   237
  Scan.repeat (parse_decorated_atom pool) --| $$ "|" --| $$ "|"
blanchet@38034
   238
    -- Scan.repeat (parse_decorated_atom pool) --| $$ "-" --| $$ ">"
blanchet@38034
   239
    -- Scan.repeat (parse_decorated_atom pool)
blanchet@37991
   240
  >> (mk_horn o apfst (op @))
paulson@21978
   241
blanchet@36558
   242
(* Syntax: <num>[0:<inference><annotations>]
blanchet@38034
   243
   <atoms> || <atoms> -> <atoms>. *)
blanchet@36402
   244
fun parse_spass_line pool =
blanchet@39370
   245
  scan_general_id --| $$ "[" --| $$ "0" --| $$ ":" --| Symbol.scan_id
blanchet@38035
   246
    -- parse_spass_annotations --| $$ "]" -- parse_horn_clause pool --| $$ "."
blanchet@39368
   247
  >> (fn ((num, deps), u) => Inference (Num num, u, map Num deps))
blanchet@36291
   248
blanchet@38035
   249
fun parse_line pool =
blanchet@38035
   250
  parse_tstp_line pool || parse_vampire_line pool || parse_spass_line pool
blanchet@36548
   251
fun parse_lines pool = Scan.repeat1 (parse_line pool)
blanchet@36548
   252
fun parse_proof pool =
blanchet@36548
   253
  fst o Scan.finite Symbol.stopper
blanchet@36548
   254
            (Scan.error (!! (fn _ => raise Fail "unrecognized ATP output")
blanchet@36548
   255
                            (parse_lines pool)))
blanchet@38738
   256
  o explode o strip_spaces_except_between_ident_chars
paulson@21978
   257
paulson@21978
   258
(**** INTERPRETATION OF TSTP SYNTAX TREES ****)
paulson@21978
   259
blanchet@37991
   260
exception FO_TERM of string fo_term list
blanchet@37994
   261
exception FORMULA of (string, string fo_term) formula list
blanchet@37991
   262
exception SAME of unit
paulson@21978
   263
blanchet@36909
   264
(* Type variables are given the basic sort "HOL.type". Some will later be
blanchet@37991
   265
   constrained by information from type literals, or by type inference. *)
blanchet@37991
   266
fun type_from_fo_term tfrees (u as ATerm (a, us)) =
blanchet@37991
   267
  let val Ts = map (type_from_fo_term tfrees) us in
blanchet@38748
   268
    case strip_prefix_and_unascii type_const_prefix a of
blanchet@37991
   269
      SOME b => Type (invert_const b, Ts)
blanchet@37991
   270
    | NONE =>
blanchet@37991
   271
      if not (null us) then
blanchet@37991
   272
        raise FO_TERM [u]  (* only "tconst"s have type arguments *)
blanchet@38748
   273
      else case strip_prefix_and_unascii tfree_prefix a of
blanchet@37991
   274
        SOME b =>
blanchet@37991
   275
        let val s = "'" ^ b in
blanchet@37991
   276
          TFree (s, AList.lookup (op =) tfrees s |> the_default HOLogic.typeS)
blanchet@37991
   277
        end
blanchet@36486
   278
      | NONE =>
blanchet@38748
   279
        case strip_prefix_and_unascii tvar_prefix a of
blanchet@37991
   280
          SOME b => TVar (("'" ^ b, 0), HOLogic.typeS)
blanchet@36486
   281
        | NONE =>
blanchet@37991
   282
          (* Variable from the ATP, say "X1" *)
blanchet@37991
   283
          Type_Infer.param 0 (a, HOLogic.typeS)
blanchet@37991
   284
  end
paulson@21978
   285
blanchet@38014
   286
(* Type class literal applied to a type. Returns triple of polarity, class,
blanchet@38014
   287
   type. *)
blanchet@38014
   288
fun type_constraint_from_term pos tfrees (u as ATerm (a, us)) =
blanchet@38748
   289
  case (strip_prefix_and_unascii class_prefix a,
blanchet@38014
   290
        map (type_from_fo_term tfrees) us) of
blanchet@38014
   291
    (SOME b, [T]) => (pos, b, T)
blanchet@38014
   292
  | _ => raise FO_TERM [u]
blanchet@38014
   293
blanchet@38085
   294
(** Accumulate type constraints in a formula: negative type literals **)
blanchet@38014
   295
fun add_var (key, z)  = Vartab.map_default (key, []) (cons z)
blanchet@38014
   296
fun add_type_constraint (false, cl, TFree (a ,_)) = add_var ((a, ~1), cl)
blanchet@38014
   297
  | add_type_constraint (false, cl, TVar (ix, _)) = add_var (ix, cl)
blanchet@38014
   298
  | add_type_constraint _ = I
blanchet@38014
   299
blanchet@38490
   300
fun repair_atp_variable_name f s =
blanchet@36486
   301
  let
blanchet@36486
   302
    fun subscript_name s n = s ^ nat_subscript n
blanchet@38488
   303
    val s = String.map f s
blanchet@36486
   304
  in
blanchet@36486
   305
    case space_explode "_" s of
blanchet@36486
   306
      [_] => (case take_suffix Char.isDigit (String.explode s) of
blanchet@36486
   307
                (cs1 as _ :: _, cs2 as _ :: _) =>
blanchet@36486
   308
                subscript_name (String.implode cs1)
blanchet@36486
   309
                               (the (Int.fromString (String.implode cs2)))
blanchet@36486
   310
              | (_, _) => s)
blanchet@36486
   311
    | [s1, s2] => (case Int.fromString s2 of
blanchet@36486
   312
                     SOME n => subscript_name s1 n
blanchet@36486
   313
                   | NONE => s)
blanchet@36486
   314
    | _ => s
blanchet@36486
   315
  end
blanchet@36486
   316
blanchet@36909
   317
(* First-order translation. No types are known for variables. "HOLogic.typeT"
blanchet@38014
   318
   should allow them to be inferred. *)
blanchet@38014
   319
fun raw_term_from_pred thy full_types tfrees =
blanchet@36909
   320
  let
blanchet@37643
   321
    fun aux opt_T extra_us u =
blanchet@36909
   322
      case u of
blanchet@37991
   323
        ATerm ("hBOOL", [u1]) => aux (SOME @{typ bool}) [] u1
blanchet@37991
   324
      | ATerm ("hAPP", [u1, u2]) => aux opt_T (u2 :: extra_us) u1
blanchet@37991
   325
      | ATerm (a, us) =>
blanchet@36909
   326
        if a = type_wrapper_name then
blanchet@36909
   327
          case us of
blanchet@37643
   328
            [typ_u, term_u] =>
blanchet@37991
   329
            aux (SOME (type_from_fo_term tfrees typ_u)) extra_us term_u
blanchet@37991
   330
          | _ => raise FO_TERM us
blanchet@38748
   331
        else case strip_prefix_and_unascii const_prefix a of
blanchet@36909
   332
          SOME "equal" =>
blanchet@39106
   333
          let val ts = map (aux NONE []) us in
blanchet@39106
   334
            if length ts = 2 andalso hd ts aconv List.last ts then
blanchet@39106
   335
              (* Vampire is keen on producing these. *)
blanchet@39106
   336
              @{const True}
blanchet@39106
   337
            else
blanchet@39106
   338
              list_comb (Const (@{const_name HOL.eq}, HOLogic.typeT), ts)
blanchet@39106
   339
          end
blanchet@36909
   340
        | SOME b =>
blanchet@36909
   341
          let
blanchet@36909
   342
            val c = invert_const b
blanchet@36909
   343
            val num_type_args = num_type_args thy c
blanchet@37643
   344
            val (type_us, term_us) =
blanchet@37643
   345
              chop (if full_types then 0 else num_type_args) us
blanchet@37643
   346
            (* Extra args from "hAPP" come after any arguments given directly to
blanchet@37643
   347
               the constant. *)
blanchet@37643
   348
            val term_ts = map (aux NONE []) term_us
blanchet@37643
   349
            val extra_ts = map (aux NONE []) extra_us
blanchet@36909
   350
            val t =
blanchet@36909
   351
              Const (c, if full_types then
blanchet@36909
   352
                          case opt_T of
blanchet@37643
   353
                            SOME T => map fastype_of term_ts ---> T
blanchet@36909
   354
                          | NONE =>
blanchet@36909
   355
                            if num_type_args = 0 then
blanchet@36909
   356
                              Sign.const_instance thy (c, [])
blanchet@36909
   357
                            else
blanchet@36909
   358
                              raise Fail ("no type information for " ^ quote c)
blanchet@36909
   359
                        else
blanchet@37998
   360
                          Sign.const_instance thy (c,
blanchet@37998
   361
                              map (type_from_fo_term tfrees) type_us))
blanchet@37643
   362
          in list_comb (t, term_ts @ extra_ts) end
blanchet@36909
   363
        | NONE => (* a free or schematic variable *)
blanchet@36909
   364
          let
blanchet@37643
   365
            val ts = map (aux NONE []) (us @ extra_us)
blanchet@36909
   366
            val T = map fastype_of ts ---> HOLogic.typeT
blanchet@36909
   367
            val t =
blanchet@38748
   368
              case strip_prefix_and_unascii fixed_var_prefix a of
blanchet@36909
   369
                SOME b => Free (b, T)
blanchet@36909
   370
              | NONE =>
blanchet@38748
   371
                case strip_prefix_and_unascii schematic_var_prefix a of
blanchet@36967
   372
                  SOME b => Var ((b, 0), T)
blanchet@36909
   373
                | NONE =>
blanchet@38017
   374
                  if is_tptp_variable a then
blanchet@38490
   375
                    Var ((repair_atp_variable_name Char.toLower a, 0), T)
blanchet@38017
   376
                  else
blanchet@38488
   377
                    (* Skolem constants? *)
blanchet@38490
   378
                    Var ((repair_atp_variable_name Char.toUpper a, 0), T)
blanchet@36909
   379
          in list_comb (t, ts) end
blanchet@38014
   380
  in aux (SOME HOLogic.boolT) [] end
paulson@21978
   381
blanchet@38014
   382
fun term_from_pred thy full_types tfrees pos (u as ATerm (s, _)) =
blanchet@38014
   383
  if String.isPrefix class_prefix s then
blanchet@38014
   384
    add_type_constraint (type_constraint_from_term pos tfrees u)
blanchet@38014
   385
    #> pair @{const True}
blanchet@38014
   386
  else
blanchet@38014
   387
    pair (raw_term_from_pred thy full_types tfrees u)
blanchet@36402
   388
blanchet@36555
   389
val combinator_table =
blanchet@36555
   390
  [(@{const_name COMBI}, @{thm COMBI_def_raw}),
blanchet@36555
   391
   (@{const_name COMBK}, @{thm COMBK_def_raw}),
blanchet@36555
   392
   (@{const_name COMBB}, @{thm COMBB_def_raw}),
blanchet@36555
   393
   (@{const_name COMBC}, @{thm COMBC_def_raw}),
blanchet@36555
   394
   (@{const_name COMBS}, @{thm COMBS_def_raw})]
blanchet@36555
   395
blanchet@36555
   396
fun uncombine_term (t1 $ t2) = betapply (pairself uncombine_term (t1, t2))
blanchet@36555
   397
  | uncombine_term (Abs (s, T, t')) = Abs (s, T, uncombine_term t')
blanchet@36555
   398
  | uncombine_term (t as Const (x as (s, _))) =
blanchet@36555
   399
    (case AList.lookup (op =) combinator_table s of
blanchet@36555
   400
       SOME thm => thm |> prop_of |> specialize_type @{theory} x |> Logic.dest_equals |> snd
blanchet@36555
   401
     | NONE => t)
blanchet@36555
   402
  | uncombine_term t = t
blanchet@36555
   403
blanchet@37991
   404
(* Update schematic type variables with detected sort constraints. It's not
blanchet@37991
   405
   totally clear when this code is necessary. *)
blanchet@38014
   406
fun repair_tvar_sorts (t, tvar_tab) =
blanchet@36909
   407
  let
blanchet@37991
   408
    fun do_type (Type (a, Ts)) = Type (a, map do_type Ts)
blanchet@37991
   409
      | do_type (TVar (xi, s)) =
blanchet@37991
   410
        TVar (xi, the_default s (Vartab.lookup tvar_tab xi))
blanchet@37991
   411
      | do_type (TFree z) = TFree z
blanchet@37991
   412
    fun do_term (Const (a, T)) = Const (a, do_type T)
blanchet@37991
   413
      | do_term (Free (a, T)) = Free (a, do_type T)
blanchet@37991
   414
      | do_term (Var (xi, T)) = Var (xi, do_type T)
blanchet@37991
   415
      | do_term (t as Bound _) = t
blanchet@37991
   416
      | do_term (Abs (a, T, t)) = Abs (a, do_type T, do_term t)
blanchet@37991
   417
      | do_term (t1 $ t2) = do_term t1 $ do_term t2
blanchet@37991
   418
  in t |> not (Vartab.is_empty tvar_tab) ? do_term end
blanchet@37991
   419
blanchet@39370
   420
(* ### TODO: looks broken; see forall_of below *)
blanchet@37991
   421
fun quantify_over_free quant_s free_s body_t =
blanchet@37991
   422
  case Term.add_frees body_t [] |> filter (curry (op =) free_s o fst) of
blanchet@37991
   423
    [] => body_t
blanchet@37991
   424
  | frees as (_, free_T) :: _ =>
blanchet@37991
   425
    Abs (free_s, free_T, fold (curry abstract_over) (map Free frees) body_t)
blanchet@37991
   426
blanchet@38085
   427
(* Interpret an ATP formula as a HOL term, extracting sort constraints as they
blanchet@38085
   428
   appear in the formula. *)
blanchet@38014
   429
fun prop_from_formula thy full_types tfrees phi =
blanchet@38014
   430
  let
blanchet@38014
   431
    fun do_formula pos phi =
blanchet@37991
   432
      case phi of
blanchet@38014
   433
        AQuant (_, [], phi) => do_formula pos phi
blanchet@37991
   434
      | AQuant (q, x :: xs, phi') =>
blanchet@38014
   435
        do_formula pos (AQuant (q, xs, phi'))
blanchet@38014
   436
        #>> quantify_over_free (case q of
blanchet@38014
   437
                                  AForall => @{const_name All}
blanchet@38490
   438
                                | AExists => @{const_name Ex})
blanchet@38490
   439
                               (repair_atp_variable_name Char.toLower x)
blanchet@38014
   440
      | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not
blanchet@37991
   441
      | AConn (c, [phi1, phi2]) =>
blanchet@38014
   442
        do_formula (pos |> c = AImplies ? not) phi1
blanchet@38014
   443
        ##>> do_formula pos phi2
blanchet@38014
   444
        #>> (case c of
blanchet@38014
   445
               AAnd => s_conj
blanchet@38014
   446
             | AOr => s_disj
blanchet@38014
   447
             | AImplies => s_imp
blanchet@38038
   448
             | AIf => s_imp o swap
blanchet@38038
   449
             | AIff => s_iff
blanchet@38038
   450
             | ANotIff => s_not o s_iff)
blanchet@38034
   451
      | AAtom tm => term_from_pred thy full_types tfrees pos tm
blanchet@37991
   452
      | _ => raise FORMULA [phi]
blanchet@38014
   453
  in repair_tvar_sorts (do_formula true phi Vartab.empty) end
blanchet@37991
   454
blanchet@36556
   455
fun check_formula ctxt =
wenzelm@39288
   456
  Type.constraint HOLogic.boolT
blanchet@36486
   457
  #> Syntax.check_term (ProofContext.set_mode ProofContext.mode_schematic ctxt)
paulson@21978
   458
paulson@21978
   459
paulson@21978
   460
(**** Translation of TSTP files to Isar Proofs ****)
paulson@21978
   461
blanchet@36486
   462
fun unvarify_term (Var ((s, 0), T)) = Free (s, T)
blanchet@36486
   463
  | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t])
paulson@21978
   464
blanchet@39368
   465
fun decode_line full_types tfrees (Definition (name, phi1, phi2)) ctxt =
blanchet@36486
   466
    let
blanchet@37991
   467
      val thy = ProofContext.theory_of ctxt
blanchet@37991
   468
      val t1 = prop_from_formula thy full_types tfrees phi1
blanchet@36551
   469
      val vars = snd (strip_comb t1)
blanchet@36486
   470
      val frees = map unvarify_term vars
blanchet@36486
   471
      val unvarify_args = subst_atomic (vars ~~ frees)
blanchet@37991
   472
      val t2 = prop_from_formula thy full_types tfrees phi2
blanchet@36551
   473
      val (t1, t2) =
blanchet@36551
   474
        HOLogic.eq_const HOLogic.typeT $ t1 $ t2
blanchet@36556
   475
        |> unvarify_args |> uncombine_term |> check_formula ctxt
blanchet@36555
   476
        |> HOLogic.dest_eq
blanchet@36486
   477
    in
blanchet@39368
   478
      (Definition (name, t1, t2),
blanchet@36551
   479
       fold Variable.declare_term (maps OldTerm.term_frees [t1, t2]) ctxt)
blanchet@36486
   480
    end
blanchet@39368
   481
  | decode_line full_types tfrees (Inference (name, u, deps)) ctxt =
blanchet@36551
   482
    let
blanchet@37991
   483
      val thy = ProofContext.theory_of ctxt
blanchet@37991
   484
      val t = u |> prop_from_formula thy full_types tfrees
blanchet@37998
   485
                |> uncombine_term |> check_formula ctxt
blanchet@36551
   486
    in
blanchet@39368
   487
      (Inference (name, t, deps),
blanchet@36551
   488
       fold Variable.declare_term (OldTerm.term_frees t) ctxt)
blanchet@36486
   489
    end
blanchet@36967
   490
fun decode_lines ctxt full_types tfrees lines =
blanchet@36967
   491
  fst (fold_map (decode_line full_types tfrees) lines ctxt)
paulson@21978
   492
blanchet@38035
   493
fun is_same_inference _ (Definition _) = false
blanchet@38035
   494
  | is_same_inference t (Inference (_, t', _)) = t aconv t'
blanchet@36486
   495
blanchet@36486
   496
(* No "real" literals means only type information (tfree_tcs, clsrel, or
blanchet@36486
   497
   clsarity). *)
blanchet@36486
   498
val is_only_type_information = curry (op aconv) HOLogic.true_const
blanchet@36486
   499
blanchet@36486
   500
fun replace_one_dep (old, new) dep = if dep = old then new else [dep]
blanchet@36486
   501
fun replace_deps_in_line _ (line as Definition _) = line
blanchet@39368
   502
  | replace_deps_in_line p (Inference (name, t, deps)) =
blanchet@39368
   503
    Inference (name, t, fold (union (op =) o replace_one_dep p) deps [])
paulson@21978
   504
blanchet@38085
   505
(* Discard axioms; consolidate adjacent lines that prove the same formula, since
blanchet@38085
   506
   they differ only in type information.*)
blanchet@36551
   507
fun add_line _ _ (line as Definition _) lines = line :: lines
blanchet@39368
   508
  | add_line conjecture_shape axiom_names (Inference (name, t, [])) lines =
blanchet@38085
   509
    (* No dependencies: axiom, conjecture, or (for Vampire) internal axioms or
blanchet@38085
   510
       definitions. *)
blanchet@39370
   511
    if is_axiom axiom_names name then
blanchet@36486
   512
      (* Axioms are not proof lines. *)
blanchet@36486
   513
      if is_only_type_information t then
blanchet@39368
   514
        map (replace_deps_in_line (name, [])) lines
blanchet@36486
   515
      (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@38035
   516
      else case take_prefix (not o is_same_inference t) lines of
blanchet@36486
   517
        (_, []) => lines (*no repetition of proof line*)
blanchet@39368
   518
      | (pre, Inference (name', _, _) :: post) =>
blanchet@39368
   519
        pre @ map (replace_deps_in_line (name', [name])) post
blanchet@39370
   520
    else if is_conjecture conjecture_shape name then
blanchet@39368
   521
      Inference (name, negate_term t, []) :: lines
blanchet@36551
   522
    else
blanchet@39368
   523
      map (replace_deps_in_line (name, [])) lines
blanchet@39368
   524
  | add_line _ _ (Inference (name, t, deps)) lines =
blanchet@36486
   525
    (* Type information will be deleted later; skip repetition test. *)
blanchet@36486
   526
    if is_only_type_information t then
blanchet@39368
   527
      Inference (name, t, deps) :: lines
blanchet@36486
   528
    (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@38035
   529
    else case take_prefix (not o is_same_inference t) lines of
blanchet@36486
   530
      (* FIXME: Doesn't this code risk conflating proofs involving different
blanchet@38035
   531
         types? *)
blanchet@39368
   532
       (_, []) => Inference (name, t, deps) :: lines
blanchet@39368
   533
     | (pre, Inference (name', t', _) :: post) =>
blanchet@39368
   534
       Inference (name, t', deps) ::
blanchet@39368
   535
       pre @ map (replace_deps_in_line (name', [name])) post
paulson@22044
   536
blanchet@36486
   537
(* Recursively delete empty lines (type information) from the proof. *)
blanchet@39368
   538
fun add_nontrivial_line (Inference (name, t, [])) lines =
blanchet@39368
   539
    if is_only_type_information t then delete_dep name lines
blanchet@39368
   540
    else Inference (name, t, []) :: lines
blanchet@36486
   541
  | add_nontrivial_line line lines = line :: lines
blanchet@39368
   542
and delete_dep name lines =
blanchet@39368
   543
  fold_rev add_nontrivial_line (map (replace_deps_in_line (name, [])) lines) []
blanchet@36486
   544
blanchet@37323
   545
(* ATPs sometimes reuse free variable names in the strangest ways. Removing
blanchet@37323
   546
   offending lines often does the trick. *)
blanchet@36560
   547
fun is_bad_free frees (Free x) = not (member (op =) frees x)
blanchet@36560
   548
  | is_bad_free _ _ = false
paulson@22470
   549
blanchet@39368
   550
fun add_desired_line _ _ _ _ (line as Definition (name, _, _)) (j, lines) =
blanchet@39368
   551
    (j, line :: map (replace_deps_in_line (name, [])) lines)
blanchet@38282
   552
  | add_desired_line isar_shrink_factor conjecture_shape axiom_names frees
blanchet@39368
   553
                     (Inference (name, t, deps)) (j, lines) =
blanchet@36402
   554
    (j + 1,
blanchet@39370
   555
     if is_axiom axiom_names name orelse
blanchet@39370
   556
        is_conjecture conjecture_shape name orelse
blanchet@36570
   557
        (not (is_only_type_information t) andalso
blanchet@36570
   558
         null (Term.add_tvars t []) andalso
blanchet@36570
   559
         not (exists_subterm (is_bad_free frees) t) andalso
blanchet@36570
   560
         (null lines orelse (* last line must be kept *)
blanchet@36924
   561
          (length deps >= 2 andalso j mod isar_shrink_factor = 0))) then
blanchet@39368
   562
       Inference (name, t, deps) :: lines  (* keep line *)
blanchet@36402
   563
     else
blanchet@39368
   564
       map (replace_deps_in_line (name, deps)) lines)  (* drop line *)
paulson@21978
   565
blanchet@36402
   566
(** EXTRACTING LEMMAS **)
paulson@21979
   567
blanchet@38599
   568
(* Like "split_line", but ignores "\n" that follow a comma (as in SNARK's
blanchet@38599
   569
   output). *)
blanchet@38599
   570
val split_proof_lines =
blanchet@38599
   571
  let
blanchet@38599
   572
    fun aux [] [] = []
blanchet@38599
   573
      | aux line [] = [implode (rev line)]
blanchet@38599
   574
      | aux line ("," :: "\n" :: rest) = aux ("," :: line) rest
blanchet@38599
   575
      | aux line ("\n" :: rest) = aux line [] @ aux [] rest
blanchet@38599
   576
      | aux line (s :: rest) = aux (s :: line) rest
blanchet@38599
   577
  in aux [] o explode end
blanchet@38599
   578
blanchet@37991
   579
(* A list consisting of the first number in each line is returned. For TSTP,
blanchet@37991
   580
   interesting lines have the form "fof(108, axiom, ...)", where the number
blanchet@37991
   581
   (108) is extracted. For SPASS, lines have the form "108[0:Inp] ...", where
blanchet@38033
   582
   the first number (108) is extracted. For Vampire, we look for
blanchet@38033
   583
   "108. ... [input]". *)
blanchet@38282
   584
fun used_facts_in_atp_proof axiom_names atp_proof =
blanchet@35865
   585
  let
blanchet@38039
   586
    val tokens_of =
blanchet@38039
   587
      String.tokens (fn c => not (Char.isAlphaNum c) andalso c <> #"_")
blanchet@38599
   588
    fun do_line (tag :: num :: "axiom" :: (rest as _ :: _)) =
blanchet@38599
   589
        if tag = "cnf" orelse tag = "fof" then
blanchet@38748
   590
          (case strip_prefix_and_unascii axiom_prefix (List.last rest) of
blanchet@38599
   591
             SOME name =>
blanchet@38698
   592
             if member (op =) rest "file" then
blanchet@38818
   593
               ([(name, name |> find_first_in_list_vector axiom_names |> the)]
blanchet@38818
   594
                handle Option.Option =>
blanchet@38818
   595
                       error ("No such fact: " ^ quote name ^ "."))
blanchet@38698
   596
             else
blanchet@39370
   597
               resolve_axiom axiom_names (Num num)
blanchet@39370
   598
           | NONE => resolve_axiom axiom_names (Num num))
blanchet@38599
   599
        else
blanchet@38818
   600
          []
blanchet@39370
   601
      | do_line (num :: "0" :: "Inp" :: _) = resolve_axiom axiom_names (Num num)
blanchet@38039
   602
      | do_line (num :: rest) =
blanchet@39370
   603
        (case List.last rest of
blanchet@39370
   604
           "input" => resolve_axiom axiom_names (Num num)
blanchet@39370
   605
         | _ => [])
blanchet@38818
   606
      | do_line _ = []
blanchet@38818
   607
  in atp_proof |> split_proof_lines |> maps (do_line o tokens_of) end
blanchet@37399
   608
blanchet@37399
   609
val indent_size = 2
blanchet@37399
   610
val no_label = ("", ~1)
blanchet@37399
   611
blanchet@37399
   612
val raw_prefix = "X"
blanchet@37399
   613
val assum_prefix = "A"
blanchet@37399
   614
val fact_prefix = "F"
blanchet@37399
   615
blanchet@37399
   616
fun string_for_label (s, num) = s ^ string_of_int num
blanchet@37399
   617
blanchet@39370
   618
fun name_num (Str (num, _)) = num
blanchet@39370
   619
  | name_num (Num num) = num
blanchet@39370
   620
blanchet@39370
   621
fun raw_label_for_name conjecture_shape name =
blanchet@39370
   622
  case resolve_conjecture conjecture_shape name of
blanchet@39370
   623
    [j] => (conjecture_prefix, j)
blanchet@39370
   624
  | _ => case Int.fromString (name_num name) of
blanchet@39370
   625
           SOME j => (raw_prefix, j)
blanchet@39370
   626
         | NONE => (raw_prefix ^ name_num name, 0)
blanchet@39368
   627
blanchet@37399
   628
fun metis_using [] = ""
blanchet@37399
   629
  | metis_using ls =
blanchet@37399
   630
    "using " ^ space_implode " " (map string_for_label ls) ^ " "
blanchet@37399
   631
fun metis_apply _ 1 = "by "
blanchet@37399
   632
  | metis_apply 1 _ = "apply "
blanchet@37399
   633
  | metis_apply i _ = "prefer " ^ string_of_int i ^ " apply "
blanchet@37479
   634
fun metis_name full_types = if full_types then "metisFT" else "metis"
blanchet@37479
   635
fun metis_call full_types [] = metis_name full_types
blanchet@37479
   636
  | metis_call full_types ss =
blanchet@37479
   637
    "(" ^ metis_name full_types ^ " " ^ space_implode " " ss ^ ")"
blanchet@37479
   638
fun metis_command full_types i n (ls, ss) =
blanchet@37479
   639
  metis_using ls ^ metis_apply i n ^ metis_call full_types ss
blanchet@39327
   640
fun metis_line banner full_types i n ss =
blanchet@39327
   641
  banner ^ ": " ^
blanchet@38597
   642
  Markup.markup Markup.sendback (metis_command full_types i n ([], ss)) ^ "."
blanchet@36281
   643
fun minimize_line _ [] = ""
blanchet@38696
   644
  | minimize_line minimize_command ss =
blanchet@38696
   645
    case minimize_command ss of
blanchet@36281
   646
      "" => ""
blanchet@36281
   647
    | command =>
blanchet@38597
   648
      "\nTo minimize the number of lemmas, try this: " ^
blanchet@38597
   649
      Markup.markup Markup.sendback command ^ "."
immler@31840
   650
blanchet@38282
   651
fun used_facts axiom_names =
blanchet@38282
   652
  used_facts_in_atp_proof axiom_names
blanchet@38752
   653
  #> List.partition (curry (op =) Chained o snd)
blanchet@38752
   654
  #> pairself (sort_distinct (string_ord o pairself fst))
blanchet@38015
   655
blanchet@39327
   656
fun metis_proof_text (banner, full_types, minimize_command, atp_proof,
blanchet@39327
   657
                      axiom_names, goal, i) =
blanchet@36063
   658
  let
blanchet@38282
   659
    val (chained_lemmas, other_lemmas) = used_facts axiom_names atp_proof
blanchet@36063
   660
    val n = Logic.count_prems (prop_of goal)
blanchet@37171
   661
  in
blanchet@39327
   662
    (metis_line banner full_types i n (map fst other_lemmas) ^
blanchet@38752
   663
     minimize_line minimize_command (map fst (other_lemmas @ chained_lemmas)),
blanchet@38752
   664
     other_lemmas @ chained_lemmas)
blanchet@37171
   665
  end
immler@31037
   666
blanchet@36486
   667
(** Isar proof construction and manipulation **)
blanchet@36486
   668
blanchet@36486
   669
fun merge_fact_sets (ls1, ss1) (ls2, ss2) =
blanchet@36486
   670
  (union (op =) ls1 ls2, union (op =) ss1 ss2)
blanchet@36402
   671
blanchet@36402
   672
type label = string * int
blanchet@36402
   673
type facts = label list * string list
blanchet@36402
   674
blanchet@36402
   675
datatype qualifier = Show | Then | Moreover | Ultimately
blanchet@36291
   676
blanchet@36402
   677
datatype step =
blanchet@36478
   678
  Fix of (string * typ) list |
blanchet@36486
   679
  Let of term * term |
blanchet@36402
   680
  Assume of label * term |
blanchet@36402
   681
  Have of qualifier list * label * term * byline
blanchet@36402
   682
and byline =
blanchet@36564
   683
  ByMetis of facts |
blanchet@36402
   684
  CaseSplit of step list list * facts
blanchet@36402
   685
blanchet@36574
   686
fun smart_case_split [] facts = ByMetis facts
blanchet@36574
   687
  | smart_case_split proofs facts = CaseSplit (proofs, facts)
blanchet@36574
   688
blanchet@39370
   689
fun add_fact_from_dep conjecture_shape axiom_names name =
blanchet@39370
   690
  if is_axiom axiom_names name then
blanchet@39368
   691
    apsnd (union (op =) (map fst (resolve_axiom axiom_names name)))
blanchet@36475
   692
  else
blanchet@39370
   693
    apfst (insert (op =) (raw_label_for_name conjecture_shape name))
blanchet@36402
   694
blanchet@37998
   695
fun forall_of v t = HOLogic.all_const (fastype_of v) $ lambda v t
blanchet@36491
   696
fun forall_vars t = fold_rev forall_of (map Var (Term.add_vars t [])) t
blanchet@36491
   697
blanchet@39370
   698
fun step_for_line _ _ _ (Definition (_, t1, t2)) = Let (t1, t2)
blanchet@39370
   699
  | step_for_line conjecture_shape _ _ (Inference (name, t, [])) =
blanchet@39370
   700
    Assume (raw_label_for_name conjecture_shape name, t)
blanchet@39370
   701
  | step_for_line conjecture_shape axiom_names j (Inference (name, t, deps)) =
blanchet@39370
   702
    Have (if j = 1 then [Show] else [],
blanchet@39370
   703
          raw_label_for_name conjecture_shape name, forall_vars t,
blanchet@39370
   704
          ByMetis (fold (add_fact_from_dep conjecture_shape axiom_names) deps
blanchet@39370
   705
                        ([], [])))
blanchet@36291
   706
blanchet@36967
   707
fun proof_from_atp_proof pool ctxt full_types tfrees isar_shrink_factor
blanchet@38282
   708
                         atp_proof conjecture_shape axiom_names params frees =
blanchet@36402
   709
  let
blanchet@36486
   710
    val lines =
blanchet@38035
   711
      atp_proof ^ "$" (* the $ sign acts as a sentinel (FIXME: needed?) *)
blanchet@36548
   712
      |> parse_proof pool
blanchet@39370
   713
(*### FIXME
blanchet@39368
   714
      |> sort (tstp_name_ord o pairself raw_step_name)
blanchet@39370
   715
blanchet@39370
   716
fun raw_step_name (Definition (name, _, _)) = name
blanchet@39370
   717
  | raw_step_name (Inference (name, _, _)) = name
blanchet@39370
   718
*)
blanchet@36967
   719
      |> decode_lines ctxt full_types tfrees
blanchet@38282
   720
      |> rpair [] |-> fold_rev (add_line conjecture_shape axiom_names)
blanchet@36486
   721
      |> rpair [] |-> fold_rev add_nontrivial_line
blanchet@37498
   722
      |> rpair (0, []) |-> fold_rev (add_desired_line isar_shrink_factor
blanchet@38282
   723
                                             conjecture_shape axiom_names frees)
blanchet@36486
   724
      |> snd
blanchet@36402
   725
  in
blanchet@36909
   726
    (if null params then [] else [Fix params]) @
blanchet@39370
   727
    map2 (step_for_line conjecture_shape axiom_names) (length lines downto 1)
blanchet@39370
   728
         lines
blanchet@36402
   729
  end
blanchet@36402
   730
blanchet@36402
   731
(* When redirecting proofs, we keep information about the labels seen so far in
blanchet@36402
   732
   the "backpatches" data structure. The first component indicates which facts
blanchet@36402
   733
   should be associated with forthcoming proof steps. The second component is a
blanchet@37322
   734
   pair ("assum_ls", "drop_ls"), where "assum_ls" are the labels that should
blanchet@37322
   735
   become assumptions and "drop_ls" are the labels that should be dropped in a
blanchet@37322
   736
   case split. *)
blanchet@36402
   737
type backpatches = (label * facts) list * (label list * label list)
blanchet@36402
   738
blanchet@36556
   739
fun used_labels_of_step (Have (_, _, _, by)) =
blanchet@36402
   740
    (case by of
blanchet@36564
   741
       ByMetis (ls, _) => ls
blanchet@36556
   742
     | CaseSplit (proofs, (ls, _)) =>
blanchet@36556
   743
       fold (union (op =) o used_labels_of) proofs ls)
blanchet@36556
   744
  | used_labels_of_step _ = []
blanchet@36556
   745
and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof []
blanchet@36402
   746
blanchet@36402
   747
fun new_labels_of_step (Fix _) = []
blanchet@36486
   748
  | new_labels_of_step (Let _) = []
blanchet@36402
   749
  | new_labels_of_step (Assume (l, _)) = [l]
blanchet@36402
   750
  | new_labels_of_step (Have (_, l, _, _)) = [l]
blanchet@36402
   751
val new_labels_of = maps new_labels_of_step
blanchet@36402
   752
blanchet@36402
   753
val join_proofs =
blanchet@36402
   754
  let
blanchet@36402
   755
    fun aux _ [] = NONE
blanchet@36402
   756
      | aux proof_tail (proofs as (proof1 :: _)) =
blanchet@36402
   757
        if exists null proofs then
blanchet@36402
   758
          NONE
blanchet@36402
   759
        else if forall (curry (op =) (hd proof1) o hd) (tl proofs) then
blanchet@36402
   760
          aux (hd proof1 :: proof_tail) (map tl proofs)
blanchet@36402
   761
        else case hd proof1 of
blanchet@37498
   762
          Have ([], l, t, _) => (* FIXME: should we really ignore the "by"? *)
blanchet@36402
   763
          if forall (fn Have ([], l', t', _) :: _ => (l, t) = (l', t')
blanchet@36402
   764
                      | _ => false) (tl proofs) andalso
blanchet@36402
   765
             not (exists (member (op =) (maps new_labels_of proofs))
blanchet@36556
   766
                         (used_labels_of proof_tail)) then
blanchet@36402
   767
            SOME (l, t, map rev proofs, proof_tail)
blanchet@36402
   768
          else
blanchet@36402
   769
            NONE
blanchet@36402
   770
        | _ => NONE
blanchet@36402
   771
  in aux [] o map rev end
blanchet@36402
   772
blanchet@36402
   773
fun case_split_qualifiers proofs =
blanchet@36402
   774
  case length proofs of
blanchet@36402
   775
    0 => []
blanchet@36402
   776
  | 1 => [Then]
blanchet@36402
   777
  | _ => [Ultimately]
blanchet@36402
   778
blanchet@37991
   779
fun redirect_proof conjecture_shape hyp_ts concl_t proof =
wenzelm@33310
   780
  let
blanchet@37324
   781
    (* The first pass outputs those steps that are independent of the negated
blanchet@37324
   782
       conjecture. The second pass flips the proof by contradiction to obtain a
blanchet@37324
   783
       direct proof, introducing case splits when an inference depends on
blanchet@37324
   784
       several facts that depend on the negated conjecture. *)
blanchet@39370
   785
    fun find_hyp j =
blanchet@39370
   786
      nth hyp_ts (index_in_shape j conjecture_shape)
blanchet@38038
   787
      handle Subscript =>
blanchet@39370
   788
             raise Fail ("Cannot find hypothesis " ^ Int.toString j)
blanchet@39370
   789
     val concl_ls = map (pair conjecture_prefix) (List.last conjecture_shape)
blanchet@39370
   790
val _ = priority ("*** " ^ PolyML.makestring concl_ls)(*###*)
blanchet@38040
   791
     val canonicalize_labels =
blanchet@38040
   792
       map (fn l => if member (op =) concl_ls l then hd concl_ls else l)
blanchet@38040
   793
       #> distinct (op =)
blanchet@38040
   794
     fun first_pass ([], contra) = ([], contra)
blanchet@38040
   795
       | first_pass ((step as Fix _) :: proof, contra) =
blanchet@38040
   796
         first_pass (proof, contra) |>> cons step
blanchet@38040
   797
       | first_pass ((step as Let _) :: proof, contra) =
blanchet@38040
   798
         first_pass (proof, contra) |>> cons step
blanchet@39370
   799
       | first_pass ((step as Assume (l as (_, j), _)) :: proof, contra) =
blanchet@38040
   800
         if member (op =) concl_ls l then
blanchet@38040
   801
           first_pass (proof, contra ||> l = hd concl_ls ? cons step)
blanchet@38040
   802
         else
blanchet@39370
   803
           first_pass (proof, contra) |>> cons (Assume (l, find_hyp j))
blanchet@38040
   804
       | first_pass (Have (qs, l, t, ByMetis (ls, ss)) :: proof, contra) =
blanchet@38040
   805
         let
blanchet@38040
   806
           val ls = canonicalize_labels ls
blanchet@38040
   807
           val step = Have (qs, l, t, ByMetis (ls, ss))
blanchet@38040
   808
         in
blanchet@38040
   809
           if exists (member (op =) (fst contra)) ls then
blanchet@38040
   810
             first_pass (proof, contra |>> cons l ||> cons step)
blanchet@38040
   811
           else
blanchet@38040
   812
             first_pass (proof, contra) |>> cons step
blanchet@38040
   813
         end
blanchet@38040
   814
       | first_pass _ = raise Fail "malformed proof"
blanchet@36402
   815
    val (proof_top, (contra_ls, contra_proof)) =
blanchet@38040
   816
      first_pass (proof, (concl_ls, []))
blanchet@36402
   817
    val backpatch_label = the_default ([], []) oo AList.lookup (op =) o fst
blanchet@36402
   818
    fun backpatch_labels patches ls =
blanchet@36402
   819
      fold merge_fact_sets (map (backpatch_label patches) ls) ([], [])
blanchet@36402
   820
    fun second_pass end_qs ([], assums, patches) =
blanchet@37324
   821
        ([Have (end_qs, no_label, concl_t,
blanchet@36564
   822
                ByMetis (backpatch_labels patches (map snd assums)))], patches)
blanchet@36402
   823
      | second_pass end_qs (Assume (l, t) :: proof, assums, patches) =
blanchet@36402
   824
        second_pass end_qs (proof, (t, l) :: assums, patches)
blanchet@36564
   825
      | second_pass end_qs (Have (qs, l, t, ByMetis (ls, ss)) :: proof, assums,
blanchet@36402
   826
                            patches) =
blanchet@36402
   827
        if member (op =) (snd (snd patches)) l andalso
blanchet@37322
   828
           not (member (op =) (fst (snd patches)) l) andalso
blanchet@36402
   829
           not (AList.defined (op =) (fst patches) l) then
blanchet@36402
   830
          second_pass end_qs (proof, assums, patches ||> apsnd (append ls))
blanchet@36402
   831
        else
blanchet@36402
   832
          (case List.partition (member (op =) contra_ls) ls of
blanchet@36402
   833
             ([contra_l], co_ls) =>
blanchet@37322
   834
             if member (op =) qs Show then
blanchet@37322
   835
               second_pass end_qs (proof, assums,
blanchet@37322
   836
                                   patches |>> cons (contra_l, (co_ls, ss)))
blanchet@37322
   837
             else
blanchet@36402
   838
               second_pass end_qs
blanchet@36402
   839
                           (proof, assums,
blanchet@36402
   840
                            patches |>> cons (contra_l, (l :: co_ls, ss)))
blanchet@36402
   841
               |>> cons (if member (op =) (fst (snd patches)) l then
blanchet@37991
   842
                           Assume (l, negate_term t)
blanchet@36402
   843
                         else
blanchet@37991
   844
                           Have (qs, l, negate_term t,
blanchet@36564
   845
                                 ByMetis (backpatch_label patches l)))
blanchet@36402
   846
           | (contra_ls as _ :: _, co_ls) =>
blanchet@36402
   847
             let
blanchet@36402
   848
               val proofs =
blanchet@36402
   849
                 map_filter
blanchet@36402
   850
                     (fn l =>
blanchet@38040
   851
                         if member (op =) concl_ls l then
blanchet@36402
   852
                           NONE
blanchet@36402
   853
                         else
blanchet@36402
   854
                           let
blanchet@36402
   855
                             val drop_ls = filter (curry (op <>) l) contra_ls
blanchet@36402
   856
                           in
blanchet@36402
   857
                             second_pass []
blanchet@36402
   858
                                 (proof, assums,
blanchet@36402
   859
                                  patches ||> apfst (insert (op =) l)
blanchet@36402
   860
                                          ||> apsnd (union (op =) drop_ls))
blanchet@36402
   861
                             |> fst |> SOME
blanchet@36402
   862
                           end) contra_ls
blanchet@37324
   863
               val (assumes, facts) =
blanchet@37324
   864
                 if member (op =) (fst (snd patches)) l then
blanchet@37991
   865
                   ([Assume (l, negate_term t)], (l :: co_ls, ss))
blanchet@37324
   866
                 else
blanchet@37324
   867
                   ([], (co_ls, ss))
blanchet@36402
   868
             in
blanchet@36402
   869
               (case join_proofs proofs of
blanchet@36402
   870
                  SOME (l, t, proofs, proof_tail) =>
blanchet@36402
   871
                  Have (case_split_qualifiers proofs @
blanchet@36402
   872
                        (if null proof_tail then end_qs else []), l, t,
blanchet@36574
   873
                        smart_case_split proofs facts) :: proof_tail
blanchet@36402
   874
                | NONE =>
blanchet@36402
   875
                  [Have (case_split_qualifiers proofs @ end_qs, no_label,
blanchet@36574
   876
                         concl_t, smart_case_split proofs facts)],
blanchet@36402
   877
                patches)
blanchet@37324
   878
               |>> append assumes
blanchet@36402
   879
             end
blanchet@36402
   880
           | _ => raise Fail "malformed proof")
blanchet@36402
   881
       | second_pass _ _ = raise Fail "malformed proof"
blanchet@36486
   882
    val proof_bottom =
blanchet@36486
   883
      second_pass [Show] (contra_proof, [], ([], ([], []))) |> fst
blanchet@36402
   884
  in proof_top @ proof_bottom end
blanchet@36402
   885
blanchet@38490
   886
(* FIXME: Still needed? Probably not. *)
blanchet@36402
   887
val kill_duplicate_assumptions_in_proof =
blanchet@36402
   888
  let
blanchet@36402
   889
    fun relabel_facts subst =
blanchet@36402
   890
      apfst (map (fn l => AList.lookup (op =) subst l |> the_default l))
blanchet@36491
   891
    fun do_step (step as Assume (l, t)) (proof, subst, assums) =
blanchet@36402
   892
        (case AList.lookup (op aconv) assums t of
blanchet@36967
   893
           SOME l' => (proof, (l, l') :: subst, assums)
blanchet@36491
   894
         | NONE => (step :: proof, subst, (t, l) :: assums))
blanchet@36402
   895
      | do_step (Have (qs, l, t, by)) (proof, subst, assums) =
blanchet@36402
   896
        (Have (qs, l, t,
blanchet@36402
   897
               case by of
blanchet@36564
   898
                 ByMetis facts => ByMetis (relabel_facts subst facts)
blanchet@36402
   899
               | CaseSplit (proofs, facts) =>
blanchet@36402
   900
                 CaseSplit (map do_proof proofs, relabel_facts subst facts)) ::
blanchet@36402
   901
         proof, subst, assums)
blanchet@36491
   902
      | do_step step (proof, subst, assums) = (step :: proof, subst, assums)
blanchet@36402
   903
    and do_proof proof = fold do_step proof ([], [], []) |> #1 |> rev
blanchet@36402
   904
  in do_proof end
blanchet@36402
   905
blanchet@36402
   906
val then_chain_proof =
blanchet@36402
   907
  let
blanchet@36402
   908
    fun aux _ [] = []
blanchet@36491
   909
      | aux _ ((step as Assume (l, _)) :: proof) = step :: aux l proof
blanchet@36402
   910
      | aux l' (Have (qs, l, t, by) :: proof) =
blanchet@36402
   911
        (case by of
blanchet@36564
   912
           ByMetis (ls, ss) =>
blanchet@36402
   913
           Have (if member (op =) ls l' then
blanchet@36402
   914
                   (Then :: qs, l, t,
blanchet@36564
   915
                    ByMetis (filter_out (curry (op =) l') ls, ss))
blanchet@36402
   916
                 else
blanchet@36564
   917
                   (qs, l, t, ByMetis (ls, ss)))
blanchet@36402
   918
         | CaseSplit (proofs, facts) =>
blanchet@36402
   919
           Have (qs, l, t, CaseSplit (map (aux no_label) proofs, facts))) ::
blanchet@36402
   920
        aux l proof
blanchet@36491
   921
      | aux _ (step :: proof) = step :: aux no_label proof
blanchet@36402
   922
  in aux no_label end
blanchet@36402
   923
blanchet@36402
   924
fun kill_useless_labels_in_proof proof =
blanchet@36402
   925
  let
blanchet@36556
   926
    val used_ls = used_labels_of proof
blanchet@36402
   927
    fun do_label l = if member (op =) used_ls l then l else no_label
blanchet@36556
   928
    fun do_step (Assume (l, t)) = Assume (do_label l, t)
blanchet@36556
   929
      | do_step (Have (qs, l, t, by)) =
blanchet@36402
   930
        Have (qs, do_label l, t,
blanchet@36402
   931
              case by of
blanchet@36402
   932
                CaseSplit (proofs, facts) =>
blanchet@36556
   933
                CaseSplit (map (map do_step) proofs, facts)
blanchet@36402
   934
              | _ => by)
blanchet@36556
   935
      | do_step step = step
blanchet@36556
   936
  in map do_step proof end
blanchet@36402
   937
blanchet@36402
   938
fun prefix_for_depth n = replicate_string (n + 1)
blanchet@36402
   939
blanchet@36402
   940
val relabel_proof =
blanchet@36402
   941
  let
blanchet@36402
   942
    fun aux _ _ _ [] = []
blanchet@36402
   943
      | aux subst depth (next_assum, next_fact) (Assume (l, t) :: proof) =
blanchet@36402
   944
        if l = no_label then
blanchet@36402
   945
          Assume (l, t) :: aux subst depth (next_assum, next_fact) proof
blanchet@36402
   946
        else
blanchet@36402
   947
          let val l' = (prefix_for_depth depth assum_prefix, next_assum) in
blanchet@36402
   948
            Assume (l', t) ::
blanchet@36402
   949
            aux ((l, l') :: subst) depth (next_assum + 1, next_fact) proof
blanchet@36402
   950
          end
blanchet@36402
   951
      | aux subst depth (next_assum, next_fact) (Have (qs, l, t, by) :: proof) =
blanchet@36402
   952
        let
blanchet@36402
   953
          val (l', subst, next_fact) =
blanchet@36402
   954
            if l = no_label then
blanchet@36402
   955
              (l, subst, next_fact)
blanchet@36402
   956
            else
blanchet@36402
   957
              let
blanchet@36402
   958
                val l' = (prefix_for_depth depth fact_prefix, next_fact)
blanchet@36402
   959
              in (l', (l, l') :: subst, next_fact + 1) end
blanchet@36570
   960
          val relabel_facts =
blanchet@39370
   961
            apfst (maps (the_list o AList.lookup (op =) subst))
blanchet@36402
   962
          val by =
blanchet@36402
   963
            case by of
blanchet@36564
   964
              ByMetis facts => ByMetis (relabel_facts facts)
blanchet@36402
   965
            | CaseSplit (proofs, facts) =>
blanchet@36402
   966
              CaseSplit (map (aux subst (depth + 1) (1, 1)) proofs,
blanchet@36402
   967
                         relabel_facts facts)
blanchet@36402
   968
        in
blanchet@36402
   969
          Have (qs, l', t, by) ::
blanchet@36402
   970
          aux subst depth (next_assum, next_fact) proof
blanchet@36402
   971
        end
blanchet@36491
   972
      | aux subst depth nextp (step :: proof) =
blanchet@36491
   973
        step :: aux subst depth nextp proof
blanchet@36402
   974
  in aux [] 0 (1, 1) end
blanchet@36402
   975
wenzelm@39115
   976
fun string_for_proof ctxt0 full_types i n =
blanchet@36402
   977
  let
wenzelm@39134
   978
    val ctxt = ctxt0
wenzelm@39134
   979
      |> Config.put show_free_types false
wenzelm@39134
   980
      |> Config.put show_types true
blanchet@37319
   981
    fun fix_print_mode f x =
wenzelm@39134
   982
      Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN)
wenzelm@39134
   983
                               (print_mode_value ())) f x
blanchet@36402
   984
    fun do_indent ind = replicate_string (ind * indent_size) " "
blanchet@36478
   985
    fun do_free (s, T) =
blanchet@36478
   986
      maybe_quote s ^ " :: " ^
blanchet@36478
   987
      maybe_quote (fix_print_mode (Syntax.string_of_typ ctxt) T)
blanchet@36570
   988
    fun do_label l = if l = no_label then "" else string_for_label l ^ ": "
blanchet@36402
   989
    fun do_have qs =
blanchet@36402
   990
      (if member (op =) qs Moreover then "moreover " else "") ^
blanchet@36402
   991
      (if member (op =) qs Ultimately then "ultimately " else "") ^
blanchet@36402
   992
      (if member (op =) qs Then then
blanchet@36402
   993
         if member (op =) qs Show then "thus" else "hence"
blanchet@36402
   994
       else
blanchet@36402
   995
         if member (op =) qs Show then "show" else "have")
blanchet@36478
   996
    val do_term = maybe_quote o fix_print_mode (Syntax.string_of_term ctxt)
blanchet@36570
   997
    fun do_facts (ls, ss) =
blanchet@38698
   998
      metis_command full_types 1 1
blanchet@38698
   999
                    (ls |> sort_distinct (prod_ord string_ord int_ord),
blanchet@38698
  1000
                     ss |> sort_distinct string_ord)
blanchet@36478
  1001
    and do_step ind (Fix xs) =
blanchet@36478
  1002
        do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n"
blanchet@36486
  1003
      | do_step ind (Let (t1, t2)) =
blanchet@36486
  1004
        do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n"
blanchet@36402
  1005
      | do_step ind (Assume (l, t)) =
blanchet@36402
  1006
        do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n"
blanchet@36564
  1007
      | do_step ind (Have (qs, l, t, ByMetis facts)) =
blanchet@36402
  1008
        do_indent ind ^ do_have qs ^ " " ^
blanchet@36479
  1009
        do_label l ^ do_term t ^ " " ^ do_facts facts ^ "\n"
blanchet@36402
  1010
      | do_step ind (Have (qs, l, t, CaseSplit (proofs, facts))) =
blanchet@36402
  1011
        space_implode (do_indent ind ^ "moreover\n")
blanchet@36402
  1012
                      (map (do_block ind) proofs) ^
blanchet@36479
  1013
        do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ " " ^
blanchet@36478
  1014
        do_facts facts ^ "\n"
blanchet@36402
  1015
    and do_steps prefix suffix ind steps =
blanchet@36402
  1016
      let val s = implode (map (do_step ind) steps) in
blanchet@36402
  1017
        replicate_string (ind * indent_size - size prefix) " " ^ prefix ^
blanchet@36402
  1018
        String.extract (s, ind * indent_size,
blanchet@36402
  1019
                        SOME (size s - ind * indent_size - 1)) ^
blanchet@36402
  1020
        suffix ^ "\n"
blanchet@36402
  1021
      end
blanchet@36402
  1022
    and do_block ind proof = do_steps "{ " " }" (ind + 1) proof
blanchet@36564
  1023
    (* One-step proofs are pointless; better use the Metis one-liner
blanchet@36564
  1024
       directly. *)
blanchet@36564
  1025
    and do_proof [Have (_, _, _, ByMetis _)] = ""
blanchet@36564
  1026
      | do_proof proof =
blanchet@36480
  1027
        (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
blanchet@36480
  1028
        do_indent 0 ^ "proof -\n" ^
blanchet@36480
  1029
        do_steps "" "" 1 proof ^
blanchet@38599
  1030
        do_indent 0 ^ (if n <> 1 then "next" else "qed")
blanchet@36488
  1031
  in do_proof end
blanchet@36402
  1032
blanchet@37479
  1033
fun isar_proof_text (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
blanchet@39327
  1034
                    (other_params as (_, full_types, _, atp_proof, axiom_names,
blanchet@38282
  1035
                                      goal, i)) =
blanchet@36402
  1036
  let
blanchet@36909
  1037
    val (params, hyp_ts, concl_t) = strip_subgoal goal i
blanchet@36909
  1038
    val frees = fold Term.add_frees (concl_t :: hyp_ts) []
blanchet@36967
  1039
    val tfrees = fold Term.add_tfrees (concl_t :: hyp_ts) []
blanchet@36402
  1040
    val n = Logic.count_prems (prop_of goal)
blanchet@37479
  1041
    val (one_line_proof, lemma_names) = metis_proof_text other_params
blanchet@36283
  1042
    fun isar_proof_for () =
blanchet@36967
  1043
      case proof_from_atp_proof pool ctxt full_types tfrees isar_shrink_factor
blanchet@38282
  1044
                                atp_proof conjecture_shape axiom_names params
blanchet@36924
  1045
                                frees
blanchet@39370
  1046
(*###
blanchet@37991
  1047
           |> redirect_proof conjecture_shape hyp_ts concl_t
blanchet@36402
  1048
           |> kill_duplicate_assumptions_in_proof
blanchet@36402
  1049
           |> then_chain_proof
blanchet@36402
  1050
           |> kill_useless_labels_in_proof
blanchet@36402
  1051
           |> relabel_proof
blanchet@39370
  1052
*)
blanchet@37479
  1053
           |> string_for_proof ctxt full_types i n of
blanchet@38599
  1054
        "" => "\nNo structured proof available."
blanchet@38599
  1055
      | proof => "\n\nStructured proof:\n" ^ Markup.markup Markup.sendback proof
blanchet@35868
  1056
    val isar_proof =
blanchet@36402
  1057
      if debug then
blanchet@36283
  1058
        isar_proof_for ()
blanchet@36283
  1059
      else
blanchet@36283
  1060
        try isar_proof_for ()
blanchet@38599
  1061
        |> the_default "\nWarning: The Isar proof construction failed."
blanchet@36283
  1062
  in (one_line_proof ^ isar_proof, lemma_names) end
paulson@21978
  1063
blanchet@36557
  1064
fun proof_text isar_proof isar_params other_params =
blanchet@36557
  1065
  (if isar_proof then isar_proof_text isar_params else metis_proof_text)
blanchet@36557
  1066
      other_params
blanchet@36223
  1067
immler@31038
  1068
end;