src/HOL/Tools/Sledgehammer/sledgehammer_filter.ML
author wenzelm
Sat Apr 16 16:15:37 2011 +0200 (2011-04-16 ago)
changeset 42361 23f352990944
parent 42358 b47d41d9f4b5
child 42449 494e4ac5b0f8
permissions -rw-r--r--
modernized structure Proof_Context;
blanchet@38988
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_filter.ML
blanchet@38027
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
blanchet@36393
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@39958
     4
blanchet@39958
     5
Sledgehammer's relevance filter.
wenzelm@33309
     6
*)
paulson@15452
     7
blanchet@38988
     8
signature SLEDGEHAMMER_FILTER =
wenzelm@16802
     9
sig
blanchet@38993
    10
  datatype locality = General | Intro | Elim | Simp | Local | Assum | Chained
blanchet@38752
    11
blanchet@40070
    12
  type relevance_fudge =
blanchet@41159
    13
    {allow_ext : bool,
blanchet@41790
    14
     local_const_multiplier : real,
blanchet@41159
    15
     worse_irrel_freq : real,
blanchet@40070
    16
     higher_order_irrel_weight : real,
blanchet@40070
    17
     abs_rel_weight : real,
blanchet@40070
    18
     abs_irrel_weight : real,
blanchet@40070
    19
     skolem_irrel_weight : real,
blanchet@40070
    20
     theory_const_rel_weight : real,
blanchet@40070
    21
     theory_const_irrel_weight : real,
blanchet@40070
    22
     intro_bonus : real,
blanchet@40070
    23
     elim_bonus : real,
blanchet@40070
    24
     simp_bonus : real,
blanchet@40070
    25
     local_bonus : real,
blanchet@40070
    26
     assum_bonus : real,
blanchet@40070
    27
     chained_bonus : real,
blanchet@40070
    28
     max_imperfect : real,
blanchet@40070
    29
     max_imperfect_exp : real,
blanchet@40070
    30
     threshold_divisor : real,
blanchet@40070
    31
     ridiculous_threshold : real}
blanchet@40070
    32
blanchet@35966
    33
  type relevance_override =
blanchet@40070
    34
    {add : (Facts.ref * Attrib.src list) list,
blanchet@40070
    35
     del : (Facts.ref * Attrib.src list) list,
blanchet@40070
    36
     only : bool}
blanchet@35966
    37
blanchet@37616
    38
  val trace : bool Unsynchronized.ref
blanchet@40205
    39
  val fact_from_ref :
blanchet@38996
    40
    Proof.context -> unit Symtab.table -> thm list
blanchet@38996
    41
    -> Facts.ref * Attrib.src list -> ((string * locality) * thm) list
blanchet@41767
    42
  val all_facts :
blanchet@41989
    43
    Proof.context -> 'a Symtab.table -> bool -> bool -> relevance_fudge
blanchet@41989
    44
    -> thm list -> thm list -> (((unit -> string) * locality) * (bool * thm)) list
blanchet@41768
    45
  val const_names_in_fact :
blanchet@41768
    46
    theory -> (string * typ -> term list -> bool * term list) -> term
blanchet@41768
    47
    -> string list
blanchet@37347
    48
  val relevant_facts :
blanchet@41066
    49
    Proof.context -> bool -> real * real -> int
blanchet@41336
    50
    -> (string * typ -> term list -> bool * term list) -> relevance_fudge
blanchet@41066
    51
    -> relevance_override -> thm list -> term list -> term
blanchet@40070
    52
    -> ((string * locality) * thm) list
paulson@15347
    53
end;
paulson@15347
    54
blanchet@38988
    55
structure Sledgehammer_Filter : SLEDGEHAMMER_FILTER =
paulson@15347
    56
struct
paulson@15347
    57
blanchet@38652
    58
open Sledgehammer_Util
blanchet@38652
    59
blanchet@37616
    60
val trace = Unsynchronized.ref false
blanchet@37616
    61
fun trace_msg msg = if !trace then tracing (msg ()) else ()
blanchet@35826
    62
blanchet@41273
    63
(* experimental features *)
blanchet@38997
    64
val respect_no_atp = true
blanchet@41273
    65
val instantiate_inducts = false
blanchet@38827
    66
blanchet@38993
    67
datatype locality = General | Intro | Elim | Simp | Local | Assum | Chained
blanchet@38752
    68
blanchet@40070
    69
type relevance_fudge =
blanchet@41159
    70
  {allow_ext : bool,
blanchet@41790
    71
   local_const_multiplier : real,
blanchet@41159
    72
   worse_irrel_freq : real,
blanchet@40070
    73
   higher_order_irrel_weight : real,
blanchet@40070
    74
   abs_rel_weight : real,
blanchet@40070
    75
   abs_irrel_weight : real,
blanchet@40070
    76
   skolem_irrel_weight : real,
blanchet@40070
    77
   theory_const_rel_weight : real,
blanchet@40070
    78
   theory_const_irrel_weight : real,
blanchet@40070
    79
   intro_bonus : real,
blanchet@40070
    80
   elim_bonus : real,
blanchet@40070
    81
   simp_bonus : real,
blanchet@40070
    82
   local_bonus : real,
blanchet@40070
    83
   assum_bonus : real,
blanchet@40070
    84
   chained_bonus : real,
blanchet@40070
    85
   max_imperfect : real,
blanchet@40070
    86
   max_imperfect_exp : real,
blanchet@40070
    87
   threshold_divisor : real,
blanchet@40070
    88
   ridiculous_threshold : real}
blanchet@40070
    89
blanchet@35966
    90
type relevance_override =
blanchet@40070
    91
  {add : (Facts.ref * Attrib.src list) list,
blanchet@40070
    92
   del : (Facts.ref * Attrib.src list) list,
blanchet@40070
    93
   only : bool}
paulson@21070
    94
blanchet@37616
    95
val sledgehammer_prefix = "Sledgehammer" ^ Long_Name.separator
blanchet@39896
    96
val abs_name = sledgehammer_prefix ^ "abs"
blanchet@39896
    97
val skolem_prefix = sledgehammer_prefix ^ "sko"
blanchet@38992
    98
val theory_const_suffix = Long_Name.separator ^ " 1"
blanchet@37616
    99
blanchet@40227
   100
fun needs_quoting reserved s =
blanchet@40375
   101
  Symtab.defined reserved s orelse
wenzelm@42290
   102
  exists (not o Lexicon.is_identifier) (Long_Name.explode s)
blanchet@40227
   103
blanchet@40375
   104
fun make_name reserved multi j name =
blanchet@40227
   105
  (name |> needs_quoting reserved name ? quote) ^
wenzelm@41491
   106
  (if multi then "(" ^ string_of_int j ^ ")" else "")
blanchet@38744
   107
blanchet@40375
   108
fun explode_interval _ (Facts.FromTo (i, j)) = i upto j
blanchet@40375
   109
  | explode_interval max (Facts.From i) = i upto i + max - 1
blanchet@40375
   110
  | explode_interval _ (Facts.Single i) = [i]
blanchet@40375
   111
blanchet@41279
   112
val backquote =
blanchet@41279
   113
  raw_explode #> map (fn "`" => "\\`" | s => s) #> implode #> enclose "`" "`"
blanchet@40205
   114
fun fact_from_ref ctxt reserved chained_ths (xthm as (xref, args)) =
blanchet@38744
   115
  let
blanchet@38996
   116
    val ths = Attrib.eval_thms ctxt [xthm]
blanchet@38996
   117
    val bracket =
blanchet@41999
   118
      map (enclose "[" "]" o Pretty.str_of o Args.pretty_src ctxt) args
blanchet@41999
   119
      |> implode
blanchet@40375
   120
    fun nth_name j =
blanchet@38996
   121
      case xref of
blanchet@41279
   122
        Facts.Fact s => backquote s ^ bracket
blanchet@38996
   123
      | Facts.Named (("", _), _) => "[" ^ bracket ^ "]"
blanchet@40375
   124
      | Facts.Named ((name, _), NONE) =>
blanchet@40375
   125
        make_name reserved (length ths > 1) (j + 1) name ^ bracket
blanchet@40375
   126
      | Facts.Named ((name, _), SOME intervals) =>
