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