src/HOL/Tools/Sledgehammer/sledgehammer_fact.ML
author blanchet
Wed, 18 Jul 2012 08:44:04 +0200
changeset 49342 568b3193e53e
parent 49314 5e5c6616f0fe
child 49347 271a4a6af734
permissions -rw-r--r--
don't include hidden facts in relevance filter + tweak MaSh learning
blanchet@49265
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_fact.ML
blanchet@49265
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
blanchet@49265
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@49265
     4
blanchet@49265
     5
Sledgehammer fact handling.
blanchet@49265
     6
*)
blanchet@49265
     7
blanchet@49265
     8
signature SLEDGEHAMMER_FACT =
blanchet@49265
     9
sig
blanchet@49265
    10
  type status = ATP_Problem_Generate.status
blanchet@49265
    11
  type stature = ATP_Problem_Generate.stature
blanchet@49265
    12
blanchet@49311
    13
  type fact = ((unit -> string) * stature) * thm
blanchet@49311
    14
blanchet@49307
    15
  type fact_override =
blanchet@49265
    16
    {add : (Facts.ref * Attrib.src list) list,
blanchet@49265
    17
     del : (Facts.ref * Attrib.src list) list,
blanchet@49265
    18
     only : bool}
blanchet@49265
    19
blanchet@49265
    20
  val ignore_no_atp : bool Config.T
blanchet@49265
    21
  val instantiate_inducts : bool Config.T
blanchet@49307
    22
  val no_fact_override : fact_override
blanchet@49265
    23
  val fact_from_ref :
blanchet@49265
    24
    Proof.context -> unit Symtab.table -> thm list -> status Termtab.table
blanchet@49265
    25
    -> Facts.ref * Attrib.src list -> ((string * stature) * thm) list
blanchet@49265
    26
  val clasimpset_rule_table_of : Proof.context -> status Termtab.table
blanchet@49265
    27
  val maybe_instantiate_inducts :
