src/Pure/Tools/find_theorems.ML
author kleing
Wed Oct 21 16:41:22 2009 +1100 (2009-10-21)
changeset 33036 c61fe520602b
parent 33029 2fefe039edf1
child 33039 5018f6a76b3f
permissions -rw-r--r--
find_theorems: better handling of abbreviations (by Timothy Bourke)
wenzelm@30143
     1
(*  Title:      Pure/Tools/find_theorems.ML
wenzelm@26283
     2
    Author:     Rafal Kolanski and Gerwin Klein, NICTA
wenzelm@16033
     3
wenzelm@16033
     4
Retrieve theorems from proof context.
wenzelm@16033
     5
*)
wenzelm@16033
     6
wenzelm@16033
     7
signature FIND_THEOREMS =
wenzelm@16033
     8
sig
wenzelm@16036
     9
  datatype 'term criterion =
Timothy@31042
    10
    Name of string | Intro | IntroIff | Elim | Dest | Solves | Simp of 'term |
kleing@29857
    11
    Pattern of 'term
wenzelm@32738
    12
  val tac_limit: int Unsynchronized.ref
wenzelm@32738
    13
  val limit: int Unsynchronized.ref
Timothy@30785
    14
  val find_theorems: Proof.context -> thm option -> int option -> bool ->
Timothy@30785
    15
    (bool * string criterion) list -> int option * (Facts.ref * thm) list
wenzelm@30186
    16
  val pretty_thm: Proof.context -> Facts.ref * thm -> Pretty.T
kleing@29857
    17
  val print_theorems: Proof.context -> thm option -> int option -> bool ->
wenzelm@16036
    18
    (bool * string criterion) list -> unit
wenzelm@16033
    19
end;
wenzelm@16033
    20
wenzelm@16033
    21
structure FindTheorems: FIND_THEOREMS =
wenzelm@16033
    22
struct
wenzelm@16033
    23
wenzelm@16033
    24
(** search criteria **)
wenzelm@16033
    25
wenzelm@16036
    26
datatype 'term criterion =
Timothy@31042
    27
  Name of string | Intro | IntroIff | Elim | Dest | Solves | Simp of 'term |
kleing@29857
    28
  Pattern of 'term;
wenzelm@16036
    29
kleing@33036
    30
fun apply_dummies tm =
kleing@33036
    31
  strip_abs tm
kleing@33036
    32
  |> fst
kleing@33036
    33
  |> map (Term.dummy_pattern o snd)
kleing@33036
    34
  |> betapplys o pair tm
kleing@33036
    35
  |> (fn x => Term.replace_dummy_patterns x 1)
kleing@33036
    36
  |> fst;
kleing@33036
    37
kleing@33036
    38
fun parse_pattern ctxt nm =
kleing@33036
    39
  let
kleing@33036
    40
    val nm' = case Syntax.parse_term ctxt nm of Const (n, _) => n | _ => nm;
kleing@33036
    41
    val consts = ProofContext.consts_of ctxt;
kleing@33036
    42
  in
kleing@33036
    43
    nm'
kleing@33036
    44
    |> Consts.intern consts
kleing@33036
    45
    |> Consts.the_abbreviation consts
kleing@33036
    46
    |> snd
kleing@33036
    47
    |> apply_dummies
kleing@33036
    48
    handle TYPE _ => ProofContext.read_term_pattern ctxt nm
kleing@33036
    49
  end;
kleing@33036
    50
wenzelm@16036
    51
fun read_criterion _ (Name name) = Name name
wenzelm@16036
    52
  | read_criterion _ Intro = Intro
Timothy@31042
    53
  | read_criterion _ IntroIff = IntroIff
wenzelm@16036
    54
  | read_criterion _ Elim = Elim
wenzelm@16036
    55
  | read_criterion _ Dest = Dest
kleing@29857
    56
  | read_criterion _ Solves = Solves
wenzelm@24683
    57
  | read_criterion ctxt (Simp str) = Simp (ProofContext.read_term_pattern ctxt str)
