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