| author | steckerm | 
| Sat, 20 Sep 2014 10:42:08 +0200 | |
| changeset 58401 | b8ca69d9897b | 
| parent 57918 | f5d73caba4e5 | 
| child 58837 | e84d900cd287 | 
| permissions | -rw-r--r-- | 
| 30143 | 1 | (* Title: Pure/Tools/find_theorems.ML | 
| 26283 | 2 | Author: Rafal Kolanski and Gerwin Klein, NICTA | 
| 46718 | 3 | Author: Lars Noschinski and Alexander Krauss, TU Muenchen | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 4 | |
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 5 | Retrieve theorems from proof context. | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 6 | *) | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 7 | |
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 8 | signature FIND_THEOREMS = | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 9 | sig | 
| 16036 | 10 | datatype 'term criterion = | 
| 46717 
b09afce1e54f
removed broken/unused introiff (cf. d452117ba564);
 wenzelm parents: 
46716diff
changeset | 11 | Name of string | Intro | Elim | Dest | Solves | Simp of 'term | Pattern of 'term | 
| 43070 | 12 |   type 'term query = {
 | 
| 13 | goal: thm option, | |
| 14 | limit: int option, | |
| 15 | rem_dups: bool, | |
| 16 | criteria: (bool * 'term criterion) list | |
| 17 | } | |
| 52925 | 18 | val read_query: Position.T -> string -> (bool * string criterion) list | 
| 30785 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 19 | val find_theorems: Proof.context -> thm option -> int option -> bool -> | 
| 43067 | 20 | (bool * term criterion) list -> int option * (Facts.ref * thm) list | 
| 21 | val find_theorems_cmd: Proof.context -> thm option -> int option -> bool -> | |
| 30785 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 22 | (bool * string criterion) list -> int option * (Facts.ref * thm) list | 
| 30186 
1f836e949ac2
replaced archaic Display.pretty_fact by FindTheorems.pretty_thm, which observes the context properly (as did the former prt_fact already);
 wenzelm parents: 
30143diff
changeset | 23 | val pretty_thm: Proof.context -> Facts.ref * thm -> Pretty.T | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 24 | end; | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 25 | |
| 33301 | 26 | structure Find_Theorems: FIND_THEOREMS = | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 27 | struct | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 28 | |
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 29 | (** search criteria **) | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 30 | |
| 16036 | 31 | datatype 'term criterion = | 
| 46717 
b09afce1e54f
removed broken/unused introiff (cf. d452117ba564);
 wenzelm parents: 
46716diff
changeset | 32 | Name of string | Intro | Elim | Dest | Solves | Simp of 'term | Pattern of 'term; | 
| 16036 | 33 | |
| 33036 
c61fe520602b
find_theorems: better handling of abbreviations (by Timothy Bourke)
 kleing parents: 
33029diff
changeset | 34 | fun apply_dummies tm = | 
| 33301 | 35 | let | 
| 36 | val (xs, _) = Term.strip_abs tm; | |
| 37 | val tm' = Term.betapplys (tm, map (Term.dummy_pattern o #2) xs); | |
| 38 | in #1 (Term.replace_dummy_patterns tm' 1) end; | |
| 33036 
c61fe520602b
find_theorems: better handling of abbreviations (by Timothy Bourke)
 kleing parents: 
33029diff
changeset | 39 | |
| 
c61fe520602b
find_theorems: better handling of abbreviations (by Timothy Bourke)
 kleing parents: 
33029diff
changeset | 40 | fun parse_pattern ctxt nm = | 
| 
c61fe520602b
find_theorems: better handling of abbreviations (by Timothy Bourke)
 kleing parents: 
33029diff
changeset | 41 | let | 
| 42360 | 42 | val consts = Proof_Context.consts_of ctxt; | 
| 33301 | 43 | val nm' = | 
| 44 | (case Syntax.parse_term ctxt nm of | |
| 45 | Const (c, _) => c | |
| 46 | | _ => Consts.intern consts nm); | |
| 33036 
c61fe520602b
find_theorems: better handling of abbreviations (by Timothy Bourke)
 kleing parents: 
33029diff
changeset | 47 | in | 
| 33301 | 48 | (case try (Consts.the_abbreviation consts) nm' of | 
| 42360 | 49 | SOME (_, rhs) => apply_dummies (Proof_Context.expand_abbrevs ctxt rhs) | 
| 50 | | NONE => Proof_Context.read_term_pattern ctxt nm) | |
| 33036 
c61fe520602b
find_theorems: better handling of abbreviations (by Timothy Bourke)
 kleing parents: 
33029diff
changeset | 51 | end; | 
| 
c61fe520602b
find_theorems: better handling of abbreviations (by Timothy Bourke)
 kleing parents: 
33029diff
changeset | 52 | |
| 16036 | 53 | fun read_criterion _ (Name name) = Name name | 
| 54 | | read_criterion _ Intro = Intro | |
| 55 | | read_criterion _ Elim = Elim | |
| 56 | | read_criterion _ Dest = Dest | |
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29848diff
changeset | 57 | | read_criterion _ Solves = Solves | 
| 42360 | 58 | | read_criterion ctxt (Simp str) = Simp (Proof_Context.read_term_pattern ctxt str) | 
| 33036 
c61fe520602b
find_theorems: better handling of abbreviations (by Timothy Bourke)
 kleing parents: 
