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