renamed goals.ML to old_goals.ML;
authorwenzelm
Tue Nov 08 10:43:11 2005 +0100 (2005-11-08)
changeset 1812041328805d4db
parent 18119 63901a9b99dc
child 18121 77b6d06ad99d
renamed goals.ML to old_goals.ML;
inline Drule.impose_hyps;
src/Pure/old_goals.ML
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/old_goals.ML	Tue Nov 08 10:43:11 2005 +0100
     1.3 @@ -0,0 +1,542 @@
     1.4 +(*  Title:      Pure/old_goals.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     1.7 +    Copyright   1993  University of Cambridge
     1.8 +
     1.9 +Old-style goal stack package.  The goal stack initially holds a dummy
    1.10 +proof, and can never become empty.  Each goal stack consists of a list
    1.11 +of levels.  The undo list is a list of goal stacks.  Finally, there
    1.12 +may be a stack of pending proofs.
    1.13 +*)
    1.14 +
    1.15 +signature GOALS =
    1.16 +sig
    1.17 +  val premises: unit -> thm list
    1.18 +  val prove_goal: theory -> string -> (thm list -> tactic list) -> thm
    1.19 +  val prove_goalw: theory -> thm list -> string -> (thm list -> tactic list) -> thm
    1.20 +  val disable_pr: unit -> unit
    1.21 +  val enable_pr: unit -> unit
    1.22 +  val topthm: unit -> thm
    1.23 +  val result: unit -> thm
    1.24 +  val uresult: unit -> thm
    1.25 +  val getgoal: int -> term
    1.26 +  val gethyps: int -> thm list
    1.27 +  val prlev: int -> unit
    1.28 +  val pr: unit -> unit
    1.29 +  val prlim: int -> unit
    1.30 +  val goal: theory -> string -> thm list
    1.31 +  val goalw: theory -> thm list -> string -> thm list
    1.32 +  val Goal: string -> thm list
    1.33 +  val Goalw: thm list -> string -> thm list
    1.34 +  val by: tactic -> unit
    1.35 +  val back: unit -> unit
    1.36 +  val choplev: int -> unit
    1.37 +  val chop: unit -> unit
    1.38 +  val undo: unit -> unit
    1.39 +  val bind_thm: string * thm -> unit
    1.40 +  val bind_thms: string * thm list -> unit
    1.41 +  val qed: string -> unit
    1.42 +  val qed_goal: string -> theory -> string -> (thm list -> tactic list) -> unit
    1.43 +  val qed_goalw: string -> theory -> thm list -> string
    1.44 +    -> (thm list -> tactic list) -> unit
    1.45 +  val qed_spec_mp: string -> unit
    1.46 +  val qed_goal_spec_mp: string -> theory -> string -> (thm list -> tactic list) -> unit
    1.47 +  val qed_goalw_spec_mp: string -> theory -> thm list -> string
    1.48 +    -> (thm list -> tactic list) -> unit
    1.49 +  val no_qed: unit -> unit
    1.50 +  val thms_containing: xstring list -> (string * thm) list
    1.51 +end;
    1.52 +
    1.53 +signature OLD_GOALS =
    1.54 +sig
    1.55 +  include GOALS
    1.56 +  val legacy: bool ref
    1.57 +  type proof
    1.58 +  val reset_goals: unit -> unit
    1.59 +  val result_error_fn: (thm -> string -> thm) ref
    1.60 +  val print_sign_exn: theory -> exn -> 'a
    1.61 +  val prove_goalw_cterm: thm list->cterm->(thm list->tactic list)->thm
    1.62 +  val prove_goalw_cterm_nocheck: thm list->cterm->(thm list->tactic list)->thm
    1.63 +  val quick_and_dirty_prove_goalw_cterm: theory -> thm list -> cterm
    1.64 +    -> (thm list -> tactic list) -> thm
    1.65 +  val print_exn: exn -> 'a
    1.66 +  val filter_goal: (term*term->bool) -> thm list -> int -> thm list
    1.67 +  val goalw_cterm: thm list -> cterm -> thm list
    1.68 +  val simple_prove_goal_cterm: cterm->(thm list->tactic list)->thm
    1.69 +  val byev: tactic list -> unit
    1.70 +  val save_proof: unit -> proof
    1.71 +  val restore_proof: proof -> thm list
    1.72 +  val push_proof: unit -> unit
    1.73 +  val pop_proof: unit -> thm list
    1.74 +  val rotate_proof: unit -> thm list
    1.75 +  val bws: thm list -> unit
    1.76 +  val bw: thm -> unit
    1.77 +  val brs: thm list -> int -> unit
    1.78 +  val br: thm -> int -> unit
    1.79 +  val bes: thm list -> int -> unit
    1.80 +  val be: thm -> int -> unit
    1.81 +  val bds: thm list -> int -> unit
    1.82 +  val bd: thm -> int -> unit
    1.83 +  val ba: int -> unit
    1.84 +  val ren: string -> int -> unit
    1.85 +  val frs: thm list -> unit
    1.86 +  val fr: thm -> unit
    1.87 +  val fes: thm list -> unit
    1.88 +  val fe: thm -> unit
    1.89 +  val fds: thm list -> unit
    1.90 +  val fd: thm -> unit
    1.91 +  val fa: unit -> unit
    1.92 +end;
    1.93 +
    1.94 +structure OldGoals: OLD_GOALS =
    1.95 +struct
    1.96 +
    1.97 +val legacy = ref false;
    1.98 +fun warn_obsolete () = if ! legacy then () else warning "Obsolete goal command encountered";
    1.99 +
   1.100 +
   1.101 +(*** Goal package ***)
   1.102 +
   1.103 +(*Each level of goal stack includes a proof state and alternative states,
   1.104 +  the output of the tactic applied to the preceeding level.  *)
   1.105 +type gstack = (thm * thm Seq.seq) list;
   1.106 +
   1.107 +datatype proof = Proof of gstack list * thm list * (bool*thm->thm);
   1.108 +
   1.109 +
   1.110 +(*** References ***)
   1.111 +
   1.112 +(*Current assumption list -- set by "goal".*)
   1.113 +val curr_prems = ref([] : thm list);
   1.114 +
   1.115 +(*Return assumption list -- useful if you didn't save "goal"'s result. *)
   1.116 +fun premises() = !curr_prems;
   1.117 +
   1.118 +(*Current result maker -- set by "goal", used by "result".  *)
   1.119 +fun init_mkresult _ = error "No goal has been supplied in subgoal module";
   1.120 +val curr_mkresult = ref (init_mkresult: bool*thm->thm);
   1.121 +
   1.122 +val dummy = Thm.trivial (read_cterm ProtoPure.thy ("PROP No_goal_has_been_supplied", propT));
   1.123 +
   1.124 +(*List of previous goal stacks, for the undo operation.  Set by setstate.
   1.125 +  A list of lists!*)
   1.126 +val undo_list = ref([[(dummy, Seq.empty)]] : gstack list);
   1.127 +
   1.128 +(* Stack of proof attempts *)
   1.129 +val proofstack = ref([]: proof list);
   1.130 +
   1.131 +(*reset all refs*)
   1.132 +fun reset_goals () =
   1.133 +  (curr_prems := []; curr_mkresult := init_mkresult;
   1.134 +    undo_list := [[(dummy, Seq.empty)]]);
   1.135 +
   1.136 +
   1.137 +(*** Setting up goal-directed proof ***)
   1.138 +
   1.139 +(*Generates the list of new theories when the proof state's theory changes*)
   1.140 +fun thy_error (thy,thy') =
   1.141 +  let val names = Context.names_of thy' \\ Context.names_of thy
   1.142 +  in  case names of
   1.143 +        [name] => "\nNew theory: " ^ name
   1.144 +      | _       => "\nNew theories: " ^ space_implode ", " names
   1.145 +  end;
   1.146 +
   1.147 +(*Default action is to print an error message; could be suppressed for
   1.148 +  special applications.*)
   1.149 +fun result_error_default state msg : thm =
   1.150 +  Pretty.str "Bad final proof state:" :: Display.pretty_goals (!goals_limit) state @
   1.151 +    [Pretty.str msg, Pretty.str "Proof failed!"] |> Pretty.chunks |> Pretty.string_of |> error;
   1.152 +
   1.153 +val result_error_fn = ref result_error_default;
   1.154 +
   1.155 +
   1.156 +(*Common treatment of "goal" and "prove_goal":
   1.157 +  Return assumptions, initial proof state, and function to make result.
   1.158 +  "atomic" indicates if the goal should be wrapped up in the function
   1.159 +  "Goal::prop=>prop" to avoid assumptions being returned separately.
   1.160 +*)
   1.161 +fun prepare_proof atomic rths chorn =
   1.162 +  let
   1.163 +      val _ = warn_obsolete ();
   1.164 +      val {thy, t=horn,...} = rep_cterm chorn;
   1.165 +      val _ = Term.no_dummy_patterns horn handle TERM (msg, _) => error msg;
   1.166 +      val (As, B) = Logic.strip_horn horn;
   1.167 +      val atoms = atomic andalso
   1.168 +            forall (fn t => not(Logic.is_implies t orelse Logic.is_all t)) As;
   1.169 +      val (As,B) = if atoms then ([],horn) else (As,B);
   1.170 +      val cAs = map (cterm_of thy) As;
   1.171 +      val prems = map (rewrite_rule rths o forall_elim_vars 0 o Thm.assume) cAs;
   1.172 +      val cB = cterm_of thy B;
   1.173 +      val st0 = let val st = Goal.init cB |> fold Thm.weaken cAs
   1.174 +                in  rewrite_goals_rule rths st end
   1.175 +      (*discharges assumptions from state in the order they appear in goal;
   1.176 +        checks (if requested) that resulting theorem is equivalent to goal. *)
   1.177 +      fun mkresult (check,state) =
   1.178 +        let val state = Seq.hd (flexflex_rule state)
   1.179 +                        handle THM _ => state   (*smash flexflex pairs*)
   1.180 +            val ngoals = nprems_of state
   1.181 +            val ath = implies_intr_list cAs state
   1.182 +            val th = Goal.conclude ath
   1.183 +            val {hyps,prop,thy=thy',...} = rep_thm th
   1.184 +            val final_th = standard th
   1.185 +        in  if not check then final_th
   1.186 +            else if not (eq_thy(thy,thy')) then !result_error_fn state
   1.187 +                ("Theory of proof state has changed!" ^
   1.188 +                 thy_error (thy,thy'))
   1.189 +            else if ngoals>0 then !result_error_fn state
   1.190 +                (string_of_int ngoals ^ " unsolved goals!")
   1.191 +            else if not (null hyps) then !result_error_fn state
   1.192 +                ("Additional hypotheses:\n" ^
   1.193 +                 cat_lines (map (Sign.string_of_term thy) hyps))
   1.194 +            else if Pattern.matches thy
   1.195 +                                    (Envir.beta_norm (term_of chorn), Envir.beta_norm prop)
   1.196 +                 then final_th
   1.197 +            else  !result_error_fn state "proved a different theorem"
   1.198 +        end
   1.199 +  in
   1.200 +     if eq_thy(thy, Thm.theory_of_thm st0)
   1.201 +     then (prems, st0, mkresult)
   1.202 +     else error ("Definitions would change the proof state's theory" ^
   1.203 +                 thy_error (thy, Thm.theory_of_thm st0))
   1.204 +  end
   1.205 +  handle THM(s,_,_) => error("prepare_proof: exception THM was raised!\n" ^ s);
   1.206 +
   1.207 +(*Prints exceptions readably to users*)
   1.208 +fun print_sign_exn_unit thy e =
   1.209 +  case e of
   1.210 +     THM (msg,i,thms) =>
   1.211 +         (writeln ("Exception THM " ^ string_of_int i ^ " raised:\n" ^ msg);
   1.212 +          List.app print_thm thms)
   1.213 +   | THEORY (msg,thys) =>
   1.214 +         (writeln ("Exception THEORY raised:\n" ^ msg);
   1.215 +          List.app (writeln o Context.str_of_thy) thys)
   1.216 +   | TERM (msg,ts) =>
   1.217 +         (writeln ("Exception TERM raised:\n" ^ msg);
   1.218 +          List.app (writeln o Sign.string_of_term thy) ts)
   1.219 +   | TYPE (msg,Ts,ts) =>
   1.220 +         (writeln ("Exception TYPE raised:\n" ^ msg);
   1.221 +          List.app (writeln o Sign.string_of_typ thy) Ts;
   1.222 +          List.app (writeln o Sign.string_of_term thy) ts)
   1.223 +   | e => raise e;
   1.224 +
   1.225 +(*Prints an exception, then fails*)
   1.226 +fun print_sign_exn thy e = (print_sign_exn_unit thy e; raise ERROR);
   1.227 +
   1.228 +(** the prove_goal.... commands
   1.229 +    Prove theorem using the listed tactics; check it has the specified form.
   1.230 +    Augment theory with all type assignments of goal.
   1.231 +    Syntax is similar to "goal" command for easy keyboard use. **)
   1.232 +
   1.233 +(*Version taking the goal as a cterm*)
   1.234 +fun prove_goalw_cterm_general check rths chorn tacsf =
   1.235 +  let val (prems, st0, mkresult) = prepare_proof false rths chorn
   1.236 +      val tac = EVERY (tacsf prems)
   1.237 +      fun statef() =
   1.238 +          (case Seq.pull (tac st0) of
   1.239 +               SOME(st,_) => st
   1.240 +             | _ => error ("prove_goal: tactic failed"))
   1.241 +  in  mkresult (check, cond_timeit (!Output.timing) statef)  end
   1.242 +  handle e => (print_sign_exn_unit (#thy (rep_cterm chorn)) e;
   1.243 +               writeln ("The exception above was raised for\n" ^
   1.244 +                      Display.string_of_cterm chorn); raise e);
   1.245 +
   1.246 +(*Two variants: one checking the result, one not.
   1.247 +  Neither prints runtime messages: they are for internal packages.*)
   1.248 +fun prove_goalw_cterm rths chorn =
   1.249 +        setmp Output.timing false (prove_goalw_cterm_general true rths chorn)
   1.250 +and prove_goalw_cterm_nocheck rths chorn =
   1.251 +        setmp Output.timing false (prove_goalw_cterm_general false rths chorn);
   1.252 +
   1.253 +
   1.254 +(*Version taking the goal as a string*)
   1.255 +fun prove_goalw thy rths agoal tacsf =
   1.256 +  let val chorn = read_cterm thy (agoal, propT)
   1.257 +  in prove_goalw_cterm_general true rths chorn tacsf end
   1.258 +  handle ERROR => error (*from read_cterm?*)
   1.259 +                ("The error(s) above occurred for " ^ quote agoal);
   1.260 +
   1.261 +(*String version with no meta-rewrite-rules*)
   1.262 +fun prove_goal thy = prove_goalw thy [];
   1.263 +
   1.264 +(*quick and dirty version (conditional)*)
   1.265 +fun quick_and_dirty_prove_goalw_cterm thy rews ct tacs =
   1.266 +  prove_goalw_cterm rews ct
   1.267 +    (if ! quick_and_dirty then (K [SkipProof.cheat_tac thy]) else tacs);
   1.268 +
   1.269 +
   1.270 +(*** Commands etc ***)
   1.271 +
   1.272 +(*Return the current goal stack, if any, from undo_list*)
   1.273 +fun getstate() : gstack = case !undo_list of
   1.274 +      []   => error"No current state in subgoal module"
   1.275 +    | x::_ => x;
   1.276 +
   1.277 +(*Pops the given goal stack*)
   1.278 +fun pop [] = error"Cannot go back past the beginning of the proof!"
   1.279 +  | pop (pair::pairs) = (pair,pairs);
   1.280 +
   1.281 +
   1.282 +(* Print a level of the goal stack -- subject to quiet mode *)
   1.283 +
   1.284 +val quiet = ref false;
   1.285 +fun disable_pr () = quiet := true;
   1.286 +fun enable_pr () = quiet := false;
   1.287 +
   1.288 +fun print_top ((th, _), pairs) =
   1.289 +  if ! quiet then ()
   1.290 +  else ! Display.print_current_goals_fn (length pairs) (! goals_limit) th;
   1.291 +
   1.292 +(*Printing can raise exceptions, so the assignment occurs last.
   1.293 +  Can do   setstate[(st,Seq.empty)]  to set st as the state.  *)
   1.294 +fun setstate newgoals =
   1.295 +  (print_top (pop newgoals);  undo_list := newgoals :: !undo_list);
   1.296 +
   1.297 +(*Given a proof state transformation, return a command that updates
   1.298 +    the goal stack*)
   1.299 +fun make_command com = setstate (com (pop (getstate())));
   1.300 +
   1.301 +(*Apply a function on proof states to the current goal stack*)
   1.302 +fun apply_fun f = f (pop(getstate()));
   1.303 +
   1.304 +(*Return the top theorem, representing the proof state*)
   1.305 +fun topthm () = apply_fun  (fn ((th,_), _) => th);
   1.306 +
   1.307 +(*Return the final result.  *)
   1.308 +fun result () = !curr_mkresult (true, topthm());
   1.309 +
   1.310 +(*Return the result UNCHECKED that it equals the goal -- for synthesis,
   1.311 +  answer extraction, or other instantiation of Vars *)
   1.312 +fun uresult () = !curr_mkresult (false, topthm());
   1.313 +
   1.314 +(*Get subgoal i from goal stack*)
   1.315 +fun getgoal i = Logic.get_goal (prop_of (topthm())) i;
   1.316 +
   1.317 +(*Return subgoal i's hypotheses as meta-level assumptions.
   1.318 +  For debugging uses of METAHYPS*)
   1.319 +local exception GETHYPS of thm list
   1.320 +in
   1.321 +fun gethyps i =
   1.322 +    (METAHYPS (fn hyps => raise (GETHYPS hyps)) i (topthm());  [])
   1.323 +    handle GETHYPS hyps => hyps
   1.324 +end;
   1.325 +
   1.326 +(*Prints exceptions nicely at top level;
   1.327 +  raises the exception in order to have a polymorphic type!*)
   1.328 +fun print_exn e = (print_sign_exn_unit (Thm.theory_of_thm (topthm())) e;  raise e);
   1.329 +
   1.330 +(*Which thms could apply to goal i? (debugs tactics involving filter_thms) *)
   1.331 +fun filter_goal could ths i = filter_thms could (999, getgoal i, ths);
   1.332 +
   1.333 +(*For inspecting earlier levels of the backward proof*)
   1.334 +fun chop_level n (pair,pairs) =
   1.335 +  let val level = length pairs
   1.336 +  in  if n<0 andalso ~n <= level
   1.337 +      then  List.drop (pair::pairs, ~n)
   1.338 +      else if 0<=n andalso n<= level
   1.339 +      then  List.drop (pair::pairs, level - n)
   1.340 +      else  error ("Level number must lie between 0 and " ^
   1.341 +                   string_of_int level)
   1.342 +  end;
   1.343 +
   1.344 +(*Print the given level of the proof; prlev ~1 prints previous level*)
   1.345 +fun prlev n = (enable_pr (); apply_fun (print_top o pop o (chop_level n)));
   1.346 +fun pr () = (enable_pr (); apply_fun print_top);
   1.347 +
   1.348 +(*Set goals_limit and print again*)
   1.349 +fun prlim n = (goals_limit:=n; pr());
   1.350 +
   1.351 +(** the goal.... commands
   1.352 +    Read main goal.  Set global variables curr_prems, curr_mkresult.
   1.353 +    Initial subgoal and premises are rewritten using rths. **)
   1.354 +
   1.355 +(*Version taking the goal as a cterm; if you have a term t and theory thy, use
   1.356 +    goalw_cterm rths (cterm_of thy t);      *)
   1.357 +fun agoalw_cterm atomic rths chorn =
   1.358 +  let val (prems, st0, mkresult) = prepare_proof atomic rths chorn
   1.359 +  in  undo_list := [];
   1.360 +      setstate [ (st0, Seq.empty) ];
   1.361 +      curr_prems := prems;
   1.362 +      curr_mkresult := mkresult;
   1.363 +      prems
   1.364 +  end;
   1.365 +
   1.366 +val goalw_cterm = agoalw_cterm false;
   1.367 +
   1.368 +(*Version taking the goal as a string*)
   1.369 +fun agoalw atomic thy rths agoal =
   1.370 +    agoalw_cterm atomic rths (read_cterm thy (agoal, propT))
   1.371 +    handle ERROR => error (*from type_assign, etc via prepare_proof*)
   1.372 +        ("The error(s) above occurred for " ^ quote agoal);
   1.373 +
   1.374 +val goalw = agoalw false;
   1.375 +fun goal thy = goalw thy [];
   1.376 +
   1.377 +(*now the versions that wrap the goal up in `Goal' to make it atomic*)
   1.378 +fun Goalw thms s = agoalw true (Context.the_context ()) thms s;
   1.379 +val Goal = Goalw [];
   1.380 +
   1.381 +(*simple version with minimal amount of checking and postprocessing*)
   1.382 +fun simple_prove_goal_cterm G f =
   1.383 +  let
   1.384 +    val _ = warn_obsolete ();
   1.385 +    val As = Drule.strip_imp_prems G;
   1.386 +    val B = Drule.strip_imp_concl G;
   1.387 +    val asms = map (Goal.norm_hhf o Thm.assume) As;
   1.388 +    fun check NONE = error "prove_goal: tactic failed"
   1.389 +      | check (SOME (thm, _)) = (case nprems_of thm of
   1.390 +            0 => thm
   1.391 +          | i => !result_error_fn thm (string_of_int i ^ " unsolved goals!"))
   1.392 +  in
   1.393 +    standard (implies_intr_list As
   1.394 +      (check (Seq.pull (EVERY (f asms) (trivial B)))))
   1.395 +  end;
   1.396 +
   1.397 +
   1.398 +(*Proof step "by" the given tactic -- apply tactic to the proof state*)
   1.399 +fun by_com tac ((th,ths), pairs) : gstack =
   1.400 +  (case  Seq.pull(tac th)  of
   1.401 +     NONE      => error"by: tactic failed"
   1.402 +   | SOME(th2,ths2) =>
   1.403 +       (if eq_thm(th,th2)
   1.404 +          then warning "Warning: same as previous level"
   1.405 +          else if eq_thm_thy(th,th2) then ()
   1.406 +          else warning ("Warning: theory of proof state has changed" ^
   1.407 +                       thy_error (Thm.theory_of_thm th, Thm.theory_of_thm th2));
   1.408 +       ((th2,ths2)::(th,ths)::pairs)));
   1.409 +
   1.410 +fun by tac = cond_timeit (!Output.timing)
   1.411 +    (fn() => make_command (by_com tac));
   1.412 +
   1.413 +(* byev[tac1,...,tacn] applies tac1 THEN ... THEN tacn.
   1.414 +   Good for debugging proofs involving prove_goal.*)
   1.415 +val byev = by o EVERY;
   1.416 +
   1.417 +
   1.418 +(*Backtracking means find an alternative result from a tactic.
   1.419 +  If none at this level, try earlier levels*)
   1.420 +fun backtrack [] = error"back: no alternatives"
   1.421 +  | backtrack ((th,thstr) :: pairs) =
   1.422 +     (case Seq.pull thstr of
   1.423 +          NONE      => (writeln"Going back a level..."; backtrack pairs)
   1.424 +        | SOME(th2,thstr2) =>
   1.425 +           (if eq_thm(th,th2)
   1.426 +              then warning "Warning: same as previous choice at this level"
   1.427 +              else if eq_thm_thy(th,th2) then ()
   1.428 +              else warning "Warning: theory of proof state has changed";
   1.429 +            (th2,thstr2)::pairs));
   1.430 +
   1.431 +fun back() = setstate (backtrack (getstate()));
   1.432 +
   1.433 +(*Chop back to previous level of the proof*)
   1.434 +fun choplev n = make_command (chop_level n);
   1.435 +
   1.436 +(*Chopping back the goal stack*)
   1.437 +fun chop () = make_command (fn (_,pairs) => pairs);
   1.438 +
   1.439 +(*Restore the previous proof state;  discard current state. *)
   1.440 +fun undo() = case !undo_list of
   1.441 +      [] => error"No proof state"
   1.442 +    | [_] => error"Already at initial state"
   1.443 +    | _::newundo =>  (undo_list := newundo;  pr()) ;
   1.444 +
   1.445 +
   1.446 +(*** Managing the proof stack ***)
   1.447 +
   1.448 +fun save_proof() = Proof(!undo_list, !curr_prems, !curr_mkresult);
   1.449 +
   1.450 +fun restore_proof(Proof(ul,prems,mk)) =
   1.451 + (undo_list:= ul;  curr_prems:= prems;  curr_mkresult := mk;  prems);
   1.452 +
   1.453 +
   1.454 +fun top_proof() = case !proofstack of
   1.455 +        [] => error("Stack of proof attempts is empty!")
   1.456 +    | p::ps => (p,ps);
   1.457 +
   1.458 +(*  push a copy of the current proof state on to the stack *)
   1.459 +fun push_proof() = (proofstack := (save_proof() :: !proofstack));
   1.460 +
   1.461 +(* discard the top proof state of the stack *)
   1.462 +fun pop_proof() =
   1.463 +  let val (p,ps) = top_proof()
   1.464 +      val prems = restore_proof p
   1.465 +  in proofstack := ps;  pr();  prems end;
   1.466 +
   1.467 +(* rotate the stack so that the top element goes to the bottom *)
   1.468 +fun rotate_proof() = let val (p,ps) = top_proof()
   1.469 +                    in proofstack := ps@[save_proof()];
   1.470 +                       restore_proof p;
   1.471 +                       pr();
   1.472 +                       !curr_prems
   1.473 +                    end;
   1.474 +
   1.475 +
   1.476 +(** Shortcuts for commonly-used tactics **)
   1.477 +
   1.478 +fun bws rls = by (rewrite_goals_tac rls);
   1.479 +fun bw rl = bws [rl];
   1.480 +
   1.481 +fun brs rls i = by (resolve_tac rls i);
   1.482 +fun br rl = brs [rl];
   1.483 +
   1.484 +fun bes rls i = by (eresolve_tac rls i);
   1.485 +fun be rl = bes [rl];
   1.486 +
   1.487 +fun bds rls i = by (dresolve_tac rls i);
   1.488 +fun bd rl = bds [rl];
   1.489 +
   1.490 +fun ba i = by (assume_tac i);
   1.491 +
   1.492 +fun ren str i = by (rename_tac str i);
   1.493 +
   1.494 +(** Shortcuts to work on the first applicable subgoal **)
   1.495 +
   1.496 +fun frs rls = by (FIRSTGOAL (trace_goalno_tac (resolve_tac rls)));
   1.497 +fun fr rl = frs [rl];
   1.498 +
   1.499 +fun fes rls = by (FIRSTGOAL (trace_goalno_tac (eresolve_tac rls)));
   1.500 +fun fe rl = fes [rl];
   1.501 +
   1.502 +fun fds rls = by (FIRSTGOAL (trace_goalno_tac (dresolve_tac rls)));
   1.503 +fun fd rl = fds [rl];
   1.504 +
   1.505 +fun fa() = by (FIRSTGOAL (trace_goalno_tac assume_tac));
   1.506 +
   1.507 +
   1.508 +(** theorem database operations **)
   1.509 +
   1.510 +(* store *)
   1.511 +
   1.512 +fun bind_thm (name, thm) = ThmDatabase.ml_store_thm (name, standard thm);
   1.513 +fun bind_thms (name, thms) = ThmDatabase.ml_store_thms (name, map standard thms);
   1.514 +
   1.515 +fun qed name = ThmDatabase.ml_store_thm (name, result ());
   1.516 +fun qed_goal name thy goal tacsf = ThmDatabase.ml_store_thm (name, prove_goal thy goal tacsf);
   1.517 +fun qed_goalw name thy rews goal tacsf =
   1.518 +  ThmDatabase.ml_store_thm (name, prove_goalw thy rews goal tacsf);
   1.519 +fun qed_spec_mp name =
   1.520 +  ThmDatabase.ml_store_thm (name, ObjectLogic.rulify_no_asm (result ()));
   1.521 +fun qed_goal_spec_mp name thy s p =
   1.522 +  bind_thm (name, ObjectLogic.rulify_no_asm (prove_goal thy s p));
   1.523 +fun qed_goalw_spec_mp name thy defs s p =
   1.524 +  bind_thm (name, ObjectLogic.rulify_no_asm (prove_goalw thy defs s p));
   1.525 +
   1.526 +fun no_qed () = ();
   1.527 +
   1.528 +
   1.529 +(* retrieve *)
   1.530 +
   1.531 +fun thms_containing raw_consts =
   1.532 +  let
   1.533 +    val thy = Thm.theory_of_thm (topthm ());
   1.534 +    val consts = map (Sign.intern_const thy) raw_consts;
   1.535 +  in
   1.536 +    (case List.filter (is_none o Sign.const_type thy) consts of
   1.537 +      [] => ()
   1.538 +    | cs => error ("thms_containing: undeclared consts " ^ commas_quote cs));
   1.539 +    PureThy.thms_containing_consts thy consts
   1.540 +  end;
   1.541 +
   1.542 +end;
   1.543 +
   1.544 +structure Goals: GOALS = OldGoals;
   1.545 +open Goals;