src/HOL/Tools/res_clause.ML
author paulson
Thu Jul 06 12:18:17 2006 +0200 (2006-07-06)
changeset 20022 b07a138b4e7d
parent 20018 5419a71d0baa
child 20038 73231d03a2ac
permissions -rw-r--r--
some tidying; fixed the output of theorem names
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@15347
     9
signature RES_CLAUSE =
paulson@15347
    10
  sig
paulson@18868
    11
  exception CLAUSE of string * term
paulson@18868
    12
  type clause and arityClause and classrelClause
paulson@18868
    13
  type fol_type
paulson@18868
    14
  type typ_var
paulson@18868
    15
  type type_literal
paulson@18868
    16
  val add_typs_aux : (typ_var * string list) list -> type_literal list * type_literal list
paulson@18868
    17
  val arity_clause_thy: theory -> arityClause list 
paulson@18868
    18
  val ascii_of : string -> string
paulson@18868
    19
  val bracket_pack : string list -> string
paulson@18868
    20
  val classrel_clauses_thy: theory -> classrelClause list 
paulson@18868
    21
  val clause_prefix : string 
paulson@18868
    22
  val clause2tptp : clause -> string * string list
paulson@18868
    23
  val const_prefix : string
paulson@20022
    24
  val dfg_write_file:  thm list -> string -> 
paulson@20022
    25
       ((thm * (string * int)) list * classrelClause list * arityClause list) -> string list
paulson@18868
    26
  val fixed_var_prefix : string
paulson@18868
    27
  val gen_tptp_cls : int * string * string * string -> string
paulson@18868
    28
  val gen_tptp_type_cls : int * string * string * string * int -> string
paulson@18868
    29
  val get_axiomName : clause ->  string
paulson@18868
    30
  val init : theory -> unit
paulson@18868
    31
  val isMeta : string -> bool
paulson@18868
    32
  val isTaut : clause -> bool
paulson@17404
    33
  val keep_types : bool ref
paulson@18868
    34
  val list_ord : ('a * 'b -> order) -> 'a list * 'b list -> order
mengj@19443
    35
  val make_axiom_clause : thm -> string * int -> clause option
mengj@19443
    36
  val make_conjecture_clauses : thm list -> clause list
paulson@18868
    37
  val make_fixed_const : string -> string		
paulson@18868
    38
  val make_fixed_type_const : string -> string   
paulson@18868
    39
  val make_fixed_type_var : string -> string
paulson@18868
    40
  val make_fixed_var : string -> string
paulson@18868
    41
  val make_schematic_type_var : string * int -> string
paulson@18868
    42
  val make_schematic_var : string * int -> string
paulson@18868
    43
  val make_type_class : string -> string
paulson@18868
    44
  val mk_fol_type: string * string * fol_type list -> fol_type
paulson@18868
    45
  val mk_typ_var_sort : Term.typ -> typ_var * sort
paulson@18868
    46
  val paren_pack : string list -> string
paulson@18868
    47
  val schematic_var_prefix : string
paulson@18868
    48
  val special_equal : bool ref
paulson@18868
    49
  val string_of_fol_type : fol_type -> string
paulson@18868
    50
  val tconst_prefix : string 
paulson@18868
    51
  val tfree_prefix : string
paulson@17404
    52
  val tptp_arity_clause : arityClause -> string
paulson@17404
    53
  val tptp_classrelClause : classrelClause -> string
paulson@18868
    54
  val tptp_of_typeLit : type_literal -> string
paulson@18863
    55
  val tptp_tfree_clause : string -> string
paulson@20022
    56
  val tptp_write_file: thm list -> string -> 
paulson@20022
    57
       ((thm * (string * int)) list * classrelClause list * arityClause list) -> string list
paulson@17404
    58
  val tvar_prefix : string
mengj@17908
    59
  val union_all : ''a list list -> ''a list
paulson@18863
    60
  val writeln_strs: TextIO.outstream -> TextIO.vector list -> unit
mengj@19719
    61
  val dfg_sign: bool -> string -> string
mengj@19719
    62
  val dfg_of_typeLit: type_literal -> string
mengj@19719
    63
  val get_tvar_strs: (typ_var * sort) list -> string list
mengj@19719
    64
  val gen_dfg_cls: int * string * string * string * string list -> string
mengj@19719
    65
  val add_foltype_funcs: fol_type * int Symtab.table -> int Symtab.table
mengj@19719
    66
  val add_arityClause_funcs: arityClause * int Symtab.table -> int Symtab.table
mengj@19719
    67
  val add_arityClause_preds: arityClause * int Symtab.table -> int Symtab.table
mengj@19719
    68
  val add_classrelClause_preds : classrelClause * int Symtab.table -> int Symtab.table
mengj@19719
    69
  val dfg_tfree_clause : string -> string
mengj@19719
    70
  val string_of_start: string -> string
mengj@19719
    71
  val string_of_descrip : string -> string
mengj@19719
    72
  val string_of_symbols: string -> string -> string
mengj@19719
    73
  val string_of_funcs: (string * int) list -> string
mengj@19719
    74
  val string_of_preds: (string * Int.int) list -> string
mengj@19719
    75
  val dfg_classrelClause: classrelClause -> string
mengj@19719
    76
  val dfg_arity_clause: arityClause -> string
mengj@19719
    77
end;
paulson@15347
    78
paulson@18420
    79
structure ResClause : RES_CLAUSE =
paulson@15347
    80
struct
paulson@15347
    81
paulson@15347
    82
(* Added for typed equality *)
paulson@15347
    83
val special_equal = ref false; (* by default,equality does not carry type information *)
paulson@15347
    84
val eq_typ_wrapper = "typeinfo"; (* default string *)
paulson@15347
    85
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"),
paulson@18676
   113
		   ("Divides.op 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"),
paulson@18390
   120
		   ("List.op @", "append")];
paulson@15347
   121
paulson@17230
   122
val type_const_trans_table =
paulson@18411
   123
      Symtab.make [("*", "prod"),
paulson@18411
   124
	  	   ("+", "sum"),
paulson@18411
   125
		   ("~=>", "map")];
paulson@15347
   126
paulson@15610
   127
(*Escaping of special characters.
paulson@15610
   128
  Alphanumeric characters are left unchanged.
paulson@15610
   129
  The character _ goes to __
paulson@15610
   130
  Characters in the range ASCII space to / go to _A to _P, respectively.
paulson@15610
   131
  Other printing characters go to _NNN where NNN is the decimal ASCII code.*)
paulson@15610
   132
local
paulson@15610
   133
paulson@15610
   134
val A_minus_space = Char.ord #"A" - Char.ord #" ";
paulson@15610
   135
paulson@15347
   136
fun ascii_of_c c =
paulson@15610
   137
  if Char.isAlphaNum c then String.str c
paulson@15610
   138
  else if c = #"_" then "__"
paulson@15610
   139
  else if #" " <= c andalso c <= #"/" 
paulson@15610
   140
       then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
paulson@15610
   141
  else if Char.isPrint c then ("_" ^ Int.toString (Char.ord c))
