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