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