| author | wenzelm | 
| Wed, 29 Jun 2005 15:13:38 +0200 | |
| changeset 16608 | 4f8d7b83c7e2 | 
| parent 16500 | 09d43301b195 | 
| child 16876 | f57b38cced32 | 
| permissions | -rw-r--r-- | 
| 5824 | 1 | (* Title: Pure/Isar/method.ML | 
| 2 | ID: $Id$ | |
| 3 | Author: Markus Wenzel, TU Muenchen | |
| 4 | ||
| 5 | Proof methods. | |
| 6 | *) | |
| 7 | ||
| 8 | signature BASIC_METHOD = | |
| 9 | sig | |
| 11731 | 10 | val trace_rules: bool ref | 
| 5824 | 11 | val print_methods: theory -> unit | 
| 12 | val Method: bstring -> (Args.src -> Proof.context -> Proof.method) -> string -> unit | |
| 13 | end; | |
| 14 | ||
| 15 | signature METHOD = | |
| 16 | sig | |
| 17 | include BASIC_METHOD | |
| 15703 | 18 | type src | 
| 12055 | 19 | val trace: Proof.context -> thm list -> unit | 
| 12144 | 20 | val RAW_METHOD: (thm list -> tactic) -> Proof.method | 
| 16448 | 21 | val RAW_METHOD_CASES: (thm list -> RuleCases.tactic) -> Proof.method | 
| 6091 | 22 | val METHOD: (thm list -> tactic) -> Proof.method | 
| 16448 | 23 | val METHOD_CASES: (thm list -> RuleCases.tactic) -> Proof.method | 
| 9706 | 24 | val SIMPLE_METHOD: tactic -> Proof.method | 
| 25 | val SIMPLE_METHOD': ((int -> tactic) -> tactic) -> (int -> tactic) -> Proof.method | |
| 5824 | 26 | val fail: Proof.method | 
| 27 | val succeed: Proof.method | |
| 8167 | 28 | val defer: int option -> Proof.method | 
| 29 | val prefer: int -> Proof.method | |
| 7419 | 30 | val insert_tac: thm list -> int -> tactic | 
| 7574 | 31 | val insert: thm list -> Proof.method | 
| 7555 | 32 | val insert_facts: Proof.method | 
| 7601 | 33 | val unfold: thm list -> Proof.method | 
| 7419 | 34 | val fold: thm list -> Proof.method | 
| 35 | val multi_resolve: thm list -> thm -> thm Seq.seq | |
| 36 | val multi_resolves: thm list -> thm list -> thm Seq.seq | |
| 12347 | 37 | val rules_tac: Proof.context -> int option -> int -> tactic | 
| 6091 | 38 | val rule_tac: thm list -> thm list -> int -> tactic | 
| 10309 | 39 | val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic | 
| 6091 | 40 | val rule: thm list -> Proof.method | 
| 10744 | 41 | val erule: int -> thm list -> Proof.method | 
| 42 | val drule: int -> thm list -> Proof.method | |
| 43 | val frule: int -> thm list -> Proof.method | |
| 8195 | 44 | val this: Proof.method | 
| 7555 | 45 | val assumption: Proof.context -> Proof.method | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 46 | val bires_inst_tac: bool -> Proof.context -> (indexname * string) list -> thm -> int -> tactic | 
| 8351 | 47 | val set_tactic: (Proof.context -> thm list -> tactic) -> unit | 
| 48 | val tactic: string -> Proof.context -> Proof.method | |
| 5916 | 49 | exception METHOD_FAIL of (string * Position.T) * exn | 
| 15703 | 50 | val method: theory -> src -> Proof.context -> Proof.method | 
| 51 | val add_method: bstring * (src -> Proof.context -> Proof.method) * string | |
| 9539 | 52 | -> theory -> theory | 
| 15703 | 53 | val add_methods: (bstring * (src -> Proof.context -> Proof.method) * string) list | 
| 5824 | 54 | -> theory -> theory | 
| 5884 | 55 | val syntax: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) -> | 
| 15703 | 56 | src -> Proof.context -> Proof.context * 'a | 
| 8351 | 57 | val simple_args: (Args.T list -> 'a * Args.T list) | 
| 15703 | 58 |     -> ('a -> Proof.context -> Proof.method) -> src -> Proof.context -> Proof.method
 | 
| 59 | val ctxt_args: (Proof.context -> Proof.method) -> src -> Proof.context -> Proof.method | |
| 60 | val no_args: Proof.method -> src -> Proof.context -> Proof.method | |
| 7268 | 61 | type modifier | 
| 7601 | 62 | val sectioned_args: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) -> | 
| 7268 | 63 | (Args.T list -> modifier * Args.T list) list -> | 
| 15703 | 64 |     ('a -> Proof.context -> 'b) -> src -> Proof.context -> 'b
 | 
| 7601 | 65 | val bang_sectioned_args: | 
| 66 | (Args.T list -> modifier * Args.T list) list -> | |
| 15703 | 67 | (thm list -> Proof.context -> 'a) -> src -> Proof.context -> 'a | 
| 9777 | 68 | val bang_sectioned_args': | 
| 69 | (Args.T list -> modifier * Args.T list) list -> | |
| 70 | (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) -> | |
| 15703 | 71 |     ('a -> thm list -> Proof.context -> 'b) -> src -> Proof.context -> 'b
 | 
| 7601 | 72 | val only_sectioned_args: | 
| 73 | (Args.T list -> modifier * Args.T list) list -> | |
| 15703 | 74 | (Proof.context -> 'a) -> src -> Proof.context -> 'a | 
| 75 | val thms_ctxt_args: (thm list -> Proof.context -> 'a) -> src -> Proof.context -> 'a | |
| 76 | val thms_args: (thm list -> 'a) -> src -> Proof.context -> 'a | |
| 77 | val thm_args: (thm -> 'a) -> src -> Proof.context -> 'a | |
| 5824 | 78 | datatype text = | 
| 79 | Basic of (Proof.context -> Proof.method) | | |
| 15703 | 80 | Source of src | | 
| 5824 | 81 | Then of text list | | 
| 82 | Orelse of text list | | |
| 83 | Try of text | | |
| 84 | Repeat1 of text | |
| 85 | val refine: text -> Proof.state -> Proof.state Seq.seq | |
| 8238 | 86 | val refine_end: text -> Proof.state -> Proof.state Seq.seq | 
| 5824 | 87 | val proof: text option -> Proof.state -> Proof.state Seq.seq | 
| 8966 | 88 | val local_qed: bool -> text option | 
| 12144 | 89 | -> (Proof.context -> string * (string * thm list) list -> unit) * | 
| 12055 | 90 | (Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq | 
| 6981 | 91 | val local_terminal_proof: text * text option | 
| 12144 | 92 | -> (Proof.context -> string * (string * thm list) list -> unit) * | 
| 12055 | 93 | (Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq | 
| 12144 | 94 | val local_default_proof: (Proof.context -> string * (string * thm list) list -> unit) * | 
| 12055 | 95 | (Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq | 
| 12144 | 96 | val local_immediate_proof: (Proof.context -> string * (string * thm list) list -> unit) * | 
| 12055 | 97 | (Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq | 
| 12144 | 98 | val local_done_proof: (Proof.context -> string * (string * thm list) list -> unit) * | 
| 12055 | 99 | (Proof.context -> thm -> unit) -> Proof.state -> Proof.state Seq.seq | 
| 8966 | 100 | val global_qed: bool -> text option | 
| 12244 | 101 | -> Proof.state -> theory * ((string * string) * (string * thm list) list) | 
| 6934 | 102 | val global_terminal_proof: text * text option | 
| 12244 | 103 | -> Proof.state -> theory * ((string * string) * (string * thm list) list) | 
| 104 | val global_default_proof: Proof.state -> theory * ((string * string) * (string * thm list) list) | |
| 105 | val global_immediate_proof: Proof.state -> | |
| 106 | theory * ((string * string) * (string * thm list) list) | |
| 107 | val global_done_proof: Proof.state -> theory * ((string * string) * (string * thm list) list) | |
| 9539 | 108 |   val goal_args: (Args.T list -> 'a * Args.T list) -> ('a -> int -> tactic)
 | 
| 15703 | 109 | -> src -> Proof.context -> Proof.method | 
| 9539 | 110 | val goal_args': (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) | 
| 15703 | 111 |     -> ('a -> int -> tactic) -> src -> Proof.context -> Proof.method
 | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 112 | val goal_args_ctxt: (Args.T list -> 'a * Args.T list) -> (Proof.context -> 'a -> int -> tactic) | 
| 15703 | 113 | -> src -> Proof.context -> Proof.method | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 114 | val goal_args_ctxt': (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) | 
| 15703 | 115 | -> (Proof.context -> 'a -> int -> tactic) -> src -> Proof.context -> Proof.method | 
| 5824 | 116 | end; | 
| 117 | ||
| 118 | structure Method: METHOD = | |
| 119 | struct | |
| 120 | ||
| 15703 | 121 | type src = Args.src; | 
| 122 | ||
| 5824 | 123 | |
| 12324 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 124 | (** proof methods **) | 
| 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 125 | |
| 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 126 | (* tracing *) | 
| 11731 | 127 | |
| 128 | val trace_rules = ref false; | |
| 129 | ||
| 12055 | 130 | fun trace ctxt rules = | 
| 12262 | 131 | conditional (! trace_rules andalso not (null rules)) (fn () => | 
| 132 | Pretty.big_list "rules:" (map (ProofContext.pretty_thm ctxt) rules) | |
| 133 | |> Pretty.string_of |> tracing); | |
| 11731 | 134 | |
| 135 | ||
| 8372 | 136 | (* make methods *) | 
| 5824 | 137 | |
| 12144 | 138 | val RAW_METHOD = Proof.method; | 
| 139 | val RAW_METHOD_CASES = Proof.method_cases; | |
| 140 | ||
| 141 | fun METHOD m = Proof.method (fn facts => TRY Tactic.conjunction_tac THEN m facts); | |
| 142 | fun METHOD_CASES m = | |
| 143 | Proof.method_cases (fn facts => Seq.THEN (TRY Tactic.conjunction_tac, m facts)); | |
| 8372 | 144 | |
| 5824 | 145 | |
| 146 | (* primitive *) | |
| 147 | ||
| 148 | val fail = METHOD (K no_tac); | |
| 149 | val succeed = METHOD (K all_tac); | |
| 150 | ||
| 151 | ||
| 8167 | 152 | (* shuffle *) | 
| 153 | ||
| 8240 | 154 | fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1))); | 
| 15973 | 155 | fun defer opt_i = METHOD (K (Tactic.defer_tac (if_none opt_i 1))); | 
| 8167 | 156 | |
| 157 | ||
| 7419 | 158 | (* insert *) | 
| 159 | ||
| 160 | local | |
| 5824 | 161 | |
| 6981 | 162 | fun cut_rule_tac raw_rule = | 
| 163 | let | |
| 164 | val rule = Drule.forall_intr_vars raw_rule; | |
| 165 | val revcut_rl = Drule.incr_indexes_wrt [] [] [] [rule] Drule.revcut_rl; | |
| 7555 | 166 | in Tactic.rtac (rule COMP revcut_rl) end; | 
| 6981 | 167 | |
| 7419 | 168 | in | 
| 5824 | 169 | |
| 7419 | 170 | fun insert_tac [] i = all_tac | 
| 171 | | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts); | |
| 6981 | 172 | |
| 7555 | 173 | val insert_facts = METHOD (ALLGOALS o insert_tac); | 
| 7664 | 174 | fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms)); | 
| 7419 | 175 | |
| 9706 | 176 | fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac); | 
| 177 | fun SIMPLE_METHOD' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac)); | |
| 178 | ||
| 12324 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 179 | end; | 
| 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 180 | |
| 9706 | 181 | |
| 12384 | 182 | (* unfold/fold definitions *) | 
| 183 | ||
| 16145 | 184 | fun unfold_meth ths = SIMPLE_METHOD (CHANGED_PROP (rewrite_goals_tac ths)); | 
| 185 | fun fold_meth ths = SIMPLE_METHOD (CHANGED_PROP (fold_goals_tac ths)); | |
| 6532 | 186 | |
| 12384 | 187 | |
| 12829 | 188 | (* atomize rule statements *) | 
| 189 | ||
| 190 | fun atomize false = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o ObjectLogic.atomize_tac) | |
| 191 | | atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o ObjectLogic.full_atomize_tac))); | |
| 192 | ||
| 193 | ||
| 12384 | 194 | (* unfold intro/elim rules *) | 
| 195 | ||
| 196 | fun intro ths = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths)); | |
| 197 | fun elim ths = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths)); | |
| 9484 | 198 | |
| 199 | ||
| 7419 | 200 | (* multi_resolve *) | 
| 201 | ||
| 202 | local | |
| 203 | ||
| 204 | fun res th i rule = | |
| 205 | Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty; | |
| 206 | ||
| 207 | fun multi_res _ [] rule = Seq.single rule | |
| 208 | | multi_res i (th :: ths) rule = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths rule)); | |
| 209 | ||
| 210 | in | |
| 211 | ||
| 212 | val multi_resolve = multi_res 1; | |
| 8372 | 213 | fun multi_resolves facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules)); | 
| 7419 | 214 | |
| 215 | end; | |
| 216 | ||
| 217 | ||
| 12347 | 218 | (* rules_tac *) | 
| 8372 | 219 | |
| 7419 | 220 | local | 
| 5824 | 221 | |
| 12359 | 222 | val remdups_tac = SUBGOAL (fn (g, i) => | 
| 223 | let val prems = Logic.strip_assums_hyp g in | |
| 224 | REPEAT_DETERM_N (length prems - length (gen_distinct op aconv prems)) | |
| 225 | (Tactic.ematch_tac [Drule.remdups_rl] i THEN Tactic.eq_assume_tac i) | |
| 226 | end); | |
| 12347 | 227 | |
| 228 | fun REMDUPS tac = tac THEN_ALL_NEW remdups_tac; | |
| 229 | ||
| 230 | fun gen_eq_set e s1 s2 = | |
| 231 | length s1 = length s2 andalso | |
| 232 | gen_subset e (s1, s2) andalso gen_subset e (s2, s1); | |
| 233 | ||
| 12350 | 234 | val bires_tac = Tactic.biresolution_from_nets_tac ContextRules.orderlist; | 
| 12347 | 235 | |
| 236 | fun safe_step_tac ctxt = | |
| 12350 | 237 | ContextRules.Swrap ctxt | 
| 238 | (eq_assume_tac ORELSE' | |
| 239 | bires_tac true (ContextRules.netpair_bang ctxt)); | |
| 12347 | 240 | |
| 241 | fun unsafe_step_tac ctxt = | |
| 12350 | 242 | ContextRules.wrap ctxt | 
| 243 | (assume_tac APPEND' | |
| 244 | bires_tac false (ContextRules.netpair_bang ctxt) APPEND' | |
| 245 | bires_tac false (ContextRules.netpair ctxt)); | |
| 12347 | 246 | |
| 247 | fun step_tac ctxt i = | |
| 248 | REPEAT_DETERM1 (REMDUPS (safe_step_tac ctxt) i) ORELSE | |
| 249 | REMDUPS (unsafe_step_tac ctxt) i; | |
| 250 | ||
| 251 | fun intpr_tac ctxt gs d lim = SUBGOAL (fn (g, i) => if d > lim then no_tac else | |
| 252 | let | |
| 253 | val ps = Logic.strip_assums_hyp g; | |
| 254 | val c = Logic.strip_assums_concl g; | |
| 255 | in | |
| 256 | if gen_mem (fn ((ps1, c1), (ps2, c2)) => | |
| 257 | c1 aconv c2 andalso gen_eq_set op aconv ps1 ps2) ((ps, c), gs) then no_tac | |
| 258 | else (step_tac ctxt THEN_ALL_NEW intpr_tac ctxt ((ps, c) :: gs) (d + 1) lim) i | |
| 259 | end); | |
| 260 | ||
| 261 | in | |
| 262 | ||
| 12359 | 263 | fun rules_tac ctxt opt_lim = | 
| 15973 | 264 | SELECT_GOAL (DEEPEN (2, if_none opt_lim 20) (intpr_tac ctxt [] 0) 4 1); | 
| 12347 | 265 | |
| 266 | end; | |
| 267 | ||
| 268 | ||
| 269 | (* rule_tac etc. *) | |
| 270 | ||
| 271 | local | |
| 272 | ||
| 10541 
fdec07d4f047
resolveq(_cases)_tac moved to HOL/Tools/induct_method.ML;
 wenzelm parents: 
