src/Pure/Isar/method.ML
author wenzelm
Fri Jan 19 22:08:27 2007 +0100 (2007-01-19)
changeset 22118 16639b216295
parent 22086 cf6019fece63
child 22134 ab01073210e4
permissions -rw-r--r--
removed obsolete Method;
added parse (from outer_parse.ML);
moved ML context stuff to from Context to ML_Context;
tactic: no need to rebind thm/thms;
     1 (*  Title:      Pure/Isar/method.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Isar proof methods.
     6 *)
     7 
     8 signature BASIC_METHOD =
     9 sig
    10   val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
    11   val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
    12   type method
    13   val trace_rules: bool ref
    14   val print_methods: theory -> unit
    15 end;
    16 
    17 signature METHOD =
    18 sig
    19   include BASIC_METHOD
    20   val apply: method -> thm list -> cases_tactic
    21   val RAW_METHOD_CASES: (thm list -> cases_tactic) -> method
    22   val RAW_METHOD: (thm list -> tactic) -> method
    23   val METHOD_CASES: (thm list -> cases_tactic) -> method
    24   val METHOD: (thm list -> tactic) -> method
    25   val fail: method
    26   val succeed: method
    27   val insert_tac: thm list -> int -> tactic
    28   val insert: thm list -> method
    29   val insert_facts: method
    30   val SIMPLE_METHOD: tactic -> method
    31   val SIMPLE_METHOD': (int -> tactic) -> method
    32   val SIMPLE_METHOD'': ((int -> tactic) -> tactic) -> (int -> tactic) -> method
    33   val defer: int option -> method
    34   val prefer: int -> method
    35   val cheating: bool -> Proof.context -> method
    36   val intro: thm list -> method
    37   val elim: thm list -> method
    38   val unfold: thm list -> Proof.context -> method
    39   val fold: thm list -> Proof.context -> method
    40   val atomize: bool -> method
    41   val this: method
    42   val fact: thm list -> Proof.context -> method
    43   val assumption: Proof.context -> method
    44   val close: bool -> Proof.context -> method
    45   val trace: Proof.context -> thm list -> unit
    46   val rule_tac: thm list -> thm list -> int -> tactic
    47   val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic
    48   val rule: thm list -> method
    49   val erule: int -> thm list -> method
    50   val drule: int -> thm list -> method
    51   val frule: int -> thm list -> method
    52   val iprover_tac: Proof.context -> int option -> int -> tactic
    53   val set_tactic: (Proof.context -> thm list -> tactic) -> unit
    54   val tactic: string -> Proof.context -> method
    55   type src
    56   datatype text =
    57     Basic of (Proof.context -> method) |
    58     Source of src |
    59     Source_i of src |
    60     Then of text list |
    61     Orelse of text list |
    62     Try of text |
    63     Repeat1 of text |
    64     SelectGoals of int * text
    65   val primitive_text: (thm -> thm) -> text
    66   val succeed_text: text
    67   val default_text: text
    68   val this_text: text
    69   val done_text: text
    70   val sorry_text: bool -> text
    71   val finish_text: text option * bool -> text
    72   exception METHOD_FAIL of (string * Position.T) * exn
    73   val method: theory -> src -> Proof.context -> method
    74   val method_i: theory -> src -> Proof.context -> method
    75   val add_methods: (bstring * (src -> Proof.context -> method) * string) list
    76     -> theory -> theory
    77   val add_method: bstring * (src -> Proof.context -> method) * string
    78     -> theory -> theory
    79   val method_setup: bstring * string * string -> theory -> theory
    80   val syntax: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list))
    81     -> src -> Proof.context -> 'a * Proof.context
    82   val simple_args: (Args.T list -> 'a * Args.T list)
    83     -> ('a -> Proof.context -> method) -> src -> Proof.context -> method
    84   val ctxt_args: (Proof.context -> method) -> src -> Proof.context -> method
    85   val no_args: method -> src -> Proof.context -> method
    86   type modifier
    87   val sectioned_args: (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) ->
    88     (Args.T list -> modifier * Args.T list) list ->
    89     ('a -> Proof.context -> 'b) -> src -> Proof.context -> 'b
    90   val bang_sectioned_args:
    91     (Args.T list -> modifier * Args.T list) list ->
    92     (thm list -> Proof.context -> 'a) -> src -> Proof.context -> 'a
    93   val bang_sectioned_args':
    94     (Args.T list -> modifier * Args.T list) list ->
    95     (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) ->
    96     ('a -> thm list -> Proof.context -> 'b) -> src -> Proof.context -> 'b
    97   val only_sectioned_args:
    98     (Args.T list -> modifier * Args.T list) list ->
    99     (Proof.context -> 'a) -> src -> Proof.context -> 'a
   100   val thms_ctxt_args: (thm list -> Proof.context -> 'a) -> src ->
   101     Proof.context -> 'a
   102   val thms_args: (thm list -> 'a) -> src -> Proof.context -> 'a
   103   val thm_args: (thm -> 'a) -> src -> Proof.context -> 'a
   104   val goal_args: (Args.T list -> 'a * Args.T list) -> ('a -> int -> tactic)
   105     -> src -> Proof.context -> method
   106   val goal_args': (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list))
   107     -> ('a -> int -> tactic) -> src -> Proof.context -> method
   108   val goal_args_ctxt: (Args.T list -> 'a * Args.T list) ->
   109     (Proof.context -> 'a -> int -> tactic) -> src -> Proof.context -> method
   110   val goal_args_ctxt': (Context.generic * Args.T list -> 'a * (Context.generic * Args.T list)) ->
   111     (Proof.context -> 'a -> int -> tactic) -> src -> Proof.context -> method
   112   val parse: OuterLex.token list -> text * OuterLex.token list
   113 end;
   114 
   115 structure Method: METHOD =
   116 struct
   117 
   118 (** generic tools **)
   119 
   120 (* goal addressing *)
   121 
   122 fun FINDGOAL tac st =
   123   let fun find i n = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1) n)
   124   in find 1 (Thm.nprems_of st) st end;
   125 
   126 fun HEADGOAL tac = tac 1;
   127 
   128 
   129 
   130 (** proof methods **)
   131 
   132 (* datatype method *)
   133 
   134 datatype method = Meth of thm list -> cases_tactic;
   135 
   136 fun apply (Meth m) = m;
   137 
   138 val RAW_METHOD_CASES = Meth;
   139 
   140 fun RAW_METHOD tac = RAW_METHOD_CASES (NO_CASES o tac);
   141 
   142 fun METHOD_CASES tac = RAW_METHOD_CASES (fn facts =>
   143   Seq.THEN (ALLGOALS Goal.conjunction_tac, tac facts));
   144 
   145 fun METHOD tac = RAW_METHOD (fn facts => ALLGOALS Goal.conjunction_tac THEN tac facts);
   146 
   147 val fail = METHOD (K no_tac);
   148 val succeed = METHOD (K all_tac);
   149 
   150 
   151 (* insert facts *)
   152 
   153 local
   154 
   155 fun cut_rule_tac rule =
   156   Tactic.rtac (Drule.forall_intr_vars rule COMP_INCR revcut_rl);
   157 
   158 in
   159 
   160 fun insert_tac [] i = all_tac
   161   | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
   162 
   163 val insert_facts = METHOD (ALLGOALS o insert_tac);
   164 fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
   165 
   166 fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac);
   167 fun SIMPLE_METHOD'' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac));
   168 val SIMPLE_METHOD' = SIMPLE_METHOD'' HEADGOAL;
   169 
   170 end;
   171 
   172 
   173 (* shuffle subgoals *)
   174 
   175 fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1)));
   176 fun defer opt_i = METHOD (K (Tactic.defer_tac (the_default 1 opt_i)));
   177 
   178 
   179 (* cheating *)
   180 
   181 fun cheating int ctxt = METHOD (K (setmp quick_and_dirty (int orelse ! quick_and_dirty)
   182     (SkipProof.cheat_tac (ProofContext.theory_of ctxt))));
   183 
   184 
   185 (* unfold intro/elim rules *)
   186 
   187 fun intro ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths));
   188 fun elim ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths));
   189 
   190 
   191 (* unfold/fold definitions *)
   192 
   193 fun unfold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.unfold_tac ctxt ths));
   194 fun fold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.fold_tac ctxt ths));
   195 
   196 
   197 (* atomize rule statements *)
   198 
   199 fun atomize false = SIMPLE_METHOD' (CHANGED_PROP o ObjectLogic.atomize_tac)
   200   | atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o ObjectLogic.full_atomize_tac)));
   201 
   202 
   203 (* this -- resolve facts directly *)
   204 
   205 val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac));
   206 
   207 
   208 (* fact -- composition by facts from context *)
   209 
   210 fun fact [] ctxt = SIMPLE_METHOD' (ProofContext.some_fact_tac ctxt)
   211   | fact rules _ = SIMPLE_METHOD' (ProofContext.fact_tac rules);
   212 
   213 
   214 (* assumption *)
   215 
   216 local
   217 
   218 fun asm_tac ths =
   219   foldr (op APPEND') (K no_tac) (map (fn th => Tactic.rtac th THEN_ALL_NEW assume_tac) ths);
   220 
   221 fun cond_rtac cond rule = SUBGOAL (fn (prop, i) =>
   222   if cond (Logic.strip_assums_concl prop)
   223   then Tactic.rtac rule i else no_tac);
   224 
   225 fun assm_tac ctxt =
   226   assume_tac APPEND'
   227   asm_tac (Assumption.prems_of ctxt) APPEND'
   228   cond_rtac (can Logic.dest_equals) Drule.reflexive_thm APPEND'
   229   cond_rtac (can Logic.dest_term) Drule.termI;
   230 
   231 fun assumption_tac ctxt [] = assm_tac ctxt
   232   | assumption_tac _ [fact] = asm_tac [fact]
   233   | assumption_tac _ _ = K no_tac;
   234 
   235 in
   236 
   237 fun assumption ctxt = METHOD (HEADGOAL o assumption_tac ctxt);
   238 fun close immed ctxt = METHOD (K
   239   (FILTER Thm.no_prems ((if immed then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac)));
   240 
   241 end;
   242 
   243 
   244 (* rule etc. -- single-step refinements *)
   245 
   246 val trace_rules = ref false;
   247 
   248 fun trace ctxt rules =
   249   if ! trace_rules andalso not (null rules) then
   250     Pretty.big_list "rules:" (map (ProofContext.pretty_thm ctxt) rules)
   251     |> Pretty.string_of |> tracing
   252   else ();
   253 
   254 local
   255 
   256 fun gen_rule_tac tac rules facts =
   257   (fn i => fn st =>
   258     if null facts then tac rules i st
   259     else Seq.maps (fn rule => (tac o single) rule i st) (Drule.multi_resolves facts rules))
   260   THEN_ALL_NEW Goal.norm_hhf_tac;
   261 
   262 fun gen_arule_tac tac j rules facts =
   263   EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac);
   264 
   265 fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) =>
   266   let
   267     val rules =
   268       if not (null arg_rules) then arg_rules
   269       else flat (ContextRules.find_rules false facts goal ctxt)
   270   in trace ctxt rules; tac rules facts i end);
   271 
   272 fun meth tac x = METHOD (HEADGOAL o tac x);
   273 fun meth' tac x y = METHOD (HEADGOAL o tac x y);
   274 
   275 in
   276 
   277 val rule_tac = gen_rule_tac Tactic.resolve_tac;
   278 val rule = meth rule_tac;
   279 val some_rule_tac = gen_some_rule_tac rule_tac;
   280 val some_rule = meth' some_rule_tac;
   281 
   282 val erule = meth' (gen_arule_tac Tactic.eresolve_tac);
   283 val drule = meth' (gen_arule_tac Tactic.dresolve_tac);
   284 val frule = meth' (gen_arule_tac Tactic.forward_tac);
   285 
   286 end;
   287 
   288 
   289 (* iprover -- intuitionistic proof search *)
   290 
   291 local
   292 
   293 val remdups_tac = SUBGOAL (fn (g, i) =>
   294   let val prems = Logic.strip_assums_hyp g in
   295     REPEAT_DETERM_N (length prems - length (distinct op aconv prems))
   296     (Tactic.ematch_tac [Drule.remdups_rl] i THEN Tactic.eq_assume_tac i)
   297   end);
   298 
   299 fun REMDUPS tac = tac THEN_ALL_NEW remdups_tac;
   300 
   301 val bires_tac = Tactic.biresolution_from_nets_tac ContextRules.orderlist;
   302 
   303 fun safe_step_tac ctxt =
   304   ContextRules.Swrap ctxt
   305    (eq_assume_tac ORELSE'
   306     bires_tac true (ContextRules.netpair_bang ctxt));
   307 
   308 fun unsafe_step_tac ctxt =
   309   ContextRules.wrap ctxt
   310    (assume_tac APPEND'
   311     bires_tac false (ContextRules.netpair_bang ctxt) APPEND'
   312     bires_tac false (ContextRules.netpair ctxt));
   313 
   314 fun step_tac ctxt i =
   315   REPEAT_DETERM1 (REMDUPS (safe_step_tac ctxt) i) ORELSE
   316   REMDUPS (unsafe_step_tac ctxt) i;
   317 
   318 fun intprover_tac ctxt gs d lim = SUBGOAL (fn (g, i) => if d > lim then no_tac else
   319   let
   320     val ps = Logic.strip_assums_hyp g;
   321     val c = Logic.strip_assums_concl g;
   322   in
   323     if member (fn ((ps1, c1), (ps2, c2)) =>
   324         c1 aconv c2 andalso
   325         length ps1 = length ps2 andalso
   326         gen_eq_set (op aconv) (ps1, ps2)) gs (ps, c) then no_tac
   327     else (step_tac ctxt THEN_ALL_NEW intprover_tac ctxt ((ps, c) :: gs) (d + 1) lim) i
   328   end);
   329 
   330 in
   331 
   332 fun iprover_tac ctxt opt_lim =
   333   SELECT_GOAL (DEEPEN (2, the_default 20 opt_lim) (intprover_tac ctxt [] 0) 4 1);
   334 
   335 end;
   336 
   337 
   338 (* ML tactics *)
   339 
   340 val tactic_ref = ref ((fn _ => raise Match): Proof.context -> thm list -> tactic);
   341 fun set_tactic f = tactic_ref := f;
   342 
   343 fun tactic txt ctxt = METHOD (fn facts =>
   344   (ML_Context.use_mltext
   345     ("let fun tactic (ctxt: Proof.context) (facts: thm list) : tactic = \
   346        \let val thm = ProofContext.get_thm_closure ctxt o PureThy.Name\n\
   347        \  and thms = ProofContext.get_thms_closure ctxt o PureThy.Name in\n"
   348        ^ txt ^
   349        "\nend in Method.set_tactic tactic end")
   350     false NONE;
   351     ML_Context.setmp (SOME (Context.Proof ctxt)) (! tactic_ref ctxt) facts));
   352 
   353 
   354 
   355 (** method syntax **)
   356 
   357 (* method text *)
   358 
   359 type src = Args.src;
   360 
   361 datatype text =
   362   Basic of (Proof.context -> method) |
   363   Source of src |
   364   Source_i of src |
   365   Then of text list |
   366   Orelse of text list |
   367   Try of text |
   368   Repeat1 of text |
   369   SelectGoals of int * text;
   370 
   371 val primitive_text = Basic o K o SIMPLE_METHOD o PRIMITIVE;
   372 val succeed_text = Basic (K succeed);
   373 val default_text = Source (Args.src (("default", []), Position.none));
   374 val this_text = Basic (K this);
   375 val done_text = Basic (K (SIMPLE_METHOD all_tac));
   376 val sorry_text = Basic o cheating;
   377 
   378 fun finish_text (NONE, immed) = Basic (close immed)
   379   | finish_text (SOME txt, immed) = Then [txt, Basic (close immed)];
   380 
   381 
   382 (* method definitions *)
   383 
   384 structure MethodsData = TheoryDataFun
   385 (struct
   386   val name = "Isar/methods";
   387   type T = (((src -> Proof.context -> method) * string) * stamp) NameSpace.table;
   388 
   389   val empty = NameSpace.empty_table;
   390   val copy = I;
   391   val extend = I;
   392   fun merge _ tables = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUPS dups =>
   393     error ("Attempt to merge different versions of method(s) " ^ commas_quote dups);
   394 
   395   fun print _ meths =
   396     let
   397       fun prt_meth (name, ((_, comment), _)) = Pretty.block
   398         [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   399     in
   400       [Pretty.big_list "methods:" (map prt_meth (NameSpace.extern_table meths))]
   401       |> Pretty.chunks |> Pretty.writeln
   402     end;
   403 end);
   404 
   405 val _ = Context.add_setup MethodsData.init;
   406 val print_methods = MethodsData.print;
   407 
   408 
   409 (* get methods *)
   410 
   411 exception METHOD_FAIL of (string * Position.T) * exn;
   412 
   413 fun method_i thy =
   414   let
   415     val meths = #2 (MethodsData.get thy);
   416     fun meth src =
   417       let val ((name, _), pos) = Args.dest_src src in
   418         (case Symtab.lookup meths name of
   419           NONE => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
   420         | SOME ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
   421       end;
   422   in meth end;
   423 
   424 fun method thy = method_i thy o Args.map_name (NameSpace.intern (#1 (MethodsData.get thy)));
   425 
   426 
   427 (* add method *)
   428 
   429 fun add_methods raw_meths thy =
   430   let
   431     val new_meths = raw_meths |> map (fn (name, f, comment) =>
   432       (name, ((f, comment), stamp ())));
   433 
   434     fun add meths = NameSpace.extend_table (Sign.naming_of thy) (meths, new_meths)
   435       handle Symtab.DUPS dups =>
   436         error ("Duplicate declaration of method(s) " ^ commas_quote dups);
   437   in MethodsData.map add thy end;
   438 
   439 val add_method = add_methods o Library.single;
   440 
   441 
   442 (* method_setup *)
   443 
   444 fun method_setup (name, txt, cmt) =
   445   ML_Context.use_let
   446     "val method: bstring * (Method.src -> Proof.context -> Proof.method) * string"
   447     "Context.map_theory (Method.add_method method)"
   448     ("(" ^ quote name ^ ", " ^ txt ^ ", " ^ quote cmt ^ ")")
   449   |> Context.theory_map;
   450 
   451 
   452 
   453 (** concrete syntax **)
   454 
   455 (* basic *)
   456 
   457 fun syntax scan = Args.context_syntax "method" scan;
   458 
   459 fun simple_args scan f src ctxt : method =
   460   fst (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt);
   461 
   462 fun ctxt_args (f: Proof.context -> method) src ctxt =
   463   fst (syntax (Scan.succeed (f ctxt)) src ctxt);
   464 
   465 fun no_args m = ctxt_args (K m);
   466 
   467 
   468 (* sections *)
   469 
   470 type modifier = (Proof.context -> Proof.context) * attribute;
   471 
   472 local
   473 
   474 fun sect ss = Scan.first (map Scan.lift ss);
   475 fun thms ss = Scan.repeat (Scan.unless (sect ss) Attrib.multi_thm) >> flat;
   476 
   477 fun app (f, att) (context, ths) = foldl_map att (Context.map_proof f context, ths);
   478 
   479 fun section ss = (sect ss -- thms ss) :-- (fn (m, ths) => Scan.depend (fn context =>
   480   Scan.succeed (app m (context, ths)))) >> #2;
   481 
   482 fun sectioned args ss = args -- Scan.repeat (section ss);
   483 
   484 in
   485 
   486 fun sectioned_args args ss f src ctxt =
   487   let val ((x, _), ctxt') = syntax (sectioned args ss) src ctxt
   488   in f x ctxt' end;
   489 
   490 fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f;
   491 fun bang_sectioned_args' ss scan f =
   492   sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f);
   493 fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f);
   494 
   495 fun thms_ctxt_args f = sectioned_args (thms []) [] f;
   496 fun thms_args f = thms_ctxt_args (K o f);
   497 fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected");
   498 
   499 end;
   500 
   501 
   502 (* iprover syntax *)
   503 
   504 local
   505 
   506 val introN = "intro";
   507 val elimN = "elim";
   508 val destN = "dest";
   509 val ruleN = "rule";
   510 
   511 fun modifier name kind kind' att =
   512   Args.$$$ name |-- (kind >> K NONE || kind' |-- Args.nat --| Args.colon >> SOME)
   513     >> (pair (I: Proof.context -> Proof.context) o att);
   514 
   515 val iprover_modifiers =
   516  [modifier destN Args.bang_colon Args.bang ContextRules.dest_bang,
   517   modifier destN Args.colon (Scan.succeed ()) ContextRules.dest,
   518   modifier elimN Args.bang_colon Args.bang ContextRules.elim_bang,
   519   modifier elimN Args.colon (Scan.succeed ()) ContextRules.elim,
   520   modifier introN Args.bang_colon Args.bang ContextRules.intro_bang,
   521   modifier introN Args.colon (Scan.succeed ()) ContextRules.intro,
   522   Args.del -- Args.colon >> K (I, ContextRules.rule_del)];
   523 
   524 in
   525 
   526 val iprover_meth =
   527   bang_sectioned_args' iprover_modifiers (Scan.lift (Scan.option Args.nat))
   528     (fn n => fn prems => fn ctxt => METHOD (fn facts =>
   529       HEADGOAL (insert_tac (prems @ facts) THEN'
   530       ObjectLogic.atomize_tac THEN' iprover_tac ctxt n)));
   531 
   532 end;
   533 
   534 
   535 (* tactic syntax *)
   536 
   537 fun nat_thms_args f = uncurry f oo
   538   (fst oo syntax (Scan.lift (Scan.optional (Args.parens Args.nat) 0) -- Attrib.thms));
   539 
   540 fun goal_args' args tac src ctxt = fst (syntax (Args.goal_spec HEADGOAL -- args >>
   541   (fn (quant, s) => SIMPLE_METHOD'' quant (tac s))) src ctxt);
   542 
   543 fun goal_args args tac = goal_args' (Scan.lift args) tac;
   544 
   545 fun goal_args_ctxt' args tac src ctxt =
   546   fst (syntax (Args.goal_spec HEADGOAL -- args >>
   547   (fn (quant, s) => SIMPLE_METHOD'' quant (tac ctxt s))) src ctxt);
   548 
   549 fun goal_args_ctxt args tac = goal_args_ctxt' (Scan.lift args) tac;
   550 
   551 
   552 (* theory setup *)
   553 
   554 val _ = Context.add_setup (add_methods
   555  [("fail", no_args fail, "force failure"),
   556   ("succeed", no_args succeed, "succeed"),
   557   ("-", no_args insert_facts, "do nothing (insert current facts only)"),
   558   ("insert", thms_args insert, "insert theorems, ignoring facts (improper)"),
   559   ("intro", thms_args intro, "repeatedly apply introduction rules"),
   560   ("elim", thms_args elim, "repeatedly apply elimination rules"),
   561   ("unfold", thms_ctxt_args unfold_meth, "unfold definitions"),
   562   ("fold", thms_ctxt_args fold_meth, "fold definitions"),
   563   ("atomize", (atomize o fst) oo syntax (Args.mode "full"),
   564     "present local premises as object-level statements"),
   565   ("iprover", iprover_meth, "intuitionistic proof search"),
   566   ("rule", thms_ctxt_args some_rule, "apply some intro/elim rule"),
   567   ("erule", nat_thms_args erule, "apply rule in elimination manner (improper)"),
   568   ("drule", nat_thms_args drule, "apply rule in destruct manner (improper)"),
   569   ("frule", nat_thms_args frule, "apply rule in forward manner (improper)"),
   570   ("this", no_args this, "apply current facts as rules"),
   571   ("fact", thms_ctxt_args fact, "composition by facts from context"),
   572   ("assumption", ctxt_args assumption, "proof by assumption, preferring facts"),
   573   ("rename_tac", goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac,
   574     "rename parameters of goal (dynamic instantiation)"),
   575   ("rotate_tac", goal_args (Scan.optional Args.int 1) Tactic.rotate_tac,
   576     "rotate assumptions of goal"),
   577   ("tactic", simple_args Args.name tactic, "ML tactic as proof method")]);
   578 
   579 
   580 (* outer parser *)
   581 
   582 local
   583 
   584 structure T = OuterLex;
   585 structure P = OuterParse;
   586 
   587 fun is_symid_meth s =
   588   s <> "|" andalso s <> "?" andalso s <> "+" andalso T.is_sid s;
   589 
   590 fun meth4 x =
   591  (P.position (P.xname >> rpair []) >> (Source o Args.src) ||
   592   P.$$$ "(" |-- P.!!! (meth0 --| P.$$$ ")")) x
   593 and meth3 x =
   594  (meth4 --| P.$$$ "?" >> Try ||
   595   meth4 --| P.$$$ "+" >> Repeat1 ||
   596   meth4 -- (P.$$$ "[" |-- Scan.optional P.nat 1 --| P.$$$ "]") >> (SelectGoals o swap) ||
   597   meth4) x
   598 and meth2 x =
   599  (P.position (P.xname -- P.args1 is_symid_meth false) >> (Source o Args.src) ||
   600   meth3) x
   601 and meth1 x = (P.enum1 "," meth2 >> (fn [m] => m | ms => Then ms)) x
   602 and meth0 x = (P.enum1 "|" meth1 >> (fn [m] => m | ms => Orelse ms)) x;
   603 
   604 in val parse = meth3 end;
   605 
   606 
   607 (*final declarations of this structure!*)
   608 val unfold = unfold_meth;
   609 val fold = fold_meth;
   610 
   611 end;
   612 
   613 structure BasicMethod: BASIC_METHOD = Method;
   614 open BasicMethod;