| author | wenzelm | 
| Sun, 11 Mar 2012 13:39:16 +0100 | |
| changeset 46867 | 0883804b67bb | 
| parent 46775 | 6287653e63ec | 
| child 47005 | 421760a1efe7 | 
| permissions | -rw-r--r-- | 
| 5824 | 1 | (* Title: Pure/Isar/method.ML | 
| 2 | Author: Markus Wenzel, TU Muenchen | |
| 3 | ||
| 17110 | 4 | Isar proof methods. | 
| 5824 | 5 | *) | 
| 6 | ||
| 7 | signature METHOD = | |
| 8 | sig | |
| 30508 
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
 wenzelm parents: 
30466diff
changeset | 9 | type method | 
| 32193 
c314b4836031
basic method application: avoid Position.setmp_thread_data_seq, which destroys transaction context;
 wenzelm parents: 
32091diff
changeset | 10 | val apply: (Proof.context -> method) -> Proof.context -> thm list -> cases_tactic | 
| 18227 | 11 | val RAW_METHOD_CASES: (thm list -> cases_tactic) -> method | 
| 17110 | 12 | val RAW_METHOD: (thm list -> tactic) -> method | 
| 18227 | 13 | val METHOD_CASES: (thm list -> cases_tactic) -> method | 
| 17110 | 14 | val METHOD: (thm list -> tactic) -> method | 
| 15 | val fail: method | |
| 16 | val succeed: method | |
| 17 | val insert_tac: thm list -> int -> tactic | |
| 18 | val insert: thm list -> method | |
| 19 | val insert_facts: method | |
| 20 | val SIMPLE_METHOD: tactic -> method | |
| 21592 | 21 | val SIMPLE_METHOD': (int -> tactic) -> method | 
| 22 | val SIMPLE_METHOD'': ((int -> tactic) -> tactic) -> (int -> tactic) -> method | |
| 17110 | 23 | val defer: int option -> method | 
| 24 | val prefer: int -> method | |
| 20289 | 25 | val cheating: bool -> Proof.context -> method | 
| 17110 | 26 | val intro: thm list -> method | 
| 27 | val elim: thm list -> method | |
| 20289 | 28 | val unfold: thm list -> Proof.context -> method | 
| 29 | val fold: thm list -> Proof.context -> method | |
| 17110 | 30 | val atomize: bool -> method | 
| 31 | val this: method | |
| 20289 | 32 | val fact: thm list -> Proof.context -> method | 
| 30234 
7dd251bce291
renamed Method.assumption_tac back to Method.assm_tac -- as assumption_tac it would have to be exactly the tactic behind the assumption method (with facts);
 wenzelm parents: 
30190diff
changeset | 33 | val assm_tac: Proof.context -> int -> tactic | 
| 30567 
cd8e20f86795
close/all_assm_tac: finish all subgoals from left to right (cf. Proof.goal_tac a28d83e903ce) -- NB: ALLGOALS/THEN_ALL_NEW operate from right to left;
 wenzelm parents: 
30544diff
changeset | 34 | val all_assm_tac: Proof.context -> tactic | 
| 20289 | 35 | val assumption: Proof.context -> method | 
| 36 | val close: bool -> Proof.context -> method | |
| 46466 
61c7214b4885
tuned signature, according to actual usage of these operations;
 wenzelm parents: 
45375diff
changeset | 37 | val rule_trace: bool Config.T | 
| 20289 | 38 | val trace: Proof.context -> thm list -> unit | 
| 6091 | 39 | val rule_tac: thm list -> thm list -> int -> tactic | 
| 20289 | 40 | val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic | 
| 25270 | 41 | val intros_tac: thm list -> thm list -> tactic | 
| 36093 
0880493627ca
Graceful treatment of non-locale subgoals by methods unfold_locales and intro_locales.
 ballarin parents: 
33522diff
changeset | 42 | val try_intros_tac: thm list -> thm list -> tactic | 
| 17110 | 43 | val rule: thm list -> method | 
| 44 | val erule: int -> thm list -> method | |
| 45 | val drule: int -> thm list -> method | |
| 46 | val frule: int -> thm list -> method | |
| 27235 | 47 | val set_tactic: (thm list -> tactic) -> Proof.context -> Proof.context | 
| 26385 
ae7564661e76
ML runtime compilation: pass position, tuned signature;
 wenzelm parents: 
26291diff
changeset | 48 | val tactic: string * Position.T -> Proof.context -> method | 
| 27235 | 49 | val raw_tactic: string * Position.T -> Proof.context -> method | 
| 27729 | 50 | type src = Args.src | 
| 5824 | 51 | datatype text = | 
| 32193 
c314b4836031
basic method application: avoid Position.setmp_thread_data_seq, which destroys transaction context;
 wenzelm parents: 
