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