src/HOL/Tools/ATP/atp_problem.ML
author nik
Tue, 05 Jul 2011 17:09:59 +0100
changeset 44536 2cd0b478d1b6
parent 44535 3b0b448b4d69
child 44537 56d352659500
permissions -rw-r--r--
added generation of lambdas in THF
     1 (*  Title:      HOL/Tools/ATP/atp_problem.ML
     2     Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
     3     Author:     Jasmin Blanchette, TU Muenchen
     4 
     5 Abstract representation of ATP problems and TPTP syntax.
     6 *)
     7 
     8 signature ATP_PROBLEM =
     9 sig
    10   datatype ('a, 'b) ho_term =
    11     ATerm of 'a * ('a, 'b) ho_term list |
    12     AAbs of ('a * 'b) * ('a, 'b) ho_term
    13   datatype quantifier = AForall | AExists
    14   datatype connective = ANot | AAnd | AOr | AImplies | AIff
    15   datatype ('a, 'b, 'c) formula =
    16     AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
    17     AConn of connective * ('a, 'b, 'c) formula list |
    18     AAtom of 'c
    19 
    20   datatype 'a ho_type = AType of 'a | AFun of 'a ho_type * 'a ho_type
    21 
    22   datatype format = CNF | CNF_UEQ | FOF | TFF | THF
    23   datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
    24   datatype 'a problem_line =
    25     Decl of string * 'a * 'a ho_type |
    26     Formula of string * formula_kind * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
    27                * (string, string ho_type) ho_term option * (string, string ho_type) ho_term option
    28   type 'a problem = (string * 'a problem_line list) list
    29 
    30   val tptp_cnf : string
    31   val tptp_fof : string
    32   val tptp_tff : string
    33   val tptp_thf : string
    34   val tptp_has_type : string
    35   val tptp_type_of_types : string
    36   val tptp_bool_type : string
    37   val tptp_individual_type : string
    38   val tptp_fun_type : string
    39   val tptp_product_type : string
    40   val tptp_forall : string
    41   val tptp_exists : string
    42   val tptp_not : string
    43   val tptp_and : string
    44   val tptp_or : string
    45   val tptp_implies : string
    46   val tptp_if : string
    47   val tptp_iff : string
    48   val tptp_not_iff : string
    49   val tptp_app : string
    50   val tptp_not_infix : string
    51   val tptp_equal : string
    52   val tptp_old_equal : string
    53   val tptp_false : string
    54   val tptp_true : string
    55   val tptp_empty_list : string
    56   val is_tptp_equal : string -> bool
    57   val is_built_in_tptp_symbol : string -> bool
    58   val is_tptp_variable : string -> bool
    59   val is_tptp_user_symbol : string -> bool
    60   val mk_anot : ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
    61   val mk_aconn :
    62     connective -> ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
    63     -> ('a, 'b, 'c) formula
    64   val aconn_fold :
    65     bool option -> (bool option -> 'a -> 'b -> 'b) -> connective * 'a list
    66     -> 'b -> 'b
    67   val aconn_map :
    68     bool option -> (bool option -> 'a -> ('b, 'c, 'd) formula)
    69     -> connective * 'a list -> ('b, 'c, 'd) formula
    70   val formula_fold :
    71     bool option -> (bool option -> 'c -> 'd -> 'd) -> ('a, 'b, 'c) formula
    72     -> 'd -> 'd
    73   val formula_map : ('c -> 'd) -> ('a, 'b, 'c) formula -> ('a, 'b, 'd) formula
    74   val is_format_typed : format -> bool
    75   val tptp_lines_for_atp_problem : format -> string problem -> string list
    76   val ensure_cnf_problem :
    77     (string * string) problem -> (string * string) problem
    78   val filter_cnf_ueq_problem :
    79     (string * string) problem -> (string * string) problem
    80   val declare_undeclared_syms_in_atp_problem :
    81     string -> string -> (string * string) problem -> (string * string) problem
    82   val nice_atp_problem :
    83     bool -> ('a * (string * string) problem_line list) list
    84     -> ('a * string problem_line list) list
    85        * (string Symtab.table * string Symtab.table) option
    86 end;
    87 
    88 structure ATP_Problem : ATP_PROBLEM =
    89 struct
    90 
    91 open ATP_Util
    92 
    93 
    94 (** ATP problem **)
    95 
    96 datatype ('a, 'b) ho_term =
    97   ATerm of 'a * ('a, 'b) ho_term list |
    98   AAbs of ('a * 'b) * ('a, 'b) ho_term
    99 datatype quantifier = AForall | AExists
   100 datatype connective = ANot | AAnd | AOr | AImplies | AIff
   101 datatype ('a, 'b, 'c) formula =
   102   AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
   103   AConn of connective * ('a, 'b, 'c) formula list |
   104   AAtom of 'c
   105 
   106 datatype 'a ho_type = AType of 'a | AFun of 'a ho_type * 'a ho_type
   107 
   108 datatype format = CNF | CNF_UEQ | FOF | TFF | THF
   109 datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
   110 datatype 'a problem_line =
   111   Decl of string * 'a * 'a ho_type |
   112   Formula of string * formula_kind * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
   113              * (string, string ho_type) ho_term option * (string, string ho_type) ho_term option
   114 type 'a problem = (string * 'a problem_line list) list
   115 
   116 (* official TPTP syntax *)
   117 val tptp_cnf = "cnf"
   118 val tptp_fof = "fof"
   119 val tptp_tff = "tff"
   120 val tptp_thf = "thf"
   121 val tptp_has_type = ":"
   122 val tptp_type_of_types = "$tType"
   123 val tptp_bool_type = "$o"
   124 val tptp_individual_type = "$i"
   125 val tptp_fun_type = ">"
   126 val tptp_product_type = "*"
   127 val tptp_forall = "!"
   128 val tptp_exists = "?"
   129 val tptp_not = "~"
   130 val tptp_and = "&"
   131 val tptp_or = "|"
   132 val tptp_implies = "=>"
   133 val tptp_if = "<="
   134 val tptp_iff = "<=>"
   135 val tptp_not_iff = "<~>"
   136 val tptp_app = "@"
   137 val tptp_not_infix = "!"
   138 val tptp_equal = "="
   139 val tptp_old_equal = "equal"
   140 val tptp_false = "$false"
   141 val tptp_true = "$true"
   142 val tptp_empty_list = "[]"
   143 
   144 fun is_tptp_equal s = (s = tptp_equal orelse s = tptp_old_equal)
   145 fun is_built_in_tptp_symbol s =
   146   s = tptp_old_equal orelse not (Char.isAlpha (String.sub (s, 0)))
   147 fun is_tptp_variable s = Char.isUpper (String.sub (s, 0))
   148 val is_tptp_user_symbol = not o (is_tptp_variable orf is_built_in_tptp_symbol)
   149 
   150 fun raw_polarities_of_conn ANot = (SOME false, NONE)
   151   | raw_polarities_of_conn AAnd = (SOME true, SOME true)
   152   | raw_polarities_of_conn AOr = (SOME true, SOME true)
   153   | raw_polarities_of_conn AImplies = (SOME false, SOME true)
   154   | raw_polarities_of_conn AIff = (NONE, NONE)
   155 fun polarities_of_conn NONE = K (NONE, NONE)
   156   | polarities_of_conn (SOME pos) =
   157     raw_polarities_of_conn #> not pos ? pairself (Option.map not)
   158 
   159 fun mk_anot (AConn (ANot, [phi])) = phi
   160   | mk_anot phi = AConn (ANot, [phi])
   161 fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
   162 
   163 fun aconn_fold pos f (ANot, [phi]) = f (Option.map not pos) phi
   164   | aconn_fold pos f (AImplies, [phi1, phi2]) =
   165     f (Option.map not pos) phi1 #> f pos phi2
   166   | aconn_fold pos f (AAnd, phis) = fold (f pos) phis
   167   | aconn_fold pos f (AOr, phis) = fold (f pos) phis
   168   | aconn_fold _ f (_, phis) = fold (f NONE) phis
   169 
   170 fun aconn_map pos f (ANot, [phi]) = AConn (ANot, [f (Option.map not pos) phi])
   171   | aconn_map pos f (AImplies, [phi1, phi2]) =
   172     AConn (AImplies, [f (Option.map not pos) phi1, f pos phi2])
   173   | aconn_map pos f (AAnd, phis) = AConn (AAnd, map (f pos) phis)
   174   | aconn_map pos f (AOr, phis) = AConn (AOr, map (f pos) phis)
   175   | aconn_map _ f (c, phis) = AConn (c, map (f NONE) phis)
   176 
   177 fun formula_fold pos f =
   178   let
   179     fun aux pos (AQuant (_, _, phi)) = aux pos phi
   180       | aux pos (AConn conn) = aconn_fold pos aux conn
   181       | aux pos (AAtom tm) = f pos tm
   182   in aux pos end
   183 
   184 fun formula_map f (AQuant (q, xs, phi)) = AQuant (q, xs, formula_map f phi)
   185   | formula_map f (AConn (c, phis)) = AConn (c, map (formula_map f) phis)
   186   | formula_map f (AAtom tm) = AAtom (f tm)
   187 
   188 val is_format_typed = member (op =) [TFF, THF]
   189 
   190 fun string_for_kind Axiom = "axiom"
   191   | string_for_kind Definition = "definition"
   192   | string_for_kind Lemma = "lemma"
   193   | string_for_kind Hypothesis = "hypothesis"
   194   | string_for_kind Conjecture = "conjecture"
   195 
   196 fun strip_tff_type (AFun (AType s, ty)) = strip_tff_type ty |>> cons s
   197   | strip_tff_type (AFun (AFun _, _)) =
   198     raise Fail "unexpected higher-order type in first-order format"
   199   | strip_tff_type (AType s) = ([], s)
   200 
   201 fun string_for_type THF ty =
   202     let
   203       fun aux _ (AType s) = s
   204         | aux rhs (AFun (ty1, ty2)) =
   205           aux false ty1 ^ " " ^ tptp_fun_type ^ " " ^ aux true ty2
   206           |> not rhs ? enclose "(" ")"
   207     in aux true ty end
   208   | string_for_type TFF ty =
   209     (case strip_tff_type ty of
   210        ([], s) => s
   211      | ([s'], s) => s' ^ " " ^ tptp_fun_type ^ " " ^ s
   212      | (ss, s) =>
   213        "(" ^ space_implode (" " ^ tptp_product_type ^ " ") ss ^ ") " ^
   214        tptp_fun_type ^ " " ^ s)
   215   | string_for_type _ _ = raise Fail "unexpected type in untyped format"
   216 
   217 fun string_for_term _ (ATerm (s, [])) = s
   218   | string_for_term format (ATerm (s, ts)) =
   219     if s = tptp_empty_list then
   220       (* used for lists in the optional "source" field of a derivation *)
   221       "[" ^ commas (map (string_for_term format) ts) ^ "]"
   222     else if is_tptp_equal s then
   223       space_implode (" " ^ tptp_equal ^ " ") (map (string_for_term format) ts)
   224       |> format = THF ? enclose "(" ")"
   225     else
   226       let val ss = map (string_for_term format) ts in
   227         if format = THF then
   228           "(" ^ space_implode (" " ^ tptp_app ^ " ") (s :: ss) ^ ")"
   229         else
   230           s ^ "(" ^ commas ss ^ ")"
   231       end
   232   | string_for_term THF (AAbs ((s, ty), tm)) =
   233     "(^[" ^ s ^ ":" ^ string_for_type THF ty ^ "] : " ^ string_for_term THF tm ^ ")"
   234   | string_for_term _ _ = raise Fail "unexpected term in first-order format"
   235 
   236 fun string_for_quantifier AForall = tptp_forall
   237   | string_for_quantifier AExists = tptp_exists
   238 
   239 fun string_for_connective ANot = tptp_not
   240   | string_for_connective AAnd = tptp_and
   241   | string_for_connective AOr = tptp_or
   242   | string_for_connective AImplies = tptp_implies
   243   | string_for_connective AIff = tptp_iff
   244 
   245 fun string_for_bound_var format (s, ty) =
   246   s ^ (if format = TFF orelse format = THF then
   247          " " ^ tptp_has_type ^ " " ^
   248          string_for_type format (ty |> the_default (AType tptp_individual_type))
   249        else
   250          "")
   251 
   252 fun string_for_formula format (AQuant (q, xs, phi)) =
   253     string_for_quantifier q ^
   254     "[" ^ commas (map (string_for_bound_var format) xs) ^ "] : " ^
   255     string_for_formula format phi
   256     |> enclose "(" ")"
   257   | string_for_formula format
   258         (AConn (ANot, [AAtom (ATerm ("=" (* tptp_equal *), ts))])) =
   259     space_implode (" " ^ tptp_not_infix ^ tptp_equal ^ " ")
   260                   (map (string_for_term format) ts)
   261     |> format = THF ? enclose "(" ")"
   262   | string_for_formula format (AConn (c, [phi])) =
   263     string_for_connective c ^ " " ^
   264     (string_for_formula format phi |> format = THF ? enclose "(" ")")
   265     |> enclose "(" ")"
   266   | string_for_formula format (AConn (c, phis)) =
   267     space_implode (" " ^ string_for_connective c ^ " ")
   268                   (map (string_for_formula format) phis)
   269     |> enclose "(" ")"
   270   | string_for_formula format (AAtom tm) = string_for_term format tm
   271 
   272 val default_source =
   273   ATerm ("inference", ATerm ("isabelle", []) :: replicate 2 (ATerm ("[]", [])))
   274 
   275 fun string_for_format CNF = tptp_cnf
   276   | string_for_format CNF_UEQ = tptp_cnf
   277   | string_for_format FOF = tptp_fof
   278   | string_for_format TFF = tptp_tff
   279   | string_for_format THF = tptp_thf
   280 
   281 fun string_for_problem_line format (Decl (ident, sym, ty)) =
   282     string_for_format format ^ "(" ^ ident ^ ", type,\n    " ^ sym ^ " : " ^
   283     string_for_type format ty ^ ").\n"
   284   | string_for_problem_line format (Formula (ident, kind, phi, source, info)) =
   285     string_for_format format ^ "(" ^ ident ^ ", " ^ string_for_kind kind ^
   286     ",\n    (" ^ string_for_formula format phi ^ ")" ^
   287     (case (source, info) of
   288        (NONE, NONE) => ""
   289      | (SOME tm, NONE) => ", " ^ string_for_term format tm
   290      | (_, SOME tm) =>
   291        ", " ^ string_for_term format (source |> the_default default_source) ^
   292        ", " ^ string_for_term format tm) ^ ").\n"
   293 fun tptp_lines_for_atp_problem format problem =
   294   "% This file was generated by Isabelle (most likely Sledgehammer)\n\
   295   \% " ^ timestamp () ^ "\n" ::
   296   maps (fn (_, []) => []
   297          | (heading, lines) =>
   298            "\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
   299            map (string_for_problem_line format) lines)
   300        problem
   301 
   302 
   303 (** CNF (Metis) and CNF UEQ (Waldmeister) **)
   304 
   305 fun is_problem_line_negated (Formula (_, _, AConn (ANot, _), _, _)) = true
   306   | is_problem_line_negated _ = false
   307 
   308 fun is_problem_line_cnf_ueq (Formula (_, _, AAtom (ATerm ((s, _), _)), _, _)) =
   309     is_tptp_equal s
   310   | is_problem_line_cnf_ueq _ = false
   311 
   312 fun open_conjecture_term (ATerm ((s, s'), tms)) =
   313     ATerm (if is_tptp_variable s then (s |> Name.desymbolize false, s')
   314            else (s, s'), tms |> map open_conjecture_term)
   315   | open_conjecture_term _ = raise Fail "unexpected higher-order term"
   316 fun open_formula conj =
   317   let
   318     (* We are conveniently assuming that all bound variable names are
   319        distinct, which should be the case for the formulas we generate. *)
   320     fun opn (pos as SOME true) (AQuant (AForall, _, phi)) = opn pos phi
   321       | opn (pos as SOME false) (AQuant (AExists, _, phi)) = opn pos phi
   322       | opn pos (AConn (ANot, [phi])) = mk_anot (opn (Option.map not pos) phi)
   323       | opn pos (AConn (c, [phi1, phi2])) =
   324         let val (pos1, pos2) = polarities_of_conn pos c in
   325           AConn (c, [opn pos1 phi1, opn pos2 phi2])
   326         end
   327       | opn _ (AAtom t) = AAtom (t |> conj ? open_conjecture_term)
   328       | opn _ phi = phi
   329   in opn (SOME (not conj)) end
   330 fun open_formula_line (Formula (ident, kind, phi, source, info)) =
   331     Formula (ident, kind, open_formula (kind = Conjecture) phi, source, info)
   332   | open_formula_line line = line
   333 
   334 fun negate_conjecture_line (Formula (ident, Conjecture, phi, source, info)) =
   335     Formula (ident, Hypothesis, mk_anot phi, source, info)
   336   | negate_conjecture_line line = line
   337 
   338 exception CLAUSIFY of unit
   339 
   340 (* This "clausification" only expands syntactic sugar, such as "phi => psi" to
   341    "~ phi | psi" and "phi <=> psi" to "~ phi | psi" and "~ psi | phi". We don't
   342    attempt to distribute conjunctions over disjunctions. *)
   343 fun clausify_formula pos (phi as AAtom _) = [phi |> not pos ? mk_anot]
   344   | clausify_formula pos (AConn (ANot, [phi])) = clausify_formula (not pos) phi
   345   | clausify_formula true (AConn (AOr, [phi1, phi2])) =
   346     (phi1, phi2) |> pairself (clausify_formula true)
   347                  |> uncurry (map_product (mk_aconn AOr))
   348   | clausify_formula false (AConn (AAnd, [phi1, phi2])) =
   349     (phi1, phi2) |> pairself (clausify_formula false)
   350                  |> uncurry (map_product (mk_aconn AOr))
   351   | clausify_formula true (AConn (AImplies, [phi1, phi2])) =
   352     clausify_formula true (AConn (AOr, [mk_anot phi1, phi2]))
   353   | clausify_formula true (AConn (AIff, phis)) =
   354     clausify_formula true (AConn (AImplies, phis)) @
   355     clausify_formula true (AConn (AImplies, rev phis))
   356   | clausify_formula _ _ = raise CLAUSIFY ()
   357 
   358 fun clausify_formula_line (Formula (ident, kind, phi, source, info)) =
   359     let
   360       val (n, phis) = phi |> try (clausify_formula true) |> these |> `length
   361     in
   362       map2 (fn phi => fn j =>
   363                Formula (ident ^ replicate_string (j - 1) "x", kind, phi, source,
   364                         info))
   365            phis (1 upto n)
   366     end
   367   | clausify_formula_line _ = []
   368 
   369 fun ensure_cnf_problem_line line =
   370   line |> open_formula_line |> negate_conjecture_line |> clausify_formula_line
   371 
   372 fun ensure_cnf_problem problem =
   373   problem |> map (apsnd (maps ensure_cnf_problem_line))
   374 
   375 fun filter_cnf_ueq_problem problem =
   376   problem
   377   |> map (apsnd (map open_formula_line
   378                  #> filter is_problem_line_cnf_ueq
   379                  #> map negate_conjecture_line))
   380   |> (fn problem =>
   381          let
   382            val conjs = problem |> maps snd |> filter is_problem_line_negated
   383          in if length conjs = 1 then problem else [] end)
   384 
   385 
   386 (** Symbol declarations **)
   387 
   388 (* TFF allows implicit declarations of types, function symbols, and predicate
   389    symbols (with "$i" as the type of individuals), but some provers (e.g.,
   390    SNARK) require explicit declarations. The situation is similar for THF. *)
   391 
   392 val atype_of_types = AType (`I tptp_type_of_types)
   393 val bool_atype = AType (`I tptp_bool_type)
   394 val individual_atype = AType (`I tptp_individual_type)
   395 
   396 fun default_type pred_sym =
   397   let
   398     fun typ 0 = if pred_sym then bool_atype else individual_atype
   399       | typ ary = AFun (individual_atype, typ (ary - 1))
   400   in typ end
   401 
   402 fun add_declared_syms_in_problem_line (Decl (_, sym, _)) = insert (op =) sym
   403   | add_declared_syms_in_problem_line _ = I
   404 fun declared_syms_in_problem problem =
   405   fold (fold add_declared_syms_in_problem_line o snd) problem []
   406 
   407 fun undeclared_syms_in_problem declared problem =
   408   let
   409     fun do_sym name ty =
   410       if member (op =) declared name then I else AList.default (op =) (name, ty)
   411     fun do_type (AFun (ty1, ty2)) = fold do_type [ty1, ty2]
   412       | do_type (AType name) = do_sym name (K atype_of_types)
   413     fun do_term pred_sym (ATerm (name as (s, _), tms)) =
   414         is_tptp_user_symbol s
   415         ? do_sym name (fn _ => default_type pred_sym (length tms))
   416         #> fold (do_term false) tms
   417       | do_term _ (AAbs ((_, ty), tm)) = do_type ty #> do_term false tm
   418     fun do_formula (AQuant (_, xs, phi)) =
   419         fold do_type (map_filter snd xs) #> do_formula phi
   420       | do_formula (AConn (_, phis)) = fold do_formula phis
   421       | do_formula (AAtom tm) = do_term true tm
   422     fun do_problem_line (Decl (_, _, ty)) = do_type ty
   423       | do_problem_line (Formula (_, _, phi, _, _)) = do_formula phi
   424   in
   425     fold (fold do_problem_line o snd) problem []
   426     |> filter_out (is_built_in_tptp_symbol o fst o fst)
   427   end
   428 
   429 fun declare_undeclared_syms_in_atp_problem prefix heading problem =
   430   let
   431     fun decl_line (x as (s, _), ty) = Decl (prefix ^ s, x, ty ())
   432     val declared = problem |> declared_syms_in_problem
   433     val decls =
   434       problem |> undeclared_syms_in_problem declared
   435               |> sort_wrt (fst o fst)
   436               |> map decl_line
   437   in (heading, decls) :: problem end
   438 
   439 (** Nice names **)
   440 
   441 fun empty_name_pool readable_names =
   442   if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
   443 
   444 fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
   445 fun pool_map f xs =
   446   pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
   447 
   448 val no_qualifiers =
   449   let
   450     fun skip [] = []
   451       | skip (#"." :: cs) = skip cs
   452       | skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
   453     and keep [] = []
   454       | keep (#"." :: cs) = skip cs
   455       | keep (c :: cs) = c :: keep cs
   456   in String.explode #> rev #> keep #> rev #> String.implode end
   457 
   458 (* Long names can slow down the ATPs. *)
   459 val max_readable_name_size = 20
   460 
   461 (* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
   462    unreadable "op_1", "op_2", etc., in the problem files. "eq" is reserved to
   463    ensure that "HOL.eq" is correctly mapped to equality (not clear whether this
   464    is still necessary). *)
   465 val reserved_nice_names = [tptp_old_equal, "op", "eq"]
   466 
   467 fun readable_name full_name s =
   468   if s = full_name then
   469     s
   470   else
   471     s |> no_qualifiers
   472       |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
   473       |> (fn s =>
   474              if size s > max_readable_name_size then
   475                String.substring (s, 0, max_readable_name_size div 2 - 4) ^
   476                Word.toString (hashw_string (full_name, 0w0)) ^
   477                String.extract (s, size s - max_readable_name_size div 2 + 4,
   478                                NONE)
   479              else
   480                s)
   481       |> (fn s => if member (op =) reserved_nice_names s then full_name else s)
   482 
   483 fun nice_name (full_name, _) NONE = (full_name, NONE)
   484   | nice_name (full_name, desired_name) (SOME the_pool) =
   485     if is_built_in_tptp_symbol full_name then
   486       (full_name, SOME the_pool)
   487     else case Symtab.lookup (fst the_pool) full_name of
   488       SOME nice_name => (nice_name, SOME the_pool)
   489     | NONE =>
   490       let
   491         val nice_prefix = readable_name full_name desired_name
   492         fun add j =
   493           let
   494             val nice_name =
   495               nice_prefix ^ (if j = 0 then "" else "_" ^ string_of_int j)
   496           in
   497             case Symtab.lookup (snd the_pool) nice_name of
   498               SOME full_name' =>
   499               if full_name = full_name' then (nice_name, the_pool)
   500               else add (j + 1)
   501             | NONE =>
   502               (nice_name,
   503                (Symtab.update_new (full_name, nice_name) (fst the_pool),
   504                 Symtab.update_new (nice_name, full_name) (snd the_pool)))
   505           end
   506       in add 0 |> apsnd SOME end
   507 
   508 fun nice_type (AType name) = nice_name name #>> AType
   509   | nice_type (AFun (ty1, ty2)) = nice_type ty1 ##>> nice_type ty2 #>> AFun
   510 fun nice_term (ATerm (name, ts)) =
   511     nice_name name ##>> pool_map nice_term ts #>> ATerm
   512   | nice_term (AAbs ((name, ty), tm)) =
   513     nice_name name ##>> nice_type ty ##>> nice_term tm #>> AAbs
   514 fun nice_formula (AQuant (q, xs, phi)) =
   515     pool_map nice_name (map fst xs)
   516     ##>> pool_map (fn NONE => pair NONE
   517                     | SOME ty => nice_type ty #>> SOME) (map snd xs)
   518     ##>> nice_formula phi
   519     #>> (fn ((ss, ts), phi) => AQuant (q, ss ~~ ts, phi))
   520   | nice_formula (AConn (c, phis)) =
   521     pool_map nice_formula phis #>> curry AConn c
   522   | nice_formula (AAtom tm) = nice_term tm #>> AAtom
   523 fun nice_problem_line (Decl (ident, sym, ty)) =
   524     nice_name sym ##>> nice_type ty #>> (fn (sym, ty) => Decl (ident, sym, ty))
   525   | nice_problem_line (Formula (ident, kind, phi, source, info)) =
   526     nice_formula phi #>> (fn phi => Formula (ident, kind, phi, source, info))
   527 fun nice_problem problem =
   528   pool_map (fn (heading, lines) =>
   529                pool_map nice_problem_line lines #>> pair heading) problem
   530 fun nice_atp_problem readable_names problem =
   531   nice_problem problem (empty_name_pool readable_names)
   532 
   533 end;