src/HOL/Tools/ATP/atp_proof.ML
author blanchet
Wed, 09 Feb 2011 17:18:57 +0100
changeset 42609 eb98c60a6cf0
parent 41582 3cb52cbf0eed
child 42615 a18e7bbca258
permissions -rw-r--r--
added experimental "remote_z3_atp", Sutcliffe's TPTP-syntax-aware wrapper for Z3 -- allows to do head-to-head comparison of Sledgehammer's ATP translation and of Sascha's SMT translation
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@39694
     6
Abstract representation of ATP proofs and TSTP/Vampire/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@39693
    12
  type 'a uniform_formula = 'a ATP_Problem.uniform_formula
blanchet@39692
    13
blanchet@39731
    14
  datatype failure =
blanchet@42609
    15
    Unprovable | IncompleteUnprovable | ProofMissing | CantConnect | TimedOut |
blanchet@39731
    16
    OutOfResources | SpassTooOld | VampireTooOld | NoPerl | NoLibwwwPerl |
blanchet@41470
    17
    NoRealZ3 | MalformedInput | MalformedOutput | Interrupted | Crashed |
blanchet@41505
    18
    InternalError | UnknownError of string
blanchet@39731
    19
blanchet@39695
    20
  type step_name = string * string option
blanchet@39692
    21
blanchet@39693
    22
  datatype 'a step =
blanchet@39693
    23
    Definition of step_name * 'a * 'a |
blanchet@39693
    24
    Inference of step_name * 'a * step_name list
blanchet@39692
    25
blanchet@39693
    26
  type 'a proof = 'a uniform_formula step list
blanchet@39692
    27
blanchet@39697
    28
  val strip_spaces : (char -> bool) -> string -> string
blanchet@41505
    29
  val short_output : bool -> string -> string
blanchet@40917
    30
  val string_for_failure : string -> failure -> string
blanchet@39731
    31
  val extract_important_message : string -> string
blanchet@39731
    32
  val extract_known_failure :
blanchet@39731
    33
    (failure * string) list -> string -> failure option
blanchet@39731
    34
  val extract_tstplike_proof_and_outcome :
blanchet@41505
    35
    bool -> bool -> int -> (string * string) list -> (failure * string) list
blanchet@41505
    36
    -> string -> string * failure option
blanchet@39692
    37
  val is_same_step : step_name * step_name -> bool
blanchet@41394
    38
  val atp_proof_from_tstplike_string : bool -> string -> string proof
blanchet@39694
    39
  val map_term_names_in_atp_proof :
blanchet@39694
    40
    (string -> string) -> string proof -> string proof
blanchet@39694
    41
  val nasty_atp_proof : string Symtab.table -> string proof -> string proof
blanchet@39692
    42
end;
blanchet@39692
    43
blanchet@39692
    44
structure ATP_Proof : ATP_PROOF =
blanchet@39692
    45
struct
blanchet@39692
    46
blanchet@39731
    47
open ATP_Problem
blanchet@39731
    48
blanchet@39731
    49
datatype failure =
blanchet@42609
    50
  Unprovable | IncompleteUnprovable | ProofMissing | CantConnect | TimedOut |
blanchet@42609
    51
  OutOfResources | SpassTooOld | VampireTooOld | NoPerl | NoLibwwwPerl |
blanchet@42609
    52
  NoRealZ3 | MalformedInput | MalformedOutput | Interrupted | Crashed |
blanchet@42609
    53
  InternalError | UnknownError of string
blanchet@39731
    54
blanchet@39692
    55
fun strip_spaces_in_list _ [] = []
blanchet@39692
    56
  | strip_spaces_in_list _ [c1] = if Char.isSpace c1 then [] else [str c1]
blanchet@39692
    57
  | strip_spaces_in_list is_evil [c1, c2] =
blanchet@39692
    58
    strip_spaces_in_list is_evil [c1] @ strip_spaces_in_list is_evil [c2]
blanchet@39692
    59
  | strip_spaces_in_list is_evil (c1 :: c2 :: c3 :: cs) =
blanchet@39692
    60
    if Char.isSpace c1 then
