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