src/Pure/Tools/find_theorems.ML
 author wenzelm Fri Feb 27 16:05:40 2009 +0100 (2009-02-27) changeset 30143 98a986b02022 parent 30142 8d6145694bb5 child 30186 1f836e949ac2 permissions -rw-r--r--
observe basic Isabelle/ML coding conventions;
 wenzelm@30143 ` 1` ```(* Title: Pure/Tools/find_theorems.ML ``` wenzelm@26283 ` 2` ``` Author: Rafal Kolanski and Gerwin Klein, NICTA ``` wenzelm@16033 ` 3` wenzelm@16033 ` 4` ```Retrieve theorems from proof context. ``` wenzelm@16033 ` 5` ```*) ``` wenzelm@16033 ` 6` wenzelm@16033 ` 7` ```signature FIND_THEOREMS = ``` wenzelm@16033 ` 8` ```sig ``` wenzelm@25992 ` 9` ``` val limit: int ref ``` kleing@29857 ` 10` ``` val tac_limit: int ref ``` kleing@29857 ` 11` wenzelm@16036 ` 12` ``` datatype 'term criterion = ``` kleing@29857 ` 13` ``` Name of string | Intro | Elim | Dest | Solves | Simp of 'term | ``` kleing@29857 ` 14` ``` Pattern of 'term ``` kleing@29857 ` 15` kleing@29857 ` 16` ``` val find_theorems: Proof.context -> thm option -> bool -> ``` kleing@29857 ` 17` ``` (bool * string criterion) list -> (Facts.ref * thm) list ``` kleing@29857 ` 18` kleing@29857 ` 19` ``` val print_theorems: Proof.context -> thm option -> int option -> bool -> ``` wenzelm@16036 ` 20` ``` (bool * string criterion) list -> unit ``` wenzelm@16033 ` 21` ```end; ``` wenzelm@16033 ` 22` wenzelm@16033 ` 23` ```structure FindTheorems: FIND_THEOREMS = ``` wenzelm@16033 ` 24` ```struct ``` wenzelm@16033 ` 25` wenzelm@16033 ` 26` ```(** search criteria **) ``` wenzelm@16033 ` 27` wenzelm@16036 ` 28` ```datatype 'term criterion = ``` kleing@29857 ` 29` ``` Name of string | Intro | Elim | Dest | Solves | Simp of 'term | ``` kleing@29857 ` 30` ``` Pattern of 'term; ``` wenzelm@16036 ` 31` wenzelm@16036 ` 32` ```fun read_criterion _ (Name name) = Name name ``` wenzelm@16036 ` 33` ``` | read_criterion _ Intro = Intro ``` wenzelm@16036 ` 34` ``` | read_criterion _ Elim = Elim ``` wenzelm@16036 ` 35` ``` | read_criterion _ Dest = Dest ``` kleing@29857 ` 36` ``` | read_criterion _ Solves = Solves ``` wenzelm@24683 ` 37` ``` | read_criterion ctxt (Simp str) = Simp (ProofContext.read_term_pattern ctxt str) ``` wenzelm@24683 ` 38` ``` | read_criterion ctxt (Pattern str) = Pattern (ProofContext.read_term_pattern ctxt str); ``` wenzelm@16033 ` 39` wenzelm@16036 ` 40` ```fun pretty_criterion ctxt (b, c) = ``` wenzelm@16036 ` 41` ``` let ``` wenzelm@16036 ` 42` ``` fun prfx s = if b then s else "-" ^ s; ``` wenzelm@16036 ` 43` ``` in ``` wenzelm@16036 ` 44` ``` (case c of ``` wenzelm@16036 ` 45` ``` Name name => Pretty.str (prfx "name: " ^ quote name) ``` wenzelm@16036 ` 46` ``` | Intro => Pretty.str (prfx "intro") ``` wenzelm@16036 ` 47` ``` | Elim => Pretty.str (prfx "elim") ``` wenzelm@16036 ` 48` ``` | Dest => Pretty.str (prfx "dest") ``` kleing@29857 ` 49` ``` | Solves => Pretty.str (prfx "solves") ``` kleing@16088 ` 50` ``` | Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1, ``` wenzelm@24920 ` 51` ``` Pretty.quote (Syntax.pretty_term ctxt (Term.show_dummy_patterns pat))] ``` wenzelm@16036 ` 52` ``` | Pattern pat => Pretty.enclose (prfx " \"") "\"" ``` wenzelm@24920 ` 53` ``` [Syntax.pretty_term ctxt (Term.show_dummy_patterns pat)]) ``` wenzelm@16036 ` 54` ``` end; ``` wenzelm@16033 ` 55` wenzelm@30142 ` 56` wenzelm@30142 ` 57` wenzelm@16033 ` 58` ```(** search criterion filters **) ``` wenzelm@16033 ` 59` kleing@16895 ` 60` ```(*generated filters are to be of the form ``` wenzelm@26336 ` 61` ``` input: (Facts.ref * thm) ``` wenzelm@17106 ` 62` ``` output: (p:int, s:int) option, where ``` kleing@16895 ` 63` ``` NONE indicates no match ``` wenzelm@17106 ` 64` ``` p is the primary sorting criterion ``` kleing@16895 ` 65` ``` (eg. number of assumptions in the theorem) ``` kleing@16895 ` 66` ``` s is the secondary sorting criterion ``` kleing@16895 ` 67` ``` (eg. size of the substitution for intro, elim and dest) ``` kleing@16895 ` 68` ``` when applying a set of filters to a thm, fold results in: ``` kleing@16895 ` 69` ``` (biggest p, sum of all s) ``` wenzelm@17106 ` 70` ``` currently p and s only matter for intro, elim, dest and simp filters, ``` wenzelm@17106 ` 71` ``` otherwise the default ordering is used. ``` kleing@16895 ` 72` ```*) ``` kleing@16895 ` 73` kleing@16088 ` 74` kleing@16088 ` 75` ```(* matching theorems *) ``` wenzelm@17106 ` 76` wenzelm@17205 ` 77` ```fun is_nontrivial thy = Term.is_Const o Term.head_of o ObjectLogic.drop_judgment thy; ``` kleing@16088 ` 78` kleing@16964 ` 79` ```(*extract terms from term_src, refine them to the parts that concern us, ``` kleing@16964 ` 80` ``` if po try match them against obj else vice versa. ``` kleing@16964 ` 81` ``` trivial matches are ignored. ``` kleing@16964 ` 82` ``` returns: smallest substitution size*) ``` kleing@16964 ` 83` ```fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src = ``` kleing@16088 ` 84` ``` let ``` wenzelm@17106 ` 85` ``` val thy = ProofContext.theory_of ctxt; ``` kleing@16088 ` 86` wenzelm@16486 ` 87` ``` fun matches pat = ``` wenzelm@17106 ` 88` ``` is_nontrivial thy pat andalso ``` wenzelm@17205 ` 89` ``` Pattern.matches thy (if po then (pat, obj) else (obj, pat)); ``` kleing@16895 ` 90` kleing@16895 ` 91` ``` fun substsize pat = ``` wenzelm@18184 ` 92` ``` let val (_, subst) = ``` wenzelm@18184 ` 93` ``` Pattern.match thy (if po then (pat, obj) else (obj, pat)) (Vartab.empty, Vartab.empty) ``` wenzelm@17205 ` 94` ``` in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end; ``` kleing@16088 ` 95` kleing@16895 ` 96` ``` fun bestmatch [] = NONE ``` wenzelm@17205 ` 97` ``` | bestmatch xs = SOME (foldr1 Int.min xs); ``` kleing@16895 ` 98` kleing@16964 ` 99` ``` val match_thm = matches o refine_term; ``` wenzelm@16486 ` 100` ``` in ``` wenzelm@26283 ` 101` ``` map (substsize o refine_term) (filter match_thm (extract_terms term_src)) ``` wenzelm@26283 ` 102` ``` |> bestmatch ``` kleing@16088 ` 103` ``` end; ``` kleing@16088 ` 104` kleing@16088 ` 105` wenzelm@16033 ` 106` ```(* filter_name *) ``` wenzelm@16033 ` 107` wenzelm@17106 ` 108` ```fun filter_name str_pat (thmref, _) = ``` wenzelm@26336 ` 109` ``` if match_string str_pat (Facts.name_of_ref thmref) ``` wenzelm@17205 ` 110` ``` then SOME (0, 0) else NONE; ``` wenzelm@16033 ` 111` wenzelm@30142 ` 112` kleing@29857 ` 113` ```(* filter intro/elim/dest/solves rules *) ``` wenzelm@16033 ` 114` wenzelm@17205 ` 115` ```fun filter_dest ctxt goal (_, thm) = ``` wenzelm@16033 ` 116` ``` let ``` kleing@16964 ` 117` ``` val extract_dest = ``` wenzelm@17205 ` 118` ``` (fn thm => if Thm.no_prems thm then [] else [Thm.full_prop_of thm], ``` wenzelm@16033 ` 119` ``` hd o Logic.strip_imp_prems); ``` wenzelm@16033 ` 120` ``` val prems = Logic.prems_of_goal goal 1; ``` kleing@16895 ` 121` kleing@16964 ` 122` ``` fun try_subst prem = is_matching_thm extract_dest ctxt true prem thm; ``` wenzelm@19482 ` 123` ``` val successful = prems |> map_filter try_subst; ``` wenzelm@16033 ` 124` ``` in ``` kleing@16895 ` 125` ``` (*if possible, keep best substitution (one with smallest size)*) ``` wenzelm@17106 ` 126` ``` (*dest rules always have assumptions, so a dest with one ``` kleing@16895 ` 127` ``` assumption is as good as an intro rule with none*) ``` wenzelm@17205 ` 128` ``` if not (null successful) ``` wenzelm@17205 ` 129` ``` then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE ``` wenzelm@16033 ` 130` ``` end; ``` wenzelm@16033 ` 131` wenzelm@17205 ` 132` ```fun filter_intro ctxt goal (_, thm) = ``` wenzelm@16033 ` 133` ``` let ``` wenzelm@17205 ` 134` ``` val extract_intro = (single o Thm.full_prop_of, Logic.strip_imp_concl); ``` wenzelm@16036 ` 135` ``` val concl = Logic.concl_of_goal goal 1; ``` kleing@16964 ` 136` ``` val ss = is_matching_thm extract_intro ctxt true concl thm; ``` wenzelm@16033 ` 137` ``` in ``` wenzelm@18939 ` 138` ``` if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE ``` wenzelm@16033 ` 139` ``` end; ``` wenzelm@16033 ` 140` wenzelm@17205 ` 141` ```fun filter_elim ctxt goal (_, thm) = ``` kleing@16964 ` 142` ``` if not (Thm.no_prems thm) then ``` kleing@16964 ` 143` ``` let ``` wenzelm@17205 ` 144` ``` val rule = Thm.full_prop_of thm; ``` kleing@16964 ` 145` ``` val prems = Logic.prems_of_goal goal 1; ``` kleing@16964 ` 146` ``` val goal_concl = Logic.concl_of_goal goal 1; ``` wenzelm@26283 ` 147` ``` val rule_mp = hd (Logic.strip_imp_prems rule); ``` kleing@16964 ` 148` ``` val rule_concl = Logic.strip_imp_concl rule; ``` wenzelm@26283 ` 149` ``` fun combine t1 t2 = Const ("*combine*", dummyT --> dummyT) \$ (t1 \$ t2); ``` kleing@16964 ` 150` ``` val rule_tree = combine rule_mp rule_concl; ``` wenzelm@26283 ` 151` ``` fun goal_tree prem = combine prem goal_concl; ``` wenzelm@17106 ` 152` ``` fun try_subst prem = ``` kleing@16964 ` 153` ``` is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree; ``` wenzelm@19482 ` 154` ``` val successful = prems |> map_filter try_subst; ``` kleing@16964 ` 155` ``` in ``` wenzelm@17106 ` 156` ``` (*elim rules always have assumptions, so an elim with one ``` kleing@16964 ` 157` ``` assumption is as good as an intro rule with none*) ``` wenzelm@17106 ` 158` ``` if is_nontrivial (ProofContext.theory_of ctxt) (Thm.major_prem_of thm) ``` wenzelm@17205 ` 159` ``` andalso not (null successful) ``` wenzelm@17205 ` 160` ``` then SOME (Thm.nprems_of thm - 1, foldr1 Int.min successful) else NONE ``` kleing@16964 ` 161` ``` end ``` kleing@16964 ` 162` ``` else NONE ``` wenzelm@16036 ` 163` kleing@29857 ` 164` ```val tac_limit = ref 5; ``` kleing@29857 ` 165` wenzelm@30143 ` 166` ```fun filter_solves ctxt goal = ``` wenzelm@30143 ` 167` ``` let ``` wenzelm@30143 ` 168` ``` val baregoal = Logic.get_goal (Thm.prop_of goal) 1; ``` kleing@29857 ` 169` wenzelm@30143 ` 170` ``` fun etacn thm i = Seq.take (! tac_limit) o etac thm i; ``` wenzelm@30143 ` 171` ``` fun try_thm thm = ``` wenzelm@30143 ` 172` ``` if Thm.no_prems thm then rtac thm 1 goal ``` wenzelm@30143 ` 173` ``` else (etacn thm THEN_ALL_NEW ``` wenzelm@30143 ` 174` ``` (Goal.norm_hhf_tac THEN' ``` wenzelm@30143 ` 175` ``` Method.assumption_tac ctxt)) 1 goal; ``` kleing@29857 ` 176` ``` in ``` wenzelm@30143 ` 177` ``` fn (_, thm) => ``` wenzelm@30143 ` 178` ``` if (is_some o Seq.pull o try_thm) thm ``` wenzelm@30143 ` 179` ``` then SOME (Thm.nprems_of thm, 0) else NONE ``` kleing@29857 ` 180` ``` end; ``` wenzelm@16033 ` 181` wenzelm@30142 ` 182` kleing@16074 ` 183` ```(* filter_simp *) ``` wenzelm@16033 ` 184` wenzelm@17205 ` 185` ```fun filter_simp ctxt t (_, thm) = ``` wenzelm@16033 ` 186` ``` let ``` wenzelm@16033 ` 187` ``` val (_, {mk_rews = {mk, ...}, ...}) = ``` wenzelm@29302 ` 188` ``` Simplifier.rep_ss (Simplifier.local_simpset_of ctxt); ``` wenzelm@17106 ` 189` ``` val extract_simp = ``` wenzelm@17205 ` 190` ``` (map Thm.full_prop_of o mk, #1 o Logic.dest_equals o Logic.strip_imp_concl); ``` kleing@16964 ` 191` ``` val ss = is_matching_thm extract_simp ctxt false t thm ``` wenzelm@17106 ` 192` ``` in ``` wenzelm@18939 ` 193` ``` if is_some ss then SOME (Thm.nprems_of thm, the ss) else NONE ``` kleing@16964 ` 194` ``` end; ``` wenzelm@16033 ` 195` wenzelm@16033 ` 196` wenzelm@16033 ` 197` ```(* filter_pattern *) ``` wenzelm@16033 ` 198` kleing@29857 ` 199` ```fun get_names t = (Term.add_const_names t []) union (Term.add_free_names t []); ``` kleing@29857 ` 200` ```fun get_thm_names (_, thm) = get_names (Thm.full_prop_of thm); ``` kleing@28900 ` 201` wenzelm@30143 ` 202` ```(*Including all constants and frees is only sound because ``` wenzelm@30143 ` 203` ``` matching uses higher-order patterns. If full matching ``` wenzelm@30143 ` 204` ``` were used, then constants that may be subject to ``` wenzelm@30143 ` 205` ``` beta-reduction after substitution of frees should ``` wenzelm@30143 ` 206` ``` not be included for LHS set because they could be ``` wenzelm@30143 ` 207` ``` thrown away by the substituted function. ``` wenzelm@30143 ` 208` ``` e.g. for (?F 1 2) do not include 1 or 2, if it were ``` wenzelm@30143 ` 209` ``` possible for ?F to be (% x y. 3) ``` wenzelm@30143 ` 210` ``` The largest possible set should always be included on ``` wenzelm@30143 ` 211` ``` the RHS.*) ``` wenzelm@30143 ` 212` wenzelm@30143 ` 213` ```fun filter_pattern ctxt pat = ``` wenzelm@30143 ` 214` ``` let ``` kleing@29857 ` 215` ``` val pat_consts = get_names pat; ``` kleing@28900 ` 216` kleing@29857 ` 217` ``` fun check (t, NONE) = check (t, SOME (get_thm_names t)) ``` kleing@28900 ` 218` ``` | check ((_, thm), c as SOME thm_consts) = ``` kleing@28900 ` 219` ``` (if pat_consts subset_string thm_consts ``` kleing@28900 ` 220` ``` andalso (Pattern.matches_subterm (ProofContext.theory_of ctxt) ``` kleing@28900 ` 221` ``` (pat, Thm.full_prop_of thm)) ``` kleing@28900 ` 222` ``` then SOME (0, 0) else NONE, c); ``` kleing@28900 ` 223` ``` in check end; ``` wenzelm@16033 ` 224` wenzelm@30142 ` 225` wenzelm@16033 ` 226` ```(* interpret criteria as filters *) ``` wenzelm@16033 ` 227` wenzelm@16036 ` 228` ```local ``` wenzelm@16036 ` 229` wenzelm@16036 ` 230` ```fun err_no_goal c = ``` wenzelm@16036 ` 231` ``` error ("Current goal required for " ^ c ^ " search criterion"); ``` wenzelm@16036 ` 232` kleing@29857 ` 233` ```val fix_goal = Thm.prop_of; ``` kleing@29857 ` 234` ```val fix_goalo = Option.map fix_goal; ``` kleing@29857 ` 235` kleing@28900 ` 236` ```fun filter_crit _ _ (Name name) = apfst (filter_name name) ``` wenzelm@16036 ` 237` ``` | filter_crit _ NONE Intro = err_no_goal "intro" ``` wenzelm@16036 ` 238` ``` | filter_crit _ NONE Elim = err_no_goal "elim" ``` wenzelm@16036 ` 239` ``` | filter_crit _ NONE Dest = err_no_goal "dest" ``` kleing@29857 ` 240` ``` | filter_crit _ NONE Solves = err_no_goal "solves" ``` wenzelm@30143 ` 241` ``` | filter_crit ctxt (SOME goal) Intro = apfst (filter_intro ctxt (fix_goal goal)) ``` wenzelm@30143 ` 242` ``` | filter_crit ctxt (SOME goal) Elim = apfst (filter_elim ctxt (fix_goal goal)) ``` wenzelm@30143 ` 243` ``` | filter_crit ctxt (SOME goal) Dest = apfst (filter_dest ctxt (fix_goal goal)) ``` kleing@29857 ` 244` ``` | filter_crit ctxt (SOME goal) Solves = apfst (filter_solves ctxt goal) ``` kleing@28900 ` 245` ``` | filter_crit ctxt _ (Simp pat) = apfst (filter_simp ctxt pat) ``` kleing@16088 ` 246` ``` | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat; ``` wenzelm@16036 ` 247` wenzelm@19502 ` 248` ```fun opt_not x = if is_some x then NONE else SOME (0, 0); ``` kleing@16895 ` 249` wenzelm@17756 ` 250` ```fun opt_add (SOME (a, x)) (SOME (b, y)) = SOME (Int.max (a, b), x + y : int) ``` wenzelm@26283 ` 251` ``` | opt_add _ _ = NONE; ``` kleing@16895 ` 252` wenzelm@30143 ` 253` ```fun app_filters thm = ``` wenzelm@30143 ` 254` ``` let ``` kleing@28900 ` 255` ``` fun app (NONE, _, _) = NONE ``` kleing@28900 ` 256` ``` | app (SOME v, consts, []) = SOME (v, thm) ``` wenzelm@30143 ` 257` ``` | app (r, consts, f :: fs) = ``` wenzelm@30143 ` 258` ``` let val (r', consts') = f (thm, consts) ``` wenzelm@30143 ` 259` ``` in app (opt_add r r', consts', fs) end; ``` kleing@28900 ` 260` ``` in app end; ``` kleing@28900 ` 261` wenzelm@16036 ` 262` ```in ``` wenzelm@16033 ` 263` wenzelm@16033 ` 264` ```fun filter_criterion ctxt opt_goal (b, c) = ``` kleing@28900 ` 265` ``` (if b then I else (apfst opt_not)) o filter_crit ctxt opt_goal c; ``` kleing@16895 ` 266` kleing@16895 ` 267` ```fun all_filters filters thms = ``` kleing@16895 ` 268` ``` let ``` kleing@28900 ` 269` ``` fun eval_filters thm = app_filters thm (SOME (0, 0), NONE, filters); ``` wenzelm@16033 ` 270` kleing@16895 ` 271` ``` (*filters return: (number of assumptions, substitution size) option, so ``` kleing@16964 ` 272` ``` sort (desc. in both cases) according to number of assumptions first, ``` kleing@16895 ` 273` ``` then by the substitution size*) ``` wenzelm@17205 ` 274` ``` fun thm_ord (((p0, s0), _), ((p1, s1), _)) = ``` wenzelm@17205 ` 275` ``` prod_ord int_ord int_ord ((p1, s1), (p0, s0)); ``` kleing@28900 ` 276` ``` in map_filter eval_filters thms |> sort thm_ord |> map #2 end; ``` wenzelm@16033 ` 277` wenzelm@16036 ` 278` ```end; ``` wenzelm@16036 ` 279` wenzelm@16033 ` 280` kleing@22414 ` 281` ```(* removing duplicates, preferring nicer names, roughly n log n *) ``` kleing@22340 ` 282` wenzelm@25226 ` 283` ```local ``` wenzelm@25226 ` 284` huffman@27486 ` 285` ```val index_ord = option_ord (K EQUAL); ``` wenzelm@25226 ` 286` ```val hidden_ord = bool_ord o pairself NameSpace.is_hidden; ``` wenzelm@25226 ` 287` ```val qual_ord = int_ord o pairself (length o NameSpace.explode); ``` wenzelm@25226 ` 288` ```val txt_ord = int_ord o pairself size; ``` wenzelm@25226 ` 289` huffman@27486 ` 290` ```fun nicer_name (x, i) (y, j) = ``` huffman@27486 ` 291` ``` (case hidden_ord (x, y) of EQUAL => ``` huffman@27486 ` 292` ``` (case index_ord (i, j) of EQUAL => ``` huffman@27486 ` 293` ``` (case qual_ord (x, y) of EQUAL => txt_ord (x, y) | ord => ord) ``` huffman@27486 ` 294` ``` | ord => ord) ``` wenzelm@25226 ` 295` ``` | ord => ord) <> GREATER; ``` wenzelm@25226 ` 296` Timothy@29848 ` 297` ```fun rem_cdups nicer xs = ``` wenzelm@26336 ` 298` ``` let ``` wenzelm@26336 ` 299` ``` fun rem_c rev_seen [] = rev rev_seen ``` wenzelm@26336 ` 300` ``` | rem_c rev_seen [x] = rem_c (x :: rev_seen) [] ``` wenzelm@26336 ` 301` ``` | rem_c rev_seen ((x as ((n, t), _)) :: (y as ((n', t'), _)) :: xs) = ``` wenzelm@26336 ` 302` ``` if Thm.eq_thm_prop (t, t') ``` wenzelm@26336 ` 303` ``` then rem_c rev_seen ((if nicer n n' then x else y) :: xs) ``` wenzelm@26336 ` 304` ``` else rem_c (x :: rev_seen) (y :: xs) ``` wenzelm@26336 ` 305` ``` in rem_c [] xs end; ``` wenzelm@25226 ` 306` wenzelm@26336 ` 307` ```in ``` wenzelm@25226 ` 308` wenzelm@30143 ` 309` ```fun nicer_shortest ctxt = ``` wenzelm@30143 ` 310` ``` let ``` Timothy@29848 ` 311` ``` val ns = ProofContext.theory_of ctxt ``` Timothy@29848 ` 312` ``` |> PureThy.facts_of ``` Timothy@29848 ` 313` ``` |> Facts.space_of; ``` Timothy@29848 ` 314` Timothy@29848 ` 315` ``` val len_sort = sort (int_ord o (pairself size)); ``` Timothy@29848 ` 316` ``` fun shorten s = (case len_sort (NameSpace.get_accesses ns s) of ``` Timothy@29848 ` 317` ``` [] => s ``` Timothy@29848 ` 318` ``` | s'::_ => s'); ``` Timothy@29848 ` 319` Timothy@29848 ` 320` ``` fun nicer (Facts.Named ((x, _), i)) (Facts.Named ((y, _), j)) = ``` Timothy@29848 ` 321` ``` nicer_name (shorten x, i) (shorten y, j) ``` Timothy@29848 ` 322` ``` | nicer (Facts.Fact _) (Facts.Named _) = true ``` Timothy@29848 ` 323` ``` | nicer (Facts.Named _) (Facts.Fact _) = false; ``` Timothy@29848 ` 324` ``` in nicer end; ``` Timothy@29848 ` 325` Timothy@29848 ` 326` ```fun rem_thm_dups nicer xs = ``` wenzelm@26336 ` 327` ``` xs ~~ (1 upto length xs) ``` wenzelm@29269 ` 328` ``` |> sort (TermOrd.fast_term_ord o pairself (Thm.prop_of o #2 o #1)) ``` Timothy@29848 ` 329` ``` |> rem_cdups nicer ``` wenzelm@26336 ` 330` ``` |> sort (int_ord o pairself #2) ``` wenzelm@26336 ` 331` ``` |> map #1; ``` kleing@22340 ` 332` wenzelm@26336 ` 333` ```end; ``` kleing@22340 ` 334` kleing@22340 ` 335` wenzelm@16033 ` 336` ```(* print_theorems *) ``` wenzelm@16033 ` 337` wenzelm@26283 ` 338` ```fun all_facts_of ctxt = ``` wenzelm@26336 ` 339` ``` maps Facts.selections ``` wenzelm@27173 ` 340` ``` (Facts.dest_static [] (PureThy.facts_of (ProofContext.theory_of ctxt)) @ ``` wenzelm@27173 ` 341` ``` Facts.dest_static [] (ProofContext.facts_of ctxt)); ``` wenzelm@17972 ` 342` wenzelm@25992 ` 343` ```val limit = ref 40; ``` wenzelm@25992 ` 344` kleing@29857 ` 345` ```fun find_theorems ctxt opt_goal rem_dups raw_criteria = ``` wenzelm@16033 ` 346` ``` let ``` kleing@29857 ` 347` ``` val add_prems = Seq.hd o (TRY (Method.insert_tac ``` kleing@29857 ` 348` ``` (Assumption.prems_of ctxt) 1)); ``` kleing@29857 ` 349` ``` val opt_goal' = Option.map add_prems opt_goal; ``` kleing@29857 ` 350` wenzelm@16036 ` 351` ``` val criteria = map (apsnd (read_criterion ctxt)) raw_criteria; ``` kleing@29857 ` 352` ``` val filters = map (filter_criterion ctxt opt_goal') criteria; ``` wenzelm@16033 ` 353` wenzelm@26283 ` 354` ``` val raw_matches = all_filters filters (all_facts_of ctxt); ``` kleing@28900 ` 355` wenzelm@22360 ` 356` ``` val matches = ``` kleing@22414 ` 357` ``` if rem_dups ``` Timothy@29848 ` 358` ``` then rem_thm_dups (nicer_shortest ctxt) raw_matches ``` wenzelm@22360 ` 359` ``` else raw_matches; ``` kleing@29857 ` 360` ``` in matches end; ``` kleing@29857 ` 361` wenzelm@30143 ` 362` ```fun print_theorems ctxt opt_goal opt_limit rem_dups raw_criteria = ``` wenzelm@30143 ` 363` ``` let ``` kleing@29857 ` 364` ``` val start = start_timing (); ``` kleing@29857 ` 365` kleing@29857 ` 366` ``` val criteria = map (apsnd (read_criterion ctxt)) raw_criteria; ``` kleing@29857 ` 367` ``` val matches = find_theorems ctxt opt_goal rem_dups raw_criteria; ``` kleing@22340 ` 368` wenzelm@16033 ` 369` ``` val len = length matches; ``` wenzelm@25992 ` 370` ``` val lim = the_default (! limit) opt_limit; ``` wenzelm@25992 ` 371` ``` val thms = Library.drop (len - lim, matches); ``` wenzelm@16033 ` 372` kleing@28900 ` 373` ``` val end_msg = " in " ^ ``` kleing@28900 ` 374` ``` (List.nth (String.tokens Char.isSpace (end_timing start), 3)) ``` kleing@28900 ` 375` ``` ^ " secs" ``` wenzelm@16033 ` 376` ``` in ``` kleing@28900 ` 377` ``` Pretty.big_list "searched for:" (map (pretty_criterion ctxt) criteria) ``` kleing@29857 ` 378` ``` :: Pretty.str "" :: ``` kleing@28900 ` 379` ``` (if null thms then [Pretty.str ("nothing found" ^ end_msg)] ``` wenzelm@16033 ` 380` ``` else ``` wenzelm@16036 ` 381` ``` [Pretty.str ("found " ^ string_of_int len ^ " theorems" ^ ``` kleing@28900 ` 382` ``` (if len <= lim then "" ``` kleing@28900 ` 383` ``` else " (" ^ string_of_int lim ^ " displayed)") ``` kleing@28900 ` 384` ``` ^ end_msg ^ ":"), Pretty.str ""] @ ``` kleing@29858 ` 385` ``` map Display.pretty_fact thms) ``` wenzelm@16033 ` 386` ``` |> Pretty.chunks |> Pretty.writeln ``` wenzelm@30142 ` 387` ``` end; ``` wenzelm@30142 ` 388` wenzelm@30142 ` 389` wenzelm@30142 ` 390` wenzelm@30142 ` 391` ```(** command syntax **) ``` wenzelm@30142 ` 392` wenzelm@30142 ` 393` ```fun find_theorems_cmd ((opt_lim, rem_dups), spec) = ``` wenzelm@30142 ` 394` ``` Toplevel.unknown_theory o Toplevel.keep (fn state => ``` wenzelm@30142 ` 395` ``` let ``` wenzelm@30142 ` 396` ``` val proof_state = Toplevel.enter_proof_body state; ``` wenzelm@30142 ` 397` ``` val ctxt = Proof.context_of proof_state; ``` wenzelm@30142 ` 398` ``` val opt_goal = try Proof.get_goal proof_state |> Option.map (#2 o #2); ``` wenzelm@30142 ` 399` ``` in print_theorems ctxt opt_goal opt_lim rem_dups spec end); ``` wenzelm@30142 ` 400` wenzelm@30142 ` 401` ```local ``` wenzelm@30142 ` 402` wenzelm@30142 ` 403` ```structure P = OuterParse and K = OuterKeyword; ``` wenzelm@30142 ` 404` wenzelm@30142 ` 405` ```val criterion = ``` wenzelm@30142 ` 406` ``` P.reserved "name" |-- P.!!! (P.\$\$\$ ":" |-- P.xname) >> Name || ``` wenzelm@30142 ` 407` ``` P.reserved "intro" >> K Intro || ``` wenzelm@30142 ` 408` ``` P.reserved "elim" >> K Elim || ``` wenzelm@30142 ` 409` ``` P.reserved "dest" >> K Dest || ``` wenzelm@30142 ` 410` ``` P.reserved "solves" >> K Solves || ``` wenzelm@30142 ` 411` ``` P.reserved "simp" |-- P.!!! (P.\$\$\$ ":" |-- P.term) >> Simp || ``` wenzelm@30142 ` 412` ``` P.term >> Pattern; ``` wenzelm@30142 ` 413` wenzelm@30142 ` 414` ```val options = ``` wenzelm@30142 ` 415` ``` Scan.optional ``` wenzelm@30142 ` 416` ``` (P.\$\$\$ "(" |-- ``` wenzelm@30142 ` 417` ``` P.!!! (Scan.option P.nat -- Scan.optional (P.reserved "with_dups" >> K false) true ``` wenzelm@30142 ` 418` ``` --| P.\$\$\$ ")")) (NONE, true); ``` wenzelm@30142 ` 419` ```in ``` wenzelm@30142 ` 420` wenzelm@30142 ` 421` ```val _ = ``` wenzelm@30142 ` 422` ``` OuterSyntax.improper_command "find_theorems" "print theorems meeting specified criteria" K.diag ``` wenzelm@30142 ` 423` ``` (options -- Scan.repeat (((Scan.option P.minus >> is_none) -- criterion)) ``` wenzelm@30142 ` 424` ``` >> (Toplevel.no_timing oo find_theorems_cmd)); ``` wenzelm@16033 ` 425` wenzelm@16033 ` 426` ```end; ``` wenzelm@30142 ` 427` wenzelm@30142 ` 428` ```end; ```