src/Pure/Isar/find_theorems.ML
author wenzelm
Tue Dec 05 22:14:42 2006 +0100 (2006-12-05)
changeset 21658 5e31241e1e3c
parent 19502 369cde91963d
child 22340 275802767bf3
permissions -rw-r--r--
Attrib.internal: morphism;
     1 (*  Title:      Pure/Isar/find_theorems.ML
     2     ID:         $Id$
     3     Author:     Rafal Kolanski, NICTA and Tobias Nipkow, TU Muenchen
     4 
     5 Retrieve theorems from proof context.
     6 *)
     7 
     8 val thms_containing_limit = ref 40;
     9 
    10 signature FIND_THEOREMS =
    11 sig
    12   datatype 'term criterion =
    13     Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term
    14   val print_theorems: Proof.context -> term option -> int option ->
    15     (bool * string criterion) list -> unit
    16 end;
    17 
    18 structure FindTheorems: FIND_THEOREMS =
    19 struct
    20 
    21 (** search criteria **)
    22 
    23 datatype 'term criterion =
    24   Name of string | Intro | Elim | Dest | Simp of 'term | Pattern of 'term;
    25 
    26 fun read_criterion _ (Name name) = Name name
    27   | read_criterion _ Intro = Intro
    28   | read_criterion _ Elim = Elim
    29   | read_criterion _ Dest = Dest
    30   | read_criterion ctxt (Simp str) =
    31       Simp (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str]))
    32   | read_criterion ctxt (Pattern str) =
    33       Pattern (hd (ProofContext.read_term_pats TypeInfer.logicT ctxt [str]));
    34 
    35 fun pretty_criterion ctxt (b, c) =
    36   let
    37     fun prfx s = if b then s else "-" ^ s;
    38   in
    39     (case c of
    40       Name name => Pretty.str (prfx "name: " ^ quote name)
    41     | Intro => Pretty.str (prfx "intro")
    42     | Elim => Pretty.str (prfx "elim")
    43     | Dest => Pretty.str (prfx "dest")
    44     | Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1,
    45         Pretty.quote (ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat))]
    46     | Pattern pat => Pretty.enclose (prfx " \"") "\""
    47         [ProofContext.pretty_term ctxt (Term.show_dummy_patterns pat)])
    48   end;
    49 
    50 
    51 
    52 (** search criterion filters **)
    53 
    54 (*generated filters are to be of the form
    55   input: (thmref * thm)
    56   output: (p:int, s:int) option, where
    57     NONE indicates no match
    58     p is the primary sorting criterion
    59       (eg. number of assumptions in the theorem)
    60     s is the secondary sorting criterion
    61       (eg. size of the substitution for intro, elim and dest)
    62   when applying a set of filters to a thm, fold results in:
    63     (biggest p, sum of all s)
    64   currently p and s only matter for intro, elim, dest and simp filters,
    65   otherwise the default ordering is used.
    66 *)
    67 
    68 
    69 (* matching theorems *)
    70 
    71 fun is_nontrivial thy = Term.is_Const o Term.head_of o ObjectLogic.drop_judgment thy;
    72 
    73 (*extract terms from term_src, refine them to the parts that concern us,
    74   if po try match them against obj else vice versa.
    75   trivial matches are ignored.
    76   returns: smallest substitution size*)
    77 fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src =
    78   let
    79     val thy = ProofContext.theory_of ctxt;
    80 
    81     fun matches pat =
    82       is_nontrivial thy pat andalso
    83       Pattern.matches thy (if po then (pat, obj) else (obj, pat));
    84 
    85     fun substsize pat =
    86       let val (_, subst) =
    87         Pattern.match thy (if po then (pat, obj) else (obj, pat)) (Vartab.empty, Vartab.empty)
    88       in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end;
    89 
    90     fun bestmatch [] = NONE
    91      |  bestmatch xs = SOME (foldr1 Int.min xs);
    92 
    93     val match_thm = matches o refine_term;
    94   in
    95     map (substsize o refine_term)
    96         (filter match_thm (extract_terms term_src)) |> bestmatch
    97   end;
    98 
    99 
   100 (* filter_name *)
   101 
   102 fun match_string pat str =
   103   let
   104     fun match [] _ = true
   105       | match (p :: ps) s =
   106           size p <= size s andalso
   107             (case try (unprefix p) s of
   108               SOME s' => match ps s'
   109             | NONE => match (p :: ps) (String.substring (s, 1, size s - 1)));
   110   in match (space_explode "*" pat) str end;
   111 
   112 fun filter_name str_pat (thmref, _) =
   113   if match_string str_pat (PureThy.name_of_thmref thmref)
   114   then SOME (0, 0) else NONE;
   115 
   116 
   117 (* filter intro/elim/dest rules *)
   118 
   119 fun filter_dest ctxt goal (_, thm) =
   120   let
   121     val extract_dest =
   122      (fn thm => if Thm.no_prems thm then [] else [Thm.full_prop_of thm],
   123       hd o Logic.strip_imp_prems);
   124     val prems = Logic.prems_of_goal goal 1;
   125 
   126     fun try_subst prem = is_matching_thm extract_dest ctxt true prem thm;
   127     val successful = prems |> map_filter try_subst;
   128   in
   129     (*if possible, keep best substitution (one with smallest size)*)
   130     (*dest rules always have assumptions, so a dest with one
   131       assumption is as good as an intro rule with none*)
   132     if not (null successful)
   133     then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
   134   end;
   135 
   136 fun filter_intro ctxt goal (_, thm) =
   137   let
   138     val extract_intro = (single o Thm.full_prop_of, Logic.strip_imp_concl);
   139     val concl = Logic.concl_of_goal goal 1;
   140     val ss = is_matching_thm extract_intro ctxt true concl thm;
   141   in
   142     if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
   143   end;
   144 
   145 fun filter_elim ctxt goal (_, thm) =
   146   if not (Thm.no_prems thm) then
   147     let
   148       val rule = Thm.full_prop_of thm;
   149       val prems = Logic.prems_of_goal goal 1;
   150       val goal_concl = Logic.concl_of_goal goal 1;
   151       val rule_mp = (hd o Logic.strip_imp_prems) rule;
   152       val rule_concl = Logic.strip_imp_concl rule;
   153       fun combine t1 t2 = Const ("combine", dummyT --> dummyT) $ (t1 $ t2);
   154       val rule_tree = combine rule_mp rule_concl;
   155       fun goal_tree prem = (combine prem goal_concl);
   156       fun try_subst prem =
   157         is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree;
   158       val successful = prems |> map_filter try_subst;
   159     in
   160     (*elim rules always have assumptions, so an elim with one
   161       assumption is as good as an intro rule with none*)
   162       if is_nontrivial (ProofContext.theory_of ctxt) (Thm.major_prem_of thm)
   163         andalso not (null successful)
   164       then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
   165     end
   166   else NONE
   167 
   168 
   169 (* filter_simp *)
   170 
   171 fun filter_simp ctxt t (_, thm) =
   172   let
   173     val (_, {mk_rews = {mk, ...}, ...}) =
   174       MetaSimplifier.rep_ss (Simplifier.local_simpset_of ctxt);
   175     val extract_simp =
   176       (map Thm.full_prop_of o mk, #1 o Logic.dest_equals o Logic.strip_imp_concl);
   177     val ss = is_matching_thm extract_simp ctxt false t thm
   178   in
   179     if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
   180   end;
   181 
   182 
   183 (* filter_pattern *)
   184 
   185 fun filter_pattern ctxt pat (_, thm) =
   186   if Pattern.matches_subterm (ProofContext.theory_of ctxt) (pat, Thm.full_prop_of thm)
   187   then SOME (0, 0) else NONE;
   188 
   189 
   190 (* interpret criteria as filters *)
   191 
   192 local
   193 
   194 fun err_no_goal c =
   195   error ("Current goal required for " ^ c ^ " search criterion");
   196 
   197 fun filter_crit _ _ (Name name) = filter_name name
   198   | filter_crit _ NONE Intro = err_no_goal "intro"
   199   | filter_crit _ NONE Elim = err_no_goal "elim"
   200   | filter_crit _ NONE Dest = err_no_goal "dest"
   201   | filter_crit ctxt (SOME goal) Intro = filter_intro ctxt goal
   202   | filter_crit ctxt (SOME goal) Elim = filter_elim ctxt goal
   203   | filter_crit ctxt (SOME goal) Dest = filter_dest ctxt goal
   204   | filter_crit ctxt _ (Simp pat) = filter_simp ctxt pat
   205   | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
   206 
   207 fun opt_not x = if is_some x then NONE else SOME (0, 0);
   208 
   209 fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y : int)
   210  |  opt_add _ _ = NONE;
   211 
   212 in
   213 
   214 fun filter_criterion ctxt opt_goal (b, c) =
   215   (if b then I else opt_not) o filter_crit ctxt opt_goal c;
   216 
   217 fun all_filters filters thms =
   218   let
   219     fun eval_filters filters thm =
   220       fold opt_add (map (fn f => f thm) filters) (SOME (0, 0));
   221 
   222     (*filters return: (number of assumptions, substitution size) option, so
   223       sort (desc. in both cases) according to number of assumptions first,
   224       then by the substitution size*)
   225     fun thm_ord (((p0, s0), _), ((p1, s1), _)) =
   226       prod_ord int_ord int_ord ((p1, s1), (p0, s0));
   227   in
   228     map (`(eval_filters filters)) thms
   229     |> map_filter (fn (SOME x, y) => SOME (x, y) | (NONE, _) => NONE)
   230     |> sort thm_ord |> map #2
   231   end;
   232 
   233 end;
   234 
   235 
   236 (* print_theorems *)
   237 
   238 fun find_thms ctxt spec =
   239   (PureThy.thms_containing (ProofContext.theory_of ctxt) spec
   240     |> maps PureThy.selections) @
   241   (ProofContext.lthms_containing ctxt spec
   242     |> maps PureThy.selections
   243     |> distinct (fn ((r1, th1), (r2, th2)) =>
   244         r1 = r2 andalso Drule.eq_thm_prop (th1, th2)));
   245 
   246 fun print_theorems ctxt opt_goal opt_limit raw_criteria =
   247   let
   248     val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
   249     val filters = map (filter_criterion ctxt opt_goal) criteria;
   250 
   251     val matches = all_filters filters (find_thms ctxt ([], []));
   252     val len = length matches;
   253     val limit = the_default (! thms_containing_limit) opt_limit;
   254 
   255     fun prt_fact (thmref, thm) =
   256       ProofContext.pretty_fact ctxt (PureThy.string_of_thmref thmref, [thm]);
   257   in
   258     Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) :: Pretty.str "" ::
   259      (if null matches then [Pretty.str "nothing found"]
   260       else
   261         [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
   262           (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":"),
   263          Pretty.str ""] @
   264         map prt_fact (Library.drop (len - limit, matches)))
   265     |> Pretty.chunks |> Pretty.writeln
   266   end;
   267 
   268 end;