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