src/HOL/Tools/res_clause.ML
author paulson
Tue Oct 09 18:14:00 2007 +0200 (2007-10-09)
changeset 24937 340523598914
parent 24322 dc7336b8c54c
child 24940 8f9dea697b8d
permissions -rw-r--r--
context-based treatment of generalization; also handling TFrees in axiom clauses
paulson@15347
     1
(*  Author: Jia Meng, Cambridge University Computer Laboratory
paulson@15347
     2
    ID: $Id$
paulson@15347
     3
    Copyright 2004 University of Cambridge
paulson@15347
     4
wenzelm@24310
     5
Storing/printing FOL clauses and arity clauses.
paulson@15347
     6
Typed equality is treated differently.
paulson@15347
     7
*)
paulson@15347
     8
paulson@23385
     9
(*FIXME: combine with res_hol_clause!*)
paulson@15347
    10
signature RES_CLAUSE =
wenzelm@24310
    11
sig
wenzelm@24310
    12
  val schematic_var_prefix: string
wenzelm@24310
    13
  val fixed_var_prefix: string
wenzelm@24310
    14
  val tvar_prefix: string
wenzelm@24310
    15
  val tfree_prefix: string
wenzelm@24310
    16
  val clause_prefix: string
wenzelm@24310
    17
  val const_prefix: string
wenzelm@24310
    18
  val tconst_prefix: string
wenzelm@24310
    19
  val class_prefix: string
wenzelm@24310
    20
  val union_all: ''a list list -> ''a list
wenzelm@24310
    21
  val const_trans_table: string Symtab.table
wenzelm@24310
    22
  val type_const_trans_table: string Symtab.table
wenzelm@24310
    23
  val ascii_of: string -> string
wenzelm@24310
    24
  val undo_ascii_of: string -> string
wenzelm@24310
    25
  val paren_pack : string list -> string
wenzelm@24310
    26
  val make_schematic_var : string * int -> string
paulson@18868
    27
  val make_fixed_var : string -> string
paulson@18868
    28
  val make_schematic_type_var : string * int -> string
wenzelm@24310
    29
  val make_fixed_type_var : string -> string
wenzelm@24310
    30
  val dfg_format: bool ref
wenzelm@24310
    31
  val make_fixed_const : string -> string
wenzelm@24310
    32
  val make_fixed_type_const : string -> string
paulson@18868
    33
  val make_type_class : string -> string
wenzelm@24310
    34
  datatype kind = Axiom | Conjecture
wenzelm@24310
    35
  type axiom_name = string
wenzelm@24310
    36
  datatype typ_var = FOLTVar of indexname | FOLTFree of string
wenzelm@24310
    37
  datatype fol_type =
wenzelm@24310
    38
      AtomV of string
wenzelm@24310
    39
    | AtomF of string
wenzelm@24310
    40
    | Comp of string * fol_type list
paulson@18868
    41
  val string_of_fol_type : fol_type -> string
wenzelm@24310
    42
  datatype type_literal = LTVar of string * string | LTFree of string * string
wenzelm@24310
    43
  val mk_typ_var_sort: typ -> typ_var * sort
wenzelm@24310
    44
  exception CLAUSE of string * term
wenzelm@24310
    45
  val isMeta : string -> bool
paulson@24937
    46
  val add_typs : (typ_var * string list) list -> type_literal list
wenzelm@24310
    47
  val get_tvar_strs: (typ_var * sort) list -> string list
wenzelm@24310
    48
  datatype arLit =
wenzelm@24310
    49
      TConsLit of class * string * string list
wenzelm@24310
    50
    | TVarLit of class * string
wenzelm@24310
    51
  datatype arityClause = ArityClause of
paulson@24937
    52
   {axiom_name: axiom_name, conclLit: arLit, premLits: arLit list}
wenzelm@24310
    53
  datatype classrelClause = ClassrelClause of
paulson@24937
    54
   {axiom_name: axiom_name, subclass: class, superclass: class}
wenzelm@24310
    55
  val make_classrel_clauses: theory -> class list -> class list -> classrelClause list
wenzelm@24310
    56
  val make_arity_clauses: theory -> string list -> class list -> class list * arityClause list
wenzelm@24310
    57
  val add_type_sort_preds: (typ_var * string list) * int Symtab.table -> int Symtab.table
wenzelm@24310
    58
  val add_classrelClause_preds : classrelClause * int Symtab.table -> int Symtab.table
wenzelm@24310
    59
  val class_of_arityLit: arLit -> class
wenzelm@24310
    60
  val add_arityClause_preds: arityClause * int Symtab.table -> int Symtab.table
wenzelm@24310
    61
  val add_foltype_funcs: fol_type * int Symtab.table -> int Symtab.table
wenzelm@24310
    62
  val add_arityClause_funcs: arityClause * int Symtab.table -> int Symtab.table
wenzelm@24310
    63
  val init_functab: int Symtab.table
