src/HOL/Tools/Sledgehammer/sledgehammer_fact_preprocessor.ML
author blanchet
Wed Mar 17 19:26:05 2010 +0100 (2010-03-17)
changeset 35826 1590abc3d42a
parent 35825 a6aad5a70ed4
child 35828 46cfc4b8112e
permissions -rw-r--r--
renamed Sledgehammer structures
blanchet@35826
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_fact_preprocessor.ML
wenzelm@33311
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory
paulson@15347
     3
wenzelm@20461
     4
Transformation of axiom rules (elim/intro/etc) into CNF forms.
paulson@15347
     5
*)
paulson@15347
     6
blanchet@35826
     7
signature SLEDGEHAMMER_FACT_PREPROCESSOR =
wenzelm@21505
     8
sig
wenzelm@32955
     9
  val trace: bool Unsynchronized.ref
wenzelm@32955
    10
  val trace_msg: (unit -> string) -> unit
wenzelm@27179
    11
  val cnf_axiom: theory -> thm -> thm list
wenzelm@24669
    12
  val pairname: thm -> string * thm
wenzelm@27184
    13
  val multi_base_blacklist: string list
paulson@25243
    14
  val bad_for_atp: thm -> bool
wenzelm@35568
    15
  val type_has_topsort: typ -> bool
wenzelm@27179
    16
  val cnf_rules_pairs: theory -> (string * thm) list -> (thm * (string * int)) list
wenzelm@24669
    17
  val neg_clausify: thm list -> thm list
wenzelm@24669
    18
  val expand_defs_tac: thm -> tactic
paulson@24827
    19
  val combinators: thm -> thm
wenzelm@32257
    20
  val neg_conjecture_clauses: Proof.context -> thm -> int -> thm list * (string * typ) list
wenzelm@32740
    21
  val suppress_endtheory: bool Unsynchronized.ref
wenzelm@32740
    22
    (*for emergency use where endtheory causes problems*)
wenzelm@24669
    23
  val setup: theory -> theory
wenzelm@21505
    24
end;
mengj@19196
    25
blanchet@35826
    26
structure Sledgehammer_Fact_Preprocessor : SLEDGEHAMMER_FACT_PREPROCESSOR =
paulson@15997
    27
struct
paulson@15347
    28
wenzelm@32955
    29
val trace = Unsynchronized.ref false;
wenzelm@32955
    30
fun trace_msg msg = if ! trace then tracing (msg ()) else ();
wenzelm@32955
    31
wenzelm@33832
    32
fun freeze_thm th = #1 (Drule.legacy_freeze_thaw th);
paulson@20863
    33
wenzelm@35568
    34
val type_has_topsort = Term.exists_subtype
wenzelm@35568
    35
  (fn TFree (_, []) => true
wenzelm@35568
    36
    | TVar (_, []) => true
wenzelm@35568
    37
    | _ => false);
wenzelm@27184
    38
wenzelm@28544
    39
paulson@15997
    40
(**** Transformation of Elimination Rules into First-Order Formulas****)
paulson@15347
    41
wenzelm@29064
    42
val cfalse = cterm_of @{theory HOL} HOLogic.false_const;
wenzelm@29064
    43
val ctp_false = cterm_of @{theory HOL} (HOLogic.mk_Trueprop HOLogic.false_const);
wenzelm@20461
    44
paulson@21430
    45
(*Converts an elim-rule into an equivalent theorem that does not have the
paulson@21430
    46
  predicate variable.  Leaves other theorems unchanged.  We simply instantiate the
paulson@21430
    47
  conclusion variable to False.*)
paulson@16009
    48
fun transform_elim th =
paulson@21430
    49
  case concl_of th of    (*conclusion variable*)
wenzelm@24669
    50
       Const("Trueprop",_) $ (v as Var(_,Type("bool",[]))) =>
wenzelm@29064
    51
           Thm.instantiate ([], [(cterm_of @{theory HOL} v, cfalse)]) th
wenzelm@24669
    52
    | v as Var(_, Type("prop",[])) =>
wenzelm@29064
    53
           Thm.instantiate ([], [(cterm_of @{theory HOL} v, ctp_false)]) th
paulson@21430
    54
    | _ => th;
paulson@15997
    55
paulson@24742
    56
(*To enforce single-threading*)
paulson@24742
    57
exception Clausify_failure of theory;
wenzelm@20461
    58
wenzelm@28544
    59
paulson@16009
    60
(**** SKOLEMIZATION BY INFERENCE (lcp) ****)
paulson@16009
    61
paulson@24742
    62
fun rhs_extra_types lhsT rhs =
paulson@24742
    63
  let val lhs_vars = Term.add_tfreesT lhsT []
paulson@24742
    64
      fun add_new_TFrees (TFree v) =
wenzelm@24821
    65
            if member (op =) lhs_vars v then I else insert (op =) (TFree v)
wenzelm@24821
    66
        | add_new_TFrees _ = I
paulson@24742
    67
      val rhs_consts = fold_aterms (fn Const c => insert (op =) c | _ => I) rhs []
