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