| author | wenzelm | 
| Thu, 12 Mar 2009 21:29:04 +0100 | |
| changeset 30480 | f3421e8379ab | 
| parent 30466 | 5f31e24937c5 | 
| child 30508 | 958cc116d03b | 
| 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 BASIC_METHOD = | |
| 8 | sig | |
| 17110 | 9 | val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq | 
| 10 | val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq | |
| 11 | type method | |
| 11731 | 12 | val trace_rules: bool ref | 
| 5824 | 13 | end; | 
| 14 | ||
| 15 | signature METHOD = | |
| 16 | sig | |
| 17 | include BASIC_METHOD | |
| 25957 
2cfb703fa8d8
improved apply: handle thread position, apply to context here;
 wenzelm parents: 
25699diff
changeset | 18 | val apply: Position.T -> (Proof.context -> method) -> Proof.context -> thm list -> cases_tactic | 
| 18227 | 19 | val RAW_METHOD_CASES: (thm list -> cases_tactic) -> method | 
| 17110 | 20 | val RAW_METHOD: (thm list -> tactic) -> method | 
| 18227 | 21 | val METHOD_CASES: (thm list -> cases_tactic) -> method | 
| 17110 | 22 | val METHOD: (thm list -> tactic) -> method | 
| 23 | val fail: method | |
| 24 | val succeed: method | |
| 25 | val insert_tac: thm list -> int -> tactic | |
| 26 | val insert: thm list -> method | |
| 27 | val insert_facts: method | |
| 28 | val SIMPLE_METHOD: tactic -> method | |
| 21592 | 29 | val SIMPLE_METHOD': (int -> tactic) -> method | 
| 30 | val SIMPLE_METHOD'': ((int -> tactic) -> tactic) -> (int -> tactic) -> method | |
| 17110 | 31 | val defer: int option -> method | 
| 32 | val prefer: int -> method | |
| 20289 | 33 | val cheating: bool -> Proof.context -> method | 
| 17110 | 34 | val intro: thm list -> method | 
| 35 | val elim: thm list -> method | |
| 20289 | 36 | val unfold: thm list -> Proof.context -> method | 
| 37 | val fold: thm list -> Proof.context -> method | |
| 17110 | 38 | val atomize: bool -> method | 
| 39 | val this: method | |
| 20289 | 40 | 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 | 41 | val assm_tac: Proof.context -> int -> tactic | 
| 20289 | 42 | val assumption: Proof.context -> method | 
| 43 | val close: bool -> Proof.context -> method | |
| 44 | val trace: Proof.context -> thm list -> unit | |
| 6091 | 45 | val rule_tac: thm list -> thm list -> int -> tactic | 
| 20289 | 46 | val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic | 
| 25270 | 47 | val intros_tac: thm list -> thm list -> tactic | 
| 17110 | 48 | val rule: thm list -> method | 
| 49 | val erule: int -> thm list -> method | |
| 50 | val drule: int -> thm list -> method | |
| 51 | val frule: int -> thm list -> method | |
| 27235 | 52 | val set_tactic: (thm list -> tactic) -> Proof.context -> Proof.context | 
| 26385 
ae7564661e76
ML runtime compilation: pass position, tuned signature;
 wenzelm parents: 
26291diff
changeset | 53 | val tactic: string * Position.T -> Proof.context -> method | 
| 27235 | 54 | val raw_tactic: string * Position.T -> Proof.context -> method | 
| 27729 | 55 | type src = Args.src | 
| 5824 | 56 | datatype text = | 
| 23349 | 57 | Basic of (Proof.context -> method) * Position.T | | 
| 15703 | 58 | Source of src | | 
| 20030 | 59 | Source_i of src | | 
| 5824 | 60 | Then of text list | | 
| 61 | Orelse of text list | | |
| 62 | Try of text | | |
| 19186 | 63 | Repeat1 of text | | 
| 64 | SelectGoals of int * text | |
| 17857 | 65 | val primitive_text: (thm -> thm) -> text | 
| 66 | val succeed_text: text | |
| 17110 | 67 | val default_text: text | 
| 68 | val this_text: text | |
| 69 | val done_text: text | |
| 17356 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 70 | val sorry_text: bool -> text | 
| 23349 | 71 | val finish_text: text option * bool -> Position.T -> text | 
| 27729 | 72 | val print_methods: theory -> unit | 
| 26892 | 73 | val intern: theory -> xstring -> string | 
| 74 | val defined: theory -> string -> bool | |
| 20289 | 75 | val method: theory -> src -> Proof.context -> method | 
| 76 | val method_i: theory -> src -> Proof.context -> method | |
| 77 | val add_methods: (bstring * (src -> Proof.context -> method) * string) list | |
| 17110 | 78 | -> theory -> theory | 
| 20289 | 79 | val add_method: bstring * (src -> Proof.context -> method) * string | 
| 17110 | 80 | -> theory -> theory | 
| 26385 
ae7564661e76
ML runtime compilation: pass position, tuned signature;
 wenzelm parents: 
