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