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