src/HOL/Tools/Sledgehammer/sledgehammer_atp_reconstruct.ML
author blanchet
Thu, 12 May 2011 15:29:19 +0200
changeset 42759 fdd15e889ad7
parent 42755 4603154a3018
child 42761 8ea9c6fa8b53
permissions -rw-r--r--
no need to use metisFT for Isar proofs -- metis falls back on it anyway
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40114
blanchet
parents: 40074
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_atp_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
39495
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents: 39494
diff changeset
     6
Proof reconstruction for Sledgehammer.
33310
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
     7
*)
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
     8
40068
ed2869dd9bfa renamed modules
blanchet
parents: 40067
diff changeset
     9
signature SLEDGEHAMMER_ATP_RECONSTRUCT =
24425
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
    10
sig
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
    11
  type 'a proof = 'a ATP_Proof.proof
38988
483879af0643 finished renaming
blanchet
parents: 38986
diff changeset
    12
  type locality = Sledgehammer_Filter.locality
41136
30bedf58b177 implemented new type system encoding "overload_args", which is more lightweight than "const_args" (the unsound default) and hopefully almost as sound
blanchet
parents: 40723
diff changeset
    13
  type type_system = Sledgehammer_ATP_Translate.type_system
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    14
  type minimize_command = string list -> string
38818
61cf050f8b2e improve SPASS hack, when a clause comes from several facts
blanchet
parents: 38752
diff changeset
    15
  type metis_params =
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
    16
    string * minimize_command * type_system * string proof * int
41136
30bedf58b177 implemented new type system encoding "overload_args", which is more lightweight than "const_args" (the unsound default) and hopefully almost as sound
blanchet
parents: 40723
diff changeset
    17
    * (string * locality) list vector * thm * int
38818
61cf050f8b2e improve SPASS hack, when a clause comes from several facts
blanchet
parents: 38752
diff changeset
    18
  type isar_params =
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
    19
    Proof.context * bool * int * string Symtab.table * int list list
4603154a3018 robustly detect how many type args were passed to the ATP, even if some of them were omitted
blanchet
parents: 42751
diff changeset
    20
    * int Symtab.table
38818
61cf050f8b2e improve SPASS hack, when a clause comes from several facts
blanchet
parents: 38752
diff changeset
    21
  type text_result = string * (string * locality) list
61cf050f8b2e improve SPASS hack, when a clause comes from several facts
blanchet
parents: 38752
diff changeset
    22
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
    23
  val repair_conjecture_shape_and_fact_names :
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
    24
    type_system -> string -> int list list -> int
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
    25
    -> (string * locality) list vector
42587
4fbb1de05169 fixed SPASS fact offset calculation and report unexpected unsound proofs with so-called sound encodings
blanchet
parents: 42579
diff changeset
    26
    -> int list list * int * (string * locality) list vector
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
    27
  val used_facts_in_atp_proof :
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
    28
    type_system -> int -> (string * locality) list vector -> string proof
42541
8938507b2054 move type declarations to the front, for TFF-compliance
blanchet
parents: 42539
diff changeset
    29
    -> (string * locality) list
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
    30
  val is_unsound_proof :
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
    31
    type_system -> int list list -> int -> (string * locality) list vector
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
    32
    -> string proof -> bool
41151
d58157bb1ae7 generate a "using [[smt_solver = ...]]" command if a proof is found by another SMT solver than the current one, to ensure it's also used for reconstruction
blanchet
parents: 41146
diff changeset
    33
  val apply_on_subgoal : string -> int -> int -> string
40060
5ef6747aa619 first step in adding support for an SMT backend to Sledgehammer
blanchet
parents: 39953
diff changeset
    34
  val command_call : string -> string list -> string
40064
db8413d82c3b got rid of duplicate functionality ("run_smt_solver_somehow");
blanchet
parents: 40060
diff changeset
    35
  val try_command_line : string -> string -> string
db8413d82c3b got rid of duplicate functionality ("run_smt_solver_somehow");
blanchet
parents: 40060
diff changeset
    36
  val minimize_line : ('a list -> string) -> 'a list -> string
40723
a82badd0e6ef put facts found by SMT solver in alphabetical order and omit chained facts, as was done already for ATP proofs
blanchet
parents: 40627
diff changeset
    37
  val split_used_facts :
a82badd0e6ef put facts found by SMT solver in alphabetical order and omit chained facts, as was done already for ATP proofs
blanchet
parents: 40627
diff changeset
    38
    (string * locality) list
a82badd0e6ef put facts found by SMT solver in alphabetical order and omit chained facts, as was done already for ATP proofs
blanchet
parents: 40627
diff changeset
    39
    -> (string * locality) list * (string * locality) list
41742
11e862c68b40 automatically minimize Z3-as-an-ATP proofs (cf. CVC3 and Yices)
blanchet
parents: 41203
diff changeset
    40
  val metis_proof_text : metis_params -> text_result
38818
61cf050f8b2e improve SPASS hack, when a clause comes from several facts
blanchet
parents: 38752
diff changeset
    41
  val isar_proof_text : isar_params -> metis_params -> text_result
61cf050f8b2e improve SPASS hack, when a clause comes from several facts
blanchet
parents: 38752
diff changeset
    42
  val proof_text : bool -> isar_params -> metis_params -> text_result
24425
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
    43
end;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    44
40068
ed2869dd9bfa renamed modules
blanchet
parents: 40067
diff changeset
    45
structure Sledgehammer_ATP_Reconstruct : SLEDGEHAMMER_ATP_RECONSTRUCT =
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    46
struct
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    47
38028
22dcaec5fa77 minor refactoring
blanchet
parents: 38027
diff changeset
    48
open ATP_Problem
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
    49
open ATP_Proof
39494
bf7dd4902321 rename "Metis_Clauses" to "Metis_Translate" for consistency with "Sledgehammer_Translate"
blanchet
parents: 39493
diff changeset
    50
open Metis_Translate
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
    51
open Sledgehammer_Util
38988
483879af0643 finished renaming
blanchet
parents: 38986
diff changeset
    52
open Sledgehammer_Filter
40074
87edcef4fab0 compile
blanchet
parents: 40069
diff changeset
    53
open Sledgehammer_ATP_Translate
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    54
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    55
type minimize_command = string list -> string
38818
61cf050f8b2e improve SPASS hack, when a clause comes from several facts
blanchet
parents: 38752
diff changeset
    56
type metis_params =
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
    57
  string * minimize_command * type_system * string proof * int
41136
30bedf58b177 implemented new type system encoding "overload_args", which is more lightweight than "const_args" (the unsound default) and hopefully almost as sound
blanchet
parents: 40723
diff changeset
    58
  * (string * locality) list vector * thm * int
38818
61cf050f8b2e improve SPASS hack, when a clause comes from several facts
blanchet
parents: 38752
diff changeset
    59
type isar_params =
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
    60
  Proof.context * bool * int * string Symtab.table * int list list
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
    61
  * int Symtab.table
38818
61cf050f8b2e improve SPASS hack, when a clause comes from several facts
blanchet
parents: 38752
diff changeset
    62
type text_result = string * (string * locality) list
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    63
39500
d91ef7fbc500 move functions around
blanchet
parents: 39495
diff changeset
    64
fun is_head_digit s = Char.isDigit (String.sub (s, 0))
d91ef7fbc500 move functions around
blanchet
parents: 39495
diff changeset
    65
val scan_integer = Scan.many1 is_head_digit >> (the o Int.fromString o implode)
d91ef7fbc500 move functions around
blanchet
parents: 39495
diff changeset
    66
d91ef7fbc500 move functions around
blanchet
parents: 39495
diff changeset
    67
fun find_first_in_list_vector vec key =
d91ef7fbc500 move functions around
blanchet
parents: 39495
diff changeset
    68
  Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key
d91ef7fbc500 move functions around
blanchet
parents: 39495
diff changeset
    69
                 | (_, value) => value) NONE vec
d91ef7fbc500 move functions around
blanchet
parents: 39495
diff changeset
    70
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
    71
