src/HOL/Tools/ATP/atp_problem.ML
author blanchet
Tue, 26 Jun 2012 11:14:40 +0200
changeset 49148 a5ab5964065f
parent 49147 9aa0fad4e864
child 49150 a44f34694406
permissions -rw-r--r--
implement polymorphic DFG output, without type classes for now
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 =
blanchet@49147
    11
    ATerm of ('a * 'b list) * ('a, 'b) ho_term list |
blanchet@48926
    12
    AAbs of (('a * 'b) * ('a, 'b) ho_term) * ('a, 'b) ho_term list
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@49148
    16
    ATyQuant of quantifier * 'b list * ('a, 'b, 'c) formula |
blanchet@43402
    17
    AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
blanchet@43402
    18
    AConn of connective * ('a, 'b, 'c) formula list |
blanchet@43402
    19
    AAtom of 'c
blanchet@38228
    20
blanchet@45457
    21
  datatype 'a ho_type =
blanchet@45457
    22
    AType of 'a * 'a ho_type list |
blanchet@45458
    23
    AFun of 'a ho_type * 'a ho_type |
blanchet@49148
    24
    APi of 'a list * 'a ho_type
blanchet@43804
    25
blanchet@47909
    26
  type term_order =
blanchet@47909
    27
    {is_lpo : bool,
blanchet@47909
    28
     gen_weights : bool,
blanchet@47909
    29
     gen_prec : bool,
blanchet@47909
    30
     gen_simp : bool}
blanchet@47909
    31
blanchet@49146
    32
  datatype polymorphism = Monomorphic | Polymorphic | Type_Classes
blanchet@45618
    33
  datatype tptp_explicitness = TPTP_Implicit | TPTP_Explicit
blanchet@49019
    34
  datatype thf_choice = THF_Without_Choice | THF_With_Choice
blanchet@49019
    35
  datatype thf_defs = THF_Without_Defs | THF_With_Defs
blanchet@46172
    36
blanchet@46172
    37
  datatype atp_format =
blanchet@45106
    38
    CNF |
blanchet@45106
    39
    CNF_UEQ |
blanchet@45106
    40
    FOF |
blanchet@49145
    41
    TFF of polymorphism * tptp_explicitness |
blanchet@49145
    42
    THF of polymorphism * tptp_explicitness * thf_choice * thf_defs |
blanchet@49146
    43
    DFG of polymorphism
blanchet@45106
    44
blanchet@48991
    45
  datatype formula_role = Axiom | Definition | Lemma | Hypothesis | Conjecture
blanchet@43398
    46
  datatype 'a problem_line =
blanchet@43804
    47
    Decl of string * 'a * 'a ho_type |
blanchet@48991
    48
    Formula of string * formula_role
blanchet@45261
    49
               * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
blanchet@45261
    50
               * (string, string ho_type) ho_term option
blanchet@47234
    51
               * (string, string ho_type) ho_term list
blanchet@38251
    52
  type 'a problem = (string * 'a problem_line list) list
blanchet@38226
    53
blanchet@43809
    54
  val tptp_cnf : string
blanchet@43809
    55
  val tptp_fof : string
blanchet@43809
    56
  val tptp_tff : string
blanchet@43809
    57
  val tptp_thf : string
blanchet@43808
    58
  val tptp_has_type : string
blanchet@43803
    59
  val tptp_type_of_types : string
blanchet@43803
    60
  val tptp_bool_type : string
blanchet@43803
    61
  val tptp_individual_type : string
blanchet@43804
    62
  val tptp_fun_type : string
blanchet@43839
    63
  val tptp_product_type : string
blanchet@43809
    64
  val tptp_forall : string
nik@44537
    65
  val tptp_ho_forall : string
blanchet@45514
    66
  val tptp_pi_binder : string
blanchet@43809
    67
  val tptp_exists : string
nik@44537
    68
  val tptp_ho_exists : string
nik@45350
    69
  val tptp_choice : string
blanchet@43809
    70
  val tptp_not : string
blanchet@43809
    71
  val tptp_and : string
blanchet@43809
    72
  val tptp_or : string
blanchet@43809
    73
  val tptp_implies : string
blanchet@43809
    74
  val tptp_if : string
blanchet@43809
    75
  val tptp_iff : string
blanchet@43809
    76
  val tptp_not_iff : string
blanchet@43809
    77
  val tptp_app : string
blanchet@43809
    78
  val tptp_not_infix : string
blanchet@43809
    79
  val tptp_equal : string
blanchet@43841
    80
  val tptp_old_equal : string
blanchet@43807
    81
  val tptp_false : string
blanchet@43807
    82
  val tptp_true : string
blanchet@43841
    83
  val tptp_empty_list : string
blanchet@47234
    84
  val isabelle_info_prefix : string
