src/HOL/Tools/res_clause.ML
author haftmann
Wed Oct 04 14:17:38 2006 +0200 (2006-10-04)
changeset 20854 f9cf9e62d11c
parent 20824 aca7d38283bf
child 21254 d53f76357f41
permissions -rw-r--r--
insert replacing ins ins_int ins_string
paulson@15347
     1
(*  Author: Jia Meng, Cambridge University Computer Laboratory
paulson@15347
     2
    ID: $Id$
paulson@15347
     3
    Copyright 2004 University of Cambridge
paulson@15347
     4
paulson@15347
     5
ML data structure for storing/printing FOL clauses and arity clauses.
paulson@15347
     6
Typed equality is treated differently.
paulson@15347
     7
*)
paulson@15347
     8
paulson@20422
     9
(*FIXME: is this signature necessary? Or maybe define and open a Basic_ResClause?*)
paulson@15347
    10
signature RES_CLAUSE =
paulson@15347
    11
  sig
paulson@18868
    12
  exception CLAUSE of string * term
paulson@18868
    13
  type clause and arityClause and classrelClause
paulson@20824
    14
  datatype fol_type = AtomV of string
paulson@20824
    15
                    | AtomF of string
paulson@20824
    16
                    | Comp of string * fol_type list;
paulson@20824
    17
  datatype fol_term = UVar of string * fol_type
paulson@20824
    18
                    | Fun of string * fol_type list * fol_term list;
paulson@20824
    19
  datatype predicate = Predicate of string * fol_type list * fol_term list;
paulson@20824
    20
  type typ_var and type_literal and literal
paulson@20824
    21
  val literals_of_term: Term.term -> literal list * (typ_var * Term.sort) list
paulson@18868
    22
  val add_typs_aux : (typ_var * string list) list -> type_literal list * type_literal list
paulson@18868
    23
  val arity_clause_thy: theory -> arityClause list 
paulson@18868
    24
  val ascii_of : string -> string
paulson@18868
    25
  val bracket_pack : string list -> string
paulson@18868
    26
  val classrel_clauses_thy: theory -> classrelClause list 
paulson@18868
    27
  val clause_prefix : string 
paulson@18868
    28
  val clause2tptp : clause -> string * string list
paulson@18868
    29
  val const_prefix : string
paulson@20022
    30
  val dfg_write_file:  thm list -> string -> 
paulson@20022
    31
       ((thm * (string * int)) list * classrelClause list * arityClause list) -> string list
paulson@18868
    32
  val fixed_var_prefix : string
paulson@18868
    33
  val gen_tptp_cls : int * string * string * string -> string
paulson@18868
    34
  val gen_tptp_type_cls : int * string * string * string * int -> string
paulson@18868
    35
  val get_axiomName : clause ->  string
paulson@20824
    36
  val get_literals : clause -> literal list
paulson@18868
    37
  val init : theory -> unit
paulson@18868
    38
  val isMeta : string -> bool
paulson@18868
    39
  val isTaut : clause -> bool
paulson@17404
    40
  val keep_types : bool ref
paulson@18868
    41
  val list_ord : ('a * 'b -> order) -> 'a list * 'b list -> order
mengj@19443
    42
  val make_axiom_clause : thm -> string * int -> clause option
mengj@19443
    43
  val make_conjecture_clauses : thm list -> clause list
paulson@18868
    44
  val make_fixed_const : string -> string		
paulson@18868
    45
  val make_fixed_type_const : string -> string   
paulson@18868
    46
  val make_fixed_type_var : string -> string
paulson@18868
    47
  val make_fixed_var : string -> string
paulson@18868
    48
  val make_schematic_type_var : string * int -> string
paulson@18868
    49
  val make_schematic_var : string * int -> string
paulson@18868
    50
  val make_type_class : string -> string
paulson@18868
    51
  val mk_fol_type: string * string * fol_type list -> fol_type
paulson@18868
    52
  val mk_typ_var_sort : Term.typ -> typ_var * sort
paulson@18868
    53
  val paren_pack : string list -> string
paulson@18868
    54
  val schematic_var_prefix : string
paulson@18868
    55
  val special_equal : bool ref
paulson@18868
    56
  val string_of_fol_type : fol_type -> string
paulson@18868
    57
  val tconst_prefix : string 
paulson@18868
    58
  val tfree_prefix : string
paulson@17404
    59
  val tptp_arity_clause : arityClause -> string
paulson@17404
    60
  val tptp_classrelClause : classrelClause -> string
paulson@18868
    61
  val tptp_of_typeLit : type_literal -> string
paulson@18863
    62
  val tptp_tfree_clause : string -> string
paulson@20022
    63
  val tptp_write_file: thm list -> string -> 
paulson@20022
    64
       ((thm * (string * int)) list * classrelClause list * arityClause list) -> string list
paulson@17404
    65
  val tvar_prefix : string
mengj@17908
    66
  val union_all : ''a list list -> ''a list
paulson@18863
    67
  val writeln_strs: TextIO.outstream -> TextIO.vector list -> unit
mengj@19719
    68
  val dfg_sign: bool -> string -> string
mengj@19719
    69
  val dfg_of_typeLit: type_literal -> string
mengj@19719
    70
  val get_tvar_strs: (typ_var * sort) list -> string list
mengj@19719
    71
  val gen_dfg_cls: int * string * string * string * string list -> string
mengj@19719
    72
  val add_foltype_funcs: fol_type * int Symtab.table -> int Symtab.table
mengj@19719
    73
  val add_arityClause_funcs: arityClause * int Symtab.table -> int Symtab.table
mengj@19719
    74
  val add_arityClause_preds: arityClause * int Symtab.table -> int Symtab.table
mengj@19719
    75
  val add_classrelClause_preds : classrelClause * int Symtab.table -> int Symtab.table
mengj@19719
    76
  val dfg_tfree_clause : string -> string
mengj@19719
    77
  val string_of_start: string -> string
mengj@19719
    78
  val string_of_descrip : string -> string
mengj@19719
    79
  val string_of_symbols: string -> string -> string
mengj@19719
    80
  val string_of_funcs: (string * int) list -> string
mengj@19719
    81
  val string_of_preds: (string * Int.int) list -> string
mengj@19719
    82
  val dfg_classrelClause: classrelClause -> string
mengj@19719
    83
  val dfg_arity_clause: arityClause -> string
mengj@19719
    84
end;
paulson@15347
    85
paulson@20422
    86
structure ResClause =
paulson@15347
    87
struct
paulson@15347
    88
paulson@15347
    89
(* Added for typed equality *)
paulson@15347
    90
val special_equal = ref false; (* by default,equality does not carry type information *)
paulson@15347
    91
val eq_typ_wrapper = "typeinfo"; (* default string *)
paulson@15347
    92
paulson@15347
    93
paulson@15347
    94
val schematic_var_prefix = "V_";
paulson@15347
    95
val fixed_var_prefix = "v_";
paulson@15347
    96
paulson@17230
    97
val tvar_prefix = "T_";
paulson@17230
    98
val tfree_prefix = "t_";
paulson@15347
    99
paulson@15347
   100
val clause_prefix = "cls_"; 
paulson@17525
   101
val arclause_prefix = "clsarity_" 
paulson@17525
   102
val clrelclause_prefix = "clsrel_";
paulson@15347
   103
paulson@17230
   104
val const_prefix = "c_";
paulson@17230
   105
val tconst_prefix = "tc_"; 
paulson@16199
   106
val class_prefix = "class_"; 
paulson@15347
   107
paulson@17775
   108
fun union_all xss = foldl (op union) [] xss;
paulson@17775
   109
paulson@17775
   110
