src/HOL/Tools/Sledgehammer/sledgehammer_reconstruct.ML
author smolkas
Wed, 26 Jun 2013 18:26:00 +0200
changeset 52454 b528a975b256
parent 52453 2cba5906d836
child 52555 6811291d1869
permissions -rw-r--r--
tuned: cleaned up data structure
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_reconstruct.ML
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     3
    Author:     Steffen Juilf Smolka, TU Muenchen
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     4
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
     5
Isar proof reconstruction from ATP proofs.
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     6
*)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     7
52374
blanchet
parents: 52369
diff changeset
     8
signature SLEDGEHAMMER_RECONSTRUCT =
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     9
sig
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    10
  type 'a proof = 'a ATP_Proof.proof
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    11
  type stature = ATP_Problem_Generate.stature
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    12
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    13
  datatype reconstructor =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    14
    Metis of string * string |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    15
    SMT
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    16
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    17
  datatype play =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    18
    Played of reconstructor * Time.time |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    19
    Trust_Playable of reconstructor * Time.time option |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    20
    Failed_to_Play of reconstructor
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    21
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    22
  type minimize_command = string list -> string
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    23
  type one_line_params =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    24
    play * string * (string * stature) list * minimize_command * int * int
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    25
  type isar_params =
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51741
diff changeset
    26
    bool * bool * Time.time option * bool * real * string Symtab.table
52150
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
    27
    * (string * stature) list vector * (string * term) list * int Symtab.table
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
    28
    * string proof * thm
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    29
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    30
  val smtN : string
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
    31
  val string_of_reconstructor : reconstructor -> string
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    32
  val lam_trans_of_atp_proof : string proof -> string -> string
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    33
  val is_typed_helper_used_in_atp_proof : string proof -> bool
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    34
  val used_facts_in_atp_proof :
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    35
    Proof.context -> (string * stature) list vector -> string proof ->
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    36
    (string * stature) list
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    37
  val used_facts_in_unsound_atp_proof :
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    38
    Proof.context -> (string * stature) list vector -> 'a proof ->
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    39
    string list option
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    40
  val one_line_proof_text : int -> one_line_params -> string
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    41
  val isar_proof_text :
51190
2654b3965c8d made "isar_proofs" a 3-way option, to provide a way to totally disable isar_proofs if desired
blanchet
parents: 51187
diff changeset
    42
    Proof.context -> bool option -> isar_params -> one_line_params -> string
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    43
  val proof_text :
51190
2654b3965c8d made "isar_proofs" a 3-way option, to provide a way to totally disable isar_proofs if desired
blanchet
parents: 51187
diff changeset
    44
    Proof.context -> bool option -> isar_params -> int -> one_line_params
2654b3965c8d made "isar_proofs" a 3-way option, to provide a way to totally disable isar_proofs if desired
blanchet
parents: 51187
diff changeset
    45
    -> string
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    46
end;
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    47
52374
blanchet
parents: 52369
diff changeset
    48
structure Sledgehammer_Reconstruct : SLEDGEHAMMER_RECONSTRUCT =
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    49
struct
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    50
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    51
open ATP_Util
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    52
open ATP_Problem
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    53
open ATP_Proof
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    54
open ATP_Problem_Generate
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    55
open ATP_Proof_Reconstruct
49918
cf441f4a358b renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents: 49917
diff changeset
    56
open Sledgehammer_Util
50264
a9ec48b98734 renamed sledgehammer_isar_reconstruct to sledgehammer_proof
smolkas
parents: 50262
diff changeset
    57
open Sledgehammer_Proof
50258
1c708d7728c7 put annotate in own structure
smolkas
parents: 50257
diff changeset
    58
open Sledgehammer_Annotate
51130
76d68444cd59 renamed sledgehammer_shrink to sledgehammer_compress
smolkas
parents: 51129
diff changeset
    59
open Sledgehammer_Compress
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    60
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    61
structure String_Redirect = ATP_Proof_Redirect(
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    62
  type key = step_name
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    63
  val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s')
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    64
  val string_of = fst)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    65
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    66
open String_Redirect
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    67
49916
412346127bfa fixed theorem lookup code in Isar proof reconstruction
blanchet
parents: 49915
diff changeset
    68
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    69
(** reconstructors **)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    70
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    71
datatype reconstructor =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    72
  Metis of string * string |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    73
  SMT
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    74
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    75
datatype play =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    76
  Played of reconstructor * Time.time |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    77
  Trust_Playable of reconstructor * Time.time option |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    78
  Failed_to_Play of reconstructor
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    79
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    80
val smtN = "smt"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    81
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
    82
