src/HOL/Tools/Sledgehammer/sledgehammer_fact_filter.ML
author blanchet
Mon Aug 30 17:14:54 2010 +0200 (2010-08-30 ago)
changeset 38904 5e760c0f81a6
parent 38901 ee36b983ca22
child 38906 f76ac80e9bcd
permissions -rw-r--r--
rule out low-level class facts
blanchet@35826
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_fact_filter.ML
blanchet@38027
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
blanchet@36393
     3
    Author:     Jasmin Blanchette, TU Muenchen
wenzelm@33309
     4
*)
paulson@15452
     5
blanchet@35826
     6
signature SLEDGEHAMMER_FACT_FILTER =
wenzelm@16802
     7
sig
blanchet@38752
     8
  datatype locality = General | Theory | Local | Chained
blanchet@38752
     9
blanchet@35966
    10
  type relevance_override =
blanchet@35966
    11
    {add: Facts.ref list,
blanchet@35966
    12
     del: Facts.ref list,
blanchet@35966
    13
     only: bool}
blanchet@35966
    14
blanchet@37616
    15
  val trace : bool Unsynchronized.ref
blanchet@38899
    16
  val abs_rel_weight : real Unsynchronized.ref
blanchet@38899
    17
  val abs_irrel_weight : real Unsynchronized.ref
blanchet@38899
    18
  val skolem_irrel_weight : real Unsynchronized.ref
blanchet@38899
    19
  val theory_bonus : real Unsynchronized.ref
blanchet@38899
    20
  val local_bonus : real Unsynchronized.ref
blanchet@38899
    21
  val chained_bonus : real Unsynchronized.ref
blanchet@38904
    22
  val max_imperfect : real Unsynchronized.ref
blanchet@38904
    23
  val max_imperfect_exp : real Unsynchronized.ref
blanchet@38899
    24
  val threshold_divisor : real Unsynchronized.ref
blanchet@38899
    25
  val ridiculous_threshold : real Unsynchronized.ref
blanchet@38744
    26
  val name_thm_pairs_from_ref :
blanchet@38696
    27
    Proof.context -> unit Symtab.table -> thm list -> Facts.ref
blanchet@38752
    28
    -> ((string * locality) * thm) list
blanchet@37347
    29
  val relevant_facts :
blanchet@38891
    30
    Proof.context -> bool -> real * real -> int -> bool -> relevance_override
blanchet@38891
    31
    -> thm list -> term list -> term -> ((string * locality) * thm) list
paulson@15347
    32
end;
paulson@15347
    33
blanchet@35826
    34
structure Sledgehammer_Fact_Filter : SLEDGEHAMMER_FACT_FILTER =
paulson@15347
    35
struct
paulson@15347
    36
blanchet@38652
    37
open Sledgehammer_Util
blanchet@38652
    38
blanchet@37616
    39
val trace = Unsynchronized.ref false
blanchet@37616
    40
fun trace_msg msg = if !trace then tracing (msg ()) else ()
blanchet@35826
    41
blanchet@38828
    42
(* experimental feature *)
blanchet@38889
    43
val term_patterns = false
blanchet@38827
    44
blanchet@37580
    45
val respect_no_atp = true
blanchet@37505
    46
blanchet@38752
    47
datatype locality = General | Theory | Local | Chained
blanchet@38752
    48
blanchet@35966
    49
type relevance_override =
blanchet@35966
    50
  {add: Facts.ref list,
blanchet@35966
    51
   del: Facts.ref list,
blanchet@35966
    52
   only: bool}
paulson@21070
    53
blanchet@37616
    54
val sledgehammer_prefix = "Sledgehammer" ^ Long_Name.separator
blanchet@37616
    55
blanchet@38744
    56
fun repair_name reserved multi j name =
blanchet@38744
    57
  (name |> Symtab.defined reserved name ? quote) ^
blanchet@38744
    58
  (if multi then "(" ^ Int.toString j ^ ")" else "")
blanchet@38744
    59
blanchet@38744
    60
fun name_thm_pairs_from_ref ctxt reserved chained_ths xref =
blanchet@38744
    61
  let
blanchet@38744
    62
    val ths = ProofContext.get_fact ctxt xref
blanchet@38744
    63
    val name = Facts.string_of_ref xref
blanchet@38744
    64
    val multi = length ths > 1
blanchet@38744
    65
  in
blanchet@38752
    66
    (ths, (1, []))
blanchet@38752
    67
    |-> fold (fn th => fn (j, rest) =>
blanchet@38752
    68
                 (j + 1, ((repair_name reserved multi j name,
blanchet@38752
    69
                          if member Thm.eq_thm chained_ths th then Chained
blanchet@38752
    70
                          else General), th) :: rest))
blanchet@38744
    71
    |> snd
blanchet@38699
    72
  end
blanchet@37616
    73
wenzelm@28477
    74
(***************************************************************)
wenzelm@28477
    75
(* Relevance Filtering                                         *)
wenzelm@28477
    76
(***************************************************************)
mengj@19194
    77
paulson@24287
    78
(*** constants with types ***)
paulson@24287
    79
blanchet@38823
    80
(* An abstraction of Isabelle types and first-order terms *)
blanchet@38823
    81
datatype pattern = PVar | PApp of string * pattern list
blanchet@38744
    82
blanchet@38823
    83
fun string_for_pattern PVar = "_"
blanchet@38823
    84
  | string_for_pattern (PApp (s, ps)) =
blanchet@38823
    85
    if null ps then s else s ^ string_for_patterns ps
blanchet@38823
    86
and string_for_patterns ps = "(" ^ commas (map string_for_pattern ps) ^ ")"
paulson@24287
    87
paulson@24287
    88
(*Is the second type an instance of the first one?*)
blanchet@38824
    89
fun match_pattern (PVar, _) = true
blanchet@38824
    90
  | match_pattern (PApp _, PVar) = false
blanchet@38824
    91
  | match_pattern (PApp (s, ps), PApp (t, qs)) =
blanchet@38824
    92
    s = t andalso match_patterns (ps, qs)
blanchet@38824
    93
and match_patterns (_, []) = true
blanchet@38824
    94
  | match_patterns ([], _) = false
blanchet@38824
    95
  | match_patterns (p :: ps, q :: qs) =
blanchet@38824
    96
    match_pattern (p, q) andalso match_patterns (ps, qs)
paulson@24287
    97
blanchet@38823
    98
(* Is there a unifiable constant? *)
blanchet@38827
    99
