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