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