32091diff
changeset | 52 | Basic of Proof.context -> method | | 
| 15703 | 53 | Source of src | | 
| 20030 | 54 | Source_i of src | | 
| 5824 | 55 | Then of text list | | 
| 56 | Orelse of text list | | |
| 57 | Try of text | | |
| 19186 | 58 | Repeat1 of text | | 
| 59 | SelectGoals of int * text | |
| 17857 | 60 | val primitive_text: (thm -> thm) -> text | 
| 61 | val succeed_text: text | |
| 17110 | 62 | val default_text: text | 
| 63 | val this_text: text | |
| 64 | val done_text: text | |
| 17356 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 65 | val sorry_text: bool -> text | 
| 32193 
c314b4836031
basic method application: avoid Position.setmp_thread_data_seq, which destroys transaction context;
 wenzelm parents: 
32091diff
changeset | 66 | val finish_text: text option * bool -> text | 
| 27729 | 67 | val print_methods: theory -> unit | 
| 26892 | 68 | val intern: theory -> xstring -> string | 
| 69 | val defined: theory -> string -> bool | |
| 20289 | 70 | val method: theory -> src -> Proof.context -> method | 
| 71 | val method_i: theory -> src -> Proof.context -> method | |
| 30512 | 72 | val syntax: 'a context_parser -> src -> Proof.context -> 'a * Proof.context | 
| 73 | val setup: binding -> (Proof.context -> method) context_parser -> string -> theory -> theory | |
| 30575 | 74 | val method_setup: bstring * Position.T -> Symbol_Pos.text * Position.T -> string -> | 
| 75 | theory -> theory | |
| 30540 | 76 | type modifier = (Proof.context -> Proof.context) * attribute | 
| 77 | val section: modifier parser list -> thm list context_parser | |
| 78 | val sections: modifier parser list -> thm list list context_parser | |
| 30512 | 79 | val parse: text parser | 
| 5824 | 80 | end; | 
| 81 | ||
| 82 | structure Method: METHOD = | |
| 83 | struct | |
| 84 | ||
| 12324 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 85 | (** proof methods **) | 
| 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 86 | |
| 17110 | 87 | (* datatype method *) | 
| 11731 | 88 | |
| 18227 | 89 | datatype method = Meth of thm list -> cases_tactic; | 
| 11731 | 90 | |
| 32193 
c314b4836031
basic method application: avoid Position.setmp_thread_data_seq, which destroys transaction context;
 wenzelm parents: 
