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