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