paulson@15610
   142
  else ""
paulson@15347
   143
paulson@15610
   144
in
paulson@15610
   145
paulson@15610
   146
val ascii_of = String.translate ascii_of_c;
paulson@15610
   147
paulson@15610
   148
end;
paulson@15347
   149
paulson@17525
   150
(* convert a list of strings into one single string; surrounded by brackets *)
paulson@18218
   151
fun paren_pack [] = ""   (*empty argument list*)
paulson@18218
   152
  | paren_pack strings = "(" ^ commas strings ^ ")";
paulson@17525
   153
paulson@17525
   154
fun bracket_pack strings = "[" ^ commas strings ^ "]";
paulson@17525
   155
paulson@17525
   156
paulson@16925
   157
(*Remove the initial ' character from a type variable, if it is present*)
paulson@16925
   158
fun trim_type_var s =
paulson@16925
   159
  if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
paulson@16925
   160
  else error ("trim_type: Malformed type variable encountered: " ^ s);
paulson@16925
   161
paulson@16903
   162
fun ascii_of_indexname (v,0) = ascii_of v
paulson@17525
   163
  | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
paulson@15347
   164
paulson@17230
   165
fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
paulson@15347
   166
fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
paulson@15347
   167
paulson@16925
   168
fun make_schematic_type_var (x,i) = 
paulson@16925
   169
      tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
paulson@16925
   170
fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
paulson@15347
   171
paulson@18411
   172
fun lookup_const c =
wenzelm@17412
   173
    case Symtab.lookup const_trans_table c of
paulson@17230
   174
        SOME c' => c'
paulson@18411
   175
      | NONE => ascii_of c;
paulson@17230
   176
paulson@18411
   177
fun lookup_type_const c = 
wenzelm@17412
   178
    case Symtab.lookup type_const_trans_table c of
paulson@17230
   179
        SOME c' => c'
paulson@18411
   180
      | NONE => ascii_of c;
paulson@18411
   181
paulson@18411
   182
fun make_fixed_const "op =" = "equal"   (*MUST BE "equal" because it's built-in to ATPs*)
paulson@18411
   183
  | make_fixed_const c      = const_prefix ^ lookup_const c;
paulson@18411
   184
paulson@18411
   185
fun make_fixed_type_const c = tconst_prefix ^ lookup_type_const c;
quigley@17150
   186
wenzelm@17261
   187
fun make_type_class clas = class_prefix ^ ascii_of clas;
quigley@17150
   188
quigley@17150
   189
paulson@18798
   190
(***** definitions and functions for FOL clauses, for conversion to TPTP or DFG format. *****)
paulson@15347
   191
paulson@17230
   192
val keep_types = ref true;
paulson@15347
   193
paulson@15347
   194
datatype kind = Axiom | Hypothesis | Conjecture;
paulson@15347
   195
fun name_of_kind Axiom = "axiom"
paulson@15347
   196
  | name_of_kind Hypothesis = "hypothesis"
paulson@15347
   197
  | name_of_kind Conjecture = "conjecture";
paulson@15347
   198
paulson@15347
   199
type clause_id = int;
paulson@15347
   200
type axiom_name = string;
paulson@15347
   201
type polarity = bool;
paulson@15347
   202
paulson@15347
   203
(**** Isabelle FOL clauses ****)
paulson@15347
   204
paulson@15347
   205
type pred_name = string;
mengj@18402
   206
mengj@18402
   207
datatype typ_var = FOLTVar of indexname | FOLTFree of string;
mengj@18402
   208
paulson@18798
   209
(*FIXME: give the constructors more sensible names*)
mengj@18402
   210
datatype fol_type = AtomV of string
mengj@18402
   211
		  | AtomF of string
mengj@18402
   212
		  | Comp of string * fol_type list;
mengj@18402
   213
mengj@18402
   214
fun string_of_fol_type (AtomV x) = x
mengj@18402
   215
  | string_of_fol_type (AtomF x) = x
mengj@18402
   216
  | string_of_fol_type (Comp(tcon,tps)) = 
paulson@18856
   217
      tcon ^ (paren_pack (map string_of_fol_type tps));
paulson@18856
   218
      
mengj@18439
   219
fun mk_fol_type ("Var",x,_) = AtomV(x)
mengj@18439
   220
  | mk_fol_type ("Fixed",x,_) = AtomF(x)
mengj@18439
   221
  | mk_fol_type ("Comp",con,args) = Comp(con,args)
mengj@18439
   222
mengj@18439
   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@18856
   227
datatype fol_term = UVar of string * fol_type
paulson@18856
   228
                 | Fun of string * fol_type list * fol_term list;
paulson@18856
   229
datatype predicate = Predicate of pred_name * 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@17404
   248
exception CLAUSE of string * term;
paulson@15347
   249
paulson@20018
   250
fun isFalse (Literal (pol, Predicate(pname,_,[]))) =
paulson@18856
   251
      (pol andalso pname = "c_False") orelse
paulson@18856
   252
      (not pol andalso pname = "c_True")
paulson@17317
   253
  | isFalse _ = false;
paulson@17317
   254
paulson@20018
   255
fun isTrue (Literal (pol, Predicate(pname,_,[]))) =
paulson@18856
   256
      (pol andalso pname = "c_True") orelse
paulson@18856
   257
      (not pol andalso pname = "c_False")
paulson@17404
   258
  | isTrue _ = false;
paulson@17404
   259
  
paulson@17404
   260
fun isTaut (Clause {literals,...}) = exists isTrue literals;  
paulson@17404
   261
paulson@17317
   262
paulson@18218
   263
(*Declarations of the current theory--to allow suppressing types.*)
paulson@18218
   264
val const_typargs = ref (Library.K [] : (string*typ -> typ list));
paulson@17317
   265
paulson@18218
   266
fun num_typargs(s,T) = if !keep_types then length (!const_typargs (s,T)) else 0;
paulson@16925
   267
paulson@16925
   268
(*Initialize the type suppression mechanism with the current theory before
paulson@16925
   269
    producing any clauses!*)
paulson@18218
   270
fun init thy = (const_typargs := Sign.const_typargs thy);
paulson@16925
   271
    
quigley@17150
   272
mengj@18402
   273
(*Flatten a type to a fol_type while accumulating sort constraints on the TFrees and
paulson@16925
   274
  TVars it contains.*)    
paulson@18218
   275
fun type_of (Type (a, Ts)) = 
paulson@18798
   276
      let val (folTyps, ts) = types_of Ts 
paulson@18218
   277
	  val t = make_fixed_type_const a
paulson@18798
   278
      in (Comp(t,folTyps), ts) end
paulson@18798
   279
  | type_of (TFree (a,s)) = (AtomF(make_fixed_type_var a), [(FOLTFree a, s)]) 
paulson@18798
   280
  | type_of (TVar (v, s)) = (AtomV(make_schematic_type_var v), [(FOLTVar v, s)])
paulson@18218
   281
and types_of Ts =
paulson@18798
   282
      let val (folTyps,ts) = ListPair.unzip (map type_of Ts)
paulson@18798
   283
      in (folTyps, union_all ts) end;
paulson@15390
   284
mengj@18439
   285
paulson@18218
   286
fun const_types_of (c,T) = types_of (!const_typargs (c,T));
paulson@16925
   287
paulson@16903
   288
(* Any variables created via the METAHYPS tactical should be treated as
paulson@16903
   289
   universal vars, although it is represented as "Free(...)" by Isabelle *)
paulson@16903
   290
val isMeta = String.isPrefix "METAHYP1_"
quigley@17150
   291
paulson@18798
   292
fun pred_name_type (Const(c,T)) = (make_fixed_const c, const_types_of (c,T))
paulson@15390
   293
  | pred_name_type (Free(x,T))  = 
paulson@17404
   294
      if isMeta x then raise CLAUSE("Predicate Not First Order 1", Free(x,T)) 
paulson@18798
   295
      else (make_fixed_var x, ([],[]))
paulson@17404
   296
  | pred_name_type (v as Var _) = raise CLAUSE("Predicate Not First Order 2", v)
paulson@17404
   297
  | pred_name_type t        = raise CLAUSE("Predicate input unexpected", t);
paulson@15347
   298
paulson@15615
   299
paulson@18218
   300
(* For typed equality *)
paulson@15615
   301
(* here "arg_typ" is the type of "="'s argument's type, not the type of the equality *)
paulson@15615
   302
(* Find type of equality arg *)
paulson@15615
   303
fun eq_arg_type (Type("fun",[T,_])) = 
paulson@15615
   304
    let val (folT,_) = type_of T;
paulson@17230
   305
    in  folT  end;
paulson@15347
   306
paulson@18798
   307
fun fun_name_type (Const(c,T)) args = (make_fixed_const c, const_types_of (c,T))
paulson@18798
   308
  | fun_name_type (Free(x,T)) args  = 
paulson@18798
   309
       if isMeta x then raise CLAUSE("Function Not First Order", Free(x,T))
paulson@18798
   310
       else (make_fixed_var x, ([],[]))
paulson@17404
   311
  | fun_name_type f args = raise CLAUSE("Function Not First Order 1", f);
quigley@17150
   312
paulson@18856
   313
(*Convert a term to a fol_term while accumulating sort constraints on the TFrees and
paulson@18856
   314
  TVars it contains.*)    
paulson@15347
   315
fun term_of (Var(ind_nm,T)) = 
paulson@18798
   316
      let val (folType,ts) = type_of T
paulson@18798
   317
      in (UVar(make_schematic_var ind_nm, folType), ts) end
paulson@15347
   318
  | term_of (Free(x,T)) = 
paulson@18798
   319
      let val (folType, ts) = type_of T
paulson@17230
   320
      in
paulson@18798
   321
	  if isMeta x then (UVar(make_schematic_var(x,0),folType), ts)
paulson@18798
   322
	  else (Fun(make_fixed_var x, [folType], []), ts)
paulson@17230
   323
      end
paulson@18218
   324
  | term_of app = 
paulson@17230
   325
      let val (f,args) = strip_comb app
paulson@18798
   326
	  val (funName,(contys,ts1)) = fun_name_type f args
paulson@18798
   327
	  val (args',ts2) = terms_of args
paulson@17230
   328
      in
paulson@18868
   329
	  (Fun(funName,contys,args'), union_all (ts1::ts2))
paulson@17230
   330
      end
paulson@18798
   331
and terms_of ts = ListPair.unzip (map term_of ts)
paulson@15390
   332
paulson@18856
   333
(*Create a predicate value, again accumulating sort constraints.*)    
paulson@17404
   334
fun pred_of (Const("op =", typ), args) =
paulson@17404
   335
      let val arg_typ = eq_arg_type typ 
paulson@18798
   336
	  val (args',ts) = terms_of args
paulson@17404
   337
	  val equal_name = make_fixed_const "op ="
paulson@17404
   338
      in
paulson@18218
   339
	  (Predicate(equal_name,[arg_typ],args'),
paulson@18856
   340
	   union_all ts)
paulson@17404
   341
      end
paulson@17404
   342
  | pred_of (pred,args) = 
paulson@18856
   343
      let val (pname, (predType,ts1)) = pred_name_type pred
paulson@18798
   344
	  val (args',ts2) = terms_of args
paulson@17404
   345
      in
paulson@18856
   346
	  (Predicate(pname,predType,args'), union_all (ts1::ts2))
paulson@17404
   347
      end;
paulson@15347
   348
paulson@20018
   349
(*Treatment of literals, possibly negated*)
paulson@20018
   350
fun predicate_of ((Const("Not",_) $ P), polarity) = predicate_of (P, not polarity)
paulson@20018
   351
  | predicate_of (term,polarity) = (pred_of (strip_comb term), polarity);
paulson@15347
   352
paulson@17888
   353
fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
paulson@18856
   354
  | literals_of_term1 args (Const("op |",_) $ P $ Q) = 
paulson@18856
   355
      literals_of_term1 (literals_of_term1 args P) Q
paulson@18856
   356
  | literals_of_term1 (lits, ts) P =
paulson@20018
   357
      let val ((pred, ts'), polarity) = predicate_of (P,true)
paulson@20018
   358
	  val lits' = Literal(polarity,pred) :: lits
paulson@17234
   359
      in
paulson@18856
   360
	  (lits', ts union ts')
paulson@17234
   361
      end;
quigley@17150
   362
paulson@18856
   363
val literals_of_term = literals_of_term1 ([],[]);
quigley@17150
   364
mengj@18403
   365
mengj@18403
   366
fun list_ord _ ([],[]) = EQUAL
mengj@18403
   367
  | list_ord _ ([],_) = LESS
mengj@18403
   368
  | list_ord _ (_,[]) = GREATER
paulson@19207
   369
  | list_ord ord (x::xs, y::ys) = 
paulson@19207
   370
      (case ord(x,y) of EQUAL => list_ord ord (xs,ys)
paulson@19207
   371
	 	      | xy_ord => xy_ord);
paulson@19207
   372
		     
paulson@18798
   373
(*Make literals for sorted type variables.  FIXME: can it use map?*) 
quigley@17150
   374
fun sorts_on_typs (_, [])   = ([]) 
paulson@16199
   375
  | sorts_on_typs (v, "HOL.type" :: s) =
paulson@18411
   376
      sorts_on_typs (v,s)                (*IGNORE sort "type"*)
paulson@18798
   377
  | sorts_on_typs ((FOLTVar indx), s::ss) =
paulson@18798
   378
      LTVar(make_type_class s, make_schematic_type_var indx) :: 
paulson@18798
   379
      sorts_on_typs ((FOLTVar indx), ss)
paulson@18798
   380
  | sorts_on_typs ((FOLTFree x), s::ss) =
paulson@18798
   381
      LTFree(make_type_class s, make_fixed_type_var x) :: 
paulson@18798
   382
      sorts_on_typs ((FOLTFree x), ss);
paulson@15347
   383
quigley@17150
   384
paulson@18798
   385
fun pred_of_sort (LTVar (s,ty)) = (s,1)
paulson@18798
   386
|   pred_of_sort (LTFree (s,ty)) = (s,1)
quigley@17150
   387
paulson@16199
   388
(*Given a list of sorted type variables, return two separate lists.
paulson@16199
   389
  The first is for TVars, the second for TFrees.*)
paulson@18856
   390
fun add_typs_aux [] = ([],[])
paulson@18856
   391
  | add_typs_aux ((FOLTVar indx,s)::tss) = 
paulson@17230
   392
      let val vs = sorts_on_typs (FOLTVar indx, s)
paulson@18856
   393
	  val (vss,fss) = add_typs_aux tss
quigley@17150
   394
      in
paulson@18856
   395
	  (vs union vss, fss)
quigley@17150
   396
      end
paulson@18856
   397
  | add_typs_aux ((FOLTFree x,s)::tss) =
paulson@17230
   398
      let val fs = sorts_on_typs (FOLTFree x, s)
paulson@18856
   399
	  val (vss,fss) = add_typs_aux tss
quigley@17150
   400
      in
paulson@18856
   401
	  (vss, fs union fss)
quigley@17150
   402
      end;
quigley@17150
   403
paulson@18920
   404
(** Too general means, positive equality literal with a variable X as one operand,
paulson@18920
   405
  when X does not occur properly in the other operand. This rules out clearly
paulson@18920
   406
  inconsistent clauses such as V=a|V=b, though it by no means guarantees soundness. **)
paulson@18920
   407
paulson@18920
   408
fun occurs a (UVar(b,_)) = a=b
paulson@18920
   409
  | occurs a (Fun (_,_,ts)) = exists (occurs a) ts
paulson@18920
   410
paulson@18920
   411
(*Is the first operand a variable that does not properly occur in the second operand?*)
paulson@18920
   412
fun too_general_terms (UVar _, UVar _) = false
paulson@18920
   413
  | too_general_terms (Fun _, _) = false
paulson@18920
   414
  | too_general_terms (UVar (a,_), t) = not (occurs a t);
paulson@18920
   415
paulson@20018
   416
fun too_general_lit (Literal (true, Predicate("equal",_,[x,y]))) =
paulson@18920
   417
      too_general_terms (x,y) orelse too_general_terms(y,x)
paulson@18920
   418
  | too_general_lit _ = false;
quigley@17150
   419
mengj@20015
   420
mengj@20015
   421
mengj@20015
   422
(** make axiom and conjecture clauses. **)
mengj@20015
   423
mengj@20015
   424
exception MAKE_CLAUSE;
mengj@20015
   425
fun make_clause (clause_id, axiom_name, th, kind) =
paulson@20018
   426
  let val term = prop_of th
paulson@20018
   427
      val (lits,types_sorts) = literals_of_term term
paulson@20018
   428
  in if forall isFalse lits 
paulson@20018
   429
     then error "Problem too trivial for resolution (empty clause)"
paulson@20018
   430
     else if kind=Axiom andalso forall too_general_lit lits 
paulson@20018
   431
     then (Output.debug ("Omitting " ^ axiom_name ^ ": equalities are too general"); 
paulson@20018
   432
           raise MAKE_CLAUSE)
paulson@20018
   433
     else Clause {clause_id = clause_id, axiom_name = axiom_name, th = th, 
paulson@20018
   434
                  kind = kind, literals = lits, types_sorts = types_sorts}
paulson@20018
   435
  end;		     
mengj@20015
   436
mengj@20015
   437
fun get_tvar_strs [] = []
mengj@20015
   438
  | get_tvar_strs ((FOLTVar indx,s)::tss) = 
mengj@20015
   439
      (make_schematic_type_var indx) ins (get_tvar_strs tss)
mengj@20015
   440
  | get_tvar_strs((FOLTFree x,s)::tss) = get_tvar_strs tss
mengj@20015
   441
mengj@20015
   442
(* check if a clause is first-order before making a conjecture clause*)
mengj@20015
   443
fun make_conjecture_clause n thm =
mengj@20015
   444
    let val t = prop_of thm
mengj@20015
   445
	val _ = check_is_fol_term t
mengj@20015
   446
	    handle TERM("check_is_fol_term",_) => raise CLAUSE("Goal is not FOL",t)
mengj@20015
   447
    in
mengj@20015
   448
	make_clause(n, "conjecture", thm, Conjecture)
mengj@20015
   449
    end;
mengj@20015
   450
    
mengj@20015
   451
fun make_conjecture_clauses_aux _ [] = []
mengj@20015
   452
  | make_conjecture_clauses_aux n (t::ts) =
mengj@20015
   453
      make_conjecture_clause n t :: make_conjecture_clauses_aux (n+1) ts
mengj@20015
   454
mengj@20015
   455
val make_conjecture_clauses = make_conjecture_clauses_aux 0
mengj@20015
   456
mengj@18199
   457
(*before converting an axiom clause to "clause" format, check if it is FOL*)
mengj@19443
   458
fun make_axiom_clause thm (ax_name,cls_id) =
paulson@20022
   459
  if Meson.is_fol_term (prop_of thm) 
paulson@20022
   460
  then (SOME (make_clause(cls_id, ax_name, thm, Axiom)) handle MAKE_CLAUSE => NONE)
paulson@20022
   461
  else (Output.debug ("Omitting " ^ ax_name ^ ": Axiom is not FOL"); NONE)
mengj@20015
   462
    
mengj@19443
   463
fun make_axiom_clauses [] = []
mengj@19443
   464
  | make_axiom_clauses ((thm,(name,id))::thms) =
paulson@20022
   465
      case make_axiom_clause thm (name,id) of 
paulson@20022
   466
          SOME cls => if isTaut cls then make_axiom_clauses thms 
paulson@20022
   467
                      else (name,cls) :: make_axiom_clauses thms
paulson@20022
   468
        | NONE => make_axiom_clauses thms;
mengj@19354
   469
paulson@15347
   470
(**** Isabelle arities ****)
paulson@15347
   471
paulson@15347
   472
exception ARCLAUSE of string;
paulson@15347
   473
 
paulson@15347
   474
type class = string; 
paulson@15347
   475
type tcons = string; 
paulson@15347
   476
paulson@18868
   477
datatype arLit = TConsLit of bool * (class * tcons * string list)
paulson@18868
   478
               | TVarLit of bool * (class * string);
paulson@15347
   479
 
paulson@15347
   480
datatype arityClause =  
paulson@15347
   481
	 ArityClause of {clause_id: clause_id,
paulson@17845
   482
	  	         axiom_name: axiom_name,
paulson@15347
   483
			 kind: kind,
paulson@15347
   484
			 conclLit: arLit,
paulson@15347
   485
			 premLits: arLit list};
paulson@15347
   486
paulson@15347
   487
paulson@18798
   488
fun gen_TVars 0 = []
paulson@18798
   489
  | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
paulson@15347
   490
paulson@18411
   491
fun pack_sort(_,[])  = []
paulson@18411
   492
  | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
paulson@18411
   493
  | pack_sort(tvar, cls::srt) =  (make_type_class cls, tvar) :: pack_sort(tvar, srt);
paulson@15347
   494
    
paulson@18868
   495
fun make_TVarLit (b, (cls,str)) = TVarLit(b, (cls,str));
paulson@18868
   496
fun make_TConsLit (b, (cls,tcons,tvars)) = 
paulson@18868
   497
      TConsLit(b, (make_type_class cls, make_fixed_type_const tcons, tvars));
paulson@15347
   498
paulson@18411
   499
(*Arity of type constructor tcon :: (arg1,...,argN)res*)
paulson@18411
   500
fun make_axiom_arity_clause (tcons, n, (res,args)) =
paulson@17845
   501
   let val nargs = length args
paulson@18798
   502
       val tvars = gen_TVars nargs
paulson@17845
   503
       val tvars_srts = ListPair.zip (tvars,args)
paulson@17845
   504
       val tvars_srts' = union_all(map pack_sort tvars_srts)
paulson@17845
   505
       val false_tvars_srts' = map (pair false) tvars_srts'
paulson@17845
   506
   in
paulson@17845
   507
      ArityClause {clause_id = n, kind = Axiom, 
paulson@18411
   508
                   axiom_name = lookup_type_const tcons,
paulson@18411
   509
                   conclLit = make_TConsLit(true, (res,tcons,tvars)), 
paulson@17845
   510
                   premLits = map make_TVarLit false_tvars_srts'}
paulson@17845
   511
   end;
paulson@15347
   512
paulson@15347
   513
paulson@15347
   514
(**** Isabelle class relations ****)
paulson@15347
   515
paulson@15347
   516
datatype classrelClause = 
paulson@18868
   517
	 ClassrelClause of {axiom_name: axiom_name,
paulson@15347
   518
			    subclass: class,
paulson@18411
   519
			    superclass: class};
paulson@17845
   520
paulson@17845
   521
fun make_axiom_classrelClause n subclass superclass =
paulson@18868
   522
  ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of subclass ^ 
paulson@18868
   523
                                "_" ^ Int.toString n,
paulson@18868
   524
                  subclass = make_type_class subclass, 
paulson@18868
   525
                  superclass = make_type_class superclass};
paulson@15347
   526
paulson@17845
   527
fun classrelClauses_of_aux n sub [] = []
paulson@18411
   528
  | classrelClauses_of_aux n sub ("HOL.type"::sups) = (*Should be ignored*)
paulson@18411
   529
      classrelClauses_of_aux n sub sups
paulson@17845
   530
  | classrelClauses_of_aux n sub (sup::sups) =
paulson@18868
   531
      make_axiom_classrelClause n sub sup :: classrelClauses_of_aux (n+1) sub sups;
paulson@15347
   532
paulson@18411
   533
fun classrelClauses_of (sub,sups) = classrelClauses_of_aux 0 sub sups;
paulson@17845
   534
wenzelm@19642
   535
val classrel_clauses_thy =
wenzelm@19642
   536
  maps classrelClauses_of o Graph.dest o #classes o Sorts.rep_algebra o Sign.classes_of;
paulson@18868
   537
paulson@18868
   538
paulson@18868
   539
(** Isabelle arities **)
paulson@17845
   540
paulson@17845
   541
fun arity_clause _ (tcons, []) = []
paulson@19155
   542
  | arity_clause n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
paulson@18411
   543
      arity_clause n (tcons,ars)
paulson@17845
   544
  | arity_clause n (tcons, ar::ars) =
paulson@17845
   545
      make_axiom_arity_clause (tcons,n,ar) :: 
paulson@17845
   546
      arity_clause (n+1) (tcons,ars);
paulson@17845
   547
paulson@17845
   548
fun multi_arity_clause [] = []
paulson@19155
   549
  | multi_arity_clause ((tcons,ars) :: tc_arlists) =
paulson@19155
   550
      (*Reversal ensures that older entries always get the same axiom name*)
paulson@19155
   551
      arity_clause 0 (tcons, rev ars)  @  
paulson@19155
   552
      multi_arity_clause tc_arlists 
paulson@17845
   553
paulson@17845
   554
fun arity_clause_thy thy =
wenzelm@19642
   555
  let val arities = thy |> Sign.classes_of
wenzelm@19642
   556
    |> Sorts.rep_algebra |> #arities |> Symtab.dest
wenzelm@19642
   557
    |> map (apsnd (map (fn (c, (_, Ss)) => (c, Ss))));
wenzelm@19521
   558
  in multi_arity_clause (rev arities) end;
paulson@17845
   559
paulson@17845
   560
paulson@18868
   561
(**** Find occurrences of predicates in clauses ****)
paulson@18868
   562
paulson@18868
   563
(*FIXME: multiple-arity checking doesn't work, as update_new is the wrong 
paulson@18868
   564
  function (it flags repeated declarations of a function, even with the same arity)*)
paulson@18868
   565
paulson@18868
   566
fun update_many (tab, keypairs) = foldl (uncurry Symtab.update) tab keypairs;
paulson@18868
   567
paulson@18868
   568
fun add_predicate_preds (Predicate(pname,tys,tms), preds) = 
paulson@18868
   569
  if pname = "equal" then preds (*equality is built-in and requires no declaration*)
paulson@18868
   570
  else Symtab.update (pname, length tys + length tms) preds
paulson@18868
   571
paulson@20018
   572
fun add_literal_preds (Literal(_,pred), preds) = add_predicate_preds (pred,preds)
paulson@18868
   573
paulson@18868
   574
fun add_type_sort_preds ((FOLTVar indx,s), preds) = 
paulson@18868
   575
      update_many (preds, map pred_of_sort (sorts_on_typs (FOLTVar indx, s)))
paulson@18868
   576
  | add_type_sort_preds ((FOLTFree x,s), preds) =
paulson@18868
   577
      update_many (preds, map pred_of_sort (sorts_on_typs (FOLTFree x, s)));
paulson@17845
   578
paulson@18868
   579
fun add_clause_preds (Clause {literals, types_sorts, ...}, preds) =
paulson@18868
   580
  foldl add_literal_preds (foldl add_type_sort_preds preds types_sorts) literals
paulson@18868
   581
  handle Symtab.DUP a => raise ERROR ("predicate " ^ a ^ " has multiple arities")
paulson@18868
   582
paulson@18868
   583
fun add_classrelClause_preds (ClassrelClause {subclass,superclass,...}, preds) =
paulson@18868
   584
  Symtab.update (subclass,1) (Symtab.update (superclass,1) preds);
paulson@17845
   585
paulson@18868
   586
fun add_arityClause_preds (ArityClause {conclLit,...}, preds) =
paulson@18868
   587
  let val TConsLit(_, (tclass, _, _)) = conclLit
paulson@18868
   588
  in  Symtab.update (tclass,1) preds  end;
paulson@18868
   589
paulson@18868
   590
fun preds_of_clauses clauses clsrel_clauses arity_clauses = 
paulson@18868
   591
  Symtab.dest
paulson@18868
   592
    (foldl add_classrelClause_preds 
paulson@18868
   593
      (foldl add_arityClause_preds
paulson@18868
   594
        (foldl add_clause_preds Symtab.empty clauses)
paulson@18868
   595
        arity_clauses)
paulson@18868
   596
      clsrel_clauses)
paulson@18798
   597
paulson@18868
   598
(*** Find occurrences of functions in clauses ***)
paulson@18868
   599
paulson@18868
   600
fun add_foltype_funcs (AtomV _, funcs) = funcs
paulson@18868
   601
  | add_foltype_funcs (AtomF a, funcs) = Symtab.update (a,0) funcs
paulson@18868
   602
  | add_foltype_funcs (Comp(a,tys), funcs) = 
paulson@18868
   603
      foldl add_foltype_funcs (Symtab.update (a, length tys) funcs) tys;
paulson@18868
   604
paulson@18868
   605
fun add_folterm_funcs (UVar _, funcs) = funcs
mengj@20015
   606
  | add_folterm_funcs (Fun(a,tys,[]), funcs) = Symtab.update (a,length tys) funcs
paulson@18868
   607
  | add_folterm_funcs (Fun(a,tys,tms), funcs) = 
paulson@18868
   608
      foldl add_foltype_funcs 
paulson@18868
   609
	    (foldl add_folterm_funcs (Symtab.update (a, length tys + length tms) funcs) 
paulson@18868
   610
	           tms) 
paulson@18868
   611
	    tys
paulson@18798
   612
paulson@18868
   613
fun add_predicate_funcs (Predicate(_,tys,tms), funcs) = 
paulson@18868
   614
    foldl add_foltype_funcs (foldl add_folterm_funcs funcs tms) tys;
paulson@18868
   615
paulson@20018
   616
fun add_literal_funcs (Literal(_,pred), funcs) = add_predicate_funcs (pred,funcs)
paulson@18868
   617
paulson@18868
   618
fun add_arityClause_funcs (ArityClause {conclLit,...}, funcs) =
paulson@18868
   619
  let val TConsLit(_, (_, tcons, tvars)) = conclLit
paulson@18868
   620
  in  Symtab.update (tcons, length tvars) funcs  end;
paulson@17845
   621
paulson@18868
   622
fun add_clause_funcs (Clause {literals, ...}, funcs) =
paulson@18868
   623
  foldl add_literal_funcs funcs literals
paulson@18868
   624
  handle Symtab.DUP a => raise ERROR ("function " ^ a ^ " has multiple arities")
paulson@18868
   625
paulson@18868
   626
fun funcs_of_clauses clauses arity_clauses = 
paulson@18868
   627
  Symtab.dest (foldl add_arityClause_funcs 
paulson@18868
   628
                     (foldl add_clause_funcs Symtab.empty clauses)
paulson@18868
   629
                     arity_clauses)
paulson@18868
   630
paulson@18868
   631
paulson@18868
   632
(**** String-oriented operations ****)
paulson@15347
   633
paulson@15347
   634
fun wrap_eq_type typ t = eq_typ_wrapper ^"(" ^ t ^ "," ^ typ ^ ")";
paulson@15347
   635
paulson@18218
   636
(*Only need to wrap equality's arguments with "typeinfo" if the output clauses are typed 
paulson@18218
   637
 and if we specifically ask for types to be included.   *)
paulson@15347
   638
fun string_of_equality (typ,terms) =
paulson@17230
   639
      let val [tstr1,tstr2] = map string_of_term terms
mengj@18402
   640
	  val typ' = string_of_fol_type typ
paulson@17230
   641
      in
paulson@17230
   642
	  if !keep_types andalso !special_equal 
mengj@18402
   643
	  then "equal(" ^ (wrap_eq_type typ' tstr1) ^ "," ^ 
mengj@18402
   644
		 	  (wrap_eq_type typ' tstr2) ^ ")"
paulson@17230
   645
	  else "equal(" ^ tstr1 ^ "," ^ tstr2 ^ ")"
paulson@17230
   646
      end
paulson@17230
   647
and string_of_term (UVar(x,_)) = x
paulson@18218
   648
  | string_of_term (Fun("equal",[typ],terms)) = string_of_equality(typ,terms)
mengj@20015
   649
  | string_of_term (Fun (name,typs,[])) = name ^ (paren_pack (map string_of_fol_type typs))
paulson@18218
   650
  | string_of_term (Fun (name,typs,terms)) = 
paulson@18218
   651
      let val terms_as_strings = map string_of_term terms
mengj@18402
   652
	  val typs' = if !keep_types then map string_of_fol_type typs else []
paulson@18420
   653
      in  name ^ (paren_pack (terms_as_strings @ typs'))  end;
paulson@15347
   654
paulson@15347
   655
(* before output the string of the predicate, check if the predicate corresponds to an equality or not. *)
paulson@18218
   656
fun string_of_predicate (Predicate("equal",[typ],terms)) = string_of_equality(typ,terms)
paulson@18218
   657
  | string_of_predicate (Predicate(name,typs,terms)) = 
paulson@17230
   658
      let val terms_as_strings = map string_of_term terms
mengj@18402
   659
	  val typs' = if !keep_types then map string_of_fol_type typs else []
paulson@18420
   660
      in  name ^ (paren_pack (terms_as_strings @ typs'))  end;
paulson@17317
   661
paulson@17317
   662
fun string_of_clausename (cls_id,ax_name) = 
paulson@17525
   663
    clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id;
paulson@17317
   664
paulson@17317
   665
fun string_of_type_clsname (cls_id,ax_name,idx) = 
paulson@17525
   666
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
paulson@18863
   667
paulson@18863
   668
(*Write a list of strings to a file*)
paulson@18863
   669
fun writeln_strs os = List.app (fn s => TextIO.output (os,s));
paulson@18863
   670
quigley@17150
   671
    
paulson@18868
   672
(**** Producing DFG files ****)
quigley@17150
   673
paulson@18863
   674
(*Attach sign in DFG syntax: false means negate.*)
paulson@18863
   675
fun dfg_sign true s = s
paulson@18863
   676
  | dfg_sign false s = "not(" ^ s ^ ")"  
paulson@18863
   677
paulson@20018
   678
fun dfg_literal (Literal(pol,pred)) = dfg_sign pol (string_of_predicate pred)
quigley@17150
   679
paulson@18798
   680
fun dfg_of_typeLit (LTVar (s,ty)) = "not(" ^ s ^ "(" ^ ty ^ "))"
paulson@18856
   681
  | dfg_of_typeLit (LTFree (s,ty)) = s ^ "(" ^ ty ^ ")";
quigley@17150
   682
 
paulson@18868
   683
(*Enclose the clause body by quantifiers, if necessary*)
paulson@18868
   684
fun dfg_forall [] body = body  
paulson@18868
   685
  | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
quigley@17150
   686
paulson@18868
   687
fun gen_dfg_cls (cls_id, ax_name, knd, lits, vars) = 
paulson@18868
   688
    "clause( %(" ^ knd ^ ")\n" ^ 
paulson@18868
   689
    dfg_forall vars ("or(" ^ lits ^ ")") ^ ",\n" ^ 
paulson@18863
   690
    string_of_clausename (cls_id,ax_name) ^  ").\n\n";
quigley@17150
   691
paulson@18869
   692
fun dfg_clause_aux (Clause{literals, types_sorts, ...}) = 
paulson@18868
   693
  let val lits = map dfg_literal literals
paulson@18869
   694
      val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@17230
   695
      val tvar_lits_strs = 
paulson@18869
   696
	  if !keep_types then map dfg_of_typeLit tvar_lits else []
paulson@17230
   697
      val tfree_lits =
paulson@18869
   698
          if !keep_types then map dfg_of_typeLit tfree_lits else []
paulson@17230
   699
  in
paulson@17234
   700
      (tvar_lits_strs @ lits, tfree_lits)
paulson@17230
   701
  end; 
quigley@17150
   702
paulson@20018
   703
fun dfg_folterms (Literal(pol,pred)) = 
paulson@18856
   704
  let val Predicate (_, _, folterms) = pred
paulson@18218
   705
  in  folterms  end
quigley@17150
   706
paulson@17404
   707
fun get_uvars (UVar(a,typ)) = [a] 
paulson@18868
   708
  | get_uvars (Fun (_,typ,tlist)) = union_all(map get_uvars tlist)
paulson@17404
   709
paulson@18868
   710
fun dfg_vars (Clause {literals,...}) =
paulson@18920
   711
  union_all (map get_uvars (List.concat (map dfg_folterms literals)))
quigley@17150
   712
paulson@18798
   713
fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,types_sorts,...}) =
paulson@17230
   714
    let val (lits,tfree_lits) = dfg_clause_aux cls 
paulson@18798
   715
            (*"lits" includes the typing assumptions (TVars)*)
quigley@17150
   716
        val vars = dfg_vars cls
paulson@18798
   717
        val tvars = get_tvar_strs types_sorts
paulson@18798
   718
	val knd = name_of_kind kind
paulson@17234
   719
	val lits_str = commas lits
paulson@18868
   720
	val cls_str = gen_dfg_cls(clause_id, axiom_name, knd, lits_str, tvars@vars) 
paulson@18798
   721
    in (cls_str, tfree_lits) end;
quigley@17150
   722
paulson@18798
   723
fun string_of_arity (name, num) =  "(" ^ name ^ "," ^ Int.toString num ^ ")"
quigley@17150
   724
paulson@18856
   725
fun string_of_preds [] = ""
paulson@18856
   726
  | string_of_preds preds = "predicates[" ^ commas(map string_of_arity preds) ^ "].\n";
quigley@17150
   727
paulson@18856
   728
fun string_of_funcs [] = ""
paulson@18856
   729
  | string_of_funcs funcs = "functions[" ^ commas(map string_of_arity funcs) ^ "].\n" ;
quigley@17150
   730
paulson@17234
   731
fun string_of_symbols predstr funcstr = 
paulson@17234
   732
  "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
quigley@17150
   733
paulson@18798
   734
fun string_of_start name = "begin_problem(" ^ name ^ ").\n\n";
quigley@17150
   735
paulson@18863
   736
fun string_of_descrip name = 
paulson@18868
   737
  "list_of_descriptions.\nname({*" ^ name ^ 
paulson@18868
   738
  "*}).\nauthor({*Isabelle*}).\nstatus(unknown).\ndescription({*auto-generated*}).\nend_of_list.\n\n"
quigley@17150
   739
paulson@18863
   740
fun dfg_tfree_clause tfree_lit =
paulson@18863
   741
  "clause( %(conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ").\n\n"
paulson@18863
   742
paulson@17845
   743
fun string_of_arClauseID (ArityClause {clause_id,axiom_name,...}) =
paulson@17845
   744
    arclause_prefix ^ ascii_of axiom_name ^ "_" ^ Int.toString clause_id;
quigley@17150
   745
paulson@18863
   746
fun dfg_of_arLit (TConsLit(pol,(c,t,args))) =
paulson@18863
   747
      dfg_sign pol (c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@18863
   748
  | dfg_of_arLit (TVarLit(pol,(c,str))) =
paulson@18863
   749
      dfg_sign pol (c ^ "(" ^ str ^ ")")
paulson@17525
   750
    
paulson@18863
   751
fun dfg_classrelLits sub sup = 
paulson@18863
   752
    let val tvar = "(T)"
paulson@18863
   753
    in 
paulson@18863
   754
	"not(" ^ sub ^ tvar ^ "), " ^ sup ^ tvar
paulson@18863
   755
    end;
paulson@17525
   756
paulson@18868
   757
fun dfg_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
paulson@18868
   758
  "clause(forall([T],\nor( " ^ dfg_classrelLits subclass superclass ^ ")),\n" ^
paulson@18868
   759
  axiom_name ^ ").\n\n";
paulson@18868
   760
      
paulson@18868
   761
fun dfg_arity_clause (arcls as ArityClause{kind,conclLit,premLits,...}) = 
paulson@18868
   762
  let val arcls_id = string_of_arClauseID arcls
paulson@18868
   763
      val knd = name_of_kind kind
paulson@18868
   764
      val TConsLit(_, (_,_,tvars)) = conclLit
paulson@18868
   765
      val lits = map dfg_of_arLit (conclLit :: premLits)
paulson@18863
   766
  in
paulson@18868
   767
      "clause( %(" ^ knd ^ ")\n" ^ 
paulson@18868
   768
      dfg_forall tvars ("or( " ^ commas lits ^ ")") ^ ",\n" ^
paulson@18868
   769
      arcls_id ^ ").\n\n"
paulson@18863
   770
  end;
paulson@18863
   771
paulson@18863
   772
(* write out a subgoal in DFG format to the file "xxxx_N"*)
mengj@19719
   773
fun dfg_write_file thms filename (axclauses,classrel_clauses,arity_clauses) = 
paulson@18863
   774
  let 
paulson@19207
   775
    val _ = Output.debug ("Preparing to write the DFG file " ^ filename)
mengj@19719
   776
    val conjectures = make_conjecture_clauses thms
paulson@20022
   777
    val (clnames,axclauses') = ListPair.unzip (make_axiom_clauses axclauses)
paulson@18868
   778
    val (dfg_clss, tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
mengj@19719
   779
    val clss = conjectures @ axclauses'
paulson@18868
   780
    val funcs = funcs_of_clauses clss arity_clauses
paulson@18868
   781
    and preds = preds_of_clauses clss classrel_clauses arity_clauses
paulson@18863
   782
    and probname = Path.pack (Path.base (Path.unpack filename))
mengj@19719
   783
    val (axstrs, _) = ListPair.unzip (map clause2dfg axclauses')
paulson@18863
   784
    val tfree_clss = map dfg_tfree_clause (union_all tfree_litss) 
paulson@19155
   785
    val out = TextIO.openOut filename
paulson@18863
   786
  in
paulson@18868
   787
    TextIO.output (out, string_of_start probname); 
paulson@18868
   788
    TextIO.output (out, string_of_descrip probname); 
paulson@18868
   789
    TextIO.output (out, string_of_symbols (string_of_funcs funcs) (string_of_preds preds)); 
paulson@18868
   790
    TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
paulson@18868
   791
    writeln_strs out axstrs;
paulson@18868
   792
    List.app (curry TextIO.output out o dfg_classrelClause) classrel_clauses;
paulson@18868
   793
    List.app (curry TextIO.output out o dfg_arity_clause) arity_clauses;
paulson@18868
   794
    TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
paulson@18868
   795
    writeln_strs out tfree_clss;
paulson@18868
   796
    writeln_strs out dfg_clss;
paulson@18868
   797
    TextIO.output (out, "end_of_list.\n\nend_problem.\n");
paulson@20022
   798
    TextIO.closeOut out;
paulson@20022
   799
    clnames
paulson@17525
   800
  end;
quigley@17150
   801
quigley@17150
   802
paulson@18869
   803
(**** Produce TPTP files ****)
paulson@18868
   804
paulson@18868
   805
(*Attach sign in TPTP syntax: false means negate.*)
paulson@18868
   806
fun tptp_sign true s = "++" ^ s
paulson@18868
   807
  | tptp_sign false s = "--" ^ s
paulson@18868
   808
paulson@20018
   809
fun tptp_literal (Literal(pol,pred)) = 
paulson@20018
   810
      (if pol then "++" else "--") ^ string_of_predicate pred;
paulson@15347
   811
paulson@18798
   812
fun tptp_of_typeLit (LTVar (s,ty)) = "--" ^ s ^ "(" ^ ty ^ ")"
paulson@18798
   813
  | tptp_of_typeLit (LTFree (s,ty)) = "++" ^ s ^ "(" ^ ty ^ ")";
paulson@15347
   814
 
paulson@15347
   815
fun gen_tptp_cls (cls_id,ax_name,knd,lits) = 
paulson@17317
   816
    "input_clause(" ^ string_of_clausename (cls_id,ax_name) ^ "," ^ 
paulson@18863
   817
    knd ^ "," ^ lits ^ ").\n";
paulson@15347
   818
paulson@17317
   819
fun gen_tptp_type_cls (cls_id,ax_name,knd,tfree_lit,idx) = 
paulson@17317
   820
    "input_clause(" ^ string_of_type_clsname (cls_id,ax_name,idx) ^ "," ^ 
paulson@18863
   821
    knd ^ ",[" ^ tfree_lit ^ "]).\n";
paulson@15347
   822
paulson@18869
   823
fun tptp_type_lits (Clause {literals, types_sorts, ...}) = 
paulson@18869
   824
    let val lits = map tptp_literal literals
paulson@18869
   825
	val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@18869
   826
        val tvar_lits_strs =
paulson@18869
   827
            if !keep_types then map tptp_of_typeLit tvar_lits else []
paulson@18869
   828
	val tfree_lits =
paulson@18869
   829
	    if !keep_types then map tptp_of_typeLit tfree_lits else []
paulson@15347
   830
    in
paulson@17305
   831
	(tvar_lits_strs @ lits, tfree_lits)
paulson@15347
   832
    end; 
paulson@15347
   833
paulson@18869
   834
fun clause2tptp (cls as Clause {clause_id, axiom_name, kind, ...}) =
paulson@17422
   835
    let val (lits,tfree_lits) = tptp_type_lits cls 
paulson@17305
   836
            (*"lits" includes the typing assumptions (TVars)*)
paulson@18869
   837
	val knd = name_of_kind kind
paulson@18869
   838
	val cls_str = gen_tptp_cls(clause_id, axiom_name, knd, bracket_pack lits) 
paulson@15608
   839
    in
paulson@15608
   840
	(cls_str,tfree_lits) 
paulson@15608
   841
    end;
paulson@15608
   842
paulson@18863
   843
fun tptp_tfree_clause tfree_lit =
paulson@18863
   844
    "input_clause(" ^ "tfree_tcs," ^ "conjecture" ^ ",[" ^ tfree_lit ^ "]).\n";
paulson@15608
   845
paulson@17525
   846
fun tptp_of_arLit (TConsLit(b,(c,t,args))) =
paulson@18868
   847
      tptp_sign b (c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@17525
   848
  | tptp_of_arLit (TVarLit(b,(c,str))) =
paulson@18868
   849
      tptp_sign b (c ^ "(" ^ str ^ ")")
paulson@15347
   850
    
paulson@18868
   851
fun tptp_arity_clause (arcls as ArityClause{kind,conclLit,premLits,...}) = 
paulson@18868
   852
  let val arcls_id = string_of_arClauseID arcls
paulson@18868
   853
      val knd = name_of_kind kind
paulson@18868
   854
      val lits = map tptp_of_arLit (conclLit :: premLits)
paulson@18868
   855
  in
paulson@18868
   856
    "input_clause(" ^ arcls_id ^ "," ^ knd ^ "," ^ bracket_pack lits ^ ").\n"
paulson@18868
   857
  end;
paulson@15347
   858
paulson@15347
   859
fun tptp_classrelLits sub sup = 
paulson@15347
   860
    let val tvar = "(T)"
paulson@15347
   861
    in 
paulson@18411
   862
	"[--" ^ sub ^ tvar ^ ",++" ^ sup ^ tvar ^ "]"
paulson@15347
   863
    end;
paulson@15347
   864
paulson@18868
   865
fun tptp_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
paulson@18868
   866
  "input_clause(" ^ axiom_name ^ ",axiom," ^ tptp_classrelLits subclass superclass ^ ").\n" 
quigley@17150
   867
paulson@18863
   868
(* write out a subgoal as tptp clauses to the file "xxxx_N"*)
mengj@19443
   869
fun tptp_write_file thms filename (axclauses,classrel_clauses,arity_clauses) =
paulson@18863
   870
  let
paulson@19207
   871
    val _ = Output.debug ("Preparing to write the TPTP file " ^ filename)
mengj@19443
   872
    val clss = make_conjecture_clauses thms
paulson@20022
   873
    val (clnames,axclauses') = ListPair.unzip (make_axiom_clauses axclauses)
paulson@18863
   874
    val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp clss)
paulson@18863
   875
    val tfree_clss = map tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
paulson@18863
   876
    val out = TextIO.openOut filename
paulson@18863
   877
  in
mengj@19197
   878
    List.app (curry TextIO.output out o #1 o clause2tptp) axclauses';
paulson@18863
   879
    writeln_strs out tfree_clss;
paulson@18863
   880
    writeln_strs out tptp_clss;
paulson@18868
   881
    List.app (curry TextIO.output out o tptp_classrelClause) classrel_clauses;
paulson@18868
   882
    List.app (curry TextIO.output out o tptp_arity_clause) arity_clauses;
paulson@20022
   883
    TextIO.closeOut out;
paulson@20022
   884
    clnames
paulson@18863
   885
  end;
paulson@18863
   886
mengj@19354
   887
mengj@19354
   888
mengj@19354
   889
mengj@19354
   890
paulson@15347
   891
end;