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