src/HOL/Tools/Sledgehammer/sledgehammer_proof_reconstruct.ML
author blanchet
Sun, 25 Apr 2010 15:04:20 +0200
changeset 36395 e73923451f6f
parent 36393 be73a2b2443b
child 36396 417cdfb2746a
permissions -rw-r--r--
cosmetics
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
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    11
  type name_pool = Sledgehammer_FOL_Clause.name_pool
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    12
25492
4cc7976948ac Chained theorems are no longer mentioned in metis calls and (if used) they prevent the
paulson
parents: 25414
diff changeset
    13
  val chained_hint: string
24425
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
    14
  val invert_const: string -> string
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
    15
  val invert_type_const: string -> string
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33042
diff changeset
    16
  val num_typargs: theory -> string -> int
24425
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
    17
  val make_tvar: string -> typ
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
    18
  val strip_prefix: string -> string -> string option
36063
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
    19
  val metis_line: int -> int -> string list -> string
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
    20
  val metis_proof_text:
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
    21
    minimize_command * string * string vector * thm * int
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    22
    -> string * string list
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
    23
  val isar_proof_text:
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    24
    name_pool option -> bool -> int -> bool -> Proof.context
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
    25
    -> minimize_command * string * string vector * thm * int
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
    26
    -> string * string list
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
    27
  val proof_text:
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    28
    bool -> name_pool option -> bool -> int -> bool -> Proof.context
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
    29
    -> minimize_command * string * string vector * thm * int
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
    30
    -> string * string list
24425
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
    31
end;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    32
35826
1590abc3d42a renamed Sledgehammer structures
blanchet
parents: 35825
diff changeset
    33
structure Sledgehammer_Proof_Reconstruct : SLEDGEHAMMER_PROOF_RECONSTRUCT =
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    34
struct
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    35
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
    36
open Sledgehammer_FOL_Clause
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
    37
open Sledgehammer_Fact_Preprocessor
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    38
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    39
type minimize_command = string list -> string
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
    40
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
    41
fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    42
fun is_head_digit s = Char.isDigit (String.sub (s, 0))
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
    43
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
    44
fun is_axiom_clause_number thm_names line_num =
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
    45
  line_num <= Vector.length thm_names
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
    46
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    47
fun ugly_name NONE s = s
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    48
  | ugly_name (SOME the_pool) s =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    49
    case Symtab.lookup (snd the_pool) s of
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    50
      SOME s' => s'
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    51
    | NONE => s
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    52
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    53
val trace_path = Path.basic "sledgehammer_proof_trace"
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    54
fun trace_proof_msg f =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    55
  if !trace then File.append (File.tmp_path trace_path) (f ()) else ();
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    56
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    57
val string_of_thm = PrintMode.setmp [] o Display.string_of_thm
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    58
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    59
(**** PARSING OF TSTP FORMAT ****)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    60
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
    61
(* Syntax trees, either term list or formulae *)
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    62
datatype stree = SInt of int | SBranch of string * stree list;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    63
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    64
fun atom x = SBranch (x, [])
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    65
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    66
fun scons (x, y) = SBranch ("cons", [x, y])
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    67
val slist_of = List.foldl scons (atom "nil")
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    68
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    69
(*Strings enclosed in single quotes, e.g. filenames*)
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    70
val parse_quoted = $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    71
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    72
(*Integer constants, typically proof line numbers*)
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    73
val parse_integer = Scan.many1 is_head_digit >> (the o Int.fromString o implode)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    74
36369
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36293
diff changeset
    75
