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