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