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