32091diff
changeset | 91 | fun apply meth ctxt = let val Meth m = meth ctxt in m end; | 
| 11731 | 92 | |
| 17756 | 93 | val RAW_METHOD_CASES = Meth; | 
| 11731 | 94 | |
| 17110 | 95 | fun RAW_METHOD tac = RAW_METHOD_CASES (NO_CASES o tac); | 
| 12144 | 96 | |
| 17110 | 97 | fun METHOD_CASES tac = RAW_METHOD_CASES (fn facts => | 
| 21687 | 98 | Seq.THEN (ALLGOALS Goal.conjunction_tac, tac facts)); | 
| 8372 | 99 | |
| 21687 | 100 | fun METHOD tac = RAW_METHOD (fn facts => ALLGOALS Goal.conjunction_tac THEN tac facts); | 
| 5824 | 101 | |
| 102 | val fail = METHOD (K no_tac); | |
| 103 | val succeed = METHOD (K all_tac); | |
| 104 | ||
| 105 | ||
| 17110 | 106 | (* insert facts *) | 
| 7419 | 107 | |
| 108 | local | |
| 5824 | 109 | |
| 21579 | 110 | fun cut_rule_tac rule = | 
| 111 | Tactic.rtac (Drule.forall_intr_vars rule COMP_INCR revcut_rl); | |
| 6981 | 112 | |
| 7419 | 113 | in | 
| 5824 | 114 | |
| 7419 | 115 | fun insert_tac [] i = all_tac | 
| 116 | | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts); | |
| 6981 | 117 | |
| 7555 | 118 | val insert_facts = METHOD (ALLGOALS o insert_tac); | 
| 7664 | 119 | fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms)); | 
| 7419 | 120 | |
| 9706 | 121 | fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac); | 
| 21592 | 122 | fun SIMPLE_METHOD'' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac)); | 
| 123 | val SIMPLE_METHOD' = SIMPLE_METHOD'' HEADGOAL; | |
| 9706 | 124 | |
| 12324 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 125 | end; | 
| 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 126 | |
| 9706 | 127 | |
| 17110 | 128 | (* shuffle subgoals *) | 
| 129 | ||
| 130 | fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1))); | |
| 18939 | 131 | fun defer opt_i = METHOD (K (Tactic.defer_tac (the_default 1 opt_i))); | 
| 17110 | 132 | |
| 133 | ||
| 17356 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 134 | (* cheating *) | 
| 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 135 | |
| 32969 | 136 | fun cheating int ctxt = | 
| 137 | if int orelse ! quick_and_dirty then | |
| 42360 | 138 | METHOD (K (Skip_Proof.cheat_tac (Proof_Context.theory_of ctxt))) | 
| 32969 | 139 | else error "Cheating requires quick_and_dirty mode!"; | 
| 17356 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 140 | |
| 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 141 | |
| 17110 | 142 | (* unfold intro/elim rules *) | 
| 143 | ||
| 21592 | 144 | fun intro ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths)); | 
| 145 | fun elim ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths)); | |
| 17110 | 146 | |
| 147 | ||
| 12384 | 148 | (* unfold/fold definitions *) | 
| 149 | ||
| 35624 | 150 | fun unfold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (Local_Defs.unfold_tac ctxt ths)); | 
| 151 | fun fold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (Local_Defs.fold_tac ctxt ths)); | |
| 6532 | 152 | |
| 12384 | 153 | |
| 12829 | 154 | (* atomize rule statements *) | 
| 155 | ||
| 35625 | 156 | fun atomize false = SIMPLE_METHOD' (CHANGED_PROP o Object_Logic.atomize_prems_tac) | 
| 157 | | atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o Object_Logic.full_atomize_tac))); | |
| 12829 | 158 | |
| 159 | ||
| 18039 | 160 | (* this -- resolve facts directly *) | 
| 12384 | 161 | |
| 17110 | 162 | val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac)); | 
| 9484 | 163 | |
| 164 | ||
| 18039 | 165 | (* fact -- composition by facts from context *) | 
| 166 | ||
| 42360 | 167 | fun fact [] ctxt = SIMPLE_METHOD' (Proof_Context.some_fact_tac ctxt) | 
| 168 | | fact rules _ = SIMPLE_METHOD' (Proof_Context.fact_tac rules); | |
| 18039 | 169 | |
| 170 | ||
| 17110 | 171 | (* assumption *) | 
| 7419 | 172 | |
| 173 | local | |
| 174 | ||
| 19778 | 175 | fun cond_rtac cond rule = SUBGOAL (fn (prop, i) => | 
| 176 | if cond (Logic.strip_assums_concl prop) | |
| 177 | then Tactic.rtac rule i else no_tac); | |
| 7419 | 178 | |
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29301diff
changeset | 179 | in | 
| 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29301diff
changeset | 180 | |
| 30234 
7dd251bce291
renamed Method.assumption_tac back to Method.assm_tac -- as assumption_tac it would have to be exactly the tactic behind the assumption method (with facts);
 wenzelm parents: 
30190diff
changeset | 181 | fun assm_tac ctxt = | 
| 17110 | 182 | assume_tac APPEND' | 
| 23349 | 183 | Goal.assume_rule_tac ctxt APPEND' | 
| 19778 | 184 | cond_rtac (can Logic.dest_equals) Drule.reflexive_thm APPEND' | 
| 185 | cond_rtac (can Logic.dest_term) Drule.termI; | |
| 17110 | 186 | |
| 30567 
cd8e20f86795
close/all_assm_tac: finish all subgoals from left to right (cf. Proof.goal_tac a28d83e903ce) -- NB: ALLGOALS/THEN_ALL_NEW operate from right to left;
 wenzelm parents: 
30544diff
changeset | 187 | fun all_assm_tac ctxt st = EVERY1 (replicate (Thm.nprems_of st) (assm_tac ctxt)) st; | 
| 
cd8e20f86795
close/all_assm_tac: finish all subgoals from left to right (cf. Proof.goal_tac a28d83e903ce) -- NB: ALLGOALS/THEN_ALL_NEW operate from right to left;
 wenzelm parents: 
