src/HOL/Tools/Sledgehammer/sledgehammer_hol_clause.ML
author blanchet
Tue Jun 22 14:28:22 2010 +0200 (2010-06-22 ago)
changeset 37498 b426cbdb5a23
parent 37496 9ae78e12e126
child 37500 7587b6e63454
permissions -rw-r--r--
removed Sledgehammer's support for the DFG syntax;
this removes 350 buggy lines from Sledgehammer. SPASS 3.5 and above support the TPTP syntax.
blanchet@36375
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_hol_clause.ML
wenzelm@33311
     2
    Author:     Jia Meng, NICTA
blanchet@36375
     3
    Author:     Jasmin Blanchette, TU Muenchen
mengj@17998
     4
wenzelm@24311
     5
FOL clauses translated from HOL formulae.
mengj@17998
     6
*)
mengj@17998
     7
blanchet@35826
     8
signature SLEDGEHAMMER_HOL_CLAUSE =
wenzelm@24311
     9
sig
blanchet@36170
    10
  type name = Sledgehammer_FOL_Clause.name
blanchet@36393
    11
  type name_pool = Sledgehammer_FOL_Clause.name_pool
blanchet@35865
    12
  type kind = Sledgehammer_FOL_Clause.kind
blanchet@35865
    13
  type fol_type = Sledgehammer_FOL_Clause.fol_type
blanchet@35865
    14
  type classrel_clause = Sledgehammer_FOL_Clause.classrel_clause
blanchet@35865
    15
  type arity_clause = Sledgehammer_FOL_Clause.arity_clause
wenzelm@24311
    16
  type axiom_name = string
wenzelm@24311
    17
  type polarity = bool
blanchet@35865
    18
  type hol_clause_id = int
blanchet@35865
    19
wenzelm@24311
    20
  datatype combterm =
blanchet@36170
    21
    CombConst of name * fol_type * fol_type list (* Const and Free *) |
blanchet@36170
    22
    CombVar of name * fol_type |
blanchet@35865
    23
    CombApp of combterm * combterm
wenzelm@24311
    24
  datatype literal = Literal of polarity * combterm
blanchet@35865
    25
  datatype hol_clause =
blanchet@35865
    26
    HOLClause of {clause_id: hol_clause_id, axiom_name: axiom_name, th: thm,
blanchet@35865
    27
                  kind: kind, literals: literal list, ctypes_sorts: typ list}
blanchet@35865
    28
blanchet@35865
    29
  val type_of_combterm : combterm -> fol_type
blanchet@35865
    30
  val strip_combterm_comb : combterm -> combterm * combterm list
blanchet@35865
    31
  val literals_of_term : theory -> term -> literal list * typ list
blanchet@37410
    32
  val conceal_skolem_somes :
blanchet@37410
    33
    int -> (string * term) list -> term -> (string * term) list * term
blanchet@35865
    34
  exception TRIVIAL
blanchet@37498
    35
  val make_conjecture_clauses : theory -> thm list -> hol_clause list
blanchet@37498
    36
  val make_axiom_clauses :
blanchet@37498
    37
    theory -> (thm * (axiom_name * hol_clause_id)) list
blanchet@37498
    38
    -> (axiom_name * hol_clause) list
blanchet@36909
    39
  val type_wrapper_name : string
blanchet@37399
    40
  val get_helper_clauses :
blanchet@37498
    41
    theory -> bool -> bool -> hol_clause list
blanchet@37498
    42
    -> (thm * (axiom_name * hol_clause_id)) list -> hol_clause list
blanchet@36235
    43
  val write_tptp_file : bool -> bool -> bool -> Path.T ->
blanchet@35865
    44
    hol_clause list * hol_clause list * hol_clause list * hol_clause list *
blanchet@36402
    45
    classrel_clause list * arity_clause list -> name_pool option * int
wenzelm@24311
    46
end
mengj@17998
    47
blanchet@35826
    48
structure Sledgehammer_HOL_Clause : SLEDGEHAMMER_HOL_CLAUSE =
mengj@17998
    49
struct
mengj@17998
    50
blanchet@36167
    51
open Sledgehammer_Util
blanchet@35865
    52
open Sledgehammer_FOL_Clause
blanchet@35865
    53
open Sledgehammer_Fact_Preprocessor
paulson@22825
    54