(* needed for SPASS's output format *)
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    76
fun repair_bool_literal "true" = "c_True"
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    77
  | repair_bool_literal "false" = "c_False"
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    78
fun repair_name pool "equal" = "c_equal"
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    79
  | repair_name pool s = ugly_name pool s
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    80
(* 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
    81
(* 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
    82
   forever at compile time. *)
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    83
fun parse_term pool x =
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    84
  (parse_quoted >> atom
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    85
   || parse_integer >> SInt
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    86
   || $$ "$" |-- Symbol.scan_id >> (atom o repair_bool_literal)
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    87
   || (Symbol.scan_id >> repair_name pool)
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    88
      -- Scan.optional ($$ "(" |-- parse_terms pool --| $$ ")") [] >> SBranch
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    89
   || $$ "(" |-- parse_term pool --| $$ ")"
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    90
   || $$ "[" |-- Scan.optional (parse_terms pool) [] --| $$ "]" >> slist_of) x
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    91
and parse_terms pool x =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    92
  (parse_term pool ::: Scan.repeat ($$ "," |-- parse_term pool)) x
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    93
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    94
fun negate_stree t = SBranch ("c_Not", [t])
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    95
fun equate_strees t1 t2 = SBranch ("c_equal", [t1, t2]);
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    96
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
    97
(* Apply equal or not-equal to a term. *)
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    98
fun repair_predicate_term (t, NONE) = t
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
    99
  | repair_predicate_term (t1, SOME (NONE, t2)) = equate_strees t1 t2
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   100
  | repair_predicate_term (t1, SOME (SOME _, t2)) =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   101
    negate_stree (equate_strees t1 t2)
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   102
fun parse_predicate_term pool =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   103
  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
   104
                                  -- parse_term pool)
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   105
  >> repair_predicate_term
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   106
(*Literals can involve negation, = and !=.*)
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   107
fun parse_literal pool x =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   108
  ($$ "~" |-- parse_literal pool >> negate_stree || parse_predicate_term pool) x
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   109
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   110
fun parse_literals pool =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   111
  parse_literal pool ::: Scan.repeat ($$ "|" |-- parse_literal pool)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   112
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   113
(*Clause: a list of literals separated by the disjunction sign*)
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   114
fun parse_clause pool =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   115
  $$ "(" |-- parse_literals pool --| $$ ")" || Scan.single (parse_literal pool)
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   116
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   117
fun ints_of_stree (SInt n) = cons n
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   118
  | ints_of_stree (SBranch (_, ts)) = fold ints_of_stree ts
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   119
val parse_tstp_annotations =
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   120
  Scan.optional ($$ "," |-- parse_term NONE
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   121
                   --| Scan.option ($$ "," |-- parse_terms NONE)
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   122
                 >> (fn source => ints_of_stree source [])) []
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   123
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   124
(* <cnf_annotated> ::= cnf(<name>, <formula_role>, <cnf_formula> <annotations>).
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   125
   The <name> could be an identifier, but we assume integers. *)
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   126
fun retuple_tstp_line ((name, ts), deps) = (name, ts, deps)
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   127
fun parse_tstp_line pool =
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   128
  (Scan.this_string "cnf" -- $$ "(") |-- parse_integer --| $$ ","
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   129
   --| Symbol.scan_id --| $$ "," -- parse_clause pool -- parse_tstp_annotations
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   130
   --| $$ ")" --| $$ "."
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   131
  >> retuple_tstp_line
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   132
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   133
(**** 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
   134
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   135
(* SPASS returns clause references of the form "x.y". We ignore "y", whose role
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   136
   is not clear anyway. *)
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   137
val parse_dot_name = parse_integer --| $$ "." --| parse_integer
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   138
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   139
val parse_spass_annotations =
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   140
  Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   141
                                         --| 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
   142
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   143
(* It is not clear why some literals are followed by sequences of stars. We
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   144
   ignore them. *)
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   145
fun parse_starred_predicate_term pool =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   146
  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
   147
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   148
fun parse_horn_clause pool =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   149
  Scan.repeat (parse_starred_predicate_term pool) --| $$ "-" --| $$ ">"
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   150
  -- Scan.repeat (parse_starred_predicate_term pool)
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   151
  >> (fn ([], []) => [atom "c_False"]
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   152
       | (clauses1, clauses2) => map negate_stree clauses1 @ clauses2)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   153
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   154
(* Syntax: <name>[0:<inference><annotations>] ||
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   155
           <cnf_formulas> -> <cnf_formulas>. *)
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   156
fun retuple_spass_proof_line ((name, deps), ts) = (name, ts, deps)
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   157
fun parse_spass_proof_line pool =
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   158
  parse_integer --| $$ "[" --| $$ "0" --| $$ ":" --| Symbol.scan_id
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   159
  -- parse_spass_annotations --| $$ "]" --| $$ "|" --| $$ "|"
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   160
  -- parse_horn_clause pool --| $$ "."
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   161
  >> retuple_spass_proof_line
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   162
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   163
fun parse_proof_line pool = 
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   164
  fst o (parse_tstp_line pool || parse_spass_proof_line pool)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   165
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   166
(**** INTERPRETATION OF TSTP SYNTAX TREES ****)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   167
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   168
exception STREE of stree;
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   169
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   170
(*If string s has the prefix s1, return the result of deleting it.*)
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   171
fun strip_prefix s1 s =
31038
immler@in.tum.de
parents: 31037
diff changeset
   172
  if String.isPrefix s1 s
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   173
  then SOME (undo_ascii_of (String.extract (s, size s1, NONE)))
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   174
  else NONE;
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   175
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   176
(*Invert the table of translations between Isabelle and ATPs*)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   177
val type_const_trans_table_inv =
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   178
      Symtab.make (map swap (Symtab.dest type_const_trans_table));
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   179
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   180
fun invert_type_const c =
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   181
    case Symtab.lookup type_const_trans_table_inv c of
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   182
        SOME c' => c'
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   183
      | NONE => c;
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   184
36285
d868b34d604c Isar proof reconstruction: type variables introduced by the ATP should be treated as type parameters for type inference purposes;
blanchet
parents: 36283
diff changeset
   185
fun make_tvar s = TVar (("'" ^ s, 0), HOLogic.typeS);
d868b34d604c Isar proof reconstruction: type variables introduced by the ATP should be treated as type parameters for type inference purposes;
blanchet
parents: 36283
diff changeset
   186
fun make_tparam s = TypeInfer.param 0 (s, HOLogic.typeS)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   187
fun make_var (b,T) = Var((b,0),T);
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   188
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   189
(*Type variables are given the basic sort, HOL.type. Some will later be constrained
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   190
  by information from type literals, or by type inference.*)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   191
fun type_of_stree t =
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   192
  case t of
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   193
      SInt _ => raise STREE t
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   194
    | SBranch (a,ts) =>
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   195
        let val Ts = map type_of_stree ts
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   196
        in
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   197
          case strip_prefix tconst_prefix a of
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   198
              SOME b => Type(invert_type_const b, Ts)
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   199
            | NONE =>
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   200
                if not (null ts) then raise STREE t  (*only tconsts have type arguments*)
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   201
                else
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   202
                case strip_prefix tfree_prefix a of
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   203
                    SOME b => TFree("'" ^ b, HOLogic.typeS)
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   204
                  | NONE =>
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   205
                case strip_prefix tvar_prefix a of
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   206
                    SOME b => make_tvar b
36285
d868b34d604c Isar proof reconstruction: type variables introduced by the ATP should be treated as type parameters for type inference purposes;
blanchet
parents: 36283
diff changeset
   207
                  | NONE => make_tparam a  (* Variable from the ATP, say "X1" *)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   208
        end;
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   209
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   210
(*Invert the table of translations between Isabelle and ATPs*)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   211
val const_trans_table_inv =
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   212
      Symtab.update ("fequal", "op =")
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   213
        (Symtab.make (map swap (Symtab.dest const_trans_table)));
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   214
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   215
fun invert_const c =
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   216
    case Symtab.lookup const_trans_table_inv c of
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   217
        SOME c' => c'
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   218
      | NONE => c;
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   219
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   220
(*The number of type arguments of a constant, zero if it's monomorphic*)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   221
fun num_typargs thy s = length (Sign.const_typargs thy (s, Sign.the_const_type thy s));
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   222
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   223
(*Generates a constant, given its type arguments*)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   224
fun const_of thy (a,Ts) = Const(a, Sign.const_instance thy (a,Ts));
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   225
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   226
(*First-order translation. No types are known for variables. HOLogic.typeT should allow
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   227
  them to be inferred.*)
22428
1755e6381b2c First stab at reconstructing HO problems
paulson
parents: 22372
diff changeset
   228
fun term_of_stree args thy t =
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   229
  case t of
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   230
      SInt _ => raise STREE t
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   231
    | SBranch ("hBOOL", [t]) => term_of_stree [] thy t  (*ignore hBOOL*)
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   232
    | SBranch ("hAPP", [t, u]) => term_of_stree (u::args) thy t
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   233
    | SBranch (a, ts) =>
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   234
        case strip_prefix const_prefix a of
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   235
            SOME "equal" =>
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   236
              list_comb(Const (@{const_name "op ="}, HOLogic.typeT), List.map (term_of_stree [] thy) ts)
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   237
          | SOME b =>
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   238
              let val c = invert_const b
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   239
                  val nterms = length ts - num_typargs thy c
22428
1755e6381b2c First stab at reconstructing HO problems
paulson
parents: 22372
diff changeset
   240
                  val us = List.map (term_of_stree [] thy) (List.take(ts,nterms) @ args)
1755e6381b2c First stab at reconstructing HO problems
paulson
parents: 22372
diff changeset
   241
                  (*Extra args from hAPP come AFTER any arguments given directly to the
1755e6381b2c First stab at reconstructing HO problems
paulson
parents: 22372
diff changeset
   242
                    constant.*)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   243
                  val Ts = List.map type_of_stree (List.drop(ts,nterms))
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   244
              in  list_comb(const_of thy (c, Ts), us)  end
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   245
          | NONE => (*a variable, not a constant*)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   246
              let val T = HOLogic.typeT
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   247
                  val opr = (*a Free variable is typically a Skolem function*)
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   248
                    case strip_prefix fixed_var_prefix a of
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   249
                        SOME b => Free(b,T)
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   250
                      | NONE =>
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   251
                    case strip_prefix schematic_var_prefix a of
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   252
                        SOME b => make_var (b,T)
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   253
                      | NONE => make_var (a,T)  (* Variable from the ATP, say "X1" *)
23519
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   254
              in  list_comb (opr, List.map (term_of_stree [] thy) (ts@args))  end;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   255
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   256
(* Type class literal applied to a type. Returns triple of polarity, class,
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   257
   type. *)
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   258
fun constraint_of_stree pol (SBranch ("c_Not", [t])) =
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   259
    constraint_of_stree (not pol) t
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   260
  | constraint_of_stree pol t = case t of
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   261
        SInt _ => raise STREE t
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   262
      | SBranch (a, ts) =>
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   263
            (case (strip_prefix class_prefix a, map type_of_stree ts) of
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   264
                 (SOME b, [T]) => (pol, b, T)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   265
               | _ => raise STREE t);
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   266
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   267
(** Accumulate type constraints in a clause: negative type literals **)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   268
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   269
fun addix (key,z)  = Vartab.map_default (key,[]) (cons z);
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   270
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   271
fun add_constraint ((false, cl, TFree(a,_)), vt) = addix ((a,~1),cl) vt
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   272
  | add_constraint ((false, cl, TVar(ix,_)), vt) = addix (ix,cl) vt
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   273
  | add_constraint (_, vt) = vt;
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   274
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   275
(* Final treatment of the list of "real" literals from a clause. *)
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   276
fun finish [] =
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   277
    (* No "real" literals means only type information. *)
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   278
    HOLogic.true_const
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   279
  | finish lits =
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   280
    case filter_out (curry (op =) HOLogic.false_const) lits of
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   281
      [] => HOLogic.false_const
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
   282
    | xs => foldr1 HOLogic.mk_disj (rev xs);
22491
535fbed859da Numerous bug fixes. Type clauses distinguished from empty clauses. Working proof reduction.
paulson
parents: 22470
diff changeset
   283
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   284
(*Accumulate sort constraints in vt, with "real" literals in lits.*)
32994
ccc07fbbfefd removed some unreferenced material;
wenzelm
parents: 32966
diff changeset
   285
fun lits_of_strees _ (vt, lits) [] = (vt, finish lits)
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   286
  | lits_of_strees ctxt (vt, lits) (t::ts) =
22012
adf68479ae1b Proof.context now sent to watcher and used in type inference step of proof reconstruction
paulson
parents: 21999
diff changeset
   287
      lits_of_strees ctxt (add_constraint (constraint_of_stree true t, vt), lits) ts
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   288
      handle STREE _ =>
22428
1755e6381b2c First stab at reconstructing HO problems
paulson
parents: 22372
diff changeset
   289
      lits_of_strees ctxt (vt, term_of_stree [] (ProofContext.theory_of ctxt) t :: lits) ts;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   290
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   291
(*Update TVars/TFrees with detected sort constraints.*)
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   292
fun repair_sorts vt =
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   293
  let fun tysubst (Type (a, Ts)) = Type (a, map tysubst Ts)
33035
15eab423e573 standardized basic operations on type option;
wenzelm
parents: 32994
diff changeset
   294
        | tysubst (TVar (xi, s)) = TVar (xi, the_default s (Vartab.lookup vt xi))
15eab423e573 standardized basic operations on type option;
wenzelm
parents: 32994
diff changeset
   295
        | tysubst (TFree (x, s)) = TFree (x, the_default s (Vartab.lookup vt (x, ~1)))
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   296
      fun tmsubst (Const (a, T)) = Const (a, tysubst T)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   297
        | tmsubst (Free (a, T)) = Free (a, tysubst T)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   298
        | tmsubst (Var (xi, T)) = Var (xi, tysubst T)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   299
        | tmsubst (t as Bound _) = t
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   300
        | tmsubst (Abs (a, T, t)) = Abs (a, tysubst T, tmsubst t)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   301
        | tmsubst (t $ u) = tmsubst t $ tmsubst u;
36285
d868b34d604c Isar proof reconstruction: type variables introduced by the ATP should be treated as type parameters for type inference purposes;
blanchet
parents: 36283
diff changeset
   302
  in not (Vartab.is_empty vt) ? tmsubst end;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   303
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   304
(*Interpret a list of syntax trees as a clause, given by "real" literals and sort constraints.
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   305
  vt0 holds the initial sort constraints, from the conjecture clauses.*)
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   306
fun clause_of_strees ctxt vt ts =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   307
  let val (vt, dt) = lits_of_strees ctxt (vt, []) ts in
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   308
    dt |> repair_sorts vt |> TypeInfer.constrain HOLogic.boolT
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   309
       |> Syntax.check_term ctxt
36285
d868b34d604c Isar proof reconstruction: type variables introduced by the ATP should be treated as type parameters for type inference purposes;
blanchet
parents: 36283
diff changeset
   310
  end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   311
29268
6aefc5ff8e63 use exists_subterm directly;
wenzelm
parents: 28572
diff changeset
   312
fun gen_all_vars t = fold_rev Logic.all (OldTerm.term_vars t) t;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   313
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   314
fun decode_proof_step vt0 (name, ts, deps) ctxt =
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   315
  let val cl = clause_of_strees ctxt vt0 ts in
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   316
    ((name, cl, deps), fold Variable.declare_term (OldTerm.term_frees cl) ctxt)
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   317
  end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   318
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   319
(** Global sort constraints on TFrees (from tfree_tcs) are positive unit clauses. **)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   320
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   321
fun add_tfree_constraint ((true, cl, TFree(a,_)), vt) = addix ((a,~1),cl) vt
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   322
  | add_tfree_constraint (_, vt) = vt;
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   323
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   324
fun tfree_constraints_of_clauses vt [] = vt
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   325
  | tfree_constraints_of_clauses vt ([lit]::tss) =
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   326
      (tfree_constraints_of_clauses (add_tfree_constraint (constraint_of_stree true lit, vt)) tss
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   327
       handle STREE _ => (*not a positive type constraint: ignore*)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   328
       tfree_constraints_of_clauses vt tss)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   329
  | tfree_constraints_of_clauses vt (_::tss) = tfree_constraints_of_clauses vt tss;
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   330
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   331
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   332
(**** Translation of TSTP files to Isar Proofs ****)
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   333
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   334
fun decode_proof_steps ctxt tuples =
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   335
  let val vt0 = tfree_constraints_of_clauses Vartab.empty (map #2 tuples) in
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   336
    #1 (fold_map (decode_proof_step vt0) tuples ctxt)
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   337
  end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   338
23519
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   339
(** Finding a matching assumption. The literals may be permuted, and variable names
36293
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   340
    may disagree. We must try all combinations of literals (quadratic!) and
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   341
    match the variable names consistently. **)
23519
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   342
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   343
fun strip_alls_aux n (Const(@{const_name all}, _)$Abs(a,T,t))  =
23519
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   344
      strip_alls_aux (n+1) (subst_bound (Var ((a,n), T), t))
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   345
  | strip_alls_aux _ t  =  t;
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   346
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   347
val strip_alls = strip_alls_aux 0;
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   348
36293
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   349
exception MATCH_LITERAL of unit
22012
adf68479ae1b Proof.context now sent to watcher and used in type inference step of proof reconstruction
paulson
parents: 21999
diff changeset
   350
36293
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   351
(* Remark 1: Ignore types. They are not to be trusted.
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   352
   Remark 2: Ignore order of arguments for equality. SPASS sometimes swaps
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   353
   them for no apparent reason. *)
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   354
fun match_literal (Const (@{const_name "op ="}, _) $ t1 $ u1)
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   355
                  (Const (@{const_name "op ="}, _) $ t2 $ u2) env =
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   356
    (env |> match_literal t1 t2 |> match_literal u1 u2
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   357
     handle MATCH_LITERAL () =>
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   358
            env |> match_literal t1 u2 |> match_literal u1 t2)
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   359
  | match_literal (t1 $ u1) (t2 $ u2) env =
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   360
    env |> match_literal t1 t2 |> match_literal u1 u2
31038
immler@in.tum.de
parents: 31037
diff changeset
   361
  | match_literal (Abs (_,_,t1)) (Abs (_,_,t2)) env =
36293
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   362
    match_literal t1 t2 env
31038
immler@in.tum.de
parents: 31037
diff changeset
   363
  | match_literal (Bound i1) (Bound i2) env =
36293
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   364
    if i1=i2 then env else raise MATCH_LITERAL ()
31038
immler@in.tum.de
parents: 31037
diff changeset
   365
  | match_literal (Const(a1,_)) (Const(a2,_)) env =
36293
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   366
    if a1=a2 then env else raise MATCH_LITERAL ()
31038
immler@in.tum.de
parents: 31037
diff changeset
   367
  | match_literal (Free(a1,_)) (Free(a2,_)) env =
36293
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   368
    if a1=a2 then env else raise MATCH_LITERAL ()
23519
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   369
  | match_literal (Var(ix1,_)) (Var(ix2,_)) env = insert (op =) (ix1,ix2) env
36293
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   370
  | match_literal _ _ _ = raise MATCH_LITERAL ()
23519
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   371
36293
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   372
(* Checking that all variable associations are unique. The list "env" contains
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   373
   no repetitions, but does it contain say (x, y) and (y, y)? *)
31038
immler@in.tum.de
parents: 31037
diff changeset
   374
fun good env =
23519
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   375
  let val (xs,ys) = ListPair.unzip env
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   376
  in  not (has_duplicates (op=) xs orelse has_duplicates (op=) ys)  end;
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   377
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   378
(*Match one list of literals against another, ignoring types and the order of
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   379
  literals. Sorting is unreliable because we don't have types or variable names.*)
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   380
fun matches_aux _ [] [] = true
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   381
  | matches_aux env (lit::lits) ts =
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   382
      let fun match1 us [] = false
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   383
            | match1 us (t::ts) =
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   384
                let val env' = match_literal lit t env
31038
immler@in.tum.de
parents: 31037
diff changeset
   385
                in  (good env' andalso matches_aux env' lits (us@ts)) orelse
immler@in.tum.de
parents: 31037
diff changeset
   386
                    match1 (t::us) ts
23519
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   387
                end
36293
e6db3ba0b61d handle SPASS's equality predicate correctly in Isar proof reconstruction
blanchet
parents: 36291
diff changeset
   388
                handle MATCH_LITERAL () => match1 (t::us) ts
31038
immler@in.tum.de
parents: 31037
diff changeset
   389
      in  match1 [] ts  end;
23519
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   390
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   391
(*Is this length test useful?*)
31038
immler@in.tum.de
parents: 31037
diff changeset
   392
fun matches (lits1,lits2) =
immler@in.tum.de
parents: 31037
diff changeset
   393
  length lits1 = length lits2  andalso
23519
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   394
  matches_aux [] (map Envir.eta_contract lits1) (map Envir.eta_contract lits2);
21999
0cf192e489e2 improvements to proof reconstruction. Some files loaded in a different order
paulson
parents: 21979
diff changeset
   395
0cf192e489e2 improvements to proof reconstruction. Some files loaded in a different order
paulson
parents: 21979
diff changeset
   396
fun permuted_clause t =
24958
ff15f76741bd rationalized redundant code
paulson
parents: 24956
diff changeset
   397
  let val lits = HOLogic.disjuncts t
21999
0cf192e489e2 improvements to proof reconstruction. Some files loaded in a different order
paulson
parents: 21979
diff changeset
   398
      fun perm [] = NONE
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   399
        | perm (ctm::ctms) =
24958
ff15f76741bd rationalized redundant code
paulson
parents: 24956
diff changeset
   400
            if matches (lits, HOLogic.disjuncts (HOLogic.dest_Trueprop (strip_alls ctm)))
23519
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   401
            then SOME ctm else perm ctms
21999
0cf192e489e2 improvements to proof reconstruction. Some files loaded in a different order
paulson
parents: 21979
diff changeset
   402
  in perm end;
0cf192e489e2 improvements to proof reconstruction. Some files loaded in a different order
paulson
parents: 21979
diff changeset
   403
0cf192e489e2 improvements to proof reconstruction. Some files loaded in a different order
paulson
parents: 21979
diff changeset
   404
(*ctms is a list of conjecture clauses as yielded by Isabelle. Those returned by the
0cf192e489e2 improvements to proof reconstruction. Some files loaded in a different order
paulson
parents: 21979
diff changeset
   405
  ATP may have their literals reordered.*)
36064
48aec67c284f added "modulus" and "sorts" options to control Sledgehammer's Isar proof output
blanchet
parents: 36063
diff changeset
   406
fun isar_proof_body ctxt sorts ctms =
35869
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   407
  let
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   408
    val _ = trace_proof_msg (K "\n\nisar_proof_body: start\n")
36064
48aec67c284f added "modulus" and "sorts" options to control Sledgehammer's Isar proof output
blanchet
parents: 36063
diff changeset
   409
    val string_of_term = 
48aec67c284f added "modulus" and "sorts" options to control Sledgehammer's Isar proof output
blanchet
parents: 36063
diff changeset
   410
      PrintMode.setmp (filter (curry (op =) Symbol.xsymbolsN)
48aec67c284f added "modulus" and "sorts" options to control Sledgehammer's Isar proof output
blanchet
parents: 36063
diff changeset
   411
                              (print_mode_value ()))
48aec67c284f added "modulus" and "sorts" options to control Sledgehammer's Isar proof output
blanchet
parents: 36063
diff changeset
   412
                      (Syntax.string_of_term ctxt)
35966
f8c738abaed8 honor the newly introduced Sledgehammer parameters and fixed the parsing;
blanchet
parents: 35963
diff changeset
   413
    fun have_or_show "show" _ = "  show \""
f8c738abaed8 honor the newly introduced Sledgehammer parameters and fixed the parsing;
blanchet
parents: 35963
diff changeset
   414
      | have_or_show have lname = "  " ^ have ^ " " ^ lname ^ ": \""
35869
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   415
    fun do_line _ (lname, t, []) =
36285
d868b34d604c Isar proof reconstruction: type variables introduced by the ATP should be treated as type parameters for type inference purposes;
blanchet
parents: 36283
diff changeset
   416
       (* No depedencies: it's a conjecture clause, with no proof. *)
35869
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   417
       (case permuted_clause t ctms of
35966
f8c738abaed8 honor the newly introduced Sledgehammer parameters and fixed the parsing;
blanchet
parents: 35963
diff changeset
   418
          SOME u => "  assume " ^ lname ^ ": \"" ^ string_of_term u ^ "\"\n"
35869
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   419
        | NONE => raise TERM ("Sledgehammer_Proof_Reconstruct.isar_proof_body",
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   420
                              [t]))
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   421
      | do_line have (lname, t, deps) =
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   422
        have_or_show have lname ^
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   423
        string_of_term (gen_all_vars (HOLogic.mk_Trueprop t)) ^
35966
f8c738abaed8 honor the newly introduced Sledgehammer parameters and fixed the parsing;
blanchet
parents: 35963
diff changeset
   424
        "\"\n    by (metis " ^ space_implode " " deps ^ ")\n"
35869
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   425
    fun do_lines [(lname, t, deps)] = [do_line "show" (lname, t, deps)]
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   426
      | do_lines ((lname, t, deps) :: lines) =
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   427
        do_line "have" (lname, t, deps) :: do_lines lines
36064
48aec67c284f added "modulus" and "sorts" options to control Sledgehammer's Isar proof output
blanchet
parents: 36063
diff changeset
   428
  in setmp_CRITICAL show_sorts sorts do_lines end;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   429
35869
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   430
fun unequal t (_, t', _) = not (t aconv t');
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   431
22491
535fbed859da Numerous bug fixes. Type clauses distinguished from empty clauses. Working proof reduction.
paulson
parents: 22470
diff changeset
   432
(*No "real" literals means only type information*)
23519
a4ffa756d8eb bug fixes to proof reconstruction
paulson
parents: 23139
diff changeset
   433
fun eq_types t = t aconv HOLogic.true_const;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   434
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   435
fun replace_dep (old, new) dep = if dep = old then new else [dep]
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   436
fun replace_deps p (num, t, deps) =
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   437
  (num, t, fold (union (op =) o replace_dep p) deps [])
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   438
22491
535fbed859da Numerous bug fixes. Type clauses distinguished from empty clauses. Working proof reduction.
paulson
parents: 22470
diff changeset
   439
(*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
   440
  only in type information.*)
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   441
fun add_proof_line thm_names (num, t, []) lines =
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   442
      (* No dependencies: axiom or conjecture clause *)
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   443
      if is_axiom_clause_number thm_names num then
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   444
        (* Axioms are not proof lines *)
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   445
        if eq_types t then
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   446
          (* Must be clsrel/clsarity: type information, so delete refs to it *)
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   447
          map (replace_deps (num, [])) lines
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   448
        else
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   449
          (case take_prefix (unequal t) lines of
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   450
             (_,[]) => lines                  (*no repetition of proof line*)
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   451
           | (pre, (num', _, _) :: post) =>   (*repetition: replace later line by earlier one*)
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   452
               pre @ map (replace_deps (num', [num])) post)
22470
0d52e5157124 No label on "show"; tries to remove dependencies more cleanly
paulson
parents: 22428
diff changeset
   453
      else
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   454
        (num, t, []) :: lines
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   455
  | add_proof_line _ (num, t, deps) lines =
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   456
      if eq_types t then (num, t, deps) :: lines
22491
535fbed859da Numerous bug fixes. Type clauses distinguished from empty clauses. Working proof reduction.
paulson
parents: 22470
diff changeset
   457
      (*Type information will be deleted later; skip repetition test.*)
535fbed859da Numerous bug fixes. Type clauses distinguished from empty clauses. Working proof reduction.
paulson
parents: 22470
diff changeset
   458
      else (*FIXME: Doesn't this code risk conflating proofs involving different types??*)
35869
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   459
      case take_prefix (unequal t) lines of
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   460
         (_,[]) => (num, t, deps) :: lines  (*no repetition of proof line*)
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   461
       | (pre, (num', t', _) :: post) =>
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   462
           (num, t', deps) ::               (*repetition: replace later line by earlier one*)
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   463
           (pre @ map (replace_deps (num', [num])) post);
22044
6c0702a96076 More compact proof reconstruction: lines having fewer than !min_deps dependences are folded
paulson
parents: 22012
diff changeset
   464
22470
0d52e5157124 No label on "show"; tries to remove dependencies more cleanly
paulson
parents: 22428
diff changeset
   465
(*Recursively delete empty lines (type information) from the proof.*)
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   466
fun add_nonnull_prfline ((num, t, []), lines) = (*no dependencies, so a conjecture clause*)
22491
535fbed859da Numerous bug fixes. Type clauses distinguished from empty clauses. Working proof reduction.
paulson
parents: 22470
diff changeset
   467
     if eq_types t (*must be type information, tfree_tcs, clsrel, clsarity: delete refs to it*)
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   468
     then delete_dep num lines
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   469
     else (num, t, []) :: lines
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   470
  | add_nonnull_prfline ((num, t, deps), lines) = (num, t, deps) :: lines
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   471
and delete_dep num lines =
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   472
  List.foldr add_nonnull_prfline [] (map (replace_deps (num, [])) lines);
22470
0d52e5157124 No label on "show"; tries to remove dependencies more cleanly
paulson
parents: 22428
diff changeset
   473
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   474
fun bad_free (Free (a,_)) = String.isPrefix skolem_prefix a
22731
abfdccaed085 trying to make single-step proofs work better, especially if they contain
paulson
parents: 22728
diff changeset
   475
  | bad_free _ = false;
abfdccaed085 trying to make single-step proofs work better, especially if they contain
paulson
parents: 22728
diff changeset
   476
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 23083
diff changeset
   477
(*TVars are forbidden in goals. Also, we don't want lines with <2 dependencies.
22491
535fbed859da Numerous bug fixes. Type clauses distinguished from empty clauses. Working proof reduction.
paulson
parents: 22470
diff changeset
   478
  To further compress proofs, setting modulus:=n deletes every nth line, and nlines
535fbed859da Numerous bug fixes. Type clauses distinguished from empty clauses. Working proof reduction.
paulson
parents: 22470
diff changeset
   479
  counts the number of proof lines processed so far.
535fbed859da Numerous bug fixes. Type clauses distinguished from empty clauses. Working proof reduction.
paulson
parents: 22470
diff changeset
   480
  Deleted lines are replaced by their own dependencies. Note that the "add_nonnull_prfline"
22044
6c0702a96076 More compact proof reconstruction: lines having fewer than !min_deps dependences are folded
paulson
parents: 22012
diff changeset
   481
  phase may delete some dependencies, hence this phase comes later.*)
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   482
fun add_wanted_prfline ctxt _ ((num, t, []), (nlines, lines)) =
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   483
      (nlines, (num, t, []) :: lines)   (*conjecture clauses must be kept*)
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   484
  | add_wanted_prfline ctxt modulus ((num, t, deps), (nlines, lines)) =
29272
fb3ccf499df5 use regular Term.add_XXX etc.;
wenzelm
parents: 29268
diff changeset
   485
      if eq_types t orelse not (null (Term.add_tvars t [])) orelse
29268
6aefc5ff8e63 use exists_subterm directly;
wenzelm
parents: 28572
diff changeset
   486
         exists_subterm bad_free t orelse
24937
340523598914 context-based treatment of generalization; also handling TFrees in axiom clauses
paulson
parents: 24920
diff changeset
   487
         (not (null lines) andalso   (*final line can't be deleted for these reasons*)
36064
48aec67c284f added "modulus" and "sorts" options to control Sledgehammer's Isar proof output
blanchet
parents: 36063
diff changeset
   488
          (length deps < 2 orelse nlines mod modulus <> 0))
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   489
      then (nlines+1, map (replace_deps (num, deps)) lines) (*Delete line*)
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   490
      else (nlines+1, (num, t, deps) :: lines);
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   491
21999
0cf192e489e2 improvements to proof reconstruction. Some files loaded in a different order
paulson
parents: 21979
diff changeset
   492
(*Replace numeric proof lines by strings, either from thm_names or sequential line numbers*)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   493
fun stringify_deps thm_names deps_map [] = []
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   494
  | stringify_deps thm_names deps_map ((num, t, deps) :: lines) =
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   495
    if is_axiom_clause_number thm_names num then
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   496
      (Vector.sub (thm_names, num - 1), t, []) ::
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   497
      stringify_deps thm_names deps_map lines
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   498
    else
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   499
      let
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   500
        val lname = Int.toString (length deps_map)
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   501
        fun fix num = if is_axiom_clause_number thm_names num
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   502
                      then SOME(Vector.sub(thm_names,num-1))
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   503
                      else AList.lookup (op =) deps_map num;
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   504
      in
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   505
        (lname, t, map_filter fix (distinct (op=) deps)) ::
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   506
        stringify_deps thm_names ((num, lname) :: deps_map) lines
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   507
      end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   508
36063
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   509
fun isar_proof_start i =
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   510
  (if i = 1 then "" else "prefer " ^ string_of_int i ^ "\n") ^
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   511
  "proof (neg_clausify)\n";
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   512
fun isar_fixes [] = ""
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   513
  | isar_fixes ts = "  fix " ^ space_implode " " ts ^ "\n";
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   514
fun isar_proof_end 1 = "qed"
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   515
  | isar_proof_end _ = "next"
21979
80e10f181c46 Improvements to proof reconstruction. Now "fixes" is inserted
paulson
parents: 21978
diff changeset
   516
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   517
fun isar_proof_from_atp_proof pool modulus sorts ctxt cnfs thm_names goal i =
35868
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   518
  let
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   519
    val _ = trace_proof_msg (K "\nisar_proof_from_atp_proof: start\n")
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   520
    val tuples = map (parse_proof_line pool o explode) cnfs
35868
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   521
    val _ = trace_proof_msg (fn () =>
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   522
      Int.toString (length tuples) ^ " tuples extracted\n")
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   523
    val ctxt = ProofContext.set_mode ProofContext.mode_schematic ctxt
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   524
    val raw_lines =
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   525
      fold_rev (add_proof_line thm_names) (decode_proof_steps ctxt tuples) []
35868
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   526
    val _ = trace_proof_msg (fn () =>
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   527
      Int.toString (length raw_lines) ^ " raw_lines extracted\n")
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   528
    val nonnull_lines = List.foldr add_nonnull_prfline [] raw_lines
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   529
    val _ = trace_proof_msg (fn () =>
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   530
      Int.toString (length nonnull_lines) ^ " nonnull_lines extracted\n")
36064
48aec67c284f added "modulus" and "sorts" options to control Sledgehammer's Isar proof output
blanchet
parents: 36063
diff changeset
   531
    val (_, lines) = List.foldr (add_wanted_prfline ctxt modulus) (0,[]) nonnull_lines
35868
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   532
    val _ = trace_proof_msg (fn () =>
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   533
      Int.toString (length lines) ^ " lines extracted\n")
36063
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   534
    val (ccls, fixes) = neg_conjecture_clauses ctxt goal i
35868
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   535
    val _ = trace_proof_msg (fn () =>
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   536
      Int.toString (length ccls) ^ " conjecture clauses\n")
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   537
    val ccls = map forall_intr_vars ccls
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   538
    val _ = app (fn th => trace_proof_msg
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   539
                              (fn () => "\nccl: " ^ string_of_thm ctxt th)) ccls
36064
48aec67c284f added "modulus" and "sorts" options to control Sledgehammer's Isar proof output
blanchet
parents: 36063
diff changeset
   540
    val body = isar_proof_body ctxt sorts (map prop_of ccls)
35869
cac366550624 start work on direct proof reconstruction for Sledgehammer
blanchet
parents: 35868
diff changeset
   541
                               (stringify_deps thm_names [] lines)
36063
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   542
    val n = Logic.count_prems (prop_of goal)
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   543
    val _ = trace_proof_msg (K "\nisar_proof_from_atp_proof: finishing\n")
36063
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   544
  in
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   545
    isar_proof_start i ^ isar_fixes (map #1 fixes) ^ implode body ^
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   546
    isar_proof_end n ^ "\n"
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   547
  end
36288
156e4f179bb0 minor code cleanup
blanchet
parents: 36287
diff changeset
   548
  handle STREE _ => raise Fail "Cannot parse ATP output";
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   549
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   550
33310
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
   551
(* === EXTRACTING LEMMAS === *)
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
   552
(* A list consisting of the first number in each line is returned.
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   553
   TSTP: Interesting lines have the form "cnf(108, axiom, ...)", where the
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
   554
   number (108) is extracted.
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   555
   SPASS: Lines have the form "108[0:Inp] ...", where the first number (108) is
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
   556
   extracted. *)
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   557
fun extract_clause_numbers_in_proof proof =
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   558
  let
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   559
    val tokens_of = String.tokens (not o is_ident_char)
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   560
    fun extract_num ("cnf" :: num :: _ :: _) = Int.fromString num
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   561
      | extract_num (num :: "0" :: "Inp" :: _) = Int.fromString num
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   562
      | extract_num _ = NONE
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   563
  in proof |> split_lines |> map_filter (extract_num o tokens_of) end
33310
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
   564
  
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   565
(* Used to label theorems chained into the Sledgehammer call (or rather
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   566
   goal?) *)
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   567
val chained_hint = "sledgehammer_chained"
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   568
36063
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   569
fun apply_command _ 1 = "by "
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   570
  | apply_command 1 _ = "apply "
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   571
  | apply_command i _ = "prefer " ^ string_of_int i ^ " apply "
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   572
fun metis_command i n [] =
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   573
    apply_command i n ^ "metis"
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   574
  | metis_command i n xs =
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   575
    apply_command i n ^ "(metis " ^ space_implode " " xs ^ ")"
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   576
fun metis_line i n xs =
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   577
  "Try this command: " ^
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   578
  Markup.markup Markup.sendback (metis_command i n xs) ^ ".\n" 
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
   579
fun minimize_line _ [] = ""
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
   580
  | minimize_line minimize_command facts =
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
   581
    case minimize_command facts of
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
   582
      "" => ""
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
   583
    | command =>
36065
3fc077c4780a fixed layout of Sledgehammer output
blanchet
parents: 36064
diff changeset
   584
      "To minimize the number of lemmas, try this command: " ^
36281
dbbf4d5d584d pass relevant options from "sledgehammer" to "sledgehammer minimize";
blanchet
parents: 36231
diff changeset
   585
      Markup.markup Markup.sendback command ^ ".\n"
31840
beeaa1ed1f47 check if conjectures have been used in proof
immler@in.tum.de
parents: 31479
diff changeset
   586
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
   587
fun metis_proof_text (minimize_command, proof, thm_names, goal, i) =
36063
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   588
  let
36231
bede2d49ba3b get rid of "conjecture_pos", which is no longer necessary now that it's Metis's job, not Sledgehammer's, to report inconsistent contexts
blanchet
parents: 36225
diff changeset
   589
    val lemmas =
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   590
      proof |> extract_clause_numbers_in_proof
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   591
            |> filter (is_axiom_clause_number thm_names)
36231
bede2d49ba3b get rid of "conjecture_pos", which is no longer necessary now that it's Metis's job, not Sledgehammer's, to report inconsistent contexts
blanchet
parents: 36225
diff changeset
   592
            |> map (fn i => Vector.sub (thm_names, i - 1))
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   593
            |> filter_out (fn s => s = "??.unknown" orelse s = chained_hint)
36231
bede2d49ba3b get rid of "conjecture_pos", which is no longer necessary now that it's Metis's job, not Sledgehammer's, to report inconsistent contexts
blanchet
parents: 36225
diff changeset
   594
            |> sort_distinct string_ord
36063
cdc6855a6387 make Sledgehammer output "by" vs. "apply", "qed" vs. "next", and any necessary "prefer"
blanchet
parents: 36001
diff changeset
   595
    val n = Logic.count_prems (prop_of goal)
36395
e73923451f6f cosmetics
blanchet
parents: 36393
diff changeset
   596
  in (metis_line i n lemmas ^ minimize_line minimize_command lemmas, lemmas) end
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents: 30874
diff changeset
   597
36291
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   598
val is_proof_line = String.isPrefix "cnf(" orf String.isSubstring "||"
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   599
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   600
fun do_space c = if Char.isSpace c then "" else str c
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   601
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   602
fun strip_spaces_in_list [] = ""
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   603
  | strip_spaces_in_list [c1] = do_space c1
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   604
  | strip_spaces_in_list [c1, c2] = do_space c1 ^ do_space c2
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   605
  | strip_spaces_in_list (c1 :: c2 :: c3 :: cs) =
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   606
    if Char.isSpace c1 then
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   607
      strip_spaces_in_list (c2 :: c3 :: cs)
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   608
    else if Char.isSpace c2 then
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   609
      if Char.isSpace c3 then
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   610
        strip_spaces_in_list (c1 :: c3 :: cs)
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   611
      else
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   612
        str c1 ^
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   613
        (if is_ident_char c1 andalso is_ident_char c3 then " " else "") ^
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   614
        strip_spaces_in_list (c3 :: cs)
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   615
    else
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   616
      str c1 ^ strip_spaces_in_list (c2 :: c3 :: cs)
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   617
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   618
val strip_spaces = strip_spaces_in_list o String.explode
b4c2043cc96c added Isar proof reconstruction support for SPASS -- which means all provers can now yield Isar proofs;
blanchet
parents: 36288
diff changeset
   619
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   620
fun isar_proof_text pool debug modulus sorts ctxt
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
   621
                    (minimize_command, proof, thm_names, goal, i) =
33310
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
   622
  let
36369
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36293
diff changeset
   623
    val cnfs = proof |> split_lines |> map strip_spaces |> filter is_proof_line
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
   624
    val (one_line_proof, lemma_names) =
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
   625
      metis_proof_text (minimize_command, proof, thm_names, goal, i)
35868
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   626
    val tokens = String.tokens (fn c => c = #" ") one_line_proof
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
   627
    fun isar_proof_for () =
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   628
      case isar_proof_from_atp_proof pool modulus sorts ctxt cnfs thm_names goal
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   629
                                     i 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
   630
        "" => ""
36285
d868b34d604c Isar proof reconstruction: type variables introduced by the ATP should be treated as type parameters for type inference purposes;
blanchet
parents: 36283
diff changeset
   631
      | isar_proof =>
d868b34d604c Isar proof reconstruction: type variables introduced by the ATP should be treated as type parameters for type inference purposes;
blanchet
parents: 36283
diff changeset
   632
        "\nStructured proof:\n" ^ Markup.markup Markup.sendback isar_proof
35868
491a97039ce1 renamed "e_full" and "vampire_full" to "e_isar" and "vampire_isar";
blanchet
parents: 35865
diff changeset
   633
    val isar_proof =
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
   634
      if member (op =) tokens chained_hint then
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
   635
        ""
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
   636
      else if debug then
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
   637
        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
   638
      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
   639
        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
   640
        |> 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
   641
  in (one_line_proof ^ isar_proof, lemma_names) end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   642
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   643
fun proof_text isar_proof pool debug modulus sorts ctxt =
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36392
diff changeset
   644
  if isar_proof then isar_proof_text pool debug modulus sorts ctxt
36288
156e4f179bb0 minor code cleanup
blanchet
parents: 36287
diff changeset
   645
  else metis_proof_text
36223
217ca1273786 make Sledgehammer's minimizer also minimize Isar proofs
blanchet
parents: 36140
diff changeset
   646
31038
immler@in.tum.de
parents: 31037
diff changeset
   647
end;