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