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