26291diff
changeset | 81 | val method_setup: bstring -> string * Position.T -> string -> theory -> theory | 
| 18999 | 82 | val syntax: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) | 
| 21879 | 83 | -> src -> Proof.context -> 'a * Proof.context | 
| 17110 | 84 | val simple_args: (Args.T list -> 'a * Args.T list) | 
| 20289 | 85 |     -> ('a -> Proof.context -> method) -> src -> Proof.context -> method
 | 
| 86 | val ctxt_args: (Proof.context -> method) -> src -> Proof.context -> method | |
| 87 | val no_args: method -> src -> Proof.context -> method | |
| 17110 | 88 | type modifier | 
| 18999 | 89 | val sectioned_args: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) -> | 
| 17110 | 90 | (Args.T list -> modifier * Args.T list) list -> | 
| 20289 | 91 |     ('a -> Proof.context -> 'b) -> src -> Proof.context -> 'b
 | 
| 17110 | 92 | val bang_sectioned_args: | 
| 93 | (Args.T list -> modifier * Args.T list) list -> | |
| 20289 | 94 | (thm list -> Proof.context -> 'a) -> src -> Proof.context -> 'a | 
| 17110 | 95 | val bang_sectioned_args': | 
| 96 | (Args.T list -> modifier * Args.T list) list -> | |
| 18999 | 97 | (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) -> | 
| 20289 | 98 |     ('a -> thm list -> Proof.context -> 'b) -> src -> Proof.context -> 'b
 | 
| 17110 | 99 | val only_sectioned_args: | 
| 100 | (Args.T list -> modifier * Args.T list) list -> | |
| 20289 | 101 | (Proof.context -> 'a) -> src -> Proof.context -> 'a | 
| 102 | val thms_ctxt_args: (thm list -> Proof.context -> 'a) -> src -> | |
| 103 | Proof.context -> 'a | |
| 104 | val thms_args: (thm list -> 'a) -> src -> Proof.context -> 'a | |
| 105 | val thm_args: (thm -> 'a) -> src -> Proof.context -> 'a | |
| 9539 | 106 |   val goal_args: (Args.T list -> 'a * Args.T list) -> ('a -> int -> tactic)
 | 
| 20289 | 107 | -> src -> Proof.context -> method | 
| 18999 | 108 | val goal_args': (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) | 
| 20289 | 109 |     -> ('a -> int -> tactic) -> src -> Proof.context -> method
 | 
| 17110 | 110 | val goal_args_ctxt: (Args.T list -> 'a * Args.T list) -> | 
| 20289 | 111 | (Proof.context -> 'a -> int -> tactic) -> src -> Proof.context -> method | 
| 18999 | 112 | val goal_args_ctxt': (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) -> | 
| 20289 | 113 | (Proof.context -> 'a -> int -> tactic) -> src -> Proof.context -> method | 
| 22118 | 114 | val parse: OuterLex.token list -> text * OuterLex.token list | 
| 5824 | 115 | end; | 
| 116 | ||
| 117 | structure Method: METHOD = | |
| 118 | struct | |
| 119 | ||
| 17110 | 120 | (** generic tools **) | 
| 121 | ||
| 122 | (* goal addressing *) | |
| 123 | ||
| 124 | fun FINDGOAL tac st = | |
| 125 | let fun find i n = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1) n) | |
| 126 | in find 1 (Thm.nprems_of st) st end; | |
| 127 | ||
| 128 | fun HEADGOAL tac = tac 1; | |
| 129 | ||
| 130 | ||
| 5824 | 131 | |
| 12324 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 132 | (** proof methods **) | 
| 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 133 | |
| 17110 | 134 | (* datatype method *) | 
| 11731 | 135 | |
| 18227 | 136 | datatype method = Meth of thm list -> cases_tactic; | 
| 11731 | 137 | |
| 25957 
2cfb703fa8d8
improved apply: handle thread position, apply to context here;
 wenzelm parents: 
25699diff
changeset | 138 | fun apply pos meth_fun ctxt facts goal = Position.setmp_thread_data_seq pos | 
| 
2cfb703fa8d8
improved apply: handle thread position, apply to context here;
 wenzelm parents: 
25699diff
changeset | 139 | (fn () => let val Meth meth = meth_fun ctxt in meth facts goal end) (); | 
| 11731 | 140 | |
| 17756 | 141 | val RAW_METHOD_CASES = Meth; | 
| 11731 | 142 | |
| 17110 | 143 | fun RAW_METHOD tac = RAW_METHOD_CASES (NO_CASES o tac); | 
| 12144 | 144 | |
| 17110 | 145 | fun METHOD_CASES tac = RAW_METHOD_CASES (fn facts => | 
| 21687 | 146 | Seq.THEN (ALLGOALS Goal.conjunction_tac, tac facts)); | 
| 8372 | 147 | |
| 21687 | 148 | fun METHOD tac = RAW_METHOD (fn facts => ALLGOALS Goal.conjunction_tac THEN tac facts); | 
| 5824 | 149 | |
| 150 | val fail = METHOD (K no_tac); | |
| 151 | val succeed = METHOD (K all_tac); | |
| 152 | ||
| 153 | ||
| 17110 | 154 | (* insert facts *) | 
| 7419 | 155 | |
| 156 | local | |
| 5824 | 157 | |
| 21579 | 158 | fun cut_rule_tac rule = | 
| 159 | Tactic.rtac (Drule.forall_intr_vars rule COMP_INCR revcut_rl); | |
| 6981 | 160 | |
| 7419 | 161 | in | 
| 5824 | 162 | |
| 7419 | 163 | fun insert_tac [] i = all_tac | 
| 164 | | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts); | |
| 6981 | 165 | |
| 7555 | 166 | val insert_facts = METHOD (ALLGOALS o insert_tac); | 
| 7664 | 167 | fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms)); | 
| 7419 | 168 | |
| 9706 | 169 | fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac); | 
| 21592 | 170 | fun SIMPLE_METHOD'' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac)); | 
| 171 | val SIMPLE_METHOD' = SIMPLE_METHOD'' HEADGOAL; | |
| 9706 | 172 | |
| 12324 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 173 | end; | 
| 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 174 | |
| 9706 | 175 | |
| 17110 | 176 | (* shuffle subgoals *) | 
| 177 | ||
| 178 | fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1))); | |
| 18939 | 179 | fun defer opt_i = METHOD (K (Tactic.defer_tac (the_default 1 opt_i))); | 
| 17110 | 180 | |
| 181 | ||
| 17356 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 182 | (* cheating *) | 
| 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 183 | |
| 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 184 | fun cheating int ctxt = METHOD (K (setmp quick_and_dirty (int orelse ! quick_and_dirty) | 
| 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 185 | (SkipProof.cheat_tac (ProofContext.theory_of ctxt)))); | 
| 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 186 | |
| 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 187 | |
| 17110 | 188 | (* unfold intro/elim rules *) | 
| 189 | ||
| 21592 | 190 | fun intro ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths)); | 
| 191 | fun elim ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths)); | |
| 17110 | 192 | |
| 193 | ||
| 12384 | 194 | (* unfold/fold definitions *) | 
| 195 | ||
| 18877 | 196 | fun unfold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.unfold_tac ctxt ths)); | 
| 197 | fun fold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.fold_tac ctxt ths)); | |
| 6532 | 198 | |
| 12384 | 199 | |
| 12829 | 200 | (* atomize rule statements *) | 
| 201 | ||
| 23590 
ad95084a5c63
renamed ObjectLogic.atomize_tac to ObjectLogic.atomize_prems_tac;
 wenzelm parents: 
