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