src/HOL/Tools/res_hol_clause.ML
author mengj
Tue Dec 06 06:21:07 2005 +0100 (2005-12-06 ago)
changeset 18356 443717b3a9ad
parent 18276 c62ec94e326e
child 18440 72ee07f4b56b
permissions -rw-r--r--
Added new type embedding methods for translating HOL clauses.
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@17998
    20
(**********************************************************************)
mengj@17998
    21
(* convert a Term.term with lambdas into a Term.term with combinators *) 
mengj@17998
    22
(**********************************************************************)
mengj@17998
    23
mengj@17998
    24
fun is_free (Bound(a)) n = (a = n)
mengj@17998
    25
  | is_free (Abs(x,_,b)) n = (is_free b (n+1))
mengj@17998
    26
  | is_free (P $ Q) n = ((is_free P n) orelse (is_free Q n))
mengj@17998
    27
  | is_free _ _ = false;
mengj@17998
    28
mengj@17998
    29
mengj@17998
    30
exception LAM2COMB of term;
mengj@17998
    31
mengj@17998
    32
exception BND of term;
mengj@17998
    33
mengj@17998
    34
fun decre_bndVar (Bound n) = Bound (n-1)
mengj@17998
    35
  | decre_bndVar (P $ Q) = (decre_bndVar P) $ (decre_bndVar Q)
mengj@17998
    36
  | decre_bndVar t =
mengj@17998
    37
    case t of Const(_,_) => t
mengj@17998
    38
	    | Free(_,_) => t
mengj@17998
    39
	    | Var(_,_) => t
mengj@17998
    40
	    | Abs(_,_,_) => raise BND(t); (*should not occur*)
mengj@17998
    41
mengj@17998
    42
mengj@17998
    43
(*******************************************)
mengj@17998
    44
fun lam2comb (Abs(x,tp,Bound 0)) _ = 
mengj@17998
    45
    let val tpI = Type("fun",[tp,tp])
mengj@17998
    46
    in 
mengj@18276
    47
	include_min_comb:=true;
mengj@17998
    48
	Const("COMBI",tpI) 
mengj@17998
    49
    end
mengj@18356
    50
  | lam2comb (Abs(x,tp,Bound n)) Bnds = 
