src/Pure/Isar/method.ML
author wenzelm
Tue Sep 19 23:53:00 2000 +0200 (2000-09-19)
changeset 10034 4bca6b2d2589
parent 10008 61eb9f3aa92a
child 10309 a7f961fb62c6
permissions -rw-r--r--
tuned args;
     1 (*  Title:      Pure/Isar/method.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Proof methods.
     7 *)
     8 
     9 signature BASIC_METHOD =
    10 sig
    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
    18   val print_global_rules: theory -> unit
    19   val print_local_rules: Proof.context -> unit
    20   val dest_global: theory attribute
    21   val elim_global: theory attribute
    22   val intro_global: theory attribute
    23   val rule_del_global: theory attribute
    24   val dest_local: Proof.context attribute
    25   val elim_local: Proof.context attribute
    26   val intro_local: Proof.context attribute
    27   val rule_del_local: Proof.context attribute
    28   val METHOD: (thm list -> tactic) -> Proof.method
    29   val METHOD_CASES:
    30     (thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq) -> Proof.method
    31   val SIMPLE_METHOD: tactic -> Proof.method
    32   val SIMPLE_METHOD': ((int -> tactic) -> tactic) -> (int -> tactic) -> Proof.method
    33   val fail: Proof.method
    34   val succeed: Proof.method
    35   val defer: int option -> Proof.method
    36   val prefer: int -> Proof.method
    37   val insert_tac: thm list -> int -> tactic
    38   val insert: thm list -> Proof.method
    39   val insert_facts: Proof.method
    40   val unfold: thm list -> Proof.method
    41   val fold: thm list -> Proof.method
    42   val atomize_tac: thm list -> int -> tactic
    43   val atomize_goal: thm list -> int -> thm -> thm
    44   val multi_resolve: thm list -> thm -> thm Seq.seq
    45   val multi_resolves: thm list -> thm list -> thm Seq.seq
    46   val resolveq_tac: thm Seq.seq -> int -> tactic
    47   val resolveq_cases_tac: bool -> (thm * string list) Seq.seq
    48     -> int -> thm -> (thm * (string * RuleCases.T) list) Seq.seq
    49   val rule_tac: thm list -> thm list -> int -> tactic
    50   val erule_tac: thm list -> thm list -> int -> tactic
    51   val rule: thm list -> Proof.method
    52   val erule: thm list -> Proof.method
    53   val drule: thm list -> Proof.method
    54   val frule: thm list -> Proof.method
    55   val this: Proof.method
    56   val assumption: Proof.context -> Proof.method
    57   val set_tactic: (Proof.context -> thm list -> tactic) -> unit
    58   val tactic: string -> Proof.context -> Proof.method
    59   exception METHOD_FAIL of (string * Position.T) * exn
    60   val method: theory -> Args.src -> Proof.context -> Proof.method
    61   val add_method: bstring * (Args.src -> Proof.context -> Proof.method) * string
    62     -> theory -> theory
    63   val add_methods: (bstring * (Args.src -> Proof.context -> Proof.method) * string) list
    64     -> theory -> theory
    65   val syntax: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
    66     Args.src -> Proof.context -> Proof.context * 'a
    67   val simple_args: (Args.T list -> 'a * Args.T list)
    68     -> ('a -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    69   val ctxt_args: (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    70   val no_args: Proof.method -> Args.src -> Proof.context -> Proof.method
    71   type modifier
    72   val sectioned_args: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
    73     (Args.T list -> modifier * Args.T list) list ->
    74     ('a -> Proof.context -> 'b) -> Args.src -> Proof.context -> 'b
    75   val bang_sectioned_args:
    76     (Args.T list -> modifier * Args.T list) list ->
    77     (thm list -> Proof.context -> 'a) -> Args.src -> Proof.context -> 'a
    78   val bang_sectioned_args':
    79     (Args.T list -> modifier * Args.T list) list ->
    80     (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
    81     ('a -> thm list -> Proof.context -> 'b) -> Args.src -> Proof.context -> 'b
    82   val only_sectioned_args:
    83     (Args.T list -> modifier * Args.T list) list ->
    84     (Proof.context -> 'a) -> Args.src -> Proof.context -> 'a
    85   val thms_ctxt_args: (thm list -> Proof.context -> 'a) -> Args.src -> Proof.context -> 'a
    86   val thms_args: (thm list -> 'a) -> Args.src -> Proof.context -> 'a
    87   val thm_args: (thm -> 'a) -> Args.src -> Proof.context -> 'a
    88   datatype text =
    89     Basic of (Proof.context -> Proof.method) |
    90     Source of Args.src |
    91     Then of text list |
    92     Orelse of text list |
    93     Try of text |
    94     Repeat1 of text
    95   val refine: text -> Proof.state -> Proof.state Seq.seq
    96   val refine_end: text -> Proof.state -> Proof.state Seq.seq
    97   val proof: text option -> Proof.state -> Proof.state Seq.seq
    98   val local_qed: bool -> text option
    99     -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
   100     -> Proof.state -> Proof.state Seq.seq
   101   val local_terminal_proof: text * text option
   102     -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
   103     -> Proof.state -> Proof.state Seq.seq
   104   val local_default_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
   105     -> Proof.state -> Proof.state Seq.seq
   106   val local_immediate_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
   107     -> Proof.state -> Proof.state Seq.seq
   108   val local_done_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
   109     -> Proof.state -> Proof.state Seq.seq
   110   val global_qed: bool -> text option
   111     -> Proof.state -> theory * {kind: string, name: string, thm: thm}
   112   val global_terminal_proof: text * text option
   113     -> Proof.state -> theory * {kind: string, name: string, thm: thm}
   114   val global_default_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
   115   val global_immediate_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
   116   val global_done_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
   117   val goal_args: (Args.T list -> 'a * Args.T list) -> ('a -> int -> tactic)
   118     -> Args.src -> Proof.context -> Proof.method
   119   val goal_args': (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))
   120     -> ('a -> int -> tactic) -> Args.src -> Proof.context -> Proof.method
   121   val setup: (theory -> theory) list
   122 end;
   123 
   124 structure Method: METHOD =
   125 struct
   126 
   127 
   128 (** global and local rule data **)
   129 
   130 local
   131   fun prt_rules kind sg ths =
   132     Pretty.writeln (Pretty.big_list ("standard " ^ kind ^ " rules:")
   133       (map (Display.pretty_thm_sg sg) ths));
   134 in
   135   fun print_rules sg (intro, elim) =
   136     (prt_rules "introduction" sg intro; prt_rules "elimination" sg elim);
   137 end;
   138 
   139 
   140 (* theory data kind 'Isar/rules' *)
   141 
   142 structure GlobalRulesArgs =
   143 struct
   144   val name = "Isar/rules";
   145   type T = thm list * thm list;
   146 
   147   val empty = ([], []);
   148   val copy = I;
   149   val prep_ext = I;
   150   fun merge ((intro1, elim1), (intro2, elim2)) =
   151     (Drule.merge_rules (intro1, intro2), Drule.merge_rules (elim1, elim2));
   152   val print = print_rules;
   153 end;
   154 
   155 structure GlobalRules = TheoryDataFun(GlobalRulesArgs);
   156 val print_global_rules = GlobalRules.print;
   157 
   158 
   159 (* proof data kind 'Isar/rules' *)
   160 
   161 structure LocalRulesArgs =
   162 struct
   163   val name = "Isar/rules";
   164   type T = thm list * thm list;
   165 
   166   val init = GlobalRules.get;
   167   val print = print_rules o ProofContext.sign_of;
   168 end;
   169 
   170 structure LocalRules = ProofDataFun(LocalRulesArgs);
   171 val print_local_rules = LocalRules.print;
   172 
   173 
   174 
   175 (** attributes **)
   176 
   177 (* add rules *)
   178 
   179 local
   180 
   181 fun add_dest th (intro, elim) = (intro, Drule.add_rules [Tactic.make_elim th] elim);
   182 fun add_elim th (intro, elim) = (intro, Drule.add_rules [th] elim);
   183 fun add_intro th (intro, elim) = (Drule.add_rules [th] intro, elim);
   184 
   185 fun del_rule th (intro, elim) =
   186   let
   187     val th' = Tactic.make_elim th;
   188     val del = Drule.del_rules [th'] o Drule.del_rules [th];
   189   in (del intro, del elim) end;
   190 
   191 fun mk_att f g (x, th) = (f (g th) x, th);
   192 
   193 in
   194 
   195 val dest_global = mk_att GlobalRules.map add_dest;
   196 val elim_global = mk_att GlobalRules.map add_elim;
   197 val intro_global = mk_att GlobalRules.map add_intro;
   198 val rule_del_global = mk_att GlobalRules.map del_rule;
   199 
   200 val dest_local = mk_att LocalRules.map add_dest;
   201 val elim_local = mk_att LocalRules.map add_elim;
   202 val intro_local = mk_att LocalRules.map add_intro;
   203 val rule_del_local = mk_att LocalRules.map del_rule;
   204 
   205 fun del_args att = Attrib.syntax (Scan.lift Args.del >> K att);
   206 
   207 end;
   208 
   209 
   210 (* concrete syntax *)
   211 
   212 val rule_atts =
   213  [("dest", (Attrib.no_args dest_global, Attrib.no_args dest_local),
   214     "declaration of destruction rule"),
   215   ("elim", (Attrib.no_args elim_global, Attrib.no_args elim_local),
   216     "declaration of elimination rule"),
   217   ("intro", (Attrib.no_args intro_global, Attrib.no_args intro_local),
   218     "declaration of introduction rule"),
   219   ("rule", (del_args rule_del_global, del_args rule_del_local),
   220     "remove declaration of elim/intro rule")];
   221 
   222 
   223 
   224 (** proof methods **)
   225 
   226 (* make methods *)
   227 
   228 val METHOD = Proof.method;
   229 val METHOD_CASES = Proof.method_cases;
   230 
   231 
   232 (* primitive *)
   233 
   234 val fail = METHOD (K no_tac);
   235 val succeed = METHOD (K all_tac);
   236 
   237 
   238 (* shuffle *)
   239 
   240 fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1)));
   241 fun defer opt_i = METHOD (K (Tactic.defer_tac (if_none opt_i 1)));
   242 
   243 
   244 (* insert *)
   245 
   246 local
   247 
   248 fun cut_rule_tac raw_rule =
   249   let
   250     val rule = Drule.forall_intr_vars raw_rule;
   251     val revcut_rl = Drule.incr_indexes_wrt [] [] [] [rule] Drule.revcut_rl;
   252   in Tactic.rtac (rule COMP revcut_rl) end;
   253 
   254 in
   255 
   256 fun insert_tac [] i = all_tac
   257   | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
   258 
   259 val insert_facts = METHOD (ALLGOALS o insert_tac);
   260 fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
   261 
   262 end;
   263 
   264 
   265 (* simple methods *)
   266 
   267 fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac);
   268 fun SIMPLE_METHOD' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac));
   269 
   270 
   271 (* unfold / fold definitions *)
   272 
   273 fun unfold thms = SIMPLE_METHOD (CHANGED (rewrite_goals_tac thms));
   274 fun fold thms = SIMPLE_METHOD (CHANGED (fold_goals_tac thms));
   275 
   276 
   277 (* atomize meta-connectives *)
   278 
   279 fun atomize_tac rews i thm =
   280   if Logic.has_meta_prems (#prop (Thm.rep_thm thm)) i then
   281     Tactic.asm_rewrite_goal_tac (true, false, false) (K no_tac) (Thm.mss_of rews) i thm
   282   else all_tac thm;
   283 
   284 fun atomize_goal rews i thm =
   285   (case Seq.pull (atomize_tac rews i thm) of
   286     None => thm
   287   | Some (thm', _) => thm');
   288 
   289 
   290 (* multi_resolve *)
   291 
   292 local
   293 
   294 fun res th i rule =
   295   Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty;
   296 
   297 fun multi_res _ [] rule = Seq.single rule
   298   | multi_res i (th :: ths) rule = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths rule));
   299 
   300 in
   301 
   302 val multi_resolve = multi_res 1;
   303 fun multi_resolves facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules));
   304 
   305 end;
   306 
   307 
   308 (* general rule *)
   309 
   310 fun gen_resolveq_tac tac rules i st =
   311   Seq.flat (Seq.map (fn rule => tac rule i st) rules);
   312 
   313 val resolveq_tac = gen_resolveq_tac Tactic.rtac;
   314 
   315 fun resolveq_cases_tac open_parms = gen_resolveq_tac (fn (rule, cases) => fn i => fn st =>
   316   Seq.map (rpair (RuleCases.make (not open_parms) rule cases)) (Tactic.rtac rule i st));
   317 
   318 
   319 (* simple rule *)
   320 
   321 local
   322 
   323 fun gen_rule_tac tac rules [] = tac rules
   324   | gen_rule_tac tac erules facts = gen_resolveq_tac (tac o single) (multi_resolves facts erules);
   325 
   326 fun gen_rule tac rules = METHOD (HEADGOAL o tac rules);
   327 
   328 fun gen_rule' tac arg_rules ctxt = METHOD (fn facts =>
   329   let val rules =
   330     if not (null arg_rules) then arg_rules
   331     else if null facts then #1 (LocalRules.get ctxt)
   332     else op @ (LocalRules.get ctxt);
   333   in HEADGOAL (tac rules facts) end);
   334 
   335 fun setup raw_tac =
   336   let val tac = gen_rule_tac raw_tac
   337   in (tac, gen_rule tac, gen_rule' tac) end;
   338 
   339 in
   340 
   341 val (rule_tac, rule, some_rule) = setup Tactic.resolve_tac;
   342 val (erule_tac, erule, some_erule) = setup Tactic.eresolve_tac;
   343 val (drule_tac, drule, some_drule) = setup Tactic.dresolve_tac;
   344 val (frule_tac, frule, some_frule) = setup Tactic.forward_tac;
   345 
   346 end;
   347 
   348 
   349 (* this *)
   350 
   351 val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac));
   352 
   353 
   354 (* assumption *)
   355 
   356 fun assm_tac ctxt =
   357   assume_tac APPEND' resolve_tac (filter Thm.no_prems (ProofContext.prems_of ctxt));
   358 
   359 fun assumption_tac ctxt [] = assm_tac ctxt
   360   | assumption_tac _ [fact] = resolve_tac [fact]
   361   | assumption_tac _ _ = K no_tac;
   362 
   363 fun assumption ctxt = METHOD (HEADGOAL o assumption_tac ctxt);
   364 
   365 
   366 (* res_inst_tac etc. *)
   367 
   368 (*Note: insts refer to the implicit (!!) goal context; use at your own risk*)
   369 fun gen_res_inst _ tac (quant, ([], thms)) =
   370       METHOD (fn facts => (quant (insert_tac facts THEN' tac thms)))
   371   | gen_res_inst tac _ (quant, (insts, [thm])) =
   372       METHOD (fn facts => (quant (insert_tac facts THEN' tac insts thm)))
   373   | gen_res_inst _ _ _ = error "Cannot have instantiations with multiple rules";
   374 
   375 val res_inst = gen_res_inst Tactic.res_inst_tac Tactic.resolve_tac;
   376 val eres_inst = gen_res_inst Tactic.eres_inst_tac Tactic.eresolve_tac;
   377 val dres_inst = gen_res_inst Tactic.dres_inst_tac Tactic.dresolve_tac;
   378 val forw_inst = gen_res_inst Tactic.forw_inst_tac Tactic.forward_tac;
   379 val cut_inst = gen_res_inst Tactic.cut_inst_tac Tactic.cut_facts_tac;
   380 
   381 
   382 (* simple Prolog interpreter *)
   383 
   384 fun prolog_tac rules facts =
   385   DEPTH_SOLVE_1 (HEADGOAL (Tactic.assume_tac APPEND' Tactic.resolve_tac (facts @ rules)));
   386 
   387 val prolog = METHOD o prolog_tac;
   388 
   389 
   390 (* ML tactics *)
   391 
   392 val tactic_ref = ref ((fn _ => raise Match): Proof.context -> thm list -> tactic);
   393 fun set_tactic f = tactic_ref := f;
   394 
   395 fun tactic txt ctxt = METHOD (fn facts =>
   396   (Context.use_mltext
   397     ("let fun tactic (ctxt: PureIsar.Proof.context) (facts: thm list) : tactic = \
   398      \let val thm = PureIsar.ProofContext.get_thm_closure ctxt\n\
   399      \  and thms = PureIsar.ProofContext.get_thms_closure ctxt in\n"
   400      ^ txt ^
   401      "\nend in PureIsar.Method.set_tactic tactic end")
   402     false None;
   403     Context.setmp (Some (ProofContext.theory_of ctxt)) (! tactic_ref ctxt) facts));
   404 
   405 
   406 
   407 (** methods theory data **)
   408 
   409 (* data kind 'Isar/methods' *)
   410 
   411 structure MethodsDataArgs =
   412 struct
   413   val name = "Isar/methods";
   414   type T =
   415     {space: NameSpace.T,
   416      meths: (((Args.src -> Proof.context -> Proof.method) * string) * stamp) Symtab.table};
   417 
   418   val empty = {space = NameSpace.empty, meths = Symtab.empty};
   419   val copy = I;
   420   val prep_ext = I;
   421   fun merge ({space = space1, meths = meths1}, {space = space2, meths = meths2}) =
   422     {space = NameSpace.merge (space1, space2),
   423       meths = Symtab.merge eq_snd (meths1, meths2) handle Symtab.DUPS dups =>
   424         error ("Attempt to merge different versions of methods " ^ commas_quote dups)};
   425 
   426   fun print _ {space, meths} =
   427     let
   428       fun prt_meth (name, ((_, comment), _)) = Pretty.block
   429         [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   430     in
   431       [Pretty.big_list "methods:" (map prt_meth (NameSpace.cond_extern_table space meths))]
   432       |> Pretty.chunks |> Pretty.writeln
   433     end;
   434 end;
   435 
   436 structure MethodsData = TheoryDataFun(MethodsDataArgs);
   437 val print_methods = MethodsData.print;
   438 
   439 
   440 (* get methods *)
   441 
   442 exception METHOD_FAIL of (string * Position.T) * exn;
   443 
   444 fun method thy =
   445   let
   446     val {space, meths} = MethodsData.get thy;
   447 
   448     fun meth src =
   449       let
   450         val ((raw_name, _), pos) = Args.dest_src src;
   451         val name = NameSpace.intern space raw_name;
   452       in
   453         (case Symtab.lookup (meths, name) of
   454           None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
   455         | Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
   456       end;
   457   in meth end;
   458 
   459 
   460 (* add_method(s) *)
   461 
   462 fun add_methods raw_meths thy =
   463   let
   464     val full = Sign.full_name (Theory.sign_of thy);
   465     val new_meths =
   466       map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths;
   467 
   468     val {space, meths} = MethodsData.get thy;
   469     val space' = NameSpace.extend (space, map fst new_meths);
   470     val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups =>
   471       error ("Duplicate declaration of method(s) " ^ commas_quote dups);
   472   in
   473     thy |> MethodsData.put {space = space', meths = meths'}
   474   end;
   475 
   476 val add_method = add_methods o Library.single;
   477 
   478 (*implicit version*)
   479 fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]);
   480 
   481 
   482 
   483 (** method syntax **)
   484 
   485 (* basic *)
   486 
   487 fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
   488   Args.syntax "method" scan;
   489 
   490 fun simple_args scan f src ctxt : Proof.method =
   491   #2 (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt);
   492 
   493 fun ctxt_args (f: Proof.context -> Proof.method) src ctxt =
   494   #2 (syntax (Scan.succeed (f ctxt)) src ctxt);
   495 
   496 fun no_args m = ctxt_args (K m);
   497 
   498 
   499 
   500 (* sections *)
   501 
   502 type modifier = (Proof.context -> Proof.context) * Proof.context attribute;
   503 
   504 local
   505 
   506 fun sect ss = Scan.first (map Scan.lift ss);
   507 fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
   508 fun thmss ss = Scan.repeat (thms ss) >> flat;
   509 
   510 fun apply (f, att) (ctxt, ths) = Thm.applys_attributes ((f ctxt, ths), [att]);
   511 
   512 fun section ss = (sect ss -- thmss ss) :-- (fn (m, ths) => Scan.depend (fn ctxt =>
   513   Scan.succeed (apply m (ctxt, ths)))) >> #2;
   514 
   515 fun sectioned args ss = args -- Scan.repeat (section ss);
   516 
   517 in
   518 
   519 fun sectioned_args args ss f src ctxt =
   520   let val (ctxt', (x, _)) = syntax (sectioned args ss) src ctxt
   521   in f x ctxt' end;
   522 
   523 fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f;
   524 fun bang_sectioned_args' ss scan f =
   525   sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f);
   526 fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f);
   527 
   528 fun thms_ctxt_args f = sectioned_args (thmss []) [] f;
   529 fun thms_args f = thms_ctxt_args (K o f);
   530 fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected");
   531 
   532 end;
   533 
   534 
   535 (* tactic syntax *)
   536 
   537 val insts =
   538   Scan.optional
   539     (Args.enum1 "and" (Scan.lift (Args.name -- (Args.$$$ "=" |-- Args.!!! Args.name))) --|
   540       Scan.lift (Args.$$$ "in")) [] -- Attrib.local_thmss;
   541 
   542 fun inst_args f = f oo (#2 oo syntax (Args.goal_spec HEADGOAL -- insts));
   543 
   544 
   545 fun goal_args' args tac = #2 oo syntax (Args.goal_spec HEADGOAL -- args >>
   546   (fn (quant, s) => SIMPLE_METHOD' quant (tac s)));
   547 
   548 fun goal_args args tac = goal_args' (Scan.lift args) tac;
   549 
   550 
   551 
   552 (** method text **)
   553 
   554 (* datatype text *)
   555 
   556 datatype text =
   557   Basic of (Proof.context -> Proof.method) |
   558   Source of Args.src |
   559   Then of text list |
   560   Orelse of text list |
   561   Try of text |
   562   Repeat1 of text;
   563 
   564 
   565 (* refine *)
   566 
   567 fun gen_refine f text state =
   568   let
   569     val thy = Proof.theory_of state;
   570 
   571     fun eval (Basic mth) = f mth
   572       | eval (Source src) = f (method thy src)
   573       | eval (Then txts) = Seq.EVERY (map eval txts)
   574       | eval (Orelse txts) = Seq.FIRST (map eval txts)
   575       | eval (Try txt) = Seq.TRY (eval txt)
   576       | eval (Repeat1 txt) = Seq.REPEAT1 (eval txt);
   577   in eval text state end;
   578 
   579 val refine = gen_refine Proof.refine;
   580 val refine_end = gen_refine Proof.refine_end;
   581 
   582 
   583 (* structured proof steps *)
   584 
   585 val default_text = Source (Args.src (("default", []), Position.none));
   586 val this_text = Basic (K this);
   587 val done_text = Basic (K (SIMPLE_METHOD all_tac));
   588 
   589 fun close_text asm = Basic (fn ctxt => METHOD (K
   590   (FILTER Thm.no_prems ((if asm then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac))));
   591 
   592 fun finish_text asm None = close_text asm
   593   | finish_text asm (Some txt) = Then [txt, close_text asm];
   594 
   595 fun proof opt_text state =
   596   state
   597   |> Proof.assert_backward
   598   |> refine (if_none opt_text default_text)
   599   |> Seq.map (Proof.goal_facts (K []))
   600   |> Seq.map Proof.enter_forward;
   601 
   602 fun local_qed asm opt_text = Proof.local_qed (refine (finish_text asm opt_text));
   603 fun local_terminal_proof (text, opt_text) pr =
   604   Seq.THEN (proof (Some text), local_qed true opt_text pr);
   605 val local_default_proof = local_terminal_proof (default_text, None);
   606 val local_immediate_proof = local_terminal_proof (this_text, None);
   607 fun local_done_proof pr = Seq.THEN (proof (Some done_text), local_qed false None pr);
   608 
   609 
   610 fun global_qeds asm opt_text = Proof.global_qed (refine (finish_text asm opt_text));
   611 
   612 fun global_qed asm opt_text state =
   613   state
   614   |> global_qeds asm opt_text
   615   |> Proof.check_result "Failed to finish proof" state
   616   |> Seq.hd;
   617 
   618 fun global_term_proof asm (text, opt_text) state =
   619   state
   620   |> proof (Some text)
   621   |> Proof.check_result "Terminal proof method failed" state
   622   |> (Seq.flat o Seq.map (global_qeds asm opt_text))
   623   |> Proof.check_result "Failed to finish proof (after successful terminal method)" state
   624   |> Seq.hd;
   625 
   626 val global_terminal_proof = global_term_proof true;
   627 val global_default_proof = global_terminal_proof (default_text, None);
   628 val global_immediate_proof = global_terminal_proof (this_text, None);
   629 val global_done_proof = global_term_proof false (done_text, None);
   630 
   631 
   632 (** theory setup **)
   633 
   634 (* misc tactic emulations *)
   635 
   636 val subgoal_meth = goal_args (Scan.repeat1 Args.name) Tactic.subgoals_tac;
   637 val thin_meth = goal_args Args.name Tactic.thin_tac;
   638 val rename_meth = goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac;
   639 val rotate_meth = goal_args (Scan.optional Args.int 1) Tactic.rotate_tac;
   640 
   641 
   642 (* pure_methods *)
   643 
   644 val pure_methods =
   645  [("fail", no_args fail, "force failure"),
   646   ("succeed", no_args succeed, "succeed"),
   647   ("-", no_args insert_facts, "do nothing (insert current facts only)"),
   648   ("insert", thms_args insert, "insert theorems, ignoring facts (improper)"),
   649   ("unfold", thms_args unfold, "unfold definitions"),
   650   ("fold", thms_args fold, "fold definitions"),
   651   ("default", thms_ctxt_args some_rule, "apply some rule"),
   652   ("rule", thms_ctxt_args some_rule, "apply some rule"),
   653   ("erule", thms_ctxt_args some_erule, "apply some rule in elimination manner (improper)"),
   654   ("drule", thms_ctxt_args some_drule, "apply some rule in destruct manner (improper)"),
   655   ("frule", thms_ctxt_args some_frule, "apply some rule in forward manner (improper)"),
   656   ("this", no_args this, "apply current facts as rules"),
   657   ("assumption", ctxt_args assumption, "proof by assumption, preferring facts"),
   658   ("rule_tac", inst_args res_inst, "apply rule (dynamic instantiation!)"),
   659   ("erule_tac", inst_args eres_inst, "apply rule in elimination manner (dynamic instantiation!)"),
   660   ("drule_tac", inst_args dres_inst, "apply rule in destruct manner (dynamic instantiation!)"),
   661   ("frule_tac", inst_args forw_inst, "apply rule in forward manner (dynamic instantiation!)"),
   662   ("cut_tac", inst_args cut_inst, "cut rule (dynamic instantiation!)"),
   663   ("subgoal_tac", subgoal_meth, "insert subgoal (dynamic instantiation!)"),
   664   ("thin_tac", thin_meth, "remove premise (dynamic instantiation!)"),
   665   ("rename_tac", rename_meth, "rename parameters of goal (dynamic instantiation!)"),
   666   ("rotate_tac", rotate_meth, "rotate assumptions of goal"),
   667   ("prolog", thms_args prolog, "simple prolog interpreter"),
   668   ("tactic", simple_args Args.name tactic, "ML tactic as proof method")];
   669 
   670 
   671 (* setup *)
   672 
   673 val setup =
   674  [GlobalRules.init, LocalRules.init, Attrib.add_attributes rule_atts,
   675   MethodsData.init, add_methods pure_methods];
   676 
   677 
   678 end;
   679 
   680 
   681 structure BasicMethod: BASIC_METHOD = Method;
   682 open BasicMethod;