fun string_of_reconstructor (Metis (type_enc, lam_trans)) =
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    83
    metis_call type_enc lam_trans
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
    84
  | string_of_reconstructor SMT = smtN
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    85
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    86
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    87
(** fact extraction from ATP proofs **)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    88
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    89
fun find_first_in_list_vector vec key =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    90
  Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    91
                 | (_, value) => value) NONE vec
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    92
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    93
val unprefix_fact_number = space_implode "_" o tl o space_explode "_"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    94
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    95
fun resolve_one_named_fact fact_names s =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    96
  case try (unprefix fact_prefix) s of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    97
    SOME s' =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    98
    let val s' = s' |> unprefix_fact_number |> unascii_of in
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    99
      s' |> find_first_in_list_vector fact_names |> Option.map (pair s')
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   100
    end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   101
  | NONE => NONE
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   102
fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   103
fun is_fact fact_names = not o null o resolve_fact fact_names
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   104
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   105
fun resolve_one_named_conjecture s =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   106
  case try (unprefix conjecture_prefix) s of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   107
    SOME s' => Int.fromString s'
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   108
  | NONE => NONE
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   109
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   110
val resolve_conjecture = map_filter resolve_one_named_conjecture
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   111
val is_conjecture = not o null o resolve_conjecture
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   112
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   113
val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   114
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   115
(* overapproximation (good enough) *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   116
fun is_lam_lifted s =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   117
  String.isPrefix fact_prefix s andalso
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   118
  String.isSubstring ascii_of_lam_fact_prefix s
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   119
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   120
val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   121
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   122
fun is_axiom_used_in_proof pred =
51202
3278cd5de3b1 added case taken out by mistake
blanchet
parents: 51201
diff changeset
   123
  exists (fn ((_, ss), _, _, _, []) => exists pred ss | _ => false)
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   124
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
   125
fun lam_trans_of_atp_proof atp_proof default =
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   126
  case (is_axiom_used_in_proof is_combinator_def atp_proof,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   127
        is_axiom_used_in_proof is_lam_lifted atp_proof) of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   128
    (false, false) => default
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   129
  | (false, true) => liftingN
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   130
(*  | (true, true) => combs_and_liftingN -- not supported by "metis" *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   131
  | (true, _) => combsN
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   132
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   133
val is_typed_helper_name =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   134
  String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix
51031
63d71b247323 more robustness in Isar proof reconstruction (cf. bug report by Ondrej)
blanchet
parents: 51026
diff changeset
   135
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   136
fun is_typed_helper_used_in_atp_proof atp_proof =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   137
  is_axiom_used_in_proof is_typed_helper_name atp_proof
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   138
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   139
fun add_non_rec_defs fact_names accum =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   140
  Vector.foldl (fn (facts, facts') =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   141
      union (op =) (filter (fn (_, (_, status)) => status = Non_Rec_Def) facts)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   142
            facts')
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   143
    accum fact_names
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   144
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   145
val isa_ext = Thm.get_name_hint @{thm ext}
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   146
val isa_short_ext = Long_Name.base_name isa_ext
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   147
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   148
fun ext_name ctxt =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   149
  if Thm.eq_thm_prop (@{thm ext},
51026
48e82e199df1 tuned indent
blanchet
parents: 50924
diff changeset
   150
       singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   151
    isa_short_ext
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   152
  else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   153
    isa_ext
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   154
50675
e3e707c8ac57 keep E's and Vampire's skolemization steps
blanchet
parents: 50674
diff changeset
   155
val leo2_extcnf_equal_neg_rule = "extcnf_equal_neg"
e3e707c8ac57 keep E's and Vampire's skolemization steps
blanchet
parents: 50674
diff changeset
   156
val leo2_unfold_def_rule = "unfold_def"
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   157
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   158
fun add_fact ctxt fact_names ((_, ss), _, _, rule, deps) =
51198
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   159
  (if rule = leo2_extcnf_equal_neg_rule then
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   160
     insert (op =) (ext_name ctxt, (Global, General))
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   161
   else if rule = leo2_unfold_def_rule then
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   162
     (* LEO 1.3.3 does not record definitions properly, leading to missing
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   163
        dependencies in the TSTP proof. Remove the next line once this is
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   164
        fixed. *)
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   165
     add_non_rec_defs fact_names
52077
788b27dfaefa parse agsyHOL proofs (as unsat cores)
blanchet
parents: 52034
diff changeset
   166
   else if rule = agsyhol_coreN orelse rule = satallax_coreN then
51198
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   167
     (fn [] =>
52077
788b27dfaefa parse agsyHOL proofs (as unsat cores)
blanchet
parents: 52034
diff changeset
   168
         (* agsyHOL and Satallax don't include definitions in their
788b27dfaefa parse agsyHOL proofs (as unsat cores)
blanchet
parents: 52034
diff changeset
   169
            unsatisfiable cores, so we assume the worst and include them all
788b27dfaefa parse agsyHOL proofs (as unsat cores)
blanchet
parents: 52034
diff changeset
   170
            here. *)
51198
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   171
         [(ext_name ctxt, (Global, General))] |> add_non_rec_defs fact_names
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   172
       | facts => facts)
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   173
   else
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   174
     I)
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   175
  #> (if null deps then union (op =) (resolve_fact fact_names ss) else I)
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   176
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   177
fun used_facts_in_atp_proof ctxt fact_names atp_proof =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   178
  if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   179
  else fold (add_fact ctxt fact_names) atp_proof []
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   180
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   181
fun used_facts_in_unsound_atp_proof _ _ [] = NONE
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   182
  | used_facts_in_unsound_atp_proof ctxt fact_names atp_proof =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   183
    let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   184
      if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   185
         not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   186
        SOME (map fst used_facts)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   187
      else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   188
        NONE
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   189
    end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   190
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   191
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   192
(** one-liner reconstructor proofs **)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   193
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   194
fun show_time NONE = ""
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
   195
  | show_time (SOME ext_time) = " (" ^ string_of_ext_time ext_time ^ ")"
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   196
49986
90e7be285b49 took out "using only ..." comments in Sledgehammer generated metis/smt calls, until these can be generated soundly
blanchet
parents: 49983
diff changeset
   197
(* FIXME: Various bugs, esp. with "unfolding"
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   198
fun unusing_chained_facts _ 0 = ""
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   199
  | unusing_chained_facts used_chaineds num_chained =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   200
    if length used_chaineds = num_chained then ""
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   201
    else if null used_chaineds then "(* using no facts *) "
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   202
    else "(* using only " ^ space_implode " " used_chaineds ^ " *) "
49986
90e7be285b49 took out "using only ..." comments in Sledgehammer generated metis/smt calls, until these can be generated soundly
blanchet
parents: 49983
diff changeset
   203
*)
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   204
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   205
fun apply_on_subgoal _ 1 = "by "
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   206
  | apply_on_subgoal 1 _ = "apply "
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   207
  | apply_on_subgoal i n =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   208
    "prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal 1 n
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   209
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   210
fun using_labels [] = ""
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   211
  | using_labels ls =
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
   212
    "using " ^ space_implode " " (map string_of_label ls) ^ " "
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   213
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   214
fun command_call name [] =
50239
fb579401dc26 tuned signature;
wenzelm
parents: 50218
diff changeset
   215
    name |> not (Symbol_Pos.is_identifier name) ? enclose "(" ")"
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   216
  | command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   217
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   218
fun reconstructor_command reconstr i n used_chaineds num_chained (ls, ss) =
49986
90e7be285b49 took out "using only ..." comments in Sledgehammer generated metis/smt calls, until these can be generated soundly
blanchet
parents: 49983
diff changeset
   219
  (* unusing_chained_facts used_chaineds num_chained ^ *)
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   220
  using_labels ls ^ apply_on_subgoal i n ^
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
   221
  command_call (string_of_reconstructor reconstr) ss
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   222
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   223
fun try_command_line banner time command =
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50410
diff changeset
   224
  banner ^ ": " ^ Active.sendback_markup command ^ show_time time ^ "."
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   225
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   226
fun minimize_line _ [] = ""
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   227
  | minimize_line minimize_command ss =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   228
    case minimize_command ss of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   229
      "" => ""
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   230
    | command =>
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50410
diff changeset
   231
      "\nTo minimize: " ^ Active.sendback_markup command ^ "."
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   232
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   233
fun split_used_facts facts =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   234
  facts |> List.partition (fn (_, (sc, _)) => sc = Chained)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   235
        |> pairself (sort_distinct (string_ord o pairself fst))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   236
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   237
type minimize_command = string list -> string
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   238
type one_line_params =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   239
  play * string * (string * stature) list * minimize_command * int * int
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   240
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   241
fun one_line_proof_text num_chained
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   242
        (preplay, banner, used_facts, minimize_command, subgoal,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   243
         subgoal_count) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   244
  let
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   245
    val (chained, extra) = split_used_facts used_facts
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   246
    val (failed, reconstr, ext_time) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   247
      case preplay of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   248
        Played (reconstr, time) => (false, reconstr, (SOME (false, time)))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   249
      | Trust_Playable (reconstr, time) =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   250
        (false, reconstr,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   251
         case time of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   252
           NONE => NONE
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   253
         | SOME time =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   254
           if time = Time.zeroTime then NONE else SOME (true, time))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   255
      | Failed_to_Play reconstr => (true, reconstr, NONE)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   256
    val try_line =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   257
      ([], map fst extra)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   258
      |> reconstructor_command reconstr subgoal subgoal_count (map fst chained)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   259
                               num_chained
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   260
      |> (if failed then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   261
            enclose "One-line proof reconstruction failed: "
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   262
                     ".\n(Invoking \"sledgehammer\" with \"[strict]\" might \
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   263
                     \solve this.)"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   264
          else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   265
            try_command_line banner ext_time)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   266
  in try_line ^ minimize_line minimize_command (map fst (extra @ chained)) end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   267
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   268
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   269
(** Isar proof construction and manipulation **)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   270
50017
d9c1b11a78d2 avoid name clashes
blanchet
parents: 50016
diff changeset
   271
val assume_prefix = "a"
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   272
val have_prefix = "f"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   273
val raw_prefix = "x"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   274
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
   275
fun raw_label_of_name (num, ss) =
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   276
  case resolve_conjecture ss of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   277
    [j] => (conjecture_prefix, j)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   278
  | _ => (raw_prefix ^ ascii_of num, 0)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   279
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
   280
fun label_of_clause [name] = raw_label_of_name name
51976
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   281
  | label_of_clause c =
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
   282
    (space_implode "___" (map (fst o raw_label_of_name) c), 0)
50005
e9a9bff107da handle non-unit clauses gracefully
blanchet
parents: 50004
diff changeset
   283
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
   284
fun add_fact_of_dependencies fact_names (names as [(_, ss)]) =
50005
e9a9bff107da handle non-unit clauses gracefully
blanchet
parents: 50004
diff changeset
   285
    if is_fact fact_names ss then
e9a9bff107da handle non-unit clauses gracefully
blanchet
parents: 50004
diff changeset
   286
      apsnd (union (op =) (map fst (resolve_fact fact_names ss)))
e9a9bff107da handle non-unit clauses gracefully
blanchet
parents: 50004
diff changeset
   287
    else
e9a9bff107da handle non-unit clauses gracefully
blanchet
parents: 50004
diff changeset
   288
      apfst (insert (op =) (label_of_clause names))
52034
11b48e7a4e7e correctly 'repair' the monomorphization context for SMT solvers from Sledgehammer
blanchet
parents: 52031
diff changeset
   289
  | add_fact_of_dependencies _ names =
50005
e9a9bff107da handle non-unit clauses gracefully
blanchet
parents: 50004
diff changeset
   290
    apfst (insert (op =) (label_of_clause names))
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   291
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   292
fun repair_name "$true" = "c_True"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   293
  | repair_name "$false" = "c_False"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   294
  | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   295
  | repair_name s =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   296
    if is_tptp_equal s orelse
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   297
       (* seen in Vampire proofs *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   298
       (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   299
      tptp_equal
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   300
    else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   301
      s
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   302
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   303
fun infer_formula_types ctxt =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   304
  Type.constraint HOLogic.boolT
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   305
  #> Syntax.check_term
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   306
         (Proof_Context.set_mode Proof_Context.mode_schematic ctxt)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   307
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   308
val combinator_table =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   309
  [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def [abs_def]}),
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   310
   (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def [abs_def]}),
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   311
   (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def [abs_def]}),
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   312
   (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def [abs_def]}),
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   313
   (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def [abs_def]})]
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   314
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   315
fun uncombine_term thy =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   316
  let
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   317
    fun aux (t1 $ t2) = betapply (pairself aux (t1, t2))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   318
      | aux (Abs (s, T, t')) = Abs (s, T, aux t')
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   319
      | aux (t as Const (x as (s, _))) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   320
        (case AList.lookup (op =) combinator_table s of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   321
           SOME thm => thm |> prop_of |> specialize_type thy x
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   322
                           |> Logic.dest_equals |> snd
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   323
         | NONE => t)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   324
      | aux t = t
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   325
  in aux end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   326
52150
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   327
fun unlift_term lifted =
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   328
  map_aterms (fn t as Const (s, _) =>
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   329
                 if String.isPrefix lam_lifted_prefix s then
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   330
                   case AList.lookup (op =) lifted s of
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   331
                     SOME t =>
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   332
                     (* FIXME: do something about the types *)
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   333
                     unlift_term lifted t
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   334
                   | NONE => t
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   335
                 else
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   336
                   t
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   337
               | t => t)
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   338
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   339
fun decode_line ctxt lifted sym_tab (name, role, u, rule, deps) =
51198
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   340
  let
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   341
    val thy = Proof_Context.theory_of ctxt
52125
ac7830871177 improved handling of free variables' types in Isar proofs
blanchet
parents: 52077
diff changeset
   342
    val t =
ac7830871177 improved handling of free variables' types in Isar proofs
blanchet
parents: 52077
diff changeset
   343
      u |> prop_of_atp ctxt true sym_tab
ac7830871177 improved handling of free variables' types in Isar proofs
blanchet
parents: 52077
diff changeset
   344
        |> uncombine_term thy
52150
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   345
        |> unlift_term lifted
52125
ac7830871177 improved handling of free variables' types in Isar proofs
blanchet
parents: 52077
diff changeset
   346
        |> infer_formula_types ctxt
ac7830871177 improved handling of free variables' types in Isar proofs
blanchet
parents: 52077
diff changeset
   347
  in (name, role, t, rule, deps) end
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   348
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   349
fun replace_one_dependency (old, new) dep =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   350
  if is_same_atp_step dep old then new else [dep]
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   351
fun replace_dependencies_in_line p (name, role, t, rule, deps) =
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   352
  (name, role, t, rule, fold (union (op =) o replace_one_dependency p) deps [])
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   353
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   354
(* No "real" literals means only type information (tfree_tcs, clsrel, or
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   355
   clsarity). *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   356
fun is_only_type_information t = t aconv @{term True}
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   357
50905
db99fcf69761 more improvements to Isar proof reconstructions
blanchet
parents: 50705
diff changeset
   358
fun s_maybe_not role = role <> Conjecture ? s_not
db99fcf69761 more improvements to Isar proof reconstructions
blanchet
parents: 50705
diff changeset
   359
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   360
fun is_same_inference (role, t) (_, role', t', _, _) =
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   361
  s_maybe_not role t aconv s_maybe_not role' t'
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   362
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   363
(* Discard facts; consolidate adjacent lines that prove the same formula, since
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   364
   they differ only in type information.*)
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   365
fun add_line fact_names (name as (_, ss), role, t, rule, []) lines =
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   366
    (* No dependencies: fact, conjecture, or (for Vampire) internal facts or
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   367
       definitions. *)
50905
db99fcf69761 more improvements to Isar proof reconstructions
blanchet
parents: 50705
diff changeset
   368
    if is_conjecture ss then
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   369
      (name, role, t, rule, []) :: lines
50905
db99fcf69761 more improvements to Isar proof reconstructions
blanchet
parents: 50705
diff changeset
   370
    else if is_fact fact_names ss then
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   371
      (* Facts are not proof lines. *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   372
      if is_only_type_information t then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   373
        map (replace_dependencies_in_line (name, [])) lines
50905
db99fcf69761 more improvements to Isar proof reconstructions
blanchet
parents: 50705
diff changeset
   374
      else
db99fcf69761 more improvements to Isar proof reconstructions
blanchet
parents: 50705
diff changeset
   375
        lines
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   376
    else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   377
      map (replace_dependencies_in_line (name, [])) lines
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   378
  | add_line _ (line as (name, role, t, _, _)) lines =
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   379
    (* Type information will be deleted later; skip repetition test. *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   380
    if is_only_type_information t then
50675
e3e707c8ac57 keep E's and Vampire's skolemization steps
blanchet
parents: 50674
diff changeset
   381
      line :: lines
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   382
    (* Is there a repetition? If so, replace later line by earlier one. *)
50905
db99fcf69761 more improvements to Isar proof reconstructions
blanchet
parents: 50705
diff changeset
   383
    else case take_prefix (not o is_same_inference (role, t)) lines of
50675
e3e707c8ac57 keep E's and Vampire's skolemization steps
blanchet
parents: 50674
diff changeset
   384
      (_, []) => line :: lines
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   385
    | (pre, (name', _, _, _, _) :: post) =>
50675
e3e707c8ac57 keep E's and Vampire's skolemization steps
blanchet
parents: 50674
diff changeset
   386
      line :: pre @ map (replace_dependencies_in_line (name', [name])) post
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   387
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   388
val waldmeister_conjecture_num = "1.0.0.0"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   389
51258
28b60ee75ef8 make SML/NJ happy;
wenzelm
parents: 51215
diff changeset
   390
fun repair_waldmeister_endgame arg =
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   391
  let
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   392
    fun do_tail (name, _, t, rule, deps) =
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   393
      (name, Negated_Conjecture, s_not t, rule, deps)
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   394
    fun do_body [] = []
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   395
      | do_body ((line as ((num, _), _, _, _, _)) :: lines) =
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   396
        if num = waldmeister_conjecture_num then map do_tail (line :: lines)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   397
        else line :: do_body lines
51258
28b60ee75ef8 make SML/NJ happy;
wenzelm
parents: 51215
diff changeset
   398
  in do_body arg end
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   399
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   400
(* Recursively delete empty lines (type information) from the proof. *)
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   401
fun add_nontrivial_line (line as (name, _, t, _, [])) lines =
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   402
    if is_only_type_information t then delete_dependency name lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   403
    else line :: lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   404
  | add_nontrivial_line line lines = line :: lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   405
and delete_dependency name lines =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   406
  fold_rev add_nontrivial_line
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   407
           (map (replace_dependencies_in_line (name, [])) lines) []
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   408
50675
e3e707c8ac57 keep E's and Vampire's skolemization steps
blanchet
parents: 50674
diff changeset
   409
val e_skolemize_rule = "skolemize"
e3e707c8ac57 keep E's and Vampire's skolemization steps
blanchet
parents: 50674
diff changeset
   410
val vampire_skolemisation_rule = "skolemisation"
e3e707c8ac57 keep E's and Vampire's skolemization steps
blanchet
parents: 50674
diff changeset
   411
50676
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   412
val is_skolemize_rule =
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   413
  member (op =) [e_skolemize_rule, vampire_skolemisation_rule]
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   414
52125
ac7830871177 improved handling of free variables' types in Isar proofs
blanchet
parents: 52077
diff changeset
   415
fun add_desired_line fact_names (name as (_, ss), role, t, rule, deps)
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   416
                     (j, lines) =
51198
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   417
  (j + 1,
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   418
   if is_fact fact_names ss orelse
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   419
      is_conjecture ss orelse
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   420
      is_skolemize_rule rule orelse
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   421
      (* the last line must be kept *)
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   422
      j = 0 orelse
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   423
      (not (is_only_type_information t) andalso
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   424
       null (Term.add_tvars t []) andalso
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   425
       length deps >= 2 andalso
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   426
       (* kill next to last line, which usually results in a trivial step *)
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   427
       j <> 1) then
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   428
     (name, role, t, rule, deps) :: lines  (* keep line *)
51198
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   429
   else
4dd63cf5bba5 got rid of rump support for Vampire definitions
blanchet
parents: 51194
diff changeset
   430
     map (replace_dependencies_in_line (name, deps)) lines)  (* drop line *)
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   431
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   432
val indent_size = 2
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   433
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
   434
fun string_of_proof ctxt type_enc lam_trans i n proof =
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   435
  let
52366
ff89424b5094 make use of show_type_emphasis instead of using hack; make sure global configurations don't affect proof script creation
smolkas
parents: 52196
diff changeset
   436
    val ctxt =
ff89424b5094 make use of show_type_emphasis instead of using hack; make sure global configurations don't affect proof script creation
smolkas
parents: 52196
diff changeset
   437
      (* make sure type constraint are actually printed *)
ff89424b5094 make use of show_type_emphasis instead of using hack; make sure global configurations don't affect proof script creation
smolkas
parents: 52196
diff changeset
   438
      ctxt |> Config.put show_markup false
ff89424b5094 make use of show_type_emphasis instead of using hack; make sure global configurations don't affect proof script creation
smolkas
parents: 52196
diff changeset
   439
      (* make sure only type constraints inserted by sh_annotate are printed *)
ff89424b5094 make use of show_type_emphasis instead of using hack; make sure global configurations don't affect proof script creation
smolkas
parents: 52196
diff changeset
   440
           |> Config.put Printer.show_type_emphasis false
ff89424b5094 make use of show_type_emphasis instead of using hack; make sure global configurations don't affect proof script creation
smolkas
parents: 52196
diff changeset
   441
           |> Config.put show_types false
ff89424b5094 make use of show_type_emphasis instead of using hack; make sure global configurations don't affect proof script creation
smolkas
parents: 52196
diff changeset
   442
           |> Config.put show_sorts false
ff89424b5094 make use of show_type_emphasis instead of using hack; make sure global configurations don't affect proof script creation
smolkas
parents: 52196
diff changeset
   443
           |> Config.put show_consts false
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   444
    val register_fixes = map Free #> fold Variable.auto_fixes
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   445
    fun add_suffix suffix (s, ctxt) = (s ^ suffix, ctxt)
51158
f432363eebf4 annotate obtains with types
blanchet
parents: 51156
diff changeset
   446
    fun of_indent ind = replicate_string (ind * indent_size) " "
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   447
    fun of_moreover ind = of_indent ind ^ "moreover\n"
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
   448
    fun of_label l = if l = no_label then "" else string_of_label l ^ ": "
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   449
    fun of_obtain qs nr =
51976
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   450
      (if nr > 1 orelse (nr = 1 andalso member (op =) qs Then) then
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   451
         "ultimately "
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   452
       else if nr=1 orelse member (op =) qs Then then
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   453
         "then "
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   454
       else
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   455
         "") ^ "obtain"
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   456
    fun of_show_have qs = if member (op =) qs Show then "show" else "have"
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   457
    fun of_thus_hence qs = if member (op =) qs Show then "thus" else "hence"
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   458
    fun of_prove qs nr =
51976
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   459
      if nr > 1 orelse (nr = 1 andalso member (op =) qs Then) then
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   460
        "ultimately " ^ of_show_have qs
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   461
      else if nr=1 orelse member (op =) qs Then then
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   462
        of_thus_hence qs
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   463
      else
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   464
        of_show_have qs
51741
blanchet
parents: 51258
diff changeset
   465
    fun add_term term (s, ctxt) =
52369
0b395800fdf0 uncheck terms before annotation to avoid awkward syntax
smolkas
parents: 52366
diff changeset
   466
      (s ^ (term
0b395800fdf0 uncheck terms before annotation to avoid awkward syntax
smolkas
parents: 52366
diff changeset
   467
            |> singleton (Syntax.uncheck_terms ctxt)
0b395800fdf0 uncheck terms before annotation to avoid awkward syntax
smolkas
parents: 52366
diff changeset
   468
            |> annotate_types ctxt
0b395800fdf0 uncheck terms before annotation to avoid awkward syntax
smolkas
parents: 52366
diff changeset
   469
            |> with_vanilla_print_mode (Syntax.unparse_term ctxt #> Pretty.string_of)
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   470
            |> simplify_spaces
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   471
            |> maybe_quote),
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   472
       ctxt |> Variable.auto_fixes term)
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   473
    val reconstr = Metis (type_enc, lam_trans)
51158
f432363eebf4 annotate obtains with types
blanchet
parents: 51156
diff changeset
   474
    fun of_metis ind options (ls, ss) =
f432363eebf4 annotate obtains with types
blanchet
parents: 51156
diff changeset
   475
      "\n" ^ of_indent (ind + 1) ^ options ^
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   476
      reconstructor_command reconstr 1 1 [] 0
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   477
          (ls |> sort_distinct (prod_ord string_ord int_ord),
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   478
           ss |> sort_distinct string_ord)
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   479
    fun of_free (s, T) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   480
      maybe_quote s ^ " :: " ^
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   481
      maybe_quote (simplify_spaces (with_vanilla_print_mode
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   482
        (Syntax.string_of_typ ctxt) T))
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   483
    fun add_frees xs (s, ctxt) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   484
      (s ^ space_implode " and " (map of_free xs), ctxt |> register_fixes xs)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   485
    fun add_fix _ [] = I
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   486
      | add_fix ind xs = add_suffix (of_indent ind ^ "fix ")
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   487
                        #> add_frees xs
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   488
                        #> add_suffix "\n"
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   489
    fun add_assm ind (l, t) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   490
      add_suffix (of_indent ind ^ "assume " ^ of_label l)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   491
      #> add_term t
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   492
      #> add_suffix "\n"
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   493
    fun add_assms ind assms = fold (add_assm ind) assms
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   494
    fun add_step_post ind l t facts options =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   495
      add_suffix (of_label l)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   496
      #> add_term t
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   497
      #> add_suffix (of_metis ind options facts ^ "\n")
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   498
    fun of_subproof ind ctxt proof =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   499
      let
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   500
        val ind = ind + 1
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   501
        val s = of_proof ind ctxt proof
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   502
        val prefix = "{ "
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   503
        val suffix = " }"
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   504
      in
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   505
        replicate_string (ind * indent_size - size prefix) " " ^ prefix ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   506
        String.extract (s, ind * indent_size,
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   507
                        SOME (size s - ind * indent_size - 1)) ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   508
        suffix ^ "\n"
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   509
      end
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   510
    and of_subproofs _ _ _ [] = ""
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   511
      | of_subproofs ind ctxt qs subproofs =
51976
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   512
        (if member (op =) qs Then then of_moreover ind else "") ^
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   513
        space_implode (of_moreover ind) (map (of_subproof ind ctxt) subproofs)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   514
    and add_step_pre ind qs subproofs (s, ctxt) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   515
      (s ^ of_subproofs ind ctxt qs subproofs ^ of_indent ind, ctxt)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   516
    and add_step ind (Let (t1, t2)) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   517
        add_suffix (of_indent ind ^ "let ")
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   518
        #> add_term t1
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   519
        #> add_suffix " = "
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   520
        #> add_term t2
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   521
        #> add_suffix "\n"
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   522
      | add_step ind (Prove (qs, Fix xs, l, t, subproofs, By_Metis facts)) =
52453
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   523
        (case xs of
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   524
          [] => (* have *)
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   525
            add_step_pre ind qs subproofs
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   526
            #> add_suffix (of_prove qs (length subproofs) ^ " ")
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   527
            #> add_step_post ind l t facts ""
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   528
        | _ => (* obtain *)
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   529
            add_step_pre ind qs subproofs
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   530
            #> add_suffix (of_obtain qs (length subproofs) ^ " ")
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   531
            #> add_frees xs
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   532
            #> add_suffix " where "
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   533
            (* The new skolemizer puts the arguments in the same order as the
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   534
               ATPs (E and Vampire -- but see also "atp_proof_reconstruct.ML"
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   535
               regarding Vampire). *)
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   536
            #> add_step_post ind l t facts
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   537
                 (if exists (fn (_, T) => length (binder_types T) > 1) xs then
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   538
                    "using [[metis_new_skolem]] "
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   539
                  else
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   540
                    ""))
51194
blanchet
parents: 51193
diff changeset
   541
    and add_steps ind = fold (add_step ind)
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   542
    and of_proof ind ctxt (Proof (Fix xs, Assume assms, steps)) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   543
      ("", ctxt)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   544
      |> add_fix ind xs
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   545
      |> add_assms ind assms
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   546
      |> add_steps ind steps
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   547
      |> fst
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   548
  in
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   549
    (* One-step proofs are pointless; better use the Metis one-liner
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   550
       directly. *)
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   551
    case proof of
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   552
      Proof (Fix [], Assume [], [Prove (_, Fix [], _, _, [], _)]) => ""
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   553
    | _ => (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   554
            of_indent 0 ^ "proof -\n" ^ of_proof 1 ctxt proof ^
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   555
            of_indent 0 ^ (if n <> 1 then "next" else "qed")
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   556
  end
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   557
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   558
val add_labels_of_proof =
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   559
  steps_of_proof #> fold_isar_steps
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   560
    (byline_of_step #> (fn SOME (By_Metis (ls, _)) => union (op =) ls
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   561
                         | _ => I))
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   562
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   563
fun kill_useless_labels_in_proof proof =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   564
  let
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51165
diff changeset
   565
    val used_ls = add_labels_of_proof proof []
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   566
    fun do_label l = if member (op =) used_ls l then l else no_label
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   567
    fun do_assms (Assume assms) = Assume (map (apfst do_label) assms)
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   568
    fun do_step (Prove (qs, xs, l, t, subproofs, by)) =
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   569
          Prove (qs, xs, do_label l, t, map do_proof subproofs, by)
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   570
      | do_step step = step
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   571
    and do_proof (Proof (fix, assms, steps)) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   572
          Proof (fix, do_assms assms, map do_step steps)
51128
0021ea861129 introduced subblock in isar_step datatype for conjecture herbrandization
smolkas
parents: 51031
diff changeset
   573
  in do_proof proof end
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   574
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
   575
fun prefix_of_depth n = replicate_string (n + 1)
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   576
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   577
val relabel_proof =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   578
  let
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   579
    fun fresh_label depth prefix (old as (l, subst, next)) =
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50671
diff changeset
   580
      if l = no_label then
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50671
diff changeset
   581
        old
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50671
diff changeset
   582
      else
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
   583
        let val l' = (prefix_of_depth depth prefix, next) in
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   584
          (l', (l, l') :: subst, next + 1)
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50671
diff changeset
   585
        end
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50671
diff changeset
   586
    fun do_facts subst =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50671
diff changeset
   587
      apfst (maps (the_list o AList.lookup (op =) subst))
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   588
    fun do_assm depth (l, t) (subst, next) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   589
      let
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   590
        val (l, subst, next) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   591
          (l, subst, next) |> fresh_label depth assume_prefix
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   592
      in
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   593
        ((l, t), (subst, next))
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   594
      end
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   595
    fun do_assms subst depth (Assume assms) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   596
      fold_map (do_assm depth) assms (subst, 1)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   597
      |> apfst Assume
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   598
      |> apsnd fst
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   599
    fun do_steps _ _ _ [] = []
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   600
      | do_steps subst depth next (Prove (qs, xs, l, t, sub, by) :: steps) =
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50671
diff changeset
   601
        let
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   602
          val (l, subst, next) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   603
            (l, subst, next) |> fresh_label depth have_prefix
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   604
          val sub = do_proofs subst depth sub
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   605
          val by = by |> do_byline subst depth
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   606
        in Prove (qs, xs, l, t, sub, by) :: do_steps subst depth next steps end
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   607
      | do_steps subst depth next (step :: steps) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   608
        step :: do_steps subst depth next steps
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   609
    and do_proof subst depth (Proof (fix, assms, steps)) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   610
      let val (assms, subst) = do_assms subst depth assms in
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   611
        Proof (fix, assms, do_steps subst depth 1 steps)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   612
      end
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   613
    and do_byline subst depth (By_Metis facts) =
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   614
      By_Metis (do_facts subst facts)
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   615
    and do_proofs subst depth = map (do_proof subst (depth + 1))
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   616
  in do_proof [] 0 end
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   617
50004
c96e8e40d789 several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents: 49994
diff changeset
   618
val chain_direct_proof =
c96e8e40d789 several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents: 49994
diff changeset
   619
  let
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51165
diff changeset
   620
    fun do_qs_lfs NONE lfs = ([], lfs)
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51165
diff changeset
   621
      | do_qs_lfs (SOME l0) lfs =
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51165
diff changeset
   622
        if member (op =) lfs l0 then ([Then], lfs |> remove (op =) l0)
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51165
diff changeset
   623
        else ([], lfs)
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   624
    fun chain_step lbl (Prove (qs, xs, l, t, subproofs, By_Metis (lfs, gfs))) =
52453
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   625
          let val (qs', lfs) = do_qs_lfs lbl lfs in
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   626
            Prove (qs' @ qs, xs, l, t, chain_proofs subproofs,
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   627
                   By_Metis (lfs, gfs))
52453
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   628
          end
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50671
diff changeset
   629
      | chain_step _ step = step
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   630
    and chain_steps _ [] = []
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   631
      | chain_steps (prev as SOME _) (i :: is) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   632
        chain_step prev i :: chain_steps (label_of_step i) is
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   633
      | chain_steps _ (i :: is) = i :: chain_steps (label_of_step i) is
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   634
    and chain_proof (Proof (fix, Assume assms, steps)) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   635
      Proof (fix, Assume assms,
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   636
             chain_steps (try (List.last #> fst) assms) steps)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   637
    and chain_proofs proofs = map (chain_proof) proofs
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   638
  in chain_proof end
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   639
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   640
type isar_params =
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51741
diff changeset
   641
  bool * bool * Time.time option * bool * real * string Symtab.table
52150
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   642
  * (string * stature) list vector * (string * term) list * int Symtab.table
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   643
  * string proof * thm
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   644
49918
cf441f4a358b renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents: 49917
diff changeset
   645
fun isar_proof_text ctxt isar_proofs
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51741
diff changeset
   646
    (debug, verbose, preplay_timeout, preplay_trace, isar_compress, pool,
52150
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   647
     fact_names, lifted, sym_tab, atp_proof, goal)
49918
cf441f4a358b renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents: 49917
diff changeset
   648
    (one_line_params as (_, _, _, _, subgoal, subgoal_count)) =
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   649
  let
52196
2281f33e8da6 redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents: 52150
diff changeset
   650
    val (params, hyp_ts, concl_t) = strip_subgoal goal subgoal ctxt
2281f33e8da6 redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents: 52150
diff changeset
   651
    val (_, ctxt) =
2281f33e8da6 redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents: 52150
diff changeset
   652
      params
2281f33e8da6 redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents: 52150
diff changeset
   653
      |> map (fn (s, T) => (Binding.name s, SOME T, NoSyn))
2281f33e8da6 redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents: 52150
diff changeset
   654
      |> (fn fixes =>
2281f33e8da6 redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents: 52150
diff changeset
   655
             ctxt |> Variable.set_body false
2281f33e8da6 redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents: 52150
diff changeset
   656
                  |> Proof_Context.add_fixes fixes)
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   657
    val one_line_proof = one_line_proof_text 0 one_line_params
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   658
    val type_enc =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   659
      if is_typed_helper_used_in_atp_proof atp_proof then full_typesN
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   660
      else partial_typesN
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
   661
    val lam_trans = lam_trans_of_atp_proof atp_proof metis_default_lam_trans
50557
31313171deb5 thread no timeout properly
blanchet
parents: 50450
diff changeset
   662
    val preplay = preplay_timeout <> SOME Time.zeroTime
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   663
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   664
    fun isar_proof_of () =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   665
      let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   666
        val atp_proof =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   667
          atp_proof
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   668
          |> clean_up_atp_proof_dependencies
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   669
          |> nasty_atp_proof pool
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   670
          |> map_term_names_in_atp_proof repair_name
52150
41c885784e04 handle lambda-lifted problems in Isar construction code
blanchet
parents: 52125
diff changeset
   671
          |> map (decode_line ctxt lifted sym_tab)
50905
db99fcf69761 more improvements to Isar proof reconstructions
blanchet
parents: 50705
diff changeset
   672
          |> repair_waldmeister_endgame
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   673
          |> rpair [] |-> fold_rev (add_line fact_names)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   674
          |> rpair [] |-> fold_rev add_nontrivial_line
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   675
          |> rpair (0, [])
52125
ac7830871177 improved handling of free variables' types in Isar proofs
blanchet
parents: 52077
diff changeset
   676
          |-> fold_rev (add_desired_line fact_names)
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   677
          |> snd
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   678
        val conj_name = conjecture_prefix ^ string_of_int (length hyp_ts)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   679
        val conjs =
50010
17488e45eb5a proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents: 50005
diff changeset
   680
          atp_proof |> map_filter
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   681
            (fn (name as (_, ss), _, _, _, []) =>
50010
17488e45eb5a proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents: 50005
diff changeset
   682
                if member (op =) ss conj_name then SOME name else NONE
17488e45eb5a proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents: 50005
diff changeset
   683
              | _ => NONE)
17488e45eb5a proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents: 50005
diff changeset
   684
        val assms =
17488e45eb5a proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents: 50005
diff changeset
   685
          atp_proof |> map_filter
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   686
            (fn (name as (_, ss), _, _, _, []) =>
50013
cceec179bdca use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents: 50012
diff changeset
   687
                (case resolve_conjecture ss of
cceec179bdca use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents: 50012
diff changeset
   688
                   [j] =>
cceec179bdca use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents: 50012
diff changeset
   689
                   if j = length hyp_ts then NONE
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
   690
                   else SOME (raw_label_of_name name, nth hyp_ts j)
50013
cceec179bdca use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents: 50012
diff changeset
   691
                 | _ => NONE)
50010
17488e45eb5a proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents: 50005
diff changeset
   692
              | _ => NONE)
51212
2bbcc9cc12b4 ensure all conjecture clauses are in the graph -- to prevent exceptions later
blanchet
parents: 51208
diff changeset
   693
        val bot = atp_proof |> List.last |> #1
51145
280ece22765b tuned code
blanchet
parents: 51130
diff changeset
   694
        val refute_graph =
51212
2bbcc9cc12b4 ensure all conjecture clauses are in the graph -- to prevent exceptions later
blanchet
parents: 51208
diff changeset
   695
          atp_proof
2bbcc9cc12b4 ensure all conjecture clauses are in the graph -- to prevent exceptions later
blanchet
parents: 51208
diff changeset
   696
          |> map (fn (name, _, _, _, from) => (from, name))
2bbcc9cc12b4 ensure all conjecture clauses are in the graph -- to prevent exceptions later
blanchet
parents: 51208
diff changeset
   697
          |> make_refute_graph bot
2bbcc9cc12b4 ensure all conjecture clauses are in the graph -- to prevent exceptions later
blanchet
parents: 51208
diff changeset
   698
          |> fold (Atom_Graph.default_node o rpair ()) conjs
51145
280ece22765b tuned code
blanchet
parents: 51130
diff changeset
   699
        val axioms = axioms_of_refute_graph refute_graph conjs
280ece22765b tuned code
blanchet
parents: 51130
diff changeset
   700
        val tainted = tainted_atoms_of_refute_graph refute_graph conjs
51156
cbb640c3d203 made check for conjecture skolemization sound
blanchet
parents: 51149
diff changeset
   701
        val is_clause_tainted = exists (member (op =) tainted)
50676
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   702
        val steps =
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   703
          Symtab.empty
51201
f176855a1ee2 tuning (removed redundant datatype)
blanchet
parents: 51200
diff changeset
   704
          |> fold (fn (name as (s, _), role, t, rule, _) =>
50676
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   705
                      Symtab.update_new (s, (rule,
51156
cbb640c3d203 made check for conjecture skolemization sound
blanchet
parents: 51149
diff changeset
   706
                        t |> (if is_clause_tainted [name] then
50905
db99fcf69761 more improvements to Isar proof reconstructions
blanchet
parents: 50705
diff changeset
   707
                                s_maybe_not role
50676
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   708
                                #> fold exists_of (map Var (Term.add_vars t []))
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   709
                              else
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   710
                                I))))
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   711
                  atp_proof
51148
2246a2e17f92 tuning -- refactoring in preparation for handling skolemization of conjecture
blanchet
parents: 51147
diff changeset
   712
        fun is_clause_skolemize_rule [(s, _)] =
50676
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   713
            Option.map (is_skolemize_rule o fst) (Symtab.lookup steps s) =
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   714
            SOME true
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   715
          | is_clause_skolemize_rule _ = false
50670
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 50557
diff changeset
   716
        (* The assumptions and conjecture are "prop"s; the other formulas are
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 50557
diff changeset
   717
           "bool"s. *)
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   718
        fun prop_of_clause [(s, ss)] =
50016
0ae5328ded8c fixed more "Trueprop" issues
blanchet
parents: 50015
diff changeset
   719
            (case resolve_conjecture ss of
0ae5328ded8c fixed more "Trueprop" issues
blanchet
parents: 50015
diff changeset
   720
               [j] => if j = length hyp_ts then concl_t else nth hyp_ts j
50676
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   721
             | _ => the_default ("", @{term False}) (Symtab.lookup steps s)
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   722
                    |> snd |> HOLogic.mk_Trueprop |> close_form)
50016
0ae5328ded8c fixed more "Trueprop" issues
blanchet
parents: 50015
diff changeset
   723
          | prop_of_clause names =
50676
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   724
            let
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   725
              val lits = map snd (map_filter (Symtab.lookup steps o fst) names)
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   726
            in
50018
4ea26c74d7ea use implications rather than disjunctions to improve readability
blanchet
parents: 50017
diff changeset
   727
              case List.partition (can HOLogic.dest_not) lits of
4ea26c74d7ea use implications rather than disjunctions to improve readability
blanchet
parents: 50017
diff changeset
   728
                (negs as _ :: _, pos as _ :: _) =>
51212
2bbcc9cc12b4 ensure all conjecture clauses are in the graph -- to prevent exceptions later
blanchet
parents: 51208
diff changeset
   729
                s_imp (Library.foldr1 s_conj (map HOLogic.dest_not negs),
2bbcc9cc12b4 ensure all conjecture clauses are in the graph -- to prevent exceptions later
blanchet
parents: 51208
diff changeset
   730
                       Library.foldr1 s_disj pos)
50018
4ea26c74d7ea use implications rather than disjunctions to improve readability
blanchet
parents: 50017
diff changeset
   731
              | _ => fold (curry s_disj) lits @{term False}
4ea26c74d7ea use implications rather than disjunctions to improve readability
blanchet
parents: 50017
diff changeset
   732
            end
50016
0ae5328ded8c fixed more "Trueprop" issues
blanchet
parents: 50015
diff changeset
   733
            |> HOLogic.mk_Trueprop |> close_form
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   734
        fun isar_proof_of_direct_proof infs =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   735
          let
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   736
            fun maybe_show outer c =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   737
              (outer andalso length c = 1 andalso subset (op =) (c, conjs))
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   738
              ? cons Show
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   739
            val is_fixed = Variable.is_declared ctxt orf can Name.dest_skolem
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   740
            fun skolems_of t =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   741
              Term.add_frees t [] |> filter_out (is_fixed o fst) |> rev
51976
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   742
            fun do_steps outer predecessor accum [] =
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   743
                accum
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   744
                |> (if tainted = [] then
52453
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   745
                      cons (Prove (if outer then [Show] else [],
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   746
                                   Fix [], no_label, concl_t, [],
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   747
                                   By_Metis ([the predecessor], [])))
51976
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   748
                    else
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   749
                      I)
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   750
                |> rev
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   751
              | do_steps outer _ accum (Have (gamma, c) :: infs) =
50676
83b8a5a39709 generate "obtain" steps corresponding to skolemization inferences
blanchet
parents: 50675
diff changeset
   752
                let
51148
2246a2e17f92 tuning -- refactoring in preparation for handling skolemization of conjecture
blanchet
parents: 51147
diff changeset
   753
                  val l = label_of_clause c
2246a2e17f92 tuning -- refactoring in preparation for handling skolemization of conjecture
blanchet
parents: 51147
diff changeset
   754
                  val t = prop_of_clause c
2246a2e17f92 tuning -- refactoring in preparation for handling skolemization of conjecture
blanchet
parents: 51147
diff changeset
   755
                  val by =
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   756
                    By_Metis
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   757
                      (fold (add_fact_of_dependencies fact_names) gamma no_facts)
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   758
                  fun prove sub by =
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   759
                    Prove (maybe_show outer c [], Fix [], l, t, sub, by)
51976
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   760
                  fun do_rest l step =
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   761
                    do_steps outer (SOME l) (step :: accum) infs
51148
2246a2e17f92 tuning -- refactoring in preparation for handling skolemization of conjecture
blanchet
parents: 51147
diff changeset
   762
                in
51156
cbb640c3d203 made check for conjecture skolemization sound
blanchet
parents: 51149
diff changeset
   763
                  if is_clause_tainted c then
51149
4f0147ed8bcb skolemize conjecture properly in Isar proof
blanchet
parents: 51148
diff changeset
   764
                    case gamma of
4f0147ed8bcb skolemize conjecture properly in Isar proof
blanchet
parents: 51148
diff changeset
   765
                      [g] =>
51156
cbb640c3d203 made check for conjecture skolemization sound
blanchet
parents: 51149
diff changeset
   766
                      if is_clause_skolemize_rule g andalso
cbb640c3d203 made check for conjecture skolemization sound
blanchet
parents: 51149
diff changeset
   767
                         is_clause_tainted g then
51149
4f0147ed8bcb skolemize conjecture properly in Isar proof
blanchet
parents: 51148
diff changeset
   768
                        let
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51165
diff changeset
   769
                          val subproof =
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   770
                            Proof (Fix (skolems_of (prop_of_clause g)),
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   771
                                   Assume [], rev accum)
51149
4f0147ed8bcb skolemize conjecture properly in Isar proof
blanchet
parents: 51148
diff changeset
   772
                        in
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   773
                          do_steps outer (SOME l)
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   774
                              [prove [subproof] (By_Metis no_facts)] []
51149
4f0147ed8bcb skolemize conjecture properly in Isar proof
blanchet
parents: 51148
diff changeset
   775
                        end
4f0147ed8bcb skolemize conjecture properly in Isar proof
blanchet
parents: 51148
diff changeset
   776
                      else
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   777
                        do_rest l (prove [] by)
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   778
                    | _ => do_rest l (prove [] by)
51148
2246a2e17f92 tuning -- refactoring in preparation for handling skolemization of conjecture
blanchet
parents: 51147
diff changeset
   779
                  else
51149
4f0147ed8bcb skolemize conjecture properly in Isar proof
blanchet
parents: 51148
diff changeset
   780
                    if is_clause_skolemize_rule c then
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   781
                      do_rest l (Prove ([], Fix (skolems_of t), l, t, [], by))
51149
4f0147ed8bcb skolemize conjecture properly in Isar proof
blanchet
parents: 51148
diff changeset
   782
                    else
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   783
                      do_rest l (prove [] by)
51148
2246a2e17f92 tuning -- refactoring in preparation for handling skolemization of conjecture
blanchet
parents: 51147
diff changeset
   784
                end
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   785
              | do_steps outer predecessor accum (Cases cases :: infs) =
51148
2246a2e17f92 tuning -- refactoring in preparation for handling skolemization of conjecture
blanchet
parents: 51147
diff changeset
   786
                let
2246a2e17f92 tuning -- refactoring in preparation for handling skolemization of conjecture
blanchet
parents: 51147
diff changeset
   787
                  fun do_case (c, infs) =
51976
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   788
                    do_proof false [] [(label_of_clause c, prop_of_clause c)]
e5303bd748f2 generate valid direct Isar proof also if the facts are contradictory
blanchet
parents: 51879
diff changeset
   789
                             infs
51148
2246a2e17f92 tuning -- refactoring in preparation for handling skolemization of conjecture
blanchet
parents: 51147
diff changeset
   790
                  val c = succedent_of_cases cases
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51165
diff changeset
   791
                  val l = label_of_clause c
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   792
                  val t = prop_of_clause c
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   793
                  val step =
52453
2cba5906d836 simplified data structure
smolkas
parents: 52374
diff changeset
   794
                    Prove (maybe_show outer c [], Fix [], l, t,
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   795
                      map do_case cases, By_Metis (the_list predecessor, []))
51149
4f0147ed8bcb skolemize conjecture properly in Isar proof
blanchet
parents: 51148
diff changeset
   796
                in
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   797
                  do_steps outer (SOME l) (step :: accum) infs
51149
4f0147ed8bcb skolemize conjecture properly in Isar proof
blanchet
parents: 51148
diff changeset
   798
                end
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   799
            and do_proof outer fix assms infs =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   800
              Proof (Fix fix, Assume assms, do_steps outer NONE [] infs)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   801
          in
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   802
            do_proof true params assms infs
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   803
          end
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   804
51741
blanchet
parents: 51258
diff changeset
   805
        val clean_up_labels_in_proof =
51165
blanchet
parents: 51164
diff changeset
   806
          chain_direct_proof
blanchet
parents: 51164
diff changeset
   807
          #> kill_useless_labels_in_proof
blanchet
parents: 51164
diff changeset
   808
          #> relabel_proof
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50905
diff changeset
   809
        val (isar_proof, (preplay_fail, preplay_time)) =
51145
280ece22765b tuned code
blanchet
parents: 51130
diff changeset
   810
          refute_graph
51031
63d71b247323 more robustness in Isar proof reconstruction (cf. bug report by Ondrej)
blanchet
parents: 51026
diff changeset
   811
          |> redirect_graph axioms tainted bot
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   812
          |> isar_proof_of_direct_proof
51130
76d68444cd59 renamed sledgehammer_shrink to sledgehammer_compress
smolkas
parents: 51129
diff changeset
   813
          |> (if not preplay andalso isar_compress <= 1.0 then
50677
f5c217474eca use rpair to avoid swap
smolkas
parents: 50676
diff changeset
   814
                rpair (false, (true, seconds 0.0))
50557
31313171deb5 thread no timeout properly
blanchet
parents: 50450
diff changeset
   815
              else
51741
blanchet
parents: 51258
diff changeset
   816
                compress_and_preplay_proof debug ctxt type_enc lam_trans preplay
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51741
diff changeset
   817
                  preplay_timeout preplay_trace
51190
2654b3965c8d made "isar_proofs" a 3-way option, to provide a way to totally disable isar_proofs if desired
blanchet
parents: 51187
diff changeset
   818
                  (if isar_proofs = SOME true then isar_compress else 1000.0))
51741
blanchet
parents: 51258
diff changeset
   819
          |>> clean_up_labels_in_proof
49918
cf441f4a358b renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents: 49917
diff changeset
   820
        val isar_text =
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
   821
          string_of_proof ctxt type_enc lam_trans subgoal subgoal_count
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51976
diff changeset
   822
                          isar_proof
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   823
      in
49918
cf441f4a358b renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents: 49917
diff changeset
   824
        case isar_text of
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   825
          "" =>
51190
2654b3965c8d made "isar_proofs" a 3-way option, to provide a way to totally disable isar_proofs if desired
blanchet
parents: 51187
diff changeset
   826
          if isar_proofs = SOME true then
50671
blanchet
parents: 50670
diff changeset
   827
            "\nNo structured proof available (proof too simple)."
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   828
          else
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   829
            ""
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   830
        | _ =>
50670
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 50557
diff changeset
   831
          let
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 50557
diff changeset
   832
            val msg =
51203
4c6ae305462e trust preplayed proof in Mirabelle
blanchet
parents: 51202
diff changeset
   833
              (if verbose then
4c6ae305462e trust preplayed proof in Mirabelle
blanchet
parents: 51202
diff changeset
   834
                let
4c6ae305462e trust preplayed proof in Mirabelle
blanchet
parents: 51202
diff changeset
   835
                  val num_steps = add_metis_steps (steps_of_proof isar_proof) 0
4c6ae305462e trust preplayed proof in Mirabelle
blanchet
parents: 51202
diff changeset
   836
                in [string_of_int num_steps ^ " step" ^ plural_s num_steps] end
4c6ae305462e trust preplayed proof in Mirabelle
blanchet
parents: 51202
diff changeset
   837
               else
4c6ae305462e trust preplayed proof in Mirabelle
blanchet
parents: 51202
diff changeset
   838
                 []) @
50670
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 50557
diff changeset
   839
              (if preplay then
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50905
diff changeset
   840
                [(if preplay_fail then "may fail, " else "") ^
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50905
diff changeset
   841
                   Sledgehammer_Preplay.string_of_preplay_time preplay_time]
50670
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 50557
diff changeset
   842
               else
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 50557
diff changeset
   843
                 [])
50277
e0a4d8404c76 tweaked calculation of sledgehammer messages
smolkas
parents: 50276
diff changeset
   844
          in
51741
blanchet
parents: 51258
diff changeset
   845
            "\n\nStructured proof"
blanchet
parents: 51258
diff changeset
   846
              ^ (commas msg |> not (null msg) ? enclose " (" ")")
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50410
diff changeset
   847
              ^ ":\n" ^ Active.sendback_markup isar_text
50277
e0a4d8404c76 tweaked calculation of sledgehammer messages
smolkas
parents: 50276
diff changeset
   848
          end
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   849
      end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   850
    val isar_proof =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   851
      if debug then
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   852
        isar_proof_of ()
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   853
      else case try isar_proof_of () of
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   854
        SOME s => s
51190
2654b3965c8d made "isar_proofs" a 3-way option, to provide a way to totally disable isar_proofs if desired
blanchet
parents: 51187
diff changeset
   855
      | NONE => if isar_proofs = SOME true then
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   856
                  "\nWarning: The Isar proof construction failed."
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   857
                else
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   858
                  ""
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   859
  in one_line_proof ^ isar_proof end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   860
51187
c344cf148e8f avoid using "smt" for minimization -- better use the prover itself, since then Sledgehammer gets to try metis again and gives the opportunity to output an Isar proof -- and show Isar proof as fallback for SMT proofs
blanchet
parents: 51179
diff changeset
   861
fun isar_proof_would_be_a_good_idea preplay =
c344cf148e8f avoid using "smt" for minimization -- better use the prover itself, since then Sledgehammer gets to try metis again and gives the opportunity to output an Isar proof -- and show Isar proof as fallback for SMT proofs
blanchet
parents: 51179
diff changeset
   862
  case preplay of
c344cf148e8f avoid using "smt" for minimization -- better use the prover itself, since then Sledgehammer gets to try metis again and gives the opportunity to output an Isar proof -- and show Isar proof as fallback for SMT proofs
blanchet
parents: 51179
diff changeset
   863
    Played (reconstr, _) => reconstr = SMT
51215
9ee38fc0bc81 generate Isar proof if Metis appears to be too slow
blanchet
parents: 51212
diff changeset
   864
  | Trust_Playable _ => true
51187
c344cf148e8f avoid using "smt" for minimization -- better use the prover itself, since then Sledgehammer gets to try metis again and gives the opportunity to output an Isar proof -- and show Isar proof as fallback for SMT proofs
blanchet
parents: 51179
diff changeset
   865
  | Failed_to_Play _ => true
c344cf148e8f avoid using "smt" for minimization -- better use the prover itself, since then Sledgehammer gets to try metis again and gives the opportunity to output an Isar proof -- and show Isar proof as fallback for SMT proofs
blanchet
parents: 51179
diff changeset
   866
49918
cf441f4a358b renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents: 49917
diff changeset
   867
fun proof_text ctxt isar_proofs isar_params num_chained
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   868
               (one_line_params as (preplay, _, _, _, _, _)) =
51190
2654b3965c8d made "isar_proofs" a 3-way option, to provide a way to totally disable isar_proofs if desired
blanchet
parents: 51187
diff changeset
   869
  (if isar_proofs = SOME true orelse
2654b3965c8d made "isar_proofs" a 3-way option, to provide a way to totally disable isar_proofs if desired
blanchet
parents: 51187
diff changeset
   870
      (isar_proofs = NONE andalso isar_proof_would_be_a_good_idea preplay) then
49918
cf441f4a358b renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents: 49917
diff changeset
   871
     isar_proof_text ctxt isar_proofs isar_params
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   872
   else
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   873
     one_line_proof_text num_chained) one_line_params
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   874
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   875
end;