23577diff
changeset | 202 | fun atomize false = SIMPLE_METHOD' (CHANGED_PROP o ObjectLogic.atomize_prems_tac) | 
| 12829 | 203 | | atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o ObjectLogic.full_atomize_tac))); | 
| 204 | ||
| 205 | ||
| 18039 | 206 | (* this -- resolve facts directly *) | 
| 12384 | 207 | |
| 17110 | 208 | val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac)); | 
| 9484 | 209 | |
| 210 | ||
| 18039 | 211 | (* fact -- composition by facts from context *) | 
| 212 | ||
| 21592 | 213 | fun fact [] ctxt = SIMPLE_METHOD' (ProofContext.some_fact_tac ctxt) | 
| 214 | | fact rules _ = SIMPLE_METHOD' (ProofContext.fact_tac rules); | |
| 18039 | 215 | |
| 216 | ||
| 17110 | 217 | (* assumption *) | 
| 7419 | 218 | |
| 219 | local | |
| 220 | ||
| 19778 | 221 | fun cond_rtac cond rule = SUBGOAL (fn (prop, i) => | 
| 222 | if cond (Logic.strip_assums_concl prop) | |
| 223 | then Tactic.rtac rule i else no_tac); | |
| 7419 | 224 | |
| 29857 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29301diff
changeset | 225 | in | 
| 
2cc976ed8a3c
FindTheorems: add solves feature; tidy up const name subsettin; patch by Timothy Bourke
 kleing parents: 
