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