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