wenzelm@24310
    64
  val writeln_strs: TextIO.outstream -> string list -> unit
mengj@19719
    65
  val dfg_sign: bool -> string -> string
paulson@24937
    66
  val dfg_of_typeLit: bool -> type_literal -> string
paulson@24937
    67
  val gen_dfg_cls: int * string * kind * string list * string list * string list -> string
wenzelm@24310
    68
  val string_of_preds: (string * Int.int) list -> string
wenzelm@24310
    69
  val string_of_funcs: (string * int) list -> string
wenzelm@24310
    70
  val string_of_symbols: string -> string -> string
mengj@19719
    71
  val string_of_start: string -> string
mengj@19719
    72
  val string_of_descrip : string -> string
wenzelm@24310
    73
  val dfg_tfree_clause : string -> string
mengj@19719
    74
  val dfg_classrelClause: classrelClause -> string
mengj@19719
    75
  val dfg_arity_clause: arityClause -> string
wenzelm@24310
    76
  val tptp_sign: bool -> string -> string
paulson@24937
    77
  val tptp_of_typeLit : bool -> type_literal -> string
paulson@24937
    78
  val gen_tptp_cls : int * string * kind * string list * string list -> string
wenzelm@24310
    79
  val tptp_tfree_clause : string -> string
wenzelm@24310
    80
  val tptp_arity_clause : arityClause -> string
wenzelm@24310
    81
  val tptp_classrelClause : classrelClause -> string
wenzelm@24310
    82
end
paulson@15347
    83
wenzelm@24310
    84
structure ResClause: RES_CLAUSE =
paulson@15347
    85
struct
paulson@15347
    86
paulson@15347
    87
val schematic_var_prefix = "V_";
paulson@15347
    88
val fixed_var_prefix = "v_";
paulson@15347
    89
paulson@17230
    90
val tvar_prefix = "T_";
paulson@17230
    91
val tfree_prefix = "t_";
paulson@15347
    92
wenzelm@24310
    93
val clause_prefix = "cls_";
wenzelm@24310
    94
val arclause_prefix = "clsarity_"
paulson@17525
    95
val clrelclause_prefix = "clsrel_";
paulson@15347
    96
paulson@17230
    97
val const_prefix = "c_";
wenzelm@24310
    98
val tconst_prefix = "tc_";
wenzelm@24310
    99
val class_prefix = "class_";
paulson@15347
   100
paulson@17775
   101
fun union_all xss = foldl (op union) [] xss;
paulson@17775
   102
paulson@17775
   103
(*Provide readable names for the more common symbolic functions*)
paulson@15347
   104
val const_trans_table =
paulson@15347
   105
      Symtab.make [("op =", "equal"),
wenzelm@24310
   106
                   (@{const_name HOL.less_eq}, "lessequals"),
wenzelm@24310
   107
                   (@{const_name HOL.less}, "less"),
wenzelm@24310
   108
                   ("op &", "and"),
wenzelm@24310
   109
                   ("op |", "or"),
wenzelm@24310
   110
                   (@{const_name HOL.plus}, "plus"),
wenzelm@24310
   111
                   (@{const_name HOL.minus}, "minus"),
wenzelm@24310
   112
                   (@{const_name HOL.times}, "times"),
wenzelm@24310
   113
                   (@{const_name Divides.div}, "div"),
wenzelm@24310
   114
                   (@{const_name HOL.divide}, "divide"),
wenzelm@24310
   115
                   ("op -->", "implies"),
wenzelm@24310
   116
                   ("{}", "emptyset"),
wenzelm@24310
   117
                   ("op :", "in"),
wenzelm@24310
   118
                   ("op Un", "union"),
wenzelm@24310
   119
                   ("op Int", "inter"),
wenzelm@24310
   120
                   ("List.append", "append"),
wenzelm@24310
   121
                   ("ATP_Linkup.fequal", "fequal"),
wenzelm@24310
   122
                   ("ATP_Linkup.COMBI", "COMBI"),
wenzelm@24310
   123
                   ("ATP_Linkup.COMBK", "COMBK"),
wenzelm@24310
   124
                   ("ATP_Linkup.COMBB", "COMBB"),
wenzelm@24310
   125
                   ("ATP_Linkup.COMBC", "COMBC"),
wenzelm@24310
   126
                   ("ATP_Linkup.COMBS", "COMBS"),
wenzelm@24310
   127
                   ("ATP_Linkup.COMBB'", "COMBB_e"),
wenzelm@24310
   128
                   ("ATP_Linkup.COMBC'", "COMBC_e"),
wenzelm@24310
   129
                   ("ATP_Linkup.COMBS'", "COMBS_e")];
paulson@15347
   130
paulson@17230
   131
val type_const_trans_table =
paulson@18411
   132
      Symtab.make [("*", "prod"),
wenzelm@24310
   133
                   ("+", "sum"),
wenzelm@24310
   134
                   ("~=>", "map")];
paulson@15347
   135
