src/HOL/Tools/res_clause.ML
author haftmann
Fri Dec 16 09:00:11 2005 +0100 (2005-12-16)
changeset 18418 bf448d999b7e
parent 18411 2d3165a0fb40
child 18420 9470061ab283
permissions -rw-r--r--
re-arranged tuples (theory * 'a) to ('a * theory) in Pure
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
quigley@17150
     9
(* works for writeoutclasimp on typed *)
paulson@15347
    10
signature RES_CLAUSE =
paulson@15347
    11
  sig
paulson@17404
    12
  val keep_types : bool ref
paulson@17404
    13
  val special_equal : bool ref
paulson@17404
    14
  val tagged : bool ref
paulson@17404
    15
paulson@17404
    16
  exception ARCLAUSE of string
paulson@17404
    17
  exception CLAUSE of string * term
paulson@17404
    18
  type arityClause 
paulson@17404
    19
  type classrelClause
paulson@17404
    20
  type clause
paulson@17404
    21
  val init : theory -> unit
paulson@17404
    22
  val make_axiom_clause : Term.term -> string * int -> clause
paulson@17888
    23
  val make_conjecture_clauses : term list -> clause list
paulson@17404
    24
  val get_axiomName : clause ->  string
paulson@17404
    25
  val isTaut : clause -> bool
paulson@17404
    26
  val num_of_clauses : clause -> int
quigley@17150
    27
paulson@17404
    28
  val clause2dfg : clause -> string * string list
paulson@17404
    29
  val clauses2dfg : clause list -> string -> clause list -> clause list ->
paulson@17422
    30
	   (string * int) list -> (string * int) list -> string
paulson@17404
    31
  val tfree_dfg_clause : string -> string
quigley@17150
    32
paulson@17845
    33
  val arity_clause_thy: theory -> arityClause list 
paulson@17845
    34
  val classrel_clauses_thy: theory -> classrelClause list 
paulson@17845
    35
paulson@17404
    36
  val tptp_arity_clause : arityClause -> string
paulson@17404
    37
  val tptp_classrelClause : classrelClause -> string
paulson@17404
    38
  val tptp_clause : clause -> string list
paulson@17404
    39
  val clause2tptp : clause -> string * string list
paulson@17404
    40
  val tfree_clause : string -> string
paulson@17404
    41
  val schematic_var_prefix : string
paulson@17404
    42
  val fixed_var_prefix : string
paulson@17404
    43
  val tvar_prefix : string
paulson@17404
    44
  val tfree_prefix : string
paulson@17404
    45
  val clause_prefix : string 
paulson@17404
    46
  val arclause_prefix : string
paulson@17404
    47
  val const_prefix : string
paulson@17404
    48
  val tconst_prefix : string 
paulson@17404
    49
  val class_prefix : string 
mengj@17908
    50
mengj@17908
    51
  val union_all : ''a list list -> ''a list
mengj@17908
    52
  val ascii_of : String.string -> String.string
mengj@17908
    53
  val paren_pack : string list -> string
mengj@17908
    54
  val bracket_pack : string list -> string
mengj@17908
    55
  val make_schematic_var : String.string * int -> string
mengj@17908
    56
  val make_fixed_var : String.string -> string
mengj@17908
    57
  val make_schematic_type_var : string * int -> string
mengj@17908
    58
  val make_fixed_type_var : string -> string
mengj@17908
    59
  val make_fixed_const : String.string -> string		
mengj@17908
    60
  val make_fixed_type_const : String.string -> string   
mengj@17908
    61
  val make_type_class : String.string -> string
mengj@17999
    62
  val isMeta : String.string -> bool
mengj@17999
    63
  
mengj@17999
    64
  type typ_var
mengj@17999
    65
  val mk_typ_var_sort : Term.typ -> typ_var * sort
mengj@17999
    66
  type type_literal
mengj@17999
    67
  val add_typs_aux2 : (typ_var * string list) list -> type_literal list * type_literal list
mengj@17999
    68
  val gen_tptp_cls : int * string * string * string -> string
mengj@17999
    69
  val gen_tptp_type_cls : int * string * string * string * int -> string
mengj@17999
    70
  val tptp_of_typeLit : type_literal -> string
mengj@18275
    71
paulson@15347
    72
  end;
paulson@15347
    73
quigley@17150
    74
structure ResClause: RES_CLAUSE =
paulson@15347
    75
struct
paulson@15347
    76
paulson@15347
    77
(* Added for typed equality *)
paulson@15347
    78
val special_equal = ref false; (* by default,equality does not carry type information *)
paulson@15347
    79
val eq_typ_wrapper = "typeinfo"; (* default string *)
paulson@15347
    80
paulson@15347
    81
paulson@15347
    82
val schematic_var_prefix = "V_";
paulson@15347
    83
val fixed_var_prefix = "v_";
paulson@15347
    84
paulson@17230
    85
val tvar_prefix = "T_";
paulson@17230
    86
val tfree_prefix = "t_";
paulson@15347
    87
paulson@15347
    88
val clause_prefix = "cls_"; 
paulson@17525
    89
val arclause_prefix = "clsarity_" 
paulson@17525
    90
val clrelclause_prefix = "clsrel_";
paulson@15347
    91
paulson@17230
    92
val const_prefix = "c_";
paulson@17230
    93
val tconst_prefix = "tc_"; 
paulson@15347
    94
paulson@16199
    95
val class_prefix = "class_"; 
paulson@15347
    96
paulson@15347
    97
paulson@17775
    98
fun union_all xss = foldl (op union) [] xss;
paulson@17775
    99
paulson@15347
   100
 
paulson@17775
   101
(*Provide readable names for the more common symbolic functions*)
paulson@15347
   102
val const_trans_table =
paulson@15347
   103
      Symtab.make [("op =", "equal"),
paulson@15347
   104
	  	   ("op <=", "lessequals"),
paulson@15347
   105
		   ("op <", "less"),
paulson@15347
   106
		   ("op &", "and"),
paulson@15347
   107
		   ("op |", "or"),
paulson@17375
   108
		   ("op +", "plus"),
paulson@17375
   109
		   ("op -", "minus"),
paulson@17375
   110
		   ("op *", "times"),
paulson@15347
   111
		   ("op -->", "implies"),
paulson@17375
   112
		   ("{}", "emptyset"),
paulson@15347
   113
		   ("op :", "in"),
paulson@15347
   114
		   ("op Un", "union"),
paulson@18390
   115
		   ("op Int", "inter"),
paulson@18390
   116
		   ("List.op @", "append")];
paulson@15347
   117
paulson@17230
   118
val type_const_trans_table =
paulson@18411
   119
      Symtab.make [("*", "prod"),
paulson@18411
   120
	  	   ("+", "sum"),
paulson@18411
   121
		   ("~=>", "map")];
paulson@15347
   122
paulson@15610
   123
(*Escaping of special characters.
paulson@15610
   124
  Alphanumeric characters are left unchanged.
paulson@15610
   125
  The character _ goes to __
paulson@15610
   126
  Characters in the range ASCII space to / go to _A to _P, respectively.
paulson@15610
   127
  Other printing characters go to _NNN where NNN is the decimal ASCII code.*)
paulson@15610
   128
local
paulson@15610
   129
paulson@15610
   130
val A_minus_space = Char.ord #"A" - Char.ord #" ";
paulson@15610
   131
paulson@15347
   132
fun ascii_of_c c =
paulson@15610
   133
  if Char.isAlphaNum c then String.str c
paulson@15610
   134
  else if c = #"_" then "__"
paulson@15610
   135
  else if #" " <= c andalso c <= #"/" 
paulson@15610
   136
       then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
paulson@15610
   137
  else if Char.isPrint c then ("_" ^ Int.toString (Char.ord c))
paulson@15610
   138
  else ""
paulson@15347
   139
paulson@15610
   140
in
paulson@15610
   141
paulson@15610
   142
val ascii_of = String.translate ascii_of_c;
paulson@15610
   143
paulson@15610
   144
end;
paulson@15347
   145
paulson@17525
   146
(* convert a list of strings into one single string; surrounded by brackets *)
paulson@18218
   147
fun paren_pack [] = ""   (*empty argument list*)
paulson@18218
   148
  | paren_pack strings = "(" ^ commas strings ^ ")";
paulson@17525
   149
paulson@17525
   150
fun bracket_pack strings = "[" ^ commas strings ^ "]";
paulson@17525
   151
paulson@17525
   152
paulson@16925
   153
