src/HOL/Tools/res_hol_clause.ML
author immler@in.tum.de
Wed Jun 03 16:56:41 2009 +0200 (2009-06-03)
changeset 31409 d8537ba165b5
parent 30242 aea5d7fa7ef5
child 31749 8ee34e3ceb5a
permissions -rw-r--r--
split preparing clauses and writing problemfile;
included results of count_constants in return-type of prover;
optionally pass counted constants to prover;
removed unused external_prover from signature
wenzelm@30190
     1
(*
mengj@17998
     2
   Author: Jia Meng, NICTA
mengj@17998
     3
wenzelm@24311
     4
FOL clauses translated from HOL formulae.
mengj@17998
     5
*)
mengj@17998
     6
wenzelm@24311
     7
signature RES_HOL_CLAUSE =
wenzelm@24311
     8
sig
wenzelm@24311
     9
  val ext: thm
wenzelm@24311
    10
  val comb_I: thm
wenzelm@24311
    11
  val comb_K: thm
wenzelm@24311
    12
  val comb_B: thm
wenzelm@24311
    13
  val comb_C: thm
wenzelm@24311
    14
  val comb_S: thm
paulson@24385
    15
  datatype type_level = T_FULL | T_CONST
wenzelm@30153
    16
  val typ_level: type_level
wenzelm@30153
    17
  val minimize_applies: bool
wenzelm@24311
    18
  type axiom_name = string
wenzelm@24311
    19
  type polarity = bool
wenzelm@24311
    20
  type clause_id = int
wenzelm@24311
    21
  datatype combterm =
wenzelm@24311
    22
      CombConst of string * ResClause.fol_type * ResClause.fol_type list (*Const and Free*)
wenzelm@24311
    23
    | CombVar of string * ResClause.fol_type
wenzelm@24311
    24
    | CombApp of combterm * combterm
wenzelm@24311
    25
  datatype literal = Literal of polarity * combterm
immler@31409
    26
  datatype clause = Clause of {clause_id: clause_id, axiom_name: axiom_name, th: thm,
immler@31409
    27
                    kind: ResClause.kind,literals: literal list, ctypes_sorts: typ list}
wenzelm@24311
    28
  val strip_comb: combterm -> combterm * combterm list
paulson@24940
    29
  val literals_of_term: theory -> term -> literal list * typ list
wenzelm@28835
    30
  exception TOO_TRIVIAL
immler@31409
    31
  val count_constants: clause list * clause list * clause list * 'a * 'b ->
immler@31409
    32
    int Symtab.table * bool Symtab.table
immler@31409
    33
  val prepare_clauses: bool -> theory -> bool -> thm list -> (thm * (axiom_name * clause_id)) list ->
immler@31409
    34
    string list -> axiom_name list * (clause list * clause list * clause list)
immler@31409
    35
  val tptp_write_file: string -> int Symtab.table * bool Symtab.table ->
immler@31409
    36
    clause list * clause list * clause list * ResClause.classrelClause list * ResClause.arityClause list -> unit
immler@31409
    37
  val dfg_write_file: string -> int Symtab.table * bool Symtab.table ->
immler@31409
    38
    clause list * clause list * clause list * ResClause.classrelClause list * ResClause.arityClause list -> unit
wenzelm@24311
    39
end
mengj@17998
    40
wenzelm@24311
    41
structure ResHolClause: RES_HOL_CLAUSE =
mengj@17998
    42
struct
mengj@17998
    43
paulson@22078
    44
structure RC = ResClause;
paulson@22078
    45
mengj@20791
    46
(* theorems for combinators and function extensionality *)
mengj@20791
    47
val ext = thm "HOL.ext";
wenzelm@21254
    48
val comb_I = thm "ATP_Linkup.COMBI_def";
wenzelm@21254
    49
val comb_K = thm "ATP_Linkup.COMBK_def";
wenzelm@21254
    50
val comb_B = thm "ATP_Linkup.COMBB_def";
wenzelm@21254
    51
val comb_C = thm "ATP_Linkup.COMBC_def";
wenzelm@21254
    52
val comb_S = thm "ATP_Linkup.COMBS_def";
wenzelm@21254
    53
val fequal_imp_equal = thm "ATP_Linkup.fequal_imp_equal";
wenzelm@21254
    54
val equal_imp_fequal = thm "ATP_Linkup.equal_imp_fequal";
mengj@20791
    55
paulson@22825
    56
paulson@22825
    57
(*The different translations of types*)
paulson@24385
    58
datatype type_level = T_FULL | T_CONST;
paulson@22825
    59
wenzelm@30153
    60
val typ_level = T_CONST;
paulson@22825
    61
paulson@22064
    62
(*If true, each function will be directly applied to as many arguments as possible, avoiding
paulson@24385
    63
  use of the "apply" operator. Use of hBOOL is also minimized.*)
wenzelm@30153
    64
