src/HOL/Tools/ATP/atp_proof.ML
author blanchet
Wed, 08 Dec 2010 22:17:53 +0100
changeset 41340 1b796ffa8347
parent 40932 c7ba327eb58c
child 41394 be78f4053bce
permissions -rw-r--r--
reword error 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@39694
     6
Abstract representation of ATP proofs and TSTP/Vampire/SPASS syntax.
blanchet@39692
     7
*)
blanchet@39692
     8
blanchet@39692
     9
signature ATP_PROOF =
blanchet@39692
    10
sig
blanchet@39692
    11
  type 'a fo_term = 'a ATP_Problem.fo_term
blanchet@39693
    12
  type 'a uniform_formula = 'a ATP_Problem.uniform_formula
blanchet@39692
    13
blanchet@39731
    14
  datatype failure =
blanchet@39731
    15
    Unprovable | IncompleteUnprovable | CantConnect | TimedOut |
blanchet@39731
    16
    OutOfResources | SpassTooOld | VampireTooOld | NoPerl | NoLibwwwPerl |
blanchet@40795
    17
    MalformedInput | MalformedOutput | Interrupted | Crashed | InternalError |
blanchet@39731
    18
    UnknownError
blanchet@39731
    19
blanchet@39695
    20
  type step_name = string * string option
blanchet@39692
    21
blanchet@39693
    22
  datatype 'a step =
blanchet@39693
    23
    Definition of step_name * 'a * 'a |
blanchet@39693
    24
    Inference of step_name * 'a * step_name list
blanchet@39692
    25
blanchet@39693
    26
  type 'a proof = 'a uniform_formula step list
blanchet@39692
    27
blanchet@39697
    28
  val strip_spaces : (char -> bool) -> string -> string
blanchet@40917
    29
  val string_for_failure : string -> failure -> string
blanchet@39731
    30
  val extract_important_message : string -> string
blanchet@39731
    31
  val extract_known_failure :
blanchet@39731
    32
    (failure * string) list -> string -> failure option
blanchet@39731
    33
  val extract_tstplike_proof_and_outcome :
blanchet@39731
    34
    bool -> int -> (string * string) list -> (failure * string) list -> string
blanchet@39731
    35
    -> string * failure option
blanchet@39692
    36
  val is_same_step : step_name * step_name -> bool
blanchet@39694
    37
  val atp_proof_from_tstplike_string : string -> string proof
blanchet@39694
    38
  val map_term_names_in_atp_proof :
blanchet@39694
    39
    (string -> string) -> string proof -> string proof
blanchet@39694
    40
  val nasty_atp_proof : string Symtab.table -> string proof -> string proof
blanchet@39692
    41
end;
blanchet@39692
    42
blanchet@39692
    43
structure ATP_Proof : ATP_PROOF =
blanchet@39692
    44
struct
blanchet@39692
    45
blanchet@39731
    46
open ATP_Problem
blanchet@39731
    47
blanchet@39731
    48
datatype failure =
blanchet@39731
    49
  Unprovable | IncompleteUnprovable | CantConnect | TimedOut | OutOfResources |
blanchet@39731
    50
  SpassTooOld | VampireTooOld | NoPerl | NoLibwwwPerl | MalformedInput |
blanchet@40795
    51
  MalformedOutput | Interrupted | Crashed | InternalError | UnknownError
blanchet@39731
    52
blanchet@39692
    53
fun strip_spaces_in_list _ [] = []
blanchet@39692
    54
  | strip_spaces_in_list _ [c1] = if Char.isSpace c1 then [] else [str c1]
blanchet@39692
    55
  | strip_spaces_in_list is_evil [c1, c2] =
blanchet@39692
    56
    strip_spaces_in_list is_evil [c1] @ strip_spaces_in_list is_evil [c2]
blanchet@39692
    57
  | strip_spaces_in_list is_evil (c1 :: c2 :: c3 :: cs) =