blanchet@37479
    55
fun min_arity_of const_min_arity c =
blanchet@37479
    56
  the_default 0 (Symtab.lookup const_min_arity c)
paulson@22064
    57
paulson@22825
    58
(*True if the constant ever appears outside of the top-level position in literals.
paulson@22825
    59
  If false, the constant always receives all of its arguments and is used as a predicate.*)
blanchet@36235
    60
fun needs_hBOOL explicit_apply const_needs_hBOOL c =
blanchet@37479
    61
  explicit_apply orelse the_default false (Symtab.lookup const_needs_hBOOL c)
paulson@22064
    62
mengj@19198
    63
mengj@17998
    64
(******************************************************)
mengj@17998
    65
(* data types for typed combinator expressions        *)
mengj@17998
    66
(******************************************************)
mengj@17998
    67
mengj@17998
    68
type axiom_name = string;
mengj@17998
    69
type polarity = bool;
blanchet@35865
    70
type hol_clause_id = int;
mengj@17998
    71
blanchet@35865
    72
datatype combterm =
blanchet@36170
    73
  CombConst of name * fol_type * fol_type list (* Const and Free *) |
blanchet@36170
    74
  CombVar of name * fol_type |
blanchet@35865
    75
  CombApp of combterm * combterm
wenzelm@24311
    76
mengj@17998
    77
datatype literal = Literal of polarity * combterm;
mengj@17998
    78
blanchet@35865
    79
datatype hol_clause =
blanchet@35865
    80
  HOLClause of {clause_id: hol_clause_id, axiom_name: axiom_name, th: thm,
blanchet@35865
    81
                kind: kind, literals: literal list, ctypes_sorts: typ list};
mengj@17998
    82
mengj@17998
    83
mengj@17998
    84
(*********************************************************************)
mengj@17998
    85
(* convert a clause with type Term.term to a clause with type clause *)
mengj@17998
    86
(*********************************************************************)
mengj@17998
    87
blanchet@36170
    88
fun isFalse (Literal (pol, CombConst ((c, _), _, _))) =
blanchet@35865
    89
      (pol andalso c = "c_False") orelse (not pol andalso c = "c_True")
mengj@17998
    90
  | isFalse _ = false;
mengj@17998
    91
blanchet@36170
    92
fun isTrue (Literal (pol, CombConst ((c, _), _, _))) =
mengj@17998
    93
      (pol andalso c = "c_True") orelse
mengj@17998
    94
      (not pol andalso c = "c_False")
mengj@17998
    95
  | isTrue _ = false;
wenzelm@24311
    96
blanchet@35865
    97
fun isTaut (HOLClause {literals,...}) = exists isTrue literals;
mengj@17998
    98
blanchet@37498
    99
fun type_of (Type (a, Ts)) =
blanchet@37498
   100
    let val (folTypes,ts) = types_of Ts in
blanchet@37498
   101
      (TyConstr (`make_fixed_type_const a, folTypes), ts)
blanchet@36169
   102
    end
blanchet@37498
   103
  | type_of (tp as TFree (a, _)) = (TyFree (`make_fixed_type_var a), [tp])
blanchet@37498
   104
  | type_of (tp as TVar (x, _)) =
blanchet@36169
   105
    (TyVar (make_schematic_type_var x, string_of_indexname x), [tp])
blanchet@37498
   106
and types_of Ts =
blanchet@37498
   107
    let val (folTyps, ts) = ListPair.unzip (map type_of Ts) in
blanchet@37498
   108
      (folTyps, union_all ts)
blanchet@37498
   109
    end
mengj@17998
   110
mengj@17998
   111
(* same as above, but no gathering of sort information *)
blanchet@37498
   112
fun simp_type_of (Type (a, Ts)) =
blanchet@37498
   113
      TyConstr (`make_fixed_type_const a, map simp_type_of Ts)
blanchet@37498
   114
  | simp_type_of (TFree (a, _)) = TyFree (`make_fixed_type_var a)
blanchet@37498
   115
  | simp_type_of (TVar (x, _)) =
blanchet@37498
   116
    TyVar (make_schematic_type_var x, string_of_indexname x)
mengj@18440
   117
mengj@18356
   118
mengj@17998
   119
(* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
blanchet@37498
   120
fun combterm_of thy (Const (c, T)) =
blanchet@37399
   121
      let
blanchet@37498
   122
        val (tp, ts) = type_of T
blanchet@37399
   123
        val tvar_list =
blanchet@37410
   124
          (if String.isPrefix skolem_theory_name c then
blanchet@37399
   125
             [] |> Term.add_tvarsT T |> map TVar
blanchet@37399
   126
           else
blanchet@37399
   127
             (c, T) |> Sign.const_typargs thy)
blanchet@37498
   128
          |> map simp_type_of
blanchet@37498
   129
        val c' = CombConst (`make_fixed_const c, tp, tvar_list)
paulson@22078
   130
      in  (c',ts)  end
blanchet@37498
   131
  | combterm_of _ (Free(v, T)) =
blanchet@37498
   132
      let val (tp,ts) = type_of T
blanchet@36170
   133
          val v' = CombConst (`make_fixed_var v, tp, [])
paulson@22078
   134
      in  (v',ts)  end
blanchet@37498
   135
  | combterm_of _ (Var(v, T)) =
blanchet@37498
   136
      let val (tp,ts) = type_of T
blanchet@36170
   137
          val v' = CombVar ((make_schematic_var v, string_of_indexname v), tp)
paulson@22078
   138
      in  (v',ts)  end
blanchet@37498
   139
  | combterm_of thy (P $ Q) =
blanchet@37498
   140
      let val (P', tsP) = combterm_of thy P
blanchet@37498
   141
          val (Q', tsQ) = combterm_of thy Q
blanchet@37498
   142
      in  (CombApp (P', Q'), union (op =) tsP tsQ)  end
blanchet@37498
   143
  | combterm_of _ (t as Abs _) = raise CLAUSE ("HOL clause", t)
mengj@17998
   144
blanchet@37498
   145
fun predicate_of thy ((@{const Not} $ P), polarity) =
blanchet@37498
   146
    predicate_of thy (P, not polarity)
blanchet@37498
   147
  | predicate_of thy (t, polarity) =
blanchet@37498
   148
    (combterm_of thy (Envir.eta_contract t), polarity)
mengj@17998
   149
blanchet@37498
   150
fun literals_of_term1 args thy (@{const Trueprop} $ P) =
blanchet@37498
   151
    literals_of_term1 args thy P
blanchet@37498
   152
  | literals_of_term1 args thy (@{const "op |"} $ P $ Q) =
blanchet@37498
   153
    literals_of_term1 (literals_of_term1 args thy P) thy Q
blanchet@37498
   154
  | literals_of_term1 (lits, ts) thy P =
blanchet@37498
   155
    let val ((pred, ts'), pol) = predicate_of thy (P, true) in
blanchet@37498
   156
      (Literal (pol, pred) :: lits, union (op =) ts ts')
blanchet@37498
   157
    end
blanchet@37498
   158
val literals_of_term = literals_of_term1 ([], [])
mengj@17998
   159
blanchet@37399
   160
fun skolem_name i j num_T_args =
blanchet@37399
   161
  skolem_prefix ^ (space_implode "_" (map Int.toString [i, j, num_T_args])) ^
blanchet@37399
   162
  skolem_infix ^ "g"
blanchet@37399
   163
blanchet@37410
   164
fun conceal_skolem_somes i skolem_somes t =
blanchet@37410
   165
  if exists_Const (curry (op =) @{const_name skolem_id} o fst) t then
blanchet@37399
   166
    let
blanchet@37399
   167
      fun aux skolem_somes
blanchet@37410
   168
              (t as (Const (@{const_name skolem_id}, Type (_, [_, T])) $ _)) =
blanchet@37399
   169
          let
blanchet@37496
   170
            val t = Envir.beta_eta_contract t
blanchet@37399
   171
            val (skolem_somes, s) =
blanchet@37399
   172
              if i = ~1 then
blanchet@37399
   173
                (skolem_somes, @{const_name undefined})
blanchet@37410
   174
              else case AList.find (op aconv) skolem_somes t of
blanchet@37399
   175
                s :: _ => (skolem_somes, s)
blanchet@37436
   176
              | [] =>
blanchet@37399
   177
                let
blanchet@37410
   178
                  val s = skolem_theory_name ^ "." ^
blanchet@37399
   179
                          skolem_name i (length skolem_somes)
blanchet@37399
   180
                                        (length (Term.add_tvarsT T []))
blanchet@37410
   181
                in ((s, t) :: skolem_somes, s) end
blanchet@37410
   182
          in (skolem_somes, Const (s, T)) end
blanchet@37399
   183
        | aux skolem_somes (t1 $ t2) =
blanchet@37399
   184
          let
blanchet@37399
   185
            val (skolem_somes, t1) = aux skolem_somes t1
blanchet@37399
   186
            val (skolem_somes, t2) = aux skolem_somes t2
blanchet@37399
   187
          in (skolem_somes, t1 $ t2) end
blanchet@37399
   188
        | aux skolem_somes (Abs (s, T, t')) =
blanchet@37399
   189
          let val (skolem_somes, t') = aux skolem_somes t' in
blanchet@37399
   190
            (skolem_somes, Abs (s, T, t'))
blanchet@37399
   191
          end
blanchet@37399
   192
        | aux skolem_somes t = (skolem_somes, t)
blanchet@37399
   193
    in aux skolem_somes t end
blanchet@37399
   194
  else
blanchet@37399
   195
    (skolem_somes, t)
blanchet@37399
   196
blanchet@35865
   197
(* Trivial problem, which resolution cannot handle (empty clause) *)
blanchet@35865
   198
exception TRIVIAL;
wenzelm@28835
   199
mengj@17998
   200
(* making axiom and conjecture clauses *)
blanchet@37498
   201
fun make_clause thy (clause_id, axiom_name, kind, th) skolem_somes =
blanchet@37399
   202
  let
blanchet@37399
   203
    val (skolem_somes, t) =
blanchet@37410
   204
      th |> prop_of |> conceal_skolem_somes clause_id skolem_somes
blanchet@37498
   205
    val (lits, ctypes_sorts) = literals_of_term thy t
blanchet@37399
   206
  in
blanchet@37399
   207
    if forall isFalse lits then
blanchet@37399
   208
      raise TRIVIAL
blanchet@37399
   209
    else
blanchet@37399
   210
      (skolem_somes,
blanchet@37399
   211
       HOLClause {clause_id = clause_id, axiom_name = axiom_name, th = th,
blanchet@37399
   212
                  kind = kind, literals = lits, ctypes_sorts = ctypes_sorts})
blanchet@36218
   213
  end
mengj@19354
   214
blanchet@37498
   215
fun add_axiom_clause thy (th, (name, id)) (skolem_somes, clss) =
blanchet@37399
   216
  let
blanchet@37498
   217
    val (skolem_somes, cls) = make_clause thy (id, name, Axiom, th) skolem_somes
blanchet@37399
   218
  in (skolem_somes, clss |> not (isTaut cls) ? cons (name, cls)) end
blanchet@37399
   219
blanchet@37498
   220
fun make_axiom_clauses thy clauses =
blanchet@37498
   221
  ([], []) |> fold (add_axiom_clause thy) clauses |> snd
mengj@19354
   222
blanchet@37498
   223
fun make_conjecture_clauses thy =
blanchet@37399
   224
  let
blanchet@37399
   225
    fun aux _ _ [] = []
blanchet@37399
   226
      | aux n skolem_somes (th :: ths) =
blanchet@37399
   227
        let
blanchet@37399
   228
          val (skolem_somes, cls) =
blanchet@37498
   229
            make_clause thy (n, "conjecture", Conjecture, th) skolem_somes
blanchet@37399
   230
        in cls :: aux (n + 1) skolem_somes ths end
blanchet@37399
   231
  in aux 0 [] end
mengj@17998
   232
mengj@17998
   233
(**********************************************************************)
blanchet@37498
   234
(* convert clause into TPTP format                                    *)
mengj@17998
   235
(**********************************************************************)
mengj@17998
   236
paulson@22078
   237
(*Result of a function type; no need to check that the argument type matches.*)
blanchet@36909
   238
fun result_type (TyConstr (_, [_, tp2])) = tp2
blanchet@36909
   239
  | result_type _ = raise Fail "non-function type"
paulson@22078
   240
wenzelm@32994
   241
fun type_of_combterm (CombConst (_, tp, _)) = tp
wenzelm@32994
   242
  | type_of_combterm (CombVar (_, tp)) = tp
wenzelm@32994
   243
  | type_of_combterm (CombApp (t1, _)) = result_type (type_of_combterm t1);
mengj@17998
   244
paulson@22064
   245
(*gets the head of a combinator application, along with the list of arguments*)
blanchet@35865
   246
fun strip_combterm_comb u =
paulson@22078
   247
    let fun stripc (CombApp(t,u), ts) = stripc (t, u::ts)
paulson@22064
   248
        |   stripc  x =  x
paulson@22064
   249
    in  stripc(u,[])  end;
paulson@22064
   250
blanchet@36909
   251
val type_wrapper_name = "ti"
paulson@22851
   252
blanchet@36235
   253
fun head_needs_hBOOL explicit_apply const_needs_hBOOL
blanchet@36235
   254
                     (CombConst ((c, _), _, _)) =
blanchet@36235
   255
    needs_hBOOL explicit_apply const_needs_hBOOL c
blanchet@36235
   256
  | head_needs_hBOOL _ _ _ = true
paulson@22851
   257
blanchet@36170
   258
fun wrap_type full_types (s, ty) pool =
blanchet@36170
   259
  if full_types then
blanchet@36170
   260
    let val (s', pool) = string_of_fol_type ty pool in
blanchet@36909
   261
      (type_wrapper_name ^ paren_pack [s, s'], pool)
blanchet@36170
   262
    end
blanchet@36170
   263
  else
blanchet@36170
   264
    (s, pool)
blanchet@36170
   265
blanchet@36235
   266
fun wrap_type_if full_types explicit_apply cnh (head, s, tp) =
blanchet@36235
   267
  if head_needs_hBOOL explicit_apply cnh head then
blanchet@36235
   268
    wrap_type full_types (s, tp)
blanchet@36235
   269
  else
blanchet@36235
   270
    pair s
wenzelm@24311
   271
blanchet@35865
   272
fun apply ss = "hAPP" ^ paren_pack ss;
paulson@22851
   273
paulson@22064
   274
fun rev_apply (v, []) = v
paulson@22851
   275
  | rev_apply (v, arg::args) = apply [rev_apply (v, args), arg];
paulson@22064
   276
paulson@22064
   277
fun string_apply (v, args) = rev_apply (v, rev args);
paulson@22064
   278
blanchet@36170
   279
(* Apply an operator to the argument strings, using either the "apply" operator
blanchet@36170
   280
   or direct function application. *)
blanchet@36170
   281
fun string_of_application full_types cma (CombConst ((s, s'), _, tvars), args)
blanchet@36170
   282
                          pool =
blanchet@36170
   283
    let
blanchet@36170
   284
      val s = if s = "equal" then "c_fequal" else s
blanchet@36170
   285
      val nargs = min_arity_of cma s
blanchet@36170
   286
      val args1 = List.take (args, nargs)
blanchet@36170
   287
        handle Subscript =>
blanchet@36170
   288
               raise Fail (quote s ^ " has arity " ^ Int.toString nargs ^
blanchet@36170
   289
                           " but is applied to " ^ commas (map quote args))
blanchet@36170
   290
      val args2 = List.drop (args, nargs)
blanchet@36170
   291
      val (targs, pool) = if full_types then ([], pool)
blanchet@36170
   292
                          else pool_map string_of_fol_type tvars pool
blanchet@36170
   293
      val (s, pool) = nice_name (s, s') pool
blanchet@36170
   294
    in (string_apply (s ^ paren_pack (args1 @ targs), args2), pool) end
blanchet@36170
   295
  | string_of_application _ _ (CombVar (name, _), args) pool =
blanchet@36170
   296
    nice_name name pool |>> (fn s => string_apply (s, args))
wenzelm@24311
   297
blanchet@36235
   298
fun string_of_combterm (params as (full_types, explicit_apply, cma, cnh)) t
blanchet@36235
   299
                       pool =
blanchet@36170
   300
  let
blanchet@36170
   301
    val (head, args) = strip_combterm_comb t
blanchet@36170
   302
    val (ss, pool) = pool_map (string_of_combterm params) args pool
blanchet@36170
   303
    val (s, pool) = string_of_application full_types cma (head, ss) pool
blanchet@36235
   304
  in
blanchet@36235
   305
    wrap_type_if full_types explicit_apply cnh (head, s, type_of_combterm t)
blanchet@36235
   306
                 pool
blanchet@36235
   307
  end
mengj@18356
   308
paulson@22064
   309
(*Boolean-valued terms are here converted to literals.*)
blanchet@36170
   310
fun boolify params c =
blanchet@36170
   311
  string_of_combterm params c #>> prefix "hBOOL" o paren_pack o single
paulson@22064
   312
blanchet@36235
   313
fun string_of_predicate (params as (_, explicit_apply, _, cnh)) t =
blanchet@37498
   314
  case #1 (strip_combterm_comb t) of
blanchet@37498
   315
    CombConst ((s, _), _, _) =>
blanchet@37498
   316
    (if needs_hBOOL explicit_apply cnh s then boolify else string_of_combterm)
blanchet@37498
   317
        params t
blanchet@37498
   318
  | _ => boolify params t
mengj@18356
   319
mengj@17998
   320
blanchet@36170
   321
(*** TPTP format ***)
mengj@19720
   322
blanchet@36170
   323
fun tptp_of_equality params pos (t1, t2) =
blanchet@36170
   324
  pool_map (string_of_combterm params) [t1, t2]
blanchet@36170
   325
  #>> space_implode (if pos then " = " else " != ")
wenzelm@24311
   326
blanchet@36170
   327
fun tptp_literal params
blanchet@36170
   328
        (Literal (pos, CombApp (CombApp (CombConst (("equal", _), _, _), t1),
blanchet@36170
   329
                                         t2))) =
blanchet@36170
   330
    tptp_of_equality params pos (t1, t2)
blanchet@36170
   331
  | tptp_literal params (Literal (pos, pred)) =
blanchet@36170
   332
    string_of_predicate params pred #>> tptp_sign pos
blanchet@36170
   333
 
blanchet@36170
   334
(* Given a clause, returns its literals paired with a list of literals
blanchet@36170
   335
   concerning TFrees; the latter should only occur in conjecture clauses. *)
blanchet@36556
   336
fun tptp_type_literals params pos (HOLClause {literals, ctypes_sorts, ...})
blanchet@36556
   337
                       pool =
blanchet@36556
   338
  let
blanchet@36556
   339
    val (lits, pool) = pool_map (tptp_literal params) literals pool
blanchet@36556
   340
    val (tylits, pool) = pool_map (tptp_of_type_literal pos)
blanchet@36966
   341
                                  (type_literals_for_types ctypes_sorts) pool
blanchet@36556
   342
  in ((lits, tylits), pool) end
wenzelm@24311
   343
blanchet@36170
   344
fun tptp_clause params (cls as HOLClause {axiom_name, clause_id, kind, ...})
blanchet@36170
   345
                pool =
blanchet@37498
   346
  let
blanchet@36170
   347
    val ((lits, tylits), pool) =
blanchet@36170
   348
      tptp_type_literals params (kind = Conjecture) cls pool
paulson@24937
   349
  in
blanchet@36170
   350
    ((gen_tptp_cls (clause_id, axiom_name, kind, lits, tylits), tylits), pool)
blanchet@36170
   351
  end
mengj@17998
   352
mengj@17998
   353
mengj@18440
   354
(**********************************************************************)
mengj@19198
   355
(* write clauses to files                                             *)
mengj@19198
   356
(**********************************************************************)
mengj@19198
   357
blanchet@36218
   358
fun count_combterm (CombConst ((c, _), _, _)) =
blanchet@36218
   359
    Symtab.map_entry c (Integer.add 1)
blanchet@36218
   360
  | count_combterm (CombVar _) = I
blanchet@36218
   361
  | count_combterm (CombApp (t1, t2)) = count_combterm t1 #> count_combterm t2
paulson@21573
   362
blanchet@36218
   363
fun count_literal (Literal (_, t)) = count_combterm t
paulson@21573
   364
blanchet@36218
   365
fun count_clause (HOLClause {literals, ...}) = fold count_literal literals
paulson@21573
   366
blanchet@37479
   367
val raw_cnf_rules_pairs = map (fn (name, thm) => (thm, (name, 0)))
blanchet@37479
   368
fun cnf_helper_thms thy raw =
blanchet@37479
   369
  map (`Thm.get_name_hint)
blanchet@37479
   370
  #> (if raw then raw_cnf_rules_pairs else cnf_rules_pairs thy)
blanchet@37479
   371
blanchet@37479
   372
val optional_helpers =
blanchet@37479
   373
  [(["c_COMBI", "c_COMBK"], (false, @{thms COMBI_def COMBK_def})),
blanchet@37479
   374
   (["c_COMBB", "c_COMBC"], (false, @{thms COMBB_def COMBC_def})),
blanchet@37479
   375
   (["c_COMBS"], (false, @{thms COMBS_def}))]
blanchet@37479
   376
val optional_typed_helpers =
blanchet@37479
   377
  [(["c_True", "c_False"], (true, @{thms True_or_False})),
blanchet@37479
   378
   (["c_If"], (true, @{thms if_True if_False True_or_False}))]
blanchet@37479
   379
val mandatory_helpers = @{thms fequal_imp_equal equal_imp_fequal}
mengj@20644
   380
blanchet@37479
   381
val init_counters =
blanchet@37479
   382
  Symtab.make (maps (maps (map (rpair 0) o fst))
blanchet@37479
   383
                    [optional_helpers, optional_typed_helpers])
blanchet@37479
   384
blanchet@37498
   385
fun get_helper_clauses thy is_FO full_types conjectures axcls =
blanchet@37479
   386
  let
blanchet@37498
   387
    val axclauses = map snd (make_axiom_clauses thy axcls)
blanchet@37479
   388
    val ct = fold (fold count_clause) [conjectures, axclauses] init_counters
blanchet@37479
   389
    fun is_needed c = the (Symtab.lookup ct c) > 0
blanchet@37479
   390
    val cnfs =
blanchet@37479
   391
      (optional_helpers
blanchet@37479
   392
       |> full_types ? append optional_typed_helpers
blanchet@37479
   393
       |> maps (fn (ss, (raw, ths)) =>
blanchet@37479
   394
                   if exists is_needed ss then cnf_helper_thms thy raw ths
blanchet@37479
   395
                   else []))
blanchet@37479
   396
      @ (if is_FO then [] else cnf_helper_thms thy false mandatory_helpers)
blanchet@37498
   397
  in map snd (make_axiom_clauses thy cnfs) end
mengj@20791
   398
paulson@22064
   399
(*Find the minimal arity of each function mentioned in the term. Also, note which uses
paulson@22064
   400
  are not at top level, to see if hBOOL is needed.*)
immler@30150
   401
fun count_constants_term toplev t (const_min_arity, const_needs_hBOOL) =
blanchet@35865
   402
  let val (head, args) = strip_combterm_comb t
paulson@22064
   403
      val n = length args
blanchet@36233
   404
      val (const_min_arity, const_needs_hBOOL) =
blanchet@36233
   405
        (const_min_arity, const_needs_hBOOL)
blanchet@36233
   406
        |> fold (count_constants_term false) args
paulson@22064
   407
  in
paulson@22064
   408
      case head of
blanchet@36170
   409
          CombConst ((a, _),_,_) => (*predicate or function version of "equal"?*)
wenzelm@24311
   410
            let val a = if a="equal" andalso not toplev then "c_fequal" else a
wenzelm@24311
   411
            in
blanchet@36233
   412
              (const_min_arity |> Symtab.map_default (a, n) (Integer.min n),
blanchet@36233
   413
               const_needs_hBOOL |> not toplev ? Symtab.update (a, true))
wenzelm@24311
   414
            end
wenzelm@32994
   415
        | _ => (const_min_arity, const_needs_hBOOL)
paulson@22064
   416
  end;
paulson@22064
   417
paulson@22064
   418
(*A literal is a top-level term*)
immler@30150
   419
fun count_constants_lit (Literal (_,t)) (const_min_arity, const_needs_hBOOL) =
immler@30150
   420
  count_constants_term true t (const_min_arity, const_needs_hBOOL);
paulson@22064
   421
blanchet@35865
   422
fun count_constants_clause (HOLClause {literals, ...})
blanchet@35865
   423
                           (const_min_arity, const_needs_hBOOL) =
immler@30150
   424
  fold count_constants_lit literals (const_min_arity, const_needs_hBOOL);
paulson@22064
   425
blanchet@37498
   426
fun display_arity explicit_apply const_needs_hBOOL (c, n) =
blanchet@35865
   427
  trace_msg (fn () => "Constant: " ^ c ^
blanchet@35826
   428
                " arity:\t" ^ Int.toString n ^
blanchet@36235
   429
                (if needs_hBOOL explicit_apply const_needs_hBOOL c then
blanchet@36235
   430
                   " needs hBOOL"
blanchet@36235
   431
                 else
blanchet@36235
   432
                   ""))
paulson@22064
   433
blanchet@36235
   434
fun count_constants explicit_apply
blanchet@36235
   435
                    (conjectures, _, extra_clauses, helper_clauses, _, _) =
blanchet@36235
   436
  if not explicit_apply then
immler@30150
   437
     let val (const_min_arity, const_needs_hBOOL) =
immler@30150
   438
          fold count_constants_clause conjectures (Symtab.empty, Symtab.empty)
immler@31865
   439
       |> fold count_constants_clause extra_clauses
immler@30149
   440
       |> fold count_constants_clause helper_clauses
blanchet@36481
   441
     val _ = app (display_arity explicit_apply const_needs_hBOOL)
blanchet@36481
   442
                 (Symtab.dest (const_min_arity))
immler@30150
   443
     in (const_min_arity, const_needs_hBOOL) end
immler@30150
   444
  else (Symtab.empty, Symtab.empty);
paulson@22064
   445
blanchet@35865
   446
(* TPTP format *)
immler@31749
   447
blanchet@36235
   448
fun write_tptp_file readable_names full_types explicit_apply file clauses =
immler@31409
   449
  let
blanchet@36167
   450
    fun section _ [] = []
blanchet@36393
   451
      | section name ss =
blanchet@36393
   452
        "\n% " ^ name ^ plural_s (length ss) ^ " (" ^ Int.toString (length ss) ^
blanchet@36393
   453
        ")\n" :: ss
blanchet@36222
   454
    val pool = empty_name_pool readable_names
immler@31865
   455
    val (conjectures, axclauses, _, helper_clauses,
immler@31865
   456
      classrel_clauses, arity_clauses) = clauses
blanchet@36235
   457
    val (cma, cnh) = count_constants explicit_apply clauses
blanchet@36235
   458
    val params = (full_types, explicit_apply, cma, cnh)
blanchet@36170
   459
    val ((conjecture_clss, tfree_litss), pool) =
blanchet@36170
   460
      pool_map (tptp_clause params) conjectures pool |>> ListPair.unzip
blanchet@36218
   461
    val tfree_clss = map tptp_tfree_clause (fold (union (op =)) tfree_litss [])
blanchet@36170
   462
    val (ax_clss, pool) = pool_map (apfst fst oo tptp_clause params) axclauses
blanchet@36170
   463
                                   pool
blanchet@36170
   464
    val classrel_clss = map tptp_classrel_clause classrel_clauses
blanchet@36170
   465
    val arity_clss = map tptp_arity_clause arity_clauses
blanchet@36170
   466
    val (helper_clss, pool) = pool_map (apfst fst oo tptp_clause params)
blanchet@36170
   467
                                       helper_clauses pool
blanchet@36402
   468
    val conjecture_offset =
blanchet@36402
   469
      length ax_clss + length classrel_clss + length arity_clss
blanchet@36402
   470
      + length helper_clss
blanchet@36393
   471
    val _ =
blanchet@36393
   472
      File.write_list file
blanchet@37498
   473
          ("% This file was generated by Isabelle (most likely Sledgehammer)\n\
blanchet@37498
   474
           \% " ^ timestamp () ^ "\n" ::
blanchet@36393
   475
           section "Relevant fact" ax_clss @
blanchet@36402
   476
           section "Class relationship" classrel_clss @
blanchet@36402
   477
           section "Arity declaration" arity_clss @
blanchet@36393
   478
           section "Helper fact" helper_clss @
blanchet@36393
   479
           section "Conjecture" conjecture_clss @
blanchet@36402
   480
           section "Type variable" tfree_clss)
blanchet@36402
   481
  in (pool, conjecture_offset) end
mengj@19720
   482
wenzelm@33311
   483
end;