src/HOL/Tools/ATP/atp_proof_reconstruct.ML
author haftmann
Mon, 02 Jul 2012 10:50:17 +0200
changeset 48173 c6a5a4336edf
parent 48135 a44f34694406
child 48438 3e45c98fe127
permissions -rw-r--r--
eta-expanded occurences of algebraic functionals are simplified by default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46320
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45887
diff changeset
     1
(*  Title:      HOL/Tools/ATP/atp_proof_reconstruct.ML
38027
505657ddb047 standardize "Author" tags
blanchet
parents: 38019
diff changeset
     2
    Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
505657ddb047 standardize "Author" tags
blanchet
parents: 38019
diff changeset
     3
    Author:     Claire Quigley, Cambridge University Computer Laboratory
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
     4
    Author:     Jasmin Blanchette, TU Muenchen
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
     5
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents: 43062
diff changeset
     6
Proof reconstruction from ATP proofs.
33310
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
     7
*)
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
     8
46320
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45887
diff changeset
     9
signature ATP_PROOF_RECONSTRUCT =
24425
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
    10
sig
43678
56d352659500 improved translation of lambdas in THF
nik
parents: 43481
diff changeset
    11
  type ('a, 'b) ho_term = ('a, 'b) ATP_Problem.ho_term
48135
a44f34694406 added sorts to datastructure
blanchet
parents: 48132
diff changeset
    12
  type ('a, 'b, 'c, 'd) formula = ('a, 'b, 'c, 'd) ATP_Problem.formula
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
    13
  type 'a proof = 'a ATP_Proof.proof
46340
cac402c486b0 separate orthogonal components
blanchet
parents: 46320
diff changeset
    14
  type stature = ATP_Problem_Generate.stature
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
    15
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
    16
  datatype reconstructor =
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    17
    Metis of string * string |
45379
0147a4348ca1 try "smt" as a fallback for ATPs if "metis" fails/times out
blanchet
parents: 45378
diff changeset
    18
    SMT
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
    19
43050
59284a13abc4 support "metis" and "metisFT" as provers in the architecture, so they can be used for minimizing
blanchet
parents: 43039
diff changeset
    20
  datatype play =
59284a13abc4 support "metis" and "metisFT" as provers in the architecture, so they can be used for minimizing
blanchet
parents: 43039
diff changeset
    21
    Played of reconstructor * Time.time |
44773
e701dabbfe37 perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
blanchet
parents: 44399
diff changeset
    22
    Trust_Playable of reconstructor * Time.time option |
43166
68e3cd19fee8 show what failed to play
blanchet
parents: 43163
diff changeset
    23
    Failed_to_Play of reconstructor
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
    24
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    25
  type minimize_command = string list -> string
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
    26
  type one_line_params =
46340
cac402c486b0 separate orthogonal components
blanchet
parents: 46320
diff changeset
    27
    play * string * (string * stature) list * minimize_command * int * int
38818
61cf050f8b2e improve SPASS hack, when a clause comes from several facts
blanchet
parents: 38752
diff changeset
    28
  type isar_params =
46340
cac402c486b0 separate orthogonal components
blanchet
parents: 46320
diff changeset
    29
    bool * int * string Symtab.table * (string * stature) list vector
45551
a62c7a21f4ab removed needless baggage
blanchet
parents: 45522
diff changeset
    30
    * int Symtab.table * string proof * thm
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    31
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    32
  val metisN : string
45520
2b1dde0b1c30 thread in additional options to minimizer
blanchet
parents: 45519
diff changeset
    33
  val smtN : string
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    34
  val full_typesN : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    35
  val partial_typesN : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    36
  val no_typesN : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    37
  val really_full_type_enc : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    38
  val full_type_enc : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    39
  val partial_type_enc : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    40
  val no_type_enc : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    41
  val full_type_encs : string list
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    42
  val partial_type_encs : string list
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
    43
  val metis_default_lam_trans : string
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
    44
  val metis_call : string -> string -> string
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
    45
  val string_for_reconstructor : reconstructor -> string
42451
a75fcd103cbb automatically remove offending facts when faced with an unsound proof -- instead of using the highly inefficient "full_types" option
blanchet
parents: 42449
diff changeset
    46
  val used_facts_in_atp_proof :
46340
cac402c486b0 separate orthogonal components
blanchet
parents: 46320
diff changeset
    47
    Proof.context -> (string * stature) list vector -> string proof
cac402c486b0 separate orthogonal components
blanchet
parents: 46320
diff changeset
    48
    -> (string * stature) list
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
    49
  val lam_trans_from_atp_proof : string proof -> string -> string
45590
dc9a7ff13e37 made SML/NJ happy
blanchet
parents: 45579
diff changeset
    50
  val is_typed_helper_used_in_atp_proof : string proof -> bool
42876
e336ef6313aa more informative message when Sledgehammer finds an unsound proof
blanchet
parents: 42761
diff changeset
    51
  val used_facts_in_unsound_atp_proof :
46340
cac402c486b0 separate orthogonal components
blanchet
parents: 46320
diff changeset
    52
    Proof.context -> (string * stature) list vector -> 'a proof
45551
a62c7a21f4ab removed needless baggage
blanchet
parents: 45522
diff changeset
    53
    -> string list option
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    54
  val unalias_type_enc : string -> string list
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
    55
  val one_line_proof_text : one_line_params -> string
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
    56
  val make_tvar : string -> typ
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
    57
  val make_tfree : Proof.context -> string -> typ
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
    58
  val term_from_atp :
44773
e701dabbfe37 perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
blanchet
parents: 44399
diff changeset
    59
    Proof.context -> bool -> int Symtab.table -> typ option
e701dabbfe37 perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
blanchet
parents: 44399
diff changeset
    60
    -> (string, string) ho_term -> term
43127
a3f3b7a0e99e export one more function
blanchet
parents: 43102
diff changeset
    61
  val prop_from_atp :
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
    62
    Proof.context -> bool -> int Symtab.table
48135
a44f34694406 added sorts to datastructure
blanchet
parents: 48132
diff changeset
    63
    -> (string, string, (string, string) ho_term, string) formula -> term
42968
74415622d293 more work on parsing LEO-II proofs and extracting uses of extensionality
blanchet
parents: 42966
diff changeset
    64
  val isar_proof_text :
43062
2834548a7a48 nicer failure message when one-line proof reconstruction fails
blanchet
parents: 43051
diff changeset
    65
    Proof.context -> bool -> isar_params -> one_line_params -> string
42968
74415622d293 more work on parsing LEO-II proofs and extracting uses of extensionality
blanchet
parents: 42966
diff changeset
    66
  val proof_text :
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
    67
    Proof.context -> bool -> isar_params -> one_line_params -> string
24425
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
    68
end;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    69
46320
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45887
diff changeset
    70
structure ATP_Proof_Reconstruct : ATP_PROOF_RECONSTRUCT =
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    71
struct
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    72
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents: 43062
diff changeset
    73
open ATP_Util
38028
22dcaec5fa77 minor refactoring
blanchet
parents: 38027
diff changeset
    74
open ATP_Problem
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
    75
open ATP_Proof
46320
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45887
diff changeset
    76
open ATP_Problem_Generate
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    77
46320
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45887
diff changeset
    78
structure String_Redirect = ATP_Proof_Redirect(
45887
bfb5234a70ba made SML/NJ happier
blanchet
parents: 45883
diff changeset
    79
    type key = step_name
bfb5234a70ba made SML/NJ happier
blanchet
parents: 45883
diff changeset
    80
    val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s')
bfb5234a70ba made SML/NJ happier
blanchet
parents: 45883
diff changeset
    81
    val string_of = fst)
bfb5234a70ba made SML/NJ happier
blanchet
parents: 45883
diff changeset
    82
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
    83
open String_Redirect
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
    84
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
    85
datatype reconstructor =
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    86
  Metis of string * string |
45379
0147a4348ca1 try "smt" as a fallback for ATPs if "metis" fails/times out
blanchet
parents: 45378
diff changeset
    87
  SMT
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
    88
43050
59284a13abc4 support "metis" and "metisFT" as provers in the architecture, so they can be used for minimizing
blanchet
parents: 43039
diff changeset
    89
datatype play =
59284a13abc4 support "metis" and "metisFT" as provers in the architecture, so they can be used for minimizing
blanchet
parents: 43039
diff changeset
    90
  Played of reconstructor * Time.time |
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
    91
  Trust_Playable of reconstructor * Time.time option |
43166
68e3cd19fee8 show what failed to play
blanchet
parents: 43163
diff changeset
    92
  Failed_to_Play of reconstructor
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
    93
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    94
type minimize_command = string list -> string
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
    95
type one_line_params =
46340
cac402c486b0 separate orthogonal components
blanchet
parents: 46320
diff changeset
    96
  play * string * (string * stature) list * minimize_command * int * int
38818
61cf050f8b2e improve SPASS hack, when a clause comes from several facts
blanchet
parents: 38752
diff changeset
    97
type isar_params =
46340
cac402c486b0 separate orthogonal components
blanchet
parents: 46320
diff changeset
    98
  bool * int * string Symtab.table * (string * stature) list vector
45551
a62c7a21f4ab removed needless baggage
blanchet
parents: 45522
diff changeset
    99
  * int Symtab.table * string proof * thm
42881
dbdfe2d5b6ab automatically use "metisFT" when typed helpers are necessary
blanchet
parents: 42876
diff changeset
   100
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   101
val metisN = "metis"
45520
2b1dde0b1c30 thread in additional options to minimizer
blanchet
parents: 45519
diff changeset
   102
val smtN = "smt"
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   103
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   104
val full_typesN = "full_types"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   105
val partial_typesN = "partial_types"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   106
val no_typesN = "no_types"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   107
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   108
val really_full_type_enc = "mono_tags"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   109
val full_type_enc = "poly_guards_query"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   110
val partial_type_enc = "poly_args"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   111
val no_type_enc = "erased"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   112
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   113
val full_type_encs = [full_type_enc, really_full_type_enc]
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   114
val partial_type_encs = partial_type_enc :: full_type_encs
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   115
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   116
val type_enc_aliases =
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   117
  [(full_typesN, full_type_encs),
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   118
   (partial_typesN, partial_type_encs),
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   119
   (no_typesN, [no_type_enc])]
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   120
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   121
fun unalias_type_enc s =
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   122
  AList.lookup (op =) type_enc_aliases s |> the_default [s]
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   123
46365
547d1a1dcaf6 rename lambda translation schemes
blanchet
parents: 46342
diff changeset
   124
val metis_default_lam_trans = combsN
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   125
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   126
fun metis_call type_enc lam_trans =
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   127
  let
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   128
    val type_enc =
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   129
      case AList.find (fn (enc, encs) => enc = hd encs) type_enc_aliases
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   130
                      type_enc of
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   131
        [alias] => alias
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   132
      | _ => type_enc
45522
3b951bbd2bee compile
blanchet
parents: 45520
diff changeset
   133
    val opts = [] |> type_enc <> partial_typesN ? cons type_enc
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   134
                  |> lam_trans <> metis_default_lam_trans ? cons lam_trans
