src/HOL/Tools/res_clause.ML
author haftmann
Thu May 17 19:49:40 2007 +0200 (2007-05-17)
changeset 22997 d4f3b015b50b
parent 22643 bc3bb8e9594a
child 23029 79ee75dc1e59
permissions -rw-r--r--
canonical prefixing of class constants
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"),
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@22643
   365
(*Make literals for sorted type variables*) 
paulson@22643
   366
fun sorts_on_typs (_, [])   = []
paulson@22643
   367
  | sorts_on_typs (v,  s::ss) = 
paulson@22643
   368
      let val sorts = sorts_on_typs (v, ss)
paulson@22643
   369
      in
paulson@22643
   370
          if s = "HOL.type" then sorts
paulson@22643
   371
          else case v of
paulson@22643
   372
              FOLTVar indx => LTVar(make_type_class s, make_schematic_type_var indx) :: sorts
paulson@22643
   373
            | FOLTFree x => LTFree(make_type_class s, make_fixed_type_var x) :: sorts
paulson@22643
   374
      end;
quigley@17150
   375
paulson@18798
   376
fun pred_of_sort (LTVar (s,ty)) = (s,1)
paulson@22643
   377
  | pred_of_sort (LTFree (s,ty)) = (s,1)
quigley@17150
   378
paulson@16199
   379
(*Given a list of sorted type variables, return two separate lists.
paulson@16199
   380
  The first is for TVars, the second for TFrees.*)
paulson@18856
   381
fun add_typs_aux [] = ([],[])
paulson@22643
   382
  | add_typs_aux ((FOLTVar indx, s) :: tss) = 
paulson@17230
   383
      let val vs = sorts_on_typs (FOLTVar indx, s)
paulson@18856
   384
	  val (vss,fss) = add_typs_aux tss
quigley@17150
   385
      in
paulson@18856
   386
	  (vs union vss, fss)
quigley@17150
   387
      end
paulson@22643
   388
  | add_typs_aux ((FOLTFree x, s) :: tss) =
paulson@17230
   389
      let val fs = sorts_on_typs (FOLTFree x, s)
paulson@18856
   390
	  val (vss,fss) = add_typs_aux tss
quigley@17150
   391
      in
paulson@18856
   392
	  (vss, fs union fss)
quigley@17150
   393
      end;
quigley@17150
   394
mengj@20015
   395
mengj@20015
   396
(** make axiom and conjecture clauses. **)
mengj@20015
   397
mengj@20015
   398
exception MAKE_CLAUSE;
mengj@20015
   399
fun make_clause (clause_id, axiom_name, th, kind) =
paulson@21470
   400
  let val (lits,types_sorts) = literals_of_term (prop_of th)
paulson@20018
   401
  in if forall isFalse lits 
paulson@20018
   402
     then error "Problem too trivial for resolution (empty clause)"
paulson@20018
   403
     else Clause {clause_id = clause_id, axiom_name = axiom_name, th = th, 
paulson@20018
   404
                  kind = kind, literals = lits, types_sorts = types_sorts}
paulson@20018
   405
  end;		     
mengj@20015
   406
mengj@20015
   407
fun get_tvar_strs [] = []
mengj@20015
   408
  | get_tvar_strs ((FOLTVar indx,s)::tss) = 
haftmann@20854
   409
      insert (op =) (make_schematic_type_var indx) (get_tvar_strs tss)
mengj@20015
   410
  | get_tvar_strs((FOLTFree x,s)::tss) = get_tvar_strs tss
mengj@20015
   411
mengj@20015
   412
(* check if a clause is first-order before making a conjecture clause*)
paulson@20418
   413
fun make_conjecture_clause n th =
paulson@22383
   414
  if Meson.is_fol_term (theory_of_thm th) (prop_of th) 
paulson@22383
   415
  then make_clause(n, "conjecture", th, Conjecture)
paulson@20418
   416
  else raise CLAUSE("Goal is not FOL", prop_of th);
paulson@20418
   417
  
mengj@20015
   418
fun make_conjecture_clauses_aux _ [] = []
mengj@20015
   419
  | make_conjecture_clauses_aux n (t::ts) =
mengj@20015
   420
      make_conjecture_clause n t :: make_conjecture_clauses_aux (n+1) ts
mengj@20015
   421
mengj@20015
   422