paulson@15610
   136
(*Escaping of special characters.
paulson@15610
   137
  Alphanumeric characters are left unchanged.
paulson@15610
   138
  The character _ goes to __
paulson@15610
   139
  Characters in the range ASCII space to / go to _A to _P, respectively.
paulson@24183
   140
  Other printing characters go to _nnn where nnn is the decimal ASCII code.*)
paulson@24183
   141
val A_minus_space = Char.ord #"A" - Char.ord #" ";
paulson@15610
   142
paulson@24183
   143
fun stringN_of_int 0 _ = ""
paulson@24183
   144
  | stringN_of_int k n = stringN_of_int (k-1) (n div 10) ^ Int.toString (n mod 10);
paulson@15610
   145
paulson@15347
   146
fun ascii_of_c c =
paulson@15610
   147
  if Char.isAlphaNum c then String.str c
paulson@15610
   148
  else if c = #"_" then "__"
wenzelm@24310
   149
  else if #" " <= c andalso c <= #"/"
paulson@15610
   150
       then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
wenzelm@24310
   151
  else if Char.isPrint c
paulson@24183
   152
       then ("_" ^ stringN_of_int 3 (Char.ord c))  (*fixed width, in case more digits follow*)
paulson@15610
   153
  else ""
paulson@15347
   154
paulson@15610
   155
val ascii_of = String.translate ascii_of_c;
paulson@15610
   156
paulson@24183
   157
(** Remove ASCII armouring from names in proof files **)
paulson@24183
   158
paulson@24183
   159
(*We don't raise error exceptions because this code can run inside the watcher.
paulson@24183
   160
  Also, the errors are "impossible" (hah!)*)
paulson@24183
   161