45520
2b1dde0b1c30 thread in additional options to minimizer
blanchet
parents: 45519
diff changeset
   135
  in metisN ^ (if null opts then "" else " (" ^ commas opts ^ ")") end
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   136
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   137
fun string_for_reconstructor (Metis (type_enc, lam_trans)) =
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
   138
    metis_call type_enc lam_trans
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   139
  | string_for_reconstructor SMT = smtN
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   140
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   141
fun find_first_in_list_vector vec key =
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   142
  Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   143
                 | (_, value) => value) NONE vec
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   144
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   145
val unprefix_fact_number = space_implode "_" o tl o space_explode "_"
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   146
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   147
fun resolve_one_named_fact fact_names s =
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   148
  case try (unprefix fact_prefix) s of
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   149
    SOME s' =>
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   150
    let val s' = s' |> unprefix_fact_number |> unascii_of in
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   151
      s' |> find_first_in_list_vector fact_names |> Option.map (pair s')
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   152
    end
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   153
  | NONE => NONE
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   154
fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names)
45579
2022cd224a3c gave SML/NJ a chance
blanchet
parents: 45554
diff changeset
   155
fun is_fact fact_names = not o null o resolve_fact fact_names
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   156
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   157
fun resolve_one_named_conjecture s =
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   158
  case try (unprefix conjecture_prefix) s of
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   159
    SOME s' => Int.fromString s'
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   160
  | NONE => NONE
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   161
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   162
val resolve_conjecture = map_filter resolve_one_named_conjecture
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   163
val is_conjecture = not o null o resolve_conjecture
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   164
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   165
fun is_axiom_used_in_proof pred =
47774
blanchet
parents: 47147
diff changeset
   166
  exists (fn Inference_Step ((_, ss), _, _, []) => exists pred ss | _ => false)
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   167
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   168
val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix)
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   169
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   170
val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   171
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   172
(* overapproximation (good enough) *)
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   173
fun is_lam_lifted s =
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   174
  String.isPrefix fact_prefix s andalso
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   175
  String.isSubstring ascii_of_lam_fact_prefix s
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   176
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   177
fun lam_trans_from_atp_proof atp_proof default =
46365
547d1a1dcaf6 rename lambda translation schemes
blanchet
parents: 46342
diff changeset
   178
  case (is_axiom_used_in_proof is_combinator_def atp_proof,
547d1a1dcaf6 rename lambda translation schemes
blanchet
parents: 46342
diff changeset
   179
        is_axiom_used_in_proof is_lam_lifted atp_proof) of
547d1a1dcaf6 rename lambda translation schemes
blanchet
parents: 46342
diff changeset
   180
    (false, false) => default
547d1a1dcaf6 rename lambda translation schemes
blanchet
parents: 46342
diff changeset
   181
  | (false, true) => liftingN
46367
723343a03abe avoid unsupported case in Metis
blanchet
parents: 46365
diff changeset
   182
(*  | (true, true) => combs_and_liftingN -- not supported by "metis" *)
723343a03abe avoid unsupported case in Metis
blanchet
parents: 46365
diff changeset
   183
  | (true, _) => combsN
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   184
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   185
val is_typed_helper_name =
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   186
  String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix
45590
dc9a7ff13e37 made SML/NJ happy
blanchet
parents: 45579
diff changeset
   187
fun is_typed_helper_used_in_atp_proof atp_proof =
dc9a7ff13e37 made SML/NJ happy
blanchet
parents: 45579
diff changeset
   188
  is_axiom_used_in_proof is_typed_helper_name atp_proof
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   189
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   190
val leo2_ext = "extcnf_equal_neg"
47974
08d2dcc2dab9 improved LEO-II definition handling -- still hoping for a fix directly in LEO-II
blanchet
parents: 47973
diff changeset
   191
