src/HOL/Tools/Sledgehammer/sledgehammer_hol_clause.ML
author blanchet
Tue Apr 20 16:04:36 2010 +0200 (2010-04-20 ago)
changeset 36235 61159615a0c5
parent 36233 1263bef003b3
child 36237 86e62a98deea
permissions -rw-r--r--
added "explicit_apply" option to Sledgehammer, to control whether an explicit apply function should be used as much or as little as possible (replaces a previous global variable)
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@36170
     9
  type name = Sledgehammer_FOL_Clause.name
blanchet@35865
    10
  type kind = Sledgehammer_FOL_Clause.kind
blanchet@35865
    11
  type fol_type = Sledgehammer_FOL_Clause.fol_type
blanchet@35865
    12
  type classrel_clause = Sledgehammer_FOL_Clause.classrel_clause
blanchet@35865
    13
  type arity_clause = Sledgehammer_FOL_Clause.arity_clause
wenzelm@24311
    14
  type axiom_name = string
wenzelm@24311
    15
  type polarity = bool
blanchet@35865
    16
  type hol_clause_id = int
blanchet@35865
    17
wenzelm@24311
    18
  datatype combterm =
blanchet@36170
    19
    CombConst of name * fol_type * fol_type list (* Const and Free *) |
blanchet@36170
    20
    CombVar of name * fol_type |
blanchet@35865
    21
    CombApp of combterm * combterm
wenzelm@24311
    22
  datatype literal = Literal of polarity * combterm
blanchet@35865
    23
  datatype hol_clause =
blanchet@35865
    24
    HOLClause of {clause_id: hol_clause_id, axiom_name: axiom_name, th: thm,
blanchet@35865
    25
                  kind: kind, literals: literal list, ctypes_sorts: typ list}
blanchet@35865
    26
blanchet@35865
    27
  val type_of_combterm : combterm -> fol_type
blanchet@35865
    28
  val strip_combterm_comb : combterm -> combterm * combterm list
blanchet@35865
    29
  val literals_of_term : theory -> term -> literal list * typ list
blanchet@35865
    30
  exception TRIVIAL
blanchet@35865
    31
  val make_conjecture_clauses : bool -> theory -> thm list -> hol_clause list
blanchet@35865
    32
  val make_axiom_clauses : bool -> theory ->
blanchet@35865
    33
       (thm * (axiom_name * hol_clause_id)) list -> (axiom_name * hol_clause) list
blanchet@35865
    34
  val get_helper_clauses : bool -> theory -> bool ->
blanchet@35865
    35
       hol_clause list * (thm * (axiom_name * hol_clause_id)) list * string list ->
blanchet@35865
    36
       hol_clause list
blanchet@36235
    37
  val write_tptp_file : bool -> bool -> bool -> Path.T ->
blanchet@35865
    38
    hol_clause list * hol_clause list * hol_clause list * hol_clause list *
blanchet@36231
    39
    classrel_clause list * arity_clause list -> unit
blanchet@36235
    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@36231
    42
    classrel_clause list * arity_clause list -> unit
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
blanchet@36235
    53
   exported.
paulson@22825
    54
blanchet@36235
    55
   If "explicit_apply" is false, each function will be directly applied to as
blanchet@36235
    56
   many arguments as possible, avoiding use of the "apply" operator. Use of
blanchet@36235
    57
   hBOOL is also minimized.
blanchet@36235
    58
*)
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.*)
blanchet@36235
    64
fun needs_hBOOL explicit_apply const_needs_hBOOL c =
blanchet@36235
    65
  explicit_apply 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@36170
    78
  CombConst of name * fol_type * fol_type list (* Const and Free *) |
blanchet@36170
    79
  CombVar of name * 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
blanchet@36170
    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
blanchet@36170
    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@36170
   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@36170
   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@36170
   138
          val v' = CombVar ((make_schematic_var v, string_of_indexname 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
blanchet@36218
   176
fun add_axiom_clause dfg thy (th, (name, id)) =
blanchet@36218
   177
  let val cls = make_clause dfg thy (id, name, Axiom, th) in
blanchet@36218
   178
    not (isTaut cls) ? cons (name, cls)
blanchet@36218
   179
  end
