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