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