10529diff
changeset | 273 | fun gen_rule_tac tac rules [] i st = tac rules i st | 
| 12324 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 274 | | gen_rule_tac tac rules facts i st = | 
| 
5db4b4596d1a
rule context and attributes moved to rule_context.ML;
 wenzelm parents: 
12311diff
changeset | 275 | Seq.flat (Seq.map (fn rule => (tac o single) rule i st) (multi_resolves facts rules)); | 
| 7130 | 276 | |
| 10744 | 277 | fun gen_arule_tac tac j rules facts = | 
| 278 | EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac); | |
| 279 | ||
| 11785 | 280 | fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) => | 
| 281 | let | |
| 282 | val rules = | |
| 283 | if not (null arg_rules) then arg_rules | |
| 15570 | 284 | else List.concat (ContextRules.find_rules false facts goal ctxt) | 
| 12055 | 285 | in trace ctxt rules; tac rules facts i end); | 
| 10309 | 286 | |
| 10744 | 287 | fun meth tac x = METHOD (HEADGOAL o tac x); | 
| 288 | fun meth' tac x y = METHOD (HEADGOAL o tac x y); | |
| 8220 | 289 | |
| 7419 | 290 | in | 
| 291 | ||
| 10744 | 292 | val rule_tac = gen_rule_tac Tactic.resolve_tac; | 
| 293 | val rule = meth rule_tac; | |
| 294 | val some_rule_tac = gen_some_rule_tac rule_tac; | |
| 295 | val some_rule = meth' some_rule_tac; | |
| 296 | ||
| 297 | val erule = meth' (gen_arule_tac Tactic.eresolve_tac); | |
| 298 | val drule = meth' (gen_arule_tac Tactic.dresolve_tac); | |
| 299 | val frule = meth' (gen_arule_tac Tactic.forward_tac); | |
| 5824 | 300 | |
| 7419 | 301 | end; | 
| 302 | ||
| 303 | ||
| 8195 | 304 | (* this *) | 
| 305 | ||
| 8671 | 306 | val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac)); | 
| 8195 | 307 | |
| 308 | ||
| 309 | (* assumption *) | |
| 7555 | 310 | |
| 10378 
98c95ebf804f
assumption / finish: handle non-atomic assumptions from context as well;
 wenzelm parents: 