blanchet@39692
    58
    if Char.isSpace c1 then
blanchet@39692
    59
      strip_spaces_in_list is_evil (c2 :: c3 :: cs)
blanchet@39692
    60
    else if Char.isSpace c2 then
blanchet@39692
    61
      if Char.isSpace c3 then
blanchet@39692
    62
        strip_spaces_in_list is_evil (c1 :: c3 :: cs)
blanchet@39692
    63
      else
blanchet@39692
    64
        str c1 :: (if forall is_evil [c1, c3] then [" "] else []) @
blanchet@39692
    65
        strip_spaces_in_list is_evil (c3 :: cs)
blanchet@39692
    66
    else
blanchet@39692
    67
      str c1 :: strip_spaces_in_list is_evil (c2 :: c3 :: cs)
blanchet@39692
    68
fun strip_spaces is_evil =
blanchet@39692
    69
  implode o strip_spaces_in_list is_evil o String.explode
blanchet@39692
    70
blanchet@39692
    71
fun is_ident_char c = Char.isAlphaNum c orelse c = #"_"
blanchet@39692
    72
val strip_spaces_except_between_ident_chars = strip_spaces is_ident_char
blanchet@39692
    73
blanchet@40932
    74
fun missing_message_tail prover =
blanchet@40932
    75
  " appears to be missing. You will need to install it if you want to run " ^
blanchet@40932
    76
  prover ^ "s remotely."
blanchet@39731
    77
blanchet@40917
    78
fun string_for_failure prover Unprovable =
blanchet@40917
    79
    "The " ^ prover ^ " problem is unprovable."
blanchet@40917
    80
  | string_for_failure prover IncompleteUnprovable =
blanchet@40917
    81
    "The " ^ prover ^ " cannot prove the problem."
blanchet@40917
    82
  | string_for_failure _ CantConnect = "Cannot connect to remote server."
blanchet@40917
    83
  | string_for_failure _ TimedOut = "Timed out."
blanchet@40917
    84
  | string_for_failure prover OutOfResources =
blanchet@40917
    85
    "The " ^ prover ^ " ran out of resources."
blanchet@40917
    86
  | string_for_failure _ SpassTooOld =
blanchet@39731
    87
    "Isabelle requires a more recent version of SPASS with support for the \
blanchet@39731
    88
    \TPTP syntax. To install it, download and extract the package \
blanchet@39731
    89
    \\"http://isabelle.in.tum.de/dist/contrib/spass-3.7.tar.gz\" and add the \
blanchet@39731
    90
    \\"spass-3.7\" directory's absolute path to " ^
blanchet@39731
    91
    quote (Path.implode (Path.expand (Path.appends
blanchet@39731
    92
               (Path.variable "ISABELLE_HOME_USER" ::
blanchet@39731
    93
                map Path.basic ["etc", "components"])))) ^
blanchet@39731
    94
    " on a line of its own."
blanchet@40917
    95
  | string_for_failure _ VampireTooOld =
