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