blanchet@40375
   127
        make_name reserved true
blanchet@40375
   128
                 (nth (maps (explode_interval (length ths)) intervals) j) name ^
blanchet@40375
   129
        bracket
blanchet@38744
   130
  in
blanchet@40375
   131
    (ths, (0, []))
blanchet@38752
   132
    |-> fold (fn th => fn (j, rest) =>
blanchet@40375
   133
                 (j + 1, ((nth_name j,
blanchet@38752
   134
                          if member Thm.eq_thm chained_ths th then Chained
blanchet@38752
   135
                          else General), th) :: rest))
blanchet@38744
   136
    |> snd
blanchet@38699
   137
  end
blanchet@37616
   138
blanchet@41199
   139
(* This is a terrible hack. Free variables are sometimes code as "M__" when they
blanchet@41199
   140
   are displayed as "M" and we want to avoid clashes with these. But sometimes
blanchet@41199
   141
   it's even worse: "Ma__" encodes "M". So we simply reserve all prefixes of all
blanchet@41199
   142
   free variables. In the worse case scenario, where the fact won't be resolved
blanchet@41199
   143
   correctly, the user can fix it manually, e.g., by naming the fact in
blanchet@41199
   144
   question. Ideally we would need nothing of it, but backticks just don't work
blanchet@41199
   145
   with schematic variables. *)
blanchet@41199
   146
fun all_prefixes_of s =
blanchet@41199
   147
  map (fn i => String.extract (s, 0, SOME i)) (1 upto size s - 1)
blanchet@41199
   148
fun close_form t =
blanchet@41199
   149
  (t, [] |> Term.add_free_names t |> maps all_prefixes_of)
blanchet@41199
   150
  |> fold (fn ((s, i), T) => fn (t', taken) =>
blanchet@41199
   151
              let val s' = Name.variant taken s in
blanchet@41199
   152
                ((if fastype_of t' = HOLogic.boolT then HOLogic.all_const
blanchet@41199
   153
                  else Term.all) T
blanchet@41199
   154
                 $ Abs (s', T, abstract_over (Var ((s, i), T), t')),
blanchet@41199
   155
                 s' :: taken)
blanchet@41199
   156
              end)
blanchet@41199
   157
          (Term.add_vars t [] |> sort_wrt (fst o fst))
blanchet@41199
   158
  |> fst
blanchet@41199
   159
blanchet@41199
   160
fun string_for_term ctxt t =
blanchet@41199
   161
  Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN)
blanchet@41199
   162
                   (print_mode_value ())) (Syntax.string_of_term ctxt) t
blanchet@41199
   163
  |> String.translate (fn c => if Char.isPrint c then str c else "")
blanchet@41199
   164
  |> simplify_spaces
blanchet@41199
   165
blanchet@41199
   166
(** Structural induction rules **)
blanchet@41199
   167
blanchet@41200
   168
fun struct_induct_rule_on th =
blanchet@41199
   169
  case Logic.strip_horn (prop_of th) of
blanchet@41199
   170
    (prems, @{const Trueprop}
blanchet@41199
   171
            $ ((p as Var ((p_name, 0), _)) $ (a as Var (_, ind_T)))) =>
blanchet@41199
   172
    if not (is_TVar ind_T) andalso length prems > 1 andalso
blanchet@41199
   173
       exists (exists_subterm (curry (op aconv) p)) prems andalso
blanchet@41199
   174
       not (exists (exists_subterm (curry (op aconv) a)) prems) then
blanchet@41199
   175
      SOME (p_name, ind_T)
blanchet@41199
   176
    else
blanchet@41199
   177
      NONE
blanchet@41199
   178
  | _ => NONE
blanchet@41199
   179
blanchet@41207
   180
fun instantiate_induct_rule ctxt concl_prop p_name ((name, loc), (multi, th))
blanchet@41199
   181
                            ind_x =
blanchet@41199
   182
  let
blanchet@41199
   183
    fun varify_noninducts (t as Free (s, T)) =
blanchet@41199
   184
        if (s, T) = ind_x orelse can dest_funT T then t else Var ((s, 0), T)
blanchet@41199
   185
      | varify_noninducts t = t
blanchet@41199
   186
    val p_inst =
blanchet@41199
   187
      concl_prop |> map_aterms varify_noninducts |> close_form
blanchet@41199
   188
                 |> lambda (Free ind_x)
blanchet@41199
   189
                 |> string_for_term ctxt
blanchet@41199
   190
  in
blanchet@41207
   191
    ((fn () => name () ^ "[where " ^ p_name ^ " = " ^ quote p_inst ^ "]", loc),
blanchet@41199
   192
     (multi, th |> read_instantiate ctxt [((p_name, 0), p_inst)]))
blanchet@41199
   193
  end
blanchet@41199
   194
blanchet@41199
   195
fun type_match thy (T1, T2) =
blanchet@41199
   196
  (Sign.typ_match thy (T2, T1) Vartab.empty; true)
blanchet@41199
   197
  handle Type.TYPE_MATCH => false
blanchet@41199
   198
blanchet@41199
   199
fun instantiate_if_induct_rule ctxt stmt stmt_xs (ax as (_, (_, th))) =
blanchet@41200
   200
  case struct_induct_rule_on th of
blanchet@41199
   201
    SOME (p_name, ind_T) =>
wenzelm@42361
   202
    let val thy = Proof_Context.theory_of ctxt in
blanchet@41199
   203
      stmt_xs |> filter (fn (_, T) => type_match thy (T, ind_T))
blanchet@41199
   204
              |> map_filter (try (instantiate_induct_rule ctxt stmt p_name ax))
blanchet@41199
   205
    end
blanchet@41199
   206
  | NONE => [ax]
blanchet@41199
   207
wenzelm@28477
   208
(***************************************************************)
wenzelm@28477
   209
(* Relevance Filtering                                         *)
wenzelm@28477
   210
(***************************************************************)
mengj@19194
   211
paulson@24287
   212
(*** constants with types ***)
paulson@24287
   213
blanchet@38939
   214
fun order_of_type (Type (@{type_name fun}, [T1, @{typ bool}])) =
blanchet@38939
   215
    order_of_type T1 (* cheat: pretend sets are first-order *)
blanchet@38939
   216
  | order_of_type (Type (@{type_name fun}, [T1, T2])) =
blanchet@38939
   217
    Int.max (order_of_type T1 + 1, order_of_type T2)
blanchet@38939
   218
  | order_of_type (Type (_, Ts)) = fold (Integer.max o order_of_type) Ts 0
blanchet@38939
   219
  | order_of_type _ = 0
blanchet@38939
   220
blanchet@38823
   221
(* An abstraction of Isabelle types and first-order terms *)
blanchet@38823
   222
datatype pattern = PVar | PApp of string * pattern list
blanchet@38939
   223
datatype ptype = PType of int * pattern list
blanchet@38744
   224
blanchet@38823
   225
fun string_for_pattern PVar = "_"
blanchet@38823
   226
  | string_for_pattern (PApp (s, ps)) =
blanchet@38823
   227
    if null ps then s else s ^ string_for_patterns ps
blanchet@38823
   228
and string_for_patterns ps = "(" ^ commas (map string_for_pattern ps) ^ ")"
blanchet@38939
   229
fun string_for_ptype (PType (_, ps)) = string_for_patterns ps
paulson@24287
   230
paulson@24287
   231
(*Is the second type an instance of the first one?*)
blanchet@38824
   232
fun match_pattern (PVar, _) = true
blanchet@38824
   233
  | match_pattern (PApp _, PVar) = false
blanchet@38824
   234
  | match_pattern (PApp (s, ps), PApp (t, qs)) =
blanchet@38824
   235
    s = t andalso match_patterns (ps, qs)
blanchet@38824
   236
and match_patterns (_, []) = true
blanchet@38824
   237
  | match_patterns ([], _) = false
blanchet@38824
   238
  | match_patterns (p :: ps, q :: qs) =
blanchet@38824
   239
    match_pattern (p, q) andalso match_patterns (ps, qs)
blanchet@38939
   240
fun match_ptype (PType (_, ps), PType (_, qs)) = match_patterns (ps, qs)
paulson@24287
   241
blanchet@38823
   242
