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