(*Provide readable names for the more common symbolic functions*)
paulson@15347
   111
val const_trans_table =
paulson@15347
   112
      Symtab.make [("op =", "equal"),
haftmann@19277
   113
	  	   ("Orderings.less_eq", "lessequals"),
haftmann@19277
   114
		   ("Orderings.less", "less"),
paulson@15347
   115
		   ("op &", "and"),
paulson@15347
   116
		   ("op |", "or"),
haftmann@19233
   117
		   ("HOL.plus", "plus"),
haftmann@19233
   118
		   ("HOL.minus", "minus"),
haftmann@19233
   119
		   ("HOL.times", "times"),
paulson@18676
   120
		   ("Divides.op div", "div"),
paulson@18676
   121
		   ("HOL.divide", "divide"),
paulson@15347
   122
		   ("op -->", "implies"),
paulson@17375
   123
		   ("{}", "emptyset"),
paulson@15347
   124
		   ("op :", "in"),
paulson@15347
   125
		   ("op Un", "union"),
paulson@18390
   126
		   ("op Int", "inter"),
mengj@20790
   127
		   ("List.op @", "append"),
mengj@20790
   128
		   ("Reconstruction.fequal", "fequal"),
mengj@20790
   129
		   ("Reconstruction.COMBI", "COMBI"),
mengj@20790
   130
		   ("Reconstruction.COMBK", "COMBK"),
mengj@20790
   131
		   ("Reconstruction.COMBB", "COMBB"),
mengj@20790
   132
		   ("Reconstruction.COMBC", "COMBC"),
mengj@20790
   133
		   ("Reconstruction.COMBS", "COMBS"),
mengj@20790
   134
		   ("Reconstruction.COMBB'", "COMBB_e"),
mengj@20790
   135
		   ("Reconstruction.COMBC'", "COMBC_e"),
mengj@20790
   136
		   ("Reconstruction.COMBS'", "COMBS_e")];
paulson@15347
   137
paulson@17230
   138
val type_const_trans_table =
paulson@18411
   139
      Symtab.make [("*", "prod"),
paulson@18411
   140
	  	   ("+", "sum"),
paulson@18411
   141
		   ("~=>", "map")];
paulson@15347
   142
paulson@15610
   143
(*Escaping of special characters.
paulson@15610
   144
  Alphanumeric characters are left unchanged.
paulson@15610
   145
  The character _ goes to __
paulson@15610
   146
  Characters in the range ASCII space to / go to _A to _P, respectively.
paulson@15610
   147
  Other printing characters go to _NNN where NNN is the decimal ASCII code.*)
paulson@15610
   148
local
paulson@15610
   149
paulson@15610
   150
val A_minus_space = Char.ord #"A" - Char.ord #" ";
paulson@15610
   151
paulson@15347
   152
fun ascii_of_c c =
paulson@15610
   153
  if Char.isAlphaNum c then String.str c
paulson@15610
   154
  else if c = #"_" then "__"
paulson@15610
   155
  else if #" " <= c andalso c <= #"/" 
paulson@15610
   156
       then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
paulson@15610
   157
  else if Char.isPrint c then ("_" ^ Int.toString (Char.ord c))
paulson@15610
   158
  else ""
paulson@15347
   159
paulson@15610
   160
in
paulson@15610
   161
paulson@15610
   162
val ascii_of = String.translate ascii_of_c;
paulson@15610
   163
paulson@15610
   164
end;
paulson@15347
   165
paulson@17525
   166
(* convert a list of strings into one single string; surrounded by brackets *)
paulson@18218
   167
fun paren_pack [] = ""   (*empty argument list*)
paulson@18218
   168
  | paren_pack strings = "(" ^ commas strings ^ ")";
paulson@17525
   169
paulson@17525
   170
fun bracket_pack strings = "[" ^ commas strings ^ "]";
paulson@17525
   171
paulson@17525
   172
paulson@16925
   173
