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