val leo2_unfold_def = "unfold_def"
08d2dcc2dab9 improved LEO-II definition handling -- still hoping for a fix directly in LEO-II
blanchet
parents: 47973
diff changeset
   192
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   193
val isa_ext = Thm.get_name_hint @{thm ext}
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   194
val isa_short_ext = Long_Name.base_name isa_ext
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   195
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   196
fun ext_name ctxt =
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   197
  if Thm.eq_thm_prop (@{thm ext},
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   198
         singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   199
    isa_short_ext
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   200
  else
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   201
    isa_ext
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   202
47974
08d2dcc2dab9 improved LEO-II definition handling -- still hoping for a fix directly in LEO-II
blanchet
parents: 47973
diff changeset
   203
fun add_all_defs fact_names accum =
48085
ff5e900d7b1a avoid dumping definitions several times in LEO-II proofs
blanchet
parents: 47974
diff changeset
   204
  Vector.foldl
ff5e900d7b1a avoid dumping definitions several times in LEO-II proofs
blanchet
parents: 47974
diff changeset
   205
      (fn (facts, facts') =>
ff5e900d7b1a avoid dumping definitions several times in LEO-II proofs
blanchet
parents: 47974
diff changeset
   206
          union (op =) (filter (fn (_, (_, status)) => status = Def) facts)
ff5e900d7b1a avoid dumping definitions several times in LEO-II proofs
blanchet
parents: 47974
diff changeset
   207
                facts')
ff5e900d7b1a avoid dumping definitions several times in LEO-II proofs
blanchet
parents: 47974
diff changeset
   208
      accum fact_names
47974
08d2dcc2dab9 improved LEO-II definition handling -- still hoping for a fix directly in LEO-II
blanchet
parents: 47973
diff changeset
   209
47947
7b482cc7473e include "ext" in all Satallax proofs
blanchet
parents: 47927
diff changeset
   210
fun add_fact ctxt fact_names (Inference_Step ((_, ss), _, rule, deps)) =
47973
9af7e5caf16f augment Satallax unsat cores with all definitions
blanchet
parents: 47947
diff changeset
   211
    (if rule = leo2_ext then
47947
7b482cc7473e include "ext" in all Satallax proofs
blanchet
parents: 47927
diff changeset
   212
       insert (op =) (ext_name ctxt, (Global, General))
47974
08d2dcc2dab9 improved LEO-II definition handling -- still hoping for a fix directly in LEO-II
blanchet
parents: 47973
diff changeset
   213
     else if rule = leo2_unfold_def then
08d2dcc2dab9 improved LEO-II definition handling -- still hoping for a fix directly in LEO-II
blanchet
parents: 47973
diff changeset
   214
       (* LEO 1.3.3 does not record definitions properly, leading to missing
08d2dcc2dab9 improved LEO-II definition handling -- still hoping for a fix directly in LEO-II
blanchet
parents: 47973
diff changeset
   215
         dependencies in the TSTP proof. Remove the next line once this is
08d2dcc2dab9 improved LEO-II definition handling -- still hoping for a fix directly in LEO-II
blanchet
parents: 47973
diff changeset
   216
         fixed. *)
08d2dcc2dab9 improved LEO-II definition handling -- still hoping for a fix directly in LEO-II
blanchet
parents: 47973
diff changeset
   217
       add_all_defs fact_names
47973
9af7e5caf16f augment Satallax unsat cores with all definitions
blanchet
parents: 47947
diff changeset
   218
     else if rule = satallax_unsat_coreN then
9af7e5caf16f augment Satallax unsat cores with all definitions
blanchet
parents: 47947
diff changeset
   219
       (fn [] =>
9af7e5caf16f augment Satallax unsat cores with all definitions
blanchet
parents: 47947
diff changeset
   220
           (* Satallax doesn't include definitions in its unsatisfiable cores,
9af7e5caf16f augment Satallax unsat cores with all definitions
blanchet
parents: 47947
diff changeset
   221
              so we assume the worst and include them all here. *)
47974
08d2dcc2dab9 improved LEO-II definition handling -- still hoping for a fix directly in LEO-II
blanchet
parents: 47973
diff changeset
   222
           [(ext_name ctxt, (Global, General))] |> add_all_defs fact_names
47973
9af7e5caf16f augment Satallax unsat cores with all definitions
blanchet
parents: 47947
diff changeset
   223
         | facts => facts)
47947
7b482cc7473e include "ext" in all Satallax proofs
blanchet
parents: 47927
diff changeset
   224
     else
7b482cc7473e include "ext" in all Satallax proofs
blanchet
parents: 47927
diff changeset
   225
       I)
7b482cc7473e include "ext" in all Satallax proofs
blanchet
parents: 47927
diff changeset
   226
    #> (if null deps then union (op =) (resolve_fact fact_names ss)
7b482cc7473e include "ext" in all Satallax proofs
blanchet
parents: 47927
diff changeset
   227
        else I)
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   228
  | add_fact _ _ _ = I
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   229
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   230
fun used_facts_in_atp_proof ctxt fact_names atp_proof =
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   231
  if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   232
  else fold (add_fact ctxt fact_names) atp_proof []
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   233
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   234
fun used_facts_in_unsound_atp_proof _ _ [] = NONE
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   235
  | used_facts_in_unsound_atp_proof ctxt fact_names atp_proof =
46427
4fd25dadbd94 cleaned up new SPASS parsing
blanchet
parents: 46367
diff changeset
   236
    let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in
46340
cac402c486b0 separate orthogonal components
blanchet
parents: 46320
diff changeset
   237
      if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   238
         not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   239
        SOME (map fst used_facts)
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   240
      else
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   241
        NONE
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   242
    end
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   243
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   244
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   245
(** Soft-core proof reconstruction: one-liners **)
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   246
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   247
fun string_for_label (s, num) = s ^ string_of_int num
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   248
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   249
fun show_time NONE = ""
43034
18259246abb5 try both "metis" and (on failure) "metisFT" in replay
blanchet
parents: 43033
diff changeset
   250
  | show_time (SOME ext_time) = " (" ^ string_from_ext_time ext_time ^ ")"
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   251
45379
0147a4348ca1 try "smt" as a fallback for ATPs if "metis" fails/times out
blanchet
parents: 45378
diff changeset
   252
fun apply_on_subgoal _ 1 = "by "
0147a4348ca1 try "smt" as a fallback for ATPs if "metis" fails/times out
blanchet
parents: 45378
diff changeset
   253
  | apply_on_subgoal 1 _ = "apply "
0147a4348ca1 try "smt" as a fallback for ATPs if "metis" fails/times out
blanchet
parents: 45378
diff changeset
   254
  | apply_on_subgoal i n =
0147a4348ca1 try "smt" as a fallback for ATPs if "metis" fails/times out
blanchet
parents: 45378
diff changeset
   255
    "prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal 1 n
44002
ae53f1304ad5 put parentheses around non-trivial metis call
blanchet
parents: 43907
diff changeset
   256
fun command_call name [] =
ae53f1304ad5 put parentheses around non-trivial metis call
blanchet
parents: 43907
diff changeset
   257
    name |> not (Lexicon.is_identifier name) ? enclose "(" ")"
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   258
  | command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")"
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   259
fun try_command_line banner time command =
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 45590
diff changeset
   260
  banner ^ ": " ^ Markup.markup Isabelle_Markup.sendback command ^ show_time time ^ "."
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   261
fun using_labels [] = ""
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   262
  | using_labels ls =
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   263
    "using " ^ space_implode " " (map string_for_label ls) ^ " "
45520
2b1dde0b1c30 thread in additional options to minimizer
blanchet
parents: 45519
diff changeset
   264
fun reconstructor_command reconstr i n (ls, ss) =
45379
0147a4348ca1 try "smt" as a fallback for ATPs if "metis" fails/times out
blanchet
parents: 45378
diff changeset
   265
  using_labels ls ^ apply_on_subgoal i n ^
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   266
  command_call (string_for_reconstructor reconstr) ss
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   267
fun minimize_line _ [] = ""
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   268
  | minimize_line minimize_command ss =
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   269
    case minimize_command ss of
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   270
      "" => ""
46340
cac402c486b0 separate orthogonal components
blanchet
parents: 46320
diff changeset
   271
    | command =>
cac402c486b0 separate orthogonal components
blanchet
parents: 46320
diff changeset
   272
      "\nTo minimize: " ^ Markup.markup Isabelle_Markup.sendback command ^ "."
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   273
46342
c59b8560eb48 made SML/NJ happy
blanchet
parents: 46340
diff changeset
   274
fun split_used_facts facts =
c59b8560eb48 made SML/NJ happy
blanchet
parents: 46340
diff changeset
   275
  facts |> List.partition (fn (_, (sc, _)) => sc = Chained)
c59b8560eb48 made SML/NJ happy
blanchet
parents: 46340
diff changeset
   276
        |> pairself (sort_distinct (string_ord o pairself fst))
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   277
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   278
fun one_line_proof_text (preplay, banner, used_facts, minimize_command,
43037
ade5c84f860f cleanup proof text generation code
blanchet
parents: 43034
diff changeset
   279
                         subgoal, subgoal_count) =
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   280
  let
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   281
    val (chained, extra) = split_used_facts used_facts
45520
2b1dde0b1c30 thread in additional options to minimizer
blanchet
parents: 45519
diff changeset
   282
    val (failed, reconstr, ext_time) =
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   283
      case preplay of
45520
2b1dde0b1c30 thread in additional options to minimizer
blanchet
parents: 45519
diff changeset
   284
        Played (reconstr, time) => (false, reconstr, (SOME (false, time)))
2b1dde0b1c30 thread in additional options to minimizer
blanchet
parents: 45519
diff changeset
   285
      | Trust_Playable (reconstr, time) =>
2b1dde0b1c30 thread in additional options to minimizer
blanchet
parents: 45519
diff changeset
   286
        (false, reconstr,
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   287
         case time of
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   288
           NONE => NONE
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   289
         | SOME time =>
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   290
           if time = Time.zeroTime then NONE else SOME (true, time))
45520
2b1dde0b1c30 thread in additional options to minimizer
blanchet
parents: 45519
diff changeset
   291
      | Failed_to_Play reconstr => (true, reconstr, NONE)
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   292
    val try_line =
43166
68e3cd19fee8 show what failed to play
blanchet
parents: 43163
diff changeset
   293
      ([], map fst extra)
45520
2b1dde0b1c30 thread in additional options to minimizer
blanchet
parents: 45519
diff changeset
   294
      |> reconstructor_command reconstr subgoal subgoal_count
47147
bd064bc71085 print a hint
blanchet
parents: 46904
diff changeset
   295
      |> (if failed then
bd064bc71085 print a hint
blanchet
parents: 46904
diff changeset
   296
            enclose "One-line proof reconstruction failed: "
bd064bc71085 print a hint
blanchet
parents: 46904
diff changeset
   297
                     ".\n(Invoking \"sledgehammer\" with \"[strict]\" might \
bd064bc71085 print a hint
blanchet
parents: 46904
diff changeset
   298
                     \solve this.)"
bd064bc71085 print a hint
blanchet
parents: 46904
diff changeset
   299
          else
bd064bc71085 print a hint
blanchet
parents: 46904
diff changeset
   300
            try_command_line banner ext_time)
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   301
  in try_line ^ minimize_line minimize_command (map fst (extra @ chained)) end
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   302
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   303
(** Hard-core proof reconstruction: structured Isar proofs **)
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   304
39425
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   305
fun forall_of v t = HOLogic.all_const (fastype_of v) $ lambda v t
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   306
fun exists_of v t = HOLogic.exists_const (fastype_of v) $ lambda v t
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   307
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   308
fun make_tvar s = TVar (("'" ^ s, 0), HOLogic.typeS)
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   309
fun make_tfree ctxt w =
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   310
  let val ww = "'" ^ w in
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   311
    TFree (ww, the_default HOLogic.typeS (Variable.def_sort ctxt (ww, ~1)))
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   312
  end
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   313
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   314
val indent_size = 2
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   315
val no_label = ("", ~1)
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   316
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   317
val raw_prefix = "x"
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   318
val assum_prefix = "a"
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   319
val have_prefix = "f"
39370
f8292d3020db use same hack as in "Async_Manager" to work around Proof General bug
blanchet
parents: 39368
diff changeset
   320
45552
d2139b4557fc removed more clutter
blanchet
parents: 45551
diff changeset
   321
fun raw_label_for_name (num, ss) =
d2139b4557fc removed more clutter
blanchet
parents: 45551
diff changeset
   322
  case resolve_conjecture ss of
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   323
    [j] => (conjecture_prefix, j)
47920
a5c2386518e2 ensure consistent naming of Waldmeister proof steps, so that they are not cleaned away by "clean_up_atp_proof_dependencies"
blanchet
parents: 47918
diff changeset
   324
  | _ => (raw_prefix ^ ascii_of num, 0)
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   325
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   326
(**** INTERPRETATION OF TSTP SYNTAX TREES ****)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   327
43678
56d352659500 improved translation of lambdas in THF
nik
parents: 43481
diff changeset
   328
exception HO_TERM of (string, string) ho_term list
48135
a44f34694406 added sorts to datastructure
blanchet
parents: 48132
diff changeset
   329
exception FORMULA of
a44f34694406 added sorts to datastructure
blanchet
parents: 48132
diff changeset
   330
    (string, string, (string, string) ho_term, string) formula list
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   331
exception SAME of unit
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   332
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   333
(* Type variables are given the basic sort "HOL.type". Some will later be
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   334
   constrained by information from type literals, or by type inference. *)
48132
9aa0fad4e864 added type arguments to "ATerm" constructor -- but don't use them yet
blanchet
parents: 48085
diff changeset
   335
fun typ_from_atp ctxt (u as ATerm ((a, _), us)) =
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   336
  let val Ts = map (typ_from_atp ctxt) us in
45511
9b0f8ca4388e continued implementation of lambda-lifting in Metis
blanchet
parents: 45379
diff changeset
   337
    case unprefix_and_unascii type_const_prefix a of
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   338
      SOME b => Type (invert_const b, Ts)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   339
    | NONE =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   340
      if not (null us) then
43678
56d352659500 improved translation of lambdas in THF
nik
parents: 43481
diff changeset
   341
        raise HO_TERM [u]  (* only "tconst"s have type arguments *)
45511
9b0f8ca4388e continued implementation of lambda-lifting in Metis
blanchet
parents: 45379
diff changeset
   342
      else case unprefix_and_unascii tfree_prefix a of
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   343
        SOME b => make_tfree ctxt b
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   344
      | NONE =>
43302
566f970006e5 improve sort inference in Metis proofs -- in some rare cases Metis steals Isabelle's variable names and the sorts must then be inferred as well
blanchet
parents: 43296
diff changeset
   345
        (* Could be an Isabelle variable or a variable from the ATP, say "X1"
566f970006e5 improve sort inference in Metis proofs -- in some rare cases Metis steals Isabelle's variable names and the sorts must then be inferred as well
blanchet
parents: 43296
diff changeset
   346
           or "_5018". Sometimes variables from the ATP are indistinguishable
566f970006e5 improve sort inference in Metis proofs -- in some rare cases Metis steals Isabelle's variable names and the sorts must then be inferred as well
blanchet
parents: 43296
diff changeset
   347
           from Isabelle variables, which forces us to use a type parameter in
566f970006e5 improve sort inference in Metis proofs -- in some rare cases Metis steals Isabelle's variable names and the sorts must then be inferred as well
blanchet
parents: 43296
diff changeset
   348
           all cases. *)
45511
9b0f8ca4388e continued implementation of lambda-lifting in Metis
blanchet
parents: 45379
diff changeset
   349
        (a |> perhaps (unprefix_and_unascii tvar_prefix), HOLogic.typeS)
43302
566f970006e5 improve sort inference in Metis proofs -- in some rare cases Metis steals Isabelle's variable names and the sorts must then be inferred as well
blanchet
parents: 43296
diff changeset
   350
        |> Type_Infer.param 0
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   351
  end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   352
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   353
(* Type class literal applied to a type. Returns triple of polarity, class,
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   354
   type. *)
48132
9aa0fad4e864 added type arguments to "ATerm" constructor -- but don't use them yet
blanchet
parents: 48085
diff changeset
   355
fun type_constraint_from_term ctxt (u as ATerm ((a, _), us)) =
45511
9b0f8ca4388e continued implementation of lambda-lifting in Metis
blanchet
parents: 45379
diff changeset
   356
  case (unprefix_and_unascii class_prefix a, map (typ_from_atp ctxt) us) of
42606
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   357
    (SOME b, [T]) => (b, T)
43678
56d352659500 improved translation of lambdas in THF
nik
parents: 43481
diff changeset
   358
  | _ => raise HO_TERM [u]
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   359
43907
073ab5379842 pass type arguments to lambda-lifted Frees, to account for polymorphism
blanchet
parents: 43863
diff changeset
   360
(* Accumulate type constraints in a formula: negative type literals. *)
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   361
fun add_var (key, z)  = Vartab.map_default (key, []) (cons z)
42606
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   362
fun add_type_constraint false (cl, TFree (a ,_)) = add_var ((a, ~1), cl)
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   363
  | add_type_constraint false (cl, TVar (ix, _)) = add_var (ix, cl)
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   364
  | add_type_constraint _ _ = I
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   365
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   366
fun repair_variable_name f s =
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   367
  let
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   368
    fun subscript_name s n = s ^ nat_subscript n
38488
3abda3c76df9 handle E's Skolem constants more gracefully
blanchet
parents: 38282
diff changeset
   369
    val s = String.map f s
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   370
  in
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   371
    case space_explode "_" s of
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   372
      [_] => (case take_suffix Char.isDigit (String.explode s) of
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   373
                (cs1 as _ :: _, cs2 as _ :: _) =>
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   374
                subscript_name (String.implode cs1)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   375
                               (the (Int.fromString (String.implode cs2)))
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   376
              | (_, _) => s)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   377
    | [s1, s2] => (case Int.fromString s2 of
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   378
                     SOME n => subscript_name s1 n
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   379
                   | NONE => s)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   380
    | _ => s
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   381
  end
43182
649bada59658 slacker version of "fastype_of", in case a function has dummy type
blanchet
parents: 43176
diff changeset
   382
43907
073ab5379842 pass type arguments to lambda-lifted Frees, to account for polymorphism
blanchet
parents: 43863
diff changeset
   383
(* The number of type arguments of a constant, zero if it's monomorphic. For
073ab5379842 pass type arguments to lambda-lifted Frees, to account for polymorphism
blanchet
parents: 43863
diff changeset
   384
   (instances of) Skolem pseudoconstants, this information is encoded in the
073ab5379842 pass type arguments to lambda-lifted Frees, to account for polymorphism
blanchet
parents: 43863
diff changeset
   385
   constant name. *)
073ab5379842 pass type arguments to lambda-lifted Frees, to account for polymorphism
blanchet
parents: 43863
diff changeset
   386
fun num_type_args thy s =
073ab5379842 pass type arguments to lambda-lifted Frees, to account for polymorphism
blanchet
parents: 43863
diff changeset
   387
  if String.isPrefix skolem_const_prefix s then
46711
f745bcc4a1e5 more explicit Long_Name operations (NB: analyzing qualifiers is inherently fragile);
wenzelm
parents: 46435
diff changeset
   388
    s |> Long_Name.explode |> List.last |> Int.fromString |> the
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   389
  else if String.isPrefix lam_lifted_prefix s then
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   390
    if String.isPrefix lam_lifted_poly_prefix s then 2 else 0
43907
073ab5379842 pass type arguments to lambda-lifted Frees, to account for polymorphism
blanchet
parents: 43863
diff changeset
   391
  else
073ab5379842 pass type arguments to lambda-lifted Frees, to account for polymorphism
blanchet
parents: 43863
diff changeset
   392
    (s, Sign.the_const_type thy s) |> Sign.const_typargs thy |> length
073ab5379842 pass type arguments to lambda-lifted Frees, to account for polymorphism
blanchet
parents: 43863
diff changeset
   393
43182
649bada59658 slacker version of "fastype_of", in case a function has dummy type
blanchet
parents: 43176
diff changeset
   394
fun slack_fastype_of t = fastype_of t handle TERM _ => HOLogic.typeT
649bada59658 slacker version of "fastype_of", in case a function has dummy type
blanchet
parents: 43176
diff changeset
   395
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   396
(* First-order translation. No types are known for variables. "HOLogic.typeT"
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   397
   should allow them to be inferred. *)
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   398
fun term_from_atp ctxt textual sym_tab =
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   399
  let
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   400
    val thy = Proof_Context.theory_of ctxt
43212
050a03afe024 Metis code cleanup
blanchet
parents: 43204
diff changeset
   401
    (* For Metis, we use 1 rather than 0 because variable references in clauses
050a03afe024 Metis code cleanup
blanchet
parents: 43204
diff changeset
   402
       may otherwise conflict with variable constraints in the goal. At least,
050a03afe024 Metis code cleanup
blanchet
parents: 43204
diff changeset
   403
       type inference often fails otherwise. See also "axiom_inference" in
050a03afe024 Metis code cleanup
blanchet
parents: 43204
diff changeset
   404
       "Metis_Reconstruct". *)
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   405
    val var_index = if textual then 0 else 1
43131
9e9420122f91 fixed interaction between type tags and hAPP in reconstruction code
blanchet
parents: 43130
diff changeset
   406
    fun do_term extra_ts opt_T u =
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   407
      case u of
48132
9aa0fad4e864 added type arguments to "ATerm" constructor -- but don't use them yet
blanchet
parents: 48085
diff changeset
   408
        ATerm ((s, _), us) =>
46435
e9c90516bc0d renamed type encoding
blanchet
parents: 46427
diff changeset
   409
        if String.isPrefix native_type_prefix s then
42962
3b50fdeb6cfc started adding support for THF output (but no lambdas)
blanchet
parents: 42943
diff changeset
   410
          @{const True} (* ignore TPTP type information *)
44773
e701dabbfe37 perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
blanchet
parents: 44399
diff changeset
   411
        else if s = tptp_equal then
43093
blanchet
parents: 43085
diff changeset
   412
          let val ts = map (do_term [] NONE) us in
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   413
            if textual andalso length ts = 2 andalso
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   414
              hd ts aconv List.last ts then
39106
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   415
              (* Vampire is keen on producing these. *)
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   416
              @{const True}
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   417
            else
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   418
              list_comb (Const (@{const_name HOL.eq}, HOLogic.typeT), ts)
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   419
          end
45511
9b0f8ca4388e continued implementation of lambda-lifting in Metis
blanchet
parents: 45379
diff changeset
   420
        else case unprefix_and_unascii const_prefix s of
44773
e701dabbfe37 perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
blanchet
parents: 44399
diff changeset
   421
          SOME s' =>
42761
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   422
          let
44773
e701dabbfe37 perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
blanchet
parents: 44399
diff changeset
   423
            val ((s', s''), mangled_us) =
e701dabbfe37 perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
blanchet
parents: 44399
diff changeset
   424
              s' |> unmangled_const |>> `invert_const
42761
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   425
          in
42755
4603154a3018 robustly detect how many type args were passed to the ATP, even if some of them were omitted
blanchet
parents: 42751
diff changeset
   426
            if s' = type_tag_name then
42589
9f7c48463645 restructured type systems some more -- the old naming schemes had "argshg diff |less" and "tagshg diff |less" as equivalent and didn't support a monomorphic version of "tags"
blanchet
parents: 42587
diff changeset
   427
              case mangled_us @ us of
9f7c48463645 restructured type systems some more -- the old naming schemes had "argshg diff |less" and "tagshg diff |less" as equivalent and didn't support a monomorphic version of "tags"
blanchet
parents: 42587
diff changeset
   428
                [typ_u, term_u] =>
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   429
                do_term extra_ts (SOME (typ_from_atp ctxt typ_u)) term_u
43678
56d352659500 improved translation of lambdas in THF
nik
parents: 43481
diff changeset
   430
              | _ => raise HO_TERM us
42966
4e2d6c1e5392 more work on parsing LEO-II proofs without lambdas
blanchet
parents: 42962
diff changeset
   431
            else if s' = predicator_name then
43093
blanchet
parents: 43085
diff changeset
   432
              do_term [] (SOME @{typ bool}) (hd us)
42966
4e2d6c1e5392 more work on parsing LEO-II proofs without lambdas
blanchet
parents: 42962
diff changeset
   433
            else if s' = app_op_name then
43131
9e9420122f91 fixed interaction between type tags and hAPP in reconstruction code
blanchet
parents: 43130
diff changeset
   434
              let val extra_t = do_term [] NONE (List.last us) in
9e9420122f91 fixed interaction between type tags and hAPP in reconstruction code
blanchet
parents: 43130
diff changeset
   435
                do_term (extra_t :: extra_ts)
9e9420122f91 fixed interaction between type tags and hAPP in reconstruction code
blanchet
parents: 43130
diff changeset
   436
                        (case opt_T of
43182
649bada59658 slacker version of "fastype_of", in case a function has dummy type
blanchet
parents: 43176
diff changeset
   437
                           SOME T => SOME (slack_fastype_of extra_t --> T)
43131
9e9420122f91 fixed interaction between type tags and hAPP in reconstruction code
blanchet
parents: 43130
diff changeset
   438
                         | NONE => NONE)
9e9420122f91 fixed interaction between type tags and hAPP in reconstruction code
blanchet
parents: 43130
diff changeset
   439
                        (nth us (length us - 2))
9e9420122f91 fixed interaction between type tags and hAPP in reconstruction code
blanchet
parents: 43130
diff changeset
   440
              end
44396
66b9b3fcd608 started cleaning up polymorphic monotonicity-based encodings, based on discussions with Nick Smallbone
blanchet
parents: 44121
diff changeset
   441
            else if s' = type_guard_name then
42551
cd99d6d3027a reconstruct TFF type predicates correctly for ToFoF
blanchet
parents: 42549
diff changeset
   442
              @{const True} (* ignore type predicates *)
42549
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   443
            else
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   444
              let
44773
e701dabbfe37 perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
blanchet
parents: 44399
diff changeset
   445
                val new_skolem = String.isPrefix new_skolem_const_prefix s''
42755
4603154a3018 robustly detect how many type args were passed to the ATP, even if some of them were omitted
blanchet
parents: 42751
diff changeset
   446
                val num_ty_args =
4603154a3018 robustly detect how many type args were passed to the ATP, even if some of them were omitted
blanchet
parents: 42751
diff changeset
   447
                  length us - the_default 0 (Symtab.lookup sym_tab s)
42549
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   448
                val (type_us, term_us) =
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   449
                  chop num_ty_args us |>> append mangled_us
43093
blanchet
parents: 43085
diff changeset
   450
                val term_ts = map (do_term [] NONE) term_us
42549
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   451
                val T =
43183
faece9668bce don't throw exception on unknown constants (e.g. skolems), and give more precise type to applied functions
blanchet
parents: 43182
diff changeset
   452
                  (if not (null type_us) andalso
faece9668bce don't throw exception on unknown constants (e.g. skolems), and give more precise type to applied functions
blanchet
parents: 43182
diff changeset
   453
                      num_type_args thy s' = length type_us then
43191
0a72c0527111 fixed reconstruction of new Skolem constants in new Metis
blanchet
parents: 43184
diff changeset
   454
                     let val Ts = type_us |> map (typ_from_atp ctxt) in
0a72c0527111 fixed reconstruction of new Skolem constants in new Metis
blanchet
parents: 43184
diff changeset
   455
                       if new_skolem then
0a72c0527111 fixed reconstruction of new Skolem constants in new Metis
blanchet
parents: 43184
diff changeset
   456
                         SOME (Type_Infer.paramify_vars (tl Ts ---> hd Ts))
43200
ca7b0a48515d imported patch metis_reconstr_give_type_infer_a_chance
blanchet
parents: 43199
diff changeset
   457
                       else if textual then
ca7b0a48515d imported patch metis_reconstr_give_type_infer_a_chance
blanchet
parents: 43199
diff changeset
   458
                         try (Sign.const_instance thy) (s', Ts)
43191
0a72c0527111 fixed reconstruction of new Skolem constants in new Metis
blanchet
parents: 43184
diff changeset
   459
                       else
43200
ca7b0a48515d imported patch metis_reconstr_give_type_infer_a_chance
blanchet
parents: 43199
diff changeset
   460
                         NONE
43191
0a72c0527111 fixed reconstruction of new Skolem constants in new Metis
blanchet
parents: 43184
diff changeset
   461
                     end
43183
faece9668bce don't throw exception on unknown constants (e.g. skolems), and give more precise type to applied functions
blanchet
parents: 43182
diff changeset
   462
                   else
faece9668bce don't throw exception on unknown constants (e.g. skolems), and give more precise type to applied functions
blanchet
parents: 43182
diff changeset
   463
                     NONE)
faece9668bce don't throw exception on unknown constants (e.g. skolems), and give more precise type to applied functions
blanchet
parents: 43182
diff changeset
   464
                  |> (fn SOME T => T
faece9668bce don't throw exception on unknown constants (e.g. skolems), and give more precise type to applied functions
blanchet
parents: 43182
diff changeset
   465
                       | NONE => map slack_fastype_of term_ts --->
faece9668bce don't throw exception on unknown constants (e.g. skolems), and give more precise type to applied functions
blanchet
parents: 43182
diff changeset
   466
                                 (case opt_T of
faece9668bce don't throw exception on unknown constants (e.g. skolems), and give more precise type to applied functions
blanchet
parents: 43182
diff changeset
   467
                                    SOME T => T
faece9668bce don't throw exception on unknown constants (e.g. skolems), and give more precise type to applied functions
blanchet
parents: 43182
diff changeset
   468
                                  | NONE => HOLogic.typeT))
43191
0a72c0527111 fixed reconstruction of new Skolem constants in new Metis
blanchet
parents: 43184
diff changeset
   469
                val t =
0a72c0527111 fixed reconstruction of new Skolem constants in new Metis
blanchet
parents: 43184
diff changeset
   470
                  if new_skolem then
44773
e701dabbfe37 perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
blanchet
parents: 44399
diff changeset
   471
                    Var ((new_skolem_var_name_from_const s'', var_index), T)
43191
0a72c0527111 fixed reconstruction of new Skolem constants in new Metis
blanchet
parents: 43184
diff changeset
   472
                  else
0a72c0527111 fixed reconstruction of new Skolem constants in new Metis
blanchet
parents: 43184
diff changeset
   473
                    Const (unproxify_const s', T)
0a72c0527111 fixed reconstruction of new Skolem constants in new Metis
blanchet
parents: 43184
diff changeset
   474
              in list_comb (t, term_ts @ extra_ts) end
42549
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   475
          end
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   476
        | NONE => (* a free or schematic variable *)
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   477
          let
45042
89341b897412 better type reconstruction -- prevents ill-instantiations in proof replay
blanchet
parents: 44783
diff changeset
   478
            val term_ts = map (do_term [] NONE) us
89341b897412 better type reconstruction -- prevents ill-instantiations in proof replay
blanchet
parents: 44783
diff changeset
   479
            val ts = term_ts @ extra_ts
89341b897412 better type reconstruction -- prevents ill-instantiations in proof replay
blanchet
parents: 44783
diff changeset
   480
            val T =
89341b897412 better type reconstruction -- prevents ill-instantiations in proof replay
blanchet
parents: 44783
diff changeset
   481
              case opt_T of
89341b897412 better type reconstruction -- prevents ill-instantiations in proof replay
blanchet
parents: 44783
diff changeset
   482
                SOME T => map slack_fastype_of term_ts ---> T
89341b897412 better type reconstruction -- prevents ill-instantiations in proof replay
blanchet
parents: 44783
diff changeset
   483
              | NONE => map slack_fastype_of ts ---> HOLogic.typeT
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   484
            val t =
45511
9b0f8ca4388e continued implementation of lambda-lifting in Metis
blanchet
parents: 45379
diff changeset
   485
              case unprefix_and_unascii fixed_var_prefix s of
9b0f8ca4388e continued implementation of lambda-lifting in Metis
blanchet
parents: 45379
diff changeset
   486
                SOME s => Free (s, T)
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   487
              | NONE =>
45511
9b0f8ca4388e continued implementation of lambda-lifting in Metis
blanchet
parents: 45379
diff changeset
   488
                case unprefix_and_unascii schematic_var_prefix s of
44773
e701dabbfe37 perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
blanchet
parents: 44399
diff changeset
   489
                  SOME s => Var ((s, var_index), T)
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   490
                | NONE =>
44773
e701dabbfe37 perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
blanchet
parents: 44399
diff changeset
   491
                  Var ((s |> textual ? repair_variable_name Char.toLower,
43095
ccf1c09dea82 more robust and simpler adjustment computation
blanchet
parents: 43094
diff changeset
   492
                        var_index), T)
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   493
          in list_comb (t, ts) end
43093
blanchet
parents: 43085
diff changeset
   494
  in do_term [] end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   495
48132
9aa0fad4e864 added type arguments to "ATerm" constructor -- but don't use them yet
blanchet
parents: 48085
diff changeset
   496
fun term_from_atom ctxt textual sym_tab pos (u as ATerm ((s, _), _)) =
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   497
  if String.isPrefix class_prefix s then
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   498
    add_type_constraint pos (type_constraint_from_term ctxt u)
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   499
    #> pair @{const True}
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   500
  else
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   501
    pair (term_from_atp ctxt textual sym_tab (SOME @{typ bool}) u)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   502
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   503
val combinator_table =
46904
f30e941b4512 prefer abs_def over def_raw;
wenzelm
parents: 46711
diff changeset
   504
  [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def [abs_def]}),
f30e941b4512 prefer abs_def over def_raw;
wenzelm
parents: 46711
diff changeset
   505
   (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def [abs_def]}),
f30e941b4512 prefer abs_def over def_raw;
wenzelm
parents: 46711
diff changeset
   506
   (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def [abs_def]}),
f30e941b4512 prefer abs_def over def_raw;
wenzelm
parents: 46711
diff changeset
   507
   (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def [abs_def]}),
f30e941b4512 prefer abs_def over def_raw;
wenzelm
parents: 46711
diff changeset
   508
   (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def [abs_def]})]
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   509
42761
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   510
fun uncombine_term thy =
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   511
  let
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   512
    fun aux (t1 $ t2) = betapply (pairself aux (t1, t2))
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   513
      | aux (Abs (s, T, t')) = Abs (s, T, aux t')
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   514
      | aux (t as Const (x as (s, _))) =
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   515
        (case AList.lookup (op =) combinator_table s of
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   516
           SOME thm => thm |> prop_of |> specialize_type thy x
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   517
                           |> Logic.dest_equals |> snd
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   518
         | NONE => t)
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   519
      | aux t = t
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   520
  in aux end
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   521
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   522
(* Update schematic type variables with detected sort constraints. It's not
42563
e70ffe3846d0 improve helper type instantiation code
blanchet
parents: 42562
diff changeset
   523
   totally clear whether this code is necessary. *)
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   524
fun repair_tvar_sorts (t, tvar_tab) =
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   525
  let
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   526
    fun do_type (Type (a, Ts)) = Type (a, map do_type Ts)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   527
      | do_type (TVar (xi, s)) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   528
        TVar (xi, the_default s (Vartab.lookup tvar_tab xi))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   529
      | do_type (TFree z) = TFree z
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   530
    fun do_term (Const (a, T)) = Const (a, do_type T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   531
      | do_term (Free (a, T)) = Free (a, do_type T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   532
      | do_term (Var (xi, T)) = Var (xi, do_type T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   533
      | do_term (t as Bound _) = t
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   534
      | do_term (Abs (a, T, t)) = Abs (a, do_type T, do_term t)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   535
      | do_term (t1 $ t2) = do_term t1 $ do_term t2
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   536
  in t |> not (Vartab.is_empty tvar_tab) ? do_term end
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   537
39425
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   538
fun quantify_over_var quant_of var_s t =
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   539
  let
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   540
    val vars = [] |> Term.add_vars t |> filter (fn ((s, _), _) => s = var_s)
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   541
                  |> map Var
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   542
  in fold_rev quant_of vars t end
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   543
38085
cc44e887246c avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents: 38066
diff changeset
   544
(* Interpret an ATP formula as a HOL term, extracting sort constraints as they
cc44e887246c avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents: 38066
diff changeset
   545
   appear in the formula. *)
43184
b16693484c5d reveal Skolems in new Metis
blanchet
parents: 43183
diff changeset
   546
fun prop_from_atp ctxt textual sym_tab phi =
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   547
  let
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   548
    fun do_formula pos phi =
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   549
      case phi of
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   550
        AQuant (_, [], phi) => do_formula pos phi
42526
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42451
diff changeset
   551
      | AQuant (q, (s, _) :: xs, phi') =>
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   552
        do_formula pos (AQuant (q, xs, phi'))
42526
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42451
diff changeset
   553
        (* FIXME: TFF *)
39425
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   554
        #>> quantify_over_var (case q of
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   555
                                 AForall => forall_of
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   556
                               | AExists => exists_of)
43095
ccf1c09dea82 more robust and simpler adjustment computation
blanchet
parents: 43094
diff changeset
   557
                              (s |> textual ? repair_variable_name Char.toLower)
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   558
      | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   559
      | AConn (c, [phi1, phi2]) =>
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   560
        do_formula (pos |> c = AImplies ? not) phi1
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   561
        ##>> do_formula pos phi2
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   562
        #>> (case c of
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   563
               AAnd => s_conj
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   564
             | AOr => s_disj
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   565
             | AImplies => s_imp
38038
584ab1a3a523 more robust proof reconstruction
blanchet
parents: 38036
diff changeset
   566
             | AIff => s_iff
43163
31babd4b1552 killed odd connectives
blanchet
parents: 43136
diff changeset
   567
             | ANot => raise Fail "impossible connective")
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   568
      | AAtom tm => term_from_atom ctxt textual sym_tab pos tm
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   569
      | _ => raise FORMULA [phi]
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   570
  in repair_tvar_sorts (do_formula true phi Vartab.empty) end
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   571
43131
9e9420122f91 fixed interaction between type tags and hAPP in reconstruction code
blanchet
parents: 43130
diff changeset
   572
fun infer_formula_types ctxt =
39288
f1ae2493d93f eliminated aliases of Type.constraint;
wenzelm
parents: 39134
diff changeset
   573
  Type.constraint HOLogic.boolT
42761
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   574
  #> Syntax.check_term
8ea9c6fa8b53 fixed several bugs in Isar proof reconstruction, in particular w.r.t. mangled types and hAPP
blanchet
parents: 42759
diff changeset
   575
         (Proof_Context.set_mode Proof_Context.mode_schematic ctxt)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   576
43184
b16693484c5d reveal Skolems in new Metis
blanchet
parents: 43183
diff changeset
   577
fun uncombined_etc_prop_from_atp ctxt textual sym_tab =
43136
cf5cda219058 handle lightweight tags sym theorems gracefully in the presence of TVars with interesting type classes
blanchet
parents: 43135
diff changeset
   578
  let val thy = Proof_Context.theory_of ctxt in
43184
b16693484c5d reveal Skolems in new Metis
blanchet
parents: 43183
diff changeset
   579
    prop_from_atp ctxt textual sym_tab
43176
29a3a1a7794d only uncombine combinators in textual Isar proofs, not in Metis
blanchet
parents: 43168
diff changeset
   580
    #> textual ? uncombine_term thy #> infer_formula_types ctxt
43136
cf5cda219058 handle lightweight tags sym theorems gracefully in the presence of TVars with interesting type classes
blanchet
parents: 43135
diff changeset
   581
  end
cf5cda219058 handle lightweight tags sym theorems gracefully in the presence of TVars with interesting type classes
blanchet
parents: 43135
diff changeset
   582
43093
blanchet
parents: 43085
diff changeset
   583
(**** Translation of TSTP files to Isar proofs ****)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   584
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   585
fun unvarify_term (Var ((s, 0), T)) = Free (s, T)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   586
  | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t])
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   587
47774
blanchet
parents: 47147
diff changeset
   588
fun decode_line sym_tab (Definition_Step (name, phi1, phi2)) ctxt =
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   589
    let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42227
diff changeset
   590
      val thy = Proof_Context.theory_of ctxt
43184
b16693484c5d reveal Skolems in new Metis
blanchet
parents: 43183
diff changeset
   591
      val t1 = prop_from_atp ctxt true sym_tab phi1
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   592
      val vars = snd (strip_comb t1)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   593
      val frees = map unvarify_term vars
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   594
      val unvarify_args = subst_atomic (vars ~~ frees)
43184
b16693484c5d reveal Skolems in new Metis
blanchet
parents: 43183
diff changeset
   595
      val t2 = prop_from_atp ctxt true sym_tab phi2
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   596
      val (t1, t2) =
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   597
        HOLogic.eq_const HOLogic.typeT $ t1 $ t2
43131
9e9420122f91 fixed interaction between type tags and hAPP in reconstruction code
blanchet
parents: 43130
diff changeset
   598
        |> unvarify_args |> uncombine_term thy |> infer_formula_types ctxt
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   599
        |> HOLogic.dest_eq
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   600
    in
47774
blanchet
parents: 47147
diff changeset
   601
      (Definition_Step (name, t1, t2),
44121
44adaa6db327 old term operations are legacy;
wenzelm
parents: 44002
diff changeset
   602
       fold Variable.declare_term (maps Misc_Legacy.term_frees [t1, t2]) ctxt)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   603
    end
47774
blanchet
parents: 47147
diff changeset
   604
  | decode_line sym_tab (Inference_Step (name, u, rule, deps)) ctxt =
43184
b16693484c5d reveal Skolems in new Metis
blanchet
parents: 43183
diff changeset
   605
    let val t = u |> uncombined_etc_prop_from_atp ctxt true sym_tab in
47774
blanchet
parents: 47147
diff changeset
   606
      (Inference_Step (name, t, rule, deps),
44121
44adaa6db327 old term operations are legacy;
wenzelm
parents: 44002
diff changeset
   607
       fold Variable.declare_term (Misc_Legacy.term_frees t) ctxt)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   608
    end
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   609
fun decode_lines ctxt sym_tab lines =
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   610
  fst (fold_map (decode_line sym_tab) lines ctxt)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   611
47774
blanchet
parents: 47147
diff changeset
   612
fun is_same_inference _ (Definition_Step _) = false
blanchet
parents: 47147
diff changeset
   613
  | is_same_inference t (Inference_Step (_, t', _, _)) = t aconv t'
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   614
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   615
(* No "real" literals means only type information (tfree_tcs, clsrel, or
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   616
   clsarity). *)
47918
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   617
fun is_only_type_information t = t aconv @{term True}
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   618
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   619
fun replace_one_dependency (old, new) dep =
42968
74415622d293 more work on parsing LEO-II proofs and extracting uses of extensionality
blanchet
parents: 42966
diff changeset
   620
  if is_same_atp_step dep old then new else [dep]
47774
blanchet
parents: 47147
diff changeset
   621
fun replace_dependencies_in_line _ (line as Definition_Step _) = line
blanchet
parents: 47147
diff changeset
   622
  | replace_dependencies_in_line p (Inference_Step (name, t, rule, deps)) =
blanchet
parents: 47147
diff changeset
   623
    Inference_Step (name, t, rule,
blanchet
parents: 47147
diff changeset
   624
                    fold (union (op =) o replace_one_dependency p) deps [])
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   625
40204
da97d75e20e6 standardize on "fact" terminology (vs. "axiom" or "theorem") in Sledgehammer -- but keep "Axiom" in the lower-level "ATP_Problem" module
blanchet
parents: 40114
diff changeset
   626
(* Discard facts; consolidate adjacent lines that prove the same formula, since
38085
cc44e887246c avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents: 38066
diff changeset
   627
   they differ only in type information.*)
47774
blanchet
parents: 47147
diff changeset
   628
fun add_line _ (line as Definition_Step _) lines = line :: lines
blanchet
parents: 47147
diff changeset
   629
  | add_line fact_names (Inference_Step (name as (_, ss), t, rule, [])) lines =
40204
da97d75e20e6 standardize on "fact" terminology (vs. "axiom" or "theorem") in Sledgehammer -- but keep "Axiom" in the lower-level "ATP_Problem" module
blanchet
parents: 40114
diff changeset
   630
    (* No dependencies: fact, conjecture, or (for Vampire) internal facts or
38085
cc44e887246c avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents: 38066
diff changeset
   631
       definitions. *)
45552
d2139b4557fc removed more clutter
blanchet
parents: 45551
diff changeset
   632
    if is_fact fact_names ss then
40204
da97d75e20e6 standardize on "fact" terminology (vs. "axiom" or "theorem") in Sledgehammer -- but keep "Axiom" in the lower-level "ATP_Problem" module
blanchet
parents: 40114
diff changeset
   633
      (* Facts are not proof lines. *)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   634
      if is_only_type_information t then
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   635
        map (replace_dependencies_in_line (name, [])) lines
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   636
      (* Is there a repetition? If so, replace later line by earlier one. *)
38035
0ed953eac020 fix proof reconstruction for latest Vampire
blanchet
parents: 38034
diff changeset
   637
      else case take_prefix (not o is_same_inference t) lines of
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   638
        (_, []) => lines (* no repetition of proof line *)
47774
blanchet
parents: 47147
diff changeset
   639
      | (pre, Inference_Step (name', _, _, _) :: post) =>
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   640
        pre @ map (replace_dependencies_in_line (name', [name])) post
40069
6f7bf79b1506 fixed signature of "is_smt_solver_installed";
blanchet
parents: 40068
diff changeset
   641
      | _ => raise Fail "unexpected inference"
45552
d2139b4557fc removed more clutter
blanchet
parents: 45551
diff changeset
   642
    else if is_conjecture ss then
47918
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   643
      Inference_Step (name, t, rule, []) :: lines
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   644
    else
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   645
      map (replace_dependencies_in_line (name, [])) lines
47774
blanchet
parents: 47147
diff changeset
   646
  | add_line _ (Inference_Step (name, t, rule, deps)) lines =
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   647
    (* Type information will be deleted later; skip repetition test. *)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   648
    if is_only_type_information t then
47774
blanchet
parents: 47147
diff changeset
   649
      Inference_Step (name, t, rule, deps) :: lines
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   650
    (* Is there a repetition? If so, replace later line by earlier one. *)
38035
0ed953eac020 fix proof reconstruction for latest Vampire
blanchet
parents: 38034
diff changeset
   651
    else case take_prefix (not o is_same_inference t) lines of
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   652
      (* FIXME: Doesn't this code risk conflating proofs involving different
38035
0ed953eac020 fix proof reconstruction for latest Vampire
blanchet
parents: 38034
diff changeset
   653
         types? *)
47774
blanchet
parents: 47147
diff changeset
   654
       (_, []) => Inference_Step (name, t, rule, deps) :: lines
blanchet
parents: 47147
diff changeset
   655
     | (pre, Inference_Step (name', t', rule, _) :: post) =>
blanchet
parents: 47147
diff changeset
   656
       Inference_Step (name, t', rule, deps) ::
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   657
       pre @ map (replace_dependencies_in_line (name', [name])) post
40069
6f7bf79b1506 fixed signature of "is_smt_solver_installed";
blanchet
parents: 40068
diff changeset
   658
     | _ => raise Fail "unexpected inference"
22044
6c0702a96076 More compact proof reconstruction: lines having fewer than !min_deps dependences are folded
paulson
parents: 22012
diff changeset
   659
47927
c35238d19bb9 repair the Waldmeister endgame only for Waldmeister proofs
blanchet
parents: 47921
diff changeset
   660
val waldmeister_conjecture_num = "1.0.0.0"
c35238d19bb9 repair the Waldmeister endgame only for Waldmeister proofs
blanchet
parents: 47921
diff changeset
   661
47918
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   662
val repair_waldmeister_endgame =
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   663
  let
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   664
    fun do_tail (Inference_Step (name, t, rule, deps)) =
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   665
        Inference_Step (name, s_not t, rule, deps)
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   666
      | do_tail line = line
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   667
    fun do_body [] = []
47927
c35238d19bb9 repair the Waldmeister endgame only for Waldmeister proofs
blanchet
parents: 47921
diff changeset
   668
      | do_body ((line as Inference_Step ((num, _), _, _, _)) :: lines) =
c35238d19bb9 repair the Waldmeister endgame only for Waldmeister proofs
blanchet
parents: 47921
diff changeset
   669
        if num = waldmeister_conjecture_num then map do_tail (line :: lines)
47918
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   670
        else line :: do_body lines
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   671
      | do_body (line :: lines) = line :: do_body lines
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   672
  in do_body end
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   673
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   674
(* Recursively delete empty lines (type information) from the proof. *)
47774
blanchet
parents: 47147
diff changeset
   675
fun add_nontrivial_line (line as Inference_Step (name, t, _, [])) lines =
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   676
    if is_only_type_information t then delete_dependency name lines
45209
0e5e56e32bc0 cleaner LEO-II extensionality step detection
blanchet
parents: 45042
diff changeset
   677
    else line :: lines
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   678
  | add_nontrivial_line line lines = line :: lines
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   679
and delete_dependency name lines =
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   680
  fold_rev add_nontrivial_line
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   681
           (map (replace_dependencies_in_line (name, [])) lines) []
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   682
37323
2f2f0d246d0f handle Vampire's definitions smoothly
blanchet
parents: 37322
diff changeset
   683
(* ATPs sometimes reuse free variable names in the strangest ways. Removing
2f2f0d246d0f handle Vampire's definitions smoothly
blanchet
parents: 37322
diff changeset
   684
   offending lines often does the trick. *)
36560
45c1870f234f fixed definition of "bad frees" so that it actually works
blanchet
parents: 36559
diff changeset
   685
fun is_bad_free frees (Free x) = not (member (op =) frees x)
45c1870f234f fixed definition of "bad frees" so that it actually works
blanchet
parents: 36559
diff changeset
   686
  | is_bad_free _ _ = false
22470
0d52e5157124 No label on "show"; tries to remove dependencies more cleanly
paulson
parents: 22428
diff changeset
   687
47774
blanchet
parents: 47147
diff changeset
   688
fun add_desired_line _ _ _ (line as Definition_Step (name, _, _)) (j, lines) =
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   689
    (j, line :: map (replace_dependencies_in_line (name, [])) lines)
45551
a62c7a21f4ab removed needless baggage
blanchet
parents: 45522
diff changeset
   690
  | add_desired_line isar_shrink_factor fact_names frees
47774
blanchet
parents: 47147
diff changeset
   691
        (Inference_Step (name as (_, ss), t, rule, deps)) (j, lines) =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   692
    (j + 1,
45552
d2139b4557fc removed more clutter
blanchet
parents: 45551
diff changeset
   693
     if is_fact fact_names ss orelse
d2139b4557fc removed more clutter
blanchet
parents: 45551
diff changeset
   694
        is_conjecture ss orelse
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   695
        (* the last line must be kept *)
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   696
        j = 0 orelse
36570
9bebcb40599f identify axioms/conjectures more reliably in ATP proofs (an empty dependency list doesn't always indicate an axiom or conjecture!)
blanchet
parents: 36567
diff changeset
   697
        (not (is_only_type_information t) andalso
9bebcb40599f identify axioms/conjectures more reliably in ATP proofs (an empty dependency list doesn't always indicate an axiom or conjecture!)
blanchet
parents: 36567
diff changeset
   698
         null (Term.add_tvars t []) andalso
9bebcb40599f identify axioms/conjectures more reliably in ATP proofs (an empty dependency list doesn't always indicate an axiom or conjecture!)
blanchet
parents: 36567
diff changeset
   699
         not (exists_subterm (is_bad_free frees) t) andalso
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   700
         length deps >= 2 andalso j mod isar_shrink_factor = 0 andalso
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   701
         (* kill next to last line, which usually results in a trivial step *)
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   702
         j <> 1) then
47774
blanchet
parents: 47147
diff changeset
   703
       Inference_Step (name, t, rule, deps) :: lines  (* keep line *)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   704
     else
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   705
       map (replace_dependencies_in_line (name, deps)) lines)  (* drop line *)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   706
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   707
(** Isar proof construction and manipulation **)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   708
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   709
type label = string * int
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   710
type facts = label list * string list
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   711
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
   712
datatype isar_qualifier = Show | Then | Moreover | Ultimately
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   713
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
   714
datatype isar_step =
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   715
  Fix of (string * typ) list |
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   716
  Let of term * term |
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   717
  Assume of label * term |
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   718
  Prove of isar_qualifier list * label * term * byline
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   719
and byline =
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   720
  By_Metis of facts |
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   721
  Case_Split of isar_step list list * facts
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   722
45552
d2139b4557fc removed more clutter
blanchet
parents: 45551
diff changeset
   723
fun add_fact_from_dependency fact_names (name as (_, ss)) =
d2139b4557fc removed more clutter
blanchet
parents: 45551
diff changeset
   724
  if is_fact fact_names ss then
d2139b4557fc removed more clutter
blanchet
parents: 45551
diff changeset
   725
    apsnd (union (op =) (map fst (resolve_fact fact_names ss)))
36475
05209b869a6b new Isar proof construction code: stringfy axiom names correctly
blanchet
parents: 36474
diff changeset
   726
  else
45551
a62c7a21f4ab removed needless baggage
blanchet
parents: 45522
diff changeset
   727
    apfst (insert (op =) (raw_label_for_name name))
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   728
39454
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   729
fun repair_name "$true" = "c_True"
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   730
  | repair_name "$false" = "c_False"
43000
bd424c3dde46 cleaner handling of equality and proxies (esp. for THF)
blanchet
parents: 42998
diff changeset
   731
  | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
39454
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   732
  | repair_name s =
43000
bd424c3dde46 cleaner handling of equality and proxies (esp. for THF)
blanchet
parents: 42998
diff changeset
   733
    if is_tptp_equal s orelse
bd424c3dde46 cleaner handling of equality and proxies (esp. for THF)
blanchet
parents: 42998
diff changeset
   734
       (* seen in Vampire proofs *)
bd424c3dde46 cleaner handling of equality and proxies (esp. for THF)
blanchet
parents: 42998
diff changeset
   735
       (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then
bd424c3dde46 cleaner handling of equality and proxies (esp. for THF)
blanchet
parents: 42998
diff changeset
   736
      tptp_equal
39454
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   737
    else
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   738
      s
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   739
45883
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   740
(* FIXME: Still needed? Try with SPASS proofs perhaps. *)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   741
val kill_duplicate_assumptions_in_proof =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   742
  let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   743
    fun relabel_facts subst =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   744
      apfst (map (fn l => AList.lookup (op =) subst l |> the_default l))
36491
6769f8eacaac unskolemize formulas in proof reconstruction + detect newer SPASS versions to avoid truncating identifiers if not necessary (truncating confuses proof reconstruction)
blanchet
parents: 36488
diff changeset
   745
    fun do_step (step as Assume (l, t)) (proof, subst, assums) =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   746
        (case AList.lookup (op aconv) assums t of
36967
3c804030474b fix bug in Isar proof reconstruction step relabeling + don't try to infer the sorts of TVars, since this often fails miserably
blanchet
parents: 36924
diff changeset
   747
           SOME l' => (proof, (l, l') :: subst, assums)
36491
6769f8eacaac unskolemize formulas in proof reconstruction + detect newer SPASS versions to avoid truncating identifiers if not necessary (truncating confuses proof reconstruction)
blanchet
parents: 36488
diff changeset
   748
         | NONE => (step :: proof, subst, (t, l) :: assums))
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   749
      | do_step (Prove (qs, l, t, by)) (proof, subst, assums) =
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   750
        (Prove (qs, l, t,
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   751
                case by of
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   752
                  By_Metis facts => By_Metis (relabel_facts subst facts)
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   753
                | Case_Split (proofs, facts) =>
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   754
                  Case_Split (map do_proof proofs,
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   755
                              relabel_facts subst facts)) ::
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   756
         proof, subst, assums)
36491
6769f8eacaac unskolemize formulas in proof reconstruction + detect newer SPASS versions to avoid truncating identifiers if not necessary (truncating confuses proof reconstruction)
blanchet
parents: 36488
diff changeset
   757
      | do_step step (proof, subst, assums) = (step :: proof, subst, assums)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   758
    and do_proof proof = fold do_step proof ([], [], []) |> #1 |> rev
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   759
  in do_proof end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   760
45883
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   761
fun used_labels_of_step (Prove (_, _, _, by)) =
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   762
    (case by of
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   763
       By_Metis (ls, _) => ls
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   764
     | Case_Split (proofs, (ls, _)) =>
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   765
       fold (union (op =) o used_labels_of) proofs ls)
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   766
  | used_labels_of_step _ = []
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   767
and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof []
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   768
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   769
fun kill_useless_labels_in_proof proof =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   770
  let
36556
81dc2c20f052 use readable names in "debug" mode for type vars + don't pipe facts using "using" but rather give them directly to metis (works better with type variables)
blanchet
parents: 36555
diff changeset
   771
    val used_ls = used_labels_of proof
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   772
    fun do_label l = if member (op =) used_ls l then l else no_label
36556
81dc2c20f052 use readable names in "debug" mode for type vars + don't pipe facts using "using" but rather give them directly to metis (works better with type variables)
blanchet
parents: 36555
diff changeset
   773
    fun do_step (Assume (l, t)) = Assume (do_label l, t)
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   774
      | do_step (Prove (qs, l, t, by)) =
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   775
        Prove (qs, do_label l, t,
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   776
               case by of
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   777
                 Case_Split (proofs, facts) =>
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   778
                 Case_Split (map (map do_step) proofs, facts)
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   779
               | _ => by)
36556
81dc2c20f052 use readable names in "debug" mode for type vars + don't pipe facts using "using" but rather give them directly to metis (works better with type variables)
blanchet
parents: 36555
diff changeset
   780
      | do_step step = step
81dc2c20f052 use readable names in "debug" mode for type vars + don't pipe facts using "using" but rather give them directly to metis (works better with type variables)
blanchet
parents: 36555
diff changeset
   781
  in map do_step proof end
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   782
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   783
fun prefix_for_depth n = replicate_string (n + 1)
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   784
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   785
val relabel_proof =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   786
  let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   787
    fun aux _ _ _ [] = []
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   788
      | aux subst depth (next_assum, next_fact) (Assume (l, t) :: proof) =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   789
        if l = no_label then
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   790
          Assume (l, t) :: aux subst depth (next_assum, next_fact) proof
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   791
        else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   792
          let val l' = (prefix_for_depth depth assum_prefix, next_assum) in
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   793
            Assume (l', t) ::
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   794
            aux ((l, l') :: subst) depth (next_assum + 1, next_fact) proof
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   795
          end
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   796
      | aux subst depth (next_assum, next_fact)
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   797
            (Prove (qs, l, t, by) :: proof) =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   798
        let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   799
          val (l', subst, next_fact) =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   800
            if l = no_label then
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   801
              (l, subst, next_fact)
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   802
            else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   803
              let
42180
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   804
                val l' = (prefix_for_depth depth have_prefix, next_fact)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   805
              in (l', (l, l') :: subst, next_fact + 1) end
36570
9bebcb40599f identify axioms/conjectures more reliably in ATP proofs (an empty dependency list doesn't always indicate an axiom or conjecture!)
blanchet
parents: 36567
diff changeset
   806
          val relabel_facts =
39370
f8292d3020db use same hack as in "Async_Manager" to work around Proof General bug
blanchet
parents: 39368
diff changeset
   807
            apfst (maps (the_list o AList.lookup (op =) subst))
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   808
          val by =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   809
            case by of
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   810
              By_Metis facts => By_Metis (relabel_facts facts)
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   811
            | Case_Split (proofs, facts) =>
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   812
              Case_Split (map (aux subst (depth + 1) (1, 1)) proofs,
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   813
                          relabel_facts facts)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   814
        in
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   815
          Prove (qs, l', t, by) :: aux subst depth (next_assum, next_fact) proof
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   816
        end
36491
6769f8eacaac unskolemize formulas in proof reconstruction + detect newer SPASS versions to avoid truncating identifiers if not necessary (truncating confuses proof reconstruction)
blanchet
parents: 36488
diff changeset
   817
      | aux subst depth nextp (step :: proof) =
6769f8eacaac unskolemize formulas in proof reconstruction + detect newer SPASS versions to avoid truncating identifiers if not necessary (truncating confuses proof reconstruction)
blanchet
parents: 36488
diff changeset
   818
        step :: aux subst depth nextp proof
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   819
  in aux [] 0 (1, 1) end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   820
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   821
fun string_for_proof ctxt0 type_enc lam_trans i n =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   822
  let
47920
a5c2386518e2 ensure consistent naming of Waldmeister proof steps, so that they are not cleaned away by "clean_up_atp_proof_dependencies"
blanchet
parents: 47918
diff changeset
   823
    val ctxt = ctxt0
a5c2386518e2 ensure consistent naming of Waldmeister proof steps, so that they are not cleaned away by "clean_up_atp_proof_dependencies"
blanchet
parents: 47918
diff changeset
   824
(* FIXME: Implement proper handling of type constraints:
a5c2386518e2 ensure consistent naming of Waldmeister proof steps, so that they are not cleaned away by "clean_up_atp_proof_dependencies"
blanchet
parents: 47918
diff changeset
   825
      |> Config.put show_free_types false
a5c2386518e2 ensure consistent naming of Waldmeister proof steps, so that they are not cleaned away by "clean_up_atp_proof_dependencies"
blanchet
parents: 47918
diff changeset
   826
      |> Config.put show_types false
a5c2386518e2 ensure consistent naming of Waldmeister proof steps, so that they are not cleaned away by "clean_up_atp_proof_dependencies"
blanchet
parents: 47918
diff changeset
   827
      |> Config.put show_sorts false
a5c2386518e2 ensure consistent naming of Waldmeister proof steps, so that they are not cleaned away by "clean_up_atp_proof_dependencies"
blanchet
parents: 47918
diff changeset
   828
*)
37319
42268dc7d6c4 show types in Isar proofs, but not for free variables;
blanchet
parents: 37172
diff changeset
   829
    fun fix_print_mode f x =
39134
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39115
diff changeset
   830
      Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN)
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39115
diff changeset
   831
                               (print_mode_value ())) f x
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   832
    fun do_indent ind = replicate_string (ind * indent_size) " "
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   833
    fun do_free (s, T) =
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   834
      maybe_quote s ^ " :: " ^
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   835
      maybe_quote (fix_print_mode (Syntax.string_of_typ ctxt) T)
36570
9bebcb40599f identify axioms/conjectures more reliably in ATP proofs (an empty dependency list doesn't always indicate an axiom or conjecture!)
blanchet
parents: 36567
diff changeset
   836
    fun do_label l = if l = no_label then "" else string_for_label l ^ ": "
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   837
    fun do_have qs =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   838
      (if member (op =) qs Moreover then "moreover " else "") ^
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   839
      (if member (op =) qs Ultimately then "ultimately " else "") ^
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   840
      (if member (op =) qs Then then
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   841
         if member (op =) qs Show then "thus" else "hence"
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   842
       else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   843
         if member (op =) qs Show then "show" else "have")
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   844
    val do_term = maybe_quote o fix_print_mode (Syntax.string_of_term ctxt)
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   845
    val reconstr = Metis (type_enc, lam_trans)
36570
9bebcb40599f identify axioms/conjectures more reliably in ATP proofs (an empty dependency list doesn't always indicate an axiom or conjecture!)
blanchet
parents: 36567
diff changeset
   846
    fun do_facts (ls, ss) =
45520
2b1dde0b1c30 thread in additional options to minimizer
blanchet
parents: 45519
diff changeset
   847
      reconstructor_command reconstr 1 1
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   848
          (ls |> sort_distinct (prod_ord string_ord int_ord),
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   849
           ss |> sort_distinct string_ord)
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   850
    and do_step ind (Fix xs) =
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   851
        do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n"
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   852
      | do_step ind (Let (t1, t2)) =
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   853
        do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n"
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   854
      | do_step ind (Assume (l, t)) =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   855
        do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n"
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   856
      | do_step ind (Prove (qs, l, t, By_Metis facts)) =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   857
        do_indent ind ^ do_have qs ^ " " ^
36479
fcbf412c560f reintroduce missing "gen_all_vars" call
blanchet
parents: 36478
diff changeset
   858
        do_label l ^ do_term t ^ " " ^ do_facts facts ^ "\n"
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   859
      | do_step ind (Prove (qs, l, t, Case_Split (proofs, facts))) =
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   860
        implode (map (prefix (do_indent ind ^ "moreover\n") o do_block ind)
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   861
                     proofs) ^
36479
fcbf412c560f reintroduce missing "gen_all_vars" call
blanchet
parents: 36478
diff changeset
   862
        do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ " " ^
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   863
        do_facts facts ^ "\n"
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   864
    and do_steps prefix suffix ind steps =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   865
      let val s = implode (map (do_step ind) steps) in
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   866
        replicate_string (ind * indent_size - size prefix) " " ^ prefix ^
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   867
        String.extract (s, ind * indent_size,
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   868
                        SOME (size s - ind * indent_size - 1)) ^
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   869
        suffix ^ "\n"
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   870
      end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   871
    and do_block ind proof = do_steps "{ " " }" (ind + 1) proof
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   872
    (* One-step proofs are pointless; better use the Metis one-liner
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   873
       directly. *)
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   874
    and do_proof [Prove (_, _, _, By_Metis _)] = ""
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   875
      | do_proof proof =
36480
1e01a7162435 polish Isar proofs: don't mention facts twice, and don't show one-liner "structured" proofs
blanchet
parents: 36479
diff changeset
   876
        (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
   877
        do_indent 0 ^ "proof -\n" ^ do_steps "" "" 1 proof ^ do_indent 0 ^
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
   878
        (if n <> 1 then "next" else "qed")
36488
32c92af68ec9 remove Sledgehammer's "sorts" option to annotate variables with sorts in proof;
blanchet
parents: 36486
diff changeset
   879
  in do_proof end
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   880
43062
2834548a7a48 nicer failure message when one-line proof reconstruction fails
blanchet
parents: 43051
diff changeset
   881
fun isar_proof_text ctxt isar_proof_requested
45552
d2139b4557fc removed more clutter
blanchet
parents: 45551
diff changeset
   882
        (debug, isar_shrink_factor, pool, fact_names, sym_tab, atp_proof, goal)
43037
ade5c84f860f cleanup proof text generation code
blanchet
parents: 43034
diff changeset
   883
        (one_line_params as (_, _, _, _, subgoal, subgoal_count)) =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   884
  let
43062
2834548a7a48 nicer failure message when one-line proof reconstruction fails
blanchet
parents: 43051
diff changeset
   885
    val isar_shrink_factor =
2834548a7a48 nicer failure message when one-line proof reconstruction fails
blanchet
parents: 43051
diff changeset
   886
      (if isar_proof_requested then 1 else 2) * isar_shrink_factor
43037
ade5c84f860f cleanup proof text generation code
blanchet
parents: 43034
diff changeset
   887
    val (params, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   888
    val frees = fold Term.add_frees (concl_t :: hyp_ts) []
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   889
    val one_line_proof = one_line_proof_text one_line_params
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   890
    val type_enc =
45590
dc9a7ff13e37 made SML/NJ happy
blanchet
parents: 45579
diff changeset
   891
      if is_typed_helper_used_in_atp_proof atp_proof then full_typesN
45554
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   892
      else partial_typesN
09ad83de849c don't pass "lam_lifted" option to "metis" unless there's a good reason
blanchet
parents: 45553
diff changeset
   893
    val lam_trans = lam_trans_from_atp_proof atp_proof metis_default_lam_trans
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   894
45883
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   895
    fun isar_proof_of () =
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   896
      let
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   897
        val atp_proof =
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   898
          atp_proof
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   899
          |> clean_up_atp_proof_dependencies
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   900
          |> nasty_atp_proof pool
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   901
          |> map_term_names_in_atp_proof repair_name
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   902
          |> decode_lines ctxt sym_tab
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   903
          |> rpair [] |-> fold_rev (add_line fact_names)
47918
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   904
          |> repair_waldmeister_endgame
45883
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   905
          |> rpair [] |-> fold_rev add_nontrivial_line
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   906
          |> rpair (0, [])
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   907
          |-> fold_rev (add_desired_line isar_shrink_factor fact_names frees)
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   908
          |> snd
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   909
        val conj_name = conjecture_prefix ^ string_of_int (length hyp_ts)
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   910
        val conjs =
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   911
          atp_proof
47774
blanchet
parents: 47147
diff changeset
   912
          |> map_filter (fn Inference_Step (name as (_, ss), _, _, []) =>
45883
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   913
                            if member (op =) ss conj_name then SOME name else NONE
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   914
                          | _ => NONE)
47774
blanchet
parents: 47147
diff changeset
   915
        fun dep_of_step (Definition_Step _) = NONE
blanchet
parents: 47147
diff changeset
   916
          | dep_of_step (Inference_Step (name, _, _, from)) = SOME (from, name)
45883
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   917
        val ref_graph = atp_proof |> map_filter dep_of_step |> make_ref_graph
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   918
        val axioms = axioms_of_ref_graph ref_graph conjs
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   919
        val tainted = tainted_atoms_of_ref_graph ref_graph conjs
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   920
        val props =
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   921
          Symtab.empty
47774
blanchet
parents: 47147
diff changeset
   922
          |> fold (fn Definition_Step _ => I (* FIXME *)
blanchet
parents: 47147
diff changeset
   923
                    | Inference_Step ((s, _), t, _, _) =>
45883
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   924
                      Symtab.update_new (s,
47921
fc26d5538868 ensure the "show" equation is not reoriented by Waldmeister
blanchet
parents: 47920
diff changeset
   925
                          t |> fold forall_of (map Var (Term.add_vars t []))
47918
81ae96996223 graceful handling of Waldmeister endgame
blanchet
parents: 47774
diff changeset
   926
                            |> member (op = o apsnd fst) tainted s ? s_not))
45883
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   927
                  atp_proof
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   928
        fun prop_of_clause c =
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   929
          fold (curry s_disj) (map_filter (Symtab.lookup props o fst) c)
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   930
               @{term False}
47920
a5c2386518e2 ensure consistent naming of Waldmeister proof steps, so that they are not cleaned away by "clean_up_atp_proof_dependencies"
blanchet
parents: 47918
diff changeset
   931
        fun label_of_clause [name] = raw_label_for_name name
a5c2386518e2 ensure consistent naming of Waldmeister proof steps, so that they are not cleaned away by "clean_up_atp_proof_dependencies"
blanchet
parents: 47918
diff changeset
   932
          | label_of_clause c = (space_implode "___" (map fst c), 0)
45883
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   933
        fun maybe_show outer c =
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   934
          (outer andalso length c = 1 andalso subset (op =) (c, conjs))
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   935
          ? cons Show
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   936
        fun do_have outer qs (gamma, c) =
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   937
          Prove (maybe_show outer c qs, label_of_clause c, prop_of_clause c,
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   938
                 By_Metis (fold (add_fact_from_dependency fact_names
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   939
                                 o the_single) gamma ([], [])))
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   940
        fun do_inf outer (Have z) = do_have outer [] z
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   941
          | do_inf outer (Hence z) = do_have outer [Then] z
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   942
          | do_inf outer (Cases cases) =
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   943
            let val c = succedent_of_cases cases in
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   944
              Prove (maybe_show outer c [Ultimately], label_of_clause c,
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   945
                     prop_of_clause c,
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   946
                     Case_Split (map (do_case false) cases, ([], [])))
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   947
            end
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   948
        and do_case outer (c, infs) =
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   949
          Assume (label_of_clause c, prop_of_clause c) ::
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   950
          map (do_inf outer) infs
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   951
        val isar_proof =
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   952
          (if null params then [] else [Fix params]) @
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   953
          (ref_graph
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   954
           |> redirect_graph axioms tainted
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   955
           |> chain_direct_proof
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   956
           |> map (do_inf true)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   957
           |> kill_duplicate_assumptions_in_proof
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   958
           |> kill_useless_labels_in_proof
45883
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   959
           |> relabel_proof)
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   960
          |> string_for_proof ctxt type_enc lam_trans subgoal subgoal_count
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   961
      in
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   962
        case isar_proof of
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   963
          "" =>
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   964
          if isar_proof_requested then
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   965
            "\nNo structured proof available (proof too short)."
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   966
          else
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   967
            ""
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   968
        | _ =>
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   969
          "\n\n" ^ (if isar_proof_requested then "Structured proof"
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   970
                    else "Perhaps this will work") ^
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   971
          ":\n" ^ Markup.markup Isabelle_Markup.sendback isar_proof
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   972
      end
35868
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   973
    val isar_proof =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   974
      if debug then
45883
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   975
        isar_proof_of ()
cf7ef3fca5e4 killed dead code
blanchet
parents: 45882
diff changeset
   976
      else case try isar_proof_of () of
45882
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   977
        SOME s => s
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   978
      | NONE => if isar_proof_requested then
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   979
                  "\nWarning: The Isar proof construction failed."
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   980
                else
5d8a7fe36ce5 use new redirection algorithm in Sledgehammer
blanchet
parents: 45740
diff changeset
   981
                  ""
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   982
  in one_line_proof ^ isar_proof end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   983
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   984
fun proof_text ctxt isar_proof isar_params
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   985
               (one_line_params as (preplay, _, _, _, _, _)) =
43166
68e3cd19fee8 show what failed to play
blanchet
parents: 43163
diff changeset
   986
  (if case preplay of Failed_to_Play _ => true | _ => isar_proof then
43062
2834548a7a48 nicer failure message when one-line proof reconstruction fails
blanchet
parents: 43051
diff changeset
   987
     isar_proof_text ctxt isar_proof isar_params
43033
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   988
   else
c4b9b4be90c4 show time taken for reconstruction
blanchet
parents: 43006
diff changeset
   989
     one_line_proof_text) one_line_params
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
   990
31038
immler@in.tum.de
parents: 31037
diff changeset
   991
end;