(*Remove the initial ' character from a type variable, if it is present*)
paulson@16925
   174
fun trim_type_var s =
paulson@16925
   175
  if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
paulson@16925
   176
  else error ("trim_type: Malformed type variable encountered: " ^ s);
paulson@16925
   177
paulson@16903
   178
fun ascii_of_indexname (v,0) = ascii_of v
paulson@17525
   179
  | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
paulson@15347
   180
paulson@17230
   181
fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
paulson@15347
   182
fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
paulson@15347
   183
paulson@16925
   184
fun make_schematic_type_var (x,i) = 
paulson@16925
   185
      tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
paulson@16925
   186
fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
paulson@15347
   187
paulson@18411
   188
fun lookup_const c =
wenzelm@17412
   189
    case Symtab.lookup const_trans_table c of
paulson@17230
   190
        SOME c' => c'
paulson@18411
   191
      | NONE => ascii_of c;
paulson@17230
   192
paulson@18411
   193
fun lookup_type_const c = 
wenzelm@17412
   194
    case Symtab.lookup type_const_trans_table c of
paulson@17230
   195
        SOME c' => c'
paulson@18411
   196
      | NONE => ascii_of c;
paulson@18411
   197
paulson@18411
   198
fun make_fixed_const "op =" = "equal"   (*MUST BE "equal" because it's built-in to ATPs*)
paulson@18411
   199
  | make_fixed_const c      = const_prefix ^ lookup_const c;
paulson@18411
   200
paulson@18411
   201
fun make_fixed_type_const c = tconst_prefix ^ lookup_type_const c;
quigley@17150
   202
wenzelm@17261
   203
fun make_type_class clas = class_prefix ^ ascii_of clas;
quigley@17150
   204
quigley@17150
   205
paulson@18798
   206
(***** definitions and functions for FOL clauses, for conversion to TPTP or DFG format. *****)
paulson@15347
   207
paulson@17230
   208
val keep_types = ref true;
paulson@15347
   209
paulson@15347
   210
datatype kind = Axiom | Hypothesis | Conjecture;
paulson@15347
   211
fun name_of_kind Axiom = "axiom"
paulson@15347
   212
  | name_of_kind Hypothesis = "hypothesis"
paulson@15347
   213
  | name_of_kind Conjecture = "conjecture";
paulson@15347
   214
paulson@15347
   215
type clause_id = int;
paulson@15347
   216
type axiom_name = string;
paulson@15347
   217
type polarity = bool;
paulson@15347
   218
paulson@15347
   219
(**** Isabelle FOL clauses ****)
paulson@15347
   220
mengj@18402
   221
datatype typ_var = FOLTVar of indexname | FOLTFree of string;
mengj@18402
   222
paulson@18798
   223
(*FIXME: give the constructors more sensible names*)
mengj@18402
   224
datatype fol_type = AtomV of string
mengj@18402
   225
		  | AtomF of string
mengj@18402
   226
		  | Comp of string * fol_type list;
mengj@18402
   227
mengj@18402
   228
fun string_of_fol_type (AtomV x) = x
mengj@18402
   229
  | string_of_fol_type (AtomF x) = x
mengj@18402
   230
  | string_of_fol_type (Comp(tcon,tps)) = 
paulson@18856
   231
      tcon ^ (paren_pack (map string_of_fol_type tps));
paulson@18856
   232
      
mengj@18439
   233
fun mk_fol_type ("Var",x,_) = AtomV(x)
mengj@18439
   234
  | mk_fol_type ("Fixed",x,_) = AtomF(x)
mengj@18439
   235
  | mk_fol_type ("Comp",con,args) = Comp(con,args)
mengj@18439
   236
mengj@18439
   237
paulson@18798
   238
(*First string is the type class; the second is a TVar or TFfree*)
paulson@18798
   239
datatype type_literal = LTVar of string * string | LTFree of string * string;
paulson@15347
   240
paulson@18856
   241
datatype fol_term = UVar of string * fol_type
paulson@20038
   242
                  | Fun of string * fol_type list * fol_term list;
paulson@20824
   243
datatype predicate = Predicate of string * fol_type list * fol_term list;
paulson@15347
   244
paulson@20018
   245
datatype literal = Literal of polarity * predicate;
paulson@15347
   246
mengj@17999
   247
fun mk_typ_var_sort (TFree(a,s)) = (FOLTFree a,s)
mengj@17999
   248
  | mk_typ_var_sort (TVar(v,s)) = (FOLTVar v,s);
mengj@17999
   249
mengj@17999
   250
paulson@18856
   251
(*A clause has first-order literals and other, type-related literals*)
paulson@15347
   252
datatype clause = 
paulson@15347
   253
	 Clause of {clause_id: clause_id,
paulson@15347
   254
		    axiom_name: axiom_name,
paulson@19447
   255
		    th: thm,
paulson@15347
   256
		    kind: kind,
paulson@15347
   257
		    literals: literal list,
paulson@18869
   258
		    types_sorts: (typ_var * sort) list};
paulson@18869
   259
paulson@18869
   260
fun get_axiomName (Clause cls) = #axiom_name cls;
paulson@15347
   261
paulson@20824
   262
fun get_literals (Clause cls) = #literals cls;
paulson@20824
   263
paulson@17404
   264
exception CLAUSE of string * term;
paulson@15347
   265
paulson@20018
   266
fun isFalse (Literal (pol, Predicate(pname,_,[]))) =
paulson@18856
   267
      (pol andalso pname = "c_False") orelse
paulson@18856
   268
      (not pol andalso pname = "c_True")
paulson@17317
   269
  | isFalse _ = false;
paulson@17317
   270
paulson@20018
   271
fun isTrue (Literal (pol, Predicate(pname,_,[]))) =
paulson@18856
   272
      (pol andalso pname = "c_True") orelse
paulson@18856
   273
      (not pol andalso pname = "c_False")
paulson@17404
   274
  | isTrue _ = false;
paulson@17404
   275
  
paulson@17404
   276
fun isTaut (Clause {literals,...}) = exists isTrue literals;  
paulson@17404
   277
paulson@17317
   278
paulson@18218
   279
(*Declarations of the current theory--to allow suppressing types.*)
paulson@18218
   280
val const_typargs = ref (Library.K [] : (string*typ -> typ list));
paulson@17317
   281
paulson@18218
   282
fun num_typargs(s,T) = if !keep_types then length (!const_typargs (s,T)) else 0;
paulson@16925
   283
paulson@16925
   284
(*Initialize the type suppression mechanism with the current theory before
paulson@16925
   285
    producing any clauses!*)
paulson@18218
   286
fun init thy = (const_typargs := Sign.const_typargs thy);
paulson@16925
   287
    
quigley@17150
   288
mengj@18402
   289
(*Flatten a type to a fol_type while accumulating sort constraints on the TFrees and
paulson@16925
   290
  TVars it contains.*)    
paulson@18218
   291
fun type_of (Type (a, Ts)) = 
paulson@18798
   292
      let val (folTyps, ts) = types_of Ts 
paulson@18218
   293
	  val t = make_fixed_type_const a
paulson@18798
   294
      in (Comp(t,folTyps), ts) end
paulson@18798
   295
  | type_of (TFree (a,s)) = (AtomF(make_fixed_type_var a), [(FOLTFree a, s)]) 
paulson@18798
   296
  | type_of (TVar (v, s)) = (AtomV(make_schematic_type_var v), [(FOLTVar v, s)])
paulson@18218
   297
and types_of Ts =
paulson@18798
   298
      let val (folTyps,ts) = ListPair.unzip (map type_of Ts)
paulson@18798
   299
      in (folTyps, union_all ts) end;
paulson@15390
   300
mengj@18439
   301
paulson@18218
   302
fun const_types_of (c,T) = types_of (!const_typargs (c,T));
paulson@16925
   303
paulson@16903
   304
(* Any variables created via the METAHYPS tactical should be treated as
paulson@16903
   305
   universal vars, although it is represented as "Free(...)" by Isabelle *)
paulson@16903
   306
val isMeta = String.isPrefix "METAHYP1_"
quigley@17150
   307
paulson@18798
   308
fun pred_name_type (Const(c,T)) = (make_fixed_const c, const_types_of (c,T))
paulson@15390
   309
  | pred_name_type (Free(x,T))  = 
paulson@17404
   310
      if isMeta x then raise CLAUSE("Predicate Not First Order 1", Free(x,T)) 
paulson@18798
   311
      else (make_fixed_var x, ([],[]))
paulson@17404
   312
  | pred_name_type (v as Var _) = raise CLAUSE("Predicate Not First Order 2", v)
paulson@17404
   313
  | pred_name_type t        = raise CLAUSE("Predicate input unexpected", t);
paulson@15347
   314
paulson@15615
   315
paulson@18218
   316
(* For typed equality *)
paulson@15615
   317
(* here "arg_typ" is the type of "="'s argument's type, not the type of the equality *)
paulson@15615
   318
(* Find type of equality arg *)
paulson@15615
   319
fun eq_arg_type (Type("fun",[T,_])) = 
paulson@15615
   320
    let val (folT,_) = type_of T;
paulson@17230
   321
    in  folT  end;
paulson@15347
   322
paulson@18798
   323
fun fun_name_type (Const(c,T)) args = (make_fixed_const c, const_types_of (c,T))
paulson@18798
   324
  | fun_name_type (Free(x,T)) args  = 
paulson@18798
   325
       if isMeta x then raise CLAUSE("Function Not First Order", Free(x,T))
paulson@18798
   326
       else (make_fixed_var x, ([],[]))
paulson@17404
   327
  | fun_name_type f args = raise CLAUSE("Function Not First Order 1", f);
quigley@17150
   328
paulson@18856
   329
(*Convert a term to a fol_term while accumulating sort constraints on the TFrees and
paulson@18856
   330
  TVars it contains.*)    
paulson@15347
   331
fun term_of (Var(ind_nm,T)) = 
paulson@18798
   332
      let val (folType,ts) = type_of T
paulson@18798
   333
      in (UVar(make_schematic_var ind_nm, folType), ts) end
paulson@15347
   334
  | term_of (Free(x,T)) = 
paulson@18798
   335
      let val (folType, ts) = type_of T
paulson@17230
   336
      in
paulson@18798
   337
	  if isMeta x then (UVar(make_schematic_var(x,0),folType), ts)
paulson@20038
   338
	  else (Fun(make_fixed_var x, [], []), ts)  (*Frees don't need types!*)
paulson@17230
   339
      end
paulson@18218
   340
  | term_of app = 
paulson@17230
   341
      let val (f,args) = strip_comb app
paulson@18798
   342
	  val (funName,(contys,ts1)) = fun_name_type f args
paulson@18798
   343
	  val (args',ts2) = terms_of args
paulson@17230
   344
      in
paulson@18868
   345
	  (Fun(funName,contys,args'), union_all (ts1::ts2))
paulson@17230
   346
      end
paulson@18798
   347
and terms_of ts = ListPair.unzip (map term_of ts)
paulson@15390
   348
paulson@18856
   349
(*Create a predicate value, again accumulating sort constraints.*)    
paulson@17404
   350
fun pred_of (Const("op =", typ), args) =
paulson@17404
   351
      let val arg_typ = eq_arg_type typ 
paulson@18798
   352
	  val (args',ts) = terms_of args
paulson@17404
   353
	  val equal_name = make_fixed_const "op ="
paulson@17404
   354
      in
paulson@18218
   355
	  (Predicate(equal_name,[arg_typ],args'),
paulson@18856
   356
	   union_all ts)
paulson@17404
   357
      end
paulson@17404
   358
  | pred_of (pred,args) = 
paulson@18856
   359
      let val (pname, (predType,ts1)) = pred_name_type pred
paulson@18798
   360
	  val (args',ts2) = terms_of args
paulson@17404
   361
      in
paulson@18856
   362
	  (Predicate(pname,predType,args'), union_all (ts1::ts2))
paulson@17404
   363
      end;
paulson@15347
   364
paulson@20018
   365
(*Treatment of literals, possibly negated*)
paulson@20018
   366
fun predicate_of ((Const("Not",_) $ P), polarity) = predicate_of (P, not polarity)
paulson@20018
   367
  | predicate_of (term,polarity) = (pred_of (strip_comb term), polarity);
paulson@15347
   368
paulson@17888
   369
fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
paulson@18856
   370
  | literals_of_term1 args (Const("op |",_) $ P $ Q) = 
paulson@18856
   371
      literals_of_term1 (literals_of_term1 args P) Q
paulson@18856
   372
  | literals_of_term1 (lits, ts) P =
paulson@20018
   373
      let val ((pred, ts'), polarity) = predicate_of (P,true)
paulson@20018
   374
	  val lits' = Literal(polarity,pred) :: lits
paulson@17234
   375
      in
paulson@18856
   376
	  (lits', ts union ts')
paulson@17234
   377
      end;
quigley@17150
   378
paulson@18856
   379
val literals_of_term = literals_of_term1 ([],[]);
quigley@17150
   380
mengj@18403
   381
mengj@18403
   382
fun list_ord _ ([],[]) = EQUAL
mengj@18403
   383
  | list_ord _ ([],_) = LESS
mengj@18403
   384
  | list_ord _ (_,[]) = GREATER
paulson@19207
   385
  | list_ord ord (x::xs, y::ys) = 
paulson@19207
   386
      (case ord(x,y) of EQUAL => list_ord ord (xs,ys)
paulson@19207
   387
	 	      | xy_ord => xy_ord);
paulson@19207
   388
		     
paulson@18798
   389
(*Make literals for sorted type variables.  FIXME: can it use map?*) 
quigley@17150
   390
fun sorts_on_typs (_, [])   = ([]) 
paulson@16199
   391
  | sorts_on_typs (v, "HOL.type" :: s) =
paulson@18411
   392
      sorts_on_typs (v,s)                (*IGNORE sort "type"*)
paulson@18798
   393
  | sorts_on_typs ((FOLTVar indx), s::ss) =
paulson@18798
   394
      LTVar(make_type_class s, make_schematic_type_var indx) :: 
paulson@18798
   395
      sorts_on_typs ((FOLTVar indx), ss)
paulson@18798
   396
  | sorts_on_typs ((FOLTFree x), s::ss) =
paulson@18798
   397
      LTFree(make_type_class s, make_fixed_type_var x) :: 
paulson@18798
   398
      sorts_on_typs ((FOLTFree x), ss);
paulson@15347
   399
quigley@17150
   400
paulson@18798
   401
fun pred_of_sort (LTVar (s,ty)) = (s,1)
paulson@18798
   402
|   pred_of_sort (LTFree (s,ty)) = (s,1)
quigley@17150
   403
paulson@16199
   404
(*Given a list of sorted type variables, return two separate lists.
paulson@16199
   405
  The first is for TVars, the second for TFrees.*)
paulson@18856
   406
fun add_typs_aux [] = ([],[])
paulson@18856
   407
  | add_typs_aux ((FOLTVar indx,s)::tss) = 
paulson@17230
   408
      let val vs = sorts_on_typs (FOLTVar indx, s)
paulson@18856
   409
	  val (vss,fss) = add_typs_aux tss
quigley@17150
   410
      in
paulson@18856
   411
	  (vs union vss, fss)
quigley@17150
   412
      end
paulson@18856
   413
  | add_typs_aux ((FOLTFree x,s)::tss) =
paulson@17230
   414
      let val fs = sorts_on_typs (FOLTFree x, s)
paulson@18856
   415
	  val (vss,fss) = add_typs_aux tss
quigley@17150
   416
      in
paulson@18856
   417
	  (vss, fs union fss)
quigley@17150
   418
      end;
quigley@17150
   419
paulson@18920
   420
(** Too general means, positive equality literal with a variable X as one operand,
paulson@18920
   421
  when X does not occur properly in the other operand. This rules out clearly
paulson@18920
   422
  inconsistent clauses such as V=a|V=b, though it by no means guarantees soundness. **)
paulson@18920
   423
paulson@18920
   424
fun occurs a (UVar(b,_)) = a=b
paulson@18920
   425
  | occurs a (Fun (_,_,ts)) = exists (occurs a) ts
paulson@18920
   426
paulson@18920
   427
(*Is the first operand a variable that does not properly occur in the second operand?*)
paulson@18920
   428
fun too_general_terms (UVar _, UVar _) = false
paulson@18920
   429
  | too_general_terms (Fun _, _) = false
paulson@18920
   430
  | too_general_terms (UVar (a,_), t) = not (occurs a t);
paulson@18920
   431
paulson@20018
   432
fun too_general_lit (Literal (true, Predicate("equal",_,[x,y]))) =
paulson@18920
   433
      too_general_terms (x,y) orelse too_general_terms(y,x)
paulson@18920
   434
  | too_general_lit _ = false;
quigley@17150
   435
mengj@20015
   436
mengj@20015
   437
mengj@20015
   438
(** make axiom and conjecture clauses. **)
mengj@20015
   439
mengj@20015
   440
exception MAKE_CLAUSE;
mengj@20015
   441
fun make_clause (clause_id, axiom_name, th, kind) =
paulson@20018
   442
  let val term = prop_of th
paulson@20018
   443
      val (lits,types_sorts) = literals_of_term term
paulson@20018
   444
  in if forall isFalse lits 
paulson@20018
   445
     then error "Problem too trivial for resolution (empty clause)"
paulson@20018
   446
     else if kind=Axiom andalso forall too_general_lit lits 
paulson@20018
   447
     then (Output.debug ("Omitting " ^ axiom_name ^ ": equalities are too general"); 
paulson@20018
   448
           raise MAKE_CLAUSE)
paulson@20018
   449
     else Clause {clause_id = clause_id, axiom_name = axiom_name, th = th, 
paulson@20018
   450
                  kind = kind, literals = lits, types_sorts = types_sorts}
paulson@20018
   451
  end;		     
mengj@20015
   452
mengj@20015
   453
fun get_tvar_strs [] = []
mengj@20015
   454
  | get_tvar_strs ((FOLTVar indx,s)::tss) = 
haftmann@20854
   455
      insert (op =) (make_schematic_type_var indx) (get_tvar_strs tss)
mengj@20015
   456
  | get_tvar_strs((FOLTFree x,s)::tss) = get_tvar_strs tss
mengj@20015
   457
mengj@20015
   458
(* check if a clause is first-order before making a conjecture clause*)
paulson@20418
   459
fun make_conjecture_clause n th =
paulson@20418
   460
  if Meson.is_fol_term (prop_of th) then make_clause(n, "conjecture", th, Conjecture)
paulson@20418
   461
  else raise CLAUSE("Goal is not FOL", prop_of th);
paulson@20418
   462
  
mengj@20015
   463
fun make_conjecture_clauses_aux _ [] = []
mengj@20015
   464
  | make_conjecture_clauses_aux n (t::ts) =
mengj@20015
   465
      make_conjecture_clause n t :: make_conjecture_clauses_aux (n+1) ts
mengj@20015
   466
mengj@20015
   467
val make_conjecture_clauses = make_conjecture_clauses_aux 0
mengj@20015
   468
mengj@18199
   469
(*before converting an axiom clause to "clause" format, check if it is FOL*)
mengj@19443
   470
fun make_axiom_clause thm (ax_name,cls_id) =
paulson@20022
   471
  if Meson.is_fol_term (prop_of thm) 
paulson@20022
   472
  then (SOME (make_clause(cls_id, ax_name, thm, Axiom)) handle MAKE_CLAUSE => NONE)
paulson@20022
   473
  else (Output.debug ("Omitting " ^ ax_name ^ ": Axiom is not FOL"); NONE)
mengj@20015
   474
    
mengj@19443
   475
fun make_axiom_clauses [] = []
mengj@19443
   476
  | make_axiom_clauses ((thm,(name,id))::thms) =
paulson@20022
   477
      case make_axiom_clause thm (name,id) of 
paulson@20022
   478
          SOME cls => if isTaut cls then make_axiom_clauses thms 
paulson@20022
   479
                      else (name,cls) :: make_axiom_clauses thms
paulson@20022
   480
        | NONE => make_axiom_clauses thms;
mengj@19354
   481
paulson@15347
   482
(**** Isabelle arities ****)
paulson@15347
   483
paulson@15347
   484
exception ARCLAUSE of string;
paulson@15347
   485
 
paulson@15347
   486
type class = string; 
paulson@15347
   487
type tcons = string; 
paulson@15347
   488
paulson@18868
   489
datatype arLit = TConsLit of bool * (class * tcons * string list)
paulson@18868
   490
               | TVarLit of bool * (class * string);
paulson@15347
   491
 
paulson@15347
   492
datatype arityClause =  
paulson@15347
   493
	 ArityClause of {clause_id: clause_id,
paulson@17845
   494
	  	         axiom_name: axiom_name,
paulson@15347
   495
			 kind: kind,
paulson@15347
   496
			 conclLit: arLit,
paulson@15347
   497
			 premLits: arLit list};
paulson@15347
   498
paulson@15347
   499
paulson@18798
   500
fun gen_TVars 0 = []
paulson@18798
   501
  | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
paulson@15347
   502
paulson@18411
   503
fun pack_sort(_,[])  = []
paulson@18411
   504
  | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
paulson@18411
   505
  | pack_sort(tvar, cls::srt) =  (make_type_class cls, tvar) :: pack_sort(tvar, srt);
paulson@15347
   506
    
paulson@18868
   507
fun make_TVarLit (b, (cls,str)) = TVarLit(b, (cls,str));
paulson@18868
   508
fun make_TConsLit (b, (cls,tcons,tvars)) = 
paulson@18868
   509
      TConsLit(b, (make_type_class cls, make_fixed_type_const tcons, tvars));
paulson@15347
   510
paulson@18411
   511
(*Arity of type constructor tcon :: (arg1,...,argN)res*)
paulson@18411
   512
fun make_axiom_arity_clause (tcons, n, (res,args)) =
paulson@17845
   513
   let val nargs = length args
paulson@18798
   514
       val tvars = gen_TVars nargs
paulson@17845
   515
       val tvars_srts = ListPair.zip (tvars,args)
paulson@17845
   516
       val tvars_srts' = union_all(map pack_sort tvars_srts)
paulson@17845
   517
       val false_tvars_srts' = map (pair false) tvars_srts'
paulson@17845
   518
   in
paulson@17845
   519
      ArityClause {clause_id = n, kind = Axiom, 
paulson@18411
   520
                   axiom_name = lookup_type_const tcons,
paulson@18411
   521
                   conclLit = make_TConsLit(true, (res,tcons,tvars)), 
paulson@17845
   522
                   premLits = map make_TVarLit false_tvars_srts'}
paulson@17845
   523
   end;
paulson@15347
   524
paulson@15347
   525
paulson@15347
   526
(**** Isabelle class relations ****)
paulson@15347
   527
paulson@15347
   528
datatype classrelClause = 
paulson@18868
   529
	 ClassrelClause of {axiom_name: axiom_name,
paulson@15347
   530
			    subclass: class,
paulson@18411
   531
			    superclass: class};
paulson@17845
   532
paulson@17845
   533
fun make_axiom_classrelClause n subclass superclass =
paulson@18868
   534
  ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of subclass ^ 
paulson@18868
   535
                                "_" ^ Int.toString n,
paulson@18868
   536
                  subclass = make_type_class subclass, 
paulson@18868
   537
                  superclass = make_type_class superclass};
paulson@15347
   538
paulson@17845
   539
fun classrelClauses_of_aux n sub [] = []
paulson@18411
   540
  | classrelClauses_of_aux n sub ("HOL.type"::sups) = (*Should be ignored*)
paulson@18411
   541
      classrelClauses_of_aux n sub sups
paulson@17845
   542
  | classrelClauses_of_aux n sub (sup::sups) =
paulson@18868
   543
      make_axiom_classrelClause n sub sup :: classrelClauses_of_aux (n+1) sub sups;
paulson@15347
   544
paulson@18411
   545
fun classrelClauses_of (sub,sups) = classrelClauses_of_aux 0 sub sups;
paulson@17845
   546
wenzelm@19642
   547
val classrel_clauses_thy =
wenzelm@19642
   548
  maps classrelClauses_of o Graph.dest o #classes o Sorts.rep_algebra o Sign.classes_of;
paulson@18868
   549
paulson@18868
   550
paulson@18868
   551
(** Isabelle arities **)
paulson@17845
   552
paulson@17845
   553
fun arity_clause _ (tcons, []) = []
paulson@19155
   554
  | arity_clause n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
paulson@18411
   555
      arity_clause n (tcons,ars)
paulson@17845
   556
  | arity_clause n (tcons, ar::ars) =
paulson@17845
   557
      make_axiom_arity_clause (tcons,n,ar) :: 
paulson@17845
   558
      arity_clause (n+1) (tcons,ars);
paulson@17845
   559
paulson@17845
   560
fun multi_arity_clause [] = []
paulson@19155
   561
  | multi_arity_clause ((tcons,ars) :: tc_arlists) =
paulson@19155
   562
      (*Reversal ensures that older entries always get the same axiom name*)
paulson@19155
   563
      arity_clause 0 (tcons, rev ars)  @  
paulson@19155
   564
      multi_arity_clause tc_arlists 
paulson@17845
   565
paulson@17845
   566
fun arity_clause_thy thy =
wenzelm@19642
   567
  let val arities = thy |> Sign.classes_of
wenzelm@19642
   568
    |> Sorts.rep_algebra |> #arities |> Symtab.dest
wenzelm@19642
   569
    |> map (apsnd (map (fn (c, (_, Ss)) => (c, Ss))));
wenzelm@19521
   570
  in multi_arity_clause (rev arities) end;
paulson@17845
   571
paulson@17845
   572
paulson@18868
   573
(**** Find occurrences of predicates in clauses ****)
paulson@18868
   574
paulson@18868
   575
(*FIXME: multiple-arity checking doesn't work, as update_new is the wrong 
paulson@18868
   576
  function (it flags repeated declarations of a function, even with the same arity)*)
paulson@18868
   577
paulson@18868
   578
fun update_many (tab, keypairs) = foldl (uncurry Symtab.update) tab keypairs;
paulson@18868
   579
paulson@18868
   580
fun add_predicate_preds (Predicate(pname,tys,tms), preds) = 
paulson@18868
   581
  if pname = "equal" then preds (*equality is built-in and requires no declaration*)
paulson@18868
   582
  else Symtab.update (pname, length tys + length tms) preds
paulson@18868
   583
paulson@20018
   584
fun add_literal_preds (Literal(_,pred), preds) = add_predicate_preds (pred,preds)
paulson@18868
   585
paulson@18868
   586
fun add_type_sort_preds ((FOLTVar indx,s), preds) = 
paulson@18868
   587
      update_many (preds, map pred_of_sort (sorts_on_typs (FOLTVar indx, s)))
paulson@18868
   588
  | add_type_sort_preds ((FOLTFree x,s), preds) =
paulson@18868
   589
      update_many (preds, map pred_of_sort (sorts_on_typs (FOLTFree x, s)));
paulson@17845
   590
paulson@18868
   591
fun add_clause_preds (Clause {literals, types_sorts, ...}, preds) =
paulson@18868
   592
  foldl add_literal_preds (foldl add_type_sort_preds preds types_sorts) literals
paulson@18868
   593
  handle Symtab.DUP a => raise ERROR ("predicate " ^ a ^ " has multiple arities")
paulson@18868
   594
paulson@18868
   595
fun add_classrelClause_preds (ClassrelClause {subclass,superclass,...}, preds) =
paulson@18868
   596
  Symtab.update (subclass,1) (Symtab.update (superclass,1) preds);
paulson@17845
   597
paulson@18868
   598
fun add_arityClause_preds (ArityClause {conclLit,...}, preds) =
paulson@18868
   599
  let val TConsLit(_, (tclass, _, _)) = conclLit
paulson@18868
   600
  in  Symtab.update (tclass,1) preds  end;
paulson@18868
   601
paulson@18868
   602
fun preds_of_clauses clauses clsrel_clauses arity_clauses = 
paulson@18868
   603
  Symtab.dest
paulson@18868
   604
    (foldl add_classrelClause_preds 
paulson@18868
   605
      (foldl add_arityClause_preds
paulson@18868
   606
        (foldl add_clause_preds Symtab.empty clauses)
paulson@18868
   607
        arity_clauses)
paulson@18868
   608
      clsrel_clauses)
paulson@18798
   609
paulson@18868
   610
(*** Find occurrences of functions in clauses ***)
paulson@18868
   611
paulson@18868
   612
fun add_foltype_funcs (AtomV _, funcs) = funcs
paulson@18868
   613
  | add_foltype_funcs (AtomF a, funcs) = Symtab.update (a,0) funcs
paulson@18868
   614
  | add_foltype_funcs (Comp(a,tys), funcs) = 
paulson@18868
   615
      foldl add_foltype_funcs (Symtab.update (a, length tys) funcs) tys;
paulson@18868
   616
paulson@20038
   617
fun add_folterm_funcs (UVar(_,ty), funcs) = add_foltype_funcs (ty, funcs)
paulson@18868
   618
  | add_folterm_funcs (Fun(a,tys,tms), funcs) = 
paulson@18868
   619
      foldl add_foltype_funcs 
paulson@18868
   620
	    (foldl add_folterm_funcs (Symtab.update (a, length tys + length tms) funcs) 
paulson@18868
   621
	           tms) 
paulson@18868
   622
	    tys
paulson@18798
   623
paulson@18868
   624
fun add_predicate_funcs (Predicate(_,tys,tms), funcs) = 
paulson@18868
   625
    foldl add_foltype_funcs (foldl add_folterm_funcs funcs tms) tys;
paulson@18868
   626
paulson@20018
   627
fun add_literal_funcs (Literal(_,pred), funcs) = add_predicate_funcs (pred,funcs)
paulson@18868
   628
paulson@20038
   629
(*TFrees are recorded as constants*)
paulson@20038
   630
fun add_type_sort_funcs ((FOLTVar _, _), funcs) = funcs
paulson@20038
   631
  | add_type_sort_funcs ((FOLTFree a, _), funcs) = 
paulson@20038
   632
      Symtab.update (make_fixed_type_var a, 0) funcs
paulson@20038
   633
paulson@18868
   634
fun add_arityClause_funcs (ArityClause {conclLit,...}, funcs) =
paulson@18868
   635
  let val TConsLit(_, (_, tcons, tvars)) = conclLit
paulson@18868
   636
  in  Symtab.update (tcons, length tvars) funcs  end;
paulson@17845
   637
paulson@20038
   638
fun add_clause_funcs (Clause {literals, types_sorts, ...}, funcs) =
paulson@20038
   639
  foldl add_literal_funcs (foldl add_type_sort_funcs funcs types_sorts)
paulson@20038
   640
       literals
paulson@18868
   641
  handle Symtab.DUP a => raise ERROR ("function " ^ a ^ " has multiple arities")
paulson@18868
   642
paulson@18868
   643
fun funcs_of_clauses clauses arity_clauses = 
paulson@18868
   644
  Symtab.dest (foldl add_arityClause_funcs 
paulson@18868
   645
                     (foldl add_clause_funcs Symtab.empty clauses)
paulson@18868
   646
                     arity_clauses)
paulson@18868
   647
paulson@18868
   648
paulson@18868
   649
(**** String-oriented operations ****)
paulson@15347
   650
paulson@15347
   651
fun wrap_eq_type typ t = eq_typ_wrapper ^"(" ^ t ^ "," ^ typ ^ ")";
paulson@15347
   652
paulson@18218
   653
(*Only need to wrap equality's arguments with "typeinfo" if the output clauses are typed 
paulson@18218
   654
 and if we specifically ask for types to be included.   *)
paulson@15347
   655
fun string_of_equality (typ,terms) =
paulson@17230
   656
      let val [tstr1,tstr2] = map string_of_term terms
mengj@18402
   657
	  val typ' = string_of_fol_type typ
paulson@17230
   658
      in
paulson@17230
   659
	  if !keep_types andalso !special_equal 
mengj@18402
   660
	  then "equal(" ^ (wrap_eq_type typ' tstr1) ^ "," ^ 
mengj@18402
   661
		 	  (wrap_eq_type typ' tstr2) ^ ")"
paulson@17230
   662
	  else "equal(" ^ tstr1 ^ "," ^ tstr2 ^ ")"
paulson@17230
   663
      end
paulson@17230
   664
and string_of_term (UVar(x,_)) = x
paulson@18218
   665
  | string_of_term (Fun("equal",[typ],terms)) = string_of_equality(typ,terms)
mengj@20015
   666
  | string_of_term (Fun (name,typs,[])) = name ^ (paren_pack (map string_of_fol_type typs))
paulson@18218
   667
  | string_of_term (Fun (name,typs,terms)) = 
paulson@18218
   668
      let val terms_as_strings = map string_of_term terms
mengj@18402
   669
	  val typs' = if !keep_types then map string_of_fol_type typs else []
paulson@18420
   670
      in  name ^ (paren_pack (terms_as_strings @ typs'))  end;
paulson@15347
   671
paulson@15347
   672
(* before output the string of the predicate, check if the predicate corresponds to an equality or not. *)
paulson@18218
   673
fun string_of_predicate (Predicate("equal",[typ],terms)) = string_of_equality(typ,terms)
paulson@18218
   674
  | string_of_predicate (Predicate(name,typs,terms)) = 
paulson@17230
   675
      let val terms_as_strings = map string_of_term terms
mengj@18402
   676
	  val typs' = if !keep_types then map string_of_fol_type typs else []
paulson@18420
   677
      in  name ^ (paren_pack (terms_as_strings @ typs'))  end;
paulson@17317
   678
paulson@17317
   679
fun string_of_clausename (cls_id,ax_name) = 
paulson@17525
   680
    clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id;
paulson@17317
   681
paulson@17317
   682
fun string_of_type_clsname (cls_id,ax_name,idx) = 
paulson@17525
   683
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
paulson@18863
   684
paulson@18863
   685
(*Write a list of strings to a file*)
paulson@18863
   686
fun writeln_strs os = List.app (fn s => TextIO.output (os,s));
paulson@18863
   687
quigley@17150
   688
    
paulson@18868
   689
(**** Producing DFG files ****)
quigley@17150
   690
paulson@18863
   691
(*Attach sign in DFG syntax: false means negate.*)
paulson@18863
   692
fun dfg_sign true s = s
paulson@18863
   693
  | dfg_sign false s = "not(" ^ s ^ ")"  
paulson@18863
   694
paulson@20018
   695
fun dfg_literal (Literal(pol,pred)) = dfg_sign pol (string_of_predicate pred)
quigley@17150
   696
paulson@18798
   697
fun dfg_of_typeLit (LTVar (s,ty)) = "not(" ^ s ^ "(" ^ ty ^ "))"
paulson@18856
   698
  | dfg_of_typeLit (LTFree (s,ty)) = s ^ "(" ^ ty ^ ")";
quigley@17150
   699
 
paulson@18868
   700
(*Enclose the clause body by quantifiers, if necessary*)
paulson@18868
   701
fun dfg_forall [] body = body  
paulson@18868
   702
  | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
quigley@17150
   703
paulson@18868
   704
fun gen_dfg_cls (cls_id, ax_name, knd, lits, vars) = 
paulson@18868
   705
    "clause( %(" ^ knd ^ ")\n" ^ 
paulson@18868
   706
    dfg_forall vars ("or(" ^ lits ^ ")") ^ ",\n" ^ 
paulson@18863
   707
    string_of_clausename (cls_id,ax_name) ^  ").\n\n";
quigley@17150
   708
paulson@18869
   709
fun dfg_clause_aux (Clause{literals, types_sorts, ...}) = 
paulson@18868
   710
  let val lits = map dfg_literal literals
paulson@18869
   711
      val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@17230
   712
      val tvar_lits_strs = 
paulson@18869
   713
	  if !keep_types then map dfg_of_typeLit tvar_lits else []
paulson@17230
   714
      val tfree_lits =
paulson@18869
   715
          if !keep_types then map dfg_of_typeLit tfree_lits else []
paulson@17230
   716
  in
paulson@17234
   717
      (tvar_lits_strs @ lits, tfree_lits)
paulson@17230
   718
  end; 
quigley@17150
   719
paulson@20018
   720
fun dfg_folterms (Literal(pol,pred)) = 
paulson@18856
   721
  let val Predicate (_, _, folterms) = pred
paulson@18218
   722
  in  folterms  end
quigley@17150
   723
paulson@17404
   724
fun get_uvars (UVar(a,typ)) = [a] 
paulson@18868
   725
  | get_uvars (Fun (_,typ,tlist)) = union_all(map get_uvars tlist)
paulson@17404
   726
paulson@18868
   727
fun dfg_vars (Clause {literals,...}) =
paulson@18920
   728
  union_all (map get_uvars (List.concat (map dfg_folterms literals)))
quigley@17150
   729
paulson@18798
   730
fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,types_sorts,...}) =
paulson@17230
   731
    let val (lits,tfree_lits) = dfg_clause_aux cls 
paulson@18798
   732
            (*"lits" includes the typing assumptions (TVars)*)
quigley@17150
   733
        val vars = dfg_vars cls
paulson@18798
   734
        val tvars = get_tvar_strs types_sorts
paulson@20038
   735
	val cls_str = gen_dfg_cls(clause_id, axiom_name, name_of_kind kind, 
paulson@20038
   736
	                           commas lits, tvars@vars) 
paulson@18798
   737
    in (cls_str, tfree_lits) end;
quigley@17150
   738
paulson@18798
   739
fun string_of_arity (name, num) =  "(" ^ name ^ "," ^ Int.toString num ^ ")"
quigley@17150
   740
paulson@18856
   741
fun string_of_preds [] = ""
paulson@18856
   742
  | string_of_preds preds = "predicates[" ^ commas(map string_of_arity preds) ^ "].\n";
quigley@17150
   743
paulson@18856
   744
fun string_of_funcs [] = ""
paulson@18856
   745
  | string_of_funcs funcs = "functions[" ^ commas(map string_of_arity funcs) ^ "].\n" ;
quigley@17150
   746
paulson@17234
   747
fun string_of_symbols predstr funcstr = 
paulson@17234
   748
  "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
quigley@17150
   749
paulson@18798
   750
fun string_of_start name = "begin_problem(" ^ name ^ ").\n\n";
quigley@17150
   751
paulson@18863
   752
fun string_of_descrip name = 
paulson@18868
   753
  "list_of_descriptions.\nname({*" ^ name ^ 
paulson@18868
   754
  "*}).\nauthor({*Isabelle*}).\nstatus(unknown).\ndescription({*auto-generated*}).\nend_of_list.\n\n"
quigley@17150
   755
paulson@18863
   756
fun dfg_tfree_clause tfree_lit =
paulson@18863
   757
  "clause( %(conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ").\n\n"
paulson@18863
   758
paulson@17845
   759
fun string_of_arClauseID (ArityClause {clause_id,axiom_name,...}) =
paulson@17845
   760
    arclause_prefix ^ ascii_of axiom_name ^ "_" ^ Int.toString clause_id;
quigley@17150
   761
paulson@18863
   762
fun dfg_of_arLit (TConsLit(pol,(c,t,args))) =
paulson@18863
   763
      dfg_sign pol (c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@18863
   764
  | dfg_of_arLit (TVarLit(pol,(c,str))) =
paulson@18863
   765
      dfg_sign pol (c ^ "(" ^ str ^ ")")
paulson@17525
   766
    
paulson@20038
   767
fun dfg_classrelLits sub sup =  "not(" ^ sub ^ "(T)), " ^ sup ^ "(T)";
paulson@17525
   768
paulson@18868
   769
fun dfg_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
paulson@18868
   770
  "clause(forall([T],\nor( " ^ dfg_classrelLits subclass superclass ^ ")),\n" ^
paulson@18868
   771
  axiom_name ^ ").\n\n";
paulson@18868
   772
      
paulson@18868
   773
fun dfg_arity_clause (arcls as ArityClause{kind,conclLit,premLits,...}) = 
paulson@18868
   774
  let val arcls_id = string_of_arClauseID arcls
paulson@18868
   775
      val knd = name_of_kind kind
paulson@18868
   776
      val TConsLit(_, (_,_,tvars)) = conclLit
paulson@18868
   777
      val lits = map dfg_of_arLit (conclLit :: premLits)
paulson@18863
   778
  in
paulson@18868
   779
      "clause( %(" ^ knd ^ ")\n" ^ 
paulson@18868
   780
      dfg_forall tvars ("or( " ^ commas lits ^ ")") ^ ",\n" ^
paulson@18868
   781
      arcls_id ^ ").\n\n"
paulson@18863
   782
  end;
paulson@18863
   783
paulson@18863
   784
(* write out a subgoal in DFG format to the file "xxxx_N"*)
paulson@20038
   785
fun dfg_write_file thms filename (ax_tuples,classrel_clauses,arity_clauses) = 
paulson@18863
   786
  let 
paulson@19207
   787
    val _ = Output.debug ("Preparing to write the DFG file " ^ filename)
mengj@19719
   788
    val conjectures = make_conjecture_clauses thms
paulson@20038
   789
    val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses ax_tuples)
paulson@18868
   790
    val (dfg_clss, tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
paulson@20038
   791
    val clss = conjectures @ axclauses
paulson@18868
   792
    val funcs = funcs_of_clauses clss arity_clauses
paulson@18868
   793
    and preds = preds_of_clauses clss classrel_clauses arity_clauses
paulson@18863
   794
    and probname = Path.pack (Path.base (Path.unpack filename))
paulson@20038
   795
    val (axstrs, _) = ListPair.unzip (map clause2dfg axclauses)
paulson@18863
   796
    val tfree_clss = map dfg_tfree_clause (union_all tfree_litss) 
paulson@19155
   797
    val out = TextIO.openOut filename
paulson@18863
   798
  in
paulson@18868
   799
    TextIO.output (out, string_of_start probname); 
paulson@18868
   800
    TextIO.output (out, string_of_descrip probname); 
paulson@18868
   801
    TextIO.output (out, string_of_symbols (string_of_funcs funcs) (string_of_preds preds)); 
paulson@18868
   802
    TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
paulson@18868
   803
    writeln_strs out axstrs;
paulson@18868
   804
    List.app (curry TextIO.output out o dfg_classrelClause) classrel_clauses;
paulson@18868
   805
    List.app (curry TextIO.output out o dfg_arity_clause) arity_clauses;
paulson@18868
   806
    TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
paulson@18868
   807
    writeln_strs out tfree_clss;
paulson@18868
   808
    writeln_strs out dfg_clss;
paulson@18868
   809
    TextIO.output (out, "end_of_list.\n\nend_problem.\n");
paulson@20022
   810
    TextIO.closeOut out;
paulson@20022
   811
    clnames
paulson@17525
   812
  end;
quigley@17150
   813
quigley@17150
   814
paulson@18869
   815
(**** Produce TPTP files ****)
paulson@18868
   816
paulson@18868
   817
(*Attach sign in TPTP syntax: false means negate.*)
paulson@18868
   818
fun tptp_sign true s = "++" ^ s
paulson@18868
   819
  | tptp_sign false s = "--" ^ s
paulson@18868
   820
paulson@20018
   821
fun tptp_literal (Literal(pol,pred)) = 
paulson@20018
   822
      (if pol then "++" else "--") ^ string_of_predicate pred;
paulson@15347
   823
paulson@18798
   824
fun tptp_of_typeLit (LTVar (s,ty)) = "--" ^ s ^ "(" ^ ty ^ ")"
paulson@18798
   825
  | tptp_of_typeLit (LTFree (s,ty)) = "++" ^ s ^ "(" ^ ty ^ ")";
paulson@15347
   826
 
paulson@15347
   827
fun gen_tptp_cls (cls_id,ax_name,knd,lits) = 
paulson@17317
   828
    "input_clause(" ^ string_of_clausename (cls_id,ax_name) ^ "," ^ 
paulson@18863
   829
    knd ^ "," ^ lits ^ ").\n";
paulson@15347
   830
paulson@17317
   831
fun gen_tptp_type_cls (cls_id,ax_name,knd,tfree_lit,idx) = 
paulson@17317
   832
    "input_clause(" ^ string_of_type_clsname (cls_id,ax_name,idx) ^ "," ^ 
paulson@18863
   833
    knd ^ ",[" ^ tfree_lit ^ "]).\n";
paulson@15347
   834
paulson@18869
   835
fun tptp_type_lits (Clause {literals, types_sorts, ...}) = 
paulson@18869
   836
    let val lits = map tptp_literal literals
paulson@18869
   837
	val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@18869
   838
        val tvar_lits_strs =
paulson@18869
   839
            if !keep_types then map tptp_of_typeLit tvar_lits else []
paulson@18869
   840
	val tfree_lits =
paulson@18869
   841
	    if !keep_types then map tptp_of_typeLit tfree_lits else []
paulson@15347
   842
    in
paulson@17305
   843
	(tvar_lits_strs @ lits, tfree_lits)
paulson@15347
   844
    end; 
paulson@15347
   845
paulson@18869
   846
fun clause2tptp (cls as Clause {clause_id, axiom_name, kind, ...}) =
paulson@17422
   847
    let val (lits,tfree_lits) = tptp_type_lits cls 
paulson@17305
   848
            (*"lits" includes the typing assumptions (TVars)*)
paulson@18869
   849
	val knd = name_of_kind kind
paulson@18869
   850
	val cls_str = gen_tptp_cls(clause_id, axiom_name, knd, bracket_pack lits) 
paulson@15608
   851
    in
paulson@15608
   852
	(cls_str,tfree_lits) 
paulson@15608
   853
    end;
paulson@15608
   854
paulson@18863
   855
fun tptp_tfree_clause tfree_lit =
paulson@18863
   856
    "input_clause(" ^ "tfree_tcs," ^ "conjecture" ^ ",[" ^ tfree_lit ^ "]).\n";
paulson@15608
   857
paulson@17525
   858
fun tptp_of_arLit (TConsLit(b,(c,t,args))) =
paulson@18868
   859
      tptp_sign b (c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@17525
   860
  | tptp_of_arLit (TVarLit(b,(c,str))) =
paulson@18868
   861
      tptp_sign b (c ^ "(" ^ str ^ ")")
paulson@15347
   862
    
paulson@18868
   863
fun tptp_arity_clause (arcls as ArityClause{kind,conclLit,premLits,...}) = 
paulson@18868
   864
  let val arcls_id = string_of_arClauseID arcls
paulson@18868
   865
      val knd = name_of_kind kind
paulson@18868
   866
      val lits = map tptp_of_arLit (conclLit :: premLits)
paulson@18868
   867
  in
paulson@18868
   868
    "input_clause(" ^ arcls_id ^ "," ^ knd ^ "," ^ bracket_pack lits ^ ").\n"
paulson@18868
   869
  end;
paulson@15347
   870
paulson@15347
   871
fun tptp_classrelLits sub sup = 
paulson@15347
   872
    let val tvar = "(T)"
paulson@15347
   873
    in 
paulson@18411
   874
	"[--" ^ sub ^ tvar ^ ",++" ^ sup ^ tvar ^ "]"
paulson@15347
   875
    end;
paulson@15347
   876
paulson@18868
   877
fun tptp_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
paulson@18868
   878
  "input_clause(" ^ axiom_name ^ ",axiom," ^ tptp_classrelLits subclass superclass ^ ").\n" 
quigley@17150
   879
paulson@18863
   880
(* write out a subgoal as tptp clauses to the file "xxxx_N"*)
paulson@20038
   881
fun tptp_write_file thms filename (ax_tuples,classrel_clauses,arity_clauses) =
paulson@18863
   882
  let
paulson@19207
   883
    val _ = Output.debug ("Preparing to write the TPTP file " ^ filename)
mengj@19443
   884
    val clss = make_conjecture_clauses thms
paulson@20038
   885
    val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses ax_tuples)
paulson@18863
   886
    val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp clss)
paulson@18863
   887
    val tfree_clss = map tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
paulson@18863
   888
    val out = TextIO.openOut filename
paulson@18863
   889
  in
paulson@20038
   890
    List.app (curry TextIO.output out o #1 o clause2tptp) axclauses;
paulson@18863
   891
    writeln_strs out tfree_clss;
paulson@18863
   892
    writeln_strs out tptp_clss;
paulson@18868
   893
    List.app (curry TextIO.output out o tptp_classrelClause) classrel_clauses;
paulson@18868
   894
    List.app (curry TextIO.output out o tptp_arity_clause) arity_clauses;
paulson@20022
   895
    TextIO.closeOut out;
paulson@20022
   896
    clnames
paulson@18863
   897
  end;
paulson@18863
   898
paulson@15347
   899
end;