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