src/HOL/Tools/Sledgehammer/sledgehammer_proof_reconstruct.ML
author blanchet
Mon, 26 Jul 2010 17:22:39 +0200
changeset 37998 f1b7fb87f523
parent 37996 11c076ea92e9
child 38007 f0a4aa17f23f
permissions -rw-r--r--
remove more Skolemization-aware code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35826
1590abc3d42a renamed Sledgehammer structures
blanchet
parents: 35825
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_proof_reconstruct.ML
33310
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
     2
    Author:     Lawrence C Paulson and Claire Quigley, Cambridge University Computer Laboratory
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
     3
    Author:     Jasmin Blanchette, TU Muenchen
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
     4
33310
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
     5
Transfer of proofs from external provers.
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
     6
*)
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
     7
35826
1590abc3d42a renamed Sledgehammer structures
blanchet
parents: 35825
diff changeset
     8
signature SLEDGEHAMMER_PROOF_RECONSTRUCT =
24425
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
     9
sig
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    10
  type minimize_command = string list -> string
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    11
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
    12
  val metis_line: bool -> int -> int -> string list -> string
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
    13
  val metis_proof_text:
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
    14
    bool * minimize_command * string * string vector * thm * int
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    15
    -> string * string list
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
    16
  val isar_proof_text:
37996
11c076ea92e9 simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents: 37995
diff changeset
    17
    string Symtab.table * bool * int * Proof.context * int list
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
    18
    -> bool * minimize_command * string * string vector * thm * int
36287
96f45c5ffb36 if Isar proof reconstruction is not supported, tell the user so they don't wonder why their "isar_proof" option did nothing
blanchet
parents: 36285
diff changeset
    19
    -> string * string list
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
    20
  val proof_text:
37996
11c076ea92e9 simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents: 37995
diff changeset
    21
    bool -> string Symtab.table * bool * int * Proof.context * int list
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
    22
    -> bool * minimize_command * string * string vector * thm * int
36287
96f45c5ffb36 if Isar proof reconstruction is not supported, tell the user so they don't wonder why their "isar_proof" option did nothing
blanchet
parents: 36285
diff changeset
    23
    -> string * string list
24425
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
    24
end;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    25
35826
1590abc3d42a renamed Sledgehammer structures
blanchet
parents: 35825
diff changeset
    26
structure Sledgehammer_Proof_Reconstruct : SLEDGEHAMMER_PROOF_RECONSTRUCT =
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    27
struct
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    28
37578
9367cb36b1c4 renamed "Sledgehammer_FOL_Clauses" to "Metis_Clauses", so that Metis doesn't depend on Sledgehammer
blanchet
parents: 37577
diff changeset
    29
open Metis_Clauses
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
    30
open Sledgehammer_Util
37616
c8d2d84d6011 always perform relevance filtering on original formulas
blanchet
parents: 37578
diff changeset
    31
open Sledgehammer_Fact_Filter
37624
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37616
diff changeset
    32
open Sledgehammer_TPTP_Format
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    33
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    34
type minimize_command = string list -> string
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    35
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    36
fun mk_anot phi = AConn (ANot, [phi])
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    37
fun mk_aconn c (phi1, phi2) = AConn (c, [phi1, phi2])
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    38
37996
11c076ea92e9 simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents: 37995
diff changeset
    39
val index_in_shape : int -> int list -> int = find_index o curry (op =)
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37961
diff changeset
    40
fun is_axiom_clause_number thm_names num =
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37961
diff changeset
    41
  num > 0 andalso num <= Vector.length thm_names andalso
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37961
diff changeset
    42
  Vector.sub (thm_names, num - 1) <> ""
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
    43
fun is_conjecture_clause_number conjecture_shape num =
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
    44
  index_in_shape num conjecture_shape >= 0
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
    45
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    46
fun negate_term (Const (@{const_name All}, T) $ Abs (s, T', t')) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    47
    Const (@{const_name Ex}, T) $ Abs (s, T', negate_term t')
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    48
  | negate_term (Const (@{const_name Ex}, T) $ Abs (s, T', t')) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    49
    Const (@{const_name All}, T) $ Abs (s, T', negate_term t')
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    50
  | negate_term (@{const "op -->"} $ t1 $ t2) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    51
    @{const "op &"} $ t1 $ negate_term t2
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    52
  | negate_term (@{const "op &"} $ t1 $ t2) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    53
    @{const "op |"} $ negate_term t1 $ negate_term t2
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    54
  | negate_term (@{const "op |"} $ t1 $ t2) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    55
    @{const "op &"} $ negate_term t1 $ negate_term t2
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    56
  | negate_term (@{const Not} $ t) = t
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    57
  | negate_term t = @{const Not} $ t
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    58
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
    59
