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