src/Pure/Tools/find_theorems.ML
changeset 30142 8d6145694bb5
parent 29882 29154e67731d
child 30143 98a986b02022
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/Tools/find_theorems.ML	Fri Feb 27 15:46:22 2009 +0100
     1.3 @@ -0,0 +1,422 @@
     1.4 +(*  Title:      Pure/Isar/find_theorems.ML
     1.5 +    Author:     Rafal Kolanski and Gerwin Klein, NICTA
     1.6 +
     1.7 +Retrieve theorems from proof context.
     1.8 +*)
     1.9 +
    1.10 +signature FIND_THEOREMS =
    1.11 +sig
    1.12 +  val limit: int ref
    1.13 +  val tac_limit: int ref
    1.14 +
    1.15 +  datatype 'term criterion =
    1.16 +    Name of string | Intro | Elim | Dest | Solves | Simp of 'term |
    1.17 +    Pattern of 'term
    1.18 +
    1.19 +  val find_theorems: Proof.context -> thm option -> bool ->
    1.20 +    (bool * string criterion) list -> (Facts.ref * thm) list
    1.21 +
    1.22 +  val print_theorems: Proof.context -> thm option -> int option -> bool ->
    1.23 +    (bool * string criterion) list -> unit
    1.24 +end;
    1.25 +
    1.26 +structure FindTheorems: FIND_THEOREMS =
    1.27 +struct
    1.28 +
    1.29 +(** search criteria **)
    1.30 +
    1.31 +datatype 'term criterion =
    1.32 +  Name of string | Intro | Elim | Dest | Solves | Simp of 'term |
    1.33 +  Pattern of 'term;
    1.34 +
    1.35 +fun read_criterion _ (Name name) = Name name
    1.36 +  | read_criterion _ Intro = Intro
    1.37 +  | read_criterion _ Elim = Elim
    1.38 +  | read_criterion _ Dest = Dest
    1.39 +  | read_criterion _ Solves = Solves
    1.40 +  | read_criterion ctxt (Simp str) = Simp (ProofContext.read_term_pattern ctxt str)
    1.41 +  | read_criterion ctxt (Pattern str) = Pattern (ProofContext.read_term_pattern ctxt str);
    1.42 +
    1.43 +fun pretty_criterion ctxt (b, c) =
    1.44 +  let
    1.45 +    fun prfx s = if b then s else "-" ^ s;
    1.46 +  in
    1.47 +    (case c of
    1.48 +      Name name => Pretty.str (prfx "name: " ^ quote name)
    1.49 +    | Intro => Pretty.str (prfx "intro")
    1.50 +    | Elim => Pretty.str (prfx "elim")
    1.51 +    | Dest => Pretty.str (prfx "dest")
    1.52 +    | Solves => Pretty.str (prfx "solves")
    1.53 +    | Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1,
    1.54 +        Pretty.quote (Syntax.pretty_term ctxt (Term.show_dummy_patterns pat))]
    1.55 +    | Pattern pat => Pretty.enclose (prfx " \"") "\""
    1.56 +        [Syntax.pretty_term ctxt (Term.show_dummy_patterns pat)])
    1.57 +  end;
    1.58 +
    1.59 +
    1.60 +
    1.61 +(** search criterion filters **)
    1.62 +
    1.63 +(*generated filters are to be of the form
    1.64 +  input: (Facts.ref * thm)
    1.65 +  output: (p:int, s:int) option, where
    1.66 +    NONE indicates no match
    1.67 +    p is the primary sorting criterion
    1.68 +      (eg. number of assumptions in the theorem)
    1.69 +    s is the secondary sorting criterion
    1.70 +      (eg. size of the substitution for intro, elim and dest)
    1.71 +  when applying a set of filters to a thm, fold results in:
    1.72 +    (biggest p, sum of all s)
    1.73 +  currently p and s only matter for intro, elim, dest and simp filters,
    1.74 +  otherwise the default ordering is used.
    1.75 +*)
    1.76 +
    1.77 +
    1.78 +(* matching theorems *)
    1.79 +
    1.80 +fun is_nontrivial thy = Term.is_Const o Term.head_of o ObjectLogic.drop_judgment thy;
    1.81 +
    1.82 +(*extract terms from term_src, refine them to the parts that concern us,
    1.83 +  if po try match them against obj else vice versa.
    1.84 +  trivial matches are ignored.
    1.85 +  returns: smallest substitution size*)
    1.86 +fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src =
    1.87 +  let
    1.88 +    val thy = ProofContext.theory_of ctxt;
    1.89 +
    1.90 +    fun matches pat =
    1.91 +      is_nontrivial thy pat andalso
    1.92 +      Pattern.matches thy (if po then (pat, obj) else (obj, pat));
    1.93 +
    1.94 +    fun substsize pat =
    1.95 +      let val (_, subst) =
    1.96 +        Pattern.match thy (if po then (pat, obj) else (obj, pat)) (Vartab.empty, Vartab.empty)
    1.97 +      in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end;
    1.98 +
    1.99 +    fun bestmatch [] = NONE
   1.100 +     |  bestmatch xs = SOME (foldr1 Int.min xs);
   1.101 +
   1.102 +    val match_thm = matches o refine_term;
   1.103 +  in
   1.104 +    map (substsize o refine_term) (filter match_thm (extract_terms term_src))
   1.105 +    |> bestmatch
   1.106 +  end;
   1.107 +
   1.108 +
   1.109 +(* filter_name *)
   1.110 +
   1.111 +fun filter_name str_pat (thmref, _) =
   1.112 +  if match_string str_pat (Facts.name_of_ref thmref)
   1.113 +  then SOME (0, 0) else NONE;
   1.114 +
   1.115 +
   1.116 +(* filter intro/elim/dest/solves rules *)
   1.117 +
   1.118 +fun filter_dest ctxt goal (_, thm) =
   1.119 +  let
   1.120 +    val extract_dest =
   1.121 +     (fn thm => if Thm.no_prems thm then [] else [Thm.full_prop_of thm],
   1.122 +      hd o Logic.strip_imp_prems);
   1.123 +    val prems = Logic.prems_of_goal goal 1;
   1.124 +
   1.125 +    fun try_subst prem = is_matching_thm extract_dest ctxt true prem thm;
   1.126 +    val successful = prems |> map_filter try_subst;
   1.127 +  in
   1.128 +    (*if possible, keep best substitution (one with smallest size)*)
   1.129 +    (*dest rules always have assumptions, so a dest with one
   1.130 +      assumption is as good as an intro rule with none*)
   1.131 +    if not (null successful)
   1.132 +    then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
   1.133 +  end;
   1.134 +
   1.135 +fun filter_intro ctxt goal (_, thm) =
   1.136 +  let
   1.137 +    val extract_intro = (single o Thm.full_prop_of, Logic.strip_imp_concl);
   1.138 +    val concl = Logic.concl_of_goal goal 1;
   1.139 +    val ss = is_matching_thm extract_intro ctxt true concl thm;
   1.140 +  in
   1.141 +    if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
   1.142 +  end;
   1.143 +
   1.144 +fun filter_elim ctxt goal (_, thm) =
   1.145 +  if not (Thm.no_prems thm) then
   1.146 +    let
   1.147 +      val rule = Thm.full_prop_of thm;
   1.148 +      val prems = Logic.prems_of_goal goal 1;
   1.149 +      val goal_concl = Logic.concl_of_goal goal 1;
   1.150 +      val rule_mp = hd (Logic.strip_imp_prems rule);
   1.151 +      val rule_concl = Logic.strip_imp_concl rule;
   1.152 +      fun combine t1 t2 = Const ("*combine*", dummyT --> dummyT) $ (t1 $ t2);
   1.153 +      val rule_tree = combine rule_mp rule_concl;
   1.154 +      fun goal_tree prem = combine prem goal_concl;
   1.155 +      fun try_subst prem =
   1.156 +        is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree;
   1.157 +      val successful = prems |> map_filter try_subst;
   1.158 +    in
   1.159 +    (*elim rules always have assumptions, so an elim with one
   1.160 +      assumption is as good as an intro rule with none*)
   1.161 +      if is_nontrivial (ProofContext.theory_of ctxt) (Thm.major_prem_of thm)
   1.162 +        andalso not (null successful)
   1.163 +      then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE
   1.164 +    end
   1.165 +  else NONE
   1.166 +
   1.167 +val tac_limit = ref 5;
   1.168 +
   1.169 +fun filter_solves ctxt goal = let
   1.170 +    val baregoal = Logic.get_goal (prop_of goal) 1;
   1.171 +
   1.172 +    fun etacn thm i = Seq.take (!tac_limit) o etac thm i;
   1.173 +    fun try_thm thm = if Thm.no_prems thm then rtac thm 1 goal
   1.174 +                      else (etacn thm THEN_ALL_NEW
   1.175 +                             (Goal.norm_hhf_tac THEN'
   1.176 +                               Method.assumption_tac ctxt)) 1 goal;
   1.177 +  in
   1.178 +    fn (_, thm) => if (is_some o Seq.pull o try_thm) thm
   1.179 +                   then SOME (Thm.nprems_of thm, 0) else NONE
   1.180 +  end;
   1.181 +
   1.182 +
   1.183 +(* filter_simp *)
   1.184 +
   1.185 +fun filter_simp ctxt t (_, thm) =
   1.186 +  let
   1.187 +    val (_, {mk_rews = {mk, ...}, ...}) =
   1.188 +      Simplifier.rep_ss (Simplifier.local_simpset_of ctxt);
   1.189 +    val extract_simp =
   1.190 +      (map Thm.full_prop_of o mk, #1 o Logic.dest_equals o Logic.strip_imp_concl);
   1.191 +    val ss = is_matching_thm extract_simp ctxt false t thm
   1.192 +  in
   1.193 +    if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE
   1.194 +  end;
   1.195 +
   1.196 +
   1.197 +(* filter_pattern *)
   1.198 +
   1.199 +fun get_names t = (Term.add_const_names t []) union (Term.add_free_names t []);
   1.200 +fun get_thm_names (_, thm) = get_names (Thm.full_prop_of thm);
   1.201 +  (* Including all constants and frees is only sound because
   1.202 +     matching uses higher-order patterns. If full matching
   1.203 +     were used, then constants that may be subject to
   1.204 +     beta-reduction after substitution of frees should
   1.205 +     not be included for LHS set because they could be
   1.206 +     thrown away by the substituted function.
   1.207 +     e.g. for (?F 1 2) do not include 1 or 2, if it were
   1.208 +          possible for ?F to be (% x y. 3)
   1.209 +     The largest possible set should always be included on
   1.210 +     the RHS. *)
   1.211 +
   1.212 +fun filter_pattern ctxt pat = let
   1.213 +    val pat_consts = get_names pat;
   1.214 +
   1.215 +    fun check (t, NONE) = check (t, SOME (get_thm_names t))
   1.216 +      | check ((_, thm), c as SOME thm_consts) =
   1.217 +          (if pat_consts subset_string thm_consts
   1.218 +              andalso (Pattern.matches_subterm (ProofContext.theory_of ctxt)
   1.219 +                                               (pat, Thm.full_prop_of thm))
   1.220 +           then SOME (0, 0) else NONE, c);
   1.221 +  in check end;
   1.222 +
   1.223 +
   1.224 +(* interpret criteria as filters *)
   1.225 +
   1.226 +local
   1.227 +
   1.228 +fun err_no_goal c =
   1.229 +  error ("Current goal required for " ^ c ^ " search criterion");
   1.230 +
   1.231 +val fix_goal = Thm.prop_of;
   1.232 +val fix_goalo = Option.map fix_goal;
   1.233 +
   1.234 +fun filter_crit _ _ (Name name) = apfst (filter_name name)
   1.235 +  | filter_crit _ NONE Intro = err_no_goal "intro"
   1.236 +  | filter_crit _ NONE Elim = err_no_goal "elim"
   1.237 +  | filter_crit _ NONE Dest = err_no_goal "dest"
   1.238 +  | filter_crit _ NONE Solves = err_no_goal "solves"
   1.239 +  | filter_crit ctxt (SOME goal) Intro = apfst (filter_intro ctxt
   1.240 +                                                  (fix_goal goal))
   1.241 +  | filter_crit ctxt (SOME goal) Elim = apfst (filter_elim ctxt 
   1.242 +                                                  (fix_goal goal))
   1.243 +  | filter_crit ctxt (SOME goal) Dest = apfst (filter_dest ctxt
   1.244 +                                                  (fix_goal goal))
   1.245 +  | filter_crit ctxt (SOME goal) Solves = apfst (filter_solves ctxt goal)
   1.246 +  | filter_crit ctxt _ (Simp pat) = apfst (filter_simp ctxt pat)
   1.247 +  | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat;
   1.248 +
   1.249 +fun opt_not x = if is_some x then NONE else SOME (0, 0);
   1.250 +
   1.251 +fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y : int)
   1.252 +  | opt_add _ _ = NONE;
   1.253 +
   1.254 +fun app_filters thm = let
   1.255 +    fun app (NONE, _, _) = NONE
   1.256 +      | app (SOME v, consts, []) = SOME (v, thm)
   1.257 +      | app (r, consts, f::fs) = let val (r', consts') = f (thm, consts)
   1.258 +                                 in app (opt_add r r', consts', fs) end;
   1.259 +  in app end;
   1.260 +
   1.261 +in
   1.262 +
   1.263 +fun filter_criterion ctxt opt_goal (b, c) =
   1.264 +  (if b then I else (apfst opt_not)) o filter_crit ctxt opt_goal c;
   1.265 +
   1.266 +fun all_filters filters thms =
   1.267 +  let
   1.268 +    fun eval_filters thm = app_filters thm (SOME (0, 0), NONE, filters);
   1.269 +
   1.270 +    (*filters return: (number of assumptions, substitution size) option, so
   1.271 +      sort (desc. in both cases) according to number of assumptions first,
   1.272 +      then by the substitution size*)
   1.273 +    fun thm_ord (((p0, s0), _), ((p1, s1), _)) =
   1.274 +      prod_ord int_ord int_ord ((p1, s1), (p0, s0));
   1.275 +  in map_filter eval_filters thms |> sort thm_ord |> map #2 end;
   1.276 +
   1.277 +end;
   1.278 +
   1.279 +
   1.280 +(* removing duplicates, preferring nicer names, roughly n log n *)
   1.281 +
   1.282 +local
   1.283 +
   1.284 +val index_ord = option_ord (K EQUAL);
   1.285 +val hidden_ord = bool_ord o pairself NameSpace.is_hidden;
   1.286 +val qual_ord = int_ord o pairself (length o NameSpace.explode);
   1.287 +val txt_ord = int_ord o pairself size;
   1.288 +
   1.289 +fun nicer_name (x, i) (y, j) =
   1.290 +  (case hidden_ord (x, y) of EQUAL =>
   1.291 +    (case index_ord (i, j) of EQUAL =>
   1.292 +      (case qual_ord (x, y) of EQUAL => txt_ord (x, y) | ord => ord)
   1.293 +    | ord => ord)
   1.294 +  | ord => ord) <> GREATER;
   1.295 +
   1.296 +fun rem_cdups nicer xs =
   1.297 +  let
   1.298 +    fun rem_c rev_seen [] = rev rev_seen
   1.299 +      | rem_c rev_seen [x] = rem_c (x :: rev_seen) []
   1.300 +      | rem_c rev_seen ((x as ((n, t), _)) :: (y as ((n', t'), _)) :: xs) =
   1.301 +        if Thm.eq_thm_prop (t, t')
   1.302 +        then rem_c rev_seen ((if nicer n n' then x else y) :: xs)
   1.303 +        else rem_c (x :: rev_seen) (y :: xs)
   1.304 +  in rem_c [] xs end;
   1.305 +
   1.306 +in
   1.307 +
   1.308 +fun nicer_shortest ctxt = let
   1.309 +    val ns = ProofContext.theory_of ctxt
   1.310 +             |> PureThy.facts_of
   1.311 +             |> Facts.space_of;
   1.312 +
   1.313 +    val len_sort = sort (int_ord o (pairself size));
   1.314 +    fun shorten s = (case len_sort (NameSpace.get_accesses ns s) of
   1.315 +                       [] => s
   1.316 +                     | s'::_ => s');
   1.317 +
   1.318 +    fun nicer (Facts.Named ((x, _), i)) (Facts.Named ((y, _), j)) =
   1.319 +          nicer_name (shorten x, i) (shorten y, j)
   1.320 +      | nicer (Facts.Fact _) (Facts.Named _) = true
   1.321 +      | nicer (Facts.Named _) (Facts.Fact _) = false;
   1.322 +  in nicer end;
   1.323 +
   1.324 +fun rem_thm_dups nicer xs =
   1.325 +  xs ~~ (1 upto length xs)
   1.326 +  |> sort (TermOrd.fast_term_ord o pairself (Thm.prop_of o #2 o #1))
   1.327 +  |> rem_cdups nicer
   1.328 +  |> sort (int_ord o pairself #2)
   1.329 +  |> map #1;
   1.330 +
   1.331 +end;
   1.332 +
   1.333 +
   1.334 +(* print_theorems *)
   1.335 +
   1.336 +fun all_facts_of ctxt =
   1.337 +  maps Facts.selections
   1.338 +   (Facts.dest_static [] (PureThy.facts_of (ProofContext.theory_of ctxt)) @
   1.339 +    Facts.dest_static [] (ProofContext.facts_of ctxt));
   1.340 +
   1.341 +val limit = ref 40;
   1.342 +
   1.343 +fun find_theorems ctxt opt_goal rem_dups raw_criteria =
   1.344 +  let
   1.345 +    val add_prems = Seq.hd o (TRY (Method.insert_tac
   1.346 +                                     (Assumption.prems_of ctxt) 1));
   1.347 +    val opt_goal' = Option.map add_prems opt_goal;
   1.348 +
   1.349 +    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
   1.350 +    val filters = map (filter_criterion ctxt opt_goal') criteria;
   1.351 +
   1.352 +    val raw_matches = all_filters filters (all_facts_of ctxt);
   1.353 +
   1.354 +    val matches =
   1.355 +      if rem_dups
   1.356 +      then rem_thm_dups (nicer_shortest ctxt) raw_matches
   1.357 +      else raw_matches;
   1.358 +  in matches end;
   1.359 +
   1.360 +fun print_theorems ctxt opt_goal opt_limit rem_dups raw_criteria = let
   1.361 +    val start = start_timing ();
   1.362 +
   1.363 +    val criteria = map (apsnd (read_criterion ctxt)) raw_criteria;
   1.364 +    val matches = find_theorems ctxt opt_goal rem_dups raw_criteria;
   1.365 +
   1.366 +    val len = length matches;
   1.367 +    val lim = the_default (! limit) opt_limit;
   1.368 +    val thms = Library.drop (len - lim, matches);
   1.369 +
   1.370 +    val end_msg = " in " ^
   1.371 +                  (List.nth (String.tokens Char.isSpace (end_timing start), 3))
   1.372 +                  ^ " secs"
   1.373 +  in
   1.374 +    Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria)
   1.375 +        :: Pretty.str "" ::
   1.376 +     (if null thms then [Pretty.str ("nothing found" ^ end_msg)]
   1.377 +      else
   1.378 +        [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^
   1.379 +          (if len <= lim then ""
   1.380 +           else " (" ^ string_of_int lim ^ " displayed)")
   1.381 +           ^ end_msg ^ ":"), Pretty.str ""] @
   1.382 +        map Display.pretty_fact thms)
   1.383 +    |> Pretty.chunks |> Pretty.writeln
   1.384 +  end;
   1.385 +
   1.386 +
   1.387 +
   1.388 +(** command syntax **)
   1.389 +
   1.390 +fun find_theorems_cmd ((opt_lim, rem_dups), spec) =
   1.391 +  Toplevel.unknown_theory o Toplevel.keep (fn state =>
   1.392 +  let
   1.393 +    val proof_state = Toplevel.enter_proof_body state;
   1.394 +    val ctxt = Proof.context_of proof_state;
   1.395 +    val opt_goal = try Proof.get_goal proof_state |> Option.map (#2 o #2);
   1.396 +  in print_theorems ctxt opt_goal opt_lim rem_dups spec end);
   1.397 +
   1.398 +local
   1.399 +
   1.400 +structure P = OuterParse and K = OuterKeyword;
   1.401 +
   1.402 +val criterion =
   1.403 +  P.reserved "name" |-- P.!!! (P.$$$ ":" |-- P.xname) >> Name ||
   1.404 +  P.reserved "intro" >> K Intro ||
   1.405 +  P.reserved "elim" >> K Elim ||
   1.406 +  P.reserved "dest" >> K Dest ||
   1.407 +  P.reserved "solves" >> K Solves ||
   1.408 +  P.reserved "simp" |-- P.!!! (P.$$$ ":" |-- P.term) >> Simp ||
   1.409 +  P.term >> Pattern;
   1.410 +
   1.411 +val options =
   1.412 +  Scan.optional
   1.413 +    (P.$$$ "(" |--
   1.414 +      P.!!! (Scan.option P.nat -- Scan.optional (P.reserved "with_dups" >> K false) true
   1.415 +        --| P.$$$ ")")) (NONE, true);
   1.416 +in
   1.417 +
   1.418 +val _ =
   1.419 +  OuterSyntax.improper_command "find_theorems" "print theorems meeting specified criteria" K.diag
   1.420 +    (options -- Scan.repeat (((Scan.option P.minus >> is_none) -- criterion))
   1.421 +      >> (Toplevel.no_timing oo find_theorems_cmd));
   1.422 +
   1.423 +end;
   1.424 +
   1.425 +end;