src/HOL/Tools/Sledgehammer/sledgehammer_fol_clause.ML
author blanchet
Tue Jun 22 14:28:22 2010 +0200 (2010-06-22)
changeset 37498 b426cbdb5a23
parent 37479 f6b1ee5b420b
child 37500 7587b6e63454
permissions -rw-r--r--
removed Sledgehammer's support for the DFG syntax;
this removes 350 buggy lines from Sledgehammer. SPASS 3.5 and above support the TPTP syntax.
blanchet@35826
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_fol_clause.ML
wenzelm@33311
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory
blanchet@36393
     3
    Author:     Jasmin Blanchette, TU Muenchen
paulson@15347
     4
wenzelm@33311
     5
Storing/printing FOL clauses and arity clauses.  Typed equality is
wenzelm@33311
     6
treated differently.
wenzelm@33311
     7
blanchet@36218
     8
FIXME: combine with sledgehammer_hol_clause!
paulson@15347
     9
*)
paulson@15347
    10
blanchet@35826
    11
signature SLEDGEHAMMER_FOL_CLAUSE =
wenzelm@24310
    12
sig
wenzelm@24310
    13
  val schematic_var_prefix: string
wenzelm@24310
    14
  val fixed_var_prefix: string
wenzelm@24310
    15
  val tvar_prefix: string
wenzelm@24310
    16
  val tfree_prefix: string
wenzelm@24310
    17
  val clause_prefix: string
wenzelm@24310
    18
  val const_prefix: string
wenzelm@24310
    19
  val tconst_prefix: string
wenzelm@24310
    20
  val class_prefix: string
wenzelm@24310
    21
  val union_all: ''a list list -> ''a list
wenzelm@24310
    22
  val const_trans_table: string Symtab.table
wenzelm@24310
    23
  val type_const_trans_table: string Symtab.table
wenzelm@24310
    24
  val ascii_of: string -> string
wenzelm@24310
    25
  val undo_ascii_of: string -> string
wenzelm@24310
    26
  val paren_pack : string list -> string
wenzelm@24310
    27
  val make_schematic_var : string * int -> string
paulson@18868
    28
  val make_fixed_var : string -> string
paulson@18868
    29
  val make_schematic_type_var : string * int -> string
wenzelm@24310
    30
  val make_fixed_type_var : string -> string
blanchet@37498
    31
  val make_fixed_const : string -> string
blanchet@37498
    32
  val make_fixed_type_const : string -> string
paulson@18868
    33
  val make_type_class : string -> string
blanchet@36169
    34
  type name = string * string
blanchet@36169
    35
  type name_pool = string Symtab.table * string Symtab.table
blanchet@36169
    36
  val empty_name_pool : bool -> name_pool option
blanchet@36169
    37
  val pool_map : ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
blanchet@36169
    38
  val nice_name : name -> name_pool option -> string * name_pool option
wenzelm@24310
    39
  datatype kind = Axiom | Conjecture
wenzelm@24310
    40
  type axiom_name = string
wenzelm@24310
    41
  datatype fol_type =
blanchet@36169
    42
    TyVar of name |
blanchet@36169
    43
    TyFree of name |
blanchet@36169
    44
    TyConstr of name * fol_type list
blanchet@36170
    45
  val string_of_fol_type :
blanchet@36170
    46
    fol_type -> name_pool option -> string * name_pool option
blanchet@36556
    47
  datatype type_literal =
blanchet@36556
    48
    TyLitVar of string * name |
blanchet@36556
    49
    TyLitFree of string * name
wenzelm@24310
    50
  exception CLAUSE of string * term
blanchet@36966
    51
  val type_literals_for_types : typ list -> type_literal list
wenzelm@24310
    52
  datatype arLit =
wenzelm@24310
    53
      TConsLit of class * string * string list
wenzelm@24310
    54
    | TVarLit of class * string
blanchet@35865
    55
  datatype arity_clause = ArityClause of
paulson@24937
    56
   {axiom_name: axiom_name, conclLit: arLit, premLits: arLit list}
blanchet@35865
    57
  datatype classrel_clause = ClassrelClause of
paulson@24937
    58
   {axiom_name: axiom_name, subclass: class, superclass: class}
blanchet@35865
    59
  val make_classrel_clauses: theory -> class list -> class list -> classrel_clause list