blanchet@39731
    96
    "Isabelle requires a more recent version of Vampire. To install it, follow \
blanchet@39731
    97
    \the instructions from the Sledgehammer manual (\"isabelle doc\
blanchet@39731
    98
    \ sledgehammer\")."
blanchet@40932
    99
  | string_for_failure prover NoPerl = "Perl" ^ missing_message_tail prover
blanchet@40932
   100
  | string_for_failure prover NoLibwwwPerl =
blanchet@40932
   101
    "The Perl module \"libwww-perl\"" ^ missing_message_tail prover
blanchet@40917
   102
  | string_for_failure prover MalformedInput =
blanchet@40917
   103
    "The " ^ prover ^ " problem is malformed. Please report this to the Isabelle \
blanchet@39731
   104
    \developers."
blanchet@40917
   105
  | string_for_failure prover MalformedOutput =
blanchet@40917
   106
    "The " ^ prover ^ " output is malformed."
blanchet@40917
   107
  | string_for_failure prover Interrupted = "The " ^ prover ^ " was interrupted."
blanchet@40917
   108
  | string_for_failure prover Crashed = "The " ^ prover ^ " crashed."
blanchet@40917
   109
  | string_for_failure prover InternalError =
blanchet@40917
   110
    "An internal " ^ prover ^ " error occurred."
blanchet@40917
   111
  | string_for_failure prover UnknownError =
blanchet@41340
   112
    (* "An" is correct for "ATP" and "SMT". *)
blanchet@41340
   113
    "An " ^ prover ^ " error occurred."
blanchet@39731
   114
blanchet@39731
   115
fun extract_delimited (begin_delim, end_delim) output =
blanchet@39731
   116
  output |> first_field begin_delim |> the |> snd
blanchet@39731
   117
         |> first_field end_delim |> the |> fst
blanchet@39731
   118
         |> first_field "\n" |> the |> snd
blanchet@39731
   119
  handle Option.Option => ""
blanchet@39731
   120
blanchet@39731
   121
val tstp_important_message_delims =
blanchet@39731
   122
  ("% SZS start RequiredInformation", "% SZS end RequiredInformation")
blanchet@39731
   123
blanchet@39731
   124
fun extract_important_message output =
blanchet@39731
   125
  case extract_delimited tstp_important_message_delims output of
blanchet@39731
   126
    "" => ""
blanchet@39731
   127
  | s => s |> space_explode "\n" |> filter_out (curry (op =) "")
blanchet@39731
   128
           |> map (perhaps (try (unprefix "%")))
blanchet@39731
   129
           |> map (perhaps (try (unprefix " ")))
blanchet@39731
   130
           |> space_implode "\n " |> quote
blanchet@39731
   131
blanchet@39731
   132
(* Splits by the first possible of a list of delimiters. *)
blanchet@39731
   133
fun extract_tstplike_proof delims output =
blanchet@39731
   134
  case pairself (find_first (fn s => String.isSubstring s output))
blanchet@39731
   135
                (ListPair.unzip delims) of
blanchet@39731
   136
    (SOME begin_delim, SOME end_delim) =>
blanchet@39731
   137
    extract_delimited (begin_delim, end_delim) output
blanchet@39731
   138
  | _ => ""
blanchet@39731
   139
blanchet@39731
   140
fun extract_known_failure known_failures output =
blanchet@39731
   141
  known_failures
blanchet@39731
   142
  |> find_first (fn (_, pattern) => String.isSubstring pattern output)
blanchet@39731
   143
  |> Option.map fst
blanchet@39731
   144
blanchet@39731
   145
fun extract_tstplike_proof_and_outcome complete res_code proof_delims
blanchet@39731
   146
                                       known_failures output =
blanchet@39731
   147
  case extract_known_failure known_failures output of
blanchet@39731
   148
    NONE => (case extract_tstplike_proof proof_delims output of
blanchet@39731
   149
             "" => ("", SOME (if res_code = 0 andalso output = "" then
blanchet@39731
   150
                                Interrupted
blanchet@39731
   151
                              else
blanchet@39731
   152
                                UnknownError))
blanchet@39731
   153
           | tstplike_proof => if res_code = 0 then (tstplike_proof, NONE)
blanchet@39731
   154
                               else ("", SOME UnknownError))
blanchet@39731
   155
  | SOME failure =>
blanchet@39731
   156
    ("", SOME (if failure = IncompleteUnprovable andalso complete then
blanchet@39731
   157
                 Unprovable
blanchet@39731
   158
               else
blanchet@39731
   159
                 failure))
blanchet@39692
   160
blanchet@39692
   161
fun mk_anot (AConn (ANot, [phi])) = phi
blanchet@39692
   162
  | mk_anot phi = AConn (ANot, [phi])
blanchet@39692
   163
fun mk_aconn c (phi1, phi2) = AConn (c, [phi1, phi2])
blanchet@39692
   164
blanchet@39695
   165
type step_name = string * string option
blanchet@39692
   166
blanchet@39695
   167
fun is_same_step p = p |> pairself fst |> op =
blanchet@39692
   168
blanchet@39692
   169
fun step_name_ord p =
blanchet@39695
   170
  let val q = pairself fst p in
blanchet@39692
   171
    (* The "unprefix" part is to cope with remote Vampire's output. The proper
blanchet@39692
   172
       solution would be to perform a topological sort, e.g. using the nice
blanchet@39692
   173
       "Graph" functor. *)
blanchet@39692
   174
    case pairself (Int.fromString o perhaps (try (unprefix "f"))) q of
blanchet@39692
   175
      (NONE, NONE) => string_ord q
blanchet@39692
   176
    | (NONE, SOME _) => LESS
blanchet@39692
   177
    | (SOME _, NONE) => GREATER
blanchet@39692
   178
    | (SOME i, SOME j) => int_ord (i, j)
blanchet@39692
   179
  end
blanchet@39692
   180
blanchet@39693
   181
datatype 'a step =
blanchet@39693
   182
  Definition of step_name * 'a * 'a |
blanchet@39693
   183
  Inference of step_name * 'a * step_name list
blanchet@39692
   184
blanchet@39693
   185
type 'a proof = 'a uniform_formula step list
blanchet@39692
   186
blanchet@39692
   187
fun step_name (Definition (name, _, _)) = name
blanchet@39692
   188
  | step_name (Inference (name, _, _)) = name
blanchet@39692
   189
blanchet@39692
   190
(**** PARSING OF TSTP FORMAT ****)
blanchet@39692
   191
blanchet@39692
   192
(*Strings enclosed in single quotes, e.g. filenames*)
blanchet@39692
   193
val scan_general_id =
blanchet@39692
   194
  $$ "'" |-- Scan.repeat (~$$ "'") --| $$ "'" >> implode
blanchet@39692
   195
  || Scan.repeat ($$ "$") -- Scan.many1 Symbol.is_letdig
blanchet@39692
   196
     >> (fn (ss1, ss2) => implode ss1 ^ implode ss2)
blanchet@39692
   197
blanchet@39692
   198
(* Generalized first-order terms, which include file names, numbers, etc. *)
blanchet@39692
   199
fun parse_annotation strict x =
blanchet@39692
   200
  ((scan_general_id ::: Scan.repeat ($$ " " |-- scan_general_id)
blanchet@39692
   201
      >> (strict ? filter (is_some o Int.fromString)))
blanchet@39692
   202
   -- Scan.optional (parse_annotation strict) [] >> op @
blanchet@39692
   203
   || $$ "(" |-- parse_annotations strict --| $$ ")"
blanchet@39692
   204
   || $$ "[" |-- parse_annotations strict --| $$ "]") x
blanchet@39692
   205
and parse_annotations strict x =
blanchet@39692
   206
  (Scan.optional (parse_annotation strict
blanchet@39692
   207
                  ::: Scan.repeat ($$ "," |-- parse_annotation strict)) []
blanchet@39692
   208
   >> flat) x
blanchet@39692
   209
blanchet@39692
   210
(* Vampire proof lines sometimes contain needless information such as "(0:3)",
blanchet@39692
   211
   which can be hard to disambiguate from function application in an LL(1)
blanchet@39692
   212
   parser. As a workaround, we extend the TPTP term syntax with such detritus
blanchet@39692
   213
   and ignore it. *)
blanchet@39692
   214
fun parse_vampire_detritus x =
blanchet@39692
   215
  (scan_general_id |-- $$ ":" --| scan_general_id >> K []) x
blanchet@39692
   216
blanchet@39694
   217
fun parse_term x =
blanchet@39694
   218
  (scan_general_id
blanchet@39694
   219
    -- Scan.optional ($$ "(" |-- (parse_vampire_detritus || parse_terms)
blanchet@39692
   220
                      --| $$ ")") []
blanchet@39692
   221
    --| Scan.optional ($$ "(" |-- parse_vampire_detritus --| $$ ")") []
blanchet@39692
   222
   >> ATerm) x
blanchet@39694
   223
and parse_terms x = (parse_term ::: Scan.repeat ($$ "," |-- parse_term)) x
blanchet@39692
   224
blanchet@39822
   225
fun parse_atom x =
blanchet@39822
   226
  (parse_term -- Scan.option (Scan.option ($$ "!") --| $$ "=" -- parse_term)
blanchet@39822
   227
   >> (fn (u1, NONE) => AAtom u1
blanchet@39822
   228
        | (u1, SOME (NONE, u2)) => AAtom (ATerm ("c_equal", [u1, u2]))
blanchet@39822
   229
        | (u1, SOME (SOME _, u2)) =>
blanchet@39822
   230
          mk_anot (AAtom (ATerm ("c_equal", [u1, u2]))))) x
blanchet@39692
   231
blanchet@39692
   232
fun fo_term_head (ATerm (s, _)) = s
blanchet@39692
   233
blanchet@39692
   234
(* TPTP formulas are fully parenthesized, so we don't need to worry about
blanchet@39692
   235
   operator precedence. *)
blanchet@39694
   236
fun parse_formula x =
blanchet@39694
   237
  (($$ "(" |-- parse_formula --| $$ ")"
blanchet@39692
   238
    || ($$ "!" >> K AForall || $$ "?" >> K AExists)
blanchet@39694
   239
       --| $$ "[" -- parse_terms --| $$ "]" --| $$ ":" -- parse_formula
blanchet@39692
   240
       >> (fn ((q, ts), phi) => AQuant (q, map fo_term_head ts, phi))
blanchet@39694
   241
    || $$ "~" |-- parse_formula >> mk_anot
blanchet@39694
   242
    || parse_atom)
blanchet@39692
   243
   -- Scan.option ((Scan.this_string "=>" >> K AImplies
blanchet@39692
   244
                    || Scan.this_string "<=>" >> K AIff
blanchet@39692
   245
                    || Scan.this_string "<~>" >> K ANotIff
blanchet@39692
   246
                    || Scan.this_string "<=" >> K AIf
blanchet@39692
   247
                    || $$ "|" >> K AOr || $$ "&" >> K AAnd)
blanchet@39694
   248
                   -- parse_formula)
blanchet@39692
   249
   >> (fn (phi1, NONE) => phi1
blanchet@39692
   250
        | (phi1, SOME (c, phi2)) => mk_aconn c (phi1, phi2))) x
blanchet@39692
   251
blanchet@39692
   252
val parse_tstp_extra_arguments =
blanchet@39692
   253
  Scan.optional ($$ "," |-- parse_annotation false
blanchet@39692
   254
                 --| Scan.option ($$ "," |-- parse_annotations false)) []
blanchet@39692
   255
blanchet@39692
   256
(* Syntax: (fof|cnf)\(<num>, <formula_role>, <formula> <extra_arguments>\).
blanchet@39692
   257
   The <num> could be an identifier, but we assume integers. *)
blanchet@39694
   258
val parse_tstp_line =
blanchet@39694
   259
  ((Scan.this_string "fof" || Scan.this_string "cnf") -- $$ "(")
blanchet@39694
   260
    |-- scan_general_id --| $$ "," -- Symbol.scan_id --| $$ ","
blanchet@39694
   261
    -- parse_formula -- parse_tstp_extra_arguments --| $$ ")" --| $$ "."
blanchet@39694
   262
   >> (fn (((num, role), phi), deps) =>
blanchet@39694
   263
          let
blanchet@39694
   264
            val (name, deps) =
blanchet@39694
   265
              case deps of
blanchet@39695
   266
                ["file", _, s] => ((num, SOME s), [])
blanchet@39695
   267
              | _ => ((num, NONE), deps)
blanchet@39694
   268
          in
blanchet@39694
   269
            case role of
blanchet@39694
   270
              "definition" =>
blanchet@39694
   271
              (case phi of
blanchet@39694
   272
                 AConn (AIff, [phi1 as AAtom _, phi2]) =>
blanchet@39694
   273
                 Definition (name, phi1, phi2)
blanchet@39694
   274
               | AAtom (ATerm ("c_equal", _)) =>
blanchet@39694
   275
                 (* Vampire's equality proxy axiom *)
blanchet@39695
   276
                 Inference (name, phi, map (rpair NONE) deps)
blanchet@39694
   277
               | _ => raise Fail "malformed definition")
blanchet@39695
   278
            | _ => Inference (name, phi, map (rpair NONE) deps)
blanchet@39694
   279
          end)
blanchet@39692
   280
blanchet@39692
   281
(**** PARSING OF VAMPIRE OUTPUT ****)
blanchet@39692
   282
blanchet@39692
   283
(* Syntax: <num>. <formula> <annotation> *)
blanchet@39694
   284
val parse_vampire_line =
blanchet@39694
   285
  scan_general_id --| $$ "." -- parse_formula -- parse_annotation true
blanchet@39695
   286
  >> (fn ((num, phi), deps) =>
blanchet@39695
   287
         Inference ((num, NONE), phi, map (rpair NONE) deps))
blanchet@39692
   288
blanchet@39692
   289
(**** PARSING OF SPASS OUTPUT ****)
blanchet@39692
   290
blanchet@39692
   291
(* SPASS returns clause references of the form "x.y". We ignore "y", whose role
blanchet@39692
   292
   is not clear anyway. *)
blanchet@39692
   293
val parse_dot_name = scan_general_id --| $$ "." --| scan_general_id
blanchet@39692
   294
blanchet@39692
   295
val parse_spass_annotations =
blanchet@39692
   296
  Scan.optional ($$ ":" |-- Scan.repeat (parse_dot_name
blanchet@39692
   297
                                         --| Scan.option ($$ ","))) []
blanchet@39692
   298
blanchet@39692
   299
(* It is not clear why some literals are followed by sequences of stars and/or
blanchet@39692
   300
   pluses. We ignore them. *)
blanchet@39826
   301
fun parse_decorated_atom x =
blanchet@39826
   302
  (parse_atom --| Scan.repeat ($$ "*" || $$ "+" || $$ " ")) x
blanchet@39692
   303
blanchet@39692
   304
fun mk_horn ([], []) = AAtom (ATerm ("c_False", []))
blanchet@39692
   305
  | mk_horn ([], pos_lits) = foldr1 (mk_aconn AOr) pos_lits
blanchet@39692
   306
  | mk_horn (neg_lits, []) = mk_anot (foldr1 (mk_aconn AAnd) neg_lits)
blanchet@39692
   307
  | mk_horn (neg_lits, pos_lits) =
blanchet@39692
   308
    mk_aconn AImplies (foldr1 (mk_aconn AAnd) neg_lits,
blanchet@39692
   309
                       foldr1 (mk_aconn AOr) pos_lits)
blanchet@39692
   310
blanchet@39869
   311
fun parse_horn_clause x =
blanchet@39869
   312
  (Scan.repeat parse_decorated_atom --| $$ "|" --| $$ "|"
blanchet@39869
   313
     -- Scan.repeat parse_decorated_atom --| $$ "-" --| $$ ">"
blanchet@39869
   314
     -- Scan.repeat parse_decorated_atom
blanchet@39869
   315
   >> (mk_horn o apfst (op @))) x
blanchet@39692
   316
blanchet@39692
   317
(* Syntax: <num>[0:<inference><annotations>]
blanchet@39692
   318
   <atoms> || <atoms> -> <atoms>. *)
blanchet@39869
   319
fun parse_spass_line x =
blanchet@39869
   320
  (scan_general_id --| $$ "[" --| $$ "0" --| $$ ":" --| Symbol.scan_id
blanchet@39869
   321
     -- parse_spass_annotations --| $$ "]" -- parse_horn_clause --| $$ "."
blanchet@39869
   322
   >> (fn ((num, deps), u) =>
blanchet@39869
   323
          Inference ((num, NONE), u, map (rpair NONE) deps))) x
blanchet@39692
   324
blanchet@39869
   325
fun parse_line x = (parse_tstp_line || parse_vampire_line || parse_spass_line) x
blanchet@39694
   326
val parse_proof =
blanchet@39692
   327
  fst o Scan.finite Symbol.stopper
blanchet@39692
   328
            (Scan.error (!! (fn _ => raise Fail "unrecognized ATP output")
blanchet@39694
   329
                            (Scan.repeat1 parse_line)))
wenzelm@40875
   330
  o raw_explode o strip_spaces_except_between_ident_chars
blanchet@39692
   331
blanchet@39692
   332
fun clean_up_dependency seen dep = find_first (curry is_same_step dep) seen
blanchet@39692
   333
fun clean_up_dependencies _ [] = []
blanchet@39692
   334
  | clean_up_dependencies seen ((step as Definition (name, _, _)) :: steps) =
blanchet@39692
   335
    step :: clean_up_dependencies (name :: seen) steps
blanchet@39692
   336
  | clean_up_dependencies seen (Inference (name, u, deps) :: steps) =
blanchet@39692
   337
    Inference (name, u, map_filter (clean_up_dependency seen) deps) ::
blanchet@39692
   338
    clean_up_dependencies (name :: seen) steps
blanchet@39692
   339
blanchet@39694
   340
val atp_proof_from_tstplike_string =
blanchet@39692
   341
  suffix "$" (* the $ sign acts as a sentinel (FIXME: needed?) *)
blanchet@39694
   342
  #> parse_proof
blanchet@39692
   343
  #> sort (step_name_ord o pairself step_name)
blanchet@39692
   344
  #> clean_up_dependencies []
blanchet@39692
   345
blanchet@39694
   346
fun map_term_names_in_term f (ATerm (s, ts)) =
blanchet@39694
   347
  ATerm (f s, map (map_term_names_in_term f) ts)
blanchet@39694
   348
fun map_term_names_in_formula f (AQuant (q, xs, phi)) =
blanchet@39694
   349
    AQuant (q, xs, map_term_names_in_formula f phi)
blanchet@39694
   350
  | map_term_names_in_formula f (AConn (c, phis)) =
blanchet@39694
   351
    AConn (c, map (map_term_names_in_formula f) phis)
blanchet@39694
   352
  | map_term_names_in_formula f (AAtom t) = AAtom (map_term_names_in_term f t)
blanchet@39694
   353
fun map_term_names_in_step f (Definition (name, phi1, phi2)) =
blanchet@39694
   354
    Definition (name, map_term_names_in_formula f phi1,
blanchet@39694
   355
                map_term_names_in_formula f phi2)
blanchet@39694
   356
  | map_term_names_in_step f (Inference (name, phi, deps)) =
blanchet@39694
   357
    Inference (name, map_term_names_in_formula f phi, deps)
blanchet@39694
   358
fun map_term_names_in_atp_proof f = map (map_term_names_in_step f)
blanchet@39694
   359
blanchet@39694
   360
fun nasty_name pool s = s |> Symtab.lookup pool |> the_default s
blanchet@39694
   361
fun nasty_atp_proof pool =
blanchet@39694
   362
  if Symtab.is_empty pool then I
blanchet@39694
   363
  else map_term_names_in_atp_proof (nasty_name pool)
blanchet@39694
   364
blanchet@39692
   365
end;