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