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