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