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