fun undo_ascii_aux rcs [] = String.implode(rev rcs)
paulson@24183
   162
  | undo_ascii_aux rcs [#"_"] = undo_ascii_aux (#"_"::rcs) []  (*ERROR*)
paulson@24183
   163
      (*Three types of _ escapes: __, _A to _P, _nnn*)
paulson@24183
   164
  | undo_ascii_aux rcs (#"_" :: #"_" :: cs) = undo_ascii_aux (#"_"::rcs) cs
wenzelm@24310
   165
  | undo_ascii_aux rcs (#"_" :: c :: cs) =
paulson@24183
   166
      if #"A" <= c andalso c<= #"P"  (*translation of #" " to #"/"*)
paulson@24183
   167
      then undo_ascii_aux (Char.chr(Char.ord c - A_minus_space) :: rcs) cs
wenzelm@24310
   168
      else
paulson@24183
   169
        let val digits = List.take (c::cs, 3) handle Subscript => []
wenzelm@24310
   170
        in
paulson@24183
   171
            case Int.fromString (String.implode digits) of
paulson@24183
   172
                NONE => undo_ascii_aux (c:: #"_"::rcs) cs  (*ERROR*)
paulson@24183
   173
              | SOME n => undo_ascii_aux (Char.chr n :: rcs) (List.drop (cs, 2))
paulson@24183
   174
        end
paulson@24183
   175
  | undo_ascii_aux rcs (c::cs) = undo_ascii_aux (c::rcs) cs;
paulson@24183
   176
paulson@24183
   177
val undo_ascii_of = undo_ascii_aux [] o String.explode;
paulson@15347
   178
paulson@17525
   179
(* convert a list of strings into one single string; surrounded by brackets *)
paulson@18218
   180
fun paren_pack [] = ""   (*empty argument list*)
paulson@18218
   181
  | paren_pack strings = "(" ^ commas strings ^ ")";
paulson@17525
   182
paulson@21509
   183
(*TSTP format uses (...) rather than the old [...]*)
paulson@21509
   184
fun tptp_pack strings = "(" ^ space_implode " | " strings ^ ")";
paulson@17525
   185
paulson@17525
   186
paulson@16925
   187
(*Remove the initial ' character from a type variable, if it is present*)
paulson@16925
   188
fun trim_type_var s =
paulson@16925
   189
  if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
paulson@16925
   190
  else error ("trim_type: Malformed type variable encountered: " ^ s);
paulson@16925
   191
paulson@16903
   192
fun ascii_of_indexname (v,0) = ascii_of v
paulson@17525
   193
  | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
paulson@15347
   194
paulson@17230
   195
fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
paulson@15347
   196
fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
paulson@15347
   197
wenzelm@24310
   198
fun make_schematic_type_var (x,i) =
paulson@16925
   199
      tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
paulson@16925
   200
fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
paulson@15347
   201
paulson@23075
   202
(*HACK because SPASS truncates identifiers to 63 characters :-(( *)
paulson@23075
   203
val dfg_format = ref false;
paulson@23075
   204
paulson@23075
   205
(*32-bit hash,so we expect no collisions unless there are around 65536 long identifiers...*)
paulson@23075
   206
fun controlled_length s =
wenzelm@24310
   207
  if size s > 60 andalso !dfg_format
paulson@23075
   208
  then Word.toString (Polyhash.hashw_string(s,0w0))
paulson@23075
   209
  else s;
paulson@23075
   210
paulson@18411
   211
fun lookup_const c =
wenzelm@17412
   212
    case Symtab.lookup const_trans_table c of
paulson@17230
   213
        SOME c' => c'
paulson@23075
   214
      | NONE => controlled_length (ascii_of c);
paulson@17230
   215
wenzelm@24310
   216
fun lookup_type_const c =
wenzelm@17412
   217
    case Symtab.lookup type_const_trans_table c of
paulson@17230
   218
        SOME c' => c'
paulson@23075
   219
      | NONE => controlled_length (ascii_of c);
paulson@18411
   220
paulson@18411
   221
fun make_fixed_const "op =" = "equal"   (*MUST BE "equal" because it's built-in to ATPs*)
paulson@18411
   222
  | make_fixed_const c      = const_prefix ^ lookup_const c;
paulson@18411
   223
paulson@18411
   224
fun make_fixed_type_const c = tconst_prefix ^ lookup_type_const c;
quigley@17150
   225
wenzelm@17261
   226
fun make_type_class clas = class_prefix ^ ascii_of clas;
quigley@17150
   227
quigley@17150
   228
paulson@18798
   229
(***** definitions and functions for FOL clauses, for conversion to TPTP or DFG format. *****)
paulson@15347
   230
paulson@21509
   231
datatype kind = Axiom | Conjecture;
paulson@23385
   232
paulson@15347
   233
type axiom_name = string;
paulson@15347
   234
paulson@15347
   235
(**** Isabelle FOL clauses ****)
paulson@15347
   236
mengj@18402
   237
datatype typ_var = FOLTVar of indexname | FOLTFree of string;
mengj@18402
   238
paulson@18798
   239
(*FIXME: give the constructors more sensible names*)
mengj@18402
   240
datatype fol_type = AtomV of string
wenzelm@24310
   241
                  | AtomF of string
wenzelm@24310
   242
                  | Comp of string * fol_type list;
mengj@18402
   243
mengj@18402
   244
fun string_of_fol_type (AtomV x) = x
mengj@18402
   245
  | string_of_fol_type (AtomF x) = x
wenzelm@24310
   246
  | string_of_fol_type (Comp(tcon,tps)) =
paulson@18856
   247
      tcon ^ (paren_pack (map string_of_fol_type tps));
wenzelm@24310
   248
paulson@18798
   249
(*First string is the type class; the second is a TVar or TFfree*)
paulson@18798
   250
datatype type_literal = LTVar of string * string | LTFree of string * string;
paulson@15347
   251
mengj@17999
   252
fun mk_typ_var_sort (TFree(a,s)) = (FOLTFree a,s)
mengj@17999
   253
  | mk_typ_var_sort (TVar(v,s)) = (FOLTVar v,s);
mengj@17999
   254
mengj@17999
   255
paulson@17404
   256
exception CLAUSE of string * term;
paulson@15347
   257
paulson@17317
   258
mengj@18402
   259
(*Flatten a type to a fol_type while accumulating sort constraints on the TFrees and
wenzelm@24310
   260
  TVars it contains.*)
wenzelm@24310
   261
fun type_of (Type (a, Ts)) =
wenzelm@24310
   262
      let val (folTyps, ts) = types_of Ts
wenzelm@24310
   263
          val t = make_fixed_type_const a
paulson@18798
   264
      in (Comp(t,folTyps), ts) end
wenzelm@24310
   265
  | type_of (TFree (a,s)) = (AtomF(make_fixed_type_var a), [(FOLTFree a, s)])
paulson@18798
   266
  | type_of (TVar (v, s)) = (AtomV(make_schematic_type_var v), [(FOLTVar v, s)])
paulson@18218
   267
and types_of Ts =
paulson@18798
   268
      let val (folTyps,ts) = ListPair.unzip (map type_of Ts)
paulson@18798
   269
      in (folTyps, union_all ts) end;
paulson@15390
   270
mengj@18439
   271
paulson@16903
   272
(* Any variables created via the METAHYPS tactical should be treated as
paulson@16903
   273
   universal vars, although it is represented as "Free(...)" by Isabelle *)
paulson@16903
   274
val isMeta = String.isPrefix "METAHYP1_"
quigley@17150
   275
wenzelm@24310
   276
(*Make literals for sorted type variables*)
paulson@22643
   277
fun sorts_on_typs (_, [])   = []
wenzelm@24310
   278
  | sorts_on_typs (v,  s::ss) =
paulson@22643
   279
      let val sorts = sorts_on_typs (v, ss)
paulson@22643
   280
      in
paulson@22643
   281
          if s = "HOL.type" then sorts
paulson@22643
   282
          else case v of
paulson@22643
   283
              FOLTVar indx => LTVar(make_type_class s, make_schematic_type_var indx) :: sorts
paulson@22643
   284
            | FOLTFree x => LTFree(make_type_class s, make_fixed_type_var x) :: sorts
paulson@22643
   285
      end;
quigley@17150
   286
paulson@18798
   287
fun pred_of_sort (LTVar (s,ty)) = (s,1)
paulson@22643
   288
  | pred_of_sort (LTFree (s,ty)) = (s,1)
quigley@17150
   289
paulson@24937
   290
(*Given a list of sorted type variables, return a list of type literals.*)
paulson@24937
   291
fun add_typs tss = foldl (op union) [] (map sorts_on_typs tss);
mengj@20015
   292
mengj@20015
   293
(** make axiom and conjecture clauses. **)
mengj@20015
   294
mengj@20015
   295
fun get_tvar_strs [] = []
wenzelm@24310
   296
  | get_tvar_strs ((FOLTVar indx,s)::tss) =
haftmann@20854
   297
      insert (op =) (make_schematic_type_var indx) (get_tvar_strs tss)
mengj@20015
   298
  | get_tvar_strs((FOLTFree x,s)::tss) = get_tvar_strs tss
mengj@20015
   299
wenzelm@24310
   300
mengj@19354
   301
paulson@15347
   302
(**** Isabelle arities ****)
paulson@15347
   303
paulson@15347
   304
exception ARCLAUSE of string;
paulson@15347
   305
wenzelm@24310
   306
datatype arLit = TConsLit of class * string * string list
paulson@22643
   307
               | TVarLit of class * string;
wenzelm@24310
   308
wenzelm@24310
   309
datatype arityClause =
wenzelm@24310
   310
         ArityClause of {axiom_name: axiom_name,
wenzelm@24310
   311
                         conclLit: arLit,
wenzelm@24310
   312
                         premLits: arLit list};
paulson@15347
   313
paulson@15347
   314
paulson@18798
   315
fun gen_TVars 0 = []
paulson@18798
   316
  | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
paulson@15347
   317
paulson@18411
   318
fun pack_sort(_,[])  = []
paulson@18411
   319
  | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
paulson@22643
   320
  | pack_sort(tvar, cls::srt) =  (cls, tvar) :: pack_sort(tvar, srt);
wenzelm@24310
   321
paulson@18411
   322
(*Arity of type constructor tcon :: (arg1,...,argN)res*)
paulson@22643
   323
fun make_axiom_arity_clause (tcons, axiom_name, (cls,args)) =
paulson@21560
   324
   let val tvars = gen_TVars (length args)
paulson@17845
   325
       val tvars_srts = ListPair.zip (tvars,args)
paulson@17845
   326
   in
paulson@24937
   327
      ArityClause {axiom_name = axiom_name, 
wenzelm@24310
   328
                   conclLit = TConsLit (cls, make_fixed_type_const tcons, tvars),
paulson@22643
   329
                   premLits = map TVarLit (union_all(map pack_sort tvars_srts))}
paulson@17845
   330
   end;
paulson@15347
   331
paulson@15347
   332
paulson@15347
   333
(**** Isabelle class relations ****)
paulson@15347
   334
wenzelm@24310
   335
datatype classrelClause =
wenzelm@24310
   336
         ClassrelClause of {axiom_name: axiom_name,
wenzelm@24310
   337
                            subclass: class,
wenzelm@24310
   338
                            superclass: class};
wenzelm@24310
   339
paulson@21290
   340
(*Generate all pairs (sub,super) such that sub is a proper subclass of super in theory thy.*)
paulson@21432
   341
fun class_pairs thy [] supers = []
paulson@21432
   342
  | class_pairs thy subs supers =
paulson@21432
   343
      let val class_less = Sorts.class_less(Sign.classes_of thy)
wenzelm@24310
   344
          fun add_super sub (super,pairs) =
wenzelm@24310
   345
                if class_less (sub,super) then (sub,super)::pairs else pairs
wenzelm@24310
   346
          fun add_supers (sub,pairs) = foldl (add_super sub) pairs supers
paulson@21432
   347
      in  foldl add_supers [] subs  end;
paulson@15347
   348
paulson@21290
   349
fun make_classrelClause (sub,super) =
paulson@21290
   350
  ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of sub ^ "_" ^ ascii_of super,
wenzelm@24310
   351
                  subclass = make_type_class sub,
paulson@21290
   352
                  superclass = make_type_class super};
paulson@15347
   353
paulson@21290
   354
fun make_classrel_clauses thy subs supers =
paulson@21290
   355
  map make_classrelClause (class_pairs thy subs supers);
paulson@18868
   356
paulson@18868
   357
paulson@18868
   358
(** Isabelle arities **)
paulson@17845
   359
paulson@21373
   360
fun arity_clause _ _ (tcons, []) = []
paulson@21373
   361
  | arity_clause seen n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
paulson@21373
   362
      arity_clause seen n (tcons,ars)
paulson@21373
   363
  | arity_clause seen n (tcons, (ar as (class,_)) :: ars) =
paulson@21373
   364
      if class mem_string seen then (*multiple arities for the same tycon, class pair*)
wenzelm@24310
   365
          make_axiom_arity_clause (tcons, lookup_type_const tcons ^ "_" ^ class ^ "_" ^ Int.toString n, ar) ::
wenzelm@24310
   366
          arity_clause seen (n+1) (tcons,ars)
paulson@21373
   367
      else
wenzelm@24310
   368
          make_axiom_arity_clause (tcons, lookup_type_const tcons ^ "_" ^ class, ar) ::
wenzelm@24310
   369
          arity_clause (class::seen) n (tcons,ars)
paulson@17845
   370
paulson@17845
   371
fun multi_arity_clause [] = []
paulson@19155
   372
  | multi_arity_clause ((tcons,ars) :: tc_arlists) =
wenzelm@24310
   373
      arity_clause [] 1 (tcons, ars)  @  multi_arity_clause tc_arlists
paulson@17845
   374
paulson@22643
   375
(*Generate all pairs (tycon,class,sorts) such that tycon belongs to class in theory thy
paulson@22643
   376
  provided its arguments have the corresponding sorts.*)
paulson@21373
   377
fun type_class_pairs thy tycons classes =
paulson@21373
   378
  let val alg = Sign.classes_of thy
paulson@21373
   379
      fun domain_sorts (tycon,class) = Sorts.mg_domain alg tycon [class]
wenzelm@24310
   380
      fun add_class tycon (class,pairs) =
wenzelm@24310
   381
            (class, domain_sorts(tycon,class))::pairs
paulson@21373
   382
            handle Sorts.CLASS_ERROR _ => pairs
paulson@21373
   383
      fun try_classes tycon = (tycon, foldl (add_class tycon) [] classes)
paulson@21373
   384
  in  map try_classes tycons  end;
paulson@21373
   385
paulson@22643
   386
(*Proving one (tycon, class) membership may require proving others, so iterate.*)
paulson@22643
   387
fun iter_type_class_pairs thy tycons [] = ([], [])
paulson@22643
   388
  | iter_type_class_pairs thy tycons classes =
paulson@22643
   389
      let val cpairs = type_class_pairs thy tycons classes
paulson@22643
   390
          val newclasses = union_all (union_all (union_all (map (map #2 o #2) cpairs))) \\ classes \\ HOLogic.typeS
wenzelm@24310
   391
          val _ = if null newclasses then ()
paulson@22643
   392
                  else Output.debug (fn _ => "New classes: " ^ space_implode ", " newclasses)
wenzelm@24310
   393
          val (classes', cpairs') = iter_type_class_pairs thy tycons newclasses
paulson@22643
   394
      in  (classes' union classes, cpairs' union cpairs)  end;
wenzelm@24310
   395
paulson@22643
   396
fun make_arity_clauses thy tycons classes =
wenzelm@24310
   397
  let val (classes', cpairs) = iter_type_class_pairs thy tycons classes
paulson@22643
   398
  in  (classes', multi_arity_clause cpairs)  end;
paulson@17845
   399
paulson@17845
   400
paulson@18868
   401
(**** Find occurrences of predicates in clauses ****)
paulson@18868
   402
wenzelm@24310
   403
(*FIXME: multiple-arity checking doesn't work, as update_new is the wrong
paulson@18868
   404
  function (it flags repeated declarations of a function, even with the same arity)*)
paulson@18868
   405
paulson@18868
   406
fun update_many (tab, keypairs) = foldl (uncurry Symtab.update) tab keypairs;
paulson@18868
   407
wenzelm@24310
   408
fun add_type_sort_preds ((FOLTVar indx,s), preds) =
paulson@18868
   409
      update_many (preds, map pred_of_sort (sorts_on_typs (FOLTVar indx, s)))
paulson@18868
   410
  | add_type_sort_preds ((FOLTFree x,s), preds) =
paulson@18868
   411
      update_many (preds, map pred_of_sort (sorts_on_typs (FOLTFree x, s)));
paulson@17845
   412
paulson@18868
   413
fun add_classrelClause_preds (ClassrelClause {subclass,superclass,...}, preds) =
paulson@18868
   414
  Symtab.update (subclass,1) (Symtab.update (superclass,1) preds);
paulson@17845
   415
paulson@22643
   416
fun class_of_arityLit (TConsLit (tclass, _, _)) = tclass
paulson@22643
   417
  | class_of_arityLit (TVarLit (tclass, _)) = tclass;
paulson@21373
   418
paulson@21373
   419
fun add_arityClause_preds (ArityClause {conclLit,premLits,...}, preds) =
paulson@22643
   420
  let val classes = map (make_type_class o class_of_arityLit) (conclLit::premLits)
paulson@21373
   421
      fun upd (class,preds) = Symtab.update (class,1) preds
paulson@21373
   422
  in  foldl upd preds classes  end;
paulson@18868
   423
paulson@18868
   424
(*** Find occurrences of functions in clauses ***)
paulson@18868
   425
paulson@18868
   426
fun add_foltype_funcs (AtomV _, funcs) = funcs
paulson@18868
   427
  | add_foltype_funcs (AtomF a, funcs) = Symtab.update (a,0) funcs
wenzelm@24310
   428
  | add_foltype_funcs (Comp(a,tys), funcs) =
paulson@18868
   429
      foldl add_foltype_funcs (Symtab.update (a, length tys) funcs) tys;
paulson@18868
   430
paulson@20038
   431
(*TFrees are recorded as constants*)
paulson@20038
   432
fun add_type_sort_funcs ((FOLTVar _, _), funcs) = funcs
wenzelm@24310
   433
  | add_type_sort_funcs ((FOLTFree a, _), funcs) =
paulson@20038
   434
      Symtab.update (make_fixed_type_var a, 0) funcs
paulson@20038
   435
paulson@18868
   436
fun add_arityClause_funcs (ArityClause {conclLit,...}, funcs) =
paulson@22643
   437
  let val TConsLit (_, tcons, tvars) = conclLit
paulson@18868
   438
  in  Symtab.update (tcons, length tvars) funcs  end;
paulson@17845
   439
paulson@23075
   440
(*This type can be overlooked because it is built-in...*)
paulson@23075
   441
val init_functab = Symtab.update ("tc_itself", 1) Symtab.empty;
paulson@23075
   442
paulson@18868
   443
paulson@18868
   444
(**** String-oriented operations ****)
paulson@15347
   445
wenzelm@24310
   446
fun string_of_clausename (cls_id,ax_name) =
paulson@17525
   447
    clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id;
paulson@17317
   448
wenzelm@24310
   449
fun string_of_type_clsname (cls_id,ax_name,idx) =
paulson@17525
   450
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
paulson@18863
   451
wenzelm@24310
   452
fun writeln_strs os = List.app (fn s => TextIO.output (os, s));
paulson@18863
   453
wenzelm@24310
   454
paulson@18868
   455
(**** Producing DFG files ****)
quigley@17150
   456
paulson@18863
   457
(*Attach sign in DFG syntax: false means negate.*)
paulson@18863
   458
fun dfg_sign true s = s
wenzelm@24310
   459
  | dfg_sign false s = "not(" ^ s ^ ")"
paulson@18863
   460
paulson@24937
   461
fun dfg_of_typeLit pos (LTVar (s,ty))  = dfg_sign pos (s ^ "(" ^ ty ^ ")")
paulson@24937
   462
  | dfg_of_typeLit pos (LTFree (s,ty)) = dfg_sign pos (s ^ "(" ^ ty ^ ")");
wenzelm@24310
   463
paulson@18868
   464
(*Enclose the clause body by quantifiers, if necessary*)
wenzelm@24310
   465
fun dfg_forall [] body = body
paulson@18868
   466
  | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
quigley@17150
   467
paulson@24937
   468
fun gen_dfg_cls (cls_id, ax_name, Axiom, lits, tylits, vars) =
paulson@24937
   469
      "clause( %(axiom)\n" ^
paulson@24937
   470
      dfg_forall vars ("or(" ^ commas (tylits@lits) ^ ")") ^ ",\n" ^
paulson@24937
   471
      string_of_clausename (cls_id,ax_name) ^  ").\n\n"
paulson@24937
   472
  | gen_dfg_cls (cls_id, ax_name, Conjecture, lits, _, vars) =
paulson@24937
   473
      "clause( %(negated_conjecture)\n" ^
paulson@24937
   474
      dfg_forall vars ("or(" ^ commas lits ^ ")") ^ ",\n" ^
paulson@24937
   475
      string_of_clausename (cls_id,ax_name) ^  ").\n\n";
quigley@17150
   476
paulson@18798
   477
fun string_of_arity (name, num) =  "(" ^ name ^ "," ^ Int.toString num ^ ")"
quigley@17150
   478
paulson@18856
   479
fun string_of_preds [] = ""
paulson@18856
   480
  | string_of_preds preds = "predicates[" ^ commas(map string_of_arity preds) ^ "].\n";
quigley@17150
   481
paulson@18856
   482
fun string_of_funcs [] = ""
paulson@18856
   483
  | string_of_funcs funcs = "functions[" ^ commas(map string_of_arity funcs) ^ "].\n" ;
quigley@17150
   484
wenzelm@24310
   485
fun string_of_symbols predstr funcstr =
paulson@17234
   486
  "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
quigley@17150
   487
paulson@18798
   488
fun string_of_start name = "begin_problem(" ^ name ^ ").\n\n";
quigley@17150
   489
wenzelm@24310
   490
fun string_of_descrip name =
wenzelm@24310
   491
  "list_of_descriptions.\nname({*" ^ name ^
paulson@18868
   492
  "*}).\nauthor({*Isabelle*}).\nstatus(unknown).\ndescription({*auto-generated*}).\nend_of_list.\n\n"
quigley@17150
   493
paulson@18863
   494
fun dfg_tfree_clause tfree_lit =
paulson@21509
   495
  "clause( %(negated_conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ").\n\n"
paulson@18863
   496
paulson@22643
   497
fun dfg_of_arLit (TConsLit (c,t,args)) =
paulson@22643
   498
      dfg_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@22643
   499
  | dfg_of_arLit (TVarLit (c,str)) =
paulson@22643
   500
      dfg_sign false (make_type_class c ^ "(" ^ str ^ ")")
wenzelm@24310
   501
paulson@20038
   502
fun dfg_classrelLits sub sup =  "not(" ^ sub ^ "(T)), " ^ sup ^ "(T)";
paulson@17525
   503
paulson@18868
   504
fun dfg_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
paulson@18868
   505
  "clause(forall([T],\nor( " ^ dfg_classrelLits subclass superclass ^ ")),\n" ^
paulson@18868
   506
  axiom_name ^ ").\n\n";
wenzelm@24310
   507
paulson@21560
   508
fun string_of_ar axiom_name = arclause_prefix ^ ascii_of axiom_name;
paulson@21560
   509
paulson@24937
   510
fun dfg_arity_clause (ArityClause{axiom_name,conclLit,premLits,...}) =
paulson@22643
   511
  let val TConsLit (_,_,tvars) = conclLit
paulson@18868
   512
      val lits = map dfg_of_arLit (conclLit :: premLits)
paulson@18863
   513
  in
paulson@24937
   514
      "clause( %(axiom)\n" ^
paulson@18868
   515
      dfg_forall tvars ("or( " ^ commas lits ^ ")") ^ ",\n" ^
paulson@21560
   516
      string_of_ar axiom_name ^ ").\n\n"
paulson@18863
   517
  end;
paulson@18863
   518
quigley@17150
   519
paulson@18869
   520
(**** Produce TPTP files ****)
paulson@18868
   521
paulson@18868
   522
(*Attach sign in TPTP syntax: false means negate.*)
paulson@21509
   523
fun tptp_sign true s = s
paulson@21509
   524
  | tptp_sign false s = "~ " ^ s
paulson@18868
   525
paulson@24937
   526
fun tptp_of_typeLit pos (LTVar (s,ty))  = tptp_sign pos (s ^ "(" ^ ty ^ ")")
paulson@24937
   527
  | tptp_of_typeLit pos (LTFree (s,ty)) = tptp_sign pos  (s ^ "(" ^ ty ^ ")");
wenzelm@24310
   528
paulson@24937
   529
fun gen_tptp_cls (cls_id,ax_name,Axiom,lits,tylits) =
paulson@24937
   530
      "cnf(" ^ string_of_clausename (cls_id,ax_name) ^ ",axiom," ^ 
paulson@24937
   531
               tptp_pack (tylits@lits) ^ ").\n"
paulson@24937
   532
  | gen_tptp_cls (cls_id,ax_name,Conjecture,lits,_) =
paulson@24937
   533
      "cnf(" ^ string_of_clausename (cls_id,ax_name) ^ ",negated_conjecture," ^ 
paulson@24937
   534
               tptp_pack lits ^ ").\n";
paulson@15347
   535
paulson@18863
   536
fun tptp_tfree_clause tfree_lit =
paulson@21509
   537
    "cnf(" ^ "tfree_tcs," ^ "negated_conjecture" ^ "," ^ tptp_pack[tfree_lit] ^ ").\n";
wenzelm@24310
   538
paulson@22643
   539
fun tptp_of_arLit (TConsLit (c,t,args)) =
paulson@22643
   540
      tptp_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@22643
   541
  | tptp_of_arLit (TVarLit (c,str)) =
paulson@22643
   542
      tptp_sign false (make_type_class c ^ "(" ^ str ^ ")")
wenzelm@24310
   543
paulson@24937
   544
fun tptp_arity_clause (ArityClause{axiom_name,conclLit,premLits,...}) =
paulson@24937
   545
  "cnf(" ^ string_of_ar axiom_name ^ ",axiom," ^
paulson@21560
   546
  tptp_pack (map tptp_of_arLit (conclLit :: premLits)) ^ ").\n";
paulson@15347
   547
wenzelm@24310
   548
fun tptp_classrelLits sub sup =
paulson@21509
   549
  let val tvar = "(T)"
paulson@21509
   550
  in  tptp_pack [tptp_sign false (sub^tvar), tptp_sign true (sup^tvar)]  end;
paulson@15347
   551
paulson@18868
   552
fun tptp_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
wenzelm@24310
   553
  "cnf(" ^ axiom_name ^ ",axiom," ^ tptp_classrelLits subclass superclass ^ ").\n"
quigley@17150
   554
paulson@15347
   555
end;