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