fun pconst_mem f consts (s, ps) =
blanchet@38827
   100
  exists (curry (match_patterns o f) ps)
blanchet@38827
   101
         (map snd (filter (curry (op =) s o fst) consts))
blanchet@38827
   102
fun pconst_hyper_mem f const_tab (s, ps) =
blanchet@38823
   103
  exists (curry (match_patterns o f) ps) (these (Symtab.lookup const_tab s))
blanchet@37505
   104
blanchet@38827
   105
fun ptype (Type (s, Ts)) = PApp (s, map ptype Ts)
blanchet@38827
   106
  | ptype (TFree (s, _)) = PApp (s, [])
blanchet@38827
   107
  | ptype (TVar _) = PVar
blanchet@38827
   108
blanchet@38827
   109
fun pterm thy t =
blanchet@38827
   110
  case strip_comb t of
blanchet@38827
   111
    (Const x, ts) => PApp (pconst thy true x ts)
blanchet@38827
   112
  | (Free x, ts) => PApp (pconst thy false x ts)
blanchet@38827
   113
  | (Var x, []) => PVar
blanchet@38827
   114
  | _ => PApp ("?", [])  (* equivalence class of higher-order constructs *)
blanchet@38744
   115
(* Pairs a constant with the list of its type instantiations. *)
blanchet@38827
   116
and pconst_args thy const (s, T) ts =
blanchet@38828
   117
  (if const then map ptype (these (try (Sign.const_typargs thy) (s, T)))
blanchet@38828
   118
   else []) @
blanchet@38889
   119
  (if term_patterns then map (pterm thy) ts else [])
blanchet@38827
   120
and pconst thy const (s, T) ts = (s, pconst_args thy const (s, T) ts)
paulson@24287
   121
blanchet@38827
   122
fun string_for_hyper_pconst (s, pss) =
blanchet@38823
   123
  s ^ "{" ^ commas (map string_for_patterns pss) ^ "}"
paulson@24287
   124
blanchet@38816
   125
val abs_name = "Sledgehammer.abs"
blanchet@38749
   126
val skolem_prefix = "Sledgehammer.sko"
blanchet@38747
   127
blanchet@38822
   128
(* These are typically simplified away by "Meson.presimplify". Equality is
blanchet@38822
   129
   handled specially via "fequal". *)
blanchet@38682
   130
val boring_consts =
blanchet@38822
   131
  [@{const_name False}, @{const_name True}, @{const_name If}, @{const_name Let},
blanchet@38822
   132
   @{const_name "op ="}]
blanchet@37537
   133
blanchet@38823
   134
(* Add a pconstant to the table, but a [] entry means a standard
blanchet@38819
   135
   connective, which we ignore.*)
blanchet@38824
   136
fun add_pconst_to_table also_skolem (c, ps) =
blanchet@38819
   137
  if member (op =) boring_consts c orelse
blanchet@38819
   138
     (not also_skolem andalso String.isPrefix skolem_prefix c) then
blanchet@38819
   139
    I
blanchet@38819
   140
  else
blanchet@38824
   141
    Symtab.map_default (c, [ps]) (insert (op =) ps)
blanchet@38819
   142
blanchet@38819
   143
fun is_formula_type T = (T = HOLogic.boolT orelse T = propT)
blanchet@38819
   144
blanchet@38823
   145
fun get_pconsts thy also_skolems pos ts =
blanchet@37505
   146
  let
blanchet@38819
   147
    val flip = Option.map not
blanchet@38587
   148
    (* We include free variables, as well as constants, to handle locales. For
blanchet@38587
   149
       each quantifiers that must necessarily be skolemized by the ATP, we
blanchet@38587
   150
       introduce a fresh constant to simulate the effect of Skolemization. *)
blanchet@38827
   151
    fun do_const const (s, T) ts =
blanchet@38827
   152
      add_pconst_to_table also_skolems (pconst thy const (s, T) ts)
blanchet@38827
   153
      #> fold do_term ts
blanchet@38827
   154
    and do_term t =
blanchet@38827
   155
      case strip_comb t of
blanchet@38827
   156
        (Const x, ts) => do_const true x ts
blanchet@38827
   157
      | (Free x, ts) => do_const false x ts
