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