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