datatype ('a, 'b, 'c, 'd, 'e) raw_step =
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
    60
  Definition of 'a * 'b * 'c |
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
    61
  Inference of 'a * 'd * 'e list
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
    62
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    63
(**** PARSING OF TSTP FORMAT ****)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    64
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    65
datatype int_or_string = Str of string | Int of int
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    66
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    67
(*Strings enclosed in single quotes, e.g. filenames*)
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    68
val scan_quoted = $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    69
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    70
val scan_dollar_name =
36548
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
    71
  Scan.repeat ($$ "$") -- Symbol.scan_id >> (fn (ss, s) => implode ss ^ s)
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
    72
36369
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36293
diff changeset
    73
(* needed for SPASS's output format *)
36548
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
    74
fun repair_name _ "$true" = "c_True"
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
    75
  | repair_name _ "$false" = "c_False"
36559
93b8ceabc923 don't remove last line of proof
blanchet
parents: 36558
diff changeset
    76
  | repair_name _ "$$e" = "c_equal" (* seen in Vampire 11 proofs *)
36548
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
    77
  | repair_name _ "equal" = "c_equal" (* probably not needed *)
37926
e6ff246c0cdb renamings + only need second component of name pool to reconstruct proofs
blanchet
parents: 37643
diff changeset
    78
  | repair_name pool s = Symtab.lookup pool s |> the_default s
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    79
(* Generalized first-order terms, which include file names, numbers, etc. *)
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    80
(* The "x" argument is not strictly necessary, but without it Poly/ML loops
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    81
   forever at compile time. *)
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    82
fun parse_generalized_term x =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    83
  (scan_quoted >> (fn s => ATerm (Str s, []))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    84
   || scan_dollar_name
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    85
      -- Scan.optional ($$ "(" |-- parse_generalized_terms --| $$ ")") []
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    86
      >> (fn (s, gs) => ATerm (Str s, gs))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    87
   || scan_integer >> (fn k => ATerm (Int k, []))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    88
   || $$ "(" |-- parse_generalized_term --| $$ ")"
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    89
   || $$ "[" |-- Scan.optional parse_generalized_terms [] --| $$ "]"
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    90
      >> curry ATerm (Str "list")) x
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    91
and parse_generalized_terms x =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    92
  (parse_generalized_term ::: Scan.repeat ($$ "," |-- parse_generalized_term)) x
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    93
fun parse_term pool x =
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    94
  ((scan_dollar_name >> repair_name pool)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
    95
    -- Scan.optional ($$ "(" |-- parse_terms pool --| $$ ")") [] >> ATerm) x
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    96
and parse_terms pool x =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    97
  (parse_term pool ::: Scan.repeat ($$ "," |-- parse_term pool)) x
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    98
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    99
fun parse_predicate_term pool =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   100
  parse_term pool -- Scan.option (Scan.option ($$ "!") --| $$ "="
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   101
                                  -- parse_term pool)
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   102
  >> (fn (u, NONE) => APred u
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   103
       | (u1, SOME (NONE, u2)) => APred (ATerm ("c_equal", [u1, u2]))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   104
       | (u1, SOME (SOME _, u2)) =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   105
         mk_anot (APred (ATerm ("c_equal", [u1, u2]))))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   106
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   107
fun fo_term_head (ATerm (s, _)) = s
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   108
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   109
(* TPTP formulas are fully parenthesized, so we don't need to worry about
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   110
   operator precedence. *)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   111
fun parse_formula pool x =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   112
  (($$ "(" |-- parse_formula pool --| $$ ")"
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   113
    || ($$ "!" >> K AForall || $$ "?" >> K AExists)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   114
       --| $$ "[" -- parse_terms pool --| $$ "]" --| $$ ":"
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   115
       -- parse_formula pool
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   116
       >> (fn ((q, ts), phi) => AQuant (q, map fo_term_head ts, phi))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   117
    || $$ "~" |-- parse_formula pool >> mk_anot
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   118
    || parse_predicate_term pool)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   119
   -- Scan.option ((Scan.this_string "=>" >> K AImplies
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   120
                    || Scan.this_string "<=>" >> K AIff
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   121
                    || Scan.this_string "<~>" >> K ANotIff
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   122
                    || Scan.this_string "<=" >> K AIf
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   123
                    || $$ "|" >> K AOr || $$ "&" >> K AAnd)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   124
                   -- parse_formula pool)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   125
   >> (fn (phi1, NONE) => phi1
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   126
        | (phi1, SOME (c, phi2)) => mk_aconn c (phi1, phi2))) x
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   127
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   128
fun ints_of_generalized_term (ATerm (label, gs)) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   129
  fold ints_of_generalized_term gs #> (case label of Int k => cons k | _ => I)
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   130
val parse_tstp_annotations =
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   131
  Scan.optional ($$ "," |-- parse_generalized_term
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   132
                   --| Scan.option ($$ "," |-- parse_generalized_terms)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   133
                 >> (fn g => ints_of_generalized_term g [])) []
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   134
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   135
(* Syntax: (fof|cnf)\(<num>, <formula_role>, <cnf_formula> <annotations>\).
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   136
   The <num> could be an identifier, but we assume integers. *)
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   137
 fun parse_tstp_line pool =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   138
   ((Scan.this_string "fof" || Scan.this_string "cnf") -- $$ "(")
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   139
     |-- scan_integer --| $$ "," -- Symbol.scan_id --| $$ ","
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   140
     -- parse_formula pool -- parse_tstp_annotations --| $$ ")" --| $$ "."
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   141
    >> (fn (((num, role), phi), deps) =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   142
           case role of
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   143
             "definition" =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   144
             (case phi of
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   145
                AConn (AIff, [phi1, phi2]) => Definition (num, phi1, phi2)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   146
              | _ => raise Fail "malformed definition")
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   147
           | _ => Inference (num, phi, deps))
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   148
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   149
(**** PARSING OF SPASS OUTPUT ****)
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   150
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   151
(* SPASS returns clause references of the form "x.y". We ignore "y", whose role
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   152
   is not clear anyway. *)
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37961
diff changeset
   153
val parse_dot_name = scan_integer --| $$ "." --| scan_integer
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   154
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   155
val parse_spass_annotations =
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   156
  Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   157
                                         --| Scan.option ($$ ","))) []
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   158
36574
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   159
(* It is not clear why some literals are followed by sequences of stars and/or
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   160
   pluses. We ignore them. *)
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   161
fun parse_decorated_predicate_term pool =
36562
c6c2661bf08e the SPASS output syntax is more general than I thought -- such a pity that there's no documentation
blanchet
parents: 36560
diff changeset
   162
  parse_predicate_term pool --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   163
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   164
fun mk_horn ([], []) = APred (ATerm ("c_False", []))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   165
  | mk_horn ([], pos_lits) = foldr1 (mk_aconn AOr) pos_lits
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   166
  | mk_horn (neg_lits, []) = mk_anot (foldr1 (mk_aconn AAnd) neg_lits)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   167
  | mk_horn (neg_lits, pos_lits) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   168
    mk_aconn AImplies (foldr1 (mk_aconn AAnd) neg_lits,
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   169
                       foldr1 (mk_aconn AOr) pos_lits)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   170
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   171
fun parse_horn_clause pool =
36574
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   172
  Scan.repeat (parse_decorated_predicate_term pool) --| $$ "|" --| $$ "|"
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   173
    -- Scan.repeat (parse_decorated_predicate_term pool) --| $$ "-" --| $$ ">"
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   174
    -- Scan.repeat (parse_decorated_predicate_term pool)
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   175
  >> (mk_horn o apfst (op @))
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   176
36558
36eff5a50bab handle previously unknown SPASS syntaxes in Sledgehammer's proof reconstruction
blanchet
parents: 36557
diff changeset
   177
(* Syntax: <num>[0:<inference><annotations>]
36eff5a50bab handle previously unknown SPASS syntaxes in Sledgehammer's proof reconstruction
blanchet
parents: 36557
diff changeset
   178
   <cnf_formulas> || <cnf_formulas> -> <cnf_formulas>. *)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   179
fun parse_spass_line pool =
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37961
diff changeset
   180
  scan_integer --| $$ "[" --| $$ "0" --| $$ ":" --| Symbol.scan_id
36558
36eff5a50bab handle previously unknown SPASS syntaxes in Sledgehammer's proof reconstruction
blanchet
parents: 36557
diff changeset
   181
  -- parse_spass_annotations --| $$ "]" -- parse_horn_clause pool --| $$ "."
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   182
  >> (fn ((num, deps), u) => Inference (num, u, deps))
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   183
36548
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
   184
fun parse_line pool = parse_tstp_line pool || parse_spass_line pool
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
   185
fun parse_lines pool = Scan.repeat1 (parse_line pool)
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
   186
fun parse_proof pool =
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
   187
  fst o Scan.finite Symbol.stopper
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
   188
            (Scan.error (!! (fn _ => raise Fail "unrecognized ATP output")
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
   189
                            (parse_lines pool)))
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
   190
  o explode o strip_spaces
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   191
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   192
(**** INTERPRETATION OF TSTP SYNTAX TREES ****)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   193
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   194
exception FO_TERM of string fo_term list
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37991
diff changeset
   195
exception FORMULA of (string, string fo_term) formula list
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   196
exception SAME of unit
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   197
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   198
(* 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
   199
   constrained by information from type literals, or by type inference. *)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   200
