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