val make_conjecture_clauses = make_conjecture_clauses_aux 0
mengj@20015
   423
mengj@18199
   424
(*before converting an axiom clause to "clause" format, check if it is FOL*)
paulson@22383
   425
fun make_axiom_clause th (ax_name,cls_id) =
paulson@22383
   426
  if Meson.is_fol_term (theory_of_thm th) (prop_of th) 
paulson@22383
   427
  then (SOME (make_clause(cls_id, ax_name, th, Axiom)) handle MAKE_CLAUSE => NONE)
wenzelm@22130
   428
  else (Output.debug (fn () => ("Omitting " ^ ax_name ^ ": Axiom is not FOL")); NONE)
mengj@20015
   429
    
mengj@19443
   430
fun make_axiom_clauses [] = []
paulson@22383
   431
  | make_axiom_clauses ((th,(name,id))::thms) =
paulson@22383
   432
      case make_axiom_clause th (name,id) of 
paulson@20022
   433
          SOME cls => if isTaut cls then make_axiom_clauses thms 
paulson@20022
   434
                      else (name,cls) :: make_axiom_clauses thms
paulson@20022
   435
        | NONE => make_axiom_clauses thms;
mengj@19354
   436
paulson@15347
   437
(**** Isabelle arities ****)
paulson@15347
   438
paulson@15347
   439
exception ARCLAUSE of string;
paulson@15347
   440
 
paulson@15347
   441
type class = string; 
paulson@15347
   442
type tcons = string; 
paulson@15347
   443
paulson@22643
   444
datatype arLit = TConsLit of class * tcons * string list
paulson@22643
   445
               | TVarLit of class * string;
paulson@15347
   446
 
paulson@15347
   447
datatype arityClause =  
paulson@21373
   448
	 ArityClause of {axiom_name: axiom_name,
paulson@15347
   449
			 kind: kind,
paulson@15347
   450
			 conclLit: arLit,
paulson@15347
   451
			 premLits: arLit list};
paulson@15347
   452
paulson@15347
   453
paulson@18798
   454
fun gen_TVars 0 = []
paulson@18798
   455
  | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
paulson@15347
   456
paulson@18411
   457
fun pack_sort(_,[])  = []
paulson@18411
   458
  | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
paulson@22643
   459
  | pack_sort(tvar, cls::srt) =  (cls, tvar) :: pack_sort(tvar, srt);
paulson@15347
   460
    
paulson@18411
   461
(*Arity of type constructor tcon :: (arg1,...,argN)res*)
paulson@22643
   462
fun make_axiom_arity_clause (tcons, axiom_name, (cls,args)) =
paulson@21560
   463
   let val tvars = gen_TVars (length args)
paulson@17845
   464
       val tvars_srts = ListPair.zip (tvars,args)
paulson@17845
   465
   in
paulson@21373
   466
      ArityClause {axiom_name = axiom_name, kind = Axiom, 
paulson@22643
   467
                   conclLit = TConsLit (cls, make_fixed_type_const tcons, tvars), 
paulson@22643
   468
                   premLits = map TVarLit (union_all(map pack_sort tvars_srts))}
paulson@17845
   469
   end;
paulson@15347
   470
paulson@15347
   471
paulson@15347
   472
(**** Isabelle class relations ****)
paulson@15347
   473
paulson@15347
   474
datatype classrelClause = 
paulson@18868
   475
	 ClassrelClause of {axiom_name: axiom_name,
paulson@15347
   476
			    subclass: class,
paulson@18411
   477
			    superclass: class};
paulson@21290
   478
 
paulson@21290
   479
(*Generate all pairs (sub,super) such that sub is a proper subclass of super in theory thy.*)
paulson@21432
   480
fun class_pairs thy [] supers = []
paulson@21432
   481
  | class_pairs thy subs supers =
paulson@21432
   482
      let val class_less = Sorts.class_less(Sign.classes_of thy)
paulson@21432
   483
	  fun add_super sub (super,pairs) = 
paulson@21432
   484
		if class_less (sub,super) then (sub,super)::pairs else pairs
paulson@21432
   485
	  fun add_supers (sub,pairs) = foldl (add_super sub) pairs supers
paulson@21432
   486
      in  foldl add_supers [] subs  end;
paulson@15347
   487
paulson@21290
   488
fun make_classrelClause (sub,super) =
paulson@21290
   489
  ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of sub ^ "_" ^ ascii_of super,
