src/HOL/Tools/res_clause.ML
author paulson
Tue May 22 17:56:06 2007 +0200 (2007-05-22)
changeset 23075 69e30a7e8880
parent 23029 79ee75dc1e59
child 23385 0ef4f9fc0d09
permissions -rw-r--r--
Some hacks for SPASS format
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@15347
    10
signature RES_CLAUSE =
paulson@15347
    11
  sig
paulson@18868
    12
  exception CLAUSE of string * term
paulson@18868
    13
  type clause and arityClause and classrelClause
paulson@20824
    14
  datatype fol_type = AtomV of string
paulson@20824
    15
                    | AtomF of string
paulson@20824
    16
                    | Comp of string * fol_type list;
paulson@21813
    17
  datatype fol_term = UVar of string
paulson@20824
    18
                    | Fun of string * fol_type list * fol_term list;
paulson@20824
    19
  datatype predicate = Predicate of string * fol_type list * fol_term list;
paulson@21509
    20
  datatype kind = Axiom | Conjecture;
paulson@21509
    21
  val name_of_kind : kind -> string
paulson@20824
    22
  type typ_var and type_literal and literal
paulson@20824
    23
  val literals_of_term: Term.term -> literal list * (typ_var * Term.sort) list
paulson@18868
    24
  val add_typs_aux : (typ_var * string list) list -> type_literal list * type_literal list
paulson@18868
    25
  val ascii_of : string -> string
paulson@21509
    26
  val tptp_pack : string list -> string
paulson@22643
    27
  val make_arity_clauses: theory -> (class list * arityClause list)
paulson@21290
    28
  val make_classrel_clauses: theory -> class list -> class list -> classrelClause list 
paulson@18868
    29
  val clause_prefix : string 
paulson@18868
    30
  val clause2tptp : clause -> string * string list
paulson@18868
    31
  val const_prefix : string
paulson@20022
    32
  val dfg_write_file:  thm list -> string -> 
paulson@20022
    33
       ((thm * (string * int)) list * classrelClause list * arityClause list) -> string list
paulson@18868
    34
  val fixed_var_prefix : string
paulson@21509
    35
  val gen_tptp_cls : int * string * string * string list -> string
paulson@18868
    36
  val get_axiomName : clause ->  string
paulson@20824
    37
  val get_literals : clause -> literal list
paulson@18868
    38
  val init : theory -> unit
paulson@18868
    39
  val isMeta : string -> bool
paulson@18868
    40
  val isTaut : clause -> bool
paulson@18868
    41
  val list_ord : ('a * 'b -> order) -> 'a list * 'b list -> order
mengj@19443
    42
  val make_axiom_clause : thm -> string * int -> clause option
mengj@19443
    43
  val make_conjecture_clauses : thm list -> clause list
paulson@18868
    44
  val make_fixed_const : string -> string		
paulson@18868
    45
  val make_fixed_type_const : string -> string   
paulson@18868
    46
  val make_fixed_type_var : string -> string
paulson@18868
    47
  val make_fixed_var : string -> string
paulson@18868
    48
  val make_schematic_type_var : string * int -> string
paulson@18868
    49
  val make_schematic_var : string * int -> string
paulson@18868
    50
  val make_type_class : string -> string
paulson@18868
    51
  val mk_typ_var_sort : Term.typ -> typ_var * sort
paulson@18868
    52
  val paren_pack : string list -> string
paulson@18868
    53
  val schematic_var_prefix : string
paulson@18868
    54
  val string_of_fol_type : fol_type -> string
paulson@18868
    55
  val tconst_prefix : string 
paulson@18868
    56
  val tfree_prefix : string
paulson@21813
    57
  val tvar_prefix : string
paulson@17404
    58
  val tptp_arity_clause : arityClause -> string
paulson@17404
    59
  val tptp_classrelClause : classrelClause -> string
paulson@18868
    60
  val tptp_of_typeLit : type_literal -> string
paulson@18863
    61
  val tptp_tfree_clause : string -> string
paulson@20022
    62
  val tptp_write_file: thm list -> string -> 
paulson@20022
    63
       ((thm * (string * int)) list * classrelClause list * arityClause list) -> string list
mengj@17908
    64
  val union_all : ''a list list -> ''a list
paulson@18863
    65
  val writeln_strs: TextIO.outstream -> TextIO.vector list -> unit
mengj@19719
    66
  val dfg_sign: bool -> string -> string
mengj@19719
    67
  val dfg_of_typeLit: type_literal -> string
mengj@19719
    68
  val get_tvar_strs: (typ_var * sort) list -> string list
mengj@19719
    69
  val gen_dfg_cls: int * string * string * string * string list -> string
mengj@19719
    70
  val add_foltype_funcs: fol_type * int Symtab.table -> int Symtab.table
mengj@19719
    71
  val add_arityClause_funcs: arityClause * int Symtab.table -> int Symtab.table
mengj@19719
    72
  val add_arityClause_preds: arityClause * int Symtab.table -> int Symtab.table
mengj@19719
    73
  val add_classrelClause_preds : classrelClause * int Symtab.table -> int Symtab.table
mengj@19719
    74
  val dfg_tfree_clause : string -> string
mengj@19719
    75
  val string_of_start: string -> string
mengj@19719
    76
  val string_of_descrip : string -> string
mengj@19719
    77
  val string_of_symbols: string -> string -> string
mengj@19719
    78
  val string_of_funcs: (string * int) list -> string
mengj@19719
    79
  val string_of_preds: (string * Int.int) list -> string
mengj@19719
    80
  val dfg_classrelClause: classrelClause -> string
mengj@19719
    81
  val dfg_arity_clause: arityClause -> string
mengj@19719
    82
end;
paulson@15347
    83
paulson@20422
    84
structure ResClause =
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
paulson@15347
    93
val clause_prefix = "cls_"; 
paulson@17525
    94
val arclause_prefix = "clsarity_" 
paulson@17525
    95
val clrelclause_prefix = "clsrel_";
paulson@15347
    96
paulson@17230
    97
val const_prefix = "c_";
paulson@17230
    98
val tconst_prefix = "tc_"; 
paulson@16199
    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"),
haftmann@22997
   106
	  	   (@{const_name Orderings.less_eq}, "lessequals"),
haftmann@22997
   107
		   (@{const_name Orderings.less}, "less"),
paulson@15347
   108
		   ("op &", "and"),
paulson@15347
   109
		   ("op |", "or"),
haftmann@22997
   110
		   (@{const_name HOL.plus}, "plus"),
haftmann@22997
   111
		   (@{const_name HOL.minus}, "minus"),
haftmann@22997
   112
		   (@{const_name HOL.times}, "times"),
haftmann@22997
   113
		   (@{const_name Divides.div}, "div"),
haftmann@22997
   114
		   (@{const_name HOL.divide}, "divide"),
paulson@15347
   115
		   ("op -->", "implies"),
paulson@17375
   116
		   ("{}", "emptyset"),
paulson@15347
   117
		   ("op :", "in"),
paulson@15347
   118
		   ("op Un", "union"),
paulson@18390
   119
		   ("op Int", "inter"),
haftmann@23029
   120
		   ("List.append", "append"),