paulson@24742
    68
  in fold (#2 #> Term.fold_atyps add_new_TFrees) rhs_consts [] end;
paulson@24742
    69
paulson@18141
    70
(*Traverse a theorem, declaring Skolem function definitions. String s is the suggested
wenzelm@27174
    71
  prefix for the Skolem constant.*)
wenzelm@27174
    72
fun declare_skofuns s th =
wenzelm@27174
    73
  let
wenzelm@33222
    74
    val nref = Unsynchronized.ref 0    (* FIXME ??? *)
wenzelm@27174
    75
    fun dec_sko (Const ("Ex",_) $ (xtp as Abs (_, T, p))) (axs, thy) =
wenzelm@27174
    76
          (*Existential: declare a Skolem function, then insert into body and continue*)
wenzelm@27174
    77
          let
wenzelm@32740
    78
            val cname = "sko_" ^ s ^ "_" ^ Int.toString (Unsynchronized.inc nref)
wenzelm@29265
    79
            val args0 = OldTerm.term_frees xtp  (*get the formal parameter list*)
wenzelm@27174
    80
            val Ts = map type_of args0
wenzelm@27174
    81
            val extraTs = rhs_extra_types (Ts ---> T) xtp
wenzelm@27174
    82
            val argsx = map (fn T => Free (gensym "vsk", T)) extraTs
wenzelm@27174
    83
            val args = argsx @ args0
wenzelm@27174
    84
            val cT = extraTs ---> Ts ---> T
wenzelm@27174
    85
            val rhs = list_abs_free (map dest_Free args, HOLogic.choice_const T $ xtp)
wenzelm@27174
    86
                    (*Forms a lambda-abstraction over the formal parameters*)
wenzelm@28110
    87
            val (c, thy') =
wenzelm@33173
    88
              Sign.declare_const ((Binding.conceal (Binding.name cname), cT), NoSyn) thy
wenzelm@27174
    89
            val cdef = cname ^ "_def"
wenzelm@33222
    90
            val thy'' =
wenzelm@33222
    91
              Theory.add_defs_i true false [(Binding.name cdef, Logic.mk_equals (c, rhs))] thy'
haftmann@28965
    92
            val ax = Thm.axiom thy'' (Sign.full_bname thy'' cdef)
wenzelm@27174
    93
          in dec_sko (subst_bound (list_comb (c, args), p)) (ax :: axs, thy'') end
wenzelm@32994
    94
      | dec_sko (Const ("All", _) $ (Abs (a, T, p))) thx =
wenzelm@27174
    95
          (*Universal quant: insert a free variable into body and continue*)
wenzelm@29270
    96
          let val fname = Name.variant (OldTerm.add_term_names (p, [])) a
wenzelm@27174
    97
          in dec_sko (subst_bound (Free (fname, T), p)) thx end
wenzelm@27174
    98
      | dec_sko (Const ("op &", _) $ p $ q) thx = dec_sko q (dec_sko p thx)
wenzelm@27174
    99
      | dec_sko (Const ("op |", _) $ p $ q) thx = dec_sko q (dec_sko p thx)
wenzelm@27174
   100
      | dec_sko (Const ("Trueprop", _) $ p) thx = dec_sko p thx
wenzelm@27174
   101
      | dec_sko t thx = thx (*Do nothing otherwise*)
wenzelm@27174
   102
  in fn thy => dec_sko (Thm.prop_of th) ([], thy) end;
paulson@18141
   103
paulson@18141
   104
(*Traverse a theorem, accumulating Skolem function definitions.*)
paulson@22731
   105
fun assume_skofuns s th =
wenzelm@33222
   106
  let val sko_count = Unsynchronized.ref 0   (* FIXME ??? *)
paulson@22731
   107
      fun dec_sko (Const ("Ex",_) $ (xtp as Abs(_,T,p))) defs =
wenzelm@20461
   108
            (*Existential: declare a Skolem function, then insert into body and continue*)
wenzelm@20461
   109
            let val skos = map (#1 o Logic.dest_equals) defs  (*existing sko fns*)
haftmann@33040
   110
                val args = subtract (op =) skos (OldTerm.term_frees xtp) (*the formal parameters*)
wenzelm@20461
   111
                val Ts = map type_of args
wenzelm@20461
   112
                val cT = Ts ---> T
wenzelm@32740
   113
                val id = "sko_" ^ s ^ "_" ^ Int.toString (Unsynchronized.inc sko_count)
paulson@22731
   114
                val c = Free (id, cT)
wenzelm@20461
   115
                val rhs = list_abs_free (map dest_Free args,
wenzelm@20461
   116
                                         HOLogic.choice_const T $ xtp)
wenzelm@20461
   117
                      (*Forms a lambda-abstraction over the formal parameters*)
wenzelm@27330
   118
                val def = Logic.mk_equals (c, rhs)
wenzelm@20461
   119
            in dec_sko (subst_bound (list_comb(c,args), p))
wenzelm@20461
   120
                       (def :: defs)
wenzelm@20461
   121
            end
wenzelm@32994
   122
        | dec_sko (Const ("All",_) $ Abs (a, T, p)) defs =
wenzelm@20461
   123
            (*Universal quant: insert a free variable into body and continue*)
wenzelm@29270
   124
            let val fname = Name.variant (OldTerm.add_term_names (p,[])) a
wenzelm@20461
   125
            in dec_sko (subst_bound (Free(fname,T), p)) defs end
wenzelm@20461
   126
        | dec_sko (Const ("op &", _) $ p $ q) defs = dec_sko q (dec_sko p defs)
wenzelm@20461
   127
        | dec_sko (Const ("op |", _) $ p $ q) defs = dec_sko q (dec_sko p defs)
wenzelm@20461
   128
        | dec_sko (Const ("Trueprop", _) $ p) defs = dec_sko p defs
wenzelm@20461
   129
        | dec_sko t defs = defs (*Do nothing otherwise*)
paulson@20419
   130
  in  dec_sko (prop_of th) []  end;
paulson@20419
   131
paulson@20419
   132
paulson@24827
   133
(**** REPLACING ABSTRACTIONS BY COMBINATORS ****)
paulson@20419
   134
paulson@20419
   135
(*Returns the vars of a theorem*)
paulson@20419
   136
fun vars_of_thm th =
wenzelm@22691
   137
  map (Thm.cterm_of (theory_of_thm th) o Var) (Thm.fold_terms Term.add_vars th []);
paulson@20419
   138
paulson@20419
   139
(*Make a version of fun_cong with a given variable name*)
paulson@20419
   140
local
paulson@20419
   141
    val fun_cong' = fun_cong RS asm_rl; (*renumber f, g to prevent clashes with (a,0)*)
paulson@20419
   142
    val cx = hd (vars_of_thm fun_cong');
paulson@20419
   143
    val ty = typ_of (ctyp_of_term cx);
paulson@20445
   144
    val thy = theory_of_thm fun_cong;
paulson@20419
   145
    fun mkvar a = cterm_of thy (Var((a,0),ty));
paulson@20419
   146
in
paulson@20419
   147
fun xfun_cong x = Thm.instantiate ([], [(cx, mkvar x)]) fun_cong'
paulson@20419
   148
end;
paulson@20419
   149
paulson@20863
   150
(*Removes the lambdas from an equation of the form t = (%x. u).  A non-negative n,
paulson@20863
   151
  serves as an upper bound on how many to remove.*)
paulson@20863
   152
fun strip_lambdas 0 th = th
wenzelm@24669
   153
  | strip_lambdas n th =
paulson@20863
   154
      case prop_of th of
wenzelm@24669
   155
          _ $ (Const ("op =", _) $ _ $ Abs (x,_,_)) =>
wenzelm@24669
   156
              strip_lambdas (n-1) (freeze_thm (th RS xfun_cong x))
wenzelm@24669
   157
        | _ => th;
paulson@20419
   158
wenzelm@24669
   159
val lambda_free = not o Term.has_abs;
wenzelm@20461
   160
wenzelm@32010
   161
val [f_B,g_B] = map (cterm_of @{theory}) (OldTerm.term_vars (prop_of @{thm abs_B}));
wenzelm@32010
   162
val [g_C,f_C] = map (cterm_of @{theory}) (OldTerm.term_vars (prop_of @{thm abs_C}));
wenzelm@32010
   163
val [f_S,g_S] = map (cterm_of @{theory}) (OldTerm.term_vars (prop_of @{thm abs_S}));
paulson@20863
   164
paulson@24827
   165
(*FIXME: requires more use of cterm constructors*)
paulson@24827
   166
fun abstract ct =
wenzelm@28544
   167
  let
wenzelm@28544
   168
      val thy = theory_of_cterm ct
paulson@25256
   169
      val Abs(x,_,body) = term_of ct
paulson@24827
   170
      val Type("fun",[xT,bodyT]) = typ_of (ctyp_of_term ct)
paulson@24827
   171
      val cxT = ctyp_of thy xT and cbodyT = ctyp_of thy bodyT
wenzelm@27184
   172
      fun makeK() = instantiate' [SOME cxT, SOME cbodyT] [SOME (cterm_of thy body)] @{thm abs_K}
paulson@24827
   173
  in
paulson@24827
   174
      case body of
paulson@24827
   175
          Const _ => makeK()
paulson@24827
   176
        | Free _ => makeK()
paulson@24827
   177
        | Var _ => makeK()  (*though Var isn't expected*)
wenzelm@27184
   178
        | Bound 0 => instantiate' [SOME cxT] [] @{thm abs_I} (*identity: I*)
paulson@24827
   179
        | rator$rand =>
wenzelm@27184
   180
            if loose_bvar1 (rator,0) then (*C or S*)
wenzelm@27179
   181
               if loose_bvar1 (rand,0) then (*S*)
wenzelm@27179
   182
                 let val crator = cterm_of thy (Abs(x,xT,rator))
wenzelm@27179
   183
                     val crand = cterm_of thy (Abs(x,xT,rand))
wenzelm@27184
   184
                     val abs_S' = cterm_instantiate [(f_S,crator),(g_S,crand)] @{thm abs_S}
wenzelm@27184
   185
                     val (_,rhs) = Thm.dest_equals (cprop_of abs_S')
wenzelm@27179
   186
                 in
wenzelm@27179
   187
                   Thm.transitive abs_S' (Conv.binop_conv abstract rhs)
wenzelm@27179
   188
                 end
wenzelm@27179
   189
               else (*C*)
wenzelm@27179
   190
                 let val crator = cterm_of thy (Abs(x,xT,rator))
wenzelm@27184
   191
                     val abs_C' = cterm_instantiate [(f_C,crator),(g_C,cterm_of thy rand)] @{thm abs_C}
wenzelm@27184
   192
                     val (_,rhs) = Thm.dest_equals (cprop_of abs_C')
wenzelm@27179
   193
                 in
wenzelm@27179
   194
                   Thm.transitive abs_C' (Conv.fun_conv (Conv.arg_conv abstract) rhs)
wenzelm@27179
   195
                 end
wenzelm@27184
   196
            else if loose_bvar1 (rand,0) then (*B or eta*)
wenzelm@27179
   197
               if rand = Bound 0 then eta_conversion ct
wenzelm@27179
   198
               else (*B*)
wenzelm@27179
   199
                 let val crand = cterm_of thy (Abs(x,xT,rand))
wenzelm@27179
   200
                     val crator = cterm_of thy rator
wenzelm@27184
   201
                     val abs_B' = cterm_instantiate [(f_B,crator),(g_B,crand)] @{thm abs_B}
wenzelm@27184
   202
                     val (_,rhs) = Thm.dest_equals (cprop_of abs_B')
wenzelm@27179
   203
                 in
wenzelm@27179
   204
                   Thm.transitive abs_B' (Conv.arg_conv abstract rhs)
wenzelm@27179
   205
                 end
wenzelm@27179
   206
            else makeK()
paulson@24827
   207
        | _ => error "abstract: Bad term"
paulson@24827
   208
  end;
paulson@20863
   209
paulson@20419
   210
(*Traverse a theorem, declaring abstraction function definitions. String s is the suggested
wenzelm@28544
   211
  prefix for the constants.*)
paulson@24827
   212
fun combinators_aux ct =
paulson@24827
   213
  if lambda_free (term_of ct) then reflexive ct
paulson@24827
   214
  else
paulson@24827
   215
  case term_of ct of
paulson@24827
   216
      Abs _ =>
wenzelm@32994
   217
        let val (cv, cta) = Thm.dest_abs NONE ct
wenzelm@32994
   218
            val (v, _) = dest_Free (term_of cv)
wenzelm@27179
   219
            val u_th = combinators_aux cta
wenzelm@27179
   220
            val cu = Thm.rhs_of u_th
wenzelm@27179
   221
            val comb_eq = abstract (Thm.cabs cv cu)
wenzelm@28544
   222
        in transitive (abstract_rule v cv u_th) comb_eq end
wenzelm@32994
   223
    | _ $ _ =>
wenzelm@32994
   224
        let val (ct1, ct2) = Thm.dest_comb ct
wenzelm@27179
   225
        in  combination (combinators_aux ct1) (combinators_aux ct2)  end;
wenzelm@27184
   226
paulson@24827
   227
fun combinators th =
wenzelm@27184
   228
  if lambda_free (prop_of th) then th
paulson@24827
   229
  else
wenzelm@28544
   230
    let val th = Drule.eta_contraction_rule th
wenzelm@27179
   231
        val eqth = combinators_aux (cprop_of th)
paulson@25256
   232
    in  equal_elim eqth th   end
wenzelm@27184
   233
    handle THM (msg,_,_) =>
wenzelm@32091
   234
      (warning (cat_lines
wenzelm@32091
   235
        ["Error in the combinator translation of " ^ Display.string_of_thm_without_context th,
wenzelm@32091
   236
          "  Exception message: " ^ msg]);
paulson@25256
   237
       TrueI);  (*A type variable of sort {} will cause make abstraction fail.*)
paulson@16009
   238
paulson@16009
   239
(*cterms are used throughout for efficiency*)
wenzelm@29064
   240
val cTrueprop = Thm.cterm_of @{theory HOL} HOLogic.Trueprop;
paulson@16009
   241
paulson@16009
   242
(*cterm version of mk_cTrueprop*)
paulson@16009
   243
fun c_mkTrueprop A = Thm.capply cTrueprop A;
paulson@16009
   244
paulson@16009
   245
(*Given an abstraction over n variables, replace the bound variables by free
paulson@16009
   246
  ones. Return the body, along with the list of free variables.*)
wenzelm@20461
   247
fun c_variant_abs_multi (ct0, vars) =
paulson@16009
   248
      let val (cv,ct) = Thm.dest_abs NONE ct0
paulson@16009
   249
      in  c_variant_abs_multi (ct, cv::vars)  end
paulson@16009
   250
      handle CTERM _ => (ct0, rev vars);
paulson@16009
   251
wenzelm@20461
   252
(*Given the definition of a Skolem function, return a theorem to replace
wenzelm@20461
   253
  an existential formula by a use of that function.
paulson@18141
   254
   Example: "EX x. x : A & x ~: B ==> sko A B : A & sko A B ~: B"  [.] *)
wenzelm@20461
   255
fun skolem_of_def def =
wenzelm@22902
   256
  let val (c,rhs) = Thm.dest_equals (cprop_of (freeze_thm def))
paulson@16009
   257
      val (ch, frees) = c_variant_abs_multi (rhs, [])
paulson@18141
   258
      val (chilbert,cabs) = Thm.dest_comb ch
wenzelm@26627
   259
      val thy = Thm.theory_of_cterm chilbert
wenzelm@26627
   260
      val t = Thm.term_of chilbert
paulson@18141
   261
      val T = case t of Const ("Hilbert_Choice.Eps", Type("fun",[_,T])) => T
paulson@18141
   262
                      | _ => raise THM ("skolem_of_def: expected Eps", 0, [def])
wenzelm@22596
   263
      val cex = Thm.cterm_of thy (HOLogic.exists_const T)
paulson@16009
   264
      val ex_tm = c_mkTrueprop (Thm.capply cex cabs)
paulson@16009
   265
      and conc =  c_mkTrueprop (Drule.beta_conv cabs (Drule.list_comb(c,frees)));
haftmann@31454
   266
      fun tacf [prem] = rewrite_goals_tac [def] THEN rtac (prem RS @{thm someI_ex}) 1
wenzelm@23352
   267
  in  Goal.prove_internal [ex_tm] conc tacf
paulson@18141
   268
       |> forall_intr_list frees
wenzelm@26653
   269
       |> Thm.forall_elim_vars 0  (*Introduce Vars, but don't discharge defs.*)
paulson@18141
   270
       |> Thm.varifyT
paulson@18141
   271
  end;
paulson@16009
   272
paulson@24742
   273
paulson@20863
   274
(*Converts an Isabelle theorem (intro, elim or simp format, even higher-order) into NNF.*)
paulson@24937
   275
fun to_nnf th ctxt0 =
wenzelm@27179
   276
  let val th1 = th |> transform_elim |> zero_var_indexes
wenzelm@32262
   277
      val ((_, [th2]), ctxt) = Variable.import true [th1] ctxt0
wenzelm@32262
   278
      val th3 = th2
wenzelm@35625
   279
        |> Conv.fconv_rule Object_Logic.atomize
wenzelm@32262
   280
        |> Meson.make_nnf ctxt |> strip_lambdas ~1
paulson@24937
   281
  in  (th3, ctxt)  end;
paulson@16009
   282
paulson@18141
   283
(*Generate Skolem functions for a theorem supplied in nnf*)
paulson@24937
   284
fun assume_skolem_of_def s th =
paulson@22731
   285
  map (skolem_of_def o assume o (cterm_of (theory_of_thm th))) (assume_skofuns s th);
paulson@18141
   286
paulson@25007
   287
blanchet@35826
   288
(*** Blacklisting (duplicated in "Sledgehammer_Fact_Filter"?) ***)
paulson@25007
   289
paulson@25007
   290
val max_lambda_nesting = 3;
wenzelm@27184
   291
paulson@25007
   292
fun excessive_lambdas (f$t, k) = excessive_lambdas (f,k) orelse excessive_lambdas (t,k)
paulson@25007
   293
  | excessive_lambdas (Abs(_,_,t), k) = k=0 orelse excessive_lambdas (t,k-1)
paulson@25007
   294
  | excessive_lambdas _ = false;
paulson@25007
   295
paulson@25007
   296
fun is_formula_type T = (T = HOLogic.boolT orelse T = propT);
paulson@25007
   297
paulson@25007
   298
(*Don't count nested lambdas at the level of formulas, as they are quantifiers*)
paulson@25007
   299
fun excessive_lambdas_fm Ts (Abs(_,T,t)) = excessive_lambdas_fm (T::Ts) t
paulson@25007
   300
  | excessive_lambdas_fm Ts t =
paulson@25007
   301
      if is_formula_type (fastype_of1 (Ts, t))
paulson@25007
   302
      then exists (excessive_lambdas_fm Ts) (#2 (strip_comb t))
paulson@25007
   303
      else excessive_lambdas (t, max_lambda_nesting);
paulson@25007
   304
wenzelm@33027
   305
(*The max apply_depth of any metis call in Metis_Examples (on 31-10-2007) was 11.*)
paulson@25256
   306
val max_apply_depth = 15;
wenzelm@27184
   307
paulson@25256
   308
fun apply_depth (f$t) = Int.max (apply_depth f, apply_depth t + 1)
paulson@25256
   309
  | apply_depth (Abs(_,_,t)) = apply_depth t
paulson@25256
   310
  | apply_depth _ = 0;
paulson@25256
   311
wenzelm@27184
   312
fun too_complex t =
wenzelm@27184
   313
  apply_depth t > max_apply_depth orelse
paulson@26562
   314
  Meson.too_many_clauses NONE t orelse
paulson@25256
   315
  excessive_lambdas_fm [] t;
wenzelm@27184
   316
paulson@25243
   317
fun is_strange_thm th =
paulson@25243
   318
  case head_of (concl_of th) of
wenzelm@33306
   319
      Const (a, _) => (a <> "Trueprop" andalso a <> "==")
paulson@25243
   320
    | _ => false;
paulson@25243
   321
wenzelm@27184
   322
fun bad_for_atp th =
wenzelm@33306
   323
  too_complex (prop_of th)
wenzelm@35568
   324
  orelse exists_type type_has_topsort (prop_of th)
paulson@25761
   325
  orelse is_strange_thm th;
paulson@25243
   326
paulson@25007
   327
val multi_base_blacklist =
paulson@25256
   328
  ["defs","select_defs","update_defs","induct","inducts","split","splits","split_asm",
wenzelm@33306
   329
   "cases","ext_cases"];  (* FIXME put other record thms here, or declare as "noatp" *)
paulson@25007
   330
paulson@21071
   331
(*Keep the full complexity of the original name*)
wenzelm@30364
   332
fun flatten_name s = space_implode "_X" (Long_Name.explode s);
paulson@21071
   333
paulson@22731
   334
fun fake_name th =
wenzelm@27865
   335
  if Thm.has_name_hint th then flatten_name (Thm.get_name_hint th)
paulson@22731
   336
  else gensym "unknown_thm_";
paulson@22731
   337
wenzelm@27184
   338
(*Skolemize a named theorem, with Skolem functions as additional premises.*)
wenzelm@27184
   339
fun skolem_thm (s, th) =
wenzelm@30364
   340
  if member (op =) multi_base_blacklist (Long_Name.base_name s) orelse bad_for_atp th then []
wenzelm@27184
   341
  else
wenzelm@27184
   342
    let
wenzelm@27184
   343
      val ctxt0 = Variable.thm_context th
wenzelm@27184
   344
      val (nnfth, ctxt1) = to_nnf th ctxt0
wenzelm@27184
   345
      val (cnfs, ctxt2) = Meson.make_cnf (assume_skolem_of_def s nnfth) nnfth ctxt1
wenzelm@27184
   346
    in  cnfs |> map combinators |> Variable.export ctxt2 ctxt0 |> Meson.finish_cnf  end
wenzelm@27184
   347
    handle THM _ => [];
wenzelm@27184
   348
paulson@24742
   349
(*The cache prevents repeated clausification of a theorem, and also repeated declaration of
paulson@24742
   350
  Skolem functions.*)
wenzelm@33522
   351
structure ThmCache = Theory_Data
wenzelm@22846
   352
(
wenzelm@28544
   353
  type T = thm list Thmtab.table * unit Symtab.table;
wenzelm@28544
   354
  val empty = (Thmtab.empty, Symtab.empty);
wenzelm@26618
   355
  val extend = I;
wenzelm@33522
   356
  fun merge ((cache1, seen1), (cache2, seen2)) : T =
wenzelm@27184
   357
    (Thmtab.merge (K true) (cache1, cache2), Symtab.merge (K true) (seen1, seen2));
wenzelm@22846
   358
);
paulson@22516
   359
wenzelm@27184
   360
val lookup_cache = Thmtab.lookup o #1 o ThmCache.get;
wenzelm@27184
   361
val already_seen = Symtab.defined o #2 o ThmCache.get;
wenzelm@20461
   362
wenzelm@27184
   363
val update_cache = ThmCache.map o apfst o Thmtab.update;
wenzelm@27184
   364
fun mark_seen name = ThmCache.map (apsnd (Symtab.update (name, ())));
paulson@25007
   365
wenzelm@20461
   366
(*Exported function to convert Isabelle theorems into axiom clauses*)
wenzelm@27179
   367
fun cnf_axiom thy th0 =
wenzelm@27184
   368
  let val th = Thm.transfer thy th0 in
wenzelm@27184
   369
    case lookup_cache thy th of
wenzelm@27184
   370
      NONE => map Thm.close_derivation (skolem_thm (fake_name th, th))
wenzelm@27184
   371
    | SOME cls => cls
paulson@22516
   372
  end;
paulson@15347
   373
paulson@18141
   374
wenzelm@30291
   375
(**** Rules from the context ****)
paulson@15347
   376
wenzelm@27865
   377
fun pairname th = (Thm.get_name_hint th, th);
wenzelm@27184
   378
paulson@15347
   379
paulson@22471
   380
(**** Translate a set of theorems into CNF ****)
paulson@15347
   381
paulson@19894
   382
fun pair_name_cls k (n, []) = []
paulson@19894
   383
  | pair_name_cls k (n, cls::clss) = (cls, (n,k)) :: pair_name_cls (k+1) (n, clss)
wenzelm@20461
   384
wenzelm@27179
   385
fun cnf_rules_pairs_aux _ pairs [] = pairs
wenzelm@27179
   386
  | cnf_rules_pairs_aux thy pairs ((name,th)::ths) =
wenzelm@27179
   387
      let val pairs' = (pair_name_cls 0 (name, cnf_axiom thy th)) @ pairs
blanchet@35826
   388
                       handle THM _ => pairs |
blanchet@35826
   389
                              Sledgehammer_FOL_Clause.CLAUSE _ => pairs
wenzelm@27179
   390
      in  cnf_rules_pairs_aux thy pairs' ths  end;
wenzelm@20461
   391
paulson@21290
   392
(*The combination of rev and tail recursion preserves the original order*)
wenzelm@27179
   393
fun cnf_rules_pairs thy l = cnf_rules_pairs_aux thy [] (rev l);
mengj@19353
   394
mengj@19196
   395
blanchet@35826
   396
(**** Convert all facts of the theory into clauses
blanchet@35826
   397
      (Sledgehammer_FOL_Clause.clause, or Sledgehammer_HOL_Clause.clause) ****)
paulson@15347
   398
wenzelm@28544
   399
local
wenzelm@28544
   400
wenzelm@28544
   401
fun skolem_def (name, th) thy =
wenzelm@28544
   402
  let val ctxt0 = Variable.thm_context th in
wenzelm@28544
   403
    (case try (to_nnf th) ctxt0 of
wenzelm@28544
   404
      NONE => (NONE, thy)
wenzelm@28544
   405
    | SOME (nnfth, ctxt1) =>
wenzelm@28544
   406
        let val (defs, thy') = declare_skofuns (flatten_name name) nnfth thy
wenzelm@28544
   407
        in (SOME (th, ctxt0, ctxt1, nnfth, defs), thy') end)
wenzelm@28544
   408
  end;
paulson@24742
   409
wenzelm@28544
   410
fun skolem_cnfs (th, ctxt0, ctxt1, nnfth, defs) =
wenzelm@28544
   411
  let
wenzelm@28544
   412
    val (cnfs, ctxt2) = Meson.make_cnf (map skolem_of_def defs) nnfth ctxt1;
wenzelm@28544
   413
    val cnfs' = cnfs
wenzelm@28544
   414
      |> map combinators
wenzelm@28544
   415
      |> Variable.export ctxt2 ctxt0
wenzelm@28544
   416
      |> Meson.finish_cnf
wenzelm@28544
   417
      |> map Thm.close_derivation;
wenzelm@28544
   418
    in (th, cnfs') end;
wenzelm@28544
   419
wenzelm@28544
   420
in
paulson@24742
   421
wenzelm@27184
   422
fun saturate_skolem_cache thy =
wenzelm@28544
   423
  let
wenzelm@33306
   424
    val facts = PureThy.facts_of thy;
wenzelm@33306
   425
    val new_facts = (facts, []) |-> Facts.fold_static (fn (name, ths) =>
wenzelm@33306
   426
      if Facts.is_concealed facts name orelse already_seen thy name then I
wenzelm@33306
   427
      else cons (name, ths));
wenzelm@28544
   428
    val new_thms = (new_facts, []) |-> fold (fn (name, ths) =>
wenzelm@30364
   429
      if member (op =) multi_base_blacklist (Long_Name.base_name name) then I
wenzelm@28544
   430
      else fold_index (fn (i, th) =>
wenzelm@28544
   431
        if bad_for_atp th orelse is_some (lookup_cache thy th) then I
wenzelm@28544
   432
        else cons (name ^ "_" ^ string_of_int (i + 1), Thm.transfer thy th)) ths);
wenzelm@28544
   433
  in
wenzelm@28544
   434
    if null new_facts then NONE
wenzelm@28544
   435
    else
wenzelm@28544
   436
      let
wenzelm@28544
   437
        val (defs, thy') = thy
wenzelm@28544
   438
          |> fold (mark_seen o #1) new_facts
wenzelm@28544
   439
          |> fold_map skolem_def (sort_distinct (Thm.thm_ord o pairself snd) new_thms)
wenzelm@28544
   440
          |>> map_filter I;
wenzelm@29368
   441
        val cache_entries = Par_List.map skolem_cnfs defs;
wenzelm@28544
   442
      in SOME (fold update_cache cache_entries thy') end
wenzelm@28544
   443
  end;
wenzelm@27184
   444
wenzelm@28544
   445
end;
paulson@24854
   446
wenzelm@32740
   447
val suppress_endtheory = Unsynchronized.ref false;
wenzelm@27184
   448
wenzelm@27184
   449
fun clause_cache_endtheory thy =
wenzelm@27184
   450
  if ! suppress_endtheory then NONE
wenzelm@27184
   451
  else saturate_skolem_cache thy;
wenzelm@27184
   452
paulson@20457
   453
paulson@22516
   454
(*The cache can be kept smaller by inspecting the prop of each thm. Can ignore all that are
paulson@22516
   455
  lambda_free, but then the individual theory caches become much bigger.*)
paulson@21071
   456
wenzelm@27179
   457
paulson@16563
   458
(*** meson proof methods ***)
paulson@16563
   459
wenzelm@28544
   460
(*Expand all new definitions of abstraction or Skolem functions in a proof state.*)
paulson@24827
   461
fun is_absko (Const ("==", _) $ Free (a,_) $ u) = String.isPrefix "sko_" a
paulson@22731
   462
  | is_absko _ = false;
paulson@22731
   463
paulson@22731
   464
fun is_okdef xs (Const ("==", _) $ t $ u) =   (*Definition of Free, not in certain terms*)
paulson@22731
   465
      is_Free t andalso not (member (op aconv) xs t)
paulson@22731
   466
  | is_okdef _ _ = false
paulson@22724
   467
paulson@24215
   468
(*This function tries to cope with open locales, which introduce hypotheses of the form
paulson@24215
   469
  Free == t, conjecture clauses, which introduce various hypotheses, and also definitions
paulson@24827
   470
  of sko_ functions. *)
paulson@22731
   471
fun expand_defs_tac st0 st =
paulson@22731
   472
  let val hyps0 = #hyps (rep_thm st0)
paulson@22731
   473
      val hyps = #hyps (crep_thm st)
paulson@22731
   474
      val newhyps = filter_out (member (op aconv) hyps0 o Thm.term_of) hyps
paulson@22731
   475
      val defs = filter (is_absko o Thm.term_of) newhyps
wenzelm@24669
   476
      val remaining_hyps = filter_out (member (op aconv) (map Thm.term_of defs))
paulson@22731
   477
                                      (map Thm.term_of hyps)
wenzelm@29265
   478
      val fixed = OldTerm.term_frees (concl_of st) @
wenzelm@33339
   479
                  fold (union (op aconv)) (map OldTerm.term_frees remaining_hyps) []
wenzelm@35624
   480
  in Seq.of_list [Local_Defs.expand (filter (is_okdef fixed o Thm.term_of) defs) st] end;
paulson@22724
   481
paulson@22731
   482
wenzelm@32262
   483
fun meson_general_tac ctxt ths i st0 =
wenzelm@27179
   484
  let
wenzelm@32262
   485
    val thy = ProofContext.theory_of ctxt
wenzelm@32262
   486
    val ctxt0 = Classical.put_claset HOL_cs ctxt
wenzelm@32262
   487
  in (Meson.meson_tac ctxt0 (maps (cnf_axiom thy) ths) i THEN expand_defs_tac st0) st0 end;
paulson@22724
   488
wenzelm@30515
   489
val meson_method_setup =
wenzelm@32262
   490
  Method.setup @{binding meson} (Attrib.thms >> (fn ths => fn ctxt =>
wenzelm@32262
   491
    SIMPLE_METHOD' (CHANGED_PROP o meson_general_tac ctxt ths)))
wenzelm@30515
   492
    "MESON resolution proof procedure";
paulson@15347
   493
wenzelm@27179
   494
paulson@21999
   495
(*** Converting a subgoal into negated conjecture clauses. ***)
paulson@21999
   496
wenzelm@32262
   497
fun neg_skolemize_tac ctxt =
wenzelm@35625
   498
  EVERY' [rtac ccontr, Object_Logic.atomize_prems_tac, Meson.skolemize_tac ctxt];
paulson@22471
   499
wenzelm@32257
   500
val neg_clausify = Meson.make_clauses #> map combinators #> Meson.finish_cnf;
paulson@21999
   501
wenzelm@32257
   502
fun neg_conjecture_clauses ctxt st0 n =
wenzelm@32257
   503
  let
wenzelm@32262
   504
    val st = Seq.hd (neg_skolemize_tac ctxt n st0)
wenzelm@32257
   505
    val ({params, prems, ...}, _) = Subgoal.focus (Variable.set_body false ctxt) n st
wenzelm@32257
   506
  in (neg_clausify prems, map (Term.dest_Free o Thm.term_of o #2) params) end;
paulson@21999
   507
wenzelm@24669
   508
(*Conversion of a subgoal to conjecture clauses. Each clause has
paulson@21999
   509
  leading !!-bound universal variables, to express generality. *)
wenzelm@32257
   510
fun neg_clausify_tac ctxt =
wenzelm@32262
   511
  neg_skolemize_tac ctxt THEN'
wenzelm@32257
   512
  SUBGOAL (fn (prop, i) =>
wenzelm@32257
   513
    let val ts = Logic.strip_assums_hyp prop in
wenzelm@32257
   514
      EVERY'
wenzelm@32283
   515
       [Subgoal.FOCUS
wenzelm@32257
   516
         (fn {prems, ...} =>
wenzelm@32257
   517
           (Method.insert_tac
wenzelm@32257
   518
             (map forall_intr_vars (neg_clausify prems)) i)) ctxt,
wenzelm@32257
   519
        REPEAT_DETERM_N (length ts) o etac thin_rl] i
paulson@21999
   520
     end);
paulson@21999
   521
wenzelm@30722
   522
val neg_clausify_setup =
wenzelm@32257
   523
  Method.setup @{binding neg_clausify} (Scan.succeed (SIMPLE_METHOD' o neg_clausify_tac))
wenzelm@30515
   524
  "conversion of goal to conjecture clauses";
wenzelm@24669
   525
wenzelm@27184
   526
wenzelm@27184
   527
(** Attribute for converting a theorem into clauses **)
wenzelm@27184
   528
wenzelm@30722
   529
val clausify_setup =
wenzelm@30722
   530
  Attrib.setup @{binding clausify}
wenzelm@30722
   531
    (Scan.lift OuterParse.nat >>
wenzelm@30722
   532
      (fn i => Thm.rule_attribute (fn context => fn th =>
wenzelm@30722
   533
          Meson.make_meta_clause (nth (cnf_axiom (Context.theory_of context) th) i))))
wenzelm@30722
   534
  "conversion of theorem to clauses";
wenzelm@27184
   535
wenzelm@27184
   536
wenzelm@27184
   537
wenzelm@27184
   538
(** setup **)
wenzelm@27184
   539
wenzelm@27184
   540
val setup =
wenzelm@27184
   541
  meson_method_setup #>
wenzelm@30722
   542
  neg_clausify_setup #>
wenzelm@30722
   543
  clausify_setup #>
wenzelm@27184
   544
  perhaps saturate_skolem_cache #>
wenzelm@27184
   545
  Theory.at_end clause_cache_endtheory;
paulson@18510
   546
wenzelm@20461
   547
end;