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