src/HOL/Tools/res_clause.ML
author paulson
Thu Sep 15 11:15:52 2005 +0200 (2005-09-15)
changeset 17404 d16c3a62c396
parent 17375 8727db8f0461
child 17412 e26cb20ef0cc
permissions -rw-r--r--
the experimental tagging system, and the usual tidying
paulson@15347
     1
(*  Author: Jia Meng, Cambridge University Computer Laboratory
quigley@17150
     2
paulson@15347
     3
    ID: $Id$
paulson@15347
     4
    Copyright 2004 University of Cambridge
paulson@15347
     5
paulson@15347
     6
ML data structure for storing/printing FOL clauses and arity clauses.
paulson@15347
     7
Typed equality is treated differently.
paulson@15347
     8
*)
paulson@15347
     9
quigley@17150
    10
(* works for writeoutclasimp on typed *)
paulson@15347
    11
signature RES_CLAUSE =
paulson@15347
    12
  sig
paulson@17404
    13
  val keep_types : bool ref
paulson@17404
    14
  val special_equal : bool ref
paulson@17404
    15
  val tagged : bool ref
paulson@17404
    16
paulson@17404
    17
  exception ARCLAUSE of string
paulson@17404
    18
  exception CLAUSE of string * term
paulson@17404
    19
  type arityClause 
paulson@17404
    20
  type classrelClause
paulson@17404
    21
  val classrelClauses_of : string * string list -> classrelClause list
paulson@17404
    22
  type clause
paulson@17404
    23
  val init : theory -> unit
paulson@17404
    24
  val make_axiom_arity_clause :
paulson@17404
    25
     string * (string * string list list) -> arityClause
paulson@17404
    26
  val make_axiom_classrelClause :  string * string option -> classrelClause
paulson@17404
    27
  val make_axiom_clause : Term.term -> string * int -> clause
paulson@17404
    28
  val make_conjecture_clause : Term.term -> clause
paulson@17404
    29
  val make_conjecture_clause_thm : Thm.thm -> clause
paulson@17404
    30
  val make_hypothesis_clause : Term.term -> clause
paulson@17404
    31
  val get_axiomName : clause ->  string
paulson@17404
    32
  val isTaut : clause -> bool
paulson@17404
    33
  val num_of_clauses : clause -> int
quigley@17150
    34
paulson@17404
    35
  val dfg_clauses2str : string list -> string
paulson@17404
    36
  val clause2dfg : clause -> string * string list
paulson@17404
    37
  val clauses2dfg : clause list -> string -> clause list -> clause list ->
paulson@17404
    38
	   (string * int) list -> (string * int) list -> string list -> string
paulson@17404
    39
  val tfree_dfg_clause : string -> string
quigley@17150
    40
paulson@17404
    41
  val tptp_arity_clause : arityClause -> string
paulson@17404
    42
  val tptp_classrelClause : classrelClause -> string
paulson@17404
    43
  val tptp_clause : clause -> string list
paulson@17404
    44
  val tptp_clauses2str : string list -> string
paulson@17404
    45
  val clause2tptp : clause -> string * string list
paulson@17404
    46
  val tfree_clause : string -> string
paulson@17404
    47
  val schematic_var_prefix : string
paulson@17404
    48
  val fixed_var_prefix : string
paulson@17404
    49
  val tvar_prefix : string
paulson@17404
    50
  val tfree_prefix : string
paulson@17404
    51
  val clause_prefix : string 
paulson@17404
    52
  val arclause_prefix : string
paulson@17404
    53
  val const_prefix : string
paulson@17404
    54
  val tconst_prefix : string 
paulson@17404
    55
  val class_prefix : string 
paulson@15347
    56
  end;
paulson@15347
    57
quigley@17150
    58
structure ResClause: RES_CLAUSE =
paulson@15347
    59
struct
paulson@15347
    60
paulson@15347
    61
(* Added for typed equality *)
paulson@15347
    62
val special_equal = ref false; (* by default,equality does not carry type information *)
paulson@15347
    63
val eq_typ_wrapper = "typeinfo"; (* default string *)
paulson@15347
    64
paulson@15347
    65
paulson@15347
    66
val schematic_var_prefix = "V_";
paulson@15347
    67
val fixed_var_prefix = "v_";
paulson@15347
    68
paulson@17230
    69
val tvar_prefix = "T_";
paulson@17230
    70
val tfree_prefix = "t_";
paulson@15347
    71
paulson@15347
    72
val clause_prefix = "cls_"; 
paulson@15347
    73
val arclause_prefix = "arcls_" 
paulson@17317
    74
val clrelclause_prefix = "relcls_";
paulson@15347
    75
paulson@17230
    76
val const_prefix = "c_";
paulson@17230
    77
val tconst_prefix = "tc_"; 
paulson@15347
    78
paulson@16199
    79
val class_prefix = "class_"; 
paulson@15347
    80
paulson@15347
    81
paulson@15347
    82
(**** some useful functions ****)
paulson@15347
    83
 
paulson@15347
    84
val const_trans_table =
paulson@15347
    85
      Symtab.make [("op =", "equal"),
paulson@15347
    86
	  	   ("op <=", "lessequals"),
paulson@15347
    87
		   ("op <", "less"),
paulson@15347
    88
		   ("op &", "and"),
paulson@15347
    89
		   ("op |", "or"),
paulson@17375
    90
		   ("op +", "plus"),
paulson@17375
    91
		   ("op -", "minus"),
paulson@17375
    92
		   ("op *", "times"),
paulson@15347
    93
		   ("op -->", "implies"),
paulson@17375
    94
		   ("{}", "emptyset"),
paulson@15347
    95
		   ("op :", "in"),
paulson@15347
    96
		   ("op Un", "union"),
paulson@15347
    97
		   ("op Int", "inter")];
paulson@15347
    98
paulson@17230
    99
val type_const_trans_table =
paulson@17230
   100
      Symtab.make [("*", "t_prod"),
paulson@17230
   101
	  	   ("+", "t_sum"),
paulson@17230
   102
		   ("~=>", "t_map")];
paulson@15347
   103
paulson@15610
   104
(*Escaping of special characters.
paulson@15610
   105
  Alphanumeric characters are left unchanged.
paulson@15610
   106
  The character _ goes to __
paulson@15610
   107
  Characters in the range ASCII space to / go to _A to _P, respectively.
paulson@15610
   108
  Other printing characters go to _NNN where NNN is the decimal ASCII code.*)
paulson@15610
   109
local
paulson@15610
   110
paulson@15610
   111
val A_minus_space = Char.ord #"A" - Char.ord #" ";
paulson@15610
   112
paulson@15347
   113
fun ascii_of_c c =
paulson@15610
   114
  if Char.isAlphaNum c then String.str c
paulson@15610
   115
  else if c = #"_" then "__"
paulson@15610
   116
  else if #" " <= c andalso c <= #"/" 
paulson@15610
   117
       then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
paulson@15610
   118
  else if Char.isPrint c then ("_" ^ Int.toString (Char.ord c))
paulson@15610
   119
  else ""
paulson@15347
   120
paulson@15610
   121
in
paulson@15610
   122
paulson@15610
   123
val ascii_of = String.translate ascii_of_c;
paulson@15610
   124
paulson@15610
   125
end;
paulson@15347
   126
paulson@16925
   127