kleing@33036
    58
  | read_criterion ctxt (Pattern str) = Pattern (parse_pattern ctxt str);
wenzelm@16033
    59
wenzelm@16036
    60
fun pretty_criterion ctxt (b, c) =
wenzelm@16036
    61
  let
wenzelm@16036
    62
    fun prfx s = if b then s else "-" ^ s;
wenzelm@16036
    63
  in
wenzelm@16036
    64
    (case c of
wenzelm@16036
    65
      Name name => Pretty.str (prfx "name: " ^ quote name)
wenzelm@16036
    66
    | Intro => Pretty.str (prfx "intro")
Timothy@31042
    67
    | IntroIff => Pretty.str (prfx "introiff")
wenzelm@16036
    68
    | Elim => Pretty.str (prfx "elim")
wenzelm@16036
    69
    | Dest => Pretty.str (prfx "dest")
kleing@29857
    70
    | Solves => Pretty.str (prfx "solves")
kleing@16088
    71
    | Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1,
wenzelm@24920
    72
        Pretty.quote (Syntax.pretty_term ctxt (Term.show_dummy_patterns pat))]
wenzelm@16036
    73
    | Pattern pat => Pretty.enclose (prfx " \"") "\""
wenzelm@24920
    74
        [Syntax.pretty_term ctxt (Term.show_dummy_patterns pat)])
wenzelm@16036
    75
  end;
wenzelm@16033
    76
wenzelm@30142
    77
wenzelm@30142
    78
wenzelm@16033
    79
(** search criterion filters **)
wenzelm@16033
    80
kleing@16895
    81
(*generated filters are to be of the form
wenzelm@26336
    82
  input: (Facts.ref * thm)
wenzelm@17106
    83
  output: (p:int, s:int) option, where
kleing@16895
    84
    NONE indicates no match
wenzelm@17106
    85
    p is the primary sorting criterion
kleing@16895
    86
      (eg. number of assumptions in the theorem)
kleing@16895
    87
    s is the secondary sorting criterion
kleing@16895
    88
      (eg. size of the substitution for intro, elim and dest)
kleing@16895
    89
  when applying a set of filters to a thm, fold results in:
kleing@16895
    90
    (biggest p, sum of all s)
wenzelm@17106
    91
  currently p and s only matter for intro, elim, dest and simp filters,
wenzelm@17106
    92
  otherwise the default ordering is used.
kleing@16895
    93
*)
kleing@16895
    94
kleing@16088
    95
kleing@16088
    96
(* matching theorems *)
wenzelm@17106
    97
wenzelm@17205
    98
fun is_nontrivial thy = Term.is_Const o Term.head_of o ObjectLogic.drop_judgment thy;
kleing@16088
    99
wenzelm@32798
   100
(*educated guesses on HOL*)  (* FIXME broken *)
wenzelm@32798
   101
val boolT = Type ("bool", []);
wenzelm@32798
   102
val iff_const = Const ("op =", boolT --> boolT --> boolT);
Timothy@31042
   103
kleing@16964
   104
(*extract terms from term_src, refine them to the parts that concern us,
kleing@16964
   105
  if po try match them against obj else vice versa.
kleing@16964
   106
  trivial matches are ignored.
kleing@16964
   107
  returns: smallest substitution size*)
Timothy@31042
   108
fun is_matching_thm doiff (extract_terms, refine_term) ctxt po obj term_src =
kleing@16088
   109
  let
wenzelm@17106
   110
    val thy = ProofContext.theory_of ctxt;
kleing@16088
   111
wenzelm@32798
   112
    fun check_match pat = Pattern.matches thy (if po then (pat, obj) else (obj, pat));
wenzelm@16486
   113
    fun matches pat =
Timothy@31042
   114
      let
Timothy@31042
   115
        val jpat = ObjectLogic.drop_judgment thy pat;
Timothy@31042
   116
        val c = Term.head_of jpat;
Timothy@31042
   117
        val pats =
Timothy@31042
   118
          if Term.is_Const c
wenzelm@32798
   119
          then
wenzelm@32798
   120
            if doiff andalso c = iff_const then
