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