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