10309diff
changeset | 311 | fun asm_tac ths = | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 312 | foldr (op APPEND') (K no_tac) (map (fn th => Tactic.rtac th THEN_ALL_NEW assume_tac) ths); | 
| 10378 
98c95ebf804f
assumption / finish: handle non-atomic assumptions from context as well;
 wenzelm parents: 
10309diff
changeset | 313 | |
| 16500 
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
 wenzelm parents: 
16448diff
changeset | 314 | val refl_tac = SUBGOAL (fn (prop, i) => | 
| 
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
 wenzelm parents: 
16448diff
changeset | 315 | if can Logic.dest_equals (Logic.strip_assums_concl prop) | 
| 
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
 wenzelm parents: 
16448diff
changeset | 316 | then Tactic.rtac Drule.reflexive_thm i else no_tac); | 
| 
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
 wenzelm parents: 
16448diff
changeset | 317 | |
| 10405 | 318 | fun assm_tac ctxt = | 
| 319 | assume_tac APPEND' | |
| 320 | asm_tac (ProofContext.prems_of ctxt) APPEND' | |
| 16500 
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
 wenzelm parents: 
16448diff
changeset | 321 | refl_tac; | 
| 7419 | 322 | |
| 7555 | 323 | fun assumption_tac ctxt [] = assm_tac ctxt | 
| 10378 
98c95ebf804f
assumption / finish: handle non-atomic assumptions from context as well;
 wenzelm parents: 
10309diff
changeset | 324 | | assumption_tac _ [fact] = asm_tac [fact] | 
| 7555 | 325 | | assumption_tac _ _ = K no_tac; | 
| 7419 | 326 | |
| 8671 | 327 | fun assumption ctxt = METHOD (HEADGOAL o assumption_tac ctxt); | 
| 7419 | 328 | |
| 329 | ||
| 9539 | 330 | (* res_inst_tac etc. *) | 
| 8238 | 331 | |
| 14718 | 332 | (*Reimplemented to support both static (Isar) and dynamic (proof state) | 
| 333 | context. By Clemens Ballarin.*) | |
| 12119 | 334 | |
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 335 | fun bires_inst_tac bires_flag ctxt insts thm = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 336 | let | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 337 | val sign = ProofContext.sign_of ctxt; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 338 | (* Separate type and term insts *) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 339 | fun has_type_var ((x, _), _) = (case Symbol.explode x of | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 340 | "'"::cs => true | cs => false); | 
| 15570 | 341 | val Tinsts = List.filter has_type_var insts; | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 342 | val tinsts = filter_out has_type_var insts; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 343 | (* Tactic *) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 344 | fun tac i st = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 345 | let | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 346 | (* Preprocess state: extract environment information: | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 347 | - variables and their types | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 348 | - type variables and their sorts | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 349 | - parameters and their types *) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 350 | val (types, sorts) = types_sorts st; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 351 | (* Process type insts: Tinsts_env *) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 352 | fun absent xi = error | 
| 14718 | 353 |           ("No such variable in theorem: " ^ Syntax.string_of_vname xi);
 | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 354 | val (rtypes, rsorts) = types_sorts thm; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 355 | fun readT (xi, s) = | 
| 15531 | 356 | let val S = case rsorts xi of SOME S => S | NONE => absent xi; | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 357 | val T = Sign.read_typ (sign, sorts) s; | 
| 15798 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15713diff
changeset | 358 | val U = TVar (xi, S); | 
| 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15713diff
changeset | 359 | in if Sign.typ_instance sign (T, U) then (U, T) | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 360 | else error | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 361 |              ("Instantiation of " ^ Syntax.string_of_vname xi ^ " fails")
 | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 362 | end; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 363 | val Tinsts_env = map readT Tinsts; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 364 | (* Preprocess rule: extract vars and their types, apply Tinsts *) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 365 | fun get_typ xi = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 366 | (case rtypes xi of | 
| 15798 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15713diff
changeset | 367 | SOME T => typ_subst_atomic Tinsts_env T | 
| 15531 | 368 | | NONE => absent xi); | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 369 | val (xis, ss) = Library.split_list tinsts; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 370 | val Ts = map get_typ xis; | 
| 14718 | 371 | val (_, _, Bi, _) = dest_state(st,i) | 
| 372 | val params = Logic.strip_params Bi | |
| 373 | (* params of subgoal i as string typ pairs *) | |
| 374 | val params = rev(Term.rename_wrt_term Bi params) | |
| 375 | (* as they are printed: bound variables with *) | |
| 14508 
859b11514537
Experimental command for instantiation of locales in proof contexts:
 ballarin parents: 
14215diff
changeset | 376 | (* the same name are renamed during printing *) | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 377 | fun types' (a, ~1) = (case assoc (params, a) of | 
| 15531 | 378 | NONE => types (a, ~1) | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 379 | | some => some) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 380 | | types' xi = types xi; | 
| 15973 | 381 | fun internal x = is_some (types' (x, ~1)); | 
| 15703 | 382 | val used = Drule.add_used thm (Drule.add_used st []); | 
| 14718 | 383 | val (ts, envT) = | 
| 384 | ProofContext.read_termTs_schematic ctxt internal types' sorts used (ss ~~ Ts); | |
| 15798 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15713diff
changeset | 385 | val envT' = map (fn (ixn, T) => | 
| 15973 | 386 | (TVar (ixn, the (rsorts ixn)), T)) envT @ Tinsts_env; | 
| 14718 | 387 | val cenv = | 
| 388 | map | |
| 389 | (fn (xi, t) => | |
| 390 | pairself (Thm.cterm_of sign) (Var (xi, fastype_of t), t)) | |
| 391 | (gen_distinct | |
| 392 | (fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2) | |
| 393 | (xis ~~ ts)); | |
| 394 | (* Lift and instantiate rule *) | |
| 395 |         val {maxidx, ...} = rep_thm st;
 | |