wenzelm@21254
   121
		   ("ATP_Linkup.fequal", "fequal"),
wenzelm@21254
   122
		   ("ATP_Linkup.COMBI", "COMBI"),
wenzelm@21254
   123
		   ("ATP_Linkup.COMBK", "COMBK"),
wenzelm@21254
   124
		   ("ATP_Linkup.COMBB", "COMBB"),
wenzelm@21254
   125
		   ("ATP_Linkup.COMBC", "COMBC"),
wenzelm@21254
   126
		   ("ATP_Linkup.COMBS", "COMBS"),
wenzelm@21254
   127
		   ("ATP_Linkup.COMBB'", "COMBB_e"),
wenzelm@21254
   128
		   ("ATP_Linkup.COMBC'", "COMBC_e"),
wenzelm@21254
   129
		   ("ATP_Linkup.COMBS'", "COMBS_e")];
paulson@15347
   130
paulson@17230
   131
val type_const_trans_table =
paulson@18411
   132
      Symtab.make [("*", "prod"),
paulson@18411
   133
	  	   ("+", "sum"),
paulson@18411
   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@15610
   140
  Other printing characters go to _NNN where NNN is the decimal ASCII code.*)
paulson@15610
   141
local
paulson@15610
   142
paulson@15610
   143
val A_minus_space = Char.ord #"A" - Char.ord #" ";
paulson@15610
   144
paulson@15347
   145
fun ascii_of_c c =
paulson@15610
   146
  if Char.isAlphaNum c then String.str c
paulson@15610
   147
  else if c = #"_" then "__"
paulson@15610
   148
  else if #" " <= c andalso c <= #"/" 
paulson@15610
   149
       then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
paulson@15610
   150
  else if Char.isPrint c then ("_" ^ Int.toString (Char.ord c))
paulson@15610
   151
  else ""
paulson@15347
   152
paulson@15610
   153
in
paulson@15610
   154
paulson@15610
   155
val ascii_of = String.translate ascii_of_c;
paulson@15610
   156
paulson@15610
   157
end;
paulson@15347
   158
paulson@17525
   159
(* convert a list of strings into one single string; surrounded by brackets *)
paulson@18218
   160
fun paren_pack [] = ""   (*empty argument list*)
paulson@18218
   161
  | paren_pack strings = "(" ^ commas strings ^ ")";
paulson@17525
   162
paulson@21509
   163
(*TSTP format uses (...) rather than the old [...]*)
paulson@21509
   164
fun tptp_pack strings = "(" ^ space_implode " | " strings ^ ")";
paulson@17525
   165
paulson@17525
   166
paulson@16925
   167