(* Is there a unifiable constant? *)
blanchet@38827
   243
fun pconst_mem f consts (s, ps) =
blanchet@38939
   244
  exists (curry (match_ptype o f) ps)
blanchet@38827
   245
         (map snd (filter (curry (op =) s o fst) consts))
blanchet@38827
   246
fun pconst_hyper_mem f const_tab (s, ps) =
blanchet@38939
   247
  exists (curry (match_ptype o f) ps) (these (Symtab.lookup const_tab s))
blanchet@37505
   248
blanchet@38939
   249
fun pattern_for_type (Type (s, Ts)) = PApp (s, map pattern_for_type Ts)
blanchet@38939
   250
  | pattern_for_type (TFree (s, _)) = PApp (s, [])
blanchet@38939
   251
  | pattern_for_type (TVar _) = PVar
blanchet@38827
   252
blanchet@38744
   253
(* Pairs a constant with the list of its type instantiations. *)
blanchet@41204
   254
fun ptype thy const x =
blanchet@38939
   255
  (if const then map pattern_for_type (these (try (Sign.const_typargs thy) x))
blanchet@41204
   256
   else [])
blanchet@41204
   257
fun rich_ptype thy const (s, T) =
blanchet@41204
   258
  PType (order_of_type T, ptype thy const (s, T))
blanchet@41204
   259
fun rich_pconst thy const (s, T) = (s, rich_ptype thy const (s, T))
paulson@24287
   260
blanchet@38939
   261
fun string_for_hyper_pconst (s, ps) =
blanchet@38939
   262
  s ^ "{" ^ commas (map string_for_ptype ps) ^ "}"
paulson@24287
   263
blanchet@38823
   264
(* Add a pconstant to the table, but a [] entry means a standard
blanchet@38819
   265
   connective, which we ignore.*)
blanchet@41066
   266
fun add_pconst_to_table also_skolem (s, p) =
blanchet@41066
   267
  if (not also_skolem andalso String.isPrefix skolem_prefix s) then I
blanchet@41066
   268
  else Symtab.map_default (s, [p]) (insert (op =) p)
blanchet@38819
   269
blanchet@40369
   270
fun pconsts_in_terms thy is_built_in_const also_skolems pos ts =
blanchet@37505
   271
  let
blanchet@38819
   272
    val flip = Option.map not
blanchet@38587
   273
    (* We include free variables, as well as constants, to handle locales. For
blanchet@41205
   274
       each quantifiers that must necessarily be skolemized by the automatic
blanchet@41205
   275
       prover, we introduce a fresh constant to simulate the effect of
blanchet@41205
   276
       Skolemization. *)
blanchet@40373
   277
    fun do_const const x ts =
blanchet@41336
   278
      let val (built_in, ts) = is_built_in_const x ts in
blanchet@41336
   279
        (not built_in
blanchet@41336
   280
         ? add_pconst_to_table also_skolems (rich_pconst thy const x))
blanchet@41336
   281
        #> fold do_term ts
blanchet@41336
   282
      end
blanchet@38827
   283
    and do_term t =
blanchet@38827
   284
      case strip_comb t of
blanchet@38827
   285
        (Const x, ts) => do_const true x ts
blanchet@38827
   286
      | (Free x, ts) => do_const false x ts
