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