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