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