mengj@18356
    51
    let val (Bound n') = decre_bndVar (Bound n)
mengj@18356
    52
	val tb = List.nth(Bnds,n')
mengj@18356
    53
	val tK = Type("fun",[tb,Type("fun",[tp,tb])])
mengj@18356
    54
    in
mengj@18356
    55
	include_min_comb:=true;
mengj@18356
    56
	Const("COMBK",tK) $ (Bound n')
mengj@18356
    57
    end
mengj@17998
    58
  | lam2comb (Abs(x,t1,Const(c,t2))) _ = 
mengj@17998
    59
    let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
mengj@17998
    60
    in 
mengj@18276
    61
	include_min_comb:=true;
mengj@17998
    62
	Const("COMBK",tK) $ Const(c,t2) 
mengj@17998
    63
    end
mengj@17998
    64
  | lam2comb (Abs(x,t1,Free(v,t2))) _ =
mengj@17998
    65
    let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
mengj@17998
    66
    in
mengj@18276
    67
	include_min_comb:=true;
mengj@17998
    68
	Const("COMBK",tK) $ Free(v,t2)
mengj@17998
    69
    end
mengj@17998
    70
  | lam2comb (Abs(x,t1,Var(ind,t2))) _=
mengj@17998
    71
    let val tK = Type("fun",[t2,Type("fun",[t1,t2])])
mengj@17998
    72
    in
mengj@18276
    73
	include_min_comb:=true;
mengj@17998
    74
	Const("COMBK",tK) $ Var(ind,t2)
mengj@17998
    75
    end
mengj@17998
    76
  | lam2comb (t as (Abs(x,t1,P$(Bound 0)))) Bnds =
mengj@17998
    77
    let val nfreeP = not(is_free P 0)
mengj@17998
    78
	val tr = Term.type_of1(t1::Bnds,P)
mengj@17998
    79
    in
mengj@17998
    80
	if nfreeP then (decre_bndVar P)
mengj@17998
    81
	else (
mengj@17998
    82
	      let val tI = Type("fun",[t1,t1])
mengj@17998
    83
		  val P' = lam2comb (Abs(x,t1,P)) Bnds
mengj@17998
    84
		  val tp' = Term.type_of1(Bnds,P')
mengj@17998
    85
		  val tS = Type("fun",[tp',Type("fun",[tI,tr])])
mengj@17998
    86
	      in
mengj@18276
    87
		  include_min_comb:=true;
mengj@18276
    88
		  include_combS:=true;
mengj@17998
    89
		  Const("COMBS",tS) $ P' $ Const("COMBI",tI)
mengj@17998
    90
	      end)
mengj@17998
    91
    end
mengj@17998
    92
	    
mengj@17998
    93
  | lam2comb (t as (Abs(x,t1,P$Q))) Bnds =
mengj@17998
    94
    let val (nfreeP,nfreeQ) = (not(is_free P 0),not(is_free Q 0))
mengj@17998
    95
	val tpq = Term.type_of1(t1::Bnds, P$Q) 
mengj@17998
    96
    in
mengj@17998
    97
	if(nfreeP andalso nfreeQ) then (
mengj@17998
    98
	    let val tK = Type("fun",[tpq,Type("fun",[t1,tpq])])
mengj@17998
    99
		val PQ' = decre_bndVar(P $ Q)
mengj@17998
   100
	    in 
mengj@18276
   101
		include_min_comb:=true;
mengj@17998
   102
		Const("COMBK",tK) $ PQ'
mengj@17998
   103
	    end)
mengj@17998
   104
	else (
mengj@17998
   105
	      if nfreeP then (
mengj@17998
   106
			       let val Q' = lam2comb (Abs(x,t1,Q)) Bnds
mengj@17998
   107
				   val P' = decre_bndVar P
mengj@17998
   108
				   val tp = Term.type_of1(Bnds,P')
mengj@17998
   109
				   val tq' = Term.type_of1(Bnds, Q')
mengj@17998
   110
				   val tB = Type("fun",[tp,Type("fun",[tq',Type("fun",[t1,tpq])])])
mengj@17998
   111
			       in
mengj@18276
   112
				   include_min_comb:=true;
mengj@17998
   113
				   Const("COMBB",tB) $ P' $ Q' 
mengj@17998
   114
			       end)
mengj@17998
   115
	      else (
mengj@17998
   116
		    if nfreeQ then (
mengj@17998
   117
				    let val P' = lam2comb (Abs(x,t1,P)) Bnds
mengj@17998
   118
					val Q' = decre_bndVar Q
mengj@17998
   119
					val tq = Term.type_of1(Bnds,Q')
mengj@17998
   120
					val tp' = Term.type_of1(Bnds, P')
mengj@17998
   121
					val tC = Type("fun",[tp',Type("fun",[tq,Type("fun",[t1,tpq])])])
mengj@17998
   122
				    in
mengj@18276
   123
					include_min_comb:=true;
mengj@17998
   124
					Const("COMBC",tC) $ P' $ Q'
mengj@17998
   125
				    end)
mengj@17998
   126
		    else(
mengj@17998
   127
			 let val P' = lam2comb (Abs(x,t1,P)) Bnds
mengj@17998
   128
			     val Q' = lam2comb (Abs(x,t1,Q)) Bnds
mengj@17998
   129
			     val tp' = Term.type_of1(Bnds,P')
mengj@17998
   130
			     val tq' = Term.type_of1(Bnds,Q')
mengj@17998
   131
			     val tS = Type("fun",[tp',Type("fun",[tq',Type("fun",[t1,tpq])])])
mengj@17998
   132
			 in
mengj@18276
   133
			     include_min_comb:=true;
mengj@18276
   134
			     include_combS:=true;
mengj@17998
   135
			     Const("COMBS",tS) $ P' $ Q'
mengj@17998
   136
			 end)))
mengj@17998
   137
    end
mengj@17998
   138
  | lam2comb (t as (Abs(x,t1,_))) _ = raise LAM2COMB (t);
mengj@17998
   139
mengj@17998
   140
	     
mengj@17998
   141
mengj@17998
   142
(*********************)
mengj@17998
   143
mengj@17998
   144
fun to_comb (Abs(x,tp,b)) Bnds =
mengj@17998
   145
    let val b' = to_comb b (tp::Bnds)
mengj@17998
   146
    in lam2comb (Abs(x,tp,b')) Bnds end
mengj@17998
   147
  | to_comb (P $ Q) Bnds = ((to_comb P Bnds) $ (to_comb Q Bnds))
mengj@17998
   148
  | to_comb t _ = t;
mengj@17998
   149
 
mengj@17998
   150
    
mengj@17998
   151
fun comb_of t = to_comb t [];
mengj@17998
   152
mengj@17998
   153
mengj@17998
   154
(* print a term containing combinators, used for debugging *)
mengj@17998
   155
exception TERM_COMB of term;
mengj@17998
   156
mengj@17998
   157
fun string_of_term (Const(c,t)) = c
mengj@17998
   158
  | string_of_term (Free(v,t)) = v
mengj@17998
   159
  | string_of_term (Var((x,n),t)) =
mengj@17998
   160
    let val xn = x ^ "_" ^ (string_of_int n)
mengj@17998
   161
    in xn end
mengj@17998
   162
  | string_of_term (P $ Q) =
mengj@17998
   163
    let val P' = string_of_term P
mengj@17998
   164
	val Q' = string_of_term Q
mengj@17998
   165
    in
mengj@17998
   166
	"(" ^ P' ^ " " ^ Q' ^ ")" end
mengj@17998
   167
  | string_of_term t =  raise TERM_COMB (t);
mengj@17998
   168
mengj@17998
   169
mengj@17998
   170
mengj@17998
   171
(******************************************************)
mengj@17998
   172
(* data types for typed combinator expressions        *)
mengj@17998
   173
(******************************************************)
mengj@17998
   174
mengj@17998
   175
type axiom_name = string;
mengj@17998
   176
datatype kind = Axiom | Conjecture;
mengj@17998
   177
fun name_of_kind Axiom = "axiom"
mengj@17998
   178
  | name_of_kind Conjecture = "conjecture";
mengj@17998
   179
mengj@17998
   180
type polarity = bool;
mengj@17998
   181
type indexname = Term.indexname;
mengj@17998
   182
type clause_id = int;
mengj@17998
   183
type csort = Term.sort;
mengj@17998
   184
type ctyp = string;
mengj@17998
   185
mengj@17998
   186
type ctyp_var = ResClause.typ_var;
mengj@17998
   187
mengj@17998
   188
type ctype_literal = ResClause.type_literal;
mengj@17998
   189
mengj@17998
   190
mengj@18356
   191
datatype combterm = CombConst of string * ctyp * ctyp list
mengj@17998
   192
		  | CombFree of string * ctyp
mengj@17998
   193
		  | CombVar of string * ctyp
mengj@17998
   194
		  | CombApp of combterm * combterm * ctyp
mengj@17998
   195
		  | Bool of combterm
mengj@17998
   196
		  | Equal of combterm * combterm;
mengj@17998
   197
datatype literal = Literal of polarity * combterm;
mengj@17998
   198
mengj@17998
   199
mengj@17998
   200
mengj@17998
   201
datatype clause = 
mengj@17998
   202
	 Clause of {clause_id: clause_id,
mengj@17998
   203
		    axiom_name: axiom_name,
mengj@17998
   204
		    kind: kind,
mengj@17998
   205
		    literals: literal list,
mengj@17998
   206
		    ctypes_sorts: (ctyp_var * csort) list, 
mengj@17998
   207
                    ctvar_type_literals: ctype_literal list, 
mengj@17998
   208
                    ctfree_type_literals: ctype_literal list};
mengj@17998
   209
mengj@17998
   210
mengj@17998
   211
mengj@17998
   212
fun string_of_kind (Clause cls) = name_of_kind (#kind cls);
mengj@17998
   213
fun get_axiomName (Clause cls) = #axiom_name cls;
mengj@17998
   214
fun get_clause_id (Clause cls) = #clause_id cls;
mengj@17998
   215
mengj@17998
   216
mengj@17998
   217
mengj@17998
   218
mengj@17998
   219
(*********************************************************************)
mengj@17998
   220
(* convert a clause with type Term.term to a clause with type clause *)
mengj@17998
   221
(*********************************************************************)
mengj@17998
   222
mengj@18356
   223
fun isFalse (Literal(pol,Bool(CombConst(c,_,_)))) =
mengj@17998
   224
    (pol andalso c = "c_False") orelse
mengj@17998
   225
    (not pol andalso c = "c_True")
mengj@17998
   226
  | isFalse _ = false;
mengj@17998
   227
mengj@17998
   228
mengj@18356
   229
fun isTrue (Literal (pol,Bool(CombConst(c,_,_)))) =
mengj@17998
   230
      (pol andalso c = "c_True") orelse
mengj@17998
   231
      (not pol andalso c = "c_False")
mengj@17998
   232
  | isTrue _ = false;
mengj@17998
   233
  
mengj@17998
   234
fun isTaut (Clause {literals,...}) = exists isTrue literals;  
mengj@17998
   235
mengj@17998
   236
mengj@17998
   237
mengj@17998
   238
fun make_clause(clause_id,axiom_name,kind,literals,ctypes_sorts,ctvar_type_literals,ctfree_type_literals) =
mengj@17998
   239
    if forall isFalse literals
mengj@17998
   240
    then error "Problem too trivial for resolution (empty clause)"
mengj@17998
   241
    else
mengj@17998
   242
	Clause {clause_id = clause_id, axiom_name = axiom_name, kind = kind,
mengj@17998
   243
		literals = literals, ctypes_sorts = ctypes_sorts, 
mengj@17998
   244
		ctvar_type_literals = ctvar_type_literals,
mengj@17998
   245
		ctfree_type_literals = ctfree_type_literals};
mengj@17998
   246
mengj@18356
   247
(* convert a Term.type to a string; gather sort information of type variables *)
mengj@18356
   248
fun type_of (Type (a, [])) = (ResClause.make_fixed_type_const a,[])
mengj@17998
   249
  | type_of (Type (a, Ts)) = 
mengj@18356
   250
    let val typ_ts = map type_of Ts
mengj@18356
   251
	val (typs,tsorts) = ListPair.unzip typ_ts
mengj@18356
   252
	val ts = ResClause.union_all tsorts
mengj@17998
   253
	val t = ResClause.make_fixed_type_const a
mengj@17998
   254
    in
mengj@18356
   255
	(t ^ (ResClause.paren_pack typs),ts)
mengj@17998
   256
    end
mengj@18356
   257
  | type_of (tp as (TFree (a,s))) = (ResClause.make_fixed_type_var a,[ResClause.mk_typ_var_sort tp])
mengj@18356
   258
  | type_of (tp as (TVar (v,s))) = (ResClause.make_schematic_type_var v,[ResClause.mk_typ_var_sort tp]);
mengj@18356
   259
mengj@17998
   260
mengj@17998
   261
mengj@17998
   262
(* same as above, but no gathering of sort information *)
mengj@18356
   263
fun simp_type_of (Type (a, [])) = ResClause.make_fixed_type_const a
mengj@17998
   264
  | simp_type_of (Type (a, Ts)) = 
mengj@18356
   265
    let val typs = map simp_type_of Ts
mengj@17998
   266
	val t = ResClause.make_fixed_type_const a
mengj@17998
   267
    in
mengj@18356
   268
	t ^ ResClause.paren_pack typs
mengj@17998
   269
    end
mengj@18356
   270
  | simp_type_of (TFree (a,s)) = ResClause.make_fixed_type_var a
mengj@18356
   271
  | simp_type_of (TVar (v,s)) = ResClause.make_schematic_type_var v;
mengj@17998
   272
mengj@17998
   273
mengj@18356
   274
fun comb_typ ("COMBI",t) = 
mengj@18356
   275
    let val t' = domain_type t
mengj@18356
   276
    in
mengj@18356
   277
	[simp_type_of t']
mengj@18356
   278
    end
mengj@18356
   279
  | comb_typ ("COMBK",t) = 
mengj@18356
   280
    let val (ab,_) = strip_type t
mengj@18356
   281
    in
mengj@18356
   282
	map simp_type_of ab
mengj@18356
   283
    end
mengj@18356
   284
  | comb_typ ("COMBS",t) = 
mengj@18356
   285
    let val t' = domain_type t
mengj@18356
   286
	val ([a,b],c) = strip_type t' 
mengj@18356
   287
    in 
mengj@18356
   288
	map simp_type_of [a,b,c]
mengj@18356
   289
    end
mengj@18356
   290
  | comb_typ ("COMBB",t) = 
mengj@18356
   291
    let val ([ab,ca,c],b) = strip_type t
mengj@18356
   292
	val a = domain_type ab
mengj@18356
   293
    in
mengj@18356
   294
	map simp_type_of [a,b,c]
mengj@18356
   295
    end
mengj@18356
   296
  | comb_typ ("COMBC",t) =
mengj@18356
   297
    let val t1 = domain_type t
mengj@18356
   298
	val ([a,b],c) = strip_type t1
mengj@18356
   299
    in
mengj@18356
   300
	map simp_type_of [a,b,c]
mengj@18356
   301
    end;
mengj@18356
   302
mengj@18356
   303
fun const_type_of ("COMBI",t) = 
mengj@18356
   304
    let val (tp,ts) = type_of t
mengj@18356
   305
	val I_var = comb_typ ("COMBI",t)
mengj@18356
   306
    in
mengj@18356
   307
	(tp,ts,I_var)
mengj@18356
   308
    end
mengj@18356
   309
  | const_type_of ("COMBK",t) =
mengj@18356
   310
    let val (tp,ts) = type_of t
mengj@18356
   311
	val K_var = comb_typ ("COMBK",t)
mengj@18356
   312
    in
mengj@18356
   313
	(tp,ts,K_var)
mengj@18356
   314
    end
mengj@18356
   315
  | const_type_of ("COMBS",t) =
mengj@18356
   316
    let val (tp,ts) = type_of t
mengj@18356
   317
	val S_var = comb_typ ("COMBS",t)
mengj@18356
   318
    in
mengj@18356
   319
	(tp,ts,S_var)
mengj@18356
   320
    end
mengj@18356
   321
  | const_type_of ("COMBB",t) =
mengj@18356
   322
    let val (tp,ts) = type_of t
mengj@18356
   323
	val B_var = comb_typ ("COMBB",t)
mengj@18356
   324
    in
mengj@18356
   325
	(tp,ts,B_var)
mengj@18356
   326
    end
mengj@18356
   327
  | const_type_of ("COMBC",t) =
mengj@18356
   328
    let val (tp,ts) = type_of t
mengj@18356
   329
	val C_var = comb_typ ("COMBC",t)
mengj@18356
   330
    in
mengj@18356
   331
	(tp,ts,C_var)
mengj@18356
   332
    end
mengj@18356
   333
  | const_type_of (c,t) =
mengj@18356
   334
    let val (tp,ts) = type_of t
mengj@18356
   335
	val tvars = !const_typargs(c,t)
mengj@18356
   336
	val tvars' = map simp_type_of tvars
mengj@18356
   337
    in
mengj@18356
   338
	(tp,ts,tvars')
mengj@18356
   339
    end;
mengj@18356
   340
mengj@18356
   341
fun is_bool_type (Type("bool",[])) = true
mengj@18356
   342
  | is_bool_type _ = false;
mengj@17998
   343
mengj@17998
   344
mengj@17998
   345
(* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
mengj@17998
   346
fun combterm_of (Const(c,t)) =
mengj@18356
   347
    let val (tp,ts,tvar_list) = const_type_of (c,t)
mengj@18356
   348
	val is_bool = is_bool_type t
mengj@18356
   349
	val c' = CombConst(ResClause.make_fixed_const c,tp,tvar_list)
mengj@17998
   350
	val c'' = if is_bool then Bool(c') else c'
mengj@17998
   351
    in
mengj@17998
   352
	(c'',ts)
mengj@17998
   353
    end
mengj@17998
   354
  | combterm_of (Free(v,t)) =
mengj@18356
   355
    let val (tp,ts) = type_of t
mengj@18356
   356
	val is_bool = is_bool_type t
mengj@17998
   357
	val v' = if ResClause.isMeta v then CombVar(ResClause.make_schematic_var(v,0),tp)
mengj@17998
   358
		 else CombFree(ResClause.make_fixed_var v,tp)
mengj@17998
   359
	val v'' = if is_bool then Bool(v') else v'
mengj@17998
   360
    in
mengj@17998
   361
	(v'',ts)
mengj@17998
   362
    end
mengj@17998
   363
  | combterm_of (Var(v,t)) =
mengj@18356
   364
    let val (tp,ts) = type_of t
mengj@18356
   365
	val is_bool = is_bool_type t
mengj@17998
   366
	val v' = CombVar(ResClause.make_schematic_var v,tp)
mengj@17998
   367
	val v'' = if is_bool then Bool(v') else v'
mengj@17998
   368
    in
mengj@17998
   369
	(v'',ts)
mengj@17998
   370
    end
mengj@17998
   371
  | combterm_of (Const("op =",T) $ P $ Q) = (*FIXME: allow equal between bools?*)
mengj@17998
   372
    let val (P',tsP) = combterm_of P        
mengj@17998
   373
	val (Q',tsQ) = combterm_of Q
mengj@17998
   374
    in
mengj@17998
   375
	(Equal(P',Q'),tsP union tsQ)
mengj@17998
   376
    end
mengj@17998
   377
  | combterm_of (t as (P $ Q)) =
mengj@17998
   378
    let val (P',tsP) = combterm_of P
mengj@17998
   379
	val (Q',tsQ) = combterm_of Q
mengj@17998
   380
	val tp = Term.type_of t
mengj@18356
   381
	val tp' = simp_type_of tp
mengj@18356
   382
	val is_bool = is_bool_type tp
mengj@17998
   383
	val t' = CombApp(P',Q',tp')
mengj@17998
   384
	val t'' = if is_bool then Bool(t') else t'
mengj@17998
   385
    in
mengj@17998
   386
	(t'',tsP union tsQ)
mengj@17998
   387
    end;
mengj@17998
   388
mengj@17998
   389
fun predicate_of ((Const("Not",_) $ P), polarity) =
mengj@17998
   390
    predicate_of (P, not polarity)
mengj@17998
   391
  | predicate_of (term,polarity) = (combterm_of term,polarity);
mengj@17998
   392
mengj@17998
   393
mengj@17998
   394
fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
mengj@17998
   395
  | literals_of_term1 args (Const("op |",_) $ P $ Q) = 
mengj@17998
   396
    let val args' = literals_of_term1 args P
mengj@17998
   397
    in
mengj@17998
   398
	literals_of_term1 args' Q
mengj@17998
   399
    end
mengj@17998
   400
  | literals_of_term1 (lits,ts) P =
mengj@17998
   401
    let val ((pred,ts'),pol) = predicate_of (P,true)
mengj@17998
   402
	val lits' = Literal(pol,pred)::lits
mengj@17998
   403
    in
mengj@17998
   404
	(lits',ts union ts')
mengj@17998
   405
    end;
mengj@17998
   406
mengj@17998
   407
mengj@17998
   408
fun literals_of_term P = literals_of_term1 ([],[]) P;
mengj@17998
   409
mengj@17998
   410
mengj@17998
   411
(* making axiom and conjecture clauses *)
mengj@17998
   412
fun make_axiom_clause term (ax_name,cls_id) =
mengj@17998
   413
    let val term' = comb_of term
mengj@17998
   414
	val (lits,ctypes_sorts) = literals_of_term term'
mengj@17998
   415
	val (ctvar_lits,ctfree_lits) = ResClause.add_typs_aux2 ctypes_sorts
mengj@17998
   416
    in
mengj@17998
   417
	make_clause(cls_id,ax_name,Axiom,
mengj@17998
   418
		    lits,ctypes_sorts,ctvar_lits,ctfree_lits)
mengj@17998
   419
    end;
mengj@17998
   420
mengj@17998
   421
mengj@17998
   422
fun make_conjecture_clause n t =
mengj@17998
   423
    let val t' = comb_of t
mengj@17998
   424
	val (lits,ctypes_sorts) = literals_of_term t'
mengj@17998
   425
	val (ctvar_lits,ctfree_lits) = ResClause.add_typs_aux2 ctypes_sorts
mengj@17998
   426
    in
mengj@17998
   427
	make_clause(n,"conjecture",Conjecture,lits,ctypes_sorts,ctvar_lits,ctfree_lits)
mengj@17998
   428
    end;
mengj@17998
   429
mengj@17998
   430
mengj@17998
   431
mengj@17998
   432
fun make_conjecture_clauses_aux _ [] = []
mengj@17998
   433
  | make_conjecture_clauses_aux n (t::ts) =
mengj@17998
   434
    make_conjecture_clause n t :: make_conjecture_clauses_aux (n+1) ts;
mengj@17998
   435
mengj@17998
   436
val make_conjecture_clauses = make_conjecture_clauses_aux 0;
mengj@17998
   437
mengj@17998
   438
mengj@17998
   439
(**********************************************************************)
mengj@17998
   440
(* convert clause into ATP specific formats:                          *)
mengj@17998
   441
(* TPTP used by Vampire and E                                         *)
mengj@17998
   442
(**********************************************************************)
mengj@17998
   443
mengj@17998
   444
val type_wrapper = "typeinfo";
mengj@17998
   445
mengj@18356
   446
datatype type_level = T_FULL | T_PARTIAL | T_CONST | T_NONE;
mengj@18356
   447
mengj@18356
   448
val typ_level = ref T_PARTIAL;
mengj@17998
   449
mengj@18356
   450
fun full_types () = (typ_level:=T_FULL);
mengj@18356
   451
fun partial_types () = (typ_level:=T_PARTIAL);
mengj@18356
   452
fun const_types_only () = (typ_level:=T_CONST);
mengj@18356
   453
fun no_types () = (typ_level:=T_NONE);
mengj@18356
   454
mengj@18356
   455
mengj@18356
   456
fun find_typ_level () = !typ_level;
mengj@18356
   457
mengj@18356
   458
fun wrap_type (c,t) = 
mengj@18356
   459
    case !typ_level of T_FULL => type_wrapper ^ (ResClause.paren_pack [c,t])
mengj@18356
   460
		     | _ => c;
mengj@18356
   461
    
mengj@17998
   462
mengj@17998
   463
val bool_tp = ResClause.make_fixed_type_const "bool";
mengj@17998
   464
mengj@17998
   465
val app_str = "hAPP";
mengj@17998
   466
mengj@17998
   467
val bool_str = "hBOOL";
mengj@17998
   468
mengj@18356
   469
exception STRING_OF_COMBTERM of int;
mengj@17998
   470
mengj@17998
   471
(* convert literals of clauses into strings *)
mengj@18356
   472
fun string_of_combterm1_aux _ (CombConst(c,tp,_)) = (wrap_type (c,tp),tp)
mengj@18356
   473
  | string_of_combterm1_aux _ (CombFree(v,tp)) = (wrap_type (v,tp),tp)
mengj@18356
   474
  | string_of_combterm1_aux _ (CombVar(v,tp)) = (wrap_type (v,tp),tp)
mengj@18356
   475
  | string_of_combterm1_aux is_pred (CombApp(t1,t2,tp)) =
mengj@18356
   476
    let val (s1,tp1) = string_of_combterm1_aux is_pred t1
mengj@18356
   477
	val (s2,tp2) = string_of_combterm1_aux is_pred t2
mengj@18356
   478
	val r =	case !typ_level of T_FULL => type_wrapper ^  (ResClause.paren_pack [(app_str ^ (ResClause.paren_pack [s1,s2])),tp])
mengj@18356
   479
				 | T_PARTIAL => app_str ^ (ResClause.paren_pack [s1,s2,tp1])
mengj@18356
   480
				 | T_NONE => app_str ^ (ResClause.paren_pack [s1,s2])
mengj@18356
   481
				 | T_CONST => raise STRING_OF_COMBTERM (1) (*should not happen, if happened may be a bug*)
mengj@18356
   482
    in
mengj@18356
   483
	(r,tp)
mengj@18356
   484
mengj@18356
   485
    end
mengj@18356
   486
  | string_of_combterm1_aux is_pred (Bool(t)) = 
mengj@18356
   487
    let val (t',_) = string_of_combterm1_aux false t
mengj@18356
   488
	val r = if is_pred then bool_str ^ (ResClause.paren_pack [t'])
mengj@18356
   489
		else t'
mengj@17998
   490
    in
mengj@18356
   491
	(r,bool_tp)
mengj@17998
   492
    end
mengj@18356
   493
  | string_of_combterm1_aux _ (Equal(t1,t2)) =
mengj@18356
   494
    let val (s1,_) = string_of_combterm1_aux false t1
mengj@18356
   495
	val (s2,_) = string_of_combterm1_aux false t2
mengj@18356
   496
    in
mengj@18356
   497
	("equal" ^ (ResClause.paren_pack [s1,s2]),bool_tp) 
mengj@18356
   498
    end;
mengj@18356
   499
mengj@18356
   500
fun string_of_combterm1 is_pred term = fst (string_of_combterm1_aux is_pred term);
mengj@18356
   501
mengj@18356
   502
fun string_of_combterm2 _ (CombConst(c,tp,tvars)) = c ^ (ResClause.paren_pack tvars)
mengj@18356
   503
  | string_of_combterm2 _ (CombFree(v,tp)) = v
mengj@18356
   504
  | string_of_combterm2 _ (CombVar(v,tp)) = v
mengj@18356
   505
  | string_of_combterm2 is_pred (CombApp(t1,t2,tp)) =
mengj@18356
   506
    let val s1 = string_of_combterm2 is_pred t1
mengj@18356
   507
	val s2 = string_of_combterm2 is_pred t2
mengj@18356
   508
    in
mengj@18356
   509
	app_str ^ (ResClause.paren_pack [s1,s2])
mengj@18356
   510
    end
mengj@18356
   511
  | string_of_combterm2 is_pred (Bool(t)) = 
mengj@18356
   512
    let val t' = string_of_combterm2 false t
mengj@17998
   513
    in
mengj@18200
   514
	if is_pred then bool_str ^ (ResClause.paren_pack [t'])
mengj@18200
   515
	else t'
mengj@17998
   516
    end
mengj@18356
   517
  | string_of_combterm2 _ (Equal(t1,t2)) =
mengj@18356
   518
    let val s1 = string_of_combterm2 false t1
mengj@18356
   519
	val s2 = string_of_combterm2 false t2
mengj@17998
   520
    in
mengj@18356
   521
	("equal" ^ (ResClause.paren_pack [s1,s2])) 
mengj@17998
   522
    end;
mengj@17998
   523
mengj@18356
   524
mengj@18356
   525
mengj@18356
   526
fun string_of_combterm is_pred term = 
mengj@18356
   527
    case !typ_level of T_CONST => string_of_combterm2 is_pred term
mengj@18356
   528
		     | _ => string_of_combterm1 is_pred term;
mengj@18356
   529
mengj@18356
   530
mengj@17998
   531
fun string_of_clausename (cls_id,ax_name) = 
mengj@17998
   532
    ResClause.clause_prefix ^ ResClause.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
mengj@17998
   533
mengj@17998
   534
fun string_of_type_clsname (cls_id,ax_name,idx) = 
mengj@17998
   535
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
mengj@17998
   536
mengj@17998
   537
mengj@17998
   538
fun tptp_literal (Literal(pol,pred)) =
mengj@18200
   539
    let val pred_string = string_of_combterm true pred
mengj@17998
   540
	val pol_str = if pol then "++" else "--"
mengj@17998
   541
    in
mengj@17998
   542
	pol_str ^ pred_string
mengj@17998
   543
    end;
mengj@17998
   544
mengj@17998
   545
 
mengj@17998
   546
fun tptp_type_lits (Clause cls) = 
mengj@17998
   547
    let val lits = map tptp_literal (#literals cls)
mengj@17998
   548
	val ctvar_lits_strs =
mengj@18356
   549
	    case !typ_level of T_NONE => []
mengj@18356
   550
			     | _ => (map ResClause.tptp_of_typeLit (#ctvar_type_literals cls)) 
mengj@17998
   551
	val ctfree_lits = 
mengj@18356
   552
	    case !typ_level of T_NONE => []
mengj@18356
   553
			     | _ => (map ResClause.tptp_of_typeLit (#ctfree_type_literals cls)) 
mengj@17998
   554
    in
mengj@17998
   555
	(ctvar_lits_strs @ lits, ctfree_lits)
mengj@17998
   556
    end; 
mengj@18356
   557
    
mengj@18356
   558
    
mengj@17998
   559
fun clause2tptp cls =
mengj@17998
   560
    let val (lits,ctfree_lits) = tptp_type_lits cls
mengj@17998
   561
	val cls_id = get_clause_id cls
mengj@17998
   562
	val ax_name = get_axiomName cls
mengj@17998
   563
	val knd = string_of_kind cls
mengj@17998
   564
	val lits_str = ResClause.bracket_pack lits
mengj@17998
   565
	val cls_str = ResClause.gen_tptp_cls(cls_id,ax_name,knd,lits_str)
mengj@17998
   566
    in
mengj@17998
   567
	(cls_str,ctfree_lits)
mengj@17998
   568
    end;
mengj@17998
   569
mengj@17998
   570
mengj@17998
   571
end