30544diff
changeset | 188 | |
| 23349 | 189 | fun assumption ctxt = METHOD (HEADGOAL o | 
| 30234 
7dd251bce291
renamed Method.assumption_tac back to Method.assm_tac -- as assumption_tac it would have to be exactly the tactic behind the assumption method (with facts);
 wenzelm parents: 
30190diff
changeset | 190 | (fn [] => assm_tac ctxt | 
| 23349 | 191 | | [fact] => solve_tac [fact] | 
| 192 | | _ => K no_tac)); | |
| 193 | ||
| 17356 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 194 | fun close immed ctxt = METHOD (K | 
| 30567 
cd8e20f86795
close/all_assm_tac: finish all subgoals from left to right (cf. Proof.goal_tac a28d83e903ce) -- NB: ALLGOALS/THEN_ALL_NEW operate from right to left;
 wenzelm parents: 
30544diff
changeset | 195 | (FILTER Thm.no_prems ((if immed then all_assm_tac ctxt else all_tac) THEN flexflex_tac))); | 
| 7419 | 196 | |
| 197 | end; | |
| 198 | ||
| 199 | ||
| 17110 | 200 | (* rule etc. -- single-step refinements *) | 
| 12347 | 201 | |
| 42616 
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
 wenzelm parents: 
42380diff
changeset | 202 | val rule_trace = Attrib.setup_config_bool (Binding.name "rule_trace") (fn _ => false); | 
| 12347 | 203 | |
| 17110 | 204 | fun trace ctxt rules = | 
| 41379 | 205 | if Config.get ctxt rule_trace andalso not (null rules) then | 
| 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: 
31304diff
changeset | 206 | Pretty.big_list "rules:" (map (Display.pretty_thm ctxt) rules) | 
| 21962 | 207 | |> Pretty.string_of |> tracing | 
| 208 | else (); | |
| 12347 | 209 | |
| 210 | local | |
| 211 | ||
| 18841 | 212 | fun gen_rule_tac tac rules facts = | 
| 213 | (fn i => fn st => | |
| 214 | if null facts then tac rules i st | |
| 215 | else Seq.maps (fn rule => (tac o single) rule i st) (Drule.multi_resolves facts rules)) | |
| 21687 | 216 | THEN_ALL_NEW Goal.norm_hhf_tac; | 
| 7130 | 217 | |
| 10744 | 218 | fun gen_arule_tac tac j rules facts = | 
| 219 | EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac); | |
| 220 | ||
| 11785 | 221 | fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) => | 
| 222 | let | |
| 223 | val rules = | |
| 224 | if not (null arg_rules) then arg_rules | |
| 33369 | 225 | else flat (Context_Rules.find_rules false facts goal ctxt) | 
| 12055 | 226 | in trace ctxt rules; tac rules facts i end); | 
| 10309 | 227 | |
| 10744 | 228 | fun meth tac x = METHOD (HEADGOAL o tac x); | 
| 229 | fun meth' tac x y = METHOD (HEADGOAL o tac x y); | |
| 8220 | 230 | |
| 7419 | 231 | in | 
| 232 | ||
| 10744 | 233 | val rule_tac = gen_rule_tac Tactic.resolve_tac; | 
| 234 | val rule = meth rule_tac; | |
| 235 | val some_rule_tac = gen_some_rule_tac rule_tac; | |
| 236 | val some_rule = meth' some_rule_tac; | |
| 237 | ||
| 238 | val erule = meth' (gen_arule_tac Tactic.eresolve_tac); | |
| 239 | val drule = meth' (gen_arule_tac Tactic.dresolve_tac); | |
| 240 | val frule = meth' (gen_arule_tac Tactic.forward_tac); | |
| 5824 | 241 | |
| 7419 | 242 | end; | 
| 243 | ||
| 244 | ||
| 25270 | 245 | (* intros_tac -- pervasive search spanned by intro rules *) | 
| 246 | ||
| 36093 
0880493627ca
Graceful treatment of non-locale subgoals by methods unfold_locales and intro_locales.
 ballarin parents: 
33522diff
changeset | 247 | fun gen_intros_tac goals intros facts = | 
| 
0880493627ca
Graceful treatment of non-locale subgoals by methods unfold_locales and intro_locales.
 ballarin parents: 
33522diff
changeset | 248 | goals (insert_tac facts THEN' | 
| 25270 | 249 | REPEAT_ALL_NEW (resolve_tac intros)) | 
| 250 | THEN Tactic.distinct_subgoals_tac; | |
| 251 | ||
| 36093 
0880493627ca
Graceful treatment of non-locale subgoals by methods unfold_locales and intro_locales.
 ballarin parents: 
33522diff
changeset | 252 | val intros_tac = gen_intros_tac ALLGOALS; | 
| 
0880493627ca
Graceful treatment of non-locale subgoals by methods unfold_locales and intro_locales.
 ballarin parents: 
