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