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