src/HOL/Tools/Sledgehammer/sledgehammer_hol_clause.ML
author blanchet
Fri May 14 11:23:42 2010 +0200 (2010-05-14 ago)
changeset 36909 7d5587f6d5f7
parent 36565 061475351a09
child 36922 12f87df9c1a5
permissions -rw-r--r--
made Sledgehammer's full-typed proof reconstruction work for the first time;
previously, Isar proofs and full-type mode were mutually exclusive because both options were hard-coded in the ATP names (e.g., "e_isar" and "full_vampire") -- making the options orthogonal revealed that some code was missing to handle types in the proof reconstruction code
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@35865
    32
  exception TRIVIAL
blanchet@35865
    33
  val make_conjecture_clauses : bool -> theory -> thm list -> hol_clause list
blanchet@35865
    34
  val make_axiom_clauses : bool -> theory ->
blanchet@35865
    35
       (thm * (axiom_name * hol_clause_id)) list -> (axiom_name * hol_clause) list
blanchet@36909
    36
  val type_wrapper_name : string
blanchet@35865
    37
  val get_helper_clauses : bool -> theory -> bool ->
blanchet@35865
    38
       hol_clause list * (thm * (axiom_name * hol_clause_id)) list * string list ->
blanchet@35865
    39
       hol_clause list
blanchet@36235
    40
  val write_tptp_file : bool -> bool -> bool -> Path.T ->
blanchet@35865
    41
    hol_clause list * hol_clause list * hol_clause list * hol_clause list *
blanchet@36402
    42
    classrel_clause list * arity_clause list -> name_pool option * int
blanchet@36235
    43
  val write_dfg_file : 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@35963
    55
(* Parameter "full_types" below indicates that full type information is to be
blanchet@36235
    56
   exported.
paulson@22825
    57
blanchet@36235
    58
   If "explicit_apply" is false, each function will be directly applied to as
blanchet@36235
    59
   many arguments as possible, avoiding use of the "apply" operator. Use of
blanchet@36402
    60
   "hBOOL" is also minimized.
blanchet@36235
    61
*)
paulson@22064
    62
wenzelm@33035
    63
fun min_arity_of const_min_arity c = the_default 0 (Symtab.lookup const_min_arity c);
paulson@22064
    64
paulson@22825
    65
(*True if the constant ever appears outside of the top-level position in literals.
paulson@22825
    66
  If false, the constant always receives all of its arguments and is used as a predicate.*)
blanchet@36235
    67
fun needs_hBOOL explicit_apply const_needs_hBOOL c =
blanchet@36235
    68
  explicit_apply orelse
wenzelm@33035
    69
    the_default false (Symtab.lookup const_needs_hBOOL c);
paulson@22064
    70
mengj@19198
    71
mengj@17998
    72
(******************************************************)
mengj@17998
    73
(* data types for typed combinator expressions        *)
mengj@17998
    74
(******************************************************)
mengj@17998
    75
mengj@17998
    76
type axiom_name = string;
mengj@17998
    77
type polarity = bool;
blanchet@35865
    78
type hol_clause_id = int;
mengj@17998
    79
blanchet@35865
    80
datatype combterm =
blanchet@36170
    81
  CombConst of name * fol_type * fol_type list (* Const and Free *) |
blanchet@36170
    82
  CombVar of name * fol_type |
blanchet@35865
    83
  CombApp of combterm * combterm
wenzelm@24311
    84
mengj@17998
    85
datatype literal = Literal of polarity * combterm;
mengj@17998
    86
blanchet@35865
    87
datatype hol_clause =
blanchet@35865
    88
  HOLClause of {clause_id: hol_clause_id, axiom_name: axiom_name, th: thm,
blanchet@35865
    89
                kind: kind, literals: literal list, ctypes_sorts: typ list};
mengj@17998
    90
mengj@17998
    91
mengj@17998
    92
(*********************************************************************)
mengj@17998
    93
(* convert a clause with type Term.term to a clause with type clause *)
mengj@17998
    94
(*********************************************************************)
mengj@17998
    95
blanchet@36170
    96
fun isFalse (Literal (pol, CombConst ((c, _), _, _))) =
blanchet@35865
    97
      (pol andalso c = "c_False") orelse (not pol andalso c = "c_True")
mengj@17998
    98
  | isFalse _ = false;
mengj@17998
    99
blanchet@36170
   100
fun isTrue (Literal (pol, CombConst ((c, _), _, _))) =
mengj@17998
   101
      (pol andalso c = "c_True") orelse
mengj@17998
   102
      (not pol andalso c = "c_False")
mengj@17998
   103
  | isTrue _ = false;
wenzelm@24311
   104
blanchet@35865
   105
fun isTaut (HOLClause {literals,...}) = exists isTrue literals;
mengj@17998
   106
immler@30151
   107
