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