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