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