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