wenzelm@32798
   121
              (pat :: map (ObjectLogic.ensure_propT thy) (snd (strip_comb jpat)))
wenzelm@32798
   122
                |> filter (is_nontrivial thy)
wenzelm@32798
   123
            else [pat]
Timothy@31042
   124
          else [];
wenzelm@32798
   125
      in filter check_match pats end;
kleing@16895
   126
kleing@16895
   127
    fun substsize pat =
wenzelm@18184
   128
      let val (_, subst) =
wenzelm@18184
   129
        Pattern.match thy (if po then (pat, obj) else (obj, pat)) (Vartab.empty, Vartab.empty)
wenzelm@17205
   130
      in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end;
kleing@16088
   131
kleing@16895
   132
    fun bestmatch [] = NONE
wenzelm@33029
   133
      | bestmatch xs = SOME (foldl1 Int.min xs);
kleing@16895
   134
kleing@16964
   135
    val match_thm = matches o refine_term;
wenzelm@16486
   136
  in
wenzelm@32798
   137
    maps match_thm (extract_terms term_src)
Timothy@31042
   138
    |> map substsize
wenzelm@26283
   139
    |> bestmatch
kleing@16088
   140
  end;
kleing@16088
   141
kleing@16088
   142
wenzelm@16033
   143
(* filter_name *)
wenzelm@16033
   144
wenzelm@17106
   145
fun filter_name str_pat (thmref, _) =
wenzelm@26336
   146
  if match_string str_pat (Facts.name_of_ref thmref)
wenzelm@17205
   147
  then SOME (0, 0) else NONE;
wenzelm@16033
   148
wenzelm@30142
   149
kleing@29857
   150
(* filter intro/elim/dest/solves rules *)
wenzelm@16033
   151
wenzelm@17205
   152
fun filter_dest ctxt goal (_, thm) =
wenzelm@16033
   153
  let
kleing@16964
   154
    val extract_dest =
wenzelm@17205
   155
     (fn thm => if Thm.no_prems thm then [] else [Thm.full_prop_of thm],
wenzelm@16033
   156
      hd o Logic.strip_imp_prems);
wenzelm@16033
   157
    val prems = Logic.prems_of_goal goal 1;
kleing@16895
   158
Timothy@31042
   159
    fun try_subst prem = is_matching_thm false extract_dest ctxt true prem thm;
wenzelm@19482
   160
    val successful = prems |> map_filter try_subst;
wenzelm@16033
   161
  in
kleing@16895
   162
    (*if possible, keep best substitution (one with smallest size)*)
wenzelm@17106
   163
    (*dest rules always have assumptions, so a dest with one
kleing@16895
   164
      assumption is as good as an intro rule with none*)
wenzelm@17205
   165
    if not (null successful)
wenzelm@33029
   166
    then SOME (Thm.nprems_of thm - 1, foldl1 Int.min successful) else NONE
wenzelm@16033
   167
  end;
wenzelm@16033
   168
Timothy@31042
   169
fun filter_intro doiff ctxt goal (_, thm) =
wenzelm@16033
   170
  let
wenzelm@17205
   171
    val extract_intro = (single o Thm.full_prop_of, Logic.strip_imp_concl);
wenzelm@16036
   172
    val concl = Logic.concl_of_goal goal 1;
Timothy@31042
   173
    val ss = is_matching_thm doiff extract_intro ctxt true concl thm;
wenzelm@16033
   174
  in
wenzelm@18939
   175
    if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
wenzelm@16033
   176
  end;
wenzelm@16033
   177
wenzelm@17205
   178
fun filter_elim ctxt goal (_, thm) =
kleing@16964
   179
  if not (Thm.no_prems thm) then
kleing@16964
   180
    let
wenzelm@17205
   181
      val rule = Thm.full_prop_of thm;
kleing@16964
   182
      val prems = Logic.prems_of_goal goal 1;
kleing@16964
   183
      val goal_concl = Logic.concl_of_goal goal 1;
wenzelm@26283
   184
      val rule_mp = hd (Logic.strip_imp_prems rule);
