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