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