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