src/HOL/Tools/res_hol_clause.ML
author paulson
Mon Oct 30 16:42:46 2006 +0100 (2006-10-30)
changeset 21108 04d8e6eb9a2e
parent 20997 4b500d78cb4f
child 21135 07549f79d19c
permissions -rw-r--r--
Purely cosmetic
mengj@17998
     1
(* ID: $Id$ 
mengj@17998
     2
   Author: Jia Meng, NICTA
mengj@17998
     3
mengj@17998
     4
FOL clauses translated from HOL formulae.  Combinators are used to represent lambda terms.
mengj@17998
     5
mengj@17998
     6
*)
mengj@17998
     7
mengj@17998
     8
structure ResHolClause =
mengj@17998
     9
mengj@17998
    10
struct
mengj@17998
    11
mengj@20791
    12
(* theorems for combinators and function extensionality *)
mengj@20791
    13
val ext = thm "HOL.ext";
mengj@20791
    14
val comb_I = thm "Reconstruction.COMBI_def";
mengj@20791
    15
val comb_K = thm "Reconstruction.COMBK_def";
mengj@20791
    16
val comb_B = thm "Reconstruction.COMBB_def";
mengj@20791
    17
val comb_C = thm "Reconstruction.COMBC_def";
mengj@20791
    18
val comb_S = thm "Reconstruction.COMBS_def";
mengj@20791
    19
val comb_B' = thm "Reconstruction.COMBB'_def";
mengj@20791
    20
val comb_C' = thm "Reconstruction.COMBC'_def";
mengj@20791
    21
val comb_S' = thm "Reconstruction.COMBS'_def";
mengj@20791
    22
val fequal_imp_equal = thm "Reconstruction.fequal_imp_equal";
mengj@20791
    23
val equal_imp_fequal = thm "Reconstruction.equal_imp_fequal";
mengj@20791
    24
mengj@20791
    25
paulson@21108
    26
(*A flag to set if we use the Turner optimizations. Currently FALSE, as the 5 standard
paulson@20997
    27
  combinators appear to work best.*)
paulson@21108
    28
val use_Turner = ref false;
mengj@17998
    29
paulson@21108
    30
(*FIXME: these refs should probaby replaced by code to count the combinators in the 
paulson@21108
    31
  translated form of the term.*)
mengj@20644
    32
val combI_count = ref 0;
mengj@20644
    33
val combK_count = ref 0;
mengj@20644
    34
val combB_count = ref 0;
mengj@20644
    35
val combC_count = ref 0;
mengj@20644
    36
val combS_count = ref 0;
mengj@20644
    37
mengj@20644
    38
val combB'_count = ref 0;
mengj@20644
    39
val combC'_count = ref 0;
mengj@20644
    40
val combS'_count = ref 0; 
mengj@20644
    41
mengj@17998
    42
mengj@20644
    43
fun increI count_comb =  if count_comb then combI_count := !combI_count + 1 else ();
mengj@20644
    44
fun increK count_comb =  if count_comb then combK_count := !combK_count + 1 else ();
mengj@20644
    45
fun increB count_comb =  if count_comb then combB_count := !combB_count + 1 else ();
mengj@20644
    46
fun increC count_comb =  if count_comb then combC_count := !combC_count + 1 else ();
mengj@20644
    47
fun increS count_comb =  if count_comb then combS_count := !combS_count + 1 else (); 
mengj@20644
    48
fun increB' count_comb =  if count_comb then combB'_count := !combB'_count + 1 else (); 
mengj@20644
    49
fun increC' count_comb =  if count_comb then combC'_count := !combC'_count + 1 else ();
mengj@20644
    50
fun increS' count_comb =  if count_comb then combS'_count := !combS'_count + 1 else (); 
mengj@20644
    51
mengj@20644
    52
mengj@20644
    53
exception DECRE_COMB of string;
paulson@20953
    54
fun decreB count_comb n = 
paulson@20953
    55
  if count_comb then 
paulson@20953
    56
    if !combB_count >= n then combB_count := !combB_count - n else raise DECRE_COMB "COMBB"
paulson@20953
    57
  else ();
mengj@20644
    58
paulson@20953
    59
fun decreC count_comb n =
paulson@20953
    60
  if count_comb then 
paulson@20953
    61
    if !combC_count >= n then combC_count := !combC_count - n else raise DECRE_COMB "COMBC"
paulson@20953
    62
  else ();
mengj@20644
    63
paulson@20953
    64
fun decreS count_comb n = 
paulson@20953
    65
  if count_comb then 
paulson@20953
    66
    if !combS_count >= n then combS_count := !combS_count - n else raise DECRE_COMB "COMBS"
paulson@20953
    67
  else ();
mengj@20130
    68
mengj@18356
    69
val const_typargs = ref (Library.K [] : (string*typ -> typ list));
mengj@18356
    70
mengj@20644
    71
fun init thy = (combI_count:=0; combK_count:=0;combB_count:=0;combC_count:=0;combS_count:=0;combB'_count:=0;combC'_count:=0;combS'_count:=0;
paulson@20022
    72
                const_typargs := Sign.const_typargs thy);
mengj@19198
    73
mengj@17998
    74
(**********************************************************************)
mengj@17998
    75
(* convert a Term.term with lambdas into a Term.term with combinators *) 
mengj@17998
    76
(**********************************************************************)
mengj@17998
    77
mengj@17998
    78
fun is_free (Bound(a)) n = (a = n)
mengj@17998
    79
  | is_free (Abs(x,_,b)) n = (is_free b (n+1))
mengj@17998
    80
  | is_free (P $ Q) n = ((is_free P n) orelse (is_free Q n))
mengj@17998
    81
  | is_free _ _ = false;
mengj@17998
    82
mengj@17998
    83
mengj@17998
    84
exception BND of term;
mengj@17998
    85
mengj@17998
    86
fun decre_bndVar (Bound n) = Bound (n-1)
mengj@17998
    87
  | decre_bndVar (P $ Q) = (decre_bndVar P) $ (decre_bndVar Q)
mengj@17998
    88
  | decre_bndVar t =
mengj@17998
    89
    case t of Const(_,_) => t
mengj@17998
    90
	    | Free(_,_) => t
mengj@17998
    91
	    | Var(_,_) => t
mengj@17998
    92
	    | Abs(_,_,_) => raise BND(t); (*should not occur*)
mengj@17998
    93
mengj@17998
    94
mengj@17998
    95
(*******************************************)
paulson@20953
    96
fun mk_compact_comb (tm as (Const("Reconstruction.COMBB",_)$p) $ (Const("Reconstruction.COMBB",_)$q$r)) Bnds count_comb =
mengj@20644
    97
    let val tp_p = Term.type_of1(Bnds,p)
mengj@20644
    98
	val tp_q = Term.type_of1(Bnds,q)
mengj@20644
    99
	val tp_r = Term.type_of1(Bnds,r)
mengj@20644
   100
	val typ = Term.type_of1(Bnds,tm)
