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