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