kleing@16964
   185
      val rule_concl = Logic.strip_imp_concl rule;
wenzelm@26283
   186
      fun combine t1 t2 = Const ("*combine*", dummyT --> dummyT) $ (t1 $ t2);
kleing@16964
   187
      val rule_tree = combine rule_mp rule_concl;
wenzelm@26283
   188
      fun goal_tree prem = combine prem goal_concl;
wenzelm@17106
   189
      fun try_subst prem =
Timothy@31042
   190
        is_matching_thm false (single, I) ctxt true (goal_tree prem) rule_tree;
wenzelm@19482
   191
      val successful = prems |> map_filter try_subst;
kleing@16964
   192
    in
wenzelm@32798
   193
      (*elim rules always have assumptions, so an elim with one
wenzelm@32798
   194
        assumption is as good as an intro rule with none*)
wenzelm@17106
   195
      if is_nontrivial (ProofContext.theory_of ctxt) (Thm.major_prem_of thm)
wenzelm@17205
   196
        andalso not (null successful)
wenzelm@33029
   197
      then SOME (Thm.nprems_of thm - 1, foldl1 Int.min successful) else NONE
kleing@16964
   198
    end
kleing@16964
   199
  else NONE
wenzelm@16036
   200
wenzelm@32738
   201
val tac_limit = Unsynchronized.ref 5;
kleing@29857
   202
wenzelm@30143
   203
fun filter_solves ctxt goal =
wenzelm@30143
   204
  let
wenzelm@30143
   205
    fun etacn thm i = Seq.take (! tac_limit) o etac thm i;
wenzelm@30143
   206
    fun try_thm thm =
wenzelm@30143
   207
      if Thm.no_prems thm then rtac thm 1 goal