blanchet@39692
    61
      strip_spaces_in_list is_evil (c2 :: c3 :: cs)
blanchet@39692
    62
    else if Char.isSpace c2 then
blanchet@39692
    63
      if Char.isSpace c3 then
blanchet@39692
    64
        strip_spaces_in_list is_evil (c1 :: c3 :: cs)
blanchet@39692
    65
      else
blanchet@39692
    66
        str c1 :: (if forall is_evil [c1, c3] then [" "] else []) @
blanchet@39692
    67
        strip_spaces_in_list is_evil (c3 :: cs)
blanchet@39692
    68
    else
blanchet@39692
    69
      str c1 :: strip_spaces_in_list is_evil (c2 :: c3 :: cs)
blanchet@39692
    70
fun strip_spaces is_evil =
blanchet@39692
    71
  implode o strip_spaces_in_list is_evil o String.explode
blanchet@39692
    72
blanchet@39692
    73
fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
blanchet@39692
    74
val strip_spaces_except_between_ident_chars = strip_spaces is_ident_char
blanchet@39692
    75
blanchet@41505
    76
fun elide_string threshold s =
blanchet@41505
    77
  if size s > threshold then
blanchet@41505
    78
    String.extract (s, 0, SOME (threshold div 2 - 5)) ^ " ...... " ^
blanchet@41505
    79
    String.extract (s, size s - (threshold + 1) div 2 + 6, NONE)
blanchet@41505
    80
  else
blanchet@41505
    81
    s
blanchet@41505
    82
fun short_output verbose output =
blanchet@41505
    83
  if verbose then elide_string 1000 output else ""
blanchet@41505
    84
blanchet@40932
    85
fun missing_message_tail prover =
blanchet@40932
    86
  " appears to be missing. You will need to install it if you want to run " ^
blanchet@40932
    87
  prover ^ "s remotely."
blanchet@39731
    88
blanchet@40917
    89
fun string_for_failure prover Unprovable =
blanchet@40917
    90
    "The " ^ prover ^ " problem is unprovable."
blanchet@40917
    91
  | string_for_failure prover IncompleteUnprovable =
blanchet@40917
    92
    "The " ^ prover ^ " cannot prove the problem."
blanchet@42609
    93
  | string_for_failure prover ProofMissing =
blanchet@42609
    94
    "The " ^ prover ^ " claims the conjecture is a theorem but did not provide \
blanchet@42609
    95
    \a proof."
blanchet@40917
    96
  | string_for_failure _ CantConnect = "Cannot connect to remote server."
blanchet@40917
    97
  | string_for_failure _ TimedOut = "Timed out."
blanchet@40917
    98
  | string_for_failure prover OutOfResources =
blanchet@40917
    99
    "The " ^ prover ^ " ran out of resources."
blanchet@40917
   100
  | string_for_failure _ SpassTooOld =
blanchet@39731
   101
    "Isabelle requires a more recent version of SPASS with support for the \
blanchet@39731
   102
    \TPTP syntax. To install it, download and extract the package \
blanchet@39731
   103
    \\"http://isabelle.in.tum.de/dist/contrib/spass-3.7.tar.gz\" and add the \
blanchet@39731
   104
    \\"spass-3.7\" directory's absolute path to " ^
blanchet@39731
   105
    quote (Path.implode (Path.expand (Path.appends
blanchet@39731
   106
               (Path.variable "ISABELLE_HOME_USER" ::
blanchet@39731
   107
                map Path.basic ["etc", "components"])))) ^
blanchet@39731
   108
    " on a line of its own."
blanchet@40917
   109
  | string_for_failure _ VampireTooOld =
