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