val minimize_applies = true;
paulson@22064
    65
immler@30149
    66
fun min_arity_of const_min_arity c = getOpt (Symtab.lookup const_min_arity c, 0);
paulson@22064
    67
paulson@22825
    68
(*True if the constant ever appears outside of the top-level position in literals.
paulson@22825
    69
  If false, the constant always receives all of its arguments and is used as a predicate.*)
wenzelm@30153
    70
fun needs_hBOOL const_needs_hBOOL c = not minimize_applies orelse
immler@30150
    71
                    getOpt (Symtab.lookup const_needs_hBOOL c, false);
paulson@22064
    72
mengj@19198
    73
mengj@17998
    74
(******************************************************)
mengj@17998
    75
(* data types for typed combinator expressions        *)
mengj@17998
    76
(******************************************************)
mengj@17998
    77
mengj@17998
    78
type axiom_name = string;
mengj@17998
    79
type polarity = bool;
mengj@17998
    80
type clause_id = int;
mengj@17998
    81
paulson@23386
    82
datatype combterm = CombConst of string * RC.fol_type * RC.fol_type list (*Const and Free*)
wenzelm@24311
    83
                  | CombVar of string * RC.fol_type
wenzelm@24311
    84
                  | CombApp of combterm * combterm
wenzelm@24311
    85
mengj@17998
    86
datatype literal = Literal of polarity * combterm;
mengj@17998
    87
wenzelm@24311
    88
datatype clause =
wenzelm@24311
    89
         Clause of {clause_id: clause_id,
wenzelm@24311
    90
                    axiom_name: axiom_name,
wenzelm@24311
    91
                    th: thm,
wenzelm@24311
    92
                    kind: RC.kind,
wenzelm@24311
    93
                    literals: literal list,
paulson@24940
    94
                    ctypes_sorts: typ list};
mengj@17998
    95
mengj@17998
    96
mengj@17998
    97
(*********************************************************************)
mengj@17998
    98
(* convert a clause with type Term.term to a clause with type clause *)
mengj@17998
    99
(*********************************************************************)
mengj@17998
   100
paulson@21561
   101
fun isFalse (Literal(pol, CombConst(c,_,_))) =
paulson@20360
   102
      (pol andalso c = "c_False") orelse
paulson@20360
   103
      (not pol andalso c = "c_True")
mengj@17998
   104
  | isFalse _ = false;
mengj@17998
   105
paulson@21561
   106
fun isTrue (Literal (pol, CombConst(c,_,_))) =
mengj@17998
   107
      (pol andalso c = "c_True") orelse
mengj@17998
   108
      (not pol andalso c = "c_False")
mengj@17998
   109
  | isTrue _ = false;
wenzelm@24311
   110
wenzelm@24311
   111
fun isTaut (Clause {literals,...}) = exists isTrue literals;
mengj@17998
   112
immler@30151
   113
fun type_of dfg (Type (a, Ts)) =
immler@30151
   114
      let val (folTypes,ts) = types_of dfg Ts
immler@30151
   115
      in  (RC.Comp(RC.make_fixed_type_const dfg a, folTypes), ts)  end
immler@30151
   116
  | type_of dfg (tp as (TFree(a,s))) =
paulson@24940
   117
      (RC.AtomF (RC.make_fixed_type_var a), [tp])
immler@30151
   118
  | type_of dfg (tp as (TVar(v,s))) =
paulson@24940
   119
      (RC.AtomV (RC.make_schematic_type_var v), [tp])
immler@30151
   120
and types_of dfg Ts =
immler@30151
   121
      let val (folTyps,ts) = ListPair.unzip (map (type_of dfg) Ts)
paulson@22078
   122
      in  (folTyps, RC.union_all ts)  end;
mengj@17998
   123
mengj@17998
   124
(* same as above, but no gathering of sort information *)
immler@30151
   125
fun simp_type_of dfg (Type (a, Ts)) =
immler@30151
   126
      RC.Comp(RC.make_fixed_type_const dfg a, map (simp_type_of dfg) Ts)
immler@30151
   127
  | simp_type_of dfg (TFree (a,s)) = RC.AtomF(RC.make_fixed_type_var a)
immler@30151
   128
  | simp_type_of dfg (TVar (v,s)) = RC.AtomV(RC.make_schematic_type_var v);
mengj@18440
   129
mengj@18356
   130
immler@30151
   131
fun const_type_of dfg thy (c,t) =
immler@30151
   132
      let val (tp,ts) = type_of dfg t
immler@30151
   133
      in  (tp, ts, map (simp_type_of dfg) (Sign.const_typargs thy (c,t))) end;
mengj@18356
   134
mengj@17998
   135
(* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
immler@30151
   136
fun combterm_of dfg thy (Const(c,t)) =
immler@30151
   137
      let val (tp,ts,tvar_list) = const_type_of dfg thy (c,t)
immler@30151
   138
          val c' = CombConst(RC.make_fixed_const dfg c, tp, tvar_list)
paulson@22078
   139
      in  (c',ts)  end
immler@30151
   140
  | combterm_of dfg thy (Free(v,t)) =
immler@30151
   141
      let val (tp,ts) = type_of dfg t
paulson@25243
   142
          val v' = CombConst(RC.make_fixed_var v, tp, [])
paulson@22078
   143
      in  (v',ts)  end
immler@30151
   144
  | combterm_of dfg thy (Var(v,t)) =
immler@30151
   145
      let val (tp,ts) = type_of dfg t
wenzelm@24311
   146
          val v' = CombVar(RC.make_schematic_var v,tp)
paulson@22078
   147
      in  (v',ts)  end
immler@30151
   148
  | combterm_of dfg thy (P $ Q) =
immler@30151
   149
      let val (P',tsP) = combterm_of dfg thy P
immler@30151
   150
          val (Q',tsQ) = combterm_of dfg thy Q
paulson@24385
   151
      in  (CombApp(P',Q'), tsP union tsQ)  end
immler@30151
   152
  | combterm_of _ thy (t as Abs _) = raise RC.CLAUSE("HOL CLAUSE",t);
mengj@17998
   153
immler@30151
   154
fun predicate_of dfg thy ((Const("Not",_) $ P), polarity) = predicate_of dfg thy (P, not polarity)
immler@30151
   155
  | predicate_of dfg thy (t,polarity) = (combterm_of dfg thy (Envir.eta_contract t), polarity);
mengj@17998
   156
immler@30151
   157
fun literals_of_term1 dfg thy args (Const("Trueprop",_) $ P) = literals_of_term1 dfg thy args P
immler@30151
   158
  | literals_of_term1 dfg thy args (Const("op |",_) $ P $ Q) =
immler@30151
   159
      literals_of_term1 dfg thy (literals_of_term1 dfg thy args P) Q
immler@30151
   160
  | literals_of_term1 dfg thy (lits,ts) P =
immler@30151
   161
      let val ((pred,ts'),pol) = predicate_of dfg thy (P,true)
paulson@21509
   162
      in
wenzelm@24311
   163
          (Literal(pol,pred)::lits, ts union ts')
paulson@21509
   164
      end;
mengj@17998
   165
immler@30151
   166
fun literals_of_term_dfg dfg thy P = literals_of_term1 dfg thy ([],[]) P;
immler@30151
   167
val literals_of_term = literals_of_term_dfg false;
mengj@17998
   168
wenzelm@28835
   169
(* Problem too trivial for resolution (empty clause) *)
wenzelm@28835
   170
exception TOO_TRIVIAL;
wenzelm@28835
   171
mengj@17998
   172
(* making axiom and conjecture clauses *)
immler@30151
   173
fun make_clause dfg thy (clause_id,axiom_name,kind,th) =
immler@30151
   174
    let val (lits,ctypes_sorts) = literals_of_term_dfg dfg thy (prop_of th)
mengj@17998
   175
    in
wenzelm@24311
   176
        if forall isFalse lits
wenzelm@28835
   177
        then raise TOO_TRIVIAL
wenzelm@24311
   178
        else
wenzelm@24311
   179
            Clause {clause_id = clause_id, axiom_name = axiom_name, th = th, kind = kind,
paulson@24937
   180
                    literals = lits, ctypes_sorts = ctypes_sorts}
mengj@17998
   181
    end;
mengj@17998
   182
mengj@20016
   183
immler@30151
   184
fun add_axiom_clause dfg thy ((th,(name,id)), pairs) =
immler@30151
   185
  let val cls = make_clause dfg thy (id, name, RC.Axiom, th)
paulson@21573
   186
  in
paulson@21573
   187
      if isTaut cls then pairs else (name,cls)::pairs
paulson@21573
   188
  end;
mengj@19354
   189
wenzelm@30190
   190
fun make_axiom_clauses dfg thy = List.foldl (add_axiom_clause dfg thy) [];
mengj@19354
   191
immler@30151
   192
fun make_conjecture_clauses_aux dfg _ _ [] = []
immler@30151
   193
  | make_conjecture_clauses_aux dfg thy n (th::ths) =
immler@30151
   194
      make_clause dfg thy (n,"conjecture", RC.Conjecture, th) ::
immler@30151
   195
      make_conjecture_clauses_aux dfg thy (n+1) ths;
mengj@17998
   196
immler@30151
   197
fun make_conjecture_clauses dfg thy = make_conjecture_clauses_aux dfg thy 0;
mengj@17998
   198
mengj@17998
   199
mengj@17998
   200
(**********************************************************************)
mengj@17998
   201
(* convert clause into ATP specific formats:                          *)
mengj@17998
   202
(* TPTP used by Vampire and E                                         *)
mengj@19720
   203
(* DFG used by SPASS                                                  *)
mengj@17998
   204
(**********************************************************************)
mengj@17998
   205
paulson@22078
   206
(*Result of a function type; no need to check that the argument type matches.*)
paulson@22078
   207
fun result_type (RC.Comp ("tc_fun", [_, tp2])) = tp2
wenzelm@27187
   208
  | result_type _ = error "result_type"
paulson@22078
   209
paulson@21513
   210
fun type_of_combterm (CombConst(c,tp,_)) = tp
paulson@21513
   211
  | type_of_combterm (CombVar(v,tp)) = tp
paulson@22078
   212
  | type_of_combterm (CombApp(t1,t2)) = result_type (type_of_combterm t1);
mengj@17998
   213
paulson@22064
   214
(*gets the head of a combinator application, along with the list of arguments*)
paulson@22064
   215
fun strip_comb u =
paulson@22078
   216
    let fun stripc (CombApp(t,u), ts) = stripc (t, u::ts)
paulson@22064
   217
        |   stripc  x =  x
paulson@22064
   218
    in  stripc(u,[])  end;
paulson@22064
   219
paulson@22851
   220
val type_wrapper = "ti";
paulson@22851
   221
immler@30150
   222
fun head_needs_hBOOL const_needs_hBOOL (CombConst(c,_,_)) = needs_hBOOL const_needs_hBOOL c
immler@30150
   223
  | head_needs_hBOOL const_needs_hBOOL _ = true;
paulson@22851
   224
wenzelm@24311
   225
fun wrap_type (s, tp) =
wenzelm@30153
   226
  if typ_level=T_FULL then
paulson@22851
   227
      type_wrapper ^ RC.paren_pack [s, RC.string_of_fol_type tp]
paulson@22851
   228
  else s;
wenzelm@24311
   229
paulson@22851
   230
fun apply ss = "hAPP" ^ RC.paren_pack ss;
paulson@22851
   231
paulson@22064
   232
fun rev_apply (v, []) = v
paulson@22851
   233
  | rev_apply (v, arg::args) = apply [rev_apply (v, args), arg];
paulson@22064
   234
paulson@22064
   235
fun string_apply (v, args) = rev_apply (v, rev args);
paulson@22064
   236
paulson@22064
   237
(*Apply an operator to the argument strings, using either the "apply" operator or
paulson@22064
   238
  direct function application.*)
immler@30150
   239
fun string_of_applic cma (CombConst(c,tp,tvars), args) =
paulson@22064
   240
      let val c = if c = "equal" then "c_fequal" else c
immler@30150
   241
          val nargs = min_arity_of cma c
paulson@22851
   242
          val args1 = List.take(args, nargs)
wenzelm@27187
   243
            handle Subscript => error ("string_of_applic: " ^ c ^ " has arity " ^
wenzelm@27187
   244
                                         Int.toString nargs ^ " but is applied to " ^
wenzelm@27187
   245
                                         space_implode ", " args)
paulson@22064
   246
          val args2 = List.drop(args, nargs)
wenzelm@30153
   247
          val targs = if typ_level = T_CONST then map RC.string_of_fol_type tvars
paulson@22851
   248
                      else []
paulson@21513
   249
      in
wenzelm@24311
   250
          string_apply (c ^ RC.paren_pack (args1@targs), args2)
paulson@21513
   251
      end
immler@30150
   252
  | string_of_applic cma (CombVar(v,tp), args) = string_apply (v, args)
immler@30149
   253
  | string_of_applic _ _ = error "string_of_applic";
paulson@22064
   254
immler@30150
   255
fun wrap_type_if cnh (head, s, tp) = if head_needs_hBOOL cnh head then wrap_type (s, tp) else s;
wenzelm@24311
   256
immler@30150
   257
fun string_of_combterm cma cnh t =
paulson@22064
   258
  let val (head, args) = strip_comb t
immler@30150
   259
  in  wrap_type_if cnh (head,
immler@30150
   260
                    string_of_applic cma (head, map (string_of_combterm cma cnh) args),
paulson@22851
   261
                    type_of_combterm t)
paulson@22851
   262
  end;
mengj@18356
   263
paulson@22064
   264
(*Boolean-valued terms are here converted to literals.*)
immler@30150
   265
fun boolify cma cnh t = "hBOOL" ^ RC.paren_pack [string_of_combterm cma cnh t];
paulson@22064
   266
immler@30150
   267
fun string_of_predicate cma cnh t =
paulson@22064
   268
  case t of
paulson@22078
   269
      (CombApp(CombApp(CombConst("equal",_,_), t1), t2)) =>
wenzelm@24311
   270
          (*DFG only: new TPTP prefers infix equality*)
immler@30150
   271
          ("equal" ^ RC.paren_pack [string_of_combterm cma cnh t1, string_of_combterm cma cnh t2])
wenzelm@24311
   272
    | _ =>
paulson@22064
   273
          case #1 (strip_comb t) of
immler@30150
   274
              CombConst(c,_,_) => if needs_hBOOL cnh c then boolify cma cnh t else string_of_combterm cma cnh t
immler@30150
   275
            | _ => boolify cma cnh t;
mengj@18356
   276
wenzelm@24311
   277
fun string_of_clausename (cls_id,ax_name) =
paulson@22078
   278
    RC.clause_prefix ^ RC.ascii_of ax_name ^ "_" ^ Int.toString cls_id;
mengj@17998
   279
wenzelm@24311
   280
fun string_of_type_clsname (cls_id,ax_name,idx) =
mengj@17998
   281
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
mengj@17998
   282
mengj@17998
   283
paulson@21561
   284
(*** tptp format ***)
mengj@19720
   285
immler@30150
   286
fun tptp_of_equality cma cnh pol (t1,t2) =
paulson@21513
   287
  let val eqop = if pol then " = " else " != "
immler@30150
   288
  in  string_of_combterm cma cnh t1 ^ eqop ^ string_of_combterm cma cnh t2  end;
paulson@21513
   289
immler@30150
   290
fun tptp_literal cma cnh (Literal(pol, CombApp(CombApp(CombConst("equal",_,_), t1), t2))) =
immler@30150
   291
      tptp_of_equality cma cnh pol (t1,t2)
immler@30150
   292
  | tptp_literal cma cnh (Literal(pol,pred)) =
immler@30150
   293
      RC.tptp_sign pol (string_of_predicate cma cnh pred);
wenzelm@24311
   294
paulson@22064
   295
(*Given a clause, returns its literals paired with a list of literals concerning TFrees;
paulson@22064
   296
  the latter should only occur in conjecture clauses.*)
immler@30150
   297
fun tptp_type_lits cma cnh pos (Clause{literals, ctypes_sorts, ...}) =
immler@30150
   298
      (map (tptp_literal cma cnh) literals, 
paulson@24937
   299
       map (RC.tptp_of_typeLit pos) (RC.add_typs ctypes_sorts));
wenzelm@24311
   300
immler@31409
   301
fun clause2tptp (cma, cnh) (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
immler@30150
   302
  let val (lits,tylits) = tptp_type_lits cma cnh (kind = RC.Conjecture) cls
paulson@24937
   303
  in
paulson@24937
   304
      (RC.gen_tptp_cls(clause_id,axiom_name,kind,lits,tylits), tylits)
paulson@24937
   305
  end;
mengj@17998
   306
mengj@17998
   307
paulson@21561
   308
(*** dfg format ***)
paulson@21561
   309
immler@30150
   310
fun dfg_literal cma cnh (Literal(pol,pred)) = RC.dfg_sign pol (string_of_predicate cma cnh pred);
mengj@19720
   311
immler@30150
   312
fun dfg_type_lits cma cnh pos (Clause{literals, ctypes_sorts, ...}) =
immler@30150
   313
      (map (dfg_literal cma cnh) literals, 
paulson@24937
   314
       map (RC.dfg_of_typeLit pos) (RC.add_typs ctypes_sorts));
mengj@19720
   315
paulson@22078
   316
fun get_uvars (CombConst _) vars = vars
paulson@22078
   317
  | get_uvars (CombVar(v,_)) vars = (v::vars)
paulson@22078
   318
  | get_uvars (CombApp(P,Q)) vars = get_uvars P (get_uvars Q vars);
mengj@19720
   319
mengj@19720
   320
fun get_uvars_l (Literal(_,c)) = get_uvars c [];
mengj@19720
   321
paulson@22078
   322
fun dfg_vars (Clause {literals,...}) = RC.union_all (map get_uvars_l literals);
wenzelm@24311
   323
immler@31409
   324
fun clause2dfg (cma, cnh) (cls as Clause{axiom_name,clause_id,kind,ctypes_sorts,...}) =
immler@30150
   325
  let val (lits,tylits) = dfg_type_lits cma cnh (kind = RC.Conjecture) cls
paulson@24937
   326
      val vars = dfg_vars cls
paulson@24937
   327
      val tvars = RC.get_tvar_strs ctypes_sorts
paulson@24937
   328
  in
paulson@24937
   329
      (RC.gen_dfg_cls(clause_id, axiom_name, kind, lits, tylits, tvars@vars), tylits)
paulson@24937
   330
  end;
paulson@24937
   331
mengj@19720
   332
paulson@22064
   333
(** For DFG format: accumulate function and predicate declarations **)
mengj@19720
   334
wenzelm@30190
   335
fun addtypes tvars tab = List.foldl RC.add_foltype_funcs tab tvars;
mengj@19720
   336
immler@30150
   337
fun add_decls cma cnh (CombConst(c,tp,tvars), (funcs,preds)) =
paulson@22064
   338
      if c = "equal" then (addtypes tvars funcs, preds)
paulson@21561
   339
      else
immler@30150
   340
	let val arity = min_arity_of cma c
wenzelm@30153
   341
	    val ntys = if typ_level = T_CONST then length tvars else 0
paulson@24385
   342
	    val addit = Symtab.update(c, arity+ntys)
paulson@24385
   343
	in
immler@30150
   344
	    if needs_hBOOL cnh c then (addtypes tvars (addit funcs), preds)
paulson@24385
   345
	    else (addtypes tvars funcs, addit preds)
paulson@24385
   346
	end
immler@30150
   347
  | add_decls _ _ (CombVar(_,ctp), (funcs,preds)) =
paulson@22078
   348
      (RC.add_foltype_funcs (ctp,funcs), preds)
immler@30150
   349
  | add_decls cma cnh (CombApp(P,Q),decls) = add_decls cma cnh (P,add_decls cma cnh (Q,decls));
mengj@19720
   350
immler@30150
   351
fun add_literal_decls cma cnh (Literal(_,c), decls) = add_decls cma cnh (c,decls);
mengj@19720
   352
immler@30150
   353
fun add_clause_decls cma cnh (Clause {literals, ...}, decls) =
wenzelm@30190
   354
    List.foldl (add_literal_decls cma cnh) decls literals
wenzelm@27187
   355
    handle Symtab.DUP a => error ("function " ^ a ^ " has multiple arities")
mengj@19720
   356
immler@31409
   357
fun decls_of_clauses (cma, cnh) clauses arity_clauses =
paulson@24385
   358
  let val init_functab = Symtab.update (type_wrapper,2) (Symtab.update ("hAPP",2) RC.init_functab)
paulson@22064
   359
      val init_predtab = Symtab.update ("hBOOL",1) Symtab.empty
wenzelm@30190
   360
      val (functab,predtab) = (List.foldl (add_clause_decls cma cnh) (init_functab, init_predtab) clauses)
paulson@22064
   361
  in
wenzelm@30190
   362
      (Symtab.dest (List.foldl RC.add_arityClause_funcs functab arity_clauses),
paulson@22064
   363
       Symtab.dest predtab)
paulson@22064
   364
  end;
mengj@19720
   365
paulson@21398
   366
fun add_clause_preds (Clause {ctypes_sorts, ...}, preds) =
wenzelm@30190
   367
  List.foldl RC.add_type_sort_preds preds ctypes_sorts
wenzelm@27187
   368
  handle Symtab.DUP a => error ("predicate " ^ a ^ " has multiple arities")
paulson@21398
   369
paulson@21398
   370
(*Higher-order clauses have only the predicates hBOOL and type classes.*)
wenzelm@24311
   371
fun preds_of_clauses clauses clsrel_clauses arity_clauses =
mengj@19720
   372
    Symtab.dest
wenzelm@30190
   373
        (List.foldl RC.add_classrelClause_preds
wenzelm@30190
   374
               (List.foldl RC.add_arityClause_preds
wenzelm@30190
   375
                      (List.foldl add_clause_preds Symtab.empty clauses)
wenzelm@24311
   376
                      arity_clauses)
wenzelm@24311
   377
               clsrel_clauses)
mengj@19720
   378
mengj@18440
   379
mengj@18440
   380
(**********************************************************************)
mengj@19198
   381
(* write clauses to files                                             *)
mengj@19198
   382
(**********************************************************************)
mengj@19198
   383
paulson@21573
   384
val init_counters =
paulson@21573
   385
    Symtab.make [("c_COMBI", 0), ("c_COMBK", 0),
wenzelm@24311
   386
                 ("c_COMBB", 0), ("c_COMBC", 0),
paulson@24943
   387
                 ("c_COMBS", 0)];
wenzelm@24311
   388
wenzelm@24311
   389
fun count_combterm (CombConst(c,tp,_), ct) =
paulson@21573
   390
     (case Symtab.lookup ct c of NONE => ct  (*no counter*)
paulson@21573
   391
                               | SOME n => Symtab.update (c,n+1) ct)
paulson@21573
   392
  | count_combterm (CombVar(v,tp), ct) = ct
paulson@22078
   393
  | count_combterm (CombApp(t1,t2), ct) = count_combterm(t1, count_combterm(t2, ct));
paulson@21573
   394
paulson@21573
   395
fun count_literal (Literal(_,t), ct) = count_combterm(t,ct);
paulson@21573
   396
wenzelm@30190
   397
fun count_clause (Clause{literals,...}, ct) = List.foldl count_literal ct literals;
paulson@21573
   398
wenzelm@24311
   399
fun count_user_clause user_lemmas (Clause{axiom_name,literals,...}, ct) =
wenzelm@30190
   400
  if axiom_name mem_string user_lemmas then List.foldl count_literal ct literals
paulson@21573
   401
  else ct;
paulson@21573
   402
wenzelm@27178
   403
fun cnf_helper_thms thy =
wenzelm@27178
   404
  ResAxioms.cnf_rules_pairs thy o map ResAxioms.pairname
mengj@20644
   405
immler@30151
   406
fun get_helper_clauses dfg thy isFO (conjectures, axclauses, user_lemmas) =
paulson@23386
   407
  if isFO then []  (*first-order*)
paulson@23386
   408
  else
wenzelm@30190
   409
    let val ct0 = List.foldl count_clause init_counters conjectures
wenzelm@30190
   410
        val ct = List.foldl (count_user_clause user_lemmas) ct0 axclauses
paulson@22064
   411
        fun needed c = valOf (Symtab.lookup ct c) > 0
wenzelm@24311
   412
        val IK = if needed "c_COMBI" orelse needed "c_COMBK"
wenzelm@27178
   413
                 then (Output.debug (fn () => "Include combinator I K"); cnf_helper_thms thy [comb_I,comb_K])
wenzelm@24311
   414
                 else []
wenzelm@24311
   415
        val BC = if needed "c_COMBB" orelse needed "c_COMBC"
wenzelm@27178
   416
                 then (Output.debug (fn () => "Include combinator B C"); cnf_helper_thms thy [comb_B,comb_C])
paulson@21135
   417
                 else []
wenzelm@24311
   418
        val S = if needed "c_COMBS"
wenzelm@27178
   419
                then (Output.debug (fn () => "Include combinator S"); cnf_helper_thms thy [comb_S])
wenzelm@24311
   420
                else []
wenzelm@27178
   421
        val other = cnf_helper_thms thy [ext,fequal_imp_equal,equal_imp_fequal]
mengj@20791
   422
    in
immler@30151
   423
        map #2 (make_axiom_clauses dfg thy (other @ IK @ BC @ S))
paulson@23386
   424
    end;
mengj@20791
   425
paulson@22064
   426
(*Find the minimal arity of each function mentioned in the term. Also, note which uses
paulson@22064
   427
  are not at top level, to see if hBOOL is needed.*)
immler@30150
   428
fun count_constants_term toplev t (const_min_arity, const_needs_hBOOL) =
paulson@22064
   429
  let val (head, args) = strip_comb t
paulson@22064
   430
      val n = length args
immler@30150
   431
      val (const_min_arity, const_needs_hBOOL) = fold (count_constants_term false) args (const_min_arity, const_needs_hBOOL)
paulson@22064
   432
  in
paulson@22064
   433
      case head of
wenzelm@24311
   434
          CombConst (a,_,_) => (*predicate or function version of "equal"?*)
wenzelm@24311
   435
            let val a = if a="equal" andalso not toplev then "c_fequal" else a
immler@30149
   436
            val const_min_arity = Symtab.map_default (a,n) (curry Int.min n) const_min_arity
wenzelm@24311
   437
            in
immler@30150
   438
              if toplev then (const_min_arity, const_needs_hBOOL)
immler@30150
   439
              else (const_min_arity, Symtab.update (a,true) (const_needs_hBOOL))
wenzelm@24311
   440
            end
immler@30150
   441
        | ts => (const_min_arity, const_needs_hBOOL)
paulson@22064
   442
  end;
paulson@22064
   443
paulson@22064
   444
(*A literal is a top-level term*)
immler@30150
   445
fun count_constants_lit (Literal (_,t)) (const_min_arity, const_needs_hBOOL) =
immler@30150
   446
  count_constants_term true t (const_min_arity, const_needs_hBOOL);
paulson@22064
   447
immler@30150
   448
fun count_constants_clause (Clause{literals,...}) (const_min_arity, const_needs_hBOOL) =
immler@30150
   449
  fold count_constants_lit literals (const_min_arity, const_needs_hBOOL);
paulson@22064
   450
immler@30150
   451
fun display_arity const_needs_hBOOL (c,n) =
wenzelm@24311
   452
  Output.debug (fn () => "Constant: " ^ c ^ " arity:\t" ^ Int.toString n ^
immler@30150
   453
                (if needs_hBOOL const_needs_hBOOL c then " needs hBOOL" else ""));
paulson@22064
   454
immler@31409
   455
fun count_constants (conjectures, axclauses, helper_clauses, _, _) =
wenzelm@30153
   456
  if minimize_applies then
immler@30150
   457
     let val (const_min_arity, const_needs_hBOOL) =
immler@30150
   458
          fold count_constants_clause conjectures (Symtab.empty, Symtab.empty)
immler@30149
   459
       |> fold count_constants_clause axclauses
immler@30149
   460
       |> fold count_constants_clause helper_clauses
immler@30150
   461
     val _ = List.app (display_arity const_needs_hBOOL) (Symtab.dest (const_min_arity))
immler@30150
   462
     in (const_min_arity, const_needs_hBOOL) end
immler@30150
   463
  else (Symtab.empty, Symtab.empty);
paulson@22064
   464
mengj@20791
   465
(* tptp format *)
wenzelm@24311
   466
immler@31409
   467
fun prepare_clauses dfg thy isFO thms ax_tuples user_lemmas =
immler@31409
   468
  let
immler@31409
   469
    val conjectures = make_conjecture_clauses dfg thy thms
immler@31409
   470
    val (clnames,axclauses) = ListPair.unzip (make_axiom_clauses dfg thy ax_tuples)
immler@31409
   471
    val helper_clauses = get_helper_clauses dfg thy isFO (conjectures, axclauses, user_lemmas)
immler@31409
   472
  in
immler@31409
   473
    (clnames, (conjectures, axclauses, helper_clauses))
immler@31409
   474
  end
immler@31409
   475
mengj@19198
   476
(* write TPTP format to a single file *)
immler@31409
   477
fun tptp_write_file filename const_counts (conjectures, axclauses, helper_clauses, classrel_clauses, arity_clauses) =
immler@31409
   478
  let
immler@31409
   479
    val (tptp_clss,tfree_litss) = ListPair.unzip (map (clause2tptp const_counts) conjectures)
immler@31409
   480
    val tfree_clss = map RC.tptp_tfree_clause (List.foldl (op union_string) [] tfree_litss)
immler@31409
   481
    val out = TextIO.openOut filename
immler@31409
   482
  in
immler@31409
   483
    List.app (curry TextIO.output out o #1 o (clause2tptp const_counts)) axclauses;
immler@31409
   484
    RC.writeln_strs out tfree_clss;
immler@31409
   485
    RC.writeln_strs out tptp_clss;
immler@31409
   486
    List.app (curry TextIO.output out o RC.tptp_classrelClause) classrel_clauses;
immler@31409
   487
    List.app (curry TextIO.output out o RC.tptp_arity_clause) arity_clauses;
immler@31409
   488
    List.app (curry TextIO.output out o #1 o (clause2tptp const_counts)) helper_clauses;
immler@31409
   489
    TextIO.closeOut out
immler@31409
   490
  end;
mengj@19198
   491
mengj@19720
   492
mengj@19720
   493
(* dfg format *)
mengj@19720
   494
immler@31409
   495
fun dfg_write_file filename const_counts (conjectures, axclauses, helper_clauses, classrel_clauses, arity_clauses) =
immler@31409
   496
  let
immler@31409
   497
    val (dfg_clss, tfree_litss) = ListPair.unzip (map (clause2dfg const_counts) conjectures)
immler@31409
   498
    and probname = Path.implode (Path.base (Path.explode filename))
immler@31409
   499
    val axstrs = map (#1 o (clause2dfg const_counts)) axclauses
immler@31409
   500
    val tfree_clss = map RC.dfg_tfree_clause (RC.union_all tfree_litss)
immler@31409
   501
    val out = TextIO.openOut filename
immler@31409
   502
    val helper_clauses_strs = map (#1 o (clause2dfg const_counts)) helper_clauses
immler@31409
   503
    val (funcs,cl_preds) = decls_of_clauses const_counts (helper_clauses @ conjectures @ axclauses) arity_clauses
immler@31409
   504
    and ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
immler@31409
   505
  in
immler@31409
   506
    TextIO.output (out, RC.string_of_start probname);
immler@31409
   507
    TextIO.output (out, RC.string_of_descrip probname);
immler@31409
   508
    TextIO.output (out, RC.string_of_symbols
immler@31409
   509
                          (RC.string_of_funcs funcs)
immler@31409
   510
                          (RC.string_of_preds (cl_preds @ ty_preds)));
immler@31409
   511
    TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
immler@31409
   512
    RC.writeln_strs out axstrs;
immler@31409
   513
    List.app (curry TextIO.output out o RC.dfg_classrelClause) classrel_clauses;
immler@31409
   514
    List.app (curry TextIO.output out o RC.dfg_arity_clause) arity_clauses;
immler@31409
   515
    RC.writeln_strs out helper_clauses_strs;
immler@31409
   516
    TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
immler@31409
   517
    RC.writeln_strs out tfree_clss;
immler@31409
   518
    RC.writeln_strs out dfg_clss;
immler@31409
   519
    TextIO.output (out, "end_of_list.\n\n");
immler@31409
   520
    (*VarWeight=3 helps the HO problems, probably by counteracting the presence of hAPP*)
immler@31409
   521
    TextIO.output (out, "list_of_settings(SPASS).\n{*\nset_flag(VarWeight,3).\n*}\nend_of_list.\n\n");
immler@31409
   522
    TextIO.output (out, "end_problem.\n");
immler@31409
   523
    TextIO.closeOut out
immler@31409
   524
  end;
mengj@19720
   525
wenzelm@21254
   526
end