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