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