29301diff
changeset | 226 | |
| 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 | 227 | fun assm_tac ctxt = | 
| 17110 | 228 | assume_tac APPEND' | 
| 23349 | 229 | Goal.assume_rule_tac ctxt APPEND' | 
| 19778 | 230 | cond_rtac (can Logic.dest_equals) Drule.reflexive_thm APPEND' | 
| 231 | cond_rtac (can Logic.dest_term) Drule.termI; | |
| 17110 | 232 | |
| 23349 | 233 | 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 | 234 | (fn [] => assm_tac ctxt | 
| 23349 | 235 | | [fact] => solve_tac [fact] | 
| 236 | | _ => K no_tac)); | |
| 237 | ||
| 17356 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 238 | fun close immed ctxt = METHOD (K | 
| 23349 | 239 | (FILTER Thm.no_prems | 
| 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 | 240 | ((if immed then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac))); | 
| 7419 | 241 | |
| 242 | end; | |
| 243 | ||
| 244 | ||
| 17110 | 245 | (* rule etc. -- single-step refinements *) | 
| 12347 | 246 | |
| 17110 | 247 | val trace_rules = ref false; | 
| 12347 | 248 | |
| 17110 | 249 | fun trace ctxt rules = | 
| 21962 | 250 | if ! trace_rules andalso not (null rules) then | 
| 17110 | 251 | Pretty.big_list "rules:" (map (ProofContext.pretty_thm ctxt) rules) | 
| 21962 | 252 | |> Pretty.string_of |> tracing | 
| 253 | else (); | |
| 12347 | 254 | |
| 255 | local | |
| 256 | ||
| 18841 | 257 | fun gen_rule_tac tac rules facts = | 
| 258 | (fn i => fn st => | |
| 259 | if null facts then tac rules i st | |
| 260 | else Seq.maps (fn rule => (tac o single) rule i st) (Drule.multi_resolves facts rules)) | |
| 21687 | 261 | THEN_ALL_NEW Goal.norm_hhf_tac; | 
| 7130 | 262 | |
| 10744 | 263 | fun gen_arule_tac tac j rules facts = | 
| 264 | EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac); | |
| 265 | ||
| 11785 | 266 | fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) => | 
| 267 | let | |
| 268 | val rules = | |
| 269 | if not (null arg_rules) then arg_rules | |
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19307diff
changeset | 270 | else flat (ContextRules.find_rules false facts goal ctxt) | 
| 12055 | 271 | in trace ctxt rules; tac rules facts i end); | 
| 10309 | 272 | |
| 10744 | 273 | fun meth tac x = METHOD (HEADGOAL o tac x); | 
| 274 | fun meth' tac x y = METHOD (HEADGOAL o tac x y); | |
| 8220 | 275 | |
| 7419 | 276 | in | 
| 277 | ||
| 10744 | 278 | val rule_tac = gen_rule_tac Tactic.resolve_tac; | 
| 279 | val rule = meth rule_tac; | |
| 280 | val some_rule_tac = gen_some_rule_tac rule_tac; | |
| 281 | val some_rule = meth' some_rule_tac; | |
| 282 | ||
| 283 | val erule = meth' (gen_arule_tac Tactic.eresolve_tac); | |
| 284 | val drule = meth' (gen_arule_tac Tactic.dresolve_tac); | |
| 285 | val frule = meth' (gen_arule_tac Tactic.forward_tac); | |
| 5824 | 286 | |
| 7419 | 287 | end; | 
| 288 | ||
| 289 | ||
| 25270 | 290 | (* intros_tac -- pervasive search spanned by intro rules *) | 
| 291 | ||
| 292 | fun intros_tac intros facts = | |
| 293 | ALLGOALS (insert_tac facts THEN' | |
| 294 | REPEAT_ALL_NEW (resolve_tac intros)) | |
| 295 | THEN Tactic.distinct_subgoals_tac; | |
| 296 | ||
| 297 | ||
| 8351 | 298 | (* ML tactics *) | 
| 299 | ||
| 26472 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 300 | structure TacticData = ProofDataFun | 
| 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 301 | ( | 
| 27235 | 302 | type T = thm list -> tactic; | 
| 26472 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 303 | fun init _ = undefined; | 
| 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 304 | ); | 
| 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 305 | |
| 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 306 | val set_tactic = TacticData.put; | 
| 8351 | 307 | |
| 26472 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 308 | fun ml_tactic (txt, pos) ctxt = | 
| 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 309 | let | 
| 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 310 | val ctxt' = ctxt |> Context.proof_map | 
| 27235 | 311 | (ML_Context.expression pos | 
| 312 | "fun tactic (facts: thm list) : tactic" | |
| 26472 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 313 | "Context.map_proof (Method.set_tactic tactic)" txt); | 
| 27235 | 314 | in Context.setmp_thread_data (SOME (Context.Proof ctxt)) (TacticData.get ctxt') end; | 
| 23425 | 315 | |
| 316 | fun tactic txt ctxt = METHOD (ml_tactic txt ctxt); | |
| 317 | fun raw_tactic txt ctxt = RAW_METHOD (ml_tactic txt ctxt); | |
| 8351 | 318 | |
| 319 | ||
| 5824 | 320 | |
| 17110 | 321 | (** method syntax **) | 
| 322 | ||
| 323 | (* method text *) | |
| 324 | ||
| 325 | type src = Args.src; | |
| 5824 | 326 | |
| 17110 | 327 | datatype text = | 
| 23349 | 328 | Basic of (Proof.context -> method) * Position.T | | 
| 17110 | 329 | Source of src | | 
| 20030 | 330 | Source_i of src | | 
| 17110 | 331 | Then of text list | | 
| 332 | Orelse of text list | | |
| 333 | Try of text | | |
| 19186 | 334 | Repeat1 of text | | 
| 335 | SelectGoals of int * text; | |
| 17110 | 336 | |
| 23349 | 337 | fun primitive_text r = Basic (K (SIMPLE_METHOD (PRIMITIVE r)), Position.none); | 
| 338 | val succeed_text = Basic (K succeed, Position.none); | |
| 17110 | 339 | val default_text = Source (Args.src (("default", []), Position.none));
 | 
| 23349 | 340 | val this_text = Basic (K this, Position.none); | 
| 341 | val done_text = Basic (K (SIMPLE_METHOD all_tac), Position.none); | |
| 342 | fun sorry_text int = Basic (cheating int, Position.none); | |
| 17110 | 343 | |
| 23349 | 344 | fun finish_text (NONE, immed) pos = Basic (close immed, pos) | 
| 345 | | finish_text (SOME txt, immed) pos = Then [txt, Basic (close immed, pos)]; | |
| 17110 | 346 | |
| 347 | ||
| 348 | (* method definitions *) | |
| 5824 | 349 | |
| 24116 | 350 | structure Methods = TheoryDataFun | 
| 22846 | 351 | ( | 
| 20289 | 352 | type T = (((src -> Proof.context -> method) * string) * stamp) NameSpace.table; | 
| 16347 | 353 | val empty = NameSpace.empty_table; | 
| 6546 | 354 | val copy = I; | 
| 16448 | 355 | val extend = I; | 
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
23590diff
changeset | 356 | fun merge _ tables : T = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUP dup => | 
| 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
23590diff
changeset | 357 |     error ("Attempt to merge different versions of method " ^ quote dup);
 | 
| 22846 | 358 | ); | 
| 5824 | 359 | |
| 22846 | 360 | fun print_methods thy = | 
| 361 | let | |
| 24116 | 362 | val meths = Methods.get thy; | 
| 22846 | 363 | fun prt_meth (name, ((_, comment), _)) = Pretty.block | 
| 364 | [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; | |
| 365 | in | |
| 366 | [Pretty.big_list "methods:" (map prt_meth (NameSpace.extern_table meths))] | |
| 367 | |> Pretty.chunks |> Pretty.writeln | |
| 368 | end; | |
| 7611 | 369 | |
| 5824 | 370 | |
| 371 | (* get methods *) | |
| 372 | ||
| 26892 | 373 | val intern = NameSpace.intern o #1 o Methods.get; | 
| 374 | val defined = Symtab.defined o #2 o Methods.get; | |
| 375 | ||
| 20030 | 376 | fun method_i thy = | 
| 5824 | 377 | let | 
| 24116 | 378 | val meths = #2 (Methods.get thy); | 
| 5884 | 379 | fun meth src = | 
| 20030 | 380 | let val ((name, _), pos) = Args.dest_src src in | 
| 17412 | 381 | (case Symtab.lookup meths name of | 
| 15531 | 382 |           NONE => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
 | 
| 27751 | 383 | | SOME ((mth, _), _) => (Position.report (Markup.method name) pos; mth src)) | 
| 5824 | 384 | end; | 
| 385 | in meth end; | |
| 386 | ||
| 24116 | 387 | fun method thy = method_i thy o Args.map_name (NameSpace.intern (#1 (Methods.get thy))); | 
| 20030 | 388 | |
| 5824 | 389 | |
| 17110 | 390 | (* add method *) | 
| 5824 | 391 | |
| 392 | fun add_methods raw_meths thy = | |
| 393 | let | |
| 16145 | 394 | val new_meths = raw_meths |> map (fn (name, f, comment) => | 
| 29004 | 395 | (Binding.name name, ((f, comment), stamp ()))); | 
| 5824 | 396 | |
| 30466 | 397 | fun add meths = fold (snd oo NameSpace.define (Sign.naming_of thy)) new_meths meths | 
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
23590diff
changeset | 398 |       handle Symtab.DUP dup => error ("Duplicate declaration of method " ^ quote dup);
 | 
| 24116 | 399 | in Methods.map add thy end; | 
| 5824 | 400 | |
| 9194 | 401 | val add_method = add_methods o Library.single; | 
| 402 | ||
| 5824 | 403 | |
| 17356 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 404 | (* method_setup *) | 
| 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 405 | |
| 26385 
ae7564661e76
ML runtime compilation: pass position, tuned signature;
 wenzelm parents: 
26291diff
changeset | 406 | fun method_setup name (txt, pos) cmt = | 
| 26472 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 407 | Context.theory_map (ML_Context.expression pos | 
| 22118 | 408 | "val method: bstring * (Method.src -> Proof.context -> Proof.method) * string" | 
| 22086 | 409 | "Context.map_theory (Method.add_method method)" | 
| 26472 
9afdd61cf528
ml_tactic: non-critical version via proof data and thread data;
 wenzelm parents: 
26463diff
changeset | 410 |     ("(" ^ quote name ^ ", " ^ txt ^ ", " ^ quote cmt ^ ")"));
 | 
| 17356 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 411 | |
| 
09afdf37cdb3
added cheating, sorry_text (from skip_proofs.ML);
 wenzelm parents: 
17314diff
changeset | 412 | |
| 5884 | 413 | |
| 17110 | 414 | (** concrete syntax **) | 
| 5824 | 415 | |
| 27813 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 416 | structure P = OuterParse; | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 417 | |
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 418 | |
| 5884 | 419 | (* basic *) | 
| 420 | ||
| 18999 | 421 | fun syntax scan = Args.context_syntax "method" scan; | 
| 5824 | 422 | |
| 17110 | 423 | fun simple_args scan f src ctxt : method = | 
| 21879 | 424 | fst (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt); | 
| 8351 | 425 | |
| 20289 | 426 | fun ctxt_args (f: Proof.context -> method) src ctxt = | 
| 21879 | 427 | fst (syntax (Scan.succeed (f ctxt)) src ctxt); | 
| 7555 | 428 | |
| 429 | fun no_args m = ctxt_args (K m); | |
| 5884 | 430 | |
| 431 | ||
| 432 | (* sections *) | |
| 5824 | 433 | |
| 20289 | 434 | type modifier = (Proof.context -> Proof.context) * attribute; | 
| 7268 | 435 | |
| 436 | local | |
| 437 | ||
| 24010 
2ef318813e1a
method section scanners: added [[declaration]] syntax, ignore sid-effects of thms;
 wenzelm parents: 
23937diff
changeset | 438 | fun thms ss = Scan.repeat (Scan.unless (Scan.lift (Scan.first ss)) Attrib.multi_thm) >> flat; | 
| 30190 | 439 | fun app (f, att) (context, ths) = Library.foldl_map att (Context.map_proof f context, ths); | 
| 5824 | 440 | |
| 24022 | 441 | fun section ss = Scan.depend (fn context => (Scan.first ss -- Scan.pass context (thms ss)) :|-- | 
| 442 | (fn (m, ths) => Scan.succeed (app m (context, ths)))); | |
| 5884 | 443 | |
| 7601 | 444 | fun sectioned args ss = args -- Scan.repeat (section ss); | 
| 5884 | 445 | |
| 7268 | 446 | in | 
| 5824 | 447 | |
| 5884 | 448 | fun sectioned_args args ss f src ctxt = | 
| 21879 | 449 | let val ((x, _), ctxt') = syntax (sectioned args ss) src ctxt | 
| 5921 | 450 | in f x ctxt' end; | 
| 5884 | 451 | |
| 7601 | 452 | fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f; | 
| 9777 | 453 | fun bang_sectioned_args' ss scan f = | 
| 454 | sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f); | |
| 7601 | 455 | fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f); | 
| 7268 | 456 | |
| 18999 | 457 | fun thms_ctxt_args f = sectioned_args (thms []) [] f; | 
| 8093 | 458 | fun thms_args f = thms_ctxt_args (K o f); | 
| 9706 | 459 | fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected"); | 
| 5824 | 460 | |
| 7268 | 461 | end; | 
| 462 | ||
| 5824 | 463 | |
| 9539 | 464 | (* tactic syntax *) | 
| 8238 | 465 | |
| 10744 | 466 | fun nat_thms_args f = uncurry f oo | 
| 27813 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 467 | (fst oo syntax (Scan.lift (Scan.optional (Args.parens P.nat) 0) -- Attrib.thms)); | 
| 10744 | 468 | |
| 21879 | 469 | fun goal_args' args tac src ctxt = fst (syntax (Args.goal_spec HEADGOAL -- args >> | 
| 21592 | 470 | (fn (quant, s) => SIMPLE_METHOD'' quant (tac s))) src ctxt); | 
| 8537 | 471 | |
| 9539 | 472 | fun goal_args args tac = goal_args' (Scan.lift args) tac; | 
| 8238 | 473 | |
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 474 | fun goal_args_ctxt' args tac src ctxt = | 
| 21879 | 475 | fst (syntax (Args.goal_spec HEADGOAL -- args >> | 
| 21592 | 476 | (fn (quant, s) => SIMPLE_METHOD'' quant (tac ctxt s))) src ctxt); | 
| 8238 | 477 | |
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 478 | fun goal_args_ctxt args tac = goal_args_ctxt' (Scan.lift args) tac; | 
| 5824 | 479 | |
| 14718 | 480 | |
| 27813 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 481 | (* outer parser *) | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 482 | |
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 483 | fun is_symid_meth s = | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 484 | s <> "|" andalso s <> "?" andalso s <> "+" andalso OuterLex.ident_or_symbolic s; | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 485 | |
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 486 | local | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 487 | |
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 488 | fun meth4 x = | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 489 | (P.position (P.xname >> rpair []) >> (Source o Args.src) || | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 490 |   P.$$$ "(" |-- P.!!! (meth0 --| P.$$$ ")")) x
 | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 491 | and meth3 x = | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 492 | (meth4 --| P.$$$ "?" >> Try || | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 493 | meth4 --| P.$$$ "+" >> Repeat1 || | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 494 | meth4 -- (P.$$$ "[" |-- Scan.optional P.nat 1 --| P.$$$ "]") >> (SelectGoals o swap) || | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 495 | meth4) x | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 496 | and meth2 x = | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 497 | (P.position (P.xname -- Args.parse1 is_symid_meth) >> (Source o Args.src) || | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 498 | meth3) x | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 499 | and meth1 x = (P.enum1 "," meth2 >> (fn [m] => m | ms => Then ms)) x | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 500 | and meth0 x = (P.enum1 "|" meth1 >> (fn [m] => m | ms => Orelse ms)) x; | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 501 | |
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 502 | in val parse = meth3 end; | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 503 | |
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 504 | |
| 18708 | 505 | (* theory setup *) | 
| 5824 | 506 | |
| 26463 | 507 | val _ = Context.>> (Context.map_theory | 
| 508 | (add_methods | |
| 509 |    [("fail", no_args fail, "force failure"),
 | |
| 510 |     ("succeed", no_args succeed, "succeed"),
 | |
| 511 |     ("-", no_args insert_facts, "do nothing (insert current facts only)"),
 | |
| 512 |     ("insert", thms_args insert, "insert theorems, ignoring facts (improper)"),
 | |
| 513 |     ("intro", thms_args intro, "repeatedly apply introduction rules"),
 | |
| 514 |     ("elim", thms_args elim, "repeatedly apply elimination rules"),
 | |
| 515 |     ("unfold", thms_ctxt_args unfold_meth, "unfold definitions"),
 | |
| 516 |     ("fold", thms_ctxt_args fold_meth, "fold definitions"),
 | |
| 517 |     ("atomize", (atomize o fst) oo syntax (Args.mode "full"),
 | |
| 518 | "present local premises as object-level statements"), | |
| 519 |     ("rule", thms_ctxt_args some_rule, "apply some intro/elim rule"),
 | |
| 520 |     ("erule", nat_thms_args erule, "apply rule in elimination manner (improper)"),
 | |
| 521 |     ("drule", nat_thms_args drule, "apply rule in destruct manner (improper)"),
 | |
| 522 |     ("frule", nat_thms_args frule, "apply rule in forward manner (improper)"),
 | |
| 523 |     ("this", no_args this, "apply current facts as rules"),
 | |
| 524 |     ("fact", thms_ctxt_args fact, "composition by facts from context"),
 | |
| 525 |     ("assumption", ctxt_args assumption, "proof by assumption, preferring facts"),
 | |
| 27244 | 526 |     ("rename_tac", goal_args (Scan.repeat1 Args.name) Tactic.rename_tac,
 | 
| 26463 | 527 | "rename parameters of goal"), | 
| 27813 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 528 |     ("rotate_tac", goal_args (Scan.optional P.int 1) Tactic.rotate_tac,
 | 
| 26463 | 529 | "rotate assumptions of goal"), | 
| 27813 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 530 |     ("tactic", simple_args (P.position Args.name) tactic, "ML tactic as proof method"),
 | 
| 
96fbe385a0d0
unified Args.T with OuterLex.token, renamed some operations;
 wenzelm parents: 
27751diff
changeset | 531 |     ("raw_tactic", simple_args (P.position Args.name) raw_tactic,
 | 
| 26463 | 532 | "ML tactic as raw proof method")])); | 
| 5824 | 533 | |
| 534 | ||
| 16145 | 535 | (*final declarations of this structure!*) | 
| 536 | val unfold = unfold_meth; | |
| 537 | val fold = fold_meth; | |
| 538 | ||
| 5824 | 539 | end; | 
| 540 | ||
| 541 | structure BasicMethod: BASIC_METHOD = Method; | |
| 542 | open BasicMethod; |