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