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