blanchet@47909
    85
  val isabelle_info : string -> int -> (string, 'a) ho_term list
blanchet@47270
    86
  val extract_isabelle_status : (string, 'a) ho_term list -> string option
blanchet@47270
    87
  val extract_isabelle_rank : (string, 'a) ho_term list -> int
blanchet@47234
    88
  val introN : string
blanchet@48019
    89
  val inductiveN : string
blanchet@47234
    90
  val elimN : string
blanchet@47234
    91
  val simpN : string
blanchet@48019
    92
  val defN : string
blanchet@47234
    93
  val rankN : string
blanchet@47234
    94
  val minimum_rank : int
blanchet@47234
    95
  val default_rank : int
blanchet@47901
    96
  val default_term_order_weight : int
blanchet@43841
    97
  val is_tptp_equal : string -> bool
blanchet@43839
    98
  val is_built_in_tptp_symbol : string -> bool
blanchet@43839
    99
  val is_tptp_variable : string -> bool
blanchet@43839
   100
  val is_tptp_user_symbol : string -> bool
blanchet@45459
   101
  val atype_of_types : (string * string) ho_type
blanchet@45459
   102
  val bool_atype : (string * string) ho_type
blanchet@45459
   103
  val individual_atype : (string * string) ho_type
blanchet@43783
   104
  val mk_anot : ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
blanchet@43783
   105
  val mk_aconn :
blanchet@43783
   106
    connective -> ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
blanchet@43783
   107
    -> ('a, 'b, 'c) formula
blanchet@43839
   108
  val aconn_fold :
blanchet@43839
   109
    bool option -> (bool option -> 'a -> 'b -> 'b) -> connective * 'a list
blanchet@43839
   110
    -> 'b -> 'b
blanchet@43839
   111
  val aconn_map :
blanchet@43839
   112
    bool option -> (bool option -> 'a -> ('b, 'c, 'd) formula)
blanchet@43839
   113
    -> connective * 'a list -> ('b, 'c, 'd) formula
blanchet@43839
   114
  val formula_fold :
blanchet@43839
   115
    bool option -> (bool option -> 'c -> 'd -> 'd) -> ('a, 'b, 'c) formula
blanchet@43839
   116
    -> 'd -> 'd
blanchet@43785
   117
  val formula_map : ('c -> 'd) -> ('a, 'b, 'c) formula -> ('a, 'b, 'd) formula
blanchet@47270
   118
  val is_function_type : string ho_type -> bool
blanchet@47270
   119
  val is_predicate_type : string ho_type -> bool
blanchet@46174
   120
  val is_format_higher_order : atp_format -> bool
blanchet@47270
   121
  val lines_for_atp_problem :
blanchet@47909
   122
    atp_format -> term_order -> (unit -> (string * int) list) -> string problem
blanchet@47909
   123
    -> string list
blanchet@43933
   124
  val ensure_cnf_problem :
blanchet@43933
   125
    (string * string) problem -> (string * string) problem
blanchet@43780
   126
  val filter_cnf_ueq_problem :
blanchet@43780
   127
    (string * string) problem -> (string * string) problem
blanchet@48021
   128
  val declared_syms_in_problem : 'a problem -> 'a list
blanchet@39692
   129
  val nice_atp_problem :
blanchet@46810
   130
    bool -> atp_format -> ('a * (string * string) problem_line list) list
blanchet@38251
   131
    -> ('a * string problem_line list) list
blanchet@38251
   132
       * (string Symtab.table * string Symtab.table) option
blanchet@37509
   133
end;
blanchet@37509
   134
blanchet@38253
   135
structure ATP_Problem : ATP_PROBLEM =
blanchet@37509
   136
struct
blanchet@37509
   137
blanchet@43926
   138
open ATP_Util
blanchet@43926
   139
blanchet@43926
   140
blanchet@37643
   141
(** ATP problem **)
blanchet@37643
   142
nik@44535
   143
datatype ('a, 'b) ho_term =
blanchet@49147
   144
  ATerm of ('a * 'b list) * ('a, 'b) ho_term list |
blanchet@48926
   145
  AAbs of (('a * 'b) * ('a, 'b) ho_term) * ('a, 'b) ho_term list
blanchet@38198
   146
datatype quantifier = AForall | AExists
blanchet@44004
   147
datatype connective = ANot | AAnd | AOr | AImplies | AIff
blanchet@43402
   148
datatype ('a, 'b, 'c) formula =
blanchet@49148
   149
  ATyQuant of quantifier * 'b list * ('a, 'b, 'c) formula |
blanchet@43402
   150
  AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
blanchet@43402
   151
  AConn of connective * ('a, 'b, 'c) formula list |
blanchet@43402
   152
  AAtom of 'c
blanchet@38198
   153
blanchet@45457
   154
datatype 'a ho_type =
blanchet@45457
   155
  AType of 'a * 'a ho_type list |
blanchet@45458
   156
  AFun of 'a ho_type * 'a ho_type |
blanchet@49148
   157
  APi of 'a list * 'a ho_type
blanchet@43804
   158
blanchet@47909
   159
type term_order =
blanchet@47909
   160
  {is_lpo : bool,
blanchet@47909
   161
   gen_weights : bool,
blanchet@47909
   162
   gen_prec : bool,
blanchet@47909
   163
   gen_simp : bool}
blanchet@47909
   164
blanchet@49146
   165
datatype polymorphism = Monomorphic | Polymorphic | Type_Classes
blanchet@45618
   166
datatype tptp_explicitness = TPTP_Implicit | TPTP_Explicit
blanchet@49019
   167
datatype thf_choice = THF_Without_Choice | THF_With_Choice
blanchet@49019
   168
datatype thf_defs = THF_Without_Defs | THF_With_Defs
blanchet@45354
   169
blanchet@46172
   170
datatype atp_format =
blanchet@45106
   171
  CNF |
blanchet@45106
   172
  CNF_UEQ |
blanchet@45106
   173
  FOF |
blanchet@49145
   174
  TFF of polymorphism * tptp_explicitness |
blanchet@49145
   175
  THF of polymorphism * tptp_explicitness * thf_choice * thf_defs |
blanchet@49146
   176
  DFG of polymorphism
blanchet@45106
   177
blanchet@48991
   178
datatype formula_role = Axiom | Definition | Lemma | Hypothesis | Conjecture
blanchet@43398
   179
datatype 'a problem_line =
blanchet@43804
   180
  Decl of string * 'a * 'a ho_type |
blanchet@48991
   181
  Formula of string * formula_role
blanchet@47234
   182
             * ('a, 'a ho_type, ('a, 'a ho_type) ho_term) formula
blanchet@47234
   183
             * (string, string ho_type) ho_term option
blanchet@47234
   184
             * (string, string ho_type) ho_term list
blanchet@37643
   185
type 'a problem = (string * 'a problem_line list) list
blanchet@37643
   186
blanchet@43587
   187
(* official TPTP syntax *)
blanchet@43809
   188
val tptp_cnf = "cnf"
blanchet@43809
   189
val tptp_fof = "fof"
blanchet@43809
   190
val tptp_tff = "tff"
blanchet@43809
   191
val tptp_thf = "thf"
blanchet@43808
   192
val tptp_has_type = ":"
blanchet@43803
   193
val tptp_type_of_types = "$tType"
blanchet@43803
   194
val tptp_bool_type = "$o"
blanchet@43803
   195
val tptp_individual_type = "$i"
blanchet@43804
   196
val tptp_fun_type = ">"
blanchet@43839
   197
val tptp_product_type = "*"
blanchet@43809
   198
val tptp_forall = "!"
nik@44537
   199
val tptp_ho_forall = "!!"
blanchet@45514
   200
val tptp_pi_binder = "!>"
blanchet@43809
   201
val tptp_exists = "?"
nik@44537
   202
val tptp_ho_exists = "??"
nik@45350
   203
val tptp_choice = "@+"
blanchet@43809
   204
val tptp_not = "~"
blanchet@43809
   205
val tptp_and = "&"
blanchet@43809
   206
val tptp_or = "|"
blanchet@43809
   207
val tptp_implies = "=>"
blanchet@43809
   208
val tptp_if = "<="
blanchet@43809
   209
val tptp_iff = "<=>"
blanchet@43809
   210
val tptp_not_iff = "<~>"
blanchet@43809
   211
val tptp_app = "@"
blanchet@43809
   212
val tptp_not_infix = "!"
blanchet@43809
   213
val tptp_equal = "="
blanchet@43841
   214
val tptp_old_equal = "equal"
blanchet@43807
   215
val tptp_false = "$false"
blanchet@43807
   216
val tptp_true = "$true"
blanchet@43841
   217
val tptp_empty_list = "[]"
blanchet@43587
   218
blanchet@47234
   219
val isabelle_info_prefix = "isabelle_"
blanchet@47234
   220
blanchet@47234
   221
val introN = "intro"
blanchet@48019
   222
val inductiveN = "inductive"
blanchet@47234
   223
val elimN = "elim"
blanchet@47234
   224
val simpN = "simp"
blanchet@48019
   225
val defN = "def"
blanchet@47234
   226
val rankN = "rank"
blanchet@47234
   227
blanchet@47234
   228
val minimum_rank = 0
blanchet@47234
   229
val default_rank = 1000
blanchet@47901
   230
val default_term_order_weight = 1
blanchet@47234
   231
blanchet@49144
   232
(* Currently, only SPASS 3.8ds can process Isabelle metainformation. *)
blanchet@47909
   233
fun isabelle_info status rank =
blanchet@47909
   234
  [] |> rank <> default_rank
blanchet@49147
   235
        ? cons (ATerm ((isabelle_info_prefix ^ rankN, []),
blanchet@49147
   236
                       [ATerm ((string_of_int rank, []), [])]))
blanchet@49147
   237
     |> status <> "" ? cons (ATerm ((isabelle_info_prefix ^ status, []), []))
blanchet@47234
   238
blanchet@49147
   239
fun extract_isabelle_status (ATerm ((s, []), []) :: _) =
blanchet@47234
   240
    try (unprefix isabelle_info_prefix) s
blanchet@47234
   241
  | extract_isabelle_status _ = NONE
blanchet@47234
   242
blanchet@47234
   243
fun extract_isabelle_rank (tms as _ :: _) =
blanchet@47234
   244
    (case List.last tms of
blanchet@49147
   245
       ATerm ((_, []), [ATerm ((rank, []), [])]) => the (Int.fromString rank)
blanchet@47234
   246
     | _ => default_rank)
blanchet@47234
   247
  | extract_isabelle_rank _ = default_rank
blanchet@47234
   248
blanchet@43841
   249
fun is_tptp_equal s = (s = tptp_equal orelse s = tptp_old_equal)
blanchet@43841
   250
fun is_built_in_tptp_symbol s =
blanchet@43841
   251
  s = tptp_old_equal orelse not (Char.isAlpha (String.sub (s, 0)))
blanchet@43839
   252
fun is_tptp_variable s = Char.isUpper (String.sub (s, 0))
blanchet@43839
   253
val is_tptp_user_symbol = not o (is_tptp_variable orf is_built_in_tptp_symbol)
blanchet@43780
   254
blanchet@46172
   255
val atype_of_types = AType (`I tptp_type_of_types, [])
blanchet@46172
   256
val bool_atype = AType (`I tptp_bool_type, [])
blanchet@46172
   257
val individual_atype = AType (`I tptp_individual_type, [])
blanchet@46172
   258
blanchet@43939
   259
fun raw_polarities_of_conn ANot = (SOME false, NONE)
blanchet@43939
   260
  | raw_polarities_of_conn AAnd = (SOME true, SOME true)
blanchet@43939
   261
  | raw_polarities_of_conn AOr = (SOME true, SOME true)
blanchet@43939
   262
  | raw_polarities_of_conn AImplies = (SOME false, SOME true)
blanchet@43939
   263
  | raw_polarities_of_conn AIff = (NONE, NONE)
blanchet@43939
   264
fun polarities_of_conn NONE = K (NONE, NONE)
blanchet@43939
   265
  | polarities_of_conn (SOME pos) =
blanchet@43939
   266
    raw_polarities_of_conn #> not pos ? pairself (Option.map not)
blanchet@43939
   267
blanchet@43783
   268
fun mk_anot (AConn (ANot, [phi])) = phi
blanchet@43783
   269
  | mk_anot phi = AConn (ANot, [phi])
blanchet@43783
   270
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
blanchet@43783
   271
blanchet@43839
   272
fun aconn_fold pos f (ANot, [phi]) = f (Option.map not pos) phi
blanchet@43839
   273
  | aconn_fold pos f (AImplies, [phi1, phi2]) =
blanchet@43839
   274
    f (Option.map not pos) phi1 #> f pos phi2
blanchet@43839
   275
  | aconn_fold pos f (AAnd, phis) = fold (f pos) phis
blanchet@43839
   276
  | aconn_fold pos f (AOr, phis) = fold (f pos) phis
blanchet@43839
   277
  | aconn_fold _ f (_, phis) = fold (f NONE) phis
blanchet@43839
   278
blanchet@43839
   279
fun aconn_map pos f (ANot, [phi]) = AConn (ANot, [f (Option.map not pos) phi])
blanchet@43839
   280
  | aconn_map pos f (AImplies, [phi1, phi2]) =
blanchet@43839
   281
    AConn (AImplies, [f (Option.map not pos) phi1, f pos phi2])
blanchet@43839
   282
  | aconn_map pos f (AAnd, phis) = AConn (AAnd, map (f pos) phis)
blanchet@43839
   283
  | aconn_map pos f (AOr, phis) = AConn (AOr, map (f pos) phis)
blanchet@43839
   284
  | aconn_map _ f (c, phis) = AConn (c, map (f NONE) phis)
blanchet@43839
   285
blanchet@43839
   286
fun formula_fold pos f =
blanchet@43839
   287
  let
blanchet@45356
   288
    fun fld pos (AQuant (_, _, phi)) = fld pos phi
blanchet@49148
   289
      | fld pos (ATyQuant (_, _, phi)) = fld pos phi
blanchet@45356
   290
      | fld pos (AConn conn) = aconn_fold pos fld conn
blanchet@45356
   291
      | fld pos (AAtom tm) = f pos tm
blanchet@45356
   292
  in fld pos end
blanchet@43839
   293
blanchet@43785
   294
fun formula_map f (AQuant (q, xs, phi)) = AQuant (q, xs, formula_map f phi)
blanchet@49148
   295
  | formula_map f (ATyQuant (q, xs, phi)) = ATyQuant (q, xs, formula_map f phi)
blanchet@43785
   296
  | formula_map f (AConn (c, phis)) = AConn (c, map (formula_map f) phis)
blanchet@43785
   297
  | formula_map f (AAtom tm) = AAtom (f tm)
blanchet@43785
   298
blanchet@49148
   299
fun is_function_type (APi (_, ty)) = is_function_type ty
blanchet@49148
   300
  | is_function_type (AFun (_, ty)) = is_function_type ty
blanchet@47270
   301
  | is_function_type (AType (s, _)) =
blanchet@47270
   302
    s <> tptp_type_of_types andalso s <> tptp_bool_type
blanchet@47270
   303
  | is_function_type _ = false
blanchet@49148
   304
fun is_predicate_type (APi (_, ty)) = is_predicate_type ty
blanchet@49148
   305
  | is_predicate_type (AFun (_, ty)) = is_predicate_type ty
blanchet@47270
   306
  | is_predicate_type (AType (s, _)) = (s = tptp_bool_type)
blanchet@47270
   307
  | is_predicate_type _ = false
blanchet@49148
   308
fun is_nontrivial_predicate_type (AType _) = false
blanchet@49148
   309
  | is_nontrivial_predicate_type ty = is_predicate_type ty
blanchet@47270
   310
blanchet@46174
   311
fun is_format_higher_order (THF _) = true
blanchet@46174
   312
  | is_format_higher_order _ = false
blanchet@45354
   313
fun is_format_typed (TFF _) = true
blanchet@45618
   314
  | is_format_typed (THF _) = true
blanchet@49146
   315
  | is_format_typed (DFG _) = true
blanchet@45106
   316
  | is_format_typed _ = false
blanchet@43839
   317
blanchet@48991
   318
fun tptp_string_for_role Axiom = "axiom"
blanchet@48991
   319
  | tptp_string_for_role Definition = "definition"
blanchet@48991
   320
  | tptp_string_for_role Lemma = "lemma"
blanchet@48991
   321
  | tptp_string_for_role Hypothesis = "hypothesis"
blanchet@48991
   322
  | tptp_string_for_role Conjecture = "conjecture"
blanchet@38854
   323
blanchet@49148
   324
fun tptp_string_for_app _ func [] = func
blanchet@49148
   325
  | tptp_string_for_app format func args =
blanchet@49148
   326
    if is_format_higher_order format then
blanchet@49148
   327
      "(" ^ space_implode (" " ^ tptp_app ^ " ") (func :: args) ^ ")"
blanchet@49148
   328
    else
blanchet@49148
   329
      func ^ "(" ^ commas args ^ ")"
blanchet@45655
   330
blanchet@49148
   331
fun flatten_type (APi (tys, ty)) = APi (tys, flatten_type ty)
blanchet@45458
   332
  | flatten_type (ty as AFun (ty1 as AType _, ty2)) =
blanchet@45458
   333
    (case flatten_type ty2 of
blanchet@45458
   334
       AFun (ty' as AType (s, tys), ty) =>
blanchet@45458
   335
       AFun (AType (tptp_product_type,
blanchet@45458
   336
                    ty1 :: (if s = tptp_product_type then tys else [ty'])), ty)
blanchet@45458
   337
     | _ => ty)
blanchet@45458
   338
  | flatten_type (ty as AType _) = ty
blanchet@45458
   339
  | flatten_type _ =
blanchet@43804
   340
    raise Fail "unexpected higher-order type in first-order format"
blanchet@43804
   341
blanchet@47273
   342
val dfg_individual_type = "iii" (* cannot clash *)
blanchet@47166
   343
blanchet@49148
   344
val suffix_type_of_types =
blanchet@49148
   345
  suffix (" " ^ tptp_has_type ^ " " ^ tptp_type_of_types)
blanchet@49148
   346
blanchet@45655
   347
fun str_for_type format ty =
blanchet@45457
   348
  let
blanchet@49146
   349
    val dfg = case format of DFG _ => true | _ => false
blanchet@46172
   350
    fun str _ (AType (s, [])) =
blanchet@47166
   351
        if dfg andalso s = tptp_individual_type then dfg_individual_type else s
blanchet@45458
   352
      | str _ (AType (s, tys)) =
blanchet@45655
   353
        let val ss = tys |> map (str false) in
blanchet@45655
   354
          if s = tptp_product_type then
blanchet@46172
   355
            ss |> space_implode
blanchet@46172
   356
                      (if dfg then ", " else " " ^ tptp_product_type ^ " ")
blanchet@46172
   357
               |> (not dfg andalso length ss > 1) ? enclose "(" ")"
blanchet@45655
   358
          else
blanchet@46172
   359
            tptp_string_for_app format s ss
blanchet@45655
   360
        end
blanchet@45457
   361
      | str rhs (AFun (ty1, ty2)) =
blanchet@46172
   362
        (str false ty1 |> dfg ? enclose "(" ")") ^ " " ^
blanchet@46172
   363
        (if dfg then "" else tptp_fun_type ^ " ") ^ str true ty2
blanchet@45457
   364
        |> not rhs ? enclose "(" ")"
blanchet@49148
   365
      | str _ (APi (ss, ty)) =
blanchet@49148
   366
        if dfg then
blanchet@49148
   367
          "[" ^ commas ss ^ "], " ^ str true ty
blanchet@49148
   368
        else
blanchet@49148
   369
          tptp_pi_binder ^ "[" ^ commas (map suffix_type_of_types ss) ^ "]: " ^
blanchet@49148
   370
          str false ty
blanchet@45457
   371
  in str true ty end
blanchet@45457
   372
blanchet@45655
   373
fun string_for_type (format as THF _) ty = str_for_type format ty
blanchet@46172
   374
  | string_for_type format ty = str_for_type format (flatten_type ty)
blanchet@43804
   375
blanchet@46172
   376
fun tptp_string_for_quantifier AForall = tptp_forall
blanchet@46172
   377
  | tptp_string_for_quantifier AExists = tptp_exists
blanchet@43804
   378
blanchet@46172
   379
fun tptp_string_for_connective ANot = tptp_not
blanchet@46172
   380
  | tptp_string_for_connective AAnd = tptp_and
blanchet@46172
   381
  | tptp_string_for_connective AOr = tptp_or
blanchet@46172
   382
  | tptp_string_for_connective AImplies = tptp_implies
blanchet@46172
   383
  | tptp_string_for_connective AIff = tptp_iff
blanchet@43804
   384
blanchet@43803
   385
fun string_for_bound_var format (s, ty) =
blanchet@45457
   386
  s ^
blanchet@45457
   387
  (if is_format_typed format then
blanchet@45457
   388
     " " ^ tptp_has_type ^ " " ^
blanchet@45457
   389
     (ty |> the_default (AType (tptp_individual_type, []))
blanchet@45457
   390
         |> string_for_type format)
blanchet@45457
   391
   else
blanchet@45457
   392
     "")
blanchet@43804
   393
blanchet@49147
   394
fun tptp_string_for_term _ (ATerm ((s, []), [])) = s
blanchet@49148
   395
  | tptp_string_for_term format (ATerm ((s, tys), ts)) =
blanchet@45655
   396
    (if s = tptp_empty_list then
blanchet@45655
   397
       (* used for lists in the optional "source" field of a derivation *)
blanchet@46172
   398
       "[" ^ commas (map (tptp_string_for_term format) ts) ^ "]"
blanchet@45655
   399
     else if is_tptp_equal s then
blanchet@46172
   400
       space_implode (" " ^ tptp_equal ^ " ")
blanchet@46172
   401
                     (map (tptp_string_for_term format) ts)
blanchet@46174
   402
       |> is_format_higher_order format ? enclose "(" ")"
blanchet@49019
   403
     else case (s = tptp_ho_forall orelse s = tptp_ho_exists, s = tptp_choice,
blanchet@49019
   404
                ts) of
blanchet@48926
   405
       (true, _, [AAbs (((s', ty), tm), [])]) =>
blanchet@45655
   406
       (* Use syntactic sugar "!" and "?" instead of "!!" and "??" whenever
blanchet@45655
   407
          possible, to work around LEO-II 1.2.8 parser limitation. *)
blanchet@46172
   408
       tptp_string_for_formula format
blanchet@45655
   409
           (AQuant (if s = tptp_ho_forall then AForall else AExists,
blanchet@45655
   410
                    [(s', SOME ty)], AAtom tm))
blanchet@48926
   411
     | (_, true, [AAbs (((s', ty), tm), args)]) =>
blanchet@47148
   412
       (* There is code in "ATP_Problem_Generate" to ensure that "Eps" is always
blanchet@45655
   413
          applied to an abstraction. *)
blanchet@48926
   414
       tptp_string_for_app format
blanchet@48926
   415
           (tptp_choice ^ "[" ^ s' ^ " : " ^ string_for_type format ty ^ "]: " ^
blanchet@48926
   416
            tptp_string_for_term format tm ^ ""
blanchet@48926
   417
            |> enclose "(" ")")
blanchet@48926
   418
           (map (tptp_string_for_term format) args)
blanchet@49148
   419
     | _ =>
blanchet@49148
   420
       tptp_string_for_app format s
blanchet@49148
   421
           (map (string_for_type format) tys
blanchet@49148
   422
            @ map (tptp_string_for_term format) ts))
blanchet@48926
   423
  | tptp_string_for_term (format as THF _) (AAbs (((s, ty), tm), args)) =
blanchet@48926
   424
    tptp_string_for_app format
blanchet@48926
   425
        ("(^[" ^ s ^ " : " ^ string_for_type format ty ^ "]: " ^
blanchet@48926
   426
         tptp_string_for_term format tm ^ ")")
blanchet@48926
   427
        (map (tptp_string_for_term format) args)
blanchet@46172
   428
  | tptp_string_for_term _ _ =
blanchet@46172
   429
    raise Fail "unexpected term in first-order format"
blanchet@49148
   430
and tptp_string_for_formula format (ATyQuant (q, xs, phi)) =
blanchet@49148
   431
    tptp_string_for_quantifier q ^
blanchet@49148
   432
    "[" ^ commas (map (suffix_type_of_types o string_for_type format) xs) ^
blanchet@49148
   433
    "]: " ^ tptp_string_for_formula format phi
blanchet@49148
   434
    |> enclose "(" ")"
blanchet@49148
   435
  | tptp_string_for_formula format (AQuant (q, xs, phi)) =
blanchet@46172
   436
    tptp_string_for_quantifier q ^
blanchet@45603
   437
    "[" ^ commas (map (string_for_bound_var format) xs) ^ "]: " ^
blanchet@46172
   438
    tptp_string_for_formula format phi
blanchet@43815
   439
    |> enclose "(" ")"
blanchet@46172
   440
  | tptp_string_for_formula format
blanchet@49147
   441
        (AConn (ANot, [AAtom (ATerm (("=" (* tptp_equal *), []), ts))])) =
blanchet@43809
   442
    space_implode (" " ^ tptp_not_infix ^ tptp_equal ^ " ")
blanchet@46172
   443
                  (map (tptp_string_for_term format) ts)
blanchet@46174
   444
    |> is_format_higher_order format ? enclose "(" ")"
blanchet@46172
   445
  | tptp_string_for_formula format (AConn (c, [phi])) =
blanchet@46172
   446
    tptp_string_for_connective c ^ " " ^
blanchet@46172
   447
    (tptp_string_for_formula format phi
blanchet@46174
   448
     |> is_format_higher_order format ? enclose "(" ")")
blanchet@43815
   449
    |> enclose "(" ")"
blanchet@46172
   450
  | tptp_string_for_formula format (AConn (c, phis)) =
blanchet@46172
   451
    space_implode (" " ^ tptp_string_for_connective c ^ " ")
blanchet@46172
   452
                  (map (tptp_string_for_formula format) phis)
blanchet@43815
   453
    |> enclose "(" ")"
blanchet@46172
   454
  | tptp_string_for_formula format (AAtom tm) = tptp_string_for_term format tm
blanchet@38198
   455
blanchet@46172
   456
fun tptp_string_for_format CNF = tptp_cnf
blanchet@46172
   457
  | tptp_string_for_format CNF_UEQ = tptp_cnf
blanchet@46172
   458
  | tptp_string_for_format FOF = tptp_fof
blanchet@46172
   459
  | tptp_string_for_format (TFF _) = tptp_tff
blanchet@46172
   460
  | tptp_string_for_format (THF _) = tptp_thf
blanchet@49146
   461
  | tptp_string_for_format (DFG _) = raise Fail "non-TPTP format"
blanchet@43803
   462
blanchet@46172
   463
fun tptp_string_for_problem_line format (Decl (ident, sym, ty)) =
blanchet@46172
   464
    tptp_string_for_format format ^ "(" ^ ident ^ ", type,\n    " ^ sym ^
blanchet@46172
   465
    " : " ^ string_for_type format ty ^ ").\n"
blanchet@46172
   466
  | tptp_string_for_problem_line format
blanchet@47909
   467
                                 (Formula (ident, kind, phi, source, _)) =
blanchet@46172
   468
    tptp_string_for_format format ^ "(" ^ ident ^ ", " ^
blanchet@48991
   469
    tptp_string_for_role kind ^ ",\n    (" ^
blanchet@46172
   470
    tptp_string_for_formula format phi ^ ")" ^
blanchet@47909
   471
    (case source of
blanchet@47909
   472
       SOME tm => ", " ^ tptp_string_for_term format tm
blanchet@47909
   473
     | NONE => "") ^ ").\n"
blanchet@46172
   474
blanchet@46172
   475
fun tptp_lines format =
blanchet@37643
   476
  maps (fn (_, []) => []
blanchet@37643
   477
         | (heading, lines) =>
wenzelm@41739
   478
           "\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
blanchet@46172
   479
           map (tptp_string_for_problem_line format) lines)
blanchet@46172
   480
blanchet@49148
   481
fun arity_of_type (APi (tys, ty)) =
blanchet@49148
   482
    arity_of_type ty |>> Integer.add (length tys)
blanchet@49148
   483
  | arity_of_type (AFun (_, ty)) = arity_of_type ty ||> Integer.add 1
blanchet@49148
   484
  | arity_of_type _ = (0, 0)
blanchet@46172
   485
blanchet@49148
   486
fun string_of_arity (0, n) = string_of_int n
blanchet@49148
   487
  | string_of_arity (m, n) = string_of_int m ^ "+" ^ string_of_int n
blanchet@49148
   488
blanchet@49148
   489
fun strip_atype (APi (tys, ty)) = strip_atype ty |>> apfst (append tys)
blanchet@49148
   490
  | strip_atype (AFun (ty1, ty2)) = strip_atype ty2 |>> apsnd (cons ty1)
blanchet@49148
   491
  | strip_atype ty = (([], []), ty)
blanchet@46172
   492
blanchet@49146
   493
fun dfg_string_for_formula poly gen_simp info =
blanchet@46172
   494
  let
blanchet@49148
   495
    val str_for_typ = string_for_type (DFG poly)
blanchet@47207
   496
    fun suffix_tag top_level s =
blanchet@49144
   497
      if top_level then
blanchet@47234
   498
        case extract_isabelle_status info of
blanchet@48019
   499
          SOME s' => if s' = defN then s ^ ":lt"
blanchet@47909
   500
                     else if s' = simpN andalso gen_simp then s ^ ":lr"
blanchet@47207
   501
                     else s
blanchet@47207
   502
        | NONE => s
blanchet@47207
   503
      else
blanchet@47207
   504
        s
blanchet@49147
   505
    fun str_for_term top_level (ATerm ((s, tys), tms)) =
blanchet@47207
   506
        (if is_tptp_equal s then "equal" |> suffix_tag top_level
blanchet@46172
   507
         else if s = tptp_true then "true"
blanchet@46172
   508
         else if s = tptp_false then "false"
blanchet@46172
   509
         else s) ^
blanchet@49148
   510
        (if null tys then ""
blanchet@49148
   511
         else "<" ^ commas (map (string_for_type (DFG poly)) tys) ^ ">") ^
blanchet@46172
   512
        (if null tms then ""
blanchet@46172
   513
         else "(" ^ commas (map (str_for_term false) tms) ^ ")")
blanchet@46172
   514
      | str_for_term _ _ = raise Fail "unexpected term in first-order format"
blanchet@46172
   515
    fun str_for_quant AForall = "forall"
blanchet@46172
   516
      | str_for_quant AExists = "exists"
blanchet@46172
   517
    fun str_for_conn _ ANot = "not"
blanchet@46172
   518
      | str_for_conn _ AAnd = "and"
blanchet@46172
   519
      | str_for_conn _ AOr = "or"
blanchet@46172
   520
      | str_for_conn _ AImplies = "implies"
blanchet@47207
   521
      | str_for_conn top_level AIff = "equiv" |> suffix_tag top_level
blanchet@49148
   522
    fun str_for_formula top_level (ATyQuant (q, xs, phi)) =
blanchet@49148
   523
        str_for_quant q ^ "_sorts([" ^ commas (map str_for_typ xs) ^
blanchet@49148
   524
        "], " ^ str_for_formula top_level phi ^ ")"
blanchet@49148
   525
      | str_for_formula top_level (AQuant (q, xs, phi)) =
blanchet@49148
   526
        str_for_quant q ^ "([" ^
blanchet@49146
   527
        commas (map (string_for_bound_var (DFG poly)) xs) ^ "], " ^
blanchet@47207
   528
        str_for_formula top_level phi ^ ")"
blanchet@47207
   529
      | str_for_formula top_level (AConn (c, phis)) =
blanchet@47207
   530
        str_for_conn top_level c ^ "(" ^
blanchet@46172
   531
        commas (map (str_for_formula false) phis) ^ ")"
blanchet@47207
   532
      | str_for_formula top_level (AAtom tm) = str_for_term top_level tm
blanchet@47207
   533
  in str_for_formula true end
blanchet@46172
   534
blanchet@47909
   535
fun maybe_enclose bef aft "" = "% " ^ bef ^ aft
blanchet@47909
   536
  | maybe_enclose bef aft s = bef ^ s ^ aft
blanchet@47909
   537
blanchet@49146
   538
fun dfg_lines poly {is_lpo, gen_weights, gen_prec, gen_simp} ord_info problem =
blanchet@46172
   539
  let
blanchet@49148
   540
    val str_for_typ = string_for_type (DFG poly)
blanchet@49148
   541
    fun spair (s, s') = "(" ^ s ^ ", " ^ s' ^ ")"
blanchet@49148
   542
    fun tm_ary sym ty = spair (sym, string_of_arity (arity_of_type ty))
blanchet@49148
   543
    fun ty_ary [] sym = sym
blanchet@49148
   544
      | ty_ary tys sym = "(" ^ sym ^ ", " ^ string_of_int (length tys) ^ ")"
blanchet@49148
   545
    fun fun_typ sym ty = "function(" ^ sym ^ ", " ^ str_for_typ ty ^ ")."
blanchet@46172
   546
    fun pred_typ sym ty =
blanchet@49148
   547
      let
blanchet@49148
   548
        val ((ty_vars, tys), _) = strip_atype ty
blanchet@49148
   549
        val (ty_vars, tys) =
blanchet@49148
   550
          strip_atype ty |> fst
blanchet@49148
   551
          |>> (fn [] => [] | ty_vars => ["[" ^ commas ty_vars ^ "]"])
blanchet@49148
   552
      in "predicate(" ^ commas (sym :: ty_vars @ map str_for_typ tys) ^ ")." end
blanchet@46172
   553
    fun formula pred (Formula (ident, kind, phi, _, info)) =
blanchet@46172
   554
        if pred kind then
blanchet@49144
   555
          let val rank = extract_isabelle_rank info in
blanchet@49146
   556
            "formula(" ^ dfg_string_for_formula poly gen_simp info phi ^
blanchet@47909
   557
            ", " ^ ident ^
blanchet@47234
   558
            (if rank = default_rank then "" else ", " ^ string_of_int rank) ^
blanchet@47234
   559
            ")." |> SOME
blanchet@47234
   560
          end
blanchet@46172
   561
        else
blanchet@46172
   562
          NONE
blanchet@46172
   563
      | formula _ _ = NONE
blanchet@47241
   564
    fun filt f = problem |> map (map_filter f o snd) |> filter_out null
blanchet@46172
   565
    val func_aries =
blanchet@46172
   566
      filt (fn Decl (_, sym, ty) =>
blanchet@49148
   567
               if is_function_type ty then SOME (tm_ary sym ty) else NONE
blanchet@46172
   568
             | _ => NONE)
blanchet@47909
   569
      |> flat |> commas |> maybe_enclose "functions [" "]."
blanchet@46172
   570
    val pred_aries =
blanchet@46172
   571
      filt (fn Decl (_, sym, ty) =>
blanchet@49148
   572
               if is_predicate_type ty then SOME (tm_ary sym ty) else NONE
blanchet@46172
   573
             | _ => NONE)
blanchet@47909
   574
      |> flat |> commas |> maybe_enclose "predicates [" "]."
blanchet@49144
   575
    val sorts =
blanchet@49148
   576
      filt (fn Decl (_, sym, ty) =>
blanchet@49148
   577
               (case strip_atype ty of
blanchet@49148
   578
                  (([], tys), AType (s, [])) =>
blanchet@49148
   579
                  if s = tptp_type_of_types then SOME (ty_ary tys sym) else NONE
blanchet@49148
   580
                | _ => NONE)
blanchet@47241
   581
             | _ => NONE) @ [[dfg_individual_type]]
blanchet@47909
   582
      |> flat |> commas |> maybe_enclose "sorts [" "]."
blanchet@49144
   583
    val ord_info = if gen_weights orelse gen_prec then ord_info () else []
blanchet@49144
   584
    val do_term_order_weights =
blanchet@47909
   585
      (if gen_weights then ord_info else [])
blanchet@49148
   586
      |> map (spair o apsnd string_of_int) |> commas
blanchet@49148
   587
      |> maybe_enclose "weights [" "]."
blanchet@49144
   588
    val syms = [func_aries, pred_aries, do_term_order_weights, sorts]
blanchet@49144
   589
    val func_sigs =
blanchet@46172
   590
      filt (fn Decl (_, sym, ty) =>
blanchet@46172
   591
               if is_function_type ty then SOME (fun_typ sym ty) else NONE
blanchet@46172
   592
             | _ => NONE)
blanchet@47241
   593
      |> flat
blanchet@49144
   594
    val pred_sigs =
blanchet@46172
   595
      filt (fn Decl (_, sym, ty) =>
blanchet@47219
   596
               if is_nontrivial_predicate_type ty then SOME (pred_typ sym ty)
blanchet@47219
   597
               else NONE
blanchet@46172
   598
             | _ => NONE)
blanchet@47241
   599
      |> flat
blanchet@49144
   600
    val decls = func_sigs @ pred_sigs
blanchet@47241
   601
    val axioms =
blanchet@47241
   602
      filt (formula (curry (op <>) Conjecture)) |> separate [""] |> flat
blanchet@47241
   603
    val conjs =
blanchet@47241
   604
      filt (formula (curry (op =) Conjecture)) |> separate [""] |> flat
blanchet@47909
   605
    val settings =
blanchet@47924
   606
      (if is_lpo then ["set_flag(Ordering, 1)."] else []) @
blanchet@47909
   607
      (if gen_prec then
blanchet@47909
   608
         [ord_info |> map fst |> rev |> commas
blanchet@47909
   609
                   |> maybe_enclose "set_precedence(" ")."]
blanchet@47909
   610
       else
blanchet@47909
   611
         [])
blanchet@47923
   612
    fun list_of _ [] = []
blanchet@47923
   613
      | list_of heading ss =
blanchet@47923
   614
        "list_of_" ^ heading ^ ".\n" :: map (suffix "\n") ss @
blanchet@47923
   615
        ["end_of_list.\n\n"]
blanchet@46172
   616
  in
blanchet@46172
   617
    "\nbegin_problem(isabelle).\n\n" ::
blanchet@47923
   618
    list_of "descriptions"
blanchet@46172
   619
            ["name({**}).", "author({**}).", "status(unknown).",
blanchet@46172
   620
             "description({**})."] @
blanchet@47923
   621
    list_of "symbols" syms @
blanchet@47923
   622
    list_of "declarations" decls @
blanchet@47923
   623
    list_of "formulae(axioms)" axioms @
blanchet@47923
   624
    list_of "formulae(conjectures)" conjs @
blanchet@47923
   625
    list_of "settings(SPASS)" settings @
blanchet@46172
   626
    ["end_problem.\n"]
blanchet@46172
   627
  end
blanchet@46172
   628
blanchet@47909
   629
fun lines_for_atp_problem format ord ord_info problem =
blanchet@46172
   630
  "% This file was generated by Isabelle (most likely Sledgehammer)\n\
blanchet@46172
   631
  \% " ^ timestamp () ^ "\n" ::
blanchet@49146
   632
  (case format of
blanchet@49146
   633
     DFG poly => dfg_lines poly ord ord_info
blanchet@49146
   634
   | _ => tptp_lines format) problem
blanchet@37643
   635
blanchet@43780
   636
blanchet@43939
   637
(** CNF (Metis) and CNF UEQ (Waldmeister) **)
blanchet@43780
   638
blanchet@43780
   639
fun is_problem_line_negated (Formula (_, _, AConn (ANot, _), _, _)) = true
blanchet@43780
   640
  | is_problem_line_negated _ = false
blanchet@43780
   641
blanchet@49147
   642
fun is_problem_line_cnf_ueq
blanchet@49147
   643
        (Formula (_, _, AAtom (ATerm (((s, _), _), _)), _, _)) =
blanchet@44034
   644
    is_tptp_equal s
blanchet@43780
   645
  | is_problem_line_cnf_ueq _ = false
blanchet@43780
   646
blanchet@49147
   647
fun open_conjecture_term (ATerm (((s, s'), tys), tms)) =
blanchet@49147
   648
    ATerm ((if is_tptp_variable s then (s |> Name.desymbolize false, s')
blanchet@49147
   649
            else (s, s'), tys), tms |> map open_conjecture_term)
nik@44535
   650
  | open_conjecture_term _ = raise Fail "unexpected higher-order term"
blanchet@43939
   651
fun open_formula conj =
blanchet@43939
   652
  let
blanchet@44034
   653
    (* We are conveniently assuming that all bound variable names are
blanchet@44034
   654
       distinct, which should be the case for the formulas we generate. *)
blanchet@44034
   655
    fun opn (pos as SOME true) (AQuant (AForall, _, phi)) = opn pos phi
blanchet@44034
   656
      | opn (pos as SOME false) (AQuant (AExists, _, phi)) = opn pos phi
blanchet@43939
   657
      | opn pos (AConn (ANot, [phi])) = mk_anot (opn (Option.map not pos) phi)
blanchet@43939
   658
      | opn pos (AConn (c, [phi1, phi2])) =
blanchet@43939
   659
        let val (pos1, pos2) = polarities_of_conn pos c in
blanchet@43939
   660
          AConn (c, [opn pos1 phi1, opn pos2 phi2])
blanchet@43939
   661
        end
blanchet@43939
   662
      | opn _ (AAtom t) = AAtom (t |> conj ? open_conjecture_term)
blanchet@44283
   663
      | opn _ phi = phi
blanchet@43939
   664
  in opn (SOME (not conj)) end
blanchet@43785
   665
fun open_formula_line (Formula (ident, kind, phi, source, info)) =
blanchet@43785
   666
    Formula (ident, kind, open_formula (kind = Conjecture) phi, source, info)
blanchet@43785
   667
  | open_formula_line line = line
blanchet@43780
   668
blanchet@43780
   669
fun negate_conjecture_line (Formula (ident, Conjecture, phi, source, info)) =
blanchet@43783
   670
    Formula (ident, Hypothesis, mk_anot phi, source, info)
blanchet@43780
   671
  | negate_conjecture_line line = line
blanchet@43780
   672
blanchet@43939
   673
exception CLAUSIFY of unit
blanchet@43939
   674
blanchet@43967
   675
(* This "clausification" only expands syntactic sugar, such as "phi => psi" to
blanchet@43967
   676
   "~ phi | psi" and "phi <=> psi" to "~ phi | psi" and "~ psi | phi". We don't
blanchet@43967
   677
   attempt to distribute conjunctions over disjunctions. *)
blanchet@44034
   678
fun clausify_formula pos (phi as AAtom _) = [phi |> not pos ? mk_anot]
blanchet@44034
   679
  | clausify_formula pos (AConn (ANot, [phi])) = clausify_formula (not pos) phi
blanchet@44034
   680
  | clausify_formula true (AConn (AOr, [phi1, phi2])) =
blanchet@44034
   681
    (phi1, phi2) |> pairself (clausify_formula true)
blanchet@44034
   682
                 |> uncurry (map_product (mk_aconn AOr))
blanchet@44034
   683
  | clausify_formula false (AConn (AAnd, [phi1, phi2])) =
blanchet@44034
   684
    (phi1, phi2) |> pairself (clausify_formula false)
blanchet@44034
   685
                 |> uncurry (map_product (mk_aconn AOr))
blanchet@44034
   686
  | clausify_formula true (AConn (AImplies, [phi1, phi2])) =
blanchet@44034
   687
    clausify_formula true (AConn (AOr, [mk_anot phi1, phi2]))
blanchet@44034
   688
  | clausify_formula true (AConn (AIff, phis)) =
blanchet@44034
   689
    clausify_formula true (AConn (AImplies, phis)) @
blanchet@44034
   690
    clausify_formula true (AConn (AImplies, rev phis))
blanchet@44034
   691
  | clausify_formula _ _ = raise CLAUSIFY ()
blanchet@43939
   692
blanchet@43939
   693
fun clausify_formula_line (Formula (ident, kind, phi, source, info)) =
blanchet@43967
   694
    let
blanchet@43967
   695
      val (n, phis) = phi |> try (clausify_formula true) |> these |> `length
blanchet@43967
   696
    in
blanchet@43967
   697
      map2 (fn phi => fn j =>
blanchet@44153
   698
               Formula (ident ^ replicate_string (j - 1) "x", kind, phi, source,
blanchet@44153
   699
                        info))
blanchet@43967
   700
           phis (1 upto n)
blanchet@43967
   701
    end
blanchet@43967
   702
  | clausify_formula_line _ = []
blanchet@43939
   703
blanchet@43939
   704
fun ensure_cnf_problem_line line =
blanchet@43939
   705
  line |> open_formula_line |> negate_conjecture_line |> clausify_formula_line
blanchet@43939
   706
blanchet@43939
   707
fun ensure_cnf_problem problem =
blanchet@43967
   708
  problem |> map (apsnd (maps ensure_cnf_problem_line))
blanchet@43933
   709
blanchet@43803
   710
fun filter_cnf_ueq_problem problem =
blanchet@43803
   711
  problem
blanchet@43803
   712
  |> map (apsnd (map open_formula_line
blanchet@43803
   713
                 #> filter is_problem_line_cnf_ueq
blanchet@43803
   714
                 #> map negate_conjecture_line))
blanchet@43803
   715
  |> (fn problem =>
blanchet@43780
   716
         let
blanchet@44688
   717
           val lines = problem |> maps snd
blanchet@44688
   718
           val conjs = lines |> filter is_problem_line_negated
blanchet@44688
   719
         in if length conjs = 1 andalso conjs <> lines then problem else [] end)
blanchet@38251
   720
blanchet@37643
   721
blanchet@43839
   722
(** Symbol declarations **)
blanchet@43839
   723
blanchet@48021
   724
fun add_declared_syms_in_problem_line (Decl (_, sym, _)) = cons sym
blanchet@43839
   725
  | add_declared_syms_in_problem_line _ = I
blanchet@43839
   726
fun declared_syms_in_problem problem =
blanchet@43839
   727
  fold (fold add_declared_syms_in_problem_line o snd) problem []
blanchet@43839
   728
blanchet@37643
   729
(** Nice names **)
blanchet@37643
   730
blanchet@37624
   731
fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
blanchet@37624
   732
fun pool_map f xs =
blanchet@37624
   733
  pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
blanchet@37624
   734
blanchet@43088
   735
val no_qualifiers =
blanchet@43088
   736
  let
blanchet@43088
   737
    fun skip [] = []
blanchet@43088
   738
      | skip (#"." :: cs) = skip cs
blanchet@43088
   739
      | skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
blanchet@43088
   740
    and keep [] = []
blanchet@43088
   741
      | keep (#"." :: cs) = skip cs
blanchet@43088
   742
      | keep (c :: cs) = c :: keep cs
blanchet@43088
   743
  in String.explode #> rev #> keep #> rev #> String.implode end
blanchet@43088
   744
blanchet@43626
   745
(* Long names can slow down the ATPs. *)
blanchet@43589
   746
val max_readable_name_size = 20
blanchet@43438
   747
blanchet@43841
   748
(* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
blanchet@43841
   749
   unreadable "op_1", "op_2", etc., in the problem files. "eq" is reserved to
blanchet@43841
   750
   ensure that "HOL.eq" is correctly mapped to equality (not clear whether this
blanchet@43841
   751
   is still necessary). *)
blanchet@46810
   752
val reserved_nice_names = [tptp_old_equal, "op", "eq"]
blanchet@43780
   753
blanchet@47242
   754
(* hack to get the same hashing across Mirabelle runs (see "mirabelle.pl") *)
blanchet@47242
   755
fun cleanup_mirabelle_name s =
blanchet@47242
   756
  let
blanchet@47242
   757
    val mirabelle_infix = "_Mirabelle_"
blanchet@47242
   758
    val random_suffix_len = 10
blanchet@47242
   759
    val (s1, s2) = Substring.position mirabelle_infix (Substring.full s)
blanchet@47242
   760
  in
blanchet@47242
   761
    if Substring.isEmpty s2 then
blanchet@47242
   762
      s
blanchet@47242
   763
    else
blanchet@47242
   764
      Substring.string s1 ^
blanchet@47242
   765
      Substring.string (Substring.triml (size mirabelle_infix + random_suffix_len) s2)
blanchet@47242
   766
  end
blanchet@47242
   767
blanchet@47206
   768
fun readable_name protect full_name s =
blanchet@46810
   769
  (if s = full_name then
blanchet@46810
   770
     s
blanchet@46810
   771
   else
blanchet@46810
   772
     s |> no_qualifiers
blanchet@46810
   773
       |> perhaps (try (unprefix "'"))
blanchet@46810
   774
       |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
blanchet@46810
   775
       |> (fn s =>
blanchet@46810
   776
              if size s > max_readable_name_size then
blanchet@46810
   777
                String.substring (s, 0, max_readable_name_size div 2 - 4) ^
blanchet@47242
   778
                string_of_int (hash_string (cleanup_mirabelle_name full_name)) ^
blanchet@46810
   779
                String.extract (s, size s - max_readable_name_size div 2 + 4,
blanchet@46810
   780
                                NONE)
blanchet@46810
   781
              else
blanchet@46810
   782
                s)
blanchet@46810
   783
       |> (fn s =>
blanchet@46810
   784
              if member (op =) reserved_nice_names s then full_name else s))
blanchet@47206
   785
  |> protect
blanchet@37624
   786
blanchet@46810
   787
fun nice_name _ (full_name, _) NONE = (full_name, NONE)
blanchet@47206
   788
  | nice_name protect (full_name, desired_name) (SOME the_pool) =
blanchet@43839
   789
    if is_built_in_tptp_symbol full_name then
blanchet@39630
   790
      (full_name, SOME the_pool)
blanchet@39630
   791
    else case Symtab.lookup (fst the_pool) full_name of
blanchet@37624
   792
      SOME nice_name => (nice_name, SOME the_pool)
blanchet@37624
   793
    | NONE =>
blanchet@37624
   794
      let
blanchet@47206
   795
        val nice_prefix = readable_name protect full_name desired_name
blanchet@37624
   796
        fun add j =
blanchet@37624
   797
          let
blanchet@43515
   798
            val nice_name =
blanchet@47909
   799
              nice_prefix ^ (if j = 1 then "" else string_of_int j)
blanchet@37624
   800
          in
blanchet@37624
   801
            case Symtab.lookup (snd the_pool) nice_name of
blanchet@37624
   802
              SOME full_name' =>
blanchet@37624
   803
              if full_name = full_name' then (nice_name, the_pool)
blanchet@37624
   804
              else add (j + 1)
blanchet@37624
   805
            | NONE =>
blanchet@37624
   806
              (nice_name,
blanchet@37624
   807
               (Symtab.update_new (full_name, nice_name) (fst the_pool),
blanchet@37624
   808
                Symtab.update_new (nice_name, full_name) (snd the_pool)))
blanchet@37624
   809
          end
blanchet@47909
   810
      in add 1 |> apsnd SOME end
blanchet@37624
   811
blanchet@47514
   812
fun avoid_clash_with_alt_ergo_type_vars s =
blanchet@47514
   813
  if is_tptp_variable s then s else s ^ "_"
blanchet@47514
   814
blanchet@47206
   815
fun avoid_clash_with_dfg_keywords s =
blanchet@47206
   816
  let val n = String.size s in
blanchet@47271
   817
    if n < 2 orelse (n = 2 andalso String.sub (s, 0) = String.sub (s, 1)) orelse
blanchet@47271
   818
       String.isSubstring "_" s then
blanchet@47206
   819
      s
blanchet@48021
   820
    else if is_tptp_variable s then
blanchet@48017
   821
      (* "DL" appears to be a SPASS 3.7 keyword *)
blanchet@48021
   822
      if s = "DL" then s ^ "_" else s
blanchet@47206
   823
    else
blanchet@47206
   824
      String.substring (s, 0, n - 1) ^
blanchet@47206
   825
      String.str (Char.toUpper (String.sub (s, n - 1)))
blanchet@47206
   826
  end
blanchet@47206
   827
blanchet@46810
   828
fun nice_atp_problem readable_names format problem =
blanchet@46810
   829
  let
blanchet@46810
   830
    val empty_pool =
blanchet@46810
   831
      if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
blanchet@47514
   832
    val avoid_clash =
blanchet@47514
   833
      case format of
blanchet@49145
   834
        TFF (Polymorphic, _) => avoid_clash_with_alt_ergo_type_vars
blanchet@49146
   835
      | DFG _ => avoid_clash_with_dfg_keywords
blanchet@47514
   836
      | _ => I
blanchet@47514
   837
    val nice_name = nice_name avoid_clash
blanchet@46810
   838
    fun nice_type (AType (name, tys)) =
blanchet@46810
   839
        nice_name name ##>> pool_map nice_type tys #>> AType
blanchet@46810
   840
      | nice_type (AFun (ty1, ty2)) = nice_type ty1 ##>> nice_type ty2 #>> AFun
blanchet@49148
   841
      | nice_type (APi (names, ty)) =
blanchet@49148
   842
        pool_map nice_name names ##>> nice_type ty #>> APi
blanchet@49147
   843
    fun nice_term (ATerm ((name, tys), ts)) =
blanchet@49147
   844
        nice_name name ##>> pool_map nice_type tys ##>> pool_map nice_term ts
blanchet@49147
   845
        #>> ATerm
blanchet@48926
   846
      | nice_term (AAbs (((name, ty), tm), args)) =
blanchet@48926
   847
        nice_name name ##>> nice_type ty ##>> nice_term tm
blanchet@48926
   848
        ##>> pool_map nice_term args #>> AAbs
blanchet@49148
   849
    fun nice_formula (ATyQuant (q, xs, phi)) =
blanchet@49148
   850
        pool_map nice_type xs ##>> nice_formula phi
blanchet@49148
   851
        #>> (fn (xs, phi) => ATyQuant (q, xs, phi))
blanchet@49148
   852
      | nice_formula (AQuant (q, xs, phi)) =
blanchet@46810
   853
        pool_map nice_name (map fst xs)
blanchet@46810
   854
        ##>> pool_map (fn NONE => pair NONE
blanchet@46810
   855
                        | SOME ty => nice_type ty #>> SOME) (map snd xs)
blanchet@46810
   856
        ##>> nice_formula phi
blanchet@46810
   857
        #>> (fn ((ss, ts), phi) => AQuant (q, ss ~~ ts, phi))
blanchet@46810
   858
      | nice_formula (AConn (c, phis)) =
blanchet@46810
   859
        pool_map nice_formula phis #>> curry AConn c
blanchet@46810
   860
      | nice_formula (AAtom tm) = nice_term tm #>> AAtom
blanchet@46810
   861
    fun nice_problem_line (Decl (ident, sym, ty)) =
blanchet@46810
   862
        nice_name sym ##>> nice_type ty #>> (fn (sym, ty) => Decl (ident, sym, ty))
blanchet@46810
   863
      | nice_problem_line (Formula (ident, kind, phi, source, info)) =
blanchet@46810
   864
        nice_formula phi #>> (fn phi => Formula (ident, kind, phi, source, info))
blanchet@46810
   865
    fun nice_problem problem =
blanchet@46810
   866
      pool_map (fn (heading, lines) =>
blanchet@46810
   867
                   pool_map nice_problem_line lines #>> pair heading) problem
blanchet@46810
   868
  in nice_problem problem empty_pool end
blanchet@37509
   869
blanchet@37509
   870
end;