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