blanchet@35865
    60
  val make_arity_clauses: theory -> string list -> class list -> class list * arity_clause list
wenzelm@24310
    61
  val tptp_sign: bool -> string -> string
blanchet@36556
    62
  val tptp_of_type_literal :
blanchet@36556
    63
    bool -> type_literal -> name_pool option -> string * name_pool option
paulson@24937
    64
  val gen_tptp_cls : int * string * kind * string list * string list -> string
wenzelm@24310
    65
  val tptp_tfree_clause : string -> string
blanchet@35865
    66
  val tptp_arity_clause : arity_clause -> string
blanchet@35865
    67
  val tptp_classrel_clause : classrel_clause -> string
wenzelm@24310
    68
end
paulson@15347
    69
blanchet@35826
    70
structure Sledgehammer_FOL_Clause : SLEDGEHAMMER_FOL_CLAUSE =
paulson@15347
    71
struct
paulson@15347
    72
blanchet@36063
    73
open Sledgehammer_Util
blanchet@36063
    74
paulson@15347
    75
val schematic_var_prefix = "V_";
paulson@15347
    76
val fixed_var_prefix = "v_";
paulson@15347
    77
paulson@17230
    78
val tvar_prefix = "T_";
paulson@17230
    79
val tfree_prefix = "t_";
paulson@15347
    80
wenzelm@24310
    81
val clause_prefix = "cls_";
wenzelm@24310
    82
val arclause_prefix = "clsarity_"
paulson@17525
    83
val clrelclause_prefix = "clsrel_";
paulson@15347
    84
paulson@17230
    85
val const_prefix = "c_";
wenzelm@24310
    86
val tconst_prefix = "tc_";
wenzelm@24310
    87
val class_prefix = "class_";
paulson@15347
    88
blanchet@36218
    89
fun union_all xss = fold (union (op =)) xss []
paulson@17775
    90
blanchet@36493
    91
(* Readable names for the more common symbolic functions. Do not mess with the
blanchet@37479
    92
   last nine entries of the table unless you know what you are doing. *)
paulson@15347
    93
val const_trans_table =
blanchet@35865
    94
  Symtab.make [(@{const_name "op ="}, "equal"),
blanchet@35865
    95
               (@{const_name "op &"}, "and"),
blanchet@35865
    96
               (@{const_name "op |"}, "or"),
blanchet@35865
    97
               (@{const_name "op -->"}, "implies"),
blanchet@36493
    98
               (@{const_name "op :"}, "in"),
blanchet@36493
    99
               (@{const_name fequal}, "fequal"),
blanchet@36493
   100
               (@{const_name COMBI}, "COMBI"),
blanchet@36493
   101
               (@{const_name COMBK}, "COMBK"),
blanchet@36493
   102
               (@{const_name COMBB}, "COMBB"),
blanchet@36493
   103
               (@{const_name COMBC}, "COMBC"),
blanchet@37479
   104
               (@{const_name COMBS}, "COMBS"),
blanchet@37479
   105
               (@{const_name True}, "True"),
blanchet@37479
   106
               (@{const_name False}, "False"),
blanchet@37479
   107
               (@{const_name If}, "If")]
paulson@15347
   108
paulson@17230
   109
val type_const_trans_table =
blanchet@36476
   110
  Symtab.make [(@{type_name "*"}, "prod"),
blanchet@36476
   111
               (@{type_name "+"}, "sum")]
paulson@15347
   112
paulson@15610
   113
(*Escaping of special characters.
paulson@15610
   114
  Alphanumeric characters are left unchanged.
paulson@15610
   115
  The character _ goes to __
paulson@15610
   116
  Characters in the range ASCII space to / go to _A to _P, respectively.
paulson@24183
   117
  Other printing characters go to _nnn where nnn is the decimal ASCII code.*)
paulson@24183
   118
val A_minus_space = Char.ord #"A" - Char.ord #" ";
paulson@15610
   119
paulson@24183
   120
fun stringN_of_int 0 _ = ""
paulson@24183
   121
  | stringN_of_int k n = stringN_of_int (k-1) (n div 10) ^ Int.toString (n mod 10);
paulson@15610
   122
paulson@15347
   123
fun ascii_of_c c =
paulson@15610
   124
  if Char.isAlphaNum c then String.str c
paulson@15610
   125
  else if c = #"_" then "__"
