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