src/Pure/Isar/method.ML
author wenzelm
Wed Aug 09 21:02:21 2000 +0200 (2000-08-09)
changeset 9565 3eb2ea15cc69
parent 9539 7ff8f3516d54
child 9587 1368ce019457
permissions -rw-r--r--
res_inst: include non-inst versions with multiple thms;
ML tactic: thms closure;
tuned msgs;
     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, ([], thms)) =
   352       METHOD (fn facts => (quant (insert_tac facts THEN' tac thms)))
   353   | gen_res_inst tac _ (quant, (insts, [thm])) =
   354       METHOD (fn facts => (quant (insert_tac facts THEN' tac insts thm)))
   355   | gen_res_inst _ _ _ = error "Cannot have instantiations with multiple rules";
   356 
   357 val res_inst = gen_res_inst Tactic.res_inst_tac Tactic.resolve_tac;
   358 val eres_inst = gen_res_inst Tactic.eres_inst_tac Tactic.eresolve_tac;
   359 val dres_inst = gen_res_inst Tactic.dres_inst_tac Tactic.dresolve_tac;
   360 val forw_inst = gen_res_inst Tactic.forw_inst_tac Tactic.forward_tac;
   361 val cut_inst = gen_res_inst Tactic.cut_inst_tac Tactic.cut_facts_tac;
   362 
   363 
   364 (* simple Prolog interpreter *)
   365 
   366 fun prolog_tac rules facts =
   367   DEPTH_SOLVE_1 (HEADGOAL (Tactic.assume_tac APPEND' Tactic.resolve_tac (facts @ rules)));
   368 
   369 val prolog = METHOD o prolog_tac;
   370 
   371 
   372 (* ML tactics *)
   373 
   374 val tactic_ref = ref ((fn _ => raise Match): Proof.context -> thm list -> tactic);
   375 fun set_tactic f = tactic_ref := f;
   376 
   377 fun tactic txt ctxt = METHOD (fn facts =>
   378  (Context.use_mltext
   379    ("let fun tactic (ctxt: PureIsar.Proof.context) (facts: thm list) : tactic = \
   380     \let val thm = PureIsar.ProofContext.get_thm_closure ctxt\n\
   381     \  and thms = PureIsar.ProofContext.get_thms_closure ctxt in\n"
   382     ^ txt ^
   383     "\nend in PureIsar.Method.set_tactic tactic end")
   384   false (Some (ProofContext.theory_of ctxt)); ! tactic_ref ctxt facts));
   385 
   386 
   387 
   388 (** methods theory data **)
   389 
   390 (* data kind 'Isar/methods' *)
   391 
   392 structure MethodsDataArgs =
   393 struct
   394   val name = "Isar/methods";
   395   type T =
   396     {space: NameSpace.T,
   397      meths: (((Args.src -> Proof.context -> Proof.method) * string) * stamp) Symtab.table};
   398 
   399   val empty = {space = NameSpace.empty, meths = Symtab.empty};
   400   val copy = I;
   401   val prep_ext = I;
   402   fun merge ({space = space1, meths = meths1}, {space = space2, meths = meths2}) =
   403     {space = NameSpace.merge (space1, space2),
   404       meths = Symtab.merge eq_snd (meths1, meths2) handle Symtab.DUPS dups =>
   405         error ("Attempt to merge different versions of methods " ^ commas_quote dups)};
   406 
   407   fun print _ {space, meths} =
   408     let
   409       fun prt_meth (name, ((_, comment), _)) = Pretty.block
   410         [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   411     in
   412       [Pretty.big_list "methods:" (map prt_meth (NameSpace.cond_extern_table space meths))]
   413       |> Pretty.chunks |> Pretty.writeln
   414     end;
   415 end;
   416 
   417 structure MethodsData = TheoryDataFun(MethodsDataArgs);
   418 val print_methods = MethodsData.print;
   419 
   420 
   421 (* get methods *)
   422 
   423 exception METHOD_FAIL of (string * Position.T) * exn;
   424 
   425 fun method thy =
   426   let
   427     val {space, meths} = MethodsData.get thy;
   428 
   429     fun meth src =
   430       let
   431         val ((raw_name, _), pos) = Args.dest_src src;
   432         val name = NameSpace.intern space raw_name;
   433       in
   434         (case Symtab.lookup (meths, name) of
   435           None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
   436         | Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
   437       end;
   438   in meth end;
   439 
   440 
   441 (* add_method(s) *)
   442 
   443 fun add_methods raw_meths thy =
   444   let
   445     val full = Sign.full_name (Theory.sign_of thy);
   446     val new_meths =
   447       map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths;
   448 
   449     val {space, meths} = MethodsData.get thy;
   450     val space' = NameSpace.extend (space, map fst new_meths);
   451     val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups =>
   452       error ("Duplicate declaration of method(s) " ^ commas_quote dups);
   453   in
   454     thy |> MethodsData.put {space = space', meths = meths'}
   455   end;
   456 
   457 val add_method = add_methods o Library.single;
   458 
   459 (*implicit version*)
   460 fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]);
   461 
   462 
   463 
   464 (** method syntax **)
   465 
   466 (* basic *)
   467 
   468 fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
   469   Args.syntax "method" scan;
   470 
   471 fun simple_args scan f src ctxt : Proof.method =
   472   #2 (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt);
   473 
   474 fun ctxt_args (f: Proof.context -> Proof.method) src ctxt =
   475   #2 (syntax (Scan.succeed (f ctxt)) src ctxt);
   476 
   477 fun no_args m = ctxt_args (K m);
   478 
   479 
   480 
   481 (* sections *)
   482 
   483 type modifier = (Proof.context -> Proof.context) * Proof.context attribute;
   484 
   485 local
   486 
   487 fun sect ss = Scan.first (map Scan.lift ss);
   488 fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
   489 fun thmss ss = Scan.repeat (thms ss) >> flat;
   490 
   491 fun apply (f, att) (ctxt, ths) = Thm.applys_attributes ((f ctxt, ths), [att]);
   492 
   493 fun section ss = (sect ss -- thmss ss) :-- (fn (m, ths) => Scan.depend (fn ctxt =>
   494   Scan.succeed (apply m (ctxt, ths)))) >> #2;
   495 
   496 fun sectioned args ss = args -- Scan.repeat (section ss);
   497 
   498 in
   499 
   500 fun sectioned_args args ss f src ctxt =
   501   let val (ctxt', (x, _)) = syntax (sectioned args ss) src ctxt
   502   in f x ctxt' end;
   503 
   504 fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f;
   505 fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f);
   506 
   507 fun thms_ctxt_args f = sectioned_args (thmss []) [] f;
   508 fun thms_args f = thms_ctxt_args (K o f);
   509 
   510 end;
   511 
   512 
   513 (* tactic syntax *)
   514 
   515 val insts =
   516   Scan.optional
   517     (Args.enum1 "and" (Scan.lift (Args.name -- (Args.$$$ "=" |-- Args.!!! Args.name))) --|
   518       Scan.lift (Args.$$$ "in")) [] -- Attrib.local_thmss;
   519 
   520 fun inst_args f = f oo (#2 oo syntax (Args.goal_spec HEADGOAL -- insts));
   521 
   522 
   523 fun goal_args' args tac = #2 oo syntax (Args.goal_spec HEADGOAL -- args >>
   524   (fn (quant, s) => METHOD (fn facts => quant (insert_tac facts THEN' tac s))));
   525 
   526 fun goal_args args tac = goal_args' (Scan.lift args) tac;
   527 
   528 
   529 
   530 (** method text **)
   531 
   532 (* datatype text *)
   533 
   534 datatype text =
   535   Basic of (Proof.context -> Proof.method) |
   536   Source of Args.src |
   537   Then of text list |
   538   Orelse of text list |
   539   Try of text |
   540   Repeat1 of text;
   541 
   542 
   543 (* refine *)
   544 
   545 fun gen_refine f text state =
   546   let
   547     val thy = Proof.theory_of state;
   548 
   549     fun eval (Basic mth) = f mth
   550       | eval (Source src) = f (method thy src)
   551       | eval (Then txts) = Seq.EVERY (map eval txts)
   552       | eval (Orelse txts) = Seq.FIRST (map eval txts)
   553       | eval (Try txt) = Seq.TRY (eval txt)
   554       | eval (Repeat1 txt) = Seq.REPEAT1 (eval txt);
   555   in eval text state end;
   556 
   557 val refine = gen_refine Proof.refine;
   558 val refine_end = gen_refine Proof.refine_end;
   559 
   560 
   561 (* structured proof steps *)
   562 
   563 val default_text = Source (Args.src (("default", []), Position.none));
   564 val this_text = Basic (K this);
   565 val done_text = Basic (K (METHOD0 all_tac));
   566 
   567 fun close_text asm = Basic (fn ctxt => METHOD (K
   568   (FILTER Thm.no_prems ((if asm then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac))));
   569 
   570 fun finish_text asm None = close_text asm
   571   | finish_text asm (Some txt) = Then [txt, close_text asm];
   572 
   573 fun proof opt_text state =
   574   state
   575   |> Proof.assert_backward
   576   |> refine (if_none opt_text default_text)
   577   |> Seq.map (Proof.goal_facts (K []))
   578   |> Seq.map Proof.enter_forward;
   579 
   580 fun local_qed asm opt_text = Proof.local_qed (refine (finish_text asm opt_text));
   581 fun local_terminal_proof (text, opt_text) pr =
   582   Seq.THEN (proof (Some text), local_qed true opt_text pr);
   583 val local_default_proof = local_terminal_proof (default_text, None);
   584 val local_immediate_proof = local_terminal_proof (this_text, None);
   585 fun local_done_proof pr = Seq.THEN (proof (Some done_text), local_qed false None pr);
   586 
   587 
   588 fun global_qeds asm opt_text = Proof.global_qed (refine (finish_text asm opt_text));
   589 
   590 fun global_qed asm opt_text state =
   591   state
   592   |> global_qeds asm opt_text
   593   |> Proof.check_result "Failed to finish proof" state
   594   |> Seq.hd;
   595 
   596 fun global_term_proof asm (text, opt_text) state =
   597   state
   598   |> proof (Some text)
   599   |> Proof.check_result "Terminal proof method failed" state
   600   |> (Seq.flat o Seq.map (global_qeds asm opt_text))
   601   |> Proof.check_result "Failed to finish proof (after successful terminal method)" state
   602   |> Seq.hd;
   603 
   604 val global_terminal_proof = global_term_proof true;
   605 val global_default_proof = global_terminal_proof (default_text, None);
   606 val global_immediate_proof = global_terminal_proof (this_text, None);
   607 val global_done_proof = global_term_proof false (done_text, None);
   608 
   609 
   610 (** theory setup **)
   611 
   612 (* misc tactic emulations *)
   613 
   614 val subgoal_meth = goal_args (Scan.repeat1 Args.name) Tactic.subgoals_tac;
   615 val thin_meth = goal_args Args.name Tactic.thin_tac;
   616 val rename_meth = goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac;
   617 
   618 
   619 (* pure_methods *)
   620 
   621 val pure_methods =
   622  [("fail", no_args fail, "force failure"),
   623   ("succeed", no_args succeed, "succeed"),
   624   ("-", no_args insert_facts, "do nothing, inserting current facts only"),
   625   ("insert", thms_args insert, "insert theorems, ignoring facts (improper)"),
   626   ("unfold", thms_args unfold, "unfold definitions"),
   627   ("fold", thms_args fold, "fold definitions"),
   628   ("default", thms_ctxt_args some_rule, "apply some rule"),
   629   ("rule", thms_ctxt_args some_rule, "apply some rule"),
   630   ("erule", thms_ctxt_args some_erule, "apply some rule in elimination manner (improper)"),
   631   ("drule", thms_ctxt_args some_drule, "apply some rule in destruct manner (improper)"),
   632   ("frule", thms_ctxt_args some_frule, "apply some rule in forward manner (improper)"),
   633   ("this", no_args this, "apply current facts as rules"),
   634   ("assumption", ctxt_args assumption, "proof by assumption, preferring facts"),
   635   ("rule_tac", inst_args res_inst, "apply rule (dynamic instantiation!)"),
   636   ("erule_tac", inst_args eres_inst, "apply rule in elimination manner (dynamic instantiation!)"),
   637   ("drule_tac", inst_args dres_inst, "apply rule in destruct manner (dynamic instantiation!)"),
   638   ("frule_tac", inst_args forw_inst, "apply rule in forward manner (dynamic instantiation!)"),
   639   ("cut_tac", inst_args cut_inst, "cut rule (dynamic instantiation!)"),
   640   ("subgoal_tac", subgoal_meth, "insert subgoal (dynamic instantiation!)"),
   641   ("thin_tac", thin_meth, "remove premise (dynamic instantiation!)"),
   642   ("rename_tac", rename_meth, "rename parameters (dynamic instantiation!)"),
   643   ("prolog", thms_args prolog, "simple prolog interpreter"),
   644   ("tactic", simple_args Args.name tactic, "ML tactic as proof method")];
   645 
   646 
   647 (* setup *)
   648 
   649 val setup =
   650  [GlobalRules.init, LocalRules.init, Attrib.add_attributes rule_atts,
   651   MethodsData.init, add_methods pure_methods];
   652 
   653 
   654 end;
   655 
   656 
   657 structure BasicMethod: BASIC_METHOD = Method;
   658 open BasicMethod;