33029diff
changeset | 59 | | read_criterion ctxt (Pattern str) = Pattern (parse_pattern ctxt str); | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 60 | |
| 16036 | 61 | fun pretty_criterion ctxt (b, c) = | 
| 62 | let | |
| 63 | fun prfx s = if b then s else "-" ^ s; | |
| 64 | in | |
| 65 | (case c of | |
| 66 | Name name => Pretty.str (prfx "name: " ^ quote name) | |
| 67 | | Intro => Pretty.str (prfx "intro") | |
| 68 | | Elim => Pretty.str (prfx "elim") | |
| 69 | | Dest => Pretty.str (prfx "dest") | |
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29848diff
changeset | 70 | | Solves => Pretty.str (prfx "solves") | 
| 16088 | 71 | | Simp pat => Pretty.block [Pretty.str (prfx "simp:"), Pretty.brk 1, | 
| 24920 | 72 | Pretty.quote (Syntax.pretty_term ctxt (Term.show_dummy_patterns pat))] | 
| 56914 | 73 | | Pattern pat => Pretty.enclose (prfx "\"") "\"" | 
| 24920 | 74 | [Syntax.pretty_term ctxt (Term.show_dummy_patterns pat)]) | 
| 16036 | 75 | end; | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 76 | |
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 77 | |
| 43620 | 78 | |
| 43070 | 79 | (** queries **) | 
| 80 | ||
| 81 | type 'term query = {
 | |
| 82 | goal: thm option, | |
| 83 | limit: int option, | |
| 84 | rem_dups: bool, | |
| 85 | criteria: (bool * 'term criterion) list | |
| 86 | }; | |
| 87 | ||
| 88 | fun map_criteria f {goal, limit, rem_dups, criteria} =
 | |
| 46718 | 89 |   {goal = goal, limit = limit, rem_dups = rem_dups, criteria = f criteria};
 | 
| 43070 | 90 | |
| 43620 | 91 | |
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 92 | (** search criterion filters **) | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 93 | |
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 94 | (*generated filters are to be of the form | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 95 | input: (Facts.ref * thm) | 
| 53632 | 96 | output: (p:int, s:int, t:int) option, where | 
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 97 | NONE indicates no match | 
| 17106 | 98 | p is the primary sorting criterion | 
| 53632 | 99 | (eg. size of term) | 
| 100 | s is the secondary sorting criterion | |
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 101 | (eg. number of assumptions in the theorem) | 
| 53632 | 102 | t is the tertiary sorting criterion | 
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 103 | (eg. size of the substitution for intro, elim and dest) | 
| 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 104 | when applying a set of filters to a thm, fold results in: | 
| 53632 | 105 | (max p, max s, sum of all t) | 
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 106 | *) | 
| 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 107 | |
| 16088 | 108 | |
| 109 | (* matching theorems *) | |
| 17106 | 110 | |
| 35625 | 111 | fun is_nontrivial thy = Term.is_Const o Term.head_of o Object_Logic.drop_judgment thy; | 
| 16088 | 112 | |
| 16964 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 113 | (*extract terms from term_src, refine them to the parts that concern us, | 
| 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 114 | if po try match them against obj else vice versa. | 
| 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 115 | trivial matches are ignored. | 
| 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 116 | returns: smallest substitution size*) | 
| 46717 
b09afce1e54f
removed broken/unused introiff (cf. d452117ba564);
 wenzelm parents: 
46716diff
changeset | 117 | fun is_matching_thm (extract_terms, refine_term) ctxt po obj term_src = | 
| 16088 | 118 | let | 
| 42360 | 119 | val thy = Proof_Context.theory_of ctxt; | 
| 16088 | 120 | |
| 16486 | 121 | fun matches pat = | 
| 46717 
b09afce1e54f
removed broken/unused introiff (cf. d452117ba564);
 wenzelm parents: 
46716diff
changeset | 122 | is_nontrivial thy pat andalso | 
| 
b09afce1e54f
removed broken/unused introiff (cf. d452117ba564);
 wenzelm parents: 
46716diff
changeset | 123 | Pattern.matches thy (if po then (pat, obj) else (obj, pat)); | 
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 124 | |
| 52940 | 125 | fun subst_size pat = | 
| 18184 | 126 | let val (_, subst) = | 
| 127 | Pattern.match thy (if po then (pat, obj) else (obj, pat)) (Vartab.empty, Vartab.empty) | |
| 17205 | 128 | in Vartab.fold (fn (_, (_, t)) => fn n => size_of_term t + n) subst 0 end; | 
| 16088 | 129 | |
| 52941 | 130 | fun best_match [] = NONE | 
| 131 | | best_match xs = SOME (foldl1 Int.min xs); | |
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 132 | |
| 16964 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 133 | val match_thm = matches o refine_term; | 
| 16486 | 134 | in | 
| 52940 | 135 | map (subst_size o refine_term) (filter match_thm (extract_terms term_src)) | 
| 52941 | 136 | |> best_match | 
| 16088 | 137 | end; | 
| 138 | ||
| 139 | ||
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 140 | (* filter_name *) | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 141 | |
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 142 | fun filter_name str_pat (thmref, _) = | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 143 | if match_string str_pat (Facts.name_of_ref thmref) | 
| 53632 | 144 | then SOME (0, 0, 0) else NONE; | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 145 | |
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 146 | |
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29848diff
changeset | 147 | (* filter intro/elim/dest/solves rules *) | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 148 | |
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 149 | fun filter_dest ctxt goal (_, thm) = | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 150 | let | 
| 16964 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 151 | val extract_dest = | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 152 | (fn thm => if Thm.no_prems thm then [] else [Thm.full_prop_of thm], | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 153 | hd o Logic.strip_imp_prems); | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 154 | val prems = Logic.prems_of_goal goal 1; | 
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 155 | |
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 156 | fun try_subst prem = is_matching_thm extract_dest ctxt true prem thm; | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19476diff
changeset | 157 | val successful = prems |> map_filter try_subst; | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 158 | in | 
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 159 | (*if possible, keep best substitution (one with smallest size)*) | 
| 17106 | 160 | (*dest rules always have assumptions, so a dest with one | 
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 161 | assumption is as good as an intro rule with none*) | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 162 | if not (null successful) then | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 163 | SOME (size_of_term (Thm.prop_of thm), Thm.nprems_of thm - 1, foldl1 Int.min successful) | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 164 | else NONE | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 165 | end; | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 166 | |
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 167 | fun filter_intro ctxt goal (_, thm) = | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 168 | let | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 169 | val extract_intro = (single o Thm.full_prop_of, Logic.strip_imp_concl); | 
| 16036 | 170 | val concl = Logic.concl_of_goal goal 1; | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 171 | in | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 172 | (case is_matching_thm extract_intro ctxt true concl thm of | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 173 | SOME ss => SOME (size_of_term (Thm.prop_of thm), Thm.nprems_of thm, ss) | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 174 | | NONE => NONE) | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 175 | end; | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 176 | |
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 177 | fun filter_elim ctxt goal (_, thm) = | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 178 | if Thm.nprems_of thm > 0 then | 
| 16964 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 179 | let | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 180 | val rule = Thm.full_prop_of thm; | 
| 16964 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 181 | val prems = Logic.prems_of_goal goal 1; | 
| 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 182 | val goal_concl = Logic.concl_of_goal goal 1; | 
| 26283 | 183 | val rule_mp = hd (Logic.strip_imp_prems rule); | 
| 16964 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 184 | val rule_concl = Logic.strip_imp_concl rule; | 
| 57690 | 185 |       fun combine t1 t2 = Const ("*combine*", dummyT --> dummyT) $ (t1 $ t2);  (* FIXME ?!? *)
 | 
