src/HOL/Tools/res_hol_clause.ML
author paulson
Mon Nov 27 18:18:25 2006 +0100 (2006-11-27)
changeset 21561 cfd2258f0b23
parent 21513 9e9fff87dc6c
child 21573 8a7a68c0096c
permissions -rw-r--r--
Tidied code. Bool constructor is not needed.
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
structure ResHolClause =
mengj@17998
     8
mengj@17998
     9
struct
mengj@17998
    10
mengj@20791
    11
(* theorems for combinators and function extensionality *)
mengj@20791
    12
val ext = thm "HOL.ext";
wenzelm@21254
    13
val comb_I = thm "ATP_Linkup.COMBI_def";
wenzelm@21254
    14
val comb_K = thm "ATP_Linkup.COMBK_def";
wenzelm@21254
    15
val comb_B = thm "ATP_Linkup.COMBB_def";
wenzelm@21254
    16
val comb_C = thm "ATP_Linkup.COMBC_def";
wenzelm@21254
    17
val comb_S = thm "ATP_Linkup.COMBS_def";
wenzelm@21254
    18
val comb_B' = thm "ATP_Linkup.COMBB'_def";
wenzelm@21254
    19
val comb_C' = thm "ATP_Linkup.COMBC'_def";
wenzelm@21254
    20
val comb_S' = thm "ATP_Linkup.COMBS'_def";
wenzelm@21254
    21
val fequal_imp_equal = thm "ATP_Linkup.fequal_imp_equal";
wenzelm@21254
    22
val equal_imp_fequal = thm "ATP_Linkup.equal_imp_fequal";
mengj@20791
    23
paulson@21108
    24
(*A flag to set if we use the Turner optimizations. Currently FALSE, as the 5 standard
paulson@20997
    25
  combinators appear to work best.*)
paulson@21108
    26
val use_Turner = ref false;
mengj@17998
    27
paulson@21108
    28
(*FIXME: these refs should probaby replaced by code to count the combinators in the 
paulson@21108
    29
  translated form of the term.*)
mengj@20644
    30
val combI_count = ref 0;
mengj@20644
    31
val combK_count = ref 0;
mengj@20644
    32
val combB_count = ref 0;
mengj@20644
    33
val combC_count = ref 0;
mengj@20644
    34
val combS_count = ref 0;
mengj@20644
    35
mengj@20644
    36
val combB'_count = ref 0;
mengj@20644
    37
val combC'_count = ref 0;
mengj@20644
    38
val combS'_count = ref 0; 
mengj@20644
    39
mengj@17998
    40
mengj@20644
    41
fun increI count_comb =  if count_comb then combI_count := !combI_count + 1 else ();
mengj@20644
    42
fun increK count_comb =  if count_comb then combK_count := !combK_count + 1 else ();
mengj@20644
    43
fun increB count_comb =  if count_comb then combB_count := !combB_count + 1 else ();
mengj@20644
    44
fun increC count_comb =  if count_comb then combC_count := !combC_count + 1 else ();
mengj@20644
    45
fun increS count_comb =  if count_comb then combS_count := !combS_count + 1 else (); 
mengj@20644
    46
fun increB' count_comb =  if count_comb then combB'_count := !combB'_count + 1 else (); 
mengj@20644
    47
fun increC' count_comb =  if count_comb then combC'_count := !combC'_count + 1 else ();
mengj@20644
    48
fun increS' count_comb =  if count_comb then combS'_count := !combS'_count + 1 else (); 
mengj@20644
    49
mengj@20644
    50
mengj@20644
    51
exception DECRE_COMB of string;
paulson@20953
    52
fun decreB count_comb n = 
paulson@20953
    53
  if count_comb then 
paulson@20953
    54
    if !combB_count >= n then combB_count := !combB_count - n else raise DECRE_COMB "COMBB"
paulson@20953
    55
  else ();
mengj@20644
    56
paulson@20953
    57
fun decreC count_comb n =
paulson@20953
    58
  if count_comb then 
paulson@20953
    59
    if !combC_count >= n then combC_count := !combC_count - n else raise DECRE_COMB "COMBC"
paulson@20953
    60
  else ();
mengj@20644
    61
paulson@20953
    62
fun decreS count_comb n = 
paulson@20953
    63
  if count_comb then 
paulson@20953
    64
    if !combS_count >= n then combS_count := !combS_count - n else raise DECRE_COMB "COMBS"
paulson@20953
    65
  else ();
mengj@20130
    66
mengj@18356
    67
val const_typargs = ref (Library.K [] : (string*typ -> typ list));
mengj@18356
    68
mengj@20644
    69
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
    70
                const_typargs := Sign.const_typargs thy);
mengj@19198
    71
mengj@17998
    72
(**********************************************************************)
mengj@17998
    73
(* convert a Term.term with lambdas into a Term.term with combinators *) 
mengj@17998
    74
(**********************************************************************)
mengj@17998
    75
mengj@17998
    76
fun is_free (Bound(a)) n = (a = n)
mengj@17998
    77
  | is_free (Abs(x,_,b)) n = (is_free b (n+1))
mengj@17998
    78
  | is_free (P $ Q) n = ((is_free P n) orelse (is_free Q n))
mengj@17998
    79
  | is_free _ _ = false;
mengj@17998
    80
mengj@17998
    81
mengj@17998
    82
(*******************************************)
wenzelm@21254
    83
fun mk_compact_comb (tm as (Const("ATP_Linkup.COMBB",_)$p) $ (Const("ATP_Linkup.COMBB",_)$q$r)) Bnds count_comb =
mengj@20644
    84
    let val tp_p = Term.type_of1(Bnds,p)
mengj@20644
    85
	val tp_q = Term.type_of1(Bnds,q)
mengj@20644
    86
	val tp_r = Term.type_of1(Bnds,r)
mengj@20644
    87
	val typ = Term.type_of1(Bnds,tm)
mengj@20644
    88
	val typ_B' = [tp_p,tp_q,tp_r] ---> typ
