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