src/HOL/Tools/Sledgehammer/sledgehammer_reconstruct.ML
author blanchet
Thu, 12 Sep 2013 22:10:57 +0200
changeset 54723 bd5fa6425993
parent 54189 a0db255af8c5
child 54898 4d34e267fba9
permissions -rw-r--r--
prefixed types and some functions with "atp_" for disambiguation
blanchet@50898
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_reconstruct.ML
blanchet@50898
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@50898
     3
    Author:     Steffen Juilf Smolka, TU Muenchen
blanchet@50898
     4
blanchet@50929
     5
Isar proof reconstruction from ATP proofs.
blanchet@50898
     6
*)
blanchet@50898
     7
blanchet@53511
     8
signature SLEDGEHAMMER_RECONSTRUCT =
blanchet@50898
     9
sig
blanchet@54723
    10
  type 'a atp_proof = 'a ATP_Proof.atp_proof
blanchet@50929
    11
  type stature = ATP_Problem_Generate.stature
smolkas@53692
    12
  type one_line_params = Sledgehammer_Print.one_line_params
blanchet@50929
    13
blanchet@50929
    14
  type isar_params =
smolkas@53769
    15
    bool * bool * Time.time option * bool * real * int * real * bool * bool
smolkas@53693
    16
    * string Symtab.table * (string * stature) list vector
blanchet@54723
    17
    * (string * term) list * int Symtab.table * string atp_proof * thm
blanchet@50929
    18
blanchet@54723
    19
  val lam_trans_of_atp_proof : string atp_proof -> string -> string
blanchet@54723
    20
  val is_typed_helper_used_in_atp_proof : string atp_proof -> bool
blanchet@50929
    21
  val used_facts_in_atp_proof :
blanchet@54723
    22
    Proof.context -> (string * stature) list vector -> string atp_proof ->
blanchet@50929
    23
    (string * stature) list
blanchet@50929
    24
  val used_facts_in_unsound_atp_proof :
blanchet@54723
    25
    Proof.context -> (string * stature) list vector -> 'a atp_proof ->
blanchet@50929
    26
    string list option
blanchet@50898
    27
  val isar_proof_text :
wenzelm@54189
    28
    Proof.context -> bool option -> isar_params -> one_line_params -> string
blanchet@50898
    29
  val proof_text :
wenzelm@54189
    30
    Proof.context -> bool option -> isar_params -> int -> one_line_params
blanchet@52327
    31
    -> string
blanchet@50898
    32
end;
blanchet@50898
    33
blanchet@53511
    34
structure Sledgehammer_Reconstruct : SLEDGEHAMMER_RECONSTRUCT =
blanchet@50898
    35
struct
blanchet@50898
    36
blanchet@50898
    37
open ATP_Util
blanchet@50929
    38
open ATP_Problem
blanchet@50898
    39
open ATP_Proof
blanchet@50898
    40
open ATP_Problem_Generate
blanchet@50898
    41
open ATP_Proof_Reconstruct
blanchet@50933
    42
open Sledgehammer_Util
smolkas@53692
    43
open Sledgehammer_Reconstructor
smolkas@51279
    44
open Sledgehammer_Proof
smolkas@51273
    45
open Sledgehammer_Annotate
smolkas@53692
    46
open Sledgehammer_Print
smolkas@53693
    47
open Sledgehammer_Preplay
smolkas@52267
    48
open Sledgehammer_Compress
smolkas@53729
    49
open Sledgehammer_Try0
smolkas@53748
    50
open Sledgehammer_Minimize_Isar
blanchet@50929
    51
blanchet@50929
    52
structure String_Redirect = ATP_Proof_Redirect(
blanchet@54723
    53
  type key = atp_step_name
blanchet@50929
    54
  val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s')
blanchet@50929
    55
  val string_of = fst)
blanchet@50929
    56
blanchet@50898
    57
open String_Redirect
blanchet@50898
    58
blanchet@50929
    59
(** fact extraction from ATP proofs **)
blanchet@50929
    60
blanchet@50929
    61
fun find_first_in_list_vector vec key =
blanchet@50929
    62
  Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key
blanchet@50929
    63
                 | (_, value) => value) NONE vec
blanchet@50929
    64
blanchet@50929
    65
val unprefix_fact_number = space_implode "_" o tl o space_explode "_"
blanchet@50929
    66
blanchet@50929
    67
fun resolve_one_named_fact fact_names s =
blanchet@50929
    68
  case try (unprefix fact_prefix) s of
blanchet@50929
    69
    SOME s' =>
blanchet@50929
    70
    let val s' = s' |> unprefix_fact_number |> unascii_of in
blanchet@50929
    71
      s' |> find_first_in_list_vector fact_names |> Option.map (pair s')
blanchet@50929
    72
    end
blanchet@50929
    73
  | NONE => NONE
blanchet@50929
    74
fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names)
blanchet@50929
    75
fun is_fact fact_names = not o null o resolve_fact fact_names
blanchet@50929
    76
blanchet@50929
    77
fun resolve_one_named_conjecture s =
blanchet@50929
    78
  case try (unprefix conjecture_prefix) s of
blanchet@50929
    79
    SOME s' => Int.fromString s'
blanchet@50929
    80
  | NONE => NONE
blanchet@50929
    81
blanchet@50929
    82
val resolve_conjecture = map_filter resolve_one_named_conjecture
blanchet@50929
    83
