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