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