33522diff
changeset | 253 | val try_intros_tac = gen_intros_tac TRYALL; | 
| 25270 | 254 | |
| 37216 
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
 wenzelm parents: 
37198diff
changeset | 255 | |
| 8351 | 256 | (* ML tactics *) | 
| 257 | ||
| 37216 
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
 wenzelm parents: 
37198diff
changeset | 258 | structure ML_Tactic = Proof_Data | 
| 26472 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 259 | ( | 
| 27235 | 260 | type T = thm list -> tactic; | 
| 26472 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 261 | fun init _ = undefined; | 
| 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 262 | ); | 
| 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 263 | |
| 37216 
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
 wenzelm parents: 
37198diff
changeset | 264 | val set_tactic = ML_Tactic.put; | 
| 8351 | 265 | |
| 26472 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 266 | fun ml_tactic (txt, pos) ctxt = | 
| 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 267 | let | 
| 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 268 | val ctxt' = ctxt |> Context.proof_map | 
| 27235 | 269 | (ML_Context.expression pos | 
| 270 | "fun tactic (facts: thm list) : tactic" | |
| 37198 
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
 wenzelm parents: 
36959diff
changeset | 271 | "Context.map_proof (Method.set_tactic tactic)" (ML_Lex.read pos txt)); | 
| 37216 
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
 wenzelm parents: 
