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