mengj@20644
    89
	val _ = increB' count_comb
mengj@20644
    90
	val _ = decreB count_comb 2
mengj@20644
    91
    in
wenzelm@21254
    92
	Const("ATP_Linkup.COMBB'",typ_B') $ p $ q $ r
mengj@20644
    93
    end
wenzelm@21254
    94
  | mk_compact_comb (tm as (Const("ATP_Linkup.COMBC",_) $ (Const("ATP_Linkup.COMBB",_)$p$q) $ r)) Bnds count_comb =
mengj@20644
    95
    let val tp_p = Term.type_of1(Bnds,p)
mengj@20644
    96
	val tp_q = Term.type_of1(Bnds,q)
mengj@20644
    97
	val tp_r = Term.type_of1(Bnds,r)
mengj@20644
    98
	val typ = Term.type_of1(Bnds,tm)
mengj@20644
    99
	val typ_C' = [tp_p,tp_q,tp_r] ---> typ
mengj@20644
   100
	val _ = increC' count_comb
mengj@20644
   101
	val _ = decreC count_comb 1
mengj@20644
   102
	val _ = decreB count_comb 1
mengj@20644
   103
    in
wenzelm@21254
   104
	Const("ATP_Linkup.COMBC'",typ_C') $ p $ q $ r
mengj@20644
   105
    end
wenzelm@21254
   106
  | mk_compact_comb (tm as (Const("ATP_Linkup.COMBS",_) $ (Const("ATP_Linkup.COMBB",_)$p$q) $ r)) Bnds count_comb =
mengj@20644
   107
    let val tp_p = Term.type_of1(Bnds,p)
mengj@20644
   108
	val tp_q = Term.type_of1(Bnds,q)
mengj@20644
   109
	val tp_r = Term.type_of1(Bnds,r)
mengj@20644
   110
	val typ = Term.type_of1(Bnds,tm)
mengj@20644
   111
	val typ_S' = [tp_p,tp_q,tp_r] ---> typ
mengj@20644
   112
	val _ = increS' count_comb
mengj@20644
   113
	val _ = decreS count_comb 1
mengj@20644
   114
	val _ = decreB count_comb 1
mengj@20644
   115
    in
wenzelm@21254
   116
	Const("ATP_Linkup.COMBS'",typ_S') $ p $ q $ r
mengj@20644
   117
    end
mengj@20644
   118
  | mk_compact_comb tm _ _ = tm;
mengj@20644
   119
paulson@21108
   120
fun compact_comb t Bnds count_comb = 
paulson@21108
   121
  if !use_Turner then mk_compact_comb t Bnds count_comb else t;
mengj@20644
   122
mengj@20130
   123
fun lam2comb (Abs(x,tp,Bound 0)) _ count_comb = 
paulson@21135
   124
      let val _ = increI count_comb
paulson@21108
   125
      in 
wenzelm@21254
   126
	  Const("ATP_Linkup.COMBI",tp-->tp) 
paulson@21108
   127
      end
mengj@20130
   128
  | lam2comb (Abs(x,tp,Bound n)) Bnds count_comb = 
paulson@21135
   129
      let val tb = List.nth(Bnds,n-1)
paulson@21108
   130
	  val _ = increK count_comb 
paulson@21108
   131
      in
wenzelm@21254
   132
	  Const("ATP_Linkup.COMBK", [tb,tp] ---> tb) $ (Bound (n-1))
paulson@21108
   133
      end
mengj@20130
   134
  | lam2comb (Abs(x,t1,Const(c,t2))) _ count_comb = 
paulson@21135
   135
      let val _ = increK count_comb 
paulson@21108
   136
      in 
wenzelm@21254
   137
	  Const("ATP_Linkup.COMBK",[t2,t1] ---> t2) $ Const(c,t2) 
paulson@21108
   138
      end
mengj@20130
   139
  | lam2comb (Abs(x,t1,Free(v,t2))) _ count_comb =
paulson@21135
   140
      let val _ = increK count_comb
paulson@21108
   141
      in
wenzelm@21254
   142
	  Const("ATP_Linkup.COMBK",[t2,t1] ---> t2) $ Free(v,t2)
paulson@21108
   143
      end
mengj@20130
   144
  | lam2comb (Abs(x,t1,Var(ind,t2))) _ count_comb =
paulson@21135
   145
      let val _ = increK count_comb 
paulson@21108
   146
      in
wenzelm@21254
   147
	  Const("ATP_Linkup.COMBK", [t2,t1] ---> t2) $ Var(ind,t2)
paulson@21108
   148
      end
mengj@20130
   149
  | lam2comb (t as (Abs(x,t1,P$(Bound 0)))) Bnds count_comb =
paulson@21135
   150
      let val tr = Term.type_of1(t1::Bnds,P)
paulson@21108
   151
      in
paulson@21135
   152
	  if not(is_free P 0) then (incr_boundvars ~1 P)
paulson@21108
   153
	  else 
paulson@21135
   154
	  let val tI = [t1] ---> t1
paulson@21108
   155
	      val P' = lam2comb (Abs(x,t1,P)) Bnds count_comb
paulson@21108
   156
	      val tp' = Term.type_of1(Bnds,P')
paulson@21135
   157
	      val tS = [tp',tI] ---> tr
paulson@21108
   158
	      val _ = increI count_comb
paulson@21108
   159
	      val _ = increS count_comb
paulson@21108
   160
	  in
wenzelm@21254
   161
	      compact_comb (Const("ATP_Linkup.COMBS",tS) $ P' $ 
wenzelm@21254
   162
	                     Const("ATP_Linkup.COMBI",tI)) Bnds count_comb
paulson@21108
   163
	  end
paulson@21135
   164
      end	    
mengj@20130
   165
  | lam2comb (t as (Abs(x,t1,P$Q))) Bnds count_comb =
paulson@21108
   166
      let val nfreeP = not(is_free P 0)
paulson@21108
   167
	  and nfreeQ = not(is_free Q 0)
paulson@21108
   168
	  val tpq = Term.type_of1(t1::Bnds, P$Q) 
paulson@21108
   169
      in
paulson@21108
   170
	  if nfreeP andalso nfreeQ 
paulson@21108
   171
	  then 
paulson@21135
   172
	    let val tK = [tpq,t1] ---> tpq
paulson@21135
   173
		val PQ' = incr_boundvars ~1(P $ Q)
mengj@20644
   174
		val _ = increK count_comb
mengj@17998
   175
	    in 
wenzelm@21254
   176
		Const("ATP_Linkup.COMBK",tK) $ PQ'
paulson@21108
   177
	    end
paulson@21108
   178
	  else if nfreeP then 
paulson@21108
   179
	    let val Q' = lam2comb (Abs(x,t1,Q)) Bnds count_comb
paulson@21135
   180
		val P' = incr_boundvars ~1 P
paulson@21108
   181
		val tp = Term.type_of1(Bnds,P')
paulson@21108
   182
		val tq' = Term.type_of1(Bnds, Q')
paulson@21135
   183
		val tB = [tp,tq',t1] ---> tpq
paulson@21108
   184
		val _ = increB count_comb
paulson@21108
   185
	    in
wenzelm@21254
   186
		compact_comb (Const("ATP_Linkup.COMBB",tB) $ P' $ Q') Bnds count_comb 
paulson@21108
   187
	    end
paulson@21108
   188
	  else if nfreeQ then 
paulson@21108
   189
	    let val P' = lam2comb (Abs(x,t1,P)) Bnds count_comb
paulson@21135
   190
		val Q' = incr_boundvars ~1 Q
paulson@21108
   191
		val tq = Term.type_of1(Bnds,Q')
paulson@21108
   192
		val tp' = Term.type_of1(Bnds, P')
paulson@21135
   193
		val tC = [tp',tq,t1] ---> tpq
paulson@21108
   194
		val _ = increC count_comb
paulson@21108
   195
	    in
wenzelm@21254
   196
		compact_comb (Const("ATP_Linkup.COMBC",tC) $ P' $ Q') Bnds count_comb
paulson@21108
   197
	    end
paulson@21108
   198
          else
paulson@21108
   199
	    let val P' = lam2comb (Abs(x,t1,P)) Bnds count_comb
paulson@21108
   200
		val Q' = lam2comb (Abs(x,t1,Q)) Bnds count_comb 
paulson@21108
   201
		val tp' = Term.type_of1(Bnds,P')
paulson@21108
   202
		val tq' = Term.type_of1(Bnds,Q')
paulson@21135
   203
		val tS = [tp',tq',t1] ---> tpq
paulson@21108
   204
		val _ = increS count_comb
paulson@21108
   205
	    in
wenzelm@21254
   206
		compact_comb (Const("ATP_Linkup.COMBS",tS) $ P' $ Q') Bnds count_comb
paulson@21108
   207
	    end
paulson@21108
   208
      end
mengj@20791
   209
  | lam2comb (t as (Abs(x,t1,_))) _ _ = raise ResClause.CLAUSE("HOL CLAUSE",t);
mengj@17998
   210
mengj@17998
   211
(*********************)
mengj@17998
   212
mengj@20130
   213
fun to_comb (Abs(x,tp,b)) Bnds count_comb =
mengj@20130
   214
    let val b' = to_comb b (tp::Bnds) count_comb
mengj@20130
   215
    in lam2comb (Abs(x,tp,b')) Bnds count_comb end
mengj@20130
   216
  | to_comb (P $ Q) Bnds count_comb = ((to_comb P Bnds count_comb) $ (to_comb Q Bnds count_comb))
mengj@20130
   217
  | to_comb t _ _ = t;
mengj@17998
   218
 
mengj@20644
   219
   
mengj@20130
   220
fun comb_of t count_comb = to_comb t [] count_comb;
mengj@20644
   221
 
mengj@17998
   222
(* print a term containing combinators, used for debugging *)
mengj@17998
   223
exception TERM_COMB of term;
mengj@17998
   224
mengj@17998
   225
fun string_of_term (Const(c,t)) = c
mengj@17998
   226
  | string_of_term (Free(v,t)) = v
paulson@20360
   227
  | string_of_term (Var((x,n),t)) = x ^ "_" ^ (string_of_int n)
mengj@17998
   228
  | string_of_term (P $ Q) =
paulson@20360
   229
      "(" ^ string_of_term P ^ " " ^ string_of_term Q ^ ")" 
mengj@17998
   230
  | string_of_term t =  raise TERM_COMB (t);
mengj@17998
   231
mengj@17998
   232
mengj@17998
   233
mengj@17998
   234
(******************************************************)
mengj@17998
   235
(* data types for typed combinator expressions        *)
mengj@17998
   236
(******************************************************)
mengj@17998
   237
mengj@20281
   238
datatype type_level = T_FULL | T_PARTIAL | T_CONST | T_NONE;
mengj@20281
   239
mengj@20281
   240
val typ_level = ref T_CONST;
mengj@20281
   241
mengj@20281
   242
fun full_types () = (typ_level:=T_FULL);
mengj@20281
   243
fun partial_types () = (typ_level:=T_PARTIAL);
mengj@20281
   244
fun const_types_only () = (typ_level:=T_CONST);
mengj@20281
   245
fun no_types () = (typ_level:=T_NONE);
mengj@20281
   246
mengj@20281
   247
fun find_typ_level () = !typ_level;
mengj@20281
   248
mengj@17998
   249
type axiom_name = string;
mengj@17998
   250
type polarity = bool;
mengj@17998
   251
type clause_id = int;
mengj@17998
   252
paulson@21513
   253
datatype combterm = CombConst of string * ResClause.fol_type * ResClause.fol_type list
paulson@21513
   254
		  | CombFree of string * ResClause.fol_type
paulson@21513
   255
		  | CombVar of string * ResClause.fol_type
paulson@21513
   256
		  | CombApp of combterm * combterm * ResClause.fol_type
paulson@20360
   257
		  
mengj@17998
   258
datatype literal = Literal of polarity * combterm;
mengj@17998
   259
mengj@17998
   260
datatype clause = 
mengj@17998
   261
	 Clause of {clause_id: clause_id,
mengj@17998
   262
		    axiom_name: axiom_name,
paulson@19964
   263
		    th: thm,
paulson@21509
   264
		    kind: ResClause.kind,
mengj@17998
   265
		    literals: literal list,
paulson@21561
   266
		    ctypes_sorts: (ResClause.typ_var * Term.sort) list, 
paulson@21561
   267
                    ctvar_type_literals: ResClause.type_literal list, 
paulson@21561
   268
                    ctfree_type_literals: ResClause.type_literal list};
mengj@17998
   269
mengj@17998
   270
mengj@17998
   271
(*********************************************************************)
mengj@17998
   272
(* convert a clause with type Term.term to a clause with type clause *)
mengj@17998
   273
(*********************************************************************)
mengj@17998
   274
paulson@21561
   275
fun isFalse (Literal(pol, CombConst(c,_,_))) =
paulson@20360
   276
      (pol andalso c = "c_False") orelse
paulson@20360
   277
      (not pol andalso c = "c_True")
mengj@17998
   278
  | isFalse _ = false;
mengj@17998
   279
paulson@21561
   280
fun isTrue (Literal (pol, CombConst(c,_,_))) =
mengj@17998
   281
      (pol andalso c = "c_True") orelse
mengj@17998
   282
      (not pol andalso c = "c_False")
mengj@17998
   283
  | isTrue _ = false;
mengj@17998
   284
  
mengj@17998
   285
fun isTaut (Clause {literals,...}) = exists isTrue literals;  
mengj@17998
   286
mengj@18440
   287
fun type_of (Type (a, Ts)) =
paulson@21561
   288
      let val (folTypes,ts) = types_of Ts
paulson@21561
   289
	  val t = ResClause.make_fixed_type_const a
paulson@21561
   290
      in
paulson@21561
   291
	  (ResClause.mk_fol_type("Comp",t,folTypes),ts)
paulson@21561
   292
      end
mengj@18440
   293
  | type_of (tp as (TFree(a,s))) =
paulson@21561
   294
      let val t = ResClause.make_fixed_type_var a
paulson@21561
   295
      in
paulson@21561
   296
	  (ResClause.mk_fol_type("Fixed",t,[]),[ResClause.mk_typ_var_sort tp])
paulson@21561
   297
      end
mengj@18440
   298
  | type_of (tp as (TVar(v,s))) =
paulson@21561
   299
      let val t = ResClause.make_schematic_type_var v
paulson@21561
   300
      in
paulson@21561
   301
	  (ResClause.mk_fol_type("Var",t,[]),[ResClause.mk_typ_var_sort tp])
paulson@21561
   302
      end
mengj@18440
   303
and types_of Ts =
paulson@21561
   304
      let val foltyps_ts = map type_of Ts
paulson@21561
   305
	  val (folTyps,ts) = ListPair.unzip foltyps_ts
paulson@21561
   306
      in
paulson@21561
   307
	  (folTyps,ResClause.union_all ts)
paulson@21561
   308
      end;
mengj@17998
   309
mengj@17998
   310
(* same as above, but no gathering of sort information *)
mengj@18440
   311
fun simp_type_of (Type (a, Ts)) = 
paulson@21513
   312
      let val typs = map simp_type_of Ts
paulson@21513
   313
	  val t = ResClause.make_fixed_type_const a
paulson@21513
   314
      in
paulson@21513
   315
	  ResClause.mk_fol_type("Comp",t,typs)
paulson@21513
   316
      end
mengj@18440
   317
  | simp_type_of (TFree (a,s)) = ResClause.mk_fol_type("Fixed",ResClause.make_fixed_type_var a,[])
mengj@18440
   318
  | simp_type_of (TVar (v,s)) = ResClause.mk_fol_type("Var",ResClause.make_schematic_type_var v,[]);
mengj@18440
   319
mengj@18356
   320
mengj@20865
   321
fun const_type_of (c,t) =
mengj@18356
   322
    let val (tp,ts) = type_of t
mengj@18356
   323
	val tvars = !const_typargs(c,t)
mengj@18356
   324
    in
paulson@21561
   325
	(tp, ts, map simp_type_of tvars)
mengj@18356
   326
    end;
mengj@18356
   327
mengj@17998
   328
(* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
mengj@17998
   329
fun combterm_of (Const(c,t)) =
paulson@21513
   330
      let val (tp,ts,tvar_list) = const_type_of (c,t)
paulson@21513
   331
	  val c' = CombConst(ResClause.make_fixed_const c,tp,tvar_list)
paulson@21513
   332
      in
paulson@21561
   333
	  (c',ts)
paulson@21513
   334
      end
mengj@17998
   335
  | combterm_of (Free(v,t)) =
paulson@21513
   336
      let val (tp,ts) = type_of t
paulson@21513
   337
	  val v' = if ResClause.isMeta v then CombVar(ResClause.make_schematic_var(v,0),tp)
paulson@21513
   338
		   else CombFree(ResClause.make_fixed_var v,tp)
paulson@21513
   339
      in
paulson@21561
   340
	  (v',ts)
paulson@21513
   341
      end
mengj@17998
   342
  | combterm_of (Var(v,t)) =
paulson@21513
   343
      let val (tp,ts) = type_of t
paulson@21513
   344
	  val v' = CombVar(ResClause.make_schematic_var v,tp)
paulson@21513
   345
      in
paulson@21561
   346
	  (v',ts)
paulson@21513
   347
      end
mengj@17998
   348
  | combterm_of (t as (P $ Q)) =
paulson@21513
   349
      let val (P',tsP) = combterm_of P
paulson@21513
   350
	  val (Q',tsQ) = combterm_of Q
paulson@21513
   351
	  val tp = Term.type_of t
paulson@21513
   352
	  val t' = CombApp(P',Q', simp_type_of tp)
paulson@21513
   353
      in
paulson@21561
   354
	  (t',tsP union tsQ)
paulson@21513
   355
      end;
mengj@17998
   356
mengj@17998
   357
fun predicate_of ((Const("Not",_) $ P), polarity) =
mengj@17998
   358
    predicate_of (P, not polarity)
mengj@17998
   359
  | predicate_of (term,polarity) = (combterm_of term,polarity);
mengj@17998
   360
mengj@17998
   361
mengj@17998
   362
fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
mengj@17998
   363
  | literals_of_term1 args (Const("op |",_) $ P $ Q) = 
paulson@21509
   364
      literals_of_term1 (literals_of_term1 args P) Q
mengj@17998
   365
  | literals_of_term1 (lits,ts) P =
paulson@21509
   366
      let val ((pred,ts'),pol) = predicate_of (P,true)
paulson@21509
   367
      in
paulson@21509
   368
	  (Literal(pol,pred)::lits, ts union ts')
paulson@21509
   369
      end;
mengj@17998
   370
mengj@17998
   371
fun literals_of_term P = literals_of_term1 ([],[]) P;
mengj@17998
   372
mengj@17998
   373
(* making axiom and conjecture clauses *)
mengj@20274
   374
exception MAKE_CLAUSE
paulson@21509
   375
fun make_clause(clause_id,axiom_name,kind,th,is_user) =
paulson@21509
   376
    let val (lits,ctypes_sorts) = literals_of_term (comb_of (prop_of th) is_user)
paulson@18856
   377
	val (ctvar_lits,ctfree_lits) = ResClause.add_typs_aux ctypes_sorts
mengj@17998
   378
    in
mengj@20016
   379
	if forall isFalse lits
mengj@20016
   380
	then error "Problem too trivial for resolution (empty clause)"
mengj@20016
   381
	else
paulson@21509
   382
	    Clause {clause_id = clause_id, axiom_name = axiom_name, th = th, kind = kind,
mengj@20016
   383
		    literals = lits, ctypes_sorts = ctypes_sorts, 
mengj@20016
   384
		    ctvar_type_literals = ctvar_lits,
mengj@20016
   385
		    ctfree_type_literals = ctfree_lits}
mengj@17998
   386
    end;
mengj@17998
   387
mengj@20016
   388
mengj@20130
   389
fun make_axiom_clauses [] user_lemmas = []
paulson@21509
   390
  | make_axiom_clauses ((th,(name,id))::ths) user_lemmas =
mengj@20130
   391
    let val is_user = name mem user_lemmas
paulson@21509
   392
	val cls = SOME (make_clause(id, name, ResClause.Axiom, th, is_user)) 
paulson@20360
   393
	          handle MAKE_CLAUSE => NONE
paulson@21509
   394
	val clss = make_axiom_clauses ths user_lemmas
mengj@19354
   395
    in
mengj@20130
   396
	case cls of NONE => clss
paulson@20360
   397
		  | SOME(cls') => if isTaut cls' then clss 
paulson@20360
   398
		                  else (name,cls')::clss
mengj@19354
   399
    end;
mengj@19354
   400
mengj@19354
   401
mengj@17998
   402
fun make_conjecture_clauses_aux _ [] = []
paulson@20421
   403
  | make_conjecture_clauses_aux n (th::ths) =
paulson@21509
   404
      make_clause(n,"conjecture", ResClause.Conjecture, th, true) ::
paulson@20421
   405
      make_conjecture_clauses_aux (n+1) ths;
mengj@17998
   406
mengj@17998
   407
val make_conjecture_clauses = make_conjecture_clauses_aux 0;
mengj@17998
   408
mengj@17998
   409
mengj@17998
   410
(**********************************************************************)
mengj@17998
   411
(* convert clause into ATP specific formats:                          *)
mengj@17998
   412
(* TPTP used by Vampire and E                                         *)
mengj@19720
   413
(* DFG used by SPASS                                                  *)
mengj@17998
   414
(**********************************************************************)
mengj@17998
   415
mengj@17998
   416
val type_wrapper = "typeinfo";
mengj@17998
   417
paulson@21513
   418
fun wrap_type (c,tp) = case !typ_level of
paulson@21513
   419
	T_FULL => type_wrapper ^ ResClause.paren_pack [c, ResClause.string_of_fol_type tp]
paulson@21513
   420
      | _ => c;
mengj@18356
   421
    
mengj@17998
   422
mengj@17998
   423
val bool_tp = ResClause.make_fixed_type_const "bool";
mengj@17998
   424
mengj@17998
   425
val app_str = "hAPP";
mengj@17998
   426
mengj@17998
   427
val bool_str = "hBOOL";
mengj@17998
   428
paulson@21513
   429
fun type_of_combterm (CombConst(c,tp,_)) = tp
paulson@21513
   430
  | type_of_combterm (CombFree(v,tp)) = tp
paulson@21513
   431
  | type_of_combterm (CombVar(v,tp)) = tp
paulson@21561
   432
  | type_of_combterm (CombApp(t1,t2,tp)) = tp;
mengj@17998
   433
paulson@21513
   434
fun string_of_combterm1 (CombConst(c,tp,_)) = 
paulson@21513
   435
      let val c' = if c = "equal" then "c_fequal" else c
paulson@21513
   436
      in  wrap_type (c',tp)  end
paulson@21513
   437
  | string_of_combterm1 (CombFree(v,tp)) = wrap_type (v,tp)
paulson@21513
   438
  | string_of_combterm1 (CombVar(v,tp)) = wrap_type (v,tp)
paulson@21513
   439
  | string_of_combterm1 (CombApp(t1,t2,tp)) =
paulson@21513
   440
      let val s1 = string_of_combterm1 t1
paulson@21513
   441
	  val s2 = string_of_combterm1 t2
paulson@21513
   442
      in
paulson@21513
   443
	  case !typ_level of
paulson@21513
   444
	      T_FULL => type_wrapper ^ 
paulson@21513
   445
	                ResClause.paren_pack 
paulson@21513
   446
	                  [app_str ^ ResClause.paren_pack [s1,s2], 
paulson@21513
   447
	                   ResClause.string_of_fol_type tp]
paulson@21513
   448
	    | T_PARTIAL => app_str ^ ResClause.paren_pack 
paulson@21513
   449
	                     [s1,s2, ResClause.string_of_fol_type (type_of_combterm t1)]
paulson@21513
   450
	    | T_NONE => app_str ^ ResClause.paren_pack [s1,s2]
paulson@21513
   451
	    | T_CONST => raise ERROR "string_of_combterm1"
paulson@21561
   452
      end;
mengj@18356
   453
paulson@21513
   454
fun string_of_combterm2 (CombConst(c,tp,tvars)) = 
paulson@21513
   455
      let val tvars' = map ResClause.string_of_fol_type tvars
paulson@21513
   456
	  val c' = if c = "equal" then "c_fequal" else c
paulson@21513
   457
      in
paulson@21513
   458
	  c' ^ ResClause.paren_pack tvars'
paulson@21513
   459
      end
paulson@21513
   460
  | string_of_combterm2 (CombFree(v,tp)) = v
paulson@21513
   461
  | string_of_combterm2 (CombVar(v,tp)) = v
paulson@21513
   462
  | string_of_combterm2 (CombApp(t1,t2,_)) =
paulson@21561
   463
      app_str ^ ResClause.paren_pack [string_of_combterm2 t1, string_of_combterm2 t2];
mengj@18356
   464
paulson@21513
   465
fun string_of_combterm t = 
paulson@21513
   466
    case !typ_level of T_CONST => string_of_combterm2 t
paulson@21513
   467
		           | _ => string_of_combterm1 t;
paulson@21513
   468
		           
paulson@21561
   469
fun string_of_predicate (CombApp(CombApp(CombConst("equal",_,_),t1,_),t2,_)) =
paulson@21513
   470
      ("equal" ^ ResClause.paren_pack [string_of_combterm t1, string_of_combterm t2])
paulson@21561
   471
  | string_of_predicate t = 
paulson@21513
   472
      bool_str ^ ResClause.paren_pack [string_of_combterm t]
mengj@18356
   473
mengj@17998
   474
fun string_of_clausename (cls_id,ax_name) = 
mengj@17998
   475
    ResClause.clause_prefix ^ ResClause.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
mengj@17998
   476
mengj@17998
   477
fun string_of_type_clsname (cls_id,ax_name,idx) = 
mengj@17998
   478
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
mengj@17998
   479
mengj@17998
   480
paulson@21561
   481
(*** tptp format ***)
mengj@19720
   482
paulson@21513
   483
fun tptp_of_equality pol (t1,t2) =
paulson@21513
   484
  let val eqop = if pol then " = " else " != "
paulson@21513
   485
  in  string_of_combterm t1 ^ eqop ^ string_of_combterm t2  end;
paulson@21513
   486
paulson@21561
   487
fun tptp_literal (Literal(pol, CombApp(CombApp(CombConst("equal",_,_),t1,_),t2,_))) = 
paulson@21513
   488
      tptp_of_equality pol (t1,t2)
paulson@21513
   489
  | tptp_literal (Literal(pol,pred)) = 
paulson@21513
   490
      ResClause.tptp_sign pol (string_of_predicate pred);
mengj@17998
   491
 
mengj@17998
   492
fun tptp_type_lits (Clause cls) = 
mengj@17998
   493
    let val lits = map tptp_literal (#literals cls)
mengj@17998
   494
	val ctvar_lits_strs =
mengj@18356
   495
	    case !typ_level of T_NONE => []
paulson@20360
   496
	      | _ => map ResClause.tptp_of_typeLit (#ctvar_type_literals cls)
mengj@17998
   497
	val ctfree_lits = 
mengj@18356
   498
	    case !typ_level of T_NONE => []
paulson@20360
   499
	      | _ => map ResClause.tptp_of_typeLit (#ctfree_type_literals cls)
mengj@17998
   500
    in
mengj@17998
   501
	(ctvar_lits_strs @ lits, ctfree_lits)
mengj@17998
   502
    end; 
mengj@18356
   503
    
mengj@18356
   504
    
paulson@21509
   505
fun clause2tptp (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
mengj@17998
   506
    let val (lits,ctfree_lits) = tptp_type_lits cls
paulson@21509
   507
	val cls_str = ResClause.gen_tptp_cls(clause_id,axiom_name,kind,lits)
mengj@17998
   508
    in
mengj@17998
   509
	(cls_str,ctfree_lits)
mengj@17998
   510
    end;
mengj@17998
   511
mengj@17998
   512
paulson@21561
   513
(*** dfg format ***)
paulson@21561
   514
paulson@21513
   515
fun dfg_literal (Literal(pol,pred)) = ResClause.dfg_sign pol (string_of_predicate pred);
mengj@19720
   516
mengj@19720
   517
fun dfg_clause_aux (Clause{literals, ctypes_sorts, ...}) = 
mengj@19720
   518
  let val lits = map dfg_literal literals
mengj@19720
   519
      val (tvar_lits,tfree_lits) = ResClause.add_typs_aux ctypes_sorts
mengj@19720
   520
      val tvar_lits_strs = 
mengj@19720
   521
	  case !typ_level of T_NONE => [] 
paulson@20360
   522
	      | _ => map ResClause.dfg_of_typeLit tvar_lits
mengj@19720
   523
      val tfree_lits =
mengj@19720
   524
          case !typ_level of T_NONE => []
paulson@20360
   525
	      | _ => map ResClause.dfg_of_typeLit tfree_lits 
mengj@19720
   526
  in
mengj@19720
   527
      (tvar_lits_strs @ lits, tfree_lits)
mengj@19720
   528
  end; 
mengj@19720
   529
mengj@19720
   530
fun get_uvars (CombConst(_,_,_)) vars = vars
mengj@19720
   531
  | get_uvars (CombFree(_,_)) vars = vars
mengj@19720
   532
  | get_uvars (CombVar(v,tp)) vars = (v::vars)
paulson@21561
   533
  | get_uvars (CombApp(P,Q,tp)) vars = get_uvars P (get_uvars Q vars);
mengj@19720
   534
mengj@19720
   535
fun get_uvars_l (Literal(_,c)) = get_uvars c [];
mengj@19720
   536
mengj@19720
   537
fun dfg_vars (Clause {literals,...}) = ResClause.union_all (map get_uvars_l literals);
mengj@19720
   538
 
mengj@19720
   539
fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
mengj@19720
   540
    let val (lits,tfree_lits) = dfg_clause_aux cls 
mengj@19720
   541
        val vars = dfg_vars cls
mengj@19720
   542
        val tvars = ResClause.get_tvar_strs ctypes_sorts
paulson@21509
   543
	val knd = ResClause.name_of_kind kind
mengj@19720
   544
	val lits_str = commas lits
mengj@19720
   545
	val cls_str = ResClause.gen_dfg_cls(clause_id, axiom_name, knd, lits_str, tvars@vars) 
mengj@19720
   546
    in (cls_str, tfree_lits) end;
mengj@19720
   547
mengj@19720
   548
mengj@19720
   549
fun init_funcs_tab funcs = 
paulson@21561
   550
    let val funcs1 = case !typ_level of T_PARTIAL => Symtab.update ("hAPP",3) funcs
mengj@20865
   551
				      | _ => Symtab.update ("hAPP",2) funcs
mengj@19724
   552
    in
paulson@21561
   553
	Symtab.update ("typeinfo",2) funcs1
mengj@19724
   554
    end;
mengj@19720
   555
mengj@19720
   556
mengj@19725
   557
fun add_funcs (CombConst(c,_,tvars),funcs) =
paulson@21561
   558
      if c = "equal" then foldl ResClause.add_foltype_funcs funcs tvars
paulson@21561
   559
      else
paulson@21561
   560
	(case !typ_level of
paulson@21561
   561
	     T_CONST => foldl ResClause.add_foltype_funcs (Symtab.update(c,length tvars) funcs) tvars
paulson@21561
   562
           | _ => foldl ResClause.add_foltype_funcs (Symtab.update(c,0) funcs) tvars)
mengj@19724
   563
  | add_funcs (CombFree(v,ctp),funcs) = ResClause.add_foltype_funcs (ctp,Symtab.update (v,0) funcs) 
mengj@19720
   564
  | add_funcs (CombVar(_,ctp),funcs) = ResClause.add_foltype_funcs (ctp,funcs)
paulson@21561
   565
  | add_funcs (CombApp(P,Q,_),funcs) = add_funcs(P,add_funcs (Q,funcs));
mengj@19720
   566
mengj@19720
   567
fun add_literal_funcs (Literal(_,c), funcs) = add_funcs (c,funcs);
mengj@19720
   568
mengj@19720
   569
fun add_clause_funcs (Clause {literals, ...}, funcs) =
mengj@19720
   570
    foldl add_literal_funcs funcs literals
mengj@19720
   571
    handle Symtab.DUP a => raise ERROR ("function " ^ a ^ " has multiple arities")
mengj@19720
   572
mengj@19720
   573
fun funcs_of_clauses clauses arity_clauses =
mengj@19720
   574
    Symtab.dest (foldl ResClause.add_arityClause_funcs 
mengj@19720
   575
                       (foldl add_clause_funcs (init_funcs_tab Symtab.empty) clauses)
mengj@19720
   576
                       arity_clauses)
mengj@19720
   577
paulson@21398
   578
fun add_clause_preds (Clause {ctypes_sorts, ...}, preds) =
paulson@21398
   579
  foldl ResClause.add_type_sort_preds preds ctypes_sorts
paulson@21398
   580
  handle Symtab.DUP a => raise ERROR ("predicate " ^ a ^ " has multiple arities")
paulson@21398
   581
paulson@21398
   582
(*Higher-order clauses have only the predicates hBOOL and type classes.*)
paulson@21398
   583
fun preds_of_clauses clauses clsrel_clauses arity_clauses = 
mengj@19720
   584
    Symtab.dest
mengj@19720
   585
	(foldl ResClause.add_classrelClause_preds 
mengj@19720
   586
	       (foldl ResClause.add_arityClause_preds
paulson@21398
   587
		      (Symtab.update ("hBOOL",1) 
paulson@21398
   588
		        (foldl add_clause_preds Symtab.empty clauses))
mengj@19720
   589
		      arity_clauses)
mengj@19720
   590
	       clsrel_clauses)
mengj@19720
   591
mengj@18440
   592
paulson@21398
   593
mengj@18440
   594
(**********************************************************************)
mengj@19198
   595
(* write clauses to files                                             *)
mengj@19198
   596
(**********************************************************************)
mengj@19198
   597
mengj@20791
   598
val cnf_helper_thms = ResAxioms.cnf_rules_pairs o (map ResAxioms.pairname)
mengj@20644
   599
mengj@20791
   600
fun get_helper_clauses () =
paulson@21135
   601
    let val IK = if !combI_count > 0 orelse !combK_count > 0 
paulson@21135
   602
                 then (Output.debug "Include combinator I K"; cnf_helper_thms [comb_I,comb_K]) 
paulson@21135
   603
                 else []
paulson@21135
   604
	val BC = if !combB_count > 0 orelse !combC_count > 0 
paulson@21135
   605
	         then (Output.debug "Include combinator B C"; cnf_helper_thms [comb_B,comb_C]) 
paulson@21135
   606
	         else []
paulson@21135
   607
	val S = if !combS_count > 0 
paulson@21135
   608
	        then (Output.debug "Include combinator S"; cnf_helper_thms [comb_S]) 
paulson@21135
   609
	        else []
paulson@21135
   610
	val B'C' = if !combB'_count > 0 orelse !combC'_count > 0 
paulson@21135
   611
	           then (Output.debug "Include combinator B' C'"; cnf_helper_thms [comb_B', comb_C']) 
paulson@21135
   612
	           else []
paulson@21135
   613
	val S' = if !combS'_count > 0 
paulson@21135
   614
	         then (Output.debug "Include combinator S'"; cnf_helper_thms [comb_S']) 
paulson@21135
   615
	         else []
mengj@20791
   616
	val other = cnf_helper_thms [ext,fequal_imp_equal,equal_imp_fequal]
mengj@20791
   617
    in
paulson@21470
   618
	make_axiom_clauses (other @ IK @ BC @ S @ B'C' @ S') []
mengj@20791
   619
    end
mengj@20791
   620
mengj@20791
   621
mengj@20791
   622
(* tptp format *)
mengj@19491
   623
						  
mengj@19198
   624
(* write TPTP format to a single file *)
mengj@19198
   625
(* when "get_helper_clauses" is called, "include_combS" and "include_min_comb" should have correct values already *)
mengj@20130
   626
fun tptp_write_file thms filename (axclauses,classrel_clauses,arity_clauses) user_lemmas=
mengj@19444
   627
    let val clss = make_conjecture_clauses thms
mengj@20130
   628
        val (clnames,axclauses') = ListPair.unzip (make_axiom_clauses axclauses user_lemmas)
mengj@19198
   629
	val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp clss)
mengj@19198
   630
	val tfree_clss = map ResClause.tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
mengj@19198
   631
	val out = TextIO.openOut filename
mengj@20791
   632
	val helper_clauses = (#2 o ListPair.unzip o get_helper_clauses) ()
mengj@19198
   633
    in
mengj@19198
   634
	List.app (curry TextIO.output out o #1 o clause2tptp) axclauses';
mengj@19198
   635
	ResClause.writeln_strs out tfree_clss;
mengj@19198
   636
	ResClause.writeln_strs out tptp_clss;
mengj@19198
   637
	List.app (curry TextIO.output out o ResClause.tptp_classrelClause) classrel_clauses;
mengj@19198
   638
	List.app (curry TextIO.output out o ResClause.tptp_arity_clause) arity_clauses;
mengj@20791
   639
	List.app (curry TextIO.output out o #1 o clause2tptp) helper_clauses;
paulson@20022
   640
	TextIO.closeOut out;
paulson@20022
   641
	clnames
mengj@19198
   642
    end;
mengj@19198
   643
mengj@19720
   644
mengj@20644
   645
mengj@19720
   646
(* dfg format *)
mengj@19720
   647
mengj@20130
   648
fun dfg_write_file  thms filename (axclauses,classrel_clauses,arity_clauses) user_lemmas =
mengj@19720
   649
    let val _ = Output.debug ("Preparing to write the DFG file " ^ filename) 
mengj@19720
   650
	val conjectures = make_conjecture_clauses thms
mengj@20130
   651
        val (clnames,axclauses') = ListPair.unzip (make_axiom_clauses axclauses user_lemmas)
mengj@19720
   652
	val (dfg_clss,tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
mengj@19720
   653
	and probname = Path.pack (Path.base (Path.unpack filename))
mengj@19720
   654
	val (axstrs,_) =  ListPair.unzip (map clause2dfg axclauses')
mengj@19720
   655
	val tfree_clss = map ResClause.dfg_tfree_clause (ResClause.union_all tfree_litss)
mengj@19720
   656
	val out = TextIO.openOut filename
mengj@20791
   657
	val helper_clauses = (#2 o ListPair.unzip o get_helper_clauses) ()
mengj@20791
   658
	val helper_clauses_strs = (#1 o ListPair.unzip o (map clause2dfg)) helper_clauses
paulson@20864
   659
	val funcs = funcs_of_clauses (helper_clauses @ conjectures @ axclauses') arity_clauses
paulson@21398
   660
	and preds = preds_of_clauses axclauses' classrel_clauses arity_clauses
mengj@19720
   661
    in
mengj@19720
   662
	TextIO.output (out, ResClause.string_of_start probname); 
mengj@19720
   663
	TextIO.output (out, ResClause.string_of_descrip probname); 
mengj@19720
   664
	TextIO.output (out, ResClause.string_of_symbols (ResClause.string_of_funcs funcs) (ResClause.string_of_preds preds)); 
mengj@19720
   665
	TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
mengj@19720
   666
	ResClause.writeln_strs out axstrs;
mengj@19720
   667
	List.app (curry TextIO.output out o ResClause.dfg_classrelClause) classrel_clauses;
mengj@19720
   668
	List.app (curry TextIO.output out o ResClause.dfg_arity_clause) arity_clauses;
mengj@20791
   669
	ResClause.writeln_strs out helper_clauses_strs;
mengj@19720
   670
	TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
mengj@19720
   671
	ResClause.writeln_strs out tfree_clss;
mengj@19720
   672
	ResClause.writeln_strs out dfg_clss;
paulson@20953
   673
	TextIO.output (out, "end_of_list.\n\n");
paulson@20953
   674
	(*VarWeight=3 helps the HO problems, probably by counteracting the presence of hAPP*)
paulson@20953
   675
	TextIO.output (out, "list_of_settings(SPASS).\n{*\nset_flag(VarWeight,3).\n*}\nend_of_list.\n\n");
paulson@20953
   676
	TextIO.output (out, "end_problem.\n");
paulson@20022
   677
	TextIO.closeOut out;
paulson@20022
   678
	clnames
mengj@19720
   679
    end;
mengj@19720
   680
wenzelm@21254
   681
end