src/Pure/Isar/method.ML
author wenzelm
Mon Aug 28 20:32:38 2000 +0200 (2000-08-28)
changeset 9706 8e48a19fc81e
parent 9653 2937a854e3d7
child 9777 232fb8886765
permissions -rw-r--r--
removed METHOD0;
added SIMPLE_METHOD(');
added thm_args;
     1 (*  Title:      Pure/Isar/method.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Proof methods.
     7 *)
     8 
     9 signature BASIC_METHOD =
    10 sig
    11   val print_methods: theory -> unit
    12   val Method: bstring -> (Args.src -> Proof.context -> Proof.method) -> string -> unit
    13 end;
    14 
    15 signature METHOD =
    16 sig
    17   include BASIC_METHOD
    18   val print_global_rules: theory -> unit
    19   val print_local_rules: Proof.context -> unit
    20   val dest_global: theory attribute
    21   val elim_global: theory attribute
    22   val intro_global: theory attribute
    23   val delrule_global: theory attribute
    24   val dest_local: Proof.context attribute
    25   val elim_local: Proof.context attribute
    26   val intro_local: Proof.context attribute
    27   val delrule_local: Proof.context attribute
    28   val METHOD: (thm list -> tactic) -> Proof.method
    29   val METHOD_CASES:
    30     (thm list -> thm -> (thm * (string * RuleCases.T) list) Seq.seq) -> Proof.method
    31   val SIMPLE_METHOD: tactic -> Proof.method
    32   val SIMPLE_METHOD': ((int -> tactic) -> tactic) -> (int -> tactic) -> Proof.method
    33   val fail: Proof.method
    34   val succeed: Proof.method
    35   val defer: int option -> Proof.method
    36   val prefer: int -> Proof.method
    37   val insert_tac: thm list -> int -> tactic
    38   val insert: thm list -> Proof.method
    39   val insert_facts: Proof.method
    40   val unfold: thm list -> Proof.method
    41   val fold: thm list -> Proof.method
    42   val atomize_tac: thm list -> int -> tactic
    43   val atomize_goal: thm list -> int -> thm -> thm
    44   val multi_resolve: thm list -> thm -> thm Seq.seq
    45   val multi_resolves: thm list -> thm list -> thm Seq.seq
    46   val resolveq_tac: thm Seq.seq -> int -> tactic
    47   val resolveq_cases_tac: bool -> (thm * string list) Seq.seq
    48     -> int -> thm -> (thm * (string * RuleCases.T) list) Seq.seq
    49   val rule_tac: thm list -> thm list -> int -> tactic
    50   val erule_tac: thm list -> thm list -> int -> tactic
    51   val rule: thm list -> Proof.method
    52   val erule: thm list -> Proof.method
    53   val drule: thm list -> Proof.method
    54   val frule: thm list -> Proof.method
    55   val this: Proof.method
    56   val assumption: Proof.context -> Proof.method
    57   val set_tactic: (Proof.context -> thm list -> tactic) -> unit
    58   val tactic: string -> Proof.context -> Proof.method
    59   exception METHOD_FAIL of (string * Position.T) * exn
    60   val method: theory -> Args.src -> Proof.context -> Proof.method
    61   val add_method: bstring * (Args.src -> Proof.context -> Proof.method) * string
    62     -> theory -> theory
    63   val add_methods: (bstring * (Args.src -> Proof.context -> Proof.method) * string) list
    64     -> theory -> theory
    65   val syntax: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
    66     Args.src -> Proof.context -> Proof.context * 'a
    67   val simple_args: (Args.T list -> 'a * Args.T list)
    68     -> ('a -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    69   val ctxt_args: (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    70   val no_args: Proof.method -> Args.src -> Proof.context -> Proof.method
    71   type modifier
    72   val sectioned_args: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
    73     (Args.T list -> modifier * Args.T list) list ->
    74     ('a -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    75   val bang_sectioned_args:
    76     (Args.T list -> modifier * Args.T list) list ->
    77     (thm list -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    78   val only_sectioned_args:
    79     (Args.T list -> modifier * Args.T list) list ->
    80     (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    81   val thms_ctxt_args: (thm list -> Proof.context -> Proof.method)
    82     -> Args.src -> Proof.context -> Proof.method
    83   val thms_args: (thm list -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    84   val thm_args: (thm -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    85   datatype text =
    86     Basic of (Proof.context -> Proof.method) |
    87     Source of Args.src |
    88     Then of text list |
    89     Orelse of text list |
    90     Try of text |
    91     Repeat1 of text
    92   val refine: text -> Proof.state -> Proof.state Seq.seq
    93   val refine_end: text -> Proof.state -> Proof.state Seq.seq
    94   val proof: text option -> Proof.state -> Proof.state Seq.seq
    95   val local_qed: bool -> text option
    96     -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
    97     -> Proof.state -> Proof.state Seq.seq
    98   val local_terminal_proof: text * text option
    99     -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
   100     -> Proof.state -> Proof.state Seq.seq
   101   val local_default_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
   102     -> Proof.state -> Proof.state Seq.seq
   103   val local_immediate_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
   104     -> Proof.state -> Proof.state Seq.seq
   105   val local_done_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
   106     -> Proof.state -> Proof.state Seq.seq
   107   val global_qed: bool -> text option
   108     -> Proof.state -> theory * {kind: string, name: string, thm: thm}
   109   val global_terminal_proof: text * text option
   110     -> Proof.state -> theory * {kind: string, name: string, thm: thm}
   111   val global_default_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
   112   val global_immediate_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
   113   val global_done_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
   114   val goal_args: (Args.T list -> 'a * Args.T list) -> ('a -> int -> tactic)
   115     -> Args.src -> Proof.context -> Proof.method
   116   val goal_args': (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))
   117     -> ('a -> int -> tactic) -> Args.src -> Proof.context -> Proof.method
   118   val setup: (theory -> theory) list
   119 end;
   120 
   121 structure Method: METHOD =
   122 struct
   123 
   124 
   125 (** global and local rule data **)
   126 
   127 fun prt_rules kind ths =
   128   Pretty.writeln (Pretty.big_list ("standard " ^ kind ^ " rules:") (map Display.pretty_thm ths));
   129 
   130 fun print_rules (intro, elim) =
   131   (prt_rules "introduction" intro; prt_rules "elimination" elim);
   132 
   133 
   134 (* theory data kind 'Isar/rules' *)
   135 
   136 structure GlobalRulesArgs =
   137 struct
   138   val name = "Isar/rules";
   139   type T = thm list * thm list;
   140 
   141   val empty = ([], []);
   142   val copy = I;
   143   val prep_ext = I;
   144   fun merge ((intro1, elim1), (intro2, elim2)) =
   145     (Drule.merge_rules (intro1, intro2), Drule.merge_rules (elim1, elim2));
   146   fun print _ = print_rules;
   147 end;
   148 
   149 structure GlobalRules = TheoryDataFun(GlobalRulesArgs);
   150 val print_global_rules = GlobalRules.print;
   151 
   152 
   153 (* proof data kind 'Isar/rules' *)
   154 
   155 structure LocalRulesArgs =
   156 struct
   157   val name = "Isar/rules";
   158   type T = thm list * thm list;
   159 
   160   val init = GlobalRules.get;
   161   fun print _ = print_rules;
   162 end;
   163 
   164 structure LocalRules = ProofDataFun(LocalRulesArgs);
   165 val print_local_rules = LocalRules.print;
   166 
   167 
   168 
   169 (** attributes **)
   170 
   171 (* add rules *)
   172 
   173 local
   174 
   175 fun add_rule thm rules = Library.gen_ins Thm.eq_thm (thm, rules);
   176 fun del_rule thm rules = Library.gen_rem Thm.eq_thm (rules, thm);
   177 
   178 fun add_dest thm (intro, elim) = (intro, add_rule (Tactic.make_elim thm) elim);
   179 fun add_elim thm (intro, elim) = (intro, add_rule thm elim);
   180 fun add_intro thm (intro, elim) = (add_rule thm intro, elim);
   181 fun delrule thm (intro, elim) = (del_rule thm intro, del_rule thm elim);
   182 
   183 fun mk_att f g (x, thm) = (f (g thm) x, thm);
   184 
   185 in
   186 
   187 val dest_global = mk_att GlobalRules.map add_dest;
   188 val elim_global = mk_att GlobalRules.map add_elim;
   189 val intro_global = mk_att GlobalRules.map add_intro;
   190 val delrule_global = mk_att GlobalRules.map delrule;
   191 
   192 val dest_local = mk_att LocalRules.map add_dest;
   193 val elim_local = mk_att LocalRules.map add_elim;
   194 val intro_local = mk_att LocalRules.map add_intro;
   195 val delrule_local = mk_att LocalRules.map delrule;
   196 
   197 end;
   198 
   199 
   200 (* concrete syntax *)
   201 
   202 val rule_atts =
   203  [("dest", (Attrib.no_args dest_global, Attrib.no_args dest_local), "declare destruction rule"),
   204   ("elim", (Attrib.no_args elim_global, Attrib.no_args elim_local), "declare elimination rule"),
   205   ("intro", (Attrib.no_args intro_global, Attrib.no_args intro_local), "declare introduction rule"),
   206   ("delrule", (Attrib.no_args delrule_global, Attrib.no_args delrule_local), "undeclare rule")];
   207 
   208 
   209 
   210 (** proof methods **)
   211 
   212 (* make methods *)
   213 
   214 val METHOD = Proof.method;
   215 val METHOD_CASES = Proof.method_cases;
   216 
   217 
   218 (* primitive *)
   219 
   220 val fail = METHOD (K no_tac);
   221 val succeed = METHOD (K all_tac);
   222 
   223 
   224 (* shuffle *)
   225 
   226 fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1)));
   227 fun defer opt_i = METHOD (K (Tactic.defer_tac (if_none opt_i 1)));
   228 
   229 
   230 (* insert *)
   231 
   232 local
   233 
   234 fun cut_rule_tac raw_rule =
   235   let
   236     val rule = Drule.forall_intr_vars raw_rule;
   237     val revcut_rl = Drule.incr_indexes_wrt [] [] [] [rule] Drule.revcut_rl;
   238   in Tactic.rtac (rule COMP revcut_rl) end;
   239 
   240 in
   241 
   242 fun insert_tac [] i = all_tac
   243   | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
   244 
   245 val insert_facts = METHOD (ALLGOALS o insert_tac);
   246 fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
   247 
   248 end;
   249 
   250 
   251 (* simple methods *)
   252 
   253 fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac);
   254 fun SIMPLE_METHOD' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac));
   255 
   256 
   257 (* unfold / fold definitions *)
   258 
   259 fun unfold thms = SIMPLE_METHOD (CHANGED (rewrite_goals_tac thms));
   260 fun fold thms = SIMPLE_METHOD (CHANGED (fold_goals_tac thms));
   261 
   262 
   263 (* atomize meta-connectives *)
   264 
   265 fun atomize_tac rews i thm =
   266   if Logic.has_meta_prems (#prop (Thm.rep_thm thm)) i then
   267     Tactic.asm_rewrite_goal_tac (true, false, false) (K no_tac) (Thm.mss_of rews) i thm
   268   else all_tac thm;
   269 
   270 fun atomize_goal rews i thm =
   271   (case Seq.pull (atomize_tac rews i thm) of
   272     None => thm
   273   | Some (thm', _) => thm');
   274 
   275 
   276 (* multi_resolve *)
   277 
   278 local
   279 
   280 fun res th i rule =
   281   Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty;
   282 
   283 fun multi_res _ [] rule = Seq.single rule
   284   | multi_res i (th :: ths) rule = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths rule));
   285 
   286 in
   287 
   288 val multi_resolve = multi_res 1;
   289 fun multi_resolves facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules));
   290 
   291 end;
   292 
   293 
   294 (* general rule *)
   295 
   296 fun gen_resolveq_tac tac rules i st =
   297   Seq.flat (Seq.map (fn rule => tac rule i st) rules);
   298 
   299 val resolveq_tac = gen_resolveq_tac Tactic.rtac;
   300 
   301 fun resolveq_cases_tac open_parms = gen_resolveq_tac (fn (rule, cases) => fn i => fn st =>
   302   Seq.map (rpair (RuleCases.make (not open_parms) rule cases)) (Tactic.rtac rule i st));
   303 
   304 
   305 (* simple rule *)
   306 
   307 local
   308 
   309 fun gen_rule_tac tac rules [] = tac rules
   310   | gen_rule_tac tac erules facts = gen_resolveq_tac (tac o single) (multi_resolves facts erules);
   311 
   312 fun gen_rule tac rules = METHOD (HEADGOAL o tac rules);
   313 
   314 fun gen_rule' tac arg_rules ctxt = METHOD (fn facts =>
   315   let val rules =
   316     if not (null arg_rules) then arg_rules
   317     else if null facts then #1 (LocalRules.get ctxt)
   318     else op @ (LocalRules.get ctxt);
   319   in HEADGOAL (tac rules facts) end);
   320 
   321 fun setup raw_tac =
   322   let val tac = gen_rule_tac raw_tac
   323   in (tac, gen_rule tac, gen_rule' tac) end;
   324 
   325 in
   326 
   327 val (rule_tac, rule, some_rule) = setup Tactic.resolve_tac;
   328 val (erule_tac, erule, some_erule) = setup Tactic.eresolve_tac;
   329 val (drule_tac, drule, some_drule) = setup Tactic.dresolve_tac;
   330 val (frule_tac, frule, some_frule) = setup Tactic.forward_tac;
   331 
   332 end;
   333 
   334 
   335 (* this *)
   336 
   337 val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac));
   338 
   339 
   340 (* assumption *)
   341 
   342 fun assm_tac ctxt =
   343   assume_tac APPEND' resolve_tac (filter Thm.no_prems (ProofContext.prems_of ctxt));
   344 
   345 fun assumption_tac ctxt [] = assm_tac ctxt
   346   | assumption_tac _ [fact] = resolve_tac [fact]
   347   | assumption_tac _ _ = K no_tac;
   348 
   349 fun assumption ctxt = METHOD (HEADGOAL o assumption_tac ctxt);
   350 
   351 
   352 (* res_inst_tac etc. *)
   353 
   354 (*Note: insts refer to the implicit (!!) goal context; use at your own risk*)
   355 fun gen_res_inst _ tac (quant, ([], thms)) =
   356       METHOD (fn facts => (quant (insert_tac facts THEN' tac thms)))
   357   | gen_res_inst tac _ (quant, (insts, [thm])) =
   358       METHOD (fn facts => (quant (insert_tac facts THEN' tac insts thm)))
   359   | gen_res_inst _ _ _ = error "Cannot have instantiations with multiple rules";
   360 
   361 val res_inst = gen_res_inst Tactic.res_inst_tac Tactic.resolve_tac;
   362 val eres_inst = gen_res_inst Tactic.eres_inst_tac Tactic.eresolve_tac;
   363 val dres_inst = gen_res_inst Tactic.dres_inst_tac Tactic.dresolve_tac;
   364 val forw_inst = gen_res_inst Tactic.forw_inst_tac Tactic.forward_tac;
   365 val cut_inst = gen_res_inst Tactic.cut_inst_tac Tactic.cut_facts_tac;
   366 
   367 
   368 (* simple Prolog interpreter *)
   369 
   370 fun prolog_tac rules facts =
   371   DEPTH_SOLVE_1 (HEADGOAL (Tactic.assume_tac APPEND' Tactic.resolve_tac (facts @ rules)));
   372 
   373 val prolog = METHOD o prolog_tac;
   374 
   375 
   376 (* ML tactics *)
   377 
   378 val tactic_ref = ref ((fn _ => raise Match): Proof.context -> thm list -> tactic);
   379 fun set_tactic f = tactic_ref := f;
   380 
   381 fun tactic txt ctxt = METHOD (fn facts =>
   382   (Context.use_mltext
   383     ("let fun tactic (ctxt: PureIsar.Proof.context) (facts: thm list) : tactic = \
   384      \let val thm = PureIsar.ProofContext.get_thm_closure ctxt\n\
   385      \  and thms = PureIsar.ProofContext.get_thms_closure ctxt in\n"
   386      ^ txt ^
   387      "\nend in PureIsar.Method.set_tactic tactic end")
   388     false None;
   389     Context.setmp (Some (ProofContext.theory_of ctxt)) (! tactic_ref ctxt) facts));
   390 
   391 
   392 
   393 (** methods theory data **)
   394 
   395 (* data kind 'Isar/methods' *)
   396 
   397 structure MethodsDataArgs =
   398 struct
   399   val name = "Isar/methods";
   400   type T =
   401     {space: NameSpace.T,
   402      meths: (((Args.src -> Proof.context -> Proof.method) * string) * stamp) Symtab.table};
   403 
   404   val empty = {space = NameSpace.empty, meths = Symtab.empty};
   405   val copy = I;
   406   val prep_ext = I;
   407   fun merge ({space = space1, meths = meths1}, {space = space2, meths = meths2}) =
   408     {space = NameSpace.merge (space1, space2),
   409       meths = Symtab.merge eq_snd (meths1, meths2) handle Symtab.DUPS dups =>
   410         error ("Attempt to merge different versions of methods " ^ commas_quote dups)};
   411 
   412   fun print _ {space, meths} =
   413     let
   414       fun prt_meth (name, ((_, comment), _)) = Pretty.block
   415         [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   416     in
   417       [Pretty.big_list "methods:" (map prt_meth (NameSpace.cond_extern_table space meths))]
   418       |> Pretty.chunks |> Pretty.writeln
   419     end;
   420 end;
   421 
   422 structure MethodsData = TheoryDataFun(MethodsDataArgs);
   423 val print_methods = MethodsData.print;
   424 
   425 
   426 (* get methods *)
   427 
   428 exception METHOD_FAIL of (string * Position.T) * exn;
   429 
   430 fun method thy =
   431   let
   432     val {space, meths} = MethodsData.get thy;
   433 
   434     fun meth src =
   435       let
   436         val ((raw_name, _), pos) = Args.dest_src src;
   437         val name = NameSpace.intern space raw_name;
   438       in
   439         (case Symtab.lookup (meths, name) of
   440           None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
   441         | Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
   442       end;
   443   in meth end;
   444 
   445 
   446 (* add_method(s) *)
   447 
   448 fun add_methods raw_meths thy =
   449   let
   450     val full = Sign.full_name (Theory.sign_of thy);
   451     val new_meths =
   452       map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths;
   453 
   454     val {space, meths} = MethodsData.get thy;
   455     val space' = NameSpace.extend (space, map fst new_meths);
   456     val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups =>
   457       error ("Duplicate declaration of method(s) " ^ commas_quote dups);
   458   in
   459     thy |> MethodsData.put {space = space', meths = meths'}
   460   end;
   461 
   462 val add_method = add_methods o Library.single;
   463 
   464 (*implicit version*)
   465 fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]);
   466 
   467 
   468 
   469 (** method syntax **)
   470 
   471 (* basic *)
   472 
   473 fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
   474   Args.syntax "method" scan;
   475 
   476 fun simple_args scan f src ctxt : Proof.method =
   477   #2 (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt);
   478 
   479 fun ctxt_args (f: Proof.context -> Proof.method) src ctxt =
   480   #2 (syntax (Scan.succeed (f ctxt)) src ctxt);
   481 
   482 fun no_args m = ctxt_args (K m);
   483 
   484 
   485 
   486 (* sections *)
   487 
   488 type modifier = (Proof.context -> Proof.context) * Proof.context attribute;
   489 
   490 local
   491 
   492 fun sect ss = Scan.first (map Scan.lift ss);
   493 fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
   494 fun thmss ss = Scan.repeat (thms ss) >> flat;
   495 
   496 fun apply (f, att) (ctxt, ths) = Thm.applys_attributes ((f ctxt, ths), [att]);
   497 
   498 fun section ss = (sect ss -- thmss ss) :-- (fn (m, ths) => Scan.depend (fn ctxt =>
   499   Scan.succeed (apply m (ctxt, ths)))) >> #2;
   500 
   501 fun sectioned args ss = args -- Scan.repeat (section ss);
   502 
   503 in
   504 
   505 fun sectioned_args args ss f src ctxt =
   506   let val (ctxt', (x, _)) = syntax (sectioned args ss) src ctxt
   507   in f x ctxt' end;
   508 
   509 fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f;
   510 fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f);
   511 
   512 fun thms_ctxt_args f = sectioned_args (thmss []) [] f;
   513 fun thms_args f = thms_ctxt_args (K o f);
   514 fun thm_args f = thms_args (fn [thm] => f thm | _ => error "Single theorem expected");
   515 
   516 end;
   517 
   518 
   519 (* tactic syntax *)
   520 
   521 val insts =
   522   Scan.optional
   523     (Args.enum1 "and" (Scan.lift (Args.name -- (Args.$$$ "=" |-- Args.!!! Args.name))) --|
   524       Scan.lift (Args.$$$ "in")) [] -- Attrib.local_thmss;
   525 
   526 fun inst_args f = f oo (#2 oo syntax (Args.goal_spec HEADGOAL -- insts));
   527 
   528 
   529 fun goal_args' args tac = #2 oo syntax (Args.goal_spec HEADGOAL -- args >>
   530   (fn (quant, s) => SIMPLE_METHOD' quant (tac s)));
   531 
   532 fun goal_args args tac = goal_args' (Scan.lift args) tac;
   533 
   534 
   535 
   536 (** method text **)
   537 
   538 (* datatype text *)
   539 
   540 datatype text =
   541   Basic of (Proof.context -> Proof.method) |
   542   Source of Args.src |
   543   Then of text list |
   544   Orelse of text list |
   545   Try of text |
   546   Repeat1 of text;
   547 
   548 
   549 (* refine *)
   550 
   551 fun gen_refine f text state =
   552   let
   553     val thy = Proof.theory_of state;
   554 
   555     fun eval (Basic mth) = f mth
   556       | eval (Source src) = f (method thy src)
   557       | eval (Then txts) = Seq.EVERY (map eval txts)
   558       | eval (Orelse txts) = Seq.FIRST (map eval txts)
   559       | eval (Try txt) = Seq.TRY (eval txt)
   560       | eval (Repeat1 txt) = Seq.REPEAT1 (eval txt);
   561   in eval text state end;
   562 
   563 val refine = gen_refine Proof.refine;
   564 val refine_end = gen_refine Proof.refine_end;
   565 
   566 
   567 (* structured proof steps *)
   568 
   569 val default_text = Source (Args.src (("default", []), Position.none));
   570 val this_text = Basic (K this);
   571 val done_text = Basic (K (SIMPLE_METHOD all_tac));
   572 
   573 fun close_text asm = Basic (fn ctxt => METHOD (K
   574   (FILTER Thm.no_prems ((if asm then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac))));
   575 
   576 fun finish_text asm None = close_text asm
   577   | finish_text asm (Some txt) = Then [txt, close_text asm];
   578 
   579 fun proof opt_text state =
   580   state
   581   |> Proof.assert_backward
   582   |> refine (if_none opt_text default_text)
   583   |> Seq.map (Proof.goal_facts (K []))
   584   |> Seq.map Proof.enter_forward;
   585 
   586 fun local_qed asm opt_text = Proof.local_qed (refine (finish_text asm opt_text));
   587 fun local_terminal_proof (text, opt_text) pr =
   588   Seq.THEN (proof (Some text), local_qed true opt_text pr);
   589 val local_default_proof = local_terminal_proof (default_text, None);
   590 val local_immediate_proof = local_terminal_proof (this_text, None);
   591 fun local_done_proof pr = Seq.THEN (proof (Some done_text), local_qed false None pr);
   592 
   593 
   594 fun global_qeds asm opt_text = Proof.global_qed (refine (finish_text asm opt_text));
   595 
   596 fun global_qed asm opt_text state =
   597   state
   598   |> global_qeds asm opt_text
   599   |> Proof.check_result "Failed to finish proof" state
   600   |> Seq.hd;
   601 
   602 fun global_term_proof asm (text, opt_text) state =
   603   state
   604   |> proof (Some text)
   605   |> Proof.check_result "Terminal proof method failed" state
   606   |> (Seq.flat o Seq.map (global_qeds asm opt_text))
   607   |> Proof.check_result "Failed to finish proof (after successful terminal method)" state
   608   |> Seq.hd;
   609 
   610 val global_terminal_proof = global_term_proof true;
   611 val global_default_proof = global_terminal_proof (default_text, None);
   612 val global_immediate_proof = global_terminal_proof (this_text, None);
   613 val global_done_proof = global_term_proof false (done_text, None);
   614 
   615 
   616 (** theory setup **)
   617 
   618 (* misc tactic emulations *)
   619 
   620 val subgoal_meth = goal_args (Scan.repeat1 Args.name) Tactic.subgoals_tac;
   621 val thin_meth = goal_args Args.name Tactic.thin_tac;
   622 val rename_meth = goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac;
   623 val rotate_meth = goal_args (Scan.optional Args.int 1) Tactic.rotate_tac;
   624 
   625 
   626 (* pure_methods *)
   627 
   628 val pure_methods =
   629  [("fail", no_args fail, "force failure"),
   630   ("succeed", no_args succeed, "succeed"),
   631   ("-", no_args insert_facts, "do nothing (insert current facts only)"),
   632   ("insert", thms_args insert, "insert theorems, ignoring facts (improper)"),
   633   ("unfold", thms_args unfold, "unfold definitions"),
   634   ("fold", thms_args fold, "fold definitions"),
   635   ("default", thms_ctxt_args some_rule, "apply some rule"),
   636   ("rule", thms_ctxt_args some_rule, "apply some rule"),
   637   ("erule", thms_ctxt_args some_erule, "apply some rule in elimination manner (improper)"),
   638   ("drule", thms_ctxt_args some_drule, "apply some rule in destruct manner (improper)"),
   639   ("frule", thms_ctxt_args some_frule, "apply some rule in forward manner (improper)"),
   640   ("this", no_args this, "apply current facts as rules"),
   641   ("assumption", ctxt_args assumption, "proof by assumption, preferring facts"),
   642   ("rule_tac", inst_args res_inst, "apply rule (dynamic instantiation!)"),
   643   ("erule_tac", inst_args eres_inst, "apply rule in elimination manner (dynamic instantiation!)"),
   644   ("drule_tac", inst_args dres_inst, "apply rule in destruct manner (dynamic instantiation!)"),
   645   ("frule_tac", inst_args forw_inst, "apply rule in forward manner (dynamic instantiation!)"),
   646   ("cut_tac", inst_args cut_inst, "cut rule (dynamic instantiation!)"),
   647   ("subgoal_tac", subgoal_meth, "insert subgoal (dynamic instantiation!)"),
   648   ("thin_tac", thin_meth, "remove premise (dynamic instantiation!)"),
   649   ("rename_tac", rename_meth, "rename parameters of goal (dynamic instantiation!)"),
   650   ("rotate_tac", rotate_meth, "rotate assumptions of goal"),
   651   ("prolog", thms_args prolog, "simple prolog interpreter"),
   652   ("tactic", simple_args Args.name tactic, "ML tactic as proof method")];
   653 
   654 
   655 (* setup *)
   656 
   657 val setup =
   658  [GlobalRules.init, LocalRules.init, Attrib.add_attributes rule_atts,
   659   MethodsData.init, add_methods pure_methods];
   660 
   661 
   662 end;
   663 
   664 
   665 structure BasicMethod: BASIC_METHOD = Method;
   666 open BasicMethod;