blanchet@38939
   287
      | (Abs (_, T, t'), ts) =>
blanchet@38939
   288
        (null ts
blanchet@41066
   289
         ? add_pconst_to_table true (abs_name, PType (order_of_type T + 1, [])))
blanchet@38827
   290
        #> fold do_term (t' :: ts)
blanchet@38827
   291
      | (_, ts) => fold do_term ts
blanchet@38939
   292
    fun do_quantifier will_surely_be_skolemized abs_T body_t =
blanchet@37537
   293
      do_formula pos body_t
blanchet@38747
   294
      #> (if also_skolems andalso will_surely_be_skolemized then
blanchet@41066
   295
            add_pconst_to_table true
blanchet@41207
   296
                (gensym skolem_prefix, PType (order_of_type abs_T, []))
blanchet@38587
   297
          else
blanchet@38587
   298
            I)
blanchet@38587
   299
    and do_term_or_formula T =
blanchet@41273
   300
      if T = HOLogic.boolT then do_formula NONE else do_term
blanchet@37537
   301
    and do_formula pos t =
blanchet@37537
   302
      case t of
blanchet@38939
   303
        Const (@{const_name all}, _) $ Abs (_, T, t') =>
blanchet@38939
   304
        do_quantifier (pos = SOME false) T t'
blanchet@37537
   305
      | @{const "==>"} $ t1 $ t2 =>
blanchet@37537
   306
        do_formula (flip pos) t1 #> do_formula pos t2
blanchet@37537
   307
      | Const (@{const_name "=="}, Type (_, [T, _])) $ t1 $ t2 =>
blanchet@38587
   308
        fold (do_term_or_formula T) [t1, t2]
blanchet@37537
   309
      | @{const Trueprop} $ t1 => do_formula pos t1
blanchet@41140
   310
      | @{const False} => I
blanchet@41140
   311
      | @{const True} => I
blanchet@37537
   312
      | @{const Not} $ t1 => do_formula (flip pos) t1
blanchet@38939
   313
      | Const (@{const_name All}, _) $ Abs (_, T, t') =>
blanchet@38939
   314
        do_quantifier (pos = SOME false) T t'
blanchet@38939
   315
      | Const (@{const_name Ex}, _) $ Abs (_, T, t') =>
blanchet@38939
   316
        do_quantifier (pos = SOME true) T t'
haftmann@38795
   317
      | @{const HOL.conj} $ t1 $ t2 => fold (do_formula pos) [t1, t2]
haftmann@38795
   318
      | @{const HOL.disj} $ t1 $ t2 => fold (do_formula pos) [t1, t2]
haftmann@38786
   319
      | @{const HOL.implies} $ t1 $ t2 =>
blanchet@37537
   320
        do_formula (flip pos) t1 #> do_formula pos t2
haftmann@38864
   321
      | Const (@{const_name HOL.eq}, Type (_, [T, _])) $ t1 $ t2 =>
blanchet@38587
   322
        fold (do_term_or_formula T) [t1, t2]
blanchet@38587
   323
      | Const (@{const_name If}, Type (_, [_, Type (_, [T, _])]))
blanchet@38587
   324
        $ t1 $ t2 $ t3 =>
blanchet@38587
   325
        do_formula NONE t1 #> fold (do_term_or_formula T) [t2, t3]
blanchet@38939
   326
      | Const (@{const_name Ex1}, _) $ Abs (_, T, t') =>
blanchet@38939
   327
        do_quantifier (is_some pos) T t'
blanchet@38939
   328
      | Const (@{const_name Ball}, _) $ t1 $ Abs (_, T, t') =>
blanchet@38939
   329
        do_quantifier (pos = SOME false) T
blanchet@38939
   330
                      (HOLogic.mk_imp (incr_boundvars 1 t1 $ Bound 0, t'))
blanchet@38939
   331
      | Const (@{const_name Bex}, _) $ t1 $ Abs (_, T, t') =>
blanchet@38939
   332
        do_quantifier (pos = SOME true) T
blanchet@38939
   333
                      (HOLogic.mk_conj (incr_boundvars 1 t1 $ Bound 0, t'))
blanchet@37537
   334
      | (t0 as Const (_, @{typ bool})) $ t1 =>
blanchet@37537
   335
        do_term t0 #> do_formula pos t1  (* theory constant *)
blanchet@37537
   336
      | _ => do_term t
blanchet@38819
   337
  in Symtab.empty |> fold (do_formula pos) ts end
paulson@24287
   338
paulson@24287
   339
(*Inserts a dummy "constant" referring to the theory name, so that relevance
paulson@24287
   340
  takes the given theory into account.*)
blanchet@41200
   341
fun theory_constify ({theory_const_rel_weight, theory_const_irrel_weight, ...}
blanchet@41200
   342
                     : relevance_fudge) thy_name t =
blanchet@40070
   343
  if exists (curry (op <) 0.0) [theory_const_rel_weight,
blanchet@40070
   344
                                theory_const_irrel_weight] then
blanchet@41200
   345
    Const (thy_name ^ theory_const_suffix, @{typ bool}) $ t
blanchet@37505
   346
  else
blanchet@41200
   347
    t
blanchet@41200
   348
blanchet@41200
   349
fun theory_const_prop_of fudge th =
blanchet@41200
   350
  theory_constify fudge (Context.theory_name (theory_of_thm th)) (prop_of th)
blanchet@37505
   351
paulson@24287
   352
(**** Constant / Type Frequencies ****)
paulson@24287
   353
blanchet@38743
   354
(* A two-dimensional symbol table counts frequencies of constants. It's keyed
blanchet@38743
   355
   first by constant name and second by its list of type instantiations. For the
blanchet@38823
   356
   latter, we need a linear ordering on "pattern list". *)
paulson@24287
   357
blanchet@38823
   358
fun pattern_ord p =
blanchet@38743
   359
  case p of
blanchet@38744
   360
    (PVar, PVar) => EQUAL
blanchet@38823
   361
  | (PVar, PApp _) => LESS
blanchet@38823
   362
  | (PApp _, PVar) => GREATER
blanchet@38823
   363
  | (PApp q1, PApp q2) =>
blanchet@38823
   364
    prod_ord fast_string_ord (dict_ord pattern_ord) (q1, q2)
blanchet@38939
   365
fun ptype_ord (PType p, PType q) =
blanchet@38939
   366
  prod_ord (dict_ord pattern_ord) int_ord (swap p, swap q)
paulson@24287
   367
blanchet@38939
   368
structure PType_Tab = Table(type key = ptype val ord = ptype_ord)
paulson@24287
   369
blanchet@40204
   370
fun count_fact_consts thy fudge =
blanchet@37503
   371
  let
blanchet@38827
   372
    fun do_const const (s, T) ts =
blanchet@38827
   373
      (* Two-dimensional table update. Constant maps to types maps to count. *)
blanchet@41204
   374
      PType_Tab.map_default (rich_ptype thy const (s, T), 0) (Integer.add 1)
blanchet@38939
   375
      |> Symtab.map_default (s, PType_Tab.empty)
blanchet@38827
   376
      #> fold do_term ts
blanchet@38827
   377
    and do_term t =
blanchet@38827
   378
      case strip_comb t of
blanchet@38827
   379
        (Const x, ts) => do_const true x ts
blanchet@38827
   380
      | (Free x, ts) => do_const false x ts
blanchet@38827
   381
      | (Abs (_, _, t'), ts) => fold do_term (t' :: ts)
blanchet@38827
   382
      | (_, ts) => fold do_term ts
blanchet@40070
   383
  in do_term o theory_const_prop_of fudge o snd end
paulson@24287
   384
paulson@24287
   385
paulson@24287
   386
(**** Actual Filtering Code ****)
paulson@24287
   387
blanchet@39367
   388
fun pow_int _ 0 = 1.0
blanchet@38939
   389
  | pow_int x 1 = x
blanchet@38939
   390
  | pow_int x n = if n > 0 then x * pow_int x (n - 1) else pow_int x (n + 1) / x
blanchet@38939
   391
paulson@24287
   392
(*The frequency of a constant is the sum of those of all instances of its type.*)
blanchet@38824
   393
fun pconst_freq match const_tab (c, ps) =
blanchet@38939
   394
  PType_Tab.fold (fn (qs, m) => match (ps, qs) ? Integer.add m)
blanchet@38939
   395
                 (the (Symtab.lookup const_tab c)) 0
blanchet@38686
   396
paulson@24287
   397
blanchet@38085
   398
(* A surprising number of theorems contain only a few significant constants.
blanchet@38085
   399
   These include all induction rules, and other general theorems. *)
blanchet@37503
   400
blanchet@37503
   401
(* "log" seems best in practice. A constant function of one ignores the constant
blanchet@38938
   402
   frequencies. Rare constants give more points if they are relevant than less
blanchet@38938
   403
   rare ones. *)
blanchet@39367
   404
fun rel_weight_for _ freq = 1.0 + 2.0 / Math.ln (Real.fromInt freq + 1.0)
blanchet@38938
   405
blanchet@38938
   406
(* Irrelevant constants are treated differently. We associate lower penalties to
blanchet@38938
   407
   very rare constants and very common ones -- the former because they can't
blanchet@38938
   408
   lead to the inclusion of too many new facts, and the latter because they are
blanchet@38938
   409
   so common as to be of little interest. *)
blanchet@40070
   410
fun irrel_weight_for ({worse_irrel_freq, higher_order_irrel_weight, ...}
blanchet@40070
   411
                      : relevance_fudge) order freq =
blanchet@40070
   412
  let val (k, x) = worse_irrel_freq |> `Real.ceil in
blanchet@38939
   413
    (if freq < k then Math.ln (Real.fromInt (freq + 1)) / Math.ln x
blanchet@38939
   414
     else rel_weight_for order freq / rel_weight_for order k)
blanchet@40070
   415
    * pow_int higher_order_irrel_weight (order - 1)
blanchet@38938
   416
  end
blanchet@37503
   417
blanchet@41790
   418
fun multiplier_for_const_name local_const_multiplier s =
blanchet@41790
   419
  if String.isSubstring "." s then 1.0 else local_const_multiplier
blanchet@38821
   420
blanchet@41790
   421
(* Computes a constant's weight, as determined by its frequency. *)
blanchet@41790
   422
fun generic_pconst_weight local_const_multiplier abs_weight skolem_weight
blanchet@41790
   423
                          theory_const_weight weight_for f const_tab
blanchet@41790
   424
                          (c as (s, PType (m, _))) =
blanchet@41790
   425
  if s = abs_name then
blanchet@41790
   426
    abs_weight
blanchet@41790
   427
  else if String.isPrefix skolem_prefix s then
blanchet@41790
   428
    skolem_weight
blanchet@41790
   429
  else if String.isSuffix theory_const_suffix s then
blanchet@41790
   430
    theory_const_weight
blanchet@41790
   431
  else
blanchet@41790
   432
    multiplier_for_const_name local_const_multiplier s
blanchet@41790
   433
    * weight_for m (pconst_freq (match_ptype o f) const_tab c)
blanchet@41790
   434
blanchet@41790
   435
fun rel_pconst_weight ({local_const_multiplier, abs_rel_weight,
blanchet@41790
   436
                        theory_const_rel_weight, ...} : relevance_fudge)
blanchet@41790
   437
                      const_tab =
blanchet@41790
   438
  generic_pconst_weight local_const_multiplier abs_rel_weight 0.0
blanchet@41790
   439
                        theory_const_rel_weight rel_weight_for I const_tab
blanchet@41790
   440
fun irrel_pconst_weight (fudge as {local_const_multiplier, abs_irrel_weight,
blanchet@41790
   441
                                   skolem_irrel_weight,
blanchet@40070
   442
                                   theory_const_irrel_weight, ...}) const_tab =
blanchet@41790
   443
  generic_pconst_weight local_const_multiplier abs_irrel_weight
blanchet@41790
   444
                        skolem_irrel_weight theory_const_irrel_weight
blanchet@41790
   445
                        (irrel_weight_for fudge) swap const_tab
paulson@24287
   446
blanchet@40070
   447
fun locality_bonus (_ : relevance_fudge) General = 0.0
blanchet@40070
   448
  | locality_bonus {intro_bonus, ...} Intro = intro_bonus
blanchet@40070
   449
  | locality_bonus {elim_bonus, ...} Elim = elim_bonus
blanchet@40070
   450
  | locality_bonus {simp_bonus, ...} Simp = simp_bonus
blanchet@40070
   451
  | locality_bonus {local_bonus, ...} Local = local_bonus
blanchet@40070
   452
  | locality_bonus {assum_bonus, ...} Assum = assum_bonus
blanchet@40070
   453
  | locality_bonus {chained_bonus, ...} Chained = chained_bonus
blanchet@38751
   454
blanchet@40418
   455
fun is_odd_const_name s =
blanchet@40418
   456
  s = abs_name orelse String.isPrefix skolem_prefix s orelse
blanchet@40418
   457
  String.isSuffix theory_const_suffix s
blanchet@40418
   458
blanchet@40204
   459
fun fact_weight fudge loc const_tab relevant_consts fact_consts =
blanchet@40204
   460
  case fact_consts |> List.partition (pconst_hyper_mem I relevant_consts)
blanchet@40204
   461
                   ||> filter_out (pconst_hyper_mem swap relevant_consts) of
blanchet@38827
   462
    ([], _) => 0.0
blanchet@38744
   463
  | (rel, irrel) =>
blanchet@40418
   464
    if forall (forall (is_odd_const_name o fst)) [rel, irrel] then
blanchet@40371
   465
      0.0
blanchet@40371
   466
    else
blanchet@40371
   467
      let
blanchet@40371
   468
        val irrel = irrel |> filter_out (pconst_mem swap rel)
blanchet@40371
   469
        val rel_weight =
blanchet@40371
   470
          0.0 |> fold (curry (op +) o rel_pconst_weight fudge const_tab) rel
blanchet@40371
   471
        val irrel_weight =
blanchet@40371
   472
          ~ (locality_bonus fudge loc)
blanchet@40371
   473
          |> fold (curry (op +) o irrel_pconst_weight fudge const_tab) irrel
blanchet@40371
   474
        val res = rel_weight / (rel_weight + irrel_weight)
blanchet@40371
   475
      in if Real.isFinite res then res else 0.0 end
blanchet@38747
   476
blanchet@40369
   477
fun pconsts_in_fact thy is_built_in_const t =
blanchet@38825
   478
  Symtab.fold (fn (s, pss) => fold (cons o pair s) pss)
blanchet@40369
   479
              (pconsts_in_terms thy is_built_in_const true (SOME true) [t]) []
blanchet@40369
   480
fun pair_consts_fact thy is_built_in_const fudge fact =
blanchet@40204
   481
  case fact |> snd |> theory_const_prop_of fudge
blanchet@40369
   482
            |> pconsts_in_fact thy is_built_in_const of
blanchet@38827
   483
    [] => NONE
blanchet@40204
   484
  | consts => SOME ((fact, consts), NONE)
paulson@24287
   485
blanchet@41768
   486
val const_names_in_fact = map fst ooo pconsts_in_fact
blanchet@41768
   487
blanchet@38699
   488
type annotated_thm =
blanchet@38939
   489
  (((unit -> string) * locality) * thm) * (string * ptype) list
blanchet@37505
   490
blanchet@38904
   491
fun take_most_relevant max_relevant remaining_max
blanchet@40070
   492
        ({max_imperfect, max_imperfect_exp, ...} : relevance_fudge) 
blanchet@40070
   493
        (candidates : (annotated_thm * real) list) =
blanchet@38744
   494
  let
blanchet@38747
   495
    val max_imperfect =
blanchet@40070
   496
      Real.ceil (Math.pow (max_imperfect,
blanchet@38904
   497
                    Math.pow (Real.fromInt remaining_max
blanchet@40070
   498
                              / Real.fromInt max_relevant, max_imperfect_exp)))
blanchet@38747
   499
    val (perfect, imperfect) =
blanchet@38889
   500
      candidates |> sort (Real.compare o swap o pairself snd)
blanchet@38889
   501
                 |> take_prefix (fn (_, w) => w > 0.99999)
blanchet@38747
   502
    val ((accepts, more_rejects), rejects) =
blanchet@38747
   503
      chop max_imperfect imperfect |>> append perfect |>> chop remaining_max
blanchet@38744
   504
  in
blanchet@38889
   505
    trace_msg (fn () =>
wenzelm@41491
   506
        "Actually passed (" ^ string_of_int (length accepts) ^ " of " ^
wenzelm@41491
   507
        string_of_int (length candidates) ^ "): " ^
blanchet@38889
   508
        (accepts |> map (fn ((((name, _), _), _), weight) =>
blanchet@38752
   509
                            name () ^ " [" ^ Real.toString weight ^ "]")
blanchet@38745
   510
                 |> commas));
blanchet@38747
   511
    (accepts, more_rejects @ rejects)
blanchet@38744
   512
  end
paulson@24287
   513
blanchet@40369
   514
fun if_empty_replace_with_locality thy is_built_in_const facts loc tab =
blanchet@38819
   515
  if Symtab.is_empty tab then
blanchet@40369
   516
    pconsts_in_terms thy is_built_in_const false (SOME false)
blanchet@38819
   517
        (map_filter (fn ((_, loc'), th) =>
blanchet@40204
   518
                        if loc' = loc then SOME (prop_of th) else NONE) facts)
blanchet@38819
   519
  else
blanchet@38819
   520
    tab
blanchet@38819
   521
blanchet@41158
   522
fun add_arities is_built_in_const th =
blanchet@41158
   523
  let
blanchet@41158
   524
    fun aux _ _ NONE = NONE
blanchet@41158
   525
      | aux t args (SOME tab) =
blanchet@41158
   526
        case t of
blanchet@41158
   527
          t1 $ t2 => SOME tab |> aux t1 (t2 :: args) |> aux t2 []
blanchet@41158
   528
        | Const (x as (s, _)) =>
blanchet@41336
   529
          (if is_built_in_const x args |> fst then
blanchet@41158
   530
             SOME tab
blanchet@41158
   531
           else case Symtab.lookup tab s of
blanchet@41158
   532
             NONE => SOME (Symtab.update (s, length args) tab)
blanchet@41158
   533
           | SOME n => if n = length args then SOME tab else NONE)
blanchet@41158
   534
        | _ => SOME tab
blanchet@41158
   535
  in aux (prop_of th) [] end
blanchet@41158
   536
blanchet@41158
   537
fun needs_ext is_built_in_const facts =
blanchet@41158
   538
  fold (add_arities is_built_in_const o snd) facts (SOME Symtab.empty)
blanchet@41158
   539
  |> is_none
blanchet@41158
   540
blanchet@40369
   541
fun relevance_filter ctxt threshold0 decay max_relevant is_built_in_const
blanchet@40070
   542
        (fudge as {threshold_divisor, ridiculous_threshold, ...})
blanchet@40204
   543
        ({add, del, ...} : relevance_override) facts goal_ts =
blanchet@38739
   544
  let
wenzelm@42361
   545
    val thy = Proof_Context.theory_of ctxt
blanchet@40204
   546
    val const_tab = fold (count_fact_consts thy fudge) facts Symtab.empty
blanchet@38819
   547
    val goal_const_tab =
blanchet@40369
   548
      pconsts_in_terms thy is_built_in_const false (SOME false) goal_ts
blanchet@40369
   549
      |> fold (if_empty_replace_with_locality thy is_built_in_const facts)
blanchet@38993
   550
              [Chained, Assum, Local]
blanchet@39012
   551
    val add_ths = Attrib.eval_thms ctxt add
blanchet@39012
   552
    val del_ths = Attrib.eval_thms ctxt del
blanchet@40204
   553
    val facts = facts |> filter_out (member Thm.eq_thm del_ths o snd)
blanchet@38747
   554
    fun iter j remaining_max threshold rel_const_tab hopeless hopeful =
blanchet@38739
   555
      let
blanchet@40191
   556
        fun relevant [] _ [] =
blanchet@38747
   557
            (* Nothing has been added this iteration. *)
blanchet@40070
   558
            if j = 0 andalso threshold >= ridiculous_threshold then
blanchet@38747
   559
              (* First iteration? Try again. *)
blanchet@40070
   560
              iter 0 max_relevant (threshold / threshold_divisor) rel_const_tab
blanchet@38747
   561
                   hopeless hopeful
blanchet@38744
   562
            else
blanchet@40191
   563
              []
blanchet@38889
   564
          | relevant candidates rejects [] =
blanchet@38739
   565
            let
blanchet@38747
   566
              val (accepts, more_rejects) =
blanchet@40070
   567
                take_most_relevant max_relevant remaining_max fudge candidates
blanchet@38739
   568
              val rel_const_tab' =
blanchet@38745
   569
                rel_const_tab
blanchet@41066
   570
                |> fold (add_pconst_to_table false) (maps (snd o fst) accepts)
blanchet@38744
   571
              fun is_dirty (c, _) =
blanchet@38744
   572
                Symtab.lookup rel_const_tab' c <> Symtab.lookup rel_const_tab c
blanchet@38745
   573
              val (hopeful_rejects, hopeless_rejects) =
blanchet@38745
   574
                 (rejects @ hopeless, ([], []))
blanchet@38745
   575
                 |-> fold (fn (ax as (_, consts), old_weight) =>
blanchet@38745
   576
                              if exists is_dirty consts then
blanchet@38745
   577
                                apfst (cons (ax, NONE))
blanchet@38745
   578
                              else
blanchet@38745
   579
                                apsnd (cons (ax, old_weight)))
blanchet@38745
   580
                 |>> append (more_rejects
blanchet@38745
   581
                             |> map (fn (ax as (_, consts), old_weight) =>
blanchet@38745
   582
                                        (ax, if exists is_dirty consts then NONE
blanchet@38745
   583
                                             else SOME old_weight)))
blanchet@38747
   584
              val threshold =
blanchet@38822
   585
                1.0 - (1.0 - threshold)
blanchet@38822
   586
                      * Math.pow (decay, Real.fromInt (length accepts))
blanchet@38747
   587
              val remaining_max = remaining_max - length accepts
blanchet@38739
   588
            in
blanchet@38744
   589
              trace_msg (fn () => "New or updated constants: " ^
blanchet@38744
   590
                  commas (rel_const_tab' |> Symtab.dest
blanchet@38822
   591
                          |> subtract (op =) (rel_const_tab |> Symtab.dest)
blanchet@38827
   592
                          |> map string_for_hyper_pconst));
blanchet@38745
   593
              map (fst o fst) accepts @
blanchet@38747
   594
              (if remaining_max = 0 then
blanchet@40191
   595
                 []
blanchet@38745
   596
               else
blanchet@38747
   597
                 iter (j + 1) remaining_max threshold rel_const_tab'
blanchet@38747
   598
                      hopeless_rejects hopeful_rejects)
blanchet@38739
   599
            end
blanchet@38889
   600
          | relevant candidates rejects
blanchet@40204
   601
                     (((ax as (((_, loc), _), fact_consts)), cached_weight)
blanchet@38747
   602
                      :: hopeful) =
blanchet@38739
   603
            let
blanchet@38739
   604
              val weight =
blanchet@38739
   605
                case cached_weight of
blanchet@38739
   606
                  SOME w => w
blanchet@40204
   607
                | NONE => fact_weight fudge loc const_tab rel_const_tab
blanchet@40204
   608
                                      fact_consts
blanchet@38739
   609
            in
blanchet@38741
   610
              if weight >= threshold then
blanchet@38889
   611
                relevant ((ax, weight) :: candidates) rejects hopeful
blanchet@38739
   612
              else
blanchet@38889
   613
                relevant candidates ((ax, weight) :: rejects) hopeful
blanchet@38739
   614
            end
blanchet@38739
   615
        in
blanchet@38744
   616
          trace_msg (fn () =>
blanchet@38744
   617
              "ITERATION " ^ string_of_int j ^ ": current threshold: " ^
blanchet@38744
   618
              Real.toString threshold ^ ", constants: " ^
blanchet@38744
   619
              commas (rel_const_tab |> Symtab.dest
blanchet@38744
   620
                      |> filter (curry (op <>) [] o snd)
blanchet@38827
   621
                      |> map string_for_hyper_pconst));
blanchet@38889
   622
          relevant [] [] hopeful
blanchet@38739
   623
        end
blanchet@41158
   624
    fun add_facts ths accepts =
blanchet@41167
   625
      (facts |> filter (member Thm.eq_thm ths o snd)) @
blanchet@41167
   626
      (accepts |> filter_out (member Thm.eq_thm ths o snd))
blanchet@40408
   627
      |> take max_relevant
blanchet@38739
   628
  in
blanchet@40369
   629
    facts |> map_filter (pair_consts_fact thy is_built_in_const fudge)
blanchet@40204
   630
          |> iter 0 max_relevant threshold0 goal_const_tab []
blanchet@41158
   631
          |> not (null add_ths) ? add_facts add_ths
blanchet@41158
   632
          |> (fn accepts =>
blanchet@41158
   633
                 accepts |> needs_ext is_built_in_const accepts
blanchet@41158
   634
                            ? add_facts @{thms ext})
blanchet@41158
   635
          |> tap (fn accepts => trace_msg (fn () =>
wenzelm@41491
   636
                      "Total relevant: " ^ string_of_int (length accepts)))
blanchet@38739
   637
  end
paulson@24287
   638
blanchet@38744
   639
paulson@24287
   640
(***************************************************************)
mengj@19768
   641
(* Retrieving and filtering lemmas                             *)
mengj@19768
   642
(***************************************************************)
mengj@19768
   643
paulson@33022
   644
(*** retrieve lemmas and filter them ***)
mengj@19768
   645
paulson@20757
   646
(*Reject theorems with names like "List.filter.filter_list_def" or
paulson@21690
   647
  "Accessible_Part.acc.defs", as these are definitions arising from packages.*)
paulson@20757
   648
fun is_package_def a =
blanchet@40205
   649
  let val names = Long_Name.explode a in
blanchet@40205
   650
    (length names > 2 andalso not (hd names = "local") andalso
blanchet@40205
   651
     String.isSuffix "_def" a) orelse String.isSuffix "_defs" a
blanchet@40205
   652
  end
paulson@20757
   653
blanchet@38937
   654
fun mk_fact_table f xs =
blanchet@38937
   655
  fold (Termtab.update o `(prop_of o f)) xs Termtab.empty
blanchet@38937
   656
fun uniquify xs = Termtab.fold (cons o snd) (mk_fact_table snd xs) []
mengj@19768
   657
blanchet@37626
   658
(* FIXME: put other record thms here, or declare as "no_atp" *)
blanchet@37626
   659
val multi_base_blacklist =
blanchet@41199
   660
  ["defs", "select_defs", "update_defs", "split", "splits", "split_asm",
blanchet@41199
   661
   "cases", "ext_cases", "eq.simps", "eq.refl", "nchotomy", "case_cong",
blanchet@41199
   662
   "weak_case_cong"]
blanchet@41273
   663
  |> not instantiate_inducts ? append ["induct", "inducts"]
blanchet@38682
   664
  |> map (prefix ".")
blanchet@37626
   665
blanchet@37626
   666
val max_lambda_nesting = 3
blanchet@37626
   667
blanchet@37626
   668
fun term_has_too_many_lambdas max (t1 $ t2) =
blanchet@37626
   669
    exists (term_has_too_many_lambdas max) [t1, t2]
blanchet@37626
   670
  | term_has_too_many_lambdas max (Abs (_, _, t)) =
blanchet@37626
   671
    max = 0 orelse term_has_too_many_lambdas (max - 1) t
blanchet@37626
   672
  | term_has_too_many_lambdas _ _ = false
blanchet@37626
   673
blanchet@37626
   674
(* Don't count nested lambdas at the level of formulas, since they are
blanchet@37626
   675
   quantifiers. *)
blanchet@37626
   676
fun formula_has_too_many_lambdas Ts (Abs (_, T, t)) =
blanchet@37626
   677
    formula_has_too_many_lambdas (T :: Ts) t
blanchet@37626
   678
  | formula_has_too_many_lambdas Ts t =
blanchet@41273
   679
    if member (op =) [HOLogic.boolT, propT] (fastype_of1 (Ts, t)) then
blanchet@37626
   680
      exists (formula_has_too_many_lambdas Ts) (#2 (strip_comb t))
blanchet@37626
   681
    else
blanchet@37626
   682
      term_has_too_many_lambdas max_lambda_nesting t
blanchet@37626
   683
blanchet@38692
   684
(* The max apply depth of any "metis" call in "Metis_Examples" (on 2007-10-31)
blanchet@37626
   685
   was 11. *)
blanchet@37626
   686
val max_apply_depth = 15
blanchet@37626
   687
blanchet@37626
   688
fun apply_depth (f $ t) = Int.max (apply_depth f, apply_depth t + 1)
blanchet@37626
   689
  | apply_depth (Abs (_, _, t)) = apply_depth t
blanchet@37626
   690
  | apply_depth _ = 0
blanchet@37626
   691
blanchet@37626
   692
fun is_formula_too_complex t =
blanchet@38085
   693
  apply_depth t > max_apply_depth orelse formula_has_too_many_lambdas [] t
blanchet@37626
   694
blanchet@39946
   695
(* FIXME: Extend to "Meson" and "Metis" *)
blanchet@37543
   696
val exists_sledgehammer_const =
blanchet@37626
   697
  exists_Const (fn (s, _) => String.isPrefix sledgehammer_prefix s)
blanchet@37626
   698
blanchet@38904
   699
(* FIXME: make more reliable *)
blanchet@38904
   700
val exists_low_level_class_const =
blanchet@38904
   701
  exists_Const (fn (s, _) =>
blanchet@38904
   702
     String.isSubstring (Long_Name.separator ^ "class" ^ Long_Name.separator) s)
blanchet@38904
   703
blanchet@38821
   704
fun is_metastrange_theorem th =
blanchet@37626
   705
  case head_of (concl_of th) of
blanchet@37626
   706
      Const (a, _) => (a <> @{const_name Trueprop} andalso
blanchet@37626
   707
                       a <> @{const_name "=="})
blanchet@37626
   708
    | _ => false
blanchet@37626
   709
blanchet@38821
   710
fun is_that_fact th =
blanchet@38821
   711
  String.isSuffix (Long_Name.separator ^ Obtain.thatN) (Thm.get_name_hint th)
blanchet@38821
   712
  andalso exists_subterm (fn Free (s, _) => s = Name.skolem Auto_Bind.thesisN
blanchet@38821
   713
                           | _ => false) (prop_of th)
blanchet@38821
   714
blanchet@37626
   715
val type_has_top_sort =
blanchet@37626
   716
  exists_subtype (fn TFree (_, []) => true | TVar (_, []) => true | _ => false)
blanchet@37626
   717
blanchet@38085
   718
(**** Predicates to detect unwanted facts (prolific or likely to cause
blanchet@37347
   719
      unsoundness) ****)
paulson@21470
   720
blanchet@38289
   721
(* Too general means, positive equality literal with a variable X as one
blanchet@38289
   722
   operand, when X does not occur properly in the other operand. This rules out
blanchet@38289
   723
   clearly inconsistent facts such as X = a | X = b, though it by no means
blanchet@38289
   724
   guarantees soundness. *)
paulson@21470
   725
blanchet@38289
   726
(* Unwanted equalities are those between a (bound or schematic) variable that
blanchet@38289
   727
   does not properly occur in the second operand. *)
blanchet@38607
   728
val is_exhaustive_finite =
blanchet@38607
   729
  let
blanchet@38629
   730
    fun is_bad_equal (Var z) t =
blanchet@38629
   731
        not (exists_subterm (fn Var z' => z = z' | _ => false) t)
blanchet@38629
   732
      | is_bad_equal (Bound j) t = not (loose_bvar1 (t, j))
blanchet@38629
   733
      | is_bad_equal _ _ = false
blanchet@38629
   734
    fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1
blanchet@38607
   735
    fun do_formula pos t =
blanchet@38607
   736
      case (pos, t) of
blanchet@38615
   737
        (_, @{const Trueprop} $ t1) => do_formula pos t1
blanchet@38607
   738
      | (true, Const (@{const_name all}, _) $ Abs (_, _, t')) =>
blanchet@38607
   739
        do_formula pos t'
blanchet@38607
   740
      | (true, Const (@{const_name All}, _) $ Abs (_, _, t')) =>
blanchet@38607
   741
        do_formula pos t'
blanchet@38607
   742
      | (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) =>
blanchet@38607
   743
        do_formula pos t'
blanchet@38607
   744
      | (_, @{const "==>"} $ t1 $ t2) =>
blanchet@38629
   745
        do_formula (not pos) t1 andalso
blanchet@38629
   746
        (t2 = @{prop False} orelse do_formula pos t2)
haftmann@38786
   747
      | (_, @{const HOL.implies} $ t1 $ t2) =>
blanchet@38629
   748
        do_formula (not pos) t1 andalso
blanchet@38629
   749
        (t2 = @{const False} orelse do_formula pos t2)
blanchet@38607
   750
      | (_, @{const Not} $ t1) => do_formula (not pos) t1
haftmann@38795
   751
      | (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
haftmann@38795
   752
      | (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
haftmann@38864
   753
      | (true, Const (@{const_name HOL.eq}, _) $ t1 $ t2) => do_equals t1 t2
blanchet@38607
   754
      | (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2
blanchet@38607
   755
      | _ => false
blanchet@38607
   756
  in do_formula true end
blanchet@38607
   757
blanchet@38592
   758
fun has_bound_or_var_of_type tycons =
blanchet@38592
   759
  exists_subterm (fn Var (_, Type (s, _)) => member (op =) tycons s
blanchet@38592
   760
                   | Abs (_, Type (s, _), _) => member (op =) tycons s
blanchet@38592
   761
                   | _ => false)
paulson@21431
   762
blanchet@38085
   763
(* Facts are forbidden to contain variables of these types. The typical reason
blanchet@37347
   764
   is that they lead to unsoundness. Note that "unit" satisfies numerous
blanchet@38085
   765
   equations like "?x = ()". The resulting clauses will have no type constraint,
blanchet@37347
   766
   yielding false proofs. Even "bool" leads to many unsound proofs, though only
blanchet@37347
   767
   for higher-order problems. *)
blanchet@38592
   768
val dangerous_types = [@{type_name unit}, @{type_name bool}, @{type_name prop}];
paulson@22217
   769
blanchet@38085
   770
(* Facts containing variables of type "unit" or "bool" or of the form
blanchet@38290
   771
   "ALL x. x = A | x = B | x = C" are likely to lead to unsound proofs if types
blanchet@38290
   772
   are omitted. *)
blanchet@41138
   773
fun is_dangerous_term no_dangerous_types t =
blanchet@41138
   774
  not no_dangerous_types andalso
blanchet@38679
   775
  let val t = transform_elim_term t in
blanchet@38679
   776
    has_bound_or_var_of_type dangerous_types t orelse
blanchet@38679
   777
    is_exhaustive_finite t
blanchet@38679
   778
  end
paulson@21470
   779
blanchet@41138
   780
fun is_theorem_bad_for_atps no_dangerous_types thm =
blanchet@38627
   781
  let val t = prop_of thm in
blanchet@38627
   782
    is_formula_too_complex t orelse exists_type type_has_top_sort t orelse
blanchet@41138
   783
    is_dangerous_term no_dangerous_types t orelse
blanchet@41138
   784
    exists_sledgehammer_const t orelse exists_low_level_class_const t orelse
blanchet@41138
   785
    is_metastrange_theorem thm orelse is_that_fact thm
blanchet@38627
   786
  end
blanchet@38627
   787
blanchet@38937
   788
fun clasimpset_rules_of ctxt =
blanchet@38937
   789
  let
blanchet@38937
   790
    val {safeIs, safeEs, hazIs, hazEs, ...} = ctxt |> claset_of |> rep_cs
blanchet@38937
   791
    val intros = safeIs @ hazIs
blanchet@38937
   792
    val elims = map Classical.classical_rule (safeEs @ hazEs)
blanchet@38937
   793
    val simps = ctxt |> simpset_of |> dest_ss |> #simps |> map snd
blanchet@38937
   794
  in (mk_fact_table I intros, mk_fact_table I elims, mk_fact_table I simps) end
blanchet@38937
   795
blanchet@41989
   796
fun all_facts ctxt reserved really_all no_dangerous_types
blanchet@40205
   797
              ({intro_bonus, elim_bonus, simp_bonus, ...} : relevance_fudge)
blanchet@40205
   798
              add_ths chained_ths =
blanchet@38627
   799
  let
wenzelm@42361
   800
    val thy = Proof_Context.theory_of ctxt
wenzelm@39557
   801
    val global_facts = Global_Theory.facts_of thy
wenzelm@42361
   802
    val local_facts = Proof_Context.facts_of ctxt
blanchet@38644
   803
    val named_locals = local_facts |> Facts.dest_static []
blanchet@38993
   804
    val assms = Assumption.all_assms_of ctxt
blanchet@38993
   805
    fun is_assum th = exists (fn ct => prop_of th aconv term_of ct) assms
blanchet@38752
   806
    val is_chained = member Thm.eq_thm chained_ths
blanchet@38937
   807
    val (intros, elims, simps) =
blanchet@40070
   808
      if exists (curry (op <) 0.0) [intro_bonus, elim_bonus, simp_bonus] then
blanchet@38937
   809
        clasimpset_rules_of ctxt
blanchet@38937
   810
      else
blanchet@38937
   811
        (Termtab.empty, Termtab.empty, Termtab.empty)
blanchet@38738
   812
    fun is_good_unnamed_local th =
blanchet@38820
   813
      not (Thm.has_name_hint th) andalso
blanchet@38738
   814
      forall (fn (_, ths) => not (member Thm.eq_thm ths th)) named_locals
blanchet@38644
   815
    val unnamed_locals =
blanchet@38820
   816
      union Thm.eq_thm (Facts.props local_facts) chained_ths
blanchet@38820
   817
      |> filter is_good_unnamed_local |> map (pair "" o single)
blanchet@38627
   818
    val full_space =
blanchet@38738
   819
      Name_Space.merge (Facts.space_of global_facts, Facts.space_of local_facts)
blanchet@38752
   820
    fun add_facts global foldx facts =
blanchet@38699
   821
      foldx (fn (name0, ths) =>
blanchet@41989
   822
        if not really_all andalso name0 <> "" andalso
blanchet@39012
   823
           forall (not o member Thm.eq_thm add_ths) ths andalso
blanchet@38699
   824
           (Facts.is_concealed facts name0 orelse
blanchet@38699
   825
            (respect_no_atp andalso is_package_def name0) orelse
blanchet@38699
   826
            exists (fn s => String.isSuffix s name0) multi_base_blacklist orelse
blanchet@38699
   827
            String.isSuffix "_def_raw" (* FIXME: crude hack *) name0) then
blanchet@38627
   828
          I
blanchet@38627
   829
        else
blanchet@38627
   830
          let
blanchet@38699
   831
            val multi = length ths > 1
blanchet@41279
   832
            val backquote_thm =
blanchet@41279
   833
              backquote o string_for_term ctxt o close_form o prop_of
blanchet@38699
   834
            fun check_thms a =
wenzelm@42361
   835
              case try (Proof_Context.get_thms ctxt) a of
blanchet@38699
   836
                NONE => false
blanchet@38699
   837
              | SOME ths' => Thm.eq_thms (ths, ths')
blanchet@38627
   838
          in
blanchet@38699
   839
            pair 1
blanchet@38699
   840
            #> fold (fn th => fn (j, rest) =>
blanchet@38699
   841
                 (j + 1,
blanchet@41138
   842
                  if is_theorem_bad_for_atps no_dangerous_types th andalso
blanchet@39012
   843
                     not (member Thm.eq_thm add_ths th) then
blanchet@38699
   844
                    rest
blanchet@38699
   845
                  else
blanchet@38752
   846
                    (((fn () =>
blanchet@38752
   847
                          if name0 = "" then
blanchet@41279
   848
                            th |> backquote_thm
blanchet@38752
   849
                          else
blanchet@38752
   850
                            let
wenzelm@42358
   851
                              val name1 = Facts.extern ctxt facts name0
wenzelm@42358
   852
                              val name2 = Name_Space.extern ctxt full_space name0
blanchet@38752
   853
                            in
blanchet@38752
   854
                              case find_first check_thms [name1, name2, name0] of
blanchet@40375
   855
                                SOME name => make_name reserved multi j name
blanchet@38752
   856
                              | NONE => ""
blanchet@38937
   857
                            end),
blanchet@38937
   858
                      let val t = prop_of th in
blanchet@40071
   859
                        if is_chained th then
blanchet@40071
   860
                          Chained
blanchet@38993
   861
                        else if global then
blanchet@38993
   862
                          if Termtab.defined intros t then Intro
blanchet@38993
   863
                          else if Termtab.defined elims t then Elim
blanchet@38993
   864
                          else if Termtab.defined simps t then Simp
blanchet@38993
   865
                          else General
blanchet@38993
   866
                        else
blanchet@38993
   867
                          if is_assum th then Assum else Local
blanchet@38937
   868
                      end),
blanchet@38752
   869
                      (multi, th)) :: rest)) ths
blanchet@38699
   870
            #> snd
blanchet@38627
   871
          end)
blanchet@38644
   872
  in
blanchet@38752
   873
    [] |> add_facts false fold local_facts (unnamed_locals @ named_locals)
blanchet@38752
   874
       |> add_facts true Facts.fold_static global_facts global_facts
blanchet@38644
   875
  end
blanchet@38627
   876
blanchet@38627
   877
(* The single-name theorems go after the multiple-name ones, so that single
blanchet@38627
   878
   names are preferred when both are available. *)
blanchet@40205
   879
fun rearrange_facts ctxt respect_no_atp =
blanchet@38744
   880
  List.partition (fst o snd) #> op @ #> map (apsnd snd)
blanchet@38699
   881
  #> respect_no_atp ? filter_out (No_ATPs.member ctxt o snd)
blanchet@38627
   882
blanchet@41199
   883
fun external_frees t =
blanchet@41199
   884
  [] |> Term.add_frees t |> filter_out (can Name.dest_internal o fst)
blanchet@41199
   885
blanchet@41138
   886
fun relevant_facts ctxt no_dangerous_types (threshold0, threshold1)
blanchet@41138
   887
                   max_relevant is_built_in_const fudge
blanchet@41138
   888
                   (override as {add, only, ...}) chained_ths hyp_ts concl_t =
blanchet@37538
   889
  let
wenzelm@42361
   890
    val thy = Proof_Context.theory_of ctxt
blanchet@38822
   891
    val decay = Math.pow ((1.0 - threshold1) / (1.0 - threshold0),
blanchet@38822
   892
                          1.0 / Real.fromInt (max_relevant + 1))
blanchet@39012
   893
    val add_ths = Attrib.eval_thms ctxt add
blanchet@38696
   894
    val reserved = reserved_isar_keyword_table ()
blanchet@41199
   895
    val ind_stmt =
blanchet@41199
   896
      Logic.list_implies (hyp_ts |> filter_out (null o external_frees), concl_t)
blanchet@41211
   897
      |> Object_Logic.atomize_term thy
blanchet@41199
   898
    val ind_stmt_xs = external_frees ind_stmt
blanchet@40204
   899
    val facts =
blanchet@38699
   900
      (if only then
blanchet@38752
   901
         maps (map (fn ((name, loc), th) => ((K name, loc), (true, th)))
blanchet@40205
   902
               o fact_from_ref ctxt reserved chained_ths) add
blanchet@38699
   903
       else
blanchet@41989
   904
         all_facts ctxt reserved false no_dangerous_types fudge add_ths
blanchet@41989
   905
                   chained_ths)
blanchet@41273
   906
      |> instantiate_inducts
blanchet@41273
   907
         ? maps (instantiate_if_induct_rule ctxt ind_stmt ind_stmt_xs)
blanchet@40205
   908
      |> rearrange_facts ctxt (respect_no_atp andalso not only)
blanchet@38937
   909
      |> uniquify
blanchet@37538
   910
  in
wenzelm@41491
   911
    trace_msg (fn () => "Considering " ^ string_of_int (length facts) ^
blanchet@40204
   912
                        " facts");
blanchet@39366
   913
    (if only orelse threshold1 < 0.0 then
blanchet@40204
   914
       facts
blanchet@39366
   915
     else if threshold0 > 1.0 orelse threshold0 > threshold1 orelse
blanchet@39366
   916
             max_relevant = 0 then
blanchet@38739
   917
       []
blanchet@38739
   918
     else
blanchet@41200
   919
       ((concl_t |> theory_constify fudge (Context.theory_name thy)) :: hyp_ts)
blanchet@41200
   920
       |> relevance_filter ctxt threshold0 decay max_relevant is_built_in_const
blanchet@41200
   921
                           fudge override facts)
blanchet@38822
   922
    |> map (apfst (apfst (fn f => f ())))
blanchet@37538
   923
  end
immler@30536
   924
paulson@15347
   925
end;