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