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