(*Remove the initial ' character from a type variable, if it is present*)
paulson@16925
   128
fun trim_type_var s =
paulson@16925
   129
  if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
paulson@16925
   130
  else error ("trim_type: Malformed type variable encountered: " ^ s);
paulson@16925
   131
paulson@16903
   132
fun ascii_of_indexname (v,0) = ascii_of v
paulson@16903
   133
  | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ string_of_int i;
paulson@15347
   134
paulson@17230
   135
fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
paulson@15347
   136
fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
paulson@15347
   137
paulson@16903
   138
(*Type variables contain _H because the character ' translates to that.*)
paulson@16925
   139
fun make_schematic_type_var (x,i) = 
paulson@16925
   140
      tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
paulson@16925
   141
fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
paulson@15347
   142
paulson@17230
   143
fun make_fixed_const c =
wenzelm@17261
   144
    case Symtab.curried_lookup const_trans_table c of
paulson@17230
   145
        SOME c' => c'
wenzelm@17261
   146
      | NONE =>  const_prefix ^ ascii_of c;
paulson@17230
   147
paulson@17230
   148
fun make_fixed_type_const c = 
wenzelm@17261
   149
    case Symtab.curried_lookup type_const_trans_table c of
paulson@17230
   150
        SOME c' => c'
wenzelm@17261
   151
      | NONE =>  tconst_prefix ^ ascii_of c;
quigley@17150
   152
wenzelm@17261
   153
fun make_type_class clas = class_prefix ^ ascii_of clas;
quigley@17150
   154
quigley@17150
   155
quigley@17150
   156
paulson@15347
   157
(***** definitions and functions for FOL clauses, prepared for conversion into TPTP format or SPASS format. *****)
paulson@15347
   158
paulson@17230
   159
val keep_types = ref true;
paulson@15347
   160
paulson@15347
   161
datatype kind = Axiom | Hypothesis | Conjecture;
paulson@15347
   162
fun name_of_kind Axiom = "axiom"
paulson@15347
   163
  | name_of_kind Hypothesis = "hypothesis"
paulson@15347
   164
  | name_of_kind Conjecture = "conjecture";
paulson@15347
   165
paulson@15347
   166
type clause_id = int;
paulson@15347
   167
type axiom_name = string;
paulson@15347
   168
paulson@15347
   169
paulson@15347
   170
type polarity = bool;
paulson@15347
   171
paulson@15347
   172
type indexname = Term.indexname;
paulson@15347
   173
paulson@15347
   174
paulson@15347
   175
(* "tag" is used for vampire specific syntax  *)
paulson@15347
   176
type tag = bool; 
paulson@15347
   177
paulson@15347
   178
paulson@17230
   179
val id_ref = ref 0;
quigley@17150
   180
paulson@15347
   181
fun generate_id () = 
paulson@17317
   182
  let val id = !id_ref
paulson@17317
   183
  in id_ref := id + 1; id end;
paulson@15347
   184
paulson@15347
   185
paulson@15347
   186
paulson@15347
   187
(**** Isabelle FOL clauses ****)
paulson@15347
   188
paulson@15347
   189
val tagged = ref false;
paulson@15347
   190
paulson@15347
   191
type pred_name = string;
paulson@15347
   192
type sort = Term.sort;
paulson@15347
   193
type fol_type = string;
paulson@15347
   194
paulson@15347
   195
paulson@15347
   196
datatype type_literal = LTVar of string | LTFree of string;
paulson@15347
   197
paulson@15347
   198
paulson@17317
   199
datatype folTerm = UVar of string * fol_type
paulson@17317
   200
                 | Fun of string * fol_type * folTerm list;
paulson@15347
   201
datatype predicate = Predicate of pred_name * fol_type * folTerm list;
paulson@15347
   202
paulson@15347
   203
datatype literal = Literal of polarity * predicate * tag;
paulson@15347
   204
paulson@15347
   205
datatype typ_var = FOLTVar of indexname | FOLTFree of string;
paulson@15347
   206
paulson@15347
   207
paulson@15347
   208
(* ML datatype used to repsent one single clause: disjunction of literals. *)
paulson@15347
   209
datatype clause = 
paulson@15347
   210
	 Clause of {clause_id: clause_id,
paulson@15347
   211
		    axiom_name: axiom_name,
paulson@15347
   212
		    kind: kind,
paulson@15347
   213
		    literals: literal list,
paulson@15347
   214
		    types_sorts: (typ_var * sort) list, 
paulson@15347
   215
                    tvar_type_literals: type_literal list, 
quigley@17150
   216
                    tfree_type_literals: type_literal list ,
quigley@17150
   217
                    tvars: string list,
quigley@17150
   218
                    predicates: (string*int) list,
quigley@17150
   219
                    functions: (string*int) list};
quigley@17150
   220
paulson@15347
   221
paulson@17404
   222
exception CLAUSE of string * term;
paulson@15347
   223
paulson@15347
   224
paulson@15347
   225
(*** make clauses ***)
paulson@15347
   226
paulson@17317
   227
fun isFalse (Literal (pol,Predicate(a,_,[]),_)) =
paulson@17317
   228
      (pol andalso a = "c_False") orelse
paulson@17317
   229
      (not pol andalso a = "c_True")
paulson@17317
   230
  | isFalse _ = false;
paulson@17317
   231
paulson@17404
   232
fun isTrue (Literal (pol,Predicate(a,_,[]),_)) =
paulson@17404
   233
      (pol andalso a = "c_True") orelse
paulson@17404
   234
      (not pol andalso a = "c_False")
paulson@17404
   235
  | isTrue _ = false;
paulson@17404
   236
  
paulson@17404
   237
fun isTaut (Clause {literals,...}) = exists isTrue literals;  
paulson@17404
   238
paulson@17234
   239
fun make_clause (clause_id,axiom_name,kind,literals,
paulson@17234
   240
                 types_sorts,tvar_type_literals,
paulson@17230
   241
                 tfree_type_literals,tvars, predicates, functions) =
paulson@17317
   242
  if forall isFalse literals 
paulson@17234
   243
  then error "Problem too trivial for resolution (empty clause)"
paulson@17234
   244
  else
paulson@17230
   245
     Clause {clause_id = clause_id, axiom_name = axiom_name, kind = kind, 
paulson@17230
   246
             literals = literals, types_sorts = types_sorts,
paulson@17230
   247
             tvar_type_literals = tvar_type_literals,
paulson@17230
   248
             tfree_type_literals = tfree_type_literals,
paulson@17230
   249
             tvars = tvars, predicates = predicates, 
paulson@17230
   250
             functions = functions};
quigley@17150
   251
paulson@15347
   252
paulson@17317
   253
(** Some Clause destructor functions **)
paulson@17317
   254
paulson@17317
   255
fun string_of_kind (Clause cls) = name_of_kind (#kind cls);
paulson@17317
   256
paulson@17317
   257
fun get_axiomName (Clause cls) = #axiom_name cls;
paulson@17317
   258
paulson@17317
   259
fun get_clause_id (Clause cls) = #clause_id cls;
paulson@17317
   260
paulson@17317
   261
fun funcs_of_cls (Clause cls) = #functions cls;
paulson@17317
   262
paulson@17317
   263
fun preds_of_cls (Clause cls) = #predicates cls;
paulson@17317
   264
paulson@17317
   265
paulson@17317
   266
paulson@16925
   267
(*Definitions of the current theory--to allow suppressing types.*)
paulson@16925
   268
val curr_defs = ref Defs.empty;
paulson@16925
   269
paulson@16925
   270
(*Initialize the type suppression mechanism with the current theory before
paulson@16925
   271
    producing any clauses!*)
paulson@16925
   272
fun init thy = (curr_defs := Theory.defs_of thy);
quigley@17150
   273
wenzelm@16976
   274
fun no_types_needed s = Defs.monomorphic (!curr_defs) s;
paulson@16925
   275
    
quigley@17150
   276
paulson@16925
   277
(*Flatten a type to a string while accumulating sort constraints on the TFress and
paulson@16925
   278
  TVars it contains.*)    
paulson@17230
   279
fun type_of (Type (a, [])) = 
paulson@17230
   280
      let val t = make_fixed_type_const a
paulson@17230
   281
      in (t,([],[(t,0)]))  end
paulson@15347
   282
  | type_of (Type (a, Ts)) = 
paulson@17230
   283
      let val foltyps_ts = map type_of Ts 
paulson@17230
   284
	  val (folTyps,ts_funcs) = ListPair.unzip foltyps_ts
paulson@17230
   285
	  val (ts, funcslist) = ListPair.unzip ts_funcs
paulson@17230
   286
	  val ts' = ResLib.flat_noDup ts
paulson@17230
   287
	  val funcs' = ResLib.flat_noDup funcslist
paulson@17230
   288
	  val t = make_fixed_type_const a
paulson@17230
   289
      in    
paulson@17230
   290
	  ((t ^ (ResLib.list_to_string folTyps)),(ts', (t, length Ts)::funcs') )
paulson@17230
   291
      end
paulson@17230
   292
  | type_of (TFree (a, s)) = 
paulson@17230
   293
      let val t = make_fixed_type_var a
paulson@17230
   294
      in (t, ([((FOLTFree a),s)],[(t,0)]) ) end
paulson@17230
   295
  | type_of (TVar (v, s)) = (make_schematic_type_var v, ([((FOLTVar v),s)], []))
quigley@17150
   296
paulson@15390
   297
paulson@16925
   298
fun maybe_type_of c T =
quigley@17150
   299
 if no_types_needed c then ("",([],[])) else type_of T;
paulson@16925
   300
paulson@16903
   301
(* Any variables created via the METAHYPS tactical should be treated as
paulson@16903
   302
   universal vars, although it is represented as "Free(...)" by Isabelle *)
paulson@16903
   303
val isMeta = String.isPrefix "METAHYP1_"
quigley@17150
   304
paulson@17230
   305
fun pred_name_type (Const(c,T)) = 
paulson@17230
   306
      let val (typof,(folTyps,funcs)) = maybe_type_of c T
paulson@17230
   307
      in (make_fixed_const c, (typof,folTyps), funcs) end
paulson@15390
   308
  | pred_name_type (Free(x,T))  = 
paulson@17404
   309
      if isMeta x then raise CLAUSE("Predicate Not First Order 1", Free(x,T)) 
paulson@17230
   310
      else (make_fixed_var x, ("",[]), [])
paulson@17404
   311
  | pred_name_type (v as Var _) = raise CLAUSE("Predicate Not First Order 2", v)
paulson@17404
   312
  | pred_name_type t        = raise CLAUSE("Predicate input unexpected", t);
paulson@15347
   313
paulson@15615
   314
paulson@15615
   315
(* For type equality *)
paulson@15615
   316
(* here "arg_typ" is the type of "="'s argument's type, not the type of the equality *)
paulson@15615
   317
(* Find type of equality arg *)
paulson@15615
   318
fun eq_arg_type (Type("fun",[T,_])) = 
paulson@15615
   319
    let val (folT,_) = type_of T;
paulson@17230
   320
    in  folT  end;
paulson@15347
   321
paulson@17230
   322
fun fun_name_type (Const(c,T)) args = 
paulson@17230
   323
      let val t = make_fixed_const c
paulson@17230
   324
	val (typof, (folTyps,funcs)) = maybe_type_of c T
paulson@17230
   325
	val arity = if !keep_types andalso not (no_types_needed c)
paulson@17230
   326
	            then 1 + length args
paulson@17230
   327
	            else length args
paulson@17230
   328
      in
paulson@17230
   329
	  (t, (typof,folTyps), ((t,arity)::funcs))
paulson@17230
   330
      end
paulson@17230
   331
 | fun_name_type (Free(x,T)) args  = 
paulson@17230
   332
      let val t = make_fixed_var x
paulson@17230
   333
      in
paulson@17230
   334
	    (t, ("",[]), [(t, length args)])
paulson@17230
   335
      end
paulson@17404
   336
  | fun_name_type f args = raise CLAUSE("Function Not First Order 1", f);
quigley@17150
   337
paulson@15615
   338
paulson@15347
   339
fun term_of (Var(ind_nm,T)) = 
paulson@17230
   340
      let val (folType,(ts,funcs)) = type_of T
paulson@17230
   341
      in
paulson@17230
   342
	  (UVar(make_schematic_var ind_nm, folType), (ts, funcs))
paulson@17230
   343
      end
paulson@15347
   344
  | term_of (Free(x,T)) = 
paulson@17404
   345
      let val (folType, (ts,funcs)) = type_of T
paulson@17230
   346
      in
paulson@17230
   347
	  if isMeta x then (UVar(make_schematic_var(x,0),folType),
paulson@17230
   348
			    (ts, ((make_schematic_var(x,0)),0)::funcs))
paulson@17230
   349
	  else
paulson@17404
   350
	      (Fun(make_fixed_var x, folType, []), 
paulson@17404
   351
	       (ts, ((make_fixed_var x),0)::funcs))
paulson@17230
   352
      end
paulson@17230
   353
  | term_of (Const(c,T)) =  (* impossible to be equality *)
paulson@17230
   354
      let val (folType,(ts,funcs)) = type_of T
paulson@17404
   355
      in
paulson@17404
   356
	  (Fun(make_fixed_const c, folType, []),
paulson@17404
   357
	   (ts, ((make_fixed_const c),0)::funcs))
paulson@17230
   358
      end    
paulson@17230
   359
  | term_of (app as (t $ a)) = 
paulson@17230
   360
      let val (f,args) = strip_comb app
paulson@17230
   361
	  fun term_of_aux () = 
paulson@17230
   362
	      let val (funName,(funType,ts1),funcs) = fun_name_type f args
paulson@17230
   363
		  val (args',ts_funcs) = ListPair.unzip (map term_of args)
paulson@17230
   364
		  val (ts2,funcs') = ListPair.unzip ts_funcs
paulson@17230
   365
		  val ts3 = ResLib.flat_noDup (ts1::ts2)
paulson@17230
   366
		  val funcs'' = ResLib.flat_noDup((funcs::funcs'))
paulson@17230
   367
	      in
paulson@17404
   368
		  (Fun(funName,funType,args'), (ts3,funcs''))
paulson@17230
   369
	      end
paulson@17230
   370
	  fun term_of_eq ((Const ("op =", typ)),args) =
paulson@17230
   371
	      let val arg_typ = eq_arg_type typ
paulson@17230
   372
		  val (args',ts_funcs) = ListPair.unzip (map term_of args)
paulson@17230
   373
		  val (ts,funcs) = ListPair.unzip ts_funcs
paulson@17230
   374
		  val equal_name = make_fixed_const ("op =")
paulson@17230
   375
	      in
paulson@17230
   376
		  (Fun(equal_name,arg_typ,args'),
paulson@17230
   377
		   (ResLib.flat_noDup ts, 
paulson@17404
   378
		    (make_fixed_var equal_name, 2):: ResLib.flat_noDup funcs))
paulson@17230
   379
	      end
paulson@17230
   380
      in
paulson@17404
   381
	 case f of Const ("op =", typ) => term_of_eq (f,args)
paulson@17404
   382
		 | Const(_,_) => term_of_aux ()
paulson@17404
   383
		 | Free(s,_)  => 
paulson@17404
   384
		     if isMeta s 
paulson@17404
   385
		     then raise CLAUSE("Function Not First Order 2", f)
paulson@17404
   386
		     else term_of_aux()
paulson@17404
   387
		 | _ => raise CLAUSE("Function Not First Order 3", f)
paulson@17230
   388
      end
paulson@17404
   389
  | term_of t = raise CLAUSE("Function Not First Order 4", t); 
paulson@15390
   390
paulson@15347
   391
paulson@17404
   392
fun pred_of (Const("op =", typ), args) =
paulson@17404
   393
      let val arg_typ = eq_arg_type typ 
paulson@17404
   394
	  val (args',ts_funcs) = ListPair.unzip (map term_of args)
paulson@17404
   395
	  val (ts,funcs) = ListPair.unzip ts_funcs
paulson@17404
   396
	  val equal_name = make_fixed_const "op ="
paulson@17404
   397
      in
paulson@17404
   398
	  (Predicate(equal_name,arg_typ,args'),
paulson@17404
   399
	   ResLib.flat_noDup ts, 
paulson@17404
   400
	   [((make_fixed_var equal_name), 2)], 
paulson@17404
   401
	   (ResLib.flat_noDup funcs))
paulson@17404
   402
      end
paulson@17404
   403
  | pred_of (pred,args) = 
paulson@17404
   404
      let val (predName,(predType,ts1), pfuncs) = pred_name_type pred
paulson@17404
   405
	  val (args',ts_funcs) = ListPair.unzip (map term_of args)
paulson@17404
   406
	  val (ts2,ffuncs) = ListPair.unzip ts_funcs
paulson@17404
   407
	  val ts3 = ResLib.flat_noDup (ts1::ts2)
paulson@17404
   408
	  val ffuncs' = (ResLib.flat_noDup ffuncs)
paulson@17404
   409
	  val newfuncs = distinct (pfuncs@ffuncs')
paulson@17404
   410
	  val arity = 
paulson@17404
   411
	    case pred of
paulson@17404
   412
		Const (c,_) => 
paulson@17404
   413
		      if !keep_types andalso not (no_types_needed c)
paulson@17404
   414
		      then 1 + length args
paulson@17404
   415
		      else length args
paulson@17404
   416
	      | _ => length args
paulson@17404
   417
      in
paulson@17404
   418
	  (Predicate(predName,predType,args'), ts3, 
paulson@17404
   419
	   [(predName, arity)], newfuncs)
paulson@17404
   420
      end;
paulson@15347
   421
paulson@15347
   422
paulson@17404
   423
(*Treatment of literals, possibly negated or tagged*)
paulson@17404
   424
fun predicate_of ((Const("Not",_) $ P), polarity, tag) =
paulson@17404
   425
      predicate_of (P, not polarity, tag)
paulson@17404
   426
  | predicate_of ((Const("HOL.tag",_) $ P), polarity, tag) =
paulson@17404
   427
      predicate_of (P, polarity, true)
paulson@17404
   428
  | predicate_of (term,polarity,tag) =
paulson@17404
   429
        (pred_of (strip_comb term), polarity, tag);
paulson@15347
   430
paulson@17234
   431
fun literals_of_term ((Const("Trueprop",_) $ P),lits_ts, preds, funcs) =    
paulson@17234
   432
      literals_of_term(P,lits_ts, preds, funcs)
quigley@17150
   433
  | literals_of_term ((Const("op |",_) $ P $ Q),(lits,ts), preds,funcs) = 
paulson@17234
   434
      let val (lits',ts', preds', funcs') = 
paulson@17234
   435
            literals_of_term(P,(lits,ts), preds,funcs)
paulson@17234
   436
      in
paulson@17234
   437
	  literals_of_term(Q, (lits',ts'), distinct(preds'@preds), 
paulson@17234
   438
	                   distinct(funcs'@funcs))
paulson@17234
   439
      end
quigley@17150
   440
  | literals_of_term (P,(lits,ts), preds, funcs) = 
paulson@17404
   441
      let val ((pred,ts', preds', funcs'), pol, tag) = 
paulson@17404
   442
              predicate_of (P,true,false)
paulson@17404
   443
	  val lits' = Literal(pol,pred,tag) :: lits
paulson@17234
   444
	  val ts'' = ResLib.no_rep_app ts ts' 
paulson@17234
   445
      in
paulson@17234
   446
	  (lits',ts'', distinct(preds'@preds), distinct(funcs'@funcs))
paulson@17234
   447
      end;
quigley@17150
   448
quigley@17150
   449
quigley@17150
   450
fun literals_of_thm thm = literals_of_term (prop_of thm, ([],[]), [], []);
quigley@17150
   451
quigley@17150
   452
quigley@17150
   453
(* FIX: not sure what to do with these funcs *)
quigley@17150
   454
paulson@16199
   455
(*Make literals for sorted type variables*) 
quigley@17150
   456
fun sorts_on_typs (_, [])   = ([]) 
paulson@16199
   457
  | sorts_on_typs (v, "HOL.type" :: s) =
paulson@16199
   458
      sorts_on_typs (v,s)   (*Ignore sort "type"*)
paulson@17230
   459
  | sorts_on_typs ((FOLTVar indx), (s::ss)) =
paulson@17230
   460
      LTVar((make_type_class s) ^ 
paulson@17230
   461
        "(" ^ (make_schematic_type_var indx) ^ ")") :: 
paulson@17230
   462
      (sorts_on_typs ((FOLTVar indx), ss))
paulson@17230
   463
  | sorts_on_typs ((FOLTFree x), (s::ss)) =
paulson@17230
   464
      LTFree((make_type_class s) ^ "(" ^ (make_fixed_type_var x) ^ ")") :: 
paulson@17230
   465
      (sorts_on_typs ((FOLTFree x), ss));
paulson@15347
   466
quigley@17150
   467
paulson@17312
   468
(*UGLY: seems to be parsing the "show sorts" output, removing anything that
paulson@17312
   469
  starts with a left parenthesis.*)
paulson@17312
   470
fun remove_type str = hd (String.fields (fn c => c = #"(") str);
quigley@17150
   471
quigley@17150
   472
fun pred_of_sort (LTVar x) = ((remove_type x),1)
quigley@17150
   473
|   pred_of_sort (LTFree x) = ((remove_type x),1)
quigley@17150
   474
quigley@17150
   475
quigley@17150
   476
quigley@17150
   477
paulson@16199
   478
(*Given a list of sorted type variables, return two separate lists.
paulson@16199
   479
  The first is for TVars, the second for TFrees.*)
quigley@17150
   480
fun add_typs_aux [] preds  = ([],[], preds)
paulson@17230
   481
  | add_typs_aux ((FOLTVar indx,s)::tss) preds = 
paulson@17230
   482
      let val vs = sorts_on_typs (FOLTVar indx, s)
quigley@17150
   483
          val preds' = (map pred_of_sort vs)@preds
quigley@17150
   484
	  val (vss,fss, preds'') = add_typs_aux tss preds'
quigley@17150
   485
      in
quigley@17150
   486
	  (ResLib.no_rep_app vs vss, fss, preds'')
quigley@17150
   487
      end
paulson@17230
   488
  | add_typs_aux ((FOLTFree x,s)::tss) preds  =
paulson@17230
   489
      let val fs = sorts_on_typs (FOLTFree x, s)
quigley@17150
   490
          val preds' = (map pred_of_sort fs)@preds
quigley@17150
   491
	  val (vss,fss, preds'') = add_typs_aux tss preds'
quigley@17150
   492
      in
quigley@17150
   493
	  (vss, ResLib.no_rep_app fs fss,preds'')
quigley@17150
   494
      end;
quigley@17150
   495
quigley@17150
   496
fun add_typs (Clause cls) preds  = add_typs_aux (#types_sorts cls) preds 
paulson@15347
   497
paulson@15347
   498
paulson@15347
   499
(** make axiom clauses, hypothesis clauses and conjecture clauses. **)
quigley@17150
   500
quigley@17150
   501
fun get_tvar_strs [] = []
paulson@17230
   502
  | get_tvar_strs ((FOLTVar indx,s)::tss) = 
paulson@17230
   503
      let val vstr = make_schematic_type_var indx
paulson@17230
   504
          val vstrs = get_tvar_strs tss
quigley@17150
   505
      in
quigley@17150
   506
	  (distinct( vstr:: vstrs))
quigley@17150
   507
      end
paulson@17230
   508
  | get_tvar_strs((FOLTFree x,s)::tss) = distinct (get_tvar_strs tss)
quigley@17150
   509
quigley@17150
   510
(* FIX add preds and funcs to add typs aux here *)
quigley@17150
   511
paulson@17230
   512
fun make_axiom_clause_thm thm (ax_name,cls_id) =
quigley@17150
   513
    let val (lits,types_sorts, preds, funcs) = literals_of_thm thm
quigley@17150
   514
	val (tvar_lits,tfree_lits, preds) = add_typs_aux types_sorts preds 
quigley@17150
   515
        val tvars = get_tvar_strs types_sorts
quigley@17150
   516
    in 
paulson@17230
   517
	make_clause(cls_id,ax_name,Axiom,
paulson@17230
   518
	            lits,types_sorts,tvar_lits,tfree_lits,
paulson@17230
   519
	            tvars, preds, funcs)
paulson@15347
   520
    end;
paulson@15347
   521
paulson@15347
   522
quigley@17150
   523
quigley@17150
   524
fun make_conjecture_clause_thm thm =
quigley@17150
   525
    let val (lits,types_sorts, preds, funcs) = literals_of_thm thm
quigley@17150
   526
	val cls_id = generate_id()
quigley@17150
   527
	val (tvar_lits,tfree_lits, preds) = add_typs_aux types_sorts preds 
quigley@17150
   528
        val tvars = get_tvar_strs types_sorts
quigley@17150
   529
    in
paulson@17230
   530
	make_clause(cls_id,"",Conjecture,
paulson@17230
   531
	            lits,types_sorts,tvar_lits,tfree_lits,
paulson@17230
   532
	            tvars, preds, funcs)
quigley@17150
   533
    end;
quigley@17150
   534
quigley@17150
   535
paulson@17230
   536
fun make_axiom_clause term (ax_name,cls_id) =
quigley@17150
   537
    let val (lits,types_sorts, preds,funcs) = literals_of_term (term,([],[]), [],[])
paulson@17230
   538
	val (tvar_lits,tfree_lits, preds) = add_typs_aux types_sorts preds
quigley@17150
   539
        val tvars = get_tvar_strs types_sorts	
paulson@15347
   540
    in 
paulson@17230
   541
	make_clause(cls_id,ax_name,Axiom,
paulson@17230
   542
	            lits,types_sorts,tvar_lits,tfree_lits,
paulson@17230
   543
	            tvars, preds,funcs)
paulson@15347
   544
    end;
paulson@15347
   545
paulson@15347
   546
paulson@15347
   547
fun make_hypothesis_clause term =
quigley@17150
   548
    let val (lits,types_sorts, preds, funcs) = literals_of_term (term,([],[]),[],[])
paulson@15347
   549
	val cls_id = generate_id()
quigley@17150
   550
	val (tvar_lits,tfree_lits, preds) = add_typs_aux types_sorts  preds 
quigley@17150
   551
        val tvars = get_tvar_strs types_sorts
paulson@15347
   552
    in
paulson@17230
   553
	make_clause(cls_id,"",Hypothesis,
paulson@17230
   554
	            lits,types_sorts,tvar_lits,tfree_lits,
paulson@17230
   555
	            tvars, preds, funcs)
paulson@15347
   556
    end;
paulson@15347
   557
 
paulson@15347
   558
fun make_conjecture_clause term =
quigley@17150
   559
    let val (lits,types_sorts, preds, funcs) = literals_of_term (term,([],[]),[],[])
paulson@15347
   560
	val cls_id = generate_id()
paulson@17230
   561
	val (tvar_lits,tfree_lits, preds) = add_typs_aux types_sorts preds 
quigley@17150
   562
        val tvars = get_tvar_strs types_sorts	
paulson@15347
   563
    in
paulson@17230
   564
	make_clause(cls_id,"",Conjecture,
paulson@17230
   565
	            lits,types_sorts,tvar_lits,tfree_lits,
paulson@17230
   566
	            tvars, preds, funcs)
paulson@15347
   567
    end;
paulson@15347
   568
 
paulson@15347
   569
paulson@15347
   570
 
paulson@15347
   571
(**** Isabelle arities ****)
paulson@15347
   572
paulson@15347
   573
exception ARCLAUSE of string;
paulson@15347
   574
 
paulson@15347
   575
paulson@15347
   576
type class = string; 
paulson@15347
   577
type tcons = string; 
paulson@15347
   578
paulson@15347
   579
paulson@15347
   580
datatype arLit = TConsLit of bool * (class * tcons * string list) | TVarLit of bool * (class * string);
paulson@15347
   581
 
paulson@15347
   582
datatype arityClause =  
paulson@15347
   583
	 ArityClause of {clause_id: clause_id,
paulson@15347
   584
			 kind: kind,
paulson@15347
   585
			 conclLit: arLit,
paulson@15347
   586
			 premLits: arLit list};
paulson@15347
   587
paulson@15347
   588
paulson@15347
   589
fun get_TVars 0 = []
paulson@15347
   590
  | get_TVars n = ("T_" ^ (string_of_int n)) :: get_TVars (n-1);
paulson@15347
   591
paulson@15347
   592
paulson@15347
   593
paulson@15347
   594
fun pack_sort(_,[])  = raise ARCLAUSE("Empty Sort Found") 
paulson@15347
   595
  | pack_sort(tvar, [cls]) = [(make_type_class cls, tvar)] 
paulson@15347
   596
  | pack_sort(tvar, cls::srt) =  (make_type_class cls,tvar) :: (pack_sort(tvar, srt));
paulson@15347
   597
    
paulson@15347
   598
    
paulson@15347
   599
fun make_TVarLit (b,(cls,str)) = TVarLit(b,(cls,str));
paulson@15347
   600
fun make_TConsLit (b,(cls,tcons,tvars)) = TConsLit(b,(make_type_class cls,make_fixed_type_const tcons,tvars));
paulson@15347
   601
paulson@15347
   602
paulson@15347
   603
fun make_arity_clause (clause_id,kind,conclLit,premLits) =
paulson@15347
   604
    ArityClause {clause_id = clause_id, kind = kind, conclLit = conclLit, premLits = premLits};
paulson@15347
   605
paulson@15347
   606
paulson@15347
   607
fun make_axiom_arity_clause (tcons,(res,args)) =
paulson@15347
   608
     let val cls_id = generate_id()
paulson@15347
   609
	 val nargs = length args
paulson@15347
   610
	 val tvars = get_TVars nargs
paulson@15347
   611
	 val conclLit = make_TConsLit(true,(res,tcons,tvars))
paulson@15774
   612
         val tvars_srts = ListPair.zip (tvars,args)
paulson@15347
   613
	 val tvars_srts' = ResLib.flat_noDup(map pack_sort tvars_srts)
paulson@15347
   614
         val false_tvars_srts' = ResLib.pair_ins false tvars_srts'
paulson@15347
   615
	 val premLits = map make_TVarLit false_tvars_srts'
paulson@15347
   616
     in
paulson@15347
   617
	 make_arity_clause (cls_id,Axiom,conclLit,premLits)
paulson@15347
   618
     end;
paulson@15347
   619
    
paulson@17305
   620
(*The number of clauses generated from cls, including type clauses*)
paulson@17305
   621
fun num_of_clauses (Clause cls) =
paulson@17305
   622
    let val num_tfree_lits = 
paulson@17305
   623
	      if !keep_types then length (#tfree_type_literals cls)
paulson@17305
   624
	      else 0
paulson@17305
   625
    in 	1 + num_tfree_lits  end;
paulson@15347
   626
paulson@15347
   627
paulson@15347
   628
(**** Isabelle class relations ****)
paulson@15347
   629
paulson@15347
   630
paulson@15347
   631
datatype classrelClause = 
paulson@15347
   632
	 ClassrelClause of {clause_id: clause_id,
paulson@15347
   633
			    subclass: class,
skalberg@15531
   634
			    superclass: class option};
paulson@15347
   635
paulson@15347
   636
fun make_classrelClause (clause_id,subclass,superclass) =
paulson@15347
   637
    ClassrelClause {clause_id = clause_id,subclass = subclass, superclass = superclass};
paulson@15347
   638
paulson@15347
   639
paulson@15347
   640
fun make_axiom_classrelClause (subclass,superclass) =
paulson@15347
   641
    let val cls_id = generate_id()
paulson@15347
   642
	val sub = make_type_class subclass
skalberg@15531
   643
	val sup = case superclass of NONE => NONE 
skalberg@15531
   644
				   | SOME s => SOME (make_type_class s)
paulson@15347
   645
    in
paulson@15347
   646
	make_classrelClause(cls_id,sub,sup)
paulson@15347
   647
    end;
paulson@15347
   648
paulson@15347
   649
paulson@15347
   650
paulson@15347
   651
fun classrelClauses_of_aux (sub,[]) = []
skalberg@15531
   652
  | classrelClauses_of_aux (sub,(sup::sups)) = make_axiom_classrelClause(sub,SOME sup) :: (classrelClauses_of_aux (sub,sups));
paulson@15347
   653
paulson@15347
   654
paulson@15347
   655
fun classrelClauses_of (sub,sups) = 
skalberg@15531
   656
    case sups of [] => [make_axiom_classrelClause (sub,NONE)]
paulson@15347
   657
	       | _ => classrelClauses_of_aux (sub, sups);
paulson@15347
   658
paulson@15347
   659
paulson@15347
   660
(****!!!! Changed for typed equality !!!!****)
paulson@17230
   661
paulson@15347
   662
fun wrap_eq_type typ t = eq_typ_wrapper ^"(" ^ t ^ "," ^ typ ^ ")";
paulson@15347
   663
paulson@15347
   664
(* Only need to wrap equality's arguments with "typeinfo" if the output clauses are typed && if we specifically ask for types to be included.   *)
paulson@15347
   665
fun string_of_equality (typ,terms) =
paulson@17230
   666
      let val [tstr1,tstr2] = map string_of_term terms
paulson@17230
   667
      in
paulson@17230
   668
	  if !keep_types andalso !special_equal 
paulson@17230
   669
	  then "equal(" ^ (wrap_eq_type typ tstr1) ^ "," ^ 
paulson@17230
   670
		 	  (wrap_eq_type typ tstr2) ^ ")"
paulson@17230
   671
	  else "equal(" ^ tstr1 ^ "," ^ tstr2 ^ ")"
paulson@17230
   672
      end
paulson@17230
   673
and string_of_term (UVar(x,_)) = x
paulson@15615
   674
  | string_of_term (Fun("equal",typ,terms)) = string_of_equality(typ,terms)
paulson@15615
   675
  | string_of_term (Fun (name,typ,[])) = name
paulson@15615
   676
  | string_of_term (Fun (name,typ,terms)) = 
paulson@17230
   677
      let val terms' = map string_of_term terms
paulson@17230
   678
      in
paulson@17230
   679
	  if !keep_types andalso typ<>"" 
paulson@17230
   680
	  then name ^ (ResLib.list_to_string (terms' @ [typ]))
paulson@17230
   681
	  else name ^ (ResLib.list_to_string terms')
paulson@17230
   682
      end;
paulson@15347
   683
paulson@15347
   684
(* before output the string of the predicate, check if the predicate corresponds to an equality or not. *)
paulson@17234
   685
fun string_of_predicate (Predicate("equal",typ,terms)) = 
paulson@17234
   686
      string_of_equality(typ,terms)
quigley@17150
   687
  | string_of_predicate (Predicate(name,_,[])) = name 
quigley@17150
   688
  | string_of_predicate (Predicate(name,typ,terms)) = 
paulson@17230
   689
      let val terms_as_strings = map string_of_term terms
paulson@17230
   690
      in
paulson@17230
   691
	  if !keep_types andalso typ<>""
paulson@17230
   692
	  then name ^ (ResLib.list_to_string  (terms_as_strings @ [typ]))
paulson@17230
   693
	  else name ^ (ResLib.list_to_string terms_as_strings) 
paulson@17230
   694
      end;
quigley@17150
   695
paulson@17317
   696
paulson@17317
   697
fun string_of_clausename (cls_id,ax_name) = 
paulson@17317
   698
    clause_prefix ^ ascii_of ax_name ^ "_" ^ string_of_int cls_id;
paulson@17317
   699
paulson@17317
   700
fun string_of_type_clsname (cls_id,ax_name,idx) = 
paulson@17317
   701
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (string_of_int idx);
quigley@17150
   702
    
quigley@17150
   703
quigley@17150
   704
(********************************)
quigley@17150
   705
(* Code for producing DFG files *)
quigley@17150
   706
(********************************)
quigley@17150
   707
quigley@17150
   708
fun dfg_literal (Literal(pol,pred,tag)) =
quigley@17150
   709
    let val pred_string = string_of_predicate pred
paulson@17234
   710
    in
paulson@17234
   711
	if pol then pred_string else "not(" ^pred_string ^ ")"  
quigley@17150
   712
    end;
quigley@17150
   713
quigley@17150
   714
quigley@17150
   715
(* FIX: what does this mean? *)
quigley@17150
   716
(*fun dfg_of_typeLit (LTVar x) = "not(" ^ x ^ ")"
quigley@17150
   717
  | dfg_of_typeLit (LTFree x) = "(" ^ x ^ ")";*)
quigley@17150
   718
quigley@17150
   719
fun dfg_of_typeLit (LTVar x) =  x 
quigley@17150
   720
  | dfg_of_typeLit (LTFree x) = x ;
quigley@17150
   721
 
paulson@17230
   722
(*Make the string of universal quantifiers for a clause*)
paulson@17230
   723
fun forall_open ([],[]) = ""
paulson@17230
   724
  | forall_open (vars,tvars) = "forall([" ^ (commas (tvars@vars))^ "],\n"
quigley@17150
   725
paulson@17230
   726
fun forall_close ([],[]) = ""
paulson@17230
   727
  | forall_close (vars,tvars) = ")"
quigley@17150
   728
paulson@17230
   729
fun gen_dfg_cls (cls_id,ax_name,knd,lits,tvars,vars) = 
paulson@17317
   730
    "clause( %(" ^ knd ^ ")\n" ^ forall_open(vars,tvars) ^ 
paulson@17317
   731
    "or(" ^ lits ^ ")" ^ forall_close(vars,tvars) ^ ",\n" ^ 
paulson@17317
   732
    string_of_clausename (cls_id,ax_name) ^  ").";
quigley@17150
   733
paulson@17317
   734
fun gen_dfg_type_cls (cls_id,ax_name,knd,tfree_lit,idx,tvars,vars) = 
paulson@17317
   735
    "clause( %(" ^ knd ^ ")\n" ^ forall_open(vars,tvars) ^ 
paulson@17317
   736
    "or( " ^ tfree_lit ^ ")" ^ forall_close(vars,tvars) ^ ",\n" ^ 
paulson@17317
   737
    string_of_type_clsname (cls_id,ax_name,idx) ^  ").";
quigley@17150
   738
quigley@17150
   739
fun dfg_clause_aux (Clause cls) = 
paulson@17230
   740
  let val lits = map dfg_literal (#literals cls)
paulson@17230
   741
      val tvar_lits_strs = 
paulson@17230
   742
	  if !keep_types then map dfg_of_typeLit (#tvar_type_literals cls) 
paulson@17230
   743
	  else []
paulson@17230
   744
      val tfree_lits =
paulson@17230
   745
          if !keep_types then map dfg_of_typeLit (#tfree_type_literals cls)
paulson@17230
   746
          else []
paulson@17230
   747
  in
paulson@17234
   748
      (tvar_lits_strs @ lits, tfree_lits)
paulson@17230
   749
  end; 
quigley@17150
   750
quigley@17150
   751
quigley@17150
   752
fun dfg_folterms (Literal(pol,pred,tag)) = 
paulson@17230
   753
  let val Predicate (predname, foltype, folterms) = pred
paulson@17230
   754
  in
paulson@17230
   755
      folterms
paulson@17230
   756
  end
quigley@17150
   757
quigley@17150
   758
 
paulson@17404
   759
fun get_uvars (UVar(a,typ)) = [a] 
paulson@17404
   760
|   get_uvars (Fun (_,typ,tlist)) = ResLib.flat_noDup(map get_uvars tlist)
quigley@17150
   761
quigley@17150
   762
paulson@17404
   763
fun is_uvar (UVar _) = true
paulson@17404
   764
|   is_uvar (Fun _) = false;
paulson@17404
   765
paulson@17404
   766
fun uvar_name (UVar(a,_)) = a
paulson@17404
   767
|   uvar_name (Fun (a,_,_)) = raise CLAUSE("Not a variable", Const(a,dummyT));
paulson@17404
   768
quigley@17150
   769
fun mergelist [] = []
paulson@17404
   770
|   mergelist (x::xs ) = x @ mergelist xs
quigley@17150
   771
quigley@17150
   772
fun dfg_vars (Clause cls) =
paulson@17404
   773
    let val lits = #literals cls
paulson@17317
   774
        val folterms = mergelist(map dfg_folterms lits)
quigley@17150
   775
    in 
paulson@17404
   776
        ResLib.flat_noDup(map get_uvars folterms)
quigley@17150
   777
    end
quigley@17150
   778
quigley@17150
   779
quigley@17150
   780
fun dfg_tvars (Clause cls) =(#tvars cls)
quigley@17150
   781
quigley@17150
   782
quigley@17150
   783
	
quigley@17150
   784
(* make this return funcs and preds too? *)
quigley@17150
   785
fun string_of_predname (Predicate("equal",typ,terms)) = "EQUALITY"
quigley@17150
   786
  | string_of_predname (Predicate(name,_,[])) = name 
quigley@17150
   787
  | string_of_predname (Predicate(name,typ,terms)) = name
quigley@17150
   788
    
quigley@17150
   789
	
quigley@17150
   790
(* make this return funcs and preds too? *)
quigley@17150
   791
paulson@17317
   792
fun string_of_predicate (Predicate("equal",typ,terms)) =  
paulson@17317
   793
      string_of_equality(typ,terms)
paulson@15347
   794
  | string_of_predicate (Predicate(name,_,[])) = name 
paulson@15347
   795
  | string_of_predicate (Predicate(name,typ,terms)) = 
paulson@16903
   796
      let val terms_as_strings = map string_of_term terms
paulson@16903
   797
      in
paulson@16925
   798
	  if !keep_types andalso typ<>""
paulson@16903
   799
	  then name ^ (ResLib.list_to_string  (terms_as_strings @ [typ]))
paulson@16903
   800
	  else name ^ (ResLib.list_to_string terms_as_strings) 
paulson@16903
   801
      end;
paulson@15347
   802
quigley@17150
   803
quigley@17150
   804
fun concat_with sep []  = ""
quigley@17150
   805
  | concat_with sep [x] = "(" ^ x ^ ")"
quigley@17150
   806
  | concat_with sep (x::xs) = "(" ^ x ^ ")" ^  sep ^ (concat_with sep xs);
quigley@17150
   807
paulson@17234
   808
fun dfg_pred (Literal(pol,pred,tag)) ax_name = 
paulson@17234
   809
    (string_of_predname pred) ^ " " ^ ax_name
quigley@17150
   810
quigley@17150
   811
fun dfg_clause cls =
paulson@17230
   812
    let val (lits,tfree_lits) = dfg_clause_aux cls 
paulson@17230
   813
             (*"lits" includes the typing assumptions (TVars)*)
quigley@17150
   814
        val vars = dfg_vars cls
quigley@17150
   815
        val tvars = dfg_tvars cls
quigley@17150
   816
	val knd = string_of_kind cls
paulson@17234
   817
	val lits_str = commas lits
paulson@17317
   818
	val cls_id = get_clause_id cls
paulson@17317
   819
	val axname = get_axiomName cls
paulson@17317
   820
	val cls_str = gen_dfg_cls(cls_id,axname,knd,lits_str,tvars, vars) 			
quigley@17150
   821
        fun typ_clss k [] = []
quigley@17150
   822
          | typ_clss k (tfree :: tfrees) = 
paulson@17317
   823
              (gen_dfg_type_cls(cls_id,axname,knd,tfree,k, tvars,vars)) :: 
paulson@17234
   824
              (typ_clss (k+1) tfrees)
quigley@17150
   825
    in 
quigley@17150
   826
	cls_str :: (typ_clss 0 tfree_lits)
quigley@17150
   827
    end;
quigley@17150
   828
paulson@17317
   829
fun string_of_arity (name, num) =  name ^ "," ^ (string_of_int num) 
quigley@17150
   830
paulson@17234
   831
fun string_of_preds preds = 
paulson@17234
   832
  "predicates[" ^ (concat_with ", " (map string_of_arity preds)) ^ "].\n";
quigley@17150
   833
paulson@17234
   834
fun string_of_funcs funcs =
paulson@17234
   835
  "functions[" ^ (concat_with ", " (map string_of_arity funcs)) ^ "].\n" ;
quigley@17150
   836
quigley@17150
   837
paulson@17234
   838
fun string_of_symbols predstr funcstr = 
paulson@17234
   839
  "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
quigley@17150
   840
quigley@17150
   841
paulson@17234
   842
fun string_of_axioms axstr = 
paulson@17234
   843
  "list_of_clauses(axioms,cnf).\n" ^ axstr ^ "end_of_list.\n\n";
quigley@17150
   844
quigley@17150
   845
paulson@17234
   846
fun string_of_conjectures conjstr = 
paulson@17234
   847
  "list_of_clauses(conjectures,cnf).\n" ^ conjstr ^ "end_of_list.\n\n";
quigley@17150
   848
paulson@17234
   849
fun string_of_descrip () = 
paulson@17234
   850
  "list_of_descriptions.\nname({*[ File     : ],[ Names    :]*}).\nauthor({*[ Source   :]*}).\nstatus(unknown).\ndescription({*[ Refs     :]*}).\nend_of_list.\n\n"
quigley@17150
   851
quigley@17150
   852
quigley@17150
   853
fun string_of_start name = "%------------------------------------------------------------------------------\nbegin_problem(" ^ name ^ ").\n\n";
quigley@17150
   854
quigley@17150
   855
quigley@17150
   856
fun string_of_end () = "end_problem.\n%------------------------------------------------------------------------------";
quigley@17150
   857
quigley@17150
   858
val delim = "\n";
quigley@17150
   859
val dfg_clauses2str = ResLib.list2str_sep delim; 
quigley@17150
   860
     
quigley@17150
   861
quigley@17150
   862
fun clause2dfg cls =
paulson@17234
   863
    let val (lits,tfree_lits) = dfg_clause_aux cls 
paulson@17234
   864
            (*"lits" includes the typing assumptions (TVars)*)
paulson@17317
   865
	val cls_id = get_clause_id cls
paulson@17317
   866
	val ax_name = get_axiomName cls
quigley@17150
   867
        val vars = dfg_vars cls
quigley@17150
   868
        val tvars = dfg_tvars cls
quigley@17150
   869
        val funcs = funcs_of_cls cls
quigley@17150
   870
        val preds = preds_of_cls cls
quigley@17150
   871
	val knd = string_of_kind cls
paulson@17234
   872
	val lits_str = commas lits
quigley@17150
   873
	val cls_str = gen_dfg_cls(cls_id,ax_name,knd,lits_str,tvars,vars) 
quigley@17150
   874
    in
quigley@17150
   875
	(cls_str,tfree_lits) 
quigley@17150
   876
    end;
quigley@17150
   877
quigley@17150
   878
quigley@17150
   879
paulson@17234
   880
fun tfree_dfg_clause tfree_lit =
paulson@17234
   881
  "clause( %(conjecture)\n" ^ "or( " ^ tfree_lit ^ ")),\n" ^ "tfree_tcs" ^ ")."
quigley@17150
   882
quigley@17150
   883
paulson@17230
   884
fun gen_dfg_file probname axioms conjectures funcs preds tfrees= 
paulson@17230
   885
    let val axstrs_tfrees = (map clause2dfg axioms)
quigley@17150
   886
	val (axstrs, atfrees) = ListPair.unzip axstrs_tfrees
quigley@17150
   887
        val axstr = ResLib.list2str_sep delim axstrs
paulson@17230
   888
        val conjstrs_tfrees = (map clause2dfg conjectures)
quigley@17150
   889
	val (conjstrs, atfrees) = ListPair.unzip conjstrs_tfrees
quigley@17150
   890
        val tfree_clss = map tfree_dfg_clause ((ResLib.flat_noDup atfrees) \\ tfrees) 
quigley@17150
   891
        val conjstr = ResLib.list2str_sep delim (tfree_clss@conjstrs)
quigley@17150
   892
        val funcstr = string_of_funcs funcs
quigley@17150
   893
        val predstr = string_of_preds preds
quigley@17150
   894
    in
paulson@17230
   895
       (string_of_start probname) ^ (string_of_descrip ()) ^ 
paulson@17230
   896
       (string_of_symbols funcstr predstr ) ^  
paulson@17230
   897
       (string_of_axioms axstr) ^
paulson@17230
   898
       (string_of_conjectures conjstr) ^ (string_of_end ())
quigley@17150
   899
    end;
quigley@17150
   900
   
paulson@17230
   901
fun clauses2dfg [] probname axioms conjectures funcs preds tfrees = 
paulson@17230
   902
      let val funcs' = (ResLib.flat_noDup(map funcs_of_cls axioms)) @ funcs
paulson@17230
   903
	  val preds' = (ResLib.flat_noDup(map preds_of_cls axioms)) @ preds
paulson@17230
   904
      in
paulson@17230
   905
	 gen_dfg_file probname axioms conjectures funcs' preds' tfrees 
paulson@17230
   906
	 (*(probname, axioms, conjectures, funcs, preds)*)
paulson@17230
   907
      end
paulson@17230
   908
 | clauses2dfg (cls::clss) probname axioms conjectures funcs preds tfrees = 
paulson@17230
   909
     let val (lits,tfree_lits) = dfg_clause_aux cls
paulson@17230
   910
	       (*"lits" includes the typing assumptions (TVars)*)
paulson@17317
   911
	 val cls_id = get_clause_id cls
paulson@17317
   912
	 val ax_name = get_axiomName cls
paulson@17230
   913
	 val vars = dfg_vars cls
paulson@17230
   914
	 val tvars = dfg_tvars cls
paulson@17230
   915
	 val funcs' = distinct((funcs_of_cls cls)@funcs)
paulson@17230
   916
	 val preds' = distinct((preds_of_cls cls)@preds)
paulson@17230
   917
	 val knd = string_of_kind cls
paulson@17230
   918
	 val lits_str = concat_with ", " lits
paulson@17230
   919
	 val axioms' = if knd = "axiom" then (cls::axioms) else axioms
paulson@17230
   920
	 val conjectures' = 
paulson@17230
   921
	     if knd = "conjecture" then (cls::conjectures) else conjectures
paulson@17230
   922
     in
paulson@17230
   923
	 clauses2dfg clss probname axioms' conjectures' funcs' preds' tfrees 
paulson@17230
   924
     end;
quigley@17150
   925
quigley@17150
   926
paulson@17230
   927
fun fileout f str = let val out = TextIO.openOut f
quigley@17150
   928
    in
paulson@17230
   929
	ResLib.writeln_strs out str; TextIO.closeOut out
quigley@17150
   930
    end;
quigley@17150
   931
quigley@17150
   932
quigley@17150
   933
paulson@17305
   934
fun string_of_arClauseID (ArityClause arcls) =
paulson@17305
   935
    arclause_prefix ^ string_of_int(#clause_id arcls);
quigley@17150
   936
quigley@17150
   937
fun string_of_arLit (TConsLit(b,(c,t,args))) =
quigley@17150
   938
    let val pol = if b then "++" else "--"
paulson@17305
   939
	val arg_strs = (case args of [] => "" | _ => ResLib.list_to_string args)
quigley@17150
   940
    in 
quigley@17150
   941
	pol ^ c ^ "(" ^ t ^ arg_strs ^ ")"
quigley@17150
   942
    end
quigley@17150
   943
  | string_of_arLit (TVarLit(b,(c,str))) =
quigley@17150
   944
    let val pol = if b then "++" else "--"
quigley@17150
   945
    in
quigley@17150
   946
	pol ^ c ^ "(" ^ str ^ ")"
quigley@17150
   947
    end;
paulson@15347
   948
    
paulson@15347
   949
quigley@17150
   950
fun string_of_conclLit (ArityClause arcls) = string_of_arLit (#conclLit arcls);
quigley@17150
   951
     
quigley@17150
   952
quigley@17150
   953
fun strings_of_premLits (ArityClause arcls) = map string_of_arLit (#premLits arcls);
quigley@17150
   954
		
quigley@17150
   955
quigley@17150
   956
fun string_of_arKind (ArityClause arcls) = name_of_kind(#kind arcls);
quigley@17150
   957
quigley@17150
   958
(*FIX: would this have variables in a forall? *)
quigley@17150
   959
quigley@17150
   960
fun dfg_arity_clause arcls = 
quigley@17150
   961
    let val arcls_id = string_of_arClauseID arcls
quigley@17150
   962
	val concl_lit = string_of_conclLit arcls
quigley@17150
   963
	val prems_lits = strings_of_premLits arcls
quigley@17150
   964
	val knd = string_of_arKind arcls
quigley@17150
   965
	val all_lits = concl_lit :: prems_lits
quigley@17150
   966
    in
quigley@17150
   967
paulson@17230
   968
	"clause( %(" ^ knd ^ ")\n" ^  "or( " ^ (ResLib.list_to_string' all_lits) ^ ")),\n" ^
paulson@17230
   969
	 arcls_id ^  ")."
quigley@17150
   970
    end;
quigley@17150
   971
quigley@17150
   972
quigley@17150
   973
(********************************)
quigley@17150
   974
(* code to produce TPTP files   *)
quigley@17150
   975
(********************************)
quigley@17150
   976
paulson@15347
   977
fun tptp_literal (Literal(pol,pred,tag)) =
paulson@15347
   978
    let val pred_string = string_of_predicate pred
paulson@17230
   979
	val tagged_pol = 
paulson@17230
   980
	      if (tag andalso !tagged) then (if pol then "+++" else "---")
paulson@17230
   981
	      else (if pol then "++" else "--")
paulson@15347
   982
     in
paulson@15347
   983
	tagged_pol ^ pred_string
paulson@15347
   984
    end;
paulson@15347
   985
paulson@15347
   986
paulson@15347
   987
paulson@15347
   988
fun tptp_of_typeLit (LTVar x) = "--" ^ x
paulson@15347
   989
  | tptp_of_typeLit (LTFree x) = "++" ^ x;
paulson@15347
   990
 
paulson@15347
   991
paulson@15347
   992
fun gen_tptp_cls (cls_id,ax_name,knd,lits) = 
paulson@17317
   993
    "input_clause(" ^ string_of_clausename (cls_id,ax_name) ^ "," ^ 
paulson@17317
   994
    knd ^ "," ^ lits ^ ").";
paulson@15347
   995
paulson@17317
   996
fun gen_tptp_type_cls (cls_id,ax_name,knd,tfree_lit,idx) = 
paulson@17317
   997
    "input_clause(" ^ string_of_type_clsname (cls_id,ax_name,idx) ^ "," ^ 
paulson@17230
   998
    knd ^ ",[" ^ tfree_lit ^ "]).";
paulson@15347
   999
paulson@15347
  1000
fun tptp_clause_aux (Clause cls) = 
paulson@15347
  1001
    let val lits = map tptp_literal (#literals cls)
paulson@17230
  1002
	val tvar_lits_strs =
paulson@17230
  1003
	      if !keep_types 
paulson@17230
  1004
	      then (map tptp_of_typeLit (#tvar_type_literals cls)) 
paulson@17230
  1005
	      else []
paulson@17230
  1006
	val tfree_lits = 
paulson@17230
  1007
	      if !keep_types
paulson@17230
  1008
	      then (map tptp_of_typeLit (#tfree_type_literals cls)) 
paulson@17230
  1009
	      else []
paulson@15347
  1010
    in
paulson@17305
  1011
	(tvar_lits_strs @ lits, tfree_lits)
paulson@15347
  1012
    end; 
paulson@15347
  1013
paulson@15347
  1014
fun tptp_clause cls =
paulson@17305
  1015
    let val (lits,tfree_lits) = tptp_clause_aux cls 
paulson@17305
  1016
            (*"lits" includes the typing assumptions (TVars)*)
paulson@17317
  1017
	val cls_id = get_clause_id cls
paulson@17317
  1018
	val ax_name = get_axiomName cls
paulson@15347
  1019
	val knd = string_of_kind cls
paulson@15347
  1020
	val lits_str = ResLib.list_to_string' lits
paulson@17305
  1021
	val cls_str = gen_tptp_cls(cls_id,ax_name,knd,lits_str) 			 
paulson@17305
  1022
	fun typ_clss k [] = []
paulson@15347
  1023
          | typ_clss k (tfree :: tfrees) = 
paulson@17317
  1024
              gen_tptp_type_cls(cls_id,ax_name,knd,tfree,k) :: 
paulson@17317
  1025
              typ_clss (k+1) tfrees
paulson@15347
  1026
    in 
paulson@15347
  1027
	cls_str :: (typ_clss 0 tfree_lits)
paulson@15347
  1028
    end;
paulson@15347
  1029
paulson@15608
  1030
fun clause2tptp cls =
paulson@17305
  1031
    let val (lits,tfree_lits) = tptp_clause_aux cls 
paulson@17305
  1032
            (*"lits" includes the typing assumptions (TVars)*)
paulson@17317
  1033
	val cls_id = get_clause_id cls
paulson@17317
  1034
	val ax_name = get_axiomName cls
paulson@15608
  1035
	val knd = string_of_kind cls
paulson@15608
  1036
	val lits_str = ResLib.list_to_string' lits
paulson@15608
  1037
	val cls_str = gen_tptp_cls(cls_id,ax_name,knd,lits_str) 
paulson@15608
  1038
    in
paulson@15608
  1039
	(cls_str,tfree_lits) 
paulson@15608
  1040
    end;
paulson@15608
  1041
paulson@15608
  1042
paulson@17230
  1043
fun tfree_clause tfree_lit =
paulson@17230
  1044
    "input_clause(" ^ "tfree_tcs," ^ "conjecture" ^ ",[" ^ tfree_lit ^ "]).";
paulson@15608
  1045
paulson@15347
  1046
val delim = "\n";
paulson@15347
  1047
val tptp_clauses2str = ResLib.list2str_sep delim; 
paulson@15347
  1048
     
paulson@15347
  1049
paulson@17230
  1050
fun string_of_arClauseID (ArityClause arcls) =
paulson@17230
  1051
  arclause_prefix ^ string_of_int(#clause_id arcls);
paulson@15347
  1052
paulson@15347
  1053
paulson@15347
  1054
fun string_of_arLit (TConsLit(b,(c,t,args))) =
paulson@15347
  1055
    let val pol = if b then "++" else "--"
paulson@15347
  1056
	val  arg_strs = (case args of [] => "" | _ => ResLib.list_to_string args)
paulson@15347
  1057
    in 
paulson@15347
  1058
	pol ^ c ^ "(" ^ t ^ arg_strs ^ ")"
paulson@15347
  1059
    end
paulson@15347
  1060
  | string_of_arLit (TVarLit(b,(c,str))) =
paulson@15347
  1061
    let val pol = if b then "++" else "--"
paulson@15347
  1062
    in
paulson@15347
  1063
	pol ^ c ^ "(" ^ str ^ ")"
paulson@15347
  1064
    end;
paulson@15347
  1065
    
paulson@15347
  1066
paulson@15347
  1067
fun string_of_conclLit (ArityClause arcls) = string_of_arLit (#conclLit arcls);
paulson@15347
  1068
     
paulson@17234
  1069
fun strings_of_premLits (ArityClause arcls) =
paulson@17234
  1070
 map string_of_arLit (#premLits arcls);
paulson@15347
  1071
		
paulson@15347
  1072
paulson@15347
  1073
fun string_of_arKind (ArityClause arcls) = name_of_kind(#kind arcls);
paulson@15347
  1074
paulson@15347
  1075
fun tptp_arity_clause arcls = 
paulson@15347
  1076
    let val arcls_id = string_of_arClauseID arcls
paulson@15347
  1077
	val concl_lit = string_of_conclLit arcls
paulson@15347
  1078
	val prems_lits = strings_of_premLits arcls
paulson@15347
  1079
	val knd = string_of_arKind arcls
paulson@15347
  1080
	val all_lits = concl_lit :: prems_lits
paulson@15347
  1081
    in
paulson@17317
  1082
	"input_clause(" ^ arcls_id ^ "," ^ knd ^ "," ^ 
paulson@17317
  1083
	(ResLib.list_to_string' all_lits) ^ ")."
paulson@15347
  1084
    end;
paulson@15347
  1085
paulson@15347
  1086
fun tptp_classrelLits sub sup = 
paulson@15347
  1087
    let val tvar = "(T)"
paulson@15347
  1088
    in 
skalberg@15531
  1089
	case sup of NONE => "[++" ^ sub ^ tvar ^ "]"
skalberg@15531
  1090
		  | (SOME supcls) =>  "[--" ^ sub ^ tvar ^ ",++" ^ supcls ^ tvar ^ "]"
paulson@15347
  1091
    end;
paulson@15347
  1092
paulson@15347
  1093
paulson@15347
  1094
fun tptp_classrelClause (ClassrelClause cls) =
paulson@15347
  1095
    let val relcls_id = clrelclause_prefix ^ string_of_int(#clause_id cls)
paulson@15347
  1096
	val sub = #subclass cls
paulson@15347
  1097
	val sup = #superclass cls
paulson@15347
  1098
	val lits = tptp_classrelLits sub sup
paulson@15347
  1099
    in
paulson@15347
  1100
	"input_clause(" ^ relcls_id ^ ",axiom," ^ lits ^ ")."
paulson@15347
  1101
    end; 
quigley@17150
  1102
paulson@15347
  1103
end;