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