src/Pure/Tools/find_theorems.ML
author wenzelm
Fri Aug 09 15:14:59 2013 +0200 (2013-08-09)
changeset 52940 6fce81e92e7c
parent 52928 facb4f6dc391
child 52941 28407b5f1c72
permissions -rw-r--r--
tuned;
     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 subst_size 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 (subst_size 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 matching
   279   uses higher-order patterns. If full matching were used, then
   280   constants that may be subject to beta-reduction after substitution
   281   of frees should not be included for LHS set because they could be
   282   thrown away by the substituted function.  E.g. for (?F 1 2) do not
   283   include 1 or 2, if it were possible for ?F to be (%x y. 3).  The
   284   largest possible set should always be included on the RHS.*)
   285 
   286 fun filter_pattern ctxt pat =
   287   let
   288     val pat_consts = get_names pat;
   289 
   290     fun check (theorem, NONE) = check (theorem, SOME (get_names (prop_of theorem)))
   291       | check (theorem, c as SOME thm_consts) =
   292          (if subset (op =) (pat_consts, thm_consts) andalso
   293             Pattern.matches_subterm (Proof_Context.theory_of ctxt) (pat, prop_of theorem)
   294           then SOME (0, 0) else NONE, c);
   295   in check end;
   296 
   297 
   298 (* interpret criteria as filters *)
   299 
   300 local
   301 
   302 fun err_no_goal c =
   303   error ("Current goal required for " ^ c ^ " search criterion");
   304 
   305 fun filter_crit _ _ (Name name) = apfst (filter_name name)
   306   | filter_crit _ NONE Intro = err_no_goal "intro"
   307   | filter_crit _ NONE Elim = err_no_goal "elim"
   308   | filter_crit _ NONE Dest = err_no_goal "dest"
   309   | filter_crit _ NONE Solves = err_no_goal "solves"
   310   | filter_crit ctxt (SOME goal) Intro = apfst (filter_intro ctxt (Thm.prop_of goal))
   311   | filter_crit ctxt (SOME goal) Elim = apfst (filter_elim ctxt (Thm.prop_of goal))
   312   | filter_crit ctxt (SOME goal) Dest = apfst (filter_dest ctxt (Thm.prop_of goal))
   313   | filter_crit ctxt (SOME goal) Solves = apfst (filter_solves ctxt goal)
   314   | filter_crit ctxt _ (Simp pat) = apfst (filter_simp ctxt pat)
   315   | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
   316 
   317 fun opt_not x = if is_some x then NONE else SOME (0, 0);
   318 
   319 fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y : int)
   320   | opt_add _ _ = NONE;
   321 
   322 fun app_filters thm =
   323   let
   324     fun app (NONE, _, _) = NONE
   325       | app (SOME v, _, []) = SOME (v, thm)
   326       | app (r, consts, f :: fs) =
   327           let val (r', consts') = f (thm, consts)
   328           in app (opt_add r r', consts', fs) end;
   329   in app end;
   330 
   331 in
   332 
   333 fun filter_criterion ctxt opt_goal (b, c) =
   334   (if b then I else (apfst opt_not)) o filter_crit ctxt opt_goal c;
   335 
   336 fun sorted_filter filters theorems =
   337   let
   338     fun eval_filters theorem = app_filters theorem (SOME (0, 0), NONE, filters);
   339 
   340     (*filters return: (number of assumptions, substitution size) option, so
   341       sort (desc. in both cases) according to number of assumptions first,
   342       then by the substitution size*)
   343     fun result_ord (((p0, s0), _), ((p1, s1), _)) =
   344       prod_ord int_ord int_ord ((p1, s1), (p0, s0));
   345   in
   346     grouped 100 Par_List.map eval_filters theorems
   347     |> map_filter I |> sort result_ord |> map #2
   348   end;
   349 
   350 fun lazy_filter filters =
   351   let
   352     fun lazy_match thms = Seq.make (fn () => first_match thms)
   353     and first_match [] = NONE
   354       | first_match (thm :: thms) =
   355           (case app_filters thm (SOME (0, 0), NONE, filters) of
   356             NONE => first_match thms
   357           | SOME (_, t) => SOME (t, lazy_match thms));
   358   in lazy_match end;
   359 
   360 end;
   361 
   362 
   363 (* removing duplicates, preferring nicer names, roughly O(n log n) *)
   364 
   365 local
   366 
   367 val index_ord = option_ord (K EQUAL);
   368 val hidden_ord = bool_ord o pairself Name_Space.is_hidden;
   369 val qual_ord = int_ord o pairself (length o Long_Name.explode);
   370 val txt_ord = int_ord o pairself size;
   371 
   372 fun nicer_name (x, i) (y, j) =
   373   (case hidden_ord (x, y) of EQUAL =>
   374     (case index_ord (i, j) of EQUAL =>
   375       (case qual_ord (x, y) of EQUAL => txt_ord (x, y) | ord => ord)
   376     | ord => ord)
   377   | ord => ord) <> GREATER;
   378 
   379 fun rem_cdups nicer xs =
   380   let
   381     fun rem_c rev_seen [] = rev rev_seen
   382       | rem_c rev_seen [x] = rem_c (x :: rev_seen) []
   383       | rem_c rev_seen ((x as (t, _)) :: (y as (t', _)) :: xs) =
   384           if (prop_of t) aconv (prop_of t')
   385           then rem_c rev_seen ((if nicer (fact_ref_of t) (fact_ref_of t') then x else y) :: xs)
   386           else rem_c (x :: rev_seen) (y :: xs)
   387   in rem_c [] xs end;
   388 
   389 in
   390 
   391 fun nicer_shortest ctxt =
   392   let
   393     (* FIXME Why global name space!?? *)
   394     val space = Facts.space_of (Global_Theory.facts_of (Proof_Context.theory_of ctxt));
   395 
   396     val shorten =
   397       Name_Space.extern
   398         (ctxt
   399           |> Config.put Name_Space.names_long false
   400           |> Config.put Name_Space.names_short false
   401           |> Config.put Name_Space.names_unique false) space;
   402 
   403     fun nicer (Facts.Named ((x, _), i)) (Facts.Named ((y, _), j)) =
   404           nicer_name (shorten x, i) (shorten y, j)
   405       | nicer (Facts.Fact _) (Facts.Named _) = true
   406       | nicer (Facts.Named _) (Facts.Fact _) = false;
   407   in nicer end;
   408 
   409 fun rem_thm_dups nicer xs =
   410   (xs ~~ (1 upto length xs))
   411   |> sort (Term_Ord.fast_term_ord o pairself (prop_of o #1))
   412   |> rem_cdups nicer
   413   |> sort (int_ord o pairself #2)
   414   |> map #1;
   415 
   416 end;
   417 
   418 
   419 (* pretty_theorems *)
   420 
   421 fun all_facts_of ctxt =
   422   let
   423     fun visible_facts facts =
   424       Facts.dest_static [] facts
   425       |> filter_out (Facts.is_concealed facts o #1);
   426   in
   427     maps Facts.selections
   428      (visible_facts (Global_Theory.facts_of (Proof_Context.theory_of ctxt)) @
   429       visible_facts (Proof_Context.facts_of ctxt))
   430   end;
   431 
   432 fun filter_theorems ctxt theorems query =
   433   let
   434     val {goal = opt_goal, limit = opt_limit, rem_dups, criteria} = query;
   435     val filters = map (filter_criterion ctxt opt_goal) criteria;
   436 
   437     fun find_all theorems =
   438       let
   439         val raw_matches = sorted_filter filters theorems;
   440 
   441         val matches =
   442           if rem_dups
   443           then rem_thm_dups (nicer_shortest ctxt) raw_matches
   444           else raw_matches;
   445 
   446         val len = length matches;
   447         val lim = the_default (Options.default_int @{option find_theorems_limit}) opt_limit;
   448       in (SOME len, drop (Int.max (len - lim, 0)) matches) end;
   449 
   450     val find =
   451       if rem_dups orelse is_none opt_limit
   452       then find_all
   453       else pair NONE o Seq.list_of o Seq.take (the opt_limit) o lazy_filter filters;
   454 
   455   in find theorems end;
   456 
   457 fun filter_theorems_cmd ctxt theorems raw_query =
   458   filter_theorems ctxt theorems (map_criteria
   459     (map (apsnd (read_criterion ctxt))) raw_query);
   460 
   461 fun gen_find_theorems filter ctxt opt_goal opt_limit rem_dups raw_criteria =
   462   let
   463     val assms =
   464       Proof_Context.get_fact ctxt (Facts.named "local.assms")
   465         handle ERROR _ => [];
   466     val add_prems = Seq.hd o TRY (Method.insert_tac assms 1);
   467     val opt_goal' = Option.map add_prems opt_goal;
   468   in
   469     filter ctxt (map Internal (all_facts_of ctxt))
   470       {goal = opt_goal', limit = opt_limit, rem_dups = rem_dups, criteria = raw_criteria}
   471     |> apsnd (map (fn Internal f => f))
   472   end;
   473 
   474 val find_theorems = gen_find_theorems filter_theorems;
   475 val find_theorems_cmd = gen_find_theorems filter_theorems_cmd;
   476 
   477 fun pretty_ref ctxt thmref =
   478   let
   479     val (name, sel) =
   480       (case thmref of
   481         Facts.Named ((name, _), sel) => (name, sel)
   482       | Facts.Fact _ => raise Fail "Illegal literal fact");
   483   in
   484     [Pretty.mark (Proof_Context.markup_fact ctxt name) (Pretty.str name),
   485       Pretty.str (Facts.string_of_selection sel), Pretty.str ":", Pretty.brk 1]
   486   end;
   487 
   488 fun pretty_theorem ctxt (Internal (thmref, thm)) =
   489       Pretty.block (pretty_ref ctxt thmref @ [Display.pretty_thm ctxt thm])
   490   | pretty_theorem ctxt (External (thmref, prop)) =
   491       Pretty.block (pretty_ref ctxt thmref @ [Syntax.unparse_term ctxt prop]);
   492 
   493 fun pretty_thm ctxt (thmref, thm) = pretty_theorem ctxt (Internal (thmref, thm));
   494 
   495 fun pretty_theorems ctxt opt_goal opt_limit rem_dups raw_criteria =
   496   let
   497     val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
   498     val (opt_found, theorems) =
   499       filter_theorems ctxt (map Internal (all_facts_of ctxt))
   500         {goal = opt_goal, limit = opt_limit, rem_dups = rem_dups, criteria = criteria};
   501     val returned = length theorems;
   502 
   503     val tally_msg =
   504       (case opt_found of
   505         NONE => "displaying " ^ string_of_int returned ^ " theorem(s)"
   506       | SOME found =>
   507           "found " ^ string_of_int found ^ " theorem(s)" ^
   508             (if returned < found
   509              then " (" ^ string_of_int returned ^ " displayed)"
   510              else ""));
   511   in
   512     Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) ::
   513     Pretty.str "" ::
   514     (if null theorems then [Pretty.str "nothing found"]
   515      else
   516       [Pretty.str (tally_msg ^ ":"), Pretty.str ""] @
   517         grouped 10 Par_List.map (Pretty.item o single o pretty_theorem ctxt) theorems)
   518   end |> Pretty.fbreaks |> curry Pretty.blk 0;
   519 
   520 fun pretty_theorems_cmd state opt_lim rem_dups spec =
   521   let
   522     val ctxt = Toplevel.context_of state;
   523     val opt_goal = try (Proof.simple_goal o Toplevel.proof_of) state |> Option.map #goal;
   524   in pretty_theorems ctxt opt_goal opt_lim rem_dups spec end;
   525 
   526 
   527 
   528 (** Isar command syntax **)
   529 
   530 local
   531 
   532 val criterion =
   533   Parse.reserved "name" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.xname) >> Name ||
   534   Parse.reserved "intro" >> K Intro ||
   535   Parse.reserved "elim" >> K Elim ||
   536   Parse.reserved "dest" >> K Dest ||
   537   Parse.reserved "solves" >> K Solves ||
   538   Parse.reserved "simp" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.term) >> Simp ||
   539   Parse.term >> Pattern;
   540 
   541 val options =
   542   Scan.optional
   543     (Parse.$$$ "(" |--
   544       Parse.!!! (Scan.option Parse.nat -- Scan.optional (Parse.reserved "with_dups" >> K false) true
   545         --| Parse.$$$ ")")) (NONE, true);
   546 
   547 val query = Scan.repeat ((Scan.option Parse.minus >> is_none) -- criterion);
   548 
   549 in
   550 
   551 fun read_query pos str =
   552   Outer_Syntax.scan pos str
   553   |> filter Token.is_proper
   554   |> Scan.error (Scan.finite Token.stopper (Parse.!!! (query --| Scan.ahead Parse.eof)))
   555   |> #1;
   556 
   557 val _ =
   558   Outer_Syntax.improper_command @{command_spec "find_theorems"}
   559     "find theorems meeting specified criteria"
   560     (options -- query >> (fn ((opt_lim, rem_dups), spec) =>
   561       Toplevel.keep (fn state =>
   562         Pretty.writeln (pretty_theorems_cmd state opt_lim rem_dups spec))));
   563 
   564 end;
   565 
   566 
   567 
   568 (** PIDE query operation **)
   569 
   570 val _ =
   571   Query_Operation.register "find_theorems" (fn st => fn args =>
   572     if can Toplevel.theory_of st then
   573       Pretty.string_of (pretty_theorems_cmd st NONE false (maps (read_query Position.none) args))
   574     else error "Unknown theory context");
   575 
   576 end;