src/Pure/Tools/find_theorems.ML
author wenzelm
Fri Feb 27 16:05:40 2009 +0100 (2009-02-27)
changeset 30143 98a986b02022
parent 30142 8d6145694bb5
child 30186 1f836e949ac2
permissions -rw-r--r--
observe basic Isabelle/ML coding conventions;
     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   val limit: int ref
    10   val tac_limit: int ref
    11 
    12   datatype 'term criterion =
    13     Name of string | Intro | Elim | Dest | Solves | Simp of 'term |
    14     Pattern of 'term
    15 
    16   val find_theorems: Proof.context -> thm option -> bool ->
    17     (bool * string criterion) list -> (Facts.ref * thm) list
    18 
    19   val print_theorems: Proof.context -> thm option -> int option -> bool ->
    20     (bool * string criterion) list -> unit
    21 end;
    22 
    23 structure FindTheorems: FIND_THEOREMS =
    24 struct
    25 
    26 (** search criteria **)
    27 
    28 datatype 'term criterion =
    29   Name of string | Intro | Elim | Dest | Solves | Simp of 'term |
    30   Pattern of 'term;
    31 
    32 fun read_criterion _ (Name name) = Name name
    33   | read_criterion _ Intro = Intro
    34   | read_criterion _ Elim = Elim
    35   | read_criterion _ Dest = Dest
    36   | read_criterion _ Solves = Solves
    37   | read_criterion ctxt (Simp str) = Simp (ProofContext.read_term_pattern ctxt str)
    38   | read_criterion ctxt (Pattern str) = Pattern (ProofContext.read_term_pattern ctxt str);
    39 
    40 fun pretty_criterion ctxt (b, c) =
    41   let
    42     fun prfx s = if b then s else "-" ^ s;
    43   in
    44     (case c of
    45       Name name => Pretty.str (prfx "name: " ^ quote name)
    46     | Intro => Pretty.str (prfx "intro")
    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 (*extract terms from term_src, refine them to the parts that concern us,
    80   if po try match them against obj else vice versa.
    81   trivial matches are ignored.
    82   returns: smallest substitution size*)
    83 fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src =
    84   let
    85     val thy = ProofContext.theory_of ctxt;
    86 
    87     fun matches pat =
    88       is_nontrivial thy pat andalso
    89       Pattern.matches thy (if po then (pat, obj) else (obj, pat));
    90 
    91     fun substsize pat =
    92       let val (_, subst) =
    93         Pattern.match thy (if po then (pat, obj) else (obj, pat)) (Vartab.empty, Vartab.empty)
    94       in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end;
    95 
    96     fun bestmatch [] = NONE
    97      |  bestmatch xs = SOME (foldr1 Int.min xs);
    98 
    99     val match_thm = matches o refine_term;
   100   in
   101     map (substsize o refine_term) (filter match_thm (extract_terms term_src))
   102     |> bestmatch
   103   end;
   104 
   105 
   106 (* filter_name *)
   107 
   108 fun filter_name str_pat (thmref, _) =
   109   if match_string str_pat (Facts.name_of_ref thmref)
   110   then SOME (0, 0) else NONE;
   111 
   112 
   113 (* filter intro/elim/dest/solves rules *)
   114 
   115 fun filter_dest ctxt goal (_, thm) =
   116   let
   117     val extract_dest =
   118      (fn thm => if Thm.no_prems thm then [] else [Thm.full_prop_of thm],
   119       hd o Logic.strip_imp_prems);
   120     val prems = Logic.prems_of_goal goal 1;
   121 
   122     fun try_subst prem = is_matching_thm extract_dest ctxt true prem thm;
   123     val successful = prems |> map_filter try_subst;
   124   in
   125     (*if possible, keep best substitution (one with smallest size)*)
   126     (*dest rules always have assumptions, so a dest with one
   127       assumption is as good as an intro rule with none*)
   128     if not (null successful)
   129     then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
   130   end;
   131 
   132 fun filter_intro ctxt goal (_, thm) =
   133   let
   134     val extract_intro = (single o Thm.full_prop_of, Logic.strip_imp_concl);
   135     val concl = Logic.concl_of_goal goal 1;
   136     val ss = is_matching_thm extract_intro ctxt true concl thm;
   137   in
   138     if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
   139   end;
   140 
   141 fun filter_elim ctxt goal (_, thm) =
   142   if not (Thm.no_prems thm) then
   143     let
   144       val rule = Thm.full_prop_of thm;
   145       val prems = Logic.prems_of_goal goal 1;
   146       val goal_concl = Logic.concl_of_goal goal 1;
   147       val rule_mp = hd (Logic.strip_imp_prems rule);
   148       val rule_concl = Logic.strip_imp_concl rule;
   149       fun combine t1 t2 = Const ("*combine*", dummyT --> dummyT) $ (t1 $ t2);
   150       val rule_tree = combine rule_mp rule_concl;
   151       fun goal_tree prem = combine prem goal_concl;
   152       fun try_subst prem =
   153         is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree;
   154       val successful = prems |> map_filter try_subst;
   155     in
   156     (*elim rules always have assumptions, so an elim with one
   157       assumption is as good as an intro rule with none*)
   158       if is_nontrivial (ProofContext.theory_of ctxt) (Thm.major_prem_of thm)
   159         andalso not (null successful)
   160       then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
   161     end
   162   else NONE
   163 
   164 val tac_limit = ref 5;
   165 
   166 fun filter_solves ctxt goal =
   167   let
   168     val baregoal = Logic.get_goal (Thm.prop_of goal) 1;
   169 
   170     fun etacn thm i = Seq.take (! tac_limit) o etac thm i;
   171     fun try_thm thm =
   172       if Thm.no_prems thm then rtac thm 1 goal
   173       else (etacn thm THEN_ALL_NEW
   174              (Goal.norm_hhf_tac THEN'
   175                Method.assumption_tac ctxt)) 1 goal;
   176   in
   177     fn (_, thm) =>
   178       if (is_some o Seq.pull o try_thm) thm
   179       then SOME (Thm.nprems_of thm, 0) else NONE
   180   end;
   181 
   182 
   183 (* filter_simp *)
   184 
   185 fun filter_simp ctxt t (_, thm) =
   186   let
   187     val (_, {mk_rews = {mk, ...}, ...}) =
   188       Simplifier.rep_ss (Simplifier.local_simpset_of ctxt);
   189     val extract_simp =
   190       (map Thm.full_prop_of o mk, #1 o Logic.dest_equals o Logic.strip_imp_concl);
   191     val ss = is_matching_thm extract_simp ctxt false t thm
   192   in
   193     if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
   194   end;
   195 
   196 
   197 (* filter_pattern *)
   198 
   199 fun get_names t = (Term.add_const_names t []) union (Term.add_free_names t []);
   200 fun get_thm_names (_, thm) = get_names (Thm.full_prop_of thm);
   201 
   202 (*Including all constants and frees is only sound because
   203   matching uses higher-order patterns. If full matching
   204   were used, then constants that may be subject to
   205   beta-reduction after substitution of frees should
   206   not be included for LHS set because they could be
   207   thrown away by the substituted function.
   208   e.g. for (?F 1 2) do not include 1 or 2, if it were
   209        possible for ?F to be (% x y. 3)
   210   The largest possible set should always be included on
   211   the RHS.*)
   212 
   213 fun filter_pattern ctxt pat =
   214   let
   215     val pat_consts = get_names pat;
   216 
   217     fun check (t, NONE) = check (t, SOME (get_thm_names t))
   218       | check ((_, thm), c as SOME thm_consts) =
   219           (if pat_consts subset_string thm_consts
   220               andalso (Pattern.matches_subterm (ProofContext.theory_of ctxt)
   221                                                (pat, Thm.full_prop_of thm))
   222            then SOME (0, 0) else NONE, c);
   223   in check end;
   224 
   225 
   226 (* interpret criteria as filters *)
   227 
   228 local
   229 
   230 fun err_no_goal c =
   231   error ("Current goal required for " ^ c ^ " search criterion");
   232 
   233 val fix_goal = Thm.prop_of;
   234 val fix_goalo = Option.map fix_goal;
   235 
   236 fun filter_crit _ _ (Name name) = apfst (filter_name name)
   237   | filter_crit _ NONE Intro = err_no_goal "intro"
   238   | filter_crit _ NONE Elim = err_no_goal "elim"
   239   | filter_crit _ NONE Dest = err_no_goal "dest"
   240   | filter_crit _ NONE Solves = err_no_goal "solves"
   241   | filter_crit ctxt (SOME goal) Intro = apfst (filter_intro ctxt (fix_goal goal))
   242   | filter_crit ctxt (SOME goal) Elim = apfst (filter_elim ctxt (fix_goal goal))
   243   | filter_crit ctxt (SOME goal) Dest = apfst (filter_dest ctxt (fix_goal goal))
   244   | filter_crit ctxt (SOME goal) Solves = apfst (filter_solves ctxt goal)
   245   | filter_crit ctxt _ (Simp pat) = apfst (filter_simp ctxt pat)
   246   | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
   247 
   248 fun opt_not x = if is_some x then NONE else SOME (0, 0);
   249 
   250 fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y : int)
   251   | opt_add _ _ = NONE;
   252 
   253 fun app_filters thm =
   254   let
   255     fun app (NONE, _, _) = NONE
   256       | app (SOME v, consts, []) = SOME (v, thm)
   257       | app (r, consts, f :: fs) =
   258           let val (r', consts') = f (thm, consts)
   259           in app (opt_add r r', consts', fs) end;
   260   in app end;
   261 
   262 in
   263 
   264 fun filter_criterion ctxt opt_goal (b, c) =
   265   (if b then I else (apfst opt_not)) o filter_crit ctxt opt_goal c;
   266 
   267 fun all_filters filters thms =
   268   let
   269     fun eval_filters thm = app_filters thm (SOME (0, 0), NONE, filters);
   270 
   271     (*filters return: (number of assumptions, substitution size) option, so
   272       sort (desc. in both cases) according to number of assumptions first,
   273       then by the substitution size*)
   274     fun thm_ord (((p0, s0), _), ((p1, s1), _)) =
   275       prod_ord int_ord int_ord ((p1, s1), (p0, s0));
   276   in map_filter eval_filters thms |> sort thm_ord |> map #2 end;
   277 
   278 end;
   279 
   280 
   281 (* removing duplicates, preferring nicer names, roughly n log n *)
   282 
   283 local
   284 
   285 val index_ord = option_ord (K EQUAL);
   286 val hidden_ord = bool_ord o pairself NameSpace.is_hidden;
   287 val qual_ord = int_ord o pairself (length o NameSpace.explode);
   288 val txt_ord = int_ord o pairself size;
   289 
   290 fun nicer_name (x, i) (y, j) =
   291   (case hidden_ord (x, y) of EQUAL =>
   292     (case index_ord (i, j) of EQUAL =>
   293       (case qual_ord (x, y) of EQUAL => txt_ord (x, y) | ord => ord)
   294     | ord => ord)
   295   | ord => ord) <> GREATER;
   296 
   297 fun rem_cdups nicer xs =
   298   let
   299     fun rem_c rev_seen [] = rev rev_seen
   300       | rem_c rev_seen [x] = rem_c (x :: rev_seen) []
   301       | rem_c rev_seen ((x as ((n, t), _)) :: (y as ((n', t'), _)) :: xs) =
   302         if Thm.eq_thm_prop (t, t')
   303         then rem_c rev_seen ((if nicer n n' then x else y) :: xs)
   304         else rem_c (x :: rev_seen) (y :: xs)
   305   in rem_c [] xs end;
   306 
   307 in
   308 
   309 fun nicer_shortest ctxt =
   310   let
   311     val ns = ProofContext.theory_of ctxt
   312              |> PureThy.facts_of
   313              |> Facts.space_of;
   314 
   315     val len_sort = sort (int_ord o (pairself size));
   316     fun shorten s = (case len_sort (NameSpace.get_accesses ns s) of
   317                        [] => s
   318                      | s'::_ => s');
   319 
   320     fun nicer (Facts.Named ((x, _), i)) (Facts.Named ((y, _), j)) =
   321           nicer_name (shorten x, i) (shorten y, j)
   322       | nicer (Facts.Fact _) (Facts.Named _) = true
   323       | nicer (Facts.Named _) (Facts.Fact _) = false;
   324   in nicer end;
   325 
   326 fun rem_thm_dups nicer xs =
   327   xs ~~ (1 upto length xs)
   328   |> sort (TermOrd.fast_term_ord o pairself (Thm.prop_of o #2 o #1))
   329   |> rem_cdups nicer
   330   |> sort (int_ord o pairself #2)
   331   |> map #1;
   332 
   333 end;
   334 
   335 
   336 (* print_theorems *)
   337 
   338 fun all_facts_of ctxt =
   339   maps Facts.selections
   340    (Facts.dest_static [] (PureThy.facts_of (ProofContext.theory_of ctxt)) @
   341     Facts.dest_static [] (ProofContext.facts_of ctxt));
   342 
   343 val limit = ref 40;
   344 
   345 fun find_theorems ctxt opt_goal rem_dups raw_criteria =
   346   let
   347     val add_prems = Seq.hd o (TRY (Method.insert_tac
   348                                      (Assumption.prems_of ctxt) 1));
   349     val opt_goal' = Option.map add_prems opt_goal;
   350 
   351     val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
   352     val filters = map (filter_criterion ctxt opt_goal') criteria;
   353 
   354     val raw_matches = all_filters filters (all_facts_of ctxt);
   355 
   356     val matches =
   357       if rem_dups
   358       then rem_thm_dups (nicer_shortest ctxt) raw_matches
   359       else raw_matches;
   360   in matches end;
   361 
   362 fun print_theorems ctxt opt_goal opt_limit rem_dups raw_criteria =
   363   let
   364     val start = start_timing ();
   365 
   366     val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
   367     val matches = find_theorems ctxt opt_goal rem_dups raw_criteria;
   368 
   369     val len = length matches;
   370     val lim = the_default (! limit) opt_limit;
   371     val thms = Library.drop (len - lim, matches);
   372 
   373     val end_msg = " in " ^
   374                   (List.nth (String.tokens Char.isSpace (end_timing start), 3))
   375                   ^ " secs"
   376   in
   377     Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria)
   378         :: Pretty.str "" ::
   379      (if null thms then [Pretty.str ("nothing found" ^ end_msg)]
   380       else
   381         [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
   382           (if len <= lim then ""
   383            else " (" ^ string_of_int lim ^ " displayed)")
   384            ^ end_msg ^ ":"), Pretty.str ""] @
   385         map Display.pretty_fact thms)
   386     |> Pretty.chunks |> Pretty.writeln
   387   end;
   388 
   389 
   390 
   391 (** command syntax **)
   392 
   393 fun find_theorems_cmd ((opt_lim, rem_dups), spec) =
   394   Toplevel.unknown_theory o Toplevel.keep (fn state =>
   395   let
   396     val proof_state = Toplevel.enter_proof_body state;
   397     val ctxt = Proof.context_of proof_state;
   398     val opt_goal = try Proof.get_goal proof_state |> Option.map (#2 o #2);
   399   in print_theorems ctxt opt_goal opt_lim rem_dups spec end);
   400 
   401 local
   402 
   403 structure P = OuterParse and K = OuterKeyword;
   404 
   405 val criterion =
   406   P.reserved "name" |-- P.!!! (P.$$$ ":" |-- P.xname) >> Name ||
   407   P.reserved "intro" >> K Intro ||
   408   P.reserved "elim" >> K Elim ||
   409   P.reserved "dest" >> K Dest ||
   410   P.reserved "solves" >> K Solves ||
   411   P.reserved "simp" |-- P.!!! (P.$$$ ":" |-- P.term) >> Simp ||
   412   P.term >> Pattern;
   413 
   414 val options =
   415   Scan.optional
   416     (P.$$$ "(" |--
   417       P.!!! (Scan.option P.nat -- Scan.optional (P.reserved "with_dups" >> K false) true
   418         --| P.$$$ ")")) (NONE, true);
   419 in
   420 
   421 val _ =
   422   OuterSyntax.improper_command "find_theorems" "print theorems meeting specified criteria" K.diag
   423     (options -- Scan.repeat (((Scan.option P.minus >> is_none) -- criterion))
   424       >> (Toplevel.no_timing oo find_theorems_cmd));
   425 
   426 end;
   427 
   428 end;