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