paulson@21290
   490
                  subclass = make_type_class sub, 
paulson@21290
   491
                  superclass = make_type_class super};
paulson@15347
   492
paulson@21290
   493
fun make_classrel_clauses thy subs supers =
paulson@21290
   494
  map make_classrelClause (class_pairs thy subs supers);
paulson@18868
   495
paulson@18868
   496
paulson@18868
   497
(** Isabelle arities **)
paulson@17845
   498
paulson@21373
   499
fun arity_clause _ _ (tcons, []) = []
paulson@21373
   500
  | arity_clause seen n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
paulson@21373
   501
      arity_clause seen n (tcons,ars)
paulson@21373
   502
  | arity_clause seen n (tcons, (ar as (class,_)) :: ars) =
paulson@21373
   503
      if class mem_string seen then (*multiple arities for the same tycon, class pair*)
paulson@21373
   504
	  make_axiom_arity_clause (tcons, lookup_type_const tcons ^ "_" ^ class ^ "_" ^ Int.toString n, ar) :: 
paulson@21373
   505
	  arity_clause seen (n+1) (tcons,ars)
paulson@21373
   506
      else
paulson@21373
   507
	  make_axiom_arity_clause (tcons, lookup_type_const tcons ^ "_" ^ class, ar) :: 
paulson@21373
   508
	  arity_clause (class::seen) n (tcons,ars)
paulson@17845
   509
paulson@17845
   510
fun multi_arity_clause [] = []
paulson@19155
   511
  | multi_arity_clause ((tcons,ars) :: tc_arlists) =
paulson@22643
   512
      arity_clause [] 1 (tcons, ars)  @  multi_arity_clause tc_arlists 
paulson@17845
   513
paulson@22643
   514
(*Generate all pairs (tycon,class,sorts) such that tycon belongs to class in theory thy
paulson@22643
   515
  provided its arguments have the corresponding sorts.*)
paulson@21373
   516
fun type_class_pairs thy tycons classes =
paulson@21373
   517
  let val alg = Sign.classes_of thy
paulson@21373
   518
      fun domain_sorts (tycon,class) = Sorts.mg_domain alg tycon [class]
paulson@21373
   519
      fun add_class tycon (class,pairs) = 
paulson@21373
   520
            (class, domain_sorts(tycon,class))::pairs 
paulson@21373
   521
            handle Sorts.CLASS_ERROR _ => pairs
paulson@21373
   522
      fun try_classes tycon = (tycon, foldl (add_class tycon) [] classes)
paulson@21373
   523
  in  map try_classes tycons  end;
paulson@21373
   524
paulson@22643
   525
(*Proving one (tycon, class) membership may require proving others, so iterate.*)
paulson@22643
   526
fun iter_type_class_pairs thy tycons [] = ([], [])
paulson@22643
   527
  | iter_type_class_pairs thy tycons classes =
paulson@22643
   528
      let val cpairs = type_class_pairs thy tycons classes