val is_conjecture = not o null o resolve_conjecture
blanchet@50929
    84
blanchet@50929
    85
val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix
blanchet@50929
    86
blanchet@50929
    87
(* overapproximation (good enough) *)
blanchet@50929
    88
fun is_lam_lifted s =
blanchet@50929
    89
  String.isPrefix fact_prefix s andalso
blanchet@50929
    90
  String.isSubstring ascii_of_lam_fact_prefix s
blanchet@50929
    91
blanchet@50929
    92
val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix)
blanchet@50929
    93
blanchet@50929
    94
fun is_axiom_used_in_proof pred =
blanchet@52339
    95
  exists (fn ((_, ss), _, _, _, []) => exists pred ss | _ => false)
blanchet@50929
    96
blanchet@53168
    97
fun lam_trans_of_atp_proof atp_proof default =
blanchet@50929
    98
  case (is_axiom_used_in_proof is_combinator_def atp_proof,
blanchet@50929
    99
        is_axiom_used_in_proof is_lam_lifted atp_proof) of
blanchet@50929
   100
    (false, false) => default
blanchet@50929
   101
  | (false, true) => liftingN
blanchet@50929
   102
(*  | (true, true) => combs_and_liftingN -- not supported by "metis" *)
blanchet@50929
   103
  | (true, _) => combsN
blanchet@50929
   104
blanchet@50929
   105
val is_typed_helper_name =
blanchet@50929
   106
  String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix
blanchet@52213
   107
blanchet@50929
   108
fun is_typed_helper_used_in_atp_proof atp_proof =
blanchet@50929
   109
  is_axiom_used_in_proof is_typed_helper_name atp_proof
blanchet@50929
   110
blanchet@50929
   111
fun add_non_rec_defs fact_names accum =
blanchet@50929
   112
  Vector.foldl (fn (facts, facts') =>
blanchet@50929
   113
      union (op =) (filter (fn (_, (_, status)) => status = Non_Rec_Def) facts)
blanchet@50929
   114
            facts')
blanchet@50929
   115
    accum fact_names
blanchet@50929
   116
blanchet@50929
   117
val isa_ext = Thm.get_name_hint @{thm ext}
blanchet@50929
   118
val isa_short_ext = Long_Name.base_name isa_ext
blanchet@50929
   119
blanchet@50929
   120
fun ext_name ctxt =
blanchet@50929
   121
  if Thm.eq_thm_prop (@{thm ext},
blanchet@52208
   122
       singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then
blanchet@50929
   123
    isa_short_ext
blanchet@50929
   124
  else
blanchet@50929
   125
    isa_ext
blanchet@50929
   126
blanchet@51690
   127
val leo2_extcnf_equal_neg_rule = "extcnf_equal_neg"
blanchet@51690
   128
val leo2_unfold_def_rule = "unfold_def"
blanchet@50929
   129
blanchet@52338
   130
fun add_fact ctxt fact_names ((_, ss), _, _, rule, deps) =
blanchet@52335
   131
  (if rule = leo2_extcnf_equal_neg_rule then
blanchet@52335
   132
     insert (op =) (ext_name ctxt, (Global, General))
blanchet@52335
   133
   else if rule = leo2_unfold_def_rule then
blanchet@52335
   134
     (* LEO 1.3.3 does not record definitions properly, leading to missing
blanchet@52335
   135
        dependencies in the TSTP proof. Remove the next line once this is
blanchet@52335
   136
        fixed. *)
blanchet@52335
   137
     add_non_rec_defs fact_names
blanchet@53214
   138
   else if rule = agsyhol_coreN orelse rule = satallax_coreN then
blanchet@52335
   139
     (fn [] =>
blanchet@53214
   140
         (* agsyHOL and Satallax don't include definitions in their
blanchet@53214
   141
            unsatisfiable cores, so we assume the worst and include them all
blanchet@53214
   142
            here. *)
blanchet@52335
   143
         [(ext_name ctxt, (Global, General))] |> add_non_rec_defs fact_names
blanchet@52335
   144
       | facts => facts)
blanchet@52335
   145
   else
blanchet@52335
   146
     I)
blanchet@52335
   147
  #> (if null deps then union (op =) (resolve_fact fact_names ss) else I)
blanchet@50929
   148
blanchet@50929
   149
fun used_facts_in_atp_proof ctxt fact_names atp_proof =
blanchet@50929
   150
  if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names
blanchet@50929
   151
  else fold (add_fact ctxt fact_names) atp_proof []
blanchet@50929
   152
blanchet@50929
   153
fun used_facts_in_unsound_atp_proof _ _ [] = NONE
blanchet@50929
   154
  | used_facts_in_unsound_atp_proof ctxt fact_names atp_proof =
blanchet@50929
   155
    let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in
blanchet@50929
   156
      if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso
blanchet@50929
   157
         not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then
blanchet@50929
   158
        SOME (map fst used_facts)
blanchet@50929
   159
      else
blanchet@50929
   160
        NONE
blanchet@50929
   161
    end
blanchet@50929
   162
blanchet@50929
   163
blanchet@50929
   164
(** Isar proof construction and manipulation **)
blanchet@50929
   165
blanchet@51032
   166
val assume_prefix = "a"
blanchet@50929
   167
val have_prefix = "f"
blanchet@50929
   168
val raw_prefix = "x"
blanchet@50929
   169
blanchet@53135
   170
fun raw_label_of_name (num, ss) =
blanchet@50929
   171
  case resolve_conjecture ss of
blanchet@50929
   172
    [j] => (conjecture_prefix, j)
blanchet@50929
   173
  | _ => (raw_prefix ^ ascii_of num, 0)
blanchet@50929
   174
blanchet@53135
   175
fun label_of_clause [name] = raw_label_of_name name
blanchet@53113
   176
  | label_of_clause c =
blanchet@53135
   177
    (space_implode "___" (map (fst o raw_label_of_name) c), 0)
blanchet@51020
   178
blanchet@53168
   179
fun add_fact_of_dependencies fact_names (names as [(_, ss)]) =
blanchet@51020
   180
    if is_fact fact_names ss then
blanchet@51020
   181
      apsnd (union (op =) (map fst (resolve_fact fact_names ss)))
blanchet@51020
   182
    else
blanchet@51020
   183
      apfst (insert (op =) (label_of_clause names))
blanchet@53171
   184
  | add_fact_of_dependencies _ names =
blanchet@51020
   185
    apfst (insert (op =) (label_of_clause names))
blanchet@50929
   186
blanchet@50929
   187
fun repair_name "$true" = "c_True"
blanchet@50929
   188
  | repair_name "$false" = "c_False"
blanchet@50929
   189
  | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
blanchet@50929
   190
  | repair_name s =
blanchet@50929
   191
    if is_tptp_equal s orelse
blanchet@50929
   192
       (* seen in Vampire proofs *)
blanchet@50929
   193
       (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then
blanchet@50929
   194
      tptp_equal
blanchet@50929
   195
    else
blanchet@50929
   196
      s
blanchet@50929
   197
blanchet@50929
   198
fun infer_formula_types ctxt =
blanchet@50929
   199
  Type.constraint HOLogic.boolT
blanchet@50929
   200
  #> Syntax.check_term
blanchet@50929
   201
         (Proof_Context.set_mode Proof_Context.mode_schematic ctxt)
blanchet@50929
   202
blanchet@50929
   203
val combinator_table =
blanchet@50929
   204
  [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def [abs_def]}),
blanchet@50929
   205
   (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def [abs_def]}),
blanchet@50929
   206
   (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def [abs_def]}),
blanchet@50929
   207
   (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def [abs_def]}),