| 396 | val paramTs = map #2 params | |
| 397 | and inc = maxidx+1 | |
| 398 | fun liftvar (Var ((a,j), T)) = | |
| 399 | Var((a, j+inc), paramTs ---> incr_tvar inc T) | |
| 400 |           | liftvar t = raise TERM("Variable expected", [t]);
 | |
| 401 | fun liftterm t = list_abs_free | |
| 402 | (params, Logic.incr_indexes(paramTs,inc) t) | |
| 403 | fun liftpair (cv,ct) = | |
| 404 | (cterm_fun liftvar cv, cterm_fun liftterm ct) | |
| 15798 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15713diff
changeset | 405 | val lifttvar = pairself (ctyp_of sign o incr_tvar inc); | 
| 14718 | 406 | val rule = Drule.instantiate | 
| 15798 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15713diff
changeset | 407 | (map lifttvar envT', map liftpair cenv) | 
| 14718 | 408 | (lift_rule (st, i) thm) | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 409 | in | 
| 14718 | 410 | if i > nprems_of st then no_tac st | 
| 411 | else st |> | |
| 412 | compose_tac (bires_flag, rule, nprems_of thm) i | |
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 413 | end | 
| 14718 | 414 | handle TERM (msg,_) => (warning msg; no_tac st) | 
| 415 | | THM (msg,_,_) => (warning msg; no_tac st); | |
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 416 | in | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 417 | tac | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 418 | end; | 
| 8238 | 419 | |
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 420 | fun gen_inst _ tac _ (quant, ([], thms)) = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 421 | METHOD (fn facts => quant (insert_tac facts THEN' tac thms)) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 422 | | gen_inst inst_tac _ ctxt (quant, (insts, [thm])) = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 423 | METHOD (fn facts => | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 424 | quant (insert_tac facts THEN' inst_tac ctxt insts thm)) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 425 | | gen_inst _ _ _ _ = error "Cannot have instantiations with multiple rules"; | 
| 14718 | 426 | |
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 427 | val res_inst_meth = gen_inst (bires_inst_tac false) Tactic.resolve_tac; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 428 | |
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 429 | val eres_inst_meth = gen_inst (bires_inst_tac true) Tactic.eresolve_tac; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 430 | |
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 431 | (* Preserve Var indexes of rl; increment revcut_rl instead. | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 432 | Copied from tactic.ML *) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 433 | fun make_elim_preserve rl = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 434 |   let val {maxidx,...} = rep_thm rl
 | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 435 | fun cvar xi = cterm_of (Theory.sign_of ProtoPure.thy) (Var(xi,propT)); | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 436 | val revcut_rl' = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 437 |           instantiate ([],  [(cvar("V",0), cvar("V",maxidx+1)),
 | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 438 |                              (cvar("W",0), cvar("W",maxidx+1))]) revcut_rl
 | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 439 | val arg = (false, rl, nprems_of rl) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 440 | val [th] = Seq.list_of (bicompose false arg 1 revcut_rl') | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 441 | in th end | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 442 |   handle Bind => raise THM("make_elim_preserve", 1, [rl]);
 | 
| 8238 | 443 | |
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 444 | val cut_inst_meth = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 445 | gen_inst | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 446 | (fn ctxt => fn insts => fn thm => | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 447 | bires_inst_tac false ctxt insts (make_elim_preserve thm)) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 448 | Tactic.cut_rules_tac; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 449 | |
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 450 | val dres_inst_meth = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 451 | gen_inst | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 452 | (fn ctxt => fn insts => fn rule => | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 453 | bires_inst_tac true ctxt insts (make_elim_preserve rule)) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 454 | Tactic.dresolve_tac; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 455 | |
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 456 | val forw_inst_meth = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 457 | gen_inst | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 458 | (fn ctxt => fn insts => fn rule => | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 459 | bires_inst_tac false ctxt insts (make_elim_preserve rule) THEN' | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 460 | assume_tac) | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 461 | Tactic.forward_tac; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 462 | |
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 463 | fun subgoal_tac ctxt sprop = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 464 |   DETERM o bires_inst_tac false ctxt [(("psi", 0), sprop)] cut_rl THEN'
 | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 465 | SUBGOAL (fn (prop, _) => | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 466 | let val concl' = Logic.strip_assums_concl prop in | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 467 | if null (term_tvars concl') then () | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 468 | else warning "Type variables in new subgoal: add a type constraint?"; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 469 | all_tac | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 470 | end); | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 471 | |
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 472 | fun subgoals_tac ctxt sprops = EVERY' (map (subgoal_tac ctxt) sprops); | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 473 | |
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 474 | fun thin_tac ctxt s = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 475 |   bires_inst_tac true ctxt [(("V", 0), s)] thin_rl;
 | 
| 8238 | 476 | |
| 14718 | 477 | |
| 8329 | 478 | (* simple Prolog interpreter *) | 
| 479 | ||
| 480 | fun prolog_tac rules facts = | |
| 481 | DEPTH_SOLVE_1 (HEADGOAL (Tactic.assume_tac APPEND' Tactic.resolve_tac (facts @ rules))); | |
| 482 | ||
| 483 | val prolog = METHOD o prolog_tac; | |
| 484 | ||
| 485 | ||
| 8351 | 486 | (* ML tactics *) | 
| 487 | ||
| 488 | val tactic_ref = ref ((fn _ => raise Match): Proof.context -> thm list -> tactic); | |
| 489 | fun set_tactic f = tactic_ref := f; | |
| 490 | ||
| 491 | fun tactic txt ctxt = METHOD (fn facts => | |
| 9631 | 492 | (Context.use_mltext | 
| 15829 | 493 |     ("let fun tactic (ctxt: Proof.context) (facts: thm list) : tactic = \
 | 
| 16500 
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
 wenzelm parents: 
16448diff
changeset | 494 | \let val thm = ProofContext.get_thm_closure ctxt o PureThy.Name\n\ | 
| 
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
 wenzelm parents: 
16448diff
changeset | 495 | \ and thms = ProofContext.get_thms_closure ctxt o PureThy.Name in\n" | 
| 
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
 wenzelm parents: 
16448diff
changeset | 496 | ^ txt ^ | 
| 
09d43301b195
refl_tac: avoid failure of unification, i.e. confusing trace msg;
 wenzelm parents: 
16448diff
changeset | 497 | "\nend in Method.set_tactic tactic end") | 
| 15531 | 498 | false NONE; | 
| 499 | Context.setmp (SOME (ProofContext.theory_of ctxt)) (! tactic_ref ctxt) facts)); | |
| 8351 | 500 | |
| 501 | ||
| 5824 | 502 | |
| 503 | (** methods theory data **) | |
| 504 | ||
| 505 | (* data kind 'Isar/methods' *) | |
| 506 | ||
| 16448 | 507 | structure MethodsData = TheoryDataFun | 
| 508 | (struct | |
| 5824 | 509 | val name = "Isar/methods"; | 
| 16347 | 510 | type T = (((src -> Proof.context -> Proof.method) * string) * stamp) NameSpace.table; | 
| 5824 | 511 | |
| 16347 | 512 | val empty = NameSpace.empty_table; | 
| 6546 | 513 | val copy = I; | 
| 16448 | 514 | val extend = I; | 
| 515 | fun merge _ tables = NameSpace.merge_tables eq_snd tables handle Symtab.DUPS dups => | |
| 16347 | 516 |     error ("Attempt to merge different versions of method(s) " ^ commas_quote dups);
 | 
| 5824 | 517 | |
| 16347 | 518 | fun print _ meths = | 
| 5824 | 519 | let | 
| 520 | fun prt_meth (name, ((_, comment), _)) = Pretty.block | |
| 6849 | 521 | [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; | 
| 5824 | 522 | in | 
| 16347 | 523 | [Pretty.big_list "methods:" (map prt_meth (NameSpace.extern_table meths))] | 
| 9222 | 524 | |> Pretty.chunks |> Pretty.writeln | 
| 5824 | 525 | end; | 
| 16448 | 526 | end); | 
| 5824 | 527 | |
| 15801 | 528 | val _ = Context.add_setup [MethodsData.init]; | 
| 5824 | 529 | val print_methods = MethodsData.print; | 
| 7611 | 530 | |
| 5824 | 531 | |
| 532 | (* get methods *) | |
| 533 | ||
| 5916 | 534 | exception METHOD_FAIL of (string * Position.T) * exn; | 
| 535 | ||
| 5824 | 536 | fun method thy = | 
| 537 | let | |
| 16347 | 538 | val (space, meths) = MethodsData.get thy; | 
| 5884 | 539 | fun meth src = | 
| 540 | let | |
| 541 | val ((raw_name, _), pos) = Args.dest_src src; | |
| 542 | val name = NameSpace.intern space raw_name; | |
| 543 | in | |
| 5824 | 544 | (case Symtab.lookup (meths, name) of | 
| 15531 | 545 |           NONE => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
 | 
| 546 | | SOME ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src)) | |
| 5824 | 547 | end; | 
| 548 | in meth end; | |
| 549 | ||
| 550 | ||
| 9194 | 551 | (* add_method(s) *) | 
| 5824 | 552 | |
| 553 | fun add_methods raw_meths thy = | |
| 554 | let | |
| 16145 | 555 | val sg = Theory.sign_of thy; | 
| 556 | val new_meths = raw_meths |> map (fn (name, f, comment) => | |
| 16347 | 557 | (name, ((f, comment), stamp ()))); | 
| 5824 | 558 | |
| 16347 | 559 | fun add meths = NameSpace.extend_table (Sign.naming_of sg) (meths, new_meths) | 
| 560 | handle Symtab.DUPS dups => | |
| 561 |         error ("Duplicate declaration of method(s) " ^ commas_quote dups);
 | |
| 562 | in MethodsData.map add thy end; | |
| 5824 | 563 | |
| 9194 | 564 | val add_method = add_methods o Library.single; | 
| 565 | ||
| 5824 | 566 | (*implicit version*) | 
| 567 | fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]); | |
| 568 | ||
| 569 | ||
| 5884 | 570 | |
| 571 | (** method syntax **) | |
| 5824 | 572 | |
| 5884 | 573 | (* basic *) | 
| 574 | ||
| 575 | fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) = | |
| 576 | Args.syntax "method" scan; | |
| 5824 | 577 | |
| 8351 | 578 | fun simple_args scan f src ctxt : Proof.method = | 
| 579 | #2 (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt); | |
| 580 | ||
| 7555 | 581 | fun ctxt_args (f: Proof.context -> Proof.method) src ctxt = | 
| 8282 | 582 | #2 (syntax (Scan.succeed (f ctxt)) src ctxt); | 
| 7555 | 583 | |
| 584 | fun no_args m = ctxt_args (K m); | |
| 5884 | 585 | |
| 586 | ||
| 587 | (* sections *) | |
| 5824 | 588 | |
| 7268 | 589 | type modifier = (Proof.context -> Proof.context) * Proof.context attribute; | 
| 590 | ||
| 591 | local | |
| 592 | ||
| 8381 | 593 | fun sect ss = Scan.first (map Scan.lift ss); | 
| 5884 | 594 | fun thms ss = Scan.unless (sect ss) Attrib.local_thms; | 
| 15570 | 595 | fun thmss ss = Scan.repeat (thms ss) >> List.concat; | 
| 5884 | 596 | |
| 7268 | 597 | fun apply (f, att) (ctxt, ths) = Thm.applys_attributes ((f ctxt, ths), [att]); | 
| 5824 | 598 | |
| 7268 | 599 | fun section ss = (sect ss -- thmss ss) :-- (fn (m, ths) => Scan.depend (fn ctxt => | 
| 600 | Scan.succeed (apply m (ctxt, ths)))) >> #2; | |
| 5884 | 601 | |
| 7601 | 602 | fun sectioned args ss = args -- Scan.repeat (section ss); | 
| 5884 | 603 | |
| 7268 | 604 | in | 
| 5824 | 605 | |
| 5884 | 606 | fun sectioned_args args ss f src ctxt = | 
| 8282 | 607 | let val (ctxt', (x, _)) = syntax (sectioned args ss) src ctxt | 
| 5921 | 608 | in f x ctxt' end; | 
| 5884 | 609 | |
| 7601 | 610 | fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f; | 
| 9777 | 611 | fun bang_sectioned_args' ss scan f = | 
| 612 | sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f); | |
| 7601 | 613 | fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f); | 
| 7268 | 614 | |
| 8093 | 615 | fun thms_ctxt_args f = sectioned_args (thmss []) [] f; | 
| 616 | fun thms_args f = thms_ctxt_args (K o f); | |
| 9706 | 617 | fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected"); | 
| 5824 | 618 | |
| 7268 | 619 | end; | 
| 620 | ||
| 5824 | 621 | |
| 12347 | 622 | (* rules syntax *) | 
| 623 | ||
| 624 | local | |
| 625 | ||
| 626 | val introN = "intro"; | |
| 627 | val elimN = "elim"; | |
| 628 | val destN = "dest"; | |
| 629 | val ruleN = "rule"; | |
| 630 | ||
| 631 | fun modifier name kind kind' att = | |
| 15531 | 632 | Args.$$$ name |-- (kind >> K NONE || kind' |-- Args.nat --| Args.colon >> SOME) | 
| 12347 | 633 | >> (pair (I: Proof.context -> Proof.context) o att); | 
| 634 | ||
| 635 | val rules_modifiers = | |
| 12384 | 636 | [modifier destN Args.bang_colon Args.bang ContextRules.dest_bang_local, | 
| 12350 | 637 | modifier destN Args.colon (Scan.succeed ()) ContextRules.dest_local, | 
| 638 | modifier elimN Args.bang_colon Args.bang ContextRules.elim_bang_local, | |
| 639 | modifier elimN Args.colon (Scan.succeed ()) ContextRules.elim_local, | |
| 640 | modifier introN Args.bang_colon Args.bang ContextRules.intro_bang_local, | |
| 641 | modifier introN Args.colon (Scan.succeed ()) ContextRules.intro_local, | |
| 642 | Args.del -- Args.colon >> K (I, ContextRules.rule_del_local)]; | |
| 12347 | 643 | |
| 644 | in | |
| 645 | ||
| 646 | fun rules_args m = bang_sectioned_args' rules_modifiers (Scan.lift (Scan.option Args.nat)) m; | |
| 647 | ||
| 648 | fun rules_meth n prems ctxt = METHOD (fn facts => | |
| 12350 | 649 | HEADGOAL (insert_tac (prems @ facts) THEN' ObjectLogic.atomize_tac THEN' rules_tac ctxt n)); | 
| 12347 | 650 | |
| 651 | end; | |
| 652 | ||
| 653 | ||
| 9539 | 654 | (* tactic syntax *) | 
| 8238 | 655 | |
| 10744 | 656 | fun nat_thms_args f = uncurry f oo | 
| 657 | (#2 oo syntax (Scan.lift (Scan.optional (Args.parens Args.nat) 0) -- Attrib.local_thmss)); | |
| 658 | ||
| 8238 | 659 | val insts = | 
| 9539 | 660 | Scan.optional | 
| 9565 
3eb2ea15cc69
res_inst: include non-inst versions with multiple thms;
 wenzelm parents: 
9539diff
changeset | 661 | (Args.enum1 "and" (Scan.lift (Args.name -- (Args.$$$ "=" |-- Args.!!! Args.name))) --| | 
| 
3eb2ea15cc69
res_inst: include non-inst versions with multiple thms;
 wenzelm parents: 
9539diff
changeset | 662 | Scan.lift (Args.$$$ "in")) [] -- Attrib.local_thmss; | 
| 8238 | 663 | |
| 12119 | 664 | fun inst_args f src ctxt = f ctxt (#2 (syntax (Args.goal_spec HEADGOAL -- insts) src ctxt)); | 
| 8537 | 665 | |
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 666 | val insts_var = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 667 | Scan.optional | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 668 | (Args.enum1 "and" (Scan.lift (Args.var -- (Args.$$$ "=" |-- Args.!!! Args.name))) --| | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 669 | Scan.lift (Args.$$$ "in")) [] -- Attrib.local_thmss; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 670 | |
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 671 | fun inst_args_var f src ctxt = f ctxt (#2 (syntax (Args.goal_spec HEADGOAL -- insts_var) src ctxt)); | 
| 8537 | 672 | |
| 12119 | 673 | fun goal_args' args tac src ctxt = #2 (syntax (Args.goal_spec HEADGOAL -- args >> | 
| 14215 
ebf291f3b449
Improvements to Isar/Locales:  premises generated by "includes" elements
 ballarin parents: 
14174diff
changeset | 674 | (fn (quant, s) => SIMPLE_METHOD' quant (tac s))) src ctxt); | 
| 8537 | 675 | |
| 9539 | 676 | fun goal_args args tac = goal_args' (Scan.lift args) tac; | 
| 8238 | 677 | |
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 678 | fun goal_args_ctxt' args tac src ctxt = | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 679 | #2 (syntax (Args.goal_spec HEADGOAL -- args >> | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 680 | (fn (quant, s) => SIMPLE_METHOD' quant (tac ctxt s))) src ctxt); | 
| 8238 | 681 | |
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 682 | fun goal_args_ctxt args tac = goal_args_ctxt' (Scan.lift args) tac; | 
| 5824 | 683 | |
| 14718 | 684 | |
| 5824 | 685 | (** method text **) | 
| 686 | ||
| 687 | (* datatype text *) | |
| 688 | ||
| 689 | datatype text = | |
| 690 | Basic of (Proof.context -> Proof.method) | | |
| 15703 | 691 | Source of src | | 
| 5824 | 692 | Then of text list | | 
| 693 | Orelse of text list | | |
| 694 | Try of text | | |
| 695 | Repeat1 of text; | |
| 696 | ||
| 697 | ||
| 698 | (* refine *) | |
| 699 | ||
| 8238 | 700 | fun gen_refine f text state = | 
| 5824 | 701 | let | 
| 702 | val thy = Proof.theory_of state; | |
| 703 | ||
| 8238 | 704 | fun eval (Basic mth) = f mth | 
| 705 | | eval (Source src) = f (method thy src) | |
| 5824 | 706 | | eval (Then txts) = Seq.EVERY (map eval txts) | 
| 707 | | eval (Orelse txts) = Seq.FIRST (map eval txts) | |
| 708 | | eval (Try txt) = Seq.TRY (eval txt) | |
| 709 | | eval (Repeat1 txt) = Seq.REPEAT1 (eval txt); | |
| 710 | in eval text state end; | |
| 711 | ||
| 8238 | 712 | val refine = gen_refine Proof.refine; | 
| 713 | val refine_end = gen_refine Proof.refine_end; | |
| 6404 | 714 | |
| 5824 | 715 | |
| 6404 | 716 | (* structured proof steps *) | 
| 5824 | 717 | |
| 7506 | 718 | val default_text = Source (Args.src (("default", []), Position.none));
 | 
| 8195 | 719 | val this_text = Basic (K this); | 
| 9706 | 720 | val done_text = Basic (K (SIMPLE_METHOD all_tac)); | 
| 7555 | 721 | |
| 8966 | 722 | fun close_text asm = Basic (fn ctxt => METHOD (K | 
| 723 | (FILTER Thm.no_prems ((if asm then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac)))); | |
| 724 | ||
| 15531 | 725 | fun finish_text asm NONE = close_text asm | 
| 726 | | finish_text asm (SOME txt) = Then [txt, close_text asm]; | |
| 6872 | 727 | |
| 5824 | 728 | fun proof opt_text state = | 
| 729 | state | |
| 730 | |> Proof.assert_backward | |
| 15973 | 731 | |> refine (if_none opt_text default_text) | 
| 8242 | 732 | |> Seq.map (Proof.goal_facts (K [])) | 
| 5824 | 733 | |> Seq.map Proof.enter_forward; | 
| 734 | ||
| 8966 | 735 | fun local_qed asm opt_text = Proof.local_qed (refine (finish_text asm opt_text)); | 
| 736 | fun local_terminal_proof (text, opt_text) pr = | |
| 15531 | 737 | Seq.THEN (proof (SOME text), local_qed true opt_text pr); | 
| 738 | val local_default_proof = local_terminal_proof (default_text, NONE); | |
| 739 | val local_immediate_proof = local_terminal_proof (this_text, NONE); | |
| 740 | fun local_done_proof pr = Seq.THEN (proof (SOME done_text), local_qed false NONE pr); | |
| 5824 | 741 | |
| 6872 | 742 | |
| 8966 | 743 | fun global_qeds asm opt_text = Proof.global_qed (refine (finish_text asm opt_text)); | 
| 5824 | 744 | |
| 8966 | 745 | fun global_qed asm opt_text state = | 
| 6872 | 746 | state | 
| 8966 | 747 | |> global_qeds asm opt_text | 
| 6872 | 748 | |> Proof.check_result "Failed to finish proof" state | 
| 749 | |> Seq.hd; | |
| 750 | ||
| 8966 | 751 | fun global_term_proof asm (text, opt_text) state = | 
| 6872 | 752 | state | 
| 15531 | 753 | |> proof (SOME text) | 
| 6872 | 754 | |> Proof.check_result "Terminal proof method failed" state | 
| 8966 | 755 | |> (Seq.flat o Seq.map (global_qeds asm opt_text)) | 
| 6872 | 756 | |> Proof.check_result "Failed to finish proof (after successful terminal method)" state | 
| 757 | |> Seq.hd; | |
| 758 | ||
| 8966 | 759 | val global_terminal_proof = global_term_proof true; | 
| 15531 | 760 | val global_default_proof = global_terminal_proof (default_text, NONE); | 
| 761 | val global_immediate_proof = global_terminal_proof (this_text, NONE); | |
| 762 | val global_done_proof = global_term_proof false (done_text, NONE); | |
| 5824 | 763 | |
| 764 | ||
| 9539 | 765 | (* misc tactic emulations *) | 
| 766 | ||
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 767 | val subgoal_meth = goal_args_ctxt (Scan.repeat1 Args.name) subgoals_tac; | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 768 | val thin_meth = goal_args_ctxt Args.name thin_tac; | 
| 9539 | 769 | val rename_meth = goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac; | 
| 9631 | 770 | val rotate_meth = goal_args (Scan.optional Args.int 1) Tactic.rotate_tac; | 
| 9539 | 771 | |
| 772 | ||
| 5824 | 773 | (* pure_methods *) | 
| 774 | ||
| 775 | val pure_methods = | |
| 776 |  [("fail", no_args fail, "force failure"),
 | |
| 777 |   ("succeed", no_args succeed, "succeed"),
 | |
| 9587 | 778 |   ("-", no_args insert_facts, "do nothing (insert current facts only)"),
 | 
| 9539 | 779 |   ("insert", thms_args insert, "insert theorems, ignoring facts (improper)"),
 | 
| 16145 | 780 |   ("unfold", thms_args unfold_meth, "unfold definitions"),
 | 
| 12384 | 781 |   ("intro", thms_args intro, "repeatedly apply introduction rules"),
 | 
| 782 |   ("elim", thms_args elim, "repeatedly apply elimination rules"),
 | |
| 16145 | 783 |   ("fold", thms_args fold_meth, "fold definitions"),
 | 
| 12829 | 784 |   ("atomize", (atomize o #2) oo syntax (Args.mode "full"),
 | 
| 11962 | 785 | "present local premises as object-level statements"), | 
| 12347 | 786 |   ("rules", rules_args rules_meth, "apply many rules, including proof search"),
 | 
| 12384 | 787 |   ("rule", thms_ctxt_args some_rule, "apply some intro/elim rule"),
 | 
| 10744 | 788 |   ("erule", nat_thms_args erule, "apply rule in elimination manner (improper)"),
 | 
| 789 |   ("drule", nat_thms_args drule, "apply rule in destruct manner (improper)"),
 | |
| 790 |   ("frule", nat_thms_args frule, "apply rule in forward manner (improper)"),
 | |
| 8195 | 791 |   ("this", no_args this, "apply current facts as rules"),
 | 
| 8238 | 792 |   ("assumption", ctxt_args assumption, "proof by assumption, preferring facts"),
 | 
| 14174 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 793 |   ("rule_tac", inst_args_var res_inst_meth, "apply rule (dynamic instantiation)"),
 | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 794 |   ("erule_tac", inst_args_var eres_inst_meth, "apply rule in elimination manner (dynamic instantiation)"),
 | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 795 |   ("drule_tac", inst_args_var dres_inst_meth, "apply rule in destruct manner (dynamic instantiation)"),
 | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 796 |   ("frule_tac", inst_args_var forw_inst_meth, "apply rule in forward manner (dynamic instantiation)"),
 | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 797 |   ("cut_tac", inst_args_var cut_inst_meth, "cut rule (dynamic instantiation)"),
 | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 798 |   ("subgoal_tac", subgoal_meth, "insert subgoal (dynamic instantiation)"),
 | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 799 |   ("thin_tac", thin_meth, "remove premise (dynamic instantiation)"),
 | 
| 
f3cafd2929d5
Methods rule_tac etc support static (Isar) contexts.
 ballarin parents: 
13650diff
changeset | 800 |   ("rename_tac", rename_meth, "rename parameters of goal (dynamic instantiation)"),
 | 
| 9631 | 801 |   ("rotate_tac", rotate_meth, "rotate assumptions of goal"),
 | 
| 8351 | 802 |   ("prolog", thms_args prolog, "simple prolog interpreter"),
 | 
| 803 |   ("tactic", simple_args Args.name tactic, "ML tactic as proof method")];
 | |
| 5824 | 804 | |
| 15801 | 805 | val _ = Context.add_setup [add_methods pure_methods]; | 
| 5824 | 806 | |
| 807 | ||
| 16145 | 808 | (*final declarations of this structure!*) | 
| 809 | val unfold = unfold_meth; | |
| 810 | val fold = fold_meth; | |
| 811 | ||
| 5824 | 812 | end; | 
| 813 | ||
| 814 | structure BasicMethod: BASIC_METHOD = Method; | |
| 815 | open BasicMethod; |