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