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