blanchet@39731
   110
    "Isabelle requires a more recent version of Vampire. To install it, follow \
blanchet@39731
   111
    \the instructions from the Sledgehammer manual (\"isabelle doc\
blanchet@39731
   112
    \ sledgehammer\")."
blanchet@40932
   113
  | string_for_failure prover NoPerl = "Perl" ^ missing_message_tail prover
blanchet@40932
   114
  | string_for_failure prover NoLibwwwPerl =
blanchet@40932
   115
    "The Perl module \"libwww-perl\"" ^ missing_message_tail prover
blanchet@41513
   116
  | string_for_failure _ NoRealZ3 =
blanchet@41470
   117
    "The environment variable \"Z3_REAL_SOLVER\" must be set to Z3's full path."
blanchet@40917
   118
  | string_for_failure prover MalformedInput =
blanchet@41451
   119
    "The " ^ prover ^ " problem is malformed. Please report this to the \
blanchet@41451
   120
    \Isabelle developers."
blanchet@40917
   121
  | string_for_failure prover MalformedOutput =
blanchet@40917
   122
    "The " ^ prover ^ " output is malformed."
blanchet@40917
   123
  | string_for_failure prover Crashed = "The " ^ prover ^ " crashed."
blanchet@40917
   124
  | string_for_failure prover InternalError =
blanchet@40917
   125
    "An internal " ^ prover ^ " error occurred."
blanchet@41505
   126
  | string_for_failure prover (UnknownError string) =
blanchet@41340
   127
    (* "An" is correct for "ATP" and "SMT". *)
blanchet@41505
   128
    "An " ^ prover ^ " error occurred" ^
blanchet@41582
   129
    (if string = "" then ". (Pass the \"verbose\" option for details.)"
blanchet@41582
   130
     else ":\n" ^ string)
blanchet@39731
   131
blanchet@39731
   132
fun extract_delimited (begin_delim, end_delim) output =
blanchet@39731
   133
  output |> first_field begin_delim |> the |> snd
blanchet@39731
   134
         |> first_field end_delim |> the |> fst
blanchet@39731
   135
         |> first_field "\n" |> the |> snd
blanchet@39731
   136
  handle Option.Option => ""
blanchet@39731
   137
blanchet@39731
   138
val tstp_important_message_delims =
blanchet@39731
   139
  ("% SZS start RequiredInformation", "% SZS end RequiredInformation")
blanchet@39731
   140
blanchet@39731
   141
fun extract_important_message output =
blanchet@39731
   142
  case extract_delimited tstp_important_message_delims output of
blanchet@39731
   143
    "" => ""
blanchet@39731
   144
  | s => s |> space_explode "\n" |> filter_out (curry (op =) "")
blanchet@39731
   145
           |> map (perhaps (try (unprefix "%")))
blanchet@39731
   146
           |> map (perhaps (try (unprefix " ")))
blanchet@39731
   147
           |> space_implode "\n " |> quote
blanchet@39731
   148
blanchet@39731
   149
(* Splits by the first possible of a list of delimiters. *)
blanchet@39731
   150
fun extract_tstplike_proof delims output =
blanchet@39731
   151
  case pairself (find_first (fn s => String.isSubstring s output))
blanchet@39731
   152
                (ListPair.unzip delims) of
blanchet@39731
   153
    (SOME begin_delim, SOME end_delim) =>
blanchet@39731
   154
    extract_delimited (begin_delim, end_delim) output
blanchet@39731
   155
  | _ => ""
blanchet@39731
   156
blanchet@39731
   157
fun extract_known_failure known_failures output =
blanchet@39731
   158
  known_failures
blanchet@39731
   159
  |> find_first (fn (_, pattern) => String.isSubstring pattern output)
blanchet@39731
   160
  |> Option.map fst
blanchet@39731
   161
blanchet@41505
   162
fun extract_tstplike_proof_and_outcome verbose complete res_code proof_delims
blanchet@39731
   163
                                       known_failures output =
blanchet@39731
   164
  case extract_known_failure known_failures output of
blanchet@39731
   165
    NONE => (case extract_tstplike_proof proof_delims output of
blanchet@39731
   166
             "" => ("", SOME (if res_code = 0 andalso output = "" then
blanchet@42609
   167
                                ProofMissing
blanchet@39731
   168
                              else
blanchet@41505
   169
                                UnknownError (short_output verbose output)))
blanchet@41505
   170
           | tstplike_proof =>
blanchet@41505
   171
             if res_code = 0 then (tstplike_proof, NONE)
blanchet@41505
   172
             else ("", SOME (UnknownError (short_output verbose output))))
blanchet@39731
   173
  | SOME failure =>
blanchet@39731
   174
    ("", SOME (if failure = IncompleteUnprovable andalso complete then
blanchet@39731
   175
                 Unprovable
blanchet@39731
   176
               else
blanchet@39731
   177
                 failure))
blanchet@39692
   178
blanchet@39692
   179
fun mk_anot (AConn (ANot, [phi])) = phi
blanchet@39692
   180
  | mk_anot phi = AConn (ANot, [phi])
blanchet@39692
   181
fun mk_aconn c (phi1, phi2) = AConn (c, [phi1, phi2])
blanchet@39692
   182
blanchet@39695
   183
type step_name = string * string option
blanchet@39692
   184
blanchet@39695
   185
fun is_same_step p = p |> pairself fst |> op =
blanchet@39692
   186
blanchet@39692
   187
fun step_name_ord p =
blanchet@39695
   188
  let val q = pairself fst p in
blanchet@39692
   189
    (* The "unprefix" part is to cope with remote Vampire's output. The proper
blanchet@39692
   190
       solution would be to perform a topological sort, e.g. using the nice
blanchet@39692
   191
       "Graph" functor. *)
blanchet@39692
   192
    case pairself (Int.fromString o perhaps (try (unprefix "f"))) q of
blanchet@39692
   193
      (NONE, NONE) => string_ord q
blanchet@39692
   194
    | (NONE, SOME _) => LESS
blanchet@39692
   195
    | (SOME _, NONE) => GREATER
blanchet@39692
   196
    | (SOME i, SOME j) => int_ord (i, j)
blanchet@39692
   197
  end
blanchet@39692
   198
blanchet@39693
   199
datatype 'a step =
blanchet@39693
   200
  Definition of step_name * 'a * 'a |
blanchet@39693
   201
  Inference of step_name * 'a * step_name list
blanchet@39692
   202
blanchet@39693
   203
type 'a proof = 'a uniform_formula step list
blanchet@39692
   204
blanchet@39692
   205
fun step_name (Definition (name, _, _)) = name
blanchet@39692
   206
  | step_name (Inference (name, _, _)) = name
blanchet@39692
   207
blanchet@39692
   208
(**** PARSING OF TSTP FORMAT ****)
blanchet@39692
   209
blanchet@39692
   210
(*Strings enclosed in single quotes, e.g. filenames*)
blanchet@39692
   211
val scan_general_id =
blanchet@39692
   212
  $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode
blanchet@39692
   213
  || Scan.repeat ($$ "$") -- Scan.many1 Symbol.is_letdig
blanchet@39692
   214
     >> (fn (ss1, ss2) => implode ss1 ^ implode ss2)
blanchet@39692
   215
blanchet@39692
   216
(* Generalized first-order terms, which include file names, numbers, etc. *)
blanchet@39692
   217
fun parse_annotation strict x =
blanchet@39692
   218
  ((scan_general_id ::: Scan.repeat ($$ " " |-- scan_general_id)
blanchet@39692
   219
      >> (strict ? filter (is_some o Int.fromString)))
blanchet@39692
   220
   -- Scan.optional (parse_annotation strict) [] >> op @
blanchet@39692
   221
   || $$ "(" |-- parse_annotations strict --| $$ ")"
blanchet@39692
   222
   || $$ "[" |-- parse_annotations strict --| $$ "]") x
blanchet@39692
   223
and parse_annotations strict x =
blanchet@39692
   224
  (Scan.optional (parse_annotation strict
blanchet@39692
   225
                  ::: Scan.repeat ($$ "," |-- parse_annotation strict)) []
blanchet@39692
   226
   >> flat) x
blanchet@39692
   227
blanchet@39692
   228
(* Vampire proof lines sometimes contain needless information such as "(0:3)",
blanchet@39692
   229
   which can be hard to disambiguate from function application in an LL(1)
blanchet@39692
   230
   parser. As a workaround, we extend the TPTP term syntax with such detritus
blanchet@39692
   231
   and ignore it. *)
blanchet@39692
   232
fun parse_vampire_detritus x =
blanchet@39692
   233
  (scan_general_id |-- $$ ":" --| scan_general_id >> K []) x
blanchet@39692
   234
blanchet@39694
   235
fun parse_term x =
blanchet@39694
   236
  (scan_general_id
blanchet@41451
   237
     -- Scan.optional ($$ "(" |-- (parse_vampire_detritus || parse_terms)
blanchet@41451
   238
                       --| $$ ")") []
blanchet@41451
   239
     --| Scan.optional ($$ "(" |-- parse_vampire_detritus --| $$ ")") []
blanchet@39692
   240
   >> ATerm) x
blanchet@39694
   241
and parse_terms x = (parse_term ::: Scan.repeat ($$ "," |-- parse_term)) x
blanchet@39692
   242
blanchet@39822
   243
fun parse_atom x =
blanchet@39822
   244
  (parse_term -- Scan.option (Scan.option ($$ "!") --| $$ "=" -- parse_term)
blanchet@39822
   245
   >> (fn (u1, NONE) => AAtom u1
blanchet@39822
   246
        | (u1, SOME (NONE, u2)) => AAtom (ATerm ("c_equal", [u1, u2]))
blanchet@39822
   247
        | (u1, SOME (SOME _, u2)) =>
blanchet@39822
   248
          mk_anot (AAtom (ATerm ("c_equal", [u1, u2]))))) x
blanchet@39692
   249
blanchet@39692
   250
fun fo_term_head (ATerm (s, _)) = s
blanchet@39692
   251
blanchet@39692
   252
(* TPTP formulas are fully parenthesized, so we don't need to worry about
blanchet@39692
   253
   operator precedence. *)
blanchet@39694
   254
fun parse_formula x =
blanchet@39694
   255
  (($$ "(" |-- parse_formula --| $$ ")"
blanchet@39692
   256
    || ($$ "!" >> K AForall || $$ "?" >> K AExists)
blanchet@39694
   257
       --| $$ "[" -- parse_terms --| $$ "]" --| $$ ":" -- parse_formula
blanchet@39692
   258
       >> (fn ((q, ts), phi) => AQuant (q, map fo_term_head ts, phi))
blanchet@39694
   259
    || $$ "~" |-- parse_formula >> mk_anot
blanchet@39694
   260
    || parse_atom)
blanchet@39692
   261
   -- Scan.option ((Scan.this_string "=>" >> K AImplies
blanchet@39692
   262
                    || Scan.this_string "<=>" >> K AIff
blanchet@39692
   263
                    || Scan.this_string "<~>" >> K ANotIff
blanchet@39692
   264
                    || Scan.this_string "<=" >> K AIf
blanchet@39692
   265
                    || $$ "|" >> K AOr || $$ "&" >> K AAnd)
blanchet@39694
   266
                   -- parse_formula)
blanchet@39692
   267
   >> (fn (phi1, NONE) => phi1
blanchet@39692
   268
        | (phi1, SOME (c, phi2)) => mk_aconn c (phi1, phi2))) x
blanchet@39692
   269
blanchet@39692
   270
val parse_tstp_extra_arguments =
blanchet@39692
   271
  Scan.optional ($$ "," |-- parse_annotation false
blanchet@39692
   272
                 --| Scan.option ($$ "," |-- parse_annotations false)) []
blanchet@39692
   273
blanchet@41451
   274
val vampire_unknown_fact = "unknown"
blanchet@41451
   275
blanchet@39692
   276
(* Syntax: (fof|cnf)\(<num>, <formula_role>, <formula> <extra_arguments>\).
blanchet@39692
   277
   The <num> could be an identifier, but we assume integers. *)
blanchet@39694
   278
val parse_tstp_line =
blanchet@39694
   279
  ((Scan.this_string "fof" || Scan.this_string "cnf") -- $$ "(")
blanchet@39694
   280
    |-- scan_general_id --| $$ "," -- Symbol.scan_id --| $$ ","
blanchet@39694
   281
    -- parse_formula -- parse_tstp_extra_arguments --| $$ ")" --| $$ "."
blanchet@39694
   282
   >> (fn (((num, role), phi), deps) =>
blanchet@39694
   283
          let
blanchet@39694
   284
            val (name, deps) =
blanchet@39694
   285
              case deps of
blanchet@41451
   286
                ["file", _, s] =>
blanchet@41451
   287
                ((num, if s = vampire_unknown_fact then NONE else SOME s), [])
blanchet@39695
   288
              | _ => ((num, NONE), deps)
blanchet@39694
   289
          in
blanchet@39694
   290
            case role of
blanchet@39694
   291
              "definition" =>
blanchet@39694
   292
              (case phi of
blanchet@39694
   293
                 AConn (AIff, [phi1 as AAtom _, phi2]) =>
blanchet@39694
   294
                 Definition (name, phi1, phi2)
blanchet@39694
   295
               | AAtom (ATerm ("c_equal", _)) =>
blanchet@39694
   296
                 (* Vampire's equality proxy axiom *)
blanchet@39695
   297
                 Inference (name, phi, map (rpair NONE) deps)
blanchet@39694
   298
               | _ => raise Fail "malformed definition")
blanchet@39695
   299
            | _ => Inference (name, phi, map (rpair NONE) deps)
blanchet@39694
   300
          end)
blanchet@39692
   301
blanchet@39692
   302
(**** PARSING OF VAMPIRE OUTPUT ****)
blanchet@39692
   303
blanchet@41394
   304
val parse_vampire_braced_stuff =
blanchet@41449
   305
  $$ "{" -- Scan.repeat (scan_general_id --| Scan.option ($$ ",")) -- $$ "}"
blanchet@41451
   306
val parse_vampire_parenthesized_detritus =
blanchet@41451
   307
  $$ "(" |-- parse_vampire_detritus --| $$ ")"
blanchet@41394
   308
blanchet@39692
   309
(* Syntax: <num>. <formula> <annotation> *)
blanchet@39694
   310
val parse_vampire_line =
blanchet@41394
   311
  scan_general_id --| $$ "." -- parse_formula
blanchet@41451
   312
    --| Scan.option parse_vampire_braced_stuff
blanchet@41451
   313
    --| Scan.option parse_vampire_parenthesized_detritus
blanchet@41451
   314
    -- parse_annotation true
blanchet@39695
   315
  >> (fn ((num, phi), deps) =>
blanchet@39695
   316
         Inference ((num, NONE), phi, map (rpair NONE) deps))
blanchet@39692
   317
blanchet@39692
   318
(**** PARSING OF SPASS OUTPUT ****)
blanchet@39692
   319
blanchet@39692
   320
(* SPASS returns clause references of the form "x.y". We ignore "y", whose role
blanchet@39692
   321
   is not clear anyway. *)
blanchet@39692
   322
val parse_dot_name = scan_general_id --| $$ "." --| scan_general_id
blanchet@39692
   323
blanchet@39692
   324
val parse_spass_annotations =
blanchet@39692
   325
  Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
blanchet@39692
   326
                                         --| Scan.option ($$ ","))) []