(*Remove the initial ' character from a type variable, if it is present*)
paulson@16925
   168
fun trim_type_var s =
paulson@16925
   169
  if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
paulson@16925
   170
  else error ("trim_type: Malformed type variable encountered: " ^ s);
paulson@16925
   171
paulson@16903
   172
fun ascii_of_indexname (v,0) = ascii_of v
paulson@17525
   173
  | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
paulson@15347
   174
paulson@17230
   175
fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
paulson@15347
   176
fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
paulson@15347
   177
paulson@16925
   178
fun make_schematic_type_var (x,i) = 
paulson@16925
   179
      tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
paulson@16925
   180
fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
paulson@15347
   181
paulson@23075
   182
(*HACK because SPASS truncates identifiers to 63 characters :-(( *)
paulson@23075
   183
val dfg_format = ref false;
paulson@23075
   184
paulson@23075
   185
(*32-bit hash,so we expect no collisions unless there are around 65536 long identifiers...*)
paulson@23075
   186
fun controlled_length s =
paulson@23075
   187
  if size s > 60 andalso !dfg_format   
paulson@23075
   188
  then Word.toString (Polyhash.hashw_string(s,0w0))
paulson@23075
   189
  else s;
paulson@23075
   190
paulson@18411
   191
fun lookup_const c =
wenzelm@17412
   192
    case Symtab.lookup const_trans_table c of
paulson@17230
   193
        SOME c' => c'
paulson@23075
   194
      | NONE => controlled_length (ascii_of c);
paulson@17230
   195
paulson@18411
   196
fun lookup_type_const c = 
wenzelm@17412
   197
    case Symtab.lookup type_const_trans_table c of
paulson@17230
   198
        SOME c' => c'
paulson@23075
   199
      | NONE => controlled_length (ascii_of c);
paulson@18411
   200
paulson@18411
   201
fun make_fixed_const "op =" = "equal"   (*MUST BE "equal" because it's built-in to ATPs*)
paulson@18411
   202
  | make_fixed_const c      = const_prefix ^ lookup_const c;
paulson@18411
   203
paulson@18411
   204
fun make_fixed_type_const c = tconst_prefix ^ lookup_type_const c;
quigley@17150
   205
wenzelm@17261
   206
fun make_type_class clas = class_prefix ^ ascii_of clas;
quigley@17150
   207
quigley@17150
   208
paulson@18798
   209
(***** definitions and functions for FOL clauses, for conversion to TPTP or DFG format. *****)
paulson@15347
   210
paulson@21509
   211
datatype kind = Axiom | Conjecture;
paulson@15347
   212
fun name_of_kind Axiom = "axiom"
paulson@21509
   213
  | name_of_kind Conjecture = "negated_conjecture";
paulson@15347
   214
paulson@15347
   215
type clause_id = int;
paulson@15347
   216
type axiom_name = string;
paulson@15347
   217
type polarity = bool;
paulson@15347
   218
paulson@15347
   219
(**** Isabelle FOL clauses ****)
paulson@15347
   220
mengj@18402
   221
datatype typ_var = FOLTVar of indexname | FOLTFree of string;
mengj@18402
   222
paulson@18798
   223
(*FIXME: give the constructors more sensible names*)
mengj@18402
   224
datatype fol_type = AtomV of string
mengj@18402
   225
		  | AtomF of string
mengj@18402
   226
		  | Comp of string * fol_type list;
mengj@18402
   227
mengj@18402
   228
fun string_of_fol_type (AtomV x) = x
mengj@18402
   229
  | string_of_fol_type (AtomF x) = x
mengj@18402
   230
  | string_of_fol_type (Comp(tcon,tps)) = 
paulson@18856
   231
      tcon ^ (paren_pack (map string_of_fol_type tps));
paulson@18856
   232
      
paulson@18798
   233
(*First string is the type class; the second is a TVar or TFfree*)
paulson@18798
   234
datatype type_literal = LTVar of string * string | LTFree of string * string;
paulson@15347
   235
paulson@21813
   236
datatype fol_term = UVar of string
paulson@20038
   237
                  | Fun of string * fol_type list * fol_term list;
paulson@20824
   238
datatype predicate = Predicate of string * fol_type list * fol_term list;
paulson@15347
   239
paulson@20018
   240
datatype literal = Literal of polarity * predicate;
paulson@15347
   241
mengj@17999
   242
fun mk_typ_var_sort (TFree(a,s)) = (FOLTFree a,s)
mengj@17999
   243
  | mk_typ_var_sort (TVar(v,s)) = (FOLTVar v,s);
mengj@17999
   244
mengj@17999
   245
paulson@18856
   246
(*A clause has first-order literals and other, type-related literals*)
paulson@15347
   247
datatype clause = 
paulson@15347
   248
	 Clause of {clause_id: clause_id,
paulson@15347
   249
		    axiom_name: axiom_name,
paulson@19447
   250
		    th: thm,
paulson@15347
   251
		    kind: kind,
paulson@15347
   252
		    literals: literal list,
paulson@18869
   253
		    types_sorts: (typ_var * sort) list};
paulson@18869
   254
paulson@18869
   255
fun get_axiomName (Clause cls) = #axiom_name cls;
paulson@15347
   256
paulson@20824
   257
fun get_literals (Clause cls) = #literals cls;
paulson@20824
   258
paulson@17404
   259
exception CLAUSE of string * term;
paulson@15347
   260
paulson@20018
   261
fun isFalse (Literal (pol, Predicate(pname,_,[]))) =
paulson@18856
   262
      (pol andalso pname = "c_False") orelse
paulson@18856
   263
      (not pol andalso pname = "c_True")
paulson@17317
   264
  | isFalse _ = false;
paulson@17317
   265
paulson@20018
   266
fun isTrue (Literal (pol, Predicate(pname,_,[]))) =
paulson@18856
   267
      (pol andalso pname = "c_True") orelse
paulson@18856
   268
      (not pol andalso pname = "c_False")
paulson@17404
   269
  | isTrue _ = false;
paulson@17404
   270
  
paulson@17404
   271
fun isTaut (Clause {literals,...}) = exists isTrue literals;  
paulson@17404
   272
paulson@17317
   273
paulson@18218
   274
(*Declarations of the current theory--to allow suppressing types.*)
paulson@18218
   275
val const_typargs = ref (Library.K [] : (string*typ -> typ list));
paulson@17317
   276
paulson@21790
   277
fun num_typargs(s,T) = length (!const_typargs (s,T));
paulson@16925
   278
paulson@16925
   279
(*Initialize the type suppression mechanism with the current theory before
paulson@16925
   280
    producing any clauses!*)
paulson@18218
   281
fun init thy = (const_typargs := Sign.const_typargs thy);
paulson@16925
   282
    
quigley@17150
   283
mengj@18402
   284
(*Flatten a type to a fol_type while accumulating sort constraints on the TFrees and
paulson@16925
   285
  TVars it contains.*)    
paulson@18218
   286
fun type_of (Type (a, Ts)) = 
paulson@18798
   287
      let val (folTyps, ts) = types_of Ts 
paulson@18218
   288
	  val t = make_fixed_type_const a
paulson@18798
   289
      in (Comp(t,folTyps), ts) end
paulson@18798
   290
  | type_of (TFree (a,s)) = (AtomF(make_fixed_type_var a), [(FOLTFree a, s)]) 
paulson@18798
   291
  | type_of (TVar (v, s)) = (AtomV(make_schematic_type_var v), [(FOLTVar v, s)])
paulson@18218
   292
and types_of Ts =
paulson@18798
   293
      let val (folTyps,ts) = ListPair.unzip (map type_of Ts)
paulson@18798
   294
      in (folTyps, union_all ts) end;
paulson@15390
   295
mengj@18439
   296
paulson@18218
   297
fun const_types_of (c,T) = types_of (!const_typargs (c,T));
paulson@16925
   298
paulson@16903
   299
(* Any variables created via the METAHYPS tactical should be treated as
paulson@16903
   300
   universal vars, although it is represented as "Free(...)" by Isabelle *)
paulson@16903
   301
val isMeta = String.isPrefix "METAHYP1_"
quigley@17150
   302
paulson@18798
   303
fun pred_name_type (Const(c,T)) = (make_fixed_const c, const_types_of (c,T))
paulson@15390
   304
  | pred_name_type (Free(x,T))  = 
paulson@17404
   305
      if isMeta x then raise CLAUSE("Predicate Not First Order 1", Free(x,T)) 
paulson@18798
   306
      else (make_fixed_var x, ([],[]))
paulson@17404
   307
  | pred_name_type (v as Var _) = raise CLAUSE("Predicate Not First Order 2", v)
paulson@17404
   308
  | pred_name_type t        = raise CLAUSE("Predicate input unexpected", t);
paulson@15347
   309
paulson@18798
   310
fun fun_name_type (Const(c,T)) args = (make_fixed_const c, const_types_of (c,T))
paulson@18798
   311
  | fun_name_type (Free(x,T)) args  = 
paulson@18798
   312
       if isMeta x then raise CLAUSE("Function Not First Order", Free(x,T))
paulson@18798
   313
       else (make_fixed_var x, ([],[]))
paulson@17404
   314
  | fun_name_type f args = raise CLAUSE("Function Not First Order 1", f);
quigley@17150
   315
paulson@18856
   316
(*Convert a term to a fol_term while accumulating sort constraints on the TFrees and
paulson@18856
   317
  TVars it contains.*)    
paulson@15347
   318
fun term_of (Var(ind_nm,T)) = 
paulson@21813
   319
      let val (_,ts) = type_of T
paulson@21813
   320
      in (UVar(make_schematic_var ind_nm), ts) end
paulson@15347
   321
  | term_of (Free(x,T)) = 
paulson@21813
   322
      let val (_,ts) = type_of T
paulson@17230
   323
      in
paulson@21813
   324
	  if isMeta x then (UVar(make_schematic_var(x,0)), ts)
paulson@21813
   325
	  else (Fun(make_fixed_var x, [], []), ts)
paulson@17230
   326
      end
paulson@18218
   327
  | term_of app = 
paulson@17230
   328
      let val (f,args) = strip_comb app
paulson@18798
   329
	  val (funName,(contys,ts1)) = fun_name_type f args
paulson@18798
   330
	  val (args',ts2) = terms_of args
paulson@17230
   331
      in
paulson@18868
   332
	  (Fun(funName,contys,args'), union_all (ts1::ts2))
paulson@17230
   333
      end
paulson@18798
   334
and terms_of ts = ListPair.unzip (map term_of ts)
paulson@15390
   335
paulson@18856
   336
(*Create a predicate value, again accumulating sort constraints.*)    
paulson@17404
   337
fun pred_of (Const("op =", typ), args) =
paulson@21813
   338
      let val (args',ts) = terms_of args
paulson@17404
   339
      in
paulson@21813
   340
	  (Predicate(make_fixed_const "op =", [], args'),
paulson@18856
   341
	   union_all ts)
paulson@17404
   342
      end
paulson@17404
   343
  | pred_of (pred,args) = 
paulson@18856
   344
      let val (pname, (predType,ts1)) = pred_name_type pred
paulson@18798
   345
	  val (args',ts2) = terms_of args
paulson@17404
   346
      in
paulson@18856
   347
	  (Predicate(pname,predType,args'), union_all (ts1::ts2))
paulson@17404
   348
      end;
paulson@15347
   349
paulson@20018
   350
(*Treatment of literals, possibly negated*)
paulson@20018
   351
fun predicate_of ((Const("Not",_) $ P), polarity) = predicate_of (P, not polarity)
paulson@20018
   352
  | predicate_of (term,polarity) = (pred_of (strip_comb term), polarity);
paulson@15347
   353
paulson@17888
   354
fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
paulson@18856
   355
  | literals_of_term1 args (Const("op |",_) $ P $ Q) = 
paulson@18856
   356
      literals_of_term1 (literals_of_term1 args P) Q
paulson@18856
   357
  | literals_of_term1 (lits, ts) P =
paulson@20018
   358
      let val ((pred, ts'), polarity) = predicate_of (P,true)
paulson@20018
   359
	  val lits' = Literal(polarity,pred) :: lits
paulson@17234
   360
      in
paulson@18856
   361
	  (lits', ts union ts')
paulson@17234
   362
      end;
quigley@17150
   363
paulson@18856
   364
val literals_of_term = literals_of_term1 ([],[]);
quigley@17150
   365
mengj@18403
   366
mengj@18403
   367
fun list_ord _ ([],[]) = EQUAL
mengj@18403
   368
  | list_ord _ ([],_) = LESS
mengj@18403
   369
  | list_ord _ (_,[]) = GREATER
paulson@19207
   370
  | list_ord ord (x::xs, y::ys) = 
paulson@19207
   371
      (case ord(x,y) of EQUAL => list_ord ord (xs,ys)
paulson@19207
   372
	 	      | xy_ord => xy_ord);
paulson@19207
   373
		     
paulson@22643
   374
(*Make literals for sorted type variables*) 
paulson@22643
   375
fun sorts_on_typs (_, [])   = []
paulson@22643
   376
  | sorts_on_typs (v,  s::ss) = 
paulson@22643
   377
      let val sorts = sorts_on_typs (v, ss)
paulson@22643
   378
      in
paulson@22643
   379
          if s = "HOL.type" then sorts
paulson@22643
   380
          else case v of
paulson@22643
   381
              FOLTVar indx => LTVar(make_type_class s, make_schematic_type_var indx) :: sorts
paulson@22643
   382
            | FOLTFree x => LTFree(make_type_class s, make_fixed_type_var x) :: sorts
paulson@22643
   383
      end;
quigley@17150
   384
paulson@18798
   385
fun pred_of_sort (LTVar (s,ty)) = (s,1)
paulson@22643
   386
  | pred_of_sort (LTFree (s,ty)) = (s,1)
quigley@17150
   387
paulson@16199
   388
(*Given a list of sorted type variables, return two separate lists.
paulson@16199
   389
  The first is for TVars, the second for TFrees.*)
paulson@18856
   390
fun add_typs_aux [] = ([],[])
paulson@22643
   391
  | add_typs_aux ((FOLTVar indx, s) :: tss) = 
paulson@17230
   392
      let val vs = sorts_on_typs (FOLTVar indx, s)
paulson@18856
   393
	  val (vss,fss) = add_typs_aux tss
quigley@17150
   394
      in
paulson@18856
   395
	  (vs union vss, fss)
quigley@17150
   396
      end
paulson@22643
   397
  | add_typs_aux ((FOLTFree x, s) :: tss) =
paulson@17230
   398
      let val fs = sorts_on_typs (FOLTFree x, s)
paulson@18856
   399
	  val (vss,fss) = add_typs_aux tss
quigley@17150
   400
      in
paulson@18856
   401
	  (vss, fs union fss)
quigley@17150
   402
      end;
quigley@17150
   403
mengj@20015
   404
mengj@20015
   405
(** make axiom and conjecture clauses. **)
mengj@20015
   406
mengj@20015
   407
exception MAKE_CLAUSE;
mengj@20015
   408
fun make_clause (clause_id, axiom_name, th, kind) =
paulson@21470
   409
  let val (lits,types_sorts) = literals_of_term (prop_of th)
paulson@20018
   410
  in if forall isFalse lits 
paulson@20018
   411
     then error "Problem too trivial for resolution (empty clause)"
paulson@20018
   412
     else Clause {clause_id = clause_id, axiom_name = axiom_name, th = th, 
paulson@20018
   413
                  kind = kind, literals = lits, types_sorts = types_sorts}
paulson@20018
   414
  end;		     
mengj@20015
   415
mengj@20015
   416
fun get_tvar_strs [] = []
mengj@20015
   417
  | get_tvar_strs ((FOLTVar indx,s)::tss) = 
haftmann@20854
   418
      insert (op =) (make_schematic_type_var indx) (get_tvar_strs tss)
mengj@20015
   419
  | get_tvar_strs((FOLTFree x,s)::tss) = get_tvar_strs tss
mengj@20015
   420
mengj@20015
   421
(* check if a clause is first-order before making a conjecture clause*)
paulson@20418
   422
fun make_conjecture_clause n th =
paulson@22383
   423
  if Meson.is_fol_term (theory_of_thm th) (prop_of th) 
paulson@22383
   424
  then make_clause(n, "conjecture", th, Conjecture)
paulson@20418
   425
  else raise CLAUSE("Goal is not FOL", prop_of th);
paulson@20418
   426
  
mengj@20015
   427
fun make_conjecture_clauses_aux _ [] = []
mengj@20015
   428
  | make_conjecture_clauses_aux n (t::ts) =
mengj@20015
   429
      make_conjecture_clause n t :: make_conjecture_clauses_aux (n+1) ts
mengj@20015
   430
mengj@20015
   431
val make_conjecture_clauses = make_conjecture_clauses_aux 0
mengj@20015
   432
mengj@18199
   433
(*before converting an axiom clause to "clause" format, check if it is FOL*)
paulson@22383
   434
fun make_axiom_clause th (ax_name,cls_id) =
paulson@22383
   435
  if Meson.is_fol_term (theory_of_thm th) (prop_of th) 
paulson@22383
   436
  then (SOME (make_clause(cls_id, ax_name, th, Axiom)) handle MAKE_CLAUSE => NONE)
wenzelm@22130
   437
  else (Output.debug (fn () => ("Omitting " ^ ax_name ^ ": Axiom is not FOL")); NONE)
mengj@20015
   438
    
mengj@19443
   439
fun make_axiom_clauses [] = []
paulson@22383
   440
  | make_axiom_clauses ((th,(name,id))::thms) =
paulson@22383
   441
      case make_axiom_clause th (name,id) of 
paulson@20022
   442
          SOME cls => if isTaut cls then make_axiom_clauses thms 
paulson@20022
   443
                      else (name,cls) :: make_axiom_clauses thms
paulson@20022
   444
        | NONE => make_axiom_clauses thms;
mengj@19354
   445
paulson@15347
   446
(**** Isabelle arities ****)
paulson@15347
   447
paulson@15347
   448
exception ARCLAUSE of string;
paulson@15347
   449
 
paulson@15347
   450
type class = string; 
paulson@15347
   451
type tcons = string; 
paulson@15347
   452
paulson@22643
   453
datatype arLit = TConsLit of class * tcons * string list
paulson@22643
   454
               | TVarLit of class * string;
paulson@15347
   455
 
paulson@15347
   456
datatype arityClause =  
paulson@21373
   457
	 ArityClause of {axiom_name: axiom_name,
paulson@15347
   458
			 kind: kind,
paulson@15347
   459
			 conclLit: arLit,
paulson@15347
   460
			 premLits: arLit list};
paulson@15347
   461
paulson@15347
   462
paulson@18798
   463
fun gen_TVars 0 = []
paulson@18798
   464
  | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
paulson@15347
   465
paulson@18411
   466
fun pack_sort(_,[])  = []
paulson@18411
   467
  | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
paulson@22643
   468
  | pack_sort(tvar, cls::srt) =  (cls, tvar) :: pack_sort(tvar, srt);
paulson@15347
   469
    
paulson@18411
   470
(*Arity of type constructor tcon :: (arg1,...,argN)res*)
paulson@22643
   471
fun make_axiom_arity_clause (tcons, axiom_name, (cls,args)) =
paulson@21560
   472
   let val tvars = gen_TVars (length args)
paulson@17845
   473
       val tvars_srts = ListPair.zip (tvars,args)
paulson@17845
   474
   in
paulson@21373
   475
      ArityClause {axiom_name = axiom_name, kind = Axiom, 
paulson@22643
   476
                   conclLit = TConsLit (cls, make_fixed_type_const tcons, tvars), 
paulson@22643
   477
                   premLits = map TVarLit (union_all(map pack_sort tvars_srts))}
paulson@17845
   478
   end;
paulson@15347
   479
paulson@15347
   480
paulson@15347
   481
(**** Isabelle class relations ****)
paulson@15347
   482
paulson@15347
   483
datatype classrelClause = 
paulson@18868
   484
	 ClassrelClause of {axiom_name: axiom_name,
paulson@15347
   485
			    subclass: class,
paulson@18411
   486
			    superclass: class};
paulson@21290
   487
 
paulson@21290
   488
(*Generate all pairs (sub,super) such that sub is a proper subclass of super in theory thy.*)
paulson@21432
   489
fun class_pairs thy [] supers = []
paulson@21432
   490
  | class_pairs thy subs supers =
paulson@21432
   491
      let val class_less = Sorts.class_less(Sign.classes_of thy)
paulson@21432
   492
	  fun add_super sub (super,pairs) = 
paulson@21432
   493
		if class_less (sub,super) then (sub,super)::pairs else pairs
paulson@21432
   494
	  fun add_supers (sub,pairs) = foldl (add_super sub) pairs supers
paulson@21432
   495
      in  foldl add_supers [] subs  end;
paulson@15347
   496
paulson@21290
   497
fun make_classrelClause (sub,super) =
paulson@21290
   498
  ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of sub ^ "_" ^ ascii_of super,
paulson@21290
   499
                  subclass = make_type_class sub, 
paulson@21290
   500
                  superclass = make_type_class super};
paulson@15347
   501
paulson@21290
   502
fun make_classrel_clauses thy subs supers =
paulson@21290
   503
  map make_classrelClause (class_pairs thy subs supers);
paulson@18868
   504
paulson@18868
   505
paulson@18868
   506
(** Isabelle arities **)
paulson@17845
   507
paulson@21373
   508
fun arity_clause _ _ (tcons, []) = []
paulson@21373
   509
  | arity_clause seen n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
paulson@21373
   510
      arity_clause seen n (tcons,ars)
paulson@21373
   511
  | arity_clause seen n (tcons, (ar as (class,_)) :: ars) =
paulson@21373
   512
      if class mem_string seen then (*multiple arities for the same tycon, class pair*)
paulson@21373
   513
	  make_axiom_arity_clause (tcons, lookup_type_const tcons ^ "_" ^ class ^ "_" ^ Int.toString n, ar) :: 
paulson@21373
   514
	  arity_clause seen (n+1) (tcons,ars)
paulson@21373
   515
      else
paulson@21373
   516
	  make_axiom_arity_clause (tcons, lookup_type_const tcons ^ "_" ^ class, ar) :: 
paulson@21373
   517
	  arity_clause (class::seen) n (tcons,ars)
paulson@17845
   518
paulson@17845
   519
fun multi_arity_clause [] = []
paulson@19155
   520
  | multi_arity_clause ((tcons,ars) :: tc_arlists) =
paulson@22643
   521
      arity_clause [] 1 (tcons, ars)  @  multi_arity_clause tc_arlists 
paulson@17845
   522
paulson@22643
   523
(*Generate all pairs (tycon,class,sorts) such that tycon belongs to class in theory thy
paulson@22643
   524
  provided its arguments have the corresponding sorts.*)
paulson@21373
   525
fun type_class_pairs thy tycons classes =
paulson@21373
   526
  let val alg = Sign.classes_of thy
paulson@21373
   527
      fun domain_sorts (tycon,class) = Sorts.mg_domain alg tycon [class]
paulson@21373
   528
      fun add_class tycon (class,pairs) = 
paulson@21373
   529
            (class, domain_sorts(tycon,class))::pairs 
paulson@21373
   530
            handle Sorts.CLASS_ERROR _ => pairs
paulson@21373
   531
      fun try_classes tycon = (tycon, foldl (add_class tycon) [] classes)
paulson@21373
   532
  in  map try_classes tycons  end;
paulson@21373
   533
paulson@22643
   534
(*Proving one (tycon, class) membership may require proving others, so iterate.*)
paulson@22643
   535
fun iter_type_class_pairs thy tycons [] = ([], [])
paulson@22643
   536
  | iter_type_class_pairs thy tycons classes =
paulson@22643
   537
      let val cpairs = type_class_pairs thy tycons classes
paulson@22643
   538
          val newclasses = union_all (union_all (union_all (map (map #2 o #2) cpairs))) \\ classes \\ HOLogic.typeS
paulson@22643
   539
          val _ = if null newclasses then () 
paulson@22643
   540
                  else Output.debug (fn _ => "New classes: " ^ space_implode ", " newclasses)
paulson@22643
   541
          val (classes', cpairs') = iter_type_class_pairs thy tycons newclasses  
paulson@22643
   542
      in  (classes' union classes, cpairs' union cpairs)  end;
paulson@22643
   543
      
paulson@22643
   544
fun make_arity_clauses thy tycons classes =
paulson@22643
   545
  let val (classes', cpairs) = iter_type_class_pairs thy tycons classes  
paulson@22643
   546
  in  (classes', multi_arity_clause cpairs)  end;
paulson@17845
   547
paulson@17845
   548
paulson@18868
   549
(**** Find occurrences of predicates in clauses ****)
paulson@18868
   550
paulson@18868
   551
(*FIXME: multiple-arity checking doesn't work, as update_new is the wrong 
paulson@18868
   552
  function (it flags repeated declarations of a function, even with the same arity)*)
paulson@18868
   553
paulson@18868
   554
fun update_many (tab, keypairs) = foldl (uncurry Symtab.update) tab keypairs;
paulson@18868
   555
paulson@18868
   556
fun add_predicate_preds (Predicate(pname,tys,tms), preds) = 
paulson@18868
   557
  if pname = "equal" then preds (*equality is built-in and requires no declaration*)
paulson@18868
   558
  else Symtab.update (pname, length tys + length tms) preds
paulson@18868
   559
paulson@20018
   560
fun add_literal_preds (Literal(_,pred), preds) = add_predicate_preds (pred,preds)
paulson@18868
   561
paulson@18868
   562
fun add_type_sort_preds ((FOLTVar indx,s), preds) = 
paulson@18868
   563
      update_many (preds, map pred_of_sort (sorts_on_typs (FOLTVar indx, s)))
paulson@18868
   564
  | add_type_sort_preds ((FOLTFree x,s), preds) =
paulson@18868
   565
      update_many (preds, map pred_of_sort (sorts_on_typs (FOLTFree x, s)));
paulson@17845
   566
paulson@18868
   567
fun add_clause_preds (Clause {literals, types_sorts, ...}, preds) =
paulson@18868
   568
  foldl add_literal_preds (foldl add_type_sort_preds preds types_sorts) literals
paulson@18868
   569
  handle Symtab.DUP a => raise ERROR ("predicate " ^ a ^ " has multiple arities")
paulson@18868
   570
paulson@18868
   571
fun add_classrelClause_preds (ClassrelClause {subclass,superclass,...}, preds) =
paulson@18868
   572
  Symtab.update (subclass,1) (Symtab.update (superclass,1) preds);
paulson@17845
   573
paulson@22643
   574
fun class_of_arityLit (TConsLit (tclass, _, _)) = tclass
paulson@22643
   575
  | class_of_arityLit (TVarLit (tclass, _)) = tclass;
paulson@21373
   576
paulson@21373
   577
fun add_arityClause_preds (ArityClause {conclLit,premLits,...}, preds) =
paulson@22643
   578
  let val classes = map (make_type_class o class_of_arityLit) (conclLit::premLits)
paulson@21373
   579
      fun upd (class,preds) = Symtab.update (class,1) preds
paulson@21373
   580
  in  foldl upd preds classes  end;
paulson@18868
   581
paulson@18868
   582
fun preds_of_clauses clauses clsrel_clauses arity_clauses = 
paulson@18868
   583
  Symtab.dest
paulson@18868
   584
    (foldl add_classrelClause_preds 
paulson@18868
   585
      (foldl add_arityClause_preds
paulson@18868
   586
        (foldl add_clause_preds Symtab.empty clauses)
paulson@18868
   587
        arity_clauses)
paulson@18868
   588
      clsrel_clauses)
paulson@18798
   589
paulson@18868
   590
(*** Find occurrences of functions in clauses ***)
paulson@18868
   591
paulson@18868
   592
fun add_foltype_funcs (AtomV _, funcs) = funcs
paulson@18868
   593
  | add_foltype_funcs (AtomF a, funcs) = Symtab.update (a,0) funcs
paulson@18868
   594
  | add_foltype_funcs (Comp(a,tys), funcs) = 
paulson@18868
   595
      foldl add_foltype_funcs (Symtab.update (a, length tys) funcs) tys;
paulson@18868
   596
paulson@21813
   597
fun add_folterm_funcs (UVar _, funcs) = funcs
paulson@18868
   598
  | add_folterm_funcs (Fun(a,tys,tms), funcs) = 
paulson@18868
   599
      foldl add_foltype_funcs 
paulson@18868
   600
	    (foldl add_folterm_funcs (Symtab.update (a, length tys + length tms) funcs) 
paulson@18868
   601
	           tms) 
paulson@18868
   602
	    tys
paulson@18798
   603
paulson@18868
   604
fun add_predicate_funcs (Predicate(_,tys,tms), funcs) = 
paulson@18868
   605
    foldl add_foltype_funcs (foldl add_folterm_funcs funcs tms) tys;
paulson@18868
   606
paulson@20018
   607
fun add_literal_funcs (Literal(_,pred), funcs) = add_predicate_funcs (pred,funcs)
paulson@18868
   608
paulson@20038
   609
(*TFrees are recorded as constants*)
paulson@20038
   610
fun add_type_sort_funcs ((FOLTVar _, _), funcs) = funcs
paulson@20038
   611
  | add_type_sort_funcs ((FOLTFree a, _), funcs) = 
paulson@20038
   612
      Symtab.update (make_fixed_type_var a, 0) funcs
paulson@20038
   613
paulson@18868
   614
fun add_arityClause_funcs (ArityClause {conclLit,...}, funcs) =
paulson@22643
   615
  let val TConsLit (_, tcons, tvars) = conclLit
paulson@18868
   616
  in  Symtab.update (tcons, length tvars) funcs  end;
paulson@17845
   617
paulson@20038
   618
fun add_clause_funcs (Clause {literals, types_sorts, ...}, funcs) =
paulson@20038
   619
  foldl add_literal_funcs (foldl add_type_sort_funcs funcs types_sorts)
paulson@20038
   620
       literals
paulson@18868
   621
  handle Symtab.DUP a => raise ERROR ("function " ^ a ^ " has multiple arities")
paulson@18868
   622
paulson@23075
   623
(*This type can be overlooked because it is built-in...*)
paulson@23075
   624
val init_functab = Symtab.update ("tc_itself", 1) Symtab.empty;
paulson@23075
   625
paulson@18868
   626
fun funcs_of_clauses clauses arity_clauses = 
paulson@18868
   627
  Symtab.dest (foldl add_arityClause_funcs 
paulson@23075
   628
                     (foldl add_clause_funcs init_functab clauses)
paulson@18868
   629
                     arity_clauses)
paulson@18868
   630
paulson@18868
   631
paulson@18868
   632
(**** String-oriented operations ****)
paulson@15347
   633
paulson@21813
   634
fun string_of_term (UVar x) = x
paulson@18218
   635
  | string_of_term (Fun (name,typs,terms)) = 
paulson@21790
   636
      name ^ (paren_pack (map string_of_term terms @ map string_of_fol_type typs));
paulson@21509
   637
paulson@21509
   638
fun string_of_pair [t1,t2] = (string_of_term t1, string_of_term t2)
paulson@21509
   639
  | string_of_pair _ = raise ERROR ("equality predicate requires two arguments");
paulson@21509
   640
paulson@21509
   641
fun string_of_equality ts =
paulson@21509
   642
  let val (s1,s2) = string_of_pair ts
paulson@21509
   643
  in "equal(" ^ s1 ^ "," ^ s2 ^ ")" end;
paulson@15347
   644
paulson@15347
   645
(* before output the string of the predicate, check if the predicate corresponds to an equality or not. *)
paulson@21509
   646
fun string_of_predicate (Predicate("equal",_,ts)) = string_of_equality ts
paulson@21509
   647
  | string_of_predicate (Predicate(name,typs,ts)) = 
paulson@21790
   648
      name ^ (paren_pack (map string_of_term ts @ map string_of_fol_type typs));
paulson@17317
   649
paulson@17317
   650
fun string_of_clausename (cls_id,ax_name) = 
paulson@17525
   651
    clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id;
paulson@17317
   652
mengj@21564
   653
fun clause_name_of (cls_id,ax_name) = 
mengj@21564
   654
    ascii_of ax_name ^ "_" ^ Int.toString cls_id;
mengj@21564
   655
paulson@17317
   656
fun string_of_type_clsname (cls_id,ax_name,idx) = 
paulson@17525
   657
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
paulson@18863
   658
paulson@18863
   659
(*Write a list of strings to a file*)
paulson@18863
   660
fun writeln_strs os = List.app (fn s => TextIO.output (os,s));
paulson@18863
   661
quigley@17150
   662
    
paulson@18868
   663
(**** Producing DFG files ****)
quigley@17150
   664
paulson@18863
   665
(*Attach sign in DFG syntax: false means negate.*)
paulson@18863
   666
fun dfg_sign true s = s
paulson@18863
   667
  | dfg_sign false s = "not(" ^ s ^ ")"  
paulson@18863
   668
paulson@20018
   669
fun dfg_literal (Literal(pol,pred)) = dfg_sign pol (string_of_predicate pred)
quigley@17150
   670
paulson@18798
   671
fun dfg_of_typeLit (LTVar (s,ty)) = "not(" ^ s ^ "(" ^ ty ^ "))"
paulson@18856
   672
  | dfg_of_typeLit (LTFree (s,ty)) = s ^ "(" ^ ty ^ ")";
quigley@17150
   673
 
paulson@18868
   674
(*Enclose the clause body by quantifiers, if necessary*)
paulson@18868
   675
fun dfg_forall [] body = body  
paulson@18868
   676
  | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
quigley@17150
   677
paulson@18868
   678
fun gen_dfg_cls (cls_id, ax_name, knd, lits, vars) = 
paulson@18868
   679
    "clause( %(" ^ knd ^ ")\n" ^ 
paulson@18868
   680
    dfg_forall vars ("or(" ^ lits ^ ")") ^ ",\n" ^ 
paulson@18863
   681
    string_of_clausename (cls_id,ax_name) ^  ").\n\n";
quigley@17150
   682
paulson@18869
   683
fun dfg_clause_aux (Clause{literals, types_sorts, ...}) = 
paulson@21790
   684
  let val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@21790
   685
  in  (map dfg_of_typeLit tvar_lits @ map dfg_literal literals, 
paulson@21790
   686
       map dfg_of_typeLit tfree_lits)
paulson@17230
   687
  end; 
quigley@17150
   688
paulson@20018
   689
fun dfg_folterms (Literal(pol,pred)) = 
paulson@18856
   690
  let val Predicate (_, _, folterms) = pred
paulson@18218
   691
  in  folterms  end
quigley@17150
   692
paulson@21813
   693
fun get_uvars (UVar a) = [a] 
paulson@18868
   694
  | get_uvars (Fun (_,typ,tlist)) = union_all(map get_uvars tlist)
paulson@17404
   695
paulson@18868
   696
fun dfg_vars (Clause {literals,...}) =
paulson@18920
   697
  union_all (map get_uvars (List.concat (map dfg_folterms literals)))
quigley@17150
   698
paulson@18798
   699
fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,types_sorts,...}) =
paulson@17230
   700
    let val (lits,tfree_lits) = dfg_clause_aux cls 
paulson@18798
   701
            (*"lits" includes the typing assumptions (TVars)*)
quigley@17150
   702
        val vars = dfg_vars cls
paulson@18798
   703
        val tvars = get_tvar_strs types_sorts
paulson@20038
   704
	val cls_str = gen_dfg_cls(clause_id, axiom_name, name_of_kind kind, 
paulson@20038
   705
	                           commas lits, tvars@vars) 
paulson@18798
   706
    in (cls_str, tfree_lits) end;
quigley@17150
   707
paulson@18798
   708
fun string_of_arity (name, num) =  "(" ^ name ^ "," ^ Int.toString num ^ ")"
quigley@17150
   709
paulson@18856
   710
fun string_of_preds [] = ""
paulson@18856
   711
  | string_of_preds preds = "predicates[" ^ commas(map string_of_arity preds) ^ "].\n";
quigley@17150
   712
paulson@18856
   713
fun string_of_funcs [] = ""
paulson@18856
   714
  | string_of_funcs funcs = "functions[" ^ commas(map string_of_arity funcs) ^ "].\n" ;
quigley@17150
   715
paulson@17234
   716
fun string_of_symbols predstr funcstr = 
paulson@17234
   717
  "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
quigley@17150
   718
paulson@18798
   719
fun string_of_start name = "begin_problem(" ^ name ^ ").\n\n";
quigley@17150
   720
paulson@18863
   721
fun string_of_descrip name = 
paulson@18868
   722
  "list_of_descriptions.\nname({*" ^ name ^ 
paulson@18868
   723
  "*}).\nauthor({*Isabelle*}).\nstatus(unknown).\ndescription({*auto-generated*}).\nend_of_list.\n\n"
quigley@17150
   724
paulson@18863
   725
fun dfg_tfree_clause tfree_lit =
paulson@21509
   726
  "clause( %(negated_conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ").\n\n"
paulson@18863
   727
paulson@22643
   728
fun dfg_of_arLit (TConsLit (c,t,args)) =
paulson@22643
   729
      dfg_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@22643
   730
  | dfg_of_arLit (TVarLit (c,str)) =
paulson@22643
   731
      dfg_sign false (make_type_class c ^ "(" ^ str ^ ")")
paulson@17525
   732
    
paulson@20038
   733
fun dfg_classrelLits sub sup =  "not(" ^ sub ^ "(T)), " ^ sup ^ "(T)";
paulson@17525
   734
paulson@18868
   735
fun dfg_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
paulson@18868
   736
  "clause(forall([T],\nor( " ^ dfg_classrelLits subclass superclass ^ ")),\n" ^
paulson@18868
   737
  axiom_name ^ ").\n\n";
paulson@18868
   738
      
paulson@21560
   739
fun string_of_ar axiom_name = arclause_prefix ^ ascii_of axiom_name;
paulson@21560
   740
paulson@21560
   741
fun dfg_arity_clause (ArityClause{axiom_name,kind,conclLit,premLits,...}) = 
paulson@22643
   742
  let val TConsLit (_,_,tvars) = conclLit
paulson@18868
   743
      val lits = map dfg_of_arLit (conclLit :: premLits)
paulson@18863
   744
  in
paulson@21560
   745
      "clause( %(" ^ name_of_kind kind ^ ")\n" ^ 
paulson@18868
   746
      dfg_forall tvars ("or( " ^ commas lits ^ ")") ^ ",\n" ^
paulson@21560
   747
      string_of_ar axiom_name ^ ").\n\n"
paulson@18863
   748
  end;
paulson@18863
   749
paulson@18863
   750
(* write out a subgoal in DFG format to the file "xxxx_N"*)
paulson@20038
   751
fun dfg_write_file thms filename (ax_tuples,classrel_clauses,arity_clauses) = 
paulson@18863
   752
  let 
wenzelm@22130
   753
    val _ = Output.debug (fn () => ("Preparing to write the DFG file " ^ filename))
paulson@23075
   754
    val _ = dfg_format := true
mengj@19719
   755
    val conjectures = make_conjecture_clauses thms
paulson@20038
   756
    val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses ax_tuples)
paulson@18868
   757
    val (dfg_clss, tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
paulson@20038
   758
    val clss = conjectures @ axclauses
paulson@18868
   759
    val funcs = funcs_of_clauses clss arity_clauses
paulson@18868
   760
    and preds = preds_of_clauses clss classrel_clauses arity_clauses
wenzelm@21858
   761
    and probname = Path.implode (Path.base (Path.explode filename))
paulson@20038
   762
    val (axstrs, _) = ListPair.unzip (map clause2dfg axclauses)
paulson@18863
   763
    val tfree_clss = map dfg_tfree_clause (union_all tfree_litss) 
paulson@19155
   764
    val out = TextIO.openOut filename
paulson@18863
   765
  in
paulson@18868
   766
    TextIO.output (out, string_of_start probname); 
paulson@18868
   767
    TextIO.output (out, string_of_descrip probname); 
paulson@18868
   768
    TextIO.output (out, string_of_symbols (string_of_funcs funcs) (string_of_preds preds)); 
paulson@18868
   769
    TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
paulson@18868
   770
    writeln_strs out axstrs;
paulson@18868
   771
    List.app (curry TextIO.output out o dfg_classrelClause) classrel_clauses;
paulson@18868
   772
    List.app (curry TextIO.output out o dfg_arity_clause) arity_clauses;
paulson@18868
   773
    TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
paulson@18868
   774
    writeln_strs out tfree_clss;
paulson@18868
   775
    writeln_strs out dfg_clss;
paulson@18868
   776
    TextIO.output (out, "end_of_list.\n\nend_problem.\n");
paulson@20022
   777
    TextIO.closeOut out;
paulson@20022
   778
    clnames
paulson@17525
   779
  end;
quigley@17150
   780
quigley@17150
   781
paulson@18869
   782
(**** Produce TPTP files ****)
paulson@18868
   783
paulson@18868
   784
(*Attach sign in TPTP syntax: false means negate.*)
paulson@21509
   785
fun tptp_sign true s = s
paulson@21509
   786
  | tptp_sign false s = "~ " ^ s
paulson@18868
   787
paulson@21509
   788
fun tptp_of_equality pol ts =
paulson@21509
   789
  let val (s1,s2) = string_of_pair ts
paulson@21509
   790
      val eqop = if pol then " = " else " != "
paulson@21509
   791
  in  s1 ^ eqop ^ s2  end;
paulson@15347
   792
paulson@21509
   793
fun tptp_literal (Literal(pol,Predicate("equal",_,ts))) = tptp_of_equality pol ts
paulson@21509
   794
  | tptp_literal (Literal(pol,pred)) = tptp_sign pol (string_of_predicate pred);
paulson@21509
   795
paulson@21509
   796
fun tptp_of_typeLit (LTVar (s,ty))  = tptp_sign false (s ^ "(" ^ ty ^ ")")
paulson@21509
   797
  | tptp_of_typeLit (LTFree (s,ty)) = tptp_sign true  (s ^ "(" ^ ty ^ ")");
paulson@15347
   798
 
paulson@15347
   799
fun gen_tptp_cls (cls_id,ax_name,knd,lits) = 
paulson@21509
   800
    "cnf(" ^ string_of_clausename (cls_id,ax_name) ^ "," ^ 
paulson@21509
   801
    name_of_kind knd ^ "," ^ tptp_pack lits ^ ").\n";
paulson@15347
   802
paulson@18869
   803
fun tptp_type_lits (Clause {literals, types_sorts, ...}) = 
paulson@21790
   804
  let val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@21790
   805
  in
paulson@21790
   806
      (map tptp_of_typeLit tvar_lits @ map tptp_literal literals,
paulson@21790
   807
       map tptp_of_typeLit tfree_lits)
paulson@21790
   808
  end; 
paulson@15347
   809
paulson@18869
   810
fun clause2tptp (cls as Clause {clause_id, axiom_name, kind, ...}) =
paulson@17422
   811
    let val (lits,tfree_lits) = tptp_type_lits cls 
paulson@17305
   812
            (*"lits" includes the typing assumptions (TVars)*)
paulson@21509
   813
	val cls_str = gen_tptp_cls(clause_id, axiom_name, kind, lits) 
paulson@15608
   814
    in
paulson@15608
   815
	(cls_str,tfree_lits) 
paulson@15608
   816
    end;
paulson@15608
   817
paulson@18863
   818
fun tptp_tfree_clause tfree_lit =
paulson@21509
   819
    "cnf(" ^ "tfree_tcs," ^ "negated_conjecture" ^ "," ^ tptp_pack[tfree_lit] ^ ").\n";
paulson@21509
   820
    
paulson@22643
   821
fun tptp_of_arLit (TConsLit (c,t,args)) =
paulson@22643
   822
      tptp_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@22643
   823
  | tptp_of_arLit (TVarLit (c,str)) =
paulson@22643
   824
      tptp_sign false (make_type_class c ^ "(" ^ str ^ ")")
paulson@15347
   825
    
paulson@21560
   826
fun tptp_arity_clause (ArityClause{axiom_name,kind,conclLit,premLits,...}) = 
paulson@21560
   827
  "cnf(" ^ string_of_ar axiom_name ^ "," ^ name_of_kind kind ^ "," ^ 
paulson@21560
   828
  tptp_pack (map tptp_of_arLit (conclLit :: premLits)) ^ ").\n";
paulson@15347
   829
paulson@15347
   830
fun tptp_classrelLits sub sup = 
paulson@21509
   831
  let val tvar = "(T)"
paulson@21509
   832
  in  tptp_pack [tptp_sign false (sub^tvar), tptp_sign true (sup^tvar)]  end;
paulson@15347
   833
paulson@18868
   834
fun tptp_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
paulson@21509
   835
  "cnf(" ^ axiom_name ^ ",axiom," ^ tptp_classrelLits subclass superclass ^ ").\n" 
quigley@17150
   836
paulson@18863
   837
(* write out a subgoal as tptp clauses to the file "xxxx_N"*)
paulson@20038
   838
fun tptp_write_file thms filename (ax_tuples,classrel_clauses,arity_clauses) =
paulson@18863
   839
  let
wenzelm@22130
   840
    val _ = Output.debug (fn () => ("Preparing to write the TPTP file " ^ filename))
paulson@23075
   841
    val _ = dfg_format := false
mengj@19443
   842
    val clss = make_conjecture_clauses thms
paulson@20038
   843
    val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses ax_tuples)
paulson@18863
   844
    val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp clss)
paulson@18863
   845
    val tfree_clss = map tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
paulson@18863
   846
    val out = TextIO.openOut filename
paulson@18863
   847
  in
paulson@20038
   848
    List.app (curry TextIO.output out o #1 o clause2tptp) axclauses;
paulson@18863
   849
    writeln_strs out tfree_clss;
paulson@18863
   850
    writeln_strs out tptp_clss;
paulson@18868
   851
    List.app (curry TextIO.output out o tptp_classrelClause) classrel_clauses;
paulson@18868
   852
    List.app (curry TextIO.output out o tptp_arity_clause) arity_clauses;
paulson@20022
   853
    TextIO.closeOut out;
paulson@20022
   854
    clnames
paulson@18863
   855
  end;
paulson@18863
   856
paulson@15347
   857
end;