mengj@20644
   101
	val typ_B' = [tp_p,tp_q,tp_r] ---> typ
mengj@20644
   102
	val _ = increB' count_comb
mengj@20644
   103
	val _ = decreB count_comb 2
mengj@20644
   104
    in
paulson@20953
   105
	Const("Reconstruction.COMBB'",typ_B') $ p $ q $ r
mengj@20644
   106
    end
paulson@20953
   107
  | mk_compact_comb (tm as (Const("Reconstruction.COMBC",_) $ (Const("Reconstruction.COMBB",_)$p$q) $ r)) Bnds count_comb =
mengj@20644
   108
    let val tp_p = Term.type_of1(Bnds,p)
mengj@20644
   109
	val tp_q = Term.type_of1(Bnds,q)
mengj@20644
   110
	val tp_r = Term.type_of1(Bnds,r)
mengj@20644
   111
	val typ = Term.type_of1(Bnds,tm)
mengj@20644
   112
	val typ_C' = [tp_p,tp_q,tp_r] ---> typ
mengj@20644
   113
	val _ = increC' count_comb
mengj@20644
   114
	val _ = decreC count_comb 1
mengj@20644
   115
	val _ = decreB count_comb 1
mengj@20644
   116
    in
paulson@20953
   117
	Const("Reconstruction.COMBC'",typ_C') $ p $ q $ r
mengj@20644
   118
    end
paulson@20953
   119
  | mk_compact_comb (tm as (Const("Reconstruction.COMBS",_) $ (Const("Reconstruction.COMBB",_)$p$q) $ r)) Bnds count_comb =
mengj@20644
   120
    let val tp_p = Term.type_of1(Bnds,p)
mengj@20644
   121
	val tp_q = Term.type_of1(Bnds,q)
mengj@20644
   122
	val tp_r = Term.type_of1(Bnds,r)
mengj@20644
   123
	val typ = Term.type_of1(Bnds,tm)
mengj@20644
   124
	val typ_S' = [tp_p,tp_q,tp_r] ---> typ
mengj@20644
   125
	val _ = increS' count_comb
mengj@20644
   126
	val _ = decreS count_comb 1
mengj@20644
   127
	val _ = decreB count_comb 1
mengj@20644
   128
    in
paulson@20953
   129
	Const("Reconstruction.COMBS'",typ_S') $ p $ q $ r
mengj@20644
   130
    end
mengj@20644
   131
  | mk_compact_comb tm _ _ = tm;
mengj@20644
   132
paulson@21108
   133
fun compact_comb t Bnds count_comb = 
paulson@21108
   134
  if !use_Turner then mk_compact_comb t Bnds count_comb else t;
mengj@20644
   135
mengj@20130
   136
fun lam2comb (Abs(x,tp,Bound 0)) _ count_comb = 
paulson@21108
   137
      let val tpI = Type("fun",[tp,tp])
paulson@21108
   138
	  val _ = increI count_comb
paulson@21108
   139
      in 
paulson@21108
   140
	  Const("Reconstruction.COMBI",tpI) 
paulson@21108
   141
      end
mengj@20130
   142
  | lam2comb (Abs(x,tp,Bound n)) Bnds count_comb = 
paulson@21108
   143
      let val (Bound n') = decre_bndVar (Bound n)
paulson@21108
   144
	  val tb = List.nth(Bnds,n')
paulson@21108
   145
	  val tK = Type("fun",[tb,Type("fun",[tp,tb])])
paulson@21108
   146
	  val _ = increK count_comb 
paulson@21108
   147
      in
paulson@21108
   148
	  Const("Reconstruction.COMBK",tK) $ (Bound n')
paulson@21108
   149
      end
mengj@20130
   150
  | lam2comb (Abs(x,t1,Const(c,t2))) _ count_comb = 
paulson@21108
   151
      let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
paulson@21108
   152
	  val _ = increK count_comb 
paulson@21108
   153
      in 
paulson@21108
   154
	  Const("Reconstruction.COMBK",tK) $ Const(c,t2) 
paulson@21108
   155
      end
mengj@20130
   156
  | lam2comb (Abs(x,t1,Free(v,t2))) _ count_comb =
paulson@21108
   157
      let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
paulson@21108
   158
	  val _ = increK count_comb
paulson@21108
   159
      in
paulson@21108
   160
	  Const("Reconstruction.COMBK",tK) $ Free(v,t2)
paulson@21108
   161
      end
mengj@20130
   162
  | lam2comb (Abs(x,t1,Var(ind,t2))) _ count_comb =
paulson@21108
   163
      let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
paulson@21108
   164
	  val _ = increK count_comb 
paulson@21108
   165
      in
paulson@21108
   166
	  Const("Reconstruction.COMBK",tK) $ Var(ind,t2)
paulson@21108
   167
      end
mengj@20130
   168
  | lam2comb (t as (Abs(x,t1,P$(Bound 0)))) Bnds count_comb =
paulson@21108
   169
      let val nfreeP = not(is_free P 0)
paulson@21108
   170
	  val tr = Term.type_of1(t1::Bnds,P)
paulson@21108
   171
      in
paulson@21108
   172
	  if nfreeP then (decre_bndVar P)
paulson@21108
   173
	  else 
paulson@21108
   174
	  let val tI = Type("fun",[t1,t1])
paulson@21108
   175
	      val P' = lam2comb (Abs(x,t1,P)) Bnds count_comb
paulson@21108
   176
	      val tp' = Term.type_of1(Bnds,P')
paulson@21108
   177
	      val tS = Type("fun",[tp',Type("fun",[tI,tr])])
paulson@21108
   178
	      val _ = increI count_comb
paulson@21108
   179
	      val _ = increS count_comb
paulson@21108
   180
	  in
paulson@21108
   181
	      compact_comb (Const("Reconstruction.COMBS",tS) $ P' $ Const("Reconstruction.COMBI",tI)) Bnds count_comb
paulson@21108
   182
	  end
paulson@21108
   183
      end
mengj@17998
   184
	    
mengj@20130
   185
  | lam2comb (t as (Abs(x,t1,P$Q))) Bnds count_comb =
paulson@21108
   186
      let val nfreeP = not(is_free P 0)
paulson@21108
   187
	  and nfreeQ = not(is_free Q 0)
paulson@21108
   188
	  val tpq = Term.type_of1(t1::Bnds, P$Q) 
paulson@21108
   189
      in
paulson@21108
   190
	  if nfreeP andalso nfreeQ 
paulson@21108
   191
	  then 
mengj@17998
   192
	    let val tK = Type("fun",[tpq,Type("fun",[t1,tpq])])
mengj@17998
   193
		val PQ' = decre_bndVar(P $ Q)
mengj@20644
   194
		val _ = increK count_comb
mengj@17998
   195
	    in 
paulson@20953
   196
		Const("Reconstruction.COMBK",tK) $ PQ'
paulson@21108
   197
	    end
paulson@21108
   198
	  else if nfreeP then 
paulson@21108
   199
	    let val Q' = lam2comb (Abs(x,t1,Q)) Bnds count_comb
paulson@21108
   200
		val P' = decre_bndVar P
paulson@21108
   201
		val tp = Term.type_of1(Bnds,P')
paulson@21108
   202
		val tq' = Term.type_of1(Bnds, Q')
paulson@21108
   203
		val tB = Type("fun",[tp,Type("fun",[tq',Type("fun",[t1,tpq])])])
paulson@21108
   204
		val _ = increB count_comb
paulson@21108
   205
	    in
paulson@21108
   206
		compact_comb (Const("Reconstruction.COMBB",tB) $ P' $ Q') Bnds count_comb 
paulson@21108
   207
	    end
paulson@21108
   208
	  else if nfreeQ then 
paulson@21108
   209
	    let val P' = lam2comb (Abs(x,t1,P)) Bnds count_comb
paulson@21108
   210
		val Q' = decre_bndVar Q
paulson@21108
   211
		val tq = Term.type_of1(Bnds,Q')
paulson@21108
   212
		val tp' = Term.type_of1(Bnds, P')
paulson@21108
   213
		val tC = Type("fun",[tp',Type("fun",[tq,Type("fun",[t1,tpq])])])
paulson@21108
   214
		val _ = increC count_comb
paulson@21108
   215
	    in
paulson@21108
   216
		compact_comb (Const("Reconstruction.COMBC",tC) $ P' $ Q') Bnds count_comb
paulson@21108
   217
	    end
paulson@21108
   218
          else
paulson@21108
   219
	    let val P' = lam2comb (Abs(x,t1,P)) Bnds count_comb
paulson@21108
   220
		val Q' = lam2comb (Abs(x,t1,Q)) Bnds count_comb 
paulson@21108
   221
		val tp' = Term.type_of1(Bnds,P')
paulson@21108
   222
		val tq' = Term.type_of1(Bnds,Q')
paulson@21108
   223
		val tS = Type("fun",[tp',Type("fun",[tq',Type("fun",[t1,tpq])])])
paulson@21108
   224
		val _ = increS count_comb
paulson@21108
   225
	    in
paulson@21108
   226
		compact_comb (Const("Reconstruction.COMBS",tS) $ P' $ Q') Bnds count_comb
paulson@21108
   227
	    end
paulson@21108
   228
      end
mengj@20791
   229
  | lam2comb (t as (Abs(x,t1,_))) _ _ = raise ResClause.CLAUSE("HOL CLAUSE",t);
mengj@17998
   230
mengj@17998
   231
(*********************)
mengj@17998
   232
mengj@20130
   233
fun to_comb (Abs(x,tp,b)) Bnds count_comb =
mengj@20130
   234
    let val b' = to_comb b (tp::Bnds) count_comb
mengj@20130
   235
    in lam2comb (Abs(x,tp,b')) Bnds count_comb end
mengj@20130
   236
  | to_comb (P $ Q) Bnds count_comb = ((to_comb P Bnds count_comb) $ (to_comb Q Bnds count_comb))
mengj@20130
   237
  | to_comb t _ _ = t;
mengj@17998
   238
 
mengj@20644
   239
   
mengj@20130
   240
fun comb_of t count_comb = to_comb t [] count_comb;
mengj@20644
   241
 
mengj@17998
   242
(* print a term containing combinators, used for debugging *)
mengj@17998
   243
exception TERM_COMB of term;
mengj@17998
   244
mengj@17998
   245
fun string_of_term (Const(c,t)) = c
mengj@17998
   246
  | string_of_term (Free(v,t)) = v
paulson@20360
   247
  | string_of_term (Var((x,n),t)) = x ^ "_" ^ (string_of_int n)
mengj@17998
   248
  | string_of_term (P $ Q) =
paulson@20360
   249
      "(" ^ string_of_term P ^ " " ^ string_of_term Q ^ ")" 
mengj@17998
   250
  | string_of_term t =  raise TERM_COMB (t);
mengj@17998
   251
mengj@17998
   252
mengj@17998
   253
mengj@17998
   254
(******************************************************)
mengj@17998
   255
(* data types for typed combinator expressions        *)
mengj@17998
   256
(******************************************************)
mengj@17998
   257
mengj@20281
   258
datatype type_level = T_FULL | T_PARTIAL | T_CONST | T_NONE;
mengj@20281
   259
mengj@20281
   260
val typ_level = ref T_CONST;
mengj@20281
   261
mengj@20281
   262
fun full_types () = (typ_level:=T_FULL);
mengj@20281
   263
fun partial_types () = (typ_level:=T_PARTIAL);
mengj@20281
   264
fun const_types_only () = (typ_level:=T_CONST);
mengj@20281
   265
fun no_types () = (typ_level:=T_NONE);
mengj@20281
   266
mengj@20281
   267
mengj@20281
   268
fun find_typ_level () = !typ_level;
mengj@20281
   269
mengj@20281
   270
mengj@17998
   271
type axiom_name = string;
mengj@17998
   272
datatype kind = Axiom | Conjecture;
paulson@20360
   273
mengj@17998
   274
fun name_of_kind Axiom = "axiom"
mengj@17998
   275
  | name_of_kind Conjecture = "conjecture";
mengj@17998
   276
mengj@17998
   277
type polarity = bool;
mengj@17998
   278
type indexname = Term.indexname;
mengj@17998
   279
type clause_id = int;
mengj@17998
   280
type csort = Term.sort;
mengj@18440
   281
type ctyp = ResClause.fol_type;
mengj@18440
   282
mengj@18440
   283
val string_of_ctyp = ResClause.string_of_fol_type;
mengj@17998
   284
mengj@17998
   285
type ctyp_var = ResClause.typ_var;
mengj@17998
   286
mengj@17998
   287
type ctype_literal = ResClause.type_literal;
mengj@17998
   288
mengj@17998
   289
mengj@18356
   290
datatype combterm = CombConst of string * ctyp * ctyp list
mengj@17998
   291
		  | CombFree of string * ctyp
mengj@17998
   292
		  | CombVar of string * ctyp
mengj@17998
   293
		  | CombApp of combterm * combterm * ctyp
mengj@19452
   294
		  | Bool of combterm;
paulson@20360
   295
		  
mengj@17998
   296
datatype literal = Literal of polarity * combterm;
mengj@17998
   297
mengj@17998
   298
datatype clause = 
mengj@17998
   299
	 Clause of {clause_id: clause_id,
mengj@17998
   300
		    axiom_name: axiom_name,
paulson@19964
   301
		    th: thm,
mengj@17998
   302
		    kind: kind,
mengj@17998
   303
		    literals: literal list,
mengj@17998
   304
		    ctypes_sorts: (ctyp_var * csort) list, 
mengj@17998
   305
                    ctvar_type_literals: ctype_literal list, 
mengj@17998
   306
                    ctfree_type_literals: ctype_literal list};
mengj@17998
   307
mengj@17998
   308
mengj@17998
   309
fun string_of_kind (Clause cls) = name_of_kind (#kind cls);
mengj@17998
   310
fun get_axiomName (Clause cls) = #axiom_name cls;
mengj@17998
   311
fun get_clause_id (Clause cls) = #clause_id cls;
mengj@17998
   312
mengj@18440
   313
fun get_literals (c as Clause(cls)) = #literals cls;
mengj@18440
   314
mengj@17998
   315
mengj@17998
   316
(*********************************************************************)
mengj@17998
   317
(* convert a clause with type Term.term to a clause with type clause *)
mengj@17998
   318
(*********************************************************************)
mengj@17998
   319
mengj@18356
   320
fun isFalse (Literal(pol,Bool(CombConst(c,_,_)))) =
paulson@20360
   321
      (pol andalso c = "c_False") orelse
paulson@20360
   322
      (not pol andalso c = "c_True")
mengj@17998
   323
  | isFalse _ = false;
mengj@17998
   324
mengj@17998
   325
mengj@18356
   326
fun isTrue (Literal (pol,Bool(CombConst(c,_,_)))) =
mengj@17998
   327
      (pol andalso c = "c_True") orelse
mengj@17998
   328
      (not pol andalso c = "c_False")
mengj@17998
   329
  | isTrue _ = false;
mengj@17998
   330
  
mengj@17998
   331
fun isTaut (Clause {literals,...}) = exists isTrue literals;  
mengj@17998
   332
mengj@18440
   333
fun type_of (Type (a, Ts)) =
mengj@18440
   334
    let val (folTypes,ts) = types_of Ts
mengj@17998
   335
	val t = ResClause.make_fixed_type_const a
mengj@17998
   336
    in
mengj@18440
   337
	(ResClause.mk_fol_type("Comp",t,folTypes),ts)
mengj@18440
   338
    end
mengj@18440
   339
  | type_of (tp as (TFree(a,s))) =
mengj@18440
   340
    let val t = ResClause.make_fixed_type_var a
mengj@18440
   341
    in
mengj@18440
   342
	(ResClause.mk_fol_type("Fixed",t,[]),[ResClause.mk_typ_var_sort tp])
mengj@17998
   343
    end
mengj@18440
   344
  | type_of (tp as (TVar(v,s))) =
mengj@18440
   345
    let val t = ResClause.make_schematic_type_var v
mengj@18440
   346
    in
mengj@18440
   347
	(ResClause.mk_fol_type("Var",t,[]),[ResClause.mk_typ_var_sort tp])
mengj@18440
   348
    end
mengj@18356
   349
mengj@18440
   350
and types_of Ts =
mengj@18440
   351
    let val foltyps_ts = map type_of Ts
mengj@18440
   352
	val (folTyps,ts) = ListPair.unzip foltyps_ts
mengj@18440
   353
    in
mengj@18440
   354
	(folTyps,ResClause.union_all ts)
mengj@18440
   355
    end;
mengj@17998
   356
mengj@17998
   357
(* same as above, but no gathering of sort information *)
mengj@18440
   358
fun simp_type_of (Type (a, Ts)) = 
mengj@18356
   359
    let val typs = map simp_type_of Ts
mengj@17998
   360
	val t = ResClause.make_fixed_type_const a
mengj@17998
   361
    in
mengj@18440
   362
	ResClause.mk_fol_type("Comp",t,typs)
mengj@17998
   363
    end
mengj@18440
   364
  | simp_type_of (TFree (a,s)) = ResClause.mk_fol_type("Fixed",ResClause.make_fixed_type_var a,[])
mengj@18440
   365
  | simp_type_of (TVar (v,s)) = ResClause.mk_fol_type("Var",ResClause.make_schematic_type_var v,[]);
mengj@18440
   366
mengj@18356
   367
mengj@20865
   368
fun const_type_of (c,t) =
mengj@18356
   369
    let val (tp,ts) = type_of t
mengj@18356
   370
	val tvars = !const_typargs(c,t)
mengj@18356
   371
	val tvars' = map simp_type_of tvars
mengj@18356
   372
    in
mengj@18356
   373
	(tp,ts,tvars')
mengj@18356
   374
    end;
mengj@18356
   375
mengj@20644
   376
mengj@18356
   377
fun is_bool_type (Type("bool",[])) = true
mengj@18356
   378
  | is_bool_type _ = false;
mengj@17998
   379
mengj@17998
   380
mengj@17998
   381
(* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
mengj@17998
   382
fun combterm_of (Const(c,t)) =
mengj@18356
   383
    let val (tp,ts,tvar_list) = const_type_of (c,t)
mengj@18356
   384
	val is_bool = is_bool_type t
mengj@18356
   385
	val c' = CombConst(ResClause.make_fixed_const c,tp,tvar_list)
mengj@17998
   386
	val c'' = if is_bool then Bool(c') else c'
mengj@17998
   387
    in
mengj@17998
   388
	(c'',ts)
mengj@17998
   389
    end
mengj@17998
   390
  | combterm_of (Free(v,t)) =
mengj@18356
   391
    let val (tp,ts) = type_of t
mengj@18356
   392
	val is_bool = is_bool_type t
mengj@17998
   393
	val v' = if ResClause.isMeta v then CombVar(ResClause.make_schematic_var(v,0),tp)
mengj@17998
   394
		 else CombFree(ResClause.make_fixed_var v,tp)
mengj@17998
   395
	val v'' = if is_bool then Bool(v') else v'
mengj@17998
   396
    in
mengj@17998
   397
	(v'',ts)
mengj@17998
   398
    end
mengj@17998
   399
  | combterm_of (Var(v,t)) =
mengj@18356
   400
    let val (tp,ts) = type_of t
mengj@18356
   401
	val is_bool = is_bool_type t
mengj@17998
   402
	val v' = CombVar(ResClause.make_schematic_var v,tp)
mengj@17998
   403
	val v'' = if is_bool then Bool(v') else v'
mengj@17998
   404
    in
mengj@17998
   405
	(v'',ts)
mengj@17998
   406
    end
mengj@17998
   407
  | combterm_of (t as (P $ Q)) =
mengj@17998
   408
    let val (P',tsP) = combterm_of P
mengj@17998
   409
	val (Q',tsQ) = combterm_of Q
mengj@17998
   410
	val tp = Term.type_of t
mengj@18356
   411
	val tp' = simp_type_of tp
mengj@18356
   412
	val is_bool = is_bool_type tp
mengj@17998
   413
	val t' = CombApp(P',Q',tp')
mengj@17998
   414
	val t'' = if is_bool then Bool(t') else t'
mengj@17998
   415
    in
mengj@17998
   416
	(t'',tsP union tsQ)
mengj@17998
   417
    end;
mengj@17998
   418
mengj@17998
   419
fun predicate_of ((Const("Not",_) $ P), polarity) =
mengj@17998
   420
    predicate_of (P, not polarity)
mengj@17998
   421
  | predicate_of (term,polarity) = (combterm_of term,polarity);
mengj@17998
   422
mengj@17998
   423
mengj@17998
   424
fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
mengj@17998
   425
  | literals_of_term1 args (Const("op |",_) $ P $ Q) = 
mengj@17998
   426
    let val args' = literals_of_term1 args P
mengj@17998
   427
    in
mengj@17998
   428
	literals_of_term1 args' Q
mengj@17998
   429
    end
mengj@17998
   430
  | literals_of_term1 (lits,ts) P =
mengj@17998
   431
    let val ((pred,ts'),pol) = predicate_of (P,true)
mengj@17998
   432
	val lits' = Literal(pol,pred)::lits
mengj@17998
   433
    in
mengj@17998
   434
	(lits',ts union ts')
mengj@17998
   435
    end;
mengj@17998
   436
mengj@17998
   437
mengj@17998
   438
fun literals_of_term P = literals_of_term1 ([],[]) P;
mengj@17998
   439
mengj@20274
   440
fun occurs a (CombVar(b,_)) = a = b
mengj@20274
   441
  | occurs a (CombApp(t1,t2,_)) = (occurs a t1) orelse (occurs a t2)
mengj@20274
   442
  | occurs _ _ = false
mengj@20274
   443
mengj@20274
   444
fun too_general_terms (CombVar(v,_), t) = not (occurs v t)
mengj@20274
   445
  | too_general_terms _ = false;
mengj@20274
   446
paulson@20360
   447
fun too_general_lit (Literal(true,(Bool(CombApp(CombApp(CombConst("equal",tp,tps),t1,tp1),t2,tp2))))) =
paulson@20360
   448
      too_general_terms (t1,t2) orelse too_general_terms (t2,t1)
mengj@20274
   449
  | too_general_lit _ = false;
mengj@20274
   450
mengj@20130
   451
(* forbid a clause that contains hBOOL(V) *)
mengj@20130
   452
fun too_general [] = false
mengj@20130
   453
  | too_general (lit::lits) = 
mengj@20130
   454
    case lit of Literal(_,Bool(CombVar(_,_))) => true
mengj@20130
   455
	      | _ => too_general lits;
mengj@20130
   456
mengj@17998
   457
(* making axiom and conjecture clauses *)
mengj@20274
   458
exception MAKE_CLAUSE
mengj@20130
   459
fun make_clause(clause_id,axiom_name,kind,thm,is_user) =
mengj@19444
   460
    let val term = prop_of thm
mengj@20130
   461
	val term' = comb_of term is_user
mengj@20016
   462
	val (lits,ctypes_sorts) = literals_of_term term'
paulson@18856
   463
	val (ctvar_lits,ctfree_lits) = ResClause.add_typs_aux ctypes_sorts
mengj@17998
   464
    in
mengj@20016
   465
	if forall isFalse lits
mengj@20016
   466
	then error "Problem too trivial for resolution (empty clause)"
paulson@20360
   467
	else if too_general lits 
paulson@20360
   468
	then (Output.debug ("Omitting " ^ axiom_name ^ ": clause contains universal predicates"); 
paulson@20360
   469
	     raise MAKE_CLAUSE)
mengj@20274
   470
	else
mengj@20281
   471
	    if (!typ_level <> T_FULL) andalso kind=Axiom andalso forall too_general_lit lits 
paulson@20360
   472
	    then (Output.debug ("Omitting " ^ axiom_name ^ ": equalities are too general"); 
paulson@20360
   473
	          raise MAKE_CLAUSE) 
mengj@20016
   474
	else
mengj@20016
   475
	    Clause {clause_id = clause_id, axiom_name = axiom_name, th = thm, kind = kind,
mengj@20016
   476
		    literals = lits, ctypes_sorts = ctypes_sorts, 
mengj@20016
   477
		    ctvar_type_literals = ctvar_lits,
mengj@20016
   478
		    ctfree_type_literals = ctfree_lits}
mengj@17998
   479
    end;
mengj@17998
   480
mengj@20016
   481
paulson@20360
   482
fun make_axiom_clause thm (ax_name,cls_id,is_user) = 
paulson@20360
   483
      make_clause(cls_id,ax_name,Axiom,thm,is_user);
mengj@20016
   484
 
mengj@20130
   485
fun make_axiom_clauses [] user_lemmas = []
mengj@20130
   486
  | make_axiom_clauses ((thm,(name,id))::thms) user_lemmas =
mengj@20130
   487
    let val is_user = name mem user_lemmas
paulson@20360
   488
	val cls = SOME (make_axiom_clause thm (name,id,is_user)) 
paulson@20360
   489
	          handle MAKE_CLAUSE => NONE
mengj@20130
   490
	val clss = make_axiom_clauses thms user_lemmas
mengj@19354
   491
    in
mengj@20130
   492
	case cls of NONE => clss
paulson@20360
   493
		  | SOME(cls') => if isTaut cls' then clss 
paulson@20360
   494
		                  else (name,cls')::clss
mengj@19354
   495
    end;
mengj@19354
   496
mengj@19354
   497
mengj@17998
   498
fun make_conjecture_clauses_aux _ [] = []
paulson@20421
   499
  | make_conjecture_clauses_aux n (th::ths) =
paulson@20421
   500
      make_clause(n,"conjecture",Conjecture,th,true) ::
paulson@20421
   501
      make_conjecture_clauses_aux (n+1) ths;
mengj@17998
   502
mengj@17998
   503
val make_conjecture_clauses = make_conjecture_clauses_aux 0;
mengj@17998
   504
mengj@17998
   505
mengj@17998
   506
(**********************************************************************)
mengj@17998
   507
(* convert clause into ATP specific formats:                          *)
mengj@17998
   508
(* TPTP used by Vampire and E                                         *)
mengj@19720
   509
(* DFG used by SPASS                                                  *)
mengj@17998
   510
(**********************************************************************)
mengj@17998
   511
mengj@17998
   512
val type_wrapper = "typeinfo";
mengj@17998
   513
mengj@18356
   514
fun wrap_type (c,t) = 
mengj@18356
   515
    case !typ_level of T_FULL => type_wrapper ^ (ResClause.paren_pack [c,t])
mengj@18356
   516
		     | _ => c;
mengj@18356
   517
    
mengj@17998
   518
mengj@17998
   519
val bool_tp = ResClause.make_fixed_type_const "bool";
mengj@17998
   520
mengj@17998
   521
val app_str = "hAPP";
mengj@17998
   522
mengj@17998
   523
val bool_str = "hBOOL";
mengj@17998
   524
mengj@18356
   525
exception STRING_OF_COMBTERM of int;
mengj@17998
   526
mengj@17998
   527
(* convert literals of clauses into strings *)
mengj@18440
   528
fun string_of_combterm1_aux _ (CombConst(c,tp,_)) = 
mengj@18440
   529
    let val tp' = string_of_ctyp tp
paulson@20864
   530
	val c' = if c = "equal" then "c_fequal" else c
mengj@18440
   531
    in
mengj@19452
   532
	(wrap_type (c',tp'),tp')
mengj@18440
   533
    end
mengj@18440
   534
  | string_of_combterm1_aux _ (CombFree(v,tp)) = 
mengj@18440
   535
    let val tp' = string_of_ctyp tp
mengj@18440
   536
    in
mengj@18440
   537
	(wrap_type (v,tp'),tp')
mengj@18440
   538
    end
mengj@18440
   539
  | string_of_combterm1_aux _ (CombVar(v,tp)) = 
mengj@18440
   540
    let val tp' = string_of_ctyp tp
mengj@18440
   541
    in
mengj@18440
   542
	(wrap_type (v,tp'),tp')
mengj@18440
   543
    end
mengj@18356
   544
  | string_of_combterm1_aux is_pred (CombApp(t1,t2,tp)) =
mengj@18356
   545
    let val (s1,tp1) = string_of_combterm1_aux is_pred t1
mengj@18356
   546
	val (s2,tp2) = string_of_combterm1_aux is_pred t2
mengj@18440
   547
	val tp' = ResClause.string_of_fol_type tp
mengj@18440
   548
	val r =	case !typ_level of T_FULL => type_wrapper ^  (ResClause.paren_pack [(app_str ^ (ResClause.paren_pack [s1,s2])),tp'])
mengj@18356
   549
				 | T_PARTIAL => app_str ^ (ResClause.paren_pack [s1,s2,tp1])
mengj@18356
   550
				 | T_NONE => app_str ^ (ResClause.paren_pack [s1,s2])
mengj@18356
   551
				 | T_CONST => raise STRING_OF_COMBTERM (1) (*should not happen, if happened may be a bug*)
mengj@18440
   552
    in	(r,tp')
mengj@18356
   553
mengj@18356
   554
    end
mengj@19452
   555
  | string_of_combterm1_aux is_pred (Bool(CombApp(CombApp(CombConst("equal",tp,tps),t1,tp1),t2,tp2))) =
mengj@19452
   556
    if is_pred then 
mengj@19452
   557
	let val (s1,_) = string_of_combterm1_aux false t1
mengj@19452
   558
	    val (s2,_) = string_of_combterm1_aux false t2
mengj@19452
   559
	in
mengj@19452
   560
	    ("equal" ^ (ResClause.paren_pack [s1,s2]),bool_tp)
mengj@19452
   561
	end
mengj@19452
   562
    else
mengj@19452
   563
	let val (t,_) = string_of_combterm1_aux false (CombApp(CombApp(CombConst("equal",tp,tps),t1,tp1),t2,tp2))
mengj@19452
   564
	in
mengj@19452
   565
	    (t,bool_tp)
mengj@19452
   566
	end
mengj@18356
   567
  | string_of_combterm1_aux is_pred (Bool(t)) = 
mengj@18356
   568
    let val (t',_) = string_of_combterm1_aux false t
mengj@18356
   569
	val r = if is_pred then bool_str ^ (ResClause.paren_pack [t'])
mengj@18356
   570
		else t'
mengj@17998
   571
    in
mengj@18356
   572
	(r,bool_tp)
mengj@18356
   573
    end;
mengj@18356
   574
mengj@18356
   575
fun string_of_combterm1 is_pred term = fst (string_of_combterm1_aux is_pred term);
mengj@18356
   576
mengj@18440
   577
fun string_of_combterm2 _ (CombConst(c,tp,tvars)) = 
mengj@18440
   578
    let val tvars' = map string_of_ctyp tvars
paulson@20864
   579
	val c' = if c = "equal" then "c_fequal" else c
mengj@18440
   580
    in
mengj@19452
   581
	c' ^ (ResClause.paren_pack tvars')
mengj@18440
   582
    end
mengj@18356
   583
  | string_of_combterm2 _ (CombFree(v,tp)) = v
mengj@18356
   584
  | string_of_combterm2 _ (CombVar(v,tp)) = v
mengj@18356
   585
  | string_of_combterm2 is_pred (CombApp(t1,t2,tp)) =
mengj@18356
   586
    let val s1 = string_of_combterm2 is_pred t1
mengj@18356
   587
	val s2 = string_of_combterm2 is_pred t2
mengj@18356
   588
    in
mengj@18356
   589
	app_str ^ (ResClause.paren_pack [s1,s2])
mengj@18356
   590
    end
mengj@19452
   591
  | string_of_combterm2 is_pred (Bool(CombApp(CombApp(CombConst("equal",tp,tps),t1,tp1),t2,tp2))) =
mengj@19452
   592
    if is_pred then 
mengj@19452
   593
	let val s1 = string_of_combterm2 false t1
mengj@19452
   594
	    val s2 = string_of_combterm2 false t2
mengj@19452
   595
	in
mengj@19452
   596
	    ("equal" ^ (ResClause.paren_pack [s1,s2]))
mengj@19452
   597
	end
mengj@19452
   598
    else
mengj@19452
   599
	string_of_combterm2 false (CombApp(CombApp(CombConst("equal",tp,tps),t1,tp1),t2,tp2))
mengj@19452
   600
 
mengj@18356
   601
  | string_of_combterm2 is_pred (Bool(t)) = 
mengj@18356
   602
    let val t' = string_of_combterm2 false t
mengj@17998
   603
    in
mengj@18200
   604
	if is_pred then bool_str ^ (ResClause.paren_pack [t'])
mengj@18200
   605
	else t'
mengj@17998
   606
    end;
mengj@17998
   607
mengj@18356
   608
mengj@18356
   609
mengj@18356
   610
fun string_of_combterm is_pred term = 
mengj@18356
   611
    case !typ_level of T_CONST => string_of_combterm2 is_pred term
mengj@18356
   612
		     | _ => string_of_combterm1 is_pred term;
mengj@18356
   613
mengj@18356
   614
mengj@17998
   615
fun string_of_clausename (cls_id,ax_name) = 
mengj@17998
   616
    ResClause.clause_prefix ^ ResClause.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
mengj@17998
   617
mengj@17998
   618
fun string_of_type_clsname (cls_id,ax_name,idx) = 
mengj@17998
   619
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
mengj@17998
   620
mengj@17998
   621
mengj@19720
   622
(* tptp format *)
mengj@19720
   623
mengj@17998
   624
fun tptp_literal (Literal(pol,pred)) =
mengj@18200
   625
    let val pred_string = string_of_combterm true pred
mengj@17998
   626
	val pol_str = if pol then "++" else "--"
mengj@17998
   627
    in
mengj@17998
   628
	pol_str ^ pred_string
mengj@17998
   629
    end;
mengj@17998
   630
mengj@17998
   631
 
mengj@17998
   632
fun tptp_type_lits (Clause cls) = 
mengj@17998
   633
    let val lits = map tptp_literal (#literals cls)
mengj@17998
   634
	val ctvar_lits_strs =
mengj@18356
   635
	    case !typ_level of T_NONE => []
paulson@20360
   636
	      | _ => map ResClause.tptp_of_typeLit (#ctvar_type_literals cls)
mengj@17998
   637
	val ctfree_lits = 
mengj@18356
   638
	    case !typ_level of T_NONE => []
paulson@20360
   639
	      | _ => map ResClause.tptp_of_typeLit (#ctfree_type_literals cls)
mengj@17998
   640
    in
mengj@17998
   641
	(ctvar_lits_strs @ lits, ctfree_lits)
mengj@17998
   642
    end; 
mengj@18356
   643
    
mengj@18356
   644
    
mengj@17998
   645
fun clause2tptp cls =
mengj@17998
   646
    let val (lits,ctfree_lits) = tptp_type_lits cls
mengj@17998
   647
	val cls_id = get_clause_id cls
mengj@17998
   648
	val ax_name = get_axiomName cls
mengj@17998
   649
	val knd = string_of_kind cls
mengj@17998
   650
	val lits_str = ResClause.bracket_pack lits
mengj@17998
   651
	val cls_str = ResClause.gen_tptp_cls(cls_id,ax_name,knd,lits_str)
mengj@17998
   652
    in
mengj@17998
   653
	(cls_str,ctfree_lits)
mengj@17998
   654
    end;
mengj@17998
   655
mengj@17998
   656
mengj@19720
   657
(* dfg format *)
mengj@19720
   658
fun dfg_literal (Literal(pol,pred)) = ResClause.dfg_sign pol (string_of_combterm true pred);
mengj@19720
   659
mengj@19720
   660
fun dfg_clause_aux (Clause{literals, ctypes_sorts, ...}) = 
mengj@19720
   661
  let val lits = map dfg_literal literals
mengj@19720
   662
      val (tvar_lits,tfree_lits) = ResClause.add_typs_aux ctypes_sorts
mengj@19720
   663
      val tvar_lits_strs = 
mengj@19720
   664
	  case !typ_level of T_NONE => [] 
paulson@20360
   665
	      | _ => map ResClause.dfg_of_typeLit tvar_lits
mengj@19720
   666
      val tfree_lits =
mengj@19720
   667
          case !typ_level of T_NONE => []
paulson@20360
   668
	      | _ => map ResClause.dfg_of_typeLit tfree_lits 
mengj@19720
   669
  in
mengj@19720
   670
      (tvar_lits_strs @ lits, tfree_lits)
mengj@19720
   671
  end; 
mengj@19720
   672
mengj@19720
   673
fun get_uvars (CombConst(_,_,_)) vars = vars
mengj@19720
   674
  | get_uvars (CombFree(_,_)) vars = vars
mengj@19720
   675
  | get_uvars (CombVar(v,tp)) vars = (v::vars)
mengj@19720
   676
  | get_uvars (CombApp(P,Q,tp)) vars = get_uvars P (get_uvars Q vars)
mengj@19720
   677
  | get_uvars (Bool(c)) vars = get_uvars c vars;
mengj@19720
   678
mengj@19720
   679
mengj@19720
   680
fun get_uvars_l (Literal(_,c)) = get_uvars c [];
mengj@19720
   681
mengj@19720
   682
fun dfg_vars (Clause {literals,...}) = ResClause.union_all (map get_uvars_l literals);
mengj@19720
   683
 
mengj@19720
   684
fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
mengj@19720
   685
    let val (lits,tfree_lits) = dfg_clause_aux cls 
mengj@19720
   686
        val vars = dfg_vars cls
mengj@19720
   687
        val tvars = ResClause.get_tvar_strs ctypes_sorts
mengj@19720
   688
	val knd = name_of_kind kind
mengj@19720
   689
	val lits_str = commas lits
mengj@19720
   690
	val cls_str = ResClause.gen_dfg_cls(clause_id, axiom_name, knd, lits_str, tvars@vars) 
mengj@19720
   691
    in (cls_str, tfree_lits) end;
mengj@19720
   692
mengj@19720
   693
mengj@19720
   694
fun init_funcs_tab funcs = 
mengj@19724
   695
    let val tp = !typ_level
mengj@20865
   696
	val funcs1 = case tp of T_PARTIAL => Symtab.update ("hAPP",3) funcs
mengj@20865
   697
				      | _ => Symtab.update ("hAPP",2) funcs
mengj@19724
   698
	val funcs2 = case tp of T_FULL => Symtab.update ("typeinfo",2) funcs1
mengj@19724
   699
				      | _ => funcs1
mengj@19724
   700
    in
mengj@20865
   701
	funcs2
mengj@19724
   702
    end;
mengj@19720
   703
mengj@19720
   704
mengj@19725
   705
fun add_funcs (CombConst(c,_,tvars),funcs) =
mengj@19725
   706
    if c = "equal" then foldl ResClause.add_foltype_funcs funcs tvars
mengj@19725
   707
    else
mengj@19725
   708
	(case !typ_level of T_CONST => foldl ResClause.add_foltype_funcs (Symtab.update(c,length tvars) funcs) tvars
mengj@19725
   709
			  | _ => foldl ResClause.add_foltype_funcs (Symtab.update(c,0) funcs) tvars)
mengj@19724
   710
  | add_funcs (CombFree(v,ctp),funcs) = ResClause.add_foltype_funcs (ctp,Symtab.update (v,0) funcs) 
mengj@19720
   711
  | add_funcs (CombVar(_,ctp),funcs) = ResClause.add_foltype_funcs (ctp,funcs)
mengj@19720
   712
  | add_funcs (CombApp(P,Q,_),funcs) = add_funcs(P,add_funcs (Q,funcs))
mengj@19720
   713
  | add_funcs (Bool(t),funcs) = add_funcs (t,funcs);
mengj@19720
   714
mengj@19720
   715
mengj@19720
   716
fun add_literal_funcs (Literal(_,c), funcs) = add_funcs (c,funcs);
mengj@19720
   717
mengj@19720
   718
fun add_clause_funcs (Clause {literals, ...}, funcs) =
mengj@19720
   719
    foldl add_literal_funcs funcs literals
mengj@19720
   720
    handle Symtab.DUP a => raise ERROR ("function " ^ a ^ " has multiple arities")
mengj@19720
   721
mengj@19720
   722
fun funcs_of_clauses clauses arity_clauses =
mengj@19720
   723
    Symtab.dest (foldl ResClause.add_arityClause_funcs 
mengj@19720
   724
                       (foldl add_clause_funcs (init_funcs_tab Symtab.empty) clauses)
mengj@19720
   725
                       arity_clauses)
mengj@19720
   726
mengj@19720
   727
fun preds_of clsrel_clauses arity_clauses = 
mengj@19720
   728
    Symtab.dest
mengj@19720
   729
	(foldl ResClause.add_classrelClause_preds 
mengj@19720
   730
	       (foldl ResClause.add_arityClause_preds
mengj@19720
   731
		      (Symtab.update ("hBOOL",1) Symtab.empty)
mengj@19720
   732
		      arity_clauses)
mengj@19720
   733
	       clsrel_clauses)
mengj@19720
   734
mengj@18440
   735
mengj@18440
   736
(**********************************************************************)
mengj@19198
   737
(* write clauses to files                                             *)
mengj@19198
   738
(**********************************************************************)
mengj@19198
   739
mengj@20791
   740
local
mengj@19198
   741
mengj@20791
   742
val cnf_helper_thms = ResAxioms.cnf_rules_pairs o (map ResAxioms.pairname)
mengj@20644
   743
mengj@20791
   744
in
mengj@20644
   745
mengj@20791
   746
fun get_helper_clauses () =
mengj@20791
   747
    let val IK = if !combI_count > 0 orelse !combK_count > 0 then (Output.debug "Include combinator I K"; cnf_helper_thms [comb_I,comb_K]) else []
mengj@20791
   748
	val BC = if !combB_count > 0 orelse !combC_count > 0 then (Output.debug "Include combinator B C"; cnf_helper_thms [comb_B,comb_C]) else []
mengj@20791
   749
	val S = if !combS_count > 0 then (Output.debug "Include combinator S"; cnf_helper_thms [comb_S]) else []
mengj@20791
   750
	val B'C' = if !combB'_count > 0 orelse !combC'_count > 0 then (Output.debug "Include combinator B' C'"; cnf_helper_thms [comb_B', comb_C']) else []
mengj@20791
   751
	val S' = if !combS'_count > 0 then (Output.debug "Include combinator S'"; cnf_helper_thms [comb_S']) else []
mengj@20791
   752
	val other = cnf_helper_thms [ext,fequal_imp_equal,equal_imp_fequal]
mengj@20791
   753
    in
mengj@20791
   754
	make_axiom_clauses (other @ IK @ BC @ S @ B'C' @ S') []
mengj@20791
   755
    end
mengj@20791
   756
mengj@20791
   757
end
mengj@20791
   758
mengj@20791
   759
(* tptp format *)
mengj@19491
   760
						  
mengj@19198
   761
(* write TPTP format to a single file *)
mengj@19198
   762
(* when "get_helper_clauses" is called, "include_combS" and "include_min_comb" should have correct values already *)
mengj@20130
   763
fun tptp_write_file thms filename (axclauses,classrel_clauses,arity_clauses) user_lemmas=
mengj@19444
   764
    let val clss = make_conjecture_clauses thms
mengj@20130
   765
        val (clnames,axclauses') = ListPair.unzip (make_axiom_clauses axclauses user_lemmas)
mengj@19198
   766
	val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp clss)
mengj@19198
   767
	val tfree_clss = map ResClause.tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
mengj@19198
   768
	val out = TextIO.openOut filename
mengj@20791
   769
	val helper_clauses = (#2 o ListPair.unzip o get_helper_clauses) ()
mengj@19198
   770
    in
mengj@19198
   771
	List.app (curry TextIO.output out o #1 o clause2tptp) axclauses';
mengj@19198
   772
	ResClause.writeln_strs out tfree_clss;
mengj@19198
   773
	ResClause.writeln_strs out tptp_clss;
mengj@19198
   774
	List.app (curry TextIO.output out o ResClause.tptp_classrelClause) classrel_clauses;
mengj@19198
   775
	List.app (curry TextIO.output out o ResClause.tptp_arity_clause) arity_clauses;
mengj@20791
   776
	List.app (curry TextIO.output out o #1 o clause2tptp) helper_clauses;
paulson@20022
   777
	TextIO.closeOut out;
paulson@20022
   778
	clnames
mengj@19198
   779
    end;
mengj@19198
   780
mengj@19720
   781
mengj@20644
   782
mengj@19720
   783
(* dfg format *)
mengj@19720
   784
mengj@20130
   785
fun dfg_write_file  thms filename (axclauses,classrel_clauses,arity_clauses) user_lemmas =
mengj@19720
   786
    let val _ = Output.debug ("Preparing to write the DFG file " ^ filename) 
mengj@19720
   787
	val conjectures = make_conjecture_clauses thms
mengj@20130
   788
        val (clnames,axclauses') = ListPair.unzip (make_axiom_clauses axclauses user_lemmas)
mengj@19720
   789
	val (dfg_clss,tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
mengj@19720
   790
	and probname = Path.pack (Path.base (Path.unpack filename))
mengj@19720
   791
	val (axstrs,_) =  ListPair.unzip (map clause2dfg axclauses')
mengj@19720
   792
	val tfree_clss = map ResClause.dfg_tfree_clause (ResClause.union_all tfree_litss)
mengj@19720
   793
	val out = TextIO.openOut filename
mengj@20791
   794
	val helper_clauses = (#2 o ListPair.unzip o get_helper_clauses) ()
mengj@20791
   795
	val helper_clauses_strs = (#1 o ListPair.unzip o (map clause2dfg)) helper_clauses
paulson@20864
   796
	val funcs = funcs_of_clauses (helper_clauses @ conjectures @ axclauses') arity_clauses
paulson@20864
   797
	and preds = preds_of classrel_clauses arity_clauses
mengj@19720
   798
    in
mengj@19720
   799
	TextIO.output (out, ResClause.string_of_start probname); 
mengj@19720
   800
	TextIO.output (out, ResClause.string_of_descrip probname); 
mengj@19720
   801
	TextIO.output (out, ResClause.string_of_symbols (ResClause.string_of_funcs funcs) (ResClause.string_of_preds preds)); 
mengj@19720
   802
	TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
mengj@19720
   803
	ResClause.writeln_strs out axstrs;
mengj@19720
   804
	List.app (curry TextIO.output out o ResClause.dfg_classrelClause) classrel_clauses;
mengj@19720
   805
	List.app (curry TextIO.output out o ResClause.dfg_arity_clause) arity_clauses;
mengj@20791
   806
	ResClause.writeln_strs out helper_clauses_strs;
mengj@19720
   807
	TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
mengj@19720
   808
	ResClause.writeln_strs out tfree_clss;
mengj@19720
   809
	ResClause.writeln_strs out dfg_clss;
paulson@20953
   810
	TextIO.output (out, "end_of_list.\n\n");
paulson@20953
   811
	(*VarWeight=3 helps the HO problems, probably by counteracting the presence of hAPP*)
paulson@20953
   812
	TextIO.output (out, "list_of_settings(SPASS).\n{*\nset_flag(VarWeight,3).\n*}\nend_of_list.\n\n");
paulson@20953
   813
	TextIO.output (out, "end_problem.\n");
paulson@20022
   814
	TextIO.closeOut out;
paulson@20022
   815
	clnames
mengj@19720
   816
    end;
mengj@19720
   817
mengj@17998
   818
end