src/Pure/Isar/method.ML
author wenzelm
Fri Feb 03 23:12:28 2006 +0100 (2006-02-03)
changeset 18921 f47c46d7d654
parent 18877 2ee8333a2ba1
child 18939 18e2a2676d80
permissions -rw-r--r--
canonical member/insert/merge;
     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   val Method: bstring -> (Args.src -> ProofContext.context -> method) -> string -> unit
    16 end;
    17 
    18 signature METHOD =
    19 sig
    20   include BASIC_METHOD
    21   val apply: method -> thm list -> cases_tactic
    22   val RAW_METHOD_CASES: (thm list -> cases_tactic) -> method
    23   val RAW_METHOD: (thm list -> tactic) -> method
    24   val METHOD_CASES: (thm list -> cases_tactic) -> method
    25   val METHOD: (thm list -> tactic) -> method
    26   val fail: method
    27   val succeed: method
    28   val insert_tac: thm list -> int -> tactic
    29   val insert: thm list -> method
    30   val insert_facts: method
    31   val SIMPLE_METHOD: 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 -> ProofContext.context -> method
    36   val intro: thm list -> method
    37   val elim: thm list -> method
    38   val unfold: thm list -> ProofContext.context -> method
    39   val fold: thm list -> ProofContext.context -> method
    40   val atomize: bool -> method
    41   val this: method
    42   val fact: thm list -> ProofContext.context -> method
    43   val assumption: ProofContext.context -> method
    44   val close: bool -> ProofContext.context -> method
    45   val trace: ProofContext.context -> thm list -> unit
    46   val rule_tac: thm list -> thm list -> int -> tactic
    47   val some_rule_tac: thm list -> ProofContext.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: ProofContext.context -> int option -> int -> tactic
    53   val bires_inst_tac: bool -> ProofContext.context -> (indexname * string) list ->
    54     thm -> int -> tactic
    55   val set_tactic: (ProofContext.context -> thm list -> tactic) -> unit
    56   val tactic: string -> ProofContext.context -> method
    57   type src
    58   datatype text =
    59     Basic of (ProofContext.context -> method) |
    60     Source of src |
    61     Then of text list |
    62     Orelse of text list |
    63     Try of text |
    64     Repeat1 of 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 -> ProofContext.context -> method
    74   val add_methods: (bstring * (src -> ProofContext.context -> method) * string) list
    75     -> theory -> theory
    76   val add_method: bstring * (src -> ProofContext.context -> method) * string
    77     -> theory -> theory
    78   val method_setup: bstring * string * string -> theory -> theory
    79   val syntax: (ProofContext.context * Args.T list -> 'a * (ProofContext.context * Args.T list))
    80     -> src -> ProofContext.context -> ProofContext.context * 'a
    81   val simple_args: (Args.T list -> 'a * Args.T list)
    82     -> ('a -> ProofContext.context -> method) -> src -> ProofContext.context -> method
    83   val ctxt_args: (ProofContext.context -> method) -> src -> ProofContext.context -> method
    84   val no_args: method -> src -> ProofContext.context -> method
    85   type modifier
    86   val sectioned_args: (ProofContext.context * Args.T list ->
    87       'a * (ProofContext.context * Args.T list)) ->
    88     (Args.T list -> modifier * Args.T list) list ->
    89     ('a -> ProofContext.context -> 'b) -> src -> ProofContext.context -> 'b
    90   val bang_sectioned_args:
    91     (Args.T list -> modifier * Args.T list) list ->
    92     (thm list -> ProofContext.context -> 'a) -> src -> ProofContext.context -> 'a
    93   val bang_sectioned_args':
    94     (Args.T list -> modifier * Args.T list) list ->
    95     (ProofContext.context * Args.T list -> 'a * (ProofContext.context * Args.T list)) ->
    96     ('a -> thm list -> ProofContext.context -> 'b) -> src -> ProofContext.context -> 'b
    97   val only_sectioned_args:
    98     (Args.T list -> modifier * Args.T list) list ->
    99     (ProofContext.context -> 'a) -> src -> ProofContext.context -> 'a
   100   val thms_ctxt_args: (thm list -> ProofContext.context -> 'a) -> src ->
   101     ProofContext.context -> 'a
   102   val thms_args: (thm list -> 'a) -> src -> ProofContext.context -> 'a
   103   val thm_args: (thm -> 'a) -> src -> ProofContext.context -> 'a
   104   val goal_args: (Args.T list -> 'a * Args.T list) -> ('a -> int -> tactic)
   105     -> src -> ProofContext.context -> method
   106   val goal_args': (ProofContext.context * Args.T list ->
   107       'a * (ProofContext.context * Args.T list))
   108     -> ('a -> int -> tactic) -> src -> ProofContext.context -> method
   109   val goal_args_ctxt: (Args.T list -> 'a * Args.T list) ->
   110     (ProofContext.context -> 'a -> int -> tactic) -> src -> ProofContext.context -> method
   111   val goal_args_ctxt': (ProofContext.context * Args.T list ->
   112     'a * (ProofContext.context * Args.T list)) ->
   113     (ProofContext.context -> 'a -> int -> tactic) -> src -> ProofContext.context -> method
   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 (Meth m) = m;
   138 
   139 val RAW_METHOD_CASES = Meth;
   140 
   141 fun RAW_METHOD tac = RAW_METHOD_CASES (NO_CASES o tac);
   142 
   143 fun METHOD_CASES tac = RAW_METHOD_CASES (fn facts =>
   144   Seq.THEN (ALLGOALS Tactic.conjunction_tac, tac facts));
   145 
   146 fun METHOD tac = RAW_METHOD (fn facts => ALLGOALS Tactic.conjunction_tac THEN tac facts);
   147 
   148 val fail = METHOD (K no_tac);
   149 val succeed = METHOD (K all_tac);
   150 
   151 
   152 (* insert facts *)
   153 
   154 local
   155 
   156 fun cut_rule_tac raw_rule =
   157   let
   158     val rule = Drule.forall_intr_vars raw_rule;
   159     val revcut_rl = Drule.incr_indexes rule Drule.revcut_rl;
   160   in Tactic.rtac (rule COMP revcut_rl) end;
   161 
   162 in
   163 
   164 fun insert_tac [] i = all_tac
   165   | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
   166 
   167 val insert_facts = METHOD (ALLGOALS o insert_tac);
   168 fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
   169 
   170 fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac);
   171 fun SIMPLE_METHOD' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac));
   172 
   173 end;
   174 
   175 
   176 (* shuffle subgoals *)
   177 
   178 fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1)));
   179 fun defer opt_i = METHOD (K (Tactic.defer_tac (if_none opt_i 1)));
   180 
   181 
   182 (* cheating *)
   183 
   184 fun cheating int ctxt = METHOD (K (setmp quick_and_dirty (int orelse ! quick_and_dirty)
   185     (SkipProof.cheat_tac (ProofContext.theory_of ctxt))));
   186 
   187 
   188 (* unfold intro/elim rules *)
   189 
   190 fun intro ths = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths));
   191 fun elim ths = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths));
   192 
   193 
   194 (* unfold/fold definitions *)
   195 
   196 fun unfold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.unfold_tac ctxt ths));
   197 fun fold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (LocalDefs.fold_tac ctxt ths));
   198 
   199 
   200 (* atomize rule statements *)
   201 
   202 fun atomize false = SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o ObjectLogic.atomize_tac)
   203   | atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o ObjectLogic.full_atomize_tac)));
   204 
   205 
   206 (* this -- resolve facts directly *)
   207 
   208 val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac));
   209 
   210 
   211 (* fact -- composition by facts from context *)
   212 
   213 fun fact [] ctxt = SIMPLE_METHOD' HEADGOAL (ProofContext.some_fact_tac ctxt)
   214   | fact rules _ = SIMPLE_METHOD' HEADGOAL (ProofContext.fact_tac rules);
   215 
   216 
   217 (* assumption *)
   218 
   219 local
   220 
   221 fun asm_tac ths =
   222   foldr (op APPEND') (K no_tac) (map (fn th => Tactic.rtac th THEN_ALL_NEW assume_tac) ths);
   223 
   224 val refl_tac = SUBGOAL (fn (prop, i) =>
   225   if can Logic.dest_equals (Logic.strip_assums_concl prop)
   226   then Tactic.rtac Drule.reflexive_thm i else no_tac);
   227 
   228 fun assm_tac ctxt =
   229   assume_tac APPEND'
   230   asm_tac (ProofContext.prems_of ctxt) APPEND'
   231   refl_tac;
   232 
   233 fun assumption_tac ctxt [] = assm_tac ctxt
   234   | assumption_tac _ [fact] = asm_tac [fact]
   235   | assumption_tac _ _ = K no_tac;
   236 
   237 in
   238 
   239 fun assumption ctxt = METHOD (HEADGOAL o assumption_tac ctxt);
   240 fun close immed ctxt = METHOD (K
   241   (FILTER Thm.no_prems ((if immed then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac)));
   242 
   243 end;
   244 
   245 
   246 (* rule etc. -- single-step refinements *)
   247 
   248 val trace_rules = ref false;
   249 
   250 fun trace ctxt rules =
   251   conditional (! trace_rules andalso not (null rules)) (fn () =>
   252     Pretty.big_list "rules:" (map (ProofContext.pretty_thm ctxt) rules)
   253     |> Pretty.string_of |> tracing);
   254 
   255 local
   256 
   257 fun gen_rule_tac tac rules facts =
   258   (fn i => fn st =>
   259     if null facts then tac rules i st
   260     else Seq.maps (fn rule => (tac o single) rule i st) (Drule.multi_resolves facts rules))
   261   THEN_ALL_NEW Tactic.norm_hhf_tac;
   262 
   263 fun gen_arule_tac tac j rules facts =
   264   EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac);
   265 
   266 fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) =>
   267   let
   268     val rules =
   269       if not (null arg_rules) then arg_rules
   270       else List.concat (ContextRules.find_rules false facts goal ctxt)
   271   in trace ctxt rules; tac rules facts i end);
   272 
   273 fun meth tac x = METHOD (HEADGOAL o tac x);
   274 fun meth' tac x y = METHOD (HEADGOAL o tac x y);
   275 
   276 in
   277 
   278 val rule_tac = gen_rule_tac Tactic.resolve_tac;
   279 val rule = meth rule_tac;
   280 val some_rule_tac = gen_some_rule_tac rule_tac;
   281 val some_rule = meth' some_rule_tac;
   282 
   283 val erule = meth' (gen_arule_tac Tactic.eresolve_tac);
   284 val drule = meth' (gen_arule_tac Tactic.dresolve_tac);
   285 val frule = meth' (gen_arule_tac Tactic.forward_tac);
   286 
   287 end;
   288 
   289 
   290 (* iprover -- intuitionistic proof search *)
   291 
   292 local
   293 
   294 val remdups_tac = SUBGOAL (fn (g, i) =>
   295   let val prems = Logic.strip_assums_hyp g in
   296     REPEAT_DETERM_N (length prems - length (gen_distinct op aconv prems))
   297     (Tactic.ematch_tac [Drule.remdups_rl] i THEN Tactic.eq_assume_tac i)
   298   end);
   299 
   300 fun REMDUPS tac = tac THEN_ALL_NEW remdups_tac;
   301 
   302 fun gen_eq_set e s1 s2 =
   303   length s1 = length s2 andalso
   304   gen_subset e (s1, s2) andalso gen_subset e (s2, s1);
   305 
   306 val bires_tac = Tactic.biresolution_from_nets_tac ContextRules.orderlist;
   307 
   308 fun safe_step_tac ctxt =
   309   ContextRules.Swrap ctxt
   310    (eq_assume_tac ORELSE'
   311     bires_tac true (ContextRules.netpair_bang ctxt));
   312 
   313 fun unsafe_step_tac ctxt =
   314   ContextRules.wrap ctxt
   315    (assume_tac APPEND'
   316     bires_tac false (ContextRules.netpair_bang ctxt) APPEND'
   317     bires_tac false (ContextRules.netpair ctxt));
   318 
   319 fun step_tac ctxt i =
   320   REPEAT_DETERM1 (REMDUPS (safe_step_tac ctxt) i) ORELSE
   321   REMDUPS (unsafe_step_tac ctxt) i;
   322 
   323 fun intprover_tac ctxt gs d lim = SUBGOAL (fn (g, i) => if d > lim then no_tac else
   324   let
   325     val ps = Logic.strip_assums_hyp g;
   326     val c = Logic.strip_assums_concl g;
   327   in
   328     if member (fn ((ps1, c1), (ps2, c2)) =>
   329       c1 aconv c2 andalso gen_eq_set op aconv ps1 ps2) gs (ps, c) then no_tac
   330     else (step_tac ctxt THEN_ALL_NEW intprover_tac ctxt ((ps, c) :: gs) (d + 1) lim) i
   331   end);
   332 
   333 in
   334 
   335 fun iprover_tac ctxt opt_lim =
   336   SELECT_GOAL (DEEPEN (2, if_none opt_lim 20) (intprover_tac ctxt [] 0) 4 1);
   337 
   338 end;
   339 
   340 
   341 (* rule_tac etc. -- refer to dynamic goal state!! *)
   342 
   343 fun bires_inst_tac bires_flag ctxt insts thm =
   344   let
   345     val thy = ProofContext.theory_of ctxt;
   346     (* Separate type and term insts *)
   347     fun has_type_var ((x, _), _) = (case Symbol.explode x of
   348           "'"::cs => true | cs => false);
   349     val Tinsts = List.filter has_type_var insts;
   350     val tinsts = filter_out has_type_var insts;
   351     (* Tactic *)
   352     fun tac i st =
   353       let
   354         (* Preprocess state: extract environment information:
   355            - variables and their types
   356            - type variables and their sorts
   357            - parameters and their types *)
   358         val (types, sorts) = types_sorts st;
   359     (* Process type insts: Tinsts_env *)
   360     fun absent xi = error
   361           ("No such variable in theorem: " ^ Syntax.string_of_vname xi);
   362     val (rtypes, rsorts) = types_sorts thm;
   363     fun readT (xi, s) =
   364         let val S = case rsorts xi of SOME S => S | NONE => absent xi;
   365             val T = Sign.read_typ (thy, sorts) s;
   366             val U = TVar (xi, S);
   367         in if Sign.typ_instance thy (T, U) then (U, T)
   368            else error
   369              ("Instantiation of " ^ Syntax.string_of_vname xi ^ " fails")
   370         end;
   371     val Tinsts_env = map readT Tinsts;
   372     (* Preprocess rule: extract vars and their types, apply Tinsts *)
   373     fun get_typ xi =
   374       (case rtypes xi of
   375            SOME T => typ_subst_atomic Tinsts_env T
   376          | NONE => absent xi);
   377     val (xis, ss) = Library.split_list tinsts;
   378     val Ts = map get_typ xis;
   379         val (_, _, Bi, _) = dest_state(st,i)
   380         val params = Logic.strip_params Bi
   381                              (* params of subgoal i as string typ pairs *)
   382         val params = rev(Term.rename_wrt_term Bi params)
   383                            (* as they are printed: bound variables with *)
   384                            (* the same name are renamed during printing *)
   385         fun types' (a, ~1) = (case AList.lookup (op =) params a of
   386                 NONE => types (a, ~1)
   387               | some => some)
   388           | types' xi = types xi;
   389         fun internal x = is_some (types' (x, ~1));
   390         val used = Drule.add_used thm (Drule.add_used st []);
   391         val (ts, envT) =
   392           ProofContext.read_termTs_schematic ctxt internal types' sorts used (ss ~~ Ts);
   393         val envT' = map (fn (ixn, T) =>
   394           (TVar (ixn, the (rsorts ixn)), T)) envT @ Tinsts_env;
   395         val cenv =
   396           map
   397             (fn (xi, t) =>
   398               pairself (Thm.cterm_of thy) (Var (xi, fastype_of t), t))
   399             (gen_distinct
   400               (fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2)
   401               (xis ~~ ts));
   402         (* Lift and instantiate rule *)
   403         val {maxidx, ...} = rep_thm st;
   404         val paramTs = map #2 params
   405         and inc = maxidx+1
   406         fun liftvar (Var ((a,j), T)) =
   407               Var((a, j+inc), paramTs ---> Logic.incr_tvar inc T)
   408           | liftvar t = raise TERM("Variable expected", [t]);
   409         fun liftterm t = list_abs_free
   410               (params, Logic.incr_indexes(paramTs,inc) t)
   411         fun liftpair (cv,ct) =
   412               (cterm_fun liftvar cv, cterm_fun liftterm ct)
   413         val lifttvar = pairself (ctyp_of thy o Logic.incr_tvar inc);
   414         val rule = Drule.instantiate
   415               (map lifttvar envT', map liftpair cenv)
   416               (Thm.lift_rule (Thm.cprem_of st i) thm)
   417       in
   418         if i > nprems_of st then no_tac st
   419         else st |>
   420           compose_tac (bires_flag, rule, nprems_of thm) i
   421       end
   422            handle TERM (msg,_)   => (warning msg; no_tac st)
   423                 | THM  (msg,_,_) => (warning msg; no_tac st);
   424   in
   425     tac
   426   end;
   427 
   428 local
   429 
   430 fun gen_inst _ tac _ (quant, ([], thms)) =
   431       METHOD (fn facts => quant (insert_tac facts THEN' tac thms))
   432   | gen_inst inst_tac _ ctxt (quant, (insts, [thm])) =
   433       METHOD (fn facts =>
   434         quant (insert_tac facts THEN' inst_tac ctxt insts thm))
   435   | gen_inst _ _ _ _ = error "Cannot have instantiations with multiple rules";
   436 
   437 (* Preserve Var indexes of rl; increment revcut_rl instead.
   438    Copied from tactic.ML *)
   439 fun make_elim_preserve rl =
   440   let val {maxidx,...} = rep_thm rl
   441       fun cvar xi = cterm_of ProtoPure.thy (Var(xi,propT));
   442       val revcut_rl' =
   443           instantiate ([],  [(cvar("V",0), cvar("V",maxidx+1)),
   444                              (cvar("W",0), cvar("W",maxidx+1))]) revcut_rl
   445       val arg = (false, rl, nprems_of rl)
   446       val [th] = Seq.list_of (bicompose false arg 1 revcut_rl')
   447   in  th  end
   448   handle Bind => raise THM("make_elim_preserve", 1, [rl]);
   449 
   450 in
   451 
   452 val res_inst_meth = gen_inst (bires_inst_tac false) Tactic.resolve_tac;
   453 
   454 val eres_inst_meth = gen_inst (bires_inst_tac true) Tactic.eresolve_tac;
   455 
   456 val cut_inst_meth =
   457   gen_inst
   458     (fn ctxt => fn insts => bires_inst_tac false ctxt insts o make_elim_preserve)
   459     Tactic.cut_rules_tac;
   460 
   461 val dres_inst_meth =
   462   gen_inst
   463     (fn ctxt => fn insts => bires_inst_tac true ctxt insts o make_elim_preserve)
   464     Tactic.dresolve_tac;
   465 
   466 val forw_inst_meth =
   467   gen_inst
   468     (fn ctxt => fn insts => fn rule =>
   469        bires_inst_tac false ctxt insts (make_elim_preserve rule) THEN'
   470        assume_tac)
   471     Tactic.forward_tac;
   472 
   473 fun subgoal_tac ctxt sprop =
   474   DETERM o bires_inst_tac false ctxt [(("psi", 0), sprop)] cut_rl;
   475 
   476 fun subgoals_tac ctxt sprops = EVERY' (map (subgoal_tac ctxt) sprops);
   477 
   478 fun thin_tac ctxt s =
   479   bires_inst_tac true ctxt [(("V", 0), s)] thin_rl;
   480 
   481 end;
   482 
   483 
   484 (* ML tactics *)
   485 
   486 val tactic_ref = ref ((fn _ => raise Match): ProofContext.context -> thm list -> tactic);
   487 fun set_tactic f = tactic_ref := f;
   488 
   489 fun tactic txt ctxt = METHOD (fn facts =>
   490   (Context.use_mltext
   491     ("let fun tactic (ctxt: ProofContext.context) (facts: thm list) : tactic = \
   492        \let val thm = ProofContext.get_thm_closure ctxt o PureThy.Name\n\
   493        \  and thms = ProofContext.get_thms_closure ctxt o PureThy.Name in\n"
   494        ^ txt ^
   495        "\nend in Method.set_tactic tactic end")
   496     false NONE;
   497     Context.setmp (SOME (ProofContext.theory_of ctxt)) (! tactic_ref ctxt) facts));
   498 
   499 
   500 
   501 (** method syntax **)
   502 
   503 (* method text *)
   504 
   505 type src = Args.src;
   506 
   507 datatype text =
   508   Basic of (ProofContext.context -> method) |
   509   Source of src |
   510   Then of text list |
   511   Orelse of text list |
   512   Try of text |
   513   Repeat1 of text;
   514 
   515 val primitive_text = Basic o K o SIMPLE_METHOD o PRIMITIVE;
   516 val succeed_text = Basic (K succeed);
   517 val default_text = Source (Args.src (("default", []), Position.none));
   518 val this_text = Basic (K this);
   519 val done_text = Basic (K (SIMPLE_METHOD all_tac));
   520 val sorry_text = Basic o cheating;
   521 
   522 fun finish_text (NONE, immed) = Basic (close immed)
   523   | finish_text (SOME txt, immed) = Then [txt, Basic (close immed)];
   524 
   525 
   526 (* method definitions *)
   527 
   528 structure MethodsData = TheoryDataFun
   529 (struct
   530   val name = "Isar/methods";
   531   type T = (((src -> ProofContext.context -> method) * string) * stamp) NameSpace.table;
   532 
   533   val empty = NameSpace.empty_table;
   534   val copy = I;
   535   val extend = I;
   536   fun merge _ tables = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUPS dups =>
   537     error ("Attempt to merge different versions of method(s) " ^ commas_quote dups);
   538 
   539   fun print _ meths =
   540     let
   541       fun prt_meth (name, ((_, comment), _)) = Pretty.block
   542         [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   543     in
   544       [Pretty.big_list "methods:" (map prt_meth (NameSpace.extern_table meths))]
   545       |> Pretty.chunks |> Pretty.writeln
   546     end;
   547 end);
   548 
   549 val _ = Context.add_setup MethodsData.init;
   550 val print_methods = MethodsData.print;
   551 
   552 
   553 (* get methods *)
   554 
   555 exception METHOD_FAIL of (string * Position.T) * exn;
   556 
   557 fun method thy =
   558   let
   559     val (space, meths) = MethodsData.get thy;
   560     fun meth src =
   561       let
   562         val ((raw_name, _), pos) = Args.dest_src src;
   563         val name = NameSpace.intern space raw_name;
   564       in
   565         (case Symtab.lookup meths name of
   566           NONE => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
   567         | SOME ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
   568       end;
   569   in meth end;
   570 
   571 
   572 (* add method *)
   573 
   574 fun add_methods raw_meths thy =
   575   let
   576     val new_meths = raw_meths |> map (fn (name, f, comment) =>
   577       (name, ((f, comment), stamp ())));
   578 
   579     fun add meths = NameSpace.extend_table (Sign.naming_of thy) (meths, new_meths)
   580       handle Symtab.DUPS dups =>
   581         error ("Duplicate declaration of method(s) " ^ commas_quote dups);
   582   in MethodsData.map add thy end;
   583 
   584 val add_method = add_methods o Library.single;
   585 
   586 fun Method name meth cmt = Context.>> (add_method (name, meth, cmt));
   587 
   588 
   589 (* method_setup *)
   590 
   591 fun method_setup (name, txt, cmt) =
   592   Context.use_let
   593     "val thm = PureThy.get_thm_closure (Context.the_context ()) o PureThy.Name;\n\
   594     \val thms = PureThy.get_thms_closure (Context.the_context ()) o PureThy.Name;\n\
   595     \val method: bstring * (Method.src -> ProofContext.context -> Proof.method) * string"
   596     "Method.add_method method"
   597     ("(" ^ Library.quote name ^ ", " ^ txt ^ ", " ^ Library.quote cmt ^ ")");
   598 
   599 
   600 
   601 (** concrete syntax **)
   602 
   603 (* basic *)
   604 
   605 fun syntax (scan:
   606     (ProofContext.context * Args.T list -> 'a * (ProofContext.context * Args.T list))) =
   607   Args.syntax "method" scan;
   608 
   609 fun simple_args scan f src ctxt : method =
   610   #2 (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt);
   611 
   612 fun ctxt_args (f: ProofContext.context -> method) src ctxt =
   613   #2 (syntax (Scan.succeed (f ctxt)) src ctxt);
   614 
   615 fun no_args m = ctxt_args (K m);
   616 
   617 
   618 (* sections *)
   619 
   620 type modifier = (ProofContext.context -> ProofContext.context) * attribute;
   621 
   622 local
   623 
   624 fun sect ss = Scan.first (map Scan.lift ss);
   625 fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
   626 fun thmss ss = Scan.repeat (thms ss) >> List.concat;
   627 
   628 fun apply (f, att) (ctxt, ths) = foldl_map (Thm.proof_attributes [att]) (f ctxt, ths);
   629 
   630 fun section ss = (sect ss -- thmss ss) :-- (fn (m, ths) => Scan.depend (fn ctxt =>
   631   Scan.succeed (apply m (ctxt, ths)))) >> #2;
   632 
   633 fun sectioned args ss = args -- Scan.repeat (section ss);
   634 
   635 in
   636 
   637 fun sectioned_args args ss f src ctxt =
   638   let val (ctxt', (x, _)) = syntax (sectioned args ss) src ctxt
   639   in f x ctxt' end;
   640 
   641 fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f;
   642 fun bang_sectioned_args' ss scan f =
   643   sectioned_args (Args.bang_facts -- scan >> swap) ss (uncurry f);
   644 fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f);
   645 
   646 fun thms_ctxt_args f = sectioned_args (thmss []) [] f;
   647 fun thms_args f = thms_ctxt_args (K o f);
   648 fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected");
   649 
   650 end;
   651 
   652 
   653 (* iprover syntax *)
   654 
   655 local
   656 
   657 val introN = "intro";
   658 val elimN = "elim";
   659 val destN = "dest";
   660 val ruleN = "rule";
   661 
   662 fun modifier name kind kind' att =
   663   Args.$$$ name |-- (kind >> K NONE || kind' |-- Args.nat --| Args.colon >> SOME)
   664     >> (pair (I: ProofContext.context -> ProofContext.context) o att);
   665 
   666 val iprover_modifiers =
   667  [modifier destN Args.bang_colon Args.bang ContextRules.dest_bang,
   668   modifier destN Args.colon (Scan.succeed ()) ContextRules.dest,
   669   modifier elimN Args.bang_colon Args.bang ContextRules.elim_bang,
   670   modifier elimN Args.colon (Scan.succeed ()) ContextRules.elim,
   671   modifier introN Args.bang_colon Args.bang ContextRules.intro_bang,
   672   modifier introN Args.colon (Scan.succeed ()) ContextRules.intro,
   673   Args.del -- Args.colon >> K (I, ContextRules.rule_del)];
   674 
   675 in
   676 
   677 val iprover_meth =
   678   bang_sectioned_args' iprover_modifiers (Scan.lift (Scan.option Args.nat))
   679     (fn n => fn prems => fn ctxt => METHOD (fn facts =>
   680       HEADGOAL (insert_tac (prems @ facts) THEN'
   681       ObjectLogic.atomize_tac THEN' iprover_tac ctxt n)));
   682 
   683 end;
   684 
   685 
   686 (* tactic syntax *)
   687 
   688 fun nat_thms_args f = uncurry f oo
   689   (#2 oo syntax (Scan.lift (Scan.optional (Args.parens Args.nat) 0) -- Attrib.local_thmss));
   690 
   691 val insts =
   692   Scan.optional
   693     (Args.enum1 "and" (Scan.lift (Args.name -- (Args.$$$ "=" |-- Args.!!! Args.name))) --|
   694       Scan.lift (Args.$$$ "in")) [] -- Attrib.local_thmss;
   695 
   696 fun inst_args f src ctxt = f ctxt (#2 (syntax (Args.goal_spec HEADGOAL -- insts) src ctxt));
   697 
   698 val insts_var =
   699   Scan.optional
   700     (Args.enum1 "and" (Scan.lift (Args.var -- (Args.$$$ "=" |-- Args.!!! Args.name))) --|
   701       Scan.lift (Args.$$$ "in")) [] -- Attrib.local_thmss;
   702 
   703 fun inst_args_var f src ctxt =
   704   f ctxt (#2 (syntax (Args.goal_spec HEADGOAL -- insts_var) src ctxt));
   705 
   706 fun goal_args' args tac src ctxt = #2 (syntax (Args.goal_spec HEADGOAL -- args >>
   707   (fn (quant, s) => SIMPLE_METHOD' quant (tac s))) src ctxt);
   708 
   709 fun goal_args args tac = goal_args' (Scan.lift args) tac;
   710 
   711 fun goal_args_ctxt' args tac src ctxt =
   712   #2 (syntax (Args.goal_spec HEADGOAL -- args >>
   713   (fn (quant, s) => SIMPLE_METHOD' quant (tac ctxt s))) src ctxt);
   714 
   715 fun goal_args_ctxt args tac = goal_args_ctxt' (Scan.lift args) tac;
   716 
   717 
   718 (* misc tactic emulations *)
   719 
   720 val subgoal_meth = goal_args_ctxt (Scan.repeat1 Args.name) subgoals_tac;
   721 val thin_meth = goal_args_ctxt Args.name thin_tac;
   722 val rename_meth = goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac;
   723 val rotate_meth = goal_args (Scan.optional Args.int 1) Tactic.rotate_tac;
   724 
   725 
   726 (* theory setup *)
   727 
   728 val _ = Context.add_setup (add_methods
   729  [("fail", no_args fail, "force failure"),
   730   ("succeed", no_args succeed, "succeed"),
   731   ("-", no_args insert_facts, "do nothing (insert current facts only)"),
   732   ("insert", thms_args insert, "insert theorems, ignoring facts (improper)"),
   733   ("intro", thms_args intro, "repeatedly apply introduction rules"),
   734   ("elim", thms_args elim, "repeatedly apply elimination rules"),
   735   ("unfold", thms_ctxt_args unfold_meth, "unfold definitions"),
   736   ("fold", thms_ctxt_args fold_meth, "fold definitions"),
   737   ("atomize", (atomize o #2) oo syntax (Args.mode "full"),
   738     "present local premises as object-level statements"),
   739   ("iprover", iprover_meth, "intuitionistic proof search"),
   740   ("rule", thms_ctxt_args some_rule, "apply some intro/elim rule"),
   741   ("erule", nat_thms_args erule, "apply rule in elimination manner (improper)"),
   742   ("drule", nat_thms_args drule, "apply rule in destruct manner (improper)"),
   743   ("frule", nat_thms_args frule, "apply rule in forward manner (improper)"),
   744   ("this", no_args this, "apply current facts as rules"),
   745   ("fact", thms_ctxt_args fact, "composition by facts from context"),
   746   ("assumption", ctxt_args assumption, "proof by assumption, preferring facts"),
   747   ("rule_tac", inst_args_var res_inst_meth, "apply rule (dynamic instantiation)"),
   748   ("erule_tac", inst_args_var eres_inst_meth, "apply rule in elimination manner (dynamic instantiation)"),
   749   ("drule_tac", inst_args_var dres_inst_meth, "apply rule in destruct manner (dynamic instantiation)"),
   750   ("frule_tac", inst_args_var forw_inst_meth, "apply rule in forward manner (dynamic instantiation)"),
   751   ("cut_tac", inst_args_var cut_inst_meth, "cut rule (dynamic instantiation)"),
   752   ("subgoal_tac", subgoal_meth, "insert subgoal (dynamic instantiation)"),
   753   ("thin_tac", thin_meth, "remove premise (dynamic instantiation)"),
   754   ("rename_tac", rename_meth, "rename parameters of goal (dynamic instantiation)"),
   755   ("rotate_tac", rotate_meth, "rotate assumptions of goal"),
   756   ("tactic", simple_args Args.name tactic, "ML tactic as proof method")]);
   757 
   758 
   759 (*final declarations of this structure!*)
   760 val unfold = unfold_meth;
   761 val fold = fold_meth;
   762 
   763 end;
   764 
   765 structure BasicMethod: BASIC_METHOD = Method;
   766 open BasicMethod;