fun type_from_fo_term tfrees (u as ATerm (a, us)) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   201
  let val Ts = map (type_from_fo_term tfrees) us in
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   202
    case strip_prefix_and_undo_ascii type_const_prefix a of
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   203
      SOME b => Type (invert_const b, Ts)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   204
    | NONE =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   205
      if not (null us) then
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   206
        raise FO_TERM [u]  (* only "tconst"s have type arguments *)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   207
      else case strip_prefix_and_undo_ascii tfree_prefix a of
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   208
        SOME b =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   209
        let val s = "'" ^ b in
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   210
          TFree (s, AList.lookup (op =) tfrees s |> the_default HOLogic.typeS)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   211
        end
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   212
      | NONE =>
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   213
        case strip_prefix_and_undo_ascii tvar_prefix a of
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   214
          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
   215
        | NONE =>
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   216
          (* Variable from the ATP, say "X1" *)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   217
          Type_Infer.param 0 (a, HOLogic.typeS)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   218
  end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   219
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   220
fun fix_atp_variable_name s =
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   221
  let
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   222
    fun subscript_name s n = s ^ nat_subscript n
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   223
    val s = String.map Char.toLower s
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   224
  in
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   225
    case space_explode "_" s of
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   226
      [_] => (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
   227
                (cs1 as _ :: _, cs2 as _ :: _) =>
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   228
                subscript_name (String.implode cs1)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   229
                               (the (Int.fromString (String.implode cs2)))
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   230
              | (_, _) => s)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   231
    | [s1, s2] => (case Int.fromString s2 of
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   232
                     SOME n => subscript_name s1 n
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   233
                   | NONE => s)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   234
    | _ => s
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   235
  end
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   236
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   237
(* First-order translation. No types are known for variables. "HOLogic.typeT"
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   238
   should allow them to be inferred.*)
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   239
fun term_from_fo_term thy full_types tfrees opt_T =
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   240
  let
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   241
    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
   242
      case u of
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   243
        ATerm ("hBOOL", [u1]) => aux (SOME @{typ bool}) [] u1
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   244
      | ATerm ("hAPP", [u1, u2]) => aux opt_T (u2 :: extra_us) u1
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   245
      | ATerm (a, us) =>
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   246
        if a = type_wrapper_name then
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   247
          case us of
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   248
            [typ_u, term_u] =>
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   249
            aux (SOME (type_from_fo_term tfrees typ_u)) extra_us term_u
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   250
          | _ => raise FO_TERM us
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   251
        else case strip_prefix_and_undo_ascii const_prefix a of
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   252
          SOME "equal" =>
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   253
          list_comb (Const (@{const_name "op ="}, HOLogic.typeT),
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   254
                     map (aux NONE []) us)
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   255
        | SOME b =>
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   256
          let
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   257
            val c = invert_const b
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   258
            val num_type_args = num_type_args thy c
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   259
            val (type_us, term_us) =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   260
              chop (if full_types then 0 else num_type_args) us
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   261
            (* Extra args from "hAPP" come after any arguments given directly to
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   262
               the constant. *)
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   263
            val term_ts = map (aux NONE []) term_us
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   264
            val extra_ts = map (aux NONE []) extra_us
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   265
            val t =
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   266
              Const (c, if full_types then
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   267
                          case opt_T of
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   268
                            SOME T => map fastype_of term_ts ---> T
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   269
                          | NONE =>
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   270
                            if num_type_args = 0 then
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   271
                              Sign.const_instance thy (c, [])
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   272
                            else
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   273
                              raise Fail ("no type information for " ^ quote c)
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   274
                        else
37998
f1b7fb87f523 remove more Skolemization-aware code
blanchet
parents: 37996
diff changeset
   275
                          Sign.const_instance thy (c,
f1b7fb87f523 remove more Skolemization-aware code
blanchet
parents: 37996
diff changeset
   276
                              map (type_from_fo_term tfrees) type_us))
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   277
          in list_comb (t, term_ts @ extra_ts) end
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   278
        | NONE => (* a free or schematic variable *)
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   279
          let
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37624
diff changeset
   280
            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
   281
            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
   282
            val t =
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   283
              case strip_prefix_and_undo_ascii fixed_var_prefix a of
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   284
                SOME b => Free (b, T)
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   285
              | NONE =>
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   286
                case strip_prefix_and_undo_ascii 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
   287
                  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
   288
                | NONE =>
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   289
                  (* Variable from the ATP, say "X1" *)
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
   290
                  Var ((fix_atp_variable_name a, 0), T)
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   291
          in list_comb (t, ts) end
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   292
  in aux opt_T [] end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   293
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   294
(* Type class literal applied to a type. Returns triple of polarity, class,
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   295
   type. *)
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   296
fun type_constraint_from_formula pos tfrees (AConn (ANot, [u])) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   297
    type_constraint_from_formula (not pos) tfrees u
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   298
  | type_constraint_from_formula pos tfrees (phi as APred (ATerm (a, us))) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   299
    (case (strip_prefix_and_undo_ascii class_prefix a,
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   300
           map (type_from_fo_term tfrees) us) of
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   301
       (SOME b, [T]) => (pos, b, T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   302
     | _ => raise FORMULA [phi])
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   303
  | type_constraint_from_formula _ _ phi = raise FORMULA [phi]
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   304
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   305
(** Accumulate type constraints in a clause: negative type literals **)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   306
36485
blanchet
parents: 36480
diff changeset
   307
fun add_var (key, z)  = Vartab.map_default (key, []) (cons z)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   308
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   309
fun add_type_constraint (false, cl, TFree (a ,_)) = add_var ((a, ~1), cl)
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   310
  | add_type_constraint (false, cl, TVar (ix, _)) = add_var (ix, cl)
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   311
  | add_type_constraint _ = I
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   312
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
   313
fun is_positive_literal (@{const Not} $ _) = false
37498
b426cbdb5a23 removed Sledgehammer's support for the DFG syntax;
blanchet
parents: 37479
diff changeset
   314
  | is_positive_literal _ = true
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   315
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   316
val combinator_table =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   317
  [(@{const_name COMBI}, @{thm COMBI_def_raw}),
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   318
   (@{const_name COMBK}, @{thm COMBK_def_raw}),
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   319
   (@{const_name COMBB}, @{thm COMBB_def_raw}),
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   320
   (@{const_name COMBC}, @{thm COMBC_def_raw}),
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   321
   (@{const_name COMBS}, @{thm COMBS_def_raw})]
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   322
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   323
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
   324
  | 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
   325
  | uncombine_term (t as Const (x as (s, _))) =
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   326
    (case AList.lookup (op =) combinator_table s of
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   327
       SOME thm => thm |> prop_of |> specialize_type @{theory} x |> Logic.dest_equals |> snd
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   328
     | NONE => t)
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   329
  | uncombine_term t = t
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   330
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   331
fun disjuncts (AConn (AOr, phis)) = maps disjuncts phis
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   332
  | disjuncts phi = [phi]
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   333
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   334
(* Update schematic type variables with detected sort constraints. It's not
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   335
   totally clear when this code is necessary. *)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   336
fun repair_tvar_sorts (tvar_tab, t) =
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   337
  let
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   338
    fun do_type (Type (a, Ts)) = Type (a, map do_type Ts)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   339
      | do_type (TVar (xi, s)) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   340
        TVar (xi, the_default s (Vartab.lookup tvar_tab xi))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   341
      | do_type (TFree z) = TFree z
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   342
    fun do_term (Const (a, T)) = Const (a, do_type T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   343
      | do_term (Free (a, T)) = Free (a, do_type T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   344
      | do_term (Var (xi, T)) = Var (xi, do_type T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   345
      | do_term (t as Bound _) = t
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   346
      | 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
   347
      | do_term (t1 $ t2) = do_term t1 $ do_term t2
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   348
  in t |> not (Vartab.is_empty tvar_tab) ? do_term end
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   349
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   350
fun s_disj (t1, @{const False}) = t1
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   351
  | s_disj p = HOLogic.mk_disj p
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   352
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   353
fun quantify_over_free quant_s free_s body_t =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   354
  case Term.add_frees body_t [] |> filter (curry (op =) free_s o fst) of
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   355
    [] => body_t
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   356
  | frees as (_, free_T) :: _ =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   357
    Abs (free_s, free_T, fold (curry abstract_over) (map Free frees) body_t)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   358
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   359
 (* Interpret a list of syntax trees as a clause, given by "real" literals and
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   360
   sort constraints. Accumulates sort constraints in "tvar_tab", with "real"
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   361
   literals in "lits". *)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   362
fun prop_from_formula thy full_types tfrees =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   363
   let
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   364
    val do_sort_constraint =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   365
      add_type_constraint o type_constraint_from_formula true tfrees
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   366
    fun do_real_literal phi =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   367
      case phi of
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   368
        AQuant (_, [], phi) => do_real_literal phi
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   369
      | AQuant (q, x :: xs, phi') =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   370
        let
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   371
          val body = do_real_literal (AQuant (q, xs, phi'))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   372
          val quant_s = case q of
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   373
                          AForall => @{const_name All}
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   374
                        | AExists => @{const_name Ex}
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   375
        in quantify_over_free quant_s x body end
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   376
      | AConn (ANot, [phi']) => HOLogic.mk_not (do_real_literal phi')
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   377
      | AConn (c, [phi1, phi2]) =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   378
        (phi1, phi2)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   379
        |> pairself do_real_literal
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   380
        |> (case c of
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   381
              AAnd => HOLogic.mk_conj
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   382
            | AOr => HOLogic.mk_disj
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   383
            | AImplies => HOLogic.mk_imp
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   384
            | AIff => (fn (t1, t2) => HOLogic.eq_const HOLogic.boolT $ t1 $ t2))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   385
      | APred tm =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   386
        term_from_fo_term thy full_types tfrees (SOME @{typ bool}) tm
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   387
      | _ => raise FORMULA [phi]
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   388
    fun do_literals (tvar_tab, t) [] = (tvar_tab, t)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   389
      | do_literals (tvar_tab, t) (u :: us) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   390
        (do_literals (tvar_tab |> do_sort_constraint u, t) us
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   391
         handle FO_TERM _ => raise SAME ()
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   392
              | FORMULA _ => raise SAME ())
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   393
        handle SAME () =>
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   394
               do_literals (tvar_tab, s_disj (do_real_literal u, t)) us
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   395
  in
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   396
    repair_tvar_sorts o do_literals (Vartab.empty, HOLogic.false_const)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   397
    o disjuncts
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   398
  end
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   399
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
   400
fun check_formula ctxt =
37145
01aa36932739 renamed structure TypeInfer to Type_Infer, keeping the old name as legacy alias for some time;
wenzelm
parents: 36968
diff changeset
   401
  Type_Infer.constrain @{typ bool}
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   402
  #> Syntax.check_term (ProofContext.set_mode ProofContext.mode_schematic ctxt)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   403
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   404
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   405
(**** Translation of TSTP files to Isar Proofs ****)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   406
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   407
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
   408
  | 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
   409
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   410
fun decode_line full_types tfrees (Definition (num, phi1, phi2)) ctxt =
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   411
    let
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   412
      val thy = ProofContext.theory_of ctxt
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   413
      val t1 = prop_from_formula thy full_types tfrees phi1
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   414
      val vars = snd (strip_comb t1)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   415
      val frees = map unvarify_term vars
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   416
      val unvarify_args = subst_atomic (vars ~~ frees)
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   417
      val t2 = prop_from_formula thy full_types tfrees phi2
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   418
      val (t1, t2) =
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   419
        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
   420
        |> unvarify_args |> uncombine_term |> check_formula ctxt
36555
8ff45c2076da expand combinators in Isar proofs constructed by Sledgehammer;
blanchet
parents: 36551
diff changeset
   421
        |> HOLogic.dest_eq
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   422
    in
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   423
      (Definition (num, t1, t2),
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   424
       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
   425
    end
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   426
  | decode_line full_types tfrees (Inference (num, u, deps)) ctxt =
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   427
    let
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   428
      val thy = ProofContext.theory_of ctxt
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   429
      val t = u |> prop_from_formula thy full_types tfrees
37998
f1b7fb87f523 remove more Skolemization-aware code
blanchet
parents: 37996
diff changeset
   430
                |> uncombine_term |> check_formula ctxt
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   431
    in
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   432
      (Inference (num, t, deps),
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   433
       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
   434
    end
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
   435
fun decode_lines ctxt full_types tfrees lines =
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
   436
  fst (fold_map (decode_line full_types tfrees) lines ctxt)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   437
37323
2f2f0d246d0f handle Vampire's definitions smoothly
blanchet
parents: 37322
diff changeset
   438
fun aint_actual_inference _ (Definition _) = true
2f2f0d246d0f handle Vampire's definitions smoothly
blanchet
parents: 37322
diff changeset
   439
  | aint_actual_inference t (Inference (_, t', _)) = not (t aconv t')
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   440
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   441
(* 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
   442
   clsarity). *)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   443
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
   444
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   445
fun replace_one_dep (old, new) dep = if dep = old then new else [dep]
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   446
fun replace_deps_in_line _ (line as Definition _) = line
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   447
  | replace_deps_in_line p (Inference (num, t, deps)) =
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   448
    Inference (num, t, fold (union (op =) o replace_one_dep p) deps [])
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   449
22491
535fbed859da Numerous bug fixes. Type clauses distinguished from empty clauses. Working proof reduction.
paulson
parents: 22470
diff changeset
   450
(*Discard axioms; consolidate adjacent lines that prove the same clause, since they differ
535fbed859da Numerous bug fixes. Type clauses distinguished from empty clauses. Working proof reduction.
paulson
parents: 22470
diff changeset
   451
  only in type information.*)
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   452
fun add_line _ _ (line as Definition _) lines = line :: lines
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   453
  | add_line conjecture_shape thm_names (Inference (num, t, [])) lines =
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
   454
    (* No dependencies: axiom, conjecture clause, or internal axioms or
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
   455
       definitions (Vampire). *)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   456
    if is_axiom_clause_number thm_names num then
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   457
      (* Axioms are not proof lines. *)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   458
      if is_only_type_information t then
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   459
        map (replace_deps_in_line (num, [])) lines
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   460
      (* Is there a repetition? If so, replace later line by earlier one. *)
37323
2f2f0d246d0f handle Vampire's definitions smoothly
blanchet
parents: 37322
diff changeset
   461
      else case take_prefix (aint_actual_inference t) lines of
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   462
        (_, []) => lines (*no repetition of proof line*)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   463
      | (pre, Inference (num', _, _) :: post) =>
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   464
        pre @ map (replace_deps_in_line (num', [num])) post
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
   465
    else if is_conjecture_clause_number conjecture_shape num then
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   466
      Inference (num, t, []) :: lines
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   467
    else
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
   468
      map (replace_deps_in_line (num, [])) lines
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   469
  | add_line _ _ (Inference (num, t, deps)) lines =
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   470
    (* 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
   471
    if is_only_type_information t then
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   472
      Inference (num, t, deps) :: lines
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   473
    (* Is there a repetition? If so, replace later line by earlier one. *)
37323
2f2f0d246d0f handle Vampire's definitions smoothly
blanchet
parents: 37322
diff changeset
   474
    else case take_prefix (aint_actual_inference t) lines of
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   475
      (* FIXME: Doesn't this code risk conflating proofs involving different
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   476
         types?? *)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   477
       (_, []) => Inference (num, t, deps) :: lines
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   478
     | (pre, Inference (num', t', _) :: post) =>
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   479
       Inference (num, t', deps) ::
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   480
       pre @ map (replace_deps_in_line (num', [num])) post
22044
6c0702a96076 More compact proof reconstruction: lines having fewer than !min_deps dependences are folded
paulson
parents: 22012
diff changeset
   481
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   482
(* Recursively delete empty lines (type information) from the proof. *)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   483
fun add_nontrivial_line (Inference (num, t, [])) lines =
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   484
    if is_only_type_information t then delete_dep num lines
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   485
    else Inference (num, t, []) :: lines
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   486
  | add_nontrivial_line line lines = line :: lines
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   487
and delete_dep num lines =
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   488
  fold_rev add_nontrivial_line (map (replace_deps_in_line (num, [])) lines) []
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   489
37323
2f2f0d246d0f handle Vampire's definitions smoothly
blanchet
parents: 37322
diff changeset
   490
(* ATPs sometimes reuse free variable names in the strangest ways. Removing
2f2f0d246d0f handle Vampire's definitions smoothly
blanchet
parents: 37322
diff changeset
   491
   offending lines often does the trick. *)
36560
45c1870f234f fixed definition of "bad frees" so that it actually works
blanchet
parents: 36559
diff changeset
   492
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
   493
  | is_bad_free _ _ = false
22470
0d52e5157124 No label on "show"; tries to remove dependencies more cleanly
paulson
parents: 22428
diff changeset
   494
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
   495
(* Vampire is keen on producing these. *)
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
   496
fun is_trivial_formula (@{const Not} $ (Const (@{const_name "op ="}, _)
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
   497
                                        $ t1 $ t2)) = (t1 aconv t2)
37498
b426cbdb5a23 removed Sledgehammer's support for the DFG syntax;
blanchet
parents: 37479
diff changeset
   498
  | is_trivial_formula _ = false
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
   499
37498
b426cbdb5a23 removed Sledgehammer's support for the DFG syntax;
blanchet
parents: 37479
diff changeset
   500
fun add_desired_line _ _ _ _ (line as Definition (num, _, _)) (j, lines) =
37323
2f2f0d246d0f handle Vampire's definitions smoothly
blanchet
parents: 37322
diff changeset
   501
    (j, line :: map (replace_deps_in_line (num, [])) lines)
37498
b426cbdb5a23 removed Sledgehammer's support for the DFG syntax;
blanchet
parents: 37479
diff changeset
   502
  | add_desired_line isar_shrink_factor conjecture_shape thm_names frees
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
   503
                     (Inference (num, t, deps)) (j, lines) =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   504
    (j + 1,
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
   505
     if is_axiom_clause_number thm_names num orelse
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
   506
        is_conjecture_clause_number conjecture_shape num orelse
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
   507
        (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
   508
         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
   509
         not (exists_subterm (is_bad_free frees) 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
   510
         not (is_trivial_formula 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
   511
         (null lines orelse (* last line must be kept *)
36924
ff01d3ae9ad4 renamed options
blanchet
parents: 36909
diff changeset
   512
          (length deps >= 2 andalso j mod isar_shrink_factor = 0))) then
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
   513
       Inference (num, 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
   514
     else
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
   515
       map (replace_deps_in_line (num, deps)) lines)  (* drop line *)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   516
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   517
(** EXTRACTING LEMMAS **)
21979
80e10f181c46 Improvements to proof reconstruction. Now "fixes" is inserted
paulson
parents: 21978
diff changeset
   518
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   519
(* A list consisting of the first number in each line is returned. For TSTP,
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   520
   interesting lines have the form "fof(108, axiom, ...)", where the number
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   521
   (108) is extracted. For SPASS, lines have the form "108[0:Inp] ...", where
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   522
   the first number (108) is extracted. *)
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37926
diff changeset
   523
fun extract_formula_numbers_in_atp_proof atp_proof =
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   524
  let
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37961
diff changeset
   525
    val tokens_of = String.tokens (not o Char.isAlphaNum)
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37926
diff changeset
   526
    fun extract_num ("fof" :: num :: "axiom" :: _) = Int.fromString num
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   527
      | extract_num (num :: "0" :: "Inp" :: _) = Int.fromString num
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   528
      | extract_num _ = NONE
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   529
  in atp_proof |> split_lines |> map_filter (extract_num o tokens_of) end
37399
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   530
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   531
val indent_size = 2
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   532
val no_label = ("", ~1)
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   533
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   534
val raw_prefix = "X"
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   535
val assum_prefix = "A"
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   536
val fact_prefix = "F"
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   537
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   538
fun string_for_label (s, num) = s ^ string_of_int num
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   539
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   540
fun metis_using [] = ""
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   541
  | metis_using ls =
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   542
    "using " ^ space_implode " " (map string_for_label ls) ^ " "
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   543
fun metis_apply _ 1 = "by "
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   544
  | metis_apply 1 _ = "apply "
34f080a12063 proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet
parents: 37324
diff changeset
   545
  | metis_apply i _ = "prefer " ^ string_of_int i ^ " apply "
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   546
fun metis_name full_types = if full_types then "metisFT" else "metis"
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   547
fun metis_call full_types [] = metis_name full_types
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   548
  | metis_call full_types ss =
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   549
    "(" ^ metis_name full_types ^ " " ^ space_implode " " ss ^ ")"
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   550
fun metis_command full_types i n (ls, ss) =
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   551
  metis_using ls ^ metis_apply i n ^ metis_call full_types ss
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   552
fun metis_line full_types i n ss =
36063
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   553
  "Try this command: " ^
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   554
  Markup.markup Markup.sendback (metis_command full_types i n ([], ss)) ^ ".\n"
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
   555
fun minimize_line _ [] = ""
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
   556
  | minimize_line minimize_command facts =
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
   557
    case minimize_command facts of
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
   558
      "" => ""
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
   559
    | command =>
36065
3fc077c4780a fixed layout of Sledgehammer output
blanchet
parents: 36064
diff changeset
   560
      "To minimize the number of lemmas, try this command: " ^
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
   561
      Markup.markup Markup.sendback command ^ ".\n"
31840
beeaa1ed1f47 check if conjectures have been used in proof
immler@in.tum.de
parents: 31479
diff changeset
   562
37171
fc1e20373e6a make sure chained facts appear in Isar proofs generated by Sledgehammer -- otherwise the proof won't work
blanchet
parents: 36968
diff changeset
   563
val unprefix_chained = perhaps (try (unprefix chained_prefix))
fc1e20373e6a make sure chained facts appear in Isar proofs generated by Sledgehammer -- otherwise the proof won't work
blanchet
parents: 36968
diff changeset
   564
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   565
fun metis_proof_text (full_types, minimize_command, atp_proof, thm_names, goal,
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   566
                      i) =
36063
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   567
  let
37171
fc1e20373e6a make sure chained facts appear in Isar proofs generated by Sledgehammer -- otherwise the proof won't work
blanchet
parents: 36968
diff changeset
   568
    val raw_lemmas =
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37926
diff changeset
   569
      atp_proof |> extract_formula_numbers_in_atp_proof
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   570
                |> filter (is_axiom_clause_number thm_names)
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   571
                |> map (fn i => Vector.sub (thm_names, i - 1))
37171
fc1e20373e6a make sure chained facts appear in Isar proofs generated by Sledgehammer -- otherwise the proof won't work
blanchet
parents: 36968
diff changeset
   572
    val (chained_lemmas, other_lemmas) =
fc1e20373e6a make sure chained facts appear in Isar proofs generated by Sledgehammer -- otherwise the proof won't work
blanchet
parents: 36968
diff changeset
   573
      raw_lemmas |> List.partition (String.isPrefix chained_prefix)
fc1e20373e6a make sure chained facts appear in Isar proofs generated by Sledgehammer -- otherwise the proof won't work
blanchet
parents: 36968
diff changeset
   574
                 |>> map (unprefix chained_prefix)
fc1e20373e6a make sure chained facts appear in Isar proofs generated by Sledgehammer -- otherwise the proof won't work
blanchet
parents: 36968
diff changeset
   575
                 |> pairself (sort_distinct string_ord)
fc1e20373e6a make sure chained facts appear in Isar proofs generated by Sledgehammer -- otherwise the proof won't work
blanchet
parents: 36968
diff changeset
   576
    val lemmas = other_lemmas @ chained_lemmas
36063
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   577
    val n = Logic.count_prems (prop_of goal)
37171
fc1e20373e6a make sure chained facts appear in Isar proofs generated by Sledgehammer -- otherwise the proof won't work
blanchet
parents: 36968
diff changeset
   578
  in
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   579
    (metis_line full_types i n other_lemmas ^
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   580
     minimize_line minimize_command lemmas, lemmas)
37171
fc1e20373e6a make sure chained facts appear in Isar proofs generated by Sledgehammer -- otherwise the proof won't work
blanchet
parents: 36968
diff changeset
   581
  end
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents: 30874
diff changeset
   582
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   583
(** Isar proof construction and manipulation **)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   584
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   585
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
   586
  (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
   587
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   588
type label = string * int
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   589
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
   590
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   591
datatype 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
   592
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   593
datatype step =
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   594
  Fix of (string * typ) list |
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   595
  Let of term * term |
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   596
  Assume of label * term |
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   597
  Have of qualifier list * label * term * byline
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   598
and byline =
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   599
  ByMetis of facts |
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   600
  CaseSplit of step list list * facts
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   601
36574
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   602
fun smart_case_split [] facts = ByMetis facts
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   603
  | smart_case_split proofs facts = CaseSplit (proofs, facts)
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   604
36475
05209b869a6b new Isar proof construction code: stringfy axiom names correctly
blanchet
parents: 36474
diff changeset
   605
fun add_fact_from_dep thm_names num =
05209b869a6b new Isar proof construction code: stringfy axiom names correctly
blanchet
parents: 36474
diff changeset
   606
  if is_axiom_clause_number thm_names num then
36480
1e01a7162435 polish Isar proofs: don't mention facts twice, and don't show one-liner "structured" proofs
blanchet
parents: 36479
diff changeset
   607
    apsnd (insert (op =) (Vector.sub (thm_names, num - 1)))
36475
05209b869a6b new Isar proof construction code: stringfy axiom names correctly
blanchet
parents: 36474
diff changeset
   608
  else
36480
1e01a7162435 polish Isar proofs: don't mention facts twice, and don't show one-liner "structured" proofs
blanchet
parents: 36479
diff changeset
   609
    apfst (insert (op =) (raw_prefix, num))
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   610
37998
f1b7fb87f523 remove more Skolemization-aware code
blanchet
parents: 37996
diff changeset
   611
fun forall_of v t = HOLogic.all_const (fastype_of v) $ lambda v t
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
   612
fun forall_vars t = fold_rev forall_of (map Var (Term.add_vars t [])) t
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
   613
37498
b426cbdb5a23 removed Sledgehammer's support for the DFG syntax;
blanchet
parents: 37479
diff changeset
   614
fun step_for_line _ _ (Definition (_, t1, t2)) = Let (t1, t2)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   615
  | step_for_line _ _ (Inference (num, t, [])) = Assume ((raw_prefix, num), t)
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   616
  | step_for_line thm_names j (Inference (num, t, deps)) =
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   617
    Have (if j = 1 then [Show] else [], (raw_prefix, num),
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
   618
          forall_vars t,
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   619
          ByMetis (fold (add_fact_from_dep thm_names) deps ([], [])))
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   620
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
   621
fun proof_from_atp_proof pool ctxt full_types tfrees isar_shrink_factor
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
   622
                         atp_proof conjecture_shape thm_names params frees =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   623
  let
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   624
    val lines =
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   625
      atp_proof ^ "$" (* the $ sign acts as a sentinel (FIXME: pick it up) *)
36548
a8a6d7172c8c try out Vampire 11 and parse its output correctly;
blanchet
parents: 36492
diff changeset
   626
      |> parse_proof pool
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
   627
      |> decode_lines ctxt full_types tfrees
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   628
      |> rpair [] |-> fold_rev (add_line conjecture_shape thm_names)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   629
      |> rpair [] |-> fold_rev add_nontrivial_line
37498
b426cbdb5a23 removed Sledgehammer's support for the DFG syntax;
blanchet
parents: 37479
diff changeset
   630
      |> rpair (0, []) |-> fold_rev (add_desired_line isar_shrink_factor
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
   631
                                               conjecture_shape thm_names frees)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   632
      |> snd
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   633
  in
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   634
    (if null params then [] else [Fix params]) @
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   635
    map2 (step_for_line thm_names) (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
   636
  end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   637
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   638
(* 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
   639
   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
   640
   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
   641
   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
   642
   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
   643
   case split. *)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   644
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
   645
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
   646
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
   647
    (case by of
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   648
       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
   649
     | 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
   650
       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
   651
  | 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
   652
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
   653
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   654
fun new_labels_of_step (Fix _) = []
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   655
  | 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
   656
  | 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
   657
  | 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
   658
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
   659
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   660
val join_proofs =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   661
  let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   662
    fun aux _ [] = NONE
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   663
      | 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
   664
        if exists null proofs then
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   665
          NONE
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   666
        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
   667
          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
   668
        else case hd proof1 of
37498
b426cbdb5a23 removed Sledgehammer's support for the DFG syntax;
blanchet
parents: 37479
diff changeset
   669
          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
   670
          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
   671
                      | _ => false) (tl proofs) andalso
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   672
             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
   673
                         (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
   674
            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
   675
          else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   676
            NONE
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   677
        | _ => NONE
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   678
  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
   679
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   680
fun case_split_qualifiers proofs =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   681
  case length proofs of
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   682
    0 => []
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   683
  | 1 => [Then]
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   684
  | _ => [Ultimately]
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   685
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   686
fun redirect_proof conjecture_shape hyp_ts concl_t proof =
33310
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
   687
  let
37324
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   688
    (* 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
   689
       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
   690
       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
   691
       several facts that depend on the negated conjecture. *)
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   692
    fun find_hyp num = nth hyp_ts (index_in_shape num conjecture_shape)
37996
11c076ea92e9 simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents: 37995
diff changeset
   693
    val concl_l = (raw_prefix, List.last conjecture_shape)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   694
    fun first_pass ([], contra) = ([], contra)
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
   695
      | first_pass ((step as Fix _) :: proof, contra) =
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
   696
        first_pass (proof, contra) |>> cons step
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
   697
      | first_pass ((step as Let _) :: proof, contra) =
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
   698
        first_pass (proof, contra) |>> cons step
37498
b426cbdb5a23 removed Sledgehammer's support for the DFG syntax;
blanchet
parents: 37479
diff changeset
   699
      | first_pass ((step as Assume (l as (_, num), _)) :: proof, contra) =
37996
11c076ea92e9 simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents: 37995
diff changeset
   700
        if l = concl_l then
11c076ea92e9 simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents: 37995
diff changeset
   701
          first_pass (proof, contra ||> l = concl_l ? cons step)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   702
        else
36551
cc42df660808 improve unskolemization
blanchet
parents: 36548
diff changeset
   703
          first_pass (proof, contra) |>> cons (Assume (l, find_hyp num))
37324
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   704
      | first_pass (Have (qs, l, t, ByMetis (ls, ss)) :: proof, contra) =
37996
11c076ea92e9 simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents: 37995
diff changeset
   705
        let val step = Have (qs, l, t, ByMetis (ls, ss)) in
37324
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   706
          if exists (member (op =) (fst contra)) ls then
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   707
            first_pass (proof, contra |>> cons l ||> cons step)
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   708
          else
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   709
            first_pass (proof, contra) |>> cons step
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   710
        end
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   711
      | first_pass _ = raise Fail "malformed proof"
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   712
    val (proof_top, (contra_ls, contra_proof)) =
37996
11c076ea92e9 simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents: 37995
diff changeset
   713
      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
   714
    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
   715
    fun backpatch_labels patches ls =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   716
      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
   717
    fun second_pass end_qs ([], assums, patches) =
37324
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   718
        ([Have (end_qs, no_label, concl_t,
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   719
                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
   720
      | 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
   721
        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
   722
      | 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
   723
                            patches) =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   724
        if member (op =) (snd (snd patches)) l andalso
37322
0347b1a16682 fix bug in direct Isar proofs, which was exhibited by the "BigO" example
blanchet
parents: 37319
diff changeset
   725
           not (member (op =) (fst (snd patches)) l) andalso
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   726
           not (AList.defined (op =) (fst patches) l) then
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   727
          second_pass end_qs (proof, assums, patches ||> apsnd (append ls))
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   728
        else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   729
          (case List.partition (member (op =) contra_ls) ls of
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   730
             ([contra_l], co_ls) =>
37322
0347b1a16682 fix bug in direct Isar proofs, which was exhibited by the "BigO" example
blanchet
parents: 37319
diff changeset
   731
             if member (op =) qs Show then
0347b1a16682 fix bug in direct Isar proofs, which was exhibited by the "BigO" example
blanchet
parents: 37319
diff changeset
   732
               second_pass end_qs (proof, assums,
0347b1a16682 fix bug in direct Isar proofs, which was exhibited by the "BigO" example
blanchet
parents: 37319
diff changeset
   733
                                   patches |>> cons (contra_l, (co_ls, ss)))
0347b1a16682 fix bug in direct Isar proofs, which was exhibited by the "BigO" example
blanchet
parents: 37319
diff changeset
   734
             else
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   735
               second_pass end_qs
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   736
                           (proof, assums,
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   737
                            patches |>> cons (contra_l, (l :: co_ls, ss)))
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   738
               |>> cons (if member (op =) (fst (snd patches)) l then
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   739
                           Assume (l, negate_term t)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   740
                         else
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   741
                           Have (qs, l, negate_term t,
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   742
                                 ByMetis (backpatch_label patches l)))
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   743
           | (contra_ls as _ :: _, co_ls) =>
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   744
             let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   745
               val proofs =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   746
                 map_filter
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   747
                     (fn l =>
37996
11c076ea92e9 simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents: 37995
diff changeset
   748
                         if l = concl_l then
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   749
                           NONE
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   750
                         else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   751
                           let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   752
                             val drop_ls = filter (curry (op <>) l) contra_ls
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   753
                           in
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   754
                             second_pass []
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   755
                                 (proof, assums,
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   756
                                  patches ||> apfst (insert (op =) l)
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   757
                                          ||> apsnd (union (op =) drop_ls))
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   758
                             |> fst |> SOME
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   759
                           end) contra_ls
37324
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   760
               val (assumes, facts) =
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   761
                 if member (op =) (fst (snd patches)) l then
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   762
                   ([Assume (l, negate_term t)], (l :: co_ls, ss))
37324
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   763
                 else
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   764
                   ([], (co_ls, ss))
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   765
             in
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   766
               (case join_proofs proofs of
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   767
                  SOME (l, t, proofs, proof_tail) =>
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   768
                  Have (case_split_qualifiers proofs @
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   769
                        (if null proof_tail then end_qs else []), l, t,
36574
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   770
                        smart_case_split proofs facts) :: proof_tail
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   771
                | NONE =>
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   772
                  [Have (case_split_qualifiers proofs @ end_qs, no_label,
36574
870dfa6d00ce eliminate trivial case splits from Isar proofs
blanchet
parents: 36570
diff changeset
   773
                         concl_t, smart_case_split proofs facts)],
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   774
                patches)
37324
d77250dd2416 fix bugs in Sledgehammer's Isar proof "redirection" code
blanchet
parents: 37323
diff changeset
   775
               |>> append assumes
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   776
             end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   777
           | _ => raise Fail "malformed proof")
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   778
       | second_pass _ _ = raise Fail "malformed proof"
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   779
    val proof_bottom =
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   780
      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
   781
  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
   782
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   783
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
   784
  let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   785
    fun relabel_facts subst =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   786
      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
   787
    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
   788
        (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
   789
           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
   790
         | 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
   791
      | 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
   792
        (Have (qs, l, t,
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   793
               case by of
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   794
                 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
   795
               | CaseSplit (proofs, facts) =>
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   796
                 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
   797
         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
   798
      | 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
   799
    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
   800
  in do_proof end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   801
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   802
val then_chain_proof =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   803
  let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   804
    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
   805
      | 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
   806
      | 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
   807
        (case by of
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   808
           ByMetis (ls, ss) =>
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   809
           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
   810
                   (Then :: qs, l, t,
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   811
                    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
   812
                 else
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   813
                   (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
   814
         | CaseSplit (proofs, facts) =>
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   815
           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
   816
        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
   817
      | 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
   818
  in aux no_label end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   819
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   820
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
   821
  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
   822
    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
   823
    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
   824
    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
   825
      | 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
   826
        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
   827
              case by of
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   828
                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
   829
                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
   830
              | _ => 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
   831
      | 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
   832
  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
   833
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   834
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
   835
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   836
val relabel_proof =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   837
  let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   838
    fun aux _ _ _ [] = []
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   839
      | 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
   840
        if l = no_label then
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   841
          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
   842
        else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   843
          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
   844
            Assume (l', t) ::
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   845
            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
   846
          end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   847
      | 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
   848
        let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   849
          val (l', subst, next_fact) =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   850
            if l = no_label then
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   851
              (l, subst, next_fact)
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   852
            else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   853
              let
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   854
                val l' = (prefix_for_depth depth fact_prefix, next_fact)
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   855
              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
   856
          val relabel_facts =
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
   857
            apfst (map (fn l =>
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
   858
                           case AList.lookup (op =) subst l of
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
   859
                             SOME l' => l'
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
   860
                           | NONE => raise Fail ("unknown label " ^
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
   861
                                                 quote (string_for_label l))))
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   862
          val by =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   863
            case by of
36564
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   864
              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
   865
            | CaseSplit (proofs, facts) =>
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   866
              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
   867
                         relabel_facts facts)
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   868
        in
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   869
          Have (qs, l', t, by) ::
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   870
          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
   871
        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
   872
      | 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
   873
        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
   874
  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
   875
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   876
fun string_for_proof ctxt full_types i n =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   877
  let
37319
42268dc7d6c4 show types in Isar proofs, but not for free variables;
blanchet
parents: 37172
diff changeset
   878
    fun fix_print_mode f x =
42268dc7d6c4 show types in Isar proofs, but not for free variables;
blanchet
parents: 37172
diff changeset
   879
      setmp_CRITICAL show_no_free_types true
42268dc7d6c4 show types in Isar proofs, but not for free variables;
blanchet
parents: 37172
diff changeset
   880
          (setmp_CRITICAL show_types true
42268dc7d6c4 show types in Isar proofs, but not for free variables;
blanchet
parents: 37172
diff changeset
   881
               (Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN)
42268dc7d6c4 show types in Isar proofs, but not for free variables;
blanchet
parents: 37172
diff changeset
   882
                                         (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
   883
    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
   884
    fun do_free (s, T) =
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   885
      maybe_quote s ^ " :: " ^
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   886
      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
   887
    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
   888
    fun do_have qs =
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   889
      (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
   890
      (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
   891
      (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
   892
         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
   893
       else
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   894
         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
   895
    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
   896
    fun do_facts (ls, ss) =
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
   897
      let
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
   898
        val ls = ls |> sort_distinct (prod_ord string_ord int_ord)
37171
fc1e20373e6a make sure chained facts appear in Isar proofs generated by Sledgehammer -- otherwise the proof won't work
blanchet
parents: 36968
diff changeset
   899
        val ss = ss |> map unprefix_chained |> sort_distinct string_ord
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   900
      in metis_command full_types 1 1 (ls, ss) end
36478
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   901
    and do_step ind (Fix xs) =
1aba777a367f fix types of "fix" variables to help proof reconstruction and aid readability
blanchet
parents: 36477
diff changeset
   902
        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
   903
      | do_step ind (Let (t1, t2)) =
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   904
        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
   905
      | 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
   906
        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
   907
      | 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
   908
        do_indent ind ^ do_have qs ^ " " ^
36479
fcbf412c560f reintroduce missing "gen_all_vars" call
blanchet
parents: 36478
diff changeset
   909
        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
   910
      | 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
   911
        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
   912
                      (map (do_block ind) proofs) ^
36479
fcbf412c560f reintroduce missing "gen_all_vars" call
blanchet
parents: 36478
diff changeset
   913
        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
   914
        do_facts facts ^ "\n"
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   915
    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
   916
      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
   917
        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
   918
        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
   919
                        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
   920
        suffix ^ "\n"
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   921
      end
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   922
    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
   923
    (* 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
   924
       directly. *)
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   925
    and do_proof [Have (_, _, _, ByMetis _)] = ""
96f767f546e7 be more discriminate: some one-line Isar proofs are silly
blanchet
parents: 36563
diff changeset
   926
      | 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
   927
        (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
1e01a7162435 polish Isar proofs: don't mention facts twice, and don't show one-liner "structured" proofs
blanchet
parents: 36479
diff changeset
   928
        do_indent 0 ^ "proof -\n" ^
1e01a7162435 polish Isar proofs: don't mention facts twice, and don't show one-liner "structured" proofs
blanchet
parents: 36479
diff changeset
   929
        do_steps "" "" 1 proof ^
1e01a7162435 polish Isar proofs: don't mention facts twice, and don't show one-liner "structured" proofs
blanchet
parents: 36479
diff changeset
   930
        do_indent 0 ^ (if n <> 1 then "next" else "qed") ^ "\n"
36488
32c92af68ec9 remove Sledgehammer's "sorts" option to annotate variables with sorts in proof;
blanchet
parents: 36486
diff changeset
   931
  in do_proof end
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   932
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   933
fun isar_proof_text (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   934
                    (other_params as (full_types, _, atp_proof, thm_names, goal,
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   935
                                      i)) =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   936
  let
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   937
    (* ### FIXME: avoid duplication with ATP_Systems -- and move strip_subgoal
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   938
       to ATP_Systems *)
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   939
    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
   940
    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
   941
    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
   942
    val n = Logic.count_prems (prop_of goal)
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   943
    val (one_line_proof, lemma_names) = metis_proof_text other_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
   944
    fun isar_proof_for () =
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
   945
      case proof_from_atp_proof pool ctxt full_types tfrees isar_shrink_factor
36924
ff01d3ae9ad4 renamed options
blanchet
parents: 36909
diff changeset
   946
                                atp_proof conjecture_shape thm_names params
ff01d3ae9ad4 renamed options
blanchet
parents: 36909
diff changeset
   947
                                frees
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   948
           |> redirect_proof conjecture_shape hyp_ts concl_t
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   949
           |> kill_duplicate_assumptions_in_proof
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   950
           |> then_chain_proof
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   951
           |> kill_useless_labels_in_proof
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   952
           |> relabel_proof
37479
f6b1ee5b420b try to improve Sledgehammer/Metis's behavior in full_types mode, e.g. by handing True, False, and If better
blanchet
parents: 37410
diff changeset
   953
           |> string_for_proof ctxt full_types i n of
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
   954
        "" => ""
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   955
      | proof => "\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
   956
    val isar_proof =
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   957
      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
   958
        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
   959
      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
   960
        try isar_proof_for ()
36287
96f45c5ffb36 if Isar proof reconstruction is not supported, tell the user so they don't wonder why their "isar_proof" option did nothing
blanchet
parents: 36285
diff changeset
   961
        |> the_default "Warning: The Isar proof construction failed.\n"
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
   962
  in (one_line_proof ^ isar_proof, lemma_names) end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   963
36557
3c2438efe224 make SML/NJ happy, take 2
blanchet
parents: 36556
diff changeset
   964
fun proof_text isar_proof isar_params other_params =
3c2438efe224 make SML/NJ happy, take 2
blanchet
parents: 36556
diff changeset
   965
  (if isar_proof then isar_proof_text isar_params else metis_proof_text)
3c2438efe224 make SML/NJ happy, take 2
blanchet
parents: 36556
diff changeset
   966
      other_params
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
   967
31038
immler@in.tum.de
parents: 31037
diff changeset
   968
end;