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