(*Remove the initial ' character from a type variable, if it is present*)
paulson@16925
   154
fun trim_type_var s =
paulson@16925
   155
  if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
paulson@16925
   156
  else error ("trim_type: Malformed type variable encountered: " ^ s);
paulson@16925
   157
paulson@16903
   158
fun ascii_of_indexname (v,0) = ascii_of v
paulson@17525
   159
  | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
paulson@15347
   160
paulson@17230
   161
fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
paulson@15347
   162
fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
paulson@15347
   163
paulson@16925
   164
fun make_schematic_type_var (x,i) = 
paulson@16925
   165
      tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
paulson@16925
   166
fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
paulson@15347
   167
paulson@18411
   168
fun lookup_const c =
wenzelm@17412
   169
    case Symtab.lookup const_trans_table c of
paulson@17230
   170
        SOME c' => c'
paulson@18411
   171
      | NONE => ascii_of c;
paulson@17230
   172
paulson@18411
   173
fun lookup_type_const c = 
wenzelm@17412
   174
    case Symtab.lookup type_const_trans_table c of
paulson@17230
   175
        SOME c' => c'
paulson@18411
   176
      | NONE => ascii_of c;
paulson@18411
   177
paulson@18411
   178
fun make_fixed_const "op =" = "equal"   (*MUST BE "equal" because it's built-in to ATPs*)
paulson@18411
   179
  | make_fixed_const c      = const_prefix ^ lookup_const c;
paulson@18411
   180
paulson@18411
   181
fun make_fixed_type_const c = tconst_prefix ^ lookup_type_const c;
quigley@17150
   182
wenzelm@17261
   183
fun make_type_class clas = class_prefix ^ ascii_of clas;
quigley@17150
   184
quigley@17150
   185
quigley@17150
   186
paulson@15347
   187
(***** definitions and functions for FOL clauses, prepared for conversion into TPTP format or SPASS format. *****)
paulson@15347
   188
paulson@17230
   189
val keep_types = ref true;
paulson@15347
   190
paulson@15347
   191
datatype kind = Axiom | Hypothesis | Conjecture;
paulson@15347
   192
fun name_of_kind Axiom = "axiom"
paulson@15347
   193
  | name_of_kind Hypothesis = "hypothesis"
paulson@15347
   194
  | name_of_kind Conjecture = "conjecture";
paulson@15347
   195
paulson@15347
   196
type clause_id = int;
paulson@15347
   197
type axiom_name = string;
paulson@15347
   198
paulson@15347
   199
paulson@15347
   200
type polarity = bool;
paulson@15347
   201
paulson@15347
   202
type indexname = Term.indexname;
paulson@15347
   203
paulson@15347
   204
paulson@15347
   205
(* "tag" is used for vampire specific syntax  *)
paulson@15347
   206
type tag = bool; 
paulson@15347
   207
paulson@15347
   208
paulson@15347
   209
(**** Isabelle FOL clauses ****)
paulson@15347
   210
paulson@15347
   211
val tagged = ref false;
paulson@15347
   212
paulson@15347
   213
type pred_name = string;
paulson@15347
   214
type sort = Term.sort;
mengj@18402
   215
mengj@18402
   216
mengj@18402
   217
mengj@18402
   218
datatype typ_var = FOLTVar of indexname | FOLTFree of string;
mengj@18402
   219
mengj@18402
   220
datatype fol_type = AtomV of string
mengj@18402
   221
		  | AtomF of string
mengj@18402
   222
		  | Comp of string * fol_type list;
mengj@18402
   223
mengj@18402
   224
fun string_of_fol_type (AtomV x) = x
mengj@18402
   225
  | string_of_fol_type (AtomF x) = x
mengj@18402
   226
  | string_of_fol_type (Comp(tcon,tps)) = 
mengj@18402
   227
    let val tstrs = map string_of_fol_type tps
mengj@18402
   228
    in
mengj@18402
   229
	tcon ^ (paren_pack tstrs)
mengj@18402
   230
    end;
mengj@18402
   231
paulson@15347
   232
paulson@15347
   233
datatype type_literal = LTVar of string | LTFree of string;
paulson@15347
   234
paulson@17317
   235
datatype folTerm = UVar of string * fol_type
paulson@18218
   236
                 | Fun of string * fol_type list * folTerm list;
paulson@18218
   237
datatype predicate = Predicate of pred_name * fol_type list * folTerm list;
paulson@15347
   238
paulson@15347
   239
datatype literal = Literal of polarity * predicate * tag;
paulson@15347
   240
mengj@17999
   241
fun mk_typ_var_sort (TFree(a,s)) = (FOLTFree a,s)
mengj@17999
   242
  | mk_typ_var_sort (TVar(v,s)) = (FOLTVar v,s);
mengj@17999
   243
mengj@17999
   244
paulson@15347
   245
paulson@15347
   246
(* ML datatype used to repsent one single clause: disjunction of literals. *)
paulson@15347
   247
datatype clause = 
paulson@15347
   248
	 Clause of {clause_id: clause_id,
paulson@15347
   249
		    axiom_name: axiom_name,
paulson@15347
   250
		    kind: kind,
paulson@15347
   251
		    literals: literal list,
paulson@15347
   252
		    types_sorts: (typ_var * sort) list, 
paulson@15347
   253
                    tvar_type_literals: type_literal list, 
quigley@17150
   254
                    tfree_type_literals: type_literal list ,
quigley@17150
   255
                    tvars: string list,
quigley@17150
   256
                    predicates: (string*int) list,
quigley@17150
   257
                    functions: (string*int) list};
quigley@17150
   258
paulson@15347
   259
paulson@17404
   260
exception CLAUSE of string * term;
paulson@15347
   261
mengj@18402
   262
fun get_literals (c as Clause(cls)) = #literals cls;
mengj@18402
   263
mengj@18402
   264
fun components_of_literal (Literal (pol,pred,tag)) = ((pol,pred),tag);
mengj@18402
   265
mengj@18402
   266
fun predicate_name (Predicate(predname,_,_)) = predname;
mengj@18402
   267
paulson@15347
   268
paulson@15347
   269
(*** make clauses ***)
paulson@15347
   270
paulson@17317
   271
fun isFalse (Literal (pol,Predicate(a,_,[]),_)) =
paulson@17317
   272
      (pol andalso a = "c_False") orelse
paulson@17317
   273
      (not pol andalso a = "c_True")
paulson@17317
   274
  | isFalse _ = false;
paulson@17317
   275
paulson@17404
   276
fun isTrue (Literal (pol,Predicate(a,_,[]),_)) =
paulson@17404
   277
      (pol andalso a = "c_True") orelse
paulson@17404
   278
      (not pol andalso a = "c_False")
paulson@17404
   279
  | isTrue _ = false;
paulson@17404
   280
  
paulson@17404
   281
fun isTaut (Clause {literals,...}) = exists isTrue literals;  
paulson@17404
   282
paulson@17234
   283
fun make_clause (clause_id,axiom_name,kind,literals,
paulson@17234
   284
                 types_sorts,tvar_type_literals,
paulson@17230
   285
                 tfree_type_literals,tvars, predicates, functions) =
paulson@17317
   286
  if forall isFalse literals 
paulson@17234
   287
  then error "Problem too trivial for resolution (empty clause)"
paulson@17234
   288
  else
paulson@17230
   289
     Clause {clause_id = clause_id, axiom_name = axiom_name, kind = kind, 
paulson@17230
   290
             literals = literals, types_sorts = types_sorts,
paulson@17230
   291
             tvar_type_literals = tvar_type_literals,
paulson@17230
   292
             tfree_type_literals = tfree_type_literals,
paulson@17230
   293
             tvars = tvars, predicates = predicates, 
paulson@17230
   294
             functions = functions};
quigley@17150
   295
paulson@15347
   296
paulson@17317
   297
(** Some Clause destructor functions **)
paulson@17317
   298
paulson@17317
   299
fun string_of_kind (Clause cls) = name_of_kind (#kind cls);
paulson@17317
   300
paulson@17317
   301
fun get_axiomName (Clause cls) = #axiom_name cls;
paulson@17317
   302
paulson@17317
   303
fun get_clause_id (Clause cls) = #clause_id cls;
paulson@17317
   304
paulson@17317
   305
fun funcs_of_cls (Clause cls) = #functions cls;
paulson@17317
   306
paulson@17317
   307
fun preds_of_cls (Clause cls) = #predicates cls;
paulson@17317
   308
paulson@17317
   309
paulson@18218
   310
(*Declarations of the current theory--to allow suppressing types.*)
paulson@18218
   311
val const_typargs = ref (Library.K [] : (string*typ -> typ list));
paulson@17317
   312
paulson@18218
   313
fun num_typargs(s,T) = if !keep_types then length (!const_typargs (s,T)) else 0;
paulson@16925
   314
paulson@16925
   315
(*Initialize the type suppression mechanism with the current theory before
paulson@16925
   316
    producing any clauses!*)
paulson@18218
   317
fun init thy = (const_typargs := Sign.const_typargs thy);
paulson@16925
   318
    
quigley@17150
   319
mengj@18402
   320
(*Flatten a type to a fol_type while accumulating sort constraints on the TFrees and
paulson@16925
   321
  TVars it contains.*)    
paulson@18218
   322
fun type_of (Type (a, Ts)) = 
paulson@18218
   323
      let val (folTyps, (ts, funcs)) = types_of Ts 
paulson@18218
   324
	  val t = make_fixed_type_const a
paulson@18218
   325
      in    
mengj@18402
   326
	  (Comp(t,folTyps), (ts, (t, length Ts)::funcs))
paulson@18218
   327
      end
paulson@18218
   328
  | type_of (TFree (a,s)) = 
paulson@18218
   329
      let val t = make_fixed_type_var a
mengj@18402
   330
      in (AtomF(t), ([((FOLTFree a),s)], [(t,0)])) end
mengj@18402
   331
  | type_of (TVar (v, s)) = (AtomV(make_schematic_type_var v), ([((FOLTVar v),s)], []))
paulson@18218
   332
and types_of Ts =
paulson@17230
   333
      let val foltyps_ts = map type_of Ts 
paulson@17230
   334
	  val (folTyps,ts_funcs) = ListPair.unzip foltyps_ts
paulson@17230
   335
	  val (ts, funcslist) = ListPair.unzip ts_funcs
paulson@17230
   336
      in    
paulson@18218
   337
	  (folTyps, (union_all ts, union_all funcslist))
paulson@18218
   338
      end;
quigley@17150
   339
paulson@15390
   340
paulson@18218
   341
fun const_types_of (c,T) = types_of (!const_typargs (c,T));
paulson@16925
   342
paulson@16903
   343
(* Any variables created via the METAHYPS tactical should be treated as
paulson@16903
   344
   universal vars, although it is represented as "Free(...)" by Isabelle *)
paulson@16903
   345
val isMeta = String.isPrefix "METAHYP1_"
quigley@17150
   346
paulson@17230
   347
fun pred_name_type (Const(c,T)) = 
paulson@18218
   348
      let val (contys,(folTyps,funcs)) = const_types_of (c,T)
paulson@18218
   349
      in (make_fixed_const c, (contys,folTyps), funcs) end
paulson@15390
   350
  | pred_name_type (Free(x,T))  = 
paulson@17404
   351
      if isMeta x then raise CLAUSE("Predicate Not First Order 1", Free(x,T)) 
paulson@18218
   352
      else (make_fixed_var x, ([],[]), [])
paulson@17404
   353
  | pred_name_type (v as Var _) = raise CLAUSE("Predicate Not First Order 2", v)
paulson@17404
   354
  | pred_name_type t        = raise CLAUSE("Predicate input unexpected", t);
paulson@15347
   355
paulson@15615
   356
paulson@18218
   357
(* For typed equality *)
paulson@15615
   358
(* here "arg_typ" is the type of "="'s argument's type, not the type of the equality *)
paulson@15615
   359
(* Find type of equality arg *)
paulson@15615
   360
fun eq_arg_type (Type("fun",[T,_])) = 
paulson@15615
   361
    let val (folT,_) = type_of T;
paulson@17230
   362
    in  folT  end;
paulson@15347
   363
paulson@18218
   364
fun fun_name_type (Const("op =",T)) args =   (*FIXME: Is this special treatment of = needed??*)
paulson@18218
   365
      let val t = make_fixed_const "op ="
paulson@18218
   366
      in (t, ([eq_arg_type T], []), [(t,2)]) end
paulson@18218
   367
  | fun_name_type (Const(c,T)) args = 
paulson@17230
   368
      let val t = make_fixed_const c
paulson@18218
   369
	  val (contys, (folTyps,funcs)) = const_types_of (c,T)
paulson@18218
   370
	  val arity = num_typargs(c,T) + length args
paulson@17230
   371
      in
paulson@18218
   372
	  (t, (contys,folTyps), ((t,arity)::funcs))
paulson@17230
   373
      end
paulson@17230
   374
 | fun_name_type (Free(x,T)) args  = 
paulson@17230
   375
      let val t = make_fixed_var x
paulson@18218
   376
      in  (t, ([],[]), [(t, length args)]) end
paulson@17404
   377
  | fun_name_type f args = raise CLAUSE("Function Not First Order 1", f);
quigley@17150
   378
paulson@15615
   379
paulson@15347
   380
fun term_of (Var(ind_nm,T)) = 
paulson@17230
   381
      let val (folType,(ts,funcs)) = type_of T
paulson@17230
   382
      in
paulson@17230
   383
	  (UVar(make_schematic_var ind_nm, folType), (ts, funcs))
paulson@17230
   384
      end
paulson@15347
   385
  | term_of (Free(x,T)) = 
paulson@17404
   386
      let val (folType, (ts,funcs)) = type_of T
paulson@17230
   387
      in
paulson@17230
   388
	  if isMeta x then (UVar(make_schematic_var(x,0),folType),
paulson@17230
   389
			    (ts, ((make_schematic_var(x,0)),0)::funcs))
paulson@17230
   390
	  else
paulson@18218
   391
	      (Fun(make_fixed_var x, [folType], []), 
paulson@17404
   392
	       (ts, ((make_fixed_var x),0)::funcs))
paulson@17230
   393
      end
paulson@17230
   394
  | term_of (Const(c,T)) =  (* impossible to be equality *)
paulson@18218
   395
      let val (contys, (folTyps,funcs)) = const_types_of (c,T)
paulson@17404
   396
      in
paulson@18218
   397
	  (Fun(make_fixed_const c, contys, []),
paulson@18218
   398
	   (folTyps, ((make_fixed_const c),0)::funcs))
paulson@17230
   399
      end    
paulson@18218
   400
  | term_of app = 
paulson@17230
   401
      let val (f,args) = strip_comb app
paulson@18218
   402
          val _ = case f of Const(_,_) => ()
paulson@18218
   403
			  | Free(s,_)  => 
paulson@18218
   404
			      if isMeta s 
paulson@18218
   405
			      then raise CLAUSE("Function Not First Order 2", f)
paulson@18218
   406
			      else ()
paulson@18218
   407
			  | _ => raise CLAUSE("Function Not First Order 3", f);
paulson@18218
   408
	  val (funName,(contys,ts1),funcs) = fun_name_type f args
paulson@18218
   409
	  val (args',(ts2,funcs')) = terms_of args
paulson@17230
   410
      in
paulson@18218
   411
	  (Fun(funName,contys,args'), 
paulson@18218
   412
	   (union_all (ts1::ts2), 
paulson@18218
   413
	    union_all(funcs::funcs')))
paulson@17230
   414
      end
paulson@18218
   415
  | term_of t = raise CLAUSE("Function Not First Order 4", t)
paulson@18218
   416
and terms_of ts =  
paulson@18218
   417
      let val (args, ts_funcs) = ListPair.unzip (map term_of ts)
paulson@18218
   418
      in
paulson@18218
   419
	  (args, ListPair.unzip ts_funcs)
paulson@18218
   420
      end
paulson@15390
   421
paulson@15347
   422
paulson@17404
   423
fun pred_of (Const("op =", typ), args) =
paulson@17404
   424
      let val arg_typ = eq_arg_type typ 
paulson@18218
   425
	  val (args',(ts,funcs)) = terms_of args
paulson@17404
   426
	  val equal_name = make_fixed_const "op ="
paulson@17404
   427
      in
paulson@18218
   428
	  (Predicate(equal_name,[arg_typ],args'),
paulson@17775
   429
	   union_all ts, 
paulson@17404
   430
	   [((make_fixed_var equal_name), 2)], 
paulson@17775
   431
	   union_all funcs)
paulson@17404
   432
      end
paulson@17404
   433
  | pred_of (pred,args) = 
paulson@17404
   434
      let val (predName,(predType,ts1), pfuncs) = pred_name_type pred
paulson@18218
   435
	  val (args',(ts2,ffuncs)) = terms_of args
paulson@17775
   436
	  val ts3 = union_all (ts1::ts2)
paulson@17775
   437
	  val ffuncs' = union_all ffuncs
paulson@17888
   438
	  val newfuncs = pfuncs union ffuncs'
paulson@17404
   439
	  val arity = 
paulson@17404
   440
	    case pred of
paulson@18218
   441
		Const (c,T) => num_typargs(c,T) + length args
paulson@17404
   442
	      | _ => length args
paulson@17404
   443
      in
paulson@17404
   444
	  (Predicate(predName,predType,args'), ts3, 
paulson@17404
   445
	   [(predName, arity)], newfuncs)
paulson@17404
   446
      end;
paulson@15347
   447
paulson@15347
   448
paulson@17404
   449
(*Treatment of literals, possibly negated or tagged*)
paulson@17404
   450
fun predicate_of ((Const("Not",_) $ P), polarity, tag) =
paulson@17404
   451
      predicate_of (P, not polarity, tag)
paulson@17404
   452
  | predicate_of ((Const("HOL.tag",_) $ P), polarity, tag) =
paulson@17404
   453
      predicate_of (P, polarity, true)
paulson@17404
   454
  | predicate_of (term,polarity,tag) =
paulson@17404
   455
        (pred_of (strip_comb term), polarity, tag);
paulson@15347
   456
paulson@17888
   457
fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
paulson@17888
   458
  | literals_of_term1 (args as (lits, ts, preds, funcs)) (Const("op |",_) $ P $ Q) = 
paulson@17888
   459
      let val (lits', ts', preds', funcs') = literals_of_term1 args P
paulson@17234
   460
      in
paulson@17888
   461
	  literals_of_term1 (lits', ts', preds' union preds, funcs' union funcs) Q
paulson@17234
   462
      end
paulson@17888
   463
  | literals_of_term1 (lits, ts, preds, funcs) P =
paulson@17888
   464
      let val ((pred, ts', preds', funcs'), pol, tag) = predicate_of (P,true,false)
paulson@17404
   465
	  val lits' = Literal(pol,pred,tag) :: lits
paulson@17234
   466
      in
paulson@17888
   467
	  (lits', ts union ts', preds' union preds, funcs' union funcs)
paulson@17234
   468
      end;
quigley@17150
   469
quigley@17150
   470
paulson@17888
   471
val literals_of_term = literals_of_term1 ([],[],[],[]);
quigley@17150
   472
quigley@17150
   473
mengj@18403
   474
mengj@18403
   475
fun list_ord _ ([],[]) = EQUAL
mengj@18403
   476
  | list_ord _ ([],_) = LESS
mengj@18403
   477
  | list_ord _ (_,[]) = GREATER
mengj@18403
   478
  | list_ord ord (x::xs, y::ys) =
mengj@18403
   479
    let val xy_ord = ord(x,y)
mengj@18403
   480
    in
mengj@18403
   481
	case xy_ord of EQUAL => list_ord ord (xs,ys)
mengj@18403
   482
		     | _ => xy_ord
mengj@18403
   483
    end;
mengj@18403
   484
mengj@18403
   485
fun type_ord (AtomV(_),AtomV(_)) = EQUAL
mengj@18403
   486
  | type_ord (AtomV(_),_) = LESS
mengj@18403
   487
  | type_ord (AtomF(_),AtomV(_)) = GREATER
mengj@18403
   488
  | type_ord (AtomF(f1),AtomF(f2)) = string_ord (f1,f2)
mengj@18403
   489
  | type_ord (AtomF(_),_) = LESS
mengj@18403
   490
  | type_ord (Comp(_,_),AtomV(_)) = GREATER
mengj@18403
   491
  | type_ord (Comp(_,_),AtomF(_)) = GREATER
mengj@18403
   492
  | type_ord (Comp(con1,args1),Comp(con2,args2)) = 
mengj@18403
   493
    let val con_ord = string_ord(con1,con2)
mengj@18403
   494
    in
mengj@18403
   495
	case con_ord of EQUAL => types_ord (args1,args2)
mengj@18403
   496
		      | _ => con_ord
mengj@18403
   497
    end
mengj@18403
   498
and
mengj@18403
   499
mengj@18403
   500
types_ord ([],[]) = EQUAL
mengj@18403
   501
  | types_ord (tps1,tps2) = list_ord type_ord (tps1,tps2);
mengj@18403
   502
mengj@18402
   503
mengj@18403
   504
fun term_ord (UVar(_,_),UVar(_,_)) = EQUAL
mengj@18403
   505
  | term_ord (UVar(_,_),_) = LESS
mengj@18403
   506
  | term_ord (Fun(_,_,_),UVar(_)) = GREATER
mengj@18403
   507
  | term_ord (Fun(f1,tps1,tms1),Fun(f2,tps2,tms2)) = 
mengj@18403
   508
    let val fn_ord = string_ord (f1,f2)
mengj@18403
   509
    in
mengj@18403
   510
	case fn_ord of EQUAL => 
mengj@18403
   511
		       let val tms_ord = terms_ord (tms1,tms2)
mengj@18403
   512
		       in
mengj@18403
   513
			   case tms_ord of EQUAL => types_ord (tps1,tps2)
mengj@18403
   514
					 | _ => tms_ord
mengj@18403
   515
		       end
mengj@18403
   516
		     | _ => fn_ord
mengj@18403
   517
    end
mengj@18403
   518
mengj@18403
   519
and
mengj@18403
   520
mengj@18403
   521
terms_ord ([],[]) = EQUAL
mengj@18403
   522
  | terms_ord (tms1,tms2) = list_ord term_ord (tms1,tms2);
mengj@18403
   523
mengj@18403
   524
mengj@18403
   525
mengj@18403
   526
fun predicate_ord (Predicate(predname1,ftyps1,ftms1),Predicate(predname2,ftyps2,ftms2)) = 
mengj@18403
   527
    let val predname_ord = string_ord (predname1,predname2)
mengj@18403
   528
    in
mengj@18403
   529
	case predname_ord of EQUAL => 
mengj@18403
   530
			     let val ftms_ord = terms_ord(ftms1,ftms2)
mengj@18403
   531
			     in
mengj@18403
   532
				 case ftms_ord of EQUAL => types_ord(ftyps1,ftyps2)
mengj@18403
   533
						| _ => ftms_ord
mengj@18403
   534
			     end
mengj@18403
   535
			   | _ => predname_ord
mengj@18403
   536
    end;
mengj@18402
   537
mengj@18402
   538
fun literal_ord (Literal(false,_,_),Literal(true,_,_)) = LESS
mengj@18402
   539
  | literal_ord (Literal(true,_,_),Literal(false,_,_)) = GREATER
mengj@18402
   540
  | literal_ord (Literal(_,pred1,_),Literal(_,pred2,_)) = predicate_ord(pred1,pred2);
mengj@18402
   541
mengj@18402
   542
fun sort_lits lits = sort literal_ord lits;
mengj@18402
   543
mengj@18409
   544
(********** clause equivalence ******************)
mengj@18409
   545
mengj@18409
   546
fun check_var_pairs (x,y) [] = 0 
mengj@18409
   547
  | check_var_pairs (x,y) ((u,v)::w) =
mengj@18409
   548
    if (x,y) = (u,v) then 1 
mengj@18409
   549
    else
mengj@18409
   550
	if (x = u) orelse (y = v) then 2 (*conflict*)
mengj@18409
   551
	else check_var_pairs (x,y) w;
mengj@18409
   552
mengj@18409
   553
mengj@18409
   554
fun type_eq (AtomV(v1),AtomV(v2)) (vars,tvars) =
mengj@18409
   555
    (case check_var_pairs (v1,v2) tvars of 0 => (true,(vars,(v1,v2)::tvars))
mengj@18409
   556
					 | 1 => (true,(vars,tvars))
mengj@18409
   557
					 | 2 => (false,(vars,tvars)))
mengj@18409
   558
  | type_eq (AtomV(_),_) vtvars = (false,vtvars)
mengj@18409
   559
  | type_eq (AtomF(f1),AtomF(f2)) vtvars = (f1=f2,vtvars)
mengj@18409
   560
  | type_eq (AtomF(_),_) vtvars = (false,vtvars)
mengj@18409
   561
  | type_eq (Comp(con1,args1),Comp(con2,args2)) vtvars =
mengj@18409
   562
    let val (eq1,vtvars1) = 
mengj@18409
   563
	    if (con1 = con2) then types_eq (args1,args2) vtvars
mengj@18409
   564
	    else (false,vtvars)
mengj@18409
   565
    in
mengj@18409
   566
	(eq1,vtvars1)
mengj@18409
   567
    end
mengj@18409
   568
  | type_eq (Comp(_,_),_) vtvars = (false,vtvars)
mengj@18409
   569
mengj@18409
   570
and
mengj@18409
   571
mengj@18409
   572
types_eq ([],[]) vtvars = (true,vtvars)
mengj@18409
   573
| types_eq (tp1::tps1,tp2::tps2) vtvars =
mengj@18409
   574
  let val (eq1,vtvars1) = type_eq (tp1,tp2) vtvars
mengj@18409
   575
      val (eq2,vtvars2) = if eq1 then types_eq (tps1,tps2) vtvars1
mengj@18409
   576
			  else (eq1,vtvars1)
mengj@18409
   577
  in
mengj@18409
   578
      (eq2,vtvars2)
mengj@18409
   579
  end;
mengj@18409
   580
mengj@18409
   581
mengj@18409
   582
fun term_eq (UVar(v1,tp1),UVar(v2,tp2)) (vars,tvars) =
mengj@18409
   583
    (case check_var_pairs (v1,v2) vars of 0 => type_eq (tp1,tp2) (((v1,v2)::vars),tvars)
mengj@18409
   584
					| 1 => type_eq (tp1,tp2) (vars,tvars)
mengj@18409
   585
					| 2 => (false,(vars,tvars)))
mengj@18409
   586
  | term_eq (UVar(_,_),_) vtvars = (false,vtvars)
mengj@18409
   587
  | term_eq (Fun(f1,tps1,tms1),Fun(f2,tps2,tms2)) vtvars =
mengj@18409
   588
    let val (eq1,vtvars1) = 
mengj@18409
   589
	    if (f1 = f2) then terms_eq (tms1,tms2) vtvars
mengj@18409
   590
	    else (false,vtvars)
mengj@18409
   591
	val (eq2,vtvars2) =
mengj@18409
   592
	    if eq1 then types_eq (tps1,tps2) vtvars1
mengj@18409
   593
	    else (eq1,vtvars1)
mengj@18409
   594
    in
mengj@18409
   595
	(eq2,vtvars2)
mengj@18409
   596
    end
mengj@18409
   597
  | term_eq (Fun(_,_,_),_) vtvars = (false,vtvars)
mengj@18409
   598
mengj@18409
   599
and
mengj@18409
   600
mengj@18409
   601
terms_eq ([],[]) vtvars = (true,vtvars)
mengj@18409
   602
| terms_eq (tm1::tms1,tm2::tms2) vtvars =
mengj@18409
   603
  let val (eq1,vtvars1) = term_eq (tm1,tm2) vtvars
mengj@18409
   604
      val (eq2,vtvars2) = if eq1 then terms_eq (tms1,tms2) vtvars1
mengj@18409
   605
				 else (eq1,vtvars1)
mengj@18409
   606
  in
mengj@18409
   607
      (eq2,vtvars2)
mengj@18409
   608
  end;
mengj@18409
   609
					     
mengj@18409
   610
mengj@18409
   611
fun pred_eq (Predicate(predname1,tps1,tms1),Predicate(predname2,tps2,tms2)) vtvars =
mengj@18409
   612
    let val (eq1,vtvars1) = 
mengj@18409
   613
	    if (predname1 = predname2) then terms_eq (tms1,tms2) vtvars
mengj@18409
   614
	    else (false,vtvars)
mengj@18409
   615
	val (eq2,vtvars2) = 
mengj@18409
   616
	    if eq1 then types_eq (tps1,tps2) vtvars1
mengj@18409
   617
	    else (eq1,vtvars1)
mengj@18409
   618
    in
mengj@18409
   619
	(eq2,vtvars2)
mengj@18409
   620
    end;
mengj@18409
   621
					      
mengj@18409
   622
mengj@18409
   623
fun lit_eq (Literal(pol1,pred1,_),Literal(pol2,pred2,_)) vtvars =
mengj@18409
   624
    if (pol1 = pol2) then pred_eq (pred1,pred2) vtvars
mengj@18409
   625
    else (false,vtvars);
mengj@18409
   626
mengj@18409
   627
(*must have the same number of literals*)
mengj@18409
   628
fun lits_eq ([],[]) vtvars = (true,vtvars)
mengj@18409
   629
  | lits_eq (l1::ls1,l2::ls2) vtvars = 
mengj@18409
   630
    let val (eq1,vtvars1) = lit_eq (l1,l2) vtvars
mengj@18409
   631
    in
mengj@18409
   632
	if eq1 then lits_eq (ls1,ls2) vtvars1
mengj@18409
   633
	else (false,vtvars1)
mengj@18409
   634
    end;
mengj@18409
   635
mengj@18409
   636
mengj@18409
   637
fun cls_eq (cls1,cls2) =
mengj@18409
   638
    let val lits1 = get_literals cls1
mengj@18409
   639
	val lits2 = get_literals cls2
mengj@18409
   640
    in
mengj@18409
   641
	(length lits1 = length lits2) andalso (fst (lits_eq (lits1,lits2) ([],[])))
mengj@18409
   642
    end;
mengj@18409
   643
mengj@18409
   644
mengj@18402
   645
mengj@18402
   646
quigley@17150
   647
(* FIX: not sure what to do with these funcs *)
quigley@17150
   648
paulson@16199
   649
(*Make literals for sorted type variables*) 
quigley@17150
   650
fun sorts_on_typs (_, [])   = ([]) 
paulson@16199
   651
  | sorts_on_typs (v, "HOL.type" :: s) =
paulson@18411
   652
      sorts_on_typs (v,s)                (*IGNORE sort "type"*)
paulson@17230
   653
  | sorts_on_typs ((FOLTVar indx), (s::ss)) =
paulson@17230
   654
      LTVar((make_type_class s) ^ 
paulson@17230
   655
        "(" ^ (make_schematic_type_var indx) ^ ")") :: 
paulson@17230
   656
      (sorts_on_typs ((FOLTVar indx), ss))
paulson@17230
   657
  | sorts_on_typs ((FOLTFree x), (s::ss)) =
paulson@17230
   658
      LTFree((make_type_class s) ^ "(" ^ (make_fixed_type_var x) ^ ")") :: 
paulson@17230
   659
      (sorts_on_typs ((FOLTFree x), ss));
paulson@15347
   660
quigley@17150
   661
paulson@17312
   662
(*UGLY: seems to be parsing the "show sorts" output, removing anything that
paulson@17312
   663
  starts with a left parenthesis.*)
paulson@17312
   664
fun remove_type str = hd (String.fields (fn c => c = #"(") str);
quigley@17150
   665
quigley@17150
   666
fun pred_of_sort (LTVar x) = ((remove_type x),1)
quigley@17150
   667
|   pred_of_sort (LTFree x) = ((remove_type x),1)
quigley@17150
   668
quigley@17150
   669
quigley@17150
   670
paulson@16199
   671
(*Given a list of sorted type variables, return two separate lists.
paulson@16199
   672
  The first is for TVars, the second for TFrees.*)
quigley@17150
   673
fun add_typs_aux [] preds  = ([],[], preds)
paulson@17230
   674
  | add_typs_aux ((FOLTVar indx,s)::tss) preds = 
paulson@17230
   675
      let val vs = sorts_on_typs (FOLTVar indx, s)
quigley@17150
   676
          val preds' = (map pred_of_sort vs)@preds
quigley@17150
   677
	  val (vss,fss, preds'') = add_typs_aux tss preds'
quigley@17150
   678
      in
paulson@17775
   679
	  (vs union vss, fss, preds'')
quigley@17150
   680
      end
paulson@17230
   681
  | add_typs_aux ((FOLTFree x,s)::tss) preds  =
paulson@17230
   682
      let val fs = sorts_on_typs (FOLTFree x, s)
quigley@17150
   683
          val preds' = (map pred_of_sort fs)@preds
quigley@17150
   684
	  val (vss,fss, preds'') = add_typs_aux tss preds'
quigley@17150
   685
      in
paulson@17775
   686
	  (vss, fs union fss, preds'')
quigley@17150
   687
      end;
quigley@17150
   688
mengj@17999
   689
fun add_typs_aux2 [] = ([],[])
mengj@17999
   690
  | add_typs_aux2 ((FOLTVar indx,s)::tss) =
mengj@17999
   691
    let val vs = sorts_on_typs (FOLTVar indx,s)
mengj@17999
   692
	val (vss,fss) = add_typs_aux2 tss
mengj@17999
   693
    in
mengj@17999
   694
	(vs union vss,fss)
mengj@17999
   695
    end
mengj@17999
   696
  | add_typs_aux2 ((FOLTFree x,s)::tss) =
mengj@17999
   697
    let val fs = sorts_on_typs (FOLTFree x,s)
mengj@17999
   698
	val (vss,fss) = add_typs_aux2 tss
mengj@17999
   699
    in
mengj@17999
   700
	(vss,fs union fss)
mengj@17999
   701
    end;
mengj@17999
   702
mengj@17999
   703
quigley@17150
   704
fun add_typs (Clause cls) preds  = add_typs_aux (#types_sorts cls) preds 
paulson@15347
   705
paulson@15347
   706
paulson@15347
   707
(** make axiom clauses, hypothesis clauses and conjecture clauses. **)
quigley@17150
   708
quigley@17150
   709
fun get_tvar_strs [] = []
paulson@17230
   710
  | get_tvar_strs ((FOLTVar indx,s)::tss) = 
paulson@17230
   711
      let val vstr = make_schematic_type_var indx
quigley@17150
   712
      in
paulson@17888
   713
	  vstr ins (get_tvar_strs tss)
quigley@17150
   714
      end
paulson@17230
   715
  | get_tvar_strs((FOLTFree x,s)::tss) = distinct (get_tvar_strs tss)
quigley@17150
   716
quigley@17150
   717
(* FIX add preds and funcs to add typs aux here *)
quigley@17150
   718
paulson@17230
   719
fun make_axiom_clause_thm thm (ax_name,cls_id) =
paulson@17888
   720
    let val (lits,types_sorts, preds, funcs) = literals_of_term (prop_of thm)
mengj@18402
   721
	val lits' = sort_lits lits
quigley@17150
   722
	val (tvar_lits,tfree_lits, preds) = add_typs_aux types_sorts preds 
quigley@17150
   723
        val tvars = get_tvar_strs types_sorts
quigley@17150
   724
    in 
paulson@17230
   725
	make_clause(cls_id,ax_name,Axiom,
mengj@18402
   726
	            lits',types_sorts,tvar_lits,tfree_lits,
paulson@17230
   727
	            tvars, preds, funcs)
paulson@15347
   728
    end;
paulson@15347
   729
paulson@15347
   730
mengj@18199
   731
(* check if a clause is FOL first*)
paulson@17888
   732
fun make_conjecture_clause n t =
mengj@18199
   733
    let val _ = check_is_fol_term t
mengj@18199
   734
	    handle TERM("check_is_fol_term",_) => raise CLAUSE("Goal is not FOL",t)
mengj@18199
   735
	val (lits,types_sorts, preds, funcs) = literals_of_term t
quigley@17150
   736
	val (tvar_lits,tfree_lits, preds) = add_typs_aux types_sorts preds 
quigley@17150
   737
        val tvars = get_tvar_strs types_sorts
quigley@17150
   738
    in
paulson@17845
   739
	make_clause(n,"conjecture",Conjecture,
paulson@17230
   740
	            lits,types_sorts,tvar_lits,tfree_lits,
paulson@17230
   741
	            tvars, preds, funcs)
quigley@17150
   742
    end;
paulson@17845
   743
    
paulson@17845
   744
fun make_conjecture_clauses_aux _ [] = []
paulson@17888
   745
  | make_conjecture_clauses_aux n (t::ts) =
paulson@17888
   746
      make_conjecture_clause n t :: make_conjecture_clauses_aux (n+1) ts
paulson@17845
   747
paulson@17845
   748
val make_conjecture_clauses = make_conjecture_clauses_aux 0
quigley@17150
   749
quigley@17150
   750
mengj@18199
   751
(*before converting an axiom clause to "clause" format, check if it is FOL*)
paulson@17230
   752
fun make_axiom_clause term (ax_name,cls_id) =
mengj@18199
   753
    let val _ = check_is_fol_term term 
mengj@18199
   754
	    handle TERM("check_is_fol_term",_) => raise CLAUSE("Axiom is not FOL", term) 
mengj@18199
   755
	val (lits,types_sorts, preds,funcs) = literals_of_term term
mengj@18402
   756
	val lits' = sort_lits lits
paulson@17230
   757
	val (tvar_lits,tfree_lits, preds) = add_typs_aux types_sorts preds
quigley@17150
   758
        val tvars = get_tvar_strs types_sorts	
paulson@15347
   759
    in 
paulson@17230
   760
	make_clause(cls_id,ax_name,Axiom,
mengj@18402
   761
	            lits',types_sorts,tvar_lits,tfree_lits,
paulson@17230
   762
	            tvars, preds,funcs)
paulson@15347
   763
    end;
paulson@15347
   764
paulson@15347
   765
paulson@15347
   766
paulson@15347
   767
 
paulson@15347
   768
(**** Isabelle arities ****)
paulson@15347
   769
paulson@15347
   770
exception ARCLAUSE of string;
paulson@15347
   771
 
paulson@15347
   772
paulson@15347
   773
type class = string; 
paulson@15347
   774
type tcons = string; 
paulson@15347
   775
paulson@15347
   776
paulson@15347
   777
datatype arLit = TConsLit of bool * (class * tcons * string list) | TVarLit of bool * (class * string);
paulson@15347
   778
 
paulson@15347
   779
datatype arityClause =  
paulson@15347
   780
	 ArityClause of {clause_id: clause_id,
paulson@17845
   781
	  	         axiom_name: axiom_name,
paulson@15347
   782
			 kind: kind,
paulson@15347
   783
			 conclLit: arLit,
paulson@15347
   784
			 premLits: arLit list};
paulson@15347
   785
paulson@15347
   786
paulson@15347
   787
fun get_TVars 0 = []
paulson@17525
   788
  | get_TVars n = ("T_" ^ (Int.toString n)) :: get_TVars (n-1);
paulson@15347
   789
paulson@18411
   790
fun pack_sort(_,[])  = []
paulson@18411
   791
  | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
paulson@18411
   792
  | pack_sort(tvar, cls::srt) =  (make_type_class cls, tvar) :: pack_sort(tvar, srt);
paulson@15347
   793
    
paulson@15347
   794
fun make_TVarLit (b,(cls,str)) = TVarLit(b,(cls,str));
paulson@15347
   795
fun make_TConsLit (b,(cls,tcons,tvars)) = TConsLit(b,(make_type_class cls,make_fixed_type_const tcons,tvars));
paulson@15347
   796
paulson@18411
   797
(*Arity of type constructor tcon :: (arg1,...,argN)res*)
paulson@18411
   798
fun make_axiom_arity_clause (tcons, n, (res,args)) =
paulson@17845
   799
   let val nargs = length args
paulson@17845
   800
       val tvars = get_TVars nargs
paulson@17845
   801
       val tvars_srts = ListPair.zip (tvars,args)
paulson@17845
   802
       val tvars_srts' = union_all(map pack_sort tvars_srts)
paulson@17845
   803
       val false_tvars_srts' = map (pair false) tvars_srts'
paulson@17845
   804
   in
paulson@17845
   805
      ArityClause {clause_id = n, kind = Axiom, 
paulson@18411
   806
                   axiom_name = lookup_type_const tcons,
paulson@18411
   807
                   conclLit = make_TConsLit(true, (res,tcons,tvars)), 
paulson@17845
   808
                   premLits = map make_TVarLit false_tvars_srts'}
paulson@17845
   809
   end;
paulson@15347
   810
    
paulson@17305
   811
(*The number of clauses generated from cls, including type clauses*)
paulson@17305
   812
fun num_of_clauses (Clause cls) =
paulson@17305
   813
    let val num_tfree_lits = 
paulson@17305
   814
	      if !keep_types then length (#tfree_type_literals cls)
paulson@17305
   815
	      else 0
paulson@17305
   816
    in 	1 + num_tfree_lits  end;
paulson@15347
   817
paulson@15347
   818
paulson@15347
   819
(**** Isabelle class relations ****)
paulson@15347
   820
paulson@15347
   821
datatype classrelClause = 
paulson@15347
   822
	 ClassrelClause of {clause_id: clause_id,
paulson@15347
   823
			    subclass: class,
paulson@18411
   824
			    superclass: class};
paulson@17845
   825
paulson@17845
   826
fun make_axiom_classrelClause n subclass superclass =
paulson@17845
   827
  ClassrelClause {clause_id = n,
paulson@17845
   828
                  subclass = subclass, superclass = superclass};
paulson@15347
   829
paulson@17845
   830
fun classrelClauses_of_aux n sub [] = []
paulson@18411
   831
  | classrelClauses_of_aux n sub ("HOL.type"::sups) = (*Should be ignored*)
paulson@18411
   832
      classrelClauses_of_aux n sub sups
paulson@17845
   833
  | classrelClauses_of_aux n sub (sup::sups) =
paulson@18411
   834
      ClassrelClause {clause_id = n, subclass = sub, superclass = sup} 
paulson@18411
   835
      :: classrelClauses_of_aux (n+1) sub sups;
paulson@15347
   836
paulson@18411
   837
fun classrelClauses_of (sub,sups) = classrelClauses_of_aux 0 sub sups;
paulson@17845
   838
paulson@17845
   839
paulson@17845
   840
(***** Isabelle arities *****)
paulson@17845
   841
paulson@17845
   842
fun arity_clause _ (tcons, []) = []
paulson@18411
   843
  | arity_clause n (tcons, ("HOL.type",_)::ars) =  (*Should be ignored*)
paulson@18411
   844
      arity_clause n (tcons,ars)
paulson@17845
   845
  | arity_clause n (tcons, ar::ars) =
paulson@17845
   846
      make_axiom_arity_clause (tcons,n,ar) :: 
paulson@17845
   847
      arity_clause (n+1) (tcons,ars);
paulson@17845
   848
paulson@17845
   849
fun multi_arity_clause [] = []
paulson@17845
   850
  | multi_arity_clause (tcon_ar :: tcons_ars)  =
paulson@17845
   851
      arity_clause 0 tcon_ar  @  multi_arity_clause tcons_ars 
paulson@17845
   852
paulson@17845
   853
fun arity_clause_thy thy =
paulson@17845
   854
  let val arities = #arities (Type.rep_tsig (Sign.tsig_of thy))
paulson@17845
   855
  in multi_arity_clause (Symtab.dest arities) end;
paulson@17845
   856
paulson@17845
   857
paulson@17845
   858
(* Isabelle classes *)
paulson@17845
   859
paulson@17845
   860
type classrelClauses = classrelClause list Symtab.table;
paulson@17845
   861
paulson@17845
   862
val classrel_of = #2 o #classes o Type.rep_tsig o Sign.tsig_of;
paulson@17845
   863
fun classrel_clauses_classrel (C: Sorts.classes) = map classrelClauses_of (Graph.dest C);
paulson@17845
   864
val classrel_clauses_thy = List.concat o classrel_clauses_classrel o classrel_of;
paulson@17845
   865
paulson@15347
   866
paulson@15347
   867
paulson@15347
   868
(****!!!! Changed for typed equality !!!!****)
paulson@17230
   869
paulson@15347
   870
fun wrap_eq_type typ t = eq_typ_wrapper ^"(" ^ t ^ "," ^ typ ^ ")";
paulson@15347
   871
paulson@18218
   872
(*Only need to wrap equality's arguments with "typeinfo" if the output clauses are typed 
paulson@18218
   873
 and if we specifically ask for types to be included.   *)
paulson@15347
   874
fun string_of_equality (typ,terms) =
paulson@17230
   875
      let val [tstr1,tstr2] = map string_of_term terms
mengj@18402
   876
	  val typ' = string_of_fol_type typ
paulson@17230
   877
      in
paulson@17230
   878
	  if !keep_types andalso !special_equal 
mengj@18402
   879
	  then "equal(" ^ (wrap_eq_type typ' tstr1) ^ "," ^ 
mengj@18402
   880
		 	  (wrap_eq_type typ' tstr2) ^ ")"
paulson@17230
   881
	  else "equal(" ^ tstr1 ^ "," ^ tstr2 ^ ")"
paulson@17230
   882
      end
paulson@17230
   883
and string_of_term (UVar(x,_)) = x
paulson@18218
   884
  | string_of_term (Fun("equal",[typ],terms)) = string_of_equality(typ,terms)
paulson@18218
   885
  | string_of_term (Fun (name,typs,[])) = name (*Overloaded consts like 0 don't get types!*)
paulson@18218
   886
  | string_of_term (Fun (name,typs,terms)) = 
paulson@18218
   887
      let val terms_as_strings = map string_of_term terms
mengj@18402
   888
	  val typs' = if !keep_types then map string_of_fol_type typs else []
mengj@18275
   889
      in  name ^ (paren_pack (terms_as_strings @ typs'))  end
paulson@18218
   890
  | string_of_term _ = error "string_of_term";      
paulson@15347
   891
paulson@15347
   892
(* before output the string of the predicate, check if the predicate corresponds to an equality or not. *)
paulson@18218
   893
fun string_of_predicate (Predicate("equal",[typ],terms)) = string_of_equality(typ,terms)
paulson@18218
   894
  | string_of_predicate (Predicate(name,typs,terms)) = 
paulson@17230
   895
      let val terms_as_strings = map string_of_term terms
mengj@18402
   896
	  val typs' = if !keep_types then map string_of_fol_type typs else []
mengj@18275
   897
      in  name ^ (paren_pack (terms_as_strings @ typs'))  end
paulson@18218
   898
  | string_of_predicate _ = error "string_of_predicate";      
quigley@17150
   899
paulson@17317
   900
paulson@17317
   901
fun string_of_clausename (cls_id,ax_name) = 
paulson@17525
   902
    clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id;
paulson@17317
   903
paulson@17317
   904
fun string_of_type_clsname (cls_id,ax_name,idx) = 
paulson@17525
   905
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
quigley@17150
   906
    
quigley@17150
   907
quigley@17150
   908
(********************************)
quigley@17150
   909
(* Code for producing DFG files *)
quigley@17150
   910
(********************************)
quigley@17150
   911
quigley@17150
   912
fun dfg_literal (Literal(pol,pred,tag)) =
quigley@17150
   913
    let val pred_string = string_of_predicate pred
paulson@17234
   914
    in
paulson@17234
   915
	if pol then pred_string else "not(" ^pred_string ^ ")"  
quigley@17150
   916
    end;
quigley@17150
   917
quigley@17150
   918
quigley@17150
   919
(* FIX: what does this mean? *)
quigley@17150
   920
(*fun dfg_of_typeLit (LTVar x) = "not(" ^ x ^ ")"
quigley@17150
   921
  | dfg_of_typeLit (LTFree x) = "(" ^ x ^ ")";*)
quigley@17150
   922
quigley@17150
   923
fun dfg_of_typeLit (LTVar x) =  x 
quigley@17150
   924
  | dfg_of_typeLit (LTFree x) = x ;
quigley@17150
   925
 
paulson@17230
   926
(*Make the string of universal quantifiers for a clause*)
paulson@17230
   927
fun forall_open ([],[]) = ""
paulson@17230
   928
  | forall_open (vars,tvars) = "forall([" ^ (commas (tvars@vars))^ "],\n"
quigley@17150
   929
paulson@17230
   930
fun forall_close ([],[]) = ""
paulson@17230
   931
  | forall_close (vars,tvars) = ")"
quigley@17150
   932
paulson@17230
   933
fun gen_dfg_cls (cls_id,ax_name,knd,lits,tvars,vars) = 
paulson@17317
   934
    "clause( %(" ^ knd ^ ")\n" ^ forall_open(vars,tvars) ^ 
paulson@17317
   935
    "or(" ^ lits ^ ")" ^ forall_close(vars,tvars) ^ ",\n" ^ 
paulson@17317
   936
    string_of_clausename (cls_id,ax_name) ^  ").";
quigley@17150
   937
paulson@17317
   938
fun gen_dfg_type_cls (cls_id,ax_name,knd,tfree_lit,idx,tvars,vars) = 
paulson@17317
   939
    "clause( %(" ^ knd ^ ")\n" ^ forall_open(vars,tvars) ^ 
paulson@17317
   940
    "or( " ^ tfree_lit ^ ")" ^ forall_close(vars,tvars) ^ ",\n" ^ 
paulson@17317
   941
    string_of_type_clsname (cls_id,ax_name,idx) ^  ").";
quigley@17150
   942
quigley@17150
   943
fun dfg_clause_aux (Clause cls) = 
paulson@17230
   944
  let val lits = map dfg_literal (#literals cls)
paulson@17230
   945
      val tvar_lits_strs = 
paulson@17230
   946
	  if !keep_types then map dfg_of_typeLit (#tvar_type_literals cls) 
paulson@17230
   947
	  else []
paulson@17230
   948
      val tfree_lits =
paulson@17230
   949
          if !keep_types then map dfg_of_typeLit (#tfree_type_literals cls)
paulson@17230
   950
          else []
paulson@17230
   951
  in
paulson@17234
   952
      (tvar_lits_strs @ lits, tfree_lits)
paulson@17230
   953
  end; 
quigley@17150
   954
quigley@17150
   955
quigley@17150
   956
fun dfg_folterms (Literal(pol,pred,tag)) = 
paulson@18218
   957
  let val Predicate (predname, _, folterms) = pred
paulson@18218
   958
  in  folterms  end
quigley@17150
   959
quigley@17150
   960
 
paulson@17404
   961
fun get_uvars (UVar(a,typ)) = [a] 
paulson@17775
   962
|   get_uvars (Fun (_,typ,tlist)) = union_all(map get_uvars tlist)
quigley@17150
   963
quigley@17150
   964
paulson@17404
   965
fun is_uvar (UVar _) = true
paulson@17404
   966
|   is_uvar (Fun _) = false;
paulson@17404
   967
paulson@17404
   968
fun uvar_name (UVar(a,_)) = a
paulson@17404
   969
|   uvar_name (Fun (a,_,_)) = raise CLAUSE("Not a variable", Const(a,dummyT));
paulson@17404
   970
quigley@17150
   971
fun dfg_vars (Clause cls) =
paulson@17404
   972
    let val lits = #literals cls
paulson@18218
   973
        val folterms = List.concat (map dfg_folterms lits)
quigley@17150
   974
    in 
paulson@17775
   975
        union_all(map get_uvars folterms)
quigley@17150
   976
    end
quigley@17150
   977
quigley@17150
   978
quigley@17150
   979
fun dfg_tvars (Clause cls) =(#tvars cls)
quigley@17150
   980
quigley@17150
   981
quigley@17150
   982
	
quigley@17150
   983
(* make this return funcs and preds too? *)
paulson@18218
   984
fun string_of_predname (Predicate("equal",_,terms)) = "EQUALITY"
paulson@18218
   985
  | string_of_predname (Predicate(name,_,terms)) = name
quigley@17150
   986
    
quigley@17150
   987
	
quigley@17150
   988
quigley@17150
   989
fun concat_with sep []  = ""
quigley@17150
   990
  | concat_with sep [x] = "(" ^ x ^ ")"
quigley@17150
   991
  | concat_with sep (x::xs) = "(" ^ x ^ ")" ^  sep ^ (concat_with sep xs);
quigley@17150
   992
paulson@17234
   993
fun dfg_pred (Literal(pol,pred,tag)) ax_name = 
paulson@17234
   994
    (string_of_predname pred) ^ " " ^ ax_name
quigley@17150
   995
quigley@17150
   996
fun dfg_clause cls =
paulson@17230
   997
    let val (lits,tfree_lits) = dfg_clause_aux cls 
paulson@17230
   998
             (*"lits" includes the typing assumptions (TVars)*)
quigley@17150
   999
        val vars = dfg_vars cls
quigley@17150
  1000
        val tvars = dfg_tvars cls
quigley@17150
  1001
	val knd = string_of_kind cls
paulson@17234
  1002
	val lits_str = commas lits
paulson@17317
  1003
	val cls_id = get_clause_id cls
paulson@17317
  1004
	val axname = get_axiomName cls
paulson@17317
  1005
	val cls_str = gen_dfg_cls(cls_id,axname,knd,lits_str,tvars, vars) 			
quigley@17150
  1006
        fun typ_clss k [] = []
quigley@17150
  1007
          | typ_clss k (tfree :: tfrees) = 
paulson@17317
  1008
              (gen_dfg_type_cls(cls_id,axname,knd,tfree,k, tvars,vars)) :: 
paulson@17234
  1009
              (typ_clss (k+1) tfrees)
quigley@17150
  1010
    in 
quigley@17150
  1011
	cls_str :: (typ_clss 0 tfree_lits)
quigley@17150
  1012
    end;
quigley@17150
  1013
paulson@17525
  1014
fun string_of_arity (name, num) =  name ^ "," ^ (Int.toString num) 
quigley@17150
  1015
paulson@17234
  1016
fun string_of_preds preds = 
paulson@17234
  1017
  "predicates[" ^ (concat_with ", " (map string_of_arity preds)) ^ "].\n";
quigley@17150
  1018
paulson@17234
  1019
fun string_of_funcs funcs =
paulson@17234
  1020
  "functions[" ^ (concat_with ", " (map string_of_arity funcs)) ^ "].\n" ;
quigley@17150
  1021
quigley@17150
  1022
paulson@17234
  1023
fun string_of_symbols predstr funcstr = 
paulson@17234
  1024
  "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
quigley@17150
  1025
quigley@17150
  1026
paulson@17234
  1027
fun string_of_axioms axstr = 
paulson@17234
  1028
  "list_of_clauses(axioms,cnf).\n" ^ axstr ^ "end_of_list.\n\n";
quigley@17150
  1029
quigley@17150
  1030
paulson@17234
  1031
fun string_of_conjectures conjstr = 
paulson@17234
  1032
  "list_of_clauses(conjectures,cnf).\n" ^ conjstr ^ "end_of_list.\n\n";
quigley@17150
  1033
paulson@17234
  1034
fun string_of_descrip () = 
paulson@17234
  1035
  "list_of_descriptions.\nname({*[ File     : ],[ Names    :]*}).\nauthor({*[ Source   :]*}).\nstatus(unknown).\ndescription({*[ Refs     :]*}).\nend_of_list.\n\n"
quigley@17150
  1036
quigley@17150
  1037
quigley@17150
  1038
fun string_of_start name = "%------------------------------------------------------------------------------\nbegin_problem(" ^ name ^ ").\n\n";
quigley@17150
  1039
quigley@17150
  1040
quigley@17150
  1041
fun string_of_end () = "end_problem.\n%------------------------------------------------------------------------------";
quigley@17150
  1042
quigley@17150
  1043
quigley@17150
  1044
fun clause2dfg cls =
paulson@17234
  1045
    let val (lits,tfree_lits) = dfg_clause_aux cls 
paulson@17234
  1046
            (*"lits" includes the typing assumptions (TVars)*)
paulson@17317
  1047
	val cls_id = get_clause_id cls
paulson@17317
  1048
	val ax_name = get_axiomName cls
quigley@17150
  1049
        val vars = dfg_vars cls
quigley@17150
  1050
        val tvars = dfg_tvars cls
quigley@17150
  1051
        val funcs = funcs_of_cls cls
quigley@17150
  1052
        val preds = preds_of_cls cls
quigley@17150
  1053
	val knd = string_of_kind cls
paulson@17234
  1054
	val lits_str = commas lits
quigley@17150
  1055
	val cls_str = gen_dfg_cls(cls_id,ax_name,knd,lits_str,tvars,vars) 
quigley@17150
  1056
    in
quigley@17150
  1057
	(cls_str,tfree_lits) 
quigley@17150
  1058
    end;
quigley@17150
  1059
quigley@17150
  1060
quigley@17150
  1061
paulson@17234
  1062
fun tfree_dfg_clause tfree_lit =
paulson@17422
  1063
  "clause( %(conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ")."
quigley@17150
  1064
quigley@17150
  1065
paulson@17422
  1066
fun gen_dfg_file probname axioms conjectures funcs preds = 
paulson@17230
  1067
    let val axstrs_tfrees = (map clause2dfg axioms)
quigley@17150
  1068
	val (axstrs, atfrees) = ListPair.unzip axstrs_tfrees
paulson@17764
  1069
        val axstr = (space_implode "\n" axstrs) ^ "\n\n"
paulson@17230
  1070
        val conjstrs_tfrees = (map clause2dfg conjectures)
quigley@17150
  1071
	val (conjstrs, atfrees) = ListPair.unzip conjstrs_tfrees
paulson@17775
  1072
        val tfree_clss = map tfree_dfg_clause (union_all atfrees) 
paulson@17764
  1073
        val conjstr = (space_implode "\n" (tfree_clss@conjstrs)) ^ "\n\n"
quigley@17150
  1074
        val funcstr = string_of_funcs funcs
quigley@17150
  1075
        val predstr = string_of_preds preds
quigley@17150
  1076
    in
paulson@17230
  1077
       (string_of_start probname) ^ (string_of_descrip ()) ^ 
paulson@17764
  1078
       (string_of_symbols funcstr predstr) ^  
paulson@17230
  1079
       (string_of_axioms axstr) ^
paulson@17230
  1080
       (string_of_conjectures conjstr) ^ (string_of_end ())
quigley@17150
  1081
    end;
quigley@17150
  1082
   
paulson@17422
  1083
fun clauses2dfg [] probname axioms conjectures funcs preds = 
paulson@17775
  1084
      let val funcs' = (union_all(map funcs_of_cls axioms)) @ funcs
paulson@17775
  1085
	  val preds' = (union_all(map preds_of_cls axioms)) @ preds
paulson@17230
  1086
      in
paulson@17422
  1087
	 gen_dfg_file probname axioms conjectures funcs' preds' 
paulson@17230
  1088
      end
paulson@17422
  1089
 | clauses2dfg (cls::clss) probname axioms conjectures funcs preds = 
paulson@17230
  1090
     let val (lits,tfree_lits) = dfg_clause_aux cls
paulson@17230
  1091
	       (*"lits" includes the typing assumptions (TVars)*)
paulson@17317
  1092
	 val cls_id = get_clause_id cls
paulson@17317
  1093
	 val ax_name = get_axiomName cls
paulson@17230
  1094
	 val vars = dfg_vars cls
paulson@17230
  1095
	 val tvars = dfg_tvars cls
paulson@17888
  1096
	 val funcs' = (funcs_of_cls cls) union funcs
paulson@17888
  1097
	 val preds' = (preds_of_cls cls) union preds
paulson@17230
  1098
	 val knd = string_of_kind cls
paulson@17230
  1099
	 val lits_str = concat_with ", " lits
paulson@17230
  1100
	 val axioms' = if knd = "axiom" then (cls::axioms) else axioms
paulson@17230
  1101
	 val conjectures' = 
paulson@17230
  1102
	     if knd = "conjecture" then (cls::conjectures) else conjectures
paulson@17230
  1103
     in
paulson@17422
  1104
	 clauses2dfg clss probname axioms' conjectures' funcs' preds' 
paulson@17230
  1105
     end;
quigley@17150
  1106
quigley@17150
  1107
paulson@17845
  1108
fun string_of_arClauseID (ArityClause {clause_id,axiom_name,...}) =
paulson@17845
  1109
    arclause_prefix ^ ascii_of axiom_name ^ "_" ^ Int.toString clause_id;
quigley@17150
  1110
quigley@17150
  1111
fun string_of_arKind (ArityClause arcls) = name_of_kind(#kind arcls);
quigley@17150
  1112
paulson@17525
  1113
(*FIXME!!! currently is TPTP format!*)
paulson@17525
  1114
fun dfg_of_arLit (TConsLit(b,(c,t,args))) =
paulson@17525
  1115
      let val pol = if b then "++" else "--"
paulson@18218
  1116
	  val arg_strs = paren_pack args
paulson@17525
  1117
      in 
paulson@17525
  1118
	  pol ^ c ^ "(" ^ t ^ arg_strs ^ ")"
paulson@17525
  1119
      end
paulson@17525
  1120
  | dfg_of_arLit (TVarLit(b,(c,str))) =
paulson@17525
  1121
      let val pol = if b then "++" else "--"
paulson@17525
  1122
      in
paulson@17525
  1123
	  pol ^ c ^ "(" ^ str ^ ")"
paulson@17525
  1124
      end;
paulson@17525
  1125
    
paulson@17525
  1126
paulson@17525
  1127
fun dfg_of_conclLit (ArityClause arcls) = dfg_of_arLit (#conclLit arcls);
paulson@17525
  1128
     
paulson@17525
  1129
paulson@17525
  1130
fun dfg_of_premLits (ArityClause arcls) = map dfg_of_arLit (#premLits arcls);
paulson@17525
  1131
		
paulson@17525
  1132
paulson@17525
  1133
paulson@17525
  1134
(*FIXME: would this have variables in a forall? *)
quigley@17150
  1135
quigley@17150
  1136
fun dfg_arity_clause arcls = 
paulson@17525
  1137
  let val arcls_id = string_of_arClauseID arcls
paulson@17525
  1138
      val concl_lit = dfg_of_conclLit arcls
paulson@17525
  1139
      val prems_lits = dfg_of_premLits arcls
paulson@17525
  1140
      val knd = string_of_arKind arcls
paulson@17525
  1141
      val all_lits = concl_lit :: prems_lits
paulson@17525
  1142
  in
paulson@17525
  1143
      "clause( %(" ^ knd ^ ")\n" ^  "or( " ^ (bracket_pack all_lits) ^ ")),\n" ^
paulson@17525
  1144
       arcls_id ^  ")."
paulson@17525
  1145
  end;
quigley@17150
  1146
quigley@17150
  1147
quigley@17150
  1148
(********************************)
quigley@17150
  1149
(* code to produce TPTP files   *)
quigley@17150
  1150
(********************************)
quigley@17150
  1151
paulson@15347
  1152
fun tptp_literal (Literal(pol,pred,tag)) =
paulson@15347
  1153
    let val pred_string = string_of_predicate pred
paulson@17230
  1154
	val tagged_pol = 
paulson@17230
  1155
	      if (tag andalso !tagged) then (if pol then "+++" else "---")
paulson@17230
  1156
	      else (if pol then "++" else "--")
paulson@15347
  1157
     in
paulson@15347
  1158
	tagged_pol ^ pred_string
paulson@15347
  1159
    end;
paulson@15347
  1160
paulson@15347
  1161
fun tptp_of_typeLit (LTVar x) = "--" ^ x
paulson@15347
  1162
  | tptp_of_typeLit (LTFree x) = "++" ^ x;
paulson@15347
  1163
 
paulson@15347
  1164
paulson@15347
  1165
fun gen_tptp_cls (cls_id,ax_name,knd,lits) = 
paulson@17317
  1166
    "input_clause(" ^ string_of_clausename (cls_id,ax_name) ^ "," ^ 
paulson@17317
  1167
    knd ^ "," ^ lits ^ ").";
paulson@15347
  1168
paulson@17317
  1169
fun gen_tptp_type_cls (cls_id,ax_name,knd,tfree_lit,idx) = 
paulson@17317
  1170
    "input_clause(" ^ string_of_type_clsname (cls_id,ax_name,idx) ^ "," ^ 
paulson@17230
  1171
    knd ^ ",[" ^ tfree_lit ^ "]).";
paulson@15347
  1172
paulson@17422
  1173
fun tptp_type_lits (Clause cls) = 
paulson@15347
  1174
    let val lits = map tptp_literal (#literals cls)
paulson@17230
  1175
	val tvar_lits_strs =
paulson@17230
  1176
	      if !keep_types 
paulson@17230
  1177
	      then (map tptp_of_typeLit (#tvar_type_literals cls)) 
paulson@17230
  1178
	      else []
paulson@17230
  1179
	val tfree_lits = 
paulson@17230
  1180
	      if !keep_types
paulson@17230
  1181
	      then (map tptp_of_typeLit (#tfree_type_literals cls)) 
paulson@17230
  1182
	      else []
paulson@15347
  1183
    in
paulson@17305
  1184
	(tvar_lits_strs @ lits, tfree_lits)
paulson@15347
  1185
    end; 
paulson@15347
  1186
paulson@15347
  1187
fun tptp_clause cls =
paulson@17422
  1188
    let val (lits,tfree_lits) = tptp_type_lits cls 
paulson@17305
  1189
            (*"lits" includes the typing assumptions (TVars)*)
paulson@17317
  1190
	val cls_id = get_clause_id cls
paulson@17317
  1191
	val ax_name = get_axiomName cls
paulson@15347
  1192
	val knd = string_of_kind cls
paulson@17525
  1193
	val lits_str = bracket_pack lits
paulson@17305
  1194
	val cls_str = gen_tptp_cls(cls_id,ax_name,knd,lits_str) 			 
paulson@17305
  1195
	fun typ_clss k [] = []
paulson@15347
  1196
          | typ_clss k (tfree :: tfrees) = 
paulson@17317
  1197
              gen_tptp_type_cls(cls_id,ax_name,knd,tfree,k) :: 
paulson@17317
  1198
              typ_clss (k+1) tfrees
paulson@15347
  1199
    in 
paulson@15347
  1200
	cls_str :: (typ_clss 0 tfree_lits)
paulson@15347
  1201
    end;
paulson@15347
  1202
paulson@15608
  1203
fun clause2tptp cls =
paulson@17422
  1204
    let val (lits,tfree_lits) = tptp_type_lits cls 
paulson@17305
  1205
            (*"lits" includes the typing assumptions (TVars)*)
paulson@17317
  1206
	val cls_id = get_clause_id cls
paulson@17317
  1207
	val ax_name = get_axiomName cls
paulson@15608
  1208
	val knd = string_of_kind cls
paulson@17525
  1209
	val lits_str = bracket_pack lits
paulson@15608
  1210
	val cls_str = gen_tptp_cls(cls_id,ax_name,knd,lits_str) 
paulson@15608
  1211
    in
paulson@15608
  1212
	(cls_str,tfree_lits) 
paulson@15608
  1213
    end;
paulson@15608
  1214
paulson@15608
  1215
paulson@17230
  1216
fun tfree_clause tfree_lit =
paulson@17230
  1217
    "input_clause(" ^ "tfree_tcs," ^ "conjecture" ^ ",[" ^ tfree_lit ^ "]).";
paulson@15608
  1218
paulson@15347
  1219
paulson@17525
  1220
fun tptp_of_arLit (TConsLit(b,(c,t,args))) =
paulson@17525
  1221
      let val pol = if b then "++" else "--"
paulson@18218
  1222
	  val  arg_strs = paren_pack args
paulson@17525
  1223
      in 
paulson@17525
  1224
	  pol ^ c ^ "(" ^ t ^ arg_strs ^ ")"
paulson@17525
  1225
      end
paulson@17525
  1226
  | tptp_of_arLit (TVarLit(b,(c,str))) =
paulson@17525
  1227
      let val pol = if b then "++" else "--"
paulson@17525
  1228
      in
paulson@17525
  1229
	  pol ^ c ^ "(" ^ str ^ ")"
paulson@17525
  1230
      end;
paulson@15347
  1231
    
paulson@15347
  1232
paulson@17525
  1233
fun tptp_of_conclLit (ArityClause arcls) = tptp_of_arLit (#conclLit arcls);
paulson@15347
  1234
     
paulson@17525
  1235
fun tptp_of_premLits (ArityClause arcls) = map tptp_of_arLit (#premLits arcls);
paulson@15347
  1236
		
paulson@15347
  1237
fun tptp_arity_clause arcls = 
paulson@15347
  1238
    let val arcls_id = string_of_arClauseID arcls
paulson@17525
  1239
	val concl_lit = tptp_of_conclLit arcls
paulson@17525
  1240
	val prems_lits = tptp_of_premLits arcls
paulson@15347
  1241
	val knd = string_of_arKind arcls
paulson@15347
  1242
	val all_lits = concl_lit :: prems_lits
paulson@15347
  1243
    in
paulson@17317
  1244
	"input_clause(" ^ arcls_id ^ "," ^ knd ^ "," ^ 
paulson@17525
  1245
	(bracket_pack all_lits) ^ ")."
paulson@15347
  1246
    end;
paulson@15347
  1247
paulson@15347
  1248
fun tptp_classrelLits sub sup = 
paulson@15347
  1249
    let val tvar = "(T)"
paulson@15347
  1250
    in 
paulson@18411
  1251
	"[--" ^ sub ^ tvar ^ ",++" ^ sup ^ tvar ^ "]"
paulson@15347
  1252
    end;
paulson@15347
  1253
paulson@17845
  1254
fun tptp_classrelClause (ClassrelClause {clause_id,subclass,superclass,...}) =
paulson@17845
  1255
    let val relcls_id = clrelclause_prefix ^ ascii_of subclass ^ "_" ^ 
paulson@17845
  1256
                        Int.toString clause_id
paulson@18411
  1257
	val lits = tptp_classrelLits (make_type_class subclass) (make_type_class superclass)
paulson@15347
  1258
    in
paulson@15347
  1259
	"input_clause(" ^ relcls_id ^ ",axiom," ^ lits ^ ")."
paulson@15347
  1260
    end; 
quigley@17150
  1261
paulson@15347
  1262
end;