blanchet@50929
   208
   (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def [abs_def]})]
blanchet@50929
   209
blanchet@50929
   210
fun uncombine_term thy =
blanchet@50929
   211
  let
blanchet@50929
   212
    fun aux (t1 $ t2) = betapply (pairself aux (t1, t2))
blanchet@50929
   213
      | aux (Abs (s, T, t')) = Abs (s, T, aux t')
blanchet@50929
   214
      | aux (t as Const (x as (s, _))) =
blanchet@50929
   215
        (case AList.lookup (op =) combinator_table s of
blanchet@50929
   216
           SOME thm => thm |> prop_of |> specialize_type thy x
blanchet@50929
   217
                           |> Logic.dest_equals |> snd
blanchet@50929
   218
         | NONE => t)
blanchet@50929
   219
      | aux t = t
blanchet@50929
   220
  in aux end
blanchet@50929
   221
blanchet@53287
   222
fun unlift_term lifted =
blanchet@53287
   223
  map_aterms (fn t as Const (s, _) =>
blanchet@53287
   224
                 if String.isPrefix lam_lifted_prefix s then
blanchet@53287
   225
                   case AList.lookup (op =) lifted s of
blanchet@53287
   226
                     SOME t =>
blanchet@53287
   227
                     (* FIXME: do something about the types *)
blanchet@53287
   228
                     unlift_term lifted t
blanchet@53287
   229
                   | NONE => t
blanchet@53287
   230
                 else
blanchet@53287
   231
                   t
blanchet@53287
   232
               | t => t)
blanchet@53287
   233
blanchet@53287
   234
fun decode_line ctxt lifted sym_tab (name, role, u, rule, deps) =
blanchet@52335
   235
  let
blanchet@52335
   236
    val thy = Proof_Context.theory_of ctxt
blanchet@53262
   237
    val t =
blanchet@53262
   238
      u |> prop_of_atp ctxt true sym_tab
blanchet@53262
   239
        |> uncombine_term thy
blanchet@53287
   240
        |> unlift_term lifted
blanchet@53262
   241
        |> infer_formula_types ctxt
blanchet@53262
   242
  in (name, role, t, rule, deps) end
blanchet@50929
   243
blanchet@50929
   244
fun replace_one_dependency (old, new) dep =
blanchet@50929
   245
  if is_same_atp_step dep old then new else [dep]
blanchet@52338
   246
fun replace_dependencies_in_line p (name, role, t, rule, deps) =
blanchet@52338
   247
  (name, role, t, rule, fold (union (op =) o replace_one_dependency p) deps [])
blanchet@50929
   248
blanchet@50929
   249
(* No "real" literals means only type information (tfree_tcs, clsrel, or
blanchet@50929
   250
   clsarity). *)
blanchet@50929
   251
fun is_only_type_information t = t aconv @{term True}
blanchet@50929
   252
blanchet@51920
   253
fun s_maybe_not role = role <> Conjecture ? s_not
blanchet@51920
   254
blanchet@52338
   255
fun is_same_inference (role, t) (_, role', t', _, _) =
blanchet@52338
   256
  s_maybe_not role t aconv s_maybe_not role' t'
blanchet@50929
   257
blanchet@50929
   258
(* Discard facts; consolidate adjacent lines that prove the same formula, since
blanchet@50929
   259
   they differ only in type information.*)
blanchet@52338
   260
fun add_line fact_names (name as (_, ss), role, t, rule, []) lines =
blanchet@50929
   261
    (* No dependencies: fact, conjecture, or (for Vampire) internal facts or
blanchet@50929
   262
       definitions. *)
blanchet@51920
   263
    if is_conjecture ss then
blanchet@52338
   264
      (name, role, t, rule, []) :: lines
blanchet@51920
   265
    else if is_fact fact_names ss then
blanchet@50929
   266
      (* Facts are not proof lines. *)
blanchet@50929
   267
      if is_only_type_information t then
blanchet@50929
   268
        map (replace_dependencies_in_line (name, [])) lines
blanchet@51920
   269
      else
blanchet@51920
   270
        lines
blanchet@50929
   271
    else
blanchet@50929
   272
      map (replace_dependencies_in_line (name, [])) lines
blanchet@52338
   273
  | add_line _ (line as (name, role, t, _, _)) lines =
blanchet@50929
   274
    (* Type information will be deleted later; skip repetition test. *)
blanchet@50929
   275
    if is_only_type_information t then
blanchet@51690
   276
      line :: lines
blanchet@50929
   277
    (* Is there a repetition? If so, replace later line by earlier one. *)
blanchet@51920
   278
    else case take_prefix (not o is_same_inference (role, t)) lines of
blanchet@51690
   279
      (_, []) => line :: lines
blanchet@52338
   280
    | (pre, (name', _, _, _, _) :: post) =>
blanchet@51690
   281
      line :: pre @ map (replace_dependencies_in_line (name', [name])) post
blanchet@50929
   282
blanchet@50929
   283
val waldmeister_conjecture_num = "1.0.0.0"
blanchet@50929
   284
wenzelm@52395
   285
fun repair_waldmeister_endgame arg =
blanchet@50929
   286
  let
blanchet@52338
   287
    fun do_tail (name, _, t, rule, deps) =
blanchet@52338
   288
      (name, Negated_Conjecture, s_not t, rule, deps)
blanchet@50929
   289
    fun do_body [] = []
blanchet@52338
   290
      | do_body ((line as ((num, _), _, _, _, _)) :: lines) =
blanchet@50929
   291
        if num = waldmeister_conjecture_num then map do_tail (line :: lines)
blanchet@50929
   292
        else line :: do_body lines
wenzelm@52395
   293
  in do_body arg end
blanchet@50929
   294
blanchet@50929
   295
(* Recursively delete empty lines (type information) from the proof. *)
blanchet@52338
   296
fun add_nontrivial_line (line as (name, _, t, _, [])) lines =
blanchet@50929
   297
    if is_only_type_information t then delete_dependency name lines
blanchet@50929
   298
    else line :: lines
blanchet@50929
   299
  | add_nontrivial_line line lines = line :: lines
blanchet@50929
   300
and delete_dependency name lines =
blanchet@50929
   301
  fold_rev add_nontrivial_line
blanchet@50929
   302
           (map (replace_dependencies_in_line (name, [])) lines) []
blanchet@50929
   303
blanchet@51690
   304
val e_skolemize_rule = "skolemize"
blanchet@51690
   305
val vampire_skolemisation_rule = "skolemisation"
blanchet@51690
   306
blanchet@51691
   307
val is_skolemize_rule =
blanchet@51691
   308
  member (op =) [e_skolemize_rule, vampire_skolemisation_rule]
blanchet@51691
   309
blanchet@53262
   310
fun add_desired_line fact_names (name as (_, ss), role, t, rule, deps)
blanchet@52338
   311
                     (j, lines) =
blanchet@52335
   312
  (j + 1,
blanchet@52335
   313
   if is_fact fact_names ss orelse
blanchet@52335
   314
      is_conjecture ss orelse
blanchet@52335
   315
      is_skolemize_rule rule orelse
blanchet@52335
   316
      (* the last line must be kept *)
blanchet@52335
   317
      j = 0 orelse
blanchet@52335
   318
      (not (is_only_type_information t) andalso
blanchet@52335
   319
       null (Term.add_tvars t []) andalso
blanchet@52335
   320
       length deps >= 2 andalso
blanchet@52335
   321
       (* kill next to last line, which usually results in a trivial step *)
blanchet@52335
   322
       j <> 1) then
blanchet@52338
   323
     (name, role, t, rule, deps) :: lines  (* keep line *)
blanchet@52335
   324
   else
blanchet@52335
   325
     map (replace_dependencies_in_line (name, deps)) lines)  (* drop line *)
blanchet@50929
   326
blanchet@50898
   327
smolkas@53591
   328
val add_labels_of_proof =
smolkas@53591
   329
  steps_of_proof #> fold_isar_steps
blanchet@53893
   330
    (byline_of_step #> (fn SOME (By ((ls, _), _)) => union (op =) ls | _ => I))
blanchet@50929
   331
blanchet@50929
   332
fun kill_useless_labels_in_proof proof =
blanchet@50929
   333
  let
smolkas@52315
   334
    val used_ls = add_labels_of_proof proof []
blanchet@50929
   335
    fun do_label l = if member (op =) used_ls l then l else no_label
smolkas@52316
   336
    fun do_assms (Assume assms) = Assume (map (apfst do_label) assms)
smolkas@53591
   337
    fun do_step (Prove (qs, xs, l, t, subproofs, by)) =
smolkas@53591
   338
          Prove (qs, xs, do_label l, t, map do_proof subproofs, by)
blanchet@50929
   339
      | do_step step = step
smolkas@52316
   340
    and do_proof (Proof (fix, assms, steps)) =
smolkas@52316
   341
          Proof (fix, do_assms assms, map do_step steps)
smolkas@52265
   342
  in do_proof proof end
blanchet@50929
   343
blanchet@53135
   344
fun prefix_of_depth n = replicate_string (n + 1)
blanchet@50929
   345
blanchet@50929
   346
val relabel_proof =
blanchet@50929
   347
  let
smolkas@52316
   348
    fun fresh_label depth prefix (old as (l, subst, next)) =
blanchet@51687
   349
      if l = no_label then
blanchet@51687
   350
        old
blanchet@51687
   351
      else
blanchet@53135
   352
        let val l' = (prefix_of_depth depth prefix, next) in
smolkas@52316
   353
          (l', (l, l') :: subst, next + 1)
blanchet@51687
   354
        end
blanchet@51687
   355
    fun do_facts subst =
blanchet@51687
   356
      apfst (maps (the_list o AList.lookup (op =) subst))
smolkas@52316
   357
    fun do_assm depth (l, t) (subst, next) =
smolkas@52316
   358
      let
smolkas@52316
   359
        val (l, subst, next) =
smolkas@52316
   360
          (l, subst, next) |> fresh_label depth assume_prefix
smolkas@52316
   361
      in
smolkas@52316
   362
        ((l, t), (subst, next))
smolkas@52316
   363
      end
smolkas@52316
   364
    fun do_assms subst depth (Assume assms) =
smolkas@52316
   365
      fold_map (do_assm depth) assms (subst, 1)
smolkas@52316
   366
      |> apfst Assume
smolkas@52316
   367
      |> apsnd fst
smolkas@52316
   368
    fun do_steps _ _ _ [] = []
smolkas@53591
   369
      | do_steps subst depth next (Prove (qs, xs, l, t, sub, by) :: steps) =
blanchet@51687
   370
        let
smolkas@52316
   371
          val (l, subst, next) =
smolkas@52316
   372
            (l, subst, next) |> fresh_label depth have_prefix
smolkas@53591
   373
          val sub = do_proofs subst depth sub
smolkas@53763
   374
          val by = by |> do_byline subst
smolkas@53591
   375
        in Prove (qs, xs, l, t, sub, by) :: do_steps subst depth next steps end
smolkas@52316
   376
      | do_steps subst depth next (step :: steps) =
smolkas@52316
   377
        step :: do_steps subst depth next steps
smolkas@52316
   378
    and do_proof subst depth (Proof (fix, assms, steps)) =
smolkas@52316
   379
      let val (assms, subst) = do_assms subst depth assms in
smolkas@52316
   380
        Proof (fix, assms, do_steps subst depth 1 steps)
smolkas@52316
   381
      end
smolkas@53763
   382
    and do_byline subst byline =
smolkas@53729
   383
      map_facts_of_byline (do_facts subst) byline
smolkas@52316
   384
    and do_proofs subst depth = map (do_proof subst (depth + 1))
smolkas@52316
   385
  in do_proof [] 0 end
blanchet@50929
   386
blanchet@51019
   387
val chain_direct_proof =
blanchet@51019
   388
  let
smolkas@52315
   389
    fun do_qs_lfs NONE lfs = ([], lfs)
smolkas@52315
   390
      | do_qs_lfs (SOME l0) lfs =
smolkas@52315
   391
        if member (op =) lfs l0 then ([Then], lfs |> remove (op =) l0)
smolkas@52315
   392
        else ([], lfs)
smolkas@53729
   393
    fun chain_step lbl
smolkas@53729
   394
      (Prove (qs, xs, l, t, subproofs, By ((lfs, gfs), method))) =
smolkas@53590
   395
          let val (qs', lfs) = do_qs_lfs lbl lfs in
smolkas@53591
   396
            Prove (qs' @ qs, xs, l, t, chain_proofs subproofs,
smolkas@53729
   397
                   By ((lfs, gfs), method))
smolkas@53590
   398
          end
blanchet@51687
   399
      | chain_step _ step = step
smolkas@52316
   400
    and chain_steps _ [] = []
smolkas@52316
   401
      | chain_steps (prev as SOME _) (i :: is) =
smolkas@52316
   402
        chain_step prev i :: chain_steps (label_of_step i) is
smolkas@52316
   403
      | chain_steps _ (i :: is) = i :: chain_steps (label_of_step i) is
smolkas@52316
   404
    and chain_proof (Proof (fix, Assume assms, steps)) =
smolkas@52316
   405
      Proof (fix, Assume assms,
smolkas@52316
   406
             chain_steps (try (List.last #> fst) assms) steps)
smolkas@52316
   407
    and chain_proofs proofs = map (chain_proof) proofs
smolkas@52316
   408
  in chain_proof end
blanchet@50898
   409
blanchet@50929
   410
type isar_params =
smolkas@53769
   411
  bool * bool * Time.time option * bool * real * int * real * bool * bool
smolkas@53693
   412
  * string Symtab.table * (string * stature) list vector
blanchet@54723
   413
  * (string * term) list * int Symtab.table * string atp_proof * thm
blanchet@50929
   414
wenzelm@54189
   415
fun isar_proof_text ctxt isar_proofs
smolkas@53693
   416
    (debug, verbose, preplay_timeout, preplay_trace, isar_compress,
smolkas@53769
   417
     isar_compress_degree, merge_timeout_slack, isar_try0, isar_minimize, pool,
smolkas@53769
   418
     fact_names, lifted, sym_tab, atp_proof, goal)
blanchet@50933
   419
    (one_line_params as (_, _, _, _, subgoal, subgoal_count)) =
blanchet@50898
   420
  let
blanchet@53333
   421
    val (params, hyp_ts, concl_t) = strip_subgoal goal subgoal ctxt
blanchet@53333
   422
    val (_, ctxt) =
blanchet@53333
   423
      params
blanchet@53333
   424
      |> map (fn (s, T) => (Binding.name s, SOME T, NoSyn))
blanchet@53333
   425
      |> (fn fixes =>
blanchet@53333
   426
             ctxt |> Variable.set_body false
blanchet@53333
   427
                  |> Proof_Context.add_fixes fixes)
wenzelm@54189
   428
    val one_line_proof = one_line_proof_text 0 one_line_params
blanchet@50898
   429
    val type_enc =
blanchet@50898
   430
      if is_typed_helper_used_in_atp_proof atp_proof then full_typesN
blanchet@50898
   431
      else partial_typesN
blanchet@53168
   432
    val lam_trans = lam_trans_of_atp_proof atp_proof metis_default_lam_trans
smolkas@53693
   433
    val do_preplay = preplay_timeout <> SOME Time.zeroTime
blanchet@50898
   434
blanchet@50898
   435
    fun isar_proof_of () =
blanchet@50898
   436
      let
blanchet@50898
   437
        val atp_proof =
blanchet@50898
   438
          atp_proof
blanchet@50898
   439
          |> clean_up_atp_proof_dependencies
blanchet@50898
   440
          |> nasty_atp_proof pool
blanchet@50898
   441
          |> map_term_names_in_atp_proof repair_name
blanchet@53287
   442
          |> map (decode_line ctxt lifted sym_tab)
blanchet@51920
   443
          |> repair_waldmeister_endgame
blanchet@50898
   444
          |> rpair [] |-> fold_rev (add_line fact_names)
blanchet@50898
   445
          |> rpair [] |-> fold_rev add_nontrivial_line
blanchet@50898
   446
          |> rpair (0, [])
blanchet@53262
   447
          |-> fold_rev (add_desired_line fact_names)
blanchet@50898
   448
          |> snd
blanchet@50898
   449
        val conj_name = conjecture_prefix ^ string_of_int (length hyp_ts)
blanchet@50898
   450
        val conjs =
blanchet@51025
   451
          atp_proof |> map_filter
blanchet@52338
   452
            (fn (name as (_, ss), _, _, _, []) =>
blanchet@51025
   453
                if member (op =) ss conj_name then SOME name else NONE
blanchet@51025
   454
              | _ => NONE)
blanchet@51025
   455
        val assms =
blanchet@51025
   456
          atp_proof |> map_filter
blanchet@52338
   457
            (fn (name as (_, ss), _, _, _, []) =>
blanchet@51028
   458
                (case resolve_conjecture ss of
blanchet@51028
   459
                   [j] =>
blanchet@51028
   460
                   if j = length hyp_ts then NONE
blanchet@53135
   461
                   else SOME (raw_label_of_name name, nth hyp_ts j)
blanchet@51028
   462
                 | _ => NONE)
blanchet@51025
   463
              | _ => NONE)
blanchet@52349
   464
        val bot = atp_proof |> List.last |> #1
blanchet@52282
   465
        val refute_graph =
blanchet@52349
   466
          atp_proof
blanchet@52349
   467
          |> map (fn (name, _, _, _, from) => (from, name))
blanchet@52349
   468
          |> make_refute_graph bot
blanchet@52349
   469
          |> fold (Atom_Graph.default_node o rpair ()) conjs
blanchet@52282
   470
        val axioms = axioms_of_refute_graph refute_graph conjs
blanchet@52282
   471
        val tainted = tainted_atoms_of_refute_graph refute_graph conjs
blanchet@52293
   472
        val is_clause_tainted = exists (member (op =) tainted)
blanchet@51691
   473
        val steps =
blanchet@50898
   474
          Symtab.empty
blanchet@52338
   475
          |> fold (fn (name as (s, _), role, t, rule, _) =>
blanchet@51691
   476
                      Symtab.update_new (s, (rule,
blanchet@52293
   477
                        t |> (if is_clause_tainted [name] then
blanchet@51920
   478
                                s_maybe_not role
blanchet@51691
   479
                                #> fold exists_of (map Var (Term.add_vars t []))
blanchet@51691
   480
                              else
blanchet@51691
   481
                                I))))
blanchet@50898
   482
                  atp_proof
blanchet@52285
   483
        fun is_clause_skolemize_rule [(s, _)] =
blanchet@51691
   484
            Option.map (is_skolemize_rule o fst) (Symtab.lookup steps s) =
blanchet@51691
   485
            SOME true
blanchet@51691
   486
          | is_clause_skolemize_rule _ = false
blanchet@51685
   487
        (* The assumptions and conjecture are "prop"s; the other formulas are
blanchet@51685
   488
           "bool"s. *)
smolkas@52316
   489
        fun prop_of_clause [(s, ss)] =
blanchet@51031
   490
            (case resolve_conjecture ss of
blanchet@51031
   491
               [j] => if j = length hyp_ts then concl_t else nth hyp_ts j
blanchet@51691
   492
             | _ => the_default ("", @{term False}) (Symtab.lookup steps s)
blanchet@51691
   493
                    |> snd |> HOLogic.mk_Trueprop |> close_form)
blanchet@51031
   494
          | prop_of_clause names =
blanchet@51691
   495
            let
blanchet@51691
   496
              val lits = map snd (map_filter (Symtab.lookup steps o fst) names)
blanchet@51691
   497
            in
blanchet@51033
   498
              case List.partition (can HOLogic.dest_not) lits of
blanchet@51033
   499
                (negs as _ :: _, pos as _ :: _) =>
blanchet@52349
   500
                s_imp (Library.foldr1 s_conj (map HOLogic.dest_not negs),
blanchet@52349
   501
                       Library.foldr1 s_disj pos)
blanchet@51033
   502
              | _ => fold (curry s_disj) lits @{term False}
blanchet@51033
   503
            end
blanchet@51031
   504
            |> HOLogic.mk_Trueprop |> close_form
smolkas@52316
   505
        fun isar_proof_of_direct_proof infs =
smolkas@52316
   506
          let
smolkas@52316
   507
            fun maybe_show outer c =
smolkas@52316
   508
              (outer andalso length c = 1 andalso subset (op =) (c, conjs))
smolkas@52316
   509
              ? cons Show
smolkas@52316
   510
            val is_fixed = Variable.is_declared ctxt orf can Name.dest_skolem
smolkas@52316
   511
            fun skolems_of t =
smolkas@52316
   512
              Term.add_frees t [] |> filter_out (is_fixed o fst) |> rev
blanchet@53113
   513
            fun do_steps outer predecessor accum [] =
blanchet@53113
   514
                accum
blanchet@53113
   515
                |> (if tainted = [] then
smolkas@53590
   516
                      cons (Prove (if outer then [Show] else [],
smolkas@53591
   517
                                   Fix [], no_label, concl_t, [],
smolkas@53591
   518
                                   By_Metis ([the predecessor], [])))
blanchet@53113
   519
                    else
blanchet@53113
   520
                      I)
blanchet@53113
   521
                |> rev
smolkas@52316
   522
              | do_steps outer _ accum (Have (gamma, c) :: infs) =
blanchet@51691
   523
                let
blanchet@52285
   524
                  val l = label_of_clause c
blanchet@52285
   525
                  val t = prop_of_clause c
blanchet@52285
   526
                  val by =
smolkas@53591
   527
                    By_Metis
smolkas@53591
   528
                      (fold (add_fact_of_dependencies fact_names) gamma no_facts)
smolkas@53591
   529
                  fun prove sub by =
smolkas@53591
   530
                    Prove (maybe_show outer c [], Fix [], l, t, sub, by)
blanchet@53113
   531
                  fun do_rest l step =
blanchet@53113
   532
                    do_steps outer (SOME l) (step :: accum) infs
blanchet@52285
   533
                in
blanchet@52293
   534
                  if is_clause_tainted c then
blanchet@52286
   535
                    case gamma of
blanchet@52286
   536
                      [g] =>
blanchet@52293
   537
                      if is_clause_skolemize_rule g andalso
blanchet@52293
   538
                         is_clause_tainted g then
blanchet@52286
   539
                        let
smolkas@52315
   540
                          val subproof =
smolkas@52316
   541
                            Proof (Fix (skolems_of (prop_of_clause g)),
smolkas@52316
   542
                                   Assume [], rev accum)
blanchet@52286
   543
                        in
smolkas@52316
   544
                          do_steps outer (SOME l)
smolkas@53591
   545
                              [prove [subproof] (By_Metis no_facts)] []
blanchet@52286
   546
                        end
blanchet@52286
   547
                      else
smolkas@53591
   548
                        do_rest l (prove [] by)
smolkas@53591
   549
                    | _ => do_rest l (prove [] by)
blanchet@52285
   550
                  else
blanchet@52286
   551
                    if is_clause_skolemize_rule c then
smolkas@53591
   552
                      do_rest l (Prove ([], Fix (skolems_of t), l, t, [], by))
blanchet@52286
   553
                    else
smolkas@53591
   554
                      do_rest l (prove [] by)
blanchet@52285
   555
                end
smolkas@52316
   556
              | do_steps outer predecessor accum (Cases cases :: infs) =
blanchet@52285
   557
                let
blanchet@52285
   558
                  fun do_case (c, infs) =
blanchet@53113
   559
                    do_proof false [] [(label_of_clause c, prop_of_clause c)]
blanchet@53113
   560
                             infs
blanchet@52285
   561
                  val c = succedent_of_cases cases
smolkas@52315
   562
                  val l = label_of_clause c
smolkas@52316
   563
                  val t = prop_of_clause c
smolkas@52316
   564
                  val step =
smolkas@53590
   565
                    Prove (maybe_show outer c [], Fix [], l, t,
smolkas@53591
   566
                      map do_case cases, By_Metis (the_list predecessor, []))
blanchet@52286
   567
                in
smolkas@52316
   568
                  do_steps outer (SOME l) (step :: accum) infs
blanchet@52286
   569
                end
smolkas@52316
   570
            and do_proof outer fix assms infs =
smolkas@52316
   571
              Proof (Fix fix, Assume assms, do_steps outer NONE [] infs)
smolkas@52316
   572
          in
smolkas@52316
   573
            do_proof true params assms infs
smolkas@52316
   574
          end
smolkas@52316
   575
smolkas@53729
   576
        (* 60 seconds seems like a good interpreation of "no timeout" *)
smolkas@53729
   577
        val preplay_timeout = preplay_timeout |> the_default (seconds 60.0)
smolkas@53729
   578
blanchet@52878
   579
        val clean_up_labels_in_proof =
blanchet@52302
   580
          chain_direct_proof
blanchet@52302
   581
          #> kill_useless_labels_in_proof
blanchet@52302
   582
          #> relabel_proof
smolkas@53693
   583
        val (preplay_interface as { overall_preplay_stats, ... }, isar_proof) =
blanchet@52282
   584
          refute_graph
blanchet@52213
   585
          |> redirect_graph axioms tainted bot
smolkas@52316
   586
          |> isar_proof_of_direct_proof
smolkas@53693
   587
          |> relabel_proof_canonically
smolkas@53693
   588
          |> `(proof_preplay_interface debug ctxt type_enc lam_trans do_preplay
smolkas@53693
   589
               preplay_timeout preplay_trace)
smolkas@53763
   590
        val ((preplay_time, preplay_fail), isar_proof) =
smolkas@53693
   591
          isar_proof
smolkas@53693
   592
          |> compress_proof
smolkas@53693
   593
               (if isar_proofs = SOME true then isar_compress else 1000.0)
smolkas@53693
   594
               (if isar_proofs = SOME true then isar_compress_degree else 100)
smolkas@53693
   595
               merge_timeout_slack preplay_interface
smolkas@53769
   596
          |> isar_try0 ? try0 preplay_timeout preplay_interface
smolkas@53769
   597
          |> minimize_dependencies_and_remove_unrefed_steps isar_minimize
smolkas@53769
   598
               preplay_interface
smolkas@53763
   599
          |> `overall_preplay_stats
smolkas@53763
   600
          ||> clean_up_labels_in_proof
smolkas@53763
   601
        val isar_text = string_of_proof ctxt type_enc lam_trans subgoal
smolkas@53763
   602
          subgoal_count isar_proof
blanchet@50898
   603
      in
blanchet@50933
   604
        case isar_text of
blanchet@50898
   605
          "" =>
blanchet@52327
   606
          if isar_proofs = SOME true then
blanchet@51686
   607
            "\nNo structured proof available (proof too simple)."
blanchet@50898
   608
          else
blanchet@50898
   609
            ""
blanchet@50898
   610
        | _ =>
blanchet@51685
   611
          let
blanchet@51685
   612
            val msg =
blanchet@52340
   613
              (if verbose then
blanchet@52340
   614
                let
smolkas@53729
   615
                  val num_steps = add_proof_steps (steps_of_proof isar_proof) 0
blanchet@52340
   616
                in [string_of_int num_steps ^ " step" ^ plural_s num_steps] end
blanchet@52340
   617
               else
blanchet@52340
   618
                 []) @
smolkas@53693
   619
              (if do_preplay then
smolkas@51939
   620
                [(if preplay_fail then "may fail, " else "") ^
smolkas@53693
   621
                   string_of_preplay_time preplay_time]
blanchet@51685
   622
               else
blanchet@51685
   623
                 [])
smolkas@51292
   624
          in
blanchet@52878
   625
            "\n\nStructured proof"
blanchet@52878
   626
              ^ (commas msg |> not (null msg) ? enclose " (" ")")
wenzelm@53834
   627
              ^ ":\n" ^
wenzelm@54189
   628
              Active.sendback_markup [Markup.padding_command] isar_text
smolkas@51292
   629
          end
blanchet@50898
   630
      end
blanchet@50898
   631
    val isar_proof =
blanchet@50898
   632
      if debug then
blanchet@50898
   633
        isar_proof_of ()
blanchet@50898
   634
      else case try isar_proof_of () of
blanchet@50898
   635
        SOME s => s
blanchet@52327
   636
      | NONE => if isar_proofs = SOME true then
blanchet@50898
   637
                  "\nWarning: The Isar proof construction failed."
blanchet@50898
   638
                else
blanchet@50898
   639
                  ""
blanchet@50898
   640
  in one_line_proof ^ isar_proof end
blanchet@50898
   641
blanchet@52324
   642
fun isar_proof_would_be_a_good_idea preplay =
blanchet@52324
   643
  case preplay of
blanchet@52324
   644
    Played (reconstr, _) => reconstr = SMT
blanchet@52352
   645
  | Trust_Playable _ => true
blanchet@52324
   646
  | Failed_to_Play _ => true
blanchet@52324
   647
wenzelm@54189
   648
fun proof_text ctxt isar_proofs isar_params num_chained
blanchet@50898
   649
               (one_line_params as (preplay, _, _, _, _, _)) =
blanchet@52327
   650
  (if isar_proofs = SOME true orelse
blanchet@52327
   651
      (isar_proofs = NONE andalso isar_proof_would_be_a_good_idea preplay) then
wenzelm@54189
   652
     isar_proof_text ctxt isar_proofs isar_params
blanchet@50898
   653
   else
wenzelm@54189
   654
     one_line_proof_text num_chained) one_line_params
blanchet@50898
   655
blanchet@50898
   656
end;