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