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