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