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