src/HOL/Tools/ATP/atp_proof.ML
author blanchet
Fri, 27 May 2011 10:30:08 +0200
changeset 43870 3e060b1c844b
parent 43846 c96f06bffd90
child 43891 59284a13abc4
permissions -rw-r--r--
use helpers and tweak Quickcheck's priority to it comes second (to give Solve Direct slightly more time before another prover runs)
blanchet@39692
     1
(*  Title:      HOL/Tools/ATP/atp_proof.ML
blanchet@39692
     2
    Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
blanchet@39692
     3
    Author:     Claire Quigley, Cambridge University Computer Laboratory
blanchet@39692
     4
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@39692
     5
blanchet@43745
     6
Abstract representation of ATP proofs and TSTP/SPASS syntax.
blanchet@39692
     7
*)
blanchet@39692
     8
blanchet@39692
     9
signature ATP_PROOF =
blanchet@39692
    10
sig
blanchet@39692
    11
  type 'a fo_term = 'a ATP_Problem.fo_term
blanchet@43402
    12
  type ('a, 'b, 'c) formula = ('a, 'b, 'c) ATP_Problem.formula
blanchet@43784
    13
  type 'a problem = 'a ATP_Problem.problem
blanchet@39692
    14
blanchet@43806
    15
  exception UNRECOGNIZED_ATP_PROOF of unit
blanchet@43806
    16
blanchet@39731
    17
  datatype failure =
blanchet@43458
    18
    Unprovable |
blanchet@43458
    19
    IncompleteUnprovable |
blanchet@43458
    20
    ProofMissing |
blanchet@43751
    21
    ProofIncomplete |
blanchet@43745
    22
    UnsoundProof of bool * string list |
blanchet@43458
    23
    CantConnect |
blanchet@43458
    24
    TimedOut |
blanchet@43794
    25
    Inappropriate |
blanchet@43458
    26
    OutOfResources |
blanchet@43458
    27
    SpassTooOld |
blanchet@43458
    28
    VampireTooOld |
blanchet@43458
    29
    NoPerl |
blanchet@43458
    30
    NoLibwwwPerl |
blanchet@43458
    31
    NoRealZ3 |
blanchet@43458
    32
    MalformedInput |
blanchet@43458
    33
    MalformedOutput |
blanchet@43458
    34
    Interrupted |
blanchet@43458
    35
    Crashed |
blanchet@43458
    36
    InternalError |
blanchet@43458
    37
    UnknownError of string
blanchet@39731
    38
blanchet@39695
    39
  type step_name = string * string option
blanchet@39692
    40
blanchet@39693
    41
  datatype 'a step =
blanchet@39693
    42
    Definition of step_name * 'a * 'a |
blanchet@39693
    43
    Inference of step_name * 'a * step_name list
blanchet@39692
    44
blanchet@43402
    45
  type 'a proof = ('a, 'a, 'a fo_term) formula step list
blanchet@39692
    46
blanchet@43802
    47
  val strip_spaces : bool -> (char -> bool) -> string -> string
blanchet@41505
    48
  val short_output : bool -> string -> string
blanchet@42615
    49
  val string_for_failure : failure -> string
blanchet@39731
    50
  val extract_important_message : string -> string
blanchet@39731
    51
  val extract_known_failure :
blanchet@39731
    52
    (failure * string) list -> string -> failure option
blanchet@39731
    53
  val extract_tstplike_proof_and_outcome :
blanchet@43717
    54
    bool -> bool -> int -> (string * string) list -> (failure * string) list
blanchet@43717
    55
    -> string -> string * failure option
blanchet@43809
    56
  val is_same_atp_step : step_name -> step_name -> bool
blanchet@43802
    57
  val scan_general_id : string list -> string * string list
blanchet@43802
    58
  val parse_formula :