| 16964 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 186 | val rule_tree = combine rule_mp rule_concl; | 
| 26283 | 187 | fun goal_tree prem = combine prem goal_concl; | 
| 46717 
b09afce1e54f
removed broken/unused introiff (cf. d452117ba564);
 wenzelm parents: 
46716diff
changeset | 188 | fun try_subst prem = is_matching_thm (single, I) ctxt true (goal_tree prem) rule_tree; | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19476diff
changeset | 189 | val successful = prems |> map_filter try_subst; | 
| 16964 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 190 | in | 
| 32798 | 191 | (*elim rules always have assumptions, so an elim with one | 
| 192 | assumption is as good as an intro rule with none*) | |
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 193 | if is_nontrivial (Proof_Context.theory_of ctxt) (Thm.major_prem_of thm) | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 194 | andalso not (null successful) then | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 195 | SOME (size_of_term (Thm.prop_of thm), Thm.nprems_of thm - 1, foldl1 Int.min successful) | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 196 | else NONE | 
| 16964 
6a25e42eaff5
Ordering is now: first by number of assumptions, second by the substitution size.
 kleing parents: 
16895diff
changeset | 197 | end | 
| 46718 | 198 | else NONE; | 
| 16036 | 199 | |
| 30143 | 200 | fun filter_solves ctxt goal = | 
| 201 | let | |
| 52704 
b824497c8e86
modify background theory where it is actually required (cf. 51dfdcd88e84);
 wenzelm parents: 
52703diff
changeset | 202 | val thy' = | 
| 
b824497c8e86
modify background theory where it is actually required (cf. 51dfdcd88e84);
 wenzelm parents: 
52703diff
changeset | 203 | Proof_Context.theory_of ctxt | 
| 52788 | 204 | |> Context_Position.set_visible_global (Context_Position.is_visible ctxt); | 
| 52704 
b824497c8e86
modify background theory where it is actually required (cf. 51dfdcd88e84);
 wenzelm parents: 
52703diff
changeset | 205 | val ctxt' = Proof_Context.transfer thy' ctxt; | 
| 
b824497c8e86
modify background theory where it is actually required (cf. 51dfdcd88e84);
 wenzelm parents: 
52703diff
changeset | 206 | val goal' = Thm.transfer thy' goal; | 
| 
b824497c8e86
modify background theory where it is actually required (cf. 51dfdcd88e84);
 wenzelm parents: 
52703diff
changeset | 207 | |
| 52941 | 208 | fun limited_etac thm i = | 
| 56613 | 209 |       Seq.take (Options.default_int @{system_option find_theorems_tactic_limit}) o etac thm i;
 | 
| 30143 | 210 | fun try_thm thm = | 
| 52704 
b824497c8e86
modify background theory where it is actually required (cf. 51dfdcd88e84);
 wenzelm parents: 