mengj@19354
   180
blanchet@36218
   181
fun make_axiom_clauses dfg thy clauses =
blanchet@36218
   182
  fold (add_axiom_clause dfg thy) clauses []
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
blanchet@36235
   214
fun head_needs_hBOOL explicit_apply const_needs_hBOOL
blanchet@36235
   215
                     (CombConst ((c, _), _, _)) =
blanchet@36235
   216
    needs_hBOOL explicit_apply const_needs_hBOOL c
blanchet@36235
   217
  | head_needs_hBOOL _ _ _ = true
paulson@22851
   218
blanchet@36170
   219
fun wrap_type full_types (s, ty) pool =
blanchet@36170
   220
  if full_types then
blanchet@36170
   221
    let val (s', pool) = string_of_fol_type ty pool in
blanchet@36170
   222
      (type_wrapper ^ paren_pack [s, s'], pool)
blanchet@36170
   223
    end
blanchet@36170
   224
  else
blanchet@36170
   225
    (s, pool)
blanchet@36170
   226
blanchet@36235
   227
fun wrap_type_if full_types explicit_apply cnh (head, s, tp) =
blanchet@36235
   228
  if head_needs_hBOOL explicit_apply cnh head then
blanchet@36235
   229
    wrap_type full_types (s, tp)
blanchet@36235
   230
  else
blanchet@36235
   231
    pair s
wenzelm@24311
   232
blanchet@35865
   233
fun apply ss = "hAPP" ^ paren_pack ss;
paulson@22851
   234
paulson@22064
   235
fun rev_apply (v, []) = v
paulson@22851
   236
  | rev_apply (v, arg::args) = apply [rev_apply (v, args), arg];
paulson@22064
   237
paulson@22064
   238
fun string_apply (v, args) = rev_apply (v, rev args);
paulson@22064
   239
blanchet@36170
   240
(* Apply an operator to the argument strings, using either the "apply" operator
blanchet@36170
   241
   or direct function application. *)
blanchet@36170
   242
fun string_of_application full_types cma (CombConst ((s, s'), _, tvars), args)
blanchet@36170
   243
                          pool =
blanchet@36170
   244
    let
blanchet@36170
   245
      val s = if s = "equal" then "c_fequal" else s
blanchet@36170
   246
      val nargs = min_arity_of cma s
blanchet@36170
   247
      val args1 = List.take (args, nargs)
blanchet@36170
   248
        handle Subscript =>
blanchet@36170
   249
               raise Fail (quote s ^ " has arity " ^ Int.toString nargs ^
blanchet@36170
   250
                           " but is applied to " ^ commas (map quote args))
blanchet@36170
   251
      val args2 = List.drop (args, nargs)
blanchet@36170
   252
      val (targs, pool) = if full_types then ([], pool)
blanchet@36170
   253
                          else pool_map string_of_fol_type tvars pool
blanchet@36170
   254
      val (s, pool) = nice_name (s, s') pool
blanchet@36170
   255
    in (string_apply (s ^ paren_pack (args1 @ targs), args2), pool) end
blanchet@36170
   256
  | string_of_application _ _ (CombVar (name, _), args) pool =
blanchet@36170
   257
    nice_name name pool |>> (fn s => string_apply (s, args))
wenzelm@24311
   258
blanchet@36235
   259
fun string_of_combterm (params as (full_types, explicit_apply, cma, cnh)) t
blanchet@36235
   260
                       pool =
blanchet@36170
   261
  let
blanchet@36170
   262
    val (head, args) = strip_combterm_comb t
blanchet@36170
   263
    val (ss, pool) = pool_map (string_of_combterm params) args pool
blanchet@36170
   264
    val (s, pool) = string_of_application full_types cma (head, ss) pool
blanchet@36235
   265
  in
blanchet@36235
   266
    wrap_type_if full_types explicit_apply cnh (head, s, type_of_combterm t)
blanchet@36235
   267
                 pool
blanchet@36235
   268
  end
mengj@18356
   269
paulson@22064
   270
(*Boolean-valued terms are here converted to literals.*)
blanchet@36170
   271
fun boolify params c =
blanchet@36170
   272
  string_of_combterm params c #>> prefix "hBOOL" o paren_pack o single
paulson@22064
   273
blanchet@36235
   274
fun string_of_predicate (params as (_, explicit_apply, _, cnh)) t =
paulson@22064
   275
  case t of
blanchet@36170
   276
    (CombApp (CombApp (CombConst (("equal", _), _, _), t1), t2)) =>
blanchet@36170
   277
    (* DFG only: new TPTP prefers infix equality *)
blanchet@36170
   278
    pool_map (string_of_combterm params) [t1, t2]
blanchet@36170
   279
    #>> prefix "equal" o paren_pack
blanchet@36170
   280
  | _ =>
blanchet@36170
   281
    case #1 (strip_combterm_comb t) of
blanchet@36170
   282
      CombConst ((s, _), _, _) =>
blanchet@36235
   283
      (if needs_hBOOL explicit_apply cnh s then boolify else string_of_combterm)
blanchet@36235
   284
          params t
blanchet@36170
   285
    | _ => boolify params t
mengj@18356
   286
mengj@17998
   287
blanchet@36170
   288
(*** TPTP format ***)
mengj@19720
   289
blanchet@36170
   290
fun tptp_of_equality params pos (t1, t2) =
blanchet@36170
   291
  pool_map (string_of_combterm params) [t1, t2]
blanchet@36170
   292
  #>> space_implode (if pos then " = " else " != ")
wenzelm@24311
   293
blanchet@36170
   294
fun tptp_literal params
blanchet@36170
   295
        (Literal (pos, CombApp (CombApp (CombConst (("equal", _), _, _), t1),
blanchet@36170
   296
                                         t2))) =
blanchet@36170
   297
    tptp_of_equality params pos (t1, t2)
blanchet@36170
   298
  | tptp_literal params (Literal (pos, pred)) =
blanchet@36170
   299
    string_of_predicate params pred #>> tptp_sign pos
blanchet@36170
   300
 
blanchet@36170
   301
(* Given a clause, returns its literals paired with a list of literals
blanchet@36170
   302
   concerning TFrees; the latter should only occur in conjecture clauses. *)
blanchet@36170
   303
fun tptp_type_literals params pos (HOLClause {literals, ctypes_sorts, ...}) =
blanchet@36170
   304
  pool_map (tptp_literal params) literals
blanchet@36170
   305
  #>> rpair (map (tptp_of_typeLit pos) (add_typs ctypes_sorts))
wenzelm@24311
   306
blanchet@36170
   307
fun tptp_clause params (cls as HOLClause {axiom_name, clause_id, kind, ...})
blanchet@36170
   308
                pool =
blanchet@36170
   309
let
blanchet@36170
   310
    val ((lits, tylits), pool) =
blanchet@36170
   311
      tptp_type_literals params (kind = Conjecture) cls pool
paulson@24937
   312
  in
blanchet@36170
   313
    ((gen_tptp_cls (clause_id, axiom_name, kind, lits, tylits), tylits), pool)
blanchet@36170
   314
  end
mengj@17998
   315
mengj@17998
   316
blanchet@36170
   317
(*** DFG format ***)
paulson@21561
   318
blanchet@36170
   319
fun dfg_literal params (Literal (pos, pred)) =
blanchet@36170
   320
  string_of_predicate params pred #>> dfg_sign pos
mengj@19720
   321
blanchet@36170
   322
fun dfg_type_literals params pos (HOLClause {literals, ctypes_sorts, ...}) =
blanchet@36170
   323
  pool_map (dfg_literal params) literals
blanchet@36170
   324
  #>> rpair (map (dfg_of_typeLit pos) (add_typs ctypes_sorts))
mengj@19720
   325
blanchet@36170
   326
fun get_uvars (CombConst _) vars pool = (vars, pool)
blanchet@36170
   327
  | get_uvars (CombVar (name, _)) vars pool =
blanchet@36170
   328
    nice_name name pool |>> (fn var => var :: vars)
blanchet@36170
   329
  | get_uvars (CombApp (P, Q)) vars pool =
blanchet@36170
   330
    let val (vars, pool) = get_uvars P vars pool in get_uvars Q vars pool end
mengj@19720
   331
blanchet@36170
   332
fun get_uvars_l (Literal (_, c)) = get_uvars c [];
mengj@19720
   333
blanchet@36170
   334
fun dfg_vars (HOLClause {literals, ...}) =
blanchet@36170
   335
  pool_map get_uvars_l literals #>> union_all
wenzelm@24311
   336
blanchet@36170
   337
fun dfg_clause params (cls as HOLClause {axiom_name, clause_id, kind,
blanchet@36170
   338
                                         ctypes_sorts, ...}) pool =
blanchet@36170
   339
  let
blanchet@36170
   340
    val ((lits, tylits), pool) =
blanchet@36170
   341
      dfg_type_literals params (kind = Conjecture) cls pool
blanchet@36170
   342
    val (vars, pool) = dfg_vars cls pool
blanchet@36170
   343
    val tvars = get_tvar_strs ctypes_sorts
paulson@24937
   344
  in
blanchet@36170
   345
    ((gen_dfg_cls (clause_id, axiom_name, kind, lits, tylits, tvars @ vars),
blanchet@36170
   346
      tylits), pool)
blanchet@36170
   347
  end
paulson@24937
   348
mengj@19720
   349
paulson@22064
   350
(** For DFG format: accumulate function and predicate declarations **)
mengj@19720
   351
blanchet@36218
   352
fun add_types tvars = fold add_fol_type_funcs tvars
mengj@19720
   353
blanchet@36235
   354
fun add_decls (full_types, explicit_apply, cma, cnh)
blanchet@36235
   355
              (CombConst ((c, _), _, tvars)) (funcs, preds) =
blanchet@36170
   356
      if c = "equal" then
blanchet@36218
   357
        (add_types tvars funcs, preds)
paulson@21561
   358
      else
wenzelm@32960
   359
        let val arity = min_arity_of cma c
blanchet@35963
   360
            val ntys = if not full_types then length tvars else 0
wenzelm@32960
   361
            val addit = Symtab.update(c, arity+ntys)
wenzelm@32960
   362
        in
blanchet@36235
   363
            if needs_hBOOL explicit_apply cnh c then
blanchet@36235
   364
              (add_types tvars (addit funcs), preds)
blanchet@36235
   365
            else
blanchet@36235
   366
              (add_types tvars funcs, addit preds)
wenzelm@32960
   367
        end
blanchet@36218
   368
  | add_decls _ (CombVar (_, ctp)) (funcs, preds) =
blanchet@36218
   369
      (add_fol_type_funcs ctp funcs, preds)
blanchet@36218
   370
  | add_decls params (CombApp (P, Q)) decls =
blanchet@36218
   371
      decls |> add_decls params P |> add_decls params Q
mengj@19720
   372
blanchet@36218
   373
fun add_literal_decls params (Literal (_, c)) = add_decls params c
mengj@19720
   374
blanchet@36218
   375
fun add_clause_decls params (HOLClause {literals, ...}) decls =
blanchet@36218
   376
    fold (add_literal_decls params) literals decls
wenzelm@27187
   377
    handle Symtab.DUP a => error ("function " ^ a ^ " has multiple arities")
mengj@19720
   378
nipkow@31800
   379
fun decls_of_clauses params clauses arity_clauses =
blanchet@36218
   380
  let val functab = init_functab |> Symtab.update (type_wrapper, 2)
blanchet@36218
   381
                                 |> Symtab.update ("hAPP", 2)
blanchet@36218
   382
      val predtab = Symtab.empty |> Symtab.update ("hBOOL", 1)
blanchet@36218
   383
      val (functab, predtab) =
blanchet@36218
   384
        (functab, predtab) |> fold (add_clause_decls params) clauses
blanchet@36218
   385
                           |>> fold add_arity_clause_funcs arity_clauses
blanchet@36218
   386
  in (Symtab.dest functab, Symtab.dest predtab) end
mengj@19720
   387
blanchet@36218
   388
fun add_clause_preds (HOLClause {ctypes_sorts, ...}) preds =
blanchet@36218
   389
  fold add_type_sort_preds ctypes_sorts preds
wenzelm@27187
   390
  handle Symtab.DUP a => error ("predicate " ^ a ^ " has multiple arities")
paulson@21398
   391
paulson@21398
   392
(*Higher-order clauses have only the predicates hBOOL and type classes.*)
wenzelm@24311
   393
fun preds_of_clauses clauses clsrel_clauses arity_clauses =
blanchet@36218
   394
  Symtab.empty
blanchet@36218
   395
  |> fold add_clause_preds clauses
blanchet@36218
   396
  |> fold add_arity_clause_preds arity_clauses
blanchet@36218
   397
  |> fold add_classrel_clause_preds clsrel_clauses
blanchet@36218
   398
  |> Symtab.dest
mengj@18440
   399
mengj@18440
   400
(**********************************************************************)
mengj@19198
   401
(* write clauses to files                                             *)
mengj@19198
   402
(**********************************************************************)
mengj@19198
   403
paulson@21573
   404
val init_counters =
blanchet@35865
   405
  Symtab.make [("c_COMBI", 0), ("c_COMBK", 0), ("c_COMBB", 0), ("c_COMBC", 0),
blanchet@35865
   406
               ("c_COMBS", 0)];
wenzelm@24311
   407
blanchet@36218
   408
fun count_combterm (CombConst ((c, _), _, _)) =
blanchet@36218
   409
    Symtab.map_entry c (Integer.add 1)
blanchet@36218
   410
  | count_combterm (CombVar _) = I
blanchet@36218
   411
  | count_combterm (CombApp (t1, t2)) = count_combterm t1 #> count_combterm t2
paulson@21573
   412
blanchet@36218
   413
fun count_literal (Literal (_, t)) = count_combterm t
paulson@21573
   414
blanchet@36218
   415
fun count_clause (HOLClause {literals, ...}) = fold count_literal literals
paulson@21573
   416
blanchet@36218
   417
fun count_user_clause user_lemmas (HOLClause {axiom_name, literals, ...}) =
blanchet@36218
   418
  member (op =) user_lemmas axiom_name ? fold count_literal literals
paulson@21573
   419
blanchet@36228
   420
fun cnf_helper_thms thy = cnf_rules_pairs thy o map (`Thm.get_name_hint)
mengj@20644
   421
immler@31752
   422
fun get_helper_clauses dfg thy isFO (conjectures, axcls, user_lemmas) =
blanchet@35865
   423
  if isFO then
blanchet@35865
   424
    []
paulson@23386
   425
  else
immler@31752
   426
    let
immler@31752
   427
        val axclauses = map #2 (make_axiom_clauses dfg thy axcls)
blanchet@36218
   428
        val ct = init_counters |> fold count_clause conjectures
blanchet@36218
   429
                               |> fold (count_user_clause user_lemmas) axclauses
wenzelm@33035
   430
        fun needed c = the (Symtab.lookup ct c) > 0
wenzelm@24311
   431
        val IK = if needed "c_COMBI" orelse needed "c_COMBK"
blanchet@35865
   432
                 then cnf_helper_thms thy [@{thm COMBI_def}, @{thm COMBK_def}]
wenzelm@24311
   433
                 else []
wenzelm@24311
   434
        val BC = if needed "c_COMBB" orelse needed "c_COMBC"
blanchet@35865
   435
                 then cnf_helper_thms thy [@{thm COMBB_def}, @{thm COMBC_def}]
paulson@21135
   436
                 else []
blanchet@35865
   437
        val S = if needed "c_COMBS" then cnf_helper_thms thy [@{thm COMBS_def}]
wenzelm@24311
   438
                else []
blanchet@35865
   439
        val other = cnf_helper_thms thy [@{thm fequal_imp_equal},
blanchet@35865
   440
                                         @{thm equal_imp_fequal}]
mengj@20791
   441
    in
immler@30151
   442
        map #2 (make_axiom_clauses dfg thy (other @ IK @ BC @ S))
paulson@23386
   443
    end;
mengj@20791
   444
paulson@22064
   445
(*Find the minimal arity of each function mentioned in the term. Also, note which uses
paulson@22064
   446
  are not at top level, to see if hBOOL is needed.*)
immler@30150
   447
fun count_constants_term toplev t (const_min_arity, const_needs_hBOOL) =
blanchet@35865
   448
  let val (head, args) = strip_combterm_comb t
paulson@22064
   449
      val n = length args
blanchet@36233
   450
      val (const_min_arity, const_needs_hBOOL) =
blanchet@36233
   451
        (const_min_arity, const_needs_hBOOL)
blanchet@36233
   452
        |> fold (count_constants_term false) args
paulson@22064
   453
  in
paulson@22064
   454
      case head of
blanchet@36170
   455
          CombConst ((a, _),_,_) => (*predicate or function version of "equal"?*)
wenzelm@24311
   456
            let val a = if a="equal" andalso not toplev then "c_fequal" else a
wenzelm@24311
   457
            in
blanchet@36233
   458
              (const_min_arity |> Symtab.map_default (a, n) (Integer.min n),
blanchet@36233
   459
               const_needs_hBOOL |> not toplev ? Symtab.update (a, true))
wenzelm@24311
   460
            end
wenzelm@32994
   461
        | _ => (const_min_arity, const_needs_hBOOL)
paulson@22064
   462
  end;
paulson@22064
   463
paulson@22064
   464
(*A literal is a top-level term*)
immler@30150
   465
fun count_constants_lit (Literal (_,t)) (const_min_arity, const_needs_hBOOL) =
immler@30150
   466
  count_constants_term true t (const_min_arity, const_needs_hBOOL);
paulson@22064
   467
blanchet@35865
   468
fun count_constants_clause (HOLClause {literals, ...})
blanchet@35865
   469
                           (const_min_arity, const_needs_hBOOL) =
immler@30150
   470
  fold count_constants_lit literals (const_min_arity, const_needs_hBOOL);
paulson@22064
   471
blanchet@36235
   472
fun display_arity explicit_apply const_needs_hBOOL (c,n) =
blanchet@35865
   473
  trace_msg (fn () => "Constant: " ^ c ^
blanchet@35826
   474
                " arity:\t" ^ Int.toString n ^
blanchet@36235
   475
                (if needs_hBOOL explicit_apply const_needs_hBOOL c then
blanchet@36235
   476
                   " needs hBOOL"
blanchet@36235
   477
                 else
blanchet@36235
   478
                   ""))
paulson@22064
   479
blanchet@36235
   480
fun count_constants explicit_apply
blanchet@36235
   481
                    (conjectures, _, extra_clauses, helper_clauses, _, _) =
blanchet@36235
   482
  if not explicit_apply then
immler@30150
   483
     let val (const_min_arity, const_needs_hBOOL) =
immler@30150
   484
          fold count_constants_clause conjectures (Symtab.empty, Symtab.empty)
immler@31865
   485
       |> fold count_constants_clause extra_clauses
immler@30149
   486
       |> fold count_constants_clause helper_clauses
blanchet@36235
   487
     val _ = List.app (display_arity explicit_apply const_needs_hBOOL)
blanchet@36235
   488
                      (Symtab.dest (const_min_arity))
immler@30150
   489
     in (const_min_arity, const_needs_hBOOL) end
immler@30150
   490
  else (Symtab.empty, Symtab.empty);
paulson@22064
   491
blanchet@36218
   492
fun header () =
blanchet@36218
   493
  "% This file was generated by Isabelle (most likely Sledgehammer)\n" ^
blanchet@36218
   494
  "% " ^ timestamp () ^ "\n"
blanchet@36218
   495
blanchet@35865
   496
(* TPTP format *)
immler@31749
   497
blanchet@36235
   498
fun write_tptp_file readable_names full_types explicit_apply file clauses =
immler@31409
   499
  let
blanchet@36167
   500
    fun section _ [] = []
blanchet@36167
   501
      | section name ss = "\n% " ^ name ^ plural_s (length ss) ^ "\n" :: ss
blanchet@36222
   502
    val pool = empty_name_pool readable_names
immler@31865
   503
    val (conjectures, axclauses, _, helper_clauses,
immler@31865
   504
      classrel_clauses, arity_clauses) = clauses
blanchet@36235
   505
    val (cma, cnh) = count_constants explicit_apply clauses
blanchet@36235
   506
    val params = (full_types, explicit_apply, cma, cnh)
blanchet@36170
   507
    val ((conjecture_clss, tfree_litss), pool) =
blanchet@36170
   508
      pool_map (tptp_clause params) conjectures pool |>> ListPair.unzip
blanchet@36218
   509
    val tfree_clss = map tptp_tfree_clause (fold (union (op =)) tfree_litss [])
blanchet@36170
   510
    val (ax_clss, pool) = pool_map (apfst fst oo tptp_clause params) axclauses
blanchet@36170
   511
                                   pool
blanchet@36170
   512
    val classrel_clss = map tptp_classrel_clause classrel_clauses
blanchet@36170
   513
    val arity_clss = map tptp_arity_clause arity_clauses
blanchet@36170
   514
    val (helper_clss, pool) = pool_map (apfst fst oo tptp_clause params)
blanchet@36170
   515
                                       helper_clauses pool
blanchet@36231
   516
  in
blanchet@36231
   517
    File.write_list file
blanchet@36231
   518
        (header () ::
blanchet@36231
   519
         section "Relevant fact" ax_clss @
blanchet@36231
   520
         section "Type variable" tfree_clss @
blanchet@36231
   521
         section "Conjecture" conjecture_clss @
blanchet@36231
   522
         section "Class relationship" classrel_clss @
blanchet@36231
   523
         section "Arity declaration" arity_clss @
blanchet@36231
   524
         section "Helper fact" helper_clss)
blanchet@36231
   525
  end
mengj@19198
   526
mengj@19720
   527
blanchet@35865
   528
(* DFG format *)
mengj@19720
   529
blanchet@36235
   530
fun write_dfg_file full_types explicit_apply file clauses =
immler@31409
   531
  let
blanchet@36219
   532
    (* Some of the helper functions below are not name-pool-aware. However,
blanchet@36219
   533
       there's no point in adding name pool support if the DFG format is on its
blanchet@36219
   534
       way out anyway. *)
blanchet@36219
   535
    val pool = NONE
immler@31865
   536
    val (conjectures, axclauses, _, helper_clauses,
immler@31865
   537
      classrel_clauses, arity_clauses) = clauses
blanchet@36235
   538
    val (cma, cnh) = count_constants explicit_apply clauses
blanchet@36235
   539
    val params = (full_types, explicit_apply, cma, cnh)
blanchet@36170
   540
    val ((conjecture_clss, tfree_litss), pool) =
blanchet@36170
   541
      pool_map (dfg_clause params) conjectures pool |>> ListPair.unzip
immler@31838
   542
    and probname = Path.implode (Path.base file)
blanchet@36170
   543
    val (axstrs, pool) = pool_map (apfst fst oo dfg_clause params) axclauses pool
blanchet@35865
   544
    val tfree_clss = map dfg_tfree_clause (union_all tfree_litss)
blanchet@36170
   545
    val (helper_clauses_strs, pool) =
blanchet@36170
   546
      pool_map (apfst fst oo dfg_clause params) helper_clauses pool
blanchet@36218
   547
    val (funcs, cl_preds) = decls_of_clauses params (helper_clauses @ conjectures @ axclauses) arity_clauses
immler@31409
   548
    and ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
blanchet@36170
   549
  in
blanchet@36231
   550
    File.write_list file
blanchet@36231
   551
        (header () ::
blanchet@36231
   552
         string_of_start probname ::
blanchet@36231
   553
         string_of_descrip probname ::
blanchet@36231
   554
         string_of_symbols (string_of_funcs funcs)
blanchet@36231
   555
                           (string_of_preds (cl_preds @ ty_preds)) ::
blanchet@36231
   556
         "list_of_clauses(axioms, cnf).\n" ::
blanchet@36231
   557
         axstrs @
blanchet@36231
   558
         map dfg_classrel_clause classrel_clauses @
blanchet@36231
   559
         map dfg_arity_clause arity_clauses @
blanchet@36231
   560
         helper_clauses_strs @
blanchet@36231
   561
         ["end_of_list.\n\nlist_of_clauses(conjectures, cnf).\n"] @
blanchet@36231
   562
         tfree_clss @
blanchet@36231
   563
         conjecture_clss @
blanchet@36231
   564
         ["end_of_list.\n\n",
blanchet@36231
   565
          "end_problem.\n"])
blanchet@36170
   566
  end
mengj@19720
   567
wenzelm@33311
   568
end;