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