src/HOL/Tools/Sledgehammer/sledgehammer_fact.ML
author blanchet
Fri Jan 04 21:56:19 2013 +0100 (2013-01-04 ago)
changeset 50734 73612ad116e6
parent 50733 7ce87037fbeb
child 50735 6b232d76cbc9
permissions -rw-r--r--
learn from low-level, inside-class facts
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@50047
    26
  val backquote_thm : Proof.context -> thm -> string
blanchet@50511
    27
  val is_blacklisted_or_something : Proof.context -> bool -> string -> bool
blanchet@48250
    28
  val clasimpset_rule_table_of : Proof.context -> status Termtab.table
blanchet@50485
    29
  val build_all_names :
blanchet@50485
    30
    (thm -> string) -> ('a * thm) list -> string Symtab.table
blanchet@48250
    31
  val maybe_instantiate_inducts :
blanchet@48250
    32
    Proof.context -> term list -> term -> (((unit -> string) * 'a) * thm) list
blanchet@48250
    33
    -> (((unit -> string) * 'a) * thm) list
blanchet@48250
    34
  val maybe_filter_no_atps : Proof.context -> ('a * thm) list -> ('a * thm) list
blanchet@48530
    35
  val all_facts :
blanchet@50442
    36
    Proof.context -> bool -> bool -> unit Symtab.table -> thm list -> thm list
blanchet@48530
    37
    -> status Termtab.table -> fact list
blanchet@48250
    38
  val nearly_all_facts :
blanchet@48299
    39
    Proof.context -> bool -> fact_override -> unit Symtab.table
blanchet@48299
    40
    -> status Termtab.table -> thm list -> term list -> term -> fact list
blanchet@48250
    41
end;
blanchet@48250
    42
blanchet@48250
    43
structure Sledgehammer_Fact : SLEDGEHAMMER_FACT =
blanchet@48250
    44
struct
blanchet@48250
    45
blanchet@50495
    46
open ATP_Util
blanchet@48250
    47
open ATP_Problem_Generate
blanchet@48250
    48
open Metis_Tactic
blanchet@48250
    49
open Sledgehammer_Util
blanchet@48250
    50
blanchet@48296
    51
type fact = ((unit -> string) * stature) * thm
blanchet@48296
    52
blanchet@48292
    53
type fact_override =
blanchet@48250
    54
  {add : (Facts.ref * Attrib.src list) list,
blanchet@48250
    55
   del : (Facts.ref * Attrib.src list) list,
blanchet@48250
    56
   only : bool}
blanchet@48250
    57
blanchet@48250
    58
(* experimental features *)
blanchet@48250
    59
val ignore_no_atp =
blanchet@48250
    60
  Attrib.setup_config_bool @{binding sledgehammer_ignore_no_atp} (K false)
blanchet@48250
    61
val instantiate_inducts =
blanchet@48250
    62
  Attrib.setup_config_bool @{binding sledgehammer_instantiate_inducts} (K false)
blanchet@48250
    63
blanchet@48292
    64
val no_fact_override = {add = [], del = [], only = false}
blanchet@48250
    65
blanchet@48250
    66
fun needs_quoting reserved s =
blanchet@48250
    67
  Symtab.defined reserved s orelse
wenzelm@50239
    68
  exists (not o Symbol_Pos.is_identifier) (Long_Name.explode s)
blanchet@48250
    69
blanchet@48250
    70
fun make_name reserved multi j name =
blanchet@48250
    71
  (name |> needs_quoting reserved name ? quote) ^
blanchet@48250
    72
  (if multi then "(" ^ string_of_int j ^ ")" else "")
blanchet@48250
    73
blanchet@48250
    74
fun explode_interval _ (Facts.FromTo (i, j)) = i upto j
blanchet@48250
    75
  | explode_interval max (Facts.From i) = i upto i + max - 1
blanchet@48250
    76
  | explode_interval _ (Facts.Single i) = [i]
blanchet@48250
    77
blanchet@48250
    78
val backquote =
blanchet@48250
    79
  raw_explode #> map (fn "`" => "\\`" | s => s) #> implode #> enclose "`" "`"
blanchet@48250
    80
blanchet@48250
    81
(* unfolding these can yield really huge terms *)
blanchet@48250
    82
val risky_defs = @{thms Bit0_def Bit1_def}
blanchet@48250
    83
blanchet@48250
    84
fun is_rec_eq lhs = Term.exists_subterm (curry (op =) (head_of lhs))
blanchet@48250
    85
fun is_rec_def (@{const Trueprop} $ t) = is_rec_def t
blanchet@48250
    86
  | is_rec_def (@{const ==>} $ _ $ t2) = is_rec_def t2
blanchet@48250
    87
  | is_rec_def (Const (@{const_name "=="}, _) $ t1 $ t2) = is_rec_eq t1 t2
blanchet@48250
    88
  | is_rec_def (Const (@{const_name HOL.eq}, _) $ t1 $ t2) = is_rec_eq t1 t2
blanchet@48250
    89
  | is_rec_def _ = false
blanchet@48250
    90
blanchet@48250
    91
fun is_assum assms th = exists (fn ct => prop_of th aconv term_of ct) assms
blanchet@48396
    92
fun is_chained chained = member Thm.eq_thm_prop chained
blanchet@48250
    93
blanchet@48396
    94
fun scope_of_thm global assms chained th =
blanchet@48396
    95
  if is_chained chained th then Chained
blanchet@48250
    96
  else if global then Global
blanchet@48250
    97
  else if is_assum assms th then Assum
blanchet@48250
    98
  else Local
blanchet@48250
    99
blanchet@48250
   100
val may_be_induction =
blanchet@48250
   101
  exists_subterm (fn Var (_, Type (@{type_name fun}, [_, T])) =>
blanchet@48250
   102
                     body_type T = @{typ bool}
blanchet@48250
   103
                   | _ => false)
blanchet@48250
   104
blanchet@48396
   105
fun status_of_thm css name th =
blanchet@48250
   106
  (* FIXME: use structured name *)
blanchet@48250
   107
  if (String.isSubstring ".induct" name orelse
blanchet@48250
   108
      String.isSubstring ".inducts" name) andalso
blanchet@48250
   109
     may_be_induction (prop_of th) then
blanchet@48250
   110
    Induction
blanchet@48396
   111
  else case Termtab.lookup css (prop_of th) of
blanchet@48250
   112
    SOME status => status
blanchet@48250
   113
  | NONE => General
blanchet@48250
   114
blanchet@48396
   115
fun stature_of_thm global assms chained css name th =
blanchet@48396
   116
  (scope_of_thm global assms chained th, status_of_thm css name th)
blanchet@48250
   117
blanchet@48396
   118
fun fact_from_ref ctxt reserved chained css (xthm as (xref, args)) =
blanchet@48250
   119
  let
blanchet@48250
   120
    val ths = Attrib.eval_thms ctxt [xthm]
blanchet@48250
   121
    val bracket =
blanchet@48250
   122
      map (enclose "[" "]" o Pretty.str_of o Args.pretty_src ctxt) args
blanchet@48250
   123
      |> implode
blanchet@48250
   124
    fun nth_name j =
blanchet@48250
   125
      case xref of
blanchet@48250
   126
        Facts.Fact s => backquote s ^ bracket
blanchet@48250
   127
      | Facts.Named (("", _), _) => "[" ^ bracket ^ "]"
blanchet@48250
   128
      | Facts.Named ((name, _), NONE) =>
blanchet@48250
   129
        make_name reserved (length ths > 1) (j + 1) name ^ bracket
blanchet@48250
   130
      | Facts.Named ((name, _), SOME intervals) =>
blanchet@48250
   131
        make_name reserved true
blanchet@48250
   132
                 (nth (maps (explode_interval (length ths)) intervals) j) name ^
blanchet@48250
   133
        bracket
blanchet@48396
   134
    fun add_nth th (j, rest) =
blanchet@48396
   135
      let val name = nth_name j in
blanchet@48396
   136
        (j + 1, ((name, stature_of_thm false [] chained css name th), th)
blanchet@48396
   137
                :: rest)
blanchet@48396
   138
      end
blanchet@48396
   139
  in (0, []) |> fold add_nth ths |> snd end
blanchet@48250
   140
blanchet@48250
   141
(* Reject theorems with names like "List.filter.filter_list_def" or
blanchet@48250
   142
  "Accessible_Part.acc.defs", as these are definitions arising from packages. *)
blanchet@48250
   143
fun is_package_def a =
blanchet@48250
   144
  let val names = Long_Name.explode a in
blanchet@48250
   145
    (length names > 2 andalso not (hd names = "local") andalso
blanchet@48250
   146
     String.isSuffix "_def" a) orelse String.isSuffix "_defs" a
blanchet@48250
   147
  end
blanchet@48250
   148
blanchet@48250
   149
(* FIXME: put other record thms here, or declare as "no_atp" *)
blanchet@48250
   150
fun multi_base_blacklist ctxt ho_atp =
blanchet@48250
   151
  ["defs", "select_defs", "update_defs", "split", "splits", "split_asm",
blanchet@50442
   152
   "ext_cases", "eq.simps", "eq.refl", "nchotomy", "case_cong",
blanchet@48440
   153
   "weak_case_cong", "nibble_pair_of_char_simps", "nibble.simps",
blanchet@48440
   154
   "nibble.distinct"]
blanchet@48250
   155
  |> not (ho_atp orelse (Config.get ctxt instantiate_inducts)) ?
blanchet@48250
   156
        append ["induct", "inducts"]
blanchet@48440
   157
  |> map (prefix Long_Name.separator)
blanchet@48250
   158
blanchet@50490
   159
val max_lambda_nesting = 5 (*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@48437
   169
fun formula_has_too_many_lambdas Ts (Abs (_, T, t)) =
blanchet@48437
   170
    formula_has_too_many_lambdas (T :: Ts) t
blanchet@48437
   171
  | formula_has_too_many_lambdas Ts t =
blanchet@48250
   172
    if member (op =) [HOLogic.boolT, propT] (fastype_of1 (Ts, t)) then
blanchet@48437
   173
      exists (formula_has_too_many_lambdas Ts) (#2 (strip_comb t))
blanchet@48250
   174
    else
blanchet@48250
   175
      term_has_too_many_lambdas max_lambda_nesting t
blanchet@48250
   176
blanchet@50523
   177
(* The maximum apply depth of any "metis" call in "Metis_Examples" (on
blanchet@50523
   178
   2007-10-31) was 11. *)
blanchet@50490
   179
val max_apply_depth = 18
blanchet@48250
   180
blanchet@48250
   181
fun apply_depth (f $ t) = Int.max (apply_depth f, apply_depth t + 1)
blanchet@48250
   182
  | apply_depth (Abs (_, _, t)) = apply_depth t
blanchet@48250
   183
  | apply_depth _ = 0
blanchet@48250
   184
blanchet@50512
   185
fun is_too_complex ho_atp t =
blanchet@48437
   186
  apply_depth t > max_apply_depth orelse
blanchet@48437
   187
  (not ho_atp andalso formula_has_too_many_lambdas [] t)
blanchet@48250
   188
blanchet@48667
   189
(* FIXME: Ad hoc list *)
blanchet@48667
   190
val technical_prefixes =
blanchet@48667
   191
  ["ATP", "Code_Evaluation", "DSequence", "Enum", "Lazy_Sequence", "Meson",
blanchet@48667
   192
   "Metis", "Nitpick", "New_DSequence", "New_Random_Sequence", "Quickcheck",
blanchet@48667
   193
   "Quickcheck_Exhaustive", "Quickcheck_Narrowing", "Random_Sequence",
blanchet@48667
   194
   "Sledgehammer", "SMT"]
blanchet@48667
   195
  |> map (suffix Long_Name.separator)
blanchet@48440
   196
blanchet@48667
   197
fun has_technical_prefix s =
blanchet@48667
   198
  exists (fn pref => String.isPrefix pref s) technical_prefixes
blanchet@48667
   199
val exists_technical_const = exists_Const (has_technical_prefix o fst)
blanchet@48250
   200
blanchet@48250
   201
(* FIXME: make more reliable *)
blanchet@48250
   202
val exists_low_level_class_const =
blanchet@48250
   203
  exists_Const (fn (s, _) =>
blanchet@48250
   204
     s = @{const_name equal_class.equal} orelse
blanchet@48250
   205
     String.isSubstring (Long_Name.separator ^ "class" ^ Long_Name.separator) s)
blanchet@48250
   206
blanchet@48250
   207
fun is_that_fact th =
blanchet@48250
   208
  String.isSuffix (Long_Name.separator ^ Obtain.thatN) (Thm.get_name_hint th)
blanchet@48250
   209
  andalso exists_subterm (fn Free (s, _) => s = Name.skolem Auto_Bind.thesisN
blanchet@48250
   210
                           | _ => false) (prop_of th)
blanchet@48250
   211
blanchet@50523
   212
fun is_likely_tautology_too_meta_or_too_technical th =
blanchet@48406
   213
  let
blanchet@50053
   214
    fun is_interesting_subterm (Const (s, _)) =
blanchet@50053
   215
        not (member (op =) atp_widely_irrelevant_consts s)
blanchet@50053
   216
      | is_interesting_subterm (Free _) = true
blanchet@50053
   217
      | is_interesting_subterm _ = false
blanchet@48406
   218
    fun is_boring_bool t =
blanchet@50053
   219
      not (exists_subterm is_interesting_subterm t) orelse
blanchet@48406
   220
      exists_type (exists_subtype (curry (op =) @{typ prop})) t
blanchet@50495
   221
    fun is_boring_prop _ (@{const Trueprop} $ t) = is_boring_bool t
blanchet@50495
   222
      | is_boring_prop Ts (@{const "==>"} $ t $ u) =
blanchet@50495
   223
        is_boring_prop Ts t andalso is_boring_prop Ts u
blanchet@50496
   224
      | is_boring_prop Ts (Const (@{const_name all}, _) $ Abs (_, T, t)) =
blanchet@50496
   225
        is_boring_prop (T :: Ts) t
blanchet@50495
   226
      | is_boring_prop Ts ((t as Const (@{const_name all}, _)) $ u) =
blanchet@50495
   227
        is_boring_prop Ts (t $ eta_expand Ts u 1)
blanchet@50495
   228
      | is_boring_prop _ (Const (@{const_name "=="}, _) $ t $ u) =
blanchet@48406
   229
        is_boring_bool t andalso is_boring_bool u
blanchet@50495
   230
      | is_boring_prop _ _ = true
blanchet@50523
   231
    val t = prop_of th
blanchet@48406
   232
  in
blanchet@50523
   233
    (is_boring_prop [] (prop_of th) andalso
blanchet@50523
   234
     not (Thm.eq_thm_prop (@{thm ext}, th))) orelse
blanchet@48667
   235
    exists_type type_has_top_sort t orelse exists_technical_const t orelse
blanchet@48667
   236
    exists_low_level_class_const t orelse is_that_fact th
blanchet@48438
   237
  end
blanchet@48438
   238
blanchet@50511
   239
fun is_blacklisted_or_something ctxt ho_atp name =
blanchet@50510
   240
  (not (Config.get ctxt ignore_no_atp) andalso is_package_def name) orelse
blanchet@50510
   241
  exists (fn s => String.isSuffix s name) (multi_base_blacklist ctxt ho_atp)
blanchet@50510
   242
blanchet@50048
   243
fun hackish_string_for_term ctxt =
blanchet@50049
   244
  with_vanilla_print_mode (Syntax.string_of_term ctxt) #> simplify_spaces
blanchet@48250
   245
blanchet@48250
   246
(* This is a terrible hack. Free variables are sometimes coded as "M__" when
blanchet@48250
   247
   they are displayed as "M" and we want to avoid clashes with these. But
blanchet@48250
   248
   sometimes it's even worse: "Ma__" encodes "M". So we simply reserve all
blanchet@48250
   249
   prefixes of all free variables. In the worse case scenario, where the fact
blanchet@49981
   250
   won't be resolved correctly, the user can fix it manually, e.g., by giving a
blanchet@49981
   251
   name to the offending fact. *)
blanchet@48250
   252
fun all_prefixes_of s =
blanchet@48250
   253
  map (fn i => String.extract (s, 0, SOME i)) (1 upto size s - 1)
blanchet@48250
   254
blanchet@48250
   255
fun close_form t =
blanchet@48250
   256
  (t, [] |> Term.add_free_names t |> maps all_prefixes_of)
blanchet@48250
   257
  |> fold (fn ((s, i), T) => fn (t', taken) =>
blanchet@48250
   258
              let val s' = singleton (Name.variant_list taken) s in
blanchet@48250
   259
                ((if fastype_of t' = HOLogic.boolT then HOLogic.all_const
blanchet@48250
   260
                  else Logic.all_const) T
blanchet@48250
   261
                 $ Abs (s', T, abstract_over (Var ((s, i), T), t')),
blanchet@48250
   262
                 s' :: taken)
blanchet@48250
   263
              end)
blanchet@48250
   264
          (Term.add_vars t [] |> sort_wrt (fst o fst))
blanchet@48250
   265
  |> fst
blanchet@48250
   266
blanchet@50047
   267
fun backquote_term ctxt t =
blanchet@48400
   268
  t |> close_form
blanchet@50047
   269
    |> hackish_string_for_term ctxt
blanchet@48400
   270
    |> backquote
blanchet@48400
   271
blanchet@50047
   272
fun backquote_thm ctxt th = backquote_term ctxt (prop_of th)
blanchet@48394
   273
blanchet@48250
   274
fun clasimpset_rule_table_of ctxt =
blanchet@48250
   275
  let
blanchet@48250
   276
    val thy = Proof_Context.theory_of ctxt
blanchet@48250
   277
    val atomize = HOLogic.mk_Trueprop o Object_Logic.atomize_term thy
blanchet@48250
   278
    fun add stature normalizers get_th =
blanchet@48250
   279
      fold (fn rule =>
blanchet@48250
   280
               let
blanchet@48250
   281
                 val th = rule |> get_th
blanchet@48250
   282
                 val t =
blanchet@48250
   283
                   th |> Thm.maxidx_of th > 0 ? zero_var_indexes |> prop_of
blanchet@48250
   284
               in
blanchet@48250
   285
                 fold (fn normalize => Termtab.update (normalize t, stature))
blanchet@48250
   286
                      (I :: normalizers)
blanchet@48250
   287
               end)
blanchet@48250
   288
    val {safeIs, (* safeEs, *) hazIs, (* hazEs, *) ...} =
blanchet@48250
   289
      ctxt |> claset_of |> Classical.rep_cs
blanchet@48250
   290
    val intros = Item_Net.content safeIs @ Item_Net.content hazIs
blanchet@48250
   291
(* Add once it is used:
blanchet@48250
   292
    val elims =
blanchet@48250
   293
      Item_Net.content safeEs @ Item_Net.content hazEs
blanchet@48250
   294
      |> map Classical.classical_rule
blanchet@48250
   295
*)
blanchet@48250
   296
    val simps = ctxt |> simpset_of |> dest_ss |> #simps
blanchet@48250
   297
    val specs = ctxt |> Spec_Rules.get
blanchet@48250
   298
    val (rec_defs, nonrec_defs) =
blanchet@48250
   299
      specs |> filter (curry (op =) Spec_Rules.Equational o fst)
blanchet@48250
   300
            |> maps (snd o snd)
blanchet@48250
   301
            |> filter_out (member Thm.eq_thm_prop risky_defs)
blanchet@48250
   302
            |> List.partition (is_rec_def o prop_of)
blanchet@48250
   303
    val spec_intros =
blanchet@48250
   304
      specs |> filter (member (op =) [Spec_Rules.Inductive,
blanchet@48250
   305
                                      Spec_Rules.Co_Inductive] o fst)
blanchet@48250
   306
            |> maps (snd o snd)
blanchet@48250
   307
  in
blanchet@48250
   308
    Termtab.empty |> add Simp [atomize] snd simps
blanchet@48438
   309
                  |> add Rec_Def [] I rec_defs
blanchet@48438
   310
                  |> add Non_Rec_Def [] I nonrec_defs
blanchet@48250
   311
(* Add once it is used:
blanchet@48250
   312
                  |> add Elim [] I elims
blanchet@48250
   313
*)
blanchet@48250
   314
                  |> add Intro [] I intros
blanchet@48250
   315
                  |> add Inductive [] I spec_intros
blanchet@48250
   316
  end
blanchet@48250
   317
blanchet@50481
   318
fun normalize_eq (t as @{const Trueprop}
blanchet@50481
   319
        $ ((t0 as Const (@{const_name HOL.eq}, _)) $ t1 $ t2)) =
blanchet@50481
   320
    if Term_Ord.fast_term_ord (t1, t2) <> GREATER then t
blanchet@50481
   321
    else HOLogic.mk_Trueprop (t0 $ t2 $ t1)
blanchet@50481
   322
  | normalize_eq (t as @{const Trueprop} $ (@{const Not}
blanchet@50481
   323
        $ ((t0 as Const (@{const_name HOL.eq}, _)) $ t1 $ t2))) =
blanchet@50481
   324
    if Term_Ord.fast_term_ord (t1, t2) <> GREATER then t
blanchet@50481
   325
    else HOLogic.mk_Trueprop (HOLogic.mk_not (t0 $ t2 $ t1))
blanchet@50481
   326
  | normalize_eq t = t
blanchet@50481
   327
blanchet@50485
   328
val normalize_eq_etc = normalize_eq o Term_Subst.zero_var_indexes
blanchet@50485
   329
blanchet@50485
   330
fun if_thm_before th th' =
blanchet@50485
   331
  if Theory.subthy (pairself Thm.theory_of_thm (th, th')) then th else th'
blanchet@50485
   332
blanchet@50734
   333
(* Hack: Conflate the facts about a class as seen from the outside with the
blanchet@50734
   334
   corresponding low-level facts, so that MaSh can learn from the low-level
blanchet@50734
   335
   proofs. *)
blanchet@50734
   336
fun un_class_ify s =
blanchet@50734
   337
  case first_field "_class" s of
blanchet@50734
   338
    SOME (pref, suf) => [s, pref ^ suf]
blanchet@50734
   339
  | NONE => [s]
blanchet@50734
   340
blanchet@50485
   341
fun build_all_names name_of facts =
blanchet@50733
   342
  let
blanchet@50733
   343
    fun cons_thm (_, th) = Termtab.cons_list (normalize_eq_etc (prop_of th), th)
blanchet@50733
   344
    fun add_alias canon alias =
blanchet@50734
   345
      fold (fn s => Symtab.update (s, name_of (if_thm_before canon alias)))
blanchet@50734
   346
           (un_class_ify (Thm.get_name_hint canon))
blanchet@50734
   347
    fun add_aliases (_, aliases as canon :: _) =
blanchet@50734
   348
      fold (add_alias canon) aliases
blanchet@50733
   349
    val tab = fold_rev cons_thm facts Termtab.empty
blanchet@50733
   350
  in Termtab.fold add_aliases tab Symtab.empty end
blanchet@50485
   351
blanchet@50485
   352
fun uniquify facts =
blanchet@48250
   353
  Termtab.fold (cons o snd)
blanchet@50485
   354
      (fold (Termtab.default o `(normalize_eq_etc o prop_of o snd)) facts
blanchet@50481
   355
            Termtab.empty) []
blanchet@48250
   356
blanchet@48250
   357
fun struct_induct_rule_on th =
blanchet@48250
   358
  case Logic.strip_horn (prop_of th) of
blanchet@48250
   359
    (prems, @{const Trueprop}
blanchet@48250
   360
            $ ((p as Var ((p_name, 0), _)) $ (a as Var (_, ind_T)))) =>
blanchet@48250
   361
    if not (is_TVar ind_T) andalso length prems > 1 andalso
blanchet@48250
   362
       exists (exists_subterm (curry (op aconv) p)) prems andalso
blanchet@48250
   363
       not (exists (exists_subterm (curry (op aconv) a)) prems) then
blanchet@48250
   364
      SOME (p_name, ind_T)
blanchet@48250
   365
    else
blanchet@48250
   366
      NONE
blanchet@48250
   367
  | _ => NONE
blanchet@48250
   368
blanchet@50586
   369
val instantiate_induct_timeout = seconds 0.01
blanchet@50586
   370
blanchet@48250
   371
fun instantiate_induct_rule ctxt concl_prop p_name ((name, stature), th) ind_x =
blanchet@48250
   372
  let
blanchet@48250
   373
    fun varify_noninducts (t as Free (s, T)) =
blanchet@48250
   374
        if (s, T) = ind_x orelse can dest_funT T then t else Var ((s, 0), T)
blanchet@48250
   375
      | varify_noninducts t = t
blanchet@48250
   376
    val p_inst =
blanchet@48250
   377
      concl_prop |> map_aterms varify_noninducts |> close_form
blanchet@48250
   378
                 |> lambda (Free ind_x)
blanchet@50047
   379
                 |> hackish_string_for_term ctxt
blanchet@48250
   380
  in
blanchet@48250
   381
    ((fn () => name () ^ "[where " ^ p_name ^ " = " ^ quote p_inst ^ "]",
blanchet@48250
   382
      stature), th |> read_instantiate ctxt [((p_name, 0), p_inst)])
blanchet@48250
   383
  end
blanchet@48250
   384
blanchet@48250
   385
fun type_match thy (T1, T2) =
blanchet@48250
   386
  (Sign.typ_match thy (T2, T1) Vartab.empty; true)
blanchet@48250
   387
  handle Type.TYPE_MATCH => false
blanchet@48250
   388
blanchet@48250
   389
fun instantiate_if_induct_rule ctxt stmt stmt_xs (ax as (_, th)) =
blanchet@48250
   390
  case struct_induct_rule_on th of
blanchet@48250
   391
    SOME (p_name, ind_T) =>
blanchet@48250
   392
    let val thy = Proof_Context.theory_of ctxt in
blanchet@48250
   393
      stmt_xs |> filter (fn (_, T) => type_match thy (T, ind_T))
blanchet@50586
   394
              |> map_filter (try (TimeLimit.timeLimit instantiate_induct_timeout
blanchet@50586
   395
                     (instantiate_induct_rule ctxt stmt p_name ax)))
blanchet@48250
   396
    end
blanchet@48250
   397
  | NONE => [ax]
blanchet@48250
   398
blanchet@48250
   399
fun external_frees t =
blanchet@48250
   400
  [] |> Term.add_frees t |> filter_out (can Name.dest_internal o fst)
blanchet@48250
   401
blanchet@48250
   402
fun maybe_instantiate_inducts ctxt hyp_ts concl_t =
blanchet@48250
   403
  if Config.get ctxt instantiate_inducts then
blanchet@48250
   404
    let
blanchet@48250
   405
      val thy = Proof_Context.theory_of ctxt
blanchet@48250
   406
      val ind_stmt =
blanchet@48250
   407
        (hyp_ts |> filter_out (null o external_frees), concl_t)
blanchet@48250
   408
        |> Logic.list_implies |> Object_Logic.atomize_term thy
blanchet@48250
   409
      val ind_stmt_xs = external_frees ind_stmt
blanchet@48250
   410
    in maps (instantiate_if_induct_rule ctxt ind_stmt ind_stmt_xs) end
blanchet@48250
   411
  else
blanchet@48250
   412
    I
blanchet@48250
   413
blanchet@48250
   414
fun maybe_filter_no_atps ctxt =
blanchet@48250
   415
  not (Config.get ctxt ignore_no_atp) ? filter_out (No_ATPs.member ctxt o snd)
blanchet@48250
   416
blanchet@50523
   417
fun all_facts ctxt generous ho_atp reserved add_ths chained css =
blanchet@48251
   418
  let
blanchet@48251
   419
    val thy = Proof_Context.theory_of ctxt
blanchet@48251
   420
    val global_facts = Global_Theory.facts_of thy
blanchet@48251
   421
    val local_facts = Proof_Context.facts_of ctxt
blanchet@48251
   422
    val named_locals = local_facts |> Facts.dest_static []
blanchet@48251
   423
    val assms = Assumption.all_assms_of ctxt
blanchet@48251
   424
    fun is_good_unnamed_local th =
blanchet@48251
   425
      not (Thm.has_name_hint th) andalso
blanchet@48251
   426
      forall (fn (_, ths) => not (member Thm.eq_thm_prop ths th)) named_locals
blanchet@48251
   427
    val unnamed_locals =
blanchet@48396
   428
      union Thm.eq_thm_prop (Facts.props local_facts) chained
blanchet@48251
   429
      |> filter is_good_unnamed_local |> map (pair "" o single)
blanchet@48251
   430
    val full_space =
blanchet@48251
   431
      Name_Space.merge (Facts.space_of global_facts, Facts.space_of local_facts)
blanchet@48251
   432
    fun add_facts global foldx facts =
blanchet@48251
   433
      foldx (fn (name0, ths) =>
blanchet@50512
   434
        if name0 <> "" andalso
blanchet@48251
   435
           forall (not o member Thm.eq_thm_prop add_ths) ths andalso
blanchet@50511
   436
           (Facts.is_concealed facts name0 orelse
blanchet@50511
   437
            not (can (Proof_Context.get_thms ctxt) name0) orelse
blanchet@50523
   438
            (not generous andalso
blanchet@50512
   439
             is_blacklisted_or_something ctxt ho_atp name0)) then
blanchet@48251
   440
          I
blanchet@48251
   441
        else
blanchet@48251
   442
          let
blanchet@50485
   443
            val n = length ths
blanchet@50485
   444
            val multi = n > 1
blanchet@48251
   445
            fun check_thms a =
blanchet@48251
   446
              case try (Proof_Context.get_thms ctxt) a of
blanchet@48251
   447
                NONE => false
blanchet@48251
   448
              | SOME ths' => eq_list Thm.eq_thm_prop (ths, ths')
blanchet@48251
   449
          in
blanchet@50485
   450
            pair n
blanchet@50485
   451
            #> fold_rev (fn th => fn (j, (multis, unis)) =>
blanchet@50485
   452
                   (j - 1,
blanchet@50485
   453
                    if not (member Thm.eq_thm_prop add_ths th) andalso
blanchet@50523
   454
                       (is_likely_tautology_too_meta_or_too_technical th orelse
blanchet@50523
   455
                        (not generous andalso
blanchet@50523
   456
                         is_too_complex ho_atp (prop_of th))) then
blanchet@50485
   457
                      (multis, unis)
blanchet@50485
   458
                    else
blanchet@50485
   459
                      let
blanchet@50485
   460
                        val new =
blanchet@50485
   461
                          (((fn () =>
blanchet@50485
   462
                                if name0 = "" then
blanchet@50485
   463
                                  backquote_thm ctxt th
blanchet@50485
   464
                                else
blanchet@50485
   465
                                  [Facts.extern ctxt facts name0,
blanchet@50485
   466
                                   Name_Space.extern ctxt full_space name0]
blanchet@50485
   467
                                  |> find_first check_thms
blanchet@50485
   468
                                  |> the_default name0
blanchet@50485
   469
                                  |> make_name reserved multi j),
blanchet@50485
   470
                             stature_of_thm global assms chained css name0 th),
blanchet@50485
   471
                           th)
blanchet@50485
   472
                      in
blanchet@50485
   473
                        if multi then (new :: multis, unis)
blanchet@50485
   474
                        else (multis, new :: unis)
blanchet@50485
   475
                      end)) ths
blanchet@48251
   476
            #> snd
blanchet@48251
   477
          end)
blanchet@48251
   478
  in
blanchet@48251
   479
    (* The single-name theorems go after the multiple-name ones, so that single
blanchet@48251
   480
       names are preferred when both are available. *)
blanchet@48251
   481
    ([], []) |> add_facts false fold local_facts (unnamed_locals @ named_locals)
blanchet@48251
   482
             |> add_facts true Facts.fold_static global_facts global_facts
blanchet@48251
   483
             |> op @
blanchet@48251
   484
  end
blanchet@48251
   485
blanchet@48396
   486
fun nearly_all_facts ctxt ho_atp {add, del, only} reserved css chained hyp_ts
blanchet@48396
   487
                     concl_t =
blanchet@48250
   488
  if only andalso null add then
blanchet@48250
   489
    []
blanchet@48250
   490
  else
blanchet@48250
   491
    let
blanchet@48396
   492
      val chained =
blanchet@48396
   493
        chained
blanchet@48292
   494
        |> maps (fn th => insert Thm.eq_thm_prop (zero_var_indexes th) [th])
blanchet@48250
   495
    in
blanchet@48250
   496
      (if only then
blanchet@48250
   497
         maps (map (fn ((name, stature), th) => ((K name, stature), th))
blanchet@48396
   498
               o fact_from_ref ctxt reserved chained css) add
blanchet@48250
   499
       else
blanchet@48292
   500
         let val (add, del) = pairself (Attrib.eval_thms ctxt) (add, del) in
blanchet@50442
   501
           all_facts ctxt false ho_atp reserved add chained css
blanchet@48292
   502
           |> filter_out (member Thm.eq_thm_prop del o snd)
blanchet@48292
   503
           |> maybe_filter_no_atps ctxt
blanchet@48332
   504
           |> uniquify
blanchet@48292
   505
         end)
blanchet@48250
   506
      |> maybe_instantiate_inducts ctxt hyp_ts concl_t
blanchet@48250
   507
    end
blanchet@48250
   508
blanchet@48250
   509
end;