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