src/HOL/Tools/ATP/atp_problem.ML
author blanchet
Tue, 13 Dec 2011 14:55:42 +0100
changeset 46698 3b8606fba2dd
parent 46175 e6901aa86a9e
child 46809 c99af5431dfe
permissions -rw-r--r--
correctly declare implicit TFF1 types that appear first as type arguments with "$tType" and not "$i
blanchet@38293
     1
(*  Title:      HOL/Tools/ATP/atp_problem.ML
blanchet@38261
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
blanchet@37509
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@37509
     4
blanchet@39692
     5
Abstract representation of ATP problems and TPTP syntax.
blanchet@37509
     6
*)
blanchet@37509
     7
blanchet@38253
     8
signature ATP_PROBLEM =
blanchet@37509
     9
sig
nik@44535
    10
  datatype ('a, 'b) ho_term =
nik@44535
    11
    ATerm of 'a * ('a, 'b) ho_term list |
nik@44535
    12
    AAbs of ('a * 'b) * ('a, 'b) ho_term
blanchet@38226
    13
  datatype quantifier = AForall | AExists
blanchet@44004
    14
  datatype connective = ANot | AAnd | AOr | AImplies | AIff
blanchet@43402
    15
  datatype ('a, 'b, 'c) formula =
blanchet@43402
    16
    AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
blanchet@43402
    17
    AConn of connective * ('a, 'b, 'c) formula list |
blanchet@43402
    18
    AAtom of 'c
blanchet@38228
    19
blanchet@45457
    20
  datatype 'a ho_type =
blanchet@45457
    21
    AType of 'a * 'a ho_type list |
blanchet@45458
    22
    AFun of 'a ho_type * 'a ho_type |
blanchet@45458
    23
    ATyAbs of 'a list * 'a ho_type
blanchet@43804
    24
blanchet@45618
    25
  datatype tptp_polymorphism = TPTP_Monomorphic | TPTP_Polymorphic
blanchet@45618
    26
  datatype tptp_explicitness = TPTP_Implicit | TPTP_Explicit
blanchet@45453
    27
  datatype thf_flavor = THF_Without_Choice | THF_With_Choice
blanchet@46174
    28
  datatype dfg_flavor = DFG_Unsorted | DFG_Sorted
blanchet@46172
    29
blanchet@46172
    30
  datatype atp_format =
blanchet@45106
    31
    CNF |
blanchet@45106
    32
    CNF_UEQ |
blanchet@45106
    33
    FOF |
blanchet@45618
    34
    TFF of tptp_polymorphism * tptp_explicitness |
blanchet@46172
    35
    THF of tptp_polymorphism * tptp_explicitness * thf_flavor |
blanchet@46174
    36
    DFG of dfg_flavor
blanchet@45106
    37
blanchet@43396
    38
  datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
blanchet@43398
    39
  datatype 'a problem_line =
blanchet@43804
    40
    Decl of string * 'a * 'a ho_type |
blanchet@45261
    41
    Formula of string * formula_kind
blanchet@45261
    42
               * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
blanchet@45261
    43
               * (string, string ho_type) ho_term option
blanchet@45261
    44
               * (string, string ho_type) ho_term option
blanchet@38251
    45
  type 'a problem = (string * 'a problem_line list) list
blanchet@38226
    46
blanchet@46172
    47
  val isabelle_info_prefix : string
blanchet@46172
    48
  val isabelle_info : atp_format -> string -> (string, 'a) ho_term option
blanchet@46172
    49
  val introN : string
blanchet@46172
    50
  val elimN : string
blanchet@46172
    51
  val simpN : string
blanchet@43809
    52
  val tptp_cnf : string
blanchet@43809
    53
  val tptp_fof : string
blanchet@43809
    54
  val tptp_tff : string
blanchet@43809
    55
  val tptp_thf : string
blanchet@43808
    56
  val tptp_has_type : string
blanchet@43803
    57
  val tptp_type_of_types : string
blanchet@43803
    58
  val tptp_bool_type : string
blanchet@43803
    59
  val tptp_individual_type : string
blanchet@43804
    60
  val tptp_fun_type : string
blanchet@43839
    61
  val tptp_product_type : string
blanchet@43809
    62
  val tptp_forall : string
nik@44537
    63
  val tptp_ho_forall : string
blanchet@45514
    64
  val tptp_pi_binder : string
blanchet@43809
    65
  val tptp_exists : string
nik@44537
    66
  val tptp_ho_exists : string
nik@45350
    67
  val tptp_choice : string
blanchet@43809
    68
  val tptp_not : string
blanchet@43809
    69
  val tptp_and : string
blanchet@43809
    70
  val tptp_or : string
blanchet@43809
    71
  val tptp_implies : string
blanchet@43809
    72
  val tptp_if : string
blanchet@43809
    73
  val tptp_iff : string
blanchet@43809
    74
  val tptp_not_iff : string
blanchet@43809
    75
  val tptp_app : string
blanchet@43809
    76
  val tptp_not_infix : string
blanchet@43809
    77
  val tptp_equal : string
blanchet@43841
    78
  val tptp_old_equal : string
blanchet@43807
    79
  val tptp_false : string
blanchet@43807
    80
  val tptp_true : string
blanchet@43841
    81
  val tptp_empty_list : string
blanchet@43841
    82
  val is_tptp_equal : string -> bool
blanchet@43839
    83
  val is_built_in_tptp_symbol : string -> bool
blanchet@43839
    84
  val is_tptp_variable : string -> bool
blanchet@43839
    85
  val is_tptp_user_symbol : string -> bool
blanchet@45459
    86
  val atype_of_types : (string * string) ho_type
blanchet@45459
    87
  val bool_atype : (string * string) ho_type
blanchet@45459
    88
  val individual_atype : (string * string) ho_type
blanchet@43783
    89
  val mk_anot : ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
blanchet@43783
    90
  val mk_aconn :
blanchet@43783
    91
    connective -> ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
blanchet@43783
    92
    -> ('a, 'b, 'c) formula
blanchet@43839
    93
  val aconn_fold :
blanchet@43839
    94
    bool option -> (bool option -> 'a -> 'b -> 'b) -> connective * 'a list
blanchet@43839
    95
    -> 'b -> 'b
blanchet@43839
    96
  val aconn_map :
blanchet@43839
    97
    bool option -> (bool option -> 'a -> ('b, 'c, 'd) formula)
blanchet@43839
    98
    -> connective * 'a list -> ('b, 'c, 'd) formula
blanchet@43839
    99
  val formula_fold :
blanchet@43839
   100
    bool option -> (bool option -> 'c -> 'd -> 'd) -> ('a, 'b, 'c) formula
blanchet@43839
   101
    -> 'd -> 'd
blanchet@43785
   102
  val formula_map : ('c -> 'd) -> ('a, 'b, 'c) formula -> ('a, 'b, 'd) formula
blanchet@46174
   103
  val is_format_higher_order : atp_format -> bool
blanchet@46172
   104
  val is_format_typed : atp_format -> bool
blanchet@46172
   105
  val lines_for_atp_problem : atp_format -> string problem -> string list
blanchet@43933
   106
  val ensure_cnf_problem :
blanchet@43933
   107
    (string * string) problem -> (string * string) problem
blanchet@43780
   108
  val filter_cnf_ueq_problem :
blanchet@43780
   109
    (string * string) problem -> (string * string) problem
blanchet@46698
   110
  val declared_syms_in_problem : (string * ''a) problem -> (string * ''a) list
blanchet@39692
   111
  val nice_atp_problem :
blanchet@38251
   112
    bool -> ('a * (string * string) problem_line list) list
blanchet@38251
   113
    -> ('a * string problem_line list) list
blanchet@38251
   114
       * (string Symtab.table * string Symtab.table) option
blanchet@37509
   115
end;
blanchet@37509
   116
blanchet@38253
   117
structure ATP_Problem : ATP_PROBLEM =
blanchet@37509
   118
struct
blanchet@37509
   119
blanchet@43926
   120
open ATP_Util
blanchet@43926
   121
blanchet@43926
   122
blanchet@37643
   123
(** ATP problem **)
blanchet@37643
   124
nik@44535
   125
datatype ('a, 'b) ho_term =
nik@44535
   126
  ATerm of 'a * ('a, 'b) ho_term list |
nik@44535
   127
  AAbs of ('a * 'b) * ('a, 'b) ho_term
blanchet@38198
   128
datatype quantifier = AForall | AExists
blanchet@44004
   129
datatype connective = ANot | AAnd | AOr | AImplies | AIff
blanchet@43402
   130
datatype ('a, 'b, 'c) formula =
blanchet@43402
   131
  AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
blanchet@43402
   132
  AConn of connective * ('a, 'b, 'c) formula list |
blanchet@43402
   133
  AAtom of 'c
blanchet@38198
   134
blanchet@45457
   135
datatype 'a ho_type =
blanchet@45457
   136
  AType of 'a * 'a ho_type list |
blanchet@45458
   137
  AFun of 'a ho_type * 'a ho_type |
blanchet@45458
   138
  ATyAbs of 'a list * 'a ho_type
blanchet@43804
   139
blanchet@45618
   140
datatype tptp_polymorphism = TPTP_Monomorphic | TPTP_Polymorphic
blanchet@45618
   141
datatype tptp_explicitness = TPTP_Implicit | TPTP_Explicit
blanchet@45453
   142
datatype thf_flavor = THF_Without_Choice | THF_With_Choice
blanchet@46174
   143
datatype dfg_flavor = DFG_Unsorted | DFG_Sorted
blanchet@45354
   144
blanchet@46172
   145
datatype atp_format =
blanchet@45106
   146
  CNF |
blanchet@45106
   147
  CNF_UEQ |
blanchet@45106
   148
  FOF |
blanchet@45618
   149
  TFF of tptp_polymorphism * tptp_explicitness |
blanchet@46172
   150
  THF of tptp_polymorphism * tptp_explicitness * thf_flavor |
blanchet@46174
   151
  DFG of dfg_flavor
blanchet@45106
   152
blanchet@43396
   153
datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
blanchet@43398
   154
datatype 'a problem_line =
blanchet@43804
   155
  Decl of string * 'a * 'a ho_type |
nik@44535
   156
  Formula of string * formula_kind * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
nik@44535
   157
             * (string, string ho_type) ho_term option * (string, string ho_type) ho_term option
blanchet@37643
   158
type 'a problem = (string * 'a problem_line list) list
blanchet@37643
   159
blanchet@46172
   160
val isabelle_info_prefix = "isabelle_"
blanchet@46172
   161
blanchet@46174
   162
(* Currently, only newer versions of SPASS, with sorted DFG format support, can
blanchet@46174
   163
   process Isabelle metainformation. *)
blanchet@46174
   164
fun isabelle_info (DFG DFG_Sorted) s =
blanchet@46172
   165
    SOME (ATerm ("[]", [ATerm (isabelle_info_prefix ^ s, [])]))
blanchet@46172
   166
  | isabelle_info _ _ = NONE
blanchet@46172
   167
blanchet@46172
   168
val introN = "intro"
blanchet@46172
   169
val elimN = "elim"
blanchet@46172
   170
val simpN = "simp"
blanchet@46172
   171
blanchet@46172
   172
fun is_isabelle_info suffix (SOME (ATerm ("[]", [ATerm (s, [])]))) =
blanchet@46172
   173
    s = isabelle_info_prefix ^ suffix
blanchet@46172
   174
  | is_isabelle_info _ _ = false
blanchet@46172
   175
blanchet@43587
   176
(* official TPTP syntax *)
blanchet@43809
   177
val tptp_cnf = "cnf"
blanchet@43809
   178
val tptp_fof = "fof"
blanchet@43809
   179
val tptp_tff = "tff"
blanchet@43809
   180
val tptp_thf = "thf"
blanchet@43808
   181
val tptp_has_type = ":"
blanchet@43803
   182
val tptp_type_of_types = "$tType"
blanchet@43803
   183
val tptp_bool_type = "$o"
blanchet@43803
   184
val tptp_individual_type = "$i"
blanchet@43804
   185
val tptp_fun_type = ">"
blanchet@43839
   186
val tptp_product_type = "*"
blanchet@43809
   187
val tptp_forall = "!"
nik@44537
   188
val tptp_ho_forall = "!!"
blanchet@45514
   189
val tptp_pi_binder = "!>"
blanchet@43809
   190
val tptp_exists = "?"
nik@44537
   191
val tptp_ho_exists = "??"
nik@45350
   192
val tptp_choice = "@+"
blanchet@43809
   193
val tptp_not = "~"
blanchet@43809
   194
val tptp_and = "&"
blanchet@43809
   195
val tptp_or = "|"
blanchet@43809
   196
val tptp_implies = "=>"
blanchet@43809
   197
val tptp_if = "<="
blanchet@43809
   198
val tptp_iff = "<=>"
blanchet@43809
   199
val tptp_not_iff = "<~>"
blanchet@43809
   200
val tptp_app = "@"
blanchet@43809
   201
val tptp_not_infix = "!"
blanchet@43809
   202
val tptp_equal = "="
blanchet@43841
   203
val tptp_old_equal = "equal"
blanchet@43807
   204
val tptp_false = "$false"
blanchet@43807
   205
val tptp_true = "$true"
blanchet@43841
   206
val tptp_empty_list = "[]"
blanchet@43587
   207
blanchet@43841
   208
fun is_tptp_equal s = (s = tptp_equal orelse s = tptp_old_equal)
blanchet@43841
   209
fun is_built_in_tptp_symbol s =
blanchet@43841
   210
  s = tptp_old_equal orelse not (Char.isAlpha (String.sub (s, 0)))
blanchet@43839
   211
fun is_tptp_variable s = Char.isUpper (String.sub (s, 0))
blanchet@43839
   212
val is_tptp_user_symbol = not o (is_tptp_variable orf is_built_in_tptp_symbol)
blanchet@43780
   213
blanchet@46172
   214
val atype_of_types = AType (`I tptp_type_of_types, [])
blanchet@46172
   215
val bool_atype = AType (`I tptp_bool_type, [])
blanchet@46172
   216
val individual_atype = AType (`I tptp_individual_type, [])
blanchet@46172
   217
blanchet@43939
   218
fun raw_polarities_of_conn ANot = (SOME false, NONE)
blanchet@43939
   219
  | raw_polarities_of_conn AAnd = (SOME true, SOME true)
blanchet@43939
   220
  | raw_polarities_of_conn AOr = (SOME true, SOME true)
blanchet@43939
   221
  | raw_polarities_of_conn AImplies = (SOME false, SOME true)
blanchet@43939
   222
  | raw_polarities_of_conn AIff = (NONE, NONE)
blanchet@43939
   223
fun polarities_of_conn NONE = K (NONE, NONE)
blanchet@43939
   224
  | polarities_of_conn (SOME pos) =
blanchet@43939
   225
    raw_polarities_of_conn #> not pos ? pairself (Option.map not)
blanchet@43939
   226
blanchet@43783
   227
fun mk_anot (AConn (ANot, [phi])) = phi
blanchet@43783
   228
  | mk_anot phi = AConn (ANot, [phi])
blanchet@43783
   229
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
blanchet@43783
   230
blanchet@43839
   231
fun aconn_fold pos f (ANot, [phi]) = f (Option.map not pos) phi
blanchet@43839
   232
  | aconn_fold pos f (AImplies, [phi1, phi2]) =
blanchet@43839
   233
    f (Option.map not pos) phi1 #> f pos phi2
blanchet@43839
   234
  | aconn_fold pos f (AAnd, phis) = fold (f pos) phis
blanchet@43839
   235
  | aconn_fold pos f (AOr, phis) = fold (f pos) phis
blanchet@43839
   236
  | aconn_fold _ f (_, phis) = fold (f NONE) phis
blanchet@43839
   237
blanchet@43839
   238
fun aconn_map pos f (ANot, [phi]) = AConn (ANot, [f (Option.map not pos) phi])
blanchet@43839
   239
  | aconn_map pos f (AImplies, [phi1, phi2]) =
blanchet@43839
   240
    AConn (AImplies, [f (Option.map not pos) phi1, f pos phi2])
blanchet@43839
   241
  | aconn_map pos f (AAnd, phis) = AConn (AAnd, map (f pos) phis)
blanchet@43839
   242
  | aconn_map pos f (AOr, phis) = AConn (AOr, map (f pos) phis)
blanchet@43839
   243
  | aconn_map _ f (c, phis) = AConn (c, map (f NONE) phis)
blanchet@43839
   244
blanchet@43839
   245
fun formula_fold pos f =
blanchet@43839
   246
  let
blanchet@45356
   247
    fun fld pos (AQuant (_, _, phi)) = fld pos phi
blanchet@45356
   248
      | fld pos (AConn conn) = aconn_fold pos fld conn
blanchet@45356
   249
      | fld pos (AAtom tm) = f pos tm
blanchet@45356
   250
  in fld pos end
blanchet@43839
   251
blanchet@43785
   252
fun formula_map f (AQuant (q, xs, phi)) = AQuant (q, xs, formula_map f phi)
blanchet@43785
   253
  | formula_map f (AConn (c, phis)) = AConn (c, map (formula_map f) phis)
blanchet@43785
   254
  | formula_map f (AAtom tm) = AAtom (f tm)
blanchet@43785
   255
blanchet@46174
   256
fun is_format_higher_order (THF _) = true
blanchet@46174
   257
  | is_format_higher_order _ = false
blanchet@45354
   258
fun is_format_typed (TFF _) = true
blanchet@45618
   259
  | is_format_typed (THF _) = true
blanchet@46174
   260
  | is_format_typed (DFG DFG_Sorted) = true
blanchet@45106
   261
  | is_format_typed _ = false
blanchet@43839
   262
blanchet@46172
   263
fun tptp_string_for_kind Axiom = "axiom"
blanchet@46172
   264
  | tptp_string_for_kind Definition = "definition"
blanchet@46172
   265
  | tptp_string_for_kind Lemma = "lemma"
blanchet@46172
   266
  | tptp_string_for_kind Hypothesis = "hypothesis"
blanchet@46172
   267
  | tptp_string_for_kind Conjecture = "conjecture"
blanchet@38854
   268
blanchet@46172
   269
fun tptp_string_for_app format func args =
blanchet@46174
   270
  if is_format_higher_order format then
blanchet@45655
   271
    "(" ^ space_implode (" " ^ tptp_app ^ " ") (func :: args) ^ ")"
blanchet@45655
   272
  else
blanchet@45655
   273
    func ^ "(" ^ commas args ^ ")"
blanchet@45655
   274
blanchet@45458
   275
fun flatten_type (ATyAbs (tys, ty)) = ATyAbs (tys, flatten_type ty)
blanchet@45458
   276
  | flatten_type (ty as AFun (ty1 as AType _, ty2)) =
blanchet@45458
   277
    (case flatten_type ty2 of
blanchet@45458
   278
       AFun (ty' as AType (s, tys), ty) =>
blanchet@45458
   279
       AFun (AType (tptp_product_type,
blanchet@45458
   280
                    ty1 :: (if s = tptp_product_type then tys else [ty'])), ty)
blanchet@45458
   281
     | _ => ty)
blanchet@45458
   282
  | flatten_type (ty as AType _) = ty
blanchet@45458
   283
  | flatten_type _ =
blanchet@43804
   284
    raise Fail "unexpected higher-order type in first-order format"
blanchet@43804
   285
blanchet@45655
   286
fun str_for_type format ty =
blanchet@45457
   287
  let
blanchet@46174
   288
    val dfg = (format = DFG DFG_Sorted)
blanchet@46172
   289
    fun str _ (AType (s, [])) =
blanchet@46172
   290
        if dfg andalso s = tptp_individual_type then "Top" else s
blanchet@45458
   291
      | str _ (AType (s, tys)) =
blanchet@45655
   292
        let val ss = tys |> map (str false) in
blanchet@45655
   293
          if s = tptp_product_type then
blanchet@46172
   294
            ss |> space_implode
blanchet@46172
   295
                      (if dfg then ", " else " " ^ tptp_product_type ^ " ")
blanchet@46172
   296
               |> (not dfg andalso length ss > 1) ? enclose "(" ")"
blanchet@45655
   297
          else
blanchet@46172
   298
            tptp_string_for_app format s ss
blanchet@45655
   299
        end
blanchet@45457
   300
      | str rhs (AFun (ty1, ty2)) =
blanchet@46172
   301
        (str false ty1 |> dfg ? enclose "(" ")") ^ " " ^
blanchet@46172
   302
        (if dfg then "" else tptp_fun_type ^ " ") ^ str true ty2
blanchet@45457
   303
        |> not rhs ? enclose "(" ")"
blanchet@45458
   304
      | str _ (ATyAbs (ss, ty)) =
blanchet@45514
   305
        tptp_pi_binder ^ "[" ^
blanchet@45458
   306
        commas (map (suffix (" " ^ tptp_has_type ^ " " ^ tptp_type_of_types))
blanchet@45603
   307
                    ss) ^ "]: " ^ str false ty
blanchet@45457
   308
  in str true ty end
blanchet@45457
   309
blanchet@45655
   310
fun string_for_type (format as THF _) ty = str_for_type format ty
blanchet@46172
   311
  | string_for_type format ty = str_for_type format (flatten_type ty)
blanchet@43804
   312
blanchet@46172
   313
fun tptp_string_for_quantifier AForall = tptp_forall
blanchet@46172
   314
  | tptp_string_for_quantifier AExists = tptp_exists
blanchet@43804
   315
blanchet@46172
   316
fun tptp_string_for_connective ANot = tptp_not
blanchet@46172
   317
  | tptp_string_for_connective AAnd = tptp_and
blanchet@46172
   318
  | tptp_string_for_connective AOr = tptp_or
blanchet@46172
   319
  | tptp_string_for_connective AImplies = tptp_implies
blanchet@46172
   320
  | tptp_string_for_connective AIff = tptp_iff
blanchet@43804
   321
blanchet@43803
   322
fun string_for_bound_var format (s, ty) =
blanchet@45457
   323
  s ^
blanchet@45457
   324
  (if is_format_typed format then
blanchet@45457
   325
     " " ^ tptp_has_type ^ " " ^
blanchet@45457
   326
     (ty |> the_default (AType (tptp_individual_type, []))
blanchet@45457
   327
         |> string_for_type format)
blanchet@45457
   328
   else
blanchet@45457
   329
     "")
blanchet@43804
   330
blanchet@45618
   331
fun is_format_with_choice (THF (_, _, THF_With_Choice)) = true
blanchet@45618
   332
  | is_format_with_choice _ = false
blanchet@45618
   333
blanchet@46172
   334
fun tptp_string_for_term _ (ATerm (s, [])) = s
blanchet@46172
   335
  | tptp_string_for_term format (ATerm (s, ts)) =
blanchet@45655
   336
    (if s = tptp_empty_list then
blanchet@45655
   337
       (* used for lists in the optional "source" field of a derivation *)
blanchet@46172
   338
       "[" ^ commas (map (tptp_string_for_term format) ts) ^ "]"
blanchet@45655
   339
     else if is_tptp_equal s then
blanchet@46172
   340
       space_implode (" " ^ tptp_equal ^ " ")
blanchet@46172
   341
                     (map (tptp_string_for_term format) ts)
blanchet@46174
   342
       |> is_format_higher_order format ? enclose "(" ")"
blanchet@45655
   343
     else case (s = tptp_ho_forall orelse s = tptp_ho_exists,
blanchet@45655
   344
                s = tptp_choice andalso is_format_with_choice format, ts) of
blanchet@45655
   345
       (true, _, [AAbs ((s', ty), tm)]) =>
blanchet@45655
   346
       (* Use syntactic sugar "!" and "?" instead of "!!" and "??" whenever
blanchet@45655
   347
          possible, to work around LEO-II 1.2.8 parser limitation. *)
blanchet@46172
   348
       tptp_string_for_formula format
blanchet@45655
   349
           (AQuant (if s = tptp_ho_forall then AForall else AExists,
blanchet@45655
   350
                    [(s', SOME ty)], AAtom tm))
blanchet@45655
   351
     | (_, true, [AAbs ((s', ty), tm)]) =>
blanchet@45655
   352
       (* There is code in "ATP_Translate" to ensure that "Eps" is always
blanchet@45655
   353
          applied to an abstraction. *)
blanchet@45655
   354
       tptp_choice ^ "[" ^ s' ^ " : " ^ string_for_type format ty ^ "]: " ^
blanchet@46172
   355
       tptp_string_for_term format tm ^ ""
blanchet@45655
   356
       |> enclose "(" ")"
blanchet@46172
   357
     | _ => tptp_string_for_app format s (map (tptp_string_for_term format) ts))
blanchet@46172
   358
  | tptp_string_for_term (format as THF _) (AAbs ((s, ty), tm)) =
blanchet@45603
   359
    "(^[" ^ s ^ " : " ^ string_for_type format ty ^ "]: " ^
blanchet@46172
   360
    tptp_string_for_term format tm ^ ")"
blanchet@46172
   361
  | tptp_string_for_term _ _ =
blanchet@46172
   362
    raise Fail "unexpected term in first-order format"
blanchet@46172
   363
and tptp_string_for_formula format (AQuant (q, xs, phi)) =
blanchet@46172
   364
    tptp_string_for_quantifier q ^
blanchet@45603
   365
    "[" ^ commas (map (string_for_bound_var format) xs) ^ "]: " ^
blanchet@46172
   366
    tptp_string_for_formula format phi
blanchet@43815
   367
    |> enclose "(" ")"
blanchet@46172
   368
  | tptp_string_for_formula format
blanchet@43841
   369
        (AConn (ANot, [AAtom (ATerm ("=" (* tptp_equal *), ts))])) =
blanchet@43809
   370
    space_implode (" " ^ tptp_not_infix ^ tptp_equal ^ " ")
blanchet@46172
   371
                  (map (tptp_string_for_term format) ts)
blanchet@46174
   372
    |> is_format_higher_order format ? enclose "(" ")"
blanchet@46172
   373
  | tptp_string_for_formula format (AConn (c, [phi])) =
blanchet@46172
   374
    tptp_string_for_connective c ^ " " ^
blanchet@46172
   375
    (tptp_string_for_formula format phi
blanchet@46174
   376
     |> is_format_higher_order format ? enclose "(" ")")
blanchet@43815
   377
    |> enclose "(" ")"
blanchet@46172
   378
  | tptp_string_for_formula format (AConn (c, phis)) =
blanchet@46172
   379
    space_implode (" " ^ tptp_string_for_connective c ^ " ")
blanchet@46172
   380
                  (map (tptp_string_for_formula format) phis)
blanchet@43815
   381
    |> enclose "(" ")"
blanchet@46172
   382
  | tptp_string_for_formula format (AAtom tm) = tptp_string_for_term format tm
blanchet@38198
   383
blanchet@44558
   384
fun the_source (SOME source) = source
blanchet@44558
   385
  | the_source NONE =
blanchet@44558
   386
    ATerm ("inference",
blanchet@44558
   387
           ATerm ("isabelle", []) :: replicate 2 (ATerm ("[]", [])))
blanchet@43510
   388
blanchet@46172
   389
fun tptp_string_for_format CNF = tptp_cnf
blanchet@46172
   390
  | tptp_string_for_format CNF_UEQ = tptp_cnf
blanchet@46172
   391
  | tptp_string_for_format FOF = tptp_fof
blanchet@46172
   392
  | tptp_string_for_format (TFF _) = tptp_tff
blanchet@46172
   393
  | tptp_string_for_format (THF _) = tptp_thf
blanchet@46174
   394
  | tptp_string_for_format (DFG _) = raise Fail "non-TPTP format"
blanchet@43803
   395
blanchet@46172
   396
fun tptp_string_for_problem_line format (Decl (ident, sym, ty)) =
blanchet@46172
   397
    tptp_string_for_format format ^ "(" ^ ident ^ ", type,\n    " ^ sym ^
blanchet@46172
   398
    " : " ^ string_for_type format ty ^ ").\n"
blanchet@46172
   399
  | tptp_string_for_problem_line format
blanchet@46172
   400
                                 (Formula (ident, kind, phi, source, info)) =
blanchet@46172
   401
    tptp_string_for_format format ^ "(" ^ ident ^ ", " ^
blanchet@46172
   402
    tptp_string_for_kind kind ^ ",\n    (" ^
blanchet@46172
   403
    tptp_string_for_formula format phi ^ ")" ^
blanchet@43780
   404
    (case (source, info) of
blanchet@43580
   405
       (NONE, NONE) => ""
blanchet@46172
   406
     | (SOME tm, NONE) => ", " ^ tptp_string_for_term format tm
blanchet@43580
   407
     | (_, SOME tm) =>
blanchet@46172
   408
       ", " ^ tptp_string_for_term format (the_source source) ^
blanchet@46172
   409
       ", " ^ tptp_string_for_term format tm) ^ ").\n"
blanchet@46172
   410
blanchet@46172
   411
fun tptp_lines format =
blanchet@37643
   412
  maps (fn (_, []) => []
blanchet@37643
   413
         | (heading, lines) =>
wenzelm@41739
   414
           "\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
blanchet@46172
   415
           map (tptp_string_for_problem_line format) lines)
blanchet@46172
   416
blanchet@46172
   417
fun arity_of_type (AFun (_, ty)) = 1 + arity_of_type ty
blanchet@46172
   418
  | arity_of_type _ = 0
blanchet@46172
   419
blanchet@46172
   420
fun binder_atypes (AFun (ty1, ty2)) = ty1 :: binder_atypes ty2
blanchet@46172
   421
  | binder_atypes _ = []
blanchet@46172
   422
blanchet@46172
   423
fun is_function_type (AFun (_, ty)) = is_function_type ty
blanchet@46172
   424
  | is_function_type (AType (s, _)) =
blanchet@46172
   425
    s <> tptp_type_of_types andalso s <> tptp_bool_type
blanchet@46172
   426
  | is_function_type _ = false
blanchet@46172
   427
blanchet@46172
   428
fun is_predicate_type (AFun (_, ty)) = is_predicate_type ty
blanchet@46172
   429
  | is_predicate_type (AType (s, _)) = (s = tptp_bool_type)
blanchet@46172
   430
  | is_predicate_type _ = false
blanchet@46172
   431
blanchet@46174
   432
fun dfg_string_for_formula flavor info =
blanchet@46172
   433
  let
blanchet@46172
   434
    fun str_for_term simp (ATerm (s, tms)) =
blanchet@46172
   435
        (if is_tptp_equal s then "equal" |> simp ? suffix ":lr"
blanchet@46172
   436
         else if s = tptp_true then "true"
blanchet@46172
   437
         else if s = tptp_false then "false"
blanchet@46172
   438
         else s) ^
blanchet@46172
   439
        (if null tms then ""
blanchet@46172
   440
         else "(" ^ commas (map (str_for_term false) tms) ^ ")")
blanchet@46172
   441
      | str_for_term _ _ = raise Fail "unexpected term in first-order format"
blanchet@46172
   442
    fun str_for_quant AForall = "forall"
blanchet@46172
   443
      | str_for_quant AExists = "exists"
blanchet@46172
   444
    fun str_for_conn _ ANot = "not"
blanchet@46172
   445
      | str_for_conn _ AAnd = "and"
blanchet@46172
   446
      | str_for_conn _ AOr = "or"
blanchet@46172
   447
      | str_for_conn _ AImplies = "implies"
blanchet@46172
   448
      | str_for_conn simp AIff = "equiv" |> simp ? suffix ":lr"
blanchet@46172
   449
    fun str_for_formula simp (AQuant (q, xs, phi)) =
blanchet@46172
   450
        str_for_quant q ^ "(" ^ "[" ^
blanchet@46174
   451
        commas (map (string_for_bound_var (DFG flavor)) xs) ^ "], " ^
blanchet@46172
   452
        str_for_formula simp phi ^ ")"
blanchet@46172
   453
      | str_for_formula simp (AConn (c, phis)) =
blanchet@46172
   454
        str_for_conn simp c ^ "(" ^
blanchet@46172
   455
        commas (map (str_for_formula false) phis) ^ ")"
blanchet@46172
   456
      | str_for_formula simp (AAtom tm) = str_for_term simp tm
blanchet@46172
   457
  in str_for_formula (is_isabelle_info simpN info) end
blanchet@46172
   458
blanchet@46174
   459
fun dfg_lines flavor problem =
blanchet@46172
   460
  let
blanchet@46175
   461
    val sorted = (flavor = DFG_Sorted)
blanchet@46174
   462
    val format = DFG flavor
blanchet@46172
   463
    fun ary sym ty =
blanchet@46172
   464
      "(" ^ sym ^ ", " ^ string_of_int (arity_of_type ty) ^ ")"
blanchet@46172
   465
    fun fun_typ sym ty =
blanchet@46174
   466
      "function(" ^ sym ^ ", " ^ string_for_type format ty ^ ")."
blanchet@46172
   467
    fun pred_typ sym ty =
blanchet@46172
   468
      "predicate(" ^
blanchet@46174
   469
      commas (sym :: map (string_for_type format) (binder_atypes ty)) ^ ")."
blanchet@46172
   470
    fun formula pred (Formula (ident, kind, phi, _, info)) =
blanchet@46172
   471
        if pred kind then
blanchet@46174
   472
          SOME ("formula(" ^ dfg_string_for_formula flavor info phi ^ ", " ^
blanchet@46174
   473
                ident ^ ").")
blanchet@46172
   474
        else
blanchet@46172
   475
          NONE
blanchet@46172
   476
      | formula _ _ = NONE
blanchet@46172
   477
    fun filt f = problem |> map (map_filter f o snd) |> flat
blanchet@46172
   478
    val func_aries =
blanchet@46172
   479
      filt (fn Decl (_, sym, ty) =>
blanchet@46172
   480
               if is_function_type ty then SOME (ary sym ty) else NONE
blanchet@46172
   481
             | _ => NONE)
blanchet@46172
   482
      |> commas |> enclose "functions [" "]."
blanchet@46172
   483
    val pred_aries =
blanchet@46172
   484
      filt (fn Decl (_, sym, ty) =>
blanchet@46172
   485
               if is_predicate_type ty then SOME (ary sym ty) else NONE
blanchet@46172
   486
             | _ => NONE)
blanchet@46172
   487
      |> commas |> enclose "predicates [" "]."
blanchet@46175
   488
    fun sorts () =
blanchet@46172
   489
      filt (fn Decl (_, sym, AType (s, [])) =>
blanchet@46172
   490
               if s = tptp_type_of_types then SOME sym else NONE
blanchet@46172
   491
             | _ => NONE)
blanchet@46172
   492
      |> commas |> enclose "sorts [" "]."
blanchet@46175
   493
    val syms = [func_aries, pred_aries] @ (if sorted then [sorts ()] else [])
blanchet@46175
   494
    fun func_sigs () =
blanchet@46172
   495
      filt (fn Decl (_, sym, ty) =>
blanchet@46172
   496
               if is_function_type ty then SOME (fun_typ sym ty) else NONE
blanchet@46172
   497
             | _ => NONE)
blanchet@46175
   498
    fun pred_sigs () =
blanchet@46172
   499
      filt (fn Decl (_, sym, ty) =>
blanchet@46172
   500
               if is_predicate_type ty then SOME (pred_typ sym ty) else NONE
blanchet@46172
   501
             | _ => NONE)
blanchet@46175
   502
    val decls = if sorted then func_sigs () @ pred_sigs () else []
blanchet@46172
   503
    val axioms = filt (formula (curry (op <>) Conjecture))
blanchet@46172
   504
    val conjs = filt (formula (curry (op =) Conjecture))
blanchet@46172
   505
    fun list_of _ [] = []
blanchet@46172
   506
      | list_of heading ss =
blanchet@46172
   507
        "list_of_" ^ heading ^ ".\n" :: map (suffix "\n") ss @
blanchet@46172
   508
        ["end_of_list.\n\n"]
blanchet@46172
   509
  in
blanchet@46172
   510
    "\nbegin_problem(isabelle).\n\n" ::
blanchet@46172
   511
    list_of "descriptions"
blanchet@46172
   512
            ["name({**}).", "author({**}).", "status(unknown).",
blanchet@46172
   513
             "description({**})."] @
blanchet@46175
   514
    list_of "symbols" syms @
blanchet@46175
   515
    list_of "declarations" decls @
blanchet@46172
   516
    list_of "formulae(axioms)" axioms @
blanchet@46172
   517
    list_of "formulae(conjectures)" conjs @
blanchet@46172
   518
    ["end_problem.\n"]
blanchet@46172
   519
  end
blanchet@46172
   520
blanchet@46172
   521
fun lines_for_atp_problem format problem =
blanchet@46172
   522
  "% This file was generated by Isabelle (most likely Sledgehammer)\n\
blanchet@46172
   523
  \% " ^ timestamp () ^ "\n" ::
blanchet@46174
   524
  (case format of
blanchet@46174
   525
     DFG flavor => dfg_lines flavor
blanchet@46174
   526
   | _ => tptp_lines format) problem
blanchet@37643
   527
blanchet@43780
   528
blanchet@43939
   529
(** CNF (Metis) and CNF UEQ (Waldmeister) **)
blanchet@43780
   530
blanchet@43780
   531
fun is_problem_line_negated (Formula (_, _, AConn (ANot, _), _, _)) = true
blanchet@43780
   532
  | is_problem_line_negated _ = false
blanchet@43780
   533
blanchet@44034
   534
fun is_problem_line_cnf_ueq (Formula (_, _, AAtom (ATerm ((s, _), _)), _, _)) =
blanchet@44034
   535
    is_tptp_equal s
blanchet@43780
   536
  | is_problem_line_cnf_ueq _ = false
blanchet@43780
   537
blanchet@43785
   538
fun open_conjecture_term (ATerm ((s, s'), tms)) =
nik@44535
   539
    ATerm (if is_tptp_variable s then (s |> Name.desymbolize false, s')
nik@44535
   540
           else (s, s'), tms |> map open_conjecture_term)
nik@44535
   541
  | open_conjecture_term _ = raise Fail "unexpected higher-order term"
blanchet@43939
   542
fun open_formula conj =
blanchet@43939
   543
  let
blanchet@44034
   544
    (* We are conveniently assuming that all bound variable names are
blanchet@44034
   545
       distinct, which should be the case for the formulas we generate. *)
blanchet@44034
   546
    fun opn (pos as SOME true) (AQuant (AForall, _, phi)) = opn pos phi
blanchet@44034
   547
      | opn (pos as SOME false) (AQuant (AExists, _, phi)) = opn pos phi
blanchet@43939
   548
      | opn pos (AConn (ANot, [phi])) = mk_anot (opn (Option.map not pos) phi)
blanchet@43939
   549
      | opn pos (AConn (c, [phi1, phi2])) =
blanchet@43939
   550
        let val (pos1, pos2) = polarities_of_conn pos c in
blanchet@43939
   551
          AConn (c, [opn pos1 phi1, opn pos2 phi2])
blanchet@43939
   552
        end
blanchet@43939
   553
      | opn _ (AAtom t) = AAtom (t |> conj ? open_conjecture_term)
blanchet@44283
   554
      | opn _ phi = phi
blanchet@43939
   555
  in opn (SOME (not conj)) end
blanchet@43785
   556
fun open_formula_line (Formula (ident, kind, phi, source, info)) =
blanchet@43785
   557
    Formula (ident, kind, open_formula (kind = Conjecture) phi, source, info)
blanchet@43785
   558
  | open_formula_line line = line
blanchet@43780
   559
blanchet@43780
   560
fun negate_conjecture_line (Formula (ident, Conjecture, phi, source, info)) =
blanchet@43783
   561
    Formula (ident, Hypothesis, mk_anot phi, source, info)
blanchet@43780
   562
  | negate_conjecture_line line = line
blanchet@43780
   563
blanchet@43939
   564
exception CLAUSIFY of unit
blanchet@43939
   565
blanchet@43967
   566
(* This "clausification" only expands syntactic sugar, such as "phi => psi" to
blanchet@43967
   567
   "~ phi | psi" and "phi <=> psi" to "~ phi | psi" and "~ psi | phi". We don't
blanchet@43967
   568
   attempt to distribute conjunctions over disjunctions. *)
blanchet@44034
   569
fun clausify_formula pos (phi as AAtom _) = [phi |> not pos ? mk_anot]
blanchet@44034
   570
  | clausify_formula pos (AConn (ANot, [phi])) = clausify_formula (not pos) phi
blanchet@44034
   571
  | clausify_formula true (AConn (AOr, [phi1, phi2])) =
blanchet@44034
   572
    (phi1, phi2) |> pairself (clausify_formula true)
blanchet@44034
   573
                 |> uncurry (map_product (mk_aconn AOr))
blanchet@44034
   574
  | clausify_formula false (AConn (AAnd, [phi1, phi2])) =
blanchet@44034
   575
    (phi1, phi2) |> pairself (clausify_formula false)
blanchet@44034
   576
                 |> uncurry (map_product (mk_aconn AOr))
blanchet@44034
   577
  | clausify_formula true (AConn (AImplies, [phi1, phi2])) =
blanchet@44034
   578
    clausify_formula true (AConn (AOr, [mk_anot phi1, phi2]))
blanchet@44034
   579
  | clausify_formula true (AConn (AIff, phis)) =
blanchet@44034
   580
    clausify_formula true (AConn (AImplies, phis)) @
blanchet@44034
   581
    clausify_formula true (AConn (AImplies, rev phis))
blanchet@44034
   582
  | clausify_formula _ _ = raise CLAUSIFY ()
blanchet@43939
   583
blanchet@43939
   584
fun clausify_formula_line (Formula (ident, kind, phi, source, info)) =
blanchet@43967
   585
    let
blanchet@43967
   586
      val (n, phis) = phi |> try (clausify_formula true) |> these |> `length
blanchet@43967
   587
    in
blanchet@43967
   588
      map2 (fn phi => fn j =>
blanchet@44153
   589
               Formula (ident ^ replicate_string (j - 1) "x", kind, phi, source,
blanchet@44153
   590
                        info))
blanchet@43967
   591
           phis (1 upto n)
blanchet@43967
   592
    end
blanchet@43967
   593
  | clausify_formula_line _ = []
blanchet@43939
   594
blanchet@43939
   595
fun ensure_cnf_problem_line line =
blanchet@43939
   596
  line |> open_formula_line |> negate_conjecture_line |> clausify_formula_line
blanchet@43939
   597
blanchet@43939
   598
fun ensure_cnf_problem problem =
blanchet@43967
   599
  problem |> map (apsnd (maps ensure_cnf_problem_line))
blanchet@43933
   600
blanchet@43803
   601
fun filter_cnf_ueq_problem problem =
blanchet@43803
   602
  problem
blanchet@43803
   603
  |> map (apsnd (map open_formula_line
blanchet@43803
   604
                 #> filter is_problem_line_cnf_ueq
blanchet@43803
   605
                 #> map negate_conjecture_line))
blanchet@43803
   606
  |> (fn problem =>
blanchet@43780
   607
         let
blanchet@44688
   608
           val lines = problem |> maps snd
blanchet@44688
   609
           val conjs = lines |> filter is_problem_line_negated
blanchet@44688
   610
         in if length conjs = 1 andalso conjs <> lines then problem else [] end)
blanchet@38251
   611
blanchet@37643
   612
blanchet@43839
   613
(** Symbol declarations **)
blanchet@43839
   614
blanchet@43839
   615
fun add_declared_syms_in_problem_line (Decl (_, sym, _)) = insert (op =) sym
blanchet@43839
   616
  | add_declared_syms_in_problem_line _ = I
blanchet@43839
   617
fun declared_syms_in_problem problem =
blanchet@43839
   618
  fold (fold add_declared_syms_in_problem_line o snd) problem []
blanchet@43839
   619
blanchet@37643
   620
(** Nice names **)
blanchet@37643
   621
blanchet@37624
   622
fun empty_name_pool readable_names =
blanchet@37643
   623
  if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
blanchet@37624
   624
blanchet@37624
   625
fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
blanchet@37624
   626
fun pool_map f xs =
blanchet@37624
   627
  pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
blanchet@37624
   628
blanchet@43088
   629
val no_qualifiers =
blanchet@43088
   630
  let
blanchet@43088
   631
    fun skip [] = []
blanchet@43088
   632
      | skip (#"." :: cs) = skip cs
blanchet@43088
   633
      | skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
blanchet@43088
   634
    and keep [] = []
blanchet@43088
   635
      | keep (#"." :: cs) = skip cs
blanchet@43088
   636
      | keep (c :: cs) = c :: keep cs
blanchet@43088
   637
  in String.explode #> rev #> keep #> rev #> String.implode end
blanchet@43088
   638
blanchet@43626
   639
(* Long names can slow down the ATPs. *)
blanchet@43589
   640
val max_readable_name_size = 20
blanchet@43438
   641
blanchet@43841
   642
(* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
blanchet@43841
   643
   unreadable "op_1", "op_2", etc., in the problem files. "eq" is reserved to
blanchet@43841
   644
   ensure that "HOL.eq" is correctly mapped to equality (not clear whether this
blanchet@43841
   645
   is still necessary). *)
blanchet@46172
   646
val spass_reserved_nice_names =
blanchet@46172
   647
  ["forall", "exists", "le", "ls", "ge", "gs", "plus", "minus", "mult", "fract",
blanchet@46172
   648
   "equal", "true", "false", "or", "and", "not", "implies", "implied", "equiv",
blanchet@46172
   649
   "lr", "def"]
blanchet@46172
   650
val reserved_nice_names =
blanchet@46172
   651
  [tptp_old_equal, "op", "eq"] @ spass_reserved_nice_names
blanchet@43780
   652
blanchet@37624
   653
fun readable_name full_name s =
blanchet@37643
   654
  if s = full_name then
blanchet@37643
   655
    s
blanchet@37643
   656
  else
blanchet@43438
   657
    s |> no_qualifiers
blanchet@45266
   658
      |> perhaps (try (unprefix "'"))
blanchet@43438
   659
      |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
blanchet@43454
   660
      |> (fn s =>
blanchet@43530
   661
             if size s > max_readable_name_size then
blanchet@43530
   662
               String.substring (s, 0, max_readable_name_size div 2 - 4) ^
blanchet@44691
   663
               string_of_int (hash_string full_name) ^
blanchet@43530
   664
               String.extract (s, size s - max_readable_name_size div 2 + 4,
blanchet@43530
   665
                               NONE)
blanchet@43454
   666
             else
blanchet@43454
   667
               s)
blanchet@43438
   668
      |> (fn s => if member (op =) reserved_nice_names s then full_name else s)
blanchet@37624
   669
blanchet@37624
   670
fun nice_name (full_name, _) NONE = (full_name, NONE)
blanchet@37624
   671
  | nice_name (full_name, desired_name) (SOME the_pool) =
blanchet@43839
   672
    if is_built_in_tptp_symbol full_name then
blanchet@39630
   673
      (full_name, SOME the_pool)
blanchet@39630
   674
    else case Symtab.lookup (fst the_pool) full_name of
blanchet@37624
   675
      SOME nice_name => (nice_name, SOME the_pool)
blanchet@37624
   676
    | NONE =>
blanchet@37624
   677
      let
blanchet@37624
   678
        val nice_prefix = readable_name full_name desired_name
blanchet@37624
   679
        fun add j =
blanchet@37624
   680
          let
blanchet@43515
   681
            val nice_name =
blanchet@45266
   682
              nice_prefix ^ (if j = 0 then "" else string_of_int j)
blanchet@37624
   683
          in
blanchet@37624
   684
            case Symtab.lookup (snd the_pool) nice_name of
blanchet@37624
   685
              SOME full_name' =>
blanchet@37624
   686
              if full_name = full_name' then (nice_name, the_pool)
blanchet@37624
   687
              else add (j + 1)
blanchet@37624
   688
            | NONE =>
blanchet@37624
   689
              (nice_name,
blanchet@37624
   690
               (Symtab.update_new (full_name, nice_name) (fst the_pool),
blanchet@37624
   691
                Symtab.update_new (nice_name, full_name) (snd the_pool)))
blanchet@37624
   692
          end
blanchet@37624
   693
      in add 0 |> apsnd SOME end
blanchet@37624
   694
blanchet@45457
   695
fun nice_type (AType (name, tys)) =
blanchet@45457
   696
    nice_name name ##>> pool_map nice_type tys #>> AType
blanchet@43804
   697
  | nice_type (AFun (ty1, ty2)) = nice_type ty1 ##>> nice_type ty2 #>> AFun
blanchet@45458
   698
  | nice_type (ATyAbs (names, ty)) =
blanchet@45458
   699
    pool_map nice_name names ##>> nice_type ty #>> ATyAbs
nik@44535
   700
fun nice_term (ATerm (name, ts)) =
nik@44535
   701
    nice_name name ##>> pool_map nice_term ts #>> ATerm
nik@44535
   702
  | nice_term (AAbs ((name, ty), tm)) =
nik@44535
   703
    nice_name name ##>> nice_type ty ##>> nice_term tm #>> AAbs
blanchet@38198
   704
fun nice_formula (AQuant (q, xs, phi)) =
blanchet@43397
   705
    pool_map nice_name (map fst xs)
blanchet@43397
   706
    ##>> pool_map (fn NONE => pair NONE
blanchet@43804
   707
                    | SOME ty => nice_type ty #>> SOME) (map snd xs)
blanchet@43397
   708
    ##>> nice_formula phi
blanchet@43397
   709
    #>> (fn ((ss, ts), phi) => AQuant (q, ss ~~ ts, phi))
blanchet@38198
   710
  | nice_formula (AConn (c, phis)) =
blanchet@38198
   711
    pool_map nice_formula phis #>> curry AConn c
blanchet@38280
   712
  | nice_formula (AAtom tm) = nice_term tm #>> AAtom
blanchet@43804
   713
fun nice_problem_line (Decl (ident, sym, ty)) =
blanchet@43839
   714
    nice_name sym ##>> nice_type ty #>> (fn (sym, ty) => Decl (ident, sym, ty))
blanchet@43780
   715
  | nice_problem_line (Formula (ident, kind, phi, source, info)) =
blanchet@43780
   716
    nice_formula phi #>> (fn phi => Formula (ident, kind, phi, source, info))
blanchet@38171
   717
fun nice_problem problem =
blanchet@37643
   718
  pool_map (fn (heading, lines) =>
blanchet@38171
   719
               pool_map nice_problem_line lines #>> pair heading) problem
blanchet@39692
   720
fun nice_atp_problem readable_names problem =
blanchet@38251
   721
  nice_problem problem (empty_name_pool readable_names)
blanchet@37509
   722
blanchet@37509
   723
end;