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