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