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