paulson@22643
   529
          val newclasses = union_all (union_all (union_all (map (map #2 o #2) cpairs))) \\ classes \\ HOLogic.typeS
paulson@22643
   530
          val _ = if null newclasses then () 
paulson@22643
   531
                  else Output.debug (fn _ => "New classes: " ^ space_implode ", " newclasses)
paulson@22643
   532
          val (classes', cpairs') = iter_type_class_pairs thy tycons newclasses  
paulson@22643
   533
      in  (classes' union classes, cpairs' union cpairs)  end;
paulson@22643
   534
      
paulson@22643
   535
fun make_arity_clauses thy tycons classes =
paulson@22643
   536
  let val (classes', cpairs) = iter_type_class_pairs thy tycons classes  
paulson@22643
   537
  in  (classes', multi_arity_clause cpairs)  end;
paulson@17845
   538
paulson@17845
   539
paulson@18868
   540
(**** Find occurrences of predicates in clauses ****)
paulson@18868
   541
paulson@18868
   542
(*FIXME: multiple-arity checking doesn't work, as update_new is the wrong 
paulson@18868
   543
  function (it flags repeated declarations of a function, even with the same arity)*)
paulson@18868
   544
paulson@18868
   545
fun update_many (tab, keypairs) = foldl (uncurry Symtab.update) tab keypairs;
paulson@18868
   546
paulson@18868
   547
fun add_predicate_preds (Predicate(pname,tys,tms), preds) = 
paulson@18868
   548
  if pname = "equal" then preds (*equality is built-in and requires no declaration*)
paulson@18868
   549
  else Symtab.update (pname, length tys + length tms) preds
paulson@18868
   550
paulson@20018
   551
fun add_literal_preds (Literal(_,pred), preds) = add_predicate_preds (pred,preds)
paulson@18868
   552
paulson@18868
   553
fun add_type_sort_preds ((FOLTVar indx,s), preds) = 
paulson@18868
   554
      update_many (preds, map pred_of_sort (sorts_on_typs (FOLTVar indx, s)))
paulson@18868
   555
  | add_type_sort_preds ((FOLTFree x,s), preds) =
paulson@18868
   556
      update_many (preds, map pred_of_sort (sorts_on_typs (FOLTFree x, s)));
paulson@17845
   557
paulson@18868
   558
fun add_clause_preds (Clause {literals, types_sorts, ...}, preds) =
paulson@18868
   559
  foldl add_literal_preds (foldl add_type_sort_preds preds types_sorts) literals
paulson@18868
   560
  handle Symtab.DUP a => raise ERROR ("predicate " ^ a ^ " has multiple arities")
paulson@18868
   561
paulson@18868
   562
fun add_classrelClause_preds (ClassrelClause {subclass,superclass,...}, preds) =
paulson@18868
   563
  Symtab.update (subclass,1) (Symtab.update (superclass,1) preds);
paulson@17845
   564
paulson@22643
   565
fun class_of_arityLit (TConsLit (tclass, _, _)) = tclass
paulson@22643
   566
  | class_of_arityLit (TVarLit (tclass, _)) = tclass;
paulson@21373
   567
paulson@21373
   568
fun add_arityClause_preds (ArityClause {conclLit,premLits,...}, preds) =
paulson@22643
   569
  let val classes = map (make_type_class o class_of_arityLit) (conclLit::premLits)
paulson@21373
   570
      fun upd (class,preds) = Symtab.update (class,1) preds
paulson@21373
   571
  in  foldl upd preds classes  end;
paulson@18868
   572
paulson@18868
   573
fun preds_of_clauses clauses clsrel_clauses arity_clauses = 
paulson@18868
   574
  Symtab.dest
paulson@18868
   575
    (foldl add_classrelClause_preds 
paulson@18868
   576
      (foldl add_arityClause_preds
paulson@18868
   577
        (foldl add_clause_preds Symtab.empty clauses)
paulson@18868
   578
        arity_clauses)
paulson@18868
   579
      clsrel_clauses)
paulson@18798
   580
paulson@18868
   581
(*** Find occurrences of functions in clauses ***)
paulson@18868
   582
paulson@18868
   583
fun add_foltype_funcs (AtomV _, funcs) = funcs
paulson@18868
   584
  | add_foltype_funcs (AtomF a, funcs) = Symtab.update (a,0) funcs
paulson@18868
   585
  | add_foltype_funcs (Comp(a,tys), funcs) = 
paulson@18868
   586
      foldl add_foltype_funcs (Symtab.update (a, length tys) funcs) tys;
paulson@18868
   587
paulson@21813
   588
fun add_folterm_funcs (UVar _, funcs) = funcs
paulson@18868
   589
  | add_folterm_funcs (Fun(a,tys,tms), funcs) = 
paulson@18868
   590
      foldl add_foltype_funcs 
paulson@18868
   591
	    (foldl add_folterm_funcs (Symtab.update (a, length tys + length tms) funcs) 
paulson@18868
   592
	           tms) 
paulson@18868
   593
	    tys
paulson@18798
   594
paulson@18868
   595
fun add_predicate_funcs (Predicate(_,tys,tms), funcs) = 
paulson@18868
   596
    foldl add_foltype_funcs (foldl add_folterm_funcs funcs tms) tys;
paulson@18868
   597
paulson@20018
   598
fun add_literal_funcs (Literal(_,pred), funcs) = add_predicate_funcs (pred,funcs)
paulson@18868
   599
paulson@20038
   600
(*TFrees are recorded as constants*)
paulson@20038
   601
fun add_type_sort_funcs ((FOLTVar _, _), funcs) = funcs
paulson@20038
   602
  | add_type_sort_funcs ((FOLTFree a, _), funcs) = 
paulson@20038
   603
      Symtab.update (make_fixed_type_var a, 0) funcs
paulson@20038
   604
paulson@18868
   605
fun add_arityClause_funcs (ArityClause {conclLit,...}, funcs) =
paulson@22643
   606
  let val TConsLit (_, tcons, tvars) = conclLit
paulson@18868
   607
  in  Symtab.update (tcons, length tvars) funcs  end;
paulson@17845
   608
paulson@20038
   609
fun add_clause_funcs (Clause {literals, types_sorts, ...}, funcs) =
paulson@20038
   610
  foldl add_literal_funcs (foldl add_type_sort_funcs funcs types_sorts)
paulson@20038
   611
       literals
paulson@18868
   612
  handle Symtab.DUP a => raise ERROR ("function " ^ a ^ " has multiple arities")
paulson@18868
   613
paulson@18868
   614
fun funcs_of_clauses clauses arity_clauses = 
paulson@18868
   615
  Symtab.dest (foldl add_arityClause_funcs 
paulson@18868
   616
                     (foldl add_clause_funcs Symtab.empty clauses)
paulson@18868
   617
                     arity_clauses)
paulson@18868
   618
paulson@18868
   619
paulson@18868
   620
(**** String-oriented operations ****)
paulson@15347
   621
paulson@21813
   622
fun string_of_term (UVar x) = x
paulson@18218
   623
  | string_of_term (Fun (name,typs,terms)) = 
paulson@21790
   624
      name ^ (paren_pack (map string_of_term terms @ map string_of_fol_type typs));
paulson@21509
   625
paulson@21509
   626
fun string_of_pair [t1,t2] = (string_of_term t1, string_of_term t2)
paulson@21509
   627
  | string_of_pair _ = raise ERROR ("equality predicate requires two arguments");
paulson@21509
   628
paulson@21509
   629
fun string_of_equality ts =
paulson@21509
   630
  let val (s1,s2) = string_of_pair ts
paulson@21509
   631
  in "equal(" ^ s1 ^ "," ^ s2 ^ ")" end;
paulson@15347
   632
paulson@15347
   633
(* before output the string of the predicate, check if the predicate corresponds to an equality or not. *)
paulson@21509
   634
fun string_of_predicate (Predicate("equal",_,ts)) = string_of_equality ts
paulson@21509
   635
  | string_of_predicate (Predicate(name,typs,ts)) = 
paulson@21790
   636
      name ^ (paren_pack (map string_of_term ts @ map string_of_fol_type typs));
paulson@17317
   637
paulson@17317
   638
fun string_of_clausename (cls_id,ax_name) = 
paulson@17525
   639
    clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id;
paulson@17317
   640
mengj@21564
   641
fun clause_name_of (cls_id,ax_name) = 
mengj@21564
   642
    ascii_of ax_name ^ "_" ^ Int.toString cls_id;
mengj@21564
   643
paulson@17317
   644
fun string_of_type_clsname (cls_id,ax_name,idx) = 
paulson@17525
   645
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
paulson@18863
   646
paulson@18863
   647
(*Write a list of strings to a file*)
paulson@18863
   648
fun writeln_strs os = List.app (fn s => TextIO.output (os,s));
paulson@18863
   649
quigley@17150
   650
    
paulson@18868
   651
(**** Producing DFG files ****)
quigley@17150
   652
paulson@18863
   653
(*Attach sign in DFG syntax: false means negate.*)
paulson@18863
   654
fun dfg_sign true s = s
paulson@18863
   655
  | dfg_sign false s = "not(" ^ s ^ ")"  
paulson@18863
   656
paulson@20018
   657
fun dfg_literal (Literal(pol,pred)) = dfg_sign pol (string_of_predicate pred)
quigley@17150
   658
paulson@18798
   659
fun dfg_of_typeLit (LTVar (s,ty)) = "not(" ^ s ^ "(" ^ ty ^ "))"
paulson@18856
   660
  | dfg_of_typeLit (LTFree (s,ty)) = s ^ "(" ^ ty ^ ")";
quigley@17150
   661
 
paulson@18868
   662
(*Enclose the clause body by quantifiers, if necessary*)
paulson@18868
   663
fun dfg_forall [] body = body  
paulson@18868
   664
  | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
quigley@17150
   665
paulson@18868
   666
fun gen_dfg_cls (cls_id, ax_name, knd, lits, vars) = 
paulson@18868
   667
    "clause( %(" ^ knd ^ ")\n" ^ 
paulson@18868
   668
    dfg_forall vars ("or(" ^ lits ^ ")") ^ ",\n" ^ 
paulson@18863
   669
    string_of_clausename (cls_id,ax_name) ^  ").\n\n";
quigley@17150
   670
paulson@18869
   671
fun dfg_clause_aux (Clause{literals, types_sorts, ...}) = 
paulson@21790
   672
  let val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@21790
   673
  in  (map dfg_of_typeLit tvar_lits @ map dfg_literal literals, 
paulson@21790
   674
       map dfg_of_typeLit tfree_lits)
paulson@17230
   675
  end; 
quigley@17150
   676
paulson@20018
   677
fun dfg_folterms (Literal(pol,pred)) = 
paulson@18856
   678
  let val Predicate (_, _, folterms) = pred
paulson@18218
   679
  in  folterms  end
quigley@17150
   680
paulson@21813
   681
fun get_uvars (UVar a) = [a] 
paulson@18868
   682
  | get_uvars (Fun (_,typ,tlist)) = union_all(map get_uvars tlist)
paulson@17404
   683
paulson@18868
   684
fun dfg_vars (Clause {literals,...}) =
paulson@18920
   685
  union_all (map get_uvars (List.concat (map dfg_folterms literals)))
quigley@17150
   686
paulson@18798
   687
fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,types_sorts,...}) =
paulson@17230
   688
    let val (lits,tfree_lits) = dfg_clause_aux cls 
paulson@18798
   689
            (*"lits" includes the typing assumptions (TVars)*)
quigley@17150
   690
        val vars = dfg_vars cls
paulson@18798
   691
        val tvars = get_tvar_strs types_sorts
paulson@20038
   692
	val cls_str = gen_dfg_cls(clause_id, axiom_name, name_of_kind kind, 
paulson@20038
   693
	                           commas lits, tvars@vars) 
paulson@18798
   694
    in (cls_str, tfree_lits) end;
quigley@17150
   695
paulson@18798
   696
fun string_of_arity (name, num) =  "(" ^ name ^ "," ^ Int.toString num ^ ")"
quigley@17150
   697
paulson@18856
   698
fun string_of_preds [] = ""
paulson@18856
   699
  | string_of_preds preds = "predicates[" ^ commas(map string_of_arity preds) ^ "].\n";
quigley@17150
   700
paulson@18856
   701
fun string_of_funcs [] = ""
paulson@18856
   702
  | string_of_funcs funcs = "functions[" ^ commas(map string_of_arity funcs) ^ "].\n" ;
quigley@17150
   703
paulson@17234
   704
fun string_of_symbols predstr funcstr = 
paulson@17234
   705
  "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
quigley@17150
   706
paulson@18798
   707
fun string_of_start name = "begin_problem(" ^ name ^ ").\n\n";
quigley@17150
   708
paulson@18863
   709
fun string_of_descrip name = 
paulson@18868
   710
  "list_of_descriptions.\nname({*" ^ name ^ 
paulson@18868
   711
  "*}).\nauthor({*Isabelle*}).\nstatus(unknown).\ndescription({*auto-generated*}).\nend_of_list.\n\n"
quigley@17150
   712
paulson@18863
   713
fun dfg_tfree_clause tfree_lit =
paulson@21509
   714
  "clause( %(negated_conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ").\n\n"
paulson@18863
   715
paulson@22643
   716
fun dfg_of_arLit (TConsLit (c,t,args)) =
paulson@22643
   717
      dfg_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@22643
   718
  | dfg_of_arLit (TVarLit (c,str)) =
paulson@22643
   719
      dfg_sign false (make_type_class c ^ "(" ^ str ^ ")")
paulson@17525
   720
    
paulson@20038
   721
fun dfg_classrelLits sub sup =  "not(" ^ sub ^ "(T)), " ^ sup ^ "(T)";
paulson@17525
   722
paulson@18868
   723
fun dfg_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
paulson@18868
   724
  "clause(forall([T],\nor( " ^ dfg_classrelLits subclass superclass ^ ")),\n" ^
paulson@18868
   725
  axiom_name ^ ").\n\n";
paulson@18868
   726
      
paulson@21560
   727
fun string_of_ar axiom_name = arclause_prefix ^ ascii_of axiom_name;
paulson@21560
   728
paulson@21560
   729
fun dfg_arity_clause (ArityClause{axiom_name,kind,conclLit,premLits,...}) = 
paulson@22643
   730
  let val TConsLit (_,_,tvars) = conclLit
paulson@18868
   731
      val lits = map dfg_of_arLit (conclLit :: premLits)
paulson@18863
   732
  in
paulson@21560
   733
      "clause( %(" ^ name_of_kind kind ^ ")\n" ^ 
paulson@18868
   734
      dfg_forall tvars ("or( " ^ commas lits ^ ")") ^ ",\n" ^
paulson@21560
   735
      string_of_ar axiom_name ^ ").\n\n"
paulson@18863
   736
  end;
paulson@18863
   737
paulson@18863
   738
(* write out a subgoal in DFG format to the file "xxxx_N"*)
paulson@20038
   739
fun dfg_write_file thms filename (ax_tuples,classrel_clauses,arity_clauses) = 
paulson@18863
   740
  let 
wenzelm@22130
   741
    val _ = Output.debug (fn () => ("Preparing to write the DFG file " ^ filename))
mengj@19719
   742
    val conjectures = make_conjecture_clauses thms
paulson@20038
   743
    val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses ax_tuples)
paulson@18868
   744
    val (dfg_clss, tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
paulson@20038
   745
    val clss = conjectures @ axclauses
paulson@18868
   746
    val funcs = funcs_of_clauses clss arity_clauses
paulson@18868
   747
    and preds = preds_of_clauses clss classrel_clauses arity_clauses
wenzelm@21858
   748
    and probname = Path.implode (Path.base (Path.explode filename))
paulson@20038
   749
    val (axstrs, _) = ListPair.unzip (map clause2dfg axclauses)
paulson@18863
   750
    val tfree_clss = map dfg_tfree_clause (union_all tfree_litss) 
paulson@19155
   751
    val out = TextIO.openOut filename
paulson@18863
   752
  in
paulson@18868
   753
    TextIO.output (out, string_of_start probname); 
paulson@18868
   754
    TextIO.output (out, string_of_descrip probname); 
paulson@18868
   755
    TextIO.output (out, string_of_symbols (string_of_funcs funcs) (string_of_preds preds)); 
paulson@18868
   756
    TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
paulson@18868
   757
    writeln_strs out axstrs;
paulson@18868
   758
    List.app (curry TextIO.output out o dfg_classrelClause) classrel_clauses;
paulson@18868
   759
    List.app (curry TextIO.output out o dfg_arity_clause) arity_clauses;
paulson@18868
   760
    TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
paulson@18868
   761
    writeln_strs out tfree_clss;
paulson@18868
   762
    writeln_strs out dfg_clss;
paulson@18868
   763
    TextIO.output (out, "end_of_list.\n\nend_problem.\n");
paulson@20022
   764
    TextIO.closeOut out;
paulson@20022
   765
    clnames
paulson@17525
   766
  end;
quigley@17150
   767
quigley@17150
   768
paulson@18869
   769
(**** Produce TPTP files ****)
paulson@18868
   770
paulson@18868
   771
(*Attach sign in TPTP syntax: false means negate.*)
paulson@21509
   772
fun tptp_sign true s = s
paulson@21509
   773
  | tptp_sign false s = "~ " ^ s
paulson@18868
   774
paulson@21509
   775
fun tptp_of_equality pol ts =
paulson@21509
   776
  let val (s1,s2) = string_of_pair ts
paulson@21509
   777
      val eqop = if pol then " = " else " != "
paulson@21509
   778
  in  s1 ^ eqop ^ s2  end;
paulson@15347
   779
paulson@21509
   780
fun tptp_literal (Literal(pol,Predicate("equal",_,ts))) = tptp_of_equality pol ts
paulson@21509
   781
  | tptp_literal (Literal(pol,pred)) = tptp_sign pol (string_of_predicate pred);
paulson@21509
   782
paulson@21509
   783
fun tptp_of_typeLit (LTVar (s,ty))  = tptp_sign false (s ^ "(" ^ ty ^ ")")
paulson@21509
   784
  | tptp_of_typeLit (LTFree (s,ty)) = tptp_sign true  (s ^ "(" ^ ty ^ ")");
paulson@15347
   785
 
paulson@15347
   786
fun gen_tptp_cls (cls_id,ax_name,knd,lits) = 
paulson@21509
   787
    "cnf(" ^ string_of_clausename (cls_id,ax_name) ^ "," ^ 
paulson@21509
   788
    name_of_kind knd ^ "," ^ tptp_pack lits ^ ").\n";
paulson@15347
   789
paulson@18869
   790
fun tptp_type_lits (Clause {literals, types_sorts, ...}) = 
paulson@21790
   791
  let val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@21790
   792
  in
paulson@21790
   793
      (map tptp_of_typeLit tvar_lits @ map tptp_literal literals,
paulson@21790
   794
       map tptp_of_typeLit tfree_lits)
paulson@21790
   795
  end; 
paulson@15347
   796
paulson@18869
   797
fun clause2tptp (cls as Clause {clause_id, axiom_name, kind, ...}) =
paulson@17422
   798
    let val (lits,tfree_lits) = tptp_type_lits cls 
paulson@17305
   799
            (*"lits" includes the typing assumptions (TVars)*)
paulson@21509
   800
	val cls_str = gen_tptp_cls(clause_id, axiom_name, kind, lits) 
paulson@15608
   801
    in
paulson@15608
   802
	(cls_str,tfree_lits) 
paulson@15608
   803
    end;
paulson@15608
   804
paulson@18863
   805
fun tptp_tfree_clause tfree_lit =
paulson@21509
   806
    "cnf(" ^ "tfree_tcs," ^ "negated_conjecture" ^ "," ^ tptp_pack[tfree_lit] ^ ").\n";
paulson@21509
   807
    
paulson@22643
   808
fun tptp_of_arLit (TConsLit (c,t,args)) =
paulson@22643
   809
      tptp_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@22643
   810
  | tptp_of_arLit (TVarLit (c,str)) =
paulson@22643
   811
      tptp_sign false (make_type_class c ^ "(" ^ str ^ ")")
paulson@15347
   812
    
paulson@21560
   813
fun tptp_arity_clause (ArityClause{axiom_name,kind,conclLit,premLits,...}) = 
paulson@21560
   814
  "cnf(" ^ string_of_ar axiom_name ^ "," ^ name_of_kind kind ^ "," ^ 
paulson@21560
   815
  tptp_pack (map tptp_of_arLit (conclLit :: premLits)) ^ ").\n";
paulson@15347
   816
paulson@15347
   817
fun tptp_classrelLits sub sup = 
paulson@21509
   818
  let val tvar = "(T)"
paulson@21509
   819
  in  tptp_pack [tptp_sign false (sub^tvar), tptp_sign true (sup^tvar)]  end;
paulson@15347
   820
paulson@18868
   821
fun tptp_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
paulson@21509
   822
  "cnf(" ^ axiom_name ^ ",axiom," ^ tptp_classrelLits subclass superclass ^ ").\n" 
quigley@17150
   823
paulson@18863
   824
(* write out a subgoal as tptp clauses to the file "xxxx_N"*)
paulson@20038
   825
fun tptp_write_file thms filename (ax_tuples,classrel_clauses,arity_clauses) =
paulson@18863
   826
  let
wenzelm@22130
   827
    val _ = Output.debug (fn () => ("Preparing to write the TPTP file " ^ filename))
mengj@19443
   828
    val clss = make_conjecture_clauses thms
paulson@20038
   829
    val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses ax_tuples)
paulson@18863
   830
    val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp clss)
paulson@18863
   831
    val tfree_clss = map tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
paulson@18863
   832
    val out = TextIO.openOut filename
paulson@18863
   833
  in
paulson@20038
   834
    List.app (curry TextIO.output out o #1 o clause2tptp) axclauses;
paulson@18863
   835
    writeln_strs out tfree_clss;
paulson@18863
   836
    writeln_strs out tptp_clss;
paulson@18868
   837
    List.app (curry TextIO.output out o tptp_classrelClause) classrel_clauses;
paulson@18868
   838
    List.app (curry TextIO.output out o tptp_arity_clause) arity_clauses;
paulson@20022
   839
    TextIO.closeOut out;
paulson@20022
   840
    clnames
paulson@18863
   841
  end;
paulson@18863
   842
paulson@15347
   843
end;