blanchet@39692
   327
blanchet@39692
   328
(* It is not clear why some literals are followed by sequences of stars and/or
blanchet@39692
   329
   pluses. We ignore them. *)
blanchet@39826
   330
fun parse_decorated_atom x =
blanchet@39826
   331
  (parse_atom --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")) x
blanchet@39692
   332
blanchet@39692
   333
fun mk_horn ([], []) = AAtom (ATerm ("c_False", []))
blanchet@39692
   334
  | mk_horn ([], pos_lits) = foldr1 (mk_aconn AOr) pos_lits
blanchet@39692
   335
  | mk_horn (neg_lits, []) = mk_anot (foldr1 (mk_aconn AAnd) neg_lits)
blanchet@39692
   336
  | mk_horn (neg_lits, pos_lits) =
blanchet@39692
   337
    mk_aconn AImplies (foldr1 (mk_aconn AAnd) neg_lits,
blanchet@39692
   338
                       foldr1 (mk_aconn AOr) pos_lits)
blanchet@39692
   339
blanchet@39869
   340
fun parse_horn_clause x =
blanchet@39869
   341
  (Scan.repeat parse_decorated_atom --| $$ "|" --| $$ "|"
blanchet@39869
   342
     -- Scan.repeat parse_decorated_atom --| $$ "-" --| $$ ">"
blanchet@39869
   343
     -- Scan.repeat parse_decorated_atom
blanchet@39869
   344
   >> (mk_horn o apfst (op @))) x
blanchet@39692
   345
blanchet@39692
   346
(* Syntax: <num>[0:<inference><annotations>]
blanchet@39692
   347
   <atoms> || <atoms> -> <atoms>. *)
blanchet@39869
   348
fun parse_spass_line x =
blanchet@39869
   349
  (scan_general_id --| $$ "[" --| $$ "0" --| $$ ":" --| Symbol.scan_id
blanchet@39869
   350
     -- parse_spass_annotations --| $$ "]" -- parse_horn_clause --| $$ "."
blanchet@39869
   351
   >> (fn ((num, deps), u) =>
blanchet@39869
   352
          Inference ((num, NONE), u, map (rpair NONE) deps))) x
blanchet@39692
   353
blanchet@39869
   354
fun parse_line x = (parse_tstp_line || parse_vampire_line || parse_spass_line) x
blanchet@39694
   355
val parse_proof =
blanchet@39692
   356
  fst o Scan.finite Symbol.stopper
blanchet@39692
   357
            (Scan.error (!! (fn _ => raise Fail "unrecognized ATP output")
blanchet@39694
   358
                            (Scan.repeat1 parse_line)))
wenzelm@40875
   359
  o raw_explode o strip_spaces_except_between_ident_chars
blanchet@39692
   360
blanchet@39692
   361
fun clean_up_dependency seen dep = find_first (curry is_same_step dep) seen
blanchet@39692
   362
fun clean_up_dependencies _ [] = []
blanchet@39692
   363
  | clean_up_dependencies seen ((step as Definition (name, _, _)) :: steps) =
blanchet@39692
   364
    step :: clean_up_dependencies (name :: seen) steps
blanchet@39692
   365
  | clean_up_dependencies seen (Inference (name, u, deps) :: steps) =
blanchet@39692
   366
    Inference (name, u, map_filter (clean_up_dependency seen) deps) ::
blanchet@39692
   367
    clean_up_dependencies (name :: seen) steps
blanchet@39692
   368
blanchet@41394
   369
fun atp_proof_from_tstplike_string clean =
blanchet@39692
   370
  suffix "$" (* the $ sign acts as a sentinel (FIXME: needed?) *)
blanchet@39694
   371
  #> parse_proof
blanchet@41394
   372
  #> clean ? (sort (step_name_ord o pairself step_name)
blanchet@41394
   373
              #> clean_up_dependencies [])
blanchet@39692
   374
blanchet@39694
   375
fun map_term_names_in_term f (ATerm (s, ts)) =
blanchet@39694
   376
  ATerm (f s, map (map_term_names_in_term f) ts)
blanchet@39694
   377
fun map_term_names_in_formula f (AQuant (q, xs, phi)) =
blanchet@39694
   378
    AQuant (q, xs, map_term_names_in_formula f phi)
blanchet@39694
   379
  | map_term_names_in_formula f (AConn (c, phis)) =
blanchet@39694
   380
    AConn (c, map (map_term_names_in_formula f) phis)
blanchet@39694
   381
  | map_term_names_in_formula f (AAtom t) = AAtom (map_term_names_in_term f t)
blanchet@39694
   382
fun map_term_names_in_step f (Definition (name, phi1, phi2)) =
blanchet@39694
   383
    Definition (name, map_term_names_in_formula f phi1,
blanchet@39694
   384
                map_term_names_in_formula f phi2)
blanchet@39694
   385
  | map_term_names_in_step f (Inference (name, phi, deps)) =
blanchet@39694
   386
    Inference (name, map_term_names_in_formula f phi, deps)
blanchet@39694
   387
fun map_term_names_in_atp_proof f = map (map_term_names_in_step f)
blanchet@39694
   388
blanchet@39694
   389
fun nasty_name pool s = s |> Symtab.lookup pool |> the_default s
blanchet@39694
   390
fun nasty_atp_proof pool =
blanchet@39694
   391
  if Symtab.is_empty pool then I
blanchet@39694
   392
  else map_term_names_in_atp_proof (nasty_name pool)
blanchet@39694
   393
blanchet@39692
   394
end;