blanchet@43802
    59
    string list -> (string, 'a, string fo_term) formula * string list
blanchet@43784
    60
  val atp_proof_from_tstplike_proof : string problem -> string -> string proof
blanchet@43809
    61
  val clean_up_atp_proof_dependencies : string proof -> string proof
blanchet@39694
    62
  val map_term_names_in_atp_proof :
blanchet@39694
    63
    (string -> string) -> string proof -> string proof
blanchet@39694
    64
  val nasty_atp_proof : string Symtab.table -> string proof -> string proof
blanchet@39692
    65
end;
blanchet@39692
    66
blanchet@39692
    67
structure ATP_Proof : ATP_PROOF =
blanchet@39692
    68
struct
blanchet@39692
    69
blanchet@39731
    70
open ATP_Problem
blanchet@39731
    71
blanchet@43806
    72
exception UNRECOGNIZED_ATP_PROOF of unit
blanchet@43806
    73
blanchet@39731
    74
datatype failure =
blanchet@43458
    75
  Unprovable |
blanchet@43458
    76
  IncompleteUnprovable |
blanchet@43458
    77
  ProofMissing |
blanchet@43751
    78
  ProofIncomplete |
blanchet@43745
    79
  UnsoundProof of bool * string list |
blanchet@43458
    80
  CantConnect |
blanchet@43458
    81
  TimedOut |
blanchet@43794
    82
  Inappropriate |
blanchet@43458
    83
  OutOfResources |
blanchet@43458
    84
  SpassTooOld |
blanchet@43458
    85
  VampireTooOld |
blanchet@43458
    86
  NoPerl |
blanchet@43458
    87
  NoLibwwwPerl |
blanchet@43458
    88
  NoRealZ3 |
blanchet@43458
    89
  MalformedInput |
blanchet@43458
    90
  MalformedOutput |
blanchet@43458
    91
  Interrupted |
blanchet@43458
    92
  Crashed |
blanchet@43458
    93
  InternalError |
blanchet@43458
    94
  UnknownError of string
blanchet@39731
    95
blanchet@43802
    96
fun strip_c_style_comment _ [] = []
blanchet@43802
    97
  | strip_c_style_comment is_evil (#"*" :: #"/" :: cs) =
blanchet@43802
    98
    strip_spaces_in_list true is_evil cs
blanchet@43802
    99
  | strip_c_style_comment is_evil (_ :: cs) = strip_c_style_comment is_evil cs
blanchet@43802
   100
and strip_spaces_in_list _ _ [] = []
blanchet@43802
   101
  | strip_spaces_in_list true is_evil (#"%" :: cs) =
blanchet@43802
   102
    strip_spaces_in_list true is_evil
blanchet@43802
   103
                         (cs |> chop_while (not_equal #"\n") |> snd)
blanchet@43802
   104
  | strip_spaces_in_list true is_evil (#"/" :: #"*" :: cs) =
blanchet@43802
   105
    strip_c_style_comment is_evil cs
blanchet@43802
   106
  | strip_spaces_in_list _ _ [c1] = if Char.isSpace c1 then [] else [str c1]
blanchet@43802
   107
  | strip_spaces_in_list skip_comments is_evil [c1, c2] =
blanchet@43802
   108
    strip_spaces_in_list skip_comments is_evil [c1] @
blanchet@43802
   109
    strip_spaces_in_list skip_comments is_evil [c2]
blanchet@43802
   110
  | strip_spaces_in_list skip_comments is_evil (c1 :: c2 :: c3 :: cs) =
blanchet@39692
   111
    if Char.isSpace c1 then
blanchet@43802
   112
      strip_spaces_in_list skip_comments is_evil (c2 :: c3 :: cs)
blanchet@39692
   113
    else if Char.isSpace c2 then
blanchet@39692
   114
      if Char.isSpace c3 then
blanchet@43802
   115
        strip_spaces_in_list skip_comments is_evil (c1 :: c3 :: cs)
blanchet@39692
   116
      else
blanchet@39692
   117
        str c1 :: (if forall is_evil [c1, c3] then [" "] else []) @
blanchet@43802
   118
        strip_spaces_in_list skip_comments is_evil (c3 :: cs)
blanchet@39692
   119
    else
blanchet@43802
   120
      str c1 :: strip_spaces_in_list skip_comments is_evil (c2 :: c3 :: cs)
blanchet@43802
   121
fun strip_spaces skip_comments is_evil =
blanchet@43802
   122
  implode o strip_spaces_in_list skip_comments is_evil o String.explode
blanchet@39692
   123
blanchet@39692
   124
fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
blanchet@43802
   125
val strip_spaces_except_between_ident_chars = strip_spaces true is_ident_char
blanchet@39692
   126
blanchet@41505
   127
fun elide_string threshold s =
blanchet@41505
   128
  if size s > threshold then
blanchet@41505
   129
    String.extract (s, 0, SOME (threshold div 2 - 5)) ^ " ...... " ^
blanchet@41505
   130
    String.extract (s, size s - (threshold + 1) div 2 + 6, NONE)
blanchet@41505
   131
  else
blanchet@41505
   132
    s
blanchet@41505
   133
fun short_output verbose output =
blanchet@42915
   134
  if verbose then
blanchet@42915
   135
    if output = "" then "No details available" else elide_string 1000 output
blanchet@42915
   136
  else
blanchet@42915
   137
    ""
blanchet@41505
   138
blanchet@42615
   139
val missing_message_tail =
blanchet@42615
   140
  " appears to be missing. You will need to install it if you want to invoke \
blanchet@42615
   141
  \remote provers."
blanchet@39731
   142
blanchet@43745
   143
fun involving [] = ""
blanchet@43846
   144
  | involving ss =
blanchet@43870
   145
    "involving " ^ space_implode " " (Try.serial_commas "and" (map quote ss)) ^
blanchet@43870
   146
    " "
blanchet@43745
   147
blanchet@43809
   148
fun string_for_failure Unprovable = "The problem is unprovable."
blanchet@43809
   149
  | string_for_failure IncompleteUnprovable = "The prover gave up."
blanchet@42615
   150
  | string_for_failure ProofMissing =
blanchet@42615
   151
    "The prover claims the conjecture is a theorem but did not provide a proof."
blanchet@43751
   152
  | string_for_failure ProofIncomplete =
blanchet@43751
   153
    "The prover claims the conjecture is a theorem but provided an incomplete \
blanchet@43751
   154
    \proof."
blanchet@43745
   155
  | string_for_failure (UnsoundProof (false, ss)) =
blanchet@43745
   156
    "The prover found a type-unsound proof " ^ involving ss ^
blanchet@43745
   157
    "(or, less likely, your axioms are inconsistent). Try passing the \
blanchet@43745
   158
    \\"full_types\" option to Sledgehammer to avoid such spurious proofs."
blanchet@43745
   159
  | string_for_failure (UnsoundProof (true, ss)) =
blanchet@43745
   160
    "The prover found a type-unsound proof " ^ involving ss ^
blanchet@43745
   161
    "even though a supposedly type-sound encoding was used (or, less likely, \
blanchet@43745
   162
    \your axioms are inconsistent). You might want to report this to the \
blanchet@43745
   163
    \Isabelle developers."
blanchet@42615
   164
  | string_for_failure CantConnect = "Cannot connect to remote server."
blanchet@42615
   165
  | string_for_failure TimedOut = "Timed out."
blanchet@43794
   166
  | string_for_failure Inappropriate =
blanchet@43794
   167
    "The problem lies outside the prover's scope."
blanchet@42615
   168
  | string_for_failure OutOfResources = "The prover ran out of resources."
blanchet@42615
   169
  | string_for_failure SpassTooOld =
blanchet@39731
   170
    "Isabelle requires a more recent version of SPASS with support for the \
blanchet@39731
   171
    \TPTP syntax. To install it, download and extract the package \
blanchet@39731
   172
    \\"http://isabelle.in.tum.de/dist/contrib/spass-3.7.tar.gz\" and add the \
blanchet@39731
   173
    \\"spass-3.7\" directory's absolute path to " ^
wenzelm@42815
   174
    Path.print (Path.expand (Path.appends
blanchet@39731
   175
               (Path.variable "ISABELLE_HOME_USER" ::
wenzelm@42815
   176
                map Path.basic ["etc", "components"]))) ^
blanchet@39731
   177
    " on a line of its own."
blanchet@42615
   178
  | string_for_failure VampireTooOld =
blanchet@39731
   179
    "Isabelle requires a more recent version of Vampire. To install it, follow \
blanchet@39731
   180
    \the instructions from the Sledgehammer manual (\"isabelle doc\
blanchet@39731
   181
    \ sledgehammer\")."
blanchet@42615
   182
  | string_for_failure NoPerl = "Perl" ^ missing_message_tail
blanchet@42615
   183
  | string_for_failure NoLibwwwPerl =
blanchet@42615
   184
    "The Perl module \"libwww-perl\"" ^ missing_message_tail
blanchet@42615
   185
  | string_for_failure NoRealZ3 =
blanchet@41470
   186
    "The environment variable \"Z3_REAL_SOLVER\" must be set to Z3's full path."
blanchet@42615
   187
  | string_for_failure MalformedInput =
blanchet@42615
   188
    "The generated problem is malformed. Please report this to the Isabelle \
blanchet@42615
   189
    \developers."
blanchet@42615
   190
  | string_for_failure MalformedOutput = "The prover output is malformed."
blanchet@42615
   191
  | string_for_failure Crashed = "The prover crashed."
blanchet@42615
   192
  | string_for_failure InternalError = "An internal prover error occurred."
blanchet@42615
   193
  | string_for_failure (UnknownError string) =
blanchet@42615
   194
    "A prover error occurred" ^
blanchet@41582
   195
    (if string = "" then ". (Pass the \"verbose\" option for details.)"
blanchet@41582
   196
     else ":\n" ^ string)
blanchet@39731
   197
blanchet@39731
   198
fun extract_delimited (begin_delim, end_delim) output =
blanchet@39731
   199
  output |> first_field begin_delim |> the |> snd
blanchet@39731
   200
         |> first_field end_delim |> the |> fst
blanchet@39731
   201
         |> first_field "\n" |> the |> snd
blanchet@39731
   202
  handle Option.Option => ""
blanchet@39731
   203
blanchet@39731
   204
val tstp_important_message_delims =
blanchet@39731
   205
  ("% SZS start RequiredInformation", "% SZS end RequiredInformation")
blanchet@39731
   206
blanchet@39731
   207
fun extract_important_message output =
blanchet@39731
   208
  case extract_delimited tstp_important_message_delims output of
blanchet@39731
   209
    "" => ""
blanchet@39731
   210
  | s => s |> space_explode "\n" |> filter_out (curry (op =) "")
blanchet@39731
   211
           |> map (perhaps (try (unprefix "%")))
blanchet@39731
   212
           |> map (perhaps (try (unprefix " ")))
blanchet@39731
   213
           |> space_implode "\n " |> quote
blanchet@39731
   214
blanchet@39731
   215
(* Splits by the first possible of a list of delimiters. *)
blanchet@39731
   216
fun extract_tstplike_proof delims output =
blanchet@39731
   217
  case pairself (find_first (fn s => String.isSubstring s output))
blanchet@39731
   218
                (ListPair.unzip delims) of
blanchet@39731
   219
    (SOME begin_delim, SOME end_delim) =>
blanchet@39731
   220
    extract_delimited (begin_delim, end_delim) output
blanchet@39731
   221
  | _ => ""
blanchet@39731
   222
blanchet@39731
   223
fun extract_known_failure known_failures output =
blanchet@39731
   224
  known_failures
blanchet@39731
   225
  |> find_first (fn (_, pattern) => String.isSubstring pattern output)
blanchet@39731
   226
  |> Option.map fst
blanchet@39731
   227
blanchet@43717
   228
fun extract_tstplike_proof_and_outcome verbose complete res_code proof_delims
blanchet@43717
   229
                                       known_failures output =
blanchet@43751
   230
  case (extract_tstplike_proof proof_delims output,
blanchet@43751
   231
        extract_known_failure known_failures output) of
blanchet@43751
   232
    (_, SOME ProofIncomplete) => ("", SOME ProofIncomplete)
blanchet@43751
   233
  | ("", SOME failure) =>
blanchet@43751
   234
    ("", SOME (if failure = IncompleteUnprovable andalso complete then Unprovable
blanchet@43751
   235
               else failure))
blanchet@43751
   236
  | ("", NONE) =>
blanchet@43751
   237
    ("", SOME (if res_code = 0 andalso output = "" then ProofMissing
blanchet@43751
   238
               else UnknownError (short_output verbose output)))
blanchet@43751
   239
  | (tstplike_proof, _) => (tstplike_proof, NONE)
blanchet@39692
   240
blanchet@39695
   241
type step_name = string * string option
blanchet@39692
   242
blanchet@43809
   243
fun is_same_atp_step (s1, _) (s2, _) = s1 = s2
blanchet@43809
   244
blanchet@43809
   245
val vampire_fact_prefix = "f"
blanchet@39692
   246
blanchet@39692
   247
fun step_name_ord p =
blanchet@39695
   248
  let val q = pairself fst p in
blanchet@39692
   249
    (* The "unprefix" part is to cope with remote Vampire's output. The proper
blanchet@39692
   250
       solution would be to perform a topological sort, e.g. using the nice
blanchet@39692
   251
       "Graph" functor. *)
blanchet@43809
   252
    case pairself (Int.fromString
blanchet@43809
   253
                   o perhaps (try (unprefix vampire_fact_prefix))) q of
blanchet@39692
   254
      (NONE, NONE) => string_ord q
blanchet@39692
   255
    | (NONE, SOME _) => LESS
blanchet@39692
   256
    | (SOME _, NONE) => GREATER
blanchet@39692
   257
    | (SOME i, SOME j) => int_ord (i, j)
blanchet@39692
   258
  end
blanchet@39692
   259
blanchet@39693
   260
datatype 'a step =
blanchet@39693
   261
  Definition of step_name * 'a * 'a |
blanchet@39693
   262
  Inference of step_name * 'a * step_name list
blanchet@39692
   263
blanchet@43402
   264
type 'a proof = ('a, 'a, 'a fo_term) formula step list
blanchet@39692
   265
blanchet@39692
   266
fun step_name (Definition (name, _, _)) = name
blanchet@39692
   267
  | step_name (Inference (name, _, _)) = name
blanchet@39692
   268
blanchet@39692
   269
(**** PARSING OF TSTP FORMAT ****)
blanchet@39692
   270
blanchet@43814
   271
(* FIXME: temporary hack *)
blanchet@43814
   272
fun repair_waldmeister_step_name s =
blanchet@43814
   273
  case space_explode "." s of
blanchet@43814
   274
    [a, b, c, d] =>
blanchet@43814
   275
    (case a of "0" => "X" | "1" => "Y" | _ => "Z" ^ a) ^
blanchet@43814
   276
    (if size b = 1 then "0" else "") ^ b ^ c ^ d
blanchet@43814
   277
  | _ => s
blanchet@43814
   278
blanchet@43407
   279
(* Strings enclosed in single quotes (e.g., file names) *)
blanchet@39692
   280
val scan_general_id =
blanchet@43814
   281
  $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'"
blanchet@43814
   282
     >> implode >> repair_waldmeister_step_name
blanchet@39692
   283
  || Scan.repeat ($$ "$") -- Scan.many1 Symbol.is_letdig
blanchet@39692
   284
     >> (fn (ss1, ss2) => implode ss1 ^ implode ss2)
blanchet@39692
   285
blanchet@39692
   286
(* Generalized first-order terms, which include file names, numbers, etc. *)
blanchet@43465
   287
fun parse_annotation x =
blanchet@43481
   288
  ((scan_general_id ::: Scan.repeat ($$ " " |-- scan_general_id))
blanchet@43481
   289
     -- Scan.optional parse_annotation [] >> op @
blanchet@43465
   290
   || $$ "(" |-- parse_annotations --| $$ ")"
blanchet@43465
   291
   || $$ "[" |-- parse_annotations --| $$ "]") x
blanchet@43465
   292
and parse_annotations x =
blanchet@43465
   293
  (Scan.optional (parse_annotation
blanchet@43465
   294
                  ::: Scan.repeat ($$ "," |-- parse_annotation)) []
blanchet@39692
   295
   >> flat) x
blanchet@39692
   296
blanchet@43807
   297
fun list_app (f, args) =
blanchet@43809
   298
  fold (fn arg => fn f => ATerm (tptp_app, [f, arg])) args f
blanchet@43807
   299
blanchet@43809
   300
(* We ignore TFF and THF types for now. *)
blanchet@43809
   301
fun parse_type_stuff x =
blanchet@43809
   302
  Scan.repeat (($$ tptp_has_type || $$ tptp_fun_type) |-- parse_arg) x
blanchet@43809
   303
and parse_arg x =
blanchet@43809
   304
  ($$ "(" |-- parse_term --| $$ ")" --| parse_type_stuff
blanchet@43809
   305
   || scan_general_id --| parse_type_stuff
blanchet@43809
   306
        -- Scan.optional ($$ "(" |-- parse_terms --| $$ ")") []
blanchet@43809
   307
      >> ATerm) x
blanchet@43807
   308
and parse_app x =
blanchet@43809
   309
  (parse_arg -- Scan.repeat ($$ tptp_app |-- parse_arg) >> list_app) x
blanchet@43807
   310
and parse_term x =
blanchet@43809
   311
  (parse_app -- Scan.option (Scan.option ($$ tptp_not_infix) --| $$ tptp_equal
blanchet@43809
   312
                             -- parse_app)
blanchet@43809
   313
   >> (fn (u1, NONE) => u1
blanchet@43809
   314
        | (u1, SOME (NONE, u2)) => ATerm ("equal", [u1, u2])
blanchet@43809
   315
        | (u1, SOME (SOME _, u2)) =>
blanchet@43809
   316
          ATerm (tptp_not, [ATerm ("equal", [u1, u2])]))) x
blanchet@43807
   317
and parse_terms x =
blanchet@43807
   318
  (parse_term ::: Scan.repeat ($$ "," |-- parse_term)) x
blanchet@39692
   319
blanchet@43809
   320
(* TODO: Avoid duplication with "parse_term" above. *)
blanchet@39822
   321
fun parse_atom x =
blanchet@43809
   322
  (parse_term -- Scan.option (Scan.option ($$ tptp_not_infix) --| $$ tptp_equal
blanchet@43809
   323
                              -- parse_term)
blanchet@39822
   324
   >> (fn (u1, NONE) => AAtom u1
blanchet@43784
   325
        | (u1, SOME (NONE, u2)) => AAtom (ATerm ("equal", [u1, u2]))
blanchet@39822
   326
        | (u1, SOME (SOME _, u2)) =>
blanchet@43784
   327
          mk_anot (AAtom (ATerm ("equal", [u1, u2]))))) x
blanchet@39692
   328
blanchet@39692
   329
fun fo_term_head (ATerm (s, _)) = s
blanchet@39692
   330
blanchet@39692
   331
(* TPTP formulas are fully parenthesized, so we don't need to worry about
blanchet@39692
   332
   operator precedence. *)
blanchet@43476
   333
fun parse_literal x =
blanchet@43809
   334
  ((Scan.repeat ($$ tptp_not) >> length)
blanchet@43476
   335
      -- ($$ "(" |-- parse_formula --| $$ ")"
blanchet@43476
   336
          || parse_quantified_formula
blanchet@43476
   337
          || parse_atom)
blanchet@43476
   338
      >> (fn (n, phi) => phi |> n mod 2 = 1 ? mk_anot)) x
blanchet@43476
   339
and parse_formula x =
blanchet@43476
   340
  (parse_literal
blanchet@43809
   341
   -- Scan.option ((Scan.this_string tptp_implies >> K AImplies
blanchet@43809
   342
                    || Scan.this_string tptp_iff >> K AIff
blanchet@43809
   343
                    || Scan.this_string tptp_not_iff >> K ANotIff
blanchet@43809
   344
                    || Scan.this_string tptp_if >> K AIf
blanchet@43809
   345
                    || $$ tptp_or >> K AOr
blanchet@43809
   346
                    || $$ tptp_and >> K AAnd)
blanchet@39694
   347
                   -- parse_formula)
blanchet@39692
   348
   >> (fn (phi1, NONE) => phi1
blanchet@43784
   349
        | (phi1, SOME (c, phi2)) => mk_aconn c phi1 phi2)) x
blanchet@43476
   350
and parse_quantified_formula x =
blanchet@43809
   351
  (($$ tptp_forall >> K AForall || $$ tptp_exists >> K AExists)
blanchet@43476
   352
   --| $$ "[" -- parse_terms --| $$ "]" --| $$ ":" -- parse_literal
blanchet@43476
   353
   >> (fn ((q, ts), phi) =>
blanchet@43807
   354
          (* We ignore TFF and THF types for now. *)
blanchet@43476
   355
          AQuant (q, map (rpair NONE o fo_term_head) ts, phi))) x
blanchet@39692
   356
blanchet@43809
   357
fun skip_formula ss =
blanchet@43809
   358
  let
blanchet@43809
   359
    fun skip _ [] = []
blanchet@43809
   360
      | skip 0 (ss as "," :: _) = ss
blanchet@43809
   361
      | skip 0 (ss as ")" :: _) = ss
blanchet@43809
   362
      | skip 0 (ss as "]" :: _) = ss
blanchet@43809
   363
      | skip n ("(" :: ss) = skip (n + 1) ss
blanchet@43809
   364
      | skip n ("[" :: ss) = skip (n + 1) ss
blanchet@43809
   365
      | skip n ("]" :: ss) = skip (n - 1) ss
blanchet@43809
   366
      | skip n (")" :: ss) = skip (n - 1) ss
blanchet@43809
   367
      | skip n (_ :: ss) = skip n ss
blanchet@43809
   368
  in (AAtom (ATerm ("", [])), skip 0 ss) end
blanchet@43809
   369
blanchet@39692
   370
val parse_tstp_extra_arguments =
blanchet@43465
   371
  Scan.optional ($$ "," |-- parse_annotation
blanchet@43465
   372
                 --| Scan.option ($$ "," |-- parse_annotations)) []
blanchet@39692
   373
blanchet@41451
   374
val vampire_unknown_fact = "unknown"
blanchet@43784
   375
val waldmeister_conjecture = "conjecture_1"
blanchet@43784
   376
blanchet@43407
   377
val tofof_fact_prefix = "fof_"
blanchet@41451
   378
blanchet@43784
   379
fun is_same_term subst tm1 tm2 =
blanchet@43784
   380
  let
blanchet@43784
   381
    fun do_term_pair _ NONE = NONE
blanchet@43784
   382
      | do_term_pair (ATerm (s1, tm1), ATerm (s2, tm2)) (SOME subst) =
blanchet@43839
   383
        case pairself is_tptp_variable (s1, s2) of
blanchet@43784
   384
          (true, true) =>
blanchet@43784
   385
          (case AList.lookup (op =) subst s1 of
blanchet@43784
   386
             SOME s2' => if s2' = s2 then SOME subst else NONE
blanchet@43784
   387
           | NONE =>
blanchet@43784
   388
             if null (AList.find (op =) subst s2) then SOME ((s1, s2) :: subst)
blanchet@43784
   389
             else NONE)
blanchet@43784
   390
        | (false, false) =>
blanchet@43784
   391
          if s1 = s2 andalso length tm1 = length tm2 then
blanchet@43784
   392
            SOME subst |> fold do_term_pair (tm1 ~~ tm2)
blanchet@43784
   393
          else
blanchet@43784
   394
            NONE
blanchet@43784
   395
        | _ => NONE
blanchet@43784
   396
  in SOME subst |> do_term_pair (tm1, tm2) |> is_some end
blanchet@43784
   397
blanchet@43784
   398
fun is_same_formula subst (AQuant (q1, xs1, phi1)) (AQuant (q2, xs2, phi2)) =
blanchet@43784
   399
    q1 = q2 andalso length xs1 = length xs2 andalso
blanchet@43784
   400
    is_same_formula ((map fst xs1 ~~ map fst xs2) @ subst) phi1 phi2
blanchet@43784
   401
  | is_same_formula subst (AConn (c1, phis1)) (AConn (c2, phis2)) =
blanchet@43784
   402
    c1 = c2 andalso length phis1 = length phis2 andalso
blanchet@43784
   403
    forall (uncurry (is_same_formula subst)) (phis1 ~~ phis2)
blanchet@43784
   404
  | is_same_formula subst (AAtom (ATerm ("equal", [tm11, tm12]))) (AAtom tm2) =
blanchet@43784
   405
    is_same_term subst (ATerm ("equal", [tm11, tm12])) tm2 orelse
blanchet@43784
   406
    is_same_term subst (ATerm ("equal", [tm12, tm11])) tm2
blanchet@43784
   407
  | is_same_formula subst (AAtom tm1) (AAtom tm2) = is_same_term subst tm1 tm2
blanchet@43784
   408
  | is_same_formula _ _ _ = false
blanchet@43784
   409
blanchet@43784
   410
fun matching_formula_line_identifier phi (Formula (ident, _, phi', _, _)) =
blanchet@43784
   411
    if is_same_formula [] phi phi' then SOME ident else NONE
blanchet@43784
   412
  | matching_formula_line_identifier _ _ = NONE
blanchet@43784
   413
blanchet@43784
   414
fun find_formula_in_problem problem phi =
blanchet@43784
   415
  problem |> maps snd |> map_filter (matching_formula_line_identifier phi)
blanchet@43784
   416
          |> try hd
blanchet@43784
   417
blanchet@43803
   418
(* Syntax: (cnf|fof|tff|thf)\(<num>, <formula_role>,
blanchet@43803
   419
            <formula> <extra_arguments>\).
blanchet@39692
   420
   The <num> could be an identifier, but we assume integers. *)
blanchet@43784
   421
fun parse_tstp_line problem =
blanchet@43809
   422
  ((Scan.this_string tptp_cnf || Scan.this_string tptp_fof
blanchet@43809
   423
    || Scan.this_string tptp_tff || Scan.this_string tptp_thf) -- $$ "(")
blanchet@43784
   424
    |-- scan_general_id --| $$ "," -- Symbol.scan_id --| $$ ","
blanchet@43809
   425
    -- (parse_formula || skip_formula) -- parse_tstp_extra_arguments --| $$ ")"
blanchet@43809
   426
    --| $$ "."
blanchet@43784
   427
   >> (fn (((num, role), phi), deps) =>
blanchet@43784
   428
          let
blanchet@43784
   429
            val (name, deps) =
blanchet@43784
   430
              (* Waldmeister isn't exactly helping. *)
blanchet@43784
   431
              case deps of
blanchet@43784
   432
                ["file", _, s] =>
blanchet@43784
   433
                ((num,
blanchet@43784
   434
                  if s = vampire_unknown_fact then
blanchet@43784
   435
                    NONE
blanchet@43784
   436
                  else if s = waldmeister_conjecture then
blanchet@43784
   437
                    find_formula_in_problem problem (mk_anot phi)
blanchet@43784
   438
                  else
blanchet@43784
   439
                    SOME (s |> perhaps (try (unprefix tofof_fact_prefix)))),
blanchet@43784
   440
                 [])
blanchet@43784
   441
              | ["file", _] => ((num, find_formula_in_problem problem phi), [])
blanchet@43784
   442
              | _ => ((num, NONE), deps)
blanchet@43784
   443
          in
blanchet@43784
   444
            case role of
blanchet@43784
   445
              "definition" =>
blanchet@43784
   446
              (case phi of
blanchet@43784
   447
                 AConn (AIff, [phi1 as AAtom _, phi2]) =>
blanchet@43784
   448
                 Definition (name, phi1, phi2)
blanchet@43784
   449
               | AAtom (ATerm ("equal", _)) =>
blanchet@43784
   450
                 (* Vampire's equality proxy axiom *)
blanchet@43784
   451
                 Inference (name, phi, map (rpair NONE) deps)
blanchet@43809
   452
               | _ => raise UNRECOGNIZED_ATP_PROOF ())
blanchet@43784
   453
            | _ => Inference (name, phi, map (rpair NONE) deps)
blanchet@43784
   454
          end)
blanchet@39692
   455
blanchet@39692
   456
(**** PARSING OF SPASS OUTPUT ****)
blanchet@39692
   457
blanchet@39692
   458
(* SPASS returns clause references of the form "x.y". We ignore "y", whose role
blanchet@39692
   459
   is not clear anyway. *)
blanchet@39692
   460
val parse_dot_name = scan_general_id --| $$ "." --| scan_general_id
blanchet@39692
   461
blanchet@39692
   462
val parse_spass_annotations =
blanchet@39692
   463
  Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
blanchet@39692
   464
                                         --| Scan.option ($$ ","))) []
blanchet@39692
   465
blanchet@39692
   466
(* It is not clear why some literals are followed by sequences of stars and/or
blanchet@39692
   467
   pluses. We ignore them. *)
blanchet@39826
   468
fun parse_decorated_atom x =
blanchet@39826
   469
  (parse_atom --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")) x
blanchet@39692
   470
blanchet@39692
   471
fun mk_horn ([], []) = AAtom (ATerm ("c_False", []))
blanchet@43784
   472
  | mk_horn ([], pos_lits) = foldr1 (uncurry (mk_aconn AOr)) pos_lits
blanchet@43784
   473
  | mk_horn (neg_lits, []) = mk_anot (foldr1 (uncurry (mk_aconn AAnd)) neg_lits)
blanchet@39692
   474
  | mk_horn (neg_lits, pos_lits) =
blanchet@43784
   475
    mk_aconn AImplies (foldr1 (uncurry (mk_aconn AAnd)) neg_lits)
blanchet@43784
   476
                      (foldr1 (uncurry (mk_aconn AOr)) pos_lits)
blanchet@39692
   477
blanchet@39869
   478
fun parse_horn_clause x =
blanchet@39869
   479
  (Scan.repeat parse_decorated_atom --| $$ "|" --| $$ "|"
blanchet@39869
   480
     -- Scan.repeat parse_decorated_atom --| $$ "-" --| $$ ">"
blanchet@39869
   481
     -- Scan.repeat parse_decorated_atom
blanchet@39869
   482
   >> (mk_horn o apfst (op @))) x
blanchet@39692
   483
blanchet@39692
   484
(* Syntax: <num>[0:<inference><annotations>]
blanchet@39692
   485
   <atoms> || <atoms> -> <atoms>. *)
blanchet@39869
   486
fun parse_spass_line x =
blanchet@39869
   487
  (scan_general_id --| $$ "[" --| $$ "0" --| $$ ":" --| Symbol.scan_id
blanchet@39869
   488
     -- parse_spass_annotations --| $$ "]" -- parse_horn_clause --| $$ "."
blanchet@39869
   489
   >> (fn ((num, deps), u) =>
blanchet@39869
   490
          Inference ((num, NONE), u, map (rpair NONE) deps))) x
blanchet@39692
   491
blanchet@43784
   492
fun parse_line problem = parse_tstp_line problem || parse_spass_line
blanchet@43784
   493
fun parse_proof problem s =
blanchet@43519
   494
  s |> strip_spaces_except_between_ident_chars
blanchet@43519
   495
    |> raw_explode
blanchet@43519
   496
    |> Scan.finite Symbol.stopper
blanchet@43806
   497
           (Scan.error (!! (fn _ => raise UNRECOGNIZED_ATP_PROOF ())
blanchet@43784
   498
                           (Scan.repeat1 (parse_line problem))))
blanchet@43519
   499
    |> fst
blanchet@39692
   500
blanchet@43784
   501
fun atp_proof_from_tstplike_proof _ "" = []
blanchet@43784
   502
  | atp_proof_from_tstplike_proof problem s =
blanchet@43320
   503
    s ^ "$" (* the $ sign acts as a sentinel (FIXME: needed?) *)
blanchet@43784
   504
    |> parse_proof problem
blanchet@43320
   505
    |> sort (step_name_ord o pairself step_name)
blanchet@43809
   506
blanchet@43809
   507
fun clean_up_dependencies _ [] = []
blanchet@43809
   508
  | clean_up_dependencies seen ((step as Definition (name, _, _)) :: steps) =
blanchet@43809
   509
    step :: clean_up_dependencies (name :: seen) steps
blanchet@43809
   510
  | clean_up_dependencies seen (Inference (name, u, deps) :: steps) =
blanchet@43809
   511
    Inference (name, u,
blanchet@43809
   512
               map_filter (fn dep => find_first (is_same_atp_step dep) seen)
blanchet@43809
   513
                          deps) ::
blanchet@43809
   514
    clean_up_dependencies (name :: seen) steps
blanchet@43809
   515
blanchet@43816
   516
fun clean_up_atp_proof_dependencies proof = clean_up_dependencies [] proof
blanchet@39692
   517
blanchet@39694
   518
fun map_term_names_in_term f (ATerm (s, ts)) =
blanchet@39694
   519
  ATerm (f s, map (map_term_names_in_term f) ts)
blanchet@39694
   520
fun map_term_names_in_formula f (AQuant (q, xs, phi)) =
blanchet@39694
   521
    AQuant (q, xs, map_term_names_in_formula f phi)
blanchet@39694
   522
  | map_term_names_in_formula f (AConn (c, phis)) =
blanchet@39694
   523
    AConn (c, map (map_term_names_in_formula f) phis)
blanchet@39694
   524
  | map_term_names_in_formula f (AAtom t) = AAtom (map_term_names_in_term f t)
blanchet@39694
   525
fun map_term_names_in_step f (Definition (name, phi1, phi2)) =
blanchet@39694
   526
    Definition (name, map_term_names_in_formula f phi1,
blanchet@39694
   527
                map_term_names_in_formula f phi2)
blanchet@39694
   528
  | map_term_names_in_step f (Inference (name, phi, deps)) =
blanchet@39694
   529
    Inference (name, map_term_names_in_formula f phi, deps)
blanchet@39694
   530
fun map_term_names_in_atp_proof f = map (map_term_names_in_step f)
blanchet@39694
   531
blanchet@39694
   532
fun nasty_name pool s = s |> Symtab.lookup pool |> the_default s
blanchet@39694
   533
fun nasty_atp_proof pool =
blanchet@39694
   534
  if Symtab.is_empty pool then I
blanchet@39694
   535
  else map_term_names_in_atp_proof (nasty_name pool)
blanchet@39694
   536
blanchet@39692
   537
end;