src/Pure/Tools/find_theorems.ML
 author wenzelm Thu Aug 08 23:52:35 2013 +0200 (2013-08-08) changeset 52926 6415d95bf7a2 parent 52925 71e938856a03 child 52927 9c6aef15a7ad permissions -rw-r--r--
removed unused YXML_Find_Theorems and Legacy_XML_Syntax;
```     1 (*  Title:      Pure/Tools/find_theorems.ML
```
```     2     Author:     Rafal Kolanski and Gerwin Klein, NICTA
```
```     3     Author:     Lars Noschinski and Alexander Krauss, TU Muenchen
```
```     4
```
```     5 Retrieve theorems from proof context.
```
```     6 *)
```
```     7
```
```     8 signature FIND_THEOREMS =
```
```     9 sig
```
```    10   datatype 'term criterion =
```
```    11     Name of string | Intro | Elim | Dest | Solves | Simp of 'term | Pattern of 'term
```
```    12
```
```    13   datatype theorem =
```
```    14     Internal of Facts.ref * thm | External of Facts.ref * term
```
```    15
```
```    16   type 'term query = {
```
```    17     goal: thm option,
```
```    18     limit: int option,
```
```    19     rem_dups: bool,
```
```    20     criteria: (bool * 'term criterion) list
```
```    21   }
```
```    22
```
```    23   val read_criterion: Proof.context -> string criterion -> term criterion
```
```    24   val read_query: Position.T -> string -> (bool * string criterion) list
```
```    25
```
```    26   val find_theorems: Proof.context -> thm option -> int option -> bool ->
```
```    27     (bool * term criterion) list -> int option * (Facts.ref * thm) list
```
```    28   val find_theorems_cmd: Proof.context -> thm option -> int option -> bool ->
```
```    29     (bool * string criterion) list -> int option * (Facts.ref * thm) list
```
```    30
```
```    31   val filter_theorems: Proof.context -> theorem list -> term query ->
```
```    32     int option * theorem list
```
```    33   val filter_theorems_cmd: Proof.context -> theorem list -> string query ->
```
```    34     int option * theorem list
```
```    35
```
```    36   val pretty_theorem: Proof.context -> theorem -> Pretty.T
```
```    37   val pretty_thm: Proof.context -> Facts.ref * thm -> Pretty.T
```
```    38 end;
```
```    39
```
```    40 structure Find_Theorems: FIND_THEOREMS =
```
```    41 struct
```
```    42
```
```    43 (** search criteria **)
```
```    44
```
```    45 datatype 'term criterion =
```
```    46   Name of string | Intro | Elim | Dest | Solves | Simp of 'term | Pattern of 'term;
```
```    47
```
```    48 fun apply_dummies tm =
```
```    49   let
```
```    50     val (xs, _) = Term.strip_abs tm;
```
```    51     val tm' = Term.betapplys (tm, map (Term.dummy_pattern o #2) xs);
```
```    52   in #1 (Term.replace_dummy_patterns tm' 1) end;
```
```    53
```
```    54 fun parse_pattern ctxt nm =
```
```    55   let
```
```    56     val consts = Proof_Context.consts_of ctxt;
```
```    57     val nm' =
```
```    58       (case Syntax.parse_term ctxt nm of
```
```    59         Const (c, _) => c
```
```    60       | _ => Consts.intern consts nm);
```
```    61   in
```
```    62     (case try (Consts.the_abbreviation consts) nm' of
```
```    63       SOME (_, rhs) => apply_dummies (Proof_Context.expand_abbrevs ctxt rhs)
```
```    64     | NONE => Proof_Context.read_term_pattern ctxt nm)
```
```    65   end;
```
```    66
```
```    67 fun read_criterion _ (Name name) = Name name
```
```    68   | read_criterion _ Intro = Intro
```
```    69   | read_criterion _ Elim = Elim
```
```    70   | read_criterion _ Dest = Dest
```
```    71   | read_criterion _ Solves = Solves
```
```    72   | read_criterion ctxt (Simp str) = Simp (Proof_Context.read_term_pattern ctxt str)
```
```    73   | read_criterion ctxt (Pattern str) = Pattern (parse_pattern ctxt str);
```
```    74
```
```    75 fun pretty_criterion ctxt (b, c) =
```
```    76   let
```
```    77     fun prfx s = if b then s else "-" ^ s;
```
```    78   in
```
```    79     (case c of
```
```    80       Name name => Pretty.str (prfx "name: " ^ quote name)
```
```    81     | Intro => Pretty.str (prfx "intro")
```
```    82     | Elim => Pretty.str (prfx "elim")
```
```    83     | Dest => Pretty.str (prfx "dest")
```
```    84     | Solves => Pretty.str (prfx "solves")
```
```    85     | Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1,
```
```    86         Pretty.quote (Syntax.pretty_term ctxt (Term.show_dummy_patterns pat))]
```
```    87     | Pattern pat => Pretty.enclose (prfx " \"") "\""
```
```    88         [Syntax.pretty_term ctxt (Term.show_dummy_patterns pat)])
```
```    89   end;
```
```    90
```
```    91
```
```    92
```
```    93 (** queries **)
```
```    94
```
```    95 type 'term query = {
```
```    96   goal: thm option,
```
```    97   limit: int option,
```
```    98   rem_dups: bool,
```
```    99   criteria: (bool * 'term criterion) list
```
```   100 };
```
```   101
```
```   102 fun map_criteria f {goal, limit, rem_dups, criteria} =
```
```   103   {goal = goal, limit = limit, rem_dups = rem_dups, criteria = f criteria};
```
```   104
```
```   105
```
```   106
```
```   107 (** theorems, either internal or external (without proof) **)
```
```   108
```
```   109 datatype theorem =
```
```   110   Internal of Facts.ref * thm |
```
```   111   External of Facts.ref * term; (* FIXME: Facts.ref not appropriate *)
```
```   112
```
```   113 fun fact_ref_markup (Facts.Named ((name, pos), SOME [Facts.Single i])) =
```
```   114       Position.markup pos o Markup.properties [("name", name), ("index", Markup.print_int i)]
```
```   115   | fact_ref_markup (Facts.Named ((name, pos), NONE)) =
```
```   116       Position.markup pos o Markup.properties [("name", name)]
```
```   117   | fact_ref_markup fact_ref = raise Fail "bad fact ref";
```
```   118
```
```   119 fun prop_of (Internal (_, thm)) = Thm.full_prop_of thm
```
```   120   | prop_of (External (_, prop)) = prop;
```
```   121
```
```   122 fun nprems_of (Internal (_, thm)) = Thm.nprems_of thm
```
```   123   | nprems_of (External (_, prop)) = Logic.count_prems prop;
```
```   124
```
```   125 fun major_prem_of (Internal (_, thm)) = Thm.major_prem_of thm
```
```   126   | major_prem_of (External (_, prop)) =
```
```   127       Logic.strip_assums_concl (hd (Logic.strip_imp_prems prop));
```
```   128
```
```   129 fun fact_ref_of (Internal (fact_ref, _)) = fact_ref
```
```   130   | fact_ref_of (External (fact_ref, _)) = fact_ref;
```
```   131
```
```   132
```
```   133
```
```   134 (** search criterion filters **)
```
```   135
```
```   136 (*generated filters are to be of the form
```
```   137   input: theorem
```
```   138   output: (p:int, s:int) option, where
```
```   139     NONE indicates no match
```
```   140     p is the primary sorting criterion
```
```   141       (eg. number of assumptions in the theorem)
```
```   142     s is the secondary sorting criterion
```
```   143       (eg. size of the substitution for intro, elim and dest)
```
```   144   when applying a set of filters to a thm, fold results in:
```
```   145     (biggest p, sum of all s)
```
```   146   currently p and s only matter for intro, elim, dest and simp filters,
```
```   147   otherwise the default ordering is used.
```
```   148 *)
```
```   149
```
```   150
```
```   151 (* matching theorems *)
```
```   152
```
```   153 fun is_nontrivial thy = Term.is_Const o Term.head_of o Object_Logic.drop_judgment thy;
```
```   154
```
```   155 (*extract terms from term_src, refine them to the parts that concern us,
```
```   156   if po try match them against obj else vice versa.
```
```   157   trivial matches are ignored.
```
```   158   returns: smallest substitution size*)
```
```   159 fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src =
```
```   160   let
```
```   161     val thy = Proof_Context.theory_of ctxt;
```
```   162
```
```   163     fun matches pat =
```
```   164       is_nontrivial thy pat andalso
```
```   165       Pattern.matches thy (if po then (pat, obj) else (obj, pat));
```
```   166
```
```   167     fun substsize pat =
```
```   168       let val (_, subst) =
```
```   169         Pattern.match thy (if po then (pat, obj) else (obj, pat)) (Vartab.empty, Vartab.empty)
```
```   170       in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end;
```
```   171
```
```   172     fun bestmatch [] = NONE
```
```   173       | bestmatch xs = SOME (foldl1 Int.min xs);
```
```   174
```
```   175     val match_thm = matches o refine_term;
```
```   176   in
```
```   177     map (substsize o refine_term) (filter match_thm (extract_terms term_src))
```
```   178     |> bestmatch
```
```   179   end;
```
```   180
```
```   181
```
```   182 (* filter_name *)
```
```   183
```
```   184 fun filter_name str_pat theorem =
```
```   185   if match_string str_pat (Facts.name_of_ref (fact_ref_of theorem))
```
```   186   then SOME (0, 0) else NONE;
```
```   187
```
```   188
```
```   189 (* filter intro/elim/dest/solves rules *)
```
```   190
```
```   191 fun filter_dest ctxt goal theorem =
```
```   192   let
```
```   193     val extract_dest =
```
```   194      (fn theorem => if nprems_of theorem = 0 then [] else [prop_of theorem],
```
```   195       hd o Logic.strip_imp_prems);
```
```   196     val prems = Logic.prems_of_goal goal 1;
```
```   197
```
```   198     fun try_subst prem = is_matching_thm extract_dest ctxt true prem theorem;
```
```   199     val successful = prems |> map_filter try_subst;
```
```   200   in
```
```   201     (*if possible, keep best substitution (one with smallest size)*)
```
```   202     (*dest rules always have assumptions, so a dest with one
```
```   203       assumption is as good as an intro rule with none*)
```
```   204     if not (null successful)
```
```   205     then SOME (nprems_of theorem - 1, foldl1 Int.min successful) else NONE
```
```   206   end;
```
```   207
```
```   208 fun filter_intro ctxt goal theorem =
```
```   209   let
```
```   210     val extract_intro = (single o prop_of, Logic.strip_imp_concl);
```
```   211     val concl = Logic.concl_of_goal goal 1;
```
```   212     val ss = is_matching_thm extract_intro ctxt true concl theorem;
```
```   213   in
```
```   214     if is_some ss then SOME (nprems_of theorem, the ss) else NONE
```
```   215   end;
```
```   216
```
```   217 fun filter_elim ctxt goal theorem =
```
```   218   if nprems_of theorem > 0 then
```
```   219     let
```
```   220       val rule = prop_of theorem;
```
```   221       val prems = Logic.prems_of_goal goal 1;
```
```   222       val goal_concl = Logic.concl_of_goal goal 1;
```
```   223       val rule_mp = hd (Logic.strip_imp_prems rule);
```
```   224       val rule_concl = Logic.strip_imp_concl rule;
```
```   225       fun combine t1 t2 = Const ("*combine*", dummyT --> dummyT) \$ (t1 \$ t2);
```
```   226       val rule_tree = combine rule_mp rule_concl;
```
```   227       fun goal_tree prem = combine prem goal_concl;
```
```   228       fun try_subst prem = is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree;
```
```   229       val successful = prems |> map_filter try_subst;
```
```   230     in
```
```   231       (*elim rules always have assumptions, so an elim with one
```
```   232         assumption is as good as an intro rule with none*)
```
```   233       if is_nontrivial (Proof_Context.theory_of ctxt) (major_prem_of theorem)
```
```   234         andalso not (null successful)
```
```   235       then SOME (nprems_of theorem - 1, foldl1 Int.min successful) else NONE
```
```   236     end
```
```   237   else NONE;
```
```   238
```
```   239 fun filter_solves ctxt goal =
```
```   240   let
```
```   241     val thy' =
```
```   242       Proof_Context.theory_of ctxt
```
```   243       |> Context_Position.set_visible_global (Context_Position.is_visible ctxt);
```
```   244     val ctxt' = Proof_Context.transfer thy' ctxt;
```
```   245     val goal' = Thm.transfer thy' goal;
```
```   246
```
```   247     fun etacn thm i =
```
```   248       Seq.take (Options.default_int @{option find_theorems_tac_limit}) o etac thm i;
```
```   249     fun try_thm thm =
```
```   250       if Thm.no_prems thm then rtac thm 1 goal'
```
```   251       else (etacn thm THEN_ALL_NEW (Goal.norm_hhf_tac THEN' Method.assm_tac ctxt')) 1 goal';
```
```   252   in
```
```   253     fn Internal (_, thm) =>
```
```   254         if is_some (Seq.pull (try_thm thm))
```
```   255         then SOME (Thm.nprems_of thm, 0) else NONE
```
```   256      | External _ => NONE
```
```   257   end;
```
```   258
```
```   259
```
```   260 (* filter_simp *)
```
```   261
```
```   262 fun filter_simp ctxt t (Internal (_, thm)) =
```
```   263       let
```
```   264         val mksimps = Simplifier.mksimps ctxt;
```
```   265         val extract_simp =
```
```   266           (map Thm.full_prop_of o mksimps, #1 o Logic.dest_equals o Logic.strip_imp_concl);
```
```   267         val ss = is_matching_thm extract_simp ctxt false t thm;
```
```   268       in
```
```   269         if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
```
```   270       end
```
```   271   | filter_simp _ _ (External _) = NONE;
```
```   272
```
```   273
```
```   274 (* filter_pattern *)
```
```   275
```
```   276 fun get_names t = Term.add_const_names t (Term.add_free_names t []);
```
```   277
```
```   278 (*Including all constants and frees is only sound because
```
```   279   matching uses higher-order patterns. If full matching
```
```   280   were used, then constants that may be subject to
```
```   281   beta-reduction after substitution of frees should
```
```   282   not be included for LHS set because they could be
```
```   283   thrown away by the substituted function.
```
```   284   e.g. for (?F 1 2) do not include 1 or 2, if it were
```
```   285        possible for ?F to be (% x y. 3)
```
```   286   The largest possible set should always be included on
```
```   287   the RHS.*)
```
```   288
```
```   289 fun filter_pattern ctxt pat =
```
```   290   let
```
```   291     val pat_consts = get_names pat;
```
```   292
```
```   293     fun check (theorem, NONE) = check (theorem, SOME (get_names (prop_of theorem)))
```
```   294       | check (theorem, c as SOME thm_consts) =
```
```   295          (if subset (op =) (pat_consts, thm_consts) andalso
```
```   296             Pattern.matches_subterm (Proof_Context.theory_of ctxt) (pat, prop_of theorem)
```
```   297           then SOME (0, 0) else NONE, c);
```
```   298   in check end;
```
```   299
```
```   300
```
```   301 (* interpret criteria as filters *)
```
```   302
```
```   303 local
```
```   304
```
```   305 fun err_no_goal c =
```
```   306   error ("Current goal required for " ^ c ^ " search criterion");
```
```   307
```
```   308 val fix_goal = Thm.prop_of;
```
```   309
```
```   310 fun filter_crit _ _ (Name name) = apfst (filter_name name)
```
```   311   | filter_crit _ NONE Intro = err_no_goal "intro"
```
```   312   | filter_crit _ NONE Elim = err_no_goal "elim"
```
```   313   | filter_crit _ NONE Dest = err_no_goal "dest"
```
```   314   | filter_crit _ NONE Solves = err_no_goal "solves"
```
```   315   | filter_crit ctxt (SOME goal) Intro = apfst (filter_intro ctxt (fix_goal goal))
```
```   316   | filter_crit ctxt (SOME goal) Elim = apfst (filter_elim ctxt (fix_goal goal))
```
```   317   | filter_crit ctxt (SOME goal) Dest = apfst (filter_dest ctxt (fix_goal goal))
```
```   318   | filter_crit ctxt (SOME goal) Solves = apfst (filter_solves ctxt goal)
```
```   319   | filter_crit ctxt _ (Simp pat) = apfst (filter_simp ctxt pat)
```
```   320   | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
```
```   321
```
```   322 fun opt_not x = if is_some x then NONE else SOME (0, 0);
```
```   323
```
```   324 fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y : int)
```
```   325   | opt_add _ _ = NONE;
```
```   326
```
```   327 fun app_filters thm =
```
```   328   let
```
```   329     fun app (NONE, _, _) = NONE
```
```   330       | app (SOME v, _, []) = SOME (v, thm)
```
```   331       | app (r, consts, f :: fs) =
```
```   332           let val (r', consts') = f (thm, consts)
```
```   333           in app (opt_add r r', consts', fs) end;
```
```   334   in app end;
```
```   335
```
```   336 in
```
```   337
```
```   338 fun filter_criterion ctxt opt_goal (b, c) =
```
```   339   (if b then I else (apfst opt_not)) o filter_crit ctxt opt_goal c;
```
```   340
```
```   341 fun sorted_filter filters theorems =
```
```   342   let
```
```   343     fun eval_filters theorem = app_filters theorem (SOME (0, 0), NONE, filters);
```
```   344
```
```   345     (*filters return: (number of assumptions, substitution size) option, so
```
```   346       sort (desc. in both cases) according to number of assumptions first,
```
```   347       then by the substitution size*)
```
```   348     fun result_ord (((p0, s0), _), ((p1, s1), _)) =
```
```   349       prod_ord int_ord int_ord ((p1, s1), (p0, s0));
```
```   350   in
```
```   351     grouped 100 Par_List.map eval_filters theorems
```
```   352     |> map_filter I |> sort result_ord |> map #2
```
```   353   end;
```
```   354
```
```   355 fun lazy_filter filters =
```
```   356   let
```
```   357     fun lazy_match thms = Seq.make (fn () => first_match thms)
```
```   358     and first_match [] = NONE
```
```   359       | first_match (thm :: thms) =
```
```   360           (case app_filters thm (SOME (0, 0), NONE, filters) of
```
```   361             NONE => first_match thms
```
```   362           | SOME (_, t) => SOME (t, lazy_match thms));
```
```   363   in lazy_match end;
```
```   364
```
```   365 end;
```
```   366
```
```   367
```
```   368 (* removing duplicates, preferring nicer names, roughly n log n *)
```
```   369
```
```   370 local
```
```   371
```
```   372 val index_ord = option_ord (K EQUAL);
```
```   373 val hidden_ord = bool_ord o pairself Name_Space.is_hidden;
```
```   374 val qual_ord = int_ord o pairself (length o Long_Name.explode);
```
```   375 val txt_ord = int_ord o pairself size;
```
```   376
```
```   377 fun nicer_name (x, i) (y, j) =
```
```   378   (case hidden_ord (x, y) of EQUAL =>
```
```   379     (case index_ord (i, j) of EQUAL =>
```
```   380       (case qual_ord (x, y) of EQUAL => txt_ord (x, y) | ord => ord)
```
```   381     | ord => ord)
```
```   382   | ord => ord) <> GREATER;
```
```   383
```
```   384 fun rem_cdups nicer xs =
```
```   385   let
```
```   386     fun rem_c rev_seen [] = rev rev_seen
```
```   387       | rem_c rev_seen [x] = rem_c (x :: rev_seen) []
```
```   388       | rem_c rev_seen ((x as (t, _)) :: (y as (t', _)) :: xs) =
```
```   389           if (prop_of t) aconv (prop_of t')
```
```   390           then rem_c rev_seen ((if nicer (fact_ref_of t) (fact_ref_of t') then x else y) :: xs)
```
```   391           else rem_c (x :: rev_seen) (y :: xs)
```
```   392   in rem_c [] xs end;
```
```   393
```
```   394 in
```
```   395
```
```   396 fun nicer_shortest ctxt =
```
```   397   let
```
```   398     (* FIXME Why global name space!?? *)
```
```   399     val space = Facts.space_of (Global_Theory.facts_of (Proof_Context.theory_of ctxt));
```
```   400
```
```   401     val shorten =
```
```   402       Name_Space.extern
```
```   403         (ctxt
```
```   404           |> Config.put Name_Space.names_long false
```
```   405           |> Config.put Name_Space.names_short false
```
```   406           |> Config.put Name_Space.names_unique false) space;
```
```   407
```
```   408     fun nicer (Facts.Named ((x, _), i)) (Facts.Named ((y, _), j)) =
```
```   409           nicer_name (shorten x, i) (shorten y, j)
```
```   410       | nicer (Facts.Fact _) (Facts.Named _) = true
```
```   411       | nicer (Facts.Named _) (Facts.Fact _) = false;
```
```   412   in nicer end;
```
```   413
```
```   414 fun rem_thm_dups nicer xs =
```
```   415   xs ~~ (1 upto length xs)
```
```   416   |> sort (Term_Ord.fast_term_ord o pairself (prop_of o #1))
```
```   417   |> rem_cdups nicer
```
```   418   |> sort (int_ord o pairself #2)
```
```   419   |> map #1;
```
```   420
```
```   421 end;
```
```   422
```
```   423
```
```   424 (* pretty_theorems *)
```
```   425
```
```   426 fun all_facts_of ctxt =
```
```   427   let
```
```   428     fun visible_facts facts =
```
```   429       Facts.dest_static [] facts
```
```   430       |> filter_out (Facts.is_concealed facts o #1);
```
```   431   in
```
```   432     maps Facts.selections
```
```   433      (visible_facts (Global_Theory.facts_of (Proof_Context.theory_of ctxt)) @
```
```   434       visible_facts (Proof_Context.facts_of ctxt))
```
```   435   end;
```
```   436
```
```   437 fun filter_theorems ctxt theorems query =
```
```   438   let
```
```   439     val {goal = opt_goal, limit = opt_limit, rem_dups, criteria} = query;
```
```   440     val filters = map (filter_criterion ctxt opt_goal) criteria;
```
```   441
```
```   442     fun find_all theorems =
```
```   443       let
```
```   444         val raw_matches = sorted_filter filters theorems;
```
```   445
```
```   446         val matches =
```
```   447           if rem_dups
```
```   448           then rem_thm_dups (nicer_shortest ctxt) raw_matches
```
```   449           else raw_matches;
```
```   450
```
```   451         val len = length matches;
```
```   452         val lim = the_default (Options.default_int @{option find_theorems_limit}) opt_limit;
```
```   453       in (SOME len, drop (Int.max (len - lim, 0)) matches) end;
```
```   454
```
```   455     val find =
```
```   456       if rem_dups orelse is_none opt_limit
```
```   457       then find_all
```
```   458       else pair NONE o Seq.list_of o Seq.take (the opt_limit) o lazy_filter filters;
```
```   459
```
```   460   in find theorems end;
```
```   461
```
```   462 fun filter_theorems_cmd ctxt theorems raw_query =
```
```   463   filter_theorems ctxt theorems (map_criteria
```
```   464     (map (apsnd (read_criterion ctxt))) raw_query);
```
```   465
```
```   466 fun gen_find_theorems filter ctxt opt_goal opt_limit rem_dups raw_criteria =
```
```   467   let
```
```   468     val assms =
```
```   469       Proof_Context.get_fact ctxt (Facts.named "local.assms")
```
```   470         handle ERROR _ => [];
```
```   471     val add_prems = Seq.hd o TRY (Method.insert_tac assms 1);
```
```   472     val opt_goal' = Option.map add_prems opt_goal;
```
```   473   in
```
```   474     filter ctxt (map Internal (all_facts_of ctxt))
```
```   475       {goal = opt_goal', limit = opt_limit, rem_dups = rem_dups, criteria = raw_criteria}
```
```   476     |> apsnd (map (fn Internal f => f))
```
```   477   end;
```
```   478
```
```   479 val find_theorems = gen_find_theorems filter_theorems;
```
```   480 val find_theorems_cmd = gen_find_theorems filter_theorems_cmd;
```
```   481
```
```   482 fun pretty_ref ctxt thmref =
```
```   483   let
```
```   484     val (name, sel) =
```
```   485       (case thmref of
```
```   486         Facts.Named ((name, _), sel) => (name, sel)
```
```   487       | Facts.Fact _ => raise Fail "Illegal literal fact");
```
```   488   in
```
```   489     [Pretty.mark (Proof_Context.markup_fact ctxt name) (Pretty.str name),
```
```   490       Pretty.str (Facts.string_of_selection sel), Pretty.str ":", Pretty.brk 1]
```
```   491   end;
```
```   492
```
```   493 fun pretty_theorem ctxt (Internal (thmref, thm)) =
```
```   494       Pretty.block (pretty_ref ctxt thmref @ [Display.pretty_thm ctxt thm])
```
```   495   | pretty_theorem ctxt (External (thmref, prop)) =
```
```   496       Pretty.block (pretty_ref ctxt thmref @ [Syntax.unparse_term ctxt prop]);
```
```   497
```
```   498 fun pretty_thm ctxt (thmref, thm) = pretty_theorem ctxt (Internal (thmref, thm));
```
```   499
```
```   500 fun pretty_theorems ctxt opt_goal opt_limit rem_dups raw_criteria =
```
```   501   let
```
```   502     val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
```
```   503     val (foundo, theorems) =
```
```   504       filter_theorems ctxt (map Internal (all_facts_of ctxt))
```
```   505         {goal = opt_goal, limit = opt_limit, rem_dups = rem_dups, criteria = criteria};
```
```   506     val returned = length theorems;
```
```   507
```
```   508     val tally_msg =
```
```   509       (case foundo of
```
```   510         NONE => "displaying " ^ string_of_int returned ^ " theorem(s)"
```
```   511       | SOME found =>
```
```   512           "found " ^ string_of_int found ^ " theorem(s)" ^
```
```   513             (if returned < found
```
```   514              then " (" ^ string_of_int returned ^ " displayed)"
```
```   515              else ""));
```
```   516   in
```
```   517     Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) ::
```
```   518     Pretty.str "" ::
```
```   519     (if null theorems then [Pretty.str "nothing found"]
```
```   520      else
```
```   521       [Pretty.str (tally_msg ^ ":"), Pretty.str ""] @
```
```   522         grouped 10 Par_List.map (pretty_theorem ctxt) theorems)
```
```   523   end |> Pretty.fbreaks |> curry Pretty.blk 0;
```
```   524
```
```   525 fun pretty_theorems_cmd state opt_lim rem_dups spec =
```
```   526   let
```
```   527     val ctxt = Toplevel.context_of state;
```
```   528     val opt_goal = try (Proof.simple_goal o Toplevel.proof_of) state |> Option.map #goal;
```
```   529   in pretty_theorems ctxt opt_goal opt_lim rem_dups spec end;
```
```   530
```
```   531
```
```   532
```
```   533 (** Isar command syntax **)
```
```   534
```
```   535 local
```
```   536
```
```   537 val criterion =
```
```   538   Parse.reserved "name" |-- Parse.!!! (Parse.\$\$\$ ":" |-- Parse.xname) >> Name ||
```
```   539   Parse.reserved "intro" >> K Intro ||
```
```   540   Parse.reserved "elim" >> K Elim ||
```
```   541   Parse.reserved "dest" >> K Dest ||
```
```   542   Parse.reserved "solves" >> K Solves ||
```
```   543   Parse.reserved "simp" |-- Parse.!!! (Parse.\$\$\$ ":" |-- Parse.term) >> Simp ||
```
```   544   Parse.term >> Pattern;
```
```   545
```
```   546 val options =
```
```   547   Scan.optional
```
```   548     (Parse.\$\$\$ "(" |--
```
```   549       Parse.!!! (Scan.option Parse.nat -- Scan.optional (Parse.reserved "with_dups" >> K false) true
```
```   550         --| Parse.\$\$\$ ")")) (NONE, true);
```
```   551
```
```   552 val query = Scan.repeat ((Scan.option Parse.minus >> is_none) -- criterion);
```
```   553
```
```   554 in
```
```   555
```
```   556 fun read_query pos str =
```
```   557   Outer_Syntax.scan pos str
```
```   558   |> filter Token.is_proper
```
```   559   |> Scan.error (Scan.finite Token.stopper (Parse.!!! (query --| Scan.ahead Parse.eof)))
```
```   560   |> #1;
```
```   561
```
```   562 val _ =
```
```   563   Outer_Syntax.improper_command @{command_spec "find_theorems"}
```
```   564     "find theorems meeting specified criteria"
```
```   565     (options -- query >> (fn ((opt_lim, rem_dups), spec) =>
```
```   566       Toplevel.keep (fn state =>
```
```   567         Pretty.writeln (pretty_theorems_cmd state opt_lim rem_dups spec))));
```
```   568
```
```   569 end;
```
```   570
```
```   571
```
```   572
```
```   573 (** PIDE query operation **)
```
```   574
```
```   575 val _ =
```
```   576   Query_Operation.register "find_theorems" (fn st => fn args =>
```
```   577     Pretty.string_of (pretty_theorems_cmd st NONE false (maps (read_query Position.none) args)));
```
```   578
```
```   579 end;
```