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