wenzelm@24310
   126
  else if #" " <= c andalso c <= #"/"
paulson@15610
   127
       then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
wenzelm@24310
   128
  else if Char.isPrint c
paulson@24183
   129
       then ("_" ^ stringN_of_int 3 (Char.ord c))  (*fixed width, in case more digits follow*)
paulson@15610
   130
  else ""
paulson@15347
   131
paulson@15610
   132
val ascii_of = String.translate ascii_of_c;
paulson@15610
   133
paulson@24183
   134
(** Remove ASCII armouring from names in proof files **)
paulson@24183
   135
paulson@24183
   136
(*We don't raise error exceptions because this code can run inside the watcher.
paulson@24183
   137
  Also, the errors are "impossible" (hah!)*)
paulson@24183
   138
fun undo_ascii_aux rcs [] = String.implode(rev rcs)
paulson@24183
   139
  | undo_ascii_aux rcs [#"_"] = undo_ascii_aux (#"_"::rcs) []  (*ERROR*)
paulson@24183
   140
      (*Three types of _ escapes: __, _A to _P, _nnn*)
paulson@24183
   141
  | undo_ascii_aux rcs (#"_" :: #"_" :: cs) = undo_ascii_aux (#"_"::rcs) cs
wenzelm@24310
   142
  | undo_ascii_aux rcs (#"_" :: c :: cs) =
paulson@24183
   143
      if #"A" <= c andalso c<= #"P"  (*translation of #" " to #"/"*)
paulson@24183
   144
      then undo_ascii_aux (Char.chr(Char.ord c - A_minus_space) :: rcs) cs
wenzelm@24310
   145
      else
paulson@24183
   146
        let val digits = List.take (c::cs, 3) handle Subscript => []
wenzelm@24310
   147
        in
paulson@24183
   148
            case Int.fromString (String.implode digits) of
paulson@24183
   149
                NONE => undo_ascii_aux (c:: #"_"::rcs) cs  (*ERROR*)
paulson@24183
   150
              | SOME n => undo_ascii_aux (Char.chr n :: rcs) (List.drop (cs, 2))
paulson@24183
   151
        end
paulson@24183
   152
  | undo_ascii_aux rcs (c::cs) = undo_ascii_aux (c::rcs) cs;
paulson@24183
   153
paulson@24183
   154
val undo_ascii_of = undo_ascii_aux [] o String.explode;
paulson@15347
   155
paulson@17525
   156
(* convert a list of strings into one single string; surrounded by brackets *)
paulson@18218
   157
fun paren_pack [] = ""   (*empty argument list*)
paulson@18218
   158
  | paren_pack strings = "(" ^ commas strings ^ ")";
paulson@17525
   159
blanchet@36556
   160
fun tptp_clause strings = "(" ^ space_implode " | " strings ^ ")"
paulson@17525
   161
paulson@16925
   162
(*Remove the initial ' character from a type variable, if it is present*)
paulson@16925
   163
fun trim_type_var s =
paulson@16925
   164
  if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
paulson@16925
   165
  else error ("trim_type: Malformed type variable encountered: " ^ s);
paulson@16925
   166
paulson@16903
   167
fun ascii_of_indexname (v,0) = ascii_of v
paulson@17525
   168
  | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
paulson@15347
   169
paulson@17230
   170
fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
paulson@15347
   171
fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
paulson@15347
   172
wenzelm@24310
   173
fun make_schematic_type_var (x,i) =
paulson@16925
   174
      tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
paulson@16925
   175
fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
paulson@15347
   176
blanchet@37498
   177
fun lookup_const c =
blanchet@37498
   178
  case Symtab.lookup const_trans_table c of
blanchet@37498
   179
    SOME c' => c'
blanchet@37498
   180
  | NONE => ascii_of c
paulson@23075
   181
blanchet@37498
   182
fun lookup_type_const c =
blanchet@37498
   183
  case Symtab.lookup type_const_trans_table c of
blanchet@37498
   184
    SOME c' => c'
blanchet@37498
   185
  | NONE => ascii_of c
paulson@18411
   186
blanchet@36062
   187
(* "op =" MUST BE "equal" because it's built into ATPs. *)
blanchet@37498
   188
fun make_fixed_const @{const_name "op ="} = "equal"
blanchet@37498
   189
  | make_fixed_const c = const_prefix ^ lookup_const c
paulson@18411
   190
blanchet@37498
   191
fun make_fixed_type_const c = tconst_prefix ^ lookup_type_const c
quigley@17150
   192
wenzelm@17261
   193
fun make_type_class clas = class_prefix ^ ascii_of clas;
quigley@17150
   194
quigley@17150
   195
blanchet@36169
   196
(**** name pool ****)
blanchet@36169
   197
 
blanchet@36169
   198
type name = string * string
blanchet@36169
   199
type name_pool = string Symtab.table * string Symtab.table
blanchet@36169
   200
blanchet@36222
   201
fun empty_name_pool readable_names =
blanchet@36222
   202
  if readable_names then SOME (`I Symtab.empty) else NONE
blanchet@36169
   203
blanchet@36556
   204
fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
blanchet@36169
   205
fun pool_map f xs =
blanchet@36556
   206
  pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
blanchet@36169
   207
blanchet@36169
   208
fun add_nice_name full_name nice_prefix j the_pool =
blanchet@36169
   209
  let
blanchet@36169
   210
    val nice_name = nice_prefix ^ (if j = 0 then "" else "_" ^ Int.toString j)
blanchet@36169
   211
  in
blanchet@36169
   212
    case Symtab.lookup (snd the_pool) nice_name of
blanchet@36169
   213
      SOME full_name' =>
blanchet@36169
   214
      if full_name = full_name' then (nice_name, the_pool)
blanchet@36169
   215
      else add_nice_name full_name nice_prefix (j + 1) the_pool
blanchet@36169
   216
    | NONE =>
blanchet@36169
   217
      (nice_name, (Symtab.update_new (full_name, nice_name) (fst the_pool),
blanchet@36169
   218
                   Symtab.update_new (nice_name, full_name) (snd the_pool)))
blanchet@36169
   219
  end
blanchet@36169
   220
blanchet@36169
   221
fun translate_first_char f s =
blanchet@36169
   222
  String.str (f (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
blanchet@36169
   223
blanchet@36222
   224
fun readable_name full_name s =
blanchet@36169
   225
  let
blanchet@36169
   226
    val s = s |> Long_Name.base_name
blanchet@36169
   227
              |> fold remove_all ["\<^sub>", "\<^bsub>", "\<^esub>", "\<^isub>"]
blanchet@36169
   228
    val s' = s |> explode |> rev |> dropwhile (curry (op =) "'")
blanchet@36169
   229
    val s' =
blanchet@36169
   230
      (s' |> rev
blanchet@36169
   231
          |> implode
blanchet@36169
   232
          |> String.translate
blanchet@36221
   233
                 (fn c => if Char.isAlphaNum c orelse c = #"_" then String.str c
blanchet@36221
   234
                          else ""))
blanchet@36169
   235
      ^ replicate_string (String.size s - length s') "_"
blanchet@36169
   236
    val s' =
blanchet@36169
   237
      if s' = "" orelse not (Char.isAlpha (String.sub (s', 0))) then "X" ^ s'
blanchet@36169
   238
      else s'
blanchet@36476
   239
    (* Avoid "equal", since it's built into ATPs; and "op" is very ambiguous
blanchet@36476
   240
       ("op &", "op |", etc.). *)
blanchet@36476
   241
    val s' = if s' = "equal" orelse s' = "op" then full_name else s'
blanchet@36169
   242
  in
blanchet@36169
   243
    case (Char.isLower (String.sub (full_name, 0)),
blanchet@36169
   244
          Char.isLower (String.sub (s', 0))) of
blanchet@36169
   245
      (true, false) => translate_first_char Char.toLower s'
blanchet@36169
   246
    | (false, true) => translate_first_char Char.toUpper s'
blanchet@36169
   247
    | _ => s'
blanchet@36169
   248
  end
blanchet@36169
   249
blanchet@36169
   250
fun nice_name (full_name, _) NONE = (full_name, NONE)
blanchet@36169
   251
  | nice_name (full_name, desired_name) (SOME the_pool) =
blanchet@36169
   252
    case Symtab.lookup (fst the_pool) full_name of
blanchet@36169
   253
      SOME nice_name => (nice_name, SOME the_pool)
blanchet@36222
   254
    | NONE => add_nice_name full_name (readable_name full_name desired_name) 0
blanchet@36222
   255
                            the_pool
blanchet@36169
   256
              |> apsnd SOME
blanchet@36169
   257
blanchet@37498
   258
(**** Definitions and functions for FOL clauses for TPTP format output ****)
paulson@15347
   259
paulson@21509
   260
datatype kind = Axiom | Conjecture;
paulson@23385
   261
paulson@15347
   262
type axiom_name = string;
paulson@15347
   263
paulson@15347
   264
(**** Isabelle FOL clauses ****)
paulson@15347
   265
blanchet@36168
   266
datatype fol_type =
blanchet@36169
   267
  TyVar of name |
blanchet@36169
   268
  TyFree of name |
blanchet@36169
   269
  TyConstr of name * fol_type list
mengj@18402
   270
blanchet@36170
   271
fun string_of_fol_type (TyVar sp) pool = nice_name sp pool
blanchet@36170
   272
  | string_of_fol_type (TyFree sp) pool = nice_name sp pool
blanchet@36170
   273
  | string_of_fol_type (TyConstr (sp, tys)) pool =
blanchet@36170
   274
    let
blanchet@36170
   275
      val (s, pool) = nice_name sp pool
blanchet@36170
   276
      val (ss, pool) = pool_map string_of_fol_type tys pool
blanchet@36170
   277
    in (s ^ paren_pack ss, pool) end
wenzelm@24310
   278
blanchet@36556
   279
(* The first component is the type class; the second is a TVar or TFree. *)
blanchet@36556
   280
datatype type_literal =
blanchet@36556
   281
  TyLitVar of string * name |
blanchet@36556
   282
  TyLitFree of string * name
paulson@15347
   283
paulson@17404
   284
exception CLAUSE of string * term;
paulson@15347
   285
wenzelm@24310
   286
(*Make literals for sorted type variables*)
paulson@24940
   287
fun sorts_on_typs_aux (_, [])   = []
paulson@24940
   288
  | sorts_on_typs_aux ((x,i),  s::ss) =
paulson@24940
   289
      let val sorts = sorts_on_typs_aux ((x,i), ss)
paulson@22643
   290
      in
paulson@22643
   291
          if s = "HOL.type" then sorts
blanchet@36556
   292
          else if i = ~1 then TyLitFree (make_type_class s, `make_fixed_type_var x) :: sorts
blanchet@36556
   293
          else TyLitVar (make_type_class s, (make_schematic_type_var (x,i), x)) :: sorts
paulson@22643
   294
      end;
quigley@17150
   295
paulson@24940
   296
fun sorts_on_typs (TFree (a,s)) = sorts_on_typs_aux ((a,~1),s)
paulson@24940
   297
  | sorts_on_typs (TVar (v,s))  = sorts_on_typs_aux (v,s);
paulson@24940
   298
paulson@24937
   299
(*Given a list of sorted type variables, return a list of type literals.*)
blanchet@36966
   300
fun type_literals_for_types Ts =
blanchet@36966
   301
  fold (union (op =)) (map sorts_on_typs Ts) []
mengj@20015
   302
mengj@20015
   303
(** make axiom and conjecture clauses. **)
mengj@20015
   304
paulson@15347
   305
(**** Isabelle arities ****)
paulson@15347
   306
wenzelm@24310
   307
datatype arLit = TConsLit of class * string * string list
paulson@22643
   308
               | TVarLit of class * string;
wenzelm@24310
   309
blanchet@35865
   310
datatype arity_clause =
wenzelm@24310
   311
         ArityClause of {axiom_name: axiom_name,
wenzelm@24310
   312
                         conclLit: arLit,
wenzelm@24310
   313
                         premLits: arLit list};
paulson@15347
   314
paulson@15347
   315
paulson@18798
   316
fun gen_TVars 0 = []
paulson@18798
   317
  | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
paulson@15347
   318
paulson@18411
   319
fun pack_sort(_,[])  = []
paulson@18411
   320
  | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
paulson@22643
   321
  | pack_sort(tvar, cls::srt) =  (cls, tvar) :: pack_sort(tvar, srt);
wenzelm@24310
   322
paulson@18411
   323
(*Arity of type constructor tcon :: (arg1,...,argN)res*)
blanchet@37498
   324
fun make_axiom_arity_clause (tcons, axiom_name, (cls,args)) =
paulson@21560
   325
   let val tvars = gen_TVars (length args)
paulson@17845
   326
       val tvars_srts = ListPair.zip (tvars,args)
paulson@17845
   327
   in
blanchet@37498
   328
     ArityClause {axiom_name = axiom_name, 
blanchet@37498
   329
                  conclLit = TConsLit (cls, make_fixed_type_const tcons, tvars),
blanchet@37498
   330
                  premLits = map TVarLit (union_all(map pack_sort tvars_srts))}
paulson@17845
   331
   end;
paulson@15347
   332
paulson@15347
   333
paulson@15347
   334
(**** Isabelle class relations ****)
paulson@15347
   335
blanchet@35865
   336
datatype classrel_clause =
wenzelm@24310
   337
         ClassrelClause of {axiom_name: axiom_name,
wenzelm@24310
   338
                            subclass: class,
wenzelm@24310
   339
                            superclass: class};
wenzelm@24310
   340
paulson@21290
   341
(*Generate all pairs (sub,super) such that sub is a proper subclass of super in theory thy.*)
blanchet@37498
   342
fun class_pairs _ [] _ = []
paulson@21432
   343
  | class_pairs thy subs supers =
blanchet@36218
   344
      let
blanchet@36218
   345
        val class_less = Sorts.class_less (Sign.classes_of thy)
blanchet@36218
   346
        fun add_super sub super = class_less (sub, super) ? cons (sub, super)
blanchet@36218
   347
        fun add_supers sub = fold (add_super sub) supers
blanchet@36218
   348
      in fold add_supers subs [] end
paulson@15347
   349
blanchet@35865
   350
fun make_classrel_clause (sub,super) =
paulson@21290
   351
  ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of sub ^ "_" ^ ascii_of super,
wenzelm@24310
   352
                  subclass = make_type_class sub,
paulson@21290
   353
                  superclass = make_type_class super};
paulson@15347
   354
paulson@21290
   355
fun make_classrel_clauses thy subs supers =
blanchet@35865
   356
  map make_classrel_clause (class_pairs thy subs supers);
paulson@18868
   357
paulson@18868
   358
paulson@18868
   359
(** Isabelle arities **)
paulson@17845
   360
blanchet@37498
   361
fun arity_clause _ _ (_, []) = []
blanchet@37498
   362
  | arity_clause seen n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
blanchet@37498
   363
      arity_clause seen n (tcons,ars)
blanchet@37498
   364
  | arity_clause seen n (tcons, (ar as (class,_)) :: ars) =
haftmann@36692
   365
      if member (op =) seen class then (*multiple arities for the same tycon, class pair*)
blanchet@37498
   366
          make_axiom_arity_clause (tcons, lookup_type_const tcons ^ "_" ^ class ^ "_" ^ Int.toString n, ar) ::
blanchet@37498
   367
          arity_clause seen (n+1) (tcons,ars)
paulson@21373
   368
      else
blanchet@37498
   369
          make_axiom_arity_clause (tcons, lookup_type_const tcons ^ "_" ^ class, ar) ::
blanchet@37498
   370
          arity_clause (class::seen) n (tcons,ars)
paulson@17845
   371
blanchet@37498
   372
fun multi_arity_clause [] = []
blanchet@37498
   373
  | multi_arity_clause ((tcons, ars) :: tc_arlists) =
blanchet@37498
   374
      arity_clause [] 1 (tcons, ars) @ multi_arity_clause tc_arlists
paulson@17845
   375
paulson@22643
   376
(*Generate all pairs (tycon,class,sorts) such that tycon belongs to class in theory thy
paulson@22643
   377
  provided its arguments have the corresponding sorts.*)
paulson@21373
   378
fun type_class_pairs thy tycons classes =
paulson@21373
   379
  let val alg = Sign.classes_of thy
blanchet@36218
   380
      fun domain_sorts tycon = Sorts.mg_domain alg tycon o single
blanchet@36218
   381
      fun add_class tycon class =
blanchet@36218
   382
        cons (class, domain_sorts tycon class)
blanchet@36218
   383
        handle Sorts.CLASS_ERROR _ => I
blanchet@36218
   384
      fun try_classes tycon = (tycon, fold (add_class tycon) classes [])
paulson@21373
   385
  in  map try_classes tycons  end;
paulson@21373
   386
paulson@22643
   387
(*Proving one (tycon, class) membership may require proving others, so iterate.*)
blanchet@37498
   388
fun iter_type_class_pairs _ _ [] = ([], [])
paulson@22643
   389
  | iter_type_class_pairs thy tycons classes =
paulson@22643
   390
      let val cpairs = type_class_pairs thy tycons classes
haftmann@33040
   391
          val newclasses = union_all (union_all (union_all (map (map #2 o #2) cpairs)))
haftmann@33040
   392
            |> subtract (op =) classes |> subtract (op =) HOLogic.typeS
wenzelm@24310
   393
          val (classes', cpairs') = iter_type_class_pairs thy tycons newclasses
haftmann@33042
   394
      in (union (op =) classes' classes, union (op =) cpairs' cpairs) end;
wenzelm@24310
   395
blanchet@37498
   396
fun make_arity_clauses thy tycons classes =
wenzelm@24310
   397
  let val (classes', cpairs) = iter_type_class_pairs thy tycons classes
blanchet@37498
   398
  in  (classes', multi_arity_clause cpairs)  end;
paulson@18863
   399
quigley@17150
   400
paulson@18869
   401
(**** Produce TPTP files ****)
paulson@18868
   402
blanchet@37498
   403
fun string_of_clausename (cls_id, ax_name) =
blanchet@37498
   404
    clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id
blanchet@37498
   405
paulson@21509
   406
fun tptp_sign true s = s
paulson@21509
   407
  | tptp_sign false s = "~ " ^ s
paulson@18868
   408
blanchet@36556
   409
fun tptp_of_type_literal pos (TyLitVar (s, name)) =
blanchet@36556
   410
    nice_name name #>> (fn s' => tptp_sign pos (s ^ "(" ^ s' ^ ")"))
blanchet@36556
   411
  | tptp_of_type_literal pos (TyLitFree (s, name)) =
blanchet@36556
   412
    nice_name name #>> (fn s' => tptp_sign pos (s ^ "(" ^ s' ^ ")"))
blanchet@36167
   413
blanchet@36167
   414
fun tptp_cnf name kind formula =
blanchet@36167
   415
  "cnf(" ^ name ^ ", " ^ kind ^ ",\n    " ^ formula ^ ").\n"
wenzelm@24310
   416
blanchet@36167
   417
fun gen_tptp_cls (cls_id, ax_name, Axiom, lits, tylits) =
blanchet@36167
   418
      tptp_cnf (string_of_clausename (cls_id, ax_name)) "axiom"
blanchet@36556
   419
               (tptp_clause (tylits @ lits))
blanchet@36167
   420
  | gen_tptp_cls (cls_id, ax_name, Conjecture, lits, _) =
blanchet@36167
   421
      tptp_cnf (string_of_clausename (cls_id, ax_name)) "negated_conjecture"
blanchet@36556
   422
               (tptp_clause lits)
paulson@15347
   423
paulson@18863
   424
fun tptp_tfree_clause tfree_lit =
blanchet@36556
   425
    tptp_cnf "tfree_tcs" "negated_conjecture" (tptp_clause [tfree_lit])
wenzelm@24310
   426
paulson@22643
   427
fun tptp_of_arLit (TConsLit (c,t,args)) =
paulson@22643
   428
      tptp_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@22643
   429
  | tptp_of_arLit (TVarLit (c,str)) =
paulson@22643
   430
      tptp_sign false (make_type_class c ^ "(" ^ str ^ ")")
wenzelm@24310
   431
blanchet@37498
   432
fun tptp_arity_clause (ArityClause {axiom_name, conclLit, premLits, ...}) =
blanchet@37498
   433
  tptp_cnf (arclause_prefix ^ ascii_of axiom_name) "axiom"
blanchet@36556
   434
           (tptp_clause (map tptp_of_arLit (conclLit :: premLits)))
paulson@15347
   435
wenzelm@24310
   436
fun tptp_classrelLits sub sup =
paulson@21509
   437
  let val tvar = "(T)"
blanchet@36556
   438
  in  tptp_clause [tptp_sign false (sub^tvar), tptp_sign true (sup^tvar)]  end;
paulson@15347
   439
blanchet@35865
   440
fun tptp_classrel_clause (ClassrelClause {axiom_name,subclass,superclass,...}) =
blanchet@36167
   441
  tptp_cnf axiom_name "axiom" (tptp_classrelLits subclass superclass)
quigley@17150
   442
paulson@15347
   443
end;