blanchet@38827
   158
      | (Abs (_, _, t'), ts) =>
blanchet@38901
   159
        (null ts ? add_pconst_to_table true (abs_name, []))
blanchet@38827
   160
        #> fold do_term (t' :: ts)
blanchet@38827
   161
      | (_, ts) => fold do_term ts
blanchet@38587
   162
    fun do_quantifier will_surely_be_skolemized body_t =
blanchet@37537
   163
      do_formula pos body_t
blanchet@38747
   164
      #> (if also_skolems andalso will_surely_be_skolemized then
blanchet@38823
   165
            add_pconst_to_table true (gensym skolem_prefix, [])
blanchet@38587
   166
          else
blanchet@38587
   167
            I)
blanchet@38587
   168
    and do_term_or_formula T =
blanchet@38692
   169
      if is_formula_type T then do_formula NONE else do_term
blanchet@37537
   170
    and do_formula pos t =
blanchet@37537
   171
      case t of
blanchet@37537
   172
        Const (@{const_name all}, _) $ Abs (_, _, body_t) =>
blanchet@38587
   173
        do_quantifier (pos = SOME false) body_t
blanchet@37537
   174
      | @{const "==>"} $ t1 $ t2 =>
blanchet@37537
   175
        do_formula (flip pos) t1 #> do_formula pos t2
blanchet@37537
   176
      | Const (@{const_name "=="}, Type (_, [T, _])) $ t1 $ t2 =>
blanchet@38587
   177
        fold (do_term_or_formula T) [t1, t2]
blanchet@37537
   178
      | @{const Trueprop} $ t1 => do_formula pos t1
blanchet@37537
   179
      | @{const Not} $ t1 => do_formula (flip pos) t1
blanchet@37537
   180
      | Const (@{const_name All}, _) $ Abs (_, _, body_t) =>
blanchet@38587
   181
        do_quantifier (pos = SOME false) body_t
blanchet@37537
   182
      | Const (@{const_name Ex}, _) $ Abs (_, _, body_t) =>
blanchet@38587
   183
        do_quantifier (pos = SOME true) body_t
haftmann@38795
   184
      | @{const HOL.conj} $ t1 $ t2 => fold (do_formula pos) [t1, t2]
haftmann@38795
   185
      | @{const HOL.disj} $ t1 $ t2 => fold (do_formula pos) [t1, t2]
haftmann@38786
   186
      | @{const HOL.implies} $ t1 $ t2 =>
blanchet@37537
   187
        do_formula (flip pos) t1 #> do_formula pos t2
blanchet@37537
   188
      | Const (@{const_name "op ="}, Type (_, [T, _])) $ t1 $ t2 =>
blanchet@38587
   189
        fold (do_term_or_formula T) [t1, t2]
blanchet@38587
   190
      | Const (@{const_name If}, Type (_, [_, Type (_, [T, _])]))
blanchet@38587
   191
        $ t1 $ t2 $ t3 =>
blanchet@38587
   192
        do_formula NONE t1 #> fold (do_term_or_formula T) [t2, t3]
blanchet@38587
   193
      | Const (@{const_name Ex1}, _) $ Abs (_, _, body_t) =>
blanchet@38587
   194
        do_quantifier (is_some pos) body_t
blanchet@38587
   195
      | Const (@{const_name Ball}, _) $ t1 $ Abs (_, _, body_t) =>
blanchet@38587
   196
        do_quantifier (pos = SOME false)
blanchet@38587
   197
                      (HOLogic.mk_imp (incr_boundvars 1 t1 $ Bound 0, body_t))
blanchet@38587
   198
      | Const (@{const_name Bex}, _) $ t1 $ Abs (_, _, body_t) =>
blanchet@38587
   199
        do_quantifier (pos = SOME true)
blanchet@38587
   200
                      (HOLogic.mk_conj (incr_boundvars 1 t1 $ Bound 0, body_t))
blanchet@37537
   201
      | (t0 as Const (_, @{typ bool})) $ t1 =>
blanchet@37537
   202
        do_term t0 #> do_formula pos t1  (* theory constant *)
blanchet@37537
   203
      | _ => do_term t
blanchet@38819
   204
  in Symtab.empty |> fold (do_formula pos) ts end
paulson@24287
   205
paulson@24287
   206
(*Inserts a dummy "constant" referring to the theory name, so that relevance
paulson@24287
   207
  takes the given theory into account.*)
blanchet@37616
   208
fun theory_const_prop_of theory_relevant th =
blanchet@37505
   209
  if theory_relevant then
blanchet@37505
   210
    let
blanchet@37505
   211
      val name = Context.theory_name (theory_of_thm th)
blanchet@37505
   212
      val t = Const (name ^ ". 1", @{typ bool})
blanchet@37505
   213
    in t $ prop_of th end
blanchet@37505
   214
  else
blanchet@37505
   215
    prop_of th
blanchet@37505
   216
paulson@24287
   217
(**** Constant / Type Frequencies ****)
paulson@24287
   218
blanchet@38743
   219
(* A two-dimensional symbol table counts frequencies of constants. It's keyed
blanchet@38743
   220
   first by constant name and second by its list of type instantiations. For the
blanchet@38823
   221
   latter, we need a linear ordering on "pattern list". *)
paulson@24287
   222
blanchet@38823
   223
fun pattern_ord p =
blanchet@38743
   224
  case p of
blanchet@38744
   225
    (PVar, PVar) => EQUAL
blanchet@38823
   226
  | (PVar, PApp _) => LESS
blanchet@38823
   227
  | (PApp _, PVar) => GREATER
blanchet@38823
   228
  | (PApp q1, PApp q2) =>
blanchet@38823
   229
    prod_ord fast_string_ord (dict_ord pattern_ord) (q1, q2)
paulson@24287
   230
blanchet@38743
   231
structure CTtab =
blanchet@38823
   232
  Table(type key = pattern list val ord = dict_ord pattern_ord)
paulson@24287
   233
blanchet@38827
   234
fun count_axiom_consts theory_relevant thy =
blanchet@37503
   235
  let
blanchet@38827
   236
    fun do_const const (s, T) ts =
blanchet@38827
   237
      (* Two-dimensional table update. Constant maps to types maps to count. *)
blanchet@38827
   238
      CTtab.map_default (pconst_args thy const (s, T) ts, 0) (Integer.add 1)
blanchet@38827
   239
      |> Symtab.map_default (s, CTtab.empty)
blanchet@38827
   240
      #> fold do_term ts
blanchet@38827
   241
    and do_term t =
blanchet@38827
   242
      case strip_comb t of
blanchet@38827
   243
        (Const x, ts) => do_const true x ts
blanchet@38827
   244
      | (Free x, ts) => do_const false x ts
blanchet@38827
   245
      | (Abs (_, _, t'), ts) => fold do_term (t' :: ts)
blanchet@38827
   246
      | (_, ts) => fold do_term ts
blanchet@38827
   247
  in do_term o theory_const_prop_of theory_relevant o snd end
paulson@24287
   248
paulson@24287
   249
paulson@24287
   250
(**** Actual Filtering Code ****)
paulson@24287
   251
paulson@24287
   252
(*The frequency of a constant is the sum of those of all instances of its type.*)
blanchet@38824
   253
fun pconst_freq match const_tab (c, ps) =
blanchet@38824
   254
  CTtab.fold (fn (qs, m) => match (ps, qs) ? Integer.add m)
blanchet@38686
   255
             (the (Symtab.lookup const_tab c)) 0
blanchet@38686
   256
paulson@24287
   257
blanchet@38085
   258
(* A surprising number of theorems contain only a few significant constants.
blanchet@38085
   259
   These include all induction rules, and other general theorems. *)
blanchet@37503
   260
blanchet@37503
   261
(* "log" seems best in practice. A constant function of one ignores the constant
blanchet@37503
   262
   frequencies. *)
blanchet@38747
   263
fun rel_log n = 1.0 + 2.0 / Math.ln (Real.fromInt n + 1.0)
blanchet@38747
   264
fun irrel_log n = Math.ln (Real.fromInt n + 19.0) / 6.4
blanchet@37503
   265
blanchet@38752
   266
(* FUDGE *)
blanchet@38899
   267
val abs_rel_weight = Unsynchronized.ref 0.5
blanchet@38899
   268
val abs_irrel_weight = Unsynchronized.ref 2.0
blanchet@38904
   269
val skolem_irrel_weight = Unsynchronized.ref 0.75
blanchet@38752
   270
blanchet@37503
   271
(* Computes a constant's weight, as determined by its frequency. *)
blanchet@38821
   272
fun generic_weight abs_weight skolem_weight logx f const_tab (c as (s, _)) =
blanchet@38816
   273
  if s = abs_name then abs_weight
blanchet@38816
   274
  else if String.isPrefix skolem_prefix s then skolem_weight
blanchet@38823
   275
  else logx (pconst_freq (match_patterns o f) const_tab c)
blanchet@38821
   276
blanchet@38901
   277
fun rel_weight const_tab =
blanchet@38901
   278
  generic_weight (!abs_rel_weight) 0.0 rel_log I const_tab
blanchet@38901
   279
fun irrel_weight const_tab =
blanchet@38901
   280
  generic_weight (!abs_irrel_weight) (!skolem_irrel_weight) irrel_log swap
blanchet@38901
   281
                 const_tab
paulson@24287
   282
blanchet@38752
   283
(* FUDGE *)
blanchet@38901
   284
val theory_bonus = Unsynchronized.ref 0.1
blanchet@38901
   285
val local_bonus = Unsynchronized.ref 0.2
blanchet@38901
   286
val chained_bonus = Unsynchronized.ref 1.5
blanchet@38899
   287
blanchet@38889
   288
fun locality_bonus General = 0.0
blanchet@38899
   289
  | locality_bonus Theory = !theory_bonus
blanchet@38899
   290
  | locality_bonus Local = !local_bonus
blanchet@38899
   291
  | locality_bonus Chained = !chained_bonus
blanchet@38751
   292
blanchet@38752
   293
fun axiom_weight loc const_tab relevant_consts axiom_consts =
blanchet@38827
   294
  case axiom_consts |> List.partition (pconst_hyper_mem I relevant_consts)
blanchet@38827
   295
                    ||> filter_out (pconst_hyper_mem swap relevant_consts) of
blanchet@38827
   296
    ([], _) => 0.0
blanchet@38744
   297
  | (rel, irrel) =>
blanchet@38889
   298
    let
blanchet@38889
   299
      val irrel = irrel |> filter_out (pconst_mem swap rel)
blanchet@38889
   300
      val rel_weight = 0.0 |> fold (curry (op +) o rel_weight const_tab) rel
blanchet@38889
   301
      val irrel_weight =
blanchet@38889
   302
        ~ (locality_bonus loc)
blanchet@38889
   303
        |> fold (curry (op +) o irrel_weight const_tab) irrel
blanchet@38889
   304
      val res = rel_weight / (rel_weight + irrel_weight)
blanchet@38889
   305
    in if Real.isFinite res then res else 0.0 end
blanchet@38747
   306
blanchet@38904
   307
(* FIXME: experiment
blanchet@38904
   308
fun debug_axiom_weight loc const_tab relevant_consts axiom_consts =
blanchet@38904
   309
  case axiom_consts |> List.partition (pconst_hyper_mem I relevant_consts)
blanchet@38904
   310
                    ||> filter_out (pconst_hyper_mem swap relevant_consts) of
blanchet@38904
   311
    ([], _) => 0.0
blanchet@38904
   312
  | (rel, irrel) =>
blanchet@38904
   313
    let
blanchet@38904
   314
val _ = tracing (PolyML.makestring ("REL: ", rel))
blanchet@38904
   315
val _ = tracing (PolyML.makestring ("IRREL: ", irrel))(*###*)
blanchet@38904
   316
      val irrel = irrel |> filter_out (pconst_mem swap rel)
blanchet@38904
   317
      val rel_weight = 0.0 |> fold (curry (op +) o rel_weight const_tab) rel
blanchet@38904
   318
      val irrel_weight =
blanchet@38904
   319
        ~ (locality_bonus loc)
blanchet@38904
   320
        |> fold (curry (op +) o irrel_weight const_tab) irrel
blanchet@38904
   321
      val res = rel_weight / (rel_weight + irrel_weight)
blanchet@38904
   322
    in if Real.isFinite res then res else 0.0 end
blanchet@38904
   323
*)
blanchet@38904
   324
blanchet@38823
   325
fun pconsts_in_axiom thy t =
blanchet@38825
   326
  Symtab.fold (fn (s, pss) => fold (cons o pair s) pss)
blanchet@38823
   327
              (get_pconsts thy true (SOME true) [t]) []
blanchet@38687
   328
fun pair_consts_axiom theory_relevant thy axiom =
blanchet@38827
   329
  case axiom |> snd |> theory_const_prop_of theory_relevant
blanchet@38827
   330
             |> pconsts_in_axiom thy of
blanchet@38827
   331
    [] => NONE
blanchet@38827
   332
  | consts => SOME ((axiom, consts), NONE)
paulson@24287
   333
blanchet@38699
   334
type annotated_thm =
blanchet@38823
   335
  (((unit -> string) * locality) * thm) * (string * pattern list) list
blanchet@37505
   336
blanchet@38904
   337
(* FUDGE *)
blanchet@38904
   338
val max_imperfect = Unsynchronized.ref 10.0
blanchet@38904
   339
val max_imperfect_exp = Unsynchronized.ref 1.0
blanchet@38889
   340
blanchet@38904
   341
fun take_most_relevant max_relevant remaining_max
blanchet@38747
   342
                       (candidates : (annotated_thm * real) list) =
blanchet@38744
   343
  let
blanchet@38747
   344
    val max_imperfect =
blanchet@38904
   345
      Real.ceil (Math.pow (!max_imperfect,
blanchet@38904
   346
                    Math.pow (Real.fromInt remaining_max
blanchet@38904
   347
                              / Real.fromInt max_relevant, !max_imperfect_exp)))
blanchet@38747
   348
    val (perfect, imperfect) =
blanchet@38889
   349
      candidates |> sort (Real.compare o swap o pairself snd)
blanchet@38889
   350
                 |> take_prefix (fn (_, w) => w > 0.99999)
blanchet@38747
   351
    val ((accepts, more_rejects), rejects) =
blanchet@38747
   352
      chop max_imperfect imperfect |>> append perfect |>> chop remaining_max
blanchet@38744
   353
  in
blanchet@38889
   354
    trace_msg (fn () =>
blanchet@38889
   355
        "Actually passed (" ^ Int.toString (length accepts) ^ " of " ^
blanchet@38889
   356
        Int.toString (length candidates) ^ "): " ^
blanchet@38889
   357
        (accepts |> map (fn ((((name, _), _), _), weight) =>
blanchet@38752
   358
                            name () ^ " [" ^ Real.toString weight ^ "]")
blanchet@38745
   359
                 |> commas));
blanchet@38747
   360
    (accepts, more_rejects @ rejects)
blanchet@38744
   361
  end
paulson@24287
   362
blanchet@38819
   363
fun if_empty_replace_with_locality thy axioms loc tab =
blanchet@38819
   364
  if Symtab.is_empty tab then
blanchet@38823
   365
    get_pconsts thy false (SOME false)
blanchet@38819
   366
        (map_filter (fn ((_, loc'), th) =>
blanchet@38819
   367
                        if loc' = loc then SOME (prop_of th) else NONE) axioms)
blanchet@38819
   368
  else
blanchet@38819
   369
    tab
blanchet@38819
   370
blanchet@38752
   371
(* FUDGE *)
blanchet@38899
   372
val threshold_divisor = Unsynchronized.ref 2.0
blanchet@38899
   373
val ridiculous_threshold = Unsynchronized.ref 0.1
blanchet@38683
   374
blanchet@38745
   375
fun relevance_filter ctxt threshold0 decay max_relevant theory_relevant
blanchet@38745
   376
                     ({add, del, ...} : relevance_override) axioms goal_ts =
blanchet@38739
   377
  let
blanchet@38739
   378
    val thy = ProofContext.theory_of ctxt
blanchet@38819
   379
    val const_tab =
blanchet@38819
   380
      fold (count_axiom_consts theory_relevant thy) axioms Symtab.empty
blanchet@38819
   381
    val goal_const_tab =
blanchet@38823
   382
      get_pconsts thy false (SOME false) goal_ts
blanchet@38819
   383
      |> fold (if_empty_replace_with_locality thy axioms)
blanchet@38819
   384
              [Chained, Local, Theory]
blanchet@38739
   385
    val add_thms = maps (ProofContext.get_fact ctxt) add
blanchet@38739
   386
    val del_thms = maps (ProofContext.get_fact ctxt) del
blanchet@38747
   387
    fun iter j remaining_max threshold rel_const_tab hopeless hopeful =
blanchet@38739
   388
      let
blanchet@38744
   389
        fun game_over rejects =
blanchet@38747
   390
          (* Add "add:" facts. *)
blanchet@38747
   391
          if null add_thms then
blanchet@38747
   392
            []
blanchet@38744
   393
          else
blanchet@38747
   394
            map_filter (fn ((p as (_, th), _), _) =>
blanchet@38747
   395
                           if member Thm.eq_thm add_thms th then SOME p
blanchet@38747
   396
                           else NONE) rejects
blanchet@38889
   397
        fun relevant [] rejects [] =
blanchet@38747
   398
            (* Nothing has been added this iteration. *)
blanchet@38899
   399
            if j = 0 andalso threshold >= !ridiculous_threshold then
blanchet@38747
   400
              (* First iteration? Try again. *)
blanchet@38899
   401
              iter 0 max_relevant (threshold / !threshold_divisor) rel_const_tab
blanchet@38747
   402
                   hopeless hopeful
blanchet@38744
   403
            else
blanchet@38747
   404
              game_over (rejects @ hopeless)
blanchet@38889
   405
          | relevant candidates rejects [] =
blanchet@38739
   406
            let
blanchet@38747
   407
              val (accepts, more_rejects) =
blanchet@38904
   408
                take_most_relevant max_relevant remaining_max candidates
blanchet@38739
   409
              val rel_const_tab' =
blanchet@38745
   410
                rel_const_tab
blanchet@38901
   411
                |> fold (add_pconst_to_table false) (maps (snd o fst) accepts)
blanchet@38744
   412
              fun is_dirty (c, _) =
blanchet@38744
   413
                Symtab.lookup rel_const_tab' c <> Symtab.lookup rel_const_tab c
blanchet@38745
   414
              val (hopeful_rejects, hopeless_rejects) =
blanchet@38745
   415
                 (rejects @ hopeless, ([], []))
blanchet@38745
   416
                 |-> fold (fn (ax as (_, consts), old_weight) =>
blanchet@38745
   417
                              if exists is_dirty consts then
blanchet@38745
   418
                                apfst (cons (ax, NONE))
blanchet@38745
   419
                              else
blanchet@38745
   420
                                apsnd (cons (ax, old_weight)))
blanchet@38745
   421
                 |>> append (more_rejects
blanchet@38745
   422
                             |> map (fn (ax as (_, consts), old_weight) =>
blanchet@38745
   423
                                        (ax, if exists is_dirty consts then NONE
blanchet@38745
   424
                                             else SOME old_weight)))
blanchet@38747
   425
              val threshold =
blanchet@38822
   426
                1.0 - (1.0 - threshold)
blanchet@38822
   427
                      * Math.pow (decay, Real.fromInt (length accepts))
blanchet@38747
   428
              val remaining_max = remaining_max - length accepts
blanchet@38739
   429
            in
blanchet@38744
   430
              trace_msg (fn () => "New or updated constants: " ^
blanchet@38744
   431
                  commas (rel_const_tab' |> Symtab.dest
blanchet@38822
   432
                          |> subtract (op =) (rel_const_tab |> Symtab.dest)
blanchet@38827
   433
                          |> map string_for_hyper_pconst));
blanchet@38745
   434
              map (fst o fst) accepts @
blanchet@38747
   435
              (if remaining_max = 0 then
blanchet@38745
   436
                 game_over (hopeful_rejects @ map (apsnd SOME) hopeless_rejects)
blanchet@38745
   437
               else
blanchet@38747
   438
                 iter (j + 1) remaining_max threshold rel_const_tab'
blanchet@38747
   439
                      hopeless_rejects hopeful_rejects)
blanchet@38739
   440
            end
blanchet@38889
   441
          | relevant candidates rejects
blanchet@38752
   442
                     (((ax as (((_, loc), th), axiom_consts)), cached_weight)
blanchet@38747
   443
                      :: hopeful) =
blanchet@38739
   444
            let
blanchet@38739
   445
              val weight =
blanchet@38739
   446
                case cached_weight of
blanchet@38739
   447
                  SOME w => w
blanchet@38752
   448
                | NONE => axiom_weight loc const_tab rel_const_tab axiom_consts
blanchet@38904
   449
(* FIXME: experiment
blanchet@38752
   450
val name = fst (fst (fst ax)) ()
blanchet@38904
   451
val _ = if String.isSubstring "abs_of_neg" name then
blanchet@38901
   452
tracing ("*** " ^ name ^ PolyML.makestring (debug_axiom_weight loc const_tab rel_const_tab axiom_consts))
blanchet@38747
   453
else
blanchet@38747
   454
()
blanchet@38747
   455
*)
blanchet@38739
   456
            in
blanchet@38741
   457
              if weight >= threshold then
blanchet@38889
   458
                relevant ((ax, weight) :: candidates) rejects hopeful
blanchet@38739
   459
              else
blanchet@38889
   460
                relevant candidates ((ax, weight) :: rejects) hopeful
blanchet@38739
   461
            end
blanchet@38739
   462
        in
blanchet@38744
   463
          trace_msg (fn () =>
blanchet@38744
   464
              "ITERATION " ^ string_of_int j ^ ": current threshold: " ^
blanchet@38744
   465
              Real.toString threshold ^ ", constants: " ^
blanchet@38744
   466
              commas (rel_const_tab |> Symtab.dest
blanchet@38744
   467
                      |> filter (curry (op <>) [] o snd)
blanchet@38827
   468
                      |> map string_for_hyper_pconst));
blanchet@38889
   469
          relevant [] [] hopeful
blanchet@38739
   470
        end
blanchet@38739
   471
  in
blanchet@38739
   472
    axioms |> filter_out (member Thm.eq_thm del_thms o snd)
blanchet@38827
   473
           |> map_filter (pair_consts_axiom theory_relevant thy)
blanchet@38819
   474
           |> iter 0 max_relevant threshold0 goal_const_tab []
blanchet@38739
   475
           |> tap (fn res => trace_msg (fn () =>
blanchet@38686
   476
                                "Total relevant: " ^ Int.toString (length res)))
blanchet@38739
   477
  end
paulson@24287
   478
blanchet@38744
   479
paulson@24287
   480
(***************************************************************)
mengj@19768
   481
(* Retrieving and filtering lemmas                             *)
mengj@19768
   482
(***************************************************************)
mengj@19768
   483
paulson@33022
   484
(*** retrieve lemmas and filter them ***)
mengj@19768
   485
paulson@20757
   486
(*Reject theorems with names like "List.filter.filter_list_def" or
paulson@21690
   487
  "Accessible_Part.acc.defs", as these are definitions arising from packages.*)
paulson@20757
   488
fun is_package_def a =
wenzelm@30364
   489
  let val names = Long_Name.explode a
paulson@21690
   490
  in
paulson@21690
   491
     length names > 2 andalso
paulson@21690
   492
     not (hd names = "local") andalso
paulson@21690
   493
     String.isSuffix "_def" a  orelse  String.isSuffix "_defs" a
paulson@21690
   494
  end;
paulson@20757
   495
blanchet@38085
   496
fun make_fact_table xs =
blanchet@37616
   497
  fold (Termtab.update o `(prop_of o snd)) xs Termtab.empty
blanchet@38085
   498
fun make_unique xs = Termtab.fold (cons o snd) (make_fact_table xs) []
mengj@19768
   499
blanchet@37626
   500
(* FIXME: put other record thms here, or declare as "no_atp" *)
blanchet@37626
   501
val multi_base_blacklist =
blanchet@37626
   502
  ["defs", "select_defs", "update_defs", "induct", "inducts", "split", "splits",
blanchet@38682
   503
   "split_asm", "cases", "ext_cases", "eq.simps", "eq.refl", "nchotomy",
blanchet@38682
   504
   "case_cong", "weak_case_cong"]
blanchet@38682
   505
  |> map (prefix ".")
blanchet@37626
   506
blanchet@37626
   507
val max_lambda_nesting = 3
blanchet@37626
   508
blanchet@37626
   509
fun term_has_too_many_lambdas max (t1 $ t2) =
blanchet@37626
   510
    exists (term_has_too_many_lambdas max) [t1, t2]
blanchet@37626
   511
  | term_has_too_many_lambdas max (Abs (_, _, t)) =
blanchet@37626
   512
    max = 0 orelse term_has_too_many_lambdas (max - 1) t
blanchet@37626
   513
  | term_has_too_many_lambdas _ _ = false
blanchet@37626
   514
blanchet@37626
   515
(* Don't count nested lambdas at the level of formulas, since they are
blanchet@37626
   516
   quantifiers. *)
blanchet@37626
   517
fun formula_has_too_many_lambdas Ts (Abs (_, T, t)) =
blanchet@37626
   518
    formula_has_too_many_lambdas (T :: Ts) t
blanchet@37626
   519
  | formula_has_too_many_lambdas Ts t =
blanchet@37626
   520
    if is_formula_type (fastype_of1 (Ts, t)) then
blanchet@37626
   521
      exists (formula_has_too_many_lambdas Ts) (#2 (strip_comb t))
blanchet@37626
   522
    else
blanchet@37626
   523
      term_has_too_many_lambdas max_lambda_nesting t
blanchet@37626
   524
blanchet@38692
   525
(* The max apply depth of any "metis" call in "Metis_Examples" (on 2007-10-31)
blanchet@37626
   526
   was 11. *)
blanchet@37626
   527
val max_apply_depth = 15
blanchet@37626
   528
blanchet@37626
   529
fun apply_depth (f $ t) = Int.max (apply_depth f, apply_depth t + 1)
blanchet@37626
   530
  | apply_depth (Abs (_, _, t)) = apply_depth t
blanchet@37626
   531
  | apply_depth _ = 0
blanchet@37626
   532
blanchet@37626
   533
fun is_formula_too_complex t =
blanchet@38085
   534
  apply_depth t > max_apply_depth orelse formula_has_too_many_lambdas [] t
blanchet@37626
   535
blanchet@37543
   536
val exists_sledgehammer_const =
blanchet@37626
   537
  exists_Const (fn (s, _) => String.isPrefix sledgehammer_prefix s)
blanchet@37626
   538
blanchet@38904
   539
(* FIXME: make more reliable *)
blanchet@38904
   540
val exists_low_level_class_const =
blanchet@38904
   541
  exists_Const (fn (s, _) =>
blanchet@38904
   542
     String.isSubstring (Long_Name.separator ^ "class" ^ Long_Name.separator) s)
blanchet@38904
   543
blanchet@38821
   544
fun is_metastrange_theorem th =
blanchet@37626
   545
  case head_of (concl_of th) of
blanchet@37626
   546
      Const (a, _) => (a <> @{const_name Trueprop} andalso
blanchet@37626
   547
                       a <> @{const_name "=="})
blanchet@37626
   548
    | _ => false
blanchet@37626
   549
blanchet@38821
   550
fun is_that_fact th =
blanchet@38821
   551
  String.isSuffix (Long_Name.separator ^ Obtain.thatN) (Thm.get_name_hint th)
blanchet@38821
   552
  andalso exists_subterm (fn Free (s, _) => s = Name.skolem Auto_Bind.thesisN
blanchet@38821
   553
                           | _ => false) (prop_of th)
blanchet@38821
   554
blanchet@37626
   555
val type_has_top_sort =
blanchet@37626
   556
  exists_subtype (fn TFree (_, []) => true | TVar (_, []) => true | _ => false)
blanchet@37626
   557
blanchet@38085
   558
(**** Predicates to detect unwanted facts (prolific or likely to cause
blanchet@37347
   559
      unsoundness) ****)
paulson@21470
   560
blanchet@38289
   561
(* Too general means, positive equality literal with a variable X as one
blanchet@38289
   562
   operand, when X does not occur properly in the other operand. This rules out
blanchet@38289
   563
   clearly inconsistent facts such as X = a | X = b, though it by no means
blanchet@38289
   564
   guarantees soundness. *)
paulson@21470
   565
blanchet@38289
   566
(* Unwanted equalities are those between a (bound or schematic) variable that
blanchet@38289
   567
   does not properly occur in the second operand. *)
blanchet@38607
   568
val is_exhaustive_finite =
blanchet@38607
   569
  let
blanchet@38629
   570
    fun is_bad_equal (Var z) t =
blanchet@38629
   571
        not (exists_subterm (fn Var z' => z = z' | _ => false) t)
blanchet@38629
   572
      | is_bad_equal (Bound j) t = not (loose_bvar1 (t, j))
blanchet@38629
   573
      | is_bad_equal _ _ = false
blanchet@38629
   574
    fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1
blanchet@38607
   575
    fun do_formula pos t =
blanchet@38607
   576
      case (pos, t) of
blanchet@38615
   577
        (_, @{const Trueprop} $ t1) => do_formula pos t1
blanchet@38607
   578
      | (true, Const (@{const_name all}, _) $ Abs (_, _, t')) =>
blanchet@38607
   579
        do_formula pos t'
blanchet@38607
   580
      | (true, Const (@{const_name All}, _) $ Abs (_, _, t')) =>
blanchet@38607
   581
        do_formula pos t'
blanchet@38607
   582
      | (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) =>
blanchet@38607
   583
        do_formula pos t'
blanchet@38607
   584
      | (_, @{const "==>"} $ t1 $ t2) =>
blanchet@38629
   585
        do_formula (not pos) t1 andalso
blanchet@38629
   586
        (t2 = @{prop False} orelse do_formula pos t2)
haftmann@38786
   587
      | (_, @{const HOL.implies} $ t1 $ t2) =>
blanchet@38629
   588
        do_formula (not pos) t1 andalso
blanchet@38629
   589
        (t2 = @{const False} orelse do_formula pos t2)
blanchet@38607
   590
      | (_, @{const Not} $ t1) => do_formula (not pos) t1
haftmann@38795
   591
      | (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
haftmann@38795
   592
      | (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
blanchet@38607
   593
      | (true, Const (@{const_name "op ="}, _) $ t1 $ t2) => do_equals t1 t2
blanchet@38607
   594
      | (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2
blanchet@38607
   595
      | _ => false
blanchet@38607
   596
  in do_formula true end
blanchet@38607
   597
blanchet@38592
   598
fun has_bound_or_var_of_type tycons =
blanchet@38592
   599
  exists_subterm (fn Var (_, Type (s, _)) => member (op =) tycons s
blanchet@38592
   600
                   | Abs (_, Type (s, _), _) => member (op =) tycons s
blanchet@38592
   601
                   | _ => false)
paulson@21431
   602
blanchet@38085
   603
(* Facts are forbidden to contain variables of these types. The typical reason
blanchet@37347
   604
   is that they lead to unsoundness. Note that "unit" satisfies numerous
blanchet@38085
   605
   equations like "?x = ()". The resulting clauses will have no type constraint,
blanchet@37347
   606
   yielding false proofs. Even "bool" leads to many unsound proofs, though only
blanchet@37347
   607
   for higher-order problems. *)
blanchet@38592
   608
val dangerous_types = [@{type_name unit}, @{type_name bool}, @{type_name prop}];
paulson@22217
   609
blanchet@38085
   610
(* Facts containing variables of type "unit" or "bool" or of the form
blanchet@38290
   611
   "ALL x. x = A | x = B | x = C" are likely to lead to unsound proofs if types
blanchet@38290
   612
   are omitted. *)
blanchet@38593
   613
fun is_dangerous_term full_types t =
blanchet@38609
   614
  not full_types andalso
blanchet@38679
   615
  let val t = transform_elim_term t in
blanchet@38679
   616
    has_bound_or_var_of_type dangerous_types t orelse
blanchet@38679
   617
    is_exhaustive_finite t
blanchet@38679
   618
  end
paulson@21470
   619
blanchet@38627
   620
fun is_theorem_bad_for_atps full_types thm =
blanchet@38627
   621
  let val t = prop_of thm in
blanchet@38627
   622
    is_formula_too_complex t orelse exists_type type_has_top_sort t orelse
blanchet@38627
   623
    is_dangerous_term full_types t orelse exists_sledgehammer_const t orelse
blanchet@38904
   624
    exists_low_level_class_const t orelse is_metastrange_theorem thm orelse
blanchet@38904
   625
    is_that_fact thm
blanchet@38627
   626
  end
blanchet@38627
   627
blanchet@38696
   628
fun all_name_thms_pairs ctxt reserved full_types add_thms chained_ths =
blanchet@38627
   629
  let
blanchet@38752
   630
    val thy = ProofContext.theory_of ctxt
blanchet@38752
   631
    val thy_prefix = Context.theory_name thy ^ Long_Name.separator
blanchet@38752
   632
    val global_facts = PureThy.facts_of thy
blanchet@38644
   633
    val local_facts = ProofContext.facts_of ctxt
blanchet@38644
   634
    val named_locals = local_facts |> Facts.dest_static []
blanchet@38752
   635
    val is_chained = member Thm.eq_thm chained_ths
blanchet@38818
   636
    (* Unnamed nonchained formulas with schematic variables are omitted, because
blanchet@38818
   637
       they are rejected by the backticks (`...`) parser for some reason. *)
blanchet@38738
   638
    fun is_good_unnamed_local th =
blanchet@38820
   639
      not (Thm.has_name_hint th) andalso
blanchet@38820
   640
      (not (exists_subterm is_Var (prop_of th)) orelse (is_chained th)) andalso
blanchet@38738
   641
      forall (fn (_, ths) => not (member Thm.eq_thm ths th)) named_locals
blanchet@38644
   642
    val unnamed_locals =
blanchet@38820
   643
      union Thm.eq_thm (Facts.props local_facts) chained_ths
blanchet@38820
   644
      |> filter is_good_unnamed_local |> map (pair "" o single)
blanchet@38627
   645
    val full_space =
blanchet@38738
   646
      Name_Space.merge (Facts.space_of global_facts, Facts.space_of local_facts)
blanchet@38752
   647
    fun add_facts global foldx facts =
blanchet@38699
   648
      foldx (fn (name0, ths) =>
blanchet@38699
   649
        if name0 <> "" andalso
blanchet@38699
   650
           forall (not o member Thm.eq_thm add_thms) ths andalso
blanchet@38699
   651
           (Facts.is_concealed facts name0 orelse
blanchet@38699
   652
            (respect_no_atp andalso is_package_def name0) orelse
blanchet@38699
   653
            exists (fn s => String.isSuffix s name0) multi_base_blacklist orelse
blanchet@38699
   654
            String.isSuffix "_def_raw" (* FIXME: crude hack *) name0) then
blanchet@38627
   655
          I
blanchet@38627
   656
        else
blanchet@38627
   657
          let
blanchet@38752
   658
            val base_loc =
blanchet@38752
   659
              if not global then Local
blanchet@38752
   660
              else if String.isPrefix thy_prefix name0 then Theory
blanchet@38752
   661
              else General
blanchet@38699
   662
            val multi = length ths > 1
blanchet@38696
   663
            fun backquotify th =
blanchet@38696
   664
              "`" ^ Print_Mode.setmp [Print_Mode.input]
blanchet@38696
   665
                                 (Syntax.string_of_term ctxt) (prop_of th) ^ "`"
blanchet@38738
   666
              |> String.translate (fn c => if Char.isPrint c then str c else "")
blanchet@38738
   667
              |> simplify_spaces
blanchet@38699
   668
            fun check_thms a =
blanchet@38699
   669
              case try (ProofContext.get_thms ctxt) a of
blanchet@38699
   670
                NONE => false
blanchet@38699
   671
              | SOME ths' => Thm.eq_thms (ths, ths')
blanchet@38627
   672
          in
blanchet@38699
   673
            pair 1
blanchet@38699
   674
            #> fold (fn th => fn (j, rest) =>
blanchet@38699
   675
                 (j + 1,
blanchet@38699
   676
                  if is_theorem_bad_for_atps full_types th andalso
blanchet@38699
   677
                     not (member Thm.eq_thm add_thms th) then
blanchet@38699
   678
                    rest
blanchet@38699
   679
                  else
blanchet@38752
   680
                    (((fn () =>
blanchet@38752
   681
                          if name0 = "" then
blanchet@38752
   682
                            th |> backquotify
blanchet@38752
   683
                          else
blanchet@38752
   684
                            let
blanchet@38752
   685
                              val name1 = Facts.extern facts name0
blanchet@38752
   686
                              val name2 = Name_Space.extern full_space name0
blanchet@38752
   687
                            in
blanchet@38752
   688
                              case find_first check_thms [name1, name2, name0] of
blanchet@38752
   689
                                SOME name => repair_name reserved multi j name
blanchet@38752
   690
                              | NONE => ""
blanchet@38752
   691
                            end), if is_chained th then Chained else base_loc),
blanchet@38752
   692
                      (multi, th)) :: rest)) ths
blanchet@38699
   693
            #> snd
blanchet@38627
   694
          end)
blanchet@38644
   695
  in
blanchet@38752
   696
    [] |> add_facts false fold local_facts (unnamed_locals @ named_locals)
blanchet@38752
   697
       |> add_facts true Facts.fold_static global_facts global_facts
blanchet@38644
   698
  end
blanchet@38627
   699
blanchet@38627
   700
(* The single-name theorems go after the multiple-name ones, so that single
blanchet@38627
   701
   names are preferred when both are available. *)
blanchet@38699
   702
fun name_thm_pairs ctxt respect_no_atp =
blanchet@38744
   703
  List.partition (fst o snd) #> op @ #> map (apsnd snd)
blanchet@38699
   704
  #> respect_no_atp ? filter_out (No_ATPs.member ctxt o snd)
blanchet@38627
   705
blanchet@38627
   706
(***************************************************************)
blanchet@38627
   707
(* ATP invocation methods setup                                *)
blanchet@38627
   708
(***************************************************************)
blanchet@38627
   709
blanchet@38891
   710
fun relevant_facts ctxt full_types (threshold0, threshold1) max_relevant
blanchet@38744
   711
                   theory_relevant (relevance_override as {add, del, only})
blanchet@38891
   712
                   chained_ths hyp_ts concl_t =
blanchet@37538
   713
  let
blanchet@38822
   714
    val decay = Math.pow ((1.0 - threshold1) / (1.0 - threshold0),
blanchet@38822
   715
                          1.0 / Real.fromInt (max_relevant + 1))
blanchet@37538
   716
    val add_thms = maps (ProofContext.get_fact ctxt) add
blanchet@38696
   717
    val reserved = reserved_isar_keyword_table ()
blanchet@37538
   718
    val axioms =
blanchet@38699
   719
      (if only then
blanchet@38752
   720
         maps (map (fn ((name, loc), th) => ((K name, loc), (true, th)))
blanchet@38752
   721
               o name_thm_pairs_from_ref ctxt reserved chained_ths) add
blanchet@38699
   722
       else
blanchet@38699
   723
         all_name_thms_pairs ctxt reserved full_types add_thms chained_ths)
blanchet@38688
   724
      |> name_thm_pairs ctxt (respect_no_atp andalso not only)
blanchet@38595
   725
      |> make_unique
blanchet@37538
   726
  in
blanchet@38688
   727
    trace_msg (fn () => "Considering " ^ Int.toString (length axioms) ^
blanchet@38688
   728
                        " theorems");
blanchet@38745
   729
    (if threshold0 > 1.0 orelse threshold0 > threshold1 then
blanchet@38739
   730
       []
blanchet@38745
   731
     else if threshold0 < 0.0 then
blanchet@38739
   732
       axioms
blanchet@38739
   733
     else
blanchet@38745
   734
       relevance_filter ctxt threshold0 decay max_relevant theory_relevant
blanchet@38745
   735
                        relevance_override axioms (concl_t :: hyp_ts))
blanchet@38822
   736
    |> map (apfst (apfst (fn f => f ())))
blanchet@37538
   737
  end
immler@30536
   738
paulson@15347
   739
end;