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