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