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