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