37198diff
changeset | 272 | in Context.setmp_thread_data (SOME (Context.Proof ctxt)) (ML_Tactic.get ctxt') end; | 
| 23425 | 273 | |
| 274 | fun tactic txt ctxt = METHOD (ml_tactic txt ctxt); | |
| 275 | fun raw_tactic txt ctxt = RAW_METHOD (ml_tactic txt ctxt); | |
| 8351 | 276 | |
| 277 | ||
| 5824 | 278 | |
| 17110 | 279 | (** method syntax **) | 
| 280 | ||
| 281 | (* method text *) | |
| 282 | ||
| 283 | type src = Args.src; | |
| 5824 | 284 | |
| 17110 | 285 | datatype text = | 
| 32193 
c314b4836031
basic method application: avoid Position.setmp_thread_data_seq, which destroys transaction context;
 wenzelm parents: 
32091diff
changeset | 286 | Basic of Proof.context -> method | | 
| 17110 | 287 | Source of src | | 
| 20030 | 288 | Source_i of src | | 
| 17110 | 289 | Then of text list | | 
| 290 | Orelse of text list | | |
| 291 | Try of text | | |
| 19186 | 292 | Repeat1 of text | | 
| 293 | SelectGoals of int * text; | |
| 17110 | 294 | |
| 32193 
c314b4836031
basic method application: avoid Position.setmp_thread_data_seq, which destroys transaction context;
 wenzelm parents: 
32091diff
changeset | 295 | fun primitive_text r = Basic (K (SIMPLE_METHOD (PRIMITIVE r))); | 
| 
c314b4836031
basic method application: avoid Position.setmp_thread_data_seq, which destroys transaction context;
 wenzelm parents: 
32091diff
changeset | 296 | val succeed_text = Basic (K succeed); | 
| 17110 | 297 | val default_text = Source (Args.src (("default", []), Position.none));
 | 
| 32193 
c314b4836031
basic method application: avoid Position.setmp_thread_data_seq, which destroys transaction context;
 wenzelm parents: 
32091diff
changeset | 298 | val this_text = Basic (K this); | 
| 
c314b4836031
basic method application: avoid Position.setmp_thread_data_seq, which destroys transaction context;
 wenzelm parents: 
32091diff
changeset | 299 | val done_text = Basic (K (SIMPLE_METHOD all_tac)); | 
| 
c314b4836031
basic method application: avoid Position.setmp_thread_data_seq, which destroys transaction context;
 wenzelm parents: 
32091diff
changeset | 300 | fun sorry_text int = Basic (cheating int); | 
| 17110 | 301 | |
| 32193 
c314b4836031
basic method application: avoid Position.setmp_thread_data_seq, which destroys transaction context;
 wenzelm parents: 
32091diff
changeset | 302 | fun finish_text (NONE, immed) = Basic (close immed) | 
| 
c314b4836031
basic method application: avoid Position.setmp_thread_data_seq, which destroys transaction context;
 wenzelm parents: 
32091diff
changeset | 303 | | finish_text (SOME txt, immed) = Then [txt, Basic (close immed)]; | 
| 17110 | 304 | |
| 305 | ||
| 306 | (* method definitions *) | |
| 5824 | 307 | |
| 33522 | 308 | structure Methods = Theory_Data | 
| 22846 | 309 | ( | 
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 310 | type T = ((src -> Proof.context -> method) * string) Name_Space.table; | 
| 33159 | 311 | val empty : T = Name_Space.empty_table "method"; | 
| 16448 | 312 | val extend = I; | 
| 33522 | 313 | fun merge data : T = Name_Space.merge_tables data; | 
| 22846 | 314 | ); | 
| 5824 | 315 | |
| 22846 | 316 | fun print_methods thy = | 
| 317 | let | |
| 42360 | 318 | val ctxt = Proof_Context.init_global thy; | 
| 24116 | 319 | val meths = Methods.get thy; | 
| 42813 
6c841fa92fa2
optional description for 'attribute_setup' and 'method_setup';
 wenzelm parents: 
42616diff
changeset | 320 | fun prt_meth (name, (_, "")) = Pretty.str name | 
| 
6c841fa92fa2
optional description for 'attribute_setup' and 'method_setup';
 wenzelm parents: 
42616diff
changeset | 321 | | prt_meth (name, (_, comment)) = | 
| 
6c841fa92fa2
optional description for 'attribute_setup' and 'method_setup';
 wenzelm parents: 
42616diff
changeset | 322 | Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; | 
| 22846 | 323 | in | 
| 42358 
b47d41d9f4b5
Name_Space: proper configuration options long_names, short_names, unique_names instead of former unsynchronized references;
 wenzelm parents: 
41379diff
changeset | 324 | [Pretty.big_list "methods:" (map prt_meth (Name_Space.extern_table ctxt meths))] | 
| 22846 | 325 | |> Pretty.chunks |> Pretty.writeln | 
| 326 | end; | |
| 7611 | 327 | |
| 33092 
c859019d3ac5
eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
 wenzelm parents: 
32969diff
changeset | 328 | fun add_method name meth comment thy = thy | 
| 42375 
774df7c59508
report Name_Space.declare/define, relatively to context;
 wenzelm parents: 
42360diff
changeset | 329 | |> Methods.map | 
| 
774df7c59508
report Name_Space.declare/define, relatively to context;
 wenzelm parents: 
42360diff
changeset | 330 | (Name_Space.define (Proof_Context.init_global thy) true (Sign.naming_of thy) | 
| 
774df7c59508
report Name_Space.declare/define, relatively to context;
 wenzelm parents: 
42360diff
changeset | 331 | (name, (meth, comment)) #> snd); | 
| 31304 | 332 | |
| 5824 | 333 | |
| 334 | (* get methods *) | |
| 335 | ||
| 33095 
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
 wenzelm parents: 
33092diff
changeset | 336 | val intern = Name_Space.intern o #1 o Methods.get; | 
| 26892 | 337 | val defined = Symtab.defined o #2 o Methods.get; | 
| 338 | ||
| 20030 | 339 | fun method_i thy = | 
| 5824 | 340 | let | 
| 42380 | 341 | val (space, tab) = Methods.get thy; | 
| 5884 | 342 | fun meth src = | 
| 20030 | 343 | let val ((name, _), pos) = Args.dest_src src in | 
| 42380 | 344 | (case Symtab.lookup tab name of | 
| 15531 | 345 |           NONE => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
 | 
| 42380 | 346 | | SOME (mth, _) => (Position.report pos (Name_Space.markup space name); mth src)) | 
| 5824 | 347 | end; | 
| 348 | in meth end; | |
| 349 | ||
| 45228 | 350 | fun method thy = method_i thy o Args.map_name (intern thy); | 
| 20030 | 351 | |
| 5824 | 352 | |
| 30512 | 353 | (* method setup *) | 
| 354 | ||
| 355 | fun syntax scan = Args.context_syntax "method" scan; | |
| 356 | ||
| 31304 | 357 | fun setup name scan = | 
| 358 | add_method name | |
| 359 | (fn src => fn ctxt => let val (m, ctxt') = syntax scan src ctxt in m ctxt' end); | |
| 17356 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 360 | |
| 26385 
ae7564661e76
ML runtime compilation: pass position, tuned signature;
 wenzelm parents: 
26291diff
changeset | 361 | fun method_setup name (txt, pos) cmt = | 
| 26472 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 362 | Context.theory_map (ML_Context.expression pos | 
| 30544 | 363 | "val (name, scan, comment): binding * (Proof.context -> Proof.method) context_parser * string" | 
| 364 | "Context.map_theory (Method.setup name scan comment)" | |
| 37198 
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
 wenzelm parents: 
36959diff
changeset | 365 |     (ML_Lex.read Position.none ("(" ^ ML_Syntax.make_binding name ^ ", ") @
 | 
| 
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
 wenzelm parents: 
36959diff
changeset | 366 | ML_Lex.read pos txt @ | 
| 
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
 wenzelm parents: 
36959diff
changeset | 367 |       ML_Lex.read Position.none (", " ^ ML_Syntax.print_string cmt ^ ")")));
 | 
| 17356 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 368 | |
| 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 369 | |
| 5884 | 370 | |
| 17110 | 371 | (** concrete syntax **) | 
| 5824 | 372 | |
| 5884 | 373 | (* sections *) | 
| 5824 | 374 | |
| 20289 | 375 | type modifier = (Proof.context -> Proof.context) * attribute; | 
| 7268 | 376 | |
| 377 | local | |
| 378 | ||
| 24010 
2ef318813e1a
method section scanners: added [[declaration]] syntax, ignore sid-effects of thms;
 wenzelm parents: 
23937diff
changeset | 379 | fun thms ss = Scan.repeat (Scan.unless (Scan.lift (Scan.first ss)) Attrib.multi_thm) >> flat; | 
| 46775 
6287653e63ec
canonical argument order for attribute application;
 wenzelm parents: 
46466diff
changeset | 380 | fun app (f, att) ths context = fold_map (Thm.apply_attribute att) ths (Context.map_proof f context); | 
| 5824 | 381 | |
| 30540 | 382 | in | 
| 383 | ||
| 24022 | 384 | fun section ss = Scan.depend (fn context => (Scan.first ss -- Scan.pass context (thms ss)) :|-- | 
| 46775 
6287653e63ec
canonical argument order for attribute application;
 wenzelm parents: 
46466diff
changeset | 385 | (fn (m, ths) => Scan.succeed (swap (app m ths context)))); | 
| 5884 | 386 | |
| 30540 | 387 | fun sections ss = Scan.repeat (section ss); | 
| 5824 | 388 | |
| 7268 | 389 | end; | 
| 390 | ||
| 5824 | 391 | |
| 30515 | 392 | (* extra rule methods *) | 
| 393 | ||
| 394 | fun xrule_meth m = | |
| 36950 | 395 | Scan.lift (Scan.optional (Args.parens Parse.nat) 0) -- Attrib.thms >> | 
| 30515 | 396 | (fn (n, ths) => K (m n ths)); | 
| 397 | ||
| 398 | ||
| 27813 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 399 | (* outer parser *) | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 400 | |
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 401 | fun is_symid_meth s = | 
| 36959 
f5417836dbea
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
 wenzelm parents: 
36950diff
changeset | 402 | s <> "|" andalso s <> "?" andalso s <> "+" andalso Token.ident_or_symbolic s; | 
| 27813 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 403 | |
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 404 | local | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 405 | |
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 406 | fun meth4 x = | 
| 36950 | 407 | (Parse.position (Parse.xname >> rpair []) >> (Source o Args.src) || | 
| 408 |   Parse.$$$ "(" |-- Parse.!!! (meth0 --| Parse.$$$ ")")) x
 | |
| 27813 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 409 | and meth3 x = | 
| 36950 | 410 | (meth4 --| Parse.$$$ "?" >> Try || | 
| 411 | meth4 --| Parse.$$$ "+" >> Repeat1 || | |
| 412 | meth4 -- (Parse.$$$ "[" |-- Scan.optional Parse.nat 1 --| Parse.$$$ "]") >> (SelectGoals o swap) || | |
| 27813 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 413 | meth4) x | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 414 | and meth2 x = | 
| 36950 | 415 | (Parse.position (Parse.xname -- Args.parse1 is_symid_meth) >> (Source o Args.src) || | 
| 27813 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 416 | meth3) x | 
| 36950 | 417 | and meth1 x = (Parse.enum1 "," meth2 >> (fn [m] => m | ms => Then ms)) x | 
| 418 | and meth0 x = (Parse.enum1 "|" meth1 >> (fn [m] => m | ms => Orelse ms)) x; | |
| 27813 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 419 | |
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 420 | in val parse = meth3 end; | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 421 | |
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 422 | |
| 18708 | 423 | (* theory setup *) | 
| 5824 | 424 | |
| 26463 | 425 | val _ = Context.>> (Context.map_theory | 
| 42616 
92715b528e78
added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
 wenzelm parents: 
42380diff
changeset | 426 | (setup (Binding.name "fail") (Scan.succeed (K fail)) "force failure" #> | 
| 30515 | 427 | setup (Binding.name "succeed") (Scan.succeed (K succeed)) "succeed" #> | 
| 428 | setup (Binding.name "-") (Scan.succeed (K insert_facts)) | |
| 429 | "do nothing (insert current facts only)" #> | |
| 430 | setup (Binding.name "insert") (Attrib.thms >> (K o insert)) | |
| 431 | "insert theorems, ignoring facts (improper)" #> | |
| 432 | setup (Binding.name "intro") (Attrib.thms >> (K o intro)) | |
| 433 | "repeatedly apply introduction rules" #> | |
| 434 | setup (Binding.name "elim") (Attrib.thms >> (K o elim)) | |
| 435 | "repeatedly apply elimination rules" #> | |
| 436 | setup (Binding.name "unfold") (Attrib.thms >> unfold_meth) "unfold definitions" #> | |
| 437 | setup (Binding.name "fold") (Attrib.thms >> fold_meth) "fold definitions" #> | |
| 438 | setup (Binding.name "atomize") (Args.mode "full" >> (K o atomize)) | |
| 439 | "present local premises as object-level statements" #> | |
| 440 | setup (Binding.name "rule") (Attrib.thms >> some_rule) "apply some intro/elim rule" #> | |
| 441 | setup (Binding.name "erule") (xrule_meth erule) "apply rule in elimination manner (improper)" #> | |
| 442 | setup (Binding.name "drule") (xrule_meth drule) "apply rule in destruct manner (improper)" #> | |
| 443 | setup (Binding.name "frule") (xrule_meth frule) "apply rule in forward manner (improper)" #> | |
| 444 | setup (Binding.name "this") (Scan.succeed (K this)) "apply current facts as rules" #> | |
| 445 | setup (Binding.name "fact") (Attrib.thms >> fact) "composition by facts from context" #> | |
| 446 | setup (Binding.name "assumption") (Scan.succeed assumption) | |
| 447 | "proof by assumption, preferring facts" #> | |
| 448 | setup (Binding.name "rename_tac") (Args.goal_spec -- Scan.lift (Scan.repeat1 Args.name) >> | |
| 449 | (fn (quant, xs) => K (SIMPLE_METHOD'' quant (Tactic.rename_tac xs)))) | |
| 450 | "rename parameters of goal" #> | |
| 36950 | 451 | setup (Binding.name "rotate_tac") (Args.goal_spec -- Scan.lift (Scan.optional Parse.int 1) >> | 
| 30515 | 452 | (fn (quant, i) => K (SIMPLE_METHOD'' quant (Tactic.rotate_tac i)))) | 
| 453 | "rotate assumptions of goal" #> | |
| 37198 
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
 wenzelm parents: 
36959diff
changeset | 454 | setup (Binding.name "tactic") (Scan.lift Args.name_source_position >> tactic) | 
| 30515 | 455 | "ML tactic as proof method" #> | 
| 37198 
3af985b10550
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
 wenzelm parents: 
36959diff
changeset | 456 | setup (Binding.name "raw_tactic") (Scan.lift Args.name_source_position >> raw_tactic) | 
| 30515 | 457 | "ML tactic as raw proof method")); | 
| 5824 | 458 | |
| 459 | ||
| 16145 | 460 | (*final declarations of this structure!*) | 
| 461 | val unfold = unfold_meth; | |
| 462 | val fold = fold_meth; | |
| 463 | ||
| 5824 | 464 | end; | 
| 465 | ||
| 30508 
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
 wenzelm parents: 
30466diff
changeset | 466 | val RAW_METHOD_CASES = Method.RAW_METHOD_CASES; | 
| 
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
 wenzelm parents: 
30466diff
changeset | 467 | val RAW_METHOD = Method.RAW_METHOD; | 
| 
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
 wenzelm parents: 
30466diff
changeset | 468 | val METHOD_CASES = Method.METHOD_CASES; | 
| 
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
 wenzelm parents: 
30466diff
changeset | 469 | val METHOD = Method.METHOD; | 
| 
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
 wenzelm parents: 
30466diff
changeset | 470 | val SIMPLE_METHOD = Method.SIMPLE_METHOD; | 
| 
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
 wenzelm parents: 
30466diff
changeset | 471 | val SIMPLE_METHOD' = Method.SIMPLE_METHOD'; | 
| 
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
 wenzelm parents: 
30466diff
changeset | 472 | val SIMPLE_METHOD'' = Method.SIMPLE_METHOD''; | 
| 
958cc116d03b
tuned Method exports: non-pervasive type method (cf. Proof.method), pervasive METHOD combinators;
 wenzelm parents: 
30466diff
changeset | 473 |