src/Pure/Isar/proof.ML
author wenzelm
Wed Sep 29 13:49:07 1999 +0200 (1999-09-29)
changeset 7632 25a0d2ba3a87
parent 7605 8bbfcb54054e
child 7665 1ab0c74cd748
permissions -rw-r--r--
removed extra shyps error;
     1 (*  Title:      Pure/Isar/proof.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Proof states and methods.
     6 *)
     7 
     8 signature PROOF =
     9 sig
    10   type context
    11   type state
    12   exception STATE of string * state
    13   val check_result: string -> state -> 'a Seq.seq -> 'a Seq.seq
    14   val init_state: theory -> state
    15   val context_of: state -> context
    16   val theory_of: state -> theory
    17   val sign_of: state -> Sign.sg
    18   val reset_thms: string -> state -> state
    19   val the_facts: state -> thm list
    20   val get_goal: state -> thm list * thm
    21   val goal_facts: (state -> thm list) -> state -> state
    22   val use_facts: state -> state
    23   val reset_facts: state -> state
    24   val assert_forward: state -> state
    25   val assert_backward: state -> state
    26   val enter_forward: state -> state
    27   val show_hyps: bool ref
    28   val pretty_thm: thm -> Pretty.T
    29   val pretty_thms: thm list -> Pretty.T
    30   val verbose: bool ref
    31   val print_state: int -> state -> unit
    32   val level: state -> int
    33   type method
    34   val method: (thm list -> tactic) -> method
    35   val refine: (context -> method) -> state -> state Seq.seq
    36   val export_thm: context -> thm -> thm
    37   val bind: (indexname * string) list -> state -> state
    38   val bind_i: (indexname * term) list -> state -> state
    39   val match_bind: (string list * string) list -> state -> state
    40   val match_bind_i: (term list * term) list -> state -> state
    41   val have_thmss: thm list -> string -> context attribute list ->
    42     (thm list * context attribute list) list -> state -> state
    43   val simple_have_thms: string -> thm list -> state -> state
    44   val fix: (string list * string option) list -> state -> state
    45   val fix_i: (string list * typ) list -> state -> state
    46   val assm: (int -> tactic) * (int -> tactic)
    47     -> (string * context attribute list * (string * (string list * string list)) list) list
    48     -> state -> state
    49   val assm_i: (int -> tactic) * (int -> tactic)
    50     -> (string * context attribute list * (term * (term list * term list)) list) list
    51     -> state -> state
    52   val assume: (string * context attribute list * (string * (string list * string list)) list) list
    53     -> state -> state
    54   val assume_i: (string * context attribute list * (term * (term list * term list)) list) list
    55     -> state -> state
    56   val presume: (string * context attribute list * (string * (string list * string list)) list) list
    57     -> state -> state
    58   val presume_i: (string * context attribute list * (term * (term list * term list)) list) list
    59     -> state -> state
    60   val theorem: bstring -> theory attribute list -> string * (string list * string list)
    61     -> theory -> state
    62   val theorem_i: bstring -> theory attribute list -> term * (term list * term list)
    63     -> theory -> state
    64   val lemma: bstring -> theory attribute list -> string * (string list * string list)
    65     -> theory -> state
    66   val lemma_i: bstring -> theory attribute list -> term * (term list * term list)
    67     -> theory -> state
    68   val chain: state -> state
    69   val export_chain: state -> state Seq.seq
    70   val from_facts: thm list -> state -> state
    71   val show: (state -> state Seq.seq) -> string -> context attribute list
    72     -> string * (string list * string list) -> state -> state
    73   val show_i: (state -> state Seq.seq) -> string -> context attribute list
    74     -> term * (term list * term list) -> state -> state
    75   val have: (state -> state Seq.seq) -> string -> context attribute list
    76     -> string * (string list * string list) -> state -> state
    77   val have_i: (state -> state Seq.seq) -> string -> context attribute list
    78     -> term * (term list * term list) -> state -> state
    79   val at_bottom: state -> bool
    80   val local_qed: (state -> state Seq.seq)
    81     -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit) -> state -> state Seq.seq
    82   val global_qed: (state -> state Seq.seq) -> state
    83     -> (theory * {kind: string, name: string, thm: thm}) Seq.seq
    84   val begin_block: state -> state
    85   val end_block: state -> state Seq.seq
    86   val next_block: state -> state
    87 end;
    88 
    89 signature PROOF_PRIVATE =
    90 sig
    91   include PROOF
    92   val put_data: Object.kind -> ('a -> Object.T) -> 'a -> state -> state
    93 end;
    94 
    95 structure Proof: PROOF_PRIVATE =
    96 struct
    97 
    98 
    99 (** proof state **)
   100 
   101 type context = ProofContext.context;
   102 
   103 
   104 (* type goal *)
   105 
   106 datatype kind =
   107   Theorem of theory attribute list |    (*top-level theorem*)
   108   Lemma of theory attribute list |      (*top-level lemma*)
   109   Goal of context attribute list |      (*intermediate result, solving subgoal*)
   110   Aux of context attribute list ;       (*intermediate result*)
   111 
   112 val kind_name =
   113   fn Theorem _ => "theorem" | Lemma _ => "lemma" | Goal _ => "show" | Aux _ => "have";
   114 
   115 type goal =
   116  (kind *        (*result kind*)
   117   string *      (*result name*)
   118   term) *       (*result statement*)
   119  (thm list *    (*use facts*)
   120   thm);         (*goal: subgoals ==> statement*)
   121 
   122 
   123 (* type mode *)
   124 
   125 datatype mode = Forward | ForwardChain | Backward;
   126 val mode_name = (fn Forward => "state" | ForwardChain => "chain" | Backward => "prove");
   127 
   128 
   129 (* datatype state *)
   130 
   131 datatype node =
   132   Node of
   133    {context: context,
   134     facts: thm list option,
   135     mode: mode,
   136     goal: (goal * (state -> state Seq.seq)) option}
   137 and state =
   138   State of
   139     node *              (*current*)
   140     node list;          (*parents wrt. block structure*)
   141 
   142 fun make_node (context, facts, mode, goal) =
   143   Node {context = context, facts = facts, mode = mode, goal = goal};
   144 
   145 
   146 exception STATE of string * state;
   147 
   148 fun err_malformed name state =
   149   raise STATE (name ^ ": internal error -- malformed proof state", state);
   150 
   151 fun check_result msg state sq =
   152   (case Seq.pull sq of
   153     None => raise STATE (msg, state)
   154   | Some s_sq => Seq.cons s_sq);
   155 
   156 
   157 fun map_current f (State (Node {context, facts, mode, goal}, nodes)) =
   158   State (make_node (f (context, facts, mode, goal)), nodes);
   159 
   160 fun init_state thy =
   161   State (make_node (ProofContext.init thy, None, Forward, None), []);
   162 
   163 
   164 
   165 (** basic proof state operations **)
   166 
   167 (* context *)
   168 
   169 fun context_of (State (Node {context, ...}, _)) = context;
   170 val theory_of = ProofContext.theory_of o context_of;
   171 val sign_of = ProofContext.sign_of o context_of;
   172 
   173 fun map_context f = map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
   174 
   175 fun map_context_result f (state as State (Node {context, facts, mode, goal}, nodes)) =
   176   let val (context', result) = f context
   177   in (State (make_node (context', facts, mode, goal), nodes), result) end;
   178 
   179 
   180 fun put_data kind f = map_context o ProofContext.put_data kind f;
   181 val declare_term = map_context o ProofContext.declare_term;
   182 val add_binds = map_context o ProofContext.add_binds_i;
   183 val auto_bind_goal = map_context o ProofContext.auto_bind_goal;
   184 val auto_bind_facts = map_context oo ProofContext.auto_bind_facts;
   185 val put_thms = map_context o ProofContext.put_thms;
   186 val put_thmss = map_context o ProofContext.put_thmss;
   187 val reset_thms = map_context o ProofContext.reset_thms;
   188 val assumptions = ProofContext.assumptions o context_of;
   189 
   190 
   191 (* facts *)
   192 
   193 fun the_facts (State (Node {facts = Some facts, ...}, _)) = facts
   194   | the_facts state = raise STATE ("No current facts available", state);
   195 
   196 fun assert_facts state = (the_facts state; state);
   197 fun get_facts (State (Node {facts, ...}, _)) = facts;
   198 
   199 
   200 val thisN = "this";
   201 
   202 fun put_facts facts state =
   203   state
   204   |> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal))
   205   |> (case facts of None => reset_thms thisN | Some ths => put_thms (thisN, ths));
   206 
   207 val reset_facts = put_facts None;
   208 
   209 fun have_facts (name, facts) state =
   210   state
   211   |> put_facts (Some facts)
   212   |> put_thms (name, facts);
   213 
   214 fun these_facts (state, ths) = have_facts ths state;
   215 
   216 
   217 (* goal *)
   218 
   219 fun find_goal i (state as State (Node {goal = Some goal, ...}, _)) = (context_of state, (i, goal))
   220   | find_goal i (State (Node {goal = None, ...}, node :: nodes)) =
   221       find_goal (i + 1) (State (node, nodes))
   222   | find_goal _ (state as State (_, [])) = err_malformed "find_goal" state;
   223 
   224 fun get_goal state =
   225   let val (_, (_, ((_, goal), _))) = find_goal 0 state
   226   in goal end;
   227 
   228 fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal));
   229 
   230 fun map_goal f (State (Node {context, facts, mode, goal = Some goal}, nodes)) =
   231       State (make_node (context, facts, mode, Some (f goal)), nodes)
   232   | map_goal f (State (nd, node :: nodes)) =
   233       let val State (node', nodes') = map_goal f (State (node, nodes))
   234       in State (nd, node' :: nodes') end
   235   | map_goal _ state = state;
   236 
   237 fun goal_facts get state =
   238   state
   239   |> map_goal (fn ((result, (_, thm)), f) => ((result, (get state, thm)), f));
   240 
   241 fun use_facts state =
   242   state
   243   |> goal_facts the_facts
   244   |> reset_facts;
   245 
   246 
   247 (* mode *)
   248 
   249 fun get_mode (State (Node {mode, ...}, _)) = mode;
   250 fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
   251 
   252 val enter_forward = put_mode Forward;
   253 val enter_forward_chain = put_mode ForwardChain;
   254 val enter_backward = put_mode Backward;
   255 
   256 fun assert_mode pred state =
   257   let val mode = get_mode state in
   258     if pred mode then state
   259     else raise STATE ("Illegal application of proof command in " ^ mode_name mode ^ " mode", state)
   260   end;
   261 
   262 fun is_chain state = get_mode state = ForwardChain;
   263 val assert_forward = assert_mode (equal Forward);
   264 val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain);
   265 val assert_backward = assert_mode (equal Backward);
   266 
   267 
   268 (* blocks *)
   269 
   270 fun level (State (_, nodes)) = length nodes;
   271 
   272 fun open_block (State (node, nodes)) = State (node, node :: nodes);
   273 
   274 fun new_block state =
   275   state
   276   |> open_block
   277   |> put_goal None;
   278 
   279 fun close_block (state as State (_, node :: nodes)) = State (node, nodes)
   280   | close_block state = raise STATE ("Unbalanced block parentheses", state);
   281 
   282 
   283 
   284 (** print_state **)
   285 
   286 val show_hyps = ProofContext.show_hyps;
   287 val pretty_thm = ProofContext.pretty_thm;
   288 
   289 fun pretty_thms [th] = pretty_thm th
   290   | pretty_thms ths = Pretty.blk (0, Pretty.fbreaks (map pretty_thm ths));
   291 
   292 
   293 val verbose = ProofContext.verbose;
   294 
   295 fun print_facts _ None = ()
   296   | print_facts s (Some ths) =
   297      (Pretty.writeln (Pretty.big_list (s ^ "this:") (map pretty_thm ths)); writeln "");
   298 
   299 fun print_state nr (state as State (Node {context, facts, mode, goal = _}, nodes)) =
   300   let
   301     val ref (_, _, begin_goal) = Goals.current_goals_markers;
   302 
   303     fun levels_up 0 = ""
   304       | levels_up 1 = ", 1 level up"
   305       | levels_up i = ", " ^ string_of_int i ^ " levels up";
   306 
   307     fun print_goal (_, (i, (((kind, name, _), (goal_facts, thm)), _))) =
   308       (print_facts "using "
   309           (if mode <> Backward orelse null goal_facts then None else Some goal_facts);
   310         writeln ("goal (" ^ kind_name kind ^ (if name = "" then "" else " " ^ name) ^
   311           levels_up (i div 2) ^ "):");
   312         Locale.print_goals_marker begin_goal (! goals_limit) thm);
   313 
   314     val ctxt_strings =
   315       if ! verbose orelse mode = Forward then ProofContext.strings_of_context context
   316       else if mode = Backward then ProofContext.strings_of_prems context
   317       else [];
   318   in
   319     writeln ("Proof(" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
   320       ", depth " ^ string_of_int (length nodes div 2));
   321     writeln "";
   322     if null ctxt_strings then () else (seq writeln ctxt_strings; writeln "");
   323     if ! verbose orelse mode = Forward then
   324       (print_facts "" facts; print_goal (find_goal 0 state))
   325     else if mode = ForwardChain then print_facts "picking " facts
   326     else print_goal (find_goal 0 state)
   327   end;
   328 
   329 
   330 
   331 (** proof steps **)
   332 
   333 (* datatype method *)
   334 
   335 datatype method = Method of thm list -> tactic;
   336 val method = Method;
   337 
   338 
   339 (* refine goal *)
   340 
   341 fun check_sign sg state =
   342   if Sign.subsig (sg, sign_of state) then state
   343   else raise STATE ("Bad signature of result: " ^ Sign.str_of_sg sg, state);
   344 
   345 fun refine meth_fun state =
   346   let
   347     val Method meth = meth_fun (context_of state);
   348     val (_, (_, ((result, (facts, thm)), f))) = find_goal 0 state;
   349 
   350     fun refn thm' =
   351       state
   352       |> check_sign (Thm.sign_of_thm thm')
   353       |> map_goal (K ((result, (facts, thm')), f));
   354   in Seq.map refn (meth facts thm) end;
   355 
   356 
   357 (* export *)
   358 
   359 local
   360 
   361 fun varify_frees fixes thm =
   362   let
   363     fun get_free x (None, t as Free (y, _)) = if x = y then Some t else None
   364       | get_free _ (opt, _) = opt;
   365 
   366     fun find_free t x = foldl_aterms (get_free x) (None, t);
   367 
   368     val {sign, prop, ...} = Thm.rep_thm thm;
   369     val frees = map (Thm.cterm_of sign) (mapfilter (find_free prop) fixes);
   370   in
   371     thm
   372     |> Drule.forall_intr_list frees
   373     |> Drule.forall_elim_vars 0
   374   end;
   375 
   376 fun most_general_varify_tfrees thm =
   377   let
   378     val {hyps, prop, ...} = Thm.rep_thm thm;
   379     val frees = foldr Term.add_term_frees (prop :: hyps, []);
   380     val leave_tfrees = foldr Term.add_term_tfree_names (frees, []);
   381   in thm |> Thm.varifyT' leave_tfrees end;
   382 
   383 fun diff_context inner None = (ProofContext.fixed_names inner, ProofContext.assumptions inner)
   384   | diff_context inner (Some outer) =
   385       (ProofContext.fixed_names inner \\ ProofContext.fixed_names outer,
   386         Library.drop (length (ProofContext.assumptions outer), ProofContext.assumptions inner));
   387 
   388 in
   389 
   390 fun export fixes casms thm =
   391   thm
   392   |> Drule.implies_intr_list casms
   393   |> varify_frees fixes
   394   |> most_general_varify_tfrees;
   395 
   396 fun export_wrt inner opt_outer =
   397   let
   398     val (fixes, asms) = diff_context inner opt_outer;
   399     val casms = map (Drule.mk_cgoal o #1) asms;
   400     val tacs = map #2 asms;
   401   in (export fixes casms, tacs) end;
   402 
   403 end;
   404 
   405 
   406 (* export results *)
   407 
   408 fun RANGE [] _ = all_tac
   409   | RANGE (tac :: tacs) i = RANGE tacs (i + 1) THEN tac i;
   410 
   411 fun export_goal print_rule raw_rule inner state =
   412   let
   413     val (outer, (_, ((result, (facts, thm)), f))) = find_goal 0 state;
   414     val (exp, tacs) = export_wrt inner (Some outer);
   415     val rule = exp raw_rule;
   416     val _ = print_rule rule;
   417     val thmq = FIRSTGOAL (Tactic.rtac rule THEN' RANGE (map #1 tacs)) thm;
   418   in Seq.map (fn th => map_goal (K ((result, (facts, th)), f)) state) thmq end;
   419 
   420 
   421 fun export_thm inner thm =
   422   let val (exp, tacs) = export_wrt inner None in
   423     (case Seq.chop (2, RANGE (map #2 tacs) 1 (exp thm)) of
   424       ([thm'], _) => thm'
   425     | ([], _) => raise THM ("export: failed", 0, [thm])
   426     | _ => raise THM ("export: more than one result", 0, [thm]))
   427   end;
   428 
   429 
   430 fun export_facts inner_state opt_outer_state state =
   431   let
   432     val thms = the_facts inner_state;
   433     val (exp, tacs) = export_wrt (context_of inner_state) (apsome context_of opt_outer_state);
   434     val thmqs = map (RANGE (map #2 tacs) 1 o exp) thms;
   435   in Seq.map (fn ths => put_facts (Some ths) state) (Seq.commute thmqs) end;
   436 
   437 fun transfer_facts inner_state state =
   438   (case get_facts inner_state of
   439     None => Seq.single (reset_facts state)
   440   | Some ths => export_facts inner_state (Some state) state);
   441 
   442 
   443 (* prepare result *)
   444 
   445 fun prep_result state t raw_thm =
   446   let
   447     val ctxt = context_of state;
   448     fun err msg = raise STATE (msg, state);
   449 
   450     val ngoals = Thm.nprems_of raw_thm;
   451     val _ =
   452       if ngoals = 0 then ()
   453       else (Locale.print_goals ngoals raw_thm; err (string_of_int ngoals ^ " unsolved goal(s)!"));
   454 
   455     val thm = raw_thm RS Drule.rev_triv_goal;
   456     val {hyps, prop, sign, maxidx, ...} = Thm.rep_thm thm;
   457     val tsig = Sign.tsig_of sign;
   458 
   459     val casms = map #1 (assumptions state);
   460     val bad_hyps = Library.gen_rems Term.aconv (hyps, map (Thm.term_of o Drule.mk_cgoal) casms);
   461   in
   462     if not (null bad_hyps) then
   463       err ("Additional hypotheses:\n" ^ cat_lines (map (Sign.string_of_term sign) bad_hyps))
   464     else if not (t aconv prop) then
   465       err ("Proved a different theorem: " ^ Sign.string_of_term sign prop)
   466     else Drule.forall_elim_vars (maxidx + 1) thm
   467   end;
   468 
   469 
   470 (* prepare final result *)
   471 
   472 fun strip_flexflex thm =
   473   Seq.hd (Thm.flexflex_rule thm) handle THM _ => thm;
   474 
   475 fun final_result state thm =
   476   thm
   477   |> strip_flexflex
   478   |> Drule.standard;
   479 
   480 
   481 
   482 (*** structured proof commands ***)
   483 
   484 (** context **)
   485 
   486 (* bind *)
   487 
   488 fun gen_bind f x state =
   489   state
   490   |> assert_forward
   491   |> map_context (f x)
   492   |> reset_facts;
   493 
   494 val bind = gen_bind (ProofContext.add_binds o map (apsnd Some))
   495 val bind_i = gen_bind (ProofContext.add_binds_i o map (apsnd Some))
   496 
   497 val match_bind = gen_bind ProofContext.match_binds;
   498 val match_bind_i = gen_bind ProofContext.match_binds_i;
   499 
   500 
   501 (* have_thmss *)
   502 
   503 fun have_thmss ths name atts ths_atts state =
   504   state
   505   |> assert_forward
   506   |> map_context_result (ProofContext.have_thmss ths name atts ths_atts)
   507   |> these_facts;
   508 
   509 fun simple_have_thms name thms = have_thmss [] name [] [(thms, [])];
   510 
   511 
   512 (* fix *)
   513 
   514 fun gen_fix f xs state =
   515   state
   516   |> assert_forward
   517   |> map_context (f xs)
   518   |> reset_facts;
   519 
   520 val fix = gen_fix ProofContext.fix;
   521 val fix_i = gen_fix ProofContext.fix_i;
   522 
   523 
   524 (* assume *)
   525 
   526 local
   527 
   528 fun gen_assume f tac args state =
   529   state
   530   |> assert_forward
   531   |> map_context_result (f tac args)
   532   |> (fn (st, (factss, prems)) =>
   533     foldl these_facts (st, factss)
   534     |> put_thms ("prems", prems)
   535     |> put_facts (Some (flat (map #2 factss))));
   536 
   537 val hard_asm_tac = Tactic.etac Drule.triv_goal;
   538 val soft_asm_tac = Tactic.rtac Drule.triv_goal;
   539 
   540 in
   541 
   542 val assm = gen_assume ProofContext.assume;
   543 val assm_i = gen_assume ProofContext.assume_i;
   544 val assume = assm (hard_asm_tac, soft_asm_tac);
   545 val assume_i = assm_i (hard_asm_tac, soft_asm_tac);
   546 val presume = assm (soft_asm_tac, soft_asm_tac);
   547 val presume_i = assm_i (soft_asm_tac, soft_asm_tac);
   548 
   549 end;
   550 
   551 
   552 
   553 (** goals **)
   554 
   555 (* forward chaining *)
   556 
   557 fun chain state =
   558   state
   559   |> assert_forward
   560   |> assert_facts
   561   |> enter_forward_chain;
   562 
   563 fun export_chain state =
   564   state
   565   |> assert_forward
   566   |> export_facts state None
   567   |> Seq.map chain;
   568 
   569 fun from_facts facts state =
   570   state
   571   |> put_facts (Some facts)
   572   |> chain;
   573 
   574 
   575 (* setup goals *)
   576 
   577 fun setup_goal opt_block prepp kind after_qed name atts raw_propp state =
   578   let
   579     val (state', prop) =
   580       state
   581       |> assert_forward_or_chain
   582       |> enter_forward
   583       |> map_context_result (fn c => prepp (c, raw_propp));
   584     val cprop = Thm.cterm_of (sign_of state') prop;
   585     val goal = Drule.mk_triv_goal cprop;
   586   in
   587     state'
   588     |> opt_block
   589     |> put_goal (Some (((kind atts, name, prop), ([], goal)), after_qed))
   590     |> auto_bind_goal prop
   591     |> (if is_chain state then use_facts else reset_facts)
   592     |> new_block
   593     |> enter_backward
   594   end;
   595 
   596 
   597 (*global goals*)
   598 fun global_goal prep kind name atts x thy =
   599   setup_goal I prep kind Seq.single name atts x (init_state thy);
   600 
   601 val theorem = global_goal ProofContext.bind_propp Theorem;
   602 val theorem_i = global_goal ProofContext.bind_propp_i Theorem;
   603 val lemma = global_goal ProofContext.bind_propp Lemma;
   604 val lemma_i = global_goal ProofContext.bind_propp_i Lemma;
   605 
   606 
   607 (*local goals*)
   608 fun local_goal prep kind f name atts x =
   609   setup_goal open_block prep kind f name atts x;
   610 
   611 val show   = local_goal ProofContext.bind_propp Goal;
   612 val show_i = local_goal ProofContext.bind_propp_i Goal;
   613 val have   = local_goal ProofContext.bind_propp Aux;
   614 val have_i = local_goal ProofContext.bind_propp_i Aux;
   615 
   616 
   617 
   618 (** conclusions **)
   619 
   620 (* current goal *)
   621 
   622 fun current_goal (State (Node {context, goal = Some goal, ...}, _)) = (context, goal)
   623   | current_goal state = raise STATE ("No current goal!", state);
   624 
   625 fun assert_current_goal true (state as State (Node {goal = None, ...}, _)) =
   626       raise STATE ("No goal in this block!", state)
   627   | assert_current_goal false (state as State (Node {goal = Some _, ...}, _)) =
   628       raise STATE ("Goal present in this block!", state)
   629   | assert_current_goal _ state = state;
   630 
   631 fun assert_bottom true (state as State (_, _ :: _)) =
   632       raise STATE ("Not at bottom of proof!", state)
   633   | assert_bottom false (state as State (_, [])) =
   634       raise STATE ("Already at bottom of proof!", state)
   635   | assert_bottom _ state = state;
   636 
   637 val at_bottom = can (assert_bottom true o close_block);
   638 
   639 fun end_proof bot state =
   640   state
   641   |> assert_forward
   642   |> close_block
   643   |> assert_bottom bot
   644   |> assert_current_goal true
   645   |> goal_facts (K []);
   646 
   647 
   648 (* local_qed *)
   649 
   650 fun finish_local (print_result, print_rule) state =
   651   let
   652     val (ctxt, (((kind, name, t), (_, raw_thm)), after_qed)) = current_goal state;
   653     val result = prep_result state t raw_thm;
   654     val (atts, opt_solve) =
   655       (case kind of
   656         Goal atts => (atts, export_goal print_rule result ctxt)
   657       | Aux atts => (atts, Seq.single)
   658       | _ => err_malformed "finish_local" state);
   659   in
   660     print_result {kind = kind_name kind, name = name, thm = result};
   661     state
   662     |> close_block
   663     |> auto_bind_facts name [t]
   664     |> have_thmss [] name atts [Thm.no_attributes [result]]
   665     |> opt_solve
   666     |> (Seq.flat o Seq.map after_qed)
   667   end;
   668 
   669 fun local_qed finalize print state =
   670   state
   671   |> end_proof false
   672   |> finalize
   673   |> (Seq.flat o Seq.map (finish_local print));
   674 
   675 
   676 (* global_qed *)
   677 
   678 fun finish_global state =
   679   let
   680     val (_, (((kind, name, t), (_, raw_thm)), _)) = current_goal state;   (*ignores after_qed!*)
   681     val result = final_result state (prep_result state t raw_thm);
   682 
   683     val atts =
   684       (case kind of
   685         Theorem atts => atts
   686       | Lemma atts => atts @ [Drule.tag_lemma]
   687       | _ => err_malformed "finish_global" state);
   688 
   689     val (thy', result') = PureThy.store_thm ((name, result), atts) (theory_of state);
   690   in (thy', {kind = kind_name kind, name = name, thm = result'}) end;
   691 
   692 (*Note: should inspect first result only, backtracking may destroy theory*)
   693 fun global_qed finalize state =
   694   state
   695   |> end_proof true
   696   |> finalize
   697   |> Seq.map finish_global;
   698 
   699 
   700 
   701 (** blocks **)
   702 
   703 (* begin_block *)
   704 
   705 fun begin_block state =
   706   state
   707   |> assert_forward
   708   |> new_block
   709   |> open_block;
   710 
   711 
   712 (* end_block *)
   713 
   714 fun end_block state =
   715   state
   716   |> assert_forward
   717   |> close_block
   718   |> assert_current_goal false
   719   |> close_block
   720   |> transfer_facts state;
   721 
   722 
   723 (* next_block *)
   724 
   725 fun next_block state =
   726   state
   727   |> assert_forward
   728   |> close_block
   729   |> assert_current_goal true
   730   |> new_block;
   731 
   732 
   733 end;