fun type_of dfg (Type (a, Ts)) =
blanchet@36169
   108
    let val (folTypes,ts) = types_of dfg Ts in
blanchet@36169
   109
      (TyConstr (`(make_fixed_type_const dfg) a, folTypes), ts)
blanchet@36169
   110
    end
blanchet@36169
   111
  | type_of _ (tp as TFree (a, _)) = (TyFree (`make_fixed_type_var a), [tp])
blanchet@36169
   112
  | type_of _ (tp as TVar (x, _)) =
blanchet@36169
   113
    (TyVar (make_schematic_type_var x, string_of_indexname x), [tp])
immler@30151
   114
and types_of dfg Ts =
immler@30151
   115
      let val (folTyps,ts) = ListPair.unzip (map (type_of dfg) Ts)
blanchet@35865
   116
      in  (folTyps, union_all ts)  end;
mengj@17998
   117
mengj@17998
   118
(* same as above, but no gathering of sort information *)
immler@30151
   119
fun simp_type_of dfg (Type (a, Ts)) =
blanchet@36169
   120
      TyConstr (`(make_fixed_type_const dfg) a, map (simp_type_of dfg) Ts)
blanchet@36169
   121
  | simp_type_of _ (TFree (a, _)) = TyFree (`make_fixed_type_var a)
blanchet@36169
   122
  | simp_type_of _ (TVar (x, _)) =
blanchet@36169
   123
    TyVar (make_schematic_type_var x, string_of_indexname x);
mengj@18440
   124
mengj@18356
   125
immler@30151
   126
fun const_type_of dfg thy (c,t) =
immler@30151
   127
      let val (tp,ts) = type_of dfg t
immler@30151
   128
      in  (tp, ts, map (simp_type_of dfg) (Sign.const_typargs thy (c,t))) end;
mengj@18356
   129
mengj@17998
   130
(* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
immler@30151
   131
fun combterm_of dfg thy (Const(c,t)) =
immler@30151
   132
      let val (tp,ts,tvar_list) = const_type_of dfg thy (c,t)
blanchet@36170
   133
          val c' = CombConst (`(make_fixed_const dfg) c, tp, tvar_list)
paulson@22078
   134
      in  (c',ts)  end
wenzelm@32994
   135
  | combterm_of dfg _ (Free(v,t)) =
immler@30151
   136
      let val (tp,ts) = type_of dfg t
blanchet@36170
   137
          val v' = CombConst (`make_fixed_var v, tp, [])
paulson@22078
   138
      in  (v',ts)  end
wenzelm@32994
   139
  | combterm_of dfg _ (Var(v,t)) =
immler@30151
   140
      let val (tp,ts) = type_of dfg t
blanchet@36170
   141
          val v' = CombVar ((make_schematic_var v, string_of_indexname v), tp)
paulson@22078
   142
      in  (v',ts)  end
immler@30151
   143
  | combterm_of dfg thy (P $ Q) =
immler@30151
   144
      let val (P',tsP) = combterm_of dfg thy P
immler@30151
   145
          val (Q',tsQ) = combterm_of dfg thy Q
haftmann@33042
   146
      in  (CombApp(P',Q'), union (op =) tsP tsQ)  end
blanchet@35865
   147
  | combterm_of _ _ (t as Abs _) = raise CLAUSE ("HOL CLAUSE", t);
mengj@17998
   148
blanchet@35865
   149
fun predicate_of dfg thy ((@{const Not} $ P), polarity) = predicate_of dfg thy (P, not polarity)
immler@30151
   150
  | predicate_of dfg thy (t,polarity) = (combterm_of dfg thy (Envir.eta_contract t), polarity);
mengj@17998
   151
blanchet@35865
   152
fun literals_of_term1 dfg thy args (@{const Trueprop} $ P) = literals_of_term1 dfg thy args P
blanchet@35865
   153
  | literals_of_term1 dfg thy args (@{const "op |"} $ P $ Q) =
immler@30151
   154
      literals_of_term1 dfg thy (literals_of_term1 dfg thy args P) Q
immler@30151
   155
  | literals_of_term1 dfg thy (lits,ts) P =
immler@30151
   156
      let val ((pred,ts'),pol) = predicate_of dfg thy (P,true)
paulson@21509
   157
      in
haftmann@33042
   158
          (Literal(pol,pred)::lits, union (op =) ts ts')
paulson@21509
   159
      end;
mengj@17998
   160
immler@30151
   161
fun literals_of_term_dfg dfg thy P = literals_of_term1 dfg thy ([],[]) P;
immler@30151
   162
val literals_of_term = literals_of_term_dfg false;
mengj@17998
   163
blanchet@35865
   164
(* Trivial problem, which resolution cannot handle (empty clause) *)
blanchet@35865
   165
exception TRIVIAL;
wenzelm@28835
   166
mengj@17998
   167
(* making axiom and conjecture clauses *)
blanchet@35865
   168
fun make_clause dfg thy (clause_id, axiom_name, kind, th) =
immler@30151
   169
    let val (lits,ctypes_sorts) = literals_of_term_dfg dfg thy (prop_of th)
mengj@17998
   170
    in
blanchet@35865
   171
        if forall isFalse lits then
blanchet@35865
   172
            raise TRIVIAL
wenzelm@24311
   173
        else
blanchet@35865
   174
            HOLClause {clause_id = clause_id, axiom_name = axiom_name, th = th,
blanchet@35865
   175
                       kind = kind, literals = lits, ctypes_sorts = ctypes_sorts}
mengj@17998
   176
    end;
mengj@17998
   177
mengj@20016
   178
blanchet@36218
   179
fun add_axiom_clause dfg thy (th, (name, id)) =
blanchet@36218
   180
  let val cls = make_clause dfg thy (id, name, Axiom, th) in
blanchet@36218
   181
    not (isTaut cls) ? cons (name, cls)
blanchet@36218
   182
  end
mengj@19354
   183
blanchet@36218
   184
fun make_axiom_clauses dfg thy clauses =
blanchet@36218
   185
  fold (add_axiom_clause dfg thy) clauses []
mengj@19354
   186
wenzelm@32994
   187
fun make_conjecture_clauses_aux _ _ _ [] = []
immler@30151
   188
  | make_conjecture_clauses_aux dfg thy n (th::ths) =
blanchet@35865
   189
      make_clause dfg thy (n,"conjecture", Conjecture, th) ::
immler@30151
   190
      make_conjecture_clauses_aux dfg thy (n+1) ths;
mengj@17998
   191
immler@30151
   192
fun make_conjecture_clauses dfg thy = make_conjecture_clauses_aux dfg thy 0;
mengj@17998
   193
mengj@17998
   194
mengj@17998
   195
(**********************************************************************)
mengj@17998
   196
(* convert clause into ATP specific formats:                          *)
mengj@17998
   197
(* TPTP used by Vampire and E                                         *)
mengj@19720
   198
(* DFG used by SPASS                                                  *)
mengj@17998
   199
(**********************************************************************)
mengj@17998
   200
paulson@22078
   201
(*Result of a function type; no need to check that the argument type matches.*)
blanchet@36909
   202
fun result_type (TyConstr (_, [_, tp2])) = tp2
blanchet@36909
   203
  | result_type _ = raise Fail "non-function type"
paulson@22078
   204
wenzelm@32994
   205
fun type_of_combterm (CombConst (_, tp, _)) = tp
wenzelm@32994
   206
  | type_of_combterm (CombVar (_, tp)) = tp
wenzelm@32994
   207
  | type_of_combterm (CombApp (t1, _)) = result_type (type_of_combterm t1);
mengj@17998
   208
paulson@22064
   209
(*gets the head of a combinator application, along with the list of arguments*)
blanchet@35865
   210
fun strip_combterm_comb u =
paulson@22078
   211
    let fun stripc (CombApp(t,u), ts) = stripc (t, u::ts)
paulson@22064
   212
        |   stripc  x =  x
paulson@22064
   213
    in  stripc(u,[])  end;
paulson@22064
   214
blanchet@36909
   215
val type_wrapper_name = "ti"
paulson@22851
   216
blanchet@36235
   217
fun head_needs_hBOOL explicit_apply const_needs_hBOOL
blanchet@36235
   218
                     (CombConst ((c, _), _, _)) =
blanchet@36235
   219
    needs_hBOOL explicit_apply const_needs_hBOOL c
blanchet@36235
   220
  | head_needs_hBOOL _ _ _ = true
paulson@22851
   221
blanchet@36170
   222
fun wrap_type full_types (s, ty) pool =
blanchet@36170
   223
  if full_types then
blanchet@36170
   224
    let val (s', pool) = string_of_fol_type ty pool in
blanchet@36909
   225
      (type_wrapper_name ^ paren_pack [s, s'], pool)
blanchet@36170
   226
    end
blanchet@36170
   227
  else
blanchet@36170
   228
    (s, pool)
blanchet@36170
   229
blanchet@36235
   230
fun wrap_type_if full_types explicit_apply cnh (head, s, tp) =
blanchet@36235
   231
  if head_needs_hBOOL explicit_apply cnh head then
blanchet@36235
   232
    wrap_type full_types (s, tp)
blanchet@36235
   233
  else
blanchet@36235
   234
    pair s
wenzelm@24311
   235
blanchet@35865
   236
fun apply ss = "hAPP" ^ paren_pack ss;
paulson@22851
   237
paulson@22064
   238
fun rev_apply (v, []) = v
paulson@22851
   239
  | rev_apply (v, arg::args) = apply [rev_apply (v, args), arg];
paulson@22064
   240
paulson@22064
   241
fun string_apply (v, args) = rev_apply (v, rev args);
paulson@22064
   242
blanchet@36170
   243
(* Apply an operator to the argument strings, using either the "apply" operator
blanchet@36170
   244
   or direct function application. *)
blanchet@36170
   245
fun string_of_application full_types cma (CombConst ((s, s'), _, tvars), args)
blanchet@36170
   246
                          pool =
blanchet@36170
   247
    let
blanchet@36170
   248
      val s = if s = "equal" then "c_fequal" else s
blanchet@36170
   249
      val nargs = min_arity_of cma s
blanchet@36170
   250
      val args1 = List.take (args, nargs)
blanchet@36170
   251
        handle Subscript =>
blanchet@36170
   252
               raise Fail (quote s ^ " has arity " ^ Int.toString nargs ^
blanchet@36170
   253
                           " but is applied to " ^ commas (map quote args))
blanchet@36170
   254
      val args2 = List.drop (args, nargs)
blanchet@36170
   255
      val (targs, pool) = if full_types then ([], pool)
blanchet@36170
   256
                          else pool_map string_of_fol_type tvars pool
blanchet@36170
   257
      val (s, pool) = nice_name (s, s') pool
blanchet@36170
   258
    in (string_apply (s ^ paren_pack (args1 @ targs), args2), pool) end
blanchet@36170
   259
  | string_of_application _ _ (CombVar (name, _), args) pool =
blanchet@36170
   260
    nice_name name pool |>> (fn s => string_apply (s, args))
wenzelm@24311
   261
blanchet@36235
   262
fun string_of_combterm (params as (full_types, explicit_apply, cma, cnh)) t
blanchet@36235
   263
                       pool =
blanchet@36170
   264
  let
blanchet@36170
   265
    val (head, args) = strip_combterm_comb t
blanchet@36170
   266
    val (ss, pool) = pool_map (string_of_combterm params) args pool
blanchet@36170
   267
    val (s, pool) = string_of_application full_types cma (head, ss) pool
blanchet@36235
   268
  in
blanchet@36235
   269
    wrap_type_if full_types explicit_apply cnh (head, s, type_of_combterm t)
blanchet@36235
   270
                 pool
blanchet@36235
   271
  end
mengj@18356
   272
paulson@22064
   273
(*Boolean-valued terms are here converted to literals.*)
blanchet@36170
   274
fun boolify params c =
blanchet@36170
   275
  string_of_combterm params c #>> prefix "hBOOL" o paren_pack o single
paulson@22064
   276
blanchet@36235
   277
fun string_of_predicate (params as (_, explicit_apply, _, cnh)) t =
paulson@22064
   278
  case t of
blanchet@36170
   279
    (CombApp (CombApp (CombConst (("equal", _), _, _), t1), t2)) =>
blanchet@36170
   280
    (* DFG only: new TPTP prefers infix equality *)
blanchet@36170
   281
    pool_map (string_of_combterm params) [t1, t2]
blanchet@36170
   282
    #>> prefix "equal" o paren_pack
blanchet@36170
   283
  | _ =>
blanchet@36170
   284
    case #1 (strip_combterm_comb t) of
blanchet@36170
   285
      CombConst ((s, _), _, _) =>
blanchet@36235
   286
      (if needs_hBOOL explicit_apply cnh s then boolify else string_of_combterm)
blanchet@36235
   287
          params t
blanchet@36170
   288
    | _ => boolify params t
mengj@18356
   289
mengj@17998
   290
blanchet@36170
   291
(*** TPTP format ***)
mengj@19720
   292
blanchet@36170
   293
fun tptp_of_equality params pos (t1, t2) =
blanchet@36170
   294
  pool_map (string_of_combterm params) [t1, t2]
blanchet@36170
   295
  #>> space_implode (if pos then " = " else " != ")
wenzelm@24311
   296
blanchet@36170
   297
fun tptp_literal params
blanchet@36170
   298
        (Literal (pos, CombApp (CombApp (CombConst (("equal", _), _, _), t1),
blanchet@36170
   299
                                         t2))) =
blanchet@36170
   300
    tptp_of_equality params pos (t1, t2)
blanchet@36170
   301
  | tptp_literal params (Literal (pos, pred)) =
blanchet@36170
   302
    string_of_predicate params pred #>> tptp_sign pos
blanchet@36170
   303
 
blanchet@36170
   304
(* Given a clause, returns its literals paired with a list of literals
blanchet@36170
   305
   concerning TFrees; the latter should only occur in conjecture clauses. *)
blanchet@36556
   306
fun tptp_type_literals params pos (HOLClause {literals, ctypes_sorts, ...})
blanchet@36556
   307
                       pool =
blanchet@36556
   308
  let
blanchet@36556
   309
    val (lits, pool) = pool_map (tptp_literal params) literals pool
blanchet@36556
   310
    val (tylits, pool) = pool_map (tptp_of_type_literal pos)
blanchet@36556
   311
                                  (add_type_literals ctypes_sorts) pool
blanchet@36556
   312
  in ((lits, tylits), pool) end
wenzelm@24311
   313
blanchet@36170
   314
fun tptp_clause params (cls as HOLClause {axiom_name, clause_id, kind, ...})
blanchet@36170
   315
                pool =
blanchet@36170
   316
let
blanchet@36170
   317
    val ((lits, tylits), pool) =
blanchet@36170
   318
      tptp_type_literals params (kind = Conjecture) cls pool
paulson@24937
   319
  in
blanchet@36170
   320
    ((gen_tptp_cls (clause_id, axiom_name, kind, lits, tylits), tylits), pool)
blanchet@36170
   321
  end
mengj@17998
   322
mengj@17998
   323
blanchet@36170
   324
(*** DFG format ***)
paulson@21561
   325
blanchet@36170
   326
fun dfg_literal params (Literal (pos, pred)) =
blanchet@36170
   327
  string_of_predicate params pred #>> dfg_sign pos
mengj@19720
   328
blanchet@36170
   329
fun dfg_type_literals params pos (HOLClause {literals, ctypes_sorts, ...}) =
blanchet@36170
   330
  pool_map (dfg_literal params) literals
blanchet@36556
   331
  #>> rpair (map (dfg_of_type_literal pos) (add_type_literals ctypes_sorts))
mengj@19720
   332
blanchet@36170
   333
fun get_uvars (CombConst _) vars pool = (vars, pool)
blanchet@36170
   334
  | get_uvars (CombVar (name, _)) vars pool =
blanchet@36170
   335
    nice_name name pool |>> (fn var => var :: vars)
blanchet@36170
   336
  | get_uvars (CombApp (P, Q)) vars pool =
blanchet@36170
   337
    let val (vars, pool) = get_uvars P vars pool in get_uvars Q vars pool end
mengj@19720
   338
blanchet@36170
   339
fun get_uvars_l (Literal (_, c)) = get_uvars c [];
mengj@19720
   340
blanchet@36170
   341
fun dfg_vars (HOLClause {literals, ...}) =
blanchet@36170
   342
  pool_map get_uvars_l literals #>> union_all
wenzelm@24311
   343
blanchet@36170
   344
fun dfg_clause params (cls as HOLClause {axiom_name, clause_id, kind,
blanchet@36170
   345
                                         ctypes_sorts, ...}) pool =
blanchet@36170
   346
  let
blanchet@36170
   347
    val ((lits, tylits), pool) =
blanchet@36170
   348
      dfg_type_literals params (kind = Conjecture) cls pool
blanchet@36170
   349
    val (vars, pool) = dfg_vars cls pool
blanchet@36170
   350
    val tvars = get_tvar_strs ctypes_sorts
paulson@24937
   351
  in
blanchet@36170
   352
    ((gen_dfg_cls (clause_id, axiom_name, kind, lits, tylits, tvars @ vars),
blanchet@36170
   353
      tylits), pool)
blanchet@36170
   354
  end
paulson@24937
   355
mengj@19720
   356
paulson@22064
   357
(** For DFG format: accumulate function and predicate declarations **)
mengj@19720
   358
blanchet@36218
   359
fun add_types tvars = fold add_fol_type_funcs tvars
mengj@19720
   360
blanchet@36235
   361
fun add_decls (full_types, explicit_apply, cma, cnh)
blanchet@36565
   362
              (CombConst ((c, _), ctp, tvars)) (funcs, preds) =
blanchet@36565
   363
      (if c = "equal" then
blanchet@36565
   364
         (add_types tvars funcs, preds)
blanchet@36565
   365
       else
blanchet@36565
   366
         let val arity = min_arity_of cma c
blanchet@36565
   367
             val ntys = if not full_types then length tvars else 0
blanchet@36565
   368
             val addit = Symtab.update(c, arity + ntys)
blanchet@36565
   369
         in
blanchet@36565
   370
             if needs_hBOOL explicit_apply cnh c then
blanchet@36565
   371
               (add_types tvars (addit funcs), preds)
blanchet@36565
   372
             else
blanchet@36565
   373
               (add_types tvars funcs, addit preds)
blanchet@36565
   374
         end) |>> full_types ? add_fol_type_funcs ctp
blanchet@36218
   375
  | add_decls _ (CombVar (_, ctp)) (funcs, preds) =
blanchet@36218
   376
      (add_fol_type_funcs ctp funcs, preds)
blanchet@36218
   377
  | add_decls params (CombApp (P, Q)) decls =
blanchet@36218
   378
      decls |> add_decls params P |> add_decls params Q
mengj@19720
   379
blanchet@36218
   380
fun add_literal_decls params (Literal (_, c)) = add_decls params c
mengj@19720
   381
blanchet@36218
   382
fun add_clause_decls params (HOLClause {literals, ...}) decls =
blanchet@36218
   383
    fold (add_literal_decls params) literals decls
wenzelm@27187
   384
    handle Symtab.DUP a => error ("function " ^ a ^ " has multiple arities")
mengj@19720
   385
nipkow@31800
   386
fun decls_of_clauses params clauses arity_clauses =
blanchet@36237
   387
  let val functab =
blanchet@36909
   388
        init_functab |> fold Symtab.update [(type_wrapper_name, 2), ("hAPP", 2),
blanchet@36237
   389
                                            ("tc_bool", 0)]
blanchet@36218
   390
      val predtab = Symtab.empty |> Symtab.update ("hBOOL", 1)
blanchet@36218
   391
      val (functab, predtab) =
blanchet@36218
   392
        (functab, predtab) |> fold (add_clause_decls params) clauses
blanchet@36218
   393
                           |>> fold add_arity_clause_funcs arity_clauses
blanchet@36218
   394
  in (Symtab.dest functab, Symtab.dest predtab) end
mengj@19720
   395
blanchet@36218
   396
fun add_clause_preds (HOLClause {ctypes_sorts, ...}) preds =
blanchet@36218
   397
  fold add_type_sort_preds ctypes_sorts preds
wenzelm@27187
   398
  handle Symtab.DUP a => error ("predicate " ^ a ^ " has multiple arities")
paulson@21398
   399
paulson@21398
   400
(*Higher-order clauses have only the predicates hBOOL and type classes.*)
wenzelm@24311
   401
fun preds_of_clauses clauses clsrel_clauses arity_clauses =
blanchet@36218
   402
  Symtab.empty
blanchet@36218
   403
  |> fold add_clause_preds clauses
blanchet@36218
   404
  |> fold add_arity_clause_preds arity_clauses
blanchet@36218
   405
  |> fold add_classrel_clause_preds clsrel_clauses
blanchet@36218
   406
  |> Symtab.dest
mengj@18440
   407
mengj@18440
   408
(**********************************************************************)
mengj@19198
   409
(* write clauses to files                                             *)
mengj@19198
   410
(**********************************************************************)
mengj@19198
   411
paulson@21573
   412
val init_counters =
blanchet@35865
   413
  Symtab.make [("c_COMBI", 0), ("c_COMBK", 0), ("c_COMBB", 0), ("c_COMBC", 0),
blanchet@35865
   414
               ("c_COMBS", 0)];
wenzelm@24311
   415
blanchet@36218
   416
fun count_combterm (CombConst ((c, _), _, _)) =
blanchet@36218
   417
    Symtab.map_entry c (Integer.add 1)
blanchet@36218
   418
  | count_combterm (CombVar _) = I
blanchet@36218
   419
  | count_combterm (CombApp (t1, t2)) = count_combterm t1 #> count_combterm t2
paulson@21573
   420
blanchet@36218
   421
fun count_literal (Literal (_, t)) = count_combterm t
paulson@21573
   422
blanchet@36218
   423
fun count_clause (HOLClause {literals, ...}) = fold count_literal literals
paulson@21573
   424
blanchet@36218
   425
fun count_user_clause user_lemmas (HOLClause {axiom_name, literals, ...}) =
blanchet@36218
   426
  member (op =) user_lemmas axiom_name ? fold count_literal literals
paulson@21573
   427
blanchet@36228
   428
fun cnf_helper_thms thy = cnf_rules_pairs thy o map (`Thm.get_name_hint)
mengj@20644
   429
immler@31752
   430
fun get_helper_clauses dfg thy isFO (conjectures, axcls, user_lemmas) =
blanchet@35865
   431
  if isFO then
blanchet@35865
   432
    []
paulson@23386
   433
  else
immler@31752
   434
    let
immler@31752
   435
        val axclauses = map #2 (make_axiom_clauses dfg thy axcls)
blanchet@36218
   436
        val ct = init_counters |> fold count_clause conjectures
blanchet@36218
   437
                               |> fold (count_user_clause user_lemmas) axclauses
wenzelm@33035
   438
        fun needed c = the (Symtab.lookup ct c) > 0
wenzelm@24311
   439
        val IK = if needed "c_COMBI" orelse needed "c_COMBK"
blanchet@35865
   440
                 then cnf_helper_thms thy [@{thm COMBI_def}, @{thm COMBK_def}]
wenzelm@24311
   441
                 else []
wenzelm@24311
   442
        val BC = if needed "c_COMBB" orelse needed "c_COMBC"
blanchet@35865
   443
                 then cnf_helper_thms thy [@{thm COMBB_def}, @{thm COMBC_def}]
paulson@21135
   444
                 else []
blanchet@35865
   445
        val S = if needed "c_COMBS" then cnf_helper_thms thy [@{thm COMBS_def}]
wenzelm@24311
   446
                else []
blanchet@35865
   447
        val other = cnf_helper_thms thy [@{thm fequal_imp_equal},
blanchet@35865
   448
                                         @{thm equal_imp_fequal}]
mengj@20791
   449
    in
immler@30151
   450
        map #2 (make_axiom_clauses dfg thy (other @ IK @ BC @ S))
paulson@23386
   451
    end;
mengj@20791
   452
paulson@22064
   453
(*Find the minimal arity of each function mentioned in the term. Also, note which uses
paulson@22064
   454
  are not at top level, to see if hBOOL is needed.*)
immler@30150
   455
fun count_constants_term toplev t (const_min_arity, const_needs_hBOOL) =
blanchet@35865
   456
  let val (head, args) = strip_combterm_comb t
paulson@22064
   457
      val n = length args
blanchet@36233
   458
      val (const_min_arity, const_needs_hBOOL) =
blanchet@36233
   459
        (const_min_arity, const_needs_hBOOL)
blanchet@36233
   460
        |> fold (count_constants_term false) args
paulson@22064
   461
  in
paulson@22064
   462
      case head of
blanchet@36170
   463
          CombConst ((a, _),_,_) => (*predicate or function version of "equal"?*)
wenzelm@24311
   464
            let val a = if a="equal" andalso not toplev then "c_fequal" else a
wenzelm@24311
   465
            in
blanchet@36233
   466
              (const_min_arity |> Symtab.map_default (a, n) (Integer.min n),
blanchet@36233
   467
               const_needs_hBOOL |> not toplev ? Symtab.update (a, true))
wenzelm@24311
   468
            end
wenzelm@32994
   469
        | _ => (const_min_arity, const_needs_hBOOL)
paulson@22064
   470
  end;
paulson@22064
   471
paulson@22064
   472
(*A literal is a top-level term*)
immler@30150
   473
fun count_constants_lit (Literal (_,t)) (const_min_arity, const_needs_hBOOL) =
immler@30150
   474
  count_constants_term true t (const_min_arity, const_needs_hBOOL);
paulson@22064
   475
blanchet@35865
   476
fun count_constants_clause (HOLClause {literals, ...})
blanchet@35865
   477
                           (const_min_arity, const_needs_hBOOL) =
immler@30150
   478
  fold count_constants_lit literals (const_min_arity, const_needs_hBOOL);
paulson@22064
   479
blanchet@36235
   480
fun display_arity explicit_apply const_needs_hBOOL (c,n) =
blanchet@35865
   481
  trace_msg (fn () => "Constant: " ^ c ^
blanchet@35826
   482
                " arity:\t" ^ Int.toString n ^
blanchet@36235
   483
                (if needs_hBOOL explicit_apply const_needs_hBOOL c then
blanchet@36235
   484
                   " needs hBOOL"
blanchet@36235
   485
                 else
blanchet@36235
   486
                   ""))
paulson@22064
   487
blanchet@36235
   488
fun count_constants explicit_apply
blanchet@36235
   489
                    (conjectures, _, extra_clauses, helper_clauses, _, _) =
blanchet@36235
   490
  if not explicit_apply then
immler@30150
   491
     let val (const_min_arity, const_needs_hBOOL) =
immler@30150
   492
          fold count_constants_clause conjectures (Symtab.empty, Symtab.empty)
immler@31865
   493
       |> fold count_constants_clause extra_clauses
immler@30149
   494
       |> fold count_constants_clause helper_clauses
blanchet@36481
   495
     val _ = app (display_arity explicit_apply const_needs_hBOOL)
blanchet@36481
   496
                 (Symtab.dest (const_min_arity))
immler@30150
   497
     in (const_min_arity, const_needs_hBOOL) end
immler@30150
   498
  else (Symtab.empty, Symtab.empty);
paulson@22064
   499
blanchet@36218
   500
fun header () =
blanchet@36218
   501
  "% This file was generated by Isabelle (most likely Sledgehammer)\n" ^
blanchet@36218
   502
  "% " ^ timestamp () ^ "\n"
blanchet@36218
   503
blanchet@35865
   504
(* TPTP format *)
immler@31749
   505
blanchet@36235
   506
fun write_tptp_file readable_names full_types explicit_apply file clauses =
immler@31409
   507
  let
blanchet@36167
   508
    fun section _ [] = []
blanchet@36393
   509
      | section name ss =
blanchet@36393
   510
        "\n% " ^ name ^ plural_s (length ss) ^ " (" ^ Int.toString (length ss) ^
blanchet@36393
   511
        ")\n" :: ss
blanchet@36222
   512
    val pool = empty_name_pool readable_names
immler@31865
   513
    val (conjectures, axclauses, _, helper_clauses,
immler@31865
   514
      classrel_clauses, arity_clauses) = clauses
blanchet@36235
   515
    val (cma, cnh) = count_constants explicit_apply clauses
blanchet@36235
   516
    val params = (full_types, explicit_apply, cma, cnh)
blanchet@36170
   517
    val ((conjecture_clss, tfree_litss), pool) =
blanchet@36170
   518
      pool_map (tptp_clause params) conjectures pool |>> ListPair.unzip
blanchet@36218
   519
    val tfree_clss = map tptp_tfree_clause (fold (union (op =)) tfree_litss [])
blanchet@36170
   520
    val (ax_clss, pool) = pool_map (apfst fst oo tptp_clause params) axclauses
blanchet@36170
   521
                                   pool
blanchet@36170
   522
    val classrel_clss = map tptp_classrel_clause classrel_clauses
blanchet@36170
   523
    val arity_clss = map tptp_arity_clause arity_clauses
blanchet@36170
   524
    val (helper_clss, pool) = pool_map (apfst fst oo tptp_clause params)
blanchet@36170
   525
                                       helper_clauses pool
blanchet@36402
   526
    val conjecture_offset =
blanchet@36402
   527
      length ax_clss + length classrel_clss + length arity_clss
blanchet@36402
   528
      + length helper_clss
blanchet@36393
   529
    val _ =
blanchet@36393
   530
      File.write_list file
blanchet@36393
   531
          (header () ::
blanchet@36393
   532
           section "Relevant fact" ax_clss @
blanchet@36402
   533
           section "Class relationship" classrel_clss @
blanchet@36402
   534
           section "Arity declaration" arity_clss @
blanchet@36393
   535
           section "Helper fact" helper_clss @
blanchet@36393
   536
           section "Conjecture" conjecture_clss @
blanchet@36402
   537
           section "Type variable" tfree_clss)
blanchet@36402
   538
  in (pool, conjecture_offset) end
mengj@19720
   539
blanchet@35865
   540
(* DFG format *)
mengj@19720
   541
blanchet@36235
   542
fun write_dfg_file full_types explicit_apply file clauses =
immler@31409
   543
  let
blanchet@36219
   544
    (* Some of the helper functions below are not name-pool-aware. However,
blanchet@36219
   545
       there's no point in adding name pool support if the DFG format is on its
blanchet@36219
   546
       way out anyway. *)
blanchet@36219
   547
    val pool = NONE
immler@31865
   548
    val (conjectures, axclauses, _, helper_clauses,
immler@31865
   549
      classrel_clauses, arity_clauses) = clauses
blanchet@36235
   550
    val (cma, cnh) = count_constants explicit_apply clauses
blanchet@36235
   551
    val params = (full_types, explicit_apply, cma, cnh)
blanchet@36170
   552
    val ((conjecture_clss, tfree_litss), pool) =
blanchet@36170
   553
      pool_map (dfg_clause params) conjectures pool |>> ListPair.unzip
blanchet@36393
   554
    and problem_name = Path.implode (Path.base file)
blanchet@36170
   555
    val (axstrs, pool) = pool_map (apfst fst oo dfg_clause params) axclauses pool
blanchet@35865
   556
    val tfree_clss = map dfg_tfree_clause (union_all tfree_litss)
blanchet@36170
   557
    val (helper_clauses_strs, pool) =
blanchet@36170
   558
      pool_map (apfst fst oo dfg_clause params) helper_clauses pool
blanchet@36218
   559
    val (funcs, cl_preds) = decls_of_clauses params (helper_clauses @ conjectures @ axclauses) arity_clauses
immler@31409
   560
    and ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
blanchet@36402
   561
    val conjecture_offset =
blanchet@36402
   562
      length axclauses + length classrel_clauses + length arity_clauses
blanchet@36402
   563
      + length helper_clauses
blanchet@36393
   564
    val _ =
blanchet@36393
   565
      File.write_list file
blanchet@36393
   566
          (header () ::
blanchet@36393
   567
           string_of_start problem_name ::
blanchet@36393
   568
           string_of_descrip problem_name ::
blanchet@36393
   569
           string_of_symbols (string_of_funcs funcs)
blanchet@36393
   570
                             (string_of_preds (cl_preds @ ty_preds)) ::
blanchet@36393
   571
           "list_of_clauses(axioms, cnf).\n" ::
blanchet@36393
   572
           axstrs @
blanchet@36393
   573
           map dfg_classrel_clause classrel_clauses @
blanchet@36393
   574
           map dfg_arity_clause arity_clauses @
blanchet@36393
   575
           helper_clauses_strs @
blanchet@36393
   576
           ["end_of_list.\n\nlist_of_clauses(conjectures, cnf).\n"] @
blanchet@36402
   577
           conjecture_clss @
blanchet@36393
   578
           tfree_clss @
blanchet@36393
   579
           ["end_of_list.\n\n",
blanchet@36393
   580
            "end_problem.\n"])
blanchet@36402
   581
  in (pool, conjecture_offset) end
mengj@19720
   582
wenzelm@33311
   583
end;