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