blanchet@49265
    28
    Proof.context -> term list -> term -> (((unit -> string) * 'a) * thm) list
blanchet@49265
    29
    -> (((unit -> string) * 'a) * thm) list
blanchet@49265
    30
  val maybe_filter_no_atps : Proof.context -> ('a * thm) list -> ('a * thm) list
blanchet@49314
    31
  val all_facts_of : theory -> status Termtab.table -> fact list
blanchet@49265
    32
  val nearly_all_facts :
blanchet@49314
    33
    Proof.context -> bool -> fact_override -> unit Symtab.table
blanchet@49314
    34
    -> status Termtab.table -> thm list -> term list -> term -> fact list
blanchet@49265
    35
end;
blanchet@49265
    36
blanchet@49265
    37
structure Sledgehammer_Fact : SLEDGEHAMMER_FACT =
blanchet@49265
    38
struct
blanchet@49265
    39
blanchet@49265
    40
open ATP_Problem_Generate
blanchet@49265
    41
open Metis_Tactic
blanchet@49265
    42
open Sledgehammer_Util
blanchet@49265
    43
blanchet@49311
    44
type fact = ((unit -> string) * stature) * thm
blanchet@49311
    45
blanchet@49307
    46
type fact_override =
blanchet@49265
    47
  {add : (Facts.ref * Attrib.src list) list,
blanchet@49265
    48
   del : (Facts.ref * Attrib.src list) list,
blanchet@49265
    49
   only : bool}
blanchet@49265
    50
blanchet@49265
    51
val sledgehammer_prefix = "Sledgehammer" ^ Long_Name.separator
blanchet@49265
    52
blanchet@49265
    53
(* experimental features *)
blanchet@49265
    54
val ignore_no_atp =
blanchet@49265
    55
  Attrib.setup_config_bool @{binding sledgehammer_ignore_no_atp} (K false)
blanchet@49265
    56
val instantiate_inducts =
blanchet@49265
    57
  Attrib.setup_config_bool @{binding sledgehammer_instantiate_inducts} (K false)
blanchet@49265
    58
blanchet@49307
    59
val no_fact_override = {add = [], del = [], only = false}
blanchet@49265
    60
blanchet@49265
    61
fun needs_quoting reserved s =
blanchet@49265
    62
  Symtab.defined reserved s orelse
blanchet@49265
    63
  exists (not o Lexicon.is_identifier) (Long_Name.explode s)
blanchet@49265
    64
blanchet@49265
    65
fun make_name reserved multi j name =
blanchet@49265
    66
  (name |> needs_quoting reserved name ? quote) ^
blanchet@49265
    67
  (if multi then "(" ^ string_of_int j ^ ")" else "")
blanchet@49265
    68
blanchet@49265
    69
fun explode_interval _ (Facts.FromTo (i, j)) = i upto j
blanchet@49265
    70
  | explode_interval max (Facts.From i) = i upto i + max - 1
blanchet@49265
    71
  | explode_interval _ (Facts.Single i) = [i]
blanchet@49265
    72
blanchet@49265
    73
val backquote =
blanchet@49265
    74
  raw_explode #> map (fn "`" => "\\`" | s => s) #> implode #> enclose "`" "`"
blanchet@49265
    75
blanchet@49265
    76
(* unfolding these can yield really huge terms *)
blanchet@49265
    77
val risky_defs = @{thms Bit0_def Bit1_def}
blanchet@49265
    78
blanchet@49265
    79
fun is_rec_eq lhs = Term.exists_subterm (curry (op =) (head_of lhs))
blanchet@49265
    80
fun is_rec_def (@{const Trueprop} $ t) = is_rec_def t
blanchet@49265
    81
  | is_rec_def (@{const ==>} $ _ $ t2) = is_rec_def t2
blanchet@49265
    82
  | is_rec_def (Const (@{const_name "=="}, _) $ t1 $ t2) = is_rec_eq t1 t2
blanchet@49265
    83
  | is_rec_def (Const (@{const_name HOL.eq}, _) $ t1 $ t2) = is_rec_eq t1 t2
blanchet@49265
    84
  | is_rec_def _ = false
blanchet@49265
    85
blanchet@49265
    86
fun is_assum assms th = exists (fn ct => prop_of th aconv term_of ct) assms
blanchet@49265
    87
fun is_chained chained_ths = member Thm.eq_thm_prop chained_ths
blanchet@49265
    88
blanchet@49265
    89
fun scope_of_thm global assms chained_ths th =
blanchet@49265
    90
  if is_chained chained_ths th then Chained
blanchet@49265
    91
  else if global then Global
blanchet@49265
    92
  else if is_assum assms th then Assum
blanchet@49265
    93
  else Local
blanchet@49265
    94
blanchet@49265
    95
val may_be_induction =
blanchet@49265
    96
  exists_subterm (fn Var (_, Type (@{type_name fun}, [_, T])) =>
blanchet@49265
    97
                     body_type T = @{typ bool}
blanchet@49265
    98
                   | _ => false)
blanchet@49265
    99
blanchet@49265
   100
fun status_of_thm css_table name th =
blanchet@49265
   101
  (* FIXME: use structured name *)
blanchet@49265
   102
  if (String.isSubstring ".induct" name orelse
blanchet@49265
   103
      String.isSubstring ".inducts" name) andalso
blanchet@49265
   104
     may_be_induction (prop_of th) then
blanchet@49265
   105
    Induction
blanchet@49265
   106
  else case Termtab.lookup css_table (prop_of th) of
blanchet@49265
   107
    SOME status => status
blanchet@49265
   108
  | NONE => General
blanchet@49265
   109
blanchet@49265
   110
fun stature_of_thm global assms chained_ths css_table name th =
blanchet@49265
   111
  (scope_of_thm global assms chained_ths th, status_of_thm css_table name th)
blanchet@49265
   112
blanchet@49265
   113
fun fact_from_ref ctxt reserved chained_ths css_table (xthm as (xref, args)) =
blanchet@49265
   114
  let
blanchet@49265
   115
    val ths = Attrib.eval_thms ctxt [xthm]
blanchet@49265
   116
    val bracket =
blanchet@49265
   117
      map (enclose "[" "]" o Pretty.str_of o Args.pretty_src ctxt) args
blanchet@49265
   118
      |> implode
blanchet@49265
   119
    fun nth_name j =
blanchet@49265
   120
      case xref of
blanchet@49265
   121
        Facts.Fact s => backquote s ^ bracket
blanchet@49265
   122
      | Facts.Named (("", _), _) => "[" ^ bracket ^ "]"
blanchet@49265
   123
      | Facts.Named ((name, _), NONE) =>
blanchet@49265
   124
        make_name reserved (length ths > 1) (j + 1) name ^ bracket
blanchet@49265
   125
      | Facts.Named ((name, _), SOME intervals) =>
blanchet@49265
   126
        make_name reserved true
blanchet@49265
   127
                 (nth (maps (explode_interval (length ths)) intervals) j) name ^
blanchet@49265
   128
        bracket
blanchet@49265
   129
  in
blanchet@49265
   130
    (ths, (0, []))
blanchet@49265
   131
    |-> fold (fn th => fn (j, rest) =>
blanchet@49265
   132
                 let val name = nth_name j in
blanchet@49265
   133
                   (j + 1, ((name, stature_of_thm false [] chained_ths
blanchet@49265
   134
                                             css_table name th), th) :: rest)
blanchet@49265
   135
                 end)
blanchet@49265
   136
    |> snd
blanchet@49265
   137
  end
blanchet@49265
   138
blanchet@49265
   139
(* Reject theorems with names like "List.filter.filter_list_def" or
blanchet@49265
   140
  "Accessible_Part.acc.defs", as these are definitions arising from packages. *)
blanchet@49265
   141
fun is_package_def a =
blanchet@49265
   142
  let val names = Long_Name.explode a in
blanchet@49265
   143
    (length names > 2 andalso not (hd names = "local") andalso
blanchet@49265
   144
     String.isSuffix "_def" a) orelse String.isSuffix "_defs" a
blanchet@49265
   145
  end
blanchet@49265
   146
blanchet@49265
   147
(* FIXME: put other record thms here, or declare as "no_atp" *)
blanchet@49265
   148
fun multi_base_blacklist ctxt ho_atp =
blanchet@49265
   149
  ["defs", "select_defs", "update_defs", "split", "splits", "split_asm",
blanchet@49265
   150
   "cases", "ext_cases", "eq.simps", "eq.refl", "nchotomy", "case_cong",
blanchet@49265
   151
   "weak_case_cong"]
blanchet@49265
   152
  |> not (ho_atp orelse (Config.get ctxt instantiate_inducts)) ?
blanchet@49265
   153
        append ["induct", "inducts"]
blanchet@49265
   154
  |> map (prefix ".")
blanchet@49265
   155
blanchet@49265
   156
val max_lambda_nesting = 3 (*only applies if not ho_atp*)
blanchet@49265
   157
blanchet@49265
   158
fun term_has_too_many_lambdas max (t1 $ t2) =
blanchet@49265
   159
    exists (term_has_too_many_lambdas max) [t1, t2]
blanchet@49265
   160
  | term_has_too_many_lambdas max (Abs (_, _, t)) =
blanchet@49265
   161
    max = 0 orelse term_has_too_many_lambdas (max - 1) t
blanchet@49265
   162
  | term_has_too_many_lambdas _ _ = false
blanchet@49265
   163
blanchet@49265
   164
(* Don't count nested lambdas at the level of formulas, since they are
blanchet@49265
   165
   quantifiers. *)
blanchet@49265
   166
fun formula_has_too_many_lambdas true _ _ = false (*i.e. ho_atp*)
blanchet@49265
   167
  | formula_has_too_many_lambdas _ Ts (Abs (_, T, t)) =
blanchet@49265
   168
      formula_has_too_many_lambdas false (T :: Ts) t
blanchet@49265
   169
  | formula_has_too_many_lambdas _ Ts t =
blanchet@49265
   170
    if member (op =) [HOLogic.boolT, propT] (fastype_of1 (Ts, t)) then
blanchet@49265
   171
      exists (formula_has_too_many_lambdas false Ts) (#2 (strip_comb t))
blanchet@49265
   172
    else
blanchet@49265
   173
      term_has_too_many_lambdas max_lambda_nesting t
blanchet@49265
   174
blanchet@49265
   175
(* The max apply depth of any "metis" call in "Metis_Examples" (on 2007-10-31)
blanchet@49265
   176
   was 11. *)
blanchet@49265
   177
val max_apply_depth = 15
blanchet@49265
   178
blanchet@49265
   179
fun apply_depth (f $ t) = Int.max (apply_depth f, apply_depth t + 1)
blanchet@49265
   180
  | apply_depth (Abs (_, _, t)) = apply_depth t
blanchet@49265
   181
  | apply_depth _ = 0
blanchet@49265
   182
blanchet@49265
   183
fun is_formula_too_complex ho_atp t =
blanchet@49265
   184
  apply_depth t > max_apply_depth orelse formula_has_too_many_lambdas ho_atp [] t
blanchet@49265
   185
blanchet@49265
   186
(* FIXME: Extend to "Meson" and "Metis" *)
blanchet@49265
   187
val exists_sledgehammer_const =
blanchet@49265
   188
  exists_Const (fn (s, _) => String.isPrefix sledgehammer_prefix s)
blanchet@49265
   189
blanchet@49265
   190
(* FIXME: make more reliable *)
blanchet@49265
   191
val exists_low_level_class_const =
blanchet@49265
   192
  exists_Const (fn (s, _) =>
blanchet@49265
   193
     s = @{const_name equal_class.equal} orelse
blanchet@49265
   194
     String.isSubstring (Long_Name.separator ^ "class" ^ Long_Name.separator) s)
blanchet@49265
   195
blanchet@49265
   196
fun is_metastrange_theorem th =
blanchet@49265
   197
  case head_of (concl_of th) of
blanchet@49265
   198
    Const (s, _) => (s <> @{const_name Trueprop} andalso
blanchet@49265
   199
                     s <> @{const_name "=="})
blanchet@49265
   200
  | _ => false
blanchet@49265
   201
blanchet@49265
   202
fun is_that_fact th =
blanchet@49265
   203
  String.isSuffix (Long_Name.separator ^ Obtain.thatN) (Thm.get_name_hint th)
blanchet@49265
   204
  andalso exists_subterm (fn Free (s, _) => s = Name.skolem Auto_Bind.thesisN
blanchet@49265
   205
                           | _ => false) (prop_of th)
blanchet@49265
   206
blanchet@49342
   207
fun is_theorem_bad_for_atps ho_atp thm =
blanchet@49265
   208
  is_metastrange_theorem thm orelse
blanchet@49342
   209
  let val t = prop_of thm in
blanchet@49342
   210
    is_formula_too_complex ho_atp t orelse exists_type type_has_top_sort t orelse
blanchet@49342
   211
    exists_sledgehammer_const t orelse exists_low_level_class_const t orelse
blanchet@49342
   212
    is_that_fact thm
blanchet@49342
   213
  end
blanchet@49265
   214
blanchet@49265
   215
fun hackish_string_for_term ctxt t =
blanchet@49265
   216
  Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN)
blanchet@49265
   217
                   (print_mode_value ())) (Syntax.string_of_term ctxt) t
blanchet@49265
   218
  |> String.translate (fn c => if Char.isPrint c then str c else "")
blanchet@49265
   219
  |> simplify_spaces
blanchet@49265
   220
blanchet@49265
   221
(* This is a terrible hack. Free variables are sometimes coded as "M__" when
blanchet@49265
   222
   they are displayed as "M" and we want to avoid clashes with these. But
blanchet@49265
   223
   sometimes it's even worse: "Ma__" encodes "M". So we simply reserve all
blanchet@49265
   224
   prefixes of all free variables. In the worse case scenario, where the fact
blanchet@49265
   225
   won't be resolved correctly, the user can fix it manually, e.g., by naming
blanchet@49265
   226
   the fact in question. Ideally we would need nothing of it, but backticks
blanchet@49265
   227
   simply don't work with schematic variables. *)
blanchet@49265
   228
fun all_prefixes_of s =
blanchet@49265
   229
  map (fn i => String.extract (s, 0, SOME i)) (1 upto size s - 1)
blanchet@49265
   230
blanchet@49265
   231
fun close_form t =
blanchet@49265
   232
  (t, [] |> Term.add_free_names t |> maps all_prefixes_of)
blanchet@49265
   233
  |> fold (fn ((s, i), T) => fn (t', taken) =>
blanchet@49265
   234
              let val s' = singleton (Name.variant_list taken) s in
blanchet@49265
   235
                ((if fastype_of t' = HOLogic.boolT then HOLogic.all_const
blanchet@49265
   236
                  else Logic.all_const) T
blanchet@49265
   237
                 $ Abs (s', T, abstract_over (Var ((s, i), T), t')),
blanchet@49265
   238
                 s' :: taken)
blanchet@49265
   239
              end)
blanchet@49265
   240
          (Term.add_vars t [] |> sort_wrt (fst o fst))
blanchet@49265
   241
  |> fst
blanchet@49265
   242
blanchet@49265
   243
fun clasimpset_rule_table_of ctxt =
blanchet@49265
   244
  let
blanchet@49265
   245
    val thy = Proof_Context.theory_of ctxt
blanchet@49265
   246
    val atomize = HOLogic.mk_Trueprop o Object_Logic.atomize_term thy
blanchet@49265
   247
    fun add stature normalizers get_th =
blanchet@49265
   248
      fold (fn rule =>
blanchet@49265
   249
               let
blanchet@49265
   250
                 val th = rule |> get_th
blanchet@49265
   251
                 val t =
blanchet@49265
   252
                   th |> Thm.maxidx_of th > 0 ? zero_var_indexes |> prop_of
blanchet@49265
   253
               in
blanchet@49265
   254
                 fold (fn normalize => Termtab.update (normalize t, stature))
blanchet@49265
   255
                      (I :: normalizers)
blanchet@49265
   256
               end)
blanchet@49265
   257
    val {safeIs, (* safeEs, *) hazIs, (* hazEs, *) ...} =
blanchet@49265
   258
      ctxt |> claset_of |> Classical.rep_cs
blanchet@49265
   259
    val intros = Item_Net.content safeIs @ Item_Net.content hazIs
blanchet@49265
   260
(* Add once it is used:
blanchet@49265
   261
    val elims =
blanchet@49265
   262
      Item_Net.content safeEs @ Item_Net.content hazEs
blanchet@49265
   263
      |> map Classical.classical_rule
blanchet@49265
   264
*)
blanchet@49265
   265
    val simps = ctxt |> simpset_of |> dest_ss |> #simps
blanchet@49265
   266
    val specs = ctxt |> Spec_Rules.get
blanchet@49265
   267
    val (rec_defs, nonrec_defs) =
blanchet@49265
   268
      specs |> filter (curry (op =) Spec_Rules.Equational o fst)
blanchet@49265
   269
            |> maps (snd o snd)
blanchet@49265
   270
            |> filter_out (member Thm.eq_thm_prop risky_defs)
blanchet@49265
   271
            |> List.partition (is_rec_def o prop_of)
blanchet@49265
   272
    val spec_intros =
blanchet@49265
   273
      specs |> filter (member (op =) [Spec_Rules.Inductive,
blanchet@49265
   274
                                      Spec_Rules.Co_Inductive] o fst)
blanchet@49265
   275
            |> maps (snd o snd)
blanchet@49265
   276
  in
blanchet@49265
   277
    Termtab.empty |> add Simp [atomize] snd simps
blanchet@49265
   278
                  |> add Simp [] I rec_defs
blanchet@49265
   279
                  |> add Def [] I nonrec_defs
blanchet@49265
   280
(* Add once it is used:
blanchet@49265
   281
                  |> add Elim [] I elims
blanchet@49265
   282
*)
blanchet@49265
   283
                  |> add Intro [] I intros
blanchet@49265
   284
                  |> add Inductive [] I spec_intros
blanchet@49265
   285
  end
blanchet@49265
   286
blanchet@49265
   287
fun uniquify xs =
blanchet@49265
   288
  Termtab.fold (cons o snd)
blanchet@49265
   289
               (fold (Termtab.update o `(prop_of o snd)) xs Termtab.empty) []
blanchet@49265
   290
blanchet@49265
   291
fun struct_induct_rule_on th =
blanchet@49265
   292
  case Logic.strip_horn (prop_of th) of
blanchet@49265
   293
    (prems, @{const Trueprop}
blanchet@49265
   294
            $ ((p as Var ((p_name, 0), _)) $ (a as Var (_, ind_T)))) =>
blanchet@49265
   295
    if not (is_TVar ind_T) andalso length prems > 1 andalso
blanchet@49265
   296
       exists (exists_subterm (curry (op aconv) p)) prems andalso
blanchet@49265
   297
       not (exists (exists_subterm (curry (op aconv) a)) prems) then
blanchet@49265
   298
      SOME (p_name, ind_T)
blanchet@49265
   299
    else
blanchet@49265
   300
      NONE
blanchet@49265
   301
  | _ => NONE
blanchet@49265
   302
blanchet@49265
   303
fun instantiate_induct_rule ctxt concl_prop p_name ((name, stature), th) ind_x =
blanchet@49265
   304
  let
blanchet@49265
   305
    fun varify_noninducts (t as Free (s, T)) =
blanchet@49265
   306
        if (s, T) = ind_x orelse can dest_funT T then t else Var ((s, 0), T)
blanchet@49265
   307
      | varify_noninducts t = t
blanchet@49265
   308
    val p_inst =
blanchet@49265
   309
      concl_prop |> map_aterms varify_noninducts |> close_form
blanchet@49265
   310
                 |> lambda (Free ind_x)
blanchet@49265
   311
                 |> hackish_string_for_term ctxt
blanchet@49265
   312
  in
blanchet@49265
   313
    ((fn () => name () ^ "[where " ^ p_name ^ " = " ^ quote p_inst ^ "]",
blanchet@49265
   314
      stature), th |> read_instantiate ctxt [((p_name, 0), p_inst)])
blanchet@49265
   315
  end
blanchet@49265
   316
blanchet@49265
   317
fun type_match thy (T1, T2) =
blanchet@49265
   318
  (Sign.typ_match thy (T2, T1) Vartab.empty; true)
blanchet@49265
   319
  handle Type.TYPE_MATCH => false
blanchet@49265
   320
blanchet@49265
   321
fun instantiate_if_induct_rule ctxt stmt stmt_xs (ax as (_, th)) =
blanchet@49265
   322
  case struct_induct_rule_on th of
blanchet@49265
   323
    SOME (p_name, ind_T) =>
blanchet@49265
   324
    let val thy = Proof_Context.theory_of ctxt in
blanchet@49265
   325
      stmt_xs |> filter (fn (_, T) => type_match thy (T, ind_T))
blanchet@49265
   326
              |> map_filter (try (instantiate_induct_rule ctxt stmt p_name ax))
blanchet@49265
   327
    end
blanchet@49265
   328
  | NONE => [ax]
blanchet@49265
   329
blanchet@49265
   330
fun external_frees t =
blanchet@49265
   331
  [] |> Term.add_frees t |> filter_out (can Name.dest_internal o fst)
blanchet@49265
   332
blanchet@49265
   333
fun maybe_instantiate_inducts ctxt hyp_ts concl_t =
blanchet@49265
   334
  if Config.get ctxt instantiate_inducts then
blanchet@49265
   335
    let
blanchet@49265
   336
      val thy = Proof_Context.theory_of ctxt
blanchet@49265
   337
      val ind_stmt =
blanchet@49265
   338
        (hyp_ts |> filter_out (null o external_frees), concl_t)
blanchet@49265
   339
        |> Logic.list_implies |> Object_Logic.atomize_term thy
blanchet@49265
   340
      val ind_stmt_xs = external_frees ind_stmt
blanchet@49265
   341
    in maps (instantiate_if_induct_rule ctxt ind_stmt ind_stmt_xs) end
blanchet@49265
   342
  else
blanchet@49265
   343
    I
blanchet@49265
   344
blanchet@49265
   345
fun maybe_filter_no_atps ctxt =
blanchet@49265
   346
  not (Config.get ctxt ignore_no_atp) ? filter_out (No_ATPs.member ctxt o snd)
blanchet@49265
   347
blanchet@49342
   348
fun all_facts ctxt ho_atp reserved add_ths chained_ths css_table =
blanchet@49266
   349
  let
blanchet@49266
   350
    val thy = Proof_Context.theory_of ctxt
blanchet@49266
   351
    val global_facts = Global_Theory.facts_of thy
blanchet@49266
   352
    val local_facts = Proof_Context.facts_of ctxt
blanchet@49266
   353
    val named_locals = local_facts |> Facts.dest_static []
blanchet@49266
   354
    val assms = Assumption.all_assms_of ctxt
blanchet@49266
   355
    fun is_good_unnamed_local th =
blanchet@49266
   356
      not (Thm.has_name_hint th) andalso
blanchet@49266
   357
      forall (fn (_, ths) => not (member Thm.eq_thm_prop ths th)) named_locals
blanchet@49266
   358
    val unnamed_locals =
blanchet@49266
   359
      union Thm.eq_thm_prop (Facts.props local_facts) chained_ths
blanchet@49266
   360
      |> filter is_good_unnamed_local |> map (pair "" o single)
blanchet@49266
   361
    val full_space =
blanchet@49266
   362
      Name_Space.merge (Facts.space_of global_facts, Facts.space_of local_facts)
blanchet@49266
   363
    fun add_facts global foldx facts =
blanchet@49266
   364
      foldx (fn (name0, ths) =>
blanchet@49342
   365
        if name0 <> "" andalso
blanchet@49266
   366
           forall (not o member Thm.eq_thm_prop add_ths) ths andalso
blanchet@49266
   367
           (Facts.is_concealed facts name0 orelse
blanchet@49342
   368
            not (can (Proof_Context.get_thms ctxt) name0) orelse
blanchet@49266
   369
            (not (Config.get ctxt ignore_no_atp) andalso
blanchet@49266
   370
             is_package_def name0) orelse
blanchet@49266
   371
            exists (fn s => String.isSuffix s name0)
blanchet@49266
   372
                   (multi_base_blacklist ctxt ho_atp)) then
blanchet@49266
   373
          I
blanchet@49266
   374
        else
blanchet@49266
   375
          let
blanchet@49266
   376
            val multi = length ths > 1
blanchet@49266
   377
            val backquote_thm =
blanchet@49266
   378
              backquote o hackish_string_for_term ctxt o close_form o prop_of
blanchet@49266
   379
            fun check_thms a =
blanchet@49266
   380
              case try (Proof_Context.get_thms ctxt) a of
blanchet@49266
   381
                NONE => false
blanchet@49266
   382
              | SOME ths' => eq_list Thm.eq_thm_prop (ths, ths')
blanchet@49266
   383
          in
blanchet@49266
   384
            pair 1
blanchet@49266
   385
            #> fold (fn th => fn (j, (multis, unis)) =>
blanchet@49266
   386
                        (j + 1,
blanchet@49266
   387
                         if not (member Thm.eq_thm_prop add_ths th) andalso
blanchet@49342
   388
                            is_theorem_bad_for_atps ho_atp th then
blanchet@49266
   389
                           (multis, unis)
blanchet@49266
   390
                         else
blanchet@49266
   391
                           let
blanchet@49266
   392
                             val new =
blanchet@49266
   393
                               (((fn () =>
blanchet@49342
   394
                                     if name0 = "" then
blanchet@49342
   395
                                       th |> backquote_thm
blanchet@49342
   396
                                     else
blanchet@49342
   397
                                       [Facts.extern ctxt facts name0,
blanchet@49342
   398
                                        Name_Space.extern ctxt full_space name0]
blanchet@49342
   399
                                       |> find_first check_thms
blanchet@49342
   400
                                       |> the_default name0
blanchet@49342
   401
                                       |> make_name reserved multi j),
blanchet@49342
   402
                                  stature_of_thm global assms chained_ths
blanchet@49342
   403
                                                 css_table name0 th), th)
blanchet@49266
   404
                           in
blanchet@49266
   405
                             if multi then (new :: multis, unis)
blanchet@49266
   406
                             else (multis, new :: unis)
blanchet@49266
   407
                           end)) ths
blanchet@49266
   408
            #> snd
blanchet@49266
   409
          end)
blanchet@49266
   410
  in
blanchet@49266
   411
    (* The single-name theorems go after the multiple-name ones, so that single
blanchet@49266
   412
       names are preferred when both are available. *)
blanchet@49266
   413
    ([], []) |> add_facts false fold local_facts (unnamed_locals @ named_locals)
blanchet@49266
   414
             |> add_facts true Facts.fold_static global_facts global_facts
blanchet@49266
   415
             |> op @
blanchet@49266
   416
  end
blanchet@49266
   417
blanchet@49314
   418
fun all_facts_of thy css_table =
blanchet@49266
   419
  let val ctxt = Proof_Context.init_global thy in
blanchet@49342
   420
    all_facts ctxt false Symtab.empty [] [] css_table
blanchet@49266
   421
    |> rev (* try to restore the original order of facts, for MaSh *)
blanchet@49266
   422
  end
blanchet@49266
   423
blanchet@49314
   424
fun nearly_all_facts ctxt ho_atp {add, del, only} reserved css_table chained_ths
blanchet@49314
   425
                     hyp_ts concl_t =
blanchet@49265
   426
  if only andalso null add then
blanchet@49265
   427
    []
blanchet@49265
   428
  else
blanchet@49265
   429
    let
blanchet@49307
   430
      val chained_ths =
blanchet@49307
   431
        chained_ths
blanchet@49307
   432
        |> maps (fn th => insert Thm.eq_thm_prop (zero_var_indexes th) [th])
blanchet@49265
   433
    in
blanchet@49265
   434
      (if only then
blanchet@49265
   435
         maps (map (fn ((name, stature), th) => ((K name, stature), th))
blanchet@49265
   436
               o fact_from_ref ctxt reserved chained_ths css_table) add
blanchet@49265
   437
       else
blanchet@49307
   438
         let val (add, del) = pairself (Attrib.eval_thms ctxt) (add, del) in
blanchet@49342
   439
           all_facts ctxt ho_atp reserved add chained_ths css_table
blanchet@49307
   440
           |> filter_out (member Thm.eq_thm_prop del o snd)
blanchet@49307
   441
           |> maybe_filter_no_atps ctxt
blanchet@49307
   442
         end)
blanchet@49265
   443
      |> maybe_instantiate_inducts ctxt hyp_ts concl_t
blanchet@49265
   444
      |> uniquify
blanchet@49265
   445
    end
blanchet@49265
   446
blanchet@49265
   447
end;