wenzelm@30318
   208
      else (etacn thm THEN_ALL_NEW (Goal.norm_hhf_tac THEN' Method.assm_tac ctxt)) 1 goal;
kleing@29857
   209
  in
wenzelm@30143
   210
    fn (_, thm) =>
wenzelm@32798
   211
      if is_some (Seq.pull (try_thm thm))
wenzelm@30143
   212
      then SOME (Thm.nprems_of thm, 0) else NONE
kleing@29857
   213
  end;
wenzelm@16033
   214
wenzelm@30142
   215
kleing@16074
   216
(* filter_simp *)
wenzelm@16033
   217
wenzelm@17205
   218
fun filter_simp ctxt t (_, thm) =
wenzelm@16033
   219
  let
wenzelm@32149
   220
    val mksimps = Simplifier.mksimps (simpset_of ctxt);
wenzelm@17106
   221
    val extract_simp =
wenzelm@30318
   222
      (map Thm.full_prop_of o mksimps, #1 o Logic.dest_equals o Logic.strip_imp_concl);
Timothy@31042
   223
    val ss = is_matching_thm false extract_simp ctxt false t thm;
wenzelm@17106
   224
  in
wenzelm@18939
   225
    if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
kleing@16964
   226
  end;
wenzelm@16033
   227
wenzelm@16033
   228
wenzelm@16033
   229
(* filter_pattern *)
wenzelm@16033
   230
wenzelm@32798
   231
fun get_names t = Term.add_const_names t (Term.add_free_names t []);
kleing@29857
   232
fun get_thm_names (_, thm) = get_names (Thm.full_prop_of thm);
kleing@28900
   233
wenzelm@30143
   234
(*Including all constants and frees is only sound because
wenzelm@30143
   235
  matching uses higher-order patterns. If full matching
wenzelm@30143
   236
  were used, then constants that may be subject to
wenzelm@30143
   237
  beta-reduction after substitution of frees should
wenzelm@30143
   238
  not be included for LHS set because they could be
wenzelm@30143
   239
  thrown away by the substituted function.
wenzelm@30143
   240
  e.g. for (?F 1 2) do not include 1 or 2, if it were
wenzelm@30143
   241
       possible for ?F to be (% x y. 3)
wenzelm@30143
   242
  The largest possible set should always be included on
wenzelm@30143
   243
  the RHS.*)
wenzelm@30143
   244
wenzelm@30143
   245
fun filter_pattern ctxt pat =
wenzelm@30143
   246
  let
kleing@29857
   247
    val pat_consts = get_names pat;
kleing@28900
   248
kleing@29857
   249
    fun check (t, NONE) = check (t, SOME (get_thm_names t))
kleing@28900
   250
      | check ((_, thm), c as SOME thm_consts) =
wenzelm@32798
   251
         (if pat_consts subset_string thm_consts andalso
wenzelm@32798
   252
            Pattern.matches_subterm (ProofContext.theory_of ctxt) (pat, Thm.full_prop_of thm)
wenzelm@32798
   253
          then SOME (0, 0) else NONE, c);
kleing@28900
   254
  in check end;
wenzelm@16033
   255
wenzelm@30142
   256
wenzelm@16033
   257
(* interpret criteria as filters *)
wenzelm@16033
   258
wenzelm@16036
   259
local
wenzelm@16036
   260
wenzelm@16036
   261
fun err_no_goal c =
wenzelm@16036
   262
  error ("Current goal required for " ^ c ^ " search criterion");
wenzelm@16036
   263
kleing@29857
   264
val fix_goal = Thm.prop_of;
kleing@29857
   265
kleing@28900
   266
fun filter_crit _ _ (Name name) = apfst (filter_name name)
wenzelm@16036
   267
  | filter_crit _ NONE Intro = err_no_goal "intro"
wenzelm@16036
   268
  | filter_crit _ NONE Elim = err_no_goal "elim"
wenzelm@16036
   269
  | filter_crit _ NONE Dest = err_no_goal "dest"
kleing@29857
   270
  | filter_crit _ NONE Solves = err_no_goal "solves"
Timothy@31042
   271
  | filter_crit ctxt (SOME goal) Intro = apfst (filter_intro false ctxt (fix_goal goal))
Timothy@31042
   272
  | filter_crit ctxt (SOME goal) IntroIff = apfst (filter_intro true ctxt (fix_goal goal))
wenzelm@30143
   273
  | filter_crit ctxt (SOME goal) Elim = apfst (filter_elim ctxt (fix_goal goal))
wenzelm@30143
   274
  | filter_crit ctxt (SOME goal) Dest = apfst (filter_dest ctxt (fix_goal goal))
kleing@29857
   275
  | filter_crit ctxt (SOME goal) Solves = apfst (filter_solves ctxt goal)
kleing@28900
   276
  | filter_crit ctxt _ (Simp pat) = apfst (filter_simp ctxt pat)
kleing@16088
   277
  | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
wenzelm@16036
   278
wenzelm@19502
   279
fun opt_not x = if is_some x then NONE else SOME (0, 0);
kleing@16895
   280
wenzelm@17756
   281
fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y : int)
wenzelm@26283
   282
  | opt_add _ _ = NONE;
kleing@16895
   283
wenzelm@30143
   284
fun app_filters thm =
wenzelm@30143
   285
  let
kleing@28900
   286
    fun app (NONE, _, _) = NONE
wenzelm@32798
   287
      | app (SOME v, _, []) = SOME (v, thm)
wenzelm@30143
   288
      | app (r, consts, f :: fs) =
wenzelm@30143
   289
          let val (r', consts') = f (thm, consts)
wenzelm@30143
   290
          in app (opt_add r r', consts', fs) end;
kleing@28900
   291
  in app end;
kleing@28900
   292
wenzelm@31684
   293
wenzelm@16036
   294
in
wenzelm@16033
   295
wenzelm@16033
   296
fun filter_criterion ctxt opt_goal (b, c) =
kleing@28900
   297
  (if b then I else (apfst opt_not)) o filter_crit ctxt opt_goal c;
kleing@16895
   298
Timothy@30785
   299
fun sorted_filter filters thms =
kleing@16895
   300
  let
kleing@28900
   301
    fun eval_filters thm = app_filters thm (SOME (0, 0), NONE, filters);
wenzelm@16033
   302
kleing@16895
   303
    (*filters return: (number of assumptions, substitution size) option, so
kleing@16964
   304
      sort (desc. in both cases) according to number of assumptions first,
kleing@16895
   305
      then by the substitution size*)
wenzelm@17205
   306
    fun thm_ord (((p0, s0), _), ((p1, s1), _)) =
wenzelm@17205
   307
      prod_ord int_ord int_ord ((p1, s1), (p0, s0));
kleing@28900
   308
  in map_filter eval_filters thms |> sort thm_ord |> map #2 end;
wenzelm@16033
   309
wenzelm@30822
   310
fun lazy_filter filters =
wenzelm@30822
   311
  let
Timothy@30785
   312
    fun lazy_match thms = Seq.make (fn () => first_match thms)
Timothy@30785
   313
Timothy@30785
   314
    and first_match [] = NONE
wenzelm@30822
   315
      | first_match (thm :: thms) =
wenzelm@30822
   316
          (case app_filters thm (SOME (0, 0), NONE, filters) of
Timothy@30785
   317
            NONE => first_match thms
wenzelm@30822
   318
          | SOME (_, t) => SOME (t, lazy_match thms));
Timothy@30785
   319
  in lazy_match end;
wenzelm@30822
   320
wenzelm@16036
   321
end;
wenzelm@16036
   322
wenzelm@16033
   323
kleing@22414
   324
(* removing duplicates, preferring nicer names, roughly n log n *)
kleing@22340
   325
wenzelm@25226
   326
local
wenzelm@25226
   327
huffman@27486
   328
val index_ord = option_ord (K EQUAL);
wenzelm@25226
   329
val hidden_ord = bool_ord o pairself NameSpace.is_hidden;
wenzelm@30364
   330
val qual_ord = int_ord o pairself (length o Long_Name.explode);
wenzelm@25226
   331
val txt_ord = int_ord o pairself size;
wenzelm@25226
   332
huffman@27486
   333
fun nicer_name (x, i) (y, j) =
huffman@27486
   334
  (case hidden_ord (x, y) of EQUAL =>
huffman@27486
   335
    (case index_ord (i, j) of EQUAL =>
huffman@27486
   336
      (case qual_ord (x, y) of EQUAL => txt_ord (x, y) | ord => ord)
huffman@27486
   337
    | ord => ord)
wenzelm@25226
   338
  | ord => ord) <> GREATER;
wenzelm@25226
   339
Timothy@29848
   340
fun rem_cdups nicer xs =
wenzelm@26336
   341
  let
wenzelm@26336
   342
    fun rem_c rev_seen [] = rev rev_seen
wenzelm@26336
   343
      | rem_c rev_seen [x] = rem_c (x :: rev_seen) []
wenzelm@26336
   344
      | rem_c rev_seen ((x as ((n, t), _)) :: (y as ((n', t'), _)) :: xs) =
wenzelm@30822
   345
          if Thm.eq_thm_prop (t, t')
wenzelm@30822
   346
          then rem_c rev_seen ((if nicer n n' then x else y) :: xs)
wenzelm@30822
   347
          else rem_c (x :: rev_seen) (y :: xs)
wenzelm@26336
   348
  in rem_c [] xs end;
wenzelm@25226
   349
wenzelm@26336
   350
in
wenzelm@25226
   351
wenzelm@30143
   352
fun nicer_shortest ctxt =
wenzelm@30143
   353
  let
wenzelm@30216
   354
    (* FIXME global name space!? *)
wenzelm@30216
   355
    val space = Facts.space_of (PureThy.facts_of (ProofContext.theory_of ctxt));
Timothy@29848
   356
wenzelm@30216
   357
    val shorten =
wenzelm@30216
   358
      NameSpace.extern_flags {long_names = false, short_names = false, unique_names = false} space;
Timothy@29848
   359
Timothy@29848
   360
    fun nicer (Facts.Named ((x, _), i)) (Facts.Named ((y, _), j)) =
Timothy@29848
   361
          nicer_name (shorten x, i) (shorten y, j)
Timothy@29848
   362
      | nicer (Facts.Fact _) (Facts.Named _) = true
Timothy@29848
   363
      | nicer (Facts.Named _) (Facts.Fact _) = false;
Timothy@29848
   364
  in nicer end;
Timothy@29848
   365
Timothy@29848
   366
fun rem_thm_dups nicer xs =
wenzelm@26336
   367
  xs ~~ (1 upto length xs)
wenzelm@29269
   368
  |> sort (TermOrd.fast_term_ord o pairself (Thm.prop_of o #2 o #1))
Timothy@29848
   369
  |> rem_cdups nicer
wenzelm@26336
   370
  |> sort (int_ord o pairself #2)
wenzelm@26336
   371
  |> map #1;
kleing@22340
   372
wenzelm@26336
   373
end;
kleing@22340
   374
kleing@22340
   375
wenzelm@16033
   376
(* print_theorems *)
wenzelm@16033
   377
wenzelm@26283
   378
fun all_facts_of ctxt =
wenzelm@26336
   379
  maps Facts.selections
wenzelm@27173
   380
   (Facts.dest_static [] (PureThy.facts_of (ProofContext.theory_of ctxt)) @
wenzelm@27173
   381
    Facts.dest_static [] (ProofContext.facts_of ctxt));
wenzelm@17972
   382
wenzelm@32738
   383
val limit = Unsynchronized.ref 40;
wenzelm@25992
   384
Timothy@30785
   385
fun find_theorems ctxt opt_goal opt_limit rem_dups raw_criteria =
wenzelm@16033
   386
  let
wenzelm@30822
   387
    val assms =
wenzelm@30822
   388
      ProofContext.get_fact ctxt (Facts.named "local.assms")
wenzelm@30822
   389
        handle ERROR _ => [];
wenzelm@30822
   390
    val add_prems = Seq.hd o TRY (Method.insert_tac assms 1);
kleing@29857
   391
    val opt_goal' = Option.map add_prems opt_goal;
kleing@29857
   392
wenzelm@16036
   393
    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
kleing@29857
   394
    val filters = map (filter_criterion ctxt opt_goal') criteria;
wenzelm@16033
   395
Timothy@30785
   396
    fun find_all facts =
Timothy@30785
   397
      let
Timothy@30785
   398
        val raw_matches = sorted_filter filters facts;
Timothy@30785
   399
Timothy@30785
   400
        val matches =
Timothy@30785
   401
          if rem_dups
Timothy@30785
   402
          then rem_thm_dups (nicer_shortest ctxt) raw_matches
Timothy@30785
   403
          else raw_matches;
kleing@28900
   404
Timothy@30785
   405
        val len = length matches;
Timothy@30785
   406
        val lim = the_default (! limit) opt_limit;
Timothy@30785
   407
      in (SOME len, Library.drop (len - lim, matches)) end;
Timothy@30785
   408
Timothy@30785
   409
    val find =
Timothy@30785
   410
      if rem_dups orelse is_none opt_limit
Timothy@30785
   411
      then find_all
wenzelm@30822
   412
      else pair NONE o Seq.list_of o Seq.take (the opt_limit) o lazy_filter filters;
Timothy@30785
   413
Timothy@30785
   414
  in find (all_facts_of ctxt) end;
kleing@29857
   415
wenzelm@30186
   416
wenzelm@30186
   417
fun pretty_thm ctxt (thmref, thm) = Pretty.block
wenzelm@30186
   418
  [Pretty.str (Facts.string_of_ref thmref), Pretty.str ":", Pretty.brk 1,
wenzelm@32091
   419
    Display.pretty_thm ctxt thm];
wenzelm@30186
   420
wenzelm@30143
   421
fun print_theorems ctxt opt_goal opt_limit rem_dups raw_criteria =
wenzelm@30143
   422
  let
kleing@29857
   423
    val start = start_timing ();
kleing@29857
   424
kleing@29857
   425
    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
Timothy@30785
   426
    val (foundo, thms) = find_theorems ctxt opt_goal opt_limit rem_dups raw_criteria;
Timothy@30785
   427
    val returned = length thms;
wenzelm@31684
   428
Timothy@30785
   429
    val tally_msg =
wenzelm@30822
   430
      (case foundo of
Timothy@30785
   431
        NONE => "displaying " ^ string_of_int returned ^ " theorems"
wenzelm@30822
   432
      | SOME found =>
wenzelm@30822
   433
          "found " ^ string_of_int found ^ " theorems" ^
wenzelm@30822
   434
            (if returned < found
wenzelm@30822
   435
             then " (" ^ string_of_int returned ^ " displayed)"
wenzelm@30822
   436
             else ""));
wenzelm@16033
   437
wenzelm@31687
   438
    val end_msg = " in " ^ Time.toString (#cpu (end_timing start)) ^ " secs";
wenzelm@16033
   439
  in
kleing@28900
   440
    Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria)
kleing@29857
   441
        :: Pretty.str "" ::
kleing@28900
   442
     (if null thms then [Pretty.str ("nothing found" ^ end_msg)]
wenzelm@16033
   443
      else
Timothy@30785
   444
        [Pretty.str (tally_msg ^ end_msg ^ ":"), Pretty.str ""] @
wenzelm@30186
   445
        map (pretty_thm ctxt) thms)
wenzelm@16033
   446
    |> Pretty.chunks |> Pretty.writeln
wenzelm@30142
   447
  end;
wenzelm@30142
   448
wenzelm@30142
   449
wenzelm@32798
   450
wenzelm@30142
   451
(** command syntax **)
wenzelm@30142
   452
wenzelm@30142
   453
fun find_theorems_cmd ((opt_lim, rem_dups), spec) =
wenzelm@30142
   454
  Toplevel.unknown_theory o Toplevel.keep (fn state =>
wenzelm@30822
   455
    let
wenzelm@30822
   456
      val proof_state = Toplevel.enter_proof_body state;
wenzelm@30822
   457
      val ctxt = Proof.context_of proof_state;
wenzelm@32859
   458
      val opt_goal = try Proof.flat_goal proof_state |> Option.map #2;
wenzelm@30822
   459
    in print_theorems ctxt opt_goal opt_lim rem_dups spec end);
wenzelm@30142
   460
wenzelm@30142
   461
local
wenzelm@30142
   462
wenzelm@30142
   463
structure P = OuterParse and K = OuterKeyword;
wenzelm@30142
   464
wenzelm@30142
   465
val criterion =
wenzelm@30142
   466
  P.reserved "name" |-- P.!!! (P.$$$ ":" |-- P.xname) >> Name ||
wenzelm@30142
   467
  P.reserved "intro" >> K Intro ||
Timothy@31042
   468
  P.reserved "introiff" >> K IntroIff ||
wenzelm@30142
   469
  P.reserved "elim" >> K Elim ||
wenzelm@30142
   470
  P.reserved "dest" >> K Dest ||
wenzelm@30142
   471
  P.reserved "solves" >> K Solves ||
wenzelm@30142
   472
  P.reserved "simp" |-- P.!!! (P.$$$ ":" |-- P.term) >> Simp ||
wenzelm@30142
   473
  P.term >> Pattern;
wenzelm@30142
   474
wenzelm@30142
   475
val options =
wenzelm@30142
   476
  Scan.optional
wenzelm@30142
   477
    (P.$$$ "(" |--
wenzelm@30142
   478
      P.!!! (Scan.option P.nat -- Scan.optional (P.reserved "with_dups" >> K false) true
wenzelm@30142
   479
        --| P.$$$ ")")) (NONE, true);
wenzelm@30142
   480
in
wenzelm@30142
   481
wenzelm@30142
   482
val _ =
wenzelm@30142
   483
  OuterSyntax.improper_command "find_theorems" "print theorems meeting specified criteria" K.diag
wenzelm@30142
   484
    (options -- Scan.repeat (((Scan.option P.minus >> is_none) -- criterion))
wenzelm@30142
   485
      >> (Toplevel.no_timing oo find_theorems_cmd));
wenzelm@16033
   486
wenzelm@16033
   487
end;
wenzelm@30142
   488
wenzelm@30142
   489
end;