52703diff
changeset | 211 | if Thm.no_prems thm then rtac thm 1 goal' | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53633diff
changeset | 212 | else | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53633diff
changeset | 213 | (limited_etac thm THEN_ALL_NEW (Goal.norm_hhf_tac ctxt' THEN' Method.assm_tac ctxt')) | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53633diff
changeset | 214 | 1 goal'; | 
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29848diff
changeset | 215 | in | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 216 | fn (_, thm) => | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 217 | if is_some (Seq.pull (try_thm thm)) | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 218 | then SOME (size_of_term (Thm.prop_of thm), Thm.nprems_of thm, 0) | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 219 | else NONE | 
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29848diff
changeset | 220 | end; | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 221 | |
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 222 | |
| 16074 | 223 | (* filter_simp *) | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 224 | |
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 225 | fun filter_simp ctxt t (_, thm) = | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 226 | let | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 227 | val mksimps = Simplifier.mksimps ctxt; | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 228 | val extract_simp = | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 229 | (map Thm.full_prop_of o mksimps, #1 o Logic.dest_equals o Logic.strip_imp_concl); | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 230 | in | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 231 | (case is_matching_thm extract_simp ctxt false t thm of | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 232 | SOME ss => SOME (size_of_term (Thm.prop_of thm), Thm.nprems_of thm, ss) | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 233 | | NONE => NONE) | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 234 | end; | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 235 | |
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 236 | |
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 237 | (* filter_pattern *) | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 238 | |
| 32798 | 239 | fun get_names t = Term.add_const_names t (Term.add_free_names t []); | 
| 28900 
53fd5cc685b4
FindTheorems performance improvements (from Timothy Bourke)
 kleing parents: 
28211diff
changeset | 240 | |
| 52940 | 241 | (*Including all constants and frees is only sound because matching | 
| 242 | uses higher-order patterns. If full matching were used, then | |
| 243 | constants that may be subject to beta-reduction after substitution | |
| 244 | of frees should not be included for LHS set because they could be | |
| 245 | thrown away by the substituted function. E.g. for (?F 1 2) do not | |
| 246 | include 1 or 2, if it were possible for ?F to be (%x y. 3). The | |
| 247 | largest possible set should always be included on the RHS.*) | |
| 30143 | 248 | |
| 249 | fun filter_pattern ctxt pat = | |
| 250 | let | |
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29848diff
changeset | 251 | val pat_consts = get_names pat; | 
| 28900 
53fd5cc685b4
FindTheorems performance improvements (from Timothy Bourke)
 kleing parents: 
28211diff
changeset | 252 | |
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 253 | fun check ((x, thm), NONE) = check ((x, thm), SOME (get_names (Thm.full_prop_of thm))) | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 254 | | check ((_, thm), c as SOME thm_consts) = | 
| 33038 | 255 | (if subset (op =) (pat_consts, thm_consts) andalso | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 256 | Pattern.matches_subterm (Proof_Context.theory_of ctxt) (pat, Thm.full_prop_of thm) | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 257 | then SOME (size_of_term (Thm.prop_of thm), Thm.nprems_of thm, 0) else NONE, c); | 
| 28900 
53fd5cc685b4
FindTheorems performance improvements (from Timothy Bourke)
 kleing parents: 
28211diff
changeset | 258 | in check end; | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 259 | |
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 260 | |
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 261 | (* interpret criteria as filters *) | 
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 262 | |
| 16036 | 263 | local | 
| 264 | ||
| 265 | fun err_no_goal c = | |
| 266 |   error ("Current goal required for " ^ c ^ " search criterion");
 | |
| 267 | ||
| 28900 
53fd5cc685b4
FindTheorems performance improvements (from Timothy Bourke)
 kleing parents: 
28211diff
changeset | 268 | fun filter_crit _ _ (Name name) = apfst (filter_name name) | 
| 16036 | 269 | | filter_crit _ NONE Intro = err_no_goal "intro" | 
| 270 | | filter_crit _ NONE Elim = err_no_goal "elim" | |
| 271 | | filter_crit _ NONE Dest = err_no_goal "dest" | |
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29848diff
changeset | 272 | | filter_crit _ NONE Solves = err_no_goal "solves" | 
| 52940 | 273 | | filter_crit ctxt (SOME goal) Intro = apfst (filter_intro ctxt (Thm.prop_of goal)) | 
| 274 | | filter_crit ctxt (SOME goal) Elim = apfst (filter_elim ctxt (Thm.prop_of goal)) | |
| 275 | | filter_crit ctxt (SOME goal) Dest = apfst (filter_dest ctxt (Thm.prop_of goal)) | |
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29848diff
changeset | 276 | | filter_crit ctxt (SOME goal) Solves = apfst (filter_solves ctxt goal) | 
| 28900 
53fd5cc685b4
FindTheorems performance improvements (from Timothy Bourke)
 kleing parents: 
28211diff
changeset | 277 | | filter_crit ctxt _ (Simp pat) = apfst (filter_simp ctxt pat) | 
| 16088 | 278 | | filter_crit ctxt _ (Pattern pat) = filter_pattern ctxt pat; | 
| 16036 | 279 | |
| 53632 | 280 | fun opt_not x = if is_some x then NONE else SOME (0, 0, 0); | 
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 281 | |
| 53632 | 282 | fun opt_add (SOME (a, c, x)) (SOME (b, d, y)) = SOME (Int.max (a,b), Int.max (c, d), x + y : int) | 
| 26283 | 283 | | opt_add _ _ = NONE; | 
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 284 | |
| 30143 | 285 | fun app_filters thm = | 
| 286 | let | |
| 28900 
53fd5cc685b4
FindTheorems performance improvements (from Timothy Bourke)
 kleing parents: 
28211diff
changeset | 287 | fun app (NONE, _, _) = NONE | 
| 32798 | 288 | | app (SOME v, _, []) = SOME (v, thm) | 
| 30143 | 289 | | app (r, consts, f :: fs) = | 
| 290 | let val (r', consts') = f (thm, consts) | |
| 291 | in app (opt_add r r', consts', fs) end; | |
| 28900 
53fd5cc685b4
FindTheorems performance improvements (from Timothy Bourke)
 kleing parents: 
28211diff
changeset | 292 | in app end; | 
| 
53fd5cc685b4
FindTheorems performance improvements (from Timothy Bourke)
 kleing parents: 
28211diff
changeset | 293 | |
| 16036 | 294 | in | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 295 | |
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 296 | fun filter_criterion ctxt opt_goal (b, c) = | 
| 28900 
53fd5cc685b4
FindTheorems performance improvements (from Timothy Bourke)
 kleing parents: 
28211diff
changeset | 297 | (if b then I else (apfst opt_not)) o filter_crit ctxt opt_goal c; | 
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 298 | |
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 299 | fun sorted_filter filters thms = | 
| 16895 
df67fc190e06
Sort search results in order of relevance, where relevance =
 kleing parents: 
16486diff
changeset | 300 | let | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 301 | fun eval_filters thm = app_filters thm (SOME (0, 0, 0), NONE, filters); | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 302 | |
| 53632 | 303 | (*filters return: (thm size, number of assumptions, substitution size) option, so | 
| 304 | sort according to size of thm first, then number of assumptions, | |
| 305 | then by the substitution size, then by term order *) | |
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 306 | fun result_ord (((p0, s0, t0), (_, thm0)), ((p1, s1, t1), (_, thm1))) = | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 307 | prod_ord int_ord (prod_ord int_ord (prod_ord int_ord Term_Ord.term_ord)) | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 308 | ((p1, (s1, (t1, Thm.full_prop_of thm1))), (p0, (s0, (t0, Thm.full_prop_of thm0)))); | 
| 46977 | 309 | in | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 310 | grouped 100 Par_List.map eval_filters thms | 
| 46977 | 311 | |> map_filter I |> sort result_ord |> map #2 | 
| 312 | end; | |
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 313 | |
| 30822 | 314 | fun lazy_filter filters = | 
| 315 | let | |
| 30785 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 316 | fun lazy_match thms = Seq.make (fn () => first_match thms) | 
| 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 317 | and first_match [] = NONE | 
| 30822 | 318 | | first_match (thm :: thms) = | 
| 53632 | 319 | (case app_filters thm (SOME (0, 0, 0), NONE, filters) of | 
| 30785 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 320 | NONE => first_match thms | 
| 30822 | 321 | | SOME (_, t) => SOME (t, lazy_match thms)); | 
| 30785 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 322 | in lazy_match end; | 
| 30822 | 323 | |
| 16036 | 324 | end; | 
| 325 | ||
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 326 | |
| 52940 | 327 | (* removing duplicates, preferring nicer names, roughly O(n log n) *) | 
| 22340 
275802767bf3
Remove duplicates from printed theorems in find_theorems
 kleing parents: 
19502diff
changeset | 328 | |
| 25226 
502d8676cdd6
improved notion of 'nicer' fact names (observe some name space properties);
 wenzelm parents: 
24920diff
changeset | 329 | local | 
| 
502d8676cdd6
improved notion of 'nicer' fact names (observe some name space properties);
 wenzelm parents: 
24920diff
changeset | 330 | |
| 27486 | 331 | val index_ord = option_ord (K EQUAL); | 
| 55669 | 332 | val hidden_ord = bool_ord o pairself Long_Name.is_hidden; | 
| 56023 | 333 | val qual_ord = int_ord o pairself Long_Name.qualification; | 
| 25226 
502d8676cdd6
improved notion of 'nicer' fact names (observe some name space properties);
 wenzelm parents: 
24920diff
changeset | 334 | val txt_ord = int_ord o pairself size; | 
| 
502d8676cdd6
improved notion of 'nicer' fact names (observe some name space properties);
 wenzelm parents: 
24920diff
changeset | 335 | |
| 27486 | 336 | fun nicer_name (x, i) (y, j) = | 
| 337 | (case hidden_ord (x, y) of EQUAL => | |
| 338 | (case index_ord (i, j) of EQUAL => | |
| 339 | (case qual_ord (x, y) of EQUAL => txt_ord (x, y) | ord => ord) | |
| 340 | | ord => ord) | |
| 25226 
502d8676cdd6
improved notion of 'nicer' fact names (observe some name space properties);
 wenzelm parents: 
24920diff
changeset | 341 | | ord => ord) <> GREATER; | 
| 
502d8676cdd6
improved notion of 'nicer' fact names (observe some name space properties);
 wenzelm parents: 
24920diff
changeset | 342 | |
| 29848 | 343 | fun rem_cdups nicer xs = | 
| 26336 
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
 wenzelm parents: 
26283diff
changeset | 344 | let | 
| 
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
 wenzelm parents: 
26283diff
changeset | 345 | fun rem_c rev_seen [] = rev rev_seen | 
| 
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
 wenzelm parents: 
26283diff
changeset | 346 | | rem_c rev_seen [x] = rem_c (x :: rev_seen) [] | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 347 | | rem_c rev_seen ((x as ((n, thm), _)) :: (y as ((n', thm'), _)) :: rest) = | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 348 | if Thm.eq_thm_prop (thm, thm') | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 349 | then rem_c rev_seen ((if nicer n n' then x else y) :: rest) | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 350 | else rem_c (x :: rev_seen) (y :: rest); | 
| 26336 
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
 wenzelm parents: 
26283diff
changeset | 351 | in rem_c [] xs end; | 
| 25226 
502d8676cdd6
improved notion of 'nicer' fact names (observe some name space properties);
 wenzelm parents: 
24920diff
changeset | 352 | |
| 26336 
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
 wenzelm parents: 
26283diff
changeset | 353 | in | 
| 25226 
502d8676cdd6
improved notion of 'nicer' fact names (observe some name space properties);
 wenzelm parents: 
24920diff
changeset | 354 | |
| 30143 | 355 | fun nicer_shortest ctxt = | 
| 356 | let | |
| 56143 
ed2b660a52a1
more accurate resolution of hybrid facts, which actually changes the sort order of results;
 wenzelm parents: 
56141diff
changeset | 357 | fun extern_shortest name = | 
| 
ed2b660a52a1
more accurate resolution of hybrid facts, which actually changes the sort order of results;
 wenzelm parents: 
56141diff
changeset | 358 | Name_Space.extern_shortest ctxt | 
| 
ed2b660a52a1
more accurate resolution of hybrid facts, which actually changes the sort order of results;
 wenzelm parents: 
56141diff
changeset | 359 | (Facts.space_of (Proof_Context.facts_of_fact ctxt name)) name; | 
| 29848 | 360 | |
| 361 | fun nicer (Facts.Named ((x, _), i)) (Facts.Named ((y, _), j)) = | |
| 55672 
5e25cc741ab9
support for completion within the formal context;
 wenzelm parents: 
55671diff
changeset | 362 | nicer_name (extern_shortest x, i) (extern_shortest y, j) | 
| 29848 | 363 | | nicer (Facts.Fact _) (Facts.Named _) = true | 
| 55670 | 364 | | nicer (Facts.Named _) (Facts.Fact _) = false | 
| 365 | | nicer (Facts.Fact _) (Facts.Fact _) = true; | |
| 29848 | 366 | in nicer end; | 
| 367 | ||
| 368 | fun rem_thm_dups nicer xs = | |
| 52940 | 369 | (xs ~~ (1 upto length xs)) | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 370 | |> sort (Term_Ord.fast_term_ord o pairself (Thm.full_prop_of o #2 o #1)) | 
| 29848 | 371 | |> rem_cdups nicer | 
| 26336 
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
 wenzelm parents: 
26283diff
changeset | 372 | |> sort (int_ord o pairself #2) | 
| 
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
 wenzelm parents: 
26283diff
changeset | 373 | |> map #1; | 
| 22340 
275802767bf3
Remove duplicates from printed theorems in find_theorems
 kleing parents: 
19502diff
changeset | 374 | |
| 26336 
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
 wenzelm parents: 
26283diff
changeset | 375 | end; | 
| 22340 
275802767bf3
Remove duplicates from printed theorems in find_theorems
 kleing parents: 
19502diff
changeset | 376 | |
| 
275802767bf3
Remove duplicates from printed theorems in find_theorems
 kleing parents: 
19502diff
changeset | 377 | |
| 52941 | 378 | |
| 379 | (** main operations **) | |
| 380 | ||
| 381 | (* filter_theorems *) | |
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 382 | |
| 26283 | 383 | fun all_facts_of ctxt = | 
| 33381 | 384 | let | 
| 56158 
c2c6d560e7b2
more explicit treatment of verbose mode, which includes concealed entries;
 wenzelm parents: 
56143diff
changeset | 385 | val local_facts = Proof_Context.facts_of ctxt; | 
| 
c2c6d560e7b2
more explicit treatment of verbose mode, which includes concealed entries;
 wenzelm parents: 
56143diff
changeset | 386 | val global_facts = Global_Theory.facts_of (Proof_Context.theory_of ctxt); | 
| 56141 
c06202417c4a
back to a form of hybrid facts, to reduce performance impact of ed92ce2ac88e;
 wenzelm parents: 
56140diff
changeset | 387 | in | 
| 
c06202417c4a
back to a form of hybrid facts, to reduce performance impact of ed92ce2ac88e;
 wenzelm parents: 
56140diff
changeset | 388 | maps Facts.selections | 
| 56159 | 389 | (Facts.dest_static false [global_facts] local_facts @ | 
| 56158 
c2c6d560e7b2
more explicit treatment of verbose mode, which includes concealed entries;
 wenzelm parents: 
56143diff
changeset | 390 | Facts.dest_static false [] global_facts) | 
| 56141 
c06202417c4a
back to a form of hybrid facts, to reduce performance impact of ed92ce2ac88e;
 wenzelm parents: 
56140diff
changeset | 391 | end; | 
| 17972 | 392 | |
| 43070 | 393 | fun filter_theorems ctxt theorems query = | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 394 | let | 
| 46718 | 395 |     val {goal = opt_goal, limit = opt_limit, rem_dups, criteria} = query;
 | 
| 43069 
88e45168272c
moved questionable goal modification out of filter_theorems
 krauss parents: 
43068diff
changeset | 396 | val filters = map (filter_criterion ctxt opt_goal) criteria; | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 397 | |
| 41844 
b933142e02d0
generalize find_theorems filters to work on raw propositions, too
 krauss parents: 
41841diff
changeset | 398 | fun find_all theorems = | 
| 30785 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 399 | let | 
| 41844 
b933142e02d0
generalize find_theorems filters to work on raw propositions, too
 krauss parents: 
41841diff
changeset | 400 | val raw_matches = sorted_filter filters theorems; | 
| 30785 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 401 | |
| 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 402 | val matches = | 
| 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 403 | if rem_dups | 
| 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 404 | then rem_thm_dups (nicer_shortest ctxt) raw_matches | 
| 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 405 | else raw_matches; | 
| 28900 
53fd5cc685b4
FindTheorems performance improvements (from Timothy Bourke)
 kleing parents: 
28211diff
changeset | 406 | |
| 30785 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 407 | val len = length matches; | 
| 56467 | 408 |         val lim = the_default (Options.default_int @{system_option find_theorems_limit}) opt_limit;
 | 
| 34088 | 409 | in (SOME len, drop (Int.max (len - lim, 0)) matches) end; | 
| 30785 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 410 | |
| 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 411 | val find = | 
| 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 412 | if rem_dups orelse is_none opt_limit | 
| 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 413 | then find_all | 
| 30822 | 414 | else pair NONE o Seq.list_of o Seq.take (the opt_limit) o lazy_filter filters; | 
| 30785 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 415 | |
| 41844 
b933142e02d0
generalize find_theorems filters to work on raw propositions, too
 krauss parents: 
41841diff
changeset | 416 | in find theorems end; | 
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29848diff
changeset | 417 | |
| 46718 | 418 | fun filter_theorems_cmd ctxt theorems raw_query = | 
| 52941 | 419 | filter_theorems ctxt theorems (map_criteria (map (apsnd (read_criterion ctxt))) raw_query); | 
| 420 | ||
| 421 | ||
| 422 | (* find_theorems *) | |
| 423 | ||
| 424 | local | |
| 43067 | 425 | |
| 426 | fun gen_find_theorems filter ctxt opt_goal opt_limit rem_dups raw_criteria = | |
| 43069 
88e45168272c
moved questionable goal modification out of filter_theorems
 krauss parents: 
43068diff
changeset | 427 | let | 
| 
88e45168272c
moved questionable goal modification out of filter_theorems
 krauss parents: 
43068diff
changeset | 428 | val assms = | 
| 
88e45168272c
moved questionable goal modification out of filter_theorems
 krauss parents: 
43068diff
changeset | 429 | Proof_Context.get_fact ctxt (Facts.named "local.assms") | 
| 
88e45168272c
moved questionable goal modification out of filter_theorems
 krauss parents: 
43068diff
changeset | 430 | handle ERROR _ => []; | 
| 
88e45168272c
moved questionable goal modification out of filter_theorems
 krauss parents: 
43068diff
changeset | 431 | val add_prems = Seq.hd o TRY (Method.insert_tac assms 1); | 
| 
88e45168272c
moved questionable goal modification out of filter_theorems
 krauss parents: 
43068diff
changeset | 432 | val opt_goal' = Option.map add_prems opt_goal; | 
| 
88e45168272c
moved questionable goal modification out of filter_theorems
 krauss parents: 
43068diff
changeset | 433 | in | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 434 | filter ctxt (all_facts_of ctxt) | 
| 46718 | 435 |       {goal = opt_goal', limit = opt_limit, rem_dups = rem_dups, criteria = raw_criteria}
 | 
| 43069 
88e45168272c
moved questionable goal modification out of filter_theorems
 krauss parents: 
43068diff
changeset | 436 | end; | 
| 30186 
1f836e949ac2
replaced archaic Display.pretty_fact by FindTheorems.pretty_thm, which observes the context properly (as did the former prt_fact already);
 wenzelm parents: 
30143diff
changeset | 437 | |
| 52941 | 438 | in | 
| 439 | ||
| 43067 | 440 | val find_theorems = gen_find_theorems filter_theorems; | 
| 441 | val find_theorems_cmd = gen_find_theorems filter_theorems_cmd; | |
| 442 | ||
| 52941 | 443 | end; | 
| 444 | ||
| 445 | ||
| 446 | (* pretty_theorems *) | |
| 447 | ||
| 448 | local | |
| 449 | ||
| 49888 | 450 | fun pretty_ref ctxt thmref = | 
| 451 | let | |
| 452 | val (name, sel) = | |
| 453 | (case thmref of | |
| 454 | Facts.Named ((name, _), sel) => (name, sel) | |
| 455 | | Facts.Fact _ => raise Fail "Illegal literal fact"); | |
| 456 | in | |
| 56141 
c06202417c4a
back to a form of hybrid facts, to reduce performance impact of ed92ce2ac88e;
 wenzelm parents: 
56140diff
changeset | 457 | [Pretty.mark (#1 (Proof_Context.markup_extern_fact ctxt name)) (Pretty.str name), | 
| 
c06202417c4a
back to a form of hybrid facts, to reduce performance impact of ed92ce2ac88e;
 wenzelm parents: 
56140diff
changeset | 458 | Pretty.str (Facts.string_of_selection sel), Pretty.str ":", Pretty.brk 1] | 
| 49888 | 459 | end; | 
| 460 | ||
| 52941 | 461 | in | 
| 462 | ||
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 463 | fun pretty_thm ctxt (thmref, thm) = | 
| 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 464 | Pretty.block (pretty_ref ctxt thmref @ [Display.pretty_thm ctxt thm]); | 
| 41845 
6611b9cef38b
reactivate time measurement (partly reverting c27b0b37041a);
 krauss parents: 
41844diff
changeset | 465 | |
| 52941 | 466 | fun pretty_theorems state opt_limit rem_dups raw_criteria = | 
| 30143 | 467 | let | 
| 52941 | 468 | val ctxt = Proof.context_of state; | 
| 469 | val opt_goal = try Proof.simple_goal state |> Option.map #goal; | |
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29848diff
changeset | 470 | val criteria = map (apsnd (read_criterion ctxt)) raw_criteria; | 
| 52941 | 471 | |
| 52940 | 472 | val (opt_found, theorems) = | 
| 55671 
aeca05e62fef
removed remains of old experiment (see b933142e02d0);
 wenzelm parents: 
55670diff
changeset | 473 | filter_theorems ctxt (all_facts_of ctxt) | 
| 52855 | 474 |         {goal = opt_goal, limit = opt_limit, rem_dups = rem_dups, criteria = criteria};
 | 
| 41845 
6611b9cef38b
reactivate time measurement (partly reverting c27b0b37041a);
 krauss parents: 
41844diff
changeset | 475 | val returned = length theorems; | 
| 31684 
d5d830979a54
minor tuning according to Isabelle/ML conventions;
 wenzelm parents: 
31042diff
changeset | 476 | |
| 30785 
15f64e05e703
Limit the number of results returned by auto_solves.
 Timothy Bourke parents: 
30693diff
changeset | 477 | val tally_msg = | 
| 52940 | 478 | (case opt_found of | 
| 38335 | 479 | NONE => "displaying " ^ string_of_int returned ^ " theorem(s)" | 
| 30822 | 480 | | SOME found => | 
| 38335 | 481 | "found " ^ string_of_int found ^ " theorem(s)" ^ | 
| 30822 | 482 | (if returned < found | 
| 483 |              then " (" ^ string_of_int returned ^ " displayed)"
 | |
| 484 | else "")); | |
| 56912 
293cd4dcfebc
some position markup to help locating the query context, e.g. from "Info" dockable;
 wenzelm parents: 
56908diff
changeset | 485 | val position_markup = Position.markup (Position.thread_data ()) Markup.position; | 
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 486 | in | 
| 56891 
48899c43b07d
tuned message -- more context for detached window etc.;
 wenzelm parents: 
56621diff
changeset | 487 | Pretty.block | 
| 56912 
293cd4dcfebc
some position markup to help locating the query context, e.g. from "Info" dockable;
 wenzelm parents: 
56908diff
changeset | 488 | (Pretty.fbreaks | 
| 
293cd4dcfebc
some position markup to help locating the query context, e.g. from "Info" dockable;
 wenzelm parents: 
56908diff
changeset | 489 | (Pretty.mark position_markup (Pretty.keyword1 "find_theorems") :: | 
| 
293cd4dcfebc
some position markup to help locating the query context, e.g. from "Info" dockable;
 wenzelm parents: 
56908diff
changeset | 490 | map (pretty_criterion ctxt) criteria)) :: | 
| 38335 | 491 | Pretty.str "" :: | 
| 56908 
734f7e6151c9
tuned message: more compact, imitate actual command line;
 wenzelm parents: 
56891diff
changeset | 492 | (if null theorems then [Pretty.str "found nothing"] | 
| 38335 | 493 | else | 
| 56908 
734f7e6151c9
tuned message: more compact, imitate actual command line;
 wenzelm parents: 
56891diff
changeset | 494 | Pretty.str (tally_msg ^ ":") :: | 
| 
734f7e6151c9
tuned message: more compact, imitate actual command line;
 wenzelm parents: 
56891diff
changeset | 495 | grouped 10 Par_List.map (Pretty.item o single o pretty_thm ctxt) (rev theorems)) | 
| 52855 | 496 | end |> Pretty.fbreaks |> curry Pretty.blk 0; | 
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 497 | |
| 52941 | 498 | end; | 
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 499 | |
| 32798 | 500 | |
| 46718 | 501 | |
| 52865 
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
 wenzelm parents: 
52863diff
changeset | 502 | (** Isar command syntax **) | 
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 503 | |
| 52941 | 504 | fun proof_state st = | 
| 505 | (case try Toplevel.proof_of st of | |
| 506 | SOME state => state | |
| 507 | | NONE => Proof.init (Toplevel.context_of st)); | |
| 508 | ||
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 509 | local | 
| 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 510 | |
| 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 511 | val criterion = | 
| 36950 | 512 | Parse.reserved "name" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.xname) >> Name || | 
| 513 | Parse.reserved "intro" >> K Intro || | |
| 514 | Parse.reserved "elim" >> K Elim || | |
| 515 | Parse.reserved "dest" >> K Dest || | |
| 516 | Parse.reserved "solves" >> K Solves || | |
| 517 | Parse.reserved "simp" |-- Parse.!!! (Parse.$$$ ":" |-- Parse.term) >> Simp || | |
| 518 | Parse.term >> Pattern; | |
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 519 | |
| 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 520 | val options = | 
| 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 521 | Scan.optional | 
| 36950 | 522 |     (Parse.$$$ "(" |--
 | 
| 523 | Parse.!!! (Scan.option Parse.nat -- Scan.optional (Parse.reserved "with_dups" >> K false) true | |
| 524 | --| Parse.$$$ ")")) (NONE, true); | |
| 52855 | 525 | |
| 52925 | 526 | val query = Scan.repeat ((Scan.option Parse.minus >> is_none) -- criterion); | 
| 52855 | 527 | |
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 528 | in | 
| 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 529 | |
| 52925 | 530 | fun read_query pos str = | 
| 57918 
f5d73caba4e5
tuned signature according to Scala version -- prefer explicit argument;
 wenzelm parents: 
57690diff
changeset | 531 | Outer_Syntax.scan (Keyword.get_lexicons ()) pos str | 
| 52855 | 532 | |> filter Token.is_proper | 
| 52925 | 533 | |> Scan.error (Scan.finite Token.stopper (Parse.!!! (query --| Scan.ahead Parse.eof))) | 
| 534 | |> #1; | |
| 43068 
ac769b5edd1d
exported raw query parser; removed inconsistent clone
 krauss parents: 
43067diff
changeset | 535 | |
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 536 | val _ = | 
| 48646 
91281e9472d8
more official command specifications, including source position;
 wenzelm parents: 
46977diff
changeset | 537 |   Outer_Syntax.improper_command @{command_spec "find_theorems"}
 | 
| 50214 | 538 | "find theorems meeting specified criteria" | 
| 52925 | 539 | (options -- query >> (fn ((opt_lim, rem_dups), spec) => | 
| 52941 | 540 | Toplevel.keep (fn st => | 
| 541 | Pretty.writeln (pretty_theorems (proof_state st) opt_lim rem_dups spec)))); | |
| 16033 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 542 | |
| 
f93ca3d4ffa7
Retrieve theorems from proof context -- improved version of
 wenzelm parents: diff
changeset | 543 | end; | 
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 544 | |
| 52851 
e71b5160f242
minimal print function "find_theorems", which merely echos its arguments;
 wenzelm parents: 
52788diff
changeset | 545 | |
| 
e71b5160f242
minimal print function "find_theorems", which merely echos its arguments;
 wenzelm parents: 
52788diff
changeset | 546 | |
| 52865 
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
 wenzelm parents: 
52863diff
changeset | 547 | (** PIDE query operation **) | 
| 52854 
92932931bd82
more general Output.result: allow to update arbitrary properties;
 wenzelm parents: 
52851diff
changeset | 548 | |
| 52865 
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
 wenzelm parents: 
52863diff
changeset | 549 | val _ = | 
| 52982 
8e78bd316a53
clarified Query_Operation.register: avoid hard-wired parallel policy;
 wenzelm parents: 
52955diff
changeset | 550 |   Query_Operation.register "find_theorems" (fn {state = st, args, output_result} =>
 | 
| 
8e78bd316a53
clarified Query_Operation.register: avoid hard-wired parallel policy;
 wenzelm parents: 
52955diff
changeset | 551 | if can Toplevel.context_of st then | 
| 
8e78bd316a53
clarified Query_Operation.register: avoid hard-wired parallel policy;
 wenzelm parents: 
52955diff
changeset | 552 | let | 
| 56621 
798ba1c2da12
removed odd context argument: Thy_Info.get_theory does not fit into PIDE document model;
 wenzelm parents: 
56613diff
changeset | 553 | val [limit_arg, allow_dups_arg, query_arg] = args; | 
| 
798ba1c2da12
removed odd context argument: Thy_Info.get_theory does not fit into PIDE document model;
 wenzelm parents: 
56613diff
changeset | 554 | val state = proof_state st; | 
| 52982 
8e78bd316a53
clarified Query_Operation.register: avoid hard-wired parallel policy;
 wenzelm parents: 
52955diff
changeset | 555 | val opt_limit = Int.fromString limit_arg; | 
| 
8e78bd316a53
clarified Query_Operation.register: avoid hard-wired parallel policy;
 wenzelm parents: 
52955diff
changeset | 556 | val rem_dups = allow_dups_arg = "false"; | 
| 
8e78bd316a53
clarified Query_Operation.register: avoid hard-wired parallel policy;
 wenzelm parents: 
52955diff
changeset | 557 | val criteria = read_query Position.none query_arg; | 
| 
8e78bd316a53
clarified Query_Operation.register: avoid hard-wired parallel policy;
 wenzelm parents: 
52955diff
changeset | 558 | in output_result (Pretty.string_of (pretty_theorems state opt_limit rem_dups criteria)) end | 
| 
8e78bd316a53
clarified Query_Operation.register: avoid hard-wired parallel policy;
 wenzelm parents: 
52955diff
changeset | 559 | else error "Unknown context"); | 
| 52851 
e71b5160f242
minimal print function "find_theorems", which merely echos its arguments;
 wenzelm parents: 
52788diff
changeset | 560 | |
| 30142 
8d6145694bb5
moved find_theorems.ML and find_consts.ML to Pure/Tools, collecting main implementation in one place each;
 wenzelm parents: 
29882diff
changeset | 561 | end; |