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