39493
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    72
(** SPASS's Flotter hack **)
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    73
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
    74
(* This is a hack required for keeping track of facts after they have been
39493
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    75
   clausified by SPASS's Flotter tool. The "ATP/scripts/spass" script is also
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    76
   part of this hack. *)
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    77
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    78
val set_ClauseFormulaRelationN = "set_ClauseFormulaRelation"
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    79
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    80
fun extract_clause_sequence output =
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    81
  let
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    82
    val tokens_of = String.tokens (not o Char.isAlphaNum)
42587
4fbb1de05169 fixed SPASS fact offset calculation and report unexpected unsound proofs with so-called sound encodings
blanchet
parents: 42579
diff changeset
    83
    fun extract_num ("clause" :: (ss as _ :: _)) = Int.fromString (List.last ss)
39493
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    84
      | extract_num _ = NONE
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    85
  in output |> split_lines |> map_filter (extract_num o tokens_of) end
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    86
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    87
val parse_clause_formula_pair =
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    88
  $$ "(" |-- scan_integer --| $$ ","
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    89
  -- (Symbol.scan_id ::: Scan.repeat ($$ "," |-- Symbol.scan_id)) --| $$ ")"
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    90
  --| Scan.option ($$ ",")
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    91
val parse_clause_formula_relation =
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    92
  Scan.this_string set_ClauseFormulaRelationN |-- $$ "("
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    93
  |-- Scan.repeat parse_clause_formula_pair
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    94
val extract_clause_formula_relation =
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    95
  Substring.full #> Substring.position set_ClauseFormulaRelationN
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    96
  #> snd #> Substring.position "." #> fst #> Substring.string
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 40204
diff changeset
    97
  #> raw_explode #> filter_out Symbol.is_blank #> parse_clause_formula_relation
39493
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    98
  #> fst
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
    99
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   100
fun maybe_unprefix_fact_number type_sys =
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   101
  polymorphism_of_type_sys type_sys <> Polymorphic
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   102
  ? (space_implode "_" o tl o space_explode "_")
42180
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   103
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   104
fun repair_conjecture_shape_and_fact_names type_sys output conjecture_shape
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   105
                                           fact_offset fact_names =
39493
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   106
  if String.isSubstring set_ClauseFormulaRelationN output then
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   107
    let
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   108
      val j0 = hd (hd conjecture_shape)
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   109
      val seq = extract_clause_sequence output
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   110
      val name_map = extract_clause_formula_relation output
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   111
      fun renumber_conjecture j =
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   112
        conjecture_prefix ^ string_of_int (j - j0)
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   113
        |> AList.find (fn (s, ss) => member (op =) ss s) name_map
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   114
        |> map (fn s => find_index (curry (op =) s) seq + 1)
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   115
      fun names_for_number j =
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   116
        j |> AList.lookup (op =) name_map |> these
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   117
          |> map_filter (try (unascii_of o maybe_unprefix_fact_number type_sys
42180
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   118
                              o unprefix fact_prefix))
39493
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   119
          |> map (fn name =>
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
   120
                     (name, name |> find_first_in_list_vector fact_names |> the)
39493
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   121
                     handle Option.Option =>
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   122
                            error ("No such fact: " ^ quote name ^ "."))
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   123
    in
42587
4fbb1de05169 fixed SPASS fact offset calculation and report unexpected unsound proofs with so-called sound encodings
blanchet
parents: 42579
diff changeset
   124
      (conjecture_shape |> map (maps renumber_conjecture), 0,
39493
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   125
       seq |> map names_for_number |> Vector.fromList)
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   126
    end
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   127
  else
42587
4fbb1de05169 fixed SPASS fact offset calculation and report unexpected unsound proofs with so-called sound encodings
blanchet
parents: 42579
diff changeset
   128
    (conjecture_shape, fact_offset, fact_names)
39493
cb2208f2c07d move SPASS's Flotter hack to "Sledgehammer_Reconstruct"
blanchet
parents: 39455
diff changeset
   129
42180
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   130
val vampire_step_prefix = "f" (* grrr... *)
41203
1393514094d7 fixed more issues with the Vampire output parser, and added support for Vampire's TSTP output (--proof tptp)
blanchet
parents: 41151
diff changeset
   131
42751
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   132
fun resolve_fact type_sys _ fact_names (_, SOME s) =
42180
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   133
    (case try (unprefix fact_prefix) s of
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   134
       SOME s' =>
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   135
       let val s' = s' |> maybe_unprefix_fact_number type_sys |> unascii_of in
42180
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   136
         case find_first_in_list_vector fact_names s' of
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   137
           SOME x => [(s', x)]
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   138
         | NONE => []
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   139
       end
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   140
     | NONE => [])
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   141
  | resolve_fact _ facts_offset fact_names (num, NONE) =
42180
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   142
    case Int.fromString (perhaps (try (unprefix vampire_step_prefix)) num) of
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   143
      SOME j =>
42541
8938507b2054 move type declarations to the front, for TFF-compliance
blanchet
parents: 42539
diff changeset
   144
      let val j = j - facts_offset in
8938507b2054 move type declarations to the front, for TFF-compliance
blanchet
parents: 42539
diff changeset
   145
        if j > 0 andalso j <= Vector.length fact_names then
8938507b2054 move type declarations to the front, for TFF-compliance
blanchet
parents: 42539
diff changeset
   146
          Vector.sub (fact_names, j - 1)
8938507b2054 move type declarations to the front, for TFF-compliance
blanchet
parents: 42539
diff changeset
   147
        else
8938507b2054 move type declarations to the front, for TFF-compliance
blanchet
parents: 42539
diff changeset
   148
          []
8938507b2054 move type declarations to the front, for TFF-compliance
blanchet
parents: 42539
diff changeset
   149
      end
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   150
    | NONE => []
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   151
42751
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   152
fun resolve_conjecture _ (_, SOME s) =
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   153
    (case try (unprefix conjecture_prefix) s of
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   154
       SOME s' =>
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   155
       (case Int.fromString s' of
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   156
          SOME j => [j]
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   157
        | NONE => [])
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   158
     | NONE => [])
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   159
  | resolve_conjecture conjecture_shape (num, NONE) =
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   160
    case Int.fromString (perhaps (try (unprefix vampire_step_prefix)) num) of
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   161
      SOME i =>
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   162
      (case find_index (exists (curry (op =) i)) conjecture_shape of
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   163
         ~1 => []
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   164
       | j => [j])
f42fd9754724 fixed conjecture resolution bug for Vampire 1.0's TSTP output
blanchet
parents: 42680
diff changeset
   165
    | NONE => []
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   166
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   167
fun is_fact type_sys conjecture_shape =
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   168
  not o null o resolve_fact type_sys 0 conjecture_shape
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   169
fun is_conjecture conjecture_shape =
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   170
  not o null o resolve_conjecture conjecture_shape
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   171
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   172
fun add_fact type_sys facts_offset fact_names (Inference (name, _, [])) =
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   173
    append (resolve_fact type_sys facts_offset fact_names name)
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   174
  | add_fact _ _ _ _ = I
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   175
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   176
fun used_facts_in_atp_proof type_sys facts_offset fact_names atp_proof =
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   177
  if null atp_proof then Vector.foldl (op @) [] fact_names
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   178
  else fold (add_fact type_sys facts_offset fact_names) atp_proof []
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   179
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   180
fun is_conjecture_referred_to_in_proof conjecture_shape =
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   181
  exists (fn Inference (name, _, []) => is_conjecture conjecture_shape name
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   182
           | _ => false)
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   183
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   184
fun is_unsound_proof type_sys conjecture_shape facts_offset fact_names =
42541
8938507b2054 move type declarations to the front, for TFF-compliance
blanchet
parents: 42539
diff changeset
   185
  not o is_conjecture_referred_to_in_proof conjecture_shape andf
42680
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42647
diff changeset
   186
  forall (is_locality_global o snd)
b6c27cf04fe9 exploit inferred monotonicity
blanchet
parents: 42647
diff changeset
   187
    o used_facts_in_atp_proof type_sys facts_offset fact_names
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   188
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   189
(** Soft-core proof reconstruction: Metis one-liner **)
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   190
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   191
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
   192
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   193
fun set_settings "" = ""
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   194
  | set_settings settings = "using [[" ^ settings ^ "]] "
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   195
fun apply_on_subgoal settings _ 1 = set_settings settings ^ "by "
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   196
  | apply_on_subgoal settings 1 _ = set_settings settings ^ "apply "
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   197
  | apply_on_subgoal settings i n =
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   198
    "prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal settings 1 n
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   199
fun command_call name [] = name
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   200
  | command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")"
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   201
fun try_command_line banner command =
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   202
  banner ^ ": " ^ Markup.markup Markup.sendback command ^ "."
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   203
fun using_labels [] = ""
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   204
  | using_labels ls =
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   205
    "using " ^ space_implode " " (map string_for_label ls) ^ " "
42593
f9d7f1331a00 use "metis", not "metisFT", to reconstruct proofs found in fully-typed mode -- "metisFT" is just too slow...
blanchet
parents: 42589
diff changeset
   206
fun metis_name full_types = if full_types then "metisFT" else "metis"
42595
blanchet
parents: 42593
diff changeset
   207
fun metis_call full_types ss = command_call (metis_name full_types) ss
42593
f9d7f1331a00 use "metis", not "metisFT", to reconstruct proofs found in fully-typed mode -- "metisFT" is just too slow...
blanchet
parents: 42589
diff changeset
   208
fun metis_command full_types i n (ls, ss) =
f9d7f1331a00 use "metis", not "metisFT", to reconstruct proofs found in fully-typed mode -- "metisFT" is just too slow...
blanchet
parents: 42589
diff changeset
   209
  using_labels ls ^ apply_on_subgoal "" i n ^ metis_call full_types ss
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   210
fun minimize_line _ [] = ""
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   211
  | minimize_line minimize_command ss =
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   212
    case minimize_command ss of
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   213
      "" => ""
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   214
    | command =>
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   215
      "\nTo minimize the number of lemmas, try this: " ^
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   216
      Markup.markup Markup.sendback command ^ "."
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   217
40723
a82badd0e6ef put facts found by SMT solver in alphabetical order and omit chained facts, as was done already for ATP proofs
blanchet
parents: 40627
diff changeset
   218
val split_used_facts =
a82badd0e6ef put facts found by SMT solver in alphabetical order and omit chained facts, as was done already for ATP proofs
blanchet
parents: 40627
diff changeset
   219
  List.partition (curry (op =) Chained o snd)
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   220
  #> pairself (sort_distinct (string_ord o pairself fst))
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   221
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   222
fun metis_proof_text (banner, minimize_command, type_sys, atp_proof,
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   223
                      facts_offset, fact_names, goal, i) =
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   224
  let
42593
f9d7f1331a00 use "metis", not "metisFT", to reconstruct proofs found in fully-typed mode -- "metisFT" is just too slow...
blanchet
parents: 42589
diff changeset
   225
    val (chained, extra) =
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   226
      atp_proof |> used_facts_in_atp_proof type_sys facts_offset fact_names
42541
8938507b2054 move type declarations to the front, for TFF-compliance
blanchet
parents: 42539
diff changeset
   227
                |> split_used_facts
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   228
    val n = Logic.count_prems (prop_of goal)
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   229
  in
42593
f9d7f1331a00 use "metis", not "metisFT", to reconstruct proofs found in fully-typed mode -- "metisFT" is just too slow...
blanchet
parents: 42589
diff changeset
   230
    (try_command_line banner (metis_command false i n ([], map fst extra)) ^
f9d7f1331a00 use "metis", not "metisFT", to reconstruct proofs found in fully-typed mode -- "metisFT" is just too slow...
blanchet
parents: 42589
diff changeset
   231
     minimize_line minimize_command (map fst (extra @ chained)),
f9d7f1331a00 use "metis", not "metisFT", to reconstruct proofs found in fully-typed mode -- "metisFT" is just too slow...
blanchet
parents: 42589
diff changeset
   232
     extra @ chained)
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   233
  end
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   234
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   235
(** 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
   236
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   237
(* Simple simplifications to ensure that sort annotations don't leave a trail of
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   238
   spurious "True"s. *)
42606
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   239
fun s_not (Const (@{const_name All}, T) $ Abs (s, T', t')) =
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   240
    Const (@{const_name Ex}, T) $ Abs (s, T', s_not t')
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   241
  | s_not (Const (@{const_name Ex}, T) $ Abs (s, T', t')) =
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   242
    Const (@{const_name All}, T) $ Abs (s, T', s_not t')
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   243
  | s_not (@{const HOL.implies} $ t1 $ t2) = @{const HOL.conj} $ t1 $ s_not t2
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   244
  | s_not (@{const HOL.conj} $ t1 $ t2) =
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   245
    @{const HOL.disj} $ s_not t1 $ s_not t2
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   246
  | s_not (@{const HOL.disj} $ t1 $ t2) =
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   247
    @{const HOL.conj} $ s_not t1 $ s_not t2
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   248
  | s_not (@{const False}) = @{const True}
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   249
  | s_not (@{const True}) = @{const False}
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   250
  | s_not (@{const Not} $ t) = t
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   251
  | s_not t = @{const Not} $ t
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   252
fun s_conj (@{const True}, t2) = t2
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   253
  | s_conj (t1, @{const True}) = t1
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   254
  | s_conj p = HOLogic.mk_conj p
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   255
fun s_disj (@{const False}, t2) = t2
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   256
  | s_disj (t1, @{const False}) = t1
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   257
  | s_disj p = HOLogic.mk_disj p
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   258
fun s_imp (@{const True}, t2) = t2
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   259
  | s_imp (t1, @{const False}) = s_not t1
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   260
  | s_imp p = HOLogic.mk_imp p
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   261
fun s_iff (@{const True}, t2) = t2
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   262
  | s_iff (t1, @{const True}) = t1
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   263
  | s_iff (t1, t2) = HOLogic.eq_const HOLogic.boolT $ t1 $ t2
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   264
39425
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   265
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
   266
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
   267
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   268
val indent_size = 2
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   269
val no_label = ("", ~1)
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   270
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   271
val raw_prefix = "X"
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   272
val assum_prefix = "A"
42180
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   273
val have_prefix = "F"
39370
f8292d3020db use same hack as in "Async_Manager" to work around Proof General bug
blanchet
parents: 39368
diff changeset
   274
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   275
fun raw_label_for_name conjecture_shape name =
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   276
  case resolve_conjecture conjecture_shape name of
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   277
    [j] => (conjecture_prefix, j)
39455
c6b21584f336 merge constructors
blanchet
parents: 39454
diff changeset
   278
  | _ => case Int.fromString (fst name) of
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   279
           SOME j => (raw_prefix, j)
39455
c6b21584f336 merge constructors
blanchet
parents: 39454
diff changeset
   280
         | NONE => (raw_prefix ^ fst name, 0)
39453
1740a2d6bef9 use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
blanchet
parents: 39452
diff changeset
   281
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   282
(**** INTERPRETATION OF TSTP SYNTAX TREES ****)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   283
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   284
exception FO_TERM of string fo_term list
42531
a462dbaa584f added more rudimentary type support to Sledgehammer's ATP encoding
blanchet
parents: 42526
diff changeset
   285
exception FORMULA of (string, string, string fo_term) formula list
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   286
exception SAME of unit
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   287
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   288
(* 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
   289
   constrained by information from type literals, or by type inference. *)
42563
e70ffe3846d0 improve helper type instantiation code
blanchet
parents: 42562
diff changeset
   290
fun typ_from_fo_term tfrees (u as ATerm (a, us)) =
e70ffe3846d0 improve helper type instantiation code
blanchet
parents: 42562
diff changeset
   291
  let val Ts = map (typ_from_fo_term tfrees) us in
38748
69fea359d3f8 renaming
blanchet
parents: 38738
diff changeset
   292
    case strip_prefix_and_unascii type_const_prefix a of
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   293
      SOME b => Type (invert_const b, Ts)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   294
    | NONE =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   295
      if not (null us) then
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   296
        raise FO_TERM [u]  (* only "tconst"s have type arguments *)
38748
69fea359d3f8 renaming
blanchet
parents: 38738
diff changeset
   297
      else case strip_prefix_and_unascii tfree_prefix a of
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   298
        SOME b =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   299
        let val s = "'" ^ b in
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   300
          TFree (s, AList.lookup (op =) tfrees s |> the_default HOLogic.typeS)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   301
        end
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   302
      | NONE =>
38748
69fea359d3f8 renaming
blanchet
parents: 38738
diff changeset
   303
        case strip_prefix_and_unascii tvar_prefix a of
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   304
          SOME b => TVar (("'" ^ b, 0), HOLogic.typeS)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   305
        | NONE =>
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   306
          (* Variable from the ATP, say "X1" *)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   307
          Type_Infer.param 0 (a, HOLogic.typeS)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   308
  end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   309
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   310
(* Type class literal applied to a type. Returns triple of polarity, class,
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   311
   type. *)
42606
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   312
fun type_constraint_from_term tfrees (u as ATerm (a, us)) =
38748
69fea359d3f8 renaming
blanchet
parents: 38738
diff changeset
   313
  case (strip_prefix_and_unascii class_prefix a,
42563
e70ffe3846d0 improve helper type instantiation code
blanchet
parents: 42562
diff changeset
   314
        map (typ_from_fo_term tfrees) us) of
42606
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   315
    (SOME b, [T]) => (b, T)
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   316
  | _ => raise FO_TERM [u]
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   317
38085
cc44e887246c avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents: 38066
diff changeset
   318
(** Accumulate type constraints in a formula: negative type literals **)
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   319
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
   320
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
   321
  | 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
   322
  | add_type_constraint _ _ = I
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   323
38490
blanchet
parents: 38488
diff changeset
   324
fun repair_atp_variable_name f s =
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   325
  let
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   326
    fun subscript_name s n = s ^ nat_subscript n
38488
3abda3c76df9 handle E's Skolem constants more gracefully
blanchet
parents: 38282
diff changeset
   327
    val s = String.map f s
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   328
  in
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   329
    case space_explode "_" s of
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   330
      [_] => (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
   331
                (cs1 as _ :: _, cs2 as _ :: _) =>
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   332
                subscript_name (String.implode cs1)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   333
                               (the (Int.fromString (String.implode cs2)))
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   334
              | (_, _) => s)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   335
    | [s1, s2] => (case Int.fromString s2 of
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   336
                     SOME n => subscript_name s1 n
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   337
                   | NONE => s)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   338
    | _ => s
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   339
  end
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   340
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   341
(* First-order translation. No types are known for variables. "HOLogic.typeT"
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   342
   should allow them to be inferred. *)
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
   343
fun raw_term_from_pred thy sym_tab tfrees =
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   344
  let
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   345
    fun aux opt_T extra_us u =
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   346
      case u of
42539
f6ba908b8b27 generate typing for "hBOOL" in "Many_Typed" mode + tuning
blanchet
parents: 42531
diff changeset
   347
        ATerm (a, us) =>
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
   348
        if String.isPrefix tff_type_prefix a then
42551
cd99d6d3027a reconstruct TFF type predicates correctly for ToFoF
blanchet
parents: 42549
diff changeset
   349
          @{const True} (* ignore TFF type information *)
38748
69fea359d3f8 renaming
blanchet
parents: 38738
diff changeset
   350
        else case strip_prefix_and_unascii const_prefix a of
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   351
          SOME "equal" =>
39106
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   352
          let val ts = map (aux NONE []) us in
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   353
            if length ts = 2 andalso hd ts aconv List.last ts then
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   354
              (* Vampire is keen on producing these. *)
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   355
              @{const True}
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   356
            else
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   357
              list_comb (Const (@{const_name HOL.eq}, HOLogic.typeT), ts)
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   358
          end
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
   359
        | SOME s =>
4603154a3018 robustly detect how many type args were passed to the ATP, even if some of them were omitted
blanchet
parents: 42751
diff changeset
   360
          let val (s', mangled_us) = s |> unmangled_const |>> invert_const in
4603154a3018 robustly detect how many type args were passed to the ATP, even if some of them were omitted
blanchet
parents: 42751
diff changeset
   361
            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
   362
              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
   363
                [typ_u, term_u] =>
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
   364
                aux (SOME (typ_from_fo_term tfrees typ_u)) extra_us term_u
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
   365
              | _ => raise FO_TERM us
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
   366
            else if s' = predicator_base then
42549
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   367
              aux (SOME @{typ bool}) [] (hd us)
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
   368
            else if s' = explicit_app_base then
42549
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   369
              aux opt_T (nth us 1 :: extra_us) (hd us)
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
   370
            else if s' = type_pred_base then
42551
cd99d6d3027a reconstruct TFF type predicates correctly for ToFoF
blanchet
parents: 42549
diff changeset
   371
              @{const True} (* ignore type predicates *)
42549
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   372
            else
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   373
              let
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
   374
                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
   375
                  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
   376
                val (type_us, term_us) =
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   377
                  chop num_ty_args us |>> append mangled_us
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   378
                (* Extra args from "hAPP" come after any arguments given
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   379
                   directly to the constant. *)
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   380
                val term_ts = map (aux NONE []) term_us
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   381
                val extra_ts = map (aux NONE []) extra_us
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   382
                val T =
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   383
                  case opt_T of
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   384
                    SOME T => map fastype_of term_ts ---> T
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   385
                  | NONE =>
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
   386
                    if num_type_args thy s' = length type_us then
42549
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   387
                      Sign.const_instance thy
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
   388
                          (s', map (typ_from_fo_term tfrees) type_us)
42549
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   389
                    else
b9754f26c7bc handle special constants correctly in Isar proof reconstruction code, especially type predicates
blanchet
parents: 42544
diff changeset
   390
                      HOLogic.typeT
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
   391
                val s' = s' |> unproxify_const
4603154a3018 robustly detect how many type args were passed to the ATP, even if some of them were omitted
blanchet
parents: 42751
diff changeset
   392
              in list_comb (Const (s', 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
   393
          end
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   394
        | NONE => (* a free or schematic variable *)
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   395
          let
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   396
            val ts = map (aux NONE []) (us @ extra_us)
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   397
            val T = map fastype_of ts ---> HOLogic.typeT
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   398
            val t =
38748
69fea359d3f8 renaming
blanchet
parents: 38738
diff changeset
   399
              case strip_prefix_and_unascii fixed_var_prefix a of
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   400
                SOME b => Free (b, T)
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   401
              | NONE =>
38748
69fea359d3f8 renaming
blanchet
parents: 38738
diff changeset
   402
                case strip_prefix_and_unascii schematic_var_prefix a 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
   403
                  SOME b => Var ((b, 0), T)
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   404
                | NONE =>
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
   405
                  if is_atp_variable a then
38490
blanchet
parents: 38488
diff changeset
   406
                    Var ((repair_atp_variable_name Char.toLower a, 0), T)
38017
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   407
                  else
38488
3abda3c76df9 handle E's Skolem constants more gracefully
blanchet
parents: 38282
diff changeset
   408
                    (* Skolem constants? *)
38490
blanchet
parents: 38488
diff changeset
   409
                    Var ((repair_atp_variable_name Char.toUpper a, 0), T)
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   410
          in list_comb (t, ts) end
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   411
  in aux (SOME HOLogic.boolT) [] end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   412
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
   413
fun term_from_pred thy sym_tab tfrees pos (u as ATerm (s, _)) =
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   414
  if String.isPrefix class_prefix s then
42606
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   415
    add_type_constraint pos (type_constraint_from_term tfrees u)
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   416
    #> pair @{const True}
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   417
  else
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
   418
    pair (raw_term_from_pred thy sym_tab tfrees u)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   419
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   420
val combinator_table =
39953
aa54f347e5e2 hide uninteresting MESON/Metis constants and facts and remove "meson_" prefix to (now hidden) fact names
blanchet
parents: 39710
diff changeset
   421
  [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def_raw}),
aa54f347e5e2 hide uninteresting MESON/Metis constants and facts and remove "meson_" prefix to (now hidden) fact names
blanchet
parents: 39710
diff changeset
   422
   (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def_raw}),
aa54f347e5e2 hide uninteresting MESON/Metis constants and facts and remove "meson_" prefix to (now hidden) fact names
blanchet
parents: 39710
diff changeset
   423
   (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def_raw}),
aa54f347e5e2 hide uninteresting MESON/Metis constants and facts and remove "meson_" prefix to (now hidden) fact names
blanchet
parents: 39710
diff changeset
   424
   (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def_raw}),
aa54f347e5e2 hide uninteresting MESON/Metis constants and facts and remove "meson_" prefix to (now hidden) fact names
blanchet
parents: 39710
diff changeset
   425
   (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def_raw})]
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   426
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   427
fun uncombine_term (t1 $ t2) = betapply (pairself uncombine_term (t1, t2))
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   428
  | uncombine_term (Abs (s, T, t')) = Abs (s, T, uncombine_term t')
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   429
  | uncombine_term (t as Const (x as (s, _))) =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   430
    (case AList.lookup (op =) combinator_table s of
42227
662b50b7126f if "monomorphize" is enabled, mangle the type information in the names by default
blanchet
parents: 42180
diff changeset
   431
       SOME thm => thm |> prop_of |> specialize_type @{theory} x
662b50b7126f if "monomorphize" is enabled, mangle the type information in the names by default
blanchet
parents: 42180
diff changeset
   432
                       |> Logic.dest_equals |> snd
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   433
     | NONE => t)
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   434
  | uncombine_term t = t
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   435
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   436
(* Update schematic type variables with detected sort constraints. It's not
42563
e70ffe3846d0 improve helper type instantiation code
blanchet
parents: 42562
diff changeset
   437
   totally clear whether this code is necessary. *)
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   438
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
   439
  let
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   440
    fun do_type (Type (a, Ts)) = Type (a, map do_type Ts)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   441
      | do_type (TVar (xi, s)) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   442
        TVar (xi, the_default s (Vartab.lookup tvar_tab xi))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   443
      | do_type (TFree z) = TFree z
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   444
    fun do_term (Const (a, T)) = Const (a, do_type T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   445
      | do_term (Free (a, T)) = Free (a, do_type T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   446
      | do_term (Var (xi, T)) = Var (xi, do_type T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   447
      | do_term (t as Bound _) = t
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   448
      | 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
   449
      | do_term (t1 $ t2) = do_term t1 $ do_term t2
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   450
  in t |> not (Vartab.is_empty tvar_tab) ? do_term end
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   451
39425
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   452
fun quantify_over_var quant_of var_s t =
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   453
  let
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   454
    val vars = [] |> Term.add_vars t |> filter (fn ((s, _), _) => s = var_s)
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   455
                  |> map Var
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   456
  in fold_rev quant_of vars t end
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   457
38085
cc44e887246c avoid "clause" and "cnf" terminology where it no longer makes sense
blanchet
parents: 38066
diff changeset
   458
(* 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
   459
   appear in the formula. *)
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
   460
fun prop_from_formula thy sym_tab tfrees phi =
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   461
  let
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   462
    fun do_formula pos phi =
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   463
      case phi of
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   464
        AQuant (_, [], phi) => do_formula pos phi
42526
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42451
diff changeset
   465
      | AQuant (q, (s, _) :: xs, phi') =>
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   466
        do_formula pos (AQuant (q, xs, phi'))
42526
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42451
diff changeset
   467
        (* FIXME: TFF *)
39425
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   468
        #>> quantify_over_var (case q of
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   469
                                 AForall => forall_of
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   470
                               | AExists => exists_of)
42526
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42451
diff changeset
   471
                              (repair_atp_variable_name Char.toLower s)
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   472
      | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   473
      | AConn (c, [phi1, phi2]) =>
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   474
        do_formula (pos |> c = AImplies ? not) phi1
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   475
        ##>> do_formula pos phi2
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   476
        #>> (case c of
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   477
               AAnd => s_conj
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   478
             | AOr => s_disj
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   479
             | AImplies => s_imp
38038
584ab1a3a523 more robust proof reconstruction
blanchet
parents: 38036
diff changeset
   480
             | AIf => s_imp o swap
584ab1a3a523 more robust proof reconstruction
blanchet
parents: 38036
diff changeset
   481
             | AIff => s_iff
40069
6f7bf79b1506 fixed signature of "is_smt_solver_installed";
blanchet
parents: 40068
diff changeset
   482
             | ANotIff => s_not o s_iff
6f7bf79b1506 fixed signature of "is_smt_solver_installed";
blanchet
parents: 40068
diff changeset
   483
             | _ => raise Fail "unexpected connective")
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
   484
      | AAtom tm => term_from_pred thy sym_tab tfrees pos tm
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   485
      | _ => raise FORMULA [phi]
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   486
  in repair_tvar_sorts (do_formula true phi Vartab.empty) end
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   487
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
   488
fun check_formula ctxt =
39288
f1ae2493d93f eliminated aliases of Type.constraint;
wenzelm
parents: 39134
diff changeset
   489
  Type.constraint HOLogic.boolT
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42227
diff changeset
   490
  #> Syntax.check_term (Proof_Context.set_mode Proof_Context.mode_schematic ctxt)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   491
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   492
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   493
(**** Translation of TSTP files to Isar Proofs ****)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   494
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   495
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
   496
  | 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
   497
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
   498
fun decode_line sym_tab tfrees (Definition (name, phi1, phi2)) ctxt =
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   499
    let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42227
diff changeset
   500
      val thy = Proof_Context.theory_of ctxt
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
   501
      val t1 = prop_from_formula thy sym_tab tfrees phi1
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   502
      val vars = snd (strip_comb t1)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   503
      val frees = map unvarify_term vars
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   504
      val unvarify_args = subst_atomic (vars ~~ frees)
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
   505
      val t2 = prop_from_formula thy sym_tab tfrees phi2
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   506
      val (t1, t2) =
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   507
        HOLogic.eq_const HOLogic.typeT $ t1 $ t2
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
   508
        |> unvarify_args |> uncombine_term |> check_formula ctxt
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   509
        |> HOLogic.dest_eq
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   510
    in
39368
f661064b2b80 generalize proof reconstruction code;
blanchet
parents: 39353
diff changeset
   511
      (Definition (name, t1, t2),
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   512
       fold Variable.declare_term (maps OldTerm.term_frees [t1, t2]) ctxt)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   513
    end
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
   514
  | decode_line sym_tab tfrees (Inference (name, u, deps)) ctxt =
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   515
    let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42227
diff changeset
   516
      val thy = Proof_Context.theory_of ctxt
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
   517
      val t = u |> prop_from_formula thy sym_tab tfrees
37998
f1b7fb87f523 remove more Skolemization-aware code
blanchet
parents: 37996
diff changeset
   518
                |> uncombine_term |> check_formula ctxt
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   519
    in
39368
f661064b2b80 generalize proof reconstruction code;
blanchet
parents: 39353
diff changeset
   520
      (Inference (name, t, deps),
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   521
       fold Variable.declare_term (OldTerm.term_frees t) ctxt)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   522
    end
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
   523
fun decode_lines ctxt sym_tab tfrees lines =
4603154a3018 robustly detect how many type args were passed to the ATP, even if some of them were omitted
blanchet
parents: 42751
diff changeset
   524
  fst (fold_map (decode_line sym_tab tfrees) lines ctxt)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   525
38035
0ed953eac020 fix proof reconstruction for latest Vampire
blanchet
parents: 38034
diff changeset
   526
fun is_same_inference _ (Definition _) = false
0ed953eac020 fix proof reconstruction for latest Vampire
blanchet
parents: 38034
diff changeset
   527
  | is_same_inference t (Inference (_, t', _)) = t aconv t'
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   528
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   529
(* 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
   530
   clsarity). *)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   531
val is_only_type_information = curry (op aconv) HOLogic.true_const
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   532
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   533
fun replace_one_dependency (old, new) dep =
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
   534
  if is_same_step (dep, old) then new else [dep]
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   535
fun replace_dependencies_in_line _ (line as Definition _) = line
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   536
  | replace_dependencies_in_line p (Inference (name, t, deps)) =
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   537
    Inference (name, t, fold (union (op =) o replace_one_dependency p) deps [])
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   538
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
   539
(* 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
   540
   they differ only in type information.*)
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   541
fun add_line _ _ _ (line as Definition _) lines = line :: lines
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   542
  | add_line type_sys conjecture_shape fact_names (Inference (name, t, []))
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   543
             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
   544
    (* 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
   545
       definitions. *)
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   546
    if is_fact type_sys fact_names name 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
   547
      (* Facts are not proof lines. *)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   548
      if is_only_type_information t then
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   549
        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
   550
      (* 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
   551
      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
   552
        (_, []) => lines (* no repetition of proof line *)
39368
f661064b2b80 generalize proof reconstruction code;
blanchet
parents: 39353
diff changeset
   553
      | (pre, Inference (name', _, _) :: post) =>
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   554
        pre @ map (replace_dependencies_in_line (name', [name])) post
40069
6f7bf79b1506 fixed signature of "is_smt_solver_installed";
blanchet
parents: 40068
diff changeset
   555
      | _ => raise Fail "unexpected inference"
39370
f8292d3020db use same hack as in "Async_Manager" to work around Proof General bug
blanchet
parents: 39368
diff changeset
   556
    else if is_conjecture conjecture_shape name then
42606
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   557
      Inference (name, s_not t, []) :: lines
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   558
    else
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   559
      map (replace_dependencies_in_line (name, [])) lines
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   560
  | add_line _ _ _ (Inference (name, t, deps)) lines =
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   561
    (* 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
   562
    if is_only_type_information t then
39368
f661064b2b80 generalize proof reconstruction code;
blanchet
parents: 39353
diff changeset
   563
      Inference (name, t, deps) :: lines
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   564
    (* 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
   565
    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
   566
      (* FIXME: Doesn't this code risk conflating proofs involving different
38035
0ed953eac020 fix proof reconstruction for latest Vampire
blanchet
parents: 38034
diff changeset
   567
         types? *)
39368
f661064b2b80 generalize proof reconstruction code;
blanchet
parents: 39353
diff changeset
   568
       (_, []) => Inference (name, t, deps) :: lines
f661064b2b80 generalize proof reconstruction code;
blanchet
parents: 39353
diff changeset
   569
     | (pre, Inference (name', t', _) :: post) =>
f661064b2b80 generalize proof reconstruction code;
blanchet
parents: 39353
diff changeset
   570
       Inference (name, t', deps) ::
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   571
       pre @ map (replace_dependencies_in_line (name', [name])) post
40069
6f7bf79b1506 fixed signature of "is_smt_solver_installed";
blanchet
parents: 40068
diff changeset
   572
     | _ => raise Fail "unexpected inference"
22044
6c0702a96076 More compact proof reconstruction: lines having fewer than !min_deps dependences are folded
paulson
parents: 22012
diff changeset
   573
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   574
(* Recursively delete empty lines (type information) from the proof. *)
39368
f661064b2b80 generalize proof reconstruction code;
blanchet
parents: 39353
diff changeset
   575
fun add_nontrivial_line (Inference (name, t, [])) lines =
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   576
    if is_only_type_information t then delete_dependency name lines
39368
f661064b2b80 generalize proof reconstruction code;
blanchet
parents: 39353
diff changeset
   577
    else Inference (name, t, []) :: lines
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   578
  | add_nontrivial_line line lines = line :: lines
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   579
and delete_dependency name lines =
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   580
  fold_rev add_nontrivial_line
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   581
           (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
   582
37323
2f2f0d246d0f handle Vampire's definitions smoothly
blanchet
parents: 37322
diff changeset
   583
(* ATPs sometimes reuse free variable names in the strangest ways. Removing
2f2f0d246d0f handle Vampire's definitions smoothly
blanchet
parents: 37322
diff changeset
   584
   offending lines often does the trick. *)
36560
45c1870f234f fixed definition of "bad frees" so that it actually works
blanchet
parents: 36559
diff changeset
   585
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
   586
  | is_bad_free _ _ = false
22470
0d52e5157124 No label on "show"; tries to remove dependencies more cleanly
paulson
parents: 22428
diff changeset
   587
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   588
fun add_desired_line _ _ _ _ _ (line as Definition (name, _, _)) (j, lines) =
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   589
    (j, line :: map (replace_dependencies_in_line (name, [])) lines)
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   590
  | add_desired_line type_sys isar_shrink_factor conjecture_shape fact_names
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   591
                     frees (Inference (name, t, deps)) (j, lines) =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   592
    (j + 1,
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   593
     if is_fact type_sys fact_names name orelse
39370
f8292d3020db use same hack as in "Async_Manager" to work around Proof General bug
blanchet
parents: 39368
diff changeset
   594
        is_conjecture conjecture_shape name orelse
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   595
        (* the last line must be kept *)
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   596
        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
   597
        (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
   598
         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
   599
         not (exists_subterm (is_bad_free frees) t) andalso
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   600
         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
   601
         (* 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
   602
         j <> 1) then
39368
f661064b2b80 generalize proof reconstruction code;
blanchet
parents: 39353
diff changeset
   603
       Inference (name, t, deps) :: lines  (* keep line *)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   604
     else
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   605
       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
   606
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   607
(** Isar proof construction and manipulation **)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   608
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   609
fun merge_fact_sets (ls1, ss1) (ls2, ss2) =
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   610
  (union (op =) ls1 ls2, union (op =) ss1 ss2)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   611
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   612
type label = string * int
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   613
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
   614
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
   615
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
   616
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
   617
datatype isar_step =
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   618
  Fix of (string * typ) list |
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   619
  Let of term * term |
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   620
  Assume of label * term |
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
   621
  Have 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
   622
and byline =
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   623
  ByMetis of facts |
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
   624
  CaseSplit 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
   625
36574
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   626
fun smart_case_split [] facts = ByMetis facts
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   627
  | smart_case_split proofs facts = CaseSplit (proofs, facts)
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   628
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   629
fun add_fact_from_dependency type_sys conjecture_shape facts_offset fact_names
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   630
                             name =
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   631
  if is_fact type_sys fact_names name then
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   632
    apsnd (union (op =)
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   633
          (map fst (resolve_fact type_sys facts_offset fact_names name)))
36475
05209b869a6b new Isar proof construction code: stringfy axiom names correctly
blanchet
parents: 36474
diff changeset
   634
  else
39370
f8292d3020db use same hack as in "Async_Manager" to work around Proof General bug
blanchet
parents: 39368
diff changeset
   635
    apfst (insert (op =) (raw_label_for_name conjecture_shape name))
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   636
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   637
fun step_for_line _ _ _ _ _ (Definition (_, t1, t2)) = Let (t1, t2)
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   638
  | step_for_line _ conjecture_shape _ _ _ (Inference (name, t, [])) =
39370
f8292d3020db use same hack as in "Async_Manager" to work around Proof General bug
blanchet
parents: 39368
diff changeset
   639
    Assume (raw_label_for_name conjecture_shape name, t)
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   640
  | step_for_line type_sys conjecture_shape facts_offset
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   641
                  fact_names j (Inference (name, t, deps)) =
39370
f8292d3020db use same hack as in "Async_Manager" to work around Proof General bug
blanchet
parents: 39368
diff changeset
   642
    Have (if j = 1 then [Show] else [],
39425
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   643
          raw_label_for_name conjecture_shape name,
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
   644
          fold_rev forall_of (map Var (Term.add_vars t [])) t,
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   645
          ByMetis (fold (add_fact_from_dependency type_sys conjecture_shape
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   646
                                                  facts_offset fact_names)
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   647
                        deps ([], [])))
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   648
39454
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   649
fun repair_name "$true" = "c_True"
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   650
  | repair_name "$false" = "c_False"
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   651
  | repair_name "$$e" = "c_equal" (* seen in Vampire proofs *)
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   652
  | repair_name "equal" = "c_equal" (* needed by SPASS? *)
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   653
  | repair_name s =
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   654
    if String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s then
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   655
      "c_equal" (* seen in Vampire proofs *)
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   656
    else
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   657
      s
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   658
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   659
fun isar_proof_from_atp_proof pool ctxt type_sys tfrees isar_shrink_factor
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
   660
        atp_proof conjecture_shape facts_offset fact_names sym_tab params
4603154a3018 robustly detect how many type args were passed to the ATP, even if some of them were omitted
blanchet
parents: 42751
diff changeset
   661
        frees =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   662
  let
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   663
    val lines =
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   664
      atp_proof
39454
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   665
      |> nasty_atp_proof pool
acb25e9cf6fb factor out the inverse of "nice_atp_problem"
blanchet
parents: 39453
diff changeset
   666
      |> map_term_names_in_atp_proof repair_name
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
   667
      |> decode_lines ctxt sym_tab tfrees
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   668
      |> rpair [] |-> fold_rev (add_line type_sys conjecture_shape fact_names)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   669
      |> rpair [] |-> fold_rev add_nontrivial_line
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   670
      |> rpair (0, [])
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   671
      |-> fold_rev (add_desired_line type_sys isar_shrink_factor
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   672
                                     conjecture_shape fact_names frees)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   673
      |> snd
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   674
  in
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   675
    (if null params then [] else [Fix params]) @
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   676
    map2 (step_for_line type_sys conjecture_shape facts_offset fact_names)
42541
8938507b2054 move type declarations to the front, for TFF-compliance
blanchet
parents: 42539
diff changeset
   677
         (length lines downto 1) lines
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   678
  end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   679
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   680
(* When redirecting proofs, we keep information about the labels seen so far in
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   681
   the "backpatches" data structure. The first component indicates which facts
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   682
   should be associated with forthcoming proof steps. The second component is a
37322
0347b1a16682 fix bug in direct Isar proofs, which was exhibited by the "BigO" example
blanchet
parents: 37319
diff changeset
   683
   pair ("assum_ls", "drop_ls"), where "assum_ls" are the labels that should
0347b1a16682 fix bug in direct Isar proofs, which was exhibited by the "BigO" example
blanchet
parents: 37319
diff changeset
   684
   become assumptions and "drop_ls" are the labels that should be dropped in a
0347b1a16682 fix bug in direct Isar proofs, which was exhibited by the "BigO" example
blanchet
parents: 37319
diff changeset
   685
   case split. *)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   686
type backpatches = (label * facts) list * (label list * label list)
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   687
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
   688
fun used_labels_of_step (Have (_, _, _, by)) =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   689
    (case by of
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   690
       ByMetis (ls, _) => ls
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
   691
     | CaseSplit (proofs, (ls, _)) =>
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
   692
       fold (union (op =) o used_labels_of) proofs ls)
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
   693
  | used_labels_of_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
   694
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
   695
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   696
fun new_labels_of_step (Fix _) = []
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   697
  | new_labels_of_step (Let _) = []
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   698
  | new_labels_of_step (Assume (l, _)) = [l]
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   699
  | new_labels_of_step (Have (_, l, _, _)) = [l]
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   700
val new_labels_of = maps new_labels_of_step
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   701
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   702
val join_proofs =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   703
  let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   704
    fun aux _ [] = NONE
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   705
      | aux proof_tail (proofs as (proof1 :: _)) =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   706
        if exists null proofs then
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   707
          NONE
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   708
        else if forall (curry (op =) (hd proof1) o hd) (tl proofs) then
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   709
          aux (hd proof1 :: proof_tail) (map tl proofs)
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   710
        else case hd proof1 of
37498
b426cbdb5a23 removed Sledgehammer's support for the DFG syntax;
blanchet
parents: 37479
diff changeset
   711
          Have ([], l, t, _) => (* FIXME: should we really ignore the "by"? *)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   712
          if forall (fn Have ([], l', t', _) :: _ => (l, t) = (l', t')
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   713
                      | _ => false) (tl proofs) andalso
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   714
             not (exists (member (op =) (maps new_labels_of proofs))
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
   715
                         (used_labels_of proof_tail)) then
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   716
            SOME (l, t, map rev proofs, proof_tail)
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   717
          else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   718
            NONE
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   719
        | _ => NONE
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   720
  in aux [] o map rev end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   721
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   722
fun case_split_qualifiers proofs =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   723
  case length proofs of
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   724
    0 => []
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   725
  | 1 => [Then]
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   726
  | _ => [Ultimately]
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   727
39372
2fd8a9a7080d first step in generalizing to nonnumeric proof step names (e.g. remote Vampire 0.6)
blanchet
parents: 39370
diff changeset
   728
fun redirect_proof hyp_ts concl_t proof =
33310
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
   729
  let
37324
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   730
    (* The first pass outputs those steps that are independent of the negated
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   731
       conjecture. The second pass flips the proof by contradiction to obtain a
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   732
       direct proof, introducing case splits when an inference depends on
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   733
       several facts that depend on the negated conjecture. *)
39372
2fd8a9a7080d first step in generalizing to nonnumeric proof step names (e.g. remote Vampire 0.6)
blanchet
parents: 39370
diff changeset
   734
     val concl_l = (conjecture_prefix, length hyp_ts)
38040
174568533593 fix bug in the SPASS Flotter hack, when a conjecture FOF is translated to several CNF clauses
blanchet
parents: 38039
diff changeset
   735
     fun first_pass ([], contra) = ([], contra)
174568533593 fix bug in the SPASS Flotter hack, when a conjecture FOF is translated to several CNF clauses
blanchet
parents: 38039
diff changeset
   736
       | first_pass ((step as Fix _) :: proof, contra) =
174568533593 fix bug in the SPASS Flotter hack, when a conjecture FOF is translated to several CNF clauses
blanchet
parents: 38039
diff changeset
   737
         first_pass (proof, contra) |>> cons step
174568533593 fix bug in the SPASS Flotter hack, when a conjecture FOF is translated to several CNF clauses
blanchet
parents: 38039
diff changeset
   738
       | first_pass ((step as Let _) :: proof, contra) =
174568533593 fix bug in the SPASS Flotter hack, when a conjecture FOF is translated to several CNF clauses
blanchet
parents: 38039
diff changeset
   739
         first_pass (proof, contra) |>> cons step
39370
f8292d3020db use same hack as in "Async_Manager" to work around Proof General bug
blanchet
parents: 39368
diff changeset
   740
       | first_pass ((step as Assume (l as (_, j), _)) :: proof, contra) =
39372
2fd8a9a7080d first step in generalizing to nonnumeric proof step names (e.g. remote Vampire 0.6)
blanchet
parents: 39370
diff changeset
   741
         if l = concl_l then first_pass (proof, contra ||> cons step)
2fd8a9a7080d first step in generalizing to nonnumeric proof step names (e.g. remote Vampire 0.6)
blanchet
parents: 39370
diff changeset
   742
         else first_pass (proof, contra) |>> cons (Assume (l, nth hyp_ts j))
38040
174568533593 fix bug in the SPASS Flotter hack, when a conjecture FOF is translated to several CNF clauses
blanchet
parents: 38039
diff changeset
   743
       | first_pass (Have (qs, l, t, ByMetis (ls, ss)) :: proof, contra) =
39372
2fd8a9a7080d first step in generalizing to nonnumeric proof step names (e.g. remote Vampire 0.6)
blanchet
parents: 39370
diff changeset
   744
         let val step = Have (qs, l, t, ByMetis (ls, ss)) in
38040
174568533593 fix bug in the SPASS Flotter hack, when a conjecture FOF is translated to several CNF clauses
blanchet
parents: 38039
diff changeset
   745
           if exists (member (op =) (fst contra)) ls then
174568533593 fix bug in the SPASS Flotter hack, when a conjecture FOF is translated to several CNF clauses
blanchet
parents: 38039
diff changeset
   746
             first_pass (proof, contra |>> cons l ||> cons step)
174568533593 fix bug in the SPASS Flotter hack, when a conjecture FOF is translated to several CNF clauses
blanchet
parents: 38039
diff changeset
   747
           else
174568533593 fix bug in the SPASS Flotter hack, when a conjecture FOF is translated to several CNF clauses
blanchet
parents: 38039
diff changeset
   748
             first_pass (proof, contra) |>> cons step
174568533593 fix bug in the SPASS Flotter hack, when a conjecture FOF is translated to several CNF clauses
blanchet
parents: 38039
diff changeset
   749
         end
174568533593 fix bug in the SPASS Flotter hack, when a conjecture FOF is translated to several CNF clauses
blanchet
parents: 38039
diff changeset
   750
       | first_pass _ = raise Fail "malformed proof"
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   751
    val (proof_top, (contra_ls, contra_proof)) =
39372
2fd8a9a7080d first step in generalizing to nonnumeric proof step names (e.g. remote Vampire 0.6)
blanchet
parents: 39370
diff changeset
   752
      first_pass (proof, ([concl_l], []))
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   753
    val backpatch_label = the_default ([], []) oo AList.lookup (op =) o fst
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   754
    fun backpatch_labels patches ls =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   755
      fold merge_fact_sets (map (backpatch_label patches) ls) ([], [])
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   756
    fun second_pass end_qs ([], assums, patches) =
37324
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   757
        ([Have (end_qs, no_label, concl_t,
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   758
                ByMetis (backpatch_labels patches (map snd assums)))], patches)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   759
      | second_pass end_qs (Assume (l, t) :: proof, assums, patches) =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   760
        second_pass end_qs (proof, (t, l) :: assums, patches)
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   761
      | second_pass end_qs (Have (qs, l, t, ByMetis (ls, ss)) :: proof, assums,
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   762
                            patches) =
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   763
        (if member (op =) (snd (snd patches)) l andalso
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   764
            not (member (op =) (fst (snd patches)) l) andalso
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   765
            not (AList.defined (op =) (fst patches) l) then
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   766
           second_pass end_qs (proof, assums, patches ||> apsnd (append ls))
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   767
         else case List.partition (member (op =) contra_ls) ls of
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   768
           ([contra_l], co_ls) =>
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   769
           if member (op =) qs Show then
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   770
             second_pass end_qs (proof, assums,
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   771
                                 patches |>> cons (contra_l, (co_ls, ss)))
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   772
           else
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   773
             second_pass end_qs
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   774
                         (proof, assums,
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   775
                          patches |>> cons (contra_l, (l :: co_ls, ss)))
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   776
             |>> cons (if member (op =) (fst (snd patches)) l then
42606
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   777
                         Assume (l, s_not t)
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   778
                       else
42606
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   779
                         Have (qs, l, s_not t,
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   780
                               ByMetis (backpatch_label patches l)))
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   781
         | (contra_ls as _ :: _, co_ls) =>
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   782
           let
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   783
             val proofs =
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   784
               map_filter
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   785
                   (fn l =>
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   786
                       if l = concl_l then
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   787
                         NONE
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   788
                       else
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   789
                         let
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   790
                           val drop_ls = filter (curry (op <>) l) contra_ls
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   791
                         in
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   792
                           second_pass []
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   793
                               (proof, assums,
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   794
                                patches ||> apfst (insert (op =) l)
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   795
                                        ||> apsnd (union (op =) drop_ls))
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   796
                           |> fst |> SOME
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   797
                         end) contra_ls
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   798
             val (assumes, facts) =
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   799
               if member (op =) (fst (snd patches)) l then
42606
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   800
                 ([Assume (l, s_not t)], (l :: co_ls, ss))
39373
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   801
               else
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   802
                 ([], (co_ls, ss))
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   803
           in
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   804
             (case join_proofs proofs of
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   805
                SOME (l, t, proofs, proof_tail) =>
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   806
                Have (case_split_qualifiers proofs @
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   807
                      (if null proof_tail then end_qs else []), l, t,
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   808
                      smart_case_split proofs facts) :: proof_tail
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   809
              | NONE =>
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   810
                [Have (case_split_qualifiers proofs @ end_qs, no_label,
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   811
                       concl_t, smart_case_split proofs facts)],
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   812
              patches)
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   813
             |>> append assumes
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   814
           end
fe95c860434c finish support for E 1.2 proof reconstruction;
blanchet
parents: 39372
diff changeset
   815
         | _ => raise Fail "malformed proof")
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   816
       | second_pass _ _ = raise Fail "malformed proof"
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   817
    val proof_bottom =
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   818
      second_pass [Show] (contra_proof, [], ([], ([], []))) |> fst
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   819
  in proof_top @ proof_bottom end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   820
38490
blanchet
parents: 38488
diff changeset
   821
(* FIXME: Still needed? Probably not. *)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   822
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
   823
  let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   824
    fun relabel_facts subst =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   825
      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
   826
    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
   827
        (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
   828
           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
   829
         | NONE => (step :: proof, subst, (t, l) :: assums))
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   830
      | do_step (Have (qs, l, t, by)) (proof, subst, assums) =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   831
        (Have (qs, l, t,
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   832
               case by of
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   833
                 ByMetis facts => ByMetis (relabel_facts subst facts)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   834
               | CaseSplit (proofs, facts) =>
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   835
                 CaseSplit (map do_proof proofs, relabel_facts subst facts)) ::
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   836
         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
   837
      | 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
   838
    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
   839
  in do_proof end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   840
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   841
val then_chain_proof =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   842
  let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   843
    fun aux _ [] = []
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
   844
      | aux _ ((step as Assume (l, _)) :: proof) = step :: aux l proof
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   845
      | aux l' (Have (qs, l, t, by) :: proof) =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   846
        (case by of
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   847
           ByMetis (ls, ss) =>
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   848
           Have (if member (op =) ls l' then
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   849
                   (Then :: qs, l, t,
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   850
                    ByMetis (filter_out (curry (op =) l') ls, ss))
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   851
                 else
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   852
                   (qs, l, t, ByMetis (ls, ss)))
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   853
         | CaseSplit (proofs, facts) =>
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   854
           Have (qs, l, t, CaseSplit (map (aux no_label) proofs, facts))) ::
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   855
        aux l proof
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
   856
      | aux _ (step :: proof) = step :: aux no_label proof
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   857
  in aux no_label end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   858
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   859
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
   860
  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
   861
    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
   862
    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
   863
    fun do_step (Assume (l, t)) = Assume (do_label l, t)
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
   864
      | do_step (Have (qs, l, t, by)) =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   865
        Have (qs, do_label l, t,
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   866
              case by of
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   867
                CaseSplit (proofs, facts) =>
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
   868
                CaseSplit (map (map do_step) proofs, facts)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   869
              | _ => 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
   870
      | 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
   871
  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
   872
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   873
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
   874
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   875
val relabel_proof =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   876
  let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   877
    fun aux _ _ _ [] = []
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   878
      | 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
   879
        if l = no_label then
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   880
          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
   881
        else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   882
          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
   883
            Assume (l', t) ::
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   884
            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
   885
          end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   886
      | aux subst depth (next_assum, next_fact) (Have (qs, l, t, by) :: proof) =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   887
        let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   888
          val (l', subst, next_fact) =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   889
            if l = no_label then
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   890
              (l, subst, next_fact)
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   891
            else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   892
              let
42180
a6c141925a8a added monomorphization option to Sledgehammer ATPs -- this looks promising but is still off by default
blanchet
parents: 41742
diff changeset
   893
                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
   894
              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
   895
          val relabel_facts =
39370
f8292d3020db use same hack as in "Async_Manager" to work around Proof General bug
blanchet
parents: 39368
diff changeset
   896
            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
   897
          val by =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   898
            case by of
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   899
              ByMetis facts => ByMetis (relabel_facts facts)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   900
            | CaseSplit (proofs, facts) =>
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   901
              CaseSplit (map (aux subst (depth + 1) (1, 1)) proofs,
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   902
                         relabel_facts facts)
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   903
        in
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   904
          Have (qs, l', t, by) ::
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   905
          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
   906
        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
   907
      | 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
   908
        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
   909
  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
   910
42759
fdd15e889ad7 no need to use metisFT for Isar proofs -- metis falls back on it anyway
blanchet
parents: 42755
diff changeset
   911
fun string_for_proof ctxt0 i n =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   912
  let
39134
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39115
diff changeset
   913
    val ctxt = ctxt0
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39115
diff changeset
   914
      |> Config.put show_free_types false
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39115
diff changeset
   915
      |> Config.put show_types true
42606
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   916
      |> Config.put show_sorts true
37319
42268dc7d6c4 show types in Isar proofs, but not for free variables;
blanchet
parents: 37172
diff changeset
   917
    fun fix_print_mode f x =
39134
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39115
diff changeset
   918
      Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN)
917b4b6ba3d2 turned show_sorts/show_types into proper configuration options;
wenzelm
parents: 39115
diff changeset
   919
                               (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
   920
    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
   921
    fun do_free (s, T) =
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   922
      maybe_quote s ^ " :: " ^
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   923
      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
   924
    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
   925
    fun do_have qs =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   926
      (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
   927
      (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
   928
      (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
   929
         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
   930
       else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   931
         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
   932
    val do_term = maybe_quote o fix_print_mode (Syntax.string_of_term ctxt)
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
   933
    fun do_facts (ls, ss) =
42759
fdd15e889ad7 no need to use metisFT for Isar proofs -- metis falls back on it anyway
blanchet
parents: 42755
diff changeset
   934
      metis_command false 1 1
38698
d19c3a7ce38b clean handling of whether a fact is chained or not;
blanchet
parents: 38696
diff changeset
   935
                    (ls |> sort_distinct (prod_ord string_ord int_ord),
d19c3a7ce38b clean handling of whether a fact is chained or not;
blanchet
parents: 38696
diff changeset
   936
                     ss |> sort_distinct string_ord)
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   937
    and do_step ind (Fix xs) =
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   938
        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
   939
      | do_step ind (Let (t1, t2)) =
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   940
        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
   941
      | 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
   942
        do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n"
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   943
      | do_step ind (Have (qs, l, t, ByMetis facts)) =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   944
        do_indent ind ^ do_have qs ^ " " ^
36479
fcbf412c560f reintroduce missing "gen_all_vars" call
blanchet
parents: 36478
diff changeset
   945
        do_label l ^ do_term t ^ " " ^ do_facts facts ^ "\n"
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   946
      | do_step ind (Have (qs, l, t, CaseSplit (proofs, facts))) =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   947
        space_implode (do_indent ind ^ "moreover\n")
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   948
                      (map (do_block ind) proofs) ^
36479
fcbf412c560f reintroduce missing "gen_all_vars" call
blanchet
parents: 36478
diff changeset
   949
        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
   950
        do_facts facts ^ "\n"
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   951
    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
   952
      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
   953
        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
   954
        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
   955
                        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
   956
        suffix ^ "\n"
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   957
      end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   958
    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
   959
    (* 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
   960
       directly. *)
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   961
    and do_proof [Have (_, _, _, ByMetis _)] = ""
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   962
      | 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
   963
        (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
   964
        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
   965
        (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
   966
  in do_proof end
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   967
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
   968
fun isar_proof_text (ctxt, debug, isar_shrink_factor, pool, conjecture_shape,
4603154a3018 robustly detect how many type args were passed to the ATP, even if some of them were omitted
blanchet
parents: 42751
diff changeset
   969
                     sym_tab)
42647
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   970
                    (metis_params as (_, _, type_sys, atp_proof, facts_offset,
59142dbfa3ba no need to generate fact numbers for polymorphic type systems -- this confuses the TPTP exporter
blanchet
parents: 42613
diff changeset
   971
                                      fact_names, goal, i)) =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   972
  let
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   973
    val (params, hyp_ts, concl_t) = strip_subgoal goal i
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   974
    val frees = fold Term.add_frees (concl_t :: hyp_ts) []
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
   975
    val tfrees = fold Term.add_tfrees (concl_t :: hyp_ts) []
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   976
    val n = Logic.count_prems (prop_of goal)
40723
a82badd0e6ef put facts found by SMT solver in alphabetical order and omit chained facts, as was done already for ATP proofs
blanchet
parents: 40627
diff changeset
   977
    val (one_line_proof, lemma_names) = metis_proof_text metis_params
36283
25e69e93954d failure of reconstructing the Isar proof (e.g., exception) should not prevent Sledgehammer from showing the short one-liner proof -- but in "debug" mode we do want to know what the exception is
blanchet
parents: 36281
diff changeset
   978
    fun isar_proof_for () =
42449
494e4ac5b0f8 detect some unsound proofs before showing them to the user
blanchet
parents: 42361
diff changeset
   979
      case isar_proof_from_atp_proof pool ctxt type_sys tfrees
42541
8938507b2054 move type declarations to the front, for TFF-compliance
blanchet
parents: 42539
diff changeset
   980
               isar_shrink_factor atp_proof conjecture_shape facts_offset
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
   981
               fact_names sym_tab params frees
39372
2fd8a9a7080d first step in generalizing to nonnumeric proof step names (e.g. remote Vampire 0.6)
blanchet
parents: 39370
diff changeset
   982
           |> redirect_proof hyp_ts concl_t
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   983
           |> kill_duplicate_assumptions_in_proof
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   984
           |> then_chain_proof
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   985
           |> kill_useless_labels_in_proof
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   986
           |> relabel_proof
42759
fdd15e889ad7 no need to use metisFT for Isar proofs -- metis falls back on it anyway
blanchet
parents: 42755
diff changeset
   987
           |> string_for_proof ctxt i n of
42554
f83036b85a3a added friendly hint when Isar proof is missing
blanchet
parents: 42551
diff changeset
   988
        "" => "\nNo structured proof available (proof too short)."
38599
7f510e5449fb parse SNARK proofs
blanchet
parents: 38597
diff changeset
   989
      | proof => "\n\nStructured proof:\n" ^ Markup.markup Markup.sendback proof
35868
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   990
    val isar_proof =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   991
      if debug then
36283
25e69e93954d failure of reconstructing the Isar proof (e.g., exception) should not prevent Sledgehammer from showing the short one-liner proof -- but in "debug" mode we do want to know what the exception is
blanchet
parents: 36281
diff changeset
   992
        isar_proof_for ()
25e69e93954d failure of reconstructing the Isar proof (e.g., exception) should not prevent Sledgehammer from showing the short one-liner proof -- but in "debug" mode we do want to know what the exception is
blanchet
parents: 36281
diff changeset
   993
      else
25e69e93954d failure of reconstructing the Isar proof (e.g., exception) should not prevent Sledgehammer from showing the short one-liner proof -- but in "debug" mode we do want to know what the exception is
blanchet
parents: 36281
diff changeset
   994
        try isar_proof_for ()
38599
7f510e5449fb parse SNARK proofs
blanchet
parents: 38597
diff changeset
   995
        |> the_default "\nWarning: The Isar proof construction failed."
36283
25e69e93954d failure of reconstructing the Isar proof (e.g., exception) should not prevent Sledgehammer from showing the short one-liner proof -- but in "debug" mode we do want to know what the exception is
blanchet
parents: 36281
diff changeset
   996
  in (one_line_proof ^ isar_proof, lemma_names) end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   997
40723
a82badd0e6ef put facts found by SMT solver in alphabetical order and omit chained facts, as was done already for ATP proofs
blanchet
parents: 40627
diff changeset
   998
fun proof_text isar_proof isar_params metis_params =
36557
3c2438efe224 make SML/NJ happy, take 2
blanchet
parents: 36556
diff changeset
   999
  (if isar_proof then isar_proof_text isar_params else metis_proof_text)
40723
a82badd0e6ef put facts found by SMT solver in alphabetical order and omit chained facts, as was done already for ATP proofs
blanchet
parents: 40627
diff changeset
  